openclaw - 💡(How to fix) Fix fix: resolveFreshSessionTotalTokens returns undefined when totalTokens is preserved (post-#82578 regression) [4 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

PR #82578 correctly fixed persistSessionUsageUpdate from overwriting totalTokens=undefined when there is no fresh context snapshot. After the fix, totalTokens is preserved across stale usage updates.

However, resolveFreshSessionTotalTokens() (in src/types.ts) still returns undefined when entry?.totalTokensFresh === false, even though totalTokens is now a valid, preserved number. This function is used by:

  1. /context command handler → returns undefined → "context unavailable"
  2. session-utils.ts transcript estimation → returns undefined → triggers unnecessary transcript re-read

Fix Action

Fixed

Code Example

Current session (after fix):
  totalTokens: 110392
  totalTokensFresh: True (after agent refresh) / False (after new message)
  compactionCount: 0
  cacheRead: 109568

---

// Before:
if (entry?.totalTokensFresh === false) return;
return total;

// After:
// totalTokensFresh=false only means "possibly stale", not "unavailable".
// The value itself is preserved from compaction/transcript (PR #82578).
return total;
RAW_BUFFERClick to expand / collapse

Bug: resolveFreshSessionTotalTokens returns undefined when totalTokens is preserved (post-#82578 regression)

Symptom

After every 2-3 conversational turns, the /context command shows stale/unknown context utilization, and the Control UI context meter resets to 0%. The session still works correctly and context IS being sent to the model — this is purely a display/estimation issue.

Root Cause

PR #82578 correctly fixed persistSessionUsageUpdate from overwriting totalTokens=undefined when there is no fresh context snapshot. After the fix, totalTokens is preserved across stale usage updates.

However, resolveFreshSessionTotalTokens() (in src/types.ts) still returns undefined when entry?.totalTokensFresh === false, even though totalTokens is now a valid, preserved number. This function is used by:

  1. /context command handler → returns undefined → "context unavailable"
  2. session-utils.ts transcript estimation → returns undefined → triggers unnecessary transcript re-read

Internal consumers unaffected

The compaction preflight check and memory flush logic check entry.totalTokensFresh directly on the entry object, not through resolveFreshSessionTotalTokens(). So they are unaffected and continue to work correctly.

Real Behavior Data

Current session (after fix):
  totalTokens: 110392
  totalTokensFresh: True (after agent refresh) / False (after new message)
  compactionCount: 0
  cacheRead: 109568

Flow trace:

  1. User sends message → session-updates.ts sets totalTokensFresh = false (incrementBy > 0)
  2. persistSessionUsageUpdate also sets totalTokensFresh = false when !hasFreshContextSnapshot
  3. Control UI queries status → resolveFreshSessionTotalTokens sees totalTokensFresh === false → returns undefined
  4. /context command shows stale/unavailable
  5. Agent starts preflight → entry.totalTokensFresh === false (checked directly on entry) → reads from transcript → sets totalTokensFresh = true
  6. Status becomes available again... until next message

Suggested Fix

In resolveFreshSessionTotalTokens(), return the preserved totalTokens value even when totalTokensFresh === false, since the value is now guaranteed to be valid (not undefined) after PR #82578.

// Before:
if (entry?.totalTokensFresh === false) return;
return total;

// After:
// totalTokensFresh=false only means "possibly stale", not "unavailable".
// The value itself is preserved from compaction/transcript (PR #82578).
return total;

The totalTokensFresh flag continues to serve its intended purpose for compaction preflight and memory flush, which check it directly on entry.totalTokensFresh.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix fix: resolveFreshSessionTotalTokens returns undefined when totalTokens is preserved (post-#82578 regression) [4 pull requests]