@imdeadpool/guardex 8.4.0 → 8.4.2

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 (97) hide show
  1. package/CONTRIBUTING.md +13 -4
  2. package/README.md +223 -1
  3. package/THIRD-PARTY-NOTICES.md +29 -0
  4. package/bin/multiagent-safety.js +9 -12
  5. package/package.json +4 -2
  6. package/skills/gitguardex/SKILL.md +28 -0
  7. package/skills/pr-lens/SKILL.md +87 -0
  8. package/src/abide.js +534 -0
  9. package/src/agents/activity.js +23 -4
  10. package/src/agents/inspect.js +15 -6
  11. package/src/agents/launch.js +6 -1
  12. package/src/agents/process-lease.js +84 -0
  13. package/src/agents/sessions.js +96 -16
  14. package/src/agents/start.js +18 -9
  15. package/src/agents/status.js +35 -14
  16. package/src/agents/supervise.js +87 -0
  17. package/src/budget/index.js +6 -1
  18. package/src/cli/args.js +71 -77
  19. package/src/cli/commands/agents.js +38 -10
  20. package/src/cli/commands/branch.js +116 -103
  21. package/src/cli/commands/claude.js +604 -50
  22. package/src/cli/commands/codex.js +359 -0
  23. package/src/cli/commands/context.js +52 -0
  24. package/src/cli/commands/mcp.js +5 -1
  25. package/src/cli/commands/pr.js +138 -3
  26. package/src/cli/commands/review.js +3 -1
  27. package/src/cli/commands/setup.js +25 -0
  28. package/src/cli/commands/status.js +128 -60
  29. package/src/cli/commands/storage.js +24 -0
  30. package/src/cli/commands/usage.js +48 -0
  31. package/src/cli/commands/watch.js +157 -47
  32. package/src/cli/commands/worktree-copy-ignored.js +38 -0
  33. package/src/cli/commands/worktree-estimate.js +69 -0
  34. package/src/cli/commands/worktree-hooks.js +62 -0
  35. package/src/cli/commands/worktree-navigation.js +71 -0
  36. package/src/cli/commands/worktree-provision.js +92 -0
  37. package/src/cli/main.js +229 -110
  38. package/src/cli/shared/gate-flags.js +131 -0
  39. package/src/cli/shared/scaffolding.js +31 -3
  40. package/src/cli/shared-locks.js +3 -1
  41. package/src/cockpit/pane-actions.js +43 -7
  42. package/src/context.js +373 -17
  43. package/src/doctor/index.js +29 -35
  44. package/src/finish/events.js +254 -0
  45. package/src/finish/index.js +322 -62
  46. package/src/finish/post-branch-finish-cleanup.js +316 -22
  47. package/src/finish/preflight.js +44 -1
  48. package/src/finish/progress.js +36 -0
  49. package/src/finish/review-gate.js +317 -127
  50. package/src/finish/review-receipt.js +156 -0
  51. package/src/finish/with-claims-lock.py +29 -0
  52. package/src/git/index.js +179 -42
  53. package/src/hooks/index.js +68 -3
  54. package/src/mcp/collect.js +61 -5
  55. package/src/mcp/server.js +4 -0
  56. package/src/output/index.js +167 -1
  57. package/src/pr-lens-publish.js +93 -0
  58. package/src/pr-lens.js +184 -0
  59. package/src/pr-respond.js +860 -0
  60. package/src/pr-review.js +519 -159
  61. package/src/pr.js +73 -14
  62. package/src/provider-binary.js +21 -0
  63. package/src/review-diff-context.js +90 -0
  64. package/src/review-fix.js +23 -17
  65. package/src/scaffold/agent-worktree-prep.js +14 -12
  66. package/src/scaffold/dependency-store.js +234 -0
  67. package/src/scaffold/index.js +13 -1
  68. package/src/scaffold/provision-approvals.js +83 -0
  69. package/src/scaffold/provision-config.js +169 -40
  70. package/src/shared-git-state.js +11 -1
  71. package/src/speckit/index.js +12 -1
  72. package/src/storage/run-artifacts.js +185 -0
  73. package/src/toolchain/index.js +322 -2
  74. package/src/worktree-copy-ignored.js +200 -0
  75. package/src/worktree-hook-worker.js +29 -0
  76. package/src/worktree-hooks.js +333 -0
  77. package/src/worktree-navigation.js +248 -0
  78. package/src/worktree-picker.js +89 -0
  79. package/src/worktree-quota.js +100 -0
  80. package/src/worktree-start.js +100 -0
  81. package/templates/AGENTS.multiagent-safety.md +8 -0
  82. package/templates/AGENTS.multiagent-safety.min.md +3 -1
  83. package/templates/codex/skills/gitguardex/SKILL.md +28 -0
  84. package/templates/codex/skills/opensrc/SKILL.md +24 -0
  85. package/templates/githooks/post-merge +16 -2
  86. package/templates/scripts/agent-autofinish-watch.sh +17 -18
  87. package/templates/scripts/agent-branch-finish.sh +180 -19
  88. package/templates/scripts/agent-branch-merge.sh +7 -32
  89. package/templates/scripts/agent-branch-start.sh +202 -43
  90. package/templates/scripts/agent-claude-stop-finish.sh +4 -27
  91. package/templates/scripts/agent-file-locks.py +257 -29
  92. package/templates/scripts/agent-preflight.sh +3 -0
  93. package/templates/scripts/agent-worktree-prune.sh +975 -63
  94. package/templates/scripts/codex-agent.sh +55 -5
  95. package/templates/scripts/lib/guardex-base-branch.sh +207 -0
  96. package/templates/scripts/openspec/init-change-workspace.sh +10 -18
  97. package/templates/scripts/review-bot-watch.sh +171 -4
