@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.
- package/CONTRIBUTING.md +13 -4
- package/README.md +223 -1
- package/THIRD-PARTY-NOTICES.md +29 -0
- package/bin/multiagent-safety.js +9 -12
- package/package.json +4 -2
- package/skills/gitguardex/SKILL.md +28 -0
- package/skills/pr-lens/SKILL.md +87 -0
- package/src/abide.js +534 -0
- package/src/agents/activity.js +23 -4
- package/src/agents/inspect.js +15 -6
- package/src/agents/launch.js +6 -1
- package/src/agents/process-lease.js +84 -0
- package/src/agents/sessions.js +96 -16
- package/src/agents/start.js +18 -9
- package/src/agents/status.js +35 -14
- package/src/agents/supervise.js +87 -0
- package/src/budget/index.js +6 -1
- package/src/cli/args.js +71 -77
- package/src/cli/commands/agents.js +38 -10
- package/src/cli/commands/branch.js +116 -103
- package/src/cli/commands/claude.js +604 -50
- package/src/cli/commands/codex.js +359 -0
- package/src/cli/commands/context.js +52 -0
- package/src/cli/commands/mcp.js +5 -1
- package/src/cli/commands/pr.js +138 -3
- package/src/cli/commands/review.js +3 -1
- package/src/cli/commands/setup.js +25 -0
- package/src/cli/commands/status.js +128 -60
- package/src/cli/commands/storage.js +24 -0
- package/src/cli/commands/usage.js +48 -0
- package/src/cli/commands/watch.js +157 -47
- package/src/cli/commands/worktree-copy-ignored.js +38 -0
- package/src/cli/commands/worktree-estimate.js +69 -0
- package/src/cli/commands/worktree-hooks.js +62 -0
- package/src/cli/commands/worktree-navigation.js +71 -0
- package/src/cli/commands/worktree-provision.js +92 -0
- package/src/cli/main.js +229 -110
- package/src/cli/shared/gate-flags.js +131 -0
- package/src/cli/shared/scaffolding.js +31 -3
- package/src/cli/shared-locks.js +3 -1
- package/src/cockpit/pane-actions.js +43 -7
- package/src/context.js +373 -17
- package/src/doctor/index.js +29 -35
- package/src/finish/events.js +254 -0
- package/src/finish/index.js +322 -62
- package/src/finish/post-branch-finish-cleanup.js +316 -22
- package/src/finish/preflight.js +44 -1
- package/src/finish/progress.js +36 -0
- package/src/finish/review-gate.js +317 -127
- package/src/finish/review-receipt.js +156 -0
- package/src/finish/with-claims-lock.py +29 -0
- package/src/git/index.js +179 -42
- package/src/hooks/index.js +68 -3
- package/src/mcp/collect.js +61 -5
- package/src/mcp/server.js +4 -0
- package/src/output/index.js +167 -1
- package/src/pr-lens-publish.js +93 -0
- package/src/pr-lens.js +184 -0
- package/src/pr-respond.js +860 -0
- package/src/pr-review.js +519 -159
- package/src/pr.js +73 -14
- package/src/provider-binary.js +21 -0
- package/src/review-diff-context.js +90 -0
- package/src/review-fix.js +23 -17
- package/src/scaffold/agent-worktree-prep.js +14 -12
- package/src/scaffold/dependency-store.js +234 -0
- package/src/scaffold/index.js +13 -1
- package/src/scaffold/provision-approvals.js +83 -0
- package/src/scaffold/provision-config.js +169 -40
- package/src/shared-git-state.js +11 -1
- package/src/speckit/index.js +12 -1
- package/src/storage/run-artifacts.js +185 -0
- package/src/toolchain/index.js +322 -2
- package/src/worktree-copy-ignored.js +200 -0
- package/src/worktree-hook-worker.js +29 -0
- package/src/worktree-hooks.js +333 -0
- package/src/worktree-navigation.js +248 -0
- package/src/worktree-picker.js +89 -0
- package/src/worktree-quota.js +100 -0
- package/src/worktree-start.js +100 -0
- package/templates/AGENTS.multiagent-safety.md +8 -0
- package/templates/AGENTS.multiagent-safety.min.md +3 -1
- package/templates/codex/skills/gitguardex/SKILL.md +28 -0
- package/templates/codex/skills/opensrc/SKILL.md +24 -0
- package/templates/githooks/post-merge +16 -2
- package/templates/scripts/agent-autofinish-watch.sh +17 -18
- package/templates/scripts/agent-branch-finish.sh +180 -19
- package/templates/scripts/agent-branch-merge.sh +7 -32
- package/templates/scripts/agent-branch-start.sh +202 -43
- package/templates/scripts/agent-claude-stop-finish.sh +4 -27
- package/templates/scripts/agent-file-locks.py +257 -29
- package/templates/scripts/agent-preflight.sh +3 -0
- package/templates/scripts/agent-worktree-prune.sh +975 -63
- package/templates/scripts/codex-agent.sh +55 -5
- package/templates/scripts/lib/guardex-base-branch.sh +207 -0
- package/templates/scripts/openspec/init-change-workspace.sh +10 -18
- package/templates/scripts/review-bot-watch.sh +171 -4
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
|
|
19
19
|
set -euo pipefail
|
|
20
20
|
|
|
21
|
+
_AUTOFINISH_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
22
|
+
# shellcheck source=lib/guardex-base-branch.sh
|
|
23
|
+
source "${_AUTOFINISH_SCRIPT_DIR}/lib/guardex-base-branch.sh"
|
|
24
|
+
|
|
21
25
|
MODE="once" # once | daemon
|
|
22
26
|
DRY_RUN=0
|
|
23
27
|
AUTO_MERGE=0
|
|
@@ -79,23 +83,6 @@ case "$git_common_dir" in
|
|
|
79
83
|
esac
|
|
80
84
|
repo_root="$(cd "$(dirname "$git_common_dir")" && pwd)"
|
|
81
85
|
|
|
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
86
|
is_managed_worktree_path() {
|
|
100
87
|
local entry="$1" rel
|
|
101
88
|
for rel in "${WORKTREE_ROOT_RELS[@]}"; do
|
|
@@ -120,6 +107,18 @@ now_epoch() {
|
|
|
120
107
|
|
|
121
108
|
has_live_process_in_worktree() {
|
|
122
109
|
local wt="$1" proc_cwd live_cwd
|
|
110
|
+
|
|
111
|
+
if [[ -d "${git_common_dir}/gx-process-leases" ]]; then
|
|
112
|
+
local lease_probe="${GUARDEX_CLI_ENTRY:-}"
|
|
113
|
+
[[ -n "$lease_probe" ]] || return 0
|
|
114
|
+
lease_probe="$(dirname "$lease_probe")/../src/agents/process-lease.js"
|
|
115
|
+
[[ -f "$lease_probe" ]] || return 0
|
|
116
|
+
local lease_status=0
|
|
117
|
+
"${GUARDEX_NODE_BIN:-node}" "$lease_probe" "$wt" || lease_status=$?
|
|
118
|
+
# Unknown probes fail closed too; only explicit inactive (1) can continue.
|
|
119
|
+
[[ "$lease_status" -eq 1 ]] || return 0
|
|
120
|
+
fi
|
|
121
|
+
|
|
123
122
|
[[ -d /proc ]] || return 1
|
|
124
123
|
for proc_cwd in /proc/[0-9]*/cwd; do
|
|
125
124
|
[[ -e "$proc_cwd" ]] || continue
|
|
@@ -185,7 +184,7 @@ load_pr_state() {
|
|
|
185
184
|
}
|
|
186
185
|
|
|
187
186
|
run_once() {
|
|
188
|
-
resolve_base_branch
|
|
187
|
+
[[ -z "$BASE_BRANCH" ]] && BASE_BRANCH="$(resolve_base_branch "$repo_root")"
|
|
189
188
|
MERGED_BRANCHES=()
|
|
190
189
|
OPEN_BRANCHES=()
|
|
191
190
|
load_pr_state
|
|
@@ -12,6 +12,8 @@ GH_BIN="${GUARDEX_GH_BIN:-gh}"
|
|
|
12
12
|
NODE_BIN="${GUARDEX_NODE_BIN:-node}"
|
|
13
13
|
CLI_ENTRY="${GUARDEX_CLI_ENTRY:-}"
|
|
14
14
|
FINISH_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
15
|
+
# shellcheck source=lib/guardex-base-branch.sh
|
|
16
|
+
source "${FINISH_SCRIPT_DIR}/lib/guardex-base-branch.sh"
|
|
15
17
|
CLEANUP_AFTER_MERGE_RAW="${GUARDEX_FINISH_CLEANUP:-true}"
|
|
16
18
|
WAIT_FOR_MERGE_RAW="${GUARDEX_FINISH_WAIT_FOR_MERGE:-true}"
|
|
17
19
|
WAIT_TIMEOUT_SECONDS_RAW="${GUARDEX_FINISH_WAIT_TIMEOUT_SECONDS:-1800}"
|
|
@@ -26,6 +28,9 @@ PREFLIGHT_ENABLED_RAW="${GUARDEX_FINISH_PREFLIGHT:-true}"
|
|
|
26
28
|
PREFLIGHT_SCRIPT_RAW="${GUARDEX_FINISH_PREFLIGHT_SCRIPT:-scripts/agent-preflight.sh}"
|
|
27
29
|
PREFLIGHT_REQUIRED_RAW="${GUARDEX_FINISH_REQUIRE_PREFLIGHT:-false}"
|
|
28
30
|
PREFLIGHT_CACHE_ENABLED_RAW="${GUARDEX_FINISH_PREFLIGHT_CACHE:-true}"
|
|
31
|
+
# This invocation's mode must not leak into tests or any nested finish command.
|
|
32
|
+
PREFLIGHT_ONLY="${GUARDEX_FINISH_PREFLIGHT_ONLY:-0}"
|
|
33
|
+
unset GUARDEX_FINISH_PREFLIGHT_ONLY
|
|
29
34
|
AUTO_PROMOTE_DRAFT_RAW="${GUARDEX_FINISH_AUTO_PROMOTE:-true}"
|
|
30
35
|
FINISH_CHECKLIST_RAW="${GUARDEX_FINISH_CHECKLIST:-false}"
|
|
31
36
|
FINISH_GATE_DONE_RAW="${GUARDEX_FINISH_GATE_DONE:-false}"
|
|
@@ -191,7 +196,7 @@ resolve_preflight_script() {
|
|
|
191
196
|
|
|
192
197
|
# Bind a reusable success receipt to every input that can change the result.
|
|
193
198
|
# The receipt is per source branch and is overwritten when HEAD, the base ref,
|
|
194
|
-
# or the configured preflight script changes. Mandatory
|
|
199
|
+
# or the configured preflight script changes. Mandatory CI fallback
|
|
195
200
|
# preflights deliberately bypass this cache and always execute fail-closed.
|
|
196
201
|
preflight_cache_fingerprint() {
|
|
197
202
|
local worktree="$1"
|
|
@@ -262,8 +267,8 @@ run_preflight() {
|
|
|
262
267
|
local worktree="$1"
|
|
263
268
|
if [[ "$PREFLIGHT_ENABLED" -ne 1 ]]; then
|
|
264
269
|
if [[ "$PREFLIGHT_REQUIRED" -eq 1 ]]; then
|
|
265
|
-
finish_progress failed preflight "required
|
|
266
|
-
echo "[agent-branch-finish]
|
|
270
|
+
finish_progress failed preflight "required by the GitHub CI fallback"
|
|
271
|
+
echo "[agent-branch-finish] GitHub CI fallback requires local pre-flight; --no-preflight is not allowed." >&2
|
|
267
272
|
return 1
|
|
268
273
|
fi
|
|
269
274
|
finish_progress skipped preflight "disabled by flag"
|
|
@@ -274,20 +279,21 @@ run_preflight() {
|
|
|
274
279
|
if [[ -z "$script_path" ]]; then
|
|
275
280
|
if [[ "$PREFLIGHT_REQUIRED" -eq 1 ]]; then
|
|
276
281
|
finish_progress failed preflight "required executable script missing"
|
|
277
|
-
echo "[agent-branch-finish]
|
|
282
|
+
echo "[agent-branch-finish] GitHub CI fallback requires a target-aware pre-flight script at ${PREFLIGHT_SCRIPT_RAW} in trusted base ${start_ref}; refusing push." >&2
|
|
278
283
|
return 1
|
|
279
284
|
fi
|
|
280
285
|
finish_progress skipped preflight "no executable pre-flight script"
|
|
281
286
|
echo "[agent-branch-finish] No executable pre-flight script at ${PREFLIGHT_SCRIPT_RAW} (in ${worktree}); skipping pre-flight." >&2
|
|
282
287
|
return 0
|
|
283
288
|
fi
|
|
284
|
-
finish_progress running preflight "final verification before publish"
|
|
285
|
-
echo "[agent-branch-finish] Running pre-flight: ${script_path}" >&2
|
|
286
289
|
local preflight_cwd="$worktree"
|
|
287
290
|
local trusted_tree=""
|
|
288
291
|
if [[ "$PREFLIGHT_REQUIRED" -eq 1 && "$PREFLIGHT_SCRIPT_RAW" != /* ]]; then
|
|
289
292
|
local relative_configured="${PREFLIGHT_SCRIPT_RAW#./}"
|
|
290
|
-
local
|
|
293
|
+
local _raw_tmpdir="${TMPDIR:-/tmp}"
|
|
294
|
+
local _resolved_tmpdir
|
|
295
|
+
_resolved_tmpdir="$(cd -P "$_raw_tmpdir" 2>/dev/null && pwd -P)" || _resolved_tmpdir="$_raw_tmpdir"
|
|
296
|
+
local trusted_prefix="${_resolved_tmpdir}/guardex-preflight."
|
|
291
297
|
if [[ "$script_path" != "$trusted_prefix"*"/$relative_configured" ]]; then
|
|
292
298
|
finish_progress failed preflight "trusted execution tree missing"
|
|
293
299
|
echo "[agent-branch-finish] Mandatory pre-flight did not resolve inside its trusted base archive; refusing push." >&2
|
|
@@ -323,6 +329,8 @@ run_preflight() {
|
|
|
323
329
|
fi
|
|
324
330
|
fi
|
|
325
331
|
fi
|
|
332
|
+
finish_progress running preflight "final verification before publish"
|
|
333
|
+
echo "[agent-branch-finish] Running pre-flight: ${script_path}" >&2
|
|
326
334
|
local preflight_status=0
|
|
327
335
|
if [[ -n "$trusted_tree" ]]; then
|
|
328
336
|
( cd "$preflight_cwd" && "$script_path" --guardex-target-worktree "$worktree" ) || preflight_status=$?
|
|
@@ -379,6 +387,7 @@ place_hold_marker() {
|
|
|
379
387
|
fi
|
|
380
388
|
if ! "$GH_BIN" pr edit "$pr_url" --body "${body}"$'\n\n'"${HOLD_MARKER_COMMENT}" >/dev/null 2>&1; then
|
|
381
389
|
echo "[agent-branch-finish] Warning: could not write the ${HOLD_MARKER} marker to the PR body; the hold will NOT survive an unflagged re-run of the finish flow." >&2
|
|
390
|
+
return 1
|
|
382
391
|
fi
|
|
383
392
|
}
|
|
384
393
|
|
|
@@ -892,9 +901,17 @@ if [[ "$BASE_BRANCH_EXPLICIT" -eq 0 ]]; then
|
|
|
892
901
|
if [[ -n "$source_branch_base" ]]; then
|
|
893
902
|
BASE_BRANCH="$source_branch_base"
|
|
894
903
|
else
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
904
|
+
# Infer from git history before trusting the repo-wide configured base.
|
|
905
|
+
inferred_finish_base="$(guardex_infer_base_branch "$repo_root" "$SOURCE_BRANCH" || true)"
|
|
906
|
+
if [[ -n "$inferred_finish_base" ]]; then
|
|
907
|
+
BASE_BRANCH="$inferred_finish_base"
|
|
908
|
+
printf '[gx] base for '"'"'%s'"'"' not recorded; inferred '"'"'%s'"'"' from git history (set branch.%s.guardexBase to override)\n' \
|
|
909
|
+
"$SOURCE_BRANCH" "$inferred_finish_base" "$SOURCE_BRANCH" >&2
|
|
910
|
+
else
|
|
911
|
+
configured_base="$(git -C "$repo_root" config --get multiagent.baseBranch || true)"
|
|
912
|
+
if [[ -n "$configured_base" ]]; then
|
|
913
|
+
BASE_BRANCH="$configured_base"
|
|
914
|
+
fi
|
|
898
915
|
fi
|
|
899
916
|
fi
|
|
900
917
|
fi
|
|
@@ -966,7 +983,7 @@ cleanup_missing_merged_source_branch() {
|
|
|
966
983
|
fi
|
|
967
984
|
fi
|
|
968
985
|
|
|
969
|
-
prune_args=(worktree prune --base "$BASE_BRANCH" --only-dirty-worktrees --delete-branches)
|
|
986
|
+
prune_args=(worktree prune --base "$BASE_BRANCH" --only-dirty-worktrees --keep-clean-unmerged --delete-branches)
|
|
970
987
|
if [[ "$DELETE_REMOTE_BRANCH" -eq 1 ]]; then
|
|
971
988
|
prune_args+=(--delete-remote-branches)
|
|
972
989
|
fi
|
|
@@ -1079,18 +1096,103 @@ if [[ -z "$source_worktree" ]]; then
|
|
|
1079
1096
|
created_source_probe=1
|
|
1080
1097
|
fi
|
|
1081
1098
|
|
|
1082
|
-
|
|
1099
|
+
# Untracked files count only here: finishing would strand them when the source
|
|
1100
|
+
# worktree is pruned. The other is_clean_worktree callers look at checkouts where
|
|
1101
|
+
# untracked files are normal and harmless.
|
|
1102
|
+
if ! is_clean_worktree "$source_worktree" \
|
|
1103
|
+
|| [[ -n "$(git -C "$source_worktree" ls-files --others --exclude-standard -- . \
|
|
1104
|
+
':(exclude).omx/state/agent-file-locks.json' ':(exclude).omx/state/finish-runs/')" ]]; then
|
|
1083
1105
|
echo "[agent-branch-finish] Source worktree is not clean for '${SOURCE_BRANCH}': ${source_worktree}" >&2
|
|
1084
1106
|
echo "[agent-branch-finish] Commit/stash changes on the source branch before finishing." >&2
|
|
1085
1107
|
exit 1
|
|
1086
1108
|
fi
|
|
1087
1109
|
|
|
1110
|
+
# The gate preserves the reviewed SOURCE commit. Base reads detect observed
|
|
1111
|
+
# drift, not an atomic base reservation: GitHub's PR merge API has a head CAS
|
|
1112
|
+
# only. Integration uses GitHub's current base and normal repository protections.
|
|
1113
|
+
assert_reviewed_revision() {
|
|
1114
|
+
[[ "$FINISH_GATE_DONE" -eq 1 ]] || return 0
|
|
1115
|
+
local head base branch status
|
|
1116
|
+
head="$(git -C "$source_worktree" rev-parse HEAD)" || return 1
|
|
1117
|
+
branch="$(git -C "$source_worktree" symbolic-ref --quiet --short HEAD)" || return 1
|
|
1118
|
+
status="$(git -C "$source_worktree" status --porcelain)" || return 1
|
|
1119
|
+
base="$(git -C "$repo_root" ls-remote --exit-code origin "refs/heads/$BASE_BRANCH")" || return 1
|
|
1120
|
+
base="${base%%[[:space:]]*}"
|
|
1121
|
+
if [[ "$MERGE_MODE" != "pr" || "$PUSH_ENABLED" -ne 1 || "$branch" != "$SOURCE_BRANCH" || -z "${GUARDEX_FINISH_REVIEWED_HEAD:-}" || -z "${GUARDEX_FINISH_REVIEWED_BASE:-}" \
|
|
1122
|
+
|| "$head" != "$GUARDEX_FINISH_REVIEWED_HEAD" || "$base" != "$GUARDEX_FINISH_REVIEWED_BASE" ]]; then
|
|
1123
|
+
echo "[agent-branch-finish] Reviewed revision changed or missing. Rerun the review gate; refusing synchronization, push, and merge." >&2
|
|
1124
|
+
return 1
|
|
1125
|
+
fi
|
|
1126
|
+
if [[ -n "$status" ]]; then
|
|
1127
|
+
echo "[agent-branch-finish] Reviewed revision has uncommitted changes. Rerun the review gate." >&2
|
|
1128
|
+
return 1
|
|
1129
|
+
fi
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
pre_merge_hook_ran=0
|
|
1133
|
+
run_pre_merge_hook() {
|
|
1134
|
+
[[ "$pre_merge_hook_ran" -eq 0 ]] || return 0
|
|
1135
|
+
local before after status before_status before_diff after_diff
|
|
1136
|
+
before="$(git -C "$source_worktree" rev-parse HEAD)" || return 1
|
|
1137
|
+
before_status="$(git -C "$source_worktree" status --porcelain)" || return 1
|
|
1138
|
+
before_diff="$(git -C "$source_worktree" diff --no-ext-diff --binary HEAD -- | git hash-object --stdin)" || return 1
|
|
1139
|
+
run_guardex_cli worktree hook pre-merge --source "$repo_root" --worktree "$source_worktree" --branch "$SOURCE_BRANCH" || return 1
|
|
1140
|
+
after="$(git -C "$source_worktree" rev-parse HEAD)" || return 1
|
|
1141
|
+
status="$(git -C "$source_worktree" status --porcelain)" || return 1
|
|
1142
|
+
after_diff="$(git -C "$source_worktree" diff --no-ext-diff --binary HEAD -- | git hash-object --stdin)" || return 1
|
|
1143
|
+
if [[ "$before" != "$after" || "$before_status" != "$status" || "$before_diff" != "$after_diff" ]]; then
|
|
1144
|
+
echo "[agent-branch-finish] pre-merge changed the source revision or worktree; rerun verification before merging." >&2
|
|
1145
|
+
return 1
|
|
1146
|
+
fi
|
|
1147
|
+
pre_merge_hook_ran=1
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
assert_synchronous_merge() {
|
|
1151
|
+
[[ "$FINISH_GATE_DONE" -eq 1 ]] || return 0
|
|
1152
|
+
local id queue_enabled
|
|
1153
|
+
id="$("$GH_BIN" pr view "$SOURCE_BRANCH" --json id --jq .id)" || return 1
|
|
1154
|
+
queue_enabled="$("$GH_BIN" api graphql \
|
|
1155
|
+
-f query='query($id:ID!){node(id:$id){... on PullRequest{isMergeQueueEnabled}}}' \
|
|
1156
|
+
-f id="$id" --jq '.data.node.isMergeQueueEnabled')" || return 1
|
|
1157
|
+
if [[ "$queue_enabled" != "false" ]]; then
|
|
1158
|
+
echo "[agent-branch-finish] Review gate cannot authorize a queued merge (or unknown queue policy). Refusing to enqueue." >&2
|
|
1159
|
+
return 1
|
|
1160
|
+
fi
|
|
1161
|
+
assert_reviewed_revision
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
# The CLI can run the repository preflight as soon as it promotes the reviewed
|
|
1165
|
+
# PR, while GitHub runs CI. This branch exits before any publish/merge action.
|
|
1166
|
+
# The final invocation still verifies the revision and preflight receipt, and
|
|
1167
|
+
# will always re-run a mandatory trusted-base preflight if CI fallback needs it.
|
|
1168
|
+
if [[ "${PREFLIGHT_ONLY:-0}" == 1 ]]; then
|
|
1169
|
+
[[ "$FINISH_GATE_DONE" -eq 1 ]] || exit 1
|
|
1170
|
+
[[ "$PREFLIGHT_CACHE_ENABLED" -eq 1 && "$PREFLIGHT_ENABLED" -eq 1 && "$PREFLIGHT_REQUIRED" -eq 0 ]] || exit 0
|
|
1171
|
+
[[ -n "$(resolve_preflight_script "$source_worktree" "$PREFLIGHT_SCRIPT_RAW")" ]] || exit 0
|
|
1172
|
+
start_ref="origin/${BASE_BRANCH}"
|
|
1173
|
+
cached_base_sha="$(git -C "$source_worktree" rev-parse --verify "${start_ref}^{commit}" 2>/dev/null || true)"
|
|
1174
|
+
[[ -n "$cached_base_sha" && "$cached_base_sha" == "${GUARDEX_FINISH_REVIEWED_BASE:-}" ]] || exit 0
|
|
1175
|
+
assert_reviewed_revision || exit 1
|
|
1176
|
+
run_preflight "$source_worktree" || exit 1
|
|
1177
|
+
assert_reviewed_revision || exit 1
|
|
1178
|
+
exit 0
|
|
1179
|
+
fi
|
|
1180
|
+
|
|
1181
|
+
merge_head_args=()
|
|
1182
|
+
if [[ "$FINISH_GATE_DONE" -eq 1 ]]; then
|
|
1183
|
+
assert_reviewed_revision || exit 1
|
|
1184
|
+
merge_head_args=(--match-head-commit "$GUARDEX_FINISH_REVIEWED_HEAD")
|
|
1185
|
+
fi
|
|
1186
|
+
|
|
1088
1187
|
start_ref="$BASE_BRANCH"
|
|
1089
1188
|
if git -C "$repo_root" show-ref --verify --quiet "refs/remotes/origin/${BASE_BRANCH}"; then
|
|
1090
1189
|
git -C "$repo_root" fetch origin "$BASE_BRANCH" --quiet
|
|
1091
1190
|
start_ref="origin/${BASE_BRANCH}"
|
|
1092
1191
|
fi
|
|
1093
1192
|
|
|
1193
|
+
# A successful gate authorizes one revision, not automatic synchronization.
|
|
1194
|
+
# Keep BOTH rebase and the conflict-reconciliation merge probe outside that gate.
|
|
1195
|
+
if [[ "$FINISH_GATE_DONE" -ne 1 ]]; then
|
|
1094
1196
|
require_before_finish_raw="$(git -C "$repo_root" config --get multiagent.sync.requireBeforeFinish || true)"
|
|
1095
1197
|
if [[ -z "$require_before_finish_raw" ]]; then
|
|
1096
1198
|
require_before_finish_raw="true"
|
|
@@ -1276,12 +1378,15 @@ if git -C "$repo_root" show-ref --verify --quiet "refs/remotes/origin/${BASE_BRA
|
|
|
1276
1378
|
fi
|
|
1277
1379
|
fi
|
|
1278
1380
|
|
|
1381
|
+
fi
|
|
1382
|
+
|
|
1279
1383
|
should_create_integration_helper=1
|
|
1280
1384
|
if [[ "$MERGE_MODE" == "pr" && "$PUSH_ENABLED" -eq 1 ]]; then
|
|
1281
1385
|
should_create_integration_helper=0
|
|
1282
1386
|
fi
|
|
1283
1387
|
|
|
1284
1388
|
if [[ "$should_create_integration_helper" -eq 1 ]]; then
|
|
1389
|
+
run_pre_merge_hook || exit 1
|
|
1285
1390
|
existing_base_worktree=""
|
|
1286
1391
|
if [[ "$PUSH_ENABLED" -eq 0 ]]; then
|
|
1287
1392
|
existing_base_worktree="$(get_worktree_for_branch "$BASE_BRANCH")"
|
|
@@ -1358,6 +1463,27 @@ is_remote_branch_missing_error() {
|
|
|
1358
1463
|
return 1
|
|
1359
1464
|
}
|
|
1360
1465
|
|
|
1466
|
+
# Returns 0 when the gh pr merge failure is caused by the PR head having
|
|
1467
|
+
# advanced past the SHA recorded in --match-head-commit. This is distinct from
|
|
1468
|
+
# "checks still pending": the problem is a concurrent push, not CI state, so
|
|
1469
|
+
# retrying for 30 minutes will never succeed and only wastes operator time.
|
|
1470
|
+
# Matching on the GitHub merge API's 422 body and known gh error strings.
|
|
1471
|
+
is_match_head_commit_error() {
|
|
1472
|
+
local output="$1"
|
|
1473
|
+
# Only relevant when --match-head-commit was armed; a plain merge gets
|
|
1474
|
+
# different 422 causes (e.g. merge conflict) that the loop should still retry.
|
|
1475
|
+
[[ "${#merge_head_args[@]}" -eq 0 ]] && return 1
|
|
1476
|
+
# GitHub merge API: sha parameter did not match the PR's current head.
|
|
1477
|
+
if [[ "$output" == *"merge_commit_sha is not equal"* ]] \
|
|
1478
|
+
|| [[ "$output" == *"HEAD doesn't match expected SHA"* ]] \
|
|
1479
|
+
|| [[ "$output" == *"Head doesn't match expected SHA"* ]] \
|
|
1480
|
+
|| [[ "$output" == *"Head branch was modified"* ]] \
|
|
1481
|
+
|| [[ "$output" == *"head commit"*"not match"* ]]; then
|
|
1482
|
+
return 0
|
|
1483
|
+
fi
|
|
1484
|
+
return 1
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1361
1487
|
local_branch_exists() {
|
|
1362
1488
|
local branch="$1"
|
|
1363
1489
|
git -C "$repo_root" show-ref --verify --quiet "refs/heads/${branch}"
|
|
@@ -1618,13 +1744,26 @@ wait_for_pr_merge() {
|
|
|
1618
1744
|
local merge_output=""
|
|
1619
1745
|
|
|
1620
1746
|
while true; do
|
|
1621
|
-
|
|
1747
|
+
run_pre_merge_hook || return 1
|
|
1748
|
+
assert_synchronous_merge || return 1
|
|
1749
|
+
if merge_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" --squash --delete-branch "${merge_head_args[@]}" 2>&1)"; then
|
|
1622
1750
|
return 0
|
|
1623
1751
|
fi
|
|
1624
1752
|
if is_local_branch_delete_error "$merge_output"; then
|
|
1625
1753
|
echo "[agent-branch-finish] PR merged but gh could not delete the local branch (active worktree); continuing local cleanup." >&2
|
|
1626
1754
|
return 0
|
|
1627
1755
|
fi
|
|
1756
|
+
# A SHA mismatch (--match-head-commit vs current PR head) will never resolve
|
|
1757
|
+
# by waiting: a new commit was pushed after the review gate ran. Exit now
|
|
1758
|
+
# with an actionable message rather than looping for 30 minutes.
|
|
1759
|
+
if is_match_head_commit_error "$merge_output"; then
|
|
1760
|
+
echo "[agent-branch-finish] Branch was updated after the review gate passed (SHA mismatch). Re-run gx ship to re-review and merge the latest commit." >&2
|
|
1761
|
+
if [[ -n "$pr_url" ]]; then
|
|
1762
|
+
echo "[agent-branch-finish] PR: ${pr_url}" >&2
|
|
1763
|
+
fi
|
|
1764
|
+
echo "$merge_output" >&2
|
|
1765
|
+
return 1
|
|
1766
|
+
fi
|
|
1628
1767
|
|
|
1629
1768
|
PR_STATE=""
|
|
1630
1769
|
PR_MERGED_AT=""
|
|
@@ -1685,7 +1824,13 @@ run_pr_flow() {
|
|
|
1685
1824
|
fi
|
|
1686
1825
|
|
|
1687
1826
|
maybe_push_changed_submodule_branches "$start_ref" "$SOURCE_BRANCH"
|
|
1688
|
-
|
|
1827
|
+
assert_reviewed_revision || return 1
|
|
1828
|
+
if [[ "$FINISH_GATE_DONE" -eq 1 ]]; then
|
|
1829
|
+
# Do not resolve a mutable local branch again after the revision check.
|
|
1830
|
+
git -C "$source_worktree" push origin "${GUARDEX_FINISH_REVIEWED_HEAD}:refs/heads/${SOURCE_BRANCH}" || return 1
|
|
1831
|
+
else
|
|
1832
|
+
git -C "$source_worktree" push -u origin "$SOURCE_BRANCH"
|
|
1833
|
+
fi
|
|
1689
1834
|
|
|
1690
1835
|
pr_title="$(git -C "$repo_root" log -1 --pretty=%s "$SOURCE_BRANCH" 2>/dev/null || true)"
|
|
1691
1836
|
if [[ -z "$pr_title" ]]; then
|
|
@@ -1793,7 +1938,7 @@ run_pr_flow() {
|
|
|
1793
1938
|
# the load-bearing hold.
|
|
1794
1939
|
"$GH_BIN" pr merge "$pr_url" --disable-auto >/dev/null 2>&1 || true
|
|
1795
1940
|
"$GH_BIN" pr ready --undo "$pr_url" >/dev/null 2>&1 || true
|
|
1796
|
-
place_hold_marker "$pr_url"
|
|
1941
|
+
place_hold_marker "$pr_url" || true
|
|
1797
1942
|
finish_progress skipped merge "merge hold active"
|
|
1798
1943
|
echo "[agent-branch-finish] Merge held (--no-auto-promote): PR left unmerged for review/e2e." >&2
|
|
1799
1944
|
return 2
|
|
@@ -1801,7 +1946,9 @@ run_pr_flow() {
|
|
|
1801
1946
|
|
|
1802
1947
|
finish_progress running merge "waiting for GitHub merge readiness"
|
|
1803
1948
|
merge_output=""
|
|
1804
|
-
|
|
1949
|
+
run_pre_merge_hook || return 1
|
|
1950
|
+
assert_synchronous_merge || return 1
|
|
1951
|
+
if merge_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" --squash --delete-branch "${merge_head_args[@]}" 2>&1)"; then
|
|
1805
1952
|
return 0
|
|
1806
1953
|
fi
|
|
1807
1954
|
if is_local_branch_delete_error "$merge_output"; then
|
|
@@ -1814,8 +1961,12 @@ run_pr_flow() {
|
|
|
1814
1961
|
return $?
|
|
1815
1962
|
fi
|
|
1816
1963
|
|
|
1964
|
+
if [[ "$FINISH_GATE_DONE" -eq 1 ]]; then
|
|
1965
|
+
echo "[agent-branch-finish] Review gate cannot authorize a future auto-merge; rerun finish when ready." >&2
|
|
1966
|
+
return 1
|
|
1967
|
+
fi
|
|
1817
1968
|
auto_output=""
|
|
1818
|
-
if auto_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" --squash --delete-branch --auto 2>&1)"; then
|
|
1969
|
+
if auto_output="$("$GH_BIN" pr merge "$SOURCE_BRANCH" --squash --delete-branch --auto "${merge_head_args[@]}" 2>&1)"; then
|
|
1819
1970
|
echo "[agent-branch-finish] PR auto-merge enabled; waiting for required checks/reviews." >&2
|
|
1820
1971
|
return 2
|
|
1821
1972
|
fi
|
|
@@ -1915,6 +2066,8 @@ fi
|
|
|
1915
2066
|
# the merge scrolls away. Everything below this line is cleanup: it can warn,
|
|
1916
2067
|
# it must not fail the run, because the work is already in the base branch.
|
|
1917
2068
|
finish_progress complete merge "landed in ${BASE_BRANCH}"
|
|
2069
|
+
run_guardex_cli worktree hook post-merge --source "$repo_root" --worktree "$repo_root" --branch "$SOURCE_BRANCH" \
|
|
2070
|
+
|| echo "[agent-branch-finish] Warning: merge succeeded but post-merge hook could not be queued." >&2
|
|
1918
2071
|
echo "[agent-branch-finish] ✅ MERGED ${SOURCE_BRANCH} -> ${BASE_BRANCH} (${merge_status} flow)"
|
|
1919
2072
|
if [[ -n "$pr_url" ]]; then
|
|
1920
2073
|
echo "[agent-branch-finish] ✅ PR: ${pr_url}"
|
|
@@ -1927,6 +2080,11 @@ else
|
|
|
1927
2080
|
fi
|
|
1928
2081
|
|
|
1929
2082
|
run_guardex_cli locks release --branch "$SOURCE_BRANCH" >/dev/null 2>&1 || true
|
|
2083
|
+
# Sweep any other orphaned locks from previously-merged branches. This catches
|
|
2084
|
+
# locks held by branches whose PRs landed before the current finish run
|
|
2085
|
+
# (e.g. branches merged days ago whose worktrees were never cleaned up).
|
|
2086
|
+
# Fail-safe: best-effort, never blocks a successful merge.
|
|
2087
|
+
run_guardex_cli locks reap >/dev/null 2>&1 || true
|
|
1930
2088
|
|
|
1931
2089
|
base_worktree="$(get_worktree_for_branch "$BASE_BRANCH")"
|
|
1932
2090
|
refresh_clean_base_worktree "$base_worktree"
|
|
@@ -1962,8 +2120,11 @@ if [[ "$CLEANUP_AFTER_MERGE" -eq 1 ]]; then
|
|
|
1962
2120
|
git -C "$source_worktree" checkout --detach >/dev/null 2>&1 || true
|
|
1963
2121
|
fi
|
|
1964
2122
|
|
|
2123
|
+
# Source worktree removal goes through the guarded prune path below, so
|
|
2124
|
+
# active ownership, dirty state and lifecycle vetoes cannot be bypassed.
|
|
1965
2125
|
if [[ "$source_worktree" != "$current_worktree" && "$source_worktree" == "${agent_worktree_root}"/* ]]; then
|
|
1966
|
-
|
|
2126
|
+
run_guardex_prune --base "$BASE_BRANCH" --branch "$SOURCE_BRANCH" --delete-branches --only-dirty-worktrees \
|
|
2127
|
+
|| echo "[agent-branch-finish] Warning: guarded source cleanup deferred." >&2
|
|
1967
2128
|
fi
|
|
1968
2129
|
|
|
1969
2130
|
# The merge already landed (see the MERGED banner above), so a branch that
|
|
@@ -1996,7 +2157,7 @@ if [[ "$CLEANUP_AFTER_MERGE" -eq 1 ]]; then
|
|
|
1996
2157
|
# after we deliberately kept a branch with unlanded commits would force-delete
|
|
1997
2158
|
# exactly what we just protected. Skip branch deletion for this run; the next
|
|
1998
2159
|
# finish or an explicit `gx cleanup` sweeps the rest.
|
|
1999
|
-
prune_args=(--base "$BASE_BRANCH" --only-dirty-worktrees)
|
|
2160
|
+
prune_args=(--base "$BASE_BRANCH" --only-dirty-worktrees --keep-clean-unmerged --include-pr-merged)
|
|
2000
2161
|
if [[ "$local_branch_cleaned" -eq 1 ]]; then
|
|
2001
2162
|
prune_args+=(--delete-branches)
|
|
2002
2163
|
if [[ "$DELETE_REMOTE_BRANCH" -eq 1 ]]; then
|
|
@@ -9,6 +9,9 @@ AGENT_NAME="${GUARDEX_MERGE_AGENT_NAME:-codex}"
|
|
|
9
9
|
NODE_BIN="${GUARDEX_NODE_BIN:-node}"
|
|
10
10
|
CLI_ENTRY="${GUARDEX_CLI_ENTRY:-}"
|
|
11
11
|
declare -a SOURCE_BRANCHES=()
|
|
12
|
+
_MERGE_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
13
|
+
# shellcheck source=lib/guardex-base-branch.sh
|
|
14
|
+
source "${_MERGE_SCRIPT_DIR}/lib/guardex-base-branch.sh"
|
|
12
15
|
|
|
13
16
|
usage() {
|
|
14
17
|
cat <<'EOF'
|
|
@@ -48,43 +51,15 @@ sanitize_slug() {
|
|
|
48
51
|
printf '%s' "$slug"
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
resolve_base_branch() {
|
|
52
|
-
local repo="$1"
|
|
53
|
-
local explicit_target="$2"
|
|
54
|
-
local configured=""
|
|
55
|
-
local branch_base=""
|
|
56
|
-
|
|
57
|
-
if [[ -n "$explicit_target" ]]; then
|
|
58
|
-
branch_base="$(git -C "$repo" config --get "branch.${explicit_target}.guardexBase" || true)"
|
|
59
|
-
if [[ -n "$branch_base" ]]; then
|
|
60
|
-
printf '%s' "$branch_base"
|
|
61
|
-
return 0
|
|
62
|
-
fi
|
|
63
|
-
fi
|
|
64
|
-
|
|
65
|
-
configured="$(git -C "$repo" config --get multiagent.baseBranch || true)"
|
|
66
|
-
if [[ -n "$configured" ]]; then
|
|
67
|
-
printf '%s' "$configured"
|
|
68
|
-
return 0
|
|
69
|
-
fi
|
|
70
|
-
|
|
71
|
-
for fallback in dev main master; do
|
|
72
|
-
if git -C "$repo" show-ref --verify --quiet "refs/heads/${fallback}" \
|
|
73
|
-
|| git -C "$repo" show-ref --verify --quiet "refs/remotes/origin/${fallback}"; then
|
|
74
|
-
printf '%s' "$fallback"
|
|
75
|
-
return 0
|
|
76
|
-
fi
|
|
77
|
-
done
|
|
78
|
-
|
|
79
|
-
printf '%s' "dev"
|
|
80
|
-
}
|
|
81
|
-
|
|
82
54
|
get_worktree_for_branch() {
|
|
83
55
|
local repo="$1"
|
|
84
56
|
local branch="$2"
|
|
85
57
|
git -C "$repo" worktree list --porcelain | awk -v target="refs/heads/${branch}" '
|
|
86
58
|
$1 == "worktree" { wt = $2 }
|
|
87
|
-
$1 == "branch" && $2 == target {
|
|
59
|
+
$1 == "branch" && $2 == target {
|
|
60
|
+
n = split(wt, parts, "/")
|
|
61
|
+
if (parts[n] !~ /^__source-probe-/) { print wt; exit }
|
|
62
|
+
}
|
|
88
63
|
'
|
|
89
64
|
}
|
|
90
65
|
|