Skip to content

fix: large-file byte-path fold-in (read cap, retry trim, deterministic splice)#103

Merged
whykusanagi merged 3 commits into
mainfrom
fix/large-file-byte-path
Jul 15, 2026
Merged

fix: large-file byte-path fold-in (read cap, retry trim, deterministic splice)#103
whykusanagi merged 3 commits into
mainfrom
fix/large-file-byte-path

Conversation

@whykusanagi

Copy link
Copy Markdown
Owner

Three compounding reliability bugs on the large-file byte path, folded into one release. Each is an independent commit with its own tests.

1. read_file returned oversized, silently-truncated blobs (fix)

A line range applied the range but never bounded the byte count, so a request for lines 1–220 on a minified/generated file returned the whole payload — which main.go then mid-byte-cut at 131072 (the 128 KiB history cap), poisoning the session.

  • Configurable returned-content budget (default 48 KiB, under DefaultMaxToolResultBytes) applied before the result is built.
  • Truncation cuts on a line boundary; a single minified line falls back to a byte-bounded head — never the 131072 poison blob.
  • Metadata: total_bytes, returned_bytes, truncated, actual end_line, next_offset_line + a paging hint.

2. Timeout retries replayed the identical oversized payload (fix)

  • Doomed retry: withRetry reused one ctx across attempts; callers baked a single deadline into it, so a timeout on attempt 1 left an expired ctx and attempt 2 failed instantly. Now each attempt gets a fresh per-attempt deadline from a cancel-only base ctx (same 60s default — not raised).
  • Identical replay: a beforeTry hook trims the largest text tool-result before each attempt, with a progressively tighter budget on retries (48K/24K/12K), copy-on-write so history is never mutated. Images + non-tool messages untouched.
  • Pre-flight guard: the attempt-0 pass caps any tool message already over 48 KiB before the first send.
  • Parent-cancel (Ctrl+C) short-circuits the loop.

3. Bulk byte-moving edits routed content through the model (feat)

New deterministic splice_file: moves a region between files by anchors/line-ranges — the model says where, the bytes are copied on disk, never regenerated (slow + a silent-corruption vector otherwise). Unique-anchor enforcement, line-aligned regions, copy/move semantics, post-write verification. patch_file now rejects a new_string over 16 KiB and points to splice_file.

Constraints honored (from the incident spec)

  • Client timeout not raised to mask the payload.
  • Nothing baked into provider/model config — all client-side.
  • Model does not regenerate large literal payloads as tool arguments.

Tests

Per-bug TDD: read_file byte-bounds + metadata; retry fresh-deadline / parent-cancel / trim line-boundary + copy-on-write; splice T1–T5 (115 KB inline-CSS extraction, line-range copy, replace-between-anchors, 478 KB byte-integrity, error taxonomy) + intra-file move + patch guard. Full suite + golangci-lint (0 issues) + gofmt green. Large fixtures generated at runtime — no binaries committed.

Release: contains a feat (splice_file) → minor bump.

…on metadata

read_file applied the line range but never bounded the byte count, so a request
for lines 1-220 on a minified/generated file returned the whole 443 KB — which
main.go then mid-byte-cut to 131072 (the 128 KiB history cap), poisoning the
session (celeste-cli/read_file-byte-cap).

- Add a configurable returned-content budget (default 48 KiB, well under
  context.DefaultMaxToolResultBytes = 128 KiB) applied BEFORE the result is
  built, via WithMaxResultBytes.
- budgetLines: keep as many whole lines as fit; cut on a line boundary. A single
  oversized/minified line falls back to a byte-bounded head (the only option),
  still marked truncated — never the 131072 poison blob.
- lineAlignedCut: the in-memory read ceiling now cuts on a newline, not mid-byte.
- Structured metadata: total_bytes, returned_bytes, truncated, end_line (actual
  returned), next_offset_line + a hint to page or use search.

