Skip to content

memoryFlush never triggers when compactionCount and memoryFlushCompactionCount are both 0 #47143

Description

@JameelHao

Description

memoryFlush never triggers for sessions that have never been compacted, because hasAlreadyFlushedForCurrentCompaction incorrectly returns true when both counters are 0.

Root Cause

In hasAlreadyFlushedForCurrentCompaction:

function hasAlreadyFlushedForCurrentCompaction(entry) {
  const compactionCount = entry.compactionCount ?? 0;
  const lastFlushAt = entry.memoryFlushCompactionCount;
  return typeof lastFlushAt === "number" && lastFlushAt === compactionCount;
}

When a session has:

  • compactionCount: 0 (never compacted)
  • memoryFlushCompactionCount: 0 (never flushed, or explicitly set to 0)

The function returns 0 === 0 → true, incorrectly indicating "already flushed", so flush is skipped.

Observed Behavior

// Session entry
{
  "totalTokens": 122985,      // > 80k threshold
  "totalTokensFresh": true,
  "compactionCount": 0,
  "memoryFlushCompactionCount": 0
}

Expected: memoryFlush should trigger (tokens > threshold, never flushed)
Actual: memoryFlush skipped because hasAlreadyFlushedForCurrentCompaction returns true

Suggested Fix

Option A: Check for undefined separately:

function hasAlreadyFlushedForCurrentCompaction(entry) {
  const compactionCount = entry.compactionCount ?? 0;
  const lastFlushAt = entry.memoryFlushCompactionCount;
  // Never flushed if undefined
  if (typeof lastFlushAt !== "number") return false;
  return lastFlushAt === compactionCount;
}

Option B: Use -1 or null as "never flushed" sentinel instead of 0.

Environment

  • OpenClaw: 2026.3.13 (61d171a)
  • OS: macOS (Darwin 25.3.0 arm64)

Impact

All sessions that grow past the flush threshold but have never been compacted will never auto-flush, leading to unbounded context growth until manual intervention.

Related

See also #47111 (qmd auto-update not firing) - separate issue but discovered during same investigation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal backlog priority with limited blast radius.clawsweeper:linked-pr-openClawSweeper found an open linked pull request for this issue.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.staleMarked as stale due to inactivity

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions