fix(memoryFlush): correct context token accounting for flush gating#5343
fix(memoryFlush): correct context token accounting for flush gating#5343jalehman merged 11 commits intoopenclaw:mainfrom
Conversation
Additional Comments (1)
This looks like it should be Also appears in Prompt To Fix With AIThis is a comment left during a code review.
Path: src/auto-reply/reply/session-usage.ts
Line: 41:49
Comment:
[P0] `totalTokens` is now set to `promptTokens` (input + cacheRead + cacheWrite), which drops `output` tokens even though the PR description says total should include output. That will undercount usage and can keep memory flush from triggering on long responses.
This looks like it should be `input + output + cacheRead + cacheWrite` (or use `params.usage.total` if that already includes all components).
Also appears in `src/auto-reply/reply/agent-runner-memory.ts` where `readPromptTokensFromSessionLog()` computes `totalTokens` as `inputTokens + outputTokens` but `inputTokens` is derived from prompt-only usage.
How can I resolve this? If you propose a fix, please make it concise. |
f95c99a to
42532cf
Compare
|
Thanks for the review! This was already addressed in commit a8cf37a1a ( The current formula is: const promptTokens = input + cacheRead + cacheWrite;
totalTokens: promptTokens > 0 ? promptTokens + output : (params.usage?.total ?? input + output)So |
b78014e to
145bf00
Compare
3417a11 to
27156d1
Compare
fbda31d to
a9b5709
Compare
29b80b0 to
3805199
Compare
6486869 to
33f760f
Compare
33f760f to
118cf5f
Compare
… + tests - Sum all usage entries from transcript instead of using only the last one - Gate transcript reading behind missing baseTotalTokens (performance optimization) - Add prompt token estimate to effective total - Export helper functions for testing - Add new tests for prompt estimates and transcript fallback Addresses PR review comments on openclaw#5343. Co-authored-by: Jarvis <[email protected]>
bfa5464 to
47ad192
Compare
… + tests - Sum all usage entries from transcript instead of using only the last one - Gate transcript reading behind missing baseTotalTokens (performance optimization) - Add prompt token estimate to effective total - Export helper functions for testing - Add new tests for prompt estimates and transcript fallback Addresses PR review comments on openclaw#5343. Co-authored-by: Jarvis <[email protected]>
24daaba to
dcdb785
Compare
… + tests - Sum all usage entries from transcript instead of using only the last one - Gate transcript reading behind missing baseTotalTokens (performance optimization) - Add prompt token estimate to effective total - Export helper functions for testing - Add new tests for prompt estimates and transcript fallback Addresses PR review comments on openclaw#5343. Co-authored-by: Jarvis <[email protected]>
c2a67b4 to
2233025
Compare
… + tests - Sum all usage entries from transcript instead of using only the last one - Gate transcript reading behind missing baseTotalTokens (performance optimization) - Add prompt token estimate to effective total - Export helper functions for testing - Add new tests for prompt estimates and transcript fallback Addresses PR review comments on openclaw#5343. Co-authored-by: Jarvis <[email protected]>
…nReplyAgent tests Co-authored-by: Jarvis <[email protected]>
bf66871 to
69c8860
Compare
|
Merged via squash. Thanks @jarvis-medmatic! |
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman # Conflicts: # src/auto-reply/reply/agent-runner-memory.ts # src/commands/agent/session-store.ts # src/cron/isolated-agent/run.ts
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman # Conflicts: # src/auto-reply/reply/agent-runner-memory.ts # src/commands/agent/session-store.ts # src/cron/isolated-agent/run.ts
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#5343) Merged via squash. Prepared head SHA: afaa7ba Co-authored-by: jarvis-medmatic <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
Summary
Memory flush could be skipped when session totals were stale/unknown after stricter
totalTokensFreshchecks. This PR makes flush gating deterministic by using a fresh projected next-context token count while keeping persisted session accounting semantics clear.What changed
1)
SessionEntry.totalTokensstays prompt/context-only and resilientderiveSessionTotalTokens()now consistently returns prompt/context tokens.totalTokenswhen the derived value is finite and > 0.totalTokensand marktotalTokensFresh = false.usage.total_tokensonly when available.Files:
src/agents/usage.tssrc/commands/agent/session-store.tssrc/cron/isolated-agent/run.ts2) Memory flush gates on projected next-context tokens
projected = promptTokensSnapshot + lastOutputTokens + nextUserPromptEstimateshouldRunMemoryFlush()now accepts optionaltokenCount; when present, that value is used for gating while duplicate-flush suppression still usescompactionCount/memoryFlushCompactionCount.Files:
src/auto-reply/reply/memory-flush.tssrc/auto-reply/reply/agent-runner-memory.tssrc/auto-reply/reply/agent-runner.ts3) Transcript fallback + tail-scan performance
In
agent-runner-memory.ts:totalTokenswithtotalTokensFresh = true.4) Relative
sessionFiletranscript paths are normalizedRelative session transcript paths are normalized through
resolveSessionFilePath(...)+resolveSessionFilePathOptions(...)(includingstorePath) before reading.This prevents silent fallback failures when session-store entries contain relative
sessionFilepaths.5) Test updates included in this PR
sessionFiletranscript fallback path handling.Files:
src/auto-reply/reply/agent-runner.runreplyagent.test.tsScope clarification
Validation
AI Disclosure 🤖
Co-authored-by: Jarvis [email protected]
Greptile Overview (outdated)
Greptile Summary
This PR improves memory flush triggering by making the totalTokens signal more reliable: it estimates prompt tokens for the pending turn, falls back to reading the last usage record from the session transcript when the session store entry is stale, and logs diagnostic details explaining why a flush did/didn’t trigger. It also adds unit tests around edge cases where totalTokens is missing or invalid, and adds verbose logging when usage is absent so totalTokens updates are skipped.
The changes primarily live in the reply agent runner’s memory-flush path (src/auto-reply/reply/agent-runner-memory.ts) and the session usage persistence helper (src/auto-reply/reply/session-usage.ts), which together determine when the pre-compaction “memory flush” turn should run.
Confidence Score: 3/5
src/auto-reply/reply/session-usage.ts; src/auto-reply/reply/agent-runner-memory.ts
Context used: