[PR #4745] [Engine] serve: keep the Monitor's token counts between starts #5157

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4745
Author: @au70ma70n
Created: 10/30/2026
Status: 🔄 Open

Base: main ← Head: engine-totals-persistence


📝 Commits (2)

  • 145e349 [GUI] settings: let a long help line wrap instead of squeezing every field
  • 419a478 [Engine] serve: keep the Monitor's token counts between starts

📊 Changes

4 files changed (+212 additions, -3 deletions)

View changed files

📝 docs/DETAILS.md (+5 -1)
📝 serve/server.py (+89 -0)
➕ serve/test_totals.py (+113 -0)
📝 serve/web/app.css (+5 -2)

📄 Description

Draft — the costing story wants scope agreement first: #4742. It lands after #4743 (the layout fix, already open and standing on its own); merge order is #4743 → this one → #4746 → #4747, and until the branch before it merges this pull request's diff also shows those commits and shrinks on its own as they merge.

Branch engine-totals-persistence, base main.

Problem and result

The counts the Monitor tab prints ("Since …: N requests · … input tokens read · … written") lived only in the process, so a restart lost them, and there was no way to close the window and read one session's totals on their own. Now they are kept in <the run config's name>.totals.json beside the run config, the way the shared Chat settings are kept: the file is read when the server starts and written when a request ends, so a restart or a crash loses no more than a request that never ended. The prompt and decode times travel with the counts, so the printed speeds stay averages of the whole window rather than of the last start. --totals-file none (or off) keeps the old one-start counting, and --totals-file PATH names the file. --totals-reset closes the window: this start counts from zero and the window that ended is moved aside to <the same name>.prev.json instead of being thrown away, so the session that just finished stays readable.

#4742 — the scope thread for this story (this pull request and the two that price these counts).

#4631 — "[Engine] Monitor: Clear hides only the request rows; the totals can only be reset by restarting the server" (open, no labels, no linked pull request). It wants the totals zeroed without a restart and proposes POST /metrics/reset behind the API key, calling the Clear button. This answers the same problem from the other end — the window survives restarts, and a start with --totals-reset closes it — but it does not add the button, so it does not close #4631. That issue also asks that /metrics/prometheus stay monotonic; worth settling in the same thread, because these counters come from the same totals: after this PR they span every start the file remembers, and --totals-reset starts them from zero (a reset Prometheus reads as a counter reset, not a regression). Scope for the persistence itself still needs a maintainer's yes.

Approach

Five module-level helpers beside slot_save_dir (totals_path_of, totals_load_file, totals_add_saved, totals_write_file, totals_reset_file) and a TOTALS_KEYS list that names exactly what is kept. Writes go through strata_control.atomic (a neighbouring temporary file), so a reader never sees half a write. Only numbers are added: an unknown key or a bad value changes nothing, and a file that cannot be read is treated as a first start. A disk error prints once and turns the file off rather than costing an answer. The parser's shape diagnostics (tool_calls_from_reasoning, stray_think_closers, tool_args_repaired, tool_calls_unread) are deliberately not kept — they describe a reply, not a use to be counted.

Validation

Passed: .venv\Scripts\python.exe -m unittest serve.test_totals at 870aedd9 + this change on Windows 11 (NT 10.0.26200), Python 3.10.11, no GPU and no downloads: 32 tests OK, of which the 13 named above are this PR's (TotalsPath, TotalsFile, TotalsWindow) — they cover the path choice, a missing or broken file, adding a saved window and moving since back, the write/load round trip, two starts adding up, and the .prev.json move.

Failed: none observed.

Not run: the live --totals-reset start on the served model (the file moves and the printed line are covered by unit tests only); the Terminal suite, which needs pytest and is not installed in this venv; CUDA/HIP/SYCL builds are not needed but the app's own npm test was not run here (no node).

Limits

This changes a default — with a run config present, a new small file now appears beside it and the Monitor's window spans starts; --totals-file none is the way back to today's behaviour, and I can flip the default to opt-in if a maintainer prefers that. The window belongs to a run config, so two configs of the same model count separately; there is no reset from the page yet (#4631).


🔄 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/4745 **Author:** [@au70ma70n](https://github.com/au70ma70n) **Created:** 10/30/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `engine-totals-persistence` --- ### 📝 Commits (2) - [`145e349`](https://github.com/Niko1221/Strata/commit/145e349444f39c6c7dae300f40a15223098e173b) [GUI] settings: let a long help line wrap instead of squeezing every field - [`419a478`](https://github.com/Niko1221/Strata/commit/419a4784aff7e76b94799ee19e9e29342bb58b05) [Engine] serve: keep the Monitor's token counts between starts ### 📊 Changes **4 files changed** (+212 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+5 -1) 📝 `serve/server.py` (+89 -0) ➕ `serve/test_totals.py` (+113 -0) 📝 `serve/web/app.css` (+5 -2) </details> ### 📄 Description Draft — the costing story wants scope agreement first: #4742. It lands after #4743 (the layout fix, already open and standing on its own); merge order is #4743 → this one → #4746 → #4747, and until the branch before it merges this pull request's diff also shows those commits and shrinks on its own as they merge. Branch `engine-totals-persistence`, base `main`. ## Problem and result The counts the Monitor tab prints ("Since …: N requests · … input tokens read · … written") lived only in the process, so a restart lost them, and there was no way to close the window and read one session's totals on their own. Now they are kept in `<the run config's name>.totals.json` beside the run config, the way the shared Chat settings are kept: the file is read when the server starts and written when a request ends, so a restart or a crash loses no more than a request that never ended. The prompt and decode times travel with the counts, so the printed speeds stay averages of the whole window rather than of the last start. `--totals-file none` (or `off`) keeps the old one-start counting, and `--totals-file PATH` names the file. `--totals-reset` closes the window: this start counts from zero and the window that ended is moved aside to `<the same name>.prev.json` instead of being thrown away, so the session that just finished stays readable. ## Related discussion **#4742** — the scope thread for this story (this pull request and the two that price these counts). **#4631** — "[Engine] Monitor: Clear hides only the request rows; the totals can only be reset by restarting the server" (open, no labels, no linked pull request). It wants the totals zeroed without a restart and proposes `POST /metrics/reset` behind the API key, calling the Clear button. This answers the same problem from the other end — the window survives restarts, and a start with `--totals-reset` closes it — but it does **not** add the button, so it does not close #4631. That issue also asks that `/metrics/prometheus` stay monotonic; worth settling in the same thread, because these counters come from the same `totals`: after this PR they span every start the file remembers, and `--totals-reset` starts them from zero (a reset Prometheus reads as a counter reset, not a regression). Scope for the persistence itself still needs a maintainer's yes. ## Approach Five module-level helpers beside `slot_save_dir` (`totals_path_of`, `totals_load_file`, `totals_add_saved`, `totals_write_file`, `totals_reset_file`) and a `TOTALS_KEYS` list that names exactly what is kept. Writes go through `strata_control.atomic` (a neighbouring temporary file), so a reader never sees half a write. Only numbers are added: an unknown key or a bad value changes nothing, and a file that cannot be read is treated as a first start. A disk error prints once and turns the file off rather than costing an answer. The parser's shape diagnostics (`tool_calls_from_reasoning`, `stray_think_closers`, `tool_args_repaired`, `tool_calls_unread`) are deliberately not kept — they describe a reply, not a use to be counted. ## Validation **Passed:** `.venv\Scripts\python.exe -m unittest serve.test_totals` at `870aedd9` + this change on Windows 11 (NT 10.0.26200), Python 3.10.11, no GPU and no downloads: 32 tests OK, of which the 13 named above are this PR's (`TotalsPath`, `TotalsFile`, `TotalsWindow`) — they cover the path choice, a missing or broken file, adding a saved window and moving `since` back, the write/load round trip, two starts adding up, and the `.prev.json` move. **Failed:** none observed. **Not run:** the live `--totals-reset` start on the served model (the file moves and the printed line are covered by unit tests only); the Terminal suite, which needs pytest and is not installed in this venv; CUDA/HIP/SYCL builds are not needed but the app's own `npm test` was not run here (no node). ## Limits **This changes a default** — with a run config present, a new small file now appears beside it and the Monitor's window spans starts; `--totals-file none` is the way back to today's behaviour, and I can flip the default to opt-in if a maintainer prefers that. The window belongs to a run config, so two configs of the same model count separately; there is no reset from the page yet (#4631). --- <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#5157
No description provided.