Skip to content

Bug: in-turn auto-compaction can bypass memoryFlush because memoryFlush only runs pre-turn #62420

Description

@realaboo

Summary

When agents.defaults.compaction.memoryFlush.enabled=true, OpenClaw can still perform an in-turn auto-compaction without writing any memory files.

The root cause appears to be that runMemoryFlushIfNeeded() only runs before the turn starts, while some auto-compactions happen during the turn. In that case, compaction completes, but memory flush is never retried.

Observed behavior

  • Session later shows a real compaction event in the session transcript / JSONL
  • No memory/*.md write happens for that compaction cycle
  • /status may still show Compactions: 0 at some points, so the session transcript is a more reliable source than the status card
  • This is not caused by the session-memory hook being disabled; that hook is only for /new and /reset

Expected behavior

If compaction.memoryFlush.enabled=true, then actual auto-compaction paths should not bypass memory flush.

At minimum, one of these should happen:

  1. Run memory flush immediately before any runtime-triggered auto-compaction
  2. Or run a best-effort post-compaction flush / audit when compaction occurred without a prior flush
  3. Or document clearly that memoryFlush only applies to pre-turn checks and does not cover in-turn auto-compaction

Why this is happening (code path)

From the current runtime flow in dist/agent-runner.runtime-C-sR1PRP.js:

  • runPreflightCompactionIfNeeded(...)
  • runMemoryFlushIfNeeded(...)
  • then the actual turn starts

In runReplyAgent(...), runMemoryFlushIfNeeded() is called before the main turn execution.

Later, during the actual turn, the runtime can emit compaction events (evt.stream === "compaction"), and the session compaction count is incremented afterward — but there is no corresponding memory flush retry on that path.

So the current behavior is effectively:

  • pre-turn: maybe flush
  • in-turn: maybe auto-compact
  • but no flush is retried if compaction happens later during the turn

Impact

This creates a misleading configuration/behavior mismatch:

  • config says memory flush is enabled
  • real compaction happens
  • but no durable memory file is written

For users, this looks like "memoryFlush is enabled but does not actually protect all compaction cases".

Environment

  • OpenClaw: 2026.3.28
  • Runtime: direct
  • Channel: Discord thread session
  • compaction.mode: safeguard
  • compaction.memoryFlush.enabled: true

Suggested fix

Make runtime-triggered auto-compaction paths call the same flush logic (or a safe subset of it), instead of limiting memoryFlush to pre-turn execution only.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal backlog priority with limited blast radius.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:data-lossCan lose, corrupt, or silently drop user/session/config data.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