Skip to content

cli: mark the resume of a stopped run in a desired-state dry run - #693

Open
Kiran01bm wants to merge 1 commit into
mainfrom
kiran01bm/pd130-desired-dry-run-resumes
Open

Kiran01bm wants to merge 1 commit into
mainfrom
kiran01bm/pd130-desired-dry-run-resumes

Conversation

@Kiran01bm

Copy link
Copy Markdown
Collaborator

A desired-state dry run now marks its first statement with resumes when the run would resume a stopped copy-and-swap run, as the imperative dry run already does.

Why

Copy-and-swap changes a table by building a new copy and swapping it in; a stopped run leaves its progress behind, and the next run on that table resumes it. With a desired schema file, the real run already resumes that work, but its dry run showed the statement as if it would start fresh, with no sign that a resume was coming. A reviewer reading the plan could not tell the run would pick up from where it stopped. Now the plan says so, and the guide's caveat about it is gone.

What

  • previewDesired calls a new markDesiredResume after the stopped-run check admits the plan. It asks migrate.ResumesStoppedRun whether statement 1 is the stopped run's, and if so marks it.
  • plan.ResumeStoppedRun now marks the first statement of a report of any length (before, it allowed only one statement). The whole plan becomes execute only if every statement executes, so a refused later statement keeps it refused. The imperative path is unchanged.
  • The fingerprint stays the derived plan's, which is the one the run pins. resumes stays out of the fingerprint.
  • Text output says the run "will resume the copy-and-swap run … stopped in phase X".
  • Docs: docs/schemabot-integration.md drops the caveat; docs/plan-report.md's resumes row covers desired-state plans.
  • Tests: an integration test stops a live retype of orders.qty in catching_up and at the cutover save, and checks the desired dry run reports the matching resumes.phase. Unit tests cover the multi-statement marking and the text output.

A run stopped after its swap committed is not covered here because it cannot happen in this path: the live table already has the desired shape, so the desired plan does not contain the stopped statement and the run is refused with a pointer to migrate --alter.

Before / after

A live retype of orders.qty to bigint stops at its cutover save. The same desired file (with qty bigint) is then dry-run.

input: pg-sprite migrate --desired schema.sql --dry-run --copy-and-swap --json

Before
  stopped-run check ──▶ admitted as the resume of the stopped run
  statement 1       ──▶ ALTER TABLE orders ALTER COLUMN qty TYPE bigint
                        route: copy-and-swap, resumes: (absent)
  reader sees       ──▶ a fresh copy-and-swap

After
  stopped-run check ──▶ admitted as the resume of the stopped run
  statement 1       ──▶ ALTER TABLE orders ALTER COLUMN qty TYPE bigint
                        route: copy-and-swap,
                        resumes: { phase: cutover, mode: live, last_saved: … }
  reader sees       ──▶ the run picks up the stopped run at cutover

The desired dry run showed the stopped run's statement against the
table's current shape with no resumes; mark it as the imperative plan does.
@Kiran01bm
Kiran01bm marked this pull request as ready for review December 2, 2026 02:31
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@aparajon

aparajon commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

🤖 Review 1/2 (correctness) of 6fa6806: 0 blocking, 4 non-blocking.

Inspected the whole change at 6fa68063e5445351ca182c636d0636925aa67d73 (+230/-30, 8 files) against the code it relies on: how the desired-state dry run reads a stopped run, what the real run does with the same row, and the imperative dry run it copies. CI at the time of posting: all 18 checks green, including test (PostgreSQL 14) to test (PostgreSQL 18, other packages), unit tests (no Docker; docs guards), lint, Supabase compatibility, smoke and all-green. I ran no test and no build; I worked from the diff and the files at the head.