@@ -7,6 +7,11 @@ BASE_BRANCH=""
7
7
  BASE_BRANCH_EXPLICIT=0
8
8
  WORKTREE_ROOT_REL=""
9
9
  WORKTREE_ROOT_EXPLICIT=0
10
+ SPARSE_EXCLUDES=()
11
+ TASK_ID=""
12
+ PREVIEW=0
13
+ REMEMBER_SPARSE=0
14
+ PROVISION_MODE="${GUARDEX_PROVISION_MODE:-full}"
10
15
  NODE_BIN="${GUARDEX_NODE_BIN:-node}"
11
16
  CLI_ENTRY="${GUARDEX_CLI_ENTRY:-}"
12
17
  OPENSPEC_AUTO_INIT_RAW="${GUARDEX_OPENSPEC_AUTO_INIT:-true}"
@@ -55,9 +60,18 @@ Positional:
55
60
 
56
61
  Options:
57
62
  --task <name> Task name/slug
63
+ --task-id <id> Stable task identity; reuse its worktree even after a title change
64
+ --preview Read-only checkout estimate; no fetch, cleanup or creation
65
+ --remember-sparse Save explicit --sparse-exclude choices after creating a NEW worktree
66
+ --provision-mode <minimal|docs|full> Preparation level (default full); reduced modes skip dependencies and hooks
58
67
  --agent <name> Agent name
59
68
  --base <branch> Base branch to fork from
60
69
  --worktree-root <p> Worktree root dir (default: .omx/agent-worktrees)
70
+ --sparse-exclude <dir> Omit a tracked directory from a NEW checkout (repeatable).
71
+ Literal repo-relative paths only; not globs.
72
+ Defaults to git config --add multiagent.worktreeSparseExclude <dir>.
73
+ Existing worktrees are unchanged. Restore omitted files with
74
+ git sparse-checkout disable inside the new worktree.
61
75
  --reuse-existing Reuse an existing matching worktree (default)
62
76
  --new Force a fresh worktree instead of reusing
