@imdeadpool/guardex 7.1.0 → 8.1.0

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 (58) hide show
  1. package/README.md +122 -322
  2. package/package.json +25 -5
  3. package/scripts/prepublish-bump-version.js +191 -0
  4. package/skills/gitguardex/SKILL.md +83 -3
  5. package/src/agents/activity.js +279 -0
  6. package/src/agents/finish.js +15 -0
  7. package/src/ci-init/index.js +0 -1
  8. package/src/cli/args.js +203 -6
  9. package/src/cli/commands/agents.js +17 -0
  10. package/src/cli/commands/branch.js +267 -14
  11. package/src/cli/commands/claude.js +15 -0
  12. package/src/cli/commands/doctor.js +26 -0
  13. package/src/cli/commands/onboard.js +293 -0
  14. package/src/cli/commands/prompt.js +15 -3
  15. package/src/cli/commands/review.js +18 -0
  16. package/src/cli/commands/setup.js +8 -6
  17. package/src/cli/commands/status.js +12 -0
  18. package/src/cli/main.js +2 -0
  19. package/src/cli/shared-locks.js +304 -0
  20. package/src/context.js +21 -3
  21. package/src/core/runtime.js +33 -1
  22. package/src/doctor/index.js +58 -5
  23. package/src/finish/heartbeat.js +169 -0
  24. package/src/finish/index.js +106 -4
  25. package/src/finish/progress.js +262 -0
  26. package/src/finish/review-gate.js +453 -32
  27. package/src/github-api.js +59 -0
  28. package/src/mcp/collect.js +111 -12
  29. package/src/mcp/server.js +10 -4
  30. package/src/output/index.js +210 -0
  31. package/src/pr-checks.js +121 -0
  32. package/src/pr-review.js +581 -51
  33. package/src/pr.js +143 -21
  34. package/src/provider-binary.js +101 -0
  35. package/src/review-diff.js +116 -0
  36. package/src/review-fix.js +229 -0
  37. package/src/sandbox/index.js +11 -5
  38. package/src/scaffold/agent-worktree-prep.js +9 -2
  39. package/src/scaffold/index.js +4 -16
  40. package/src/scaffold/provision-config.js +314 -0
  41. package/src/shared-git-state.js +566 -0
  42. package/src/terminal/kitty.js +20 -0
  43. package/src/terminal/tmux.js +9 -0
  44. package/templates/AGENTS.multiagent-safety.md +73 -11
  45. package/templates/AGENTS.multiagent-safety.min.md +1 -0
  46. package/templates/codex/skills/gitguardex/SKILL.md +83 -3
  47. package/templates/githooks/post-checkout +4 -9
  48. package/templates/githooks/pre-commit +17 -1
  49. package/templates/github/workflows/README.md +30 -23
  50. package/templates/scripts/agent-autofinish-watch.sh +280 -0
  51. package/templates/scripts/agent-branch-finish.sh +363 -26
  52. package/templates/scripts/agent-branch-start.sh +76 -25
  53. package/templates/scripts/agent-claude-stop-finish.sh +194 -0
  54. package/templates/scripts/agent-file-locks.py +390 -52
  55. package/templates/scripts/agent-preflight.sh +14 -2
  56. package/templates/scripts/agent-stalled-report.sh +40 -0
  57. package/templates/scripts/codex-agent.sh +10 -63
  58. package/templates/github/workflows/cr.yml +0 -46
