Skip to content

Stats: a Reset totals button, and POST /v1/stats/reset behind it - #640

Merged
FMXExpress merged 1 commit into
mainfrom
claude/stats-reset-button
Nov 13, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/stats-reset-button

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Builds on #639 (the stats fix). Until that merges, this PR's diff also shows its commit; the change here is the second commit, d3ba834.

Why

The saved totals only ever grow, so wrong ones stay wrong. Builds before #639 added leftover memory to them, and the Stats tab keeps showing billions after the fix. The only way back was deleting files by hand.

What it does

Reset totals on the Stats tab asks first, then sets every saved counter to zero. Conversations are kept.

One web UI conversation, with the counts the provider reported:

Turns Tool calls Input tokens Output tokens
Two turns, one using a tool 2 1 18,344 54
After Reset totals 0 0 0 0
After one more turn 1 0 6,178 17

The conversation was unchanged on screen, on disk and after a page reload.

Changes

  • POST /v1/stats/reset
    • The gateway's endpoint buckets hold counters only and are deleted.
    • A conversation has its stats block emptied and the rest of its file left alone.
    • /v1/stats answers with the new totals at once: its five-second cache is dropped.
    • The route is behind the gateway token like the rest of /v1/*, and a token-less gateway refuses it from another site's page.
  • A conversation with a turn running on the gateway is skipped, not waited for. The reply says how many were, and the tab passes that on.
  • ResetSessionStats (session store) rewrites one session without its counters.
    • Transcript, tool details, title, updated_at and transcript log stay.
    • A session with no recorded location is not stamped with the caller's workspace, as Save would stamp it.
    • Nothing is written when there is nothing to clear.
    • There is no lock across processes, so the new record replaces the file only if the file still holds what was read; otherwise it starts over. That narrows the window in which another process's save could be overwritten; it cannot close it.
  • TSession.Save is split into the record builder and the write so the reset can build the same record. The files it writes are unchanged: the same turns through the previous build and this one give identical session files.

Limits

  • A session open in another process (a TUI, say) writes the counters it holds in memory back with its next save.
  • prompt_cache_warm_pings counts since the gateway started and is not reset.
  • The reset cannot be undone.

Tests

  • make test-session-stats: the store routine, including a save that lands mid-reset and a file that keeps changing.
  • make test-stats-reset-e2e: the real gateway against a fake provider. Totals go to zero at once, conversations are as they were, a session in use is skipped and cleared by a second reset, and a GET and a cross-site POST reset nothing.
  • gateway_token_tests pins the route as token-gated.
  • Each of the eight behaviours, reverted alone, makes one of these fail.
  • The button itself was checked in a browser (confirm and cancel, the three notes, both themes, phone width, contrast), not by a committed test.
  • Full make test passes on FPC 3.2.2. Not built with Delphi.

Generated by Claude Code

The saved totals only ever grow, so wrong ones stay wrong: builds before
the provider usage fix added leftover memory to them, and the Stats tab
kept showing billions after the fix. There was no way back short of
deleting files by hand.

Reset totals on the Stats tab asks first, then sets every saved counter
to zero. Conversations are kept.

- POST /v1/stats/reset: the gateway's endpoint buckets hold counters only
  and are deleted; a conversation has its stats block emptied and the
  rest of its file left alone. /v1/stats answers with the new totals at
  once (its five-second cache is dropped). Behind the gateway token like
  the rest of /v1, and refused from another site's page on a token-less
  gateway.
- A conversation with a turn running on the gateway is skipped, not
  waited for, and the reply says how many were; the tab passes that on.
- ResetSessionStats (session store): rewrites one session without its
  counters. Transcript, tool details, title, updated_at and transcript
  log stay; a session with no recorded location is not stamped with the
  caller's workspace, as Save would stamp it; nothing is written when
  there is nothing to clear. There is no lock across processes, so the
  new record replaces the file only if the file still holds what was
  read, and it starts over when it does not.
- TSession.Save is split into the record builder and the write so the
  reset can build the same record; the files it writes are unchanged.

Tests: session_stats_tests.pas (the store routine, a save landing
mid-reset included) and stats_reset_e2e.py (the real gateway: totals to
zero at once, conversations as they were, a session in use skipped and
cleared by a second reset, a GET and a cross-site POST refused).
gateway_token_tests pins the route as token-gated. Each behaviour,
reverted alone, fails one of them. The button itself was checked in a
browser, not by a committed test.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T19:01:23.951029Z d3ba834 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@FMXExpress
FMXExpress merged commit a4ae348 into main Nov 13, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d3ba834943

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +4359 to +4362
GStatsCacheLock.Enter;
try
GStatsCacheUntil := 0;
GStatsCacheBody := '';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Prevent in-flight stats reads from restoring the old cache

If a concurrent GET /v1/stats misses the cache and reads the sessions before the reset finishes, it can publish its pre-reset totals after this invalidation. HandleStats performs ListSessions(True) outside GStatsCacheLock and later unconditionally assigns GStatsCacheBody, so clearing the cache here does not prevent that stale result from being cached again. The reset button's immediate refresh can consequently show the old totals despite a successful reset, and subsequent reads can receive them for another five seconds. Use a cache generation checked before publishing, or coordinate aggregation with reset, so an in-flight pre-reset aggregation cannot repopulate the invalidated cache.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Valid. Fixed in 3d8b58f on claude/pasclaw-desktop-plan-uapzcp (this PR had already merged): the cache now has a generation that goes up when a reset drops it, and an answer is stored only if the cache has not been dropped since the request started. The Stats tab also draws only its newest request's answer, so a poll sent before the reset cannot put the old totals back on screen.


Generated by Claude Code

FMXExpress pushed a commit that referenced this pull request Nov 13, 2026
…s back

Review finding on #640. POST /v1/stats/reset cleared the cached
/v1/stats answer, but a /v1/stats request already reading the sessions
at that moment stored its pre-reset answer afterwards, and the old
totals were served for another five seconds.

- The cache has a generation that goes up when it is dropped. An answer
  is stored only if the cache has not been dropped since the request
  started (StatsCacheGet / StatsCachePut / StatsCacheDrop).
- The Stats tab draws only the newest request's answer. A poll sent just
  before the reset could answer after the reset's own refresh and put
  the old totals back on screen until the next poll.

Tests: gateway_stats_buckets_tests pins the cache (an answer from before
a drop is refused, one from after is kept); removing the generation
check fails it. The page's ordering was checked in a browser with a held
request: with the check the totals stay at zero, without it the old
ones come back. No committed test for the page.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
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.

2 participants