63
77
  --tier <T1|T2|T3> OpenSpec tier for scaffolding (default T1; T2 for a
@@ -80,6 +94,34 @@ while [[ $# -gt 0 ]]; do
80
94
  TASK_NAME="${2:-task}"
81
95
  shift 2
82
96
  ;;
97
+ --task-id)
98
+ if [[ -n "$TASK_ID" ]]; then
99
+ echo "[agent-branch-start] --task-id must appear once." >&2
100
+ exit 1
101
+ fi
102
+ TASK_ID="${2:-}"
103
+ if [[ ! "$TASK_ID" =~ ^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$ ]]; then
104
+ echo "[agent-branch-start] Invalid --task-id (use 1-128 letters, digits, . _ : -)." >&2
105
+ exit 1
106
+ fi
107
+ shift 2
108
+ ;;
109
+ --preview)
110
+ PREVIEW=1
111
+ shift
112
+ ;;
113
+ --provision-mode)
114
+ PROVISION_MODE="${2:-}"
115
+ if [[ "$PROVISION_MODE" != minimal && "$PROVISION_MODE" != docs && "$PROVISION_MODE" != full ]]; then
116
+ echo "[agent-branch-start] Invalid --provision-mode; expected minimal, docs or full." >&2
117
+ exit 1
118
+ fi
119
+ shift 2
120
+ ;;
121
+ --remember-sparse)
122
+ REMEMBER_SPARSE=1
123
+ shift
124
+ ;;
83
125
  --agent)
84
126
  AGENT_NAME="${2:-agent}"
85
127
  shift 2
