Skip to content

feat(tensorfold): read cached prompt tokens from /metrics - #272

Closed
eleata wants to merge 1 commit into
MiaAI-Lab:mainfrom
eleata:tensorfold-prompt-tokens-cached
Closed

eleata wants to merge 1 commit into
MiaAI-Lab:mainfrom
eleata:tensorfold-prompt-tokens-cached

Conversation

@eleata

@eleata eleata commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

What

_applyTensorFoldMetrics now reads tensorfold:prompt_tokens_cached_total and
sets this.totalCachedTokens from it. When that counter is absent, it falls
back to tensorfold:prefix_cache_hits_total, which carries the same number. The
cached counter and prompt_tokens_total come from the same scrape, and the
cached value is clamped to that scrape's prompt total so it can never exceed the
prompt it belongs to.

Why

TensorFold builds that keep prompt states publish the cached prompt split in
/metrics, but the collector only used the prefix-cache counters for the hit
rate. The token ledger therefore had no cached counter, and the token pages
showed the cache as unknown.

How

The new read sits in the existing non-queue branch of
_applyTensorFoldMetrics, beside the prompt and generation totals. Builds that
publish neither counter keep their previous value. The /health path
(cached_tokens_total) and the queue-only poll are unchanged.

Testing

  • npm ci, npm run typecheck and npm run build: passed.
  • node --test server/collectors/__tests__/LlmProbe.tensorfold.test.js: 45/45 passed.
  • node --test server/llmtokens/__tests__/*.test.js: 26/26 passed.
  • Full server suite (the test:server file list, Node 26 on macOS): main gives 1128 tests with 20 failures; this branch gives 1129 tests with the same 20 failures, all in server/llmlaunch/__tests__ (launcher, api) and server/tooleval/__tests__ (tooleval, api), which this change does not touch.

The regression test covers the direct counter, the prefix-cache fallback, the clamp, absent counters, and the queue-only poll.

TensorFold's /metrics publishes tensorfold:prompt_tokens_cached_total on
builds that keep prompt states (the four-node DeepSeek build, and the
two-node one after its metrics port), but _applyTensorFoldMetrics only
used prefix_cache_queries_total and prefix_cache_hits_total for the hit
rate. The token ledger therefore had no cached counter and the token
pages showed the cache as unknown.

Read prompt_tokens_cached_total in the same scrape as prompt_tokens_total
and set totalCachedTokens from it, falling back to prefix_cache_hits_total
when the dedicated counter is absent. Clamp the cached count to that
scrape's prompt total so it can never exceed the prompt it belongs to.
Builds that publish neither counter keep their previous value, and the
queue-only poll and the /health cached_tokens_total path are unchanged.

Add a regression test covering the direct counter, the fallback, the
clamp, absent counters, and the queue-only poll.
@MiaAI-Lab

Copy link
Copy Markdown
Owner

The cache-counter change is now on main as fa5c0e4, preserving the original author and cherry-pick attribution. The collector reads the dedicated counter, uses the prefix-cache fallback, and clamps against the same scrape's prompt total (server/collectors/LlmProbe.js:1781). Its regression fails on the pre-fix collector and passes with this change (server/collectors/__tests__/LlmProbe.tensorfold.test.js:524); typecheck, build and the full server/frontend suites pass. The CHANGELOG credits @eleata, and this PR is being closed as completed. (Review notes prepared with Claude Code on behalf of the maintainer.)

@MiaAI-Lab MiaAI-Lab closed this Oct 23, 2026
pavelkubicek added a commit to pavelkubicek/sparkDash that referenced this pull request Oct 27, 2026
78 upstream commits: MQTT publishing (MiaAI-Lab#248), harness occupancy helpers (MiaAI-Lab#208),
GPU clock cap from Settings (MiaAI-Lab#218), settings i18n foundation (MiaAI-Lab#226),
configurable GPU temperature limits (MiaAI-Lab#273), Locals/Splash engines,
TensorFold cache-counter fixes (MiaAI-Lab#252/MiaAI-Lab#260/MiaAI-Lab#272) and the zsh-safe remote probes (MiaAI-Lab#255/MiaAI-Lab#257).

Conflict resolutions:
- server/collectors/LlmProbe.js + its TensorFold test: took main's implementation,
  which supersedes this fork's /health `live` + /metrics work with the cache-counter
  precedence, running/waiting aliases, histogram validation and prefill holds. This
  drops the fork's parallel /metrics fetch (micro-optimisation only).
- SystemCollector: main's zsh-safe find probes merged into the fork's build/parse
  refactor, keeping the fork-only RAPL package-power reads; _getRemoteNetwork now uses
  the injected executor (the merge had left the fork body calling sshExec, which made
  main's zsh-glob corpus test run real SSH).
- OverviewPage: FleetKpis keeps the fork's placement but takes all sparks, so hidden
  cluster workers count in the KPI tiles (main's fix).
- SparkPage: main's column layout (History in the hardware column, storage/network in
  the right column) over the fork's full-width History panel.
- SettingsDialog: fork VulnHunter/REA opt-in toggles kept, metrics-history toggle
  switched to main's i18n keys.
- index.js: fork vulnHunt/rea spark cleanup kept alongside MQTT removeUnit/applySettings.
- CHANGELOG/README/package.json: both sides' entries kept; test globs unioned so the
  fork-only models/autopower/vulnhunt suites still run.
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