Tests: line-aligned budget truncation, small-file untouched, minified single
line stays byte-bounded. Existing read_file tests unchanged. Root cause + the
128 KiB / 512 KB constants confirmed against source (main.go:952, limits.go:12).

Bug 1 of 3 in the large-file-byte-path fold-in; retry-trim + deterministic
inject_file follow.
Timeout retries were a no-op that replayed an identical oversized request
(celeste-cli/retry-no-context-trim). Three root causes:

A. Doomed retry: withRetry reused ONE ctx across all attempts, and callers
   baked a single deadline into it (main.go:715 60s, main.go:1889 GetTimeout).
   A timeout on attempt 1 left an already-expired ctx, so attempt 2 failed
   instantly. Now withRetry owns a FRESH per-attempt deadline derived from a
   cancel-only base ctx; callers pass context.WithCancel. Same per-request
   timeout value (60s default) — not raised — but the retry can actually run.
B. Identical replay: fn re-sent the same messages slice. A beforeTry hook now
   trims the largest text tool-result before each attempt, with a progressively
   tighter budget on retries (48K, 24K, 12K), copy-on-write so session history
   is never mutated. Image tool-results and non-tool messages are left intact.
C. No pre-flight guard: the attempt-0 trim pass caps any tool message already
   over 48 KiB before the first send.

Parent-cancel (Ctrl+C) short-circuits the loop instead of replaying. Per the
incident constraints: client-side only, nothing baked into provider/model
config, and the timeout is not raised to mask the payload.

Tests: fresh-deadline-per-attempt, stop-on-parent-cancel, beforeTry-per-attempt,
trim line-boundary + copy-on-write + image/non-tool skipped. Existing retry
tests migrated to the new signature. Full suite + lint green.

Bug 2 of 3 in the large-file-byte-path fold-in; deterministic inject_file next.
Bulk byte-moving edits routed the whole payload through the model as tool
arguments — slow, and a silent-corruption vector on a dropped/garbled stream
delta (celeste-cli/edit-routes-bytes-through-model).

splice_file moves a region between files deterministically: the model supplies
WHERE the bytes are (source + line range or a start/end anchor pair) and WHERE
they go (dest anchor to insert at, or an anchor pair to replace), and the bytes
are copied on disk — never regenerated. Anchors must be unique (ambiguous or
missing → error); anchor regions expand to whole lines so moves leave clean
boundaries; copy leaves source intact, move removes the region. Post-write
verification confirms the region is present in dest and absent from source
(cross-file move). Result reports routed_through_llm=false.

patch_file now rejects a new_string over 16 KiB and points to splice_file: a
literal that large is almost always a byte-move that must be deterministic.

Registered in Agent/Claw/Chat. Tests T1-T5 build the incident-shaped fixtures
at runtime (115 KB inline-CSS extraction, line-range copy, replace-between-
anchors, 478 KB byte-integrity, error taxonomy) plus intra-file move and the
patch_file guard — no large binaries committed. Full suite + lint green.

Bug 3 of 3 in the large-file-byte-path fold-in.
@whykusanagi
whykusanagi force-pushed the fix/large-file-byte-path branch from 3517ede to 8bb00ff Compare July 15, 2026 03:11
@whykusanagi
whykusanagi merged commit 41f74d9 into main Jul 15, 2026
5 checks passed
whykusanagi added a commit that referenced this pull request Jul 15, 2026
…lidated changelog (#105)

Follow-up to #103, found by smoke-testing the binary.

- fix(llm): raise maxToolMsgBytes to 64 KiB so the retry trim no longer re-truncates read_file's own capped result (48K content < 64K message trim < 128K history cap).
- fix(ci): build release binaries with Go 1.26.5 (GO-2026-5856 crypto/tls fix).
- test(smoke): assert index status on 'Symbols:' not stale 'index'.
- docs: consolidated 1.14.0 changelog (skills browser + file-write fold-in) + splice_file in README (45 tools).
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.

1 participant