Skip to content

Pre-compaction memoryFlush: larger default threshold, workspace-level flush instructions, and post-compaction audit #31435

@prasants

Description

@prasants

Problem

The pre-compaction memoryFlush feature exists and works, but the default configuration leads to context loss in long-running agent sessions.

Root causes

  1. softThresholdTokens default (4000) is too small. Not enough headroom for a thorough flush that updates multiple workspace files (e.g. session state, daily logs, and spec files).
  2. Default prompt is generic. It says 'store durable memories' but does not specify which files are critical. Every workspace has different critical files, and the model has no way to know which ones matter most.
  3. No feedback loop. After the flush turn, there is no verification that critical context was actually persisted. If the model skips a file or runs out of tokens mid-flush, that context is gone.

Current behaviour

  • memoryFlush fires at contextWindow - reserveTokens - 4000 tokens
  • Generic prompt: 'Store durable memories now, use memory/YYYY-MM-DD.md'
  • Model gets one turn to flush, then compaction runs
  • No post-compaction verification

Suggested improvements

  1. Increase default softThresholdTokens to 8000. This gives the model enough room for multiple file writes during the flush turn.
  2. Support a workspace-level flush instructions file (e.g. FLUSH.md). This file would be injected into the flush prompt, so each workspace can specify exactly which files MUST be updated before compaction. Different projects have different critical state files.
  3. Post-compaction audit. After compaction completes, verify that the workspace's critical files were updated during the flush turn. If not, inject a system message warning the model that context may have been lost.
  4. Expose token count to the model. Give the model a way to know how much context it has used (e.g. via session_status or a periodic system message), so it can flush proactively rather than only reactively when the threshold fires.

Workaround

Custom flush prompt via config:

{
  "agents": {
    "defaults": {
      "compaction": {
        "memoryFlush": {
          "enabled": true,
          "softThresholdTokens": 8000,
          "prompt": "Pre-compaction memory flush. MANDATORY: 1) Update SESSION-STATE.md with all current context. 2) Append new facts to today's daily log. 3) Update relevant spec files."
        }
      }
    }
  }
}

This works but should be default behaviour, not a workaround.

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