Skip to content

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

Record migration-approval provenance in a durable trail (verify finding 2)

Jeromed44d89c1cab6
diff --git a/.claude/agents/merger.md b/.claude/agents/merger.md
index 31e17d1b..fa6bc68b 100644
--- a/.claude/agents/merger.md
+++ b/.claude/agents/merger.md
@@ -53,6 +53,7 @@ The orchestrator parses this line by regex. Any other format is treated as `FAIL
| `WORKTREE_PATH` | Stage A / SIMPLE / MIGRATION | Absolute path to the feature worktree (`<WORKTREE_BASE>/<TASK_ID>` or `<WORKTREE_BASE>/simple`). |
| `SESSION_SHORT_ID` | always recommended | Short session id. The orchestrator passes it directly. If absent, derive it as the first `-`-segment of `basename(TICKETS_DIR)` (wave) or of the session-id directory in `WORKTREE_PATH`. |
| `TICKETS_DIR` | wave only | Directory holding ticket JSON files; absent in SIMPLE / migration / rollback flow. |
+| `APPROVAL_TRAILER` | MIGRATION only | One-line `Approved-by-user: ...` provenance the orchestrator built from the user's migration approval. When present, append it to the migration merge commit (Stage A) so the approval trail lives in git history. Absent = no trailer. |
`WORKTREE_BASE` is the per-session worktree root the `setup-worktree` hook uses — defined in `.claude/rules/worktree-scope.md` as `/tmp/<$CLAUDE_PROJECT_DIR with every "/" replaced by "_">/<SESSION_ID>` (the repository itself is `$CLAUDE_PROJECT_DIR`, never `/app`). The integration worktree is `<WORKTREE_BASE>/_session`.
@@ -82,6 +83,8 @@ The orchestrator parses this line by regex. Any other format is treated as `FAIL
```
`<type>` = ticket's `type` field (feat / fix / chore). On `CONFLICT`: `git merge --abort`, emit `FAILED: <TASK_ID> merge conflict in <files>`, stop. Do NOT resolve — the developer rebases onto `session/<SESSION_SHORT_ID>` and retries.
+ **MIGRATION mode with `APPROVAL_TRAILER`**: append it as a second commit paragraph so the migration's approval provenance is recorded in git history (audit finding: PD-ASK provenance), e.g. `git merge --no-ff <BRANCH_NAME> -m "chore(MIGRATION): apply approved schema change" -m "<APPROVAL_TRAILER>"`.
+
3. **Update ticket status** (skip when `TASK_ID` is `SIMPLE` / `MIGRATION` / `ROLLBACK` or `TICKETS_DIR` is absent)
```bash
if [ -n "${TICKETS_DIR:-}" ] && [ "${TASK_ID}" != "SIMPLE" ] && [ "${TASK_ID}" != "MIGRATION" ] && [ "${TASK_ID}" != "ROLLBACK" ]; then
diff --git a/.claude/agents/orchestrator.md b/.claude/agents/orchestrator.md
index 8a31c298..1e528fda 100644
--- a/.claude/agents/orchestrator.md
+++ b/.claude/agents/orchestrator.md
@@ -549,6 +549,7 @@ Ask the user whether the changes look right or need adjustment — confirm BEFOR
Fresh process: trust disk, not memory. The user already approved at PD-ASK and your dispatch prompt carries that approval, so **do NOT re-ask — skip PD-ASK/PD-RESPOND**.
1. Derive `SESSION_SHORT_ID` / `TICKETS_DIR` / `WORKTREE_BASE` from `<session_dir>`.
+1b. **Read the approval record** `<session_dir>/migration-approval.json` (the coordinator wrote it at PD-ASK). Build a one-line provenance trailer from it and carry it to STATE PD-MIG-MERGE: `Approved-by-user: "<answer>" to "<question>" at <approved_at> (session <SESSION_SHORT_ID>, via AskUserQuestion; record=<session_dir>/migration-approval.json)`. If the record is absent (older flow), proceed without a trailer and note it in your report - do not fabricate one.
2. Capture business knowledge once (the same background Mode-2 documentator dispatch shown in PD-RESPOND below).
3. Confirm there is something to apply: `Bash("node \"$CLAUDE_PROJECT_DIR/.claude/scripts/pending-deploys.mjs\" --app $CLAUDE_PROJECT_DIR --session <SESSION_SHORT_ID>")`. Empty + exit 0 → report done, STATE DONE. **Non-zero exit → UNDETERMINED; surface the error, do not guess.** Non-empty → one progress line (*"saving your changes"*) and enter STATE PD-MIG-DEV.
@@ -600,7 +601,7 @@ Dispatch the single-shot MIGRATION merger for `<SESSION_SHORT_ID>/simple` (Stage
Agent({
subagent_type: "merger",
description: "Merge migration branch <SESSION_SHORT_ID>/simple",
- prompt: "ROLE: merger (MIGRATION mode — single-shot, no team)\nSESSION_SHORT_ID: <SESSION_SHORT_ID>\nBRANCH_NAME: <SESSION_SHORT_ID>/simple\nWORKTREE_PATH: <WORKTREE_BASE>/simple\n\nFollow the WORKFLOW in merger.md. Use the MIGRATION-mode columns (Stage A then promotion in one shot).\nOutput: \"DONE: MIGRATION commit=<short sha>\" OR \"FAILED: MIGRATION <reason>\""
+ prompt: "ROLE: merger (MIGRATION mode, single-shot, no team)\nSESSION_SHORT_ID: <SESSION_SHORT_ID>\nBRANCH_NAME: <SESSION_SHORT_ID>/simple\nWORKTREE_PATH: <WORKTREE_BASE>/simple\nAPPROVAL_TRAILER: <the one-line trailer built in PD-APPLY step 1b; omit this line if there was no approval record>\n\nFollow the WORKFLOW in merger.md. Use the MIGRATION-mode columns (Stage A then promotion in one shot). Append APPROVAL_TRAILER to the migration merge commit so the approval provenance lives in git history.\nOutput: \"DONE: MIGRATION commit=<short sha>\" OR \"FAILED: MIGRATION <reason>\""
})
```
**End turn.** → `DONE` → STATE PD-DEPLOY. `FAILED`/`promote conflict` → STATE PD-PROMOTE-FIX.
diff --git a/CLAUDE.md b/CLAUDE.md
index 759ec420..6ba71459 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -9,7 +9,7 @@ Code-change requests can be handled by the **agent harness**: a team of subagent
**"Launched" is NOT "done" (read before you fill the silence).** The dispatch is *meant* to block until the orchestrator's final report, but in some runtimes (interactive Claude Code / the VS Code extension) the `Agent` tool returns immediately with `Async agent launched successfully … agentId: <id>` and the real result arrives later as a `task-notification`. That acknowledgement means dispatched, not finished. Do NOT read the quiet as "nothing is happening", and do NOT start implementing the feature yourself (no `Write`/`Edit`/`Bash` on the change, no re-dispatch): that is the exact bug that produces a duplicate, the main thread coding the same feature the orchestrator's developer is already building. While it runs your ONLY job is to surface progress and then relay the final `task-notification` report: for a `#technical-harness` run, stream its progress log into the chat (recipe under "Opting in"); for a plain `#harness` run there is no log, so just wait for the notification. This mirrors the guard the orchestrator applies to its own workers (`.claude/agents/orchestrator.md` → "launched is NOT done"). On the orchestrator's completion, before relaying, check `<session_dir>/needs-recovery`: the `completion-invariant` hook writes it when the orchestrator stopped with APPROVED-but-unmerged work (a nested orchestrator cannot resume itself). If present, dispatch a FRESH `orchestrator` whose prompt begins with `<intent>recovery</intent>` and passes the same `<session_dir>` (never `SendMessage` the old one); wait for it, then relay. It clears the marker on a clean finish.
**PD-ASK round-trip (migration confirmation).** The orchestrator ends its turn with a pending question, typically *"apply the database migration now?"*, and the task completes. Relay it to the user (plain text or `AskUserQuestion`). Do NOT resume the old orchestrator with `SendMessage` to relay the answer: the runtime tags coordinator messages as carrying no user authority, so a relayed approval is ignored and the orchestrator loops re-asking forever. On the user's reply:
-- **Approved**: dispatch a **fresh** `orchestrator` (new `Agent` call) whose prompt begins with `<intent>apply-migration</intent>`, states the approval, and passes the same `<session_dir>`; it resumes the migration round from disk and applies it.
+- **Approved**: FIRST record the approval durably (otherwise its only audit trail is this conversation): write `<session_dir>/migration-approval.json` = `{"kind":"migration-approval","session_id":"<id>","question":"<what you asked>","answer":"<the user's verbatim reply>","approved_at":"<ISO-8601>","via":"AskUserQuestion"}`. THEN dispatch a **fresh** `orchestrator` (new `Agent` call) whose prompt begins with `<intent>apply-migration</intent>`, states the approval, references that record, and passes the same `<session_dir>`; it resumes the migration round from disk, applies it, and stamps the provenance onto the migration commit. The built-in security warning on a relayed approval still fires and is expected: the record is the durable trail, not a suppression of that warning.
- **Wants changes**: dispatch a fresh `orchestrator` with the new request as usual.
While that fresh dispatch runs, do not start a parallel plan B (don't generate the migration yourself, don't `TaskStop` it); wait for it to finish, then relay its result.