diff --git a/docs/adr/0067-commit-history-and-rollback-for-ai-authoring.md b/docs/adr/0067-commit-history-and-rollback-for-ai-authoring.md index a970018c7bc..7cce092409f 100644 --- a/docs/adr/0067-commit-history-and-rollback-for-ai-authoring.md +++ b/docs/adr/0067-commit-history-and-rollback-for-ai-authoring.md @@ -1,6 +1,6 @@ # ADR-0067: Commit history and rollback for AI authoring — turns become atomic, revertible commits -**Status**: Accepted (2026-06-24; completed 2026-07-16) · **Amended** (2026-08-06, #5351/#5696 — the D2 join is now distinguishable (`owned`), and Decision-2's atomicity is enforced as a ONE-datasource promise, with audit rows carved out and possibly orphaned; see the Amendment at the end) — fully implemented: commit grouping (`sys_metadata_commit`), `revertCommit`/`rollbackToPackageCommit`/`listCommits`, REST routes; **Decision-2 landed via #3066**: `publishPackageDrafts` runs every promotion + the commit record inside ONE `engine.transaction()` (two-phase — side effects post-commit), so a commit cannot half-land; `engine.transaction()` joins ambient transactions to make nested repository writes participate. Locked by `protocol-publish-package-drafts.test.ts` (all-or-nothing + rollback tracking) and `engine-ambient-transaction.test.ts`. **Letter index** (2026-08-29, #12786): the Decision headings are relabeled `1.`–`6.` → `D1`–`D6` so `Dk` citations verify — recognition of the record's own numbering (this status line and the Amendment already say "D2"); no decision is renumbered, reordered, or rewritten. · **Amended** (2026-11-12, #32396/#32480 — Decision-2's atomicity holds where the engine can roll back (`engineCanRollBack`), which is not the same line as "a SQL driver"; elsewhere a package publish is judged first and undone by compensation, best effort; see the 2026-11-12 Amendment at the end). · **Amended** (2026-11-14, #32504 — Decision-3's revert doors are not atomic across items: `revertCommit` reverts item by item and reports `reverted[]` and `failed[]`, and `rollbackToPackageCommit` runs one `revertCommit` per commit; neither opens a transaction around the whole; see the 2026-11-14 Amendment at the end). +**Status**: Accepted (2026-06-24; completed 2026-07-16) · **Amended** (2026-08-06, #5351/#5696 — the D2 join is now distinguishable (`owned`), and Decision-2's atomicity is enforced as a ONE-datasource promise, with audit rows carved out and possibly orphaned; see the Amendment at the end) — fully implemented: commit grouping (`sys_metadata_commit`), `revertCommit`/`rollbackToPackageCommit`/`listCommits`, REST routes; **Decision-2 landed via #3066**: `publishPackageDrafts` runs every promotion + the commit record inside ONE `engine.transaction()` (two-phase — side effects post-commit), so a commit cannot half-land; `engine.transaction()` joins ambient transactions to make nested repository writes participate. Locked by `protocol-publish-package-drafts.test.ts` (all-or-nothing + rollback tracking) and `engine-ambient-transaction.test.ts`. **Letter index** (2026-08-29, #12786): the Decision headings are relabeled `1.`–`6.` → `D1`–`D6` so `Dk` citations verify — recognition of the record's own numbering (this status line and the Amendment already say "D2"); no decision is renumbered, reordered, or rewritten. · **Amended** (2026-11-12, #32396/#32480 — Decision-2's atomicity holds where the engine can roll back (`engineCanRollBack`), which is not the same line as "a SQL driver"; elsewhere a package publish is judged first and undone by compensation, best effort; see the 2026-11-12 Amendment at the end). · **Amended** (2026-11-14, #32504 — Decision-3's revert doors are not atomic across items: `revertCommit` reverts item by item and reports `reverted[]` and `failed[]`, and `rollbackToPackageCommit` runs one `revertCommit` per commit; neither opens a transaction around the whole; see the 2026-11-14 Amendment at the end). · **Amended** (2026-11-15, #33850 — after PR #33805 and PR #33979, the faults a revert reads after an item's row write (the registry write-through's answer, a storage advisory, the projector's `success: false`, the heal's answered stop where a row was removed, a later throw) are named in `failed[]` with the row state and make `success` false, while a heal outcome that answers no stop is still not reported; the revert commit is written from the revert's own plan and records the version each row held, so reverting a revert restores; an item recorded as existing with no version to restore is named in `failed[]`; the doors are still not atomic across items; see the 2026-11-15 Amendment at the end). **Deciders**: ObjectStack Protocol Architects **Builds on / amends**: [ADR-0045](./0045-additive-materialization-and-visibility-gate.md) (**amended**: ADR-0045 keeps a *draft + human-confirm* gate on mutations as the safety mechanism; this ADR replaces *confirm-before* with *revert-after* for everything except irreversible data loss, and unifies the two authoring regimes under one primitive — the commit), [ADR-0027](./0027-metadata-authoring-lifecycle.md) (draft workspace — retained as a *review affordance*, demoted from *safety mechanism*), [ADR-0033](./0033-ai-assisted-metadata-authoring.md) ("AI never publishes — it drafts" → **AI commits; commits are revertible**), [ADR-0034](./0034-transactional-writes-and-ambient-transaction.md) (per-write transaction — **extended to span a whole turn**), [ADR-0038](./0038-build-verification-loop.md) (machine gate — runs per commit, before it lands) **Consumers**: `@objectstack/objectql` (commit grouping, atomic turn-apply, `revertCommit`, history query — built on the existing `sys_metadata_history` + `restoreVersion`), `@objectstack/runtime` + `@objectstack/rest` (commit/revert routes), `../cloud/service-ai-studio` (turn = commit; auto-commit policy; data-loss confirmation), `../objectui` (commit timeline + "revert to here") @@ -80,7 +80,7 @@ All metadata writes in a turn execute inside **one `engine.transaction()`**; any ### D3 — Rollback is first-class and append-only -- `revertCommit(commitId)` — restore every item the commit touched to its **pre-commit** body, atomically (reuse `restoreVersion` per item inside one transaction). Recorded as a **new forward commit** (`operation_type='revert'`), so history is never rewritten and a revert is itself revertible. *(Amended 2026-11-14: no transaction spans the items; the revert runs per item and reports `reverted[]` and `failed[]`; and reverting a revert commit restores nothing today — see the Amendment of that date at the end.)* +- `revertCommit(commitId)` — restore every item the commit touched to its **pre-commit** body, atomically (reuse `restoreVersion` per item inside one transaction). Recorded as a **new forward commit** (`operation_type='revert'`), so history is never rewritten and a revert is itself revertible. *(Amended 2026-11-14: no transaction spans the items; the revert runs per item and reports `reverted[]` and `failed[]`; and reverting a revert commit restores nothing today — see the Amendment of that date at the end.)* *(⚠️ Amended 2026-11-15, #33850: "reverting a revert commit restores nothing today" no longer holds. Since PR #33805 a revert commit records the version each row held, and reverting it restores — see the Amendment of 2026-11-15 at the end.)* - `rollbackToPackageCommit(commitId)` — revert the package's HEAD back through every commit after the target, as one transaction. This is the "一层一层回退" the founder described — **commit-granular, not per-artifact** (§Resolved-1). *(Amended 2026-11-14: not one transaction; one `revertCommit` per commit, each step's failures collected — see the Amendment of that date at the end.)* - Revert restores **metadata** deterministically. Its effect on **data** is governed by §5. @@ -364,6 +364,20 @@ whether a revert should stay this way (below). ### What the doors do +> ⚠️ **Amended 2026-11-15 (#33850 — PR #33805, PR #33979).** This section is +> the reading at `ba35dfd7`, and `main` has moved on four points since. ⛔ On +> these the text below is no longer a statement of what the doors do: the +> faults the revert reads after an item's row write (now named in `failed[]`, +> and `success` is false); the skipped item (now named in `failed[]`, nothing +> written); what the revert commit is written from (the revert's own plan, +> with the version each row held); and "reverting it still restores nothing" +> (reverting a revert commit recorded since PR #33805 restores). In the +> closing paragraph, the sentence "A fault after an item's row write is not +> named there, and a skipped item is not named at all." goes with them. Still +> true: on the soft-remove limb, a registry heal outcome that answers no stop +> is not reported, and the item is in `reverted[]`. The rest of the section +> stands. See the Amendment (2026-11-15, #33850) at the end. + Read from the code on `main` at `ba35dfd7` — a reading of that date, not part of the decision: @@ -427,6 +441,11 @@ revert side — reported, not prevented. D3's "a revert is itself revertible" holds, and **whether a fault after an item's row write should reach `failed[]`**. Both are code questions; this record states what the code does today. + ⚠️ **Amended 2026-11-15 (#33850 — PR #33805, PR #33979).** The code has + answered both: a revert commit records the version each row held, and the + faults the revert reads after an item's row write reach `failed[]`. A heal + outcome that answers no stop still does not. See the Amendment (2026-11-15, + #33850) at the end. The first bullet, atomicity, is still open. - It changes nothing in Decision-2, the package-publish door, or the 2026-08-06 carve-out (audit rows written during rolled-back work still remain). @@ -441,3 +460,191 @@ is itself revertible; each rollback step records its revert commit), this record states the code. Pinned by `protocol.revert-stored-type-canonical.test.ts` ("reverts the neighbours of a refused item and reports the mix honestly"). + +> ⚠️ **Amended 2026-11-15 (#33850).** Two of the three places named above, +> where the docblocks said more than the code, no longer do: a revert is +> revertible in the code, and the rollback docblock now says each step +> attempts its revert commit, best effort. The first still says more than the +> code, for the outcomes the revert does not read. See the Amendment +> (2026-11-15, #33850) below. + +--- + +## Amendment (2026-11-15, #33850) — the faults a revert reads after an item's row write are now named in `failed[]`, and a revert commit records what it replaced + +The 2026-11-14 Amendment recorded what the two revert doors did on `main` at +`ba35dfd7`. Two code changes have landed since: PR #33805 (`568f214a`, +2026-11-15) and PR #33979 (`0bbb211f`, 2026-11-15). This amendment records +what the doors do now. It decides nothing. Decision-3 is unchanged, the doors +are still not atomic across items, and no text of the 2026-11-14 Amendment is +rewritten: the parts this one replaces are marked there and listed below. + +### What the doors do now + +Read from the code on `main` at `fb79d47c` — a reading of that date, not part +of the decision. The door is +`packages/metadata-protocol/src/protocol-package-commits.ts#revertCommit`. + +- **The faults the revert reads after an item's row write are named in + `failed[]`.** There are five. On the restore limb: the registry + write-through answering a refusal or a fault; the storage sync answering + any advisory (a refused sync, or column changes that were not applied; the + advisory still rides `advisories`); and the mutation projector answering + `success: false`. On the soft-remove limb, where a row was removed: the + registry heal answering a stop, and the projector answering + `success: false`. On either limb: a throw after the row write. Such an + item goes into `failed[]`, not `reverted[]`. Its entry names the row state + ("its row was removed from sys_metadata", or "its row was restored to the + body of version N") beside each step that did not land. The rule for + `success` is the one the 2026-11-14 Amendment records (true only when + `failed[]` is empty and at least one item was reverted), so one such item + makes the answer `success: false`. +- **Which PR reads which.** PR #33805 reads the storage advisory, the + projector, the later throw, and the write-through's answer, which at that + time carried only the registry's refusal of an `object`. PR #33979 adds + the rest. The restore limb now asks the write-through to answer its + faults (`answerFaults` on + `packages/metadata-protocol/src/protocol-registry-hydration.ts#RegistryWriteThroughRequest`), + so its two other branches (the `object_extension` registration, and the + overlay hydration every remaining type takes) answer a throw as well. The + heal answers its stop + (`packages/metadata-protocol/src/protocol-registry-hydration.ts#restoreArtifactRegistryView`, `#RegistryHealStop`), + and the soft-remove limb reads it only where it removed a row. The + exception the 2026-11-14 Amendment names for an `object_extension` item + still ends in `failed[]`, by the throw path, and its entry now names the + removed row. +- **What still does not reach `failed[]` after a row write.** + - Registry heal outcomes that answer no stop, on either limb. Among them, + on the soft-remove limb: the registry refusing to unregister a removed + `object` is caught and logged at `warn`, and the object stays + registered. A package-scoped refusal of the baseline read is logged at + `error`, with the entry retired or not. A degraded baseline read keeps + the entry. The heal's docblock calls the last two designed outcomes. + For a removed `object_extension`, a stored extension that cannot be + registered again is logged at `warn` + (`packages/metadata-protocol/src/protocol-class-head.ts#resyncStoredObjectExtensions`). + With no row removed, a stop the heal does answer is not reported + either. On either limb: the member views of a view container. Each + view the row no longer expands to is healed by + `packages/metadata-protocol/src/protocol-registry-hydration.ts#reconcileViewExpansions`, + which does not keep the heal's answer. The heal of a removed container + runs it, and so does the write-through of a restored one. + - The mutation event: it is emitted and its result is not read. + - The stored-expression fill, which runs after the revert commit is + written and reports on `advisories` only. + + An item with one of these is reported in `reverted[]`, and `success` does + not see it. For the heal outcomes, the 2026-11-14 sentences about the + registry heal stay the account: the outcome is not reported, the item is + in `reverted[]`, and the registry may keep serving what the revert moved + away from. +- **The revert commit is written from the revert's own plan**, not from + `reverted[]` (PR #33805). An item enters the plan when its row write + returns, whether or not it then reaches `reverted[]`, so a row that + changed under an item reported in `failed[]` is still on the commit + timeline. A soft-remove that found no row to remove enters the plan too, + recorded as `existedBefore: false`. An item refused or thrown before its + row write is in `failed[]` and not in the plan. +- **Each plan item records what the revert replaced** (PR #33805). Before + the row write, the revert reads the version of the committing package's + active row. The plan item carries that version as `prevVersion`, and + `existedBefore` is true exactly when the row was there. A read that fails + at that point fails the item into `failed[]` with nothing written. So + reverting a revert commit restores on both limbs: an item the revert + restored goes back to the version the revert replaced; an item the revert + removed is written again at the version it held; and an item the revert + wrote again because its row had been deleted is removed. Where the read + finds no version on a row that exists, the item is recorded without one + and takes the next bullet's path when the revert is reverted. +- **An item recorded as existing, with no version to restore, is named and + nothing is written for it** (PR #33805). It goes into `failed[]` before + any read or write, and the walk moves to the next item. The entry carries + no `code`; its text says the commit records no version to restore the item + to, and points at the item's own version history. Every revert commit + recorded before PR #33805 wrote its restored items that way (the + 2026-11-14 Amendment's "skipped" bullet), so reverting one of those now + names each of them in `failed[]` and answers `success: false`. The items + such a commit removed still go down the soft-remove limb, as that bullet's + parenthetical says. +- **The revert commit record is still best effort, and the rollback door's + docblock now says so** (PR #33805, for #33486). The code is as the + 2026-11-14 Amendment reads it: `recordPackageCommit` catches every error + and returns no record, and `revertCommit` then answers without + `revertCommitId`. The docblock on + `packages/metadata-protocol/src/protocol.ts#rollbackToPackageCommit` now + reads: "[#33486] That record is best effort: when the commit store is + unprovisioned or the write fails, nothing is recorded and the step's answer + omits `revertCommitId`, so the step is on no package-commit timeline and + cannot itself be reverted as a commit." The rollback itself is unchanged: + one `revertCommit` per `apply` commit newer than the target, newest first. + Each item a step reports in its own `failed[]` goes into the rollback's + `failed[]`, which now includes an item whose row the step wrote. + +So a partly reverted package now has one more shape, and it is reported: an +item in `failed[]` whose row **was** changed. Its entry says so, and says +that the stored row and what the process serves disagree until the failed +steps run again. + +### What this replaces in the 2026-11-14 Amendment + +- In "What the doors do", first bullet: the sentences from "What follows an + item's row write does not reach `failed[]`" to the end of the bullet, for + the faults the revert reads (listed above). For a heal outcome that + answers no stop, those sentences still hold. +- In the bullet "An item can also be skipped": "it appears in neither + `reverted[]` nor `failed[]`" (it is named in `failed[]`), and, for a revert + commit recorded since PR #33805, the account of what its plan holds and of + what reverting it restores. The bullet's parenthetical still holds for a + revert commit recorded before PR #33805. +- In "The revert commit": "it is written from `reverted[]`". The `success` + rule in that bullet stands. +- In "The revert commit itself is best effort": the last sentence, "Where + the record lands, reverting it still restores nothing". It still holds for + a revert commit recorded before PR #33805, as a named failure, not a + silent one. +- In the closing paragraph: "A fault after an item's row write is not named + there, and a skipped item is not named at all." A fault the revert reads + is named, and so is that item. The paragraph's last sentence stands. +- In "What this amendment does not decide", second bullet: both code + questions are answered by the code. A revert commit carries the versions + it replaced, and the faults the revert reads after an item's row write + reach `failed[]`. +- In the "Recorded by" paragraph: two of the three places where the + docblocks said more than the code. A revert is revertible, and the + rollback docblock no longer says each step records its revert commit. The + third still says more than the code: a revert that answers `success: true` + is one the process has acted on only as far as the faults the revert + reads. +- In D3's first bullet, the 2026-11-14 parenthetical's last clause, + "reverting a revert commit restores nothing today". TL;DR 3's parenthetical + and D3's second one still hold. + +The rest of the 2026-11-14 Amendment stands: the walk is per item, no +transaction spans the items or the rollback's steps, a failed item's +neighbours are still reverted, and the half-land is reported, not prevented. + +### What this amendment does not decide + +- **Whether a revert should be atomic** is still open, exactly as the + 2026-11-14 Amendment leaves it. Nothing in PR #33805 or PR #33979 opens a + transaction around the items. +- **Whether a heal outcome that answers no stop should reach `failed[]`.** + This record states what the code does today. +- It changes nothing in Decision-2, the package-publish door, or the + 2026-08-06 carve-out. + +**Recorded by**: the triage of #33850, dispatched as a text correction: this +record follows the code; no historical text is rewritten. **Implemented in** +`packages/metadata-protocol/src/protocol-package-commits.ts` +(`revertCommit`, `rollbackToPackageCommit`) and +`packages/metadata-protocol/src/protocol-registry-hydration.ts` by PR #33805 +(`568f214a`) and PR #33979 (`0bbb211f`). Pinned by +`packages/metadata-protocol/src/protocol.revert-commit-honest-outcome.test.ts`: +"[#33563] revertCommit reports a fault after an item's row write in +failed[]" (which also pins the plan, and "rollbackToPackageCommit carries the +step's post-row-write fault and answers success false"), "[#33564] reverting +a revert commit restores what the revert replaced" (restore limb, soft-remove +limb, and "an item recorded as existing with no version is named in failed[], +never skipped, and nothing is written"), "[#33851] revertCommit reports a +non-object write-through fault in failed[]" and "[#33851] revertCommit +reports a removed item whose registry heal stops in failed[]".