Skip to content

Week 4: the system authorizes — approval gate and idempotent migration - #5

Merged
TAM-DS merged 1 commit into
mainfrom
week-4-migration
Nov 14, 2026
Merged

TAM-DS merged 1 commit into
mainfrom
week-4-migration

Conversation

@TAM-DS

@TAM-DS TAM-DS commented Nov 13, 2026

Copy link
Copy Markdown
Owner

What this adds

The other half of the design rule. Weeks 1–3 were Claude proposes; this PR is the system authorizes.

The approval gate (approval.py) — a human act at the CLI, deliberately not an MCP tool:

firstsync proposals org_d            # list proposals + approval state
firstsync approve org_d              # approve them as they stand
firstsync migrate org_d --db out.db  # load approved data

An approval stores a SHA-256 of each proposal's canonical JSON. Change any proposal afterwards — the model proposes again, a file gets edited — and the migration refuses to run, naming the changed fields, until a person re-approves. Approving past open questions is allowed but loud: they're listed at approval time.

The migration engine (migrate.py) — runs only behind the gate, and builds every row with the same build_row the dry-run used (extracted so the two cannot diverge). What the admin saw in the dry-run is exactly what loads:

dry-run said migration does
clean loads
warning loads, warned optional value dropped
blocking skipped, Ids in the report
needs_decision skipped, Ids in the report

Idempotent by construction: upserts key on (tenant, external_id). On org D with Sonnet's proposals — 396 read, 387 loaded, 9 skipped (exactly the 5 contacts missing emails + 4 deals missing amounts planted in the org). Run it again: 0 inserted, 387 unchanged. A Hypothesis property test asserts N runs ≡ one run for arbitrary limits.

Target DB (target/db.py): SQLite via stdlib so tests and CI need no services; DDL generated from TARGET_SCHEMA, so the Python schema, SQLite DDL and Postgres DDL can't drift apart silently. Tenants isolated by a UUID derived from the connection handle.

Claude's only new capability: migration_get_status — read-only. There is no tool that approves or migrates.

Tests

198 passing (13 new), mypy src clean, no network, no API key. New coverage: no approval → refused; tampered proposal → refused by name; migration counts equal the dry-run's per table; tenant isolation; the N-runs property.


Generated by Claude Code

Approval (src/firstsync/approval.py) is a human act at the CLI, never
an MCP tool: `firstsync approve <org>` records a SHA-256 of each
proposal as it stands. If any proposal changes afterwards, the
migration refuses to run and names the changed fields until a person
approves again. Open questions being approved past are listed at
approval time.

The migration engine (src/firstsync/migrate.py) runs only behind that
gate and builds every row with the same build_row the dry-run used
(extracted from the dry-run engine so the two cannot diverge): clean
and warning records load, blocking and needs_decision records are
skipped with their Ids in the report. Upserts key on
(tenant, external_id), so re-running is safe by construction - the
second run reports everything unchanged and writes nothing.

Target database (src/firstsync/target/db.py): SQLite via stdlib, DDL
generated from TARGET_SCHEMA so the Python schema, the SQLite DDL and
the Postgres DDL cannot drift apart silently. Tenants are isolated by
a UUID derived from the connection handle.

Claude's only new capability is migration_get_status, read-only.

New CLI commands: proposals, approve, migrate (exits non-zero when
refused, so it can gate a pipeline).

On org D with Sonnet's proposals: 396 read, 387 loaded, 9 skipped -
exactly the 5 contacts missing emails and 4 deals missing amounts the
dry-run flagged. Second run: 0 inserted, 387 unchanged.

198 tests (13 new), mypy clean, no network, no API key.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017ggB2HHn8zjybwfaJG1g3R
@TAM-DS
TAM-DS merged commit a425f30 into main Nov 14, 2026
1 check passed
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.

2 participants