@@ -0,0 +1,280 @@
1
+ #!/usr/bin/env bash
2
+ # Detect stalled agent/* worktrees and (optionally) reap lanes whose PR already
3
+ # merged but whose worktree was retained on disk.
4
+ #
5
+ # This is the watcher that scripts/agent-stalled-report.sh (the SessionStart
6
+ # hook) expects. Without it, that shim soft-exits 0 and merged-PR worktrees are
7
+ # never cleaned up (the "retained for now" path in agent-branch-finish.sh).
8
+ #
9
+ # It does NOT reinvent cleanup: reaping delegates to `gx worktree prune`
10
+ # (scripts/agent-worktree-prune.sh), the existing, tested primitive.
11
+ #
12
+ # Per-lane status lines use the prefix the report shim greps:
13
+ # [agent-autofinish-watch] agent/<branch>: <status>
14
+ # A line is emitted ONLY for actionable lanes (merged-but-retained, or stalled
15
+ # with no open PR after the idle gate). Healthy in-flight lanes stay silent.
16
+ #
17
+ # Exit codes: 0 always (informational); reaping failures warn but do not fail.
18
+
19
+ set -euo pipefail
20
+
21
+ MODE="once" # once | daemon
22
+ DRY_RUN=0
23
+ AUTO_MERGE=0
24
+ INTERVAL=300
25
+ IDLE_MINUTES="${GUARDEX_AUTOFINISH_IDLE_MINUTES:-60}"
26
+ BASE_BRANCH="${GUARDEX_BASE_BRANCH:-}"
27
+ GH_BIN="${GUARDEX_GH_BIN:-gh}"
28
+ NOW_EPOCH_OVERRIDE="${GUARDEX_AUTOFINISH_NOW_EPOCH:-}"
29
+
30
+ WORKTREE_ROOT_RELS=(
31
+ ".omx/agent-worktrees"
32
+ ".omx/.tmp-worktrees"
33
+ ".omc/agent-worktrees"
34
+ ".omc/.tmp-worktrees"
35
+ )
36
+ LOCK_FILE_REL=".omx/state/agent-file-locks.json"
37
+
38
+ while [[ $# -gt 0 ]]; do
39
+ case "$1" in
40
+ --once) MODE="once"; shift ;;
41
+ --daemon) MODE="daemon"; shift ;;
42
+ --dry-run) DRY_RUN=1; shift ;;
43
+ --auto-merge) AUTO_MERGE=1; shift ;;
44
+ --interval)
45
+ [[ $# -ge 2 ]] || { echo "[agent-autofinish-watch] --interval requires a value" >&2; exit 1; }
46
+ INTERVAL="$2"; shift 2 ;;
47
+ --idle-minutes)
48
+ [[ $# -ge 2 ]] || { echo "[agent-autofinish-watch] --idle-minutes requires a value" >&2; exit 1; }
49
+ IDLE_MINUTES="$2"; shift 2 ;;
50
+ --base)
51
+ [[ $# -ge 2 ]] || { echo "[agent-autofinish-watch] --base requires a value" >&2; exit 1; }
52
+ BASE_BRANCH="$2"; shift 2 ;;
53
+ -h|--help)
54
+ echo "Usage: $0 [--once|--daemon] [--dry-run] [--auto-merge] [--interval SEC] [--idle-minutes MIN] [--base BRANCH]"
55
+ echo "Note: merged/open PR detection reads the most recent 200 PRs per state; a"
56
+ echo " branch whose merged PR is older than that will not be auto-reaped."
57
+ exit 0
58
+ ;;
59
+ *)
60
+ echo "[agent-autofinish-watch] Unknown argument: $1" >&2
61
+ exit 1
62
+ ;;
63
+ esac
64
+ done
65
+
66
+ if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
67
+ echo "[agent-autofinish-watch] Not inside a git repository." >&2
68
+ exit 0
69
+ fi
70
+
71
+ # Resolve the PRIMARY checkout root, not the current worktree: the managed
72
+ # worktree roots (.omc/agent-worktrees, ...) live under the primary checkout,
73
+ # and refs/reflogs are shared via the common git dir. Running from inside an
74
+ # agent worktree must still see every sibling lane.
75
+ git_common_dir="$(git rev-parse --git-common-dir 2>/dev/null)"
76
+ case "$git_common_dir" in
77
+ /*) ;;
78
+ *) git_common_dir="$(git rev-parse --show-toplevel)/${git_common_dir}" ;;
79
+ esac
80
+ repo_root="$(cd "$(dirname "$git_common_dir")" && pwd)"
81
+
82
+ resolve_base_branch() {
83
+ [[ -n "$BASE_BRANCH" ]] && return 0
84
+ local head_ref
85
+ head_ref="$(git -C "$repo_root" symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true)"
86
+ if [[ -n "$head_ref" ]]; then
87
+ BASE_BRANCH="${head_ref#origin/}"
88
+ return 0
89
+ fi
90
+ for cand in main master dev; do
91
+ if git -C "$repo_root" show-ref --verify --quiet "refs/heads/${cand}"; then
92
+ BASE_BRANCH="$cand"
93
+ return 0
94
+ fi
95
+ done
96
+ BASE_BRANCH="main"
97
+ }
98
+
99
+ is_managed_worktree_path() {
100
+ local entry="$1" rel
101
+ for rel in "${WORKTREE_ROOT_RELS[@]}"; do
102
+ [[ "$entry" == "${repo_root}/${rel}"/* ]] && return 0
103
+ done
104
+ return 1
105
+ }
106
+
107
+ is_temporary_worktree_path() {
108
+ local name
109
+ name="$(basename "$1")"
110
+ [[ "$name" == __agent_integrate-* || "$name" == __source-probe-* ]]
111
+ }
112
+
113
+ now_epoch() {
114
+ if [[ -n "$NOW_EPOCH_OVERRIDE" ]]; then
115
+ printf '%s' "$NOW_EPOCH_OVERRIDE"
116
+ else
117
+ date +%s
118
+ fi
119
+ }
120
+
121
+ has_live_process_in_worktree() {
122
+ local wt="$1" proc_cwd live_cwd
123
+ [[ -d /proc ]] || return 1
124
+ for proc_cwd in /proc/[0-9]*/cwd; do
125
+ [[ -e "$proc_cwd" ]] || continue
126
+ live_cwd="$(readlink "$proc_cwd" 2>/dev/null || true)"
127
+ [[ -n "$live_cwd" ]] || continue
128
+ live_cwd="${live_cwd% (deleted)}"
129
+ if [[ "$live_cwd" == "$wt" || "$live_cwd" == "${wt}"/* ]]; then
130
+ return 0
131
+ fi
132
+ done
133
+ return 1
134
+ }
135
+
136
+ branch_idle_minutes() {
137
+ local branch="$1" wt="$2" activity_epoch="" lock_mtime now
138
+ activity_epoch="$(git -C "$repo_root" reflog show --format='%ct' -n 1 "refs/heads/${branch}" 2>/dev/null | head -n1 | tr -d '[:space:]')"
139
+ if [[ -z "$activity_epoch" ]]; then
140
+ activity_epoch="$(git -C "$repo_root" log -1 --format='%ct' "$branch" 2>/dev/null | head -n1 | tr -d '[:space:]')"
141
+ fi
142
+ if [[ -n "$wt" && -f "${wt}/${LOCK_FILE_REL}" ]]; then
143
+ lock_mtime="$(stat -c %Y "${wt}/${LOCK_FILE_REL}" 2>/dev/null || stat -f %m "${wt}/${LOCK_FILE_REL}" 2>/dev/null || true)"
144
+ if [[ "$lock_mtime" =~ ^[0-9]+$ && ( -z "$activity_epoch" || "$lock_mtime" -gt "$activity_epoch" ) ]]; then
145
+ activity_epoch="$lock_mtime"
146
+ fi
147
+ fi
148
+ [[ "$activity_epoch" =~ ^[0-9]+$ ]] || { printf '%s' 999999; return; }
149
+ now="$(now_epoch)"
150
+ printf '%s' $(( (now - activity_epoch) / 60 ))
151
+ }
152
+
153
+ # Count uncommitted changes, ignoring lock-file churn.
154
+ dirty_count() {
155
+ local wt="$1"
156
+ git -C "$wt" status --porcelain -- . ":(exclude)${LOCK_FILE_REL}" 2>/dev/null | grep -c . || true
157
+ }
158
+
159
+ commits_ahead() {
160
+ local branch="$1"
161
+ git -C "$repo_root" rev-list --count "${BASE_BRANCH}..${branch}" 2>/dev/null || printf '0'
162
+ }
163
+
164
+ # Prefer the gx CLI; fall back to the bundled prune script.
165
+ run_prune() {
166
+ if command -v gx >/dev/null 2>&1; then
167
+ gx worktree prune "$@"
168
+ else
169
+ bash "${repo_root}/scripts/agent-worktree-prune.sh" "$@"
170
+ fi
171
+ }
172
+
173
+ declare -A MERGED_BRANCHES=()
174
+ declare -A OPEN_BRANCHES=()
175
+
176
+ load_pr_state() {
177
+ command -v "$GH_BIN" >/dev/null 2>&1 || return 0
178
+ local line
179
+ while IFS= read -r line; do
180
+ [[ -n "$line" ]] && MERGED_BRANCHES["$line"]=1
181
+ done < <("$GH_BIN" pr list --state merged --base "$BASE_BRANCH" --limit 200 --json headRefName --jq '.[].headRefName' 2>/dev/null || true)
182
+ while IFS= read -r line; do
183
+ [[ -n "$line" ]] && OPEN_BRANCHES["$line"]=1
184
+ done < <("$GH_BIN" pr list --state open --base "$BASE_BRANCH" --limit 200 --json headRefName --jq '.[].headRefName' 2>/dev/null || true)
185
+ }
186
+
187
+ run_once() {
188
+ resolve_base_branch
189
+ MERGED_BRANCHES=()
190
+ OPEN_BRANCHES=()
191
+ load_pr_state
192
+
193
+ local scanned=0 stalled=0 merged=0
194
+ local cur_wt="" cur_branch=""
195
+
196
+ while IFS= read -r line; do
197
+ if [[ "$line" == worktree\ * ]]; then
198
+ cur_wt="${line#worktree }"
199
+ cur_branch=""
200
+ elif [[ "$line" == branch\ refs/heads/* ]]; then
201
+ cur_branch="${line#branch refs/heads/}"
202
+ elif [[ -z "$line" ]]; then
203
+ process_lane "$cur_wt" "$cur_branch"
204
+ cur_wt=""; cur_branch=""
205
+ fi
206
+ done < <(git -C "$repo_root" worktree list --porcelain; printf '\n')
207
+
208
+ # Reap merged-but-retained lanes before the summary so reaped= is accurate.
209
+ if [[ "$merged" -gt 0 ]]; then
210
+ reap_merged
211
+ fi
212
+
213
+ printf '[agent-autofinish-watch] scanned=%s stalled=%s merged=%s reaped=%s\n' \
214
+ "$scanned" "$stalled" "$merged" "$reaped"
215
+ }
216
+
217
+ # process_lane mutates scanned/stalled/merged in the caller scope (bash dynamic
218
+ # scope via run_once's locals); reaped is a top-level global set by reap_merged.
219
+ process_lane() {
220
+ local wt="$1" branch="$2"
221
+ [[ -n "$wt" && -n "$branch" ]] || return 0
222
+ [[ "$branch" == agent/* ]] || return 0
223
+ is_managed_worktree_path "$wt" || return 0
224
+ is_temporary_worktree_path "$wt" && return 0
225
+ scanned=$((scanned + 1))
226
+
227
+ if [[ -n "${MERGED_BRANCHES[$branch]:-}" && -d "$wt" ]]; then
228
+ merged=$((merged + 1))
229
+ echo "[agent-autofinish-watch] ${branch}: merged PR, worktree retained -> prunable"
230
+ return 0
231
+ fi
232
+
233
+ # Open PR or live process => healthy in-flight, stay silent.
234
+ [[ -n "${OPEN_BRANCHES[$branch]:-}" ]] && return 0
235
+ has_live_process_in_worktree "$wt" && return 0
236
+
237
+ local idle dirty ahead
238
+ idle="$(branch_idle_minutes "$branch" "$wt")"
239
+ [[ "$idle" -ge "$IDLE_MINUTES" ]] || return 0
240
+
241
+ dirty="$(dirty_count "$wt")"
242
+ if [[ "$dirty" -gt 0 ]]; then
243
+ stalled=$((stalled + 1))
244
+ echo "[agent-autofinish-watch] ${branch}: ${dirty} uncommitted change(s), idle ${idle}m -> needs commit + finish"
245
+ return 0
246
+ fi
247
+
248
+ ahead="$(commits_ahead "$branch")"
249
+ if [[ "$ahead" -gt 0 ]]; then
250
+ stalled=$((stalled + 1))
251
+ echo "[agent-autofinish-watch] ${branch}: ${ahead} commit(s) ahead of ${BASE_BRANCH}, no PR, idle ${idle}m -> needs finish"
252
+ fi
253
+ }
254
+
255
+ reaped=0
256
+
257
+ reap_merged() {
258
+ [[ "$AUTO_MERGE" -eq 1 ]] || return 0
259
+ if [[ "$DRY_RUN" -eq 1 ]]; then
260
+ echo "[agent-autofinish-watch] [dry-run] would prune merged lanes: gx worktree prune --include-pr-merged --delete-branches --base ${BASE_BRANCH}"
261
+ return 0
262
+ fi
263
+ local out=""
264
+ out="$(run_prune --include-pr-merged --delete-branches --base "$BASE_BRANCH" 2>&1 || true)"
265
+ printf '%s\n' "$out"
266
+ local removed
267
+ removed="$(printf '%s\n' "$out" | sed -n 's/.*removed_worktrees=\([0-9]*\).*/\1/p' | head -n1)"
268
+ [[ "$removed" =~ ^[0-9]+$ ]] && reaped="$removed"
269
+ }
270
+
271
+ if [[ "$MODE" == "daemon" ]]; then
272
+ while true; do
273
+ reaped=0
274
+ run_once
275
+ sleep "$INTERVAL"
276
+ done
277
+ else
278
+ reaped=0
279
+ run_once
280
+ fi