A full-featured CRM built with React, shadcn/ui, and Supabase.
Enhance harness revert functionality with explicit strategy options and improve logging
Jerome4f80ad9d2d5a
diff --git a/.claude/commands/harness-revert.md b/.claude/commands/harness-revert.mdindex c06646ad..968b8581 100644--- a/.claude/commands/harness-revert.md+++ b/.claude/commands/harness-revert.md@@ -12,12 +12,15 @@ Revert everything a harness session merged into your branch, then remove its tem- exactly one → use it.- several → list them (short id + base branch via `git config --local --get sessionbase.<short>.branch` + fork date via `git --no-pager log -1 --format=%cr session-base/<short>`) and ask which with `AskUserQuestion`. Because this is destructive, **always** make the user choose explicitly when more than one exists — never silently pick the latest.-2. **Show what will be undone, then confirm.** Run `git --no-pager diff --stat session-base/<short>..session/<short>` and name the base branch it landed on. Then ask the user to confirm with `AskUserQuestion` (yes / no). Do not proceed without an explicit yes.+2. **Show what will be undone, then confirm — including the strategy.** Run `git --no-pager diff --stat session-base/<short>..session/<short>` and name the base branch it landed on. Ask the user to confirm with `AskUserQuestion`, offering the two strategies (default unless the user already said which):+ - **revert** (default, safe): keeps history, adds a revert commit; safe even if other sessions landed on the same branch afterwards.+ - **hard** (clean history): `git reset --hard` to the session's fork point — the session's commits disappear entirely (no merge, no revert commit), but anything else committed after the fork is also dropped. Best when this session is the only thing on the branch (e.g. demos). If the user asked to "completely remove the branch / drop the commits / clean history", pick this.+ Do not proceed without an explicit yes.-3. **On confirmation, run the script** from the repo root:+3. **On confirmation, run the script** from the repo root (append `--hard` for the clean-history strategy):```- node "$(git rev-parse --show-toplevel)/scripts/harness-revert.mjs" <short>+ node "$(git rev-parse --show-toplevel)/scripts/harness-revert.mjs" <short> [--hard]```- (Run it once with `--dry-run` appended first if you want to preview the exact git actions.) Relay the script's output to the user. It reverts this session's promotion merge commit(s) — safe even if other sessions landed on the same branch — then removes the session's worktrees, branches, `sessionbase.<short>` config, and `/tmp` dir.+ (Run it once with `--dry-run` appended first if you want to preview the exact git actions.) Relay the script's output. Default mode reverts this session's promotion merge commit(s) (safe even if other sessions landed on the same branch); `--hard` resets the base branch to the fork point (refuses if history diverged). Either way it then removes the session's worktrees, branches, `sessionbase.<short>` config, and `/tmp` dir.4. **If the script reports a conflict**, tell the user which step stopped and that another session likely touched the same files; they must resolve it manually before re-running. Never force the revert.diff --git a/.claude/hooks/record-review-verdict.mjs b/.claude/hooks/record-review-verdict.mjsindex 8f68b422..44de88a7 100755--- a/.claude/hooks/record-review-verdict.mjs+++ b/.claude/hooks/record-review-verdict.mjs@@ -161,8 +161,12 @@ for (let i = verdictLines.length - 1; i >= 0; i--) {}}+const _tp = input.agent_transcript_path || input.transcript_path || "";ctx.log(- `role=${role || "UNKNOWN"} task=${task || "UNKNOWN"} verdict=${verdict || "UNKNOWN"}`,+ `role=${role || "UNKNOWN"} task=${task || "UNKNOWN"} verdict=${verdict || "UNKNOWN"}` ++ (role && task && verdict+ ? ""+ : ` | DIAG agent_type=${JSON.stringify(input.agent_type)} agent_name=${JSON.stringify(input.agent_name)} last_msg=${input.last_assistant_message ? "present" : "absent"} tp=${_tp || "none"} tp_exists=${Boolean(_tp && existsSync(_tp))} keys=[${Object.keys(input).join(",")}]`),);if (!role || !task) process.exit(0); // can't key the flag — leave state untoucheddiff --git a/scripts/harness-monitor.mjs b/scripts/harness-monitor.mjsindex 30a4fdaf..7cebe10c 100755--- a/scripts/harness-monitor.mjs+++ b/scripts/harness-monitor.mjs@@ -39,7 +39,10 @@ const CLAUDE_PROJ = join(homedir(),".claude","projects",- REPO.replace(/\//g, "-"),+ // Claude Code names the project dir by replacing EVERY non-alphanumeric char+ // (slashes AND dots, e.g. ".code-workspace") with "-". Replacing only "/" broke+ // discovery for any repo path containing a dot → "no subagent transcripts found".+ REPO.replace(/[^a-zA-Z0-9]/g, "-"),);// A session id is a worktree-base subdir that has a hooks.log. Test fixtures// (td-1, td-2, …) are skipped — they are not real sessions.diff --git a/scripts/harness-revert.mjs b/scripts/harness-revert.mjsindex 9903064f..000f9e77 100755--- a/scripts/harness-revert.mjs+++ b/scripts/harness-revert.mjs@@ -27,13 +27,14 @@ import { join } from "node:path";const argv = process.argv.slice(2);const DRY_RUN = argv.includes("--dry-run") || argv.includes("-n");+const HARD = argv.includes("--hard");const SHORT = argv.find((a) => !a.startsWith("-"));const log = (msg) => process.stdout.write(`${msg}\n`);const err = (msg) => process.stderr.write(`${msg}\n`);if (!SHORT) {- err("usage: harness-revert.mjs <SESSION_SHORT_ID> [--dry-run]");+ err("usage: harness-revert.mjs <SESSION_SHORT_ID> [--hard] [--dry-run]");process.exit(2);}@@ -76,32 +77,11 @@ log(`==> Session: ${SHORT}`);log(`==> Base branch: ${BASE_BRANCH || "(unknown — cleanup only)"}`);if (DRY_RUN) log("==> Mode: DRY-RUN (no changes)");-// 1. Find this session's promotion merge commits on the base branch (newest-// first, so reverting in order unwinds the stack cleanly).-let merges = [];-if (- BASE_BRANCH &&- git(["show-ref", "--verify", "--quiet", `refs/heads/${BASE_BRANCH}`]).code ===- 0-) {- const subject = `merge(session): ${SHORT}`;- merges = git(["log", "--merges", "--format=%H%x09%s", BASE_BRANCH])- .out.split("\n")- .filter(Boolean)- .map((line) => line.split("\t"))- .filter(([, subj]) => subj === subject)- .map(([sha]) => sha);-}+if (HARD)+ log("==> Mode: HARD (reset to fork — drops the session's commits)");-// 2. Revert each promotion merge. `-m 1` keeps the base-branch side and backs out-// everything the session merged in. Abort + report on conflict (a parallel-// session may have touched the same files) — the user resolves, like the-// resolving-rollback-conflicts flow.-if (merges.length === 0) {- log(- `==> No promotion merge found for session ${SHORT} — nothing to revert (cleanup only).`,- );-} else {+// Guard: undoing commits requires being ON the base branch the session promoted to.+const requireOnBase = () => {if (BASE_BRANCH && CURRENT_BRANCH !== BASE_BRANCH) {err(`!! You are on '${CURRENT_BRANCH}' but session ${SHORT} promoted onto '${BASE_BRANCH}'.`,@@ -109,23 +89,87 @@ if (merges.length === 0) {err(` Switch to it first: git checkout ${BASE_BRANCH}`);process.exit(1);}- // Never let a revert clobber uncommitted work.+};+// Never let an undo clobber uncommitted work.+const stashDirty = () => {if (git(["status", "--porcelain", "--untracked-files=no"]).out) {log("==> Stashing uncommitted changes (recover with: git stash pop)");gitRun(["stash", "push", "-m", `harness-revert ${SHORT} safety stash`]);}- for (const sha of merges) {- log(`==> Reverting promotion merge ${sha.slice(0, 12)}`);- const r = gitRun(["revert", "--no-edit", "-m", "1", sha]);- if (r.code !== 0) {- git(["revert", "--abort"]);- err(- `!! Revert hit a conflict on ${sha.slice(0, 12)} (another session may have touched the same files).`,- );- err(- " Resolve it manually, then re-run. Aborted before cleanup — nothing was removed.",- );- process.exit(1);+};++// Undo the session's promoted work. Two strategies:+// default → `git revert` the promotion merge commit(s): keeps history, adds a+// revert commit, SAFE even if other sessions landed on the same branch+// afterwards (it only backs out THIS session's changes).+// --hard → `git reset --hard` to the session's fork point (session-base/<short>):+// drops the session's commits ENTIRELY (clean history — no merge, no+// revert commit), but ALSO drops anything else committed after the fork.+// Use it when this session is the only thing on the branch (e.g. demos).+if (HARD) {+ // Resolve the fork anchor NOW, before the cleanup section deletes it below.+ const anchor = `session-base/${SHORT}`;+ const fork = git(["rev-parse", "--verify", `refs/heads/${anchor}`]);+ if (fork.code !== 0 || !fork.out) {+ err(+ `!! No fork anchor '${anchor}' — can't hard-reset. Run without --hard to revert instead.`,+ );+ process.exit(1);+ }+ const forkSha = fork.out;+ requireOnBase();+ if (git(["merge-base", "--is-ancestor", forkSha, "HEAD"]).code !== 0) {+ err(+ `!! ${anchor} (${forkSha.slice(0, 12)}) is not an ancestor of HEAD — history diverged, refusing to hard-reset. Use the default (revert) mode.`,+ );+ process.exit(1);+ }+ stashDirty();+ log(+ `==> Hard-resetting ${CURRENT_BRANCH} to ${anchor} (${forkSha.slice(0, 12)}) — the session disappears from history`,+ );+ const r = gitRun(["reset", "--hard", forkSha]);+ if (r.code !== 0) {+ err(`!! reset --hard failed: ${r.err}`);+ process.exit(1);+ }+} else {+ // Find this session's promotion merge commits on the base branch (newest first,+ // so reverting in order unwinds the stack cleanly).+ let merges = [];+ if (+ BASE_BRANCH &&+ git(["show-ref", "--verify", "--quiet", `refs/heads/${BASE_BRANCH}`])+ .code === 0+ ) {+ const subject = `merge(session): ${SHORT}`;+ merges = git(["log", "--merges", "--format=%H%x09%s", BASE_BRANCH])+ .out.split("\n")+ .filter(Boolean)+ .map((line) => line.split("\t"))+ .filter(([, subj]) => subj === subject)+ .map(([sha]) => sha);+ }+ if (merges.length === 0) {+ log(+ `==> No promotion merge found for session ${SHORT} — nothing to revert (cleanup only).`,+ );+ } else {+ requireOnBase();+ stashDirty();+ for (const sha of merges) {+ log(`==> Reverting promotion merge ${sha.slice(0, 12)}`);+ const r = gitRun(["revert", "--no-edit", "-m", "1", sha]);+ if (r.code !== 0) {+ git(["revert", "--abort"]);+ err(+ `!! Revert hit a conflict on ${sha.slice(0, 12)} (another session may have touched the same files).`,+ );+ err(+ " Resolve it manually, then re-run. Aborted before cleanup — nothing was removed.",+ );+ process.exit(1);+ }}}}