Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Harness migration + promotion consolidation (audit D6, A6)

Jerome238c2b832783
diff --git a/.claude/agents/merger.md b/.claude/agents/merger.md
index 358161e4..2ba09b07 100644
--- a/.claude/agents/merger.md
+++ b/.claude/agents/merger.md
@@ -127,8 +127,7 @@ cd $CLAUDE_PROJECT_DIR && flock $CLAUDE_PROJECT_DIR/.promote.lock bash -c '
[ -n "$DEFAULT" ] && ! git show-ref --verify --quiet "refs/heads/$DEFAULT" && DEFAULT=
[ -z "$DEFAULT" ] && DEFAULT=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed "s@^origin/@@")
[ -z "$DEFAULT" ] && { git show-ref --verify --quiet refs/heads/master && DEFAULT=master || DEFAULT=main; }
- git reset --hard HEAD # drop working-tree debris on whatever branch we are on
- git checkout "$DEFAULT" || exit 1 # promotion target is the session fork-base branch, NOT $CLAUDE_PROJECT_DIR HEAD
+ git checkout -f "$DEFAULT" || exit 1 # switch to the promotion target (session fork-base branch, NOT $CLAUDE_PROJECT_DIR HEAD), discarding demo working-tree debris in the same step. `-f` replaces the old `git reset --hard HEAD; git checkout`: same clean-then-switch effect, but without `git reset --hard`, which the auto-mode command classifier blocks (audit #08).
# Docker-only helper: re-applies the App.tsx variant that checkout reverts. Absent in a local checkout — skip it there.
[ -x /entrypoint-helpers/apply-app-variant.sh ] && /entrypoint-helpers/apply-app-variant.sh
git merge --no-ff session/<SESSION_SHORT_ID> -m "merge(session): <SESSION_SHORT_ID>" \
@@ -165,8 +164,7 @@ cd $CLAUDE_PROJECT_DIR && flock $CLAUDE_PROJECT_DIR/.promote.lock bash -c '
[ -n "$DEFAULT" ] && ! git show-ref --verify --quiet "refs/heads/$DEFAULT" && DEFAULT=
[ -z "$DEFAULT" ] && DEFAULT=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed "s@^origin/@@")
[ -z "$DEFAULT" ] && { git show-ref --verify --quiet refs/heads/master && DEFAULT=master || DEFAULT=main; }
- git reset --hard HEAD
- git checkout "$DEFAULT" || exit 1
+ git checkout -f "$DEFAULT" || exit 1 # clean-then-switch in one step; no `git reset --hard` (auto-mode classifier blocks it, audit #08)
# Docker-only helper: re-applies the App.tsx variant that checkout reverts. Absent in a local checkout — skip it there.
[ -x /entrypoint-helpers/apply-app-variant.sh ] && /entrypoint-helpers/apply-app-variant.sh
git merge --no-ff <BRANCH_NAME> -m "rollback(<SESSION_SHORT_ID>): undo via agent" \
diff --git a/.claude/skills/writing-migrations/SKILL.md b/.claude/skills/writing-migrations/SKILL.md
index d1ce6fa0..80bf8e44 100644
--- a/.claude/skills/writing-migrations/SKILL.md
+++ b/.claude/skills/writing-migrations/SKILL.md
@@ -56,16 +56,42 @@ Ignore CSS, component layout, copy, tests.
**Checkpoint:** you have an explicit list of schema-relevant changes (possibly
empty). If empty, jump to step 3's no-op path.
-### 3. Compute the delta against what is already deployed
-
-For each changed entity, compare the desired schema (from the TS types) with the
-schema already in `supabase/migrations/` and `supabase/schemas/`. Emit ONLY the
-incremental delta. Anything already represented in `supabase/migrations/` is
-already deployed — do not re-emit it. If the net diff has no schema impact,
-write **nothing** (a no-op deploy is valid) and report `NO_MIGRATION_NEEDED`.
-
-**Checkpoint:** every line of SQL you are about to write corresponds to a delta
-NOT already present in `supabase/migrations/`.
+### 3. Regenerate ONE net migration against what is ALREADY DEPLOYED (session-base)
+
+"Deployed" = the migrations present at `session-base/<SESSION_SHORT_ID>` (the fork
+anchor, i.e. the last deployed state). A migration YOU generated in an EARLIER
+iteration of this same session is NOT deployed: it sits on the session branch but was
+never pushed. Treating it as deployed is exactly what makes iterations STACK (add
+column `foo`, then a second migration to rename it to `bar`, ...) instead of one clean
+migration. The user does not want to push N migrations per change.
+
+So, before authoring any SQL:
+
+1. Find this session's own prior migration(s):
+ `git diff --diff-filter=A session-base/<SESSION_SHORT_ID>..session/<SESSION_SHORT_ID> -- supabase/migrations/`
+ (files ADDED since the fork = generated by an earlier iteration of this session).
+2. **Delete them.** You will regenerate ONE fresh file that is the whole net change.
+3. Author SQL for the NET delta between the desired schema (from the TS types) and the
+ schema as of `session-base` ONLY. Never layer an incremental delta on top of your
+ own prior migration.
+
+Result: however many times the user iterates before deploy, the session carries
+exactly ONE migration = `net(session-base -> final schema)`. If the net diff has no
+schema impact, write **nothing** and report `NO_MIGRATION_NEEDED`.
+
+**Hard boundary:** overwrite-and-regenerate is safe ONLY because the migration is not
+yet deployed. Once it is pushed / promoted it is immutable; a later change is a NEW
+forward migration, never an edit of the pushed one.
+
+**Surface note.** Technical-harness writes to `supabase/migrations/`. On the web-chat
+surface (CRM Builder) the launcher stages migrations in `supabase/migrations-pending/`
+and promotes them to `supabase/migrations/` after the user confirms: follow the
+launcher's staging, do not invent your own pending/promoted handling. The
+regenerate-one-net-migration rule above applies either way (it is about NOT stacking,
+independent of which directory the launcher stages into).
+
+**Checkpoint:** at most ONE session-owned migration file exists, and its SQL is the
+full net delta from `session-base` (not an increment on a prior session migration).
### 4. Write idempotent SQL