Skip to content

[Feature]: Pre-reset agentic memory flush — /new and daily reset should get the same memory flush as compaction #45608

@kamikariat

Description

@kamikariat

Summary

Run a silent agentic memory flush before /new, /reset, and the daily reset destroy the session, using the same mechanism that already runs before compaction.

Problem to solve

When a session nears compaction, OpenClaw runs a silent agentic turn (runMemoryFlushIfNeeded) where the agent writes curated notes to memory/YYYY-MM-DD.md before context is compressed. This works well.

When a user sends /new or /reset — or when the daily reset fires automatically — the session is destroyed with no agentic flush. The only safety net is the session-memory hook, which mechanically dumps the last 15 raw messages into a file. The agent never gets a chance to curate what matters, update MEMORY.md, or write structured notes.

(There's also a separate bug where the hook's output uses slugged filenames that the boot sequence doesn't read — #45607.)

The asymmetry:

  • Compaction → agentic flush (agent decides what to save) ✅
  • /new and /reset → mechanical dump (raw messages, no curation) ❌
  • Daily reset → same mechanical dump, triggered silently every morning ❌

The daily reset is the big one. The default reset mode is daily with atHour: 4 (src/config/sessions/reset.ts:22-23). Every user hits it at least once per day without any manual action. Any context from the previous day that wasn't written to disk is gone.

Proposed solution

Before destroying the session on /new or /reset, run a silent agentic turn (same pattern as runMemoryFlushIfNeeded) that gives the agent a chance to write durable memories to disk.

Behavior:

  1. User sends /new (or /reset)
  2. Gateway detects reset trigger (existing code in initSessionState)
  3. New step: Before minting the new sessionId, run a silent pre-reset memory flush turn against the current session
  4. The agent writes any durable notes to memory/YYYY-MM-DD.md (or MEMORY.md), then replies NO_REPLY
  5. Session is destroyed, new session starts (existing behavior)
  6. session-memory hook fires as before (raw dump is still useful as backup / audit trail)

Most of the infrastructure already exists:

  • runMemoryFlushIfNeeded in src/auto-reply/reply/agent-runner-memory.ts:253
  • before_reset plugin hook in src/plugins/types.ts:541
  • NO_REPLY suppression for silent turns
  • Daily memory checkpoint (shouldRunDailyMemoryCheckpoint in src/auto-reply/reply/memory-flush.ts:196) already extends the flush beyond compaction

The main work is wiring runMemoryFlushIfNeeded (or a variant) into the reset path before the session is destroyed.

Guards:

  • Skip for empty sessions (no turns = no flush)
  • Skip for CLI and read-only workspaces (same as existing compaction flush)
  • Timeout cap (~30s) so /new doesn't feel stuck
  • One flush per reset (track in session entry)

Alternatives considered

  1. Status quo (session-memory hook only): Dumps raw messages, no curation. Agent can't decide what matters. Output uses slugged filenames the boot sequence can't read ([Bug]: session-memory hook writes files the boot sequence never reads #45607).
  2. Make session-memory hook agentic: Run an LLM call inside the hook. Viable but the hook is fire-and-forget with no access to the agent's tools or workspace context. The existing pre-compaction flush already has the right infrastructure.
  3. Add a /save command: Explicit user-triggered flush. Works but puts the burden on the user and doesn't help when they forget (or for the daily reset).
  4. Background flush after reset: Destroy session immediately, flush archived transcript in background. Preserves instant /new feel but requires reconstructing agent context from archived JSONL.

Impact

  • Affected: Every OpenClaw user (daily reset is default behavior)
  • Severity: Medium — silent memory loss every day
  • Frequency: At least once per day per user (daily reset), plus every manual /new or /reset
  • Consequence: Agent loses learned context between sessions; users with memory-heavy workloads (personal assistants) are most impacted

Evidence/examples

I wrote a detailed proposal with source code references, full design history, and recent commit analysis at docs/proposals/pre-reset-memory-flush.md in my fork. Key findings:

  • The pre-compaction memory flush was designed for compaction (Jan 12 2026, 7dbb21be8) and never extended to resets
  • When /new was first added (Dec 4 2025, 34eb75f63), there was no workspace memory system
  • The daily memory checkpoint (1146b3664 by @PotatoParser, Mar 5 2026) was the first acknowledgment that compaction-only wasn't sufficient — but it only covers active sessions, not sessions that ended before the boundary
  • The session-memory hook and before_reset plugin hook (Feb 14 2026, ab71fdf82) provide mechanical preservation but no agentic curation
  • The post-reset boot sequence has been carefully built up, but the pre-reset teardown path has no agentic save step

Additional information

Open questions:

  1. Blocking vs background: Should /new block until the flush completes, or start the new session immediately and flush in the background?
  2. Config location: compaction.memoryFlush.onReset (extends existing) vs session.reset.memoryFlush (cleaner separation)?
  3. Daily reset: Should it also get a flush? Options: background flush on next message, proactive "bedtime flush" via heartbeat before atHour, or accept daily checkpoint is good enough for that case.
  4. Interaction with session-memory hook: Keep both? The hook provides raw audit trail; the agentic flush provides curated memory. They serve different purposes and can coexist.

Related: #45607 (session-memory naming mismatch bug)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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