Skip to content

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

feat(harness): show worktrees as editor folders for live per-task diffs

Jerome3f5cee9a9e48
diff --git a/.claude/hooks/cleanup-session.mjs b/.claude/hooks/cleanup-session.mjs
index 59ac3dfd..f08e18fd 100755
--- a/.claude/hooks/cleanup-session.mjs
+++ b/.claude/hooks/cleanup-session.mjs
@@ -24,6 +24,7 @@ import { join } from "node:path";
import { createHookContext } from "./lib/context.mjs";
import { REPO } from "./lib/paths.mjs";
import { removeWorktreesUnder } from "./lib/worktree.mjs";
+import { removeWorktreeFolders } from "./lib/workspace-folders.mjs";
if (process.env.CHAT_SESSION_DIR) process.exit(0);
@@ -41,6 +42,13 @@ try {
// session ends). Base removal is last: nothing may log after it (this hook's log
// file lives inside it and logging would recreate it).
removeWorktreesUnder(ctx.worktreeBase);
+ // Drop this session's worktree folders from the editor workspace (setup-worktree
+ // added them on a technical run). Path prefix match on the session's worktree
+ // base, so a concurrent session's folders stay intact.
+ removeWorktreeFolders(
+ ctx.repo,
+ (p) => p === ctx.worktreeBase || p.startsWith(ctx.worktreeBase + "/"),
+ );
rmSync(join(REPO, "test-results"), { recursive: true, force: true });
// This session's rendered board only (render-status.mjs). Per-session subdir,
// so a concurrent session's board under .harness/<other-short> is left intact.
diff --git a/.claude/hooks/cleanup-worktree.mjs b/.claude/hooks/cleanup-worktree.mjs
index 15762227..6f9815fe 100755
--- a/.claude/hooks/cleanup-worktree.mjs
+++ b/.claude/hooks/cleanup-worktree.mjs
@@ -29,6 +29,7 @@ import {
sessionBranch,
} from "./lib/topology.mjs";
import { removeWorktree } from "./lib/worktree.mjs";
+import { removeWorktreeFolders } from "./lib/workspace-folders.mjs";
const raw = readFileSync(0, "utf8");
const ctx = createHookContext(raw, "cleanup-worktree");
@@ -130,6 +131,14 @@ toRemove.forEach((e) => {
});
toRemove.map((e) => e.branch).forEach(deleteBranch);
+// Drop the merged worktrees from the editor's workspace folders (setup-worktree
+// added them on a technical run). No-op under a managed launcher or a mono-folder
+// window (no `.code-workspace` to edit; `code --remove` does not exist).
+if (!process.env.CHAT_SESSION_DIR && toRemove.length) {
+ const removed = new Set(toRemove.map((e) => e.path));
+ removeWorktreeFolders(ctx.repo, (p) => removed.has(p));
+}
+
git(["worktree", "prune"]);
const registered = getWorktreePaths();
diff --git a/.claude/hooks/lib/workspace-folders.mjs b/.claude/hooks/lib/workspace-folders.mjs
new file mode 100644
index 00000000..38e1de83
--- /dev/null
+++ b/.claude/hooks/lib/workspace-folders.mjs
@@ -0,0 +1,88 @@
+// Show a harness worktree in the editor by adding it as a workspace folder, so
+// its files AND its Source Control diff appear live during a run. Two mechanisms,
+// auto-detected:
+//
+// - A `.code-workspace` file beside the repo (multi-root workspace): edit its
+// `folders` array. VS Code live-reloads the file, so add AND remove are clean.
+// - A mono-folder window (no `.code-workspace`): `code --add <path>` asks the
+// running VS Code window to add the folder (which turns it into an untitled
+// multi-root workspace). There is no `code --remove` in the VS Code CLI, so
+// removal is a no-op here; the untitled workspace is not persisted, so the
+// entry is gone on the next fresh open anyway.
+//
+// Every function is best-effort and never throws: a convenience view must not
+// break a run. A parse failure leaves the file untouched (never corrupt it).
+
+import { existsSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { exec } from "./process.mjs";
+
+// The `.code-workspace` file beside the repo (the multi-root workspace root is
+// dirname(repo)). Returns its absolute path, or null (mono-folder / none).
+export function workspaceFile(repo) {
+ const root = dirname(repo);
+ try {
+ const name = readdirSync(root).find((n) => n.endsWith(".code-workspace"));
+ return name ? join(root, name) : null;
+ } catch {
+ return null;
+ }
+}
+
+function readWorkspace(file) {
+ try {
+ const ws = JSON.parse(readFileSync(file, "utf8"));
+ return ws && Array.isArray(ws.folders) ? ws : null;
+ } catch {
+ return null; // JSONC/comments or unreadable β€” do not touch it
+ }
+}
+
+function writeWorkspace(file, ws) {
+ try {
+ writeFileSync(file, JSON.stringify(ws, null, 2) + "\n");
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+// A VS Code window is reachable from a hook subprocess (its IPC env is present).
+const canDriveWindow = () =>
+ Boolean(process.env.VSCODE_IPC_HOOK_CLI || process.env.REMOTE_CONTAINERS_IPC);
+
+/**
+ * Add `path` (an absolute worktree path) as a workspace folder named `name`.
+ * @returns {boolean} whether the folder was added (or already present).
+ */
+export function addWorktreeFolder(repo, path, name) {
+ if (!existsSync(path)) return false;
+ const file = workspaceFile(repo);
+ if (file) {
+ const ws = readWorkspace(file);
+ if (!ws) return false;
+ if (ws.folders.some((f) => f.path === path)) return true; // idempotent
+ ws.folders.push({ path, name });
+ return writeWorkspace(file, ws);
+ }
+ // Mono-folder window: ask VS Code to add it (add-only, self-heals on reopen).
+ if (canDriveWindow()) return exec("code", ["--add", path]).status === 0;
+ return false;
+}
+
+/**
+ * Remove every workspace folder whose absolute path satisfies `matches(path)`
+ * (e.g. a prefix match on the session's worktree base, or an exact path). Only
+ * the `.code-workspace` mechanism supports removal; mono-folder is a no-op.
+ * @returns {boolean} whether anything was removed.
+ */
+export function removeWorktreeFolders(repo, matches) {
+ const file = workspaceFile(repo);
+ if (!file) return false;
+ const ws = readWorkspace(file);
+ if (!ws) return false;
+ const kept = ws.folders.filter((f) => !(f.path && matches(f.path)));
+ if (kept.length === ws.folders.length) return false;
+ ws.folders = kept;
+ return writeWorkspace(file, ws);
+}
diff --git a/.claude/hooks/setup-worktree.mjs b/.claude/hooks/setup-worktree.mjs
index c70d22d0..3cd04125 100755
--- a/.claude/hooks/setup-worktree.mjs
+++ b/.claude/hooks/setup-worktree.mjs
@@ -32,6 +32,7 @@ import { parseDispatch } from "./lib/dispatch-parse.mjs";
import { getBaseBranch, getWorktreePaths, git } from "./lib/git.mjs";
import { REVIEW_ROLES, reviewFlag } from "./lib/reviews.mjs";
import { getFirstTaskId } from "./lib/teams.mjs";
+import { addWorktreeFolder } from "./lib/workspace-folders.mjs";
import {
simpleBranch,
simpleWorktreePath,
@@ -239,6 +240,22 @@ if (add.status !== 0) {
ctx.log(`CREATED branch=${branchName} path=${worktreePath}`);
toProvision.push(worktreePath);
+// Surface this worktree in the editor (files + live Source Control) for a
+// technical run, so the developer can watch the code being modified. Done under
+// the session lock so a parallel wave's edits to the shared .code-workspace do
+// not race. No-op under a managed launcher (owns its own UI) or a non-technical
+// run (no progress log), matching the render-status board's gating.
+if (
+ !process.env.CHAT_SESSION_DIR &&
+ existsSync(join(ctx.sessionDir, "harness-progress.log"))
+) {
+ addWorktreeFolder(
+ ctx.repo,
+ worktreePath,
+ `🎫 ${taskId || "simple"} · ${ctx.sessionShort}`,
+ );
+}
+
// Git-mutation region is done β€” release the lock before provisioning so a
// parallel-wave waiter can serialise its own git ops while this hook copies
// node_modules. Distinct target dirs, copied from $REPO, so the copies can run
diff --git a/.claude/hooks/test/workspace-folders.test.mjs b/.claude/hooks/test/workspace-folders.test.mjs
new file mode 100644
index 00000000..07b0bcd9
--- /dev/null
+++ b/.claude/hooks/test/workspace-folders.test.mjs
@@ -0,0 +1,103 @@
+// Tests for workspace-folders.mjs: adding/removing a harness worktree as a VS Code
+// workspace folder. Exercises the `.code-workspace` file mechanism (clean add +
+// remove) and the mono-folder no-op path. IPC env is cleared in beforeEach so the
+// mono path can never shell out to `code --add` against a real window during tests.
+
+import {
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, beforeEach, describe, expect, test } from "vitest";
+import {
+ addWorktreeFolder,
+ removeWorktreeFolders,
+} from "../lib/workspace-folders.mjs";
+
+let TMP = null;
+let saved = {};
+beforeEach(() => {
+ saved = {
+ ipc: process.env.VSCODE_IPC_HOOK_CLI,
+ rc: process.env.REMOTE_CONTAINERS_IPC,
+ };
+ delete process.env.VSCODE_IPC_HOOK_CLI;
+ delete process.env.REMOTE_CONTAINERS_IPC;
+});
+afterEach(() => {
+ if (saved.ipc !== undefined) process.env.VSCODE_IPC_HOOK_CLI = saved.ipc;
+ if (saved.rc !== undefined) process.env.REMOTE_CONTAINERS_IPC = saved.rc;
+ if (TMP) rmSync(TMP, { recursive: true, force: true });
+ TMP = null;
+});
+
+// A workspace root containing the repo folder `app` and (optionally) a
+// `.code-workspace` file beside it, plus a worktree dir living outside the repo.
+const setup = ({ withWorkspaceFile = true } = {}) => {
+ TMP = mkdtempSync(join(tmpdir(), "workspace-folders-test-"));
+ const root = join(TMP, "root");
+ const repo = join(root, "app");
+ mkdirSync(repo, { recursive: true });
+ const wsFile = join(root, "proj.code-workspace");
+ if (withWorkspaceFile) {
+ writeFileSync(
+ wsFile,
+ JSON.stringify({ folders: [{ path: "app", name: "app" }] }, null, 2),
+ );
+ }
+ const wt = join(TMP, "wt", "TASK-001");
+ mkdirSync(wt, { recursive: true });
+ const folders = () => JSON.parse(readFileSync(wsFile, "utf8")).folders;
+ return { repo, wt, wsFile, folders };
+};
+
+describe("workspace-folders", () => {
+ test("adds a worktree folder to the .code-workspace", () => {
+ const { repo, wt, folders } = setup();
+ expect(addWorktreeFolder(repo, wt, "🎫 TASK-001")).toBe(true);
+ const f = folders();
+ expect(f).toContainEqual({ path: wt, name: "🎫 TASK-001" });
+ // the original repo folder is preserved
+ expect(f).toContainEqual({ path: "app", name: "app" });
+ });
+
+ test("is idempotent (no duplicate entry)", () => {
+ const { repo, wt, folders } = setup();
+ addWorktreeFolder(repo, wt, "🎫 TASK-001");
+ addWorktreeFolder(repo, wt, "🎫 TASK-001");
+ expect(folders().filter((f) => f.path === wt)).toHaveLength(1);
+ });
+
+ test("removeWorktreeFolders removes entries matching the predicate", () => {
+ const { repo, wt, folders } = setup();
+ addWorktreeFolder(repo, wt, "🎫 TASK-001");
+ expect(removeWorktreeFolders(repo, (p) => p === wt)).toBe(true);
+ expect(folders().some((f) => f.path === wt)).toBe(false);
+ // the original repo folder survives
+ expect(folders()).toContainEqual({ path: "app", name: "app" });
+ });
+
+ test("does not add a non-existent worktree path", () => {
+ const { repo, folders } = setup();
+ expect(addWorktreeFolder(repo, join(TMP, "nope"), "x")).toBe(false);
+ expect(folders()).toHaveLength(1);
+ });
+
+ test("never corrupts a .code-workspace it cannot parse (JSONC)", () => {
+ const { repo, wt, wsFile } = setup();
+ const withComment = '// my workspace\n{ "folders": [{ "path": "app" }] }\n';
+ writeFileSync(wsFile, withComment);
+ expect(addWorktreeFolder(repo, wt, "x")).toBe(false);
+ expect(readFileSync(wsFile, "utf8")).toBe(withComment);
+ });
+
+ test("mono-folder with no IPC: add and remove are no-ops", () => {
+ const { repo, wt } = setup({ withWorkspaceFile: false });
+ expect(addWorktreeFolder(repo, wt, "x")).toBe(false);
+ expect(removeWorktreeFolders(repo, () => true)).toBe(false);
+ });
+});