@@ -127,6 +169,14 @@ while [[ $# -gt 0 ]]; do
127
169
  WORKTREE_ROOT_EXPLICIT=1
128
170
  shift 2
129
171
  ;;
172
+ --sparse-exclude)
173
+ if [[ $# -lt 2 || -z "$2" ]]; then
174
+ echo "[agent-branch-start] --sparse-exclude requires a repo-relative directory." >&2
175
+ exit 1
176
+ fi
177
+ SPARSE_EXCLUDES+=("$2")
178
+ shift 2
179
+ ;;
130
180
  --)
131
181
  shift
132
182
  while [[ $# -gt 0 ]]; do
@@ -147,6 +197,12 @@ while [[ $# -gt 0 ]]; do
147
197
  esac
148
198
  done
149
199
 
200
+ if [[ "$PROVISION_MODE" != minimal && "$PROVISION_MODE" != docs && "$PROVISION_MODE" != full ]]; then
201
+ echo "[agent-branch-start] Invalid provisioning mode; expected minimal, docs or full." >&2
202
+ exit 1
203
+ fi
204
+ export GUARDEX_PROVISION_MODE="$PROVISION_MODE"
205
+
150
206
  if [[ "${#POSITIONAL_ARGS[@]}" -gt 3 ]]; then
151
207
  echo "[agent-branch-start] Too many positional arguments." >&2
152
208
  echo "Usage: $0 [task] [agent] [base] [--worktree-root <path>] [--reuse-existing|--new]" >&2
@@ -317,7 +373,7 @@ REUSE_EXISTING_WORKTREE="$(normalize_bool "$REUSE_EXISTING_RAW" "1")"
317
373
 
318
374
  normalize_tier() {
319
375
  local raw="${1:-}"
320
- local fallback="${2:-T3}"
376
+ local fallback="${2:-T1}"
321
377
  local upper
322
378
  upper="$(printf '%s' "$raw" | tr '[:lower:]' '[:upper:]')"
323
379
  case "$upper" in
@@ -401,9 +457,7 @@ resolve_openspec_capability_slug() {
401
457
  }
402
458
 
403
459
  resolve_repo_prefix() {
404
- local root
405
- root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
406
- basename "$root"
460
+ basename "$worktree_repo_root"
407
461
  }
408
462
 
409
463
  resolve_worktree_leaf() {
@@ -431,6 +485,10 @@ resolve_worktree_leaf() {
431
485
  }
432
486
 
433
487
  print_reused_agent_worktree() {
488
+ if [[ "$REMEMBER_SPARSE" -eq 1 ]]; then
489
+ echo "[agent-branch-start] --remember-sparse requires a new worktree; no configuration changed." >&2
490
+ return 1
491
+ fi
434
492
  local branch_name="$1"
435
493
  local worktree_path="$2"
436
494
  local stored_base=""
@@ -552,8 +610,11 @@ managed_worktree_roots() {
552
610
  local root
553
611
  local seen_roots=$'\n'
554
612
 
613
+ if [[ "$explicit_root" != /* ]]; then
614
+ explicit_root="${repo}/${explicit_root}"
615
+ fi
555
616
  for root in \
556
- "${repo}/${explicit_root}" \
617
+ "$explicit_root" \
557
618
  "${repo}/.omx/agent-worktrees" \
558
619
  "${repo}/.omc/agent-worktrees"; do
559
620
  if [[ -n "$root" && "$seen_roots" != *$'\n'"$root"$'\n'* ]]; then
@@ -696,26 +757,6 @@ hydrate_local_helper_in_worktree() {
696
757
  echo "[agent-branch-start] Hydrated local helper in worktree: ${relative_path}"
697
758
  }
698
759
 
699
- hydrate_dependency_dir_symlink_in_worktree() {
700
- local repo="$1"
701
- local worktree="$2"
702
- local relative_path="$3"
703
- local source_path="${repo}/${relative_path}"
704
- local target_path="${worktree}/${relative_path}"
705
-
706
- if [[ ! -d "$source_path" ]]; then
707
- return 0
708
- fi
709
-
710
- if [[ -e "$target_path" ]]; then
711
- return 0
712
- fi
713
-
714
- mkdir -p "$(dirname "$target_path")"
715
- ln -s "$source_path" "$target_path"
716
- echo "[agent-branch-start] Linked dependency dir in worktree: ${relative_path}"
717
- }
718
-
719
760
  initialize_openspec_plan_workspace() {
720
761
  local repo="$1"
721
762
  local worktree="$2"
@@ -774,6 +815,38 @@ if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
774
815
  fi
775
816
 
776
817
  repo_root="$(git rev-parse --show-toplevel)"
818
+ if [[ "$REMEMBER_SPARSE" -eq 1 && ( "${#SPARSE_EXCLUDES[@]}" -eq 0 || "$PREVIEW" -eq 1 || "$PRINT_NAME_ONLY" -eq 1 ) ]]; then
819
+ echo "[agent-branch-start] --remember-sparse requires explicit --sparse-exclude choices and a normal start." >&2
820
+ exit 1
821
+ fi
822
+ if [[ ${#SPARSE_EXCLUDES[@]} -eq 0 ]]; then
823
+ while IFS= read -r -d '' sparse_dir; do
824
+ SPARSE_EXCLUDES+=("$sparse_dir")
825
+ done < <(git -C "$repo_root" config --null --get-all multiagent.worktreeSparseExclude || true)
826
+ fi
827
+ # Fail before moving local edits. Paths become anchored Git patterns, so refuse
828
+ # pattern syntax and traversal instead of accidentally excluding source code.
829
+ for sparse_dir in "${SPARSE_EXCLUDES[@]}"; do
830
+ case "$sparse_dir" in
831
+ ""|/*|-*|*'*'*|*'?'*|*'['*|*']'*|*'\'*|*$'\n'*|*$'\r'*|*$'\t'*|*" ")
832
+ echo "[agent-branch-start] Invalid --sparse-exclude directory: $sparse_dir" >&2
833
+ exit 1
834
+ ;;
835
+ esac
836
+ case "/$sparse_dir/" in
837
+ *"/../"*|*"/./"*|*"/.git/"*|*"//"*)
838
+ echo "[agent-branch-start] Invalid --sparse-exclude directory: $sparse_dir" >&2
839
+ exit 1
840
+ ;;
841
+ esac
842
+ done
843
+ # Keep source-checkout/base/transfer semantics, but place new lanes beside
844
+ # each other under the primary checkout, never under the current linked lane.
845
+ worktree_repo_root="$(git worktree list --porcelain | sed -n '1s/^worktree //p')"
846
+ if [[ -z "$worktree_repo_root" ]]; then
847
+ echo "[agent-branch-start] Cannot resolve the primary worktree root." >&2
848
+ exit 1
849
+ fi
777
850
 
778
851
  guardex_env_helper="${repo_root}/scripts/guardex-env.sh"
779
852
  if [[ -f "$guardex_env_helper" ]]; then
@@ -798,7 +871,46 @@ if [[ "$BASE_BRANCH_EXPLICIT" -eq 1 && -z "$BASE_BRANCH" ]]; then
798
871
  fi
799
872
 
800
873
  current_branch="$(git -C "$repo_root" rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
801
- if [[ "$REUSE_EXISTING_WORKTREE" -eq 1 && "$current_branch" == agent/* ]]; then
874
+ if [[ -n "$TASK_ID" && "$PRINT_NAME_ONLY" -eq 0 ]]; then
875
+ task_branch=""
876
+ while IFS= read -r candidate; do
877
+ [[ "$(git config --get "branch.${candidate}.guardexTaskId" || true)" == "$TASK_ID" ]] || continue
878
+ if [[ -n "$task_branch" ]]; then
879
+ echo "[agent-branch-start] Ambiguous task ID: $TASK_ID; refusing another worktree." >&2
880
+ exit 1
881
+ fi
882
+ task_branch="$candidate"
883
+ done < <(git for-each-ref --format='%(refname:short)' refs/heads/)
884
+ if [[ -n "$task_branch" ]]; then
885
+ if [[ "$REUSE_EXISTING_WORKTREE" -eq 0 || "$task_branch" != "agent/$(normalize_role "$AGENT_NAME")/"* ]]; then
886
+ echo "[agent-branch-start] Task ID already has a branch: $task_branch; refusing a duplicate or different agent role." >&2
887
+ exit 1
888
+ fi
889
+ task_worktree=""
890
+ entry_worktree=""
891
+ task_locked=0
892
+ while IFS= read -r -d '' field; do
893
+ case "$field" in
894
+ "worktree "*) entry_worktree="${field#worktree }" ;;
895
+ "branch refs/heads/$task_branch") task_worktree="$entry_worktree" ;;
896
+ locked*) [[ "$task_worktree" != "$entry_worktree" ]] || task_locked=1 ;;
897
+ esac
898
+ done < <(git worktree list --porcelain -z)
899
+ if [[ -z "$task_worktree" || ! -e "$task_worktree/.git" || "$task_locked" -eq 1 ]] \
900
+ || [[ "$(git config --get "branch.${task_branch}.guardexStartReady" || true)" != true ]] \
901
+ || branch_published_then_remote_pruned "$repo_root" "$task_branch"; then
902
+ echo "[agent-branch-start] Task ID has an unavailable, locked, incomplete or finished worktree; refusing reuse." >&2
903
+ exit 1
904
+ fi
905
+ if [[ "$PREVIEW" -eq 1 ]]; then
906
+ echo "[agent-branch-start] Preview: would reuse task $TASK_ID at $task_worktree (no new checkout)."
907
+ else
908
+ print_reused_agent_worktree "$task_branch" "$task_worktree"
909
+ fi
910
+ exit 0
911
+ fi
912
+ fi
913
+ if [[ -z "$TASK_ID" && "$PREVIEW" -eq 0 && "$REUSE_EXISTING_WORKTREE" -eq 1 && "$current_branch" == agent/* ]]; then
802
914
  print_reused_agent_worktree "$current_branch" "$repo_root"
803
915
  exit $?
804
916
  fi
@@ -818,8 +930,8 @@ if [[ "$PRINT_NAME_ONLY" -eq 1 ]]; then
818
930
  exit 0
819
931
  fi
820
932
 
821
- if [[ "$REUSE_EXISTING_WORKTREE" -eq 1 ]]; then
822
- matching_dirty_worktree="$(find_matching_dirty_agent_worktree "$repo_root" "$WORKTREE_ROOT_REL" "$task_slug" "$agent_slug")"
933
+ if [[ -z "$TASK_ID" && "$PREVIEW" -eq 0 && "$REUSE_EXISTING_WORKTREE" -eq 1 ]]; then
934
+ matching_dirty_worktree="$(find_matching_dirty_agent_worktree "$worktree_repo_root" "$WORKTREE_ROOT_REL" "$task_slug" "$agent_slug")"
823
935
  if [[ -n "$matching_dirty_worktree" ]]; then
824
936
  IFS=$'\t' read -r reused_branch reused_worktree <<<"$matching_dirty_worktree"
825
937
  echo "[agent-branch-start] Matched dirty managed worktree for requested task."
@@ -846,7 +958,7 @@ if [[ "$BASE_BRANCH_EXPLICIT" -eq 0 ]]; then
846
958
  fi
847
959
 
848
960
  if git show-ref --verify --quiet "refs/remotes/origin/${BASE_BRANCH}"; then
849
- git fetch origin "${BASE_BRANCH}" --quiet
961
+ if [[ "$PREVIEW" -eq 0 ]]; then git fetch origin "${BASE_BRANCH}" --quiet; fi
850
962
  start_ref="origin/${BASE_BRANCH}"
851
963
  else
852
964
  if ! git show-ref --verify --quiet "refs/heads/${BASE_BRANCH}"; then
@@ -856,26 +968,39 @@ else
856
968
  start_ref="${BASE_BRANCH}"
857
969
  fi
858
970
 
971
+ if [[ "$WORKTREE_ROOT_REL" == /* ]]; then
972
+ worktree_root="$WORKTREE_ROOT_REL"
973
+ else
974
+ worktree_root="${worktree_repo_root}/${WORKTREE_ROOT_REL}"
975
+ fi
976
+ estimate_args=(worktree estimate --target "$repo_root" --ref "$start_ref" --destination "$worktree_root")
977
+ if [[ "$PREVIEW" -eq 0 ]]; then estimate_args+=(--check-quota); fi
978
+ for sparse_dir in "${SPARSE_EXCLUDES[@]}"; do
979
+ estimate_args+=(--sparse-exclude "$sparse_dir")
980
+ done
981
+ run_guardex_cli "${estimate_args[@]}"
982
+ if [[ "$PREVIEW" -eq 1 ]]; then exit 0; fi
983
+
859
984
  timestamp="$(date +%Y%m%d-%H%M%S)"
985
+ mkdir -p "$worktree_root"
860
986
  branch_suffix=2
861
- while git show-ref --verify --quiet "refs/heads/${branch_name}"; do
987
+ worktree_leaf="$(resolve_worktree_leaf "$branch_name" "$agent_slug")"
988
+ # Guard against both a duplicate branch ref and a stale leftover worktree
989
+ # directory (e.g. a previous agent was killed before the ref was registered).
990
+ # Both checks share the suffix counter so a single increment resolves either
991
+ # kind of collision, keeping the timestamp format stable.
992
+ while git show-ref --verify --quiet "refs/heads/${branch_name}" \
993
+ || [[ -e "${worktree_root}/${worktree_leaf}" ]]; do
862
994
  branch_name="${branch_name_base}-${branch_suffix}"
863
995
  branch_suffix=$((branch_suffix + 1))
996
+ worktree_leaf="$(resolve_worktree_leaf "$branch_name" "$agent_slug")"
864
997
  done
865
998
 
866
- worktree_root="${repo_root}/${WORKTREE_ROOT_REL}"
867
- mkdir -p "$worktree_root"
868
- worktree_leaf="$(resolve_worktree_leaf "$branch_name" "$agent_slug")"
869
999
  worktree_path="${worktree_root}/${worktree_leaf}"
870
1000
  openspec_plan_slug="$(resolve_openspec_plan_slug "$branch_name" "$agent_slug" "$task_slug")"
871
1001
  openspec_change_slug="$(resolve_openspec_change_slug "$branch_name" "$task_slug")"
872
1002
  openspec_capability_slug="$(resolve_openspec_capability_slug "$task_slug")"
873
1003
 
874
- if [[ -e "$worktree_path" ]]; then
875
- echo "[agent-branch-start] Worktree path already exists: ${worktree_path}" >&2
876
- exit 1
877
- fi
878
-
879
1004
  auto_transfer_stash_ref=""
880
1005
  auto_transfer_message=""
881
1006
  auto_transfer_source_branch=""
@@ -1009,7 +1134,11 @@ if [[ -n "$current_branch" && "$current_branch" != "HEAD" ]] && is_protected_bra
1009
1134
  fi
1010
1135
 
1011
1136
  worktree_add_output=""
1012
- if ! worktree_add_output="$(git -C "$repo_root" worktree add -b "$branch_name" "$worktree_path" "$start_ref" 2>&1)"; then
1137
+ worktree_checkout_args=()
1138
+ if [[ ${#SPARSE_EXCLUDES[@]} -gt 0 ]]; then
1139
+ worktree_checkout_args+=(--no-checkout)
1140
+ fi
1141
+ if ! worktree_add_output="$(git -C "$repo_root" worktree add "${worktree_checkout_args[@]}" -b "$branch_name" "$worktree_path" "$start_ref" 2>&1)"; then
1013
1142
  printf '%s\n' "$worktree_add_output" >&2
1014
1143
  exit 1
1015
1144
  fi
@@ -1020,8 +1149,22 @@ if [[ ! -d "$worktree_path" || ! -e "$worktree_path/.git" ]]; then
1020
1149
  printf '%s\n' "$worktree_add_output" >&2
1021
1150
  exit 1
1022
1151
  fi
1152
+ if [[ ${#SPARSE_EXCLUDES[@]} -gt 0 ]]; then
1153
+ # Set the sparse rules before the first checkout: never allocate the omitted
1154
+ # blobs, even temporarily. Git keeps the index/rules local to this worktree.
1155
+ {
1156
+ printf '/*\n'
1157
+ printf '!/%s/\n' "${SPARSE_EXCLUDES[@]}"
1158
+ } | git -C "$worktree_path" sparse-checkout set --no-cone --no-sparse-index --stdin
1159
+ git -C "$worktree_path" read-tree -mu HEAD
1160
+ echo "[agent-branch-start] Sparse checkout: omitted ${#SPARSE_EXCLUDES[@]} configured directories; git sparse-checkout disable restores them."
1161
+ fi
1023
1162
  git -C "$repo_root" config "branch.${branch_name}.guardexBase" "$BASE_BRANCH" >/dev/null 2>&1 || true
1024
1163
  git -C "$repo_root" config "branch.${branch_name}.guardexWorktreeRoot" "$WORKTREE_ROOT_REL" >/dev/null 2>&1 || true
1164
+ if [[ -n "$TASK_ID" ]]; then
1165
+ git -C "$repo_root" config "branch.${branch_name}.guardexTaskId" "$TASK_ID"
1166
+ git -C "$repo_root" config "branch.${branch_name}.guardexStartReady" false
1167
+ fi
1025
1168
  # Fresh agent branches should start unpublished; clear any inherited base-branch tracking.
1026
1169
  git -C "$worktree_path" branch --unset-upstream "$branch_name" >/dev/null 2>&1 || true
1027
1170
 
@@ -1041,10 +1184,7 @@ if [[ -n "$auto_transfer_stash_ref" ]]; then
1041
1184
  fi
1042
1185
  fi
1043
1186
 
1044
- hydrate_dependency_dir_symlink_in_worktree "$repo_root" "$worktree_path" ".venv"
1045
- hydrate_dependency_dir_symlink_in_worktree "$repo_root" "$worktree_path" "node_modules"
1046
- hydrate_dependency_dir_symlink_in_worktree "$repo_root" "$worktree_path" "apps/frontend/node_modules"
1047
- hydrate_dependency_dir_symlink_in_worktree "$repo_root" "$worktree_path" "apps/backend/node_modules"
1187
+ run_guardex_cli worktree provision --source "$repo_root" --target "$worktree_path" --with-defaults --mode "$PROVISION_MODE"
1048
1188
  if ! initialize_openspec_change_workspace "$repo_root" "$worktree_path" "$openspec_change_slug" "$openspec_capability_slug"; then
1049
1189
  exit 1
1050
1190
  fi
@@ -1052,6 +1192,21 @@ if ! initialize_openspec_plan_workspace "$repo_root" "$worktree_path" "$openspec
1052
1192
  exit 1
1053
1193
  fi
1054
1194
 
1195
+ if [[ "$PROVISION_MODE" == "full" ]]; then
1196
+ # A failed setup gate leaves the newly created worktree intact for repair,
1197
+ # but never marks it ready or launches a worker into it.
1198
+ run_guardex_cli worktree hook pre-start --source "$repo_root" --worktree "$worktree_path" --branch "$branch_name"
1199
+ fi
1200
+
1201
+ if [[ -n "$TASK_ID" ]]; then
1202
+ git -C "$repo_root" config "branch.${branch_name}.guardexStartReady" true
1203
+ fi
1204
+ if [[ "$REMEMBER_SPARSE" -eq 1 ]]; then
1205
+ git -C "$repo_root" config --local --unset-all multiagent.worktreeSparseExclude || [[ "$?" -eq 5 ]]
1206
+ for sparse_dir in "${SPARSE_EXCLUDES[@]}"; do
1207
+ git -C "$repo_root" config --local --add multiagent.worktreeSparseExclude "$sparse_dir"
1208
+ done
1209
+ fi
1055
1210
  echo "[agent-branch-start] Created branch: ${branch_name}"
1056
1211
  echo "[agent-branch-start] Worktree: ${worktree_path}"
1057
1212
  echo "[agent-branch-start] OpenSpec tier: ${OPENSPEC_TIER}"
@@ -1069,4 +1224,8 @@ elif [[ "$OPENSPEC_SKIP_PLAN" -eq 1 ]]; then
1069
1224
  else
1070
1225
  echo "[agent-branch-start] OpenSpec plan: openspec/plan/${openspec_plan_slug}"
1071
1226
  fi
1227
+ if [[ "$PROVISION_MODE" == "full" ]]; then
1228
+ run_guardex_cli worktree hook post-start --source "$repo_root" --worktree "$worktree_path" --branch "$branch_name" \
1229
+ || echo "[agent-branch-start] Warning: post-start hook could not be queued." >&2
1230
+ fi
1072
1231
  print_agent_next_steps "$branch_name" "$worktree_path" "implement + commit" "$BASE_BRANCH"
@@ -15,6 +15,9 @@ set -euo pipefail
15
15
 
16
16
  NODE_BIN="${GUARDEX_NODE_BIN:-node}"
17
17
  CLI_ENTRY="${GUARDEX_CLI_ENTRY:-}"
18
+ _STOP_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
19
+ # shellcheck source=lib/guardex-base-branch.sh
20
+ source "${_STOP_SCRIPT_DIR}/lib/guardex-base-branch.sh"
18
21
 
19
22
  run_guardex_cli() {
20
23
  if [[ -n "$CLI_ENTRY" ]]; then
@@ -66,32 +69,6 @@ elif value is not None:
66
69
  ' "$key" 2>/dev/null || true
67
70
  }
68
71
 
69
- resolve_base_branch() {
70
- local repo="$1" branch="$2" configured head_ref cand
71
- configured="$(git -C "$repo" config --get "branch.${branch}.guardexBase" 2>/dev/null || true)"
72
- if [[ -n "$configured" ]]; then
73
- printf '%s' "$configured"
74
- return 0
75
- fi
76
- configured="$(git -C "$repo" config --get multiagent.baseBranch 2>/dev/null || true)"
77
- if [[ -n "$configured" ]]; then
78
- printf '%s' "$configured"
79
- return 0
80
- fi
81
- head_ref="$(git -C "$repo" symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true)"
82
- if [[ -n "$head_ref" ]]; then
83
- printf '%s' "${head_ref#origin/}"
84
- return 0
85
- fi
86
- for cand in main dev master; do
87
- if git -C "$repo" show-ref --verify --quiet "refs/heads/${cand}"; then
88
- printf '%s' "$cand"
89
- return 0
90
- fi
91
- done
92
- printf 'main'
93
- }
94
-
95
72
  base_ref_for_count() {
96
73
  local repo="$1" base="$2"
97
74
  if git -C "$repo" show-ref --verify --quiet "refs/heads/${base}"; then
@@ -166,7 +143,7 @@ fi
166
143
  # is the outcome this tool exists to prevent.
167
144
  finish_cmd=(
168
145
  branch finish --branch "$branch" --base "$base_branch" --via-pr
169
- --gate-review --review-provider claude
146
+ --gate-review
170
147
  --wait-for-merge --cleanup
171
148
  )
172
149