codebyplan 1.13.62 → 1.13.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/cli.js +1108 -2061
  2. package/package.json +1 -1
  3. package/templates/agents/cbp-e2e-playwright.md +10 -10
  4. package/templates/hooks/cbp-mcp-round-sync.sh +4 -15
  5. package/templates/hooks/cbp-test-hooks.sh +0 -81
  6. package/templates/hooks/hooks.json +0 -9
  7. package/templates/rules/cbp-operating-gotchas.md +8 -10
  8. package/templates/rules/supabase-branch-lifecycle.md +2 -2
  9. package/templates/rules/todo-backend.md +11 -8
  10. package/templates/settings.project.base.json +0 -5
  11. package/templates/skills/cbp-build-cc-settings/reference/cbp-permission-policy.md +1 -1
  12. package/templates/skills/cbp-checkpoint-create/SKILL.md +12 -22
  13. package/templates/skills/cbp-checkpoint-end/SKILL.md +37 -0
  14. package/templates/skills/cbp-checkpoint-plan/SKILL.md +10 -8
  15. package/templates/skills/cbp-checkpoint-start/SKILL.md +27 -19
  16. package/templates/skills/cbp-checkpoint-update/SKILL.md +1 -1
  17. package/templates/skills/cbp-finalize/SKILL.md +2 -2
  18. package/templates/skills/cbp-round-complete/SKILL.md +3 -24
  19. package/templates/skills/cbp-round-plan/SKILL.md +1 -1
  20. package/templates/skills/cbp-session-end/SKILL.md +40 -30
  21. package/templates/skills/cbp-session-start/SKILL.md +7 -7
  22. package/templates/skills/cbp-session-start/qa-regression.md +32 -25
  23. package/templates/skills/cbp-standalone-task-complete/SKILL.md +2 -5
  24. package/templates/skills/cbp-standalone-task-create/SKILL.md +5 -13
  25. package/templates/skills/cbp-standalone-task-start/SKILL.md +10 -10
  26. package/templates/skills/cbp-task-create/SKILL.md +1 -1
  27. package/templates/skills/cbp-task-start/SKILL.md +10 -10
  28. package/templates/skills/cbp-todo/SKILL.md +23 -38
  29. package/templates/skills/cbp-todo/qa-regression.md +21 -27
  30. package/templates/skills/cbp-verify/reference/round-scope.md +1 -2
  31. package/templates/hooks/cbp-mcp-caller-worktree-inject.sh +0 -78
