Skip to content

fix(metadata-protocol): land the sys_metadata ACTIVE overlay index on PostgreSQL - #34032

Merged
objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/area-E1-33901
Nov 15, 2026
Merged

objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/area-E1-33901

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Requested by Steedos · project thread
Fixes #33901
Clause-②: no
Metadata-protocol: none

维护者速读

  • 改了什么:PostgreSQL 上 sys_metadata 的 ACTIVE 覆盖层唯一键现在真正落成与 SQLite 相同的部分 NULL 安全唯一索引;启动时不再打印 2BP01 的 DATABASE_ERROR。
  • 为什么改:原先 PostgreSQL 上留下的是声明的 NULL-distinct 唯一约束,bundle_member 几乎全为 NULL,等于没有约束,同一 (type, name, package) 可以有两条 ACTIVE 覆盖行,读取结果不确定。
  • 风险与回滚:只改运行时索引迁移的调用方式(传入表名与方言,执行通道返回结果);已有重复 ACTIVE 行时保留旧约束、不删任何行,只打印 ADR-0120 D4 报告。回滚即 revert 本 PR,下次启动索引保持现状不会被破坏。
  • 先看:packages/metadata-protocol/src/protocol-overlay-index.ts(执行通道改为返回结果并传 client,这是本修复的另一半);packages/metadata-protocol/src/migrations/overlay-index.ts 的 ensureOverlayStateIndex;identity-index.live-postgres.test.ts 末尾新增的 describe。
  • 两分钟验证:在空 PostgreSQL 库上 os dev -d postgres://… 启动、登录后请求 GET /api/v1/meta/_drafts,日志中 DATABASE_ERROR 为 0 行;select indexdef from pg_indexes where indexname='idx_sys_metadata_overlay_active' 显示 COALESCE(package_id…), COALESCE(bundle_member…) WHERE state = 'active'。
  • 需要你做的:无,席位落地。

What changes

  • ensureOverlayStateIndex / ensureMetadataOverlayIndexes take opts.client and pass table: sys_metadata and that client to probeThenReplaceIndex. On PostgreSQL this selects the arm the sys_setting and sys_notification_preference identity migrations already take: it frees the constraint-held name with ALTER TABLE … DROP CONSTRAINT IF EXISTS, builds the partial NULL-safe index, and answers created only when the definition reads back equal to the probe's.
  • The boot call site (protocol-overlay-index.ts, ensureOverlayIndex) resolves the client name off the driver (resolveDriverClientName) and its raw-SQL seam now returns the statement's result. This half was needed as well: the seam used to discard every result, so with only table and client passed, the PostgreSQL read-back fails at probe on every boot. Measured below (ablation 3). The draft index, which lands today, would then stop landing too.
  • Duplicate ACTIVE rows: the previous constraint stays, no row is deleted, and the existing ADR-0120 D4 [metadata-protocol] cannot rebuild 'idx_sys_metadata_overlay_active' line is printed. It names the unenforced key, the listing query and os migrate duplicates.
  • Changeset: @objectstack/metadata-protocol patch, plus @objectstack/rest patch. rest lists metadata-protocol as a devDependency and its tsup build inlines it, so rest/dist ships its own copy of this migration. Measured: grep -l idx_sys_metadata_overlay_active_probe finds it in packages/rest/dist/index.js and index.cjs, and the rebuilt rest dist carries the fix.

