Skip to content

feat: add ownership checkpointing database foundation - #81

Merged
Aliferous3 merged 2 commits into
mainfrom
feat/ownership-checkpointing-b1
Oct 19, 2026
Merged

Aliferous3 merged 2 commits into
mainfrom
feat/ownership-checkpointing-b1

Conversation

@Aliferous3

Copy link
Copy Markdown
Owner

Summary

Phase 1C-B1 — the database substrate of the merged ownership checkpoint
architecture (PR #79), gated on the B0.5 pinned-revision spike (PR #80,
verdict PASS).

Migration 013_ownership_checkpointing.sql created — database layer
only. It adds the relations and primitives the crash-safe
multi-invocation history walk requires:

  • ownership_scope_versions (RETAINED / CORE) — separates immutable
    base-scope identity from per-version membership at grain
    (scope_id, calculation_version); excluded/exclusion_reason are
    versioned, cardinality_cap-only. Tenant pair and scope/repo pair are
    DB-enforced; the scope FK is exactly ON DELETE NO ACTION.
  • ownership_history_work (EPHEMERAL) — per
    scope×version×month×work_key accumulation counters. work_key is
    unlinked or a 64-hex pseudonymous key; no plaintext contributor
    identity, no paths, no provider payloads.
  • ownership_contributor_build_work (EPHEMERAL) — build-level
    identity staging for represented keys only ((repository, version, work_key)); synthetic buckets structurally refused; no scope
    provenance column per §5.3.
  • ownership_snapshots → ownership_scopes FK re-created ON DELETE NO ACTION (same deployed constraint name/keys) plus a new composite
    ownership_snapshots_scope_version FK
    (scope_id, repository_id, user_id, calculation_version) → ownership_scope_versions NO ACTION: a snapshot cannot exist without
    matching same-repo/same-tenant/same-version membership, while
    repository/account teardown still destroys the complete ownership
    graph in one statement through the direct tenant cascades (proven).
  • Primitives — own_checkpoint_page (page delta + durable cursor in
    one CAS transaction; replay returns stale, never double-counts),
    own_restart_scope (staging cleanup by committed-V provenance),
    own_promote_build (exact-set staged≡represented validation before the
    atomic flip + metadata apply), own_abandon_build (the one canonical
    cleanup), own_build_preflight/own_build_init (§5.6c retired-version
    GC gate, split so no third version coexists), own_sweep_stale_work
    (abandon-routed sweep). All SECURITY INVOKER, service_role-only,
    closed OWNERSHIP_INTERNAL_ERROR taxonomy.
  • dash_ownership_scopes now enumerates scopes through
    ownership_scope_versions at active_version; building-version
    scopes and exclusion state cannot leak into active output. Same
    signature, grants, invoker model.
  • 012→013 upgrade: pre-existing ownership rows are backfilled into
    membership inside 013 before the composite FK validates. Migration 012
    is untouched.
  • Backup classification: ownership_scope_versions → CORE (backed
    up); both work tables → EPHEMERAL (never in the dump — a restored
    building version is abandoned, not resumed piecemeal).

Scope

Not in this tranche (still unimplemented, by design): production GitHub
history walking, live object(oid:) traversal, runtime pagination,
ACCUMULATE/RESOLVE worker loops, cron/webhook ownership orchestration,
serverless slicing, live promotion trigger, UI, dashboard launch, feature
flags, backfill, or any production ownership writes.

Production untouched. Migration 013 was applied only to throwaway
local Postgres 17 databases. The production ownership worker is not
wired
and no ownership rows were written anywhere but synthetic
scratch databases. .env.local was never used.

Security / privacy impact

  • Security/privacy behavior changed and is explained below

  • New tables: RLS enabled, zero browser/user policies, service_role
    DML only — same model as 012. New functions: SECURITY INVOKER,
    search_path pinned, EXECUTE revoked from public/anon/
    authenticated, granted to service_role only.

  • Privacy: counter rows carry pseudonymous work_key only; identity
    fields (github_node_id, display_login) exist solely in
    represented-key staging + retained contributor rows; synthetic
    unlinked/other can never enter staging. No emails, names,
    databaseIds, paths, commit text, or provider error text anywhere.

  • Backup: work tables are backup-excluded (fail-closed classification in
    backup-scope.mjs); restore of a mid-build version routes through the
    canonical abandon primitive — proven in the drill.

Database / deployment impact

  • Migration included (013_ownership_checkpointing.sql)
  • Environment/configuration change required
  • Deployment order matters — apply via the standard db:migrate
    path; 012-era rows are backfilled by the migration itself

Verification

  • npm run build
  • npm run typecheck
  • npm test — 631 pass / 0 fail (3 pre-existing env-gated skips)
  • npm audit --audit-level=moderate — 0 vulnerabilities
  • SBOM CycloneDX valid
  • gitleaks v8.30.1 (worktree + full history) — no leaks
  • npm run db:drill — real Postgres 17, full PASS
  • Visual QA completed for UI changes — N/A, no UI

