Skip to content

fix(usage): show session reset time and add configurable reset display#87

Open
jhjaggars wants to merge 1 commit intoHaleclipse:masterfrom
jhjaggars:fix/session-reset-time
Open

fix(usage): show session reset time and add configurable reset display#87
jhjaggars wants to merge 1 commit intoHaleclipse:masterfrom
jhjaggars:fix/session-reset-time

Conversation

@jhjaggars
Copy link

@jhjaggars jhjaggars commented Feb 22, 2026

Summary

  • The usage segment was displaying the 5-hour (session) utilization percentage paired with the 7-day (weekly) reset time — a confusing mismatch. Now the reset time shown corresponds to the period being displayed.
  • Adds a reset_period option to explicitly control which reset time is shown ("session" or "weekly", default: "session").
  • Adds a reset_format option to display the reset as a countdown duration instead of a clock time (e.g. 4h 52m instead of 2-22-13).
  • Documents all Usage segment options in README.md and README.zh.md.

New options

Option Type Default Description
reset_period string "session" "session" (5-hour window) or "weekly" (7-day window)
reset_format string "time" "time" (e.g. 2-22-13) or "duration" (e.g. 4h 52m)

Example:

[[segments]]
id = "usage"
enabled = true

[segments.options]
reset_period = "session"
reset_format = "duration"

Test plan

  • Verify usage segment shows session reset time by default (not weekly)
  • Verify reset_period = "weekly" shows the 7-day reset time
  • Verify reset_format = "duration" shows countdown (e.g. 4h 52m)
  • Verify reset_format = "time" shows existing clock format
  • Verify old cache files (with resets_at field) trigger a fresh API fetch gracefully
  • Verify both READMEs render correctly

🤖 Generated with Claude Code

Summary by Sourcery

Align usage segment reset time with the displayed period and introduce configurable reset display options.

New Features:

  • Add configurable reset_period option to choose between session (5-hour) and weekly (7-day) reset times in the usage segment.
  • Add reset_format option to display reset time either as an absolute time or as a countdown duration.

Bug Fixes:

  • Ensure the usage segment shows a reset time that matches the utilization period being displayed instead of always using the weekly reset.

Enhancements:

  • Extend cached API usage data to store separate reset timestamps for session and weekly periods and handle legacy cache entries gracefully.

Documentation:

  • Document all usage segment configuration options, including reset_period and reset_format, in both English and Chinese READMEs.

Previously, the usage segment displayed the 5-hour (session) utilization
percentage but paired it with the 7-day (weekly) reset time, causing a
confusing mismatch. This also adds a duration display option.

Changes:
- Store both `five_hour_resets_at` and `seven_day_resets_at` in cache
  (replacing the single `resets_at` field which always used the weekly period)
- Add `reset_period` option (default: "session") to control which reset
  time is shown; set to "weekly" to use the 7-day reset time instead
- Add `reset_format` option (default: "time") to control display format:
  - "time": existing behavior, e.g. "2-22-13"
  - "duration": time remaining until reset, e.g. "4h 52m", "2d 3h", "45m"
- Document all Usage segment options in README.md and README.zh.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 22, 2026

Reviewer's Guide

Refactors the usage segment to cache and select separate reset timestamps for the 5‑hour session and 7‑day weekly windows, adds configurable options to control which reset period is displayed and in what format (time vs duration), and documents the new options in both English and Chinese READMEs.

Class diagram for updated usage segment cache and reset formatting

classDiagram
    class UsageSegment {
        +format_reset_time(reset_time_str)
        +format_reset_duration(reset_time_str)
        +get_cache_path()
        +is_cache_valid(cache, cache_duration)
        +fetch_api_usage(api_base_url, token, timeout)
        +save_cache(cache)
        +get_circle_icon(ratio)
    }

    class ApiUsageCache {
        +five_hour_utilization: f64
        +seven_day_utilization: f64
        +five_hour_resets_at: Option~String~
        +seven_day_resets_at: Option~String~
        +cached_at: String
    }

    class UsageSegmentConfig {
        +reset_period: String  *implicit via options["reset_period"]*
        +reset_format: String *implicit via options["reset_format"]*
    }

    UsageSegment --> ApiUsageCache : uses_for_caching
    UsageSegment --> UsageSegmentConfig : reads_options_from
