Import pr 100 into fork#120
Closed
gkachru wants to merge 3 commits into
Closed
Conversation
…1.80 rate_limits Support the rate_limits field added in Claude Code 2.1.80, which provides 5-hour and 7-day rate limit usage (used_percentage and resets_at) directly via statusline scripts, eliminating the need for separate API calls. - Parse rate_limits from Claude Code input (unix timestamp resets_at) - Split single Usage segment into Usage5h and Usage7d for independent display of 5-hour and 7-day windows with their own reset times - Auto-migrate old configs: insert usage_5h + usage_7d, disable legacy usage - Mark legacy Usage as "(deprecated)" in GUI - Fix resets_at type mismatch (i64 vs String) that caused statusline to disappear after first invocation - Update all 9 theme presets with new segment definitions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…tion Each theme file now has a single usage_segment_base(id) function that holds all configuration, with usage_segment/usage_5h_segment/usage_7d_segment as one-liner wrappers. Eliminates ~535 lines of duplicated config across 9 themes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Reviewer's GuideRefactors the usage/rate-limit segment to support separate 5-hour and 7-day views backed by a shared rate‑limit data source, adds new segment IDs and config migration, and updates all themes, presets, and UI to expose the new segments while keeping the legacy combined usage segment deprecated but functional. Sequence diagram for shared rate limit data fetching for usage segmentssequenceDiagram
participant Statusline
participant Usage5hSegment
participant RateLimitCache
participant RateLimitFetcher
participant InputData
participant FileCache
participant AnthropicAPI
Statusline->>Usage5hSegment: collect(input)
Usage5hSegment->>RateLimitCache: fetch_rate_limit_data(input)
alt cache already initialized
RateLimitCache-->>Usage5hSegment: Some(RateLimitData)
else cache not initialized
RateLimitCache->>RateLimitFetcher: fetch_rate_limit_data_inner(input)
alt InputData has rate_limits
RateLimitFetcher->>InputData: read rate_limits
InputData-->>RateLimitFetcher: RateLimits
RateLimitFetcher-->>RateLimitCache: Some(RateLimitData)
else no rate_limits in InputData
RateLimitFetcher->>FileCache: load_cache()
alt cache file exists and fresh
FileCache-->>RateLimitFetcher: ApiUsageCache
RateLimitFetcher-->>RateLimitCache: Some(RateLimitData)
else cache missing or stale
RateLimitFetcher->>AnthropicAPI: fetch_api_usage()
alt HTTP success
AnthropicAPI-->>RateLimitFetcher: ApiUsageResponse
RateLimitFetcher->>FileCache: save_cache(ApiUsageCache)
FileCache-->>RateLimitFetcher: ok
RateLimitFetcher-->>RateLimitCache: Some(RateLimitData)
else HTTP failure
FileCache-->>RateLimitFetcher: Option<ApiUsageCache>
RateLimitFetcher-->>RateLimitCache: Option<RateLimitData> from cache
end
end
end
RateLimitCache-->>Usage5hSegment: Option<RateLimitData>
end
Usage5hSegment-->>Statusline: SegmentData (primary, secondary, metadata)
%% Usage7dSegment and legacy UsageSegment call the same fetch_rate_limit_data and reuse RateLimitCache in the same way
Updated class diagram for usage segments and rate limit modelsclassDiagram
class Segment {
<<trait>>
+collect(input: InputData) SegmentData?
+id() SegmentId
}
class UsageSegment {
+new() UsageSegment
+collect(input: InputData) SegmentData?
+id() SegmentId
}
class Usage5hSegment {
+new() Usage5hSegment
+collect(input: InputData) SegmentData?
+id() SegmentId
}
class Usage7dSegment {
+new() Usage7dSegment
+collect(input: InputData) SegmentData?
+id() SegmentId
}
UsageSegment ..|> Segment
Usage5hSegment ..|> Segment
Usage7dSegment ..|> Segment
class RateLimitData {
+f64 five_hour_util
+Option~String~ five_hour_resets_at
+f64 seven_day_util
+Option~String~ seven_day_resets_at
}
class ApiUsageResponse {
+UsagePeriod five_hour
+UsagePeriod seven_day
}
class UsagePeriod {
+f64 utilization
+Option~String~ resets_at
}
class ApiUsageCache {
+f64 five_hour_utilization
+Option~String~ five_hour_resets_at
+f64 seven_day_utilization
+Option~String~ resets_at
+String cached_at
}
class RateLimitPeriod {
+f64 used_percentage
+Option~i64~ resets_at
}
class RateLimits {
+Option~RateLimitPeriod~ five_hour
+Option~RateLimitPeriod~ seven_day
}
class InputData {
+Model model
+String cwd
+String transcript_path
+Option~Cost~ cost
+Option~OutputStyle~ output_style
+Option~RateLimits~ rate_limits
}
class SegmentData {
+String primary
+String secondary
+HashMap~String,String~ metadata
}
class SegmentId {
<<enumeration>>
Usage
Usage5h
Usage7d
Directory
Git
ContextWindow
Cost
Session
OutputStyle
Update
}
class Config {
+Vec~SegmentConfig~ segments
+migrate() void
}
class SegmentConfig {
+SegmentId id
+bool enabled
+IconConfig icon
+ColorConfig colors
+TextStyleConfig styles
+HashMap~String, Value~ options
}
class ThemePresetModule {
<<module>>
+usage_segment() SegmentConfig
+usage_5h_segment() SegmentConfig
+usage_7d_segment() SegmentConfig
+usage_segment_base(id: SegmentId) SegmentConfig
}
Config --> SegmentConfig
SegmentConfig --> SegmentId
InputData --> RateLimits
RateLimits --> RateLimitPeriod
ApiUsageResponse --> UsagePeriod
ApiUsageCache --> UsagePeriod
Usage5hSegment --> RateLimitData
Usage7dSegment --> RateLimitData
UsageSegment --> RateLimitData
Usage5hSegment --> SegmentData
Usage7dSegment --> SegmentData
UsageSegment --> SegmentData
ThemePresetModule --> SegmentConfig
SegmentId <|.. UsageSegment
SegmentId <|.. Usage5hSegment
SegmentId <|.. Usage7dSegment
Flow diagram for config loading, migration, and exposure of new usage segmentsflowchart LR
A[Config_loader::load_user_config] --> B[Read config.toml]
B --> C[Deserialize into Config]
C --> D[Config.migrate]
D -->|has Usage but no Usage5h or Usage7d| E[Find existing Usage SegmentConfig]
E --> F[Clone icon, colors, styles, options]
F --> G[Insert Usage5h SegmentConfig after Usage]
G --> H[Insert Usage7d SegmentConfig after Usage5h]
H --> I[Disable original Usage segment]
D -->|already migrated or no Usage| J[No changes]
I --> K[Final Config.segments]
J --> K
K --> L[ThemePresets::theme_default]
K --> M[ThemePresets::theme_cometix]
K --> N[Other theme presets]
L --> O[theme_default::usage_5h_segment]
L --> P[theme_default::usage_7d_segment]
M --> Q[theme_cometix::usage_5h_segment]
M --> R[theme_cometix::usage_7d_segment]
O --> S[UI SegmentListComponent]
P --> S
Q --> S
R --> S
S --> T[User sees Usage 5h and Usage 7d entries]
S --> U[Legacy Usage shown as Usage - deprecated]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
RATE_LIMIT_CACHEstores anOption<RateLimitData>and cachesNoneas well, which means a transient failure or missing token on the first call will disable rate limit data for the rest of the process; consider only caching successfulSomeresults so later calls can retry.- In
RateLimitPeriod, theresets_at: Option<i64>is treated as a Unix timestamp in seconds (viaUtc.timestamp_opt(ts, 0)); consider making the unit explicit in the type/field name or adding a small conversion helper to avoid future confusion if the source ever returns milliseconds.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- `RATE_LIMIT_CACHE` stores an `Option<RateLimitData>` and caches `None` as well, which means a transient failure or missing token on the first call will disable rate limit data for the rest of the process; consider only caching successful `Some` results so later calls can retry.
- In `RateLimitPeriod`, the `resets_at: Option<i64>` is treated as a Unix timestamp in seconds (via `Utc.timestamp_opt(ts, 0)`); consider making the unit explicit in the type/field name or adding a small conversion helper to avoid future confusion if the source ever returns milliseconds.
## Individual Comments
### Comment 1
<location path="src/core/segments/usage.rs" line_range="44-45" />
<code_context>
- }
+// Process-lifetime cache: computed once per statusline render, shared across
+// all Usage segments (Usage, Usage5h, Usage7d) to avoid redundant I/O.
+static RATE_LIMIT_CACHE: std::sync::OnceLock<Option<RateLimitData>> =
+ std::sync::OnceLock::new();
+
+// ── Shared helpers ────────────────────────────────────────────────────────────
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid permanently caching a `None` rate limit result in the `OnceLock` to keep retrying after transient failures.
If the first `fetch_rate_limit_data_inner` call returns `None` (e.g. transient API failure), `RATE_LIMIT_CACHE` will be set to `None` and every subsequent call will short-circuit to `None`, effectively disabling usage segments for the rest of the process.
Consider only caching successful results (e.g. `OnceLock<RateLimitData>` and calling `get_or_init` only on success), or keep `OnceLock<Option<_>>` but avoid initializing it when the result is `None` so later calls can retry.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
…amp unit Cache only successful RateLimitData in the process-lifetime OnceLock so a None result (transient API failure or missing token) no longer disables usage segments for the rest of the process. Rename RateLimitPeriod.resets_at to resets_at_unix_secs (with serde rename to preserve the wire format) and extract a unix_secs_to_rfc3339 helper to make the unit assumption explicit at every call site.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Import #100
Summary by Sourcery
Split API usage display into separate 5-hour and 7-day segments while sharing rate limit data and caching across them, and wire the new segments through config, themes, and UI.
New Features:
Bug Fixes:
Enhancements:
Tests: