[PR #2979] [Engine] Durable opt-in lifetime token counters in Monitor #4377

Open
opened 2026-11-07 15:35:16 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2979
Author: @Zcg2021
Created: 10/17/2026
Status: 🔄 Open

Base: main ← Head: codex/usage-stats-reliability


📝 Commits (10+)

  • 3664ba0 serve: lifetime token statistics, kept across restarts, with time-window views
  • bdb9a1d docs: the usage_stats key, the usage view on /metrics, and the Usage panel
  • 007819a serve: Usage panel - opens on the 7-day view, and the browser remembers the choice
  • 7d76f60 docs: the Usage panel's default view and the browser memory for the choice
  • dc2a649 serve: the edge cases that came out of testing the Usage panel
  • 65f8120 serve: the Usage range buttons wrap on a phone
  • bfc9f1b fix(monitor): preserve lifetime usage across restarts and show all-time output
  • a62e6bd fix(monitor): keep live metrics first and document lifetime usage validation
  • c7df4b9 fix(usage): release ledger ownership when beta server returns
  • e4ff9b2 docs(usage): record beta persistence and HTTP revalidation

📊 Changes

38 files changed (+7583 additions, -13 deletions)

View changed files

📝 docs/DETAILS.md (+38 -0)
➕ docs/images/usage-stats/desktop.png (+0 -0)
➕ docs/images/usage-stats/usage-bottom.png (+0 -0)
➕ docs/usage-stats-validation.md (+91 -0)
➕ docs/usage-v020/README.md (+78 -0)
➕ docs/usage-v020/check_http.py (+89 -0)
➕ docs/usage-v020/http-mac.txt (+9 -0)
➕ docs/usage-v020/http-windows.txt (+9 -0)
➕ docs/usage-v020/lifecycle-after.txt (+6 -0)
➕ docs/usage-v020/lifecycle-before.txt (+36 -0)
➕ docs/usage-v020/mac-missing-dependency.txt (+2033 -0)
➕ docs/usage-v020/mac-tests.txt (+566 -0)
➕ docs/usage-v020/mac-upstream-control.txt (+39 -0)
➕ docs/usage-v020/monitor-top.png (+0 -0)
➕ docs/usage-v020/usage-bottom.png (+0 -0)
➕ docs/usage-v020/windows-tests.txt (+529 -0)
➕ docs/usage-v029/README.md (+92 -0)
➕ docs/usage-v029/browser-load.txt (+6 -0)
➕ docs/usage-v029/browser-usage.txt (+8 -0)
➕ docs/usage-v029/check_evidence.py (+31 -0)

...and 18 more files

📄 Description

Problem and result

Monitor's session counters reset on restart, hiding how much work a local installation has produced. This opt-in feature retains request/token totals across restarts and shows an all-time output headline in a Usage card at the bottom of Monitor. Speed, hardware and cache remain first. Disabled mode creates no ledger/lock, adds no usage metrics and shows no card.

Builds on @wokhouse's #2370, retaining its six original commits and authorship. Related proposals: #2400 and #2204. Draft pending maintainer agreement on scope/consolidation; this update does not claim that scope was approved. Reliability changes can be separated if that is preferred.

Approach

Save once per settled request: temporary snapshot, flush/fsync, atomic replacement. A cross-process writer lock prevents simultaneous servers overwriting one store (msvcrt on Windows, flock on POSIX). Failed writes remain visible and retry without double counting; corrupt files are preserved. Shutdown and startup-failure paths release ledger ownership. Hourly buckets are bounded to 31 days.

Only counters/timestamps are stored: no prompts, answers, tool arguments or credentials. Undated log imports are labelled, added only to the headline, and excluded from date-window totals and speed averages. Sequential model profiles may share a file; simultaneous servers need separate stores.

Enable with "usage_stats": true or serve/server.py --usage-stats; optionally set usage_stats_path / STRATA_USAGE_STATS. Keep the ledger across upgrades; its parent directory must exist.

Updated against v0.2.9 main c62d5e3a: preserve upstream's asynchronous language/chat startup and load/unload stale-response guard. Manual date refreshes share that guard without spawning an extra polling loop. Documentation follows the current main structure. No native source, model, scheduler or cache-format changes. This is the Python serve/web Monitor, not the separate desktop/TUI interface.

Validation

Server/UI revision 33b9c6241862ea0e32c841a1e94d234ed090b0e9, merging upstream c62d5e3a15b40ea51c96aae9e8d36928965f53ab. Browser regression tests: b37a743d. Evidence/docs: df0f7583. Tested November 2, 2026. Isolated directories and mock engines; no production restart, upgrade or test inference.

Passed:

  • Windows 11, Python 3.13.1: python -m unittest serve.test_server serve.test_usage serve.test_usage_durability serve.test_monitor serve.test_prometheus serve.test_runconfig -q — 412 tests, OK, 3 Unix-signal skips, 152.171 s. Covers abrupt exit, writer ownership, failed persistence/retry, corrupt-file preservation, disconnect/tool accounting and lifecycle cleanup.
  • Mac and Windows: python docs/usage-v020/check_http.py — eight requests each across enabled/disabled statistics and streaming/non-streaming Chat Completions/Responses. Both arms produce 72 mock tokens; persisted/API/session totals, reload and custom range match; normalized reply payloads match. Disabled mode creates no ledger/metrics field.
  • node tools/webtest/usage_test.mjs: 7 browser checks; node tools/webtest/browser_test.mjs 'load/unload:': 5 upstream browser checks. Both use WEBTEST_MODULES=<puppeteer-core installation>, Chromium, Node 24.3.0. Covers custom-date startup, stale response rejection, no duplicate polling, disabled/error states, layout order and load/unload including phone header.
  • All four node serve/test_web_*.js files, node --check serve/web/app.js, git diff origin/main --check.
  • python docs/usage-v029/check_evidence.py: published aggregate sums and two temporary-store load/save/reload cycles preserve exact values.

Failed:

  • macOS, Python 3.13.0 full suite: python -m unittest discover -s serve -p 'test_*.py' -q — 1,326 tests, 1 failure, 21 skips, 427.109 s. test_gpu_roles_server.Roles.test_every_card_off expects SystemExit, while upstream explicitly allows this case on Darwin. The exact failure reproduces on an unmodified c62d5e3a archive. That function is unchanged here; the full Mac run is not reported green.
  • Initial detached Windows launcher exited with its SSH session before running tests. Retried with SSH kept alive; both checks then returned 0.

Not run:

Native CUDA/HIP/SYCL/Metal builds or inference (native code unchanged), physical power loss, a new persistence latency benchmark, or separate desktop/TUI statistics UI. Historical beta/native results are dated separately and not substituted for this validation.

Full v0.2.9 report, commands, sanitized logs and evidence.

Field evidence and limits

A read-only snapshot from our existing deployment, taken November 2 at 11:06:10 UTC, records 22,020,156 output tokens across 23,837 requests:

Source Output tokens Requests
Detailed ledger 15,624,110 15,307
Undated retained-log import 6,396,046 8,530

Allowlisted aggregate JSON. Reused prompt tokens are counted separately and are not generated output. These are historical implementation data, not v0.2.9 production burn-in or a performance/quality benchmark. They may aggregate sequential models, development work and tests. No per-request data or date buckets are published; the snapshot alone cannot prove zero lost counts or zero overhead.

The earlier screenshot below shows 20,097,558, before that snapshot, and illustrates the headline/import/range distinction. Metadata was removed with decoded pixels verified identical. Requests killed before settlement remain excluded; undated imports must not duplicate already-counted work.

Earlier production Usage card: labelled imports and separate detailed totals


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/2979 **Author:** [@Zcg2021](https://github.com/Zcg2021) **Created:** 10/17/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `codex/usage-stats-reliability` --- ### 📝 Commits (10+) - [`3664ba0`](https://github.com/Niko1221/Strata/commit/3664ba006a68e9f17868719d97e2b8767ec48982) serve: lifetime token statistics, kept across restarts, with time-window views - [`bdb9a1d`](https://github.com/Niko1221/Strata/commit/bdb9a1d39b6f9ecf97eb5f09e44af8e6d214208b) docs: the usage_stats key, the usage view on /metrics, and the Usage panel - [`007819a`](https://github.com/Niko1221/Strata/commit/007819ae38a88f822defe87b707ab8f985100549) serve: Usage panel - opens on the 7-day view, and the browser remembers the choice - [`7d76f60`](https://github.com/Niko1221/Strata/commit/7d76f60cfc7dd1dcad126cd9f1fc38721c5c53a6) docs: the Usage panel's default view and the browser memory for the choice - [`dc2a649`](https://github.com/Niko1221/Strata/commit/dc2a6494fa1cf4e478202b0af3266074b2c3c658) serve: the edge cases that came out of testing the Usage panel - [`65f8120`](https://github.com/Niko1221/Strata/commit/65f812052efc805b4d314f87a9d450424f5476b5) serve: the Usage range buttons wrap on a phone - [`bfc9f1b`](https://github.com/Niko1221/Strata/commit/bfc9f1b984cf32c5be833ba21000c539f33b96d0) fix(monitor): preserve lifetime usage across restarts and show all-time output - [`a62e6bd`](https://github.com/Niko1221/Strata/commit/a62e6bd9abec44c391f593feab6071451259b519) fix(monitor): keep live metrics first and document lifetime usage validation - [`c7df4b9`](https://github.com/Niko1221/Strata/commit/c7df4b95b8a30ce3bde61c3972c1ff0cc545a95d) fix(usage): release ledger ownership when beta server returns - [`e4ff9b2`](https://github.com/Niko1221/Strata/commit/e4ff9b2bc810331473971d2e954b695ff6afe372) docs(usage): record beta persistence and HTTP revalidation ### 📊 Changes **38 files changed** (+7583 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+38 -0) ➕ `docs/images/usage-stats/desktop.png` (+0 -0) ➕ `docs/images/usage-stats/usage-bottom.png` (+0 -0) ➕ `docs/usage-stats-validation.md` (+91 -0) ➕ `docs/usage-v020/README.md` (+78 -0) ➕ `docs/usage-v020/check_http.py` (+89 -0) ➕ `docs/usage-v020/http-mac.txt` (+9 -0) ➕ `docs/usage-v020/http-windows.txt` (+9 -0) ➕ `docs/usage-v020/lifecycle-after.txt` (+6 -0) ➕ `docs/usage-v020/lifecycle-before.txt` (+36 -0) ➕ `docs/usage-v020/mac-missing-dependency.txt` (+2033 -0) ➕ `docs/usage-v020/mac-tests.txt` (+566 -0) ➕ `docs/usage-v020/mac-upstream-control.txt` (+39 -0) ➕ `docs/usage-v020/monitor-top.png` (+0 -0) ➕ `docs/usage-v020/usage-bottom.png` (+0 -0) ➕ `docs/usage-v020/windows-tests.txt` (+529 -0) ➕ `docs/usage-v029/README.md` (+92 -0) ➕ `docs/usage-v029/browser-load.txt` (+6 -0) ➕ `docs/usage-v029/browser-usage.txt` (+8 -0) ➕ `docs/usage-v029/check_evidence.py` (+31 -0) _...and 18 more files_ </details> ### 📄 Description ## Problem and result Monitor's session counters reset on restart, hiding how much work a local installation has produced. This opt-in feature retains request/token totals across restarts and shows an all-time output headline in a Usage card **at the bottom of Monitor**. Speed, hardware and cache remain first. Disabled mode creates no ledger/lock, adds no usage metrics and shows no card. ## Related discussion Builds on @wokhouse's #2370, retaining its six original commits and authorship. Related proposals: #2400 and #2204. **Draft pending maintainer agreement on scope/consolidation**; this update does not claim that scope was approved. Reliability changes can be separated if that is preferred. ## Approach Save once per settled request: temporary snapshot, flush/fsync, atomic replacement. A cross-process writer lock prevents simultaneous servers overwriting one store (`msvcrt` on Windows, `flock` on POSIX). Failed writes remain visible and retry without double counting; corrupt files are preserved. Shutdown and startup-failure paths release ledger ownership. Hourly buckets are bounded to 31 days. Only counters/timestamps are stored: no prompts, answers, tool arguments or credentials. Undated log imports are labelled, added only to the headline, and excluded from date-window totals and speed averages. Sequential model profiles may share a file; simultaneous servers need separate stores. Enable with `"usage_stats": true` or `serve/server.py --usage-stats`; optionally set `usage_stats_path` / `STRATA_USAGE_STATS`. Keep the ledger across upgrades; its parent directory must exist. Updated against **v0.2.9 main `c62d5e3a`**: preserve upstream's asynchronous language/chat startup and load/unload stale-response guard. Manual date refreshes share that guard without spawning an extra polling loop. Documentation follows the current main structure. No native source, model, scheduler or cache-format changes. This is the Python `serve/web` Monitor, not the separate desktop/TUI interface. ## Validation Server/UI revision **`33b9c6241862ea0e32c841a1e94d234ed090b0e9`**, merging upstream **`c62d5e3a15b40ea51c96aae9e8d36928965f53ab`**. Browser regression tests: `b37a743d`. Evidence/docs: `df0f7583`. Tested November 2, 2026. Isolated directories and mock engines; no production restart, upgrade or test inference. **Passed:** - Windows 11, Python 3.13.1: `python -m unittest serve.test_server serve.test_usage serve.test_usage_durability serve.test_monitor serve.test_prometheus serve.test_runconfig -q` — **412 tests, OK, 3 Unix-signal skips**, 152.171 s. Covers abrupt exit, writer ownership, failed persistence/retry, corrupt-file preservation, disconnect/tool accounting and lifecycle cleanup. - Mac and Windows: `python docs/usage-v020/check_http.py` — eight requests each across enabled/disabled statistics and streaming/non-streaming Chat Completions/Responses. Both arms produce 72 mock tokens; persisted/API/session totals, reload and custom range match; normalized reply payloads match. Disabled mode creates no ledger/metrics field. - `node tools/webtest/usage_test.mjs`: **7 browser checks**; `node tools/webtest/browser_test.mjs 'load/unload:'`: **5 upstream browser checks**. Both use `WEBTEST_MODULES=<puppeteer-core installation>`, Chromium, Node 24.3.0. Covers custom-date startup, stale response rejection, no duplicate polling, disabled/error states, layout order and load/unload including phone header. - All four `node serve/test_web_*.js` files, `node --check serve/web/app.js`, `git diff origin/main --check`. - `python docs/usage-v029/check_evidence.py`: published aggregate sums and two temporary-store load/save/reload cycles preserve exact values. **Failed:** - macOS, Python 3.13.0 full suite: `python -m unittest discover -s serve -p 'test_*.py' -q` — **1,326 tests, 1 failure, 21 skips**, 427.109 s. `test_gpu_roles_server.Roles.test_every_card_off` expects `SystemExit`, while upstream explicitly allows this case on Darwin. The exact failure reproduces on an unmodified `c62d5e3a` archive. That function is unchanged here; the full Mac run is **not reported green**. - Initial detached Windows launcher exited with its SSH session before running tests. Retried with SSH kept alive; both checks then returned 0. **Not run:** Native CUDA/HIP/SYCL/Metal builds or inference (native code unchanged), physical power loss, a new persistence latency benchmark, or separate desktop/TUI statistics UI. Historical beta/native results are dated separately and not substituted for this validation. [Full v0.2.9 report, commands, sanitized logs and evidence](https://github.com/Zcg2021/Strata/blob/df0f7583b346105b9da61a5595344ba2bed1441f/docs/usage-v029/README.md). ## Field evidence and limits A read-only snapshot from our **existing deployment**, taken November 2 at 11:06:10 UTC, records **22,020,156 output tokens across 23,837 requests**: | Source | Output tokens | Requests | | --- | ---: | ---: | | Detailed ledger | 15,624,110 | 15,307 | | Undated retained-log import | 6,396,046 | 8,530 | [Allowlisted aggregate JSON](https://github.com/Zcg2021/Strata/blob/df0f7583b346105b9da61a5595344ba2bed1441f/docs/usage-v029/production-counters.json). Reused prompt tokens are counted separately and are not generated output. These are historical implementation data, **not v0.2.9 production burn-in or a performance/quality benchmark**. They may aggregate sequential models, development work and tests. No per-request data or date buckets are published; the snapshot alone cannot prove zero lost counts or zero overhead. The earlier screenshot below shows **20,097,558**, before that snapshot, and illustrates the headline/import/range distinction. Metadata was removed with decoded pixels verified identical. Requests killed before settlement remain excluded; undated imports must not duplicate already-counted work. ![Earlier production Usage card: labelled imports and separate detailed totals](https://raw.githubusercontent.com/Zcg2021/Strata/df0f7583b346105b9da61a5595344ba2bed1441f/docs/usage-v029/production-history.png) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4377
No description provided.