From 126628897a07e2c7a7f071f24eb19e62d400a671 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 20:46:37 -0600 Subject: [PATCH 1/3] =?UTF-8?q?pm-stack:=20orchestration=20state=20schema?= =?UTF-8?q?=20contract=20=E2=80=94=20#5005=20phase=201,=20P1=20vetting=20s?= =?UTF-8?q?lice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Target-store decision (recorded on #5005 with document evidence): orchestration state is new PM state, so it starts in Supabase muse-dev2 under the #3142 placement — public schema, new orch_* group, per SHARED-SCHEMA-MIGRATION-PLAN.md's inherited constraints. The #6581 Supabase-to-Turso wave plan migrates existing Supabase table sets and does not classify orchestration state. This slice is the P1 schema-vetting step in the #6103 shape: specifies the target, applies nothing to muse-dev2, copies no rows, changes no writer or reader. orchestration.json stays authoritative until the writer-cutover slice. - migrations/20261111_0001_orchestration_state.up.sql: public.orch_runs (append-only run log: one row per pass, carrying the complete export document, so the generated export is the newest run's document and the #898 dashboard shape is preserved by construction) and public.orch_items (current tier / classification state: queryable columns + the item verbatim in JSONB, tier check-constrained to needsDrew/needsOrchestrator, run FKs, seen-order and document-identity checks). RLS on both; service_role gets append/read on the log and the replace set on the state table. - Down migration refuses to drop populated tables, per #6103. - ORCHESTRATION.md: the contract — column maps to the current orchestration.json fields, the writer contract the cutover slice must follow (one transaction per pass: insert run, replace items, regenerate the file export), and rollback at every stage. - test-orchestration-migration.sh: disposable-PostgreSQL-16 test in the test-migration.sh shape — privilege/RLS probes, service_role operation under RLS, six constraint rejections, snapshot-replace flow, down-migration guard and clean rollback. Verified passing on the laptop this run. Queue task 14783, lane code-g. --- pm-stack/shared-schema/ORCHESTRATION.md | 92 +++++++++ ...20261111_0001_orchestration_state.down.sql | 14 ++ .../20261111_0001_orchestration_state.up.sql | 66 +++++++ .../test-orchestration-migration.sh | 174 ++++++++++++++++++ 4 files changed, 346 insertions(+) create mode 100644 pm-stack/shared-schema/ORCHESTRATION.md create mode 100644 pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.down.sql create mode 100644 pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql create mode 100755 pm-stack/shared-schema/test-orchestration-migration.sh diff --git a/pm-stack/shared-schema/ORCHESTRATION.md b/pm-stack/shared-schema/ORCHESTRATION.md new file mode 100644 index 0000000000..ba09f74726 --- /dev/null +++ b/pm-stack/shared-schema/ORCHESTRATION.md @@ -0,0 +1,92 @@ +# Shared schema: orchestration state (#5005, phase 1 of #3142) + +Status: schema contract and local migration only. This migration has +**not** been applied to Supabase `muse-dev2`; no rows have been copied, +and the orchestration files remain authoritative. + +Target-store decision (recorded on +[#5005](https://github.com/developing-today/code/issues/5005) with the +document evidence): orchestration state is new PM state, so it starts +in Supabase `muse-dev2` under the #3142 placement, in the `public` +schema with a new `orch_*` table group — the conventions +[SHARED-SCHEMA-MIGRATION-PLAN.md](../SHARED-SCHEMA-MIGRATION-PLAN.md) +inherits from #1744. The #6581 Supabase→Turso wave plan migrates +existing Supabase table sets and does not classify orchestration +state; if a future wave classifies it, these tables move by the same +shadow → parity → flip pattern and the file export below is the +fallback until that flip verifies. + +The state lives in three files today, all in the goal workspace's +`hidden_files/`: `orchestration.json` (the rendered state the #898 +dashboard contract serves), `orchestrator-state.json` (the pass's +carried state), and `orchestrator-log.md` (the run log). The contract +maps them onto two tables. The remaining `orchestration.json` +sections (`dependencies`, `goals`, `actions`) are per-run render +content, not classification state: they live in the run's `document` +only, which is also what keeps the export byte-shape identical to +today's file. + +## `public.orch_runs` — the run log + +One row per orchestration pass; append-only (grants are `SELECT, +INSERT` — history is never rewritten or deleted through the service +role). + +| Column | Type / nullability | `orchestration.json` field | Contract | +|---|---|---|---| +| `run_id` | `text` primary key | `runId` | Stable run identity (`orch-` today). The document's `runId` must equal it. | +| `generated_at` | `timestamptz`, not null | `generatedAt` | When the pass produced the state. | +| `document_version` | `integer`, not null, ≥ 1 | `version` | Export document schema version; the document's `version` must equal it. | +| `provenance` | `jsonb`, not null, object | `provenance` | The sources the pass read, verbatim. | +| `document` | `jsonb`, not null, object | (the whole file) | The complete export document for this run. The generated export is the newest run's `document`, so the #898 dashboard shape is preserved by construction, not by a renderer. | +| `item_count` | `integer`, not null, ≥ 0 | `tiers` lengths | `len(tiers.needsDrew) + len(tiers.needsOrchestrator)` for the run; a cheap parity probe against `orch_items` after a snapshot replace. | + +## `public.orch_items` — current classification state + +One row per classified item; the table always equals the newest +run's tiers. Item fields beyond the columns below (`horizon`, +`question`, `recommendedAction`, and anything later passes add) live +verbatim in `item` — the columns are the queryable classification +surface, the JSONB is the lossless record, the same split the #6103 +contract uses for action payloads. + +| Column | Type / nullability | Item field | Contract | +|---|---|---|---| +| `id` | `text` primary key | `id` | Stable item identity; the `item` document's `id` must equal it. Never reused. | +| `tier` | `text`, not null | (the `tiers` key) | Check-constrained to `needsDrew`, `needsOrchestrator` — the two tiers the document carries today. A new tier is a contract change, not data. | +| `kind`, `title`, `source`, `state`, `reason` | `text`, not null | `kind`, `title`, `source`, `state`, `reason` | The classification itself. `kind`/`state` must be non-empty. | +| `ref` | `text`, nullable | `ref` | The item's governing reference (e.g. `gh:owner/repo#n`), when it has one. | +| `since`, `last_seen_at` | `timestamptz`, not null | `since`, `lastSeenAt` | First classified / most recently seen; `last_seen_at >= since` is enforced. | +| `item` | `jsonb`, not null, object | (the whole item) | The item verbatim. | +| `first_seen_run_id`, `last_seen_run_id` | `text`, not null, FK → `orch_runs` | — | The runs that first and most recently carried the item; `ON DELETE RESTRICT` keeps the log's referents honest. | + +## Writer contract (the cutover slice's obligations, fixed here) + +One transaction per pass, in this order: + +1. Insert the `orch_runs` row (run id, generated-at, provenance, the + complete document, item count). +2. Replace `orch_items` with the document's tiers: delete the + previous snapshot, insert one row per item, carrying + `first_seen_run_id` forward for items that already existed. +3. Regenerate `orchestration.json` from the run's `document` (until + the reader flip, the file is still written every pass — it is + both the export and the rollback path). + +Until that slice lands and verifies (a pass reads its state back +from the tables and the dashboard serves the generated export +unchanged), the files stay authoritative and the writer is the +hourly orchestration pass exactly as today. No flag-day: the reader +flip is a separate slice after the writer has run dual for at least +one full cycle, per the migration plan's reversibility rule. + +Rollback at every stage is "point the pass back at the files": the +files are never behind, because step 3 keeps writing them. + +## What this slice is not + +- No DDL applied to `muse-dev2`, no rows copied, no writer or reader + changed — the P1 schema-vetting shape the plan's precondition P1 + uses (compare the #6103 actions contract in [README.md](README.md)). +- No Turso placement: see the decision record above. The fleet-queue + database is untouched by this work, as #6581's end state requires. diff --git a/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.down.sql b/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.down.sql new file mode 100644 index 0000000000..59950c6fd7 --- /dev/null +++ b/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.down.sql @@ -0,0 +1,14 @@ +-- #5005 rollback guard: never discard rows during a schema rollback. +DO $$ +BEGIN + IF (to_regclass('public.orch_items') IS NOT NULL + AND EXISTS (SELECT 1 FROM public.orch_items LIMIT 1)) + OR (to_regclass('public.orch_runs') IS NOT NULL + AND EXISTS (SELECT 1 FROM public.orch_runs LIMIT 1)) THEN + RAISE EXCEPTION 'refusing to drop non-empty orchestration tables; export/replay rows first'; + END IF; +END +$$; + +DROP TABLE IF EXISTS public.orch_items; +DROP TABLE IF EXISTS public.orch_runs; diff --git a/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql b/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql new file mode 100644 index 0000000000..af71548e7e --- /dev/null +++ b/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql @@ -0,0 +1,66 @@ +-- #5005: schema contract for orchestration state, phase 1 of #3142. +-- Target: Supabase muse-dev2, public schema. This DDL is not applied here; +-- orchestration.json remains authoritative until the writer-cutover slice. +-- +-- Two tables, one per file the state lives in today: +-- orch_runs — the run log (orchestrator-log.md's durable successor): +-- one append-only row per orchestration pass, carrying the +-- complete export document for that run. +-- orch_items — the current tier/classification state (the tiers half of +-- orchestration.json / orchestrator-state.json): one row per +-- classified item, replaced per run in the writer's +-- transaction so the table always equals the newest run. + +CREATE TABLE public.orch_runs ( + run_id text PRIMARY KEY CHECK (btrim(run_id) <> ''), + generated_at timestamptz NOT NULL, + document_version integer NOT NULL CHECK (document_version >= 1), + provenance jsonb NOT NULL CHECK (jsonb_typeof(provenance) = 'object'), + document jsonb NOT NULL CHECK (jsonb_typeof(document) = 'object'), + item_count integer NOT NULL CHECK (item_count >= 0), + CONSTRAINT orch_runs_document_identity_check CHECK ( + document ->> 'runId' = run_id + AND document ->> 'version' ~ '^[0-9]+$' + AND (document ->> 'version')::integer = document_version + ) +); + +CREATE TABLE public.orch_items ( + id text PRIMARY KEY CHECK (btrim(id) <> ''), + tier text NOT NULL CHECK (tier IN ('needsDrew', 'needsOrchestrator')), + kind text NOT NULL CHECK (btrim(kind) <> ''), + title text NOT NULL, + source text NOT NULL, + state text NOT NULL CHECK (btrim(state) <> ''), + reason text NOT NULL, + ref text, + since timestamptz NOT NULL, + last_seen_at timestamptz NOT NULL, + item jsonb NOT NULL CHECK (jsonb_typeof(item) = 'object'), + first_seen_run_id text NOT NULL + REFERENCES public.orch_runs (run_id) ON DELETE RESTRICT, + last_seen_run_id text NOT NULL + REFERENCES public.orch_runs (run_id) ON DELETE RESTRICT, + CONSTRAINT orch_items_seen_order_check CHECK (last_seen_at >= since), + CONSTRAINT orch_items_document_identity_check CHECK (item ->> 'id' = id) +); + +CREATE INDEX orch_items_tier_state_idx + ON public.orch_items (tier, state); + +CREATE INDEX orch_items_last_seen_run_idx + ON public.orch_items (last_seen_run_id); + +ALTER TABLE public.orch_runs ENABLE ROW LEVEL SECURITY; +ALTER TABLE public.orch_items ENABLE ROW LEVEL SECURITY; + +-- The run log is append-only: service_role may read and append, never +-- rewrite or delete history. The state table is a per-run snapshot, so +-- the writer needs the full replace set. +GRANT SELECT, INSERT ON public.orch_runs TO service_role; +GRANT SELECT, INSERT, UPDATE, DELETE ON public.orch_items TO service_role; + +COMMENT ON TABLE public.orch_runs IS + 'Orchestration pass run log (#5005); orchestration.json remains authoritative until the writer-cutover slice.'; +COMMENT ON TABLE public.orch_items IS + 'Current orchestration tier/classification state (#5005); one row per classified item, replaced per run.'; diff --git a/pm-stack/shared-schema/test-orchestration-migration.sh b/pm-stack/shared-schema/test-orchestration-migration.sh new file mode 100755 index 0000000000..8b2ba746f9 --- /dev/null +++ b/pm-stack/shared-schema/test-orchestration-migration.sh @@ -0,0 +1,174 @@ +#!/usr/bin/env bash +set -euo pipefail + +schema_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +migrations="$schema_dir/migrations" +up="$migrations/20261111_0001_orchestration_state.up.sql" +down="$migrations/20261111_0001_orchestration_state.down.sql" +container="pm-orch-schema-test-$$" + +if ! command -v docker >/dev/null 2>&1; then + echo "docker is required for the disposable PostgreSQL migration test" >&2 + exit 1 +fi +if docker container inspect "$container" >/dev/null 2>&1; then + echo "refusing to reuse existing container: $container" >&2 + exit 1 +fi + +cleanup() { + docker rm -f "$container" >/dev/null 2>&1 || true +} +trap cleanup EXIT + +docker run --detach --rm \ + --name "$container" \ + --env POSTGRES_HOST_AUTH_METHOD=trust \ + --env POSTGRES_DB=pm_orch_test \ + postgres:16-alpine >/dev/null + +ready=false +for _ in $(seq 1 60); do + if docker exec "$container" psql -X -qAt -U postgres -d pm_orch_test -c 'SELECT 1' >/dev/null 2>&1; then + ready=true + break + fi + sleep 1 +done +if [[ "$ready" != true ]]; then + echo "disposable PostgreSQL did not become ready" >&2 + exit 1 +fi + +psql_test() { + docker exec -i "$container" psql -X -v ON_ERROR_STOP=1 -U postgres -d pm_orch_test "$@" +} + +# Supabase's service_role carries BYPASSRLS; model that so the test proves +# the role can actually operate under the RLS model, not just hold grants. +psql_test -c 'CREATE ROLE service_role BYPASSRLS LOGIN;' +psql_test < "$up" + +# A run row: the document's runId and version must match the columns. +psql_test -c "INSERT INTO public.orch_runs + (run_id, generated_at, document_version, provenance, document, item_count) + VALUES ('orch-2026-11-11T00:00:00Z', '2026-11-11T00:00:00Z', 1, + '{\"sources\": [\"test\"]}', + '{\"version\": 1, \"runId\": \"orch-2026-11-11T00:00:00Z\", \"tiers\": {\"needsDrew\": [], \"needsOrchestrator\": []}}', + 2);" + +# One item per tier, in the shape the current orchestration.json carries. +psql_test -c "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, ref, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-drew-1', 'needsDrew', 'decision', 'Decide a thing', + 'test', 'open', 'Only Drew can resolve it', 'gh:developing-today/code#1', + '2026-11-10T00:00:00Z', '2026-11-11T00:00:00Z', + '{\"id\": \"item-drew-1\", \"kind\": \"decision\"}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z');" +psql_test -c "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-orch-1', 'needsOrchestrator', 'stalled', 'Move a thing', + 'test', 'open', 'No movement in 48h', + '2026-11-09T00:00:00Z', '2026-11-11T00:00:00Z', + '{\"id\": \"item-orch-1\", \"kind\": \"stalled\"}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z');" + +privileges="$(psql_test -Atc "SELECT concat( + (SELECT relrowsecurity::text FROM pg_class WHERE oid = 'public.orch_runs'::regclass), ':', + (SELECT relrowsecurity::text FROM pg_class WHERE oid = 'public.orch_items'::regclass), ':', + has_table_privilege('service_role', 'public.orch_runs', 'INSERT')::text, ':', + has_table_privilege('service_role', 'public.orch_runs', 'UPDATE')::text, ':', + has_table_privilege('service_role', 'public.orch_runs', 'DELETE')::text, ':', + has_table_privilege('service_role', 'public.orch_items', 'DELETE')::text)")" +if [[ "$privileges" != "true:true:true:false:false:true" ]]; then + echo "unexpected RLS/service_role privileges: $privileges" >&2 + exit 1 +fi +echo "ok - RLS enabled; run log is append-only for service_role; state table is replaceable" + +# The service_role must actually be able to operate under RLS (Supabase's +# service_role carries BYPASSRLS): append a run and read state back. +docker exec -i "$container" psql -X -v ON_ERROR_STOP=1 -U service_role -d pm_orch_test -c "INSERT INTO public.orch_runs + (run_id, generated_at, document_version, provenance, document, item_count) + VALUES ('orch-2026-11-11T01:00:00Z', '2026-11-11T01:00:00Z', 1, + '{\"sources\": [\"test-2\"]}', + '{\"version\": 1, \"runId\": \"orch-2026-11-11T01:00:00Z\", \"tiers\": {\"needsDrew\": [], \"needsOrchestrator\": []}}', + 0);" +svc_seen="$(docker exec -i "$container" psql -X -qAt -v ON_ERROR_STOP=1 -U service_role -d pm_orch_test -c "SELECT count(*) FROM public.orch_items")" +if [[ "$svc_seen" != "2" ]]; then + echo "service_role could not read back the state rows under RLS: $svc_seen" >&2 + exit 1 +fi +echo "ok - service_role can append runs and read state under RLS" + +expect_rejected() { + local label="$1" + local sql="$2" + if psql_test -c "$sql" >/dev/null 2>&1; then + echo "expected database constraint rejection: $label" >&2 + exit 1 + fi + echo "ok - rejected $label" +} + +expect_rejected "run document with a mismatched runId" "INSERT INTO public.orch_runs + (run_id, generated_at, document_version, provenance, document, item_count) + VALUES ('orch-x', now(), 1, '{}', '{\"version\": 1, \"runId\": \"orch-y\"}', 0)" +expect_rejected "run document with a mismatched version" "INSERT INTO public.orch_runs + (run_id, generated_at, document_version, provenance, document, item_count) + VALUES ('orch-x', now(), 2, '{}', '{\"version\": 1, \"runId\": \"orch-x\"}', 0)" +expect_rejected "item in an unknown tier" "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-bad-tier', 'needsNobody', 'decision', 't', 'test', 'open', 'r', + now(), now(), '{\"id\": \"item-bad-tier\"}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z')" +expect_rejected "item whose document id differs from its key" "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-a', 'needsDrew', 'decision', 't', 'test', 'open', 'r', + now(), now(), '{\"id\": \"item-b\"}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z')" +expect_rejected "item pointing at a run that does not exist" "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-orphan', 'needsDrew', 'decision', 't', 'test', 'open', 'r', + now(), now(), '{\"id\": \"item-orphan\"}', + 'orch-missing', 'orch-missing')" +expect_rejected "item last seen before it was first seen" "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-time-travel', 'needsDrew', 'decision', 't', 'test', 'open', 'r', + '2026-11-11T00:00:00Z', '2026-11-10T00:00:00Z', '{\"id\": \"item-time-travel\"}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z')" + +# The per-run snapshot replace the writer contract prescribes: delete the +# old snapshot, insert the new run's rows, count parity with the run row. +psql_test -c "DELETE FROM public.orch_items;" +psql_test -c "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, ref, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-drew-1', 'needsDrew', 'decision', 'Decide a thing', + 'test', 'open', 'Only Drew can resolve it', 'gh:developing-today/code#1', + '2026-11-10T00:00:00Z', '2026-11-11T01:00:00Z', + '{\"id\": \"item-drew-1\", \"kind\": \"decision\"}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T01:00:00Z');" +echo "ok - snapshot replace carries first_seen_run_id forward" + +if psql_test < "$down" >/dev/null 2>&1; then + echo "down migration should refuse populated tables" >&2 + exit 1 +fi +echo "ok - down migration protects non-empty tables" + +psql_test -c 'DELETE FROM public.orch_items; DELETE FROM public.orch_runs;' +psql_test < "$down" +remaining="$(psql_test -Atc "SELECT (to_regclass('public.orch_runs') IS NULL AND to_regclass('public.orch_items') IS NULL)::text")" +if [[ "$remaining" != "true" ]]; then + echo "down migration left schema objects behind" >&2 + exit 1 +fi + +echo "ok - orchestration migration constraints and rollback verified on disposable PostgreSQL 16" From e36f29f381dadb248a006ba227ddd44f3138e6e2 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Thu, 12 Nov 2026 00:16:41 -0600 Subject: [PATCH 2/3] =?UTF-8?q?fix(pm-stack):=20orch=5Fitems.state=20is=20?= =?UTF-8?q?nullable=20=E2=80=94=20TierItem.state=20is=20optional=20(#8856)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 20261111_0001 up migration made orch_items.state NOT NULL with a non-empty check, but the writer contract makes TierItem.state optional (aggregator model.ts) and the contract corpus's decision item omits state entirely, so a valid snapshot was rejected (Codex P2 on PR #8458). Absent state is now NULL; a state that is present must still be non-empty: CHECK (state IS NULL OR btrim(state) <> ''). Probe: test-orchestration-migration.sh gains a state-less insert mirroring the corpus decision item (imports cleanly, state NULL) and a blank-state rejection case. Full harness passes on disposable PostgreSQL 16. --- .../20261111_0001_orchestration_state.up.sql | 6 ++++- .../test-orchestration-migration.sh | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql b/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql index af71548e7e..efdbb8cbef 100644 --- a/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql +++ b/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql @@ -31,7 +31,11 @@ CREATE TABLE public.orch_items ( kind text NOT NULL CHECK (btrim(kind) <> ''), title text NOT NULL, source text NOT NULL, - state text NOT NULL CHECK (btrim(state) <> ''), + -- TierItem.state is optional in the writer contract (aggregator + -- model.ts): a non-session item may carry no state at all — the + -- contract corpus's decision item omits it. Absent state is NULL; + -- a state that IS present must still be non-empty (#8856). + state text CHECK (state IS NULL OR btrim(state) <> ''), reason text NOT NULL, ref text, since timestamptz NOT NULL, diff --git a/pm-stack/shared-schema/test-orchestration-migration.sh b/pm-stack/shared-schema/test-orchestration-migration.sh index 8b2ba746f9..252b69b9c1 100755 --- a/pm-stack/shared-schema/test-orchestration-migration.sh +++ b/pm-stack/shared-schema/test-orchestration-migration.sh @@ -144,6 +144,31 @@ expect_rejected "item last seen before it was first seen" "INSERT INTO public.or '2026-11-11T00:00:00Z', '2026-11-10T00:00:00Z', '{\"id\": \"item-time-travel\"}', 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z')" +# A valid item WITHOUT a state must import cleanly (#8856): TierItem.state +# is optional in the writer contract, and the contract corpus's decision +# item (aggregator/test/contract-corpus/v1/orchestration.json) omits it +# entirely. This insert mirrors that item's shape, with the state column +# left out (NULL), under the run row above. +psql_test -c "INSERT INTO public.orch_items + (id, tier, kind, title, source, reason, ref, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('example-owner/example-repo#812', 'needsDrew', 'decision', + 'Review an example design decision', 'backlog', + 'Scope requires an owner design decision.', + 'example-owner/example-repo#812', + '2026-10-27T03:14:56Z', '2026-10-27T03:14:56Z', + '{\"id\": \"example-owner/example-repo#812\", \"kind\": \"decision\"}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z');" +echo "ok - state-less contract-corpus item imports (state is NULL)" + +# Absent state is NULL; a present-but-blank state is still rejected. +expect_rejected "item with a blank state" "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-blank-state', 'needsDrew', 'decision', 't', 'test', ' ', 'r', + now(), now(), '{\"id\": \"item-blank-state\"}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z')" + # The per-run snapshot replace the writer contract prescribes: delete the # old snapshot, insert the new run's rows, count parity with the run row. psql_test -c "DELETE FROM public.orch_items;" From cf259448b88add6d35eaaaa67307057dcd1e288e Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Thu, 12 Nov 2026 01:25:28 -0600 Subject: [PATCH 3/3] fix(pm-stack): orchestration identity CHECKs reject missing/JSON-null identity fields (#8857) --- .../20261111_0001_orchestration_state.up.sql | 16 ++++++++--- .../test-orchestration-migration.sh | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql b/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql index efdbb8cbef..f9c2038e7e 100644 --- a/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql +++ b/pm-stack/shared-schema/migrations/20261111_0001_orchestration_state.up.sql @@ -18,10 +18,16 @@ CREATE TABLE public.orch_runs ( provenance jsonb NOT NULL CHECK (jsonb_typeof(provenance) = 'object'), document jsonb NOT NULL CHECK (jsonb_typeof(document) = 'object'), item_count integer NOT NULL CHECK (item_count >= 0), + -- The comparison must be IS TRUE (#8857): a document missing runId + -- or version (or carrying one as JSON null) extracts SQL NULL for + -- that field, the conjunction evaluates to NULL, and PostgreSQL + -- accepts NULL in a CHECK — the row would pass with no identity. CONSTRAINT orch_runs_document_identity_check CHECK ( - document ->> 'runId' = run_id - AND document ->> 'version' ~ '^[0-9]+$' - AND (document ->> 'version')::integer = document_version + ( + document ->> 'runId' = run_id + AND document ->> 'version' ~ '^[0-9]+$' + AND (document ->> 'version')::integer = document_version + ) IS TRUE ) ); @@ -46,7 +52,9 @@ CREATE TABLE public.orch_items ( last_seen_run_id text NOT NULL REFERENCES public.orch_runs (run_id) ON DELETE RESTRICT, CONSTRAINT orch_items_seen_order_check CHECK (last_seen_at >= since), - CONSTRAINT orch_items_document_identity_check CHECK (item ->> 'id' = id) + -- IS TRUE for the same reason as the runs identity check (#8857): + -- item = '{}' or a JSON-null id extracts SQL NULL and would pass. + CONSTRAINT orch_items_document_identity_check CHECK ((item ->> 'id' = id) IS TRUE) ); CREATE INDEX orch_items_tier_state_idx diff --git a/pm-stack/shared-schema/test-orchestration-migration.sh b/pm-stack/shared-schema/test-orchestration-migration.sh index 252b69b9c1..e63f67d8c7 100755 --- a/pm-stack/shared-schema/test-orchestration-migration.sh +++ b/pm-stack/shared-schema/test-orchestration-migration.sh @@ -119,6 +119,20 @@ expect_rejected "run document with a mismatched runId" "INSERT INTO public.orch_ expect_rejected "run document with a mismatched version" "INSERT INTO public.orch_runs (run_id, generated_at, document_version, provenance, document, item_count) VALUES ('orch-x', now(), 2, '{}', '{\"version\": 1, \"runId\": \"orch-x\"}', 0)" + +# Identity fields must be present and non-null (#8857): a missing or +# JSON-null runId/version extracts SQL NULL for that field, and a bare +# CHECK accepts a NULL result — these probes pin the IS TRUE form of +# the identity checks. +expect_rejected "run document with no identity fields" "INSERT INTO public.orch_runs + (run_id, generated_at, document_version, provenance, document, item_count) + VALUES ('orch-x', now(), 1, '{}', '{}', 0)" +expect_rejected "run document with a JSON-null runId" "INSERT INTO public.orch_runs + (run_id, generated_at, document_version, provenance, document, item_count) + VALUES ('orch-x', now(), 1, '{}', '{\"version\": 1, \"runId\": null}', 0)" +expect_rejected "run document with a JSON-null version" "INSERT INTO public.orch_runs + (run_id, generated_at, document_version, provenance, document, item_count) + VALUES ('orch-x', now(), 1, '{}', '{\"version\": null, \"runId\": \"orch-x\"}', 0)" expect_rejected "item in an unknown tier" "INSERT INTO public.orch_items (id, tier, kind, title, source, state, reason, since, last_seen_at, item, first_seen_run_id, last_seen_run_id) @@ -131,6 +145,20 @@ expect_rejected "item whose document id differs from its key" "INSERT INTO publi VALUES ('item-a', 'needsDrew', 'decision', 't', 'test', 'open', 'r', now(), now(), '{\"id\": \"item-b\"}', 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z')" +# Same NULL pass on the item side (#8857): item = '{}' or a JSON-null +# id extracts SQL NULL and must be rejected, not accepted. +expect_rejected "item with no identity field" "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-noid', 'needsDrew', 'decision', 't', 'test', 'open', 'r', + now(), now(), '{}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z')" +expect_rejected "item with a JSON-null id" "INSERT INTO public.orch_items + (id, tier, kind, title, source, state, reason, since, last_seen_at, + item, first_seen_run_id, last_seen_run_id) + VALUES ('item-nullid', 'needsDrew', 'decision', 't', 'test', 'open', 'r', + now(), now(), '{\"id\": null}', + 'orch-2026-11-11T00:00:00Z', 'orch-2026-11-11T00:00:00Z')" expect_rejected "item pointing at a run that does not exist" "INSERT INTO public.orch_items (id, tier, kind, title, source, state, reason, since, last_seen_at, item, first_seen_run_id, last_seen_run_id)