Repository navigation
Week 4: the system authorizes — approval gate and idempotent migration - #5
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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 samebuild_rowthe dry-run used (extracted so the two cannot diverge). What the admin saw in the dry-run is exactly what loads: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 fromTARGET_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 srcclean, 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