fix(autoplan): bounded, append-tolerant owned-journal read (A2)

The publication guard no longer reads the whole parent journal or refuses
it at 32 MiB. lib/claude-owned-journal.ts streams the bytes present at open
to the last complete line (pass one), keeps a content-free index per record
and decides ownership, ancestry and causal order on it with the same checks
and reason codes as before. Pass two reloads in full only what the
evaluation reads (autoplan/bin/guard-journal.ts): every literal init and its
result, the current call, and inside the invocation window (from the latest
human turn at or before the bound chain's first init) guarded and
close-packet Read results, structured Bash results, error results, assistant
text after the first close Read and active-plan mutations, plus the earlier
Read a dedup reply cites even before the window (ENG-4).

- lib/claude-journal-records.ts: per-record event extraction, record
  metadata and the causal-order checks, shared by readPlanCountTranscript
  and the bounded reader. The batch reader keeps its whole-file read and cap.
- Appends after open never fail a read; same device and inode and no
  shrink are required (identity). A second read in one hook invocation must
  hash bytes [0, first size) the same, or it is `rewritten` (UC1 unverified);
  the unrecognized-shape double read uses that prefix hash. `changing` is
  removed (REMOVED_CODES; its anchor stays).
- OWNED_RECORD_MAX_BYTES (32 MiB): only one record over it is too_large.
  OWNED_RETAINED_MAX_BYTES (32 MiB): over it is the new oversized_invocation
  fallback denial. GSTACK_TRANSCRIPT_TEST_MAX_BYTES and
  GSTACK_TRANSCRIPT_TEST_RETAINED_BYTES can only lower them.
- ENG-5: evaluation groups uses and results by tool id once (no per-tool
  rescans).

CEO-25 / ENG-5 spike (120 MiB synthetic, 31,242 records, 4 vCPU Linux,
Bun 1.4.2): a full streaming rescan (parse every line, small index, sha256)
took 254-326 ms at 147-161 MiB peak RSS; a pass-one index cache under the
state root (load cached index, verify a 64 KiB tail hash, parse the tail)
took 26-29 ms at 47 MiB. The rescan meets the 1 s budget, so the cache is
not adopted: it would add trusted persistent state and invalidation for no
budget need. A backward tail read cannot prove competing_root or cycle, so
it was not a candidate.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
garrytanandcapy-ai[bot] committed 2026-10-07 21:03:31 +00:00
1 parent 8c11ebc519
commit cd12cb207a
13 files changed
+1140 -510

No files matched your search

+21 -7
View File
@@ -79,9 +79,11 @@ missing) and did not catch up within 2 seconds.
<a id="rewritten"></a>
### `rewritten`
The journal bytes the guard had already read changed during one hook call.
Claude Code normally only appends. Rewrites between hook calls are not
detected.
The journal bytes the guard had already read changed during one hook call:
a later read's sha256 of bytes [0, first size) differs from the first read's,
or a record pass two reloads no longer parses to the record pass one indexed.
Claude Code normally only appends, and appends never fail a read. Rewrites
between hook calls are not detected (an accepted limit).
<a id="too-large"></a><a id="journal-too-large"></a>
### `too_large`
@@ -89,6 +91,7 @@ detected.
A single journal record is over the 32 MiB record limit (`OWNED_RECORD_MAX_BYTES`).
The guard reads long journals record by record, so a large journal alone
never triggers this; one enormous record (for example a huge pasted file) does.
A record still being written (no newline yet) is not read at all.
<a id="unseen-version"></a>
### `unseen_version`
@@ -127,6 +130,15 @@ The journal path, its directories or the file failed the identity checks: a
symlink, a foreign session, the wrong directory layout, a different file, or a
journal that shrank. A rerun hits the same check.
<a id="oversized-invocation"></a>
### `oversized_invocation`
The current `/autoplan` invocation needs more journal records read in full
than the 32 MiB retained-data bound (`OWNED_RETAINED_MAX_BYTES`). The guard
indexes every record without its content and reloads in full only what it
evaluates: snapshot init results, guarded and close-packet Reads, structured
Bash results, error results, assistant text after the first close-packet Read
and edits of the active plan. A rerun reads the same records; use the fallback.
<a id="malformed"></a>
### `malformed`
A complete journal line is not valid JSON or UTF-8, or its records contradict
@@ -263,12 +275,14 @@ A phase-entry call from an earlier message has no result yet. Wait for it.
### `mutation_pending`
An edit to the active plan after the close Read has no result yet. Wait for it.
## Removed codes
<a id="changing"></a>
### `changing`
Claude Code was writing the journal on every read for 2 seconds. Wait for the
current response to finish.
## Removed codes
Before the October 7, 2026 release, `changing` denied a call when Claude Code
appended to the journal while the guard read it. The bounded reader reads the
bytes present at open and ignores a trailing record still being written, so
appends never fail a read; a changed prefix is [`rewritten`](#rewritten).
<a id="current-missing"></a><a id="pending-read"></a>
Before the October 7, 2026 release, "Native parent evidence has not reached