You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(gradient): reuse exact visible-content token counts across turns #1737
The reported provenance cache is warm (2,300 hits / 2 misses), but gradient's estimateMessage/estimateParts repeatedly call synchronous BPE for visible text and tool outputs. estimateTokens caches tokenizer instances, not string counts. Compression logged about 18.9 s after prewarm in the new trace; this identifies an expensive stage, not proof that BPE owns all of it.
Measure count calls, unique input bytes and BPE time in the gradient fitting paths, using the current benchmark fixture and representative large outputs.
Introduce a bounded exact-count cache for visible content and generated/stripped variants. Key it by the actual counted bytes, encoding/tokenizer version and relevant representation policy; message ID alone is insufficient.
Reuse immutable part/message counts and already validated source-checkpoint metadata across fitting stages and appended turns. Count changed/new content once rather than resumming by retokenizing strings.
Persist only bounded disposable hashes/counts when appropriate, reusing the source-window/cache durability rules. Preserve no-store behavior, ownership and database-generation guards.
Retain the existing budget/calibration policy and exact fallback on cache miss. Cached per-part arithmetic must match current counting boundaries; BPE counts are not generally additive across arbitrary concatenation boundaries.
Acceptance criteria
Warm continuation and fresh-process checkpoint resume avoid BPE over unchanged retained content.
Token estimates, selected layer/window and serialized model request match the uncached reference across compression stages.
Changed output, reasoning/provenance, stripping, encoding or tokenizer version invalidates the appropriate counts.
Cache capacity is bounded by entries/bytes and eviction cannot break correctness; no hidden full-history retention.
Report CPU/time and BPE-byte reduction on the same workload, including a small-session control.
Dependencies and boundary
Independent extension of merged #1710/#1726 and #1717. One caching PR; no tokenizer replacement, heuristic counting, generic streaming parser or change to context budgets. If cold misses remain a bottleneck, record that separately.
Hard prerequisites
None. This issue can be implemented independently.
Activity
Handle gateway signals early and own worker teardown (#1764)