Skip to content

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

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.mdtoday-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.

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