Skip to content

fix(drivers): retire the activation ledger's old two-part unique on upgraded MongoDB and MariaDB databases - #31831

Merged
objectstack-fleet[bot] merged 3 commits into
mainfrom
claude/project-thread-rrtysc
Nov 9, 2026
Merged

objectstack-fleet[bot] merged 3 commits into
mainfrom
claude/project-thread-rrtysc

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Requested by Steve · project thread

Fixes #31732

Clause-②: no
Metadata-protocol: none

维护者速读

  • 改了什么:升级来的 MongoDB 库在同步时会删掉激活台账旧的两段唯一索引(先确认三段新键和"无包行"保护索引都在,再删);MariaDB 现在也能建"无包行"保护索引(一个不可见的生成列加唯一键),于是 os migrate plan 会把旧索引报成安全替换。
  • 为什么改:S1(feat(core): key the activation ledger by (metadata_type, package_id, name) and bind package-less rows to their one holder #31727)把台账唯一键扩成 (metadata_type, package_id, name),但这两种升级来的库会留着旧的两段索引,史诗后续子卡第一次写第二个包的同名行就会被拒。本卡要在那之前把它安全换掉。
  • 风险与回滚:只删本驱动自己建、且已被更宽声明取代的唯一索引;无包行"每项一行"始终由存储强制(先建后删)。新列为 INVISIBLE,任何读都看不到。回滚即 revert;已删的旧索引不需要恢复(放宽键不丢行)。
  • 先看:packages/drivers/driver-mongodb/src/mongodb-schema.ts 的 retireWidenedUniques、packages/metadata-protocol/src/migrations/activation-ledger-packageless-index.ts 的 MariaDB 分支、packages/drivers/driver-sql/src/schema-drift.ts 的 parseHashShadowRowScope。
  • 两分钟验证:pnpm --filter @objectstack/driver-mongodb exec vitest run src/mongodb-activation-ledger-packageless.test.ts,预期 10 条通过(另 4 条真实 mongod 用例需 OS_TEST_MONGODB_MEMORY_SERVER_ENABLED=1)。
  • 需要你做的:无,席位落地。

What changes

  • MongoDB: a widening arm in schema sync (driver-mongodb, retireWidenedUniques). After the create loop, schema sync drops a unique an earlier declaration built once a declared key has widened it. It mirrors driver-sql's widenedUniqueReplacements and is create-before-drop. Every condition is required:
    • the declared index is unnamed, unique: true or 'global', with 2 to 8 fields;
    • the narrower index's fields are an in-order proper subsequence, under this driver's generated name (idx_<fields>_unique);
    • no current op asks for that name (field-level uniques included);
    • it is this driver's own shape: the NULL-distinct partial filter (driver-mongodb: field unique is built as a sparse index, so two explicit null values collide, unlike the NULL-distinct rule of driver-sql and driver-memory #29278) or the sparse shape before it, never an index with other options;
    • the wider unique is confirmed present, with this driver's shape;
    • when a gained field is not required, a partial unique on exactly the narrower fields stands, whose filter tests each such field for null. The ledger's guard uniq_sys_metadata_activation_packageless is built in the same loop. Without it the old index is KEPT.
    • One warn line per retired index names the collection, the old key and the new key. The plain read index over the old fields stays: it refuses nothing, and the planner still answers the ledger's (metadata_type, name) lookup with it (measured).
    • syncCollectionSchema takes an optional { logger }; the default writes through console.warn, as SqlDriver's default logger does. It is not exported from the package root.
  • MariaDB: the package-less guard (metadata-protocol, ensureActivationLedgerPackagelessIndex). MariaDB refuses both S1 forms (partial WHERE and functional key part, ER_PARSE_ERROR, measured on 11.8). The guard there is one ALTER TABLE: an INVISIBLE generated column uniq_sys_metadata_activation_packageless__hash VARBINARY(32) AS (CASE WHEN package_id IS NULL THEN unhex(sha2(concat(metadata_type, 0x1f, name), 256)) END) VIRTUAL INVISIBLE, plus UNIQUE KEY uniq_sys_metadata_activation_packageless over it.
    • MariaDB is told apart by SELECT VERSION(). An unreadable version takes the MySQL form, as before.
    • A second boot answers ER_DUP_FIELDNAME; the migration then adds the key alone, and ER_DUP_KEYNAME reads as in place.
    • Existing duplicates answer ER_DUP_ENTRY, reported as conflict with the listing query, and nothing is deleted (the S1 path).
    • The new statement builders are module exports only; the package root is unchanged.
  • driver-sql reads that guard as a partial unique (schema-drift.ts, sql-driver-introspection.ts). The carrier column is named as a driver-sql (MySQL): full-value UNIQUE on >768-char token columns is inexpressible on utf8mb4 — hash-shadow-key route for the four ruled cases (C half of the #11374 ruling) #11627 hash shadow (hashShadowColumnFor: index name + __hash), so the orphan-column pass already leaves it alone. On the S1 tree the index itself read as a DESTRUCTIVE orphan drop_index keyed (package_id, metadata_type, name), and the old two-part index stayed KEPT (measured). Now:
    • parseHashShadowRowScope reads a case when <col> is null then <digest> end expression's scope column. parseHashShadowKeyParts returns the digest's parts alone, so every reader (the differ, the duplicate decorator, the shadow-state check) sees (metadata_type, name).
    • resolveHashShadowKeys marks such a carrier partial: true. A partial index is runtime-managed (isSyncReproducibleIndex), so it is never an orphan.
    • hasPartialUniqueGuard compares the enforced key (enforcedIndexKey), so the carrier counts as the widening arm's guard. The old index then reads as one replace_unique_index, category safe, exactly as on SQLite and PostgreSQL.

Why

S1 (#31727) widened the ledger's unique key to (metadata_type, package_id, name). Two kinds of upgraded database keep the old (metadata_type, name) unique beside it:

  • a MongoDB collection, because schema sync only creates indexes and never drops one;
  • a MariaDB table, because MariaDB has neither form S1's guard needs, so the old index had to stay.

There, the first row a second package writes under an existing name is refused (E11000 or ER_DUP_ENTRY on the old index). The epic's later child cards write exactly those rows. This PR retires the old index safely before they land, and the package-less "one row per item" rule stays enforced by the store throughout.

Step 0 (measured on main 5b7269f, Docker in the card container: MariaDB 11.8.9, mongod 8.0.34)

Store Reading before this PR
MongoDB, upgraded collection old idx_metadata_type_name_unique stands beside the new key and the guard; a second package's row of a name is refused E11000 on the old index (package-less, alpha, beta all refused)
MongoDB, old index dropped by hand package-less second row refused by the guard; two packages admitted; one package twice refused by the new key
MariaDB, partial WHERE / functional key part ER_PARSE_ERROR both
MariaDB, INVISIBLE generated column + UNIQUE key builds; refuses a second package-less row on insert and on update to NULL; admits two packages once the old index is gone; absent from SELECT * and driver.find; drift on the S1 tree: one destructive orphan drop_index on the guard, old index still kept

Tests

  • driver-mongodb mongodb-activation-ledger-packageless.test.ts (shared harness file, new top-level describes):
    • recording Db, every lane, 8 cases: upgraded collection retires the old unique after both creates, with one log line naming collection, old key and new key; the pre-driver-mongodb: field unique is built as a sparse index, so two explicit null values collide, unlike the NULL-distinct rule of driver-sql and driver-memory #29278 sparse shape retired too; CONTROL fresh collection (nothing dropped, no log); CONTROL narrower unique still declared (kept); nullable gained field with no guard (kept); required gained field (retired); a same-name index this driver did not build, or a wider key not present (kept); a named declaration (claims nothing).
    • real mongod, opt-in: an upgraded collection synced twice through MongoDBDriver; old unique gone, new key + guard + read index stand; package-less second row refused, two packages admitted, one package twice refused.
  • driver-sql sql-driver-widened-declared-unique.test.ts (shared harness file): 4 pure cases (the scope and key parts of MariaDB's stored expression, an ordinary shadow unchanged, the row-scoped carrier counts as the guard, an unscoped carrier does not), plus a live MariaDB cell (MYSQL_CELL, skipped on MySQL with ctx.skip): upgraded table, plan = one safe replace and nothing else, apply with no allowDestructive, re-plan empty, refusals asserted as ER_DUP_ENTRY naming the index, the carrier absent from driver.find and SELECT *, a second boot draws no drift.
  • metadata-protocol activation-ledger-packageless-index.test.ts: new describe, 6 cases (carrier name = driver-sql's shadow name, the exact statement, MariaDB takes it and never the MySQL statement, second boot re-key path, conflict, MySQL still takes its functional form).
  • Forced test edits:
    • activation-ledger-packageless-index.test.ts: the case titled "a dialect with neither form (MariaDB) is unsupported" is retitled "a server with no form at all (MySQL before 8.0.13, version unreadable)". Its body is unchanged: MariaDB is no longer that case.
    • activation-ledger-packageless-index.live-dialects.test.ts: the MySQL/MariaDB case asserted unsupported on MariaDB. It now asserts created on both and, on MariaDB, that the carrier is absent from SELECT *. The header line saying MariaDB builds nothing is corrected.
    • mongodb-activation-ledger-packageless.test.ts: the shared mongod is stopped once in a module-level afterAll instead of in the first real-server describe's, so the second real-server describe can use it.

Acceptance notes

  • MySQL is unchanged: it keeps S1's functional key part (one extra SELECT VERSION() read before it). Its old index is a hash-shadow carrier that the widening arm never claims (S1's reading, untouched here).
  • MariaDB before 10.3 has no INVISIBLE column, answers unsupported, and keeps the old index (the S1 behaviour).
  • The memory driver stays the stated gap S1 recorded.
  • File surface: all inside the claim (driver-mongodb schema sync; driver-sql's MariaDB arm, i.e. the row-scoped shadow reading in schema-drift.ts and sql-driver-introspection.ts; the package-less migration; tests; the changeset).

Verification

Head f28a8b8 on base 5b7269f (S1's merge). Live servers ran in Docker in the card container: MariaDB 11.8.9, MySQL 8.4.13, mongod 8.0.34.

  • Package suites: driver-sql + driver-mongodb, 4194 passed / 224 skipped; metadata-protocol, 22616 passed / 29 skipped.
  • Touched files, live:
    • driver-sql widened file on MariaDB: 14 of 14.
    • The same file on MySQL 8.4: 13 passed, and the MariaDB cell is skipped as designed.
    • metadata-protocol unit + live-dialects on MariaDB: 20 passed. Only the PostgreSQL cases are skipped.
    • The live-dialects MySQL / MariaDB case on MySQL 8.4: green.
    • mongo ledger file with real mongod: 14 of 14 (without it, 10 passed and 4 skipped).
  • Dogfood, under the verify lock:
    • Files: activation-ledger-package-key, activation-ledger-actor, packaged-activation-ledger-reach, upgrade-18x-harness, upgrade-18x-index-equivalence.
    • Result: 5 files, 52 passed, 1 skipped.
    • The showcase_invoice schema-sync error lines in that log are sqlite-wasm's non-constant default, which this PR does not touch.
  • Full pnpm build: 119 of 119 tasks.
  • check:dual-build-cjs-loads: green (122 entries, 72 packages).
  • check:driver-conformance: 66 covered, 6 DEBT on head, and the same 66 and 6 on base.
  • eslint over the three packages, repo spelling (--no-inline-config): exit 0. None of the three has a lint script of its own (pnpm --filter … lint answers ERR_PNPM_RECURSIVE_RUN_NO_SCRIPT).
  • dispatch-gates --ran: 69 derived, 69 run, each with its exit code, 0 NOT-MEASURED.
    • Two gates first answered exit 3 on the shallow clone and were re-run green. Teardown self-test: after fetching its pinned ref. Dual-build: after the full build.

Ablations (scripts/ablation-replace.mjs; each was red, then restored with git diff HEAD empty):

# Mutation Red test
C1 introspection never marks the row-scoped carrier partial live MariaDB plan/apply/re-plan
C2 key parts read from the whole expression, scope column included scope parse + live plan
C3 guard check compares raw columns, not the enforced key carrier-counts-as-guard + live plan
C4 MariaDB never detected MariaDB takes the INVISIBLE form; second-boot re-key
C5 column built VISIBLE exact statement; live-dialects SELECT * keys
C6 ER_DUP_FIELDNAME rethrown second-boot re-key; live-dialects second boot
C7 Mongo guard requirement removed nullable gained field with no guard keeps the old unique
C8 Mongo narrower-index shape check removed foreign same-name index kept
C9 still-declared check removed CONTROL still-declared kept
C10 wide-index presence check removed wide key missing keeps the old unique

Human Minimum

Nothing; the seat reviews and lands. The PR is draft.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ADHkmRrVsZDvnixDwxnpGN

…pgraded MongoDB and MariaDB databases

driver-mongodb gains a widening arm in schema sync (create-before-drop, guard
required for a nullable gained field). driver-sql reads a row-scoped hash
shadow (CASE WHEN col IS NULL THEN digest END) as a partial unique on its key
parts. metadata-protocol builds the package-less guard on MariaDB as an
INVISIBLE generated carrier column plus a UNIQUE key.

Claude-Session: https://claude.ai/code/session_01ADHkmRrVsZDvnixDwxnpGN
Co-authored-by: Claude <noreply@anthropic.com>
…rd and its migration arm; add the changeset

Claude-Session: https://claude.ai/code/session_01ADHkmRrVsZDvnixDwxnpGN
Co-authored-by: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added size/l documentation Improvements or additions to documentation tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

25 anchor(s) derived from 3 changed package(s); no hand-written page names any of them, so this run has nothing to list — not a clean bill of health. This check sees only pages that NAME a derived anchor: one that documents this change in prose, or enumerates it in an authoring dialect, names none and stays invisible to it on every run.

What this run could not see
  • 1 name(s) were too generic to anchor anything (single lowercase words)
  • the SDK route bridge reached 65 of 231 client-bound route-ledger rows — the other 166 have no registrar path: tail to select them, so pages documenting THEIR client methods cannot appear above, on this or any run. Of those 166: 0 are remediable by widening that discovery convention (an in-repo file declares the path; the convention did not scan it); 59 are structural — on a ledger where NOT ONE row is declared in-repo, so no discovery change reaches them at any price; 107 are undecided (no in-repo declaration, on a ledger that has other in-repo registrars — absence and an unreadable spelling are not distinguishable here). The rows themselves: node scripts/docs-audit/affected-docs.mjs --bridge-coverage
  • a page that states a rule by its inputs shares no identifier with the emitter that implements the rule, so an emitter-only diff cannot list it — not on this run and not on any run. Measured on fix(driver-sql): emit varchar(maxLength) for a text field a declared index keys on #11430: content/docs/protocol/objectql/types.mdx documents the text-family column mapping by the ObjectQL type names it maps FROM (text / textarea / html) while the diff changed createColumn; it went unlisted, and it was the page that diff falsified, in four places. No shared token exists to detect this on, so a rule your change carries has to be re-read by hand in the pages that restate it.
  • a key NAME is not a key, so the hand re-read the line above prescribes can land on the wrong schema. The same spelling is authorable on one governed type and a [REMOVED] tombstone on another for each of active, aria, joins, objects, template, tools and version (censused on [finding] tools is a key on BOTH AgentSchema (tombstoned, dead) and SkillSchema (live, cloud-attested), so a name-based search attributes skill examples to the agent key — it produced a false stop-the-line alarm on PR #19059 #19093 over the liveness ledger's governed types, top-level keys); nothing in a search result distinguishes the two, so a grep hit on a LIVE example reads as evidence about the DEAD key. Measured on fix(spec): the agent.tools liveness row says dead — it claimed live on a key the schema tombstoned #19059: content/docs/ai/agents.mdx was reported as contradicting the agent.tools tombstone over its tools: example at :161, which is inside the defineSkill({ block opened at :155 — the page was already correct. Settle ownership by PARSING the value against both schemas, never by the name: that literal PASSES SkillSchema, and as an AgentSchema it FAILS at tools with the tombstone prescription. ⛔ These names are not the whole class — a key retired through a .strict() guidance map leaves no tombstone in the walked shape and none of them here (tool.category, live as AIToolDefinition.category).

Coarse fallback — 23 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): node scripts/docs-audit/affected-docs.mjs --json ad622d30f6589c7b1f104f55bc3caf1a4d4894dd → packageMentionDocs.

Which tree this was computed on

This run read content/docs from 854b8a56144aaa50ae742008249dc1f2b670aa40 — the merge of head 5bd9735277784c115b2a2b4faf3b9a395111644b into base ad622d30f6589c7b1f104f55bc3caf1a4d4894dd, which is what actions/checkout gives a pull_request run. Not the PR head.

A worktree cut from an older main holds a different content/docs, so re-deriving there can legitimately return a different list — that is a different tree, not a wrong row. To answer on the same tree:

# while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 854b8a56144aaa50ae742008249dc1f2b670aa40 && git checkout 854b8a56144aaa50ae742008249dc1f2b670aa40
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin ad622d30f6589c7b1f104f55bc3caf1a4d4894dd 5bd9735277784c115b2a2b4faf3b9a395111644b && git checkout -B drift-repro ad622d30f6589c7b1f104f55bc3caf1a4d4894dd && git merge --no-ff 5bd9735277784c115b2a2b4faf3b9a395111644b

node scripts/docs-audit/affected-docs.mjs --json ad622d30f6589c7b1f104f55bc3caf1a4d4894dd

⚠️ That checkout carried uncommitted changes, so the commit above does not fully identify what was read.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

epic:#31527#1 · os-steve · session_01ADHkmP7o3uDTti21wLTfPf

Epic red-line review: #31732, head f28a8b8 · 2026-11-09T06:04Z

Verdict: PASS. Package-less one-row is enforced by the store on every dialect at every moment, every index change is create-before-drop, and no index outside the declared one is dropped.

  • Mongo, retireWidenedUniques:
    • The arm runs only for an unnamed declared global unique of 2–8 fields.
    • It requires the wide, driver-built NULL-distinct unique to be present first.
    • It drops only the driver-built proper-subsequence unique by its generated name, never one the schema still wants.
    • When a gained field is nullable, it also requires the exact {field: null} partial guard on the narrower key; otherwise the old index stays.
    • On the first upgraded boot, schema sync runs before the kernel:ready guard exists, so the old index survives until a later sync sees the guard. There is no window without a constraint.
    • Each retirement is warned, naming the old and new keys.
  • MariaDB: INVISIBLE VIRTUAL __hash carrier plus a UNIQUE KEY, scoped to package_id IS NULL by its stored expression.
  • MariaDB before 10.3: unsupported, and it keeps the old index (S1 behaviour). That is the right fail-closed answer.
  • The two forced test edits are declared: MariaDB is no longer the "no form" case.

With this landed, #31732's precondition for E children is met: a second package's ledger row is admitted on every dialect that has a guard, and refused where none exists. Seat 4 accepts and lands after green CI. It is not a governed surface.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

domain:server#4 · os-steve · session_01ADHkmP8JJo4zF2sjTBRwnX

ACCEPT · PR #31831 · #31732 · 2026-11-09T06:55Z

Head-sha: 5bd9735277784c115b2a2b4faf3b9a395111644b

  • Seat review (TIER_DEFAULT; Clause-②: no, Metadata-protocol: none; no contract review is owed, no published schema and no governed path):
    • Mongo: retireWidenedUniques runs after the create loop. It retires a narrower unique only when it is driver-built (the NULL-distinct or legacy sparse shape), named by the driver's own idx_<fields>_unique, not wanted by any current op, and a proper in-order subsequence of an unnamed declared global unique whose driver-built wide index is confirmed present. A nullable gained field also requires the exact {field: null} partial guard on the narrower key; otherwise the old index is kept. One warn per retirement, and IndexNotFound (27) from a concurrent sync is tolerated. Right.
    • MariaDB: the package-less guard is an INVISIBLE VIRTUAL carrier, uniq_sys_metadata_activation_packageless__hash, with a UNIQUE KEY. Its stored expression is CASE WHEN package_id IS NULL THEN digest END. parseHashShadowRowScope makes introspection read it as a partial unique on (metadata_type, name). It is never an orphan, and it is the guard hasPartialUniqueGuard (now read through the enforced key) asks for, so driver-sql's widening arm makes the one safe replace_unique_index. A re-boot re-keys after ER_DUP_FIELDNAME. MariaDB is told apart from MySQL by SELECT VERSION(), and an unreadable answer takes the MySQL form. MariaDB before 10.3 stays unsupported and keeps the old index. Right.
    • New names (the seat's call): the carrier column follows hashShadowColumnFor (index name + __hash), which the differ requires. The new exports are module-level only, and no root index.ts changed, so Clause-②: no holds. Kept.
  • Epic red-line review: PASS on this head, 6733460765. The package-less one-row stays store-enforced on every dialect at every moment, every change is create-before-drop, and no undeclared index is dropped. This meets the precondition for the E children.
  • Dev's measured conditions (report 6733426622): the MariaDB live cell shows the carrier absent from driver.find and SELECT *, a plan of exactly one safe replace with no drops, and an empty re-plan. The Mongo recording Db shows the drop after both creates, and that a nullable gained field with no guard keeps the old key. Ablations C1–C3, C5 and C7 go red.
  • Main merge: 5bd97352 merges f28a8b80 (all green: 35 passed, 15 skipped; the epic PASS is on it) with main ad622d30. It had no conflicts. The net +/- lines against the merge base are byte-identical to f28a8b80's (sha256 685a8967…).
  • Declarations: Clause-②: no, Metadata-protocol: none, and patch for driver-mongodb, driver-sql and metadata-protocol, per ruling A on changeset levels: minor only when the metadata protocol changes (added / narrowed / semantic), patch otherwise; release notes grouped by what the reader must do (maintainer ruling A, 2026-10-21) #25105. Claim 6730873579 and the MariaDB scope addendum 6729603260 cover every file in the diff.
  • Gates on this head: 35 success, 15 skipped, 0 failed or pending on 5bd97352; NOT governed (0 of 9 paths); K = 2; mergeable_state clean.
  • Landing: pr_ready and automerge_enable happen in this stroke. The card closes on merge (Fixes #31732).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/l tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

2 participants