-
-
Notifications
You must be signed in to change notification settings - Fork 57.1k
Closed as not planned
Closed as not planned
Copy link
Labels
bugSomething isn't workingSomething isn't workingstaleMarked as stale due to inactivityMarked as stale due to inactivity
Description
Summary
The memory flush system checks entry.totalTokens to decide whether to run, but this value reflects token count from the previous turn, not the current context including the new message. This creates a race condition where large incoming messages can push context from "below threshold" to "overflow" in one jump, completely bypassing the flush.
Reproduction
- Configure
softThresholdTokens: 10000(flush triggers at 170K for 200K context) - Session is at 160K tokens after previous turn
- User sends a message that triggers a large tool output (e.g., browser snapshot = 20K+ tokens)
- Flush check sees
totalTokens = 160K(stale) → 160K < 170K → flush skipped - Agent turn runs, actual context is now 180K+
- Overflow triggers compaction
- Memory flush never ran - context lost
Root Cause
In agent-runner.js, runMemoryFlushIfNeeded is called before the agent turn, but uses sessionEntry.totalTokens which is only updated after turns complete.
// memory-flush.js - shouldRunMemoryFlush()
const totalTokens = params.entry?.totalTokens; // STALE - from previous turn
if (totalTokens < threshold) return false; // Skips flush incorrectlyProposed Fix
Estimate incoming message tokens before the flush decision:
// In agent-runner.js, before calling runMemoryFlushIfNeeded:
const incomingTokens = estimateTokens(commandBody) + systemPromptOverhead;
const projectedTokens = (sessionEntry?.totalTokens ?? 0) + incomingTokens;
// Pass projected tokens to flush decision
activeSessionEntry = await runMemoryFlushIfNeeded({
...params,
projectedTotalTokens,
});Environment
- Clawdbot version: 2026.1.24-3
- Model: anthropic/claude-opus-4-5 (200K context)
- Config:
softThresholdTokens: 10000,reserveTokensFloor: 20000
Discovered 2026-01-31 during debugging session.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingstaleMarked as stale due to inactivityMarked as stale due to inactivity