What the change does: a desired-state migrate --dry-run whose plan starts with the statement a stopped copy-and-swap run recorded now marks that first statement as the resume (resumes, the copy-and-swap route and backend, execute), as the imperative dry run already does. markDesiredResume runs after AdmitPlanStoppedRun (#L142-L153) and calls migrate.ResumesStoppedRun on the first planned statement; plan.ResumeStoppedRun (plan.go#L663-L685) now accepts a plan of any length and marks Statements[0], and the report goes execute only once every statement does.

Invariants (docs/invariants.md): ST-2 (the dry-run doors end in the refusal the apply gives, beside ResumesStoppedRun): upheld for every case but the one in finding 1. ST-3 (resume-mode-mismatch): the PR does not extend its dry-run forecast to the desired door, which is finding 1. ST-6 (preflight writes nothing): upheld; the added reads are the checkpoint row and two catalog existence probes, with no write and no lock beyond what the existing admission already takes. OC-1 (fail closed): upheld except as finding 1 describes.

Non-blocking

  1. A desired-state dry run marks a run stopped in offline mode as "will resume", and the desired-state run refuses it as resume-mode-mismatch. ResumesStoppedRun returns the run's mode but never judges it (#L285-L297, resumeMode). The imperative dry run judges it first: AdmitCopySwapPreflight calls refuseOtherModeRun for a resume before the plan is marked, and TestMigrateDryRunRefusesAnotherStatementOverAStoppedCopySwapRun pins that a live dry run of an offline-stopped run is refused (#L166-L176). The desired door's copy-and-swap admission, AdmitPlanCopySwapPreflight, has no such call (refuseOtherModeRun is reached only from dry_run_copy_swap.go and migrate.go#L435), and desired-state execution forbids --block-writes (migrate_desired.go#L46-L47), so the run always resumes in live mode. A run stopped by migrate --alter --block-writes and then met by migrate --desired --dry-run therefore gets a plan that exits 0 and says will resume the copy-and-swap run of this statement stopped in phase verifying (writeResumeNote), with resumes.mode: offline, while the run refuses statement 1 at migrate.go#L435 with nothing executed. This fails closed, so I do not count it as blocking: no write happens and the refusal names the way out. The exit 0 for this case predates the PR, but before it the plan made no claim about a resume, and now it makes a false one. Reasoned from the code, not run. The fix is one call: in markDesiredResume, when ResumesStoppedRun says it resumes and the mode is offline, refuse as AdmitCopySwapPreflight does (an exported wrapper over refuseOtherModeRun would serve both doors). The note could also name the mode, since the JSON already does.
Test case: a desired-state dry run over an offline-stopped run (not run here)

Beside TestMigrateDesiredDryRunPlansTheResumeOfAStoppedCopySwapRun, using the same fixtures: stop an offline retype with f.stopRetypeAt at checkpoint.PhaseVerifying (the PR's test passes false for the last argument; I take true to be the blocked-writes mode, which I did not confirm), or reuse stoppedOrders(t) from stopped_run_integration_test.go on a server with wal_level=logical, then run desiredCommand(t, f.url, f.schema, desired, "--dry-run", "--copy-and-swap", "--free-disk", "1TiB") with the same desired file. Expected after the fix: the command ends in verdict.ErrRefused with cause schemachange.CauseResumeModeMismatch and the row untouched. Today I expect it to exit 0 with resumes.mode == "offline". I did not check the fixture's exact signature; it follows the imperative test linked above.

  1. markDesiredResume relies on the marked statement already being the one the fingerprint covered, and nothing checks it. ResumeStoppedRun rewrites the route, backend, disposition, exec_sql and execution, all of which the fingerprint hashes, and the CLI then keeps the derived fingerprint unchanged on purpose, since the run recomputes it from the unmarked plan and pins that (migrate_desired.go#L158-L166). That holds only when the derived first statement is already copy-and-swap, execute and has no exec_sql. The integration test asserts exactly that (ComputeFingerprint() == Fingerprint) for its one fixture. I could not construct a case where the derived statement is routed otherwise (the stopped statement's shape is not live before the swap commits, and after it the plan no longer contains the statement), so this is a guard, not a bug. The doc line says the exception in plain words (plan-report.md#L139). A guard that makes it true by construction would fail closed (OC-1): return an invariant error from markDesiredResume when the statement is not already copy-and-swap/execute with no exec_sql, so a future router change cannot ship a plan whose fingerprint disagrees with its own statements without anyone noticing.

  2. The "caller's statements are copied" comment is true of the slice, not of what it holds. slices.Clone(report.Statements) copies the statement structs, but plan.ResumeStoppedRun then calls withdrawSaferAdvice, which writes through the shared Decisions slices (plan.go#L863-L868). RefuseDestructive clones each statement's Decisions for the same reason (desired.go#L26-L29). It changes nothing today, because the only caller reassigns the report and drops the old one (migrate_desired.go#L330), but the comment promises more than it does. Clone Decisions of the first statement too, or narrow the comment.

  3. The preview reads the row twice, without a lock, and the two reads can disagree. AdmitPlanStoppedRun and ResumesStoppedRun each call findStoppedRun (#L60-L83). A concurrent resume that finishes between them leaves a plan that was admitted as a resume but not marked, which is what the dry run printed before this PR; the reverse (a run that stops between them) is unmarked the same way. Both are benign for a forecast the run re-reads under the table's lock, as the resumes doc says. Nothing to change unless a single read feeds both calls, which would also remove a round trip.

Verified

  • Detection reads only the checkpoint row and the catalog: findStoppedRun inspects the row, then probes pg_class by name for the shadow or retained source only for a row it cannot judge (#L60-L129). No write, no table lock.
  • Classification: a missing row, a missing store, or a terminal (done) row is not a stopped run (found is !cp.Phase.Terminal(), #L88), so a completed run is never marked; an abandoned row that a role cannot judge is an unjudged run, which AdmitPlanStoppedRun refuses ahead of markDesiredResume (#L334-L347), so it is never marked either; a statement that is not the recorded one blocks, so it is refused before marking.
  • Marking is keyed on resumedBy (statement.SameOpsExceptTarget on the parsed statements), the comparison Run makes when it resumes (migrate.go#L404-L407), so the dry run's identity test is the run's.
  • Additive output: resumes is an existing omitempty statement field, format_version stays at 49, no other field or exit code changes, and plan.ResumeStoppedRun only loosens its input check from "exactly one statement" to "at least one". The text change moves one existing note into writeResumeNote (dryrun_text.go#L341-L344) and calls it from the three copy-and-swap branches (#L369-L371, #L379-L381, #L392-L394); the last is reachable only when the first statement is marked but not routed to copy-and-swap, which finding 2 says I could not construct.
  • Marking sits after AdmitPlan, the route and preflight admission, the partitioned-key and foreign-key admissions and AdmitPlanStoppedRun, so every refusal the desired dry run already gave still ends the preview first. A plan with other statements keeps their dispositions (TestResumeStoppedRunMarksTheFirstStatementOfAPlan).
  • Mutation table (by reading; the integration tests need a live PostgreSQL, and the PR's test (PostgreSQL 14) to test (PostgreSQL 18, other packages) legs passed at this head):
Mutant Caught by
markDesiredResume not called the integration test's ps.Resumes check
marks the wrong statement (Statements[len-1]) TestResumeStoppedRunMarksTheFirstStatementOfAPlan
report forced to execute despite a later refusal the same test's DispositionRefuse assertions
ResumesStoppedRun handed a non-matching statement no test marks a non-matching first statement and expects it left alone; blocks refuses it earlier, so I think unreachable
offline-stopped run marked survives: no test (finding 1)
fingerprint recomputed after marking the integration test's equality check passes either way (finding 2)

This review was generated by Claude Code (claude-sonnet-5-5).

@aparajon

aparajon commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

🤖 Review 2/2 (OSS adoption and integration) of 6fa6806: 0 blocking, 4 non-blocking.

Read the change from two seats: a host that renders migrate --desired --dry-run output or calls the pkg/migrate and pkg/plan helpers to preview a run (the guide's adapter), and an operator who reads the dry-run text before deciding to run. Nothing is removed from the machine-readable plan: resumes is an existing omitempty statement field, so a desired plan that had no resumes before now carries it on its first statement, format_version stays at 49 (correct, since no field or hashed value is added and the fingerprint is unchanged), and exit codes do not change. A consumer that ignores resumes sees the same plan, except that the first statement's route, backend, exec_sql and execution now read as the resume rather than as the route against the table's current shape; the guide's resumes row says so.

Non-blocking

  1. A host cannot tell from the docs that a desired-state plan's resumes.mode: offline is a plan the run will refuse. The guide now says a desired dry run "is admitted as a resume, and its plan marks that first statement the same way" (schemabot-integration.md#L1624-L1626), and the resumes row says mode is "which a resume must match" (plan-report.md#L139). Desired-state execution takes no --block-writes, so only live can resume through it; an offline run is resumed by migrate --alter --block-writes on its own statement. That is finding 1 of the first review seen from the adopter's side: until the desired door refuses the offline case, the one sentence a host needs is "a desired-state plan whose resumes.mode is offline is refused at run time as resume-mode-mismatch; resume it with migrate --alter --block-writes". The guide already says the offline resume needs the acknowledgement and a mismatch is refused (the paragraph around #L1490); the new desired-state sentence should point there. If the code is fixed as suggested, the sentence reduces to "a desired-state dry run refuses it as the run does".

  2. The Fingerprint section does not mention the desired-state exception. plan-report.md#L383-L388 tells a consumer that "an executor recomputes it at apply time and refuses on mismatch". The new text sits only in the resumes table row: a desired-state dry run keeps the derived plan's fingerprint, "the one a run recomputes and pins". A consumer that reads the Fingerprint section and recomputes the digest from the statements it received would, in any plan where marking changed a hashed field, compute a different value. In the tested case the two are equal. One sentence under Fingerprint, or a link from there to the resumes row, puts the rule where a consumer looks for it; the guard in finding 2 of the first review would let the sentence say "equal by construction".

  3. ST-2's *Enforced:* text still describes ResumesStoppedRun as the imperative dry run's read. The entry says the dry-run doors "end in the refusal the apply gives, beside ResumesStoppedRun, which reads the same row to plan the resume of the stopped statement and runs nothing" (invariants.md#L1291-L1294). That is still true; the desired door is now a second caller (markDesiredResume). The change upholds ST-2 and needs no new entry, so this is a pointer update: one clause saying both dry runs plan the resume with it, and, if the mode judgement is added, naming the desired door beside the imperative one in ST-3's resume-mode-mismatch text (#L1469-L1479). TestEveryDoorChecksForAStoppedRunBeforeItExecutes walks pkg/migrate only, so the CLI's new caller is covered by the integration test alone.

  4. plan.ResumeStoppedRun is an exported function whose contract widened, and its doc line in the guide does not say so. It used to refuse any report that did not hold exactly one statement and now marks Statements[0] of a report of any length (plan.go#L663-L685). A host that called it on a multi-statement report and relied on the error as a guard now gets a mark on the first statement instead. That is additive and the Go doc comment says it, but the guide's sentence is still "plan.ResumeStoppedRun marks its report" (#L1627-L1629). "marks the first statement of its report" tells an adapter which statement it must have passed ResumesStoppedRun for.

Overlap with pg-sprite#691

#691 (dry-run planning of an online partition create) is stacked on kiran01bm/partition-of-route, not on main, and touches five of the same files. There is no semantic conflict: #691 adds an online-create-partition execution and a plan.RouteOnlineCreatePartition, and a CREATE TABLE ... PARTITION OF is never a copy-and-swap statement, so neither change reads the other's state. #691's hashesExecution change keys on execution, which ResumeStoppedRun clears, but a resumed statement is a retype or similar, never a partition create. Textual overlap:

What reads well for adopters

  • The integration test's comment describes one concrete case (a live retype stopped on orders, then the desired file whose first statement is that retype) in the repository's usual style, so a reviewer can follow what the dry run now says without running anything.
  • Reusing ResumesStoppedRun and ResumeStoppedRun for the second door, rather than a desired-specific copy, keeps the two dry runs from drifting, and the imperative text path now shares writeResumeNote with the desired one instead of repeating the sentence three times.
  • The integration test pins three things an adopter cares about: the dry run writes nothing (the row's phase and UpdatedAt are unchanged), the resume names when the row was last saved, and the statement keeps the derived plan's identity.
  • The new text and JSON are pinned in unit tests that need no database (TestDesiredDryRunTextNamesTheResumeOfAStoppedRun, TestResumeStoppedRunMarksTheFirstStatementOfAPlan), which run in the unit tests (no Docker; docs guards) job.

This review was generated by Claude Code (claude-sonnet-5-5).

@aparajon aparajon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Stamping with comments: 0 blocking, 8 non-blocking (see the review comments above).

This stamp was left by Claude Code (claude-sonnet-5-5).

@JashLal

JashLal commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

🤖 I reviewed the complete eight-file diff at 6fa68063e5445351ca182c636d0636925aa67d73, including the desired-state admission, first-statement annotation, multi-statement disposition, fingerprint, renderer, and tests.

P1 · The preview can promise an offline resume that apply refuses. A stopped offline run can match the first desired statement on a server that supports logical decoding. The annotation passes that offline checkpoint to ResumeStoppedRun and can produce an executable plan that says it will resume. Desired mode rejects --block-writes, so the live runner refuses the same checkpoint with resume-mode-mismatch. The preflight already missed this mismatch, and the new annotation makes the promise explicit. Check the checkpoint mode against the mode apply will select, and cover this offline checkpoint alongside the existing live cases. The refusal can point operators to the imperative resume with --block-writes. Resume annotation

GitHub reported all 18 checks successful. I did not run local tests. Direct Builderbot lookup failed to resolve its host. GitHub showed no current-head Builderbot check or review, and I did not independently enumerate unresolved inline threads.

@JashLal JashLal left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Approved by Codex on Jash’s behalf after reviewing the current head, as instructed. See my substantive review comment for findings and validation limits; approval does not resolve those findings.

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.

3 participants