Skip to content

save-session: no cap on extract size → oversized Haiku prompt exceeds context window → silent rotation stop (complements #94) #96

Closed

Description

@selvi5006-commits

Summary

A single long-lived session can grow an extract far larger than Haiku's context window. save-session.sh embeds the entire extract into the Step-4 prompt with no size cap, so the claude -p --model haiku call fails (input exceeds context length), set -e aborts the save, and the daily rotation (now.md → today-YYYY-MM-DD.md) silently stops. It is self-reinforcing: a failed save never advances the saved position, so the same session re-extracts the full transcript on every subsequent save and fails identically — until an unrelated small session happens to run.

Confirmed on marketplace remember@claude-plugins-official v0.7.3, Windows / Git Bash.

This is related to but distinct from #94. #94 attributes FAILED at line ~179 to machine-load-induced hangs/timeouts plus the dead set -e error handler. My incident is not a hang — every failing save logs an oversized extract and fails in ~25s, not a 180s timeout. The two are complementary: a size cap prevents the failure; #94's || HAIKU_EXIT=$? + timeout change makes any residual failure visible and non-blocking. Neither alone is sufficient.


Evidence (from .remember/logs/memory-*.log)

Daily rotation went dark for two full days. Every save against the offending session logged:

[extract] 5389 exchanges (292 human)
[haiku] calling (branch: ...)
[rotate] ERROR: tar failed for 30 logs        # non-fatal red herring (log.sh:220 else-branch returns 0)
[error] FAILED at line 175 (exit 1)

Meanwhile small sessions on the same machine saved fine, interleaved with the failures:

20:34  [extract] 33 exchanges (2 human)   → OK
20:57  [extract] 5389 exchanges (292 human) → FAILED at line 175
20:59  [extract] 51 exchanges (5 human)   → OK, [write] appended

5389 exchanges is well past Haiku's 200K-token window, so the CLI rejects the input and exits non-zero. Because the failing line is the HAIKU_JSON=$(... < "$TMP_PROMPT") command substitution, set -e trips there and the generic ERR trap logs FAILED at line 175 — the helpful if [ "$HAIKU_EXIT" -ne 0 ] diagnostic never runs (the dead-handler issue, already noted in #94), which is why the real "context length exceeded" error is invisible in the logs.


Root cause

scripts/save-session.sh Step 3 calls pipeline.shell build-prompt "$EXTRACT_FILE" ..., which substitutes the full $EXTRACT_FILE into the prompt {{EXTRACT}} placeholder. There is no upper bound on $EXTRACT_FILE size anywhere between extract (Step 1) and the Haiku call (Step 4). A session that accumulates thousands of exchanges therefore produces a prompt that cannot fit in the model's context.


Suggested fix

Cap the extract (keeping the most-recent tail) before building the prompt. I've been running this locally with no downside — daily rotation resumed immediately:

# After the dry-run block, before "Step 2: Get last entry":
EXTRACT_MAX_BYTES=$(config ".thresholds.extract_max_bytes" 300000)
if [ -f "$EXTRACT_FILE" ]; then
    EXTRACT_BYTES=$(wc -c < "$EXTRACT_FILE" | tr -d ' ')
    if [ "${EXTRACT_BYTES:-0}" -gt "$EXTRACT_MAX_BYTES" ]; then
        TMP_CAPPED=$(mktemp "${TMPDIR:-/tmp}"/remember-extract-capped-XXXXXX)
        printf '[NOTE: transcript truncated to the last %s of %s bytes — summarize the most recent work below]\n\n' \
            "$EXTRACT_MAX_BYTES" "$EXTRACT_BYTES" > "$TMP_CAPPED"
        tail -c "$EXTRACT_MAX_BYTES" "$EXTRACT_FILE" >> "$TMP_CAPPED"
        mv "$TMP_CAPPED" "$EXTRACT_FILE"
        log "extract" "capped ${EXTRACT_BYTES}b -> ${EXTRACT_MAX_BYTES}b (oversized-prompt guard)"
    fi
fi

Default 300 KB (~75K tokens) leaves generous headroom under Haiku's 200K window; configurable via .thresholds.extract_max_bytes. Keeping the tail is appropriate since the summary should reflect the most recent work. (A token-aware cap inside build-prompt would be even better, but the byte cap is a robust, dependency-free floor.)

Combined with #94's timeout + reachable error handler, an oversized or slow Haiku call would then degrade gracefully instead of silently halting all memory writes.


Environment

  • remember@claude-plugins-official v0.7.3
  • Windows 10 / Git Bash (MSYS coreutils)
  • Symptom window: two days of missing today-*.md; FAILED at line 175 also present in 8 separate daily logs over the prior weeks whenever a session got large.

Activity

  1. fdaviddpt commented on Jun 21, 2026

    @fdaviddpt
    Contributor

    Thank you for this — genuinely one of the clearest bug reports we've had. You nailed the root cause (full extract embedded in the prompt with no upper bound), the self-reinforcing loop (failed save never advances the saved position), and correctly separated it from #94. That saved us a lot of digging.

    Fixed in #109 along the lines you suggested: cmd_build_prompt now caps the extract at thresholds.extract_max_bytes (default 300 KB), keeping the most-recent tail with a truncation note so the summary still reflects current work. 0 disables it. Ships in v0.8.2.

    We put the cap in the Python build-prompt step rather than the shell (closer to where the prompt is assembled, and unit-testable), but it's functionally your patch. Credited you in the changelog. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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