@@ -157,21 +157,21 @@ Skip this step if the task title and requirements contain no CVE ID (`CVE-YYYY-N
157
157
 
158
158
  See `dependency-vulnerability-fixes.md` "Audit Sweep at Task Start" for the source rule.
159
159
 
160
- ### Step 3.5: Worktree-Match Verification
160
+ ### Step 3.5: User-Match Verification
161
161
 
162
- Before activating the task, verify the caller's worktree matches the assigned worktree on the target row. (Per CHK-104 — DB-level hard-lock prevents cross-worktree mutations.)
162
+ Before activating the task, verify the current user is allowed to claim/edit the checkpoint row.
163
163
 
164
- 1. Read caller worktree: `CALLER_WT=$(npx codebyplan resolve-worktree 2>/dev/null)`.
165
- 2. Determine target worktree:
166
- - **Checkpoint-bound tasks**: `TARGET_WT = checkpoint.worktree_id` (read from the local checkpoint file already loaded in Step 2). Note: checkpoint-bound tasks may have a NULL `task.assigned_worktree_id` because the lock lives on the parent checkpoint — fall through to `checkpoint.worktree_id`.
167
- 3. If `TARGET_WT IS NOT NULL AND TARGET_WT != CALLER_WT`, surface this error and abort:
164
+ 1. Resolve the current user: `CURRENT_USER=$(npx codebyplan whoami --json 2>/dev/null)` → parse `user_id` and `email` from the JSON. If the command fails or returns null/empty, `CURRENT_USER_ID` is null (anonymous session).
165
+ 2. Determine the target owner:
166
+ - **Checkpoint-bound tasks**: `TARGET_USER = checkpoint.assigned_user_id` (read from the local checkpoint file already loaded in Step 2). Note: checkpoint-bound tasks may have a null `task.assigned_user_id`; the lock lives on the parent checkpoint — fall through to `checkpoint.assigned_user_id`.
167
+ 3. If `TARGET_USER IS NOT NULL AND TARGET_USER != CURRENT_USER_ID`, surface this error and abort:
168
168
 
169
169
  ```
170
- TASK-N is assigned to worktree {TARGET_WT_NAME} ({TARGET_WT}); current worktree is {CALLER_WT_NAME} ({CALLER_WT}).
171
- Use the release_assignment MCP tool (requires maintainer role) if {TARGET_WT_NAME} is dead, then re-run /cbp-task-start.
170
+ TASK-N is assigned to user {TARGET_EMAIL} ({TARGET_USER}); you are logged in as {CURRENT_EMAIL} ({CURRENT_USER_ID}).
171
+ Use the release_assignment MCP tool (requires maintainer role) if that user is no longer active, then re-run /cbp-task-start.
172
172
  ```
173
173
 
174
- 4. If `TARGET_WT IS NULL` or matches, proceed.
174
+ 4. If `TARGET_USER IS NULL` or matches `CURRENT_USER_ID`, proceed.
175
175
 
176
176
  ### Step 3.6: Main-Drift Check (optional)
177
177
 
@@ -218,7 +218,7 @@ Display context summary:
218
218
 
219
219
  ### Step 5: Set Task Status
220
220
 
221
- `codebyplan task update --id <taskId> --checkpoint-id <checkpointId> --status in_progress` (CLI write-through: local state file + REST). If a worktree_id is present, also pass `--claim-worktree-id <worktreeId>` to auto-claim the checkpoint (the CLI passes all flags as snake-case PATCH fields). Break-glass fallback: MCP `update_task(task_id, status: "in_progress", claim_worktree_id: ..., caller_worktree_id: ...)` when the CLI is unavailable — note `caller_worktree_id` is auto-injected by the `cbp-mcp-caller-worktree-inject.sh` PreToolUse hook only on the MCP path.
221
+ `codebyplan task update --id <taskId> --checkpoint-id <checkpointId> --status in_progress` (CLI write-through: local state file + REST). The server keys on the JWT user for authz no worktree or claim params are needed or accepted. Break-glass fallback: MCP `update_task(task_id, status: "in_progress")` when the CLI is unavailable — all worktree-identity params are deprecated and silently ignored server-side; omit them.
222
222
 
223
223
  ### Step 6: Auto-trigger Round Start
224
224
 
@@ -7,27 +7,13 @@ effort: medium
7
7
 
8
8
  # Todo Command
9
9
 
10
- Single entry point after `/clear`: read the pure-read todo queue, gate on worktree ownership + checkpoint planning, load context, then trigger the next command. It ensures Claude always has full context — and never auto-routes into work locked to another worktree — before any command runs.
10
+ Single entry point after `/clear`: read the pure-read todo queue, gate on user ownership + checkpoint planning, load context, then trigger the next command. It ensures Claude always has full context — and never auto-routes into work locked to another user — before any command runs.
11
11
 
12
12
  ## Instructions
13
13
 
14
- ### Step 0: Resolve Caller Identity (worktree + user)
14
+ ### Step 0: Resolve Caller Identity (user)
15
15
 
16
- Before any MCP call, resolve who and where the caller is.
17
-
18
- **Worktree** — derive the caller `worktree_id` and any distress signal in one structured read:
19
-
20
- ```bash
21
- npx codebyplan resolve-worktree --json # → {"worktree_id":"<uuid>|null","error_kind":null|"<kind>"}
22
- ```
23
-
24
- - `error_kind` is `null` or `"tuple_miss"` → healthy. `WORKTREE_ID` = `worktree_id` (may be `null`: a legitimate main-repo or unregistered-worktree case — proceed to read the queue, but downstream hard-locks reject mutations on assigned rows).
25
- - `error_kind` is `local_config_read_failed`, `local_config_write_failed`, `legacy_file_blocks_dir`, `api_failed`, `git_failed`, or `unhandled` → **broken local state**. Surface the distress line and STOP (skip Steps 1–4) — routing is unreliable when the resolver cannot read local state:
26
-
27
- ```
28
- ⚠ resolve-worktree: <error_kind> — local state is broken; routing is unreliable.
29
- Run `npx codebyplan setup` from this directory to register/repair, then re-run /cbp-todo.
30
- ```
16
+ Before any MCP call, resolve who the caller is.
31
17
 
32
18
  **User** — `get_todos` (Step 1) requires a `user_id`:
33
19
 
@@ -36,45 +22,44 @@ npx codebyplan whoami --json # → {"user_id":"<uuid>","email":"…"} or null
36
22
  ```
37
23
 
38
24
  - A `user_id` is returned → use it in Step 1.
39
- - `null` (CLI keychain empty — common in OAuth-MCP-only environments) → the queue cannot be scoped by user; skip Step 1 and use the Step 3 fallback. The Step 1.5 ownership gate still applies.
25
+ - `null` or empty `user_id` (CLI keychain empty — common in OAuth-MCP-only environments) → the queue cannot be scoped by user; skip Step 1 and use the Step 3 fallback. The Step 1.5 ownership gate still applies.
40
26
 
41
27
  ### Step 1: Read the Todo Queue (pure-read)
42
28
 
43
- With `USER_ID` resolved, read `.codebyplan/state/todos.json` (local-first). If missing or stale (`_cursor.json` absent or `sync_status !== "synced"`), run `npx codebyplan sync` once and re-read. Break-glass fallback: MCP `get_todos({ repo_id, user_id, worktree_id })` when the state dir is absent and sync fails. Filter by `user_id` and `worktree_id` (omit `worktree_id` when `WORKTREE_ID` is `null` or unresolved). Take **`rows[0]`** as the queue head (ordered by `sort_order`).
29
+ With `USER_ID` resolved, read `.codebyplan/state/todos.json` (local-first). If missing or stale (`_cursor.json` absent or `sync_status !== "synced"`), run `npx codebyplan sync` once and re-read. Break-glass fallback: MCP `get_todos({ repo_id, user_id })` when the state dir is absent and sync fails. Filter by `user_id`. Take **`rows[0]`** as the queue head (ordered by `sort_order`).
44
30
 
45
- - The head carries `command`, `instructions`, `state`, `metadata`, `worktree_id`, `checkpoint_id`, `task_id`.
31
+ - The head carries `command`, `instructions`, `state`, `metadata`, `checkpoint_id`, `task_id`.
46
32
  - The routing context (checkpoint/task) lives in **`rows[0].metadata`**.
47
33
  - `get_todos` is **pure-read** — `apps/todo-worker` is the sole regen authority. NEVER call `get_next_action` or `regenerate_todos_for_repo`.
48
34
  - Empty array, or `USER_ID` unavailable → go to Step 3 (empty-queue fallback).
49
35
 
50
36
  Queue `command` values may use the `/codebyplan:<name>` plugin-namespace form (e.g. `/codebyplan:round-start`); treat each as the matching `/cbp-<name>` skill for the Step 2 matrix.
51
37
 
52
- ### Step 1.5: Resolve Target + Worktree-Ownership Gate
38
+ ### Step 1.5: Resolve Target + User-Ownership Gate
53
39
 
54
- Resolve the routing target's checkpoint and gate on ownership BEFORE any auto-trigger — including the Step 1.6 planning hand-offs. Refuse to route into, plan, or start work locked to a different worktree.
40
+ Resolve the routing target's checkpoint and gate on ownership BEFORE any auto-trigger — including the Step 1.6 planning hand-offs. Refuse to route into, plan, or start work locked to a different user.
55
41
 
56
- Resolve the checkpoint from `rows[0].metadata` (or read `.codebyplan/state/session/current.json` for the active task, falling back to MCP `get_current_task`), then load its `worktree_id` + `plan` + `status` from `.codebyplan/state/checkpoints/<id>.json` (falling back to MCP `get_checkpoints`) and its task count from `.codebyplan/state/checkpoints/<id>/tasks/` files (falling back to MCP `get_tasks(checkpoint_id)`). This single load is reused by the Step 1.6 planning gate. Skip this gate when the routing target has no checkpoint (idle — see Step 3) or the command is `/cbp-session-start`.
42
+ Resolve the checkpoint from `rows[0].metadata` (or read `.codebyplan/state/session/current.json` for the active task, falling back to MCP `get_current_task`), then load its `assigned_user_id` + `plan` + `status` from `.codebyplan/state/checkpoints/<id>.json` (falling back to MCP `get_checkpoints`) and its task count from `.codebyplan/state/checkpoints/<id>/tasks/` files (falling back to MCP `get_tasks(checkpoint_id)`). This single load is reused by the Step 1.6 planning gate. Skip this gate when the routing target has no checkpoint (idle — see Step 3) or the command is `/cbp-session-start`.
57
43
 
58
44
  Two ownership signals:
59
45
 
60
- 1. **Server-detected conflict** — `rows[0].state === "worktree_conflict"` (command `/codebyplan:release-or-switch`): the todo-worker already detected the lock conflict. The owner is in `rows[0].metadata.conflicting_worktree_name` / `.conflicting_worktree_id`. Surface the mismatch message below and **STOP**. Do NOT auto-trigger the release-or-switch command and do NOT call `release_assignment` — switch worktrees rather than reassigning a lock to the caller.
61
- 2. **Checkpoint-ownership check** — take the target checkpoint's `worktree_id` (loaded above, keyed by `rows[0].checkpoint_id` / `metadata.checkpoint.id`) and compare with caller `WORKTREE_ID`:
62
- - target `worktree_id` is `null` → **allow** (unassigned — any worktree may pick it up; covers the both-null main-repo case).
63
- - target `worktree_id` === caller `WORKTREE_ID` → **allow**.
64
- - target `worktree_id` non-null **AND** caller `WORKTREE_ID` is `null`/unresolved → **block** (deliberate safety: identity cannot be confirmed. This does not contradict Step 0 — reading the queue is fine, auto-triggering INTO assigned work is not. Run `npx codebyplan setup` to register this worktree).
65
- - target `worktree_id` non-null and differs from a non-null caller → **block**.
46
+ 1. **Server-detected conflict** — `rows[0].state === "worktree_conflict"` (command `/codebyplan:release-or-switch`): the todo-worker already detected the lock conflict. (`worktree_conflict` is the worker's current state-value name — it now reflects a user-level lock conflict and is renamed alongside the web-UI/schema work in a later task.) Surface the mismatch message below and **STOP**. Do NOT auto-trigger the release-or-switch command and do NOT call `release_assignment` — the user who owns the lock must release it or a maintainer must use `release_assignment`.
47
+ 2. **Checkpoint-ownership check** — take the target checkpoint's `assigned_user_id` (loaded above) and compare with caller `USER_ID`:
48
+ - target `assigned_user_id` is `null` → **allow** (unassigned — any user may pick it up).
49
+ - target `assigned_user_id` === caller `USER_ID` → **allow**.
50
+ - target `assigned_user_id` non-null AND differs from caller `USER_ID` → **block**.
66
51
 
67
- On block, resolve the owning worktree's `name` + `path` via MCP `get_worktrees({ repo_id })` (display-only ownership-block path no CLI verb exists for worktrees; stays MCP), then emit and STOP:
52
+ On block, emit and STOP (show the owner email if resolvable from the whoami context, otherwise the `assigned_user_id` UUID):
68
53
 
69
54
  ```
70
- ⚠ Work mismatch: CHK-<NNN> TASK-<N> is assigned to worktree <name> (<short-uuid>), not this one (<this-name> / <this-short-uuid>).
55
+ ⚠ Work mismatch: CHK-<NNN> TASK-<N> is assigned to user <email-or-uuid>, not you.
71
56
  Options:
72
- A) Switch to <owning-worktree-path> and resume there
73
- B) Work on something else here run /cbp-checkpoint-create or /cbp-task-create
57
+ A) Work on something else — run /cbp-checkpoint-create or /cbp-task-create
58
+ B) Ask the owner to release the assignment, or a maintainer can use release_assignment, then re-run /cbp-todo
74
59
  C) /cbp-session-end
75
60
  ```
76
61
 
77
- `<short-uuid>` = first 8 chars of the worktree UUID. Caller unresolved → render "this one" as `(unresolved / —)`. Name lookup miss → show the UUID alone. Wait for the user. NEVER propose reassigning the checkpoint to the caller worktree.
62
+ Wait for the user. NEVER propose reassigning the checkpoint to the caller.
78
63
 
79
64
  ### Step 1.55: Stale-Entity Guard
80
65
 
@@ -170,8 +155,8 @@ Display a brief context summary:
170
155
 
171
156
  Reached when `get_todos` returns `[]` or `USER_ID` was unavailable.
172
157
 
173
- 1. **Fallback discovery** (worktree-scoped): read `.codebyplan/state/session/current.json` and scan `.codebyplan/state/checkpoints/` for active checkpoints (fallback: MCP `get_current_task` + `get_checkpoints`) to discover whether actionable work exists for this caller.
174
- 2. **Actionable work found** → treat the discovered checkpoint as the routing target and apply BOTH the Step 1.5 ownership gate and the Step 1.6 planning gate to it, using the `worktree_id` + `plan` + `status` returned by `get_checkpoints` (the fallback has no `rows[0]` — substitute the discovered checkpoint). If both gates pass, route via Step 2 → Step 4.
158
+ 1. **Fallback discovery**: read `.codebyplan/state/session/current.json` and scan `.codebyplan/state/checkpoints/` for active checkpoints (fallback: MCP `get_current_task` + `get_checkpoints`) to discover whether actionable work exists for this caller.
159
+ 2. **Actionable work found** → treat the discovered checkpoint as the routing target and apply BOTH the Step 1.5 ownership gate and the Step 1.6 planning gate to it, using the `assigned_user_id` + `plan` + `status` returned by `get_checkpoints` (the fallback has no `rows[0]` — substitute the discovered checkpoint). If both gates pass, route via Step 2 → Step 4.
175
160
  3. **Nothing actionable** → suggest ending the session:
176
161
 
177
162
  ```
@@ -190,6 +175,6 @@ Reached only when the Step 1.5 ownership gate allowed routing to continue, the S
190
175
  ## Integration
191
176
 
192
177
  - **Called by**: `/cbp-session-start`, `/cbp-finalize`, `/cbp-checkpoint-complete`, manual, after `/clear`
193
- - **Resolves**: `npx codebyplan resolve-worktree --json` (worktree id + distress signal), `npx codebyplan whoami --json` (user id)
194
- - **Reads**: `.codebyplan/state/todos.json`, `session/current.json`, `checkpoints/<id>.json`, `checkpoints/<id>/tasks/<id>.json`, `checkpoints/<id>/tasks/<id>/rounds/<id>.json`, `worktrees.json`. If missing/stale, run `npx codebyplan sync` once and re-read. Break-glass: MCP `get_todos`, `get_current_task`, `get_rounds`, `get_checkpoints`, `get_tasks` when state dir absent and sync fails. `get_worktrees` stays MCP (display-only ownership-block path; no CLI verb).
178
+ - **Resolves**: `npx codebyplan whoami --json` (user id)
179
+ - **Reads**: `.codebyplan/state/todos.json`, `session/current.json`, `checkpoints/<id>.json`, `checkpoints/<id>/tasks/<id>.json`, `checkpoints/<id>/tasks/<id>/rounds/<id>.json`. If missing/stale, run `npx codebyplan sync` once and re-read. Break-glass: MCP `get_todos`, `get_current_task`, `get_rounds`, `get_checkpoints`, `get_tasks` when state dir absent and sync fails.
195
180
  - **Triggers**: `rows[0].command` (auto, after the Step 1.5 ownership gate and Step 1.55 stale-entity guard pass, and the Step 1.6 planning gate falls through); Step 1.55 overrides to STOP (stale completed/cancelled entity); Step 1.6 overrides to `/cbp-checkpoint-plan` (unplanned) or `/cbp-checkpoint-start` (planned-but-pending)
@@ -1,51 +1,45 @@
1
1
  ---
2
2
  name: cbp-todo-qa-regression
3
- description: Manual regression procedure for the cbp-todo worktree-ownership gate + get_todos switch (CHK-137 TASK-2)
3
+ description: Manual regression procedure for the cbp-todo user-ownership gate + get_todos switch (CHK-225 TASK-5)
4
4
  ---
5
5
 
6
- # cbp-todo — Worktree-Ownership Regression
6
+ # cbp-todo — User-Ownership Regression
7
7
 
8
- Manual procedure verifying that `/cbp-todo` reads the pure-read `get_todos` queue and refuses to auto-route into work locked to another worktree. No automated harness exists for markdown skills; run these by hand (or read the queue with the MCP tools) whenever Step 0/1/1.5 of `SKILL.md` changes.
8
+ Manual procedure verifying that `/cbp-todo` reads the pure-read `get_todos` queue and refuses to auto-route into work locked to another user. No automated harness exists for markdown skills; run these by hand (or read the queue with the MCP tools) whenever Step 0/1/1.5 of `SKILL.md` changes.
9
9
 
10
10
  Repo under test: `2ff6d405-39c5-47b8-a6d1-59f998ac0537`. Resolve a real `user_id` with `npx codebyplan whoami --json`, or harvest a non-null `assigned_user_id` from `get_checkpoints`.
11
11
 
12
12
  ## Preconditions
13
13
 
14
14
  - `get_todos` is the only Step 1 read — confirm no `get_next_action` / `regenerate_todos_for_repo` call remains (`grep -n 'get_next_action\|regenerate_todos_for_repo' SKILL.md` → no hits).
15
- - Step 0 uses `resolve-worktree --json` and `whoami --json`.
15
+ - Step 0 uses `whoami --json` only no worktree-resolution CLI verb appears in SKILL.md (verify by grepping the banned token list against SKILL.md; zero hits expected).
16
16
  - Step 1.55 (Stale-Entity Guard) must NOT call `get_next_action` or `regenerate_todos_for_repo` when rejecting a stale head — it reuses the checkpoint `status` + task statuses already loaded in Step 1.5 (the same grep above covers it).
17
17
 
18
- ## Scenario A — caller owns the work → auto-trigger
18
+ ## Scenario A — current user owns the work → auto-trigger
19
19
 
20
- 1. From a worktree that owns the active checkpoint (caller `WORKTREE_ID` === target checkpoint `worktree_id`, or target `worktree_id` is `null`).
21
- 2. `get_todos({ repo_id, user_id, worktree_id })` returns a head whose target checkpoint is owned by the caller (or unscoped).
20
+ 1. `npx codebyplan whoami --json` returns `user_id` matching the target checkpoint's `assigned_user_id` (or `assigned_user_id` is `null` — open queue).
21
+ 2. `get_todos({ repo_id, user_id })` returns a head whose target checkpoint is owned by the caller (or unassigned).
22
22
  3. **Expected**: Step 1.5 ownership gate allows, Step 1.6 planning gate falls through, Step 4 auto-triggers `rows[0].command` (mapped to its `/cbp-<name>` form).
23
23
 
24
- ## Scenario B1server-generated conflict head → halt
24
+ ## Scenario Bcheckpoint assigned to a DIFFERENT user → halt
25
25
 
26
- 1. Caller `codebyplan-claude-2` (`38cd7dfa`). Active work assigned to `codebyplan-cli` (`016bd7f2`).
27
- 2. `get_todos` head is the server conflict todo: `state === "worktree_conflict"`, `command "/codebyplan:release-or-switch"`, `metadata.conflicting_worktree_name === "codebyplan-cli"`.
28
- 3. **MCP calls expected**: `get_todos`; `get_worktrees` (to resolve `016bd7f2` path for the message). NO `get_checkpoints` needed the server already resolved the conflict.
29
- 4. **Expected**: Step 1.5 server-conflict branch blocks and STOPS. The "Work mismatch" message names `codebyplan-cli (016bd7f2)` vs `codebyplan-claude-2 (38cd7dfa)`, offers Switch (`/Users/merilyviks/codebyplan-cli`) / other-work / session-end. NO auto-trigger; NO `release_assignment` call (never reassign the lock to the caller).
26
+ 1. `whoami` returns user A. Target checkpoint `assigned_user_id` is user B (non-null, different).
27
+ 2. `get_todos` head is a normal routing todo whose target checkpoint `assigned_user_id` is user B's UUID.
28
+ 3. **MCP calls expected**: `get_todos`; `get_checkpoints` (Step 1.5 load, reads the target `assigned_user_id`). Owner is identified by `assigned_user_id` directly (email from whoami context if resolvable, else the UUID) — no worktree-lookup MCP call is needed.
29
+ 4. **Expected**: Step 1.5 checkpoint-ownership branch blocks and STOPS with the "Work mismatch" message naming the owner email-or-uuid. NO auto-trigger; the Step 1.6 planning gate never runs (ownership precedes it). NEVER propose reassigning the checkpoint to the caller.
30
30
 
31
- ## Scenario B2normal head, mismatched checkpoint ownerhalt
31
+ ## Scenario Cempty queue / anonymous callerfallback
32
32
 
33
- 1. Caller `codebyplan-claude-2` (`38cd7dfa`). `get_todos` head is a normal routing todo whose target checkpoint `worktree_id` is `016bd7f2`.
34
- 2. **MCP calls expected**: `get_todos`; `get_checkpoints` (Step 1.5 load, reads the target `worktree_id`); `get_worktrees` (resolve `016bd7f2` name + path).
35
- 3. **Expected**: Step 1.5 checkpoint-ownership branch blocks and STOPS with the same "Work mismatch" message. NO auto-trigger; the Step 1.6 planning gate never runs (ownership precedes it).
36
-
37
- ## Scenario C — empty queue + cross-worktree current task → halt
38
-
39
- 1. `get_todos` returns `[]` (or `whoami` returned `null`, so Step 1 was skipped).
40
- 2. Step 3 fallback: `get_current_task({ repo_id, worktree_id })` / `get_checkpoints({ repo_id, worktree_id, status: 'active' })` surface a checkpoint whose `worktree_id` differs from the caller.
41
- 3. **Expected**: the Step 1.5 ownership gate (applied to the fallback target) blocks the discovered work with the same "Work mismatch" message. NO auto-trigger. `regenerate_todos_for_repo` is never called.
33
+ 1. `get_todos` returns `[]` OR `whoami` returned `null`/empty `user_id`, so Step 1 was skipped.
34
+ 2. Step 3 fallback: `get_current_task` / `get_checkpoints` surface a checkpoint whose `assigned_user_id` differs from the caller (or caller is anonymous).
35
+ 3. **Expected**: the Step 1.5 ownership gate (applied to the fallback target) blocks the discovered work with the "Work mismatch" message when the user differs, or routes to the idle suggestion when the caller is anonymous and no open work is found. NO auto-trigger. `regenerate_todos_for_repo` is never called.
42
36
 
43
37
  ## Scenario D — stale queue head for a completed/cancelled entity → halt
44
38
 
45
- 1. Caller owns the active checkpoint (Scenario A ownership holds), but the todo-worker queue is lagging — `health_check.todos_freshness_ok === false`. The `get_todos` head targets a checkpoint (or task) that has since been completed or cancelled.
46
- 2. Step 1.5 loads the target checkpoint `status` + `get_tasks(checkpoint_id)` (already required for the ownership/planning gates — no extra reads).
47
- 3. **Expected**: Step 1.55 rejects the auto-trigger — target checkpoint `status` is `completed`/`cancelled` (or every task is `completed`/`cancelled`) — surfaces the "Stale queue head" message naming the command + `CHK-NNN`[ `TASK-N`] + status, and STOPS. NO auto-trigger. NO `get_next_action` / `regenerate_todos_for_repo` call.
39
+ 1. Caller's `user_id` matches the target checkpoint's `assigned_user_id` (ownership passes), but the todo-worker queue is lagging — `health_check.todos_freshness_ok === false`. The `get_todos` head targets a checkpoint (or task) that has since been completed or cancelled.
40
+ 2. Step 1.5 loads the target checkpoint `status` + task statuses (already required for the ownership/planning gates — no extra reads).
41
+ 3. **Expected**: Step 1.55 rejects the auto-trigger — target checkpoint `status` is `completed`/`cancelled` (or every task is `completed`/`cancelled`) — surfaces the "Stale queue head" message naming the command + `CHK-NNN`[`TASK-N`] + status, and STOPS. NO auto-trigger. NO `get_next_action` / `regenerate_todos_for_repo` call.
48
42
 
49
- ## Edge — both null
43
+ ## Edge — null assigned_user_id (open work)
50
44
 
51
- Caller `WORKTREE_ID` empty AND target checkpoint `worktree_id` `null` → legitimate main-repo / unassigned work → auto-trigger allowed.
45
+ `assigned_user_id` is `null` on the target checkpoint AND caller has a valid `user_id` → open/unassigned work → auto-trigger allowed (proceeds to Step 1.6 planning gate).
@@ -58,5 +58,4 @@ human confirmation — do not add an AskUserQuestion in cbp-verify at round scop
58
58
 
59
59
  `codebyplan round update --id <round_id> --task-id <uuid> --checkpoint-id <uuid> --context '<json>'`
60
60
  (merge `verify_manifest` into existing context; the REPLACE contract requires the full object).
61
- Break-glass: MCP `update_round` (checkpoint KIND) / `update_standalone_round` (standalone KIND)
62
- pass `caller_worktree_id` on locked feat rows.
61
+ Break-glass: MCP `update_round` (checkpoint KIND) / `update_standalone_round` (standalone KIND).
@@ -1,78 +0,0 @@
1
- #!/bin/bash
2
- # @hook: PreToolUse mcp__codebyplan__(update_checkpoint|complete_checkpoint|update_task|complete_task|add_round|update_round|complete_round|create_standalone_task|update_standalone_task|complete_standalone_task|add_standalone_round|update_standalone_round|complete_standalone_round|update_standalone_file_change)
3
- # Hook: PreToolUse for MCP write tools
4
- #
5
- # Purpose: Inject caller_worktree_id into MCP mutation tool inputs when the
6
- # field is absent. Reads the worktree.local.json branch-keyed cache
7
- # first (fast path); falls back to `codebyplan resolve-worktree --cache`.
8
- #
9
- # Fail-open: ALL exit paths exit 0. A hook failure must never block a tool call.
10
- # Use explicit guards rather than set -euo pipefail (which would exit
11
- # non-zero on the first failing command before the final exit 0).
12
-
13
- # C0 — require jq; if absent, emit nothing and exit 0 (fail-open).
14
- if ! command -v jq > /dev/null 2>&1; then
15
- exit 0
16
- fi
17
-
18
- # Read stdin once into a variable.
19
- INPUT=$(cat)
20
-
21
- # C6 — if caller_worktree_id is already a non-empty string, do not overwrite.
22
- # (jq '// empty' already maps JSON null to an empty string, so a plain -n test suffices.)
23
- EXISTING=$(echo "$INPUT" | jq -r '.tool_input.caller_worktree_id // empty' 2>/dev/null)
24
- if [ -n "$EXISTING" ]; then
25
- # Already populated — plain allow (exit 0 with no output).
26
- exit 0
27
- fi
28
-
29
- # C5 — resolve worktree id, fast path first.
30
- RESOLVED_WT=""
31
-
32
- # Determine repo root: prefer $CLAUDE_PROJECT_DIR, fall back to PWD.
33
- REPO_ROOT="${CLAUDE_PROJECT_DIR:-$PWD}"
34
- CACHE_FILE="$REPO_ROOT/.codebyplan/worktree.local.json"
35
-
36
- if [ -f "$CACHE_FILE" ]; then
37
- CACHED_WT=$(jq -r '.worktree_id // empty' "$CACHE_FILE" 2>/dev/null)
38
- CACHED_BRANCH=$(jq -r '.branch // empty' "$CACHE_FILE" 2>/dev/null)
39
-
40
- if [ -n "$CACHED_WT" ] && [ "$CACHED_WT" != "null" ] && \
41
- [ -n "$CACHED_BRANCH" ] && [ "$CACHED_BRANCH" != "null" ]; then
42
- # Validate branch matches current git branch.
43
- CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
44
- if [ -n "$CURRENT_BRANCH" ] && [ "$CURRENT_BRANCH" = "$CACHED_BRANCH" ]; then
45
- RESOLVED_WT="$CACHED_WT"
46
- fi
47
- fi
48
- fi
49
-
50
- # Fallback to CLI resolution if cache miss or branch mismatch.
51
- if [ -z "$RESOLVED_WT" ]; then
52
- RESOLVED_WT=$(codebyplan resolve-worktree --cache 2>/dev/null \
53
- || npx --no-install codebyplan resolve-worktree --cache 2>/dev/null \
54
- || true)
55
- fi
56
-
57
- # UUID guard — accept only a canonical UUID (8-4-4-4-12 hex).
58
- UUID_PATTERN='^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'
59
- if [ -z "$RESOLVED_WT" ] || ! echo "$RESOLVED_WT" | grep -qE "$UUID_PATTERN"; then
60
- # Unresolved or invalid — plain allow, no updatedInput.
61
- exit 0
62
- fi
63
-
64
- # C3 — emit updatedInput as the FULL tool_input with caller_worktree_id added.
65
- # Claude Code's PreToolUse updatedInput REPLACES tool_input wholesale (it is not a
66
- # partial merge), so we must echo back every existing field merged with the new
67
- # caller_worktree_id — otherwise the tool loses round_id/duration_minutes/etc.
68
- echo "$INPUT" | jq \
69
- --arg wt "$RESOLVED_WT" \
70
- '{
71
- hookSpecificOutput: {
72
- hookEventName: "PreToolUse",
73
- permissionDecision: "allow",
74
- updatedInput: (.tool_input + { caller_worktree_id: $wt })
75
- }
76
- }'
77
-
78
- exit 0