Skip to content

fix(cold-store-reaper): bound repair_dump candidate buffer (#6525) - #8415

Merged
dezren39 merged 4 commits into
mainfrom
queue/14417-6525-repair-dump
Nov 11, 2026
Merged

dezren39 merged 4 commits into
mainfrom
queue/14417-6525-repair-dump

Conversation

@dezren39

@dezren39 dezren39 commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Fixes #6525 — repair_dump: bound the candidate buffer

repair_dump.py is documented as a constant-memory streaming filter, but a candidate INSERT INTO sqlite_schema( statement whose terminating line never arrived was buffered whole — memory grew to the size of the remaining dump and nothing was emitted until EOF.

  • Candidates are now capped at MAX_CANDIDATE_BYTES (8 MiB default; REPAIR_DUMP_MAX_CANDIDATE_BYTES override for tests/operators). Real schema statements are orders of magnitude smaller.
  • Past the cap, buffered lines flush unchanged and the rest of the statement streams through line by line, with an explicit flush per line (this Python's stdout block buffer is 128 KiB, so write-through alone would still hold output back).
  • The statement end is still recognised in pass-through (same boundary test on the current line), so the following statement is processed — and repaired — normally.
  • An over-cap statement is not repaired: repair requires holding it. That matches the filter's existing contract of passing through anything it cannot verify; the caller's truncation guard judges the stream.

Tests (in TestDumpRepairFilter, subprocess against the real script):

Full pkgs/cold-store-reaper suite: 158 tests OK (1 skipped).

Agent: queue worker lane code-m, task 14417.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…r-cap statements through (#6525)

A candidate INSERT INTO sqlite_schema statement whose terminating
line never arrives was buffered whole, so the documented
constant-memory filter grew to the size of the remaining dump and
emitted nothing until EOF.

A candidate is now capped at MAX_CANDIDATE_BYTES (8 MiB default,
REPAIR_DUMP_MAX_CANDIDATE_BYTES override). Past the cap the buffered
lines are flushed unchanged and the rest of the statement streams
through line by line with an explicit flush per line (stdout block
buffering alone would hold it back); the statement end is still
recognised, so the following statement is processed normally. An
over-cap statement is not repaired - repair requires holding it -
which matches the filter existing pass-through of anything it
cannot verify.

Tests: over-cap candidate streams byte-identical and the following
broken statement is still repaired; over-cap candidate emits output
before EOF with stdin held open. Full cold-store-reaper suite: 158
tests OK.
@dezren39
dezren39 requested a review from a team as a code owner November 11, 2026 02:02
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 55 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c40670cf-88c0-4ad2-b6fb-2dbff42b41ae
Commits

Reviewing files that changed from the base of the PR and between 9b82fb9 and 8a110d6.

Files selected for processing: 3

  • pkgs/cold-store-reaper/scripts/repair_dump.py
  • pkgs/cold-store-reaper/tests/test_export_turso.py
  • pkgs/cold-store-reaper/tests/test_repair_dump_property.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T04:53:57.493173Z 8a110d6 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0fcd1e3a37

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…sed on the terminating line (#6525)

Codex P2 on PR #8415: the over-cap flush set passthrough=True unconditionally, so when a candidate first exceeded MAX_CANDIDATE_BYTES on its own terminating line, the FOLLOWING statement streamed through unrepaired even if below the cap and in need of quote repair. The flushed chunk is now checked with the same boundary test (ends_statement) and pass-through is enabled only when the candidate has not ended. Adds test_cap_crossed_on_terminating_line_does_not_leak_passthrough (fails on the unfixed code, passes with the fix); full test_export_turso suite 18/18.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 956bd24561

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +241 to +242
if line.rstrip(b"\r\n").endswith(STATEMENT_END) and ends_statement(
line, next_line

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve statement-boundary detection in pass-through mode

If a correctly escaped multiline candidate crosses the cap before its terminating line and a blank line follows the completed statement, pass-through mode never recognizes that boundary. Calling ends_statement with only the terminating line loses its valid-literal parsing path because that line does not start with SCHEMA_INSERT, and the blank lookahead also fails its fallback check. A subsequent broken schema INSERT is therefore emitted unrepaired, causing the rebuild in scripts/export-turso.sh to fail. Preserve enough parsing state to recognize valid statement completion independently of the next line, without retaining the entire over-cap statement.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 35aa1b7 (lane tmp-code-t, task 14417).

You were right: in pass-through mode the terminating line was tested with ends_statement() alone -- its valid-literal path needs the statement head (a lone terminating line does not start with SCHEMA_INSERT), and a blank following line fails its top-level fallback, so the boundary was never recognised and the next statement streamed through unrepaired.

Fix: pass-through now preserves exactly the parsing state you named, in O(1) -- a _LiteralTracker fed every streamed byte (seeded from the flushed buffer, or the over-cap first line for the single-line entry) tracks SQL string-literal state, where a '' pair inside a literal is an escaped quote and a lone ' closes it. A terminating line now ends pass-through when the tracker is outside a literal, OR-ed with the existing ends_statement() fallback so mis-escaped candidates (whose literal state is unparseable by design) keep their old boundary test. The over-cap statement itself is still never retained.

Regression test: test_over_cap_valid_candidate_end_recognised_before_blank_line -- correctly escaped over-cap candidate, terminating line, blank line, then a broken schema INSERT that must be repaired. Verified it fails on the pre-fix code and passes now; full cold-store-reaper suite 160 tests OK (1 skipped).

… own bytes (#6525)

Codex P2 on PR #8415 (round on 956bd24): in pass-through mode the terminating line was tested with ends_statement() alone, whose valid-literal path needs the statement head -- a lone terminating line has none, and a blank following line fails its top-level fallback, so a correctly escaped over-cap candidate followed by a blank line never ended pass-through and the next statement streamed through unrepaired. Pass-through now feeds every streamed line to an O(1) _LiteralTracker (seeded from the flushed buffer / over-cap first line) that tracks SQL string-literal state ('' = escaped quote) and recognises the end from the statement's own bytes, OR-ed with the existing ends_statement fallback for mis-escaped candidates. Regression test test_over_cap_valid_candidate_end_recognised_before_blank_line fails on the old code and passes now; full cold-store-reaper suite 160 tests OK (1 skipped).

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 35aa1b7ee3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +377 to +380
filler = (b"x" * 100 + b"\n") * 100 # 10 KB, no terminator line
data = opener + filler + b"');\n" + b"\n" + BROKEN_SCHEMA_INSERT
expected = opener + filler + b"');\n" + b"\n" + ESCAPED_SCHEMA_INSERT
self.assertEqual(self.run_filter_capped(data, 4096), expected)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add property-based coverage for the streaming parser

The new literal tracker and capped-buffer transitions introduce a parser/state machine, but the added coverage consists entirely of fixed examples. This violates AGENTS.md's explicit requirement to cover meaningful parser/state-machine invariants with property tests rather than examples alone. Add generated statements with varied escaping, line boundaries, and cap positions, asserting that over-cap statements remain byte-identical and subsequent below-cap statements are still repaired; the existing buffered parser can serve as a boundary-detection oracle for correctly escaped statements.

AGENTS.md reference: AGENTS.md:L41-L45

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8a110d6 (lane tmp-code-t, task 14417).

Added property-based coverage per the repo testing doctrine (AGENTS.md L41-45) and the pkgs/deploy precedent: pkgs/cold-store-reaper/tests/test_repair_dump_property.py is a stdlib seeded generator (no hypothesis in this repo's Python tooling; fixed seed 6525, 300 cases) that generates correctly escaped statements with varied escaping, line boundaries, interior boundary-bait lines (lines ending in ');, lines that parse standalone as complete schema INSERTs, comment lines), blank lines after the statement, and cap positions from 64 bytes to just under the statement size. The oracle is a by-construction model: the over-cap statement passes byte-identical and the following below-cap broken statement is repaired to its escaped form. The one documented heuristic outside the model (a candidate ends where a new top-level statement follows) is excluded by construction and named in the docstring.

Oracle check: 43 of the 300 generated cases fail on the pre-fix head 35aa1b7; all 300 pass now. Full cold-store-reaper suite: 162 tests OK (1 skipped).

Comment on lines +280 to +281
if line.rstrip(b"\r\n").endswith(STATEMENT_END) and (
tracker.outside_literal or ends_statement(line, next_line)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid parsing an interior line as a standalone statement

For a correctly escaped over-cap schema INSERT, this fallback can end pass-through while the tracker is still inside the outer literal. For example, a SQL block comment inside a CREATE TABLE definition can contain INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql) VALUES(1,2,3,4,''''); followed by a blank line: ends_statement(line, next_line) parses that embedded line independently and returns true. A later comment line containing the same INSERT prefix with VALUES(1,2,3,4,''X''); is then treated as a new repair candidate, causing quote rewriting inside the otherwise valid outer statement and breaking the SQLite rebuild. Previously, boundary detection examined the complete outer candidate. Restrict this fallback to lookahead/EOF detection rather than standalone parsing of the current interior line.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8a110d6 (lane tmp-code-t, task 14417).

You were right: OR-ing ends_statement() let its valid-literal path parse the current interior line standalone, ending pass-through while the outer literal was still open. The pass-through end test now uses only the tracker (outside-literal, from the statement's own bytes) OR the lookahead half of ends_statement -- factored out as _next_line_starts_new() and shared by both: next line starts a new top-level statement, or EOF. The current line is never parsed standalone in pass-through mode.

Regression: test_over_cap_interior_insert_line_is_not_a_boundary builds exactly your shape (interior line that parses as a complete schema INSERT, blank line, later interior INSERT-prefixed line, tail below the cap so repair_chunk would run on it). Verified it fails on 35aa1b7 (the later line's quotes were doubled: ''X'' became '''X''') and passes now. Full suite 162 tests OK (1 skipped).

…only; add property coverage (#6525)

Codex round on 35aa1b7, two findings: (P2) the pass-through end test OR-ed ends_statement(), which parses the current line standalone -- an interior line that itself parses as a complete schema INSERT ended pass-through while the outer literal was still open, and a later interior INSERT-prefixed line was then repaired as a new candidate, corrupting the valid outer statement. The fallback is now restricted to its lookahead half (_next_line_starts_new, shared with ends_statement): tracker-outside-literal OR next-line-starts-new OR EOF -- never a standalone parse of the current line. Regression: test_over_cap_interior_insert_line_is_not_a_boundary (fails on 35aa, passes now). (P1) property-based coverage per the repo testing doctrine: tests/test_repair_dump_property.py, stdlib seeded generator (seed 6525, 300 cases) varying escaping, line boundaries, interior boundary-bait lines, blank lines and cap positions, with a by-construction model oracle (over-cap valid statement byte-identical + following broken statement repaired). 43 of 300 cases fail on 35aa; all pass now. Full cold-store-reaper suite: 162 tests OK (1 skipped).
@dezren39
dezren39 merged commit 7f4a2b3 into main Nov 11, 2026
1 of 9 checks passed
@dezren39
dezren39 deleted the queue/14417-6525-repair-dump branch November 11, 2026 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant