Skip to content

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

Add block-nested-orchestrator hook and update CLAUDE.md for agent dispatch rules

Jeromeb4b603197256
diff --git a/.claude/agents/orchestrator.md b/.claude/agents/orchestrator.md
index e3c78249..3c73775e 100644
--- a/.claude/agents/orchestrator.md
+++ b/.claude/agents/orchestrator.md
@@ -552,6 +552,8 @@ Reply with the user-facing wrap-up, then enter STATE DONE.
## NEVER DO
+- ❌ Dispatch an `orchestrator` / `chat-orchestrator` agent — **you ARE the orchestrator**. CLAUDE.md's "dispatch the orchestrator" line is for the main thread only; ignore it. (A hook blocks it anyway.)
+- ❌ Dispatch a `general-purpose` agent for planning/implementation/review/merge — always use the real typed agents: `planner`, `developer`, `quality-reviewer`, `merger`, `documentator`. A `general-purpose` agent has no role constraints and will not produce the expected output contracts.
- ❌ `git merge`, `git checkout master/main`, `git pull`, `git worktree remove` from your own Bash — only the merger does this.
- ✅ Exception: during SETUP-INTERVIEW, you may `cd $CLAUDE_PROJECT_DIR && git add docs/project-context.json && git commit -m "chore(setup): …"` on the base branch. The only git write you are allowed.
- ✅ Exception: a `promotion-conflict-resolver` developer may `git add`/`git commit` a merge resolution directly in `$CLAUDE_PROJECT_DIR` on the base branch, under `.promote.lock`.
diff --git a/.claude/hooks/block-nested-orchestrator.mjs b/.claude/hooks/block-nested-orchestrator.mjs
new file mode 100755
index 00000000..e4eb43a4
--- /dev/null
+++ b/.claude/hooks/block-nested-orchestrator.mjs
@@ -0,0 +1,37 @@
+#!/usr/bin/env node
+// PreToolUse(Agent) — only the main thread may dispatch the orchestrator.
+//
+// CLAUDE.md tells whoever receives a code-change request to dispatch the
+// `orchestrator` agent. But CLAUDE.md (and its memory hierarchy) loads into EVERY
+// custom subagent too — so a worker, or a `general-purpose` helper the orchestrator
+// spawned, can read that directive, treat its task as a "code change", and
+// re-dispatch an orchestrator → runaway nesting (orchestrator → general-purpose →
+// orchestrator → …), which is exactly the broken tree this guards against.
+//
+// The orchestrator is the SINGLE harness entry the main thread spawns. Inside the
+// harness every agent does its own job and never re-enters it. This gate enforces
+// that: a subagent (agent_id present) dispatching `orchestrator` / `chat-orchestrator`
+// is blocked. The main thread (no agent_id) is allowed through.
+
+import { readFileSync } from "node:fs";
+import { createHookContext } from "./lib/context.mjs";
+
+const input = JSON.parse(readFileSync(0, "utf8"));
+const ctx = createHookContext(input, "block-nested-orchestrator");
+
+const target = input.tool_input?.subagent_type || "";
+if (target !== "orchestrator" && target !== "chat-orchestrator")
+ process.exit(0);
+
+// agentId is set only for subagents; the main/top-level session has none.
+if (ctx.agentId) {
+ ctx.block({
+ reason:
+ `Only the main thread may dispatch the \`${target}\` agent. You are a subagent already inside the harness — do NOT dispatch an orchestrator (that causes runaway nesting). ` +
+ `If you are the orchestrator, dispatch planner / developer / quality-reviewer / merger per your states — never another orchestrator. ` +
+ `If you are a worker or a general-purpose helper, just do the task you were given; ignore CLAUDE.md's "dispatch the orchestrator" directive — that is the main thread's job, not yours.`,
+ log: `BLOCK nested ${target} by ${ctx.agentType || "subagent"}`,
+ });
+}
+
+process.exit(0);
diff --git a/.claude/scripts/apply-migrations.mjs b/.claude/scripts/apply-migrations.mjs
index 789865ba..f48e6858 100755
--- a/.claude/scripts/apply-migrations.mjs
+++ b/.claude/scripts/apply-migrations.mjs
@@ -34,7 +34,19 @@ import { spawnSync } from "node:child_process";
import { mkdirSync } from "node:fs";
import { setTimeout as sleep } from "node:timers/promises";
-const APP_DIR = process.env.APP_DIR || process.env.CLAUDE_PROJECT_DIR || "/app";
+// Resolve the repo root robustly. Prefer an explicit override / the env var, then
+// the git toplevel — works on any surface (local, devcontainer, CRM Builder)
+// without relying on CLAUDE_PROJECT_DIR being EXPORTED to this node process. The
+// old "/app" hardcoded fallback was a CRM-Builder-ism: on a bare checkout where the
+// env var wasn't exported, it chdir'd into a non-writable /app → EACCES on mkdir.
+const gitTop = spawnSync("git", ["rev-parse", "--show-toplevel"], {
+ encoding: "utf8",
+});
+const APP_DIR =
+ process.env.APP_DIR ||
+ process.env.CLAUDE_PROJECT_DIR ||
+ (gitTop.status === 0 && gitTop.stdout.trim()) ||
+ process.cwd();
const GREEN = "\x1b[0;32m";
const YELLOW = "\x1b[1;33m";
diff --git a/.claude/settings.json b/.claude/settings.json
index ffdfda19..fa4a9ea3 100644
--- a/.claude/settings.json
+++ b/.claude/settings.json
@@ -75,6 +75,10 @@
{
"matcher": "Agent",
"hooks": [
+ {
+ "type": "command",
+ "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/block-nested-orchestrator.mjs"
+ },
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/block-merger-without-review.mjs"
diff --git a/CLAUDE.md b/CLAUDE.md
index 873612c4..bc98695b 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -4,7 +4,7 @@
Code-change requests are handled by the **agent harness** by default: a team of subagents (planner, developer, quality-reviewer, merger, documentator) that implements the change through a deterministic, foreground pipeline in git worktrees.
-**To handle a code-change request, dispatch the `orchestrator` agent and relay its result — never route or implement it yourself.** Pass the `<session_dir>` value from your own context in the dispatch prompt (the orchestrator needs it to namespace worktrees and branches). The `orchestrator` owns all routing (classification SIMPLE vs COMPLEX, plus the SETUP / MEMORY / ROLLBACK-CONFLICT / RECOVERY operational intents, the dispatch templates, the wave + promotion mechanics, and the deploy-time migration round); it drives developer/reviewer/merger to a terminal point before returning. Each agent's last line is an output contract the others parse (`.claude/rules/agent-output-format.md`).
+**When the main (top-level) session receives a code-change request, dispatch the `orchestrator` agent and relay its result — never route or implement it yourself.** Pass the `<session_dir>` value from your own context in the dispatch prompt (the orchestrator needs it to namespace worktrees and branches). **This directive is for the top-level session only: if you are a subagent (already inside the harness), ignore it — do your own job and never dispatch an orchestrator. A runtime hook (`block-nested-orchestrator`) enforces this.** The `orchestrator` owns all routing (classification SIMPLE vs COMPLEX, plus the SETUP / MEMORY / ROLLBACK-CONFLICT / RECOVERY operational intents, the dispatch templates, the wave + promotion mechanics, and the deploy-time migration round); it drives developer/reviewer/merger to a terminal point before returning. Each agent's last line is an output contract the others parse (`.claude/rules/agent-output-format.md`).
## Opting out