Skip to content

Feature: guarantee last N raw messages in agent context (survive compaction and session reset) #58818

Description

@atticusfawler

Problem

When an agent wakes up in a session, its context window is built from the current session's JSONL transcript. Two mechanisms destroy or compress older messages:

  1. Daily reset (default 4am): mints a new sessionId → the agent starts with zero messages. Previous conversation is gone from context entirely.
  2. Compaction: summarizes older messages into a compact entry → the agent sees a lossy summary instead of the actual messages.

This means the agent has no guarantee of seeing the last N actual messages from the conversation. A session that was active yesterday evening starts fresh at 4am with zero raw messages. A long-running session that compacted may have replaced recent-ish messages with a summary.

For personal assistant use cases (and many others), the agent needs continuity: the last N messages should always be present in the context window as raw messages, not summaries, regardless of when they were sent.

Desired Behavior

A configurable session.minTailMessages (or similar) that guarantees the agent always sees at least the last N raw messages in its context window:

  1. Across compaction: compaction should never summarize the last N messages. The summary covers everything before the tail.
  2. Across daily/idle reset: when a new session is created, the last N messages from the previous session's transcript should be carried forward as seed messages into the new session's context (not as a summary, but as actual user/assistant message pairs).
  3. Configurable N: default could be ~20-50 messages. Users with large context windows might want more.

Example

Session A (yesterday): 200 messages, last message at 23:45
  ↓ daily reset at 04:00
Session B (today): starts with last N messages from Session A as context seed
  → Agent immediately knows what was discussed yesterday evening

Compaction behavior change

Currently compaction summarizes everything older than the internal keepRecentMessages threshold. With this feature:

  • minTailMessages acts as a floor: compaction must preserve at least N raw messages
  • The compaction summary covers messages before the tail
  • This is different from the existing internal keep-recent logic because it's a user-facing guarantee, not an implementation detail

Reset behavior change

Currently, session reset = clean slate. With this feature:

  • On reset (daily, idle, or manual /new), read the last N messages from the outgoing session's JSONL
  • Inject them as seed context into the new session (either as actual JSONL entries or as a structured "previous context" block)
  • The agent sees continuity without needing to read files or rely on session-memory dumps

Configuration

// openclaw.json
{
  session: {
    // Minimum raw messages preserved in context (survives compaction + reset)
    minTailMessages: 30,
    
    // Optional: carry forward across resets (default: true when minTailMessages > 0)
    carryForwardOnReset: true,
  }
}

Interaction with existing features

  • Pre-reset memory flush ([Feature]: Pre-reset agentic memory flush — /new and daily reset should get the same memory flush as compaction #45608): complementary. The flush lets the agent write durable notes; minTailMessages ensures it can read recent conversation. Both are needed.
  • Compaction: minTailMessages sets a floor on what compaction can touch. The existing compaction logic already has a concept of "recent messages to keep" — this makes it explicit and configurable.
  • Session pruning: unaffected (pruning only touches tool results, not user/assistant messages).
  • Context engine plugins: the assemble() method should respect minTailMessages as a contract. Plugins can exceed it but not go below.

Why this matters

Personal assistants, coding agents, and any long-running agent benefit from conversational continuity. Today, the daily reset creates an artificial amnesia boundary that forces users to either:

  • Disable daily resets (sessions grow unbounded)
  • Accept that the agent forgets everything each morning
  • Build workarounds (memory files, boot scripts)

minTailMessages provides a clean middle ground: bounded sessions with guaranteed recent context.

Edge cases

  • N exceeds context window: cap at what fits after system prompt + tools
  • Short sessions: if session has fewer than N messages, carry all of them
  • Manual /new: respect carryForwardOnReset setting (user might want a truly clean slate — could add /new --clean to override)
  • Compaction model: the compaction summary should still reference that the tail messages exist, to avoid the summary contradicting the raw messages

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:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.clawsweeper:needs-security-reviewClawSweeper marked this issue as needing security-sensitive review.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:securitySecurity boundary, credential, authz, sandbox, or sensitive-data risk.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.

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions