Skip to content

Latest commit

 

History

History

shared-schema

README.md

Shared schema v1: aggregator actions and outcomes

Status: the PostgreSQL DDL here is the original contract for the action/event tables and the #6815 add-on stores. The live copies of these tables are in Turso (the Supabase project they were first applied to has been deleted; the Turso schema was created during the 2026-11-18 recovery, see doc/2026-11-18_supabase-turso-recovery.md). They are the canonical backing store when the aggregator runs with PM_AGG_STORE_BACKEND=turso; with the default file backend the JSON files remain authoritative. The aggregator reaches them only through aggregator/src/turso-stores.ts, which translates the PostgreSQL-dialect statements in aggregator/src/remote-stores.ts to SQLite (public. prefixes dropped, jsonb/timestamptz casts removed).

The target uses the public schema and the table-group names from #1744: public.agg_actions is owned by the PM aggregator, and public.shared_events is the cross-service event record. The database is a new backing-store target under #3142, not a change in ownership. The aggregator remains the source of truth for actions and their outcomes.

public.agg_actions

One row per action record from the version-1 actions.json envelope. IDs are text because action IDs are application-generated strings, not UUIDs.

Column Type / nullability JSON action field Contract
id text primary key id Stable action identity; never reused or deleted.
client_token text, nullable clientToken Per-form idempotency token. A partial unique index covers non-null values; null remains valid for legacy pre-token records.
kind, verb text, not null kind, verb Kinds are triage, status-request, session, preference; the check constraint permits only the current VERBS_BY_KIND pairs in ../aggregator/src/actions.ts.
object_ref text, not null objectRef Non-empty object identity.
target_ref, session_id, session_source text, nullable targetRef, sessionId, sessionSource Optional routing/target references.
payload jsonb, not null payload Must be a JSON object; verb-specific validation stays at the application boundary.
author jsonb, not null author Must contain a supported kind, non-empty id, and non-empty label. This is self-asserted authorship, not authenticated identity.
created_at timestamptz, not null createdAt Creation time; no updated_at field exists in the source contract.
state text, not null, default new state Check-constrained to new, applied, rejected, superseded. A result timestamp is recorded in applied_at for applied and consumer-rejected transitions; a check constraint enforces this (applied requires applied_at, new forbids it, rejected requires it unless the row is a rejected duplicate — ActionsStore.add() creates duplicate attempts directly in state rejected with duplicateOf/resultNote and no consumer timestamp, and those records must import losslessly).
applied_at, applied_by_run, result_note timestamptz, text, text, nullable appliedAt, appliedByRun, resultNote First consumer result time, run ID, and optional explanation.
supersedes_action_id, duplicate_of_action_id text, nullable supersedes, duplicateOf Optional self-references with restrictive foreign keys. History is superseded/rejected, never deleted.

The primary key, allowed kind/verb/state checks, JSON object checks, and partial unique client_token index are database constraints. The consume-once rule remains a transactional writer contract: a future result writer must update only WHERE state = 'new' and append its outcome in the same database transaction. The schema migration does not change that writer.

public.shared_events

This table preserves the aggregator's v2 audit-event fields and adds a structured action link for DB-authored action outcomes. id remains the event's independent stable text ID; it is not the action ID.

Column Type / nullability Event field Contract
id text primary key id Stable event identity.
event_at, ingested_at timestamptz, not null at, ingestedAt When the event happened and when the store accepted it; they are distinct. ingested_at defaults to database time.
event_at_raw, ingested_at_raw text, nullable at, ingestedAt (verbatim) The exact timestamp strings the event hash was computed over (canonicalFields in ../aggregator/src/events.ts hashes the literal strings). timestamptz normalizes spellings (2026-11-05T03:00:00+03:00 and 2026-11-05T00:00:00Z are the same instant but different hash inputs), so importers MUST store the original strings here and DB-authored writers MUST store the exact strings they feed the hash; without them, exported rows cannot be hash-verified against the JSON rollback path.
actor, kind, ref, summary text, not null same Current stream fields. Action results use action-applied / action-rejected, run actor, the action's objectRef, and the current summary format.
attribution, source jsonb, not null same Attribution must have a supported confidence and boolean asserted; source must identify its writer. Defaults preserve current unknown/unasserted attribution and pm-stack/aggregator source.
attribution_raw, source_raw text, nullable same (verbatim) The exact JSON serializations the event hash was computed over: canonicalFields includes attribution and source via JSON.stringify of the original objects, and jsonb does not preserve object-key ordering, so a round-trip through jsonb alone cannot reproduce every accepted original event's hash. Importers MUST store the original serializations here; DB-authored writers MUST store the exact strings they feed the hash.
corrects_event_id text, nullable corrects Soft reference, intentionally not a foreign key: event retention can prune the corrected record.
migrated_from_version smallint, nullable migratedFromVersion If set, must be 1.
prev_hash, hash text, not null same Lowercase 64-character SHA-256 chain values. The migration validates shape; serialization and chain ordering remain a single-writer responsibility.
action_id text, nullable FK to agg_actions.id new DB linkage Set for DB-authored action-result events; null is allowed for existing JSON event rows, which have no structured action ID.
idempotency_key text, nullable new DB linkage For a DB-authored action outcome, use action-result:<action-id>. Unique when present. Existing unlinked JSON events may leave it null.

An action outcome must either be unlinked (both fields null, for existing JSON events) or have both fields set with the matching stable key. Other event kinds may use an idempotency key without an action link. A partial unique index on action_id allows at most one linked outcome per action; the key index makes retries idempotent. The two linkage columns are nullable for compatibility with current JSON outcomes: the /api/actions/result writer currently emits a random event ID and summary but does not pass an action ID to recordStreamEvent. A later DB writer must populate both fields. This schema-only slice does not alter that behavior or backfill the unlinked event file. Because the old outcome records cannot be reliably joined back to actions from their summary text, a future data-move slice must choose whether to retain those records in events.json or import them as explicitly unlinked history; it must not guess the linkage.

The action author and event actor are provenance claims, not authentication. Current events default to {confidence: "unknown", asserted: false} and {identity: "pm-stack/aggregator"}. Do not upgrade these to authenticated provenance without the separate #941 trust-hardening work.

RLS is enabled on both tables (PostgreSQL DDL only; Turso has no RLS). The migration grants service_role the minimum table privileges needed by the current contracts: actions can be read/inserted/updated but not deleted; events can be read/inserted and pruned under the existing event-retention policy, but not updated. No client-facing RLS policy or production database write is part of this change.

Migration, fallback, and rollback

There is no shared Postgres migration runner yet; migrations are applied to PostgreSQL by hand in version order. The Turso stores have a runner (#14256): turso_migrations.py translates each migration from the PostgreSQL dialect to SQLite — following the aggregator's own translation conventions in aggregator/src/turso-stores.ts, and refusing any construct it has no rule for rather than guessing — and apply-store-migrations.py applies the pending set in version order, one transaction per migration, recording each in the store's pm_schema_migrations ledger so a re-run is a reported no-op. The translation is pinned by reviewed golden files under golden/, and test_turso_migrations.py proves the translated schema accepts and rejects the same rows the PostgreSQL suites assert. The pm-store-migrate workflow dispatches the runner: against a stand-in store as the CI proof, or against a named store from pm-stack/platform/stores.json once its token secret is configured. migrations/20261105_0001_action_event_store.up.sql creates agg_actions and shared_events with their constraints, indexes, and grants. migrations/20261116_0001_aggregator_remote_stores.up.sql (#6815) adds, in dependency order after it:

Table Holds
public.agg_activity_ingestion per-source checkpoints, dedupe seen ids, source status
public.agg_activity_gaps the activity gap ledger (gaps resolve in place)
public.agg_usage_snapshots append-only usage observations, dedupe key service|metric|ts|value
public.agg_store_documents aggregator-owned JSON documents (aa-last-good; namespaced C7 report events use an append-only typed contract)

migrations/20261122_0001_shared_comments.up.sql (#1744, Move 1 step 2) adds, after those:

Table Holds
public.shared_comments dashboard comments (the DashComment record), the shared cross-service comment table of #1744

migrations/20261129_0001_roadmap_annotations.up.sql (#1744, Move 1 step 2) adds, after those:

Table Holds
public.agg_roadmap_annotations roadmap annotations (the RoadmapAnnotation record), append-only
public.agg_roadmap_annotation_aliases the annotation store's alias map: a reshuffled source-row ref to the canonical stable ref

migrations/20261129_0002_agg_views.up.sql (#1744, Move 1 step 2) adds, after those:

Table Holds
public.agg_views saved dashboard views (the SavedView record), unique name per owner, at most one default per owner

migrations/20261129_0003_agg_alerts.up.sql (#1744, Move 1 step 2) adds, after those:

Table Holds
public.agg_alerts_incidents alert incidents (the Incident record), at most one non-closed incident per (source, incident_key)
public.agg_alerts_incident_events the incident lifecycle ledger (opened / duplicate_suppressed / delivered / escalated / acked / closed / reopened)
public.agg_alerts_notification_sends the notification send ledger (one row per send attempt; acted_on_at stamped by the ack transition)

migrations/20261129_0004_shared_items_links.up.sql (#1744, Move 2 P1) adds, after those:

Table Holds
public.shared_items GitHub items (issues and PRs) in the projector's common fields, keyed by the repo-qualified ref
public.shared_links the link registry (the pmp_links record): (subject, ref, relation) rows, ref foreign-keyed to shared_items

That migration also records the projector's P1 location decision: the shared entity tables live in this shared database alongside shared_comments and shared_events; the projector's operational pmp_* tables (deliveries, sync state, push/hot events) stay in the projector's own libSQL database and get no shared contract. The tables hold no rows until the Move 2 execution slice wires the projector's dual-write; projector.db remains the live backend and the rollback target.

migrations/20261129_0005_agg_quota_current.up.sql (#1744, Move 1 step 3) adds, after those:

Table Holds
public.agg_quota_current_entries the quota gatherer's latest measured state per (service, provider), with the snapshot stamp
public.agg_quota_current_windows per-entry named quota windows, with the reader's ingest gates (state list, non-negative value) as checks

migrations/20261129_0006_agg_coverage.up.sql (#1744, Move 1 step 3) adds, after those:

Table Holds
public.agg_coverage_reports coverage snapshot reports, one row per file slot (baseline, current, weekly) with the rotation anchor
public.agg_coverage_repos per-repo coverage census rows: counts non-negative, or null when that count's query failed

migrations/20261129_0007_agg_feed.up.sql (#1744, Move 1 step 3) adds, after those:

Table Holds
public.agg_feed_snapshots fleet feed snapshots, one row per published fleet-feed.json document under its (generated_at, hash) snapshot identity

migrations/20261129_0008_agg_identity_registry.up.sql (#1744, Move 1 step 2) adds, after those:

Table Holds
public.agg_identity_sessions session identity registry records, one row per SessionRecord under its (source, id) identity, merged in place, never deleted

migrations/20261130_0001_orchestration_tier_needs_user.up.sql (#13507 slice A10) rebuilds public.orch_items with the tier check widened to needsDrew, needsUser, needsOrchestrator and data-updates stored needsDrew rows to needsUser during the copy (SQLite has no ALTER ... ADD CONSTRAINT, so the widen is a rebuild in both dialects). Its paired down rebuilds back, mapping needsUser rows to needsDrew; it is data-preserving and so does not refuse non-empty tables. The orchestration writer is dual-key during the window: a document's user tier may arrive under needsDrew or needsUser and is stored as needsUser.

Each paired .down.sql refuses to drop a non-empty table; the #6815 down migration drops its tables in reverse dependency order. The refusal is deliberate protection against an accidental data-loss rollback.

Cutover and rollback are the aggregator's PM_AGG_STORE_BACKEND switch (file default → turso), documented in the aggregator README. Switching to turso makes the tables the write path; the local JSON stores are preserved as a rebuildable cache and remain the rollback target. The migration test proves the paired PostgreSQL DDL and rollback on a disposable PostgreSQL 16 instance; it does not connect to any remote database.

This supplies reversible DDL for the #6815 slice plus the shared_comments, roadmap-annotation, agg_views, alerts, shared items/links, quota-current, coverage, feed and identity-registry contracts, and records the projector's P1 location decision. It does not complete P1 / Move 1, or land the Move 2 execution slice; #1744 acceptance criterion 3 remains unchecked.

Disposable test

Run from the repository root:

bash pm-stack/shared-schema/test-migration.sh

The script starts its own temporary PostgreSQL 16 container, applies the up migrations in order, proves key/foreign-key/check/idempotency behavior (including legacy-null compatibility, the #6815 store-table constraints, and the #1744 shared_comments, roadmap-annotation, agg_views, alerts, shared items/links, quota-current, coverage, feed and identity-registry constraints), proves each down migration refuses non-empty tables, clears only its disposable fixtures, applies the down migrations in reverse, and verifies the tables are gone. It does not connect to Turso or the PM Compose database.