Exact real-Postgres integrity tests performed (all inside
npm run db:drill, scripts/ownership-checkpoint-proofs.mjs):

  • A–F: snapshot↔membership composite-FK — orphan snapshot, wrong version,
    cross-repo, cross-tenant all rejected by name-checked constraints;
    membership delete blocked by snapshot; legal order succeeds.
  • G–I: direct scope delete rejected while referenced; legal
    snapshots→membership→scope prune succeeds.
  • J–L: single-statement DELETE FROM repositories / DELETE FROM users
    removes all seven ownership relations including both work tables.
  • M–O: building-version scopes/exclusion/identity invisible under
    active-N dash_ownership_scopes.
  • P–Q: initial-build abort zeroes V1 artifacts; N→N+1 abort keeps N
    byte-identical, prunes N+1-only contributors/scopes.
  • R–T: scope restart removes failed-scope staging (new + existing
    contributors), preserves committed-V-provenanced staging, resets to
    fresh ACCUMULATE.
  • U–W: promotion rejects extra and missing staged keys, accepts the
    exact set and applies metadata atomically with the flip.
  • X–AA: retired-version rows block init; preflight GC unblocks; forced
    mid-GC failure rolls back atomically and retries cleanly; clean-state
    preflight is a no-op.
  • Checkpoint CAS: apply → stale on replay (lost-response safe), stale
    cursor/version rejection with zero writes, additive counters, least/
    greatest timestamp merge, pass transitions, frozen is_bot staging,
    attribution drift fails closed.
  • Backup: work tables absent from the archive; ownership_scope_versions
    present; restored building version cleanly abandoned; V1 preserved.
  • Upgrade leg: real 001→012 chain + 012-era rows → 013 applies, data
    preserved, membership backfilled, confdeltype='a' on all three FKs.

Test plan

  • CI security gate (test/build/audit/SBOM)
  • CI gitleaks job
  • CI recovery-drill job (postgres:17 service)
  • Reviewer: confirm migration 012 file is byte-identical on main

Contributor agreement

Generated with Devin

Migration 013 implements the database substrate of the merged ownership
checkpoint architecture: versioned scope membership, ephemeral build
state, and the lifecycle/restart/promotion/abandon/GC primitives the
multi-invocation history walk requires.

- ownership_scope_versions (RETAINED/CORE) splits immutable base-scope
  identity from per-version membership; exclusion state is versioned.
- ownership_history_work + ownership_contributor_build_work (EPHEMERAL,
  backup-excluded) hold crash-safe counters and represented-key staging
  only — pseudonymous work keys, no plaintext identity in counters.
- ownership_snapshots->scope FK re-created ON DELETE NO ACTION; new
  composite snapshot->membership FK (scope, repo, user, version) NO ACTION
  makes snapshots impossible without same-tenant/same-repo/same-version
  membership while repository/account teardown still cascades in one
  statement through the direct tenant FKs.
- own_checkpoint_page (single-transaction page CAS + cursor advance),
  own_restart_scope (committed-V-provenance staging cleanup),
  own_promote_build (exact-set validation before atomic flip),
  own_abandon_build (the one canonical cleanup),
  own_build_preflight/own_build_init (retired-version GC gate, split per
  §5.6c), own_sweep_stale_work — all SECURITY INVOKER, service_role-only.
- dash_ownership_scopes now enumerates scopes through version membership
  at active_version; building-version scopes/exclusion never leak.
- 012-era rows are backfilled into membership inside 013 before the
  composite FK validates; migration 012 itself is untouched.
- Recovery drill extended: real-Postgres A-AA integrity matrix,
  restored-building abandon proof, and a 012->013 upgrade leg
  (MIGRATE_UP_TO bounds the applied chain).

Database layer only — no worker, no GitHub traversal, no wiring.
Real-Postgres 17 proofs run in scripts/recovery-drill.mjs (npm run
db:drill) and CI's postgres:17 service.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@vercel

vercel Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
dev-ledger Ready Ready Preview Oct 19, 2026 1:40pm UTC

own_checkpoint_page staged identity for any key with pass-1 counters,
but overflow-only contributors also carry counters — a caller bug could
persist github_node_id/display_login for a key that never reaches the
finalized scope output. The database now computes the represented set
itself (per-period touch_commits DESC, work_key ASC ranks 1..25,
unioned, unlinked excluded — bit-equivalent to the canonical reducer)
and ignores non-represented submissions before any pass-2 check or
write. Adds a reducer-vs-DB exact-set differential proof with 27+ linked
keys covering rank 25/26, tie-break, multi-month union, overflow-every-
month, and pass-2-only cases.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@Aliferous3
Aliferous3 merged commit 3ddbf41 into main Oct 19, 2026
5 checks passed

This branch was successfully deployed

1 active deployment
Preview — d185ca54 Deployed Oct 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant