Skip to content

Bug: Pre-compaction memory flush uses stale token counts, can be bypassed #5457

@AmbitiousRealism2025

Description

@AmbitiousRealism2025

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

  1. Configure softThresholdTokens: 10000 (flush triggers at 170K for 200K context)
  2. Session is at 160K tokens after previous turn
  3. User sends a message that triggers a large tool output (e.g., browser snapshot = 20K+ tokens)
  4. Flush check sees totalTokens = 160K (stale) → 160K < 170K → flush skipped
  5. Agent turn runs, actual context is now 180K+
  6. Overflow triggers compaction
  7. 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 incorrectly

Proposed 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleMarked as stale due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions