Skip to content

[Bug]: totalTokensFresh permanently set to true after first API response — pre-flight compaction skipped on all subsequent turns #65600

@nailujac

Description

@nailujac

Bug type

Behavior bug (incorrect output/state without crash)

Beta release blocker

No

Summary

...Pre-flight compaction (runPreflightCompactionIfNeeded) is skipped on every turn after the first API response because totalTokensFresh is permanently set to true and never reset between turns.

The guard at the top of the function:

if (!(entry.totalTokensFresh === false || !hasPersistedTotalTokens)) return entry;

requires totalTokensFresh === false to proceed when hasPersistedTotalTokens is true. But after the first successful API response, totalTokensFresh is set to true (line ~1912 in agent-runner.runtime) and is only ever set back to false when a brand new session is created (line ~2400). This means:

• Turn 1: fresh session → totalTokensFresh: false → pre-flight runs ✅
• Turn 2+: after API response → totalTokensFresh: true → early return, pre-flight skipped ❌

The pre-flight compaction check never evaluates the token threshold on subsequent turns, regardless of how large the context grows.

Steps to reproduce

  1. Configure agents.defaults.contextTokens to a value below the model's native context window (e.g., 120000)
  2. Set compaction.mode: "safeguard"
  3. Start a conversation and use enough turns to exceed the configured threshold
  4. Observe via /verbose or logVerbose output that preflightCompaction check logs show persistedFresh=true on every turn after the first
  5. Pre-flight compaction never triggers — the function returns early before reaching shouldRunPreflightCompaction()

Expected behavior

totalTokensFresh should be reset to false after being consumed by the pre-flight check (or at the start of each new turn), so the guard allows re-evaluation on subsequent turns.

Actual behavior

totalTokensFresh stays true permanently after the first API response, causing the early return to fire on every subsequent turn.

OpenClaw version

2026.4.9 (confirmed identical in 2026.4.11)

Operating system

Linux 6.17.0-1007-oracle (arm64)

Install method

npm global

Model

anthropic/claude-opus-4-6

Provider / routing chain

anthropic (direct)

Additional provider/model setup details

No response

Logs, screenshots, and evidence

Impact and severity

No response

Additional information

Relevant code locations:

• Early return guard: agent-runner.runtime.js → runPreflightCompactionIfNeeded() (line ~1333 in 4.9, ~1368 in 4.11)
• Set to true after API response: agent-runner.runtime.js (line ~1912 in 4.9)
• Set to false only on new session: agent-runner.runtime.js (line ~2400 in 4.9)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbug:behaviorIncorrect behavior without a crash

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions