Skip to content

Consolidation prompt is uncapped — overflows the model window and stalls saves (twin of #96) #122

Closed

Description

@presempathy-awb

Summary

v0.8.2 (#96) added an oversized-extract guard to the save path (extract_max_bytes → cmd_build_prompt), but the consolidation path has the same unbounded-input flaw and was never capped. consolidate() inlines the full staging set + recent.md + archive.md into one prompt and calls Haiku with no size check. When the assembled prompt exceeds the model window, the nested claude -p fails with Prompt is too long; run-consolidation.sh logs ERROR and exits 1, so daily rotation halts — and it is self-reinforcing, because the staging files are not retired and re-feed identically on the next run (exactly the #96 failure mode, one path over).

Observed in the wild: scanning one machine's ~/.claude/projects/, 57 crashed sdk-cli sessions carried the consolidation system prompt ("You are a memory consolidation agent…") and ended on Prompt is too long. (Affects through v0.8.3.)

Why a cap, not a truncation

The save-path fix tail-truncates the extract — safe there, because the extract only feeds a one-shot summary. Consolidation rewrites recent.md/archive.md from the model output, so tail-truncating the input would feed the model a partial archive.md and the dropped head would be permanently lost on the next write. So the guard should skip (reusing the existing ConsolidationSkipped semantics: leave staging + memory untouched) rather than truncate. Converting a crash into a clean skip is the minimal safe fix; bounding archive.md growth so consolidation can always run is the deeper follow-up (see end).

Proposed fix

New thresholds.consolidate_max_bytes (default 600000, 0 disables), threaded shell → cmd_consolidate → consolidate(), mirroring how extract_max_bytes is threaded to cmd_build_prompt.

pipeline/consolidate.py — guard before the Haiku call:

def consolidate(staging_contents, recent, archive, max_prompt_bytes=0):
    prompt = build_consolidation_prompt(staging_contents, recent, archive)

    # Oversized-prompt guard. Mirrors extract_max_bytes but SKIPS instead of
    # truncating: consolidation rewrites recent/archive, so truncating the input
    # would permanently drop the dropped head on the next write.
    if max_prompt_bytes > 0:
        prompt_bytes = len(prompt.encode("utf-8"))
        if prompt_bytes > max_prompt_bytes:
            raise ConsolidationSkipped(
                f"consolidation prompt too large ({prompt_bytes} bytes > "
                f"{max_prompt_bytes} cap) -- skipping to avoid a context-window "
                f"overflow; staging + memory left untouched"
            )

    result = call_haiku(prompt, timeout=180)
    ...

pipeline/shell.py — cmd_consolidate(..., max_prompt_bytes: int = 0) passes it to consolidate(...); dispatch parses max_prompt_bytes=int(sys.argv[5]) if len(sys.argv) > 5 else 0.

scripts/run-consolidation.sh:

CONSOLIDATE_MAX_BYTES=$(config ".thresholds.consolidate_max_bytes" 600000)
RESULT=$(... pipeline.shell consolidate "$STAGING_DIR" "$RECENT_FILE" "$ARCHIVE_FILE" "$CONSOLIDATE_MAX_BYTES" 2>&1) || { ... }

config.example.json — add thresholds.consolidate_max_bytes: 600000 + a _comments entry.

Tests

  • oversized prompt → raises ConsolidationSkipped, and call_haiku is never called;
  • under cap → Haiku called once;
  • max_prompt_bytes=0 → prior behavior preserved;
  • dispatch test updated + a new case asserting the 6th argv arg sets the cap.

I implemented and verified this locally against v0.8.3: tests/test_consolidate.py + tests/test_shell.py 61 passed (py3.12); full suite 490 passed / 41 skipped (the one failure is a pre-existing, host-specific BSD-mktemp assertion unrelated to this change). End-to-end: pipeline.shell consolidate on a 1.6 MB staging file with a 600 KB cap prints CONSOLIDATION_STATUS=skip, exit 0, with REMEMBER_CLAUDE_BIN=/nonexistent/claude never triggered (skips before any call). Happy to open a PR if useful.

Deeper follow-up (separate issue)

The skip prevents the crash but means a project whose archive.md has grown past the cap stops consolidating. Root cause is unbounded archive.md growth (every run re-feeds the whole archive through the model). Options: rotate/cap archive.md, or consolidate staging in size-bounded batches so a single run never approaches the window.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions