@imdeadpool/guardex 7.0.43 → 7.1.1

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 (68) hide show
  1. package/README.md +35 -0
  2. package/package.json +4 -1
  3. package/scripts/prepublish-bump-version.js +191 -0
  4. package/skills/gx-act/SKILL.md +82 -0
  5. package/src/agents/activity.js +279 -0
  6. package/src/agents/inspect.js +17 -4
  7. package/src/agents/launch.js +10 -1
  8. package/src/agents/status.js +9 -6
  9. package/src/budget/index.js +2 -1
  10. package/src/cli/args.js +118 -5
  11. package/src/cli/commands/agents.js +381 -0
  12. package/src/cli/commands/bootstrap.js +92 -0
  13. package/src/cli/commands/branch.js +127 -0
  14. package/src/cli/commands/claude.js +674 -0
  15. package/src/cli/commands/doctor.js +268 -0
  16. package/src/cli/commands/finish.js +26 -0
  17. package/src/cli/commands/mcp.js +122 -0
  18. package/src/cli/commands/misc.js +304 -0
  19. package/src/cli/commands/pr.js +439 -0
  20. package/src/cli/commands/prompt.js +92 -0
  21. package/src/cli/commands/release.js +305 -0
  22. package/src/cli/commands/report.js +244 -0
  23. package/src/cli/commands/review.js +32 -0
  24. package/src/cli/commands/setup.js +242 -0
  25. package/src/cli/commands/status.js +338 -0
  26. package/src/cli/commands/watch.js +234 -0
  27. package/src/cli/main.js +68 -3726
  28. package/src/cli/shared/repo-env.js +161 -0
  29. package/src/cli/shared/sandbox.js +417 -0
  30. package/src/cli/shared/scaffolding.js +535 -0
  31. package/src/cli/shared/toolchain-shims.js +420 -0
  32. package/src/context.js +229 -11
  33. package/src/core/runtime.js +6 -1
  34. package/src/doctor/index.js +42 -13
  35. package/src/finish/index.js +147 -5
  36. package/src/finish/preflight.js +177 -0
  37. package/src/finish/review-gate.js +182 -0
  38. package/src/git/index.js +446 -4
  39. package/src/hooks/index.js +0 -64
  40. package/src/mcp/collect.js +394 -0
  41. package/src/mcp/server.js +163 -0
  42. package/src/output/index.js +67 -1
  43. package/src/pr-review.js +23 -0
  44. package/src/pr.js +381 -0
  45. package/src/sandbox/index.js +13 -2
  46. package/src/scaffold/agent-worktree-prep.js +220 -0
  47. package/src/scaffold/index.js +108 -10
  48. package/src/scaffold/provision-config.js +314 -0
  49. package/src/speckit/index.js +226 -0
  50. package/src/terminal/index.js +1 -76
  51. package/src/terminal/kitty.js +20 -0
  52. package/src/terminal/tmux.js +9 -1
  53. package/src/toolchain/index.js +20 -0
  54. package/templates/AGENTS.monorepo-apps.md +26 -0
  55. package/templates/AGENTS.multiagent-safety.md +68 -344
  56. package/templates/AGENTS.multiagent-safety.min.md +11 -0
  57. package/templates/codex/skills/gx-act/SKILL.md +82 -0
  58. package/templates/githooks/pre-commit +22 -19
  59. package/templates/scripts/agent-branch-finish.sh +17 -33
  60. package/templates/scripts/agent-branch-merge.sh +4 -1
  61. package/templates/scripts/agent-branch-start.sh +115 -6
  62. package/templates/scripts/agent-file-locks.py +245 -50
  63. package/templates/scripts/agent-preflight.sh +31 -5
  64. package/templates/scripts/agent-worktree-prune.sh +1 -1
  65. package/templates/scripts/codex-agent.sh +0 -91
  66. package/src/agents/detect.js +0 -160
  67. package/src/cockpit/keybindings.js +0 -224
  68. package/src/cockpit/layout.js +0 -224
@@ -108,6 +108,18 @@ case "$codex_require_agent_branch" in
108
108
  *) should_require_codex_agent_branch=1 ;;
109
109
  esac
110
110
 
111
+ # General lockdown knob (applies to ALL agent sessions, not just Codex).
112
+ # Default OFF: any branch that is not a protected base is an acceptable agent
113
+ # branch, so `vendor/x`, `feat/y`, or any ad-hoc name commits without ceremony.
114
+ # Set GUARDEX_REQUIRE_AGENT_BRANCH=1 (or `git config multiagent.requireAgentBranch
115
+ # true`) to force agent commits back onto the agent/* namespace.
116
+ require_agent_branch_raw="${GUARDEX_REQUIRE_AGENT_BRANCH:-$(git config --get multiagent.requireAgentBranch || true)}"
117
+ require_agent_branch="$(printf '%s' "$require_agent_branch_raw" | tr '[:upper:]' '[:lower:]')"
118
+ should_require_agent_branch=0
119
+ case "$require_agent_branch" in
120
+ 1|true|yes|on) should_require_agent_branch=1 ;;
121
+ esac
122
+
111
123
  is_codex_managed_only_commit_on_protected=0
112
124
  if [[ "$is_codex_session" == "1" && "$is_protected_branch" == "1" ]]; then
113
125
  deleted_paths="$(git diff --cached --name-only --diff-filter=D)"
@@ -138,8 +150,6 @@ if [[ "$should_require_codex_agent_branch" == "1" && "${GUARDEX_ALLOW_CODEX_ON_N
138
150
  GitGuardex requires Codex work to run from an isolated agent/* branch.
139
151
  Start the sub-branch/worktree with:
140
152
  gx branch start "<task-or-plan>" "<agent-name>"
141
- Or manually:
142
- gx branch start "<task-or-plan>" "<agent-name>"
143
153
  Then commit from the created agent/* branch.
144
154
 
145
155
  Temporary bypass (not recommended):
@@ -147,19 +157,10 @@ Temporary bypass (not recommended):
147
157
  MSG
148
158
  exit 1
149
159
  fi
150
-
151
- cat >&2 <<'MSG'
152
- [codex-branch-guard] Codex agent commit blocked on non-agent branch.
153
- Use isolated branch/worktree first:
154
- gx branch start "<task-or-plan>" "<agent-name>"
155
- Then commit from the created agent/* branch.
156
-
157
- Temporary bypass (not recommended):
158
- GUARDEX_ALLOW_CODEX_ON_NON_AGENT=1 git commit ...
159
- Disable this rule for a repo (not recommended):
160
- git config multiagent.codexRequireAgentBranch false
161
- MSG
162
- exit 1
160
+ # Non-protected branches (vendor/, feat/, any ad-hoc name) are fine for
161
+ # Codex too — being OFF a protected base is the only load-bearing rule.
162
+ # Re-impose the agent/* requirement with GUARDEX_REQUIRE_AGENT_BRANCH=1
163
+ # (handled by the general lockdown gate below).
163
164
  fi
164
165
  fi
165
166
 
@@ -192,15 +193,17 @@ MSG
192
193
  exit 1
193
194
  fi
194
195
 
195
- if [[ "$is_agent_session" == "1" && "$branch" != agent/* ]]; then
196
+ if [[ "$is_agent_session" == "1" && "$branch" != agent/* && "$should_require_agent_branch" == "1" ]]; then
196
197
  cat >&2 <<'MSG'
197
- [agent-branch-guard] Agent commits must run on dedicated agent/* branches.
198
+ [agent-branch-guard] Lockdown mode: agent commits must run on dedicated agent/* branches.
199
+ GUARDEX_REQUIRE_AGENT_BRANCH (or multiagent.requireAgentBranch) is enabled.
198
200
  Start an agent branch first:
199
201
  gx branch start "<task-or-plan>" "<agent-name>"
200
202
  Then commit on that branch.
201
203
 
202
- Temporary bypass (not recommended):
203
- ALLOW_COMMIT_ON_PROTECTED_BRANCH=1 git commit ...
204
+ Relax (any non-protected branch is normally fine):
205
+ unset GUARDEX_REQUIRE_AGENT_BRANCH
206
+ # or: git config multiagent.requireAgentBranch false
204
207
  MSG
205
208
  exit 1
206
209
  fi
@@ -17,16 +17,11 @@ WAIT_TIMEOUT_SECONDS_RAW="${GUARDEX_FINISH_WAIT_TIMEOUT_SECONDS:-1800}"
17
17
  WAIT_POLL_SECONDS_RAW="${GUARDEX_FINISH_WAIT_POLL_SECONDS:-10}"
18
18
  PARENT_GITLINK_AUTO_COMMIT_RAW="${GUARDEX_FINISH_PARENT_GITLINK_AUTO_COMMIT:-true}"
19
19
  AUTO_RESOLVE_MODE_RAW="${GUARDEX_FINISH_AUTO_RESOLVE:-none}"
20
- AUTO_RESOLVE_SAFE_GLOBS_DEFAULT='.omc/**:.omx/state/**:.dev-ports.json:apps/logs/**:.agents/settings.local.json:.codex/state/**:.claude/state/**'
20
+ AUTO_RESOLVE_SAFE_GLOBS_DEFAULT='.omc/**:.omx/state/**:.dev-ports.json:apps/logs/**:.codex/settings.local.json:.claude/settings.local.json:.codex/state/**:.claude/state/**'
21
21
  AUTO_RESOLVE_SAFE_GLOBS_RAW="${GUARDEX_FINISH_AUTO_RESOLVE_SAFE_GLOBS-$AUTO_RESOLVE_SAFE_GLOBS_DEFAULT}"
22
22
  PREFLIGHT_ENABLED_RAW="${GUARDEX_FINISH_PREFLIGHT:-true}"
23
23
  PREFLIGHT_SCRIPT_RAW="${GUARDEX_FINISH_PREFLIGHT_SCRIPT:-scripts/agent-preflight.sh}"
24
24
  AUTO_PROMOTE_DRAFT_RAW="${GUARDEX_FINISH_AUTO_PROMOTE:-true}"
25
- # --skip-checks (or GUARDEX_FINISH_SKIP_CHECKS=1): append `--admin` to every
26
- # `gh pr merge` invocation, bypassing required status checks. Requires admin
27
- # permission on the target repo. Use when CI is wedged (e.g. runner billing,
28
- # infrastructure outage) and the operator has accepted the risk.
29
- SKIP_CHECKS_RAW="${GUARDEX_FINISH_SKIP_CHECKS:-false}"
30
25
 
31
26
  run_guardex_cli() {
32
27
  if [[ -n "$CLI_ENTRY" ]]; then
@@ -150,12 +145,13 @@ WAIT_FOR_MERGE="$(normalize_bool "$WAIT_FOR_MERGE_RAW" "1")"
150
145
  WAIT_TIMEOUT_SECONDS="$(normalize_int "$WAIT_TIMEOUT_SECONDS_RAW" "1800" "30")"
151
146
  WAIT_POLL_SECONDS="$(normalize_int "$WAIT_POLL_SECONDS_RAW" "10" "0")"
152
147
  PARENT_GITLINK_AUTO_COMMIT="$(normalize_bool "$PARENT_GITLINK_AUTO_COMMIT_RAW" "1")"
153
- PREFLIGHT_ENABLED="$(normalize_bool "$PREFLIGHT_ENABLED_RAW" "1")"
154
- AUTO_PROMOTE_DRAFT="$(normalize_bool "$AUTO_PROMOTE_DRAFT_RAW" "1")"
155
- SKIP_CHECKS="$(normalize_bool "$SKIP_CHECKS_RAW" "0")"
156
148
 
157
149
  while [[ $# -gt 0 ]]; do
158
150
  case "$1" in
151
+ -h|--help)
152
+ echo "Usage: $0 [--base <branch>] [--branch <branch>] [--no-push] [--cleanup|--no-cleanup] [--wait-for-merge|--no-wait-for-merge] [--wait-timeout-seconds <n>] [--wait-poll-seconds <n>] [--parent-gitlink-commit|--no-parent-gitlink-commit] [--keep-remote-branch|--delete-remote-branch] [--mode auto|direct|pr|--via-pr|--direct-only] [--auto-resolve[=none|safe|full]|--no-auto-resolve] [--no-preflight|--preflight] [--preflight-script <path>] [--no-auto-promote|--auto-promote]"
153
+ exit 0
154
+ ;;
159
155
  --base)
160
156
  BASE_BRANCH="${2:-}"
161
157
  BASE_BRANCH_EXPLICIT=1
@@ -252,14 +248,6 @@ while [[ $# -gt 0 ]]; do
252
248
  PREFLIGHT_SCRIPT_RAW="${2:-}"
253
249
  shift 2
254
250
  ;;
255
- --skip-checks)
256
- SKIP_CHECKS=1
257
- shift
258
- ;;
259
- --no-skip-checks)
260
- SKIP_CHECKS=0
261
- shift
262
- ;;
263
251
  --no-auto-promote)
264
252
  AUTO_PROMOTE_DRAFT_RAW="false"
265
253
  shift
@@ -276,6 +264,14 @@ while [[ $# -gt 0 ]]; do
276
264
  esac
277
265
  done
278
266
 
267
+ # Normalize toggles whose flags set the RAW value DURING the parse loop
268
+ # (--no-preflight/--preflight, --no-auto-promote/--auto-promote). These MUST be
269
+ # normalized AFTER the loop — doing it before (as the env-only defaults above do)
270
+ # silently ignored the flags, leaving --no-preflight inert. Flags that set the
271
+ # normalized var directly in-loop (--cleanup, --wait-for-merge, ...) are unaffected.
272
+ PREFLIGHT_ENABLED="$(normalize_bool "$PREFLIGHT_ENABLED_RAW" "1")"
273
+ AUTO_PROMOTE_DRAFT="$(normalize_bool "$AUTO_PROMOTE_DRAFT_RAW" "1")"
274
+
279
275
  if [[ "$CLEANUP_AFTER_MERGE" -eq 1 && "$DELETE_REMOTE_BRANCH_EXPLICIT" -eq 0 ]]; then
280
276
  DELETE_REMOTE_BRANCH=1
281
277
  fi
@@ -495,7 +491,7 @@ stored_worktree_root_rel="$(git -C "$repo_root" config --get "branch.${SOURCE_BR
495
491
  if [[ -z "$stored_worktree_root_rel" ]]; then
496
492
  stored_worktree_root_rel=".omx/agent-worktrees"
497
493
  fi
498
- agent_worktree_root="${repo_common_root}/${stored_worktree_root_rel}"
494
+ agent_worktree_root="${repo_root}/${stored_worktree_root_rel}"
499
495
  runtime_state_root_rel="$(dirname "$stored_worktree_root_rel")"
500
496
  temp_worktree_root="${repo_common_root}/${runtime_state_root_rel}/.tmp-worktrees"
501
497
 
@@ -1161,13 +1157,9 @@ wait_for_pr_merge() {
1161
1157
  deadline=$(( $(date +%s) + WAIT_TIMEOUT_SECONDS ))
1162
1158
  local wait_notice_printed=0
1163
1159
  local merge_output=""
1164
- local -a merge_flags=(--squash --delete-branch)
1165
- if [[ "$SKIP_CHECKS" -eq 1 ]]; then
1166
- merge_flags+=(--admin)
1167
- fi
1168
1160
 
1169
1161
  while true; do
1170
- if merge_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" "${merge_flags[@]}" 2>&1)"; then
1162
+ if merge_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" --squash --delete-branch 2>&1)"; then
1171
1163
  return 0
1172
1164
  fi
1173
1165
  if is_local_branch_delete_error "$merge_output"; then
@@ -1273,11 +1265,7 @@ run_pr_flow() {
1273
1265
  maybe_auto_promote_pr "$pr_url"
1274
1266
 
1275
1267
  merge_output=""
1276
- local -a merge_flags=(--squash --delete-branch)
1277
- if [[ "$SKIP_CHECKS" -eq 1 ]]; then
1278
- merge_flags+=(--admin)
1279
- fi
1280
- if merge_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" "${merge_flags[@]}" 2>&1)"; then
1268
+ if merge_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" --squash --delete-branch 2>&1)"; then
1281
1269
  return 0
1282
1270
  fi
1283
1271
  if is_local_branch_delete_error "$merge_output"; then
@@ -1291,11 +1279,7 @@ run_pr_flow() {
1291
1279
  fi
1292
1280
 
1293
1281
  auto_output=""
1294
- local -a auto_flags=(--squash --delete-branch --auto)
1295
- if [[ "$SKIP_CHECKS" -eq 1 ]]; then
1296
- auto_flags+=(--admin)
1297
- fi
1298
- if auto_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" "${auto_flags[@]}" 2>&1)"; then
1282
+ if auto_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" --squash --delete-branch --auto 2>&1)"; then
1299
1283
  echo "[agent-branch-finish] PR auto-merge enabled; waiting for required checks/reviews." >&2
1300
1284
  return 2
1301
1285
  fi
@@ -281,7 +281,10 @@ if [[ -z "$TARGET_BRANCH" ]]; then
281
281
  start_output=""
282
282
  if ! start_output="$(
283
283
  cd "$repo_root"
284
- GUARDEX_OPENSPEC_AUTO_INIT=1 run_guardex_cli branch start "$TASK_NAME" "$AGENT_NAME" "$BASE_BRANCH" 2>&1
284
+ # An integration lane is inherently cross-cutting, so pin it to T3 (full
285
+ # change + plan workspace) regardless of the branch-start default (now T1).
286
+ GUARDEX_OPENSPEC_AUTO_INIT=1 GUARDEX_OPENSPEC_TIER="${GUARDEX_OPENSPEC_TIER:-T3}" \
287
+ run_guardex_cli branch start "$TASK_NAME" "$AGENT_NAME" "$BASE_BRANCH" 2>&1
285
288
  )"; then
286
289
  printf '%s\n' "$start_output" >&2
287
290
  exit 1
@@ -14,10 +14,13 @@ OPENSPEC_PLAN_SLUG_OVERRIDE="${GUARDEX_OPENSPEC_PLAN_SLUG:-}"
14
14
  OPENSPEC_CHANGE_SLUG_OVERRIDE="${GUARDEX_OPENSPEC_CHANGE_SLUG:-}"
15
15
  OPENSPEC_CAPABILITY_SLUG_OVERRIDE="${GUARDEX_OPENSPEC_CAPABILITY_SLUG:-}"
16
16
  OPENSPEC_MASTERPLAN_LABEL_RAW="${GUARDEX_OPENSPEC_MASTERPLAN_LABEL-masterplan}"
17
- OPENSPEC_TIER_RAW="${GUARDEX_OPENSPEC_TIER:-T3}"
17
+ # Default tier is T1 (notes.md only, minimal scaffold): most tasks are small,
18
+ # and a full T3 plan workspace costs thousands of tokens an agent never reads.
19
+ # Escalate explicitly with --tier T2 (behavior change) or T3 (plan-driven).
20
+ OPENSPEC_TIER_RAW="${GUARDEX_OPENSPEC_TIER:-T1}"
18
21
  REUSE_EXISTING_RAW="${GUARDEX_BRANCH_START_REUSE_EXISTING:-true}"
19
22
  AUTO_TRANSFER_ENABLED_RAW="${GUARDEX_AUTO_TRANSFER:-true}"
20
- AUTO_TRANSFER_EXCLUDE_DEFAULT='.omc/**:.omx/state/**:.dev-ports.json:apps/logs/**:.agents/settings.local.json:.codex/state/**:.claude/state/**'
23
+ AUTO_TRANSFER_EXCLUDE_DEFAULT='.omc/**:.omx/state/**:.dev-ports.json:apps/logs/**:.codex/settings.local.json:.claude/settings.local.json:.codex/state/**:.claude/state/**'
21
24
  AUTO_TRANSFER_EXCLUDE_RAW="${GUARDEX_AUTO_TRANSFER_EXCLUDE-$AUTO_TRANSFER_EXCLUDE_DEFAULT}"
22
25
  PRINT_NAME_ONLY=0
23
26
  POSITIONAL_ARGS=()
@@ -39,8 +42,40 @@ run_guardex_cli() {
39
42
  return 127
40
43
  }
41
44
 
45
+ print_usage() {
46
+ cat <<'USAGE'
47
+ Usage: agent-branch-start [task] [agent] [base] [options]
48
+
49
+ Start an isolated agent/* branch + worktree for a task.
50
+
51
+ Positional:
52
+ task Task name/slug (default: "task")
53
+ agent Agent name (default: "agent")
54
+ base Base branch to fork from (default: repo default)
55
+
56
+ Options:
57
+ --task <name> Task name/slug
58
+ --agent <name> Agent name
59
+ --base <branch> Base branch to fork from
60
+ --worktree-root <p> Worktree root dir (default: .omx/agent-worktrees)
61
+ --reuse-existing Reuse an existing matching worktree (default)
62
+ --new Force a fresh worktree instead of reusing
63
+ --tier <T1|T2|T3> OpenSpec tier for scaffolding (default T1; T2 for a
64
+ behavior change, T3 for plan-driven work)
65
+ --transfer Auto-transfer uncommitted changes into the worktree (default)
66
+ --no-transfer Do not auto-transfer uncommitted changes
67
+ --transfer-exclude <globs> Colon-separated globs to exclude from transfer
68
+ --print-name-only Print the computed branch name and exit
69
+ -h, --help Show this help and exit
70
+ USAGE
71
+ }
72
+
42
73
  while [[ $# -gt 0 ]]; do
43
74
  case "$1" in
75
+ -h|--help)
76
+ print_usage
77
+ exit 0
78
+ ;;
44
79
  --task)
45
80
  TASK_NAME="${2:-task}"
46
81
  shift 2
@@ -292,7 +327,7 @@ normalize_tier() {
292
327
  esac
293
328
  }
294
329
 
295
- if ! OPENSPEC_TIER="$(normalize_tier "$OPENSPEC_TIER_RAW" "T3")"; then
330
+ if ! OPENSPEC_TIER="$(normalize_tier "$OPENSPEC_TIER_RAW" "T1")"; then
296
331
  echo "[agent-branch-start] Unsupported OpenSpec tier: ${OPENSPEC_TIER_RAW}" >&2
297
332
  exit 1
298
333
  fi
@@ -417,6 +452,20 @@ print_agent_next_steps() {
417
452
  base_branch="main"
418
453
  fi
419
454
 
455
+ # Pre-`Ready:` post-condition check. `git worktree add` has been observed
456
+ # to return 0 with the worktree dir still missing on disk (race condition
457
+ # during the OpenSpec / dependency-symlink phase between `worktree add`
458
+ # and now). If we print `Ready:` in that state, callers cd into a
459
+ # vanished directory and lose subsequent edits silently. Surface it as
460
+ # an exit-1 error instead — operator can retry + `git worktree prune`.
461
+ if [[ ! -d "$worktree_path" || ! -e "$worktree_path/.git" ]]; then
462
+ printf '[agent-branch-start] ERROR: worktree did not materialize on disk before Ready:\n' >&2
463
+ printf '[agent-branch-start] branch: %s\n' "$branch_name" >&2
464
+ printf '[agent-branch-start] expected: %s\n' "$worktree_path" >&2
465
+ printf '[agent-branch-start] Run `git worktree prune` to clear ghost entries, then retry.\n' >&2
466
+ exit 1
467
+ fi
468
+
420
469
  echo "[agent-branch-start] Ready:"
421
470
  echo " branch: ${branch_name}"
422
471
  echo " worktree: ${worktree_path}"
@@ -494,6 +543,25 @@ managed_worktree_roots() {
494
543
  done
495
544
  }
496
545
 
546
+ branch_published_then_remote_pruned() {
547
+ # Detect the post-`gx branch finish --via-pr --cleanup` state: the agent
548
+ # branch was published (so upstream config is set), but the remote-tracking
549
+ # ref no longer exists (because `push origin --delete` ran during cleanup).
550
+ # That combination only arises after a finish that successfully merged the
551
+ # PR and pruned the remote branch — a freshly-created agent branch never
552
+ # has an upstream until publish, so this never false-positives on the
553
+ # "started, dirty, no commits yet" case that we want to keep reusable.
554
+ local repo="$1"
555
+ local branch="$2"
556
+ local upstream
557
+ upstream="$(git -C "$repo" config --get "branch.${branch}.remote" 2>/dev/null || true)"
558
+ [[ -n "$upstream" ]] || return 1
559
+ if git -C "$repo" show-ref --verify --quiet "refs/remotes/${upstream}/${branch}"; then
560
+ return 1
561
+ fi
562
+ return 0
563
+ }
564
+
497
565
  find_matching_dirty_agent_worktree() {
498
566
  local repo="$1"
499
567
  local worktree_root_rel="$2"
@@ -514,6 +582,13 @@ find_matching_dirty_agent_worktree() {
514
582
  fi
515
583
  [[ "$branch" == "agent/${agent_slug}/"* ]] || continue
516
584
  has_local_changes "$entry" || continue
585
+ # Skip merged-and-cleaned worktrees that happen to still be on disk
586
+ # (e.g. operator's shell is cwd'd inside; cleanup deferred). Reusing
587
+ # such a worktree would silently hand the next agent a stale HEAD.
588
+ if branch_published_then_remote_pruned "$repo" "$branch"; then
589
+ echo "[agent-branch-start] Skipping merged-and-cleaned worktree: ${entry} (branch ${branch} has no remote tracking ref)" >&2
590
+ continue
591
+ fi
517
592
 
518
593
  descriptor="${branch#agent/${agent_slug}/}"
519
594
  score="$(token_match_score "$task_slug" "$descriptor")"
@@ -786,6 +861,20 @@ auto_transfer_message=""
786
861
  auto_transfer_source_branch=""
787
862
  auto_transfer_completed=0
788
863
 
864
+ # Preserve an un-appliable auto-transfer stash as a permanent ref instead of
865
+ # leaving it to pile up in the stash list forever. Stash entries are already
866
+ # commits, so we just point a ref at the same object and drop the stash entry;
867
+ # the work stays fully recoverable via `git stash apply <ref>`. Echoes the ref.
868
+ archive_auto_transfer_stash() {
869
+ local ref="$1"
870
+ local sha archive_ref
871
+ sha="$(git -C "$repo_root" rev-parse --verify "$ref" 2>/dev/null)" || return 1
872
+ archive_ref="refs/stash-archive/${auto_transfer_message:-auto-transfer}-${sha:0:8}"
873
+ git -C "$repo_root" update-ref "$archive_ref" "$sha" 2>/dev/null || return 1
874
+ git -C "$repo_root" stash drop "$ref" >/dev/null 2>&1 || true
875
+ printf '%s' "$archive_ref"
876
+ }
877
+
789
878
  restore_auto_transfer_stash_on_failure() {
790
879
  local exit_code="${1:-0}"
791
880
  if [[ "$exit_code" -eq 0 ]] || [[ -z "$auto_transfer_stash_ref" ]] || [[ "$auto_transfer_completed" -eq 1 ]]; then
@@ -793,13 +882,23 @@ restore_auto_transfer_stash_on_failure() {
793
882
  fi
794
883
 
795
884
  local transfer_label="${auto_transfer_source_branch:-$BASE_BRANCH}"
796
- if git -C "$repo_root" stash apply "$auto_transfer_stash_ref" >/dev/null 2>&1; then
885
+ # GUARDEX_TEST_FAIL_RESTORE_APPLY forces the restore-apply to fail so the
886
+ # archive path can be exercised deterministically in tests.
887
+ if ! env_flag_truthy "${GUARDEX_TEST_FAIL_RESTORE_APPLY:-}" \
888
+ && git -C "$repo_root" stash apply "$auto_transfer_stash_ref" >/dev/null 2>&1; then
797
889
  git -C "$repo_root" stash drop "$auto_transfer_stash_ref" >/dev/null 2>&1 || true
798
890
  auto_transfer_stash_ref=""
799
891
  echo "[agent-branch-start] Restored moved changes back to '${transfer_label}' after startup failure." >&2
800
892
  else
801
- echo "[agent-branch-start] Startup failed and auto-restore also failed." >&2
802
- echo "[agent-branch-start] Changes are preserved in ${auto_transfer_stash_ref} on ${transfer_label}." >&2
893
+ local archived_ref
894
+ if archived_ref="$(archive_auto_transfer_stash "$auto_transfer_stash_ref")"; then
895
+ auto_transfer_stash_ref=""
896
+ echo "[agent-branch-start] Startup failed and auto-restore also failed." >&2
897
+ echo "[agent-branch-start] Changes archived at ${archived_ref} on ${transfer_label} (recover with: git stash apply ${archived_ref})." >&2
898
+ else
899
+ echo "[agent-branch-start] Startup failed and auto-restore also failed." >&2
900
+ echo "[agent-branch-start] Changes are preserved in ${auto_transfer_stash_ref} on ${transfer_label}." >&2
901
+ fi
803
902
  fi
804
903
  }
805
904
 
@@ -864,6 +963,13 @@ if ! worktree_add_output="$(git -C "$repo_root" worktree add -b "$branch_name" "
864
963
  printf '%s\n' "$worktree_add_output" >&2
865
964
  exit 1
866
965
  fi
966
+ # git worktree add has been observed to exit 0 while the target dir is
967
+ # missing — verify before downstream init runs against a phantom path.
968
+ if [[ ! -d "$worktree_path" || ! -e "$worktree_path/.git" ]]; then
969
+ printf '[agent-branch-start] ERROR: git worktree add reported success but %s is not a valid worktree.\n' "$worktree_path" >&2
970
+ printf '%s\n' "$worktree_add_output" >&2
971
+ exit 1
972
+ fi
867
973
  git -C "$repo_root" config "branch.${branch_name}.guardexBase" "$BASE_BRANCH" >/dev/null 2>&1 || true
868
974
  git -C "$repo_root" config "branch.${branch_name}.guardexWorktreeRoot" "$WORKTREE_ROOT_REL" >/dev/null 2>&1 || true
869
975
  # Fresh agent branches should start unpublished; clear any inherited base-branch tracking.
@@ -899,6 +1005,9 @@ fi
899
1005
  echo "[agent-branch-start] Created branch: ${branch_name}"
900
1006
  echo "[agent-branch-start] Worktree: ${worktree_path}"
901
1007
  echo "[agent-branch-start] OpenSpec tier: ${OPENSPEC_TIER}"
1008
+ if [[ "$OPENSPEC_TIER" == "T1" ]]; then
1009
+ echo "[agent-branch-start] T1 minimal scaffold (notes.md). Escalate: --tier T2 for a behavior change, T3 for plan-driven work."
1010
+ fi
902
1011
  if [[ "$OPENSPEC_AUTO_INIT" -ne 1 ]]; then
903
1012
  echo "[agent-branch-start] OpenSpec change: skipped (GUARDEX_OPENSPEC_AUTO_INIT disabled)"
904
1013
  elif [[ "$OPENSPEC_SKIP_CHANGE" -eq 1 ]]; then