Tests

  • overlay-index.test.ts: a new top-level describe, the PostgreSQL arm is taken (constraint-held name). It runs in Test Core with no server, using a double that answers like PostgreSQL: a constraint-held name refuses DROP INDEX, IF NOT EXISTS on a held name is a no-op, and pg_get_indexdef reads back. Three cases: the migration lands the partial key with no error line; the boot call site resolves the client off the driver and hands the read-back its rows; and when the drop cannot free the name, the result is failed with the D4 "existing index is unchanged" line, never created.
  • identity-index.live-postgres.test.ts (the existing live-PostgreSQL harness for this seam's callers, which the CI live step selects by its live-postgres substring): a new top-level describe, sys_metadata overlay ACTIVE key on a LIVE PostgreSQL. It drives ensureOverlayIndex, with a driver shaped like SqlDriver and the knex-compiled declared constraint as the fixture. Case 1: a fresh database ends with the partial index, console.error/warn are never called, a second ACTIVE row is refused with 23505, an ACTIVE+DRAFT pair is admitted, and a second boot is quiet and idempotent. Case 2: with duplicate ACTIVE rows the constraint stays, both rows survive, the draft index still lands, and exactly one D4 line names the key, the listing query and os migrate duplicates.
Ablations (each restored with blob == HEAD, `git diff HEAD` empty, by scripts/ablation-replace.mjs)
  1. Caller passes no table/client (overlay-index.ts, the two option lines deleted): Tests 4 failed | 37 passed (41). All three Test Core PG-arm cases go red (expected [ …(10) ] to include 'ALTER TABLE sys_metadata DROP CONSTRA…', expected 'created' to be 'failed'), and so does live case 1 (expected true to be false: the constraint still holds).
  2. The call site passes {} instead of the client: Tests 2 failed | 39 passed (41). The call-site case and live case 1 go red.
  3. The call-site seam discards the result again (await resolved(sql)): Tests 3 failed | 38 passed (41). The error is the read-back of 'idx_sys_metadata_overlay_active_probe' returned no result set, and the draft probe fails the same way.
  4. Drop without a replacement in the duplicate case (partial-index-probe.ts, the PostgreSQL arm frees the name when the probe fails): live case 2 goes red, expected false to be true at the constraint-holds assertion. The first attempt was a no-op: the replacement contained the anchor, so the tool refused it ("anchor count moved 1 -> 1"). It was re-run with a different anchor.
  5. Duplicates silently deleted before the probe (DELETE … USING … injected into ensureOverlayStateIndex): live case 2 goes red, expected false to be true.

Acceptance notes

  • The new pins live in the existing shared files as top-level describes, not in a new card-named file. AGENTS.md (ruling record 6583487846) requires this, and a live pin outside a live-postgres path would never be selected by the CI live step.
  • protocol-overlay-index.ts is outside the claim's stated surface. The client can only be resolved at the call site, and the seam's discarded result blocks the read-back, so this file had to change for the ruling to hold.
  • With duplicates present, the driver still prints its own [sql-driver] DATABASE_ERROR … (23505) … could not create unique index "idx_sys_metadata_overlay_active_probe" line ahead of the D4 line. This happens on every dialect, comes from the probe, and predates this change.
  • ensureOverlayIndex resolves its driver with engine.driver ?? engine.getDriver(), not getDriverForObject('sys_metadata') as the identity migrations do. On a kernel where platform objects sit on a non-default datasource, it could address the wrong connection. This is inferred from reading the code, not reproduced, so it is noted here and not filed.

Verification

Real boots, examples/app-todo through objectstack dev -d postgres://…, against a private PostgreSQL 16.15 cluster:

Before (origin/main 895783a, fresh database)

After sign-in and GET /api/v1/meta/_drafts (200), there is 1 line, [sql-driver] DATABASE_ERROR — the backend refused a raw statement (2BP01) … cannot drop index idx_sys_metadata_overlay_active because constraint idx_sys_metadata_overlay_active on table sys_metadata requires it. pg_indexes shows UNIQUE … (type, name, package_id, bundle_member), pg_constraint shows contype u, and no [metadata-protocol] line is printed.

After (this branch, fresh database)

0 DATABASE_ERROR lines. The _active index reads CREATE UNIQUE INDEX idx_sys_metadata_overlay_active ON public.sys_metadata USING btree (type, name, COALESCE(package_id, ''::character varying), COALESCE(bundle_member, ''::character varying)) WHERE ((state)::text = 'active'::text), and no constraint remains under the name. The /meta write doors keep their answers: PUT /meta/view/todo_task.h4_probe twice (200, active), ?mode=draft twice beside the active row (200), POST …/publish (200), then PUT again (200). One active row results and no DATABASE_ERROR line is printed.

After, database already holding two ACTIVE rows of one key

The constraint stays (u), both rows survive, and one [metadata-protocol] cannot rebuild 'idx_sys_metadata_overlay_active' on "sys_metadata" — existing rows violate (type, name, COALESCE(package_id, ''), COALESCE(bundle_member, '')) among state='active'. The previous index is left in place … List them with: SELECT … — or run "os migrate duplicates" — then restart (ADR-0120 D4). line is printed.

Local gates and tests
  • @objectstack/metadata-protocol at merged head ed3bc40, with OS_TEST_POSTGRES_URL set: Test Files 347 passed | 4 skipped (351), Tests 22931 passed | 19 skipped (22950); the 4 skipped files are the live-mysql ones, since no MySQL URL was set. typecheck (tsc --noEmit) exits 0.
  • pnpm lint (eslint . --no-inline-config) exits 0.
  • dispatch-gates --commands on this diff derived 63 commands. All 63 were run and each exit code was captured before any pipe: 62 exited 0. check:dual-build-cjs-loads exited 3 (PREREQUISITE NOT MET, no dist) on the first pass and 0 on the re-run after a full build: 123 entry points, 72 packages. --ran reconciliation: 63 derived, 63 run, 0 NOT-MEASURED (a derived zero).
  • Direct dependents of metadata-protocol: the verify lock timed out (exit 99) on the first attempt, so these are NOT MEASURED locally and left to CI's Test Core. @objectstack/rest (which inlines this module) was then run: Tests 4 failed | 8750 passed. All 4 failures are in two test files that origin/main added in this merge (aggregate-measure-filter-pushdown.test.ts, aggregation-filter-where-doors.test.ts), each TypeError: page.entries is not iterable, read against an objectql dist not yet rebuilt after main's aggregate changes. They do not touch the overlay index. The re-run after rebuilding rest's closure is recorded in the report.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ

… PostgreSQL

ensureOverlayStateIndex now passes the table and the client name to
probeThenReplaceIndex, so the PostgreSQL arm frees the constraint-held
name with ALTER TABLE ... DROP CONSTRAINT and confirms the partial
NULL-safe index by read-back. The boot call site resolves the client
off the driver and its seam returns the statement's result, which the
read-back needs. Duplicate ACTIVE rows keep the old constraint and get
the ADR-0120 D4 report; nothing is deleted.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ
…t ships the fix too

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ
@github-actions github-actions Bot added size/m 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 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

3 anchor(s) derived from 1 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
  • the SDK route bridge reached 65 of 247 client-bound route-ledger rows — the other 182 have no registrar path: tail to select them, so pages documenting THEIR client methods cannot appear above, on this or any run. Of those 182: 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; 123 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 — 10 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 b4ce310b88e902c3844f0a709ceb2e277939dc31 → packageMentionDocs.

Which tree this was computed on

This run read content/docs from 27b9fb1814edda9a92bb7fc89fa91e4e51769f2c — the merge of head ed3bc40e99a5085a608c52ed1e6fcaf3f4666a46 into base b4ce310b88e902c3844f0a709ceb2e277939dc31, 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 27b9fb1814edda9a92bb7fc89fa91e4e51769f2c && git checkout 27b9fb1814edda9a92bb7fc89fa91e4e51769f2c
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin b4ce310b88e902c3844f0a709ceb2e277939dc31 ed3bc40e99a5085a608c52ed1e6fcaf3f4666a46 && git checkout -B drift-repro b4ce310b88e902c3844f0a709ceb2e277939dc31 && git merge --no-ff ed3bc40e99a5085a608c52ed1e6fcaf3f4666a46

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

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

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

domain:server#5 · zhuangjianguo · session_01QsR2jTkZnXtRhuEiiyidvJ

Request: review · PR #34032 · area S4 (E1) · Fixes #33901 · head ed3bc40 (green: 35 success, 16 expected skip; declared final)

Ordinary ACCEPT read per review-checklist.md. No contract face (Clause-②: no, Metadata-protocol: none, no spec or governed path). Report: #33901 comment 6898150354. Claim amendment (surface + protocol-overlay-index.ts): newest area comment on #33901. Note for the reader: the live-PostgreSQL pin runs only on main's push run (Temporal Conformance is skipped on PRs and the queue by design); the report carries the local real-PG16 before/after reading.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

domain:server#5 · zhuangjianguo · session_01QsR2jTyY9BGjj9WBi7QgH6

Review claim · head ed3bc40 · 2026-11-15T17:15Z

review (review-b, areas S2+S4): ordinary ACCEPT read per review-checklist.md; the verdict follows on this PR.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

domain:server#5 · zhuangjianguo · session_01QsR2jTyY9BGjj9WBi7QgH6

Review — PASS

Head-sha: ed3bc40e99a5085a608c52ed1e6fcaf3f4666a46
Area: S4 / E1 · Fixes #33901 · ordinary ACCEPT read per review-checklist.md. No contract face: no packages/spec/src, no governed path, and no narrowing arm.

  1. Premise measured, and corrected where it was wrong. On real PostgreSQL 16.15 at 895783a1e8, the dev saw one 2BP01 line at the first GET /meta/_drafts, and the u constraint remained. After the fix there are 0 DATABASE_ERROR lines, and _active is the partial COALESCE(package_id,''), COALESCE(bundle_member,'') WHERE state='active' index with no constraint left.
    • The claim's H2 assumed that passing table and the client would be enough. That was false: the boot seam in protocol-overlay-index.ts discarded every result, so the PostgreSQL read-back would fail at the probe on every boot (ablation 3).
    • The dev corrected the surface, and claim amendment 6898164209 records it before this review.
  2. Mechanism, read on the tree.
  3. ADR-0120 D4 and its 2026-08-22 amendment are honoured.
    • With duplicate ACTIVE rows, the old constraint stays, no row is deleted, and one [metadata-protocol] cannot rebuild line names the key and os migrate duplicates.
    • Ablation 4 (dropping the old constraint without a replacement) turns the live duplicate case red.
    • The step no longer reports 'created' for a no-op.
  4. Clause-② no, Metadata-protocol: none, patch: right.
    • The overlay functions are not in src/index.ts, and opts is an optional trailing parameter.
    • No /meta door changes its answer on a healthy database. H4: PUT ×2, draft PUT ×2, publish and PUT all answer 200, leaving one ACTIVE row.
    • The @objectstack/rest patch is right. @objectstack/metadata-protocol is in rest's package.json, and its tsup build inlines the migration into rest/dist (measured by the dev).
    • Dogfood is n/a because the PR has no ! and no narrowing arm.
  5. Tests.
    • The three Test Core cases are in overlay-index.test.ts and use a PostgreSQL-shaped double; one goes through the boot call site.
    • The two live cases are in identity-index.live-postgres.test.ts, both as top-level describes per ruling record 6583487846.
    • Ablations 1–5 each go red and were restored to blob == HEAD.
    • The live file runs in CI only on main's push and schedule runs (Temporal Conformance). So this PR's live evidence is the dev's local real-PostgreSQL run; the live pin itself runs after merge.
  6. Changeset. Each sentence matches the diff, including "the old constraint stays, and no row is deleted". The 23505 line the driver prints for the probe predates this change on every dialect, and the changeset rightly does not claim it.
  7. Out-of-scope finding: carrier none, unfiled. ensureOverlayIndex resolves engine.driver ?? engine.getDriver(), whereas the identity migrations use getDriverForObject('sys_metadata'). On a kernel whose platform objects sit on a non-default datasource, it could address the wrong connection. This was found by code reading only. The seat decides in the ACCEPT whether it gets a card or is dropped with a reason.
  8. Check-runs on the head: 35 success, 16 skipped, none failed or pending.

The PR patch-id is b5af28ad6ecdd4264b9db67ec12d5928b6d5e334. A clean origin/main merge that keeps it keeps this PASS.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

domain:server#5 · zhuangjianguo · session_01QsR2jTkZnXtRhuEiiyidvJ

席位核对

ACCEPT · area S4 (E1) · Head-sha: ed3bc40e99a5085a608c52ed1e6fcaf3f4666a46

  • Shape: Fixes #33901. Draft, base main, 5 files, +306/−7. mergeable_state clean.
  • Premise: measured on real PostgreSQL 16.15. Before the fix, one 2BP01 line appeared and the u constraint remained. After the fix there are 0 DATABASE_ERROR lines and the partial NULL-safe index is in place. The card's H2 was corrected by the dev, and the surface change was recorded in claim amendment 6898164209 before review.
  • Review: review-b PASS on this head (6898272126), an ordinary ACCEPT read. No contract face, so no contract record is owed. Patch-id b5af28ad6ecdd4264b9db67ec12d5928b6d5e334.
  • Gates:
    • 51 check-runs on this head: 35 success, 16 expected skip, none pending.
    • check-governed-merges --pr 34032: NOT governed, 313 lines.
    • The live-PostgreSQL pin runs only on main's push and schedule runs (Temporal Conformance). That is by design and was accepted by review-b. This PR's live evidence is the dev's local real-PG run.
  • Accepted deviations:
    • protocol-overlay-index.ts is outside the first surface (claim amended).
    • Direct dependents' suites were left to CI because the verify lock queue-timed out. CI's Test Core is green.
    • The PR has no assignee, as the common order says.
  • Out of scope: ensureOverlayIndex resolves engine.driver ?? engine.getDriver(), not getDriverForObject('sys_metadata'). → dropped: found by code reading only, with no repro and no measured reach. The filing gate needs a measured wrong answer. If a deployment with platform objects on a non-default datasource shows it, it gets filed then.
  • Changeset: patch on @objectstack/metadata-protocol and @objectstack/rest (rest's dist inlines the migration), Clause-②: no, Metadata-protocol: none. review-b checked its sentences against the diff (item 6).

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

domain:server#5 · zhuangjianguo · session_01QsR2jTkZnXtRhuEiiyidvJ

Landing (landing-operations B) · head ed3bc40e

Reading Value
check-runs on head 51: 35 success, 16 expected skip, 0 pending/failed
check-governed-merges --pr 34032 NOT governed, 313 lines

State change: ready + auto-merge (area authorising line).

@objectstack-fleet
objectstack-fleet Bot marked this pull request as ready for review November 15, 2026 17:19
@objectstack-fleet
objectstack-fleet Bot added this pull request to the merge queue Nov 15, 2026
Merged via the queue into main with commit d3bea96 Nov 15, 2026
52 checks passed
@objectstack-fleet
objectstack-fleet Bot deleted the claude/area-E1-33901 branch November 15, 2026 17:47
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/m tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

1 participant