Consolidate local dispatch and review scripts behind shared implementation
Problem
Refactor the new local worktree dispatch and review tooling so scripts/dispatch, scripts/dispatch-kill, scripts/review, and scripts/worktree-new share more logic or move behind a single implementation surface. Reduce shell duplication around argument parsing, base resolution, worktree discovery, dry-run formatting, and error handling.
Context
Four scripts share overlapping shell patterns:
- scripts/dispatch — creates/reuses worktree, runs claude -p
- scripts/dispatch-kill — removes worktree and branch
- scripts/review — runs claude -p on a diff (does NOT source _dispatch_lib.sh)
- scripts/worktree-new — creates worktree with just install
_dispatch_lib.sh already provides slugify and find_worktree_by_branch. The remaining duplication:
1. Repo root resolution — all 4 scripts do repo_root="$(git rev-parse --show-toplevel)"; cd "$repo_root".
2. Task slug/branch/path derivation — dispatch, dispatch-kill, worktree-new each derive slug → branch_name → default path.
3. Value-arg validation — 9 occurrences of the shift; [[ $# -gt 0 ]] || { echo "Missing value for ..." }; var="$1" pattern.
Constraints: preserve CLI behavior, keep tests passing, stay anti-slop (no wrappers that don't simplify).
Possible Solutions
-
Extend
_dispatch_lib.shwith targeted helpers — addinit_repo_root,resolve_task,shift_value. Each script sources the lib and calls helpers instead of inlining the pattern. Recommended: minimal change, clear savings, no new files. -
Rewrite all scripts in Python behind a CLI: Maximum code sharing but massive scope increase, fragile for shell-native git operations. Overkill.
-
Generate scripts from a template: Over-engineered for 4 scripts with different flows.
Plan
Extend _dispatch_lib.sh with three helpers:
- shift_value "$@" — prints $2 or errors with flag name. Replaces 9 inline checks.
- init_repo_root — sets repo_root and cd's. Replaces pattern in all 4 scripts.
- resolve_task "$name" — sets slug, branch_name, default_worktree_path. Replaces pattern in 3 scripts.
Files to modify:
1. scripts/_dispatch_lib.sh — add helpers
2. scripts/dispatch — use helpers
3. scripts/dispatch-kill — use helpers
4. scripts/review — source lib, use helpers
5. scripts/worktree-new — use helpers
Steps:
1. Add helpers to _dispatch_lib.sh
2. Update each script to use helpers
3. Run existing tests to verify behavior preserved
4. Update task worksheet
Implementation Progress
Added three helpers to _dispatch_lib.sh:
- shift_value "$@" — replaces 9 inline value-arg validation blocks across all scripts
- init_repo_root — replaces duplicated git rev-parse --show-toplevel + cd in all 4 scripts
- resolve_task "$name" — replaces duplicated slug/branch/path derivation in dispatch, dispatch-kill, worktree-new
Updated all 4 scripts to use the shared helpers. scripts/review now sources _dispatch_lib.sh (previously did not).
All 5 existing tests pass (tests/scripts/test_dispatch_scripts.py).
QA Exploration
- [x] QA exploration completed (N/A — non-UI task)
Review Feedback
- Manager review (
just review) found one functional issue inscripts/worktree-new. - Fixed
scripts/worktree-newto callinit_repo_root, restoring the repo-rootcdbehavior beforeresolve_task. - Confirmed the branch diff remains scoped to the intended task files and scripts.
- Manager validation after the review fix:
uv run pytest tests/scripts/test_dispatch_scripts.py -q- [x] Review cleared