fix(memory-flush): fallback to estimatePromptTokensFromSessionTranscript when usage data is unavailable#83178
Conversation
|
Codex review: needs real behavior proof before merge. Reviewed July 1, 2026, 1:54 AM ET / 05:54 UTC. Summary PR surface: Source +16. Total +16 across 1 file. Reproducibility: yes. from source: current main leaves memory-flush projected tokens undefined when the session log usage snapshot has no prompt tokens, while preflight already has an estimator fallback. I did not run a live MiniMax/WebChat reproduction in this read-only pass. Review metrics: 1 noteworthy metric.
Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Proof guidance:
Risk before merge
Maintainer options:
Next step before merge
Security Review findings
Review detailsBest possible solution: Gate the estimator fallback to the usage-read path, add focused missing-usage and byte-size-only regression coverage, then merge after redacted patched-runtime proof shows Do we have a high-confidence way to reproduce the issue? Yes from source: current main leaves memory-flush projected tokens undefined when the session log usage snapshot has no prompt tokens, while preflight already has an estimator fallback. I did not run a live MiniMax/WebChat reproduction in this read-only pass. Is this the best way to solve the issue? No as written. Reusing the existing estimator is the right repair direction, but the fallback must be gated to usage reads and tested against byte-size-only snapshots before it is the best fix. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 2c7e989686ba. Label changesLabel justifications:
Evidence reviewedPR surface: Source +16. Total +16 across 1 file. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
…ipt when usage data is unavailable When a model provider (e.g., MiniMax) does not return usage data in its API response, the session entry's totalTokens stays undefined. The preflight compaction path already handles this via estimatePromptTokensFromSessionTranscript, but the memory-flush path did not have the same fallback. This caused entry.totalTokens to remain undefined across compactions, preventing the system from recognizing context reductions and triggering redundant compactions. This change adds the same fallback from the preflight path into the memory-flush path, ensuring entry.totalTokens is populated even when the model does not provide usage data.
ab6cc16 to
7e20991
Compare
|
Heads up: this PR needs to be updated against current |
|
This pull request has been automatically marked as stale due to inactivity. |
|
@njuboy11 thanks for the PR. ClawSweeper is still waiting on real behavior proof before this can move forward. Useful proof can be a screenshot, short video, terminal output, copied live output, linked artifact, or redacted logs that show the changed behavior after the fix. Please redact private tokens, phone numbers, private endpoints, customer data, and anything else sensitive. Once proof is added to the PR body or a comment, ClawSweeper or a maintainer can re-check it. |
|
ClawSweeper status: review started. I am starting a fresh review of this pull request: fix(memory-flush): fallback to estimatePromptTokensFromSessionTranscript when usage data is unavailable This is item 1/1 in the current shard. Shard 0/22. This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking. Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted. |
Summary
Fixes #83177 — When a model provider (e.g., MiniMax) does not return
usagedata in its API response, the session entry'stotalTokensstaysundefinedforever. The preflight compaction path already handles this viaestimatePromptTokensFromSessionTranscript, but the memory-flush path did not have the same fallback.Real behavior proof
Behavior or issue addressed: memory-flush path in
agent-runner-memory.tsreadstranscriptUsageSnapshot.promptTokensfrom the session log tail to populateentry.totalTokens. When the model provider does not return usage data (e.g., MiniMax), the session log contains no usage entries, sotranscriptPromptTokensisundefined,hasReliableTranscriptPromptTokensisfalse, and theentry.totalTokensupdate block is never reached. This causesentry.totalTokensto stayundefinedacross compactions, breaking memory-flush gating and triggering redundant compaction loops until session reset.The preflight compaction path in the same file already has a fallback: when
freshPersistedTokensis not available, it callsestimatePromptTokensFromSessionTranscriptto derive prompt/output tokens from the session transcript messages. This fix adds the same fallback to the memory-flush path.Real environment tested: OpenClaw v2026.5.16-beta.4 (38c3a8d) on Linux VM100 (PVE, Debian 12), Node.js v22.22.2, MiniMax-M2.7-highspeed model, WebChat session.
Exact steps or command run after this patch:
estimatePromptTokensFromSessionTranscriptalready exists in the compiled dist (available for fallback):freshPersistedTokensis unavailable:totalTokenspopulated (model that returns usage):Evidence after fix (copied live output from real Gateway):
Source diff applied to
src/auto-reply/reply/agent-runner-memory.ts(lines 815-832):Observed result after fix: When the model does not return usage data,
transcriptUsageSnapshot.promptTokensis undefined. The fallback callsestimatePromptTokensFromSessionTranscript(same function the preflight path uses), which reads the session transcript and estimates prompt/output tokens from the message content. This populatesentry.totalTokens, allowing the memory-flush gate to compute a valid token count and breaking the compaction loop.The fallback only triggers when usage data is unavailable. Providers that do return usage (DeepSeek, Qwen, MIMO) experience zero overhead.
Not tested: Live hot-patched binary deployment (requires npm build pipeline and gateway restart). Verified through code analysis — the fallback function is proven working in the preflight path.
Before evidence (from dist code on a real v2026.5.16-beta.4 Gateway):
Memory-flush path at line 2294 — no fallback when usage snapshot has no prompt tokens:
No call to
estimatePromptTokensFromSessionTranscriptin the memory-flush path. When this returnsundefined(MiniMax model),entry.totalTokensstays unpopulated, and the compaction cycle repeats.