Loading

File-Level Changes

Change Details Files
Track separate reset times for session and weekly usage and use them consistently in display and caching.
  • Replace single cached reset timestamp with separate five_hour_resets_at and seven_day_resets_at fields in the ApiUsageCache struct and all related code paths.
  • Update API response handling to store both session and weekly resets_at values in the cache and to return them when using cached data.
  • Add logic to choose the reset timestamp based on the configured reset period before formatting for display.
src/core/segments/usage.rs
Add configurable reset period and reset format options to the usage segment, including a duration-style formatter.
  • Read reset_period (session vs weekly) and reset_format (time vs duration) from the segment options with sensible defaults.
  • Introduce format_reset_duration helper that parses the RFC3339 reset time, computes remaining time, and formats it as Xd Yh, Xh Ym, Xm, or now.
  • Wire the chosen reset period and format into the secondary text shown by the usage segment, falling back to the existing time formatter when reset_format is time.
  • Ensure unknown or missing reset timestamps gracefully fall back to ?.
src/core/segments/usage.rs
Document all usage segment options, including the new reset options, in both README files.
  • Add a "Usage Segment Options" section to README.md describing all supported options, types, defaults, and an example TOML configuration including reset_period and reset_format.
  • Add the equivalent "使用量段落选项" section to README.zh.md with localized descriptions and example configuration.
README.md
README.zh.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Consider modeling reset_period and reset_format as enums (with Deserialize and FromStr/TryFrom implementations) rather than raw strings so that invalid config values are caught explicitly instead of silently falling back to the default branch.
  • Since ApiUsageCache renamed resets_at to five_hour_resets_at and added seven_day_resets_at, it may be safer to add serde attributes (rename, default, etc.) to keep deserialization backward-compatible with existing cache files rather than relying on them to fail and be replaced.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider modeling `reset_period` and `reset_format` as enums (with `Deserialize` and `FromStr`/`TryFrom` implementations) rather than raw strings so that invalid config values are caught explicitly instead of silently falling back to the default branch.
- Since `ApiUsageCache` renamed `resets_at` to `five_hour_resets_at` and added `seven_day_resets_at`, it may be safer to add `serde` attributes (`rename`, `default`, etc.) to keep deserialization backward-compatible with existing cache files rather than relying on them to fail and be replaced.

## Individual Comments

### Comment 1
<location> `src/core/segments/usage.rs:76` </location>
<code_context>
+                    return "now".to_string();
+                }
+
+                let total_minutes = remaining.num_minutes();
+                let days = total_minutes / (24 * 60);
+                let hours = (total_minutes % (24 * 60)) / 60;
</code_context>

<issue_to_address>
**suggestion:** Duration formatting can show `0m` when there is still almost a full minute remaining.

Because `remaining.num_minutes()` truncates fractional minutes, any remaining time under 60 seconds will display as `0m` rather than something like `"now"`. If you want very short durations to show `"now"`, consider a small threshold (e.g. treat durations < 60 seconds as `"now"`) or base this decision on `num_seconds()` instead of minutes.

```suggestion
                if remaining.num_seconds() < 60 {
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

let reset_utc = dt.with_timezone(&Utc);
let remaining = reset_utc.signed_duration_since(now);

if remaining.num_seconds() <= 0 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Duration formatting can show 0m when there is still almost a full minute remaining.

Because remaining.num_minutes() truncates fractional minutes, any remaining time under 60 seconds will display as 0m rather than something like "now". If you want very short durations to show "now", consider a small threshold (e.g. treat durations < 60 seconds as "now") or base this decision on num_seconds() instead of minutes.

Suggested change
if remaining.num_seconds() <= 0 {
if remaining.num_seconds() < 60 {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant