ltx: keep a read error that arrives with a field's last bytes
Astra R1 P1-1 on sqlite-integrity-safe. A reader may return a field's last bytes and a transport error in one call; io.ReadFull fills its buffer and drops the error, and the next read said only "unexpected EOF". inputReader now holds any error that comes with bytes and returns it on every later read, and Decoder.Close returns a held error before reading the raw tail. errors.Is finds the original error wherever it arrives. TestLTXReadErrorIsKept now also sweeps every cut with the error in the same read as the last bytes (then io.EOF), with named cases at the file header boundary and the zero page header, through the Decoder and the Compactor. The round's findings are committed unedited.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+108
−21
docs/superpowers/reviews/CODEX-FINDINGS-sqlite-integrity-safe-R1.md+37 −0internal/lease/ltxtruncate_test.go+50 −18third_party/ltx/decoder.go+21 −3
diff --git a/docs/superpowers/reviews/CODEX-FINDINGS-sqlite-integrity-safe-R1.md b/docs/superpowers/reviews/CODEX-FINDINGS-sqlite-integrity-safe-R1.md| new file mode 100644 |
| index 000000000..b2898c31c |
| --- /dev/null |
| +++ b/docs/superpowers/reviews/CODEX-FINDINGS-sqlite-integrity-safe-R1.md |
| @@ -0,0 +1,37 @@ |
| +Round 1 found two merge blockers at `3aa2a4819e76b8f4434d8e42a4d961437efc3060`. |
| + |
| +### P1 |
| + |
| +1. **The decoder can lose a transport error returned with the last bytes of a field.** A reader may return a complete 100-byte LTX header and a connection reset in the same call, then return EOF on the next read. `inputReader.Read` passes the bytes and reset to `io.ReadFull`, which discards the error because the header is full. The next page-header read reports `io.ErrUnexpectedEOF`. The test misses this case because its reader always delivers the error in a separate, zero-byte read. Preserve a pending non-EOF error at the decoder input and make `Close` honor it before reading the raw tail. Add decoder and compactor cases at both the file-header boundary and the zero-page terminator. **Invariant:** a transport error remains discoverable with `errors.Is` regardless of which read delivers it. [Decoder header read](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/third_party/ltx/decoder.go:128) · [Input reader](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/third_party/ltx/decoder.go:307) · [Raw tail read](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/third_party/ltx/decoder.go:80) · [Go’s `ReadFull` contract](/home/paulca/.local/go-dist/src/io/io.go:346) · [Test reader](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/lease/ltxtruncate_test.go:188) |
| + |
| +2. **An unrecognized journal mode can count as current.** With source header bytes 18–19 equal to `(1,2)` and equal counters, `sourceHeader` returns `wal=false`. The box drill then counts the replica as current, and the hibernate drill records `not_stale`. The pinned SQLite reader checks byte 19 to decide whether to open the WAL, so `(1,2)` cannot establish rollback-journal freshness. Require `(1,1)` to award current, retain the existing `(2,2)` verdict, and report unknown for other pairs on the level-or-ahead path. Add a mixed-version fixture and preserve the existing lower-counter STALE rule. **Invariant:** current requires affirmative evidence of rollback-journal mode. [Mode detection](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/ops/freshness.go:150) · [Current verdict](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/ops/freshness.go:112) · [Hibernate mapping](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/ops/hibernate_verify.go:779) · [Pinned SQLite WAL decision](/home/paulca/go/pkg/mod/modernc.org/sqlite@v1.55.0/lib/sqlite_g_0000000000003a80.go:8824) |
| + |
| +### P2 |
| + |
| +None. |
| + |
| +### P3 |
| + |
| +**The summary prints “1 are current.”** The mixed-freshness test pins that exact phrase. Use count labels such as “current 1, could not be judged 2” during the planned copy review. **Invariant:** the summary reads correctly for zero, one, and multiple replicas. [Summary](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/ops/litestream.go:570) · [Output assertion](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/ops/freshness_test.go:144) |
| + |
| +### Verification |
| + |
| +By inspection, the short-tail guard precedes the checksum slice, the zero-page marker still ends the page block, and complete inputs retain index, trailer, and checksum verification. The tests sweep every strict prefix of two real replicated files through the decoder and compactor, then exercise the former panic through a pinned lease restore. I found no healthy-file regression or added S3 request in the restore, compaction, console seed, drill, move, or `dbdoor` paths. The box drill retains its existing retry and exit conditions. The hibernate drill retains its lease check and returns unknown for an empty source path. [Prefix sweep](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/lease/ltxtruncate_test.go:147) · [Restore witness](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/lease/ltxtruncate_test.go:233) · [Restore and retry](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/ops/litestream.go:516) · [Empty-path guard](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/sqlite-integrity-safe/internal/ops/hibernate_verify.go:905) |
| + |
| +The read-only sandbox prevented the targeted Go test from starting. Network restrictions also prevented reading #1673 directly or posting the census verdicts to #280. The scope ruling used here is the one recorded in the pinned brief. |
| + |
| +### Census |
| + |
| +These verdicts cover root read authority at the reviewed tip. |
| + |
| +| Unit or envelope | Verdict | Lines read and basis | |
| +|---|---|---| |
| +| `RestoreVerify` | APPROVE | `internal/ops/litestream.go:501–575,591–599` — same config, restore command, arguments, retry, and cleanup. | |
| +| `Decoder.Close` | APPROVE | `third_party/ltx/decoder.go:69–121` — reads the existing stream and checks the file and snapshot checksums. The length guard precedes slicing. | |
| +| `sourceHeader` | APPROVE | `internal/ops/freshness.go:92–110,136–150` and `internal/lease/check.go:399–413` — the replacement opens the same source path and reads the same first 28 bytes. | |
| +| `inputReader.Read` | APPROVE | `third_party/ltx/decoder.go:30–38,305–321` — delegates to the supplied reader. It introduces no path or input selection. | |
| +| `.` | APPROVE | `go.mod:102–110` — the fork comment changed. Both replacement targets are identical to the base. | |
| +| `internal/ops` | APPROVE | `freshness.go:71–150`, `litestream.go:488–575`, `hibernate_verify.go:767–799,898–909` — the existing lease gate still precedes the local read. The empty-path guard returns before opening a file. | |
| +| `third_party/ltx` | APPROVE | `decoder.go:30–38,69–121,149–206,286–322,351–385` — header, LZ4, index, and trailer handling use the caller’s stream or bytes already read from it. | |
| + |
| +**NOT READY** |
| \ No newline at end of file |
diff --git a/internal/lease/ltxtruncate_test.go b/internal/lease/ltxtruncate_test.go| index 1fb967802..7962a3714 100644 |
| --- a/internal/lease/ltxtruncate_test.go |
| +++ b/internal/lease/ltxtruncate_test.go |
| @@ -178,39 +178,71 @@ func TestTruncatedLTXNeverPanicsOrPasses(t *testing.T) { |
| } |
| } |
| -// failingReader gives n bytes of data and then err, never io.EOF: a |
| -// transfer that broke. |
| +// failingReader gives its data and then err: a transfer that broke. By |
| +// default err comes in a read of its own, again on every later read. With |
| +// withLast it comes in the same read as the last bytes, as a reader may |
| +// return it, and every later read is io.EOF: a decoder that took the bytes |
| +// and dropped the error would see only an early end. |
| type failingReader struct { |
| - data []byte |
| - err error |
| + data []byte |
| + err error |
| + withLast bool |
| + sent bool |
| } |
| func (r *failingReader) Read(p []byte) (int, error) { |
| - if len(r.data) == 0 { |
| - return 0, r.err |
| + if r.withLast && r.sent { |
| + return 0, io.EOF |
| } |
| n := copy(p, r.data) |
| r.data = r.data[n:] |
| - return n, nil |
| + if len(r.data) > 0 { |
| + return n, nil |
| + } |
| + if r.withLast { |
| + r.sent = true |
| + return n, r.err |
| + } |
| + if n > 0 { |
| + return n, nil |
| + } |
| + return 0, r.err |
| +} |
| + |
| +// assertReadErrorKept decodes and compacts data[:cut] through a reader |
| +// that then fails with its own error, and requires that error in the |
| +// chain, never io.ErrUnexpectedEOF. |
| +func assertReadErrorKept(t *testing.T, data []byte, cut int, withLast bool, what string) { |
| + t.Helper() |
| + reset := errors.New("connection reset by peer") |
| + err := decodeAll(t, what, &failingReader{data: data[:cut], err: reset, withLast: withLast}) |
| + if !errors.Is(err, reset) || errors.Is(err, io.ErrUnexpectedEOF) { |
| + t.Fatalf("%s: decode = %v, want the read's own error and no io.ErrUnexpectedEOF", what, err) |
| + } |
| + err = compactAll(t, what, &failingReader{data: data[:cut], err: reset, withLast: withLast}) |
| + if !errors.Is(err, reset) || errors.Is(err, io.ErrUnexpectedEOF) { |
| + t.Fatalf("%s: compact = %v, want the read's own error and no io.ErrUnexpectedEOF", what, err) |
| + } |
| } |
| // A read that fails partway through a file keeps its own error: the |
| // decoder names an early end only when the input itself ended, so a broken |
| -// transfer is never mistaken for a short file, wherever it breaks. |
| +// transfer is never mistaken for a short file, wherever it breaks and |
| +// whichever read delivers the error. The two boundaries are named first: a |
| +// read that fills a field exactly (the file header, the zero page header |
| +// that ends the page block) and returns the error with those bytes. |
| func TestLTXReadErrorIsKept(t *testing.T) { |
| _, files := multiPageReplica(t) |
| f := files[len(files)-1] |
| - reset := errors.New("connection reset by peer") |
| + for name, cut := range map[string]int{ |
| + "file header": ltx.HeaderSize, |
| + "zero page header": pageBlockEnd(t, f.data), |
| + } { |
| + assertReadErrorKept(t, f.data, cut, true, fmt.Sprintf("error with the last byte of the %s (%d bytes)", name, cut)) |
| + } |
| for cut := 0; cut < len(f.data); cut++ { |
| - what := fmt.Sprintf("read failing after %d of %d bytes", cut, len(f.data)) |
| - err := decodeAll(t, what, &failingReader{data: f.data[:cut], err: reset}) |
| - if !errors.Is(err, reset) || errors.Is(err, io.ErrUnexpectedEOF) { |
| - t.Fatalf("%s: decode = %v, want the read's own error and no io.ErrUnexpectedEOF", what, err) |
| - } |
| - err = compactAll(t, what, &failingReader{data: f.data[:cut], err: reset}) |
| - if !errors.Is(err, reset) || errors.Is(err, io.ErrUnexpectedEOF) { |
| - t.Fatalf("%s: compact = %v, want the read's own error and no io.ErrUnexpectedEOF", what, err) |
| - } |
| + assertReadErrorKept(t, f.data, cut, false, fmt.Sprintf("read failing after %d of %d bytes", cut, len(f.data))) |
| + assertReadErrorKept(t, f.data, cut, true, fmt.Sprintf("read failing with byte %d of %d", cut, len(f.data))) |
| } |
| } |
diff --git a/third_party/ltx/decoder.go b/third_party/ltx/decoder.go| index 1ac46e5cf..e37063101 100644 |
| --- a/third_party/ltx/decoder.go |
| +++ b/third_party/ltx/decoder.go |
| @@ -76,7 +76,11 @@ func (dec *Decoder) Close() error { |
| // Slurp the remaining data in to memory so we can use the ByteReader interface. |
| // CARLOS patch (sqlite-integrity-safe): the end of the input is expected |
| // here, so the raw reader is read; fewer bytes than a checksum is a file |
| - // cut short, not a panic, and the rest is read through inputReader. |
| + // cut short, not a panic, and the rest is read through inputReader. An |
| + // error the input returned with earlier bytes comes first. |
| + if err := dec.r.err; err != nil { |
| + return fmt.Errorf("read all: %w", err) |
| + } |
| remainingBytes, err := io.ReadAll(dec.r.r) |
| if err != nil { |
| return fmt.Errorf("read all: %w", err) |
| @@ -301,14 +305,28 @@ func (dec *Decoder) readLZ4Trailer() error { |
| // passes (header, page headers, lz4 page data, page index and its size, |
| // trailer), means DecodePage can return io.EOF only for the zero page header |
| // that ends the page block, and a cut file never passes for a complete one. |
| -// Every other error is returned unchanged. |
| -type inputReader struct{ r io.Reader } |
| +// Every other error is returned unchanged. An error that comes with bytes is |
| +// held and returned from then on: io.ReadFull drops an error that arrives |
| +// with the bytes that fill its buffer, and the next read could only say EOF. |
| +type inputReader struct { |
| + r io.Reader |
| + err error // held since the read that returned it |
| +} |
| func (in *inputReader) Read(p []byte) (int, error) { |
| + if in.err != nil { |
| + return 0, in.err |
| + } |
| n, err := in.r.Read(p) |
| if err == io.EOF { // io.EOF's contract: returned as itself, never wrapped |
| err = io.ErrUnexpectedEOF |
| } |
| + if err != nil { |
| + in.err = err |
| + if n > 0 { |
| + return n, nil |
| + } |
| + } |
| return n, err |
| } |