Skip to content

Fix #1051: apply the session_start_max_bytes budget even on a day rollover - #1094

Merged
fdaviddpt merged 5 commits into
mainfrom
fix/1051
Nov 1, 2026
Merged

fdaviddpt merged 5 commits into
mainfrom
fix/1051

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #1051.

_remember_apply_session_start_budget re-renders the MEMORY section fresh and requires
byte-equality against the tail of the already-captured SessionStart body before it will trim
anything over session_start_max_bytes. On a cache HIT (the #668 start-context cache), that
tail can legitimately hold bytes rendered on a PRIOR calendar day -- every check the loader
applies (mtimes, VERSION=) passes on a cache that is a day old by design -- so a live
TODAY-dated re-render can never byte-match it, and the function was giving up ("MEMORY section
changed before the budget check -- injected as rendered, over budget") on the first SessionStart
of every new day. This is not the #845 race the equality check exists to catch (nothing changes
mid-render); it is two different calendar days producing two different renders by construction.

_remember_start_cache_context_load now writes the exact byte-length of what it served into a
PID-scoped marker file (REMEMBER_DIR/tmp/start-context.served-bytes.$$), because that loader
runs inside the subshell that builds the SessionStart body -- a plain shell variable set there is
invisible to the parent shell's later budget check, but a file persists across that boundary.
_remember_apply_session_start_budget reads the marker once, at the top of the function before
any early return (so it is always consumed, single-use), and on a mismatch uses it to derive
head_len instead of giving up -- the head (handoff + legend) is always rendered live in the
same process and is unaffected by which day's MEMORY section the served tail holds.

Review

Two concurrent spawns reviewed this diff (a general-purpose correctness reviewer and
oss:auditor) and found three issues, all fixed in a follow-up commit:

  • a marker-file leak on the common under-budget cache-hit path (read-and-delete only ran on the
    over-budget branch)
  • a TOCTOU/symlink hazard in the marker write (switched to mktemp + mv, matching the convention
    already used three lines above it for the cache/manifest files)
  • a stale header comment describing only pre-fix behavior

Fixing those surfaced a self-caught regression in the byte-count plumbing that briefly broke the
existing test_start_context_cache_668.py byte-identity assertion; restored the direct
cat "$_cache" stdout contract and fixed it before this PR.

One further finding -- a torn marker write under disk-full conditions could silently misreport
with no warning logged -- was judged narrow-precondition and non-blocking, and is tracked in
trap.d/1051.torn-marker-write.md rather than fixed inline.

Tests

New: tests/test_session_start_budget_rollover_1051.py -- an over-budget store is still trimmed
across a day-1-to-day-2 cache-hit rollover, with a positive-control test (an under-budget store is
untouched across the same rollover) pairing the "must trim" case with a "must not touch" case.

RED: reverted only the fix (git stash push -- scripts/lib-memory-context.sh), reran -- the
over-budget test failed on the expected mismatch-warning assertion, the positive control still
passed (confirming it's a real control). GREEN: both pass with the fix restored.

Targeted run after the self-review fixes: 550 passed (the rollover test plus every related
#842/#845/#668/#660/#679/#898/#900/#332/#695 test file), plus a subset rerun under macOS system
bash 3.2 to check the cross-platform arithmetic/string handling, all green. Full suite not run
locally -- CI is this repo's full-matrix merge gate.

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 5 commits November 1, 2026 15:53
…lover

_remember_apply_session_start_budget re-renders the MEMORY section fresh and
requires it to equal, byte for byte, the tail of the already-captured body
before it will trim anything. On a cache HIT, that tail can legitimately
hold bytes rendered on a PRIOR calendar day -- every check the loader
applies (mtimes, VERSION=) passes on a cache that is a day old, by design
(#668) -- so a fresh, TODAY-dated re-render can never match it. That is not
the #845 race the equality check exists to catch (nothing changes
mid-render); it is two different calendar days producing two different
renders by construction. The mismatch made the function log "MEMORY section
changed before the budget check -- injected as rendered, over budget" and
return the body untrimmed on the first SessionStart of every new day --
exactly the failure the budget exists to prevent.

_remember_start_cache_context_load now drops the exact byte length of what
it served into a PID-scoped marker file under REMEMBER_DIR/tmp (a plain
shell variable set there never reaches the parent shell: the cache load
runs inside the subshell that builds the SessionStart body). The budget
function reads that marker once, consumes it, and on a mismatch uses it to
derive the head/tail split instead of giving up -- the head (handoff +
legend) is always rendered live in the same process and is unaffected by
which day's MEMORY section the served tail holds.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…leak, and a regression

Three issues found by the Explore and oss:auditor self-review spawns against
the first commit:

- The marker file was written with a plain `>` redirect onto a predictable
  PID-named path -- a symlink planted there before this process starts
  would be followed and truncated. Switched to mktemp + mv into place,
  matching this same file's own established convention for the cache and
  manifest files three lines above.
- The marker was only consumed inside the over-budget branch, so it leaked
  permanently on disk on every cache-hit SessionStart that stayed under
  budget -- the common case, since staying under budget is the point of
  the cache. Moved the read-and-delete to the very top of
  _remember_apply_session_start_budget so it runs on every call, hit or
  miss, budget enabled or not.
- Fixing the leak surfaced a real regression in the first commit: capturing
  the cache file's content via `$(...)` for the byte-length computation and
  then re-emitting it with `printf` (instead of streaming the original
  `cat`) silently stripped trailing newlines, breaking
  test_start_context_cache_668.py's byte-identity assertion between a cache
  hit and a live render. Restored the direct `cat` as the function's actual
  stdout; the byte-length capture now reads the file a second time, only
  when a marker path is in play, purely to compute the count.

Also fixes the stale function-header comment above
_remember_apply_session_start_budget, which still described only the
pre-#1051 "mismatch always means give up" behavior.

A non-blocking, narrow finding (a torn marker write under disk-full could
parse as valid and silently produce a wrong split) is logged to
trap.d/1051.torn-marker-write.md rather than fixed inline.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Removes the PID-scoped marker file _remember_start_cache_context_load
wrote on every cache-served SessionStart (mktemp + mv to write it, a
second cat to measure it, rm to consume it) -- measured 26 -> 30 process
launches with the #679 counting harness, on the common path rather than
the rare over-budget one. _remember_apply_session_start_budget now
recovers the same head/tail split for free, by re-reading
start-context.cache directly with $(<...) (a bash builtin, no fork) only
on the mismatch path that actually needs it, and distrusts that read
when a concurrent republish has raced it (#845) rather than trusting a
split that could be wrong. Removes the stale trap.d note describing the
marker's torn-write hazard, since the marker no longer exists. Extends
tests/test_session_start_budget_rollover_1051.py with a REMEMBER-header
intactness check, a stray-marker-file check, a spawn-count pin run
against main's own code, and a direct pin of the #845 fallback.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…kip-triage.md)

tests/test_session_start_budget_rollover_1051.py carries the same module-level
pytestmark = pytest.mark.skipif(sys.platform == "win32", ...) as its siblings
and needs a row in the same commit per this doc's own convention -- caught by
tests/test_windows_skip_triage_497.py failing on windows-latest CI rather than
locally, since that guard only runs there.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…heckout

`git archive main` failed on CI (ubuntu-latest, 3.10) with exit 128: a PR's
own checkout commonly fetches only the merge commit at depth 1, so neither
`main` nor `origin/main` is guaranteed to have a resolvable, fully-fetched
tree. _resolve_main_ref now does a fresh `git fetch --depth 1 origin main`
first (works against the same "origin" the checkout action already set
up, regardless of what the checkout happened to leave behind), falling
back to the pre-existing local-ref candidates, and the test skips loudly
with a reason rather than failing when neither path produces a usable ref.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@fdaviddpt
fdaviddpt merged commit e739f83 into main Nov 1, 2026
19 checks passed
@fdaviddpt
fdaviddpt deleted the fix/1051 branch November 1, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

session_start_max_bytes budget skipped on first session of the day ("MEMORY section changed before the budget check")

1 participant