@imdeadpool/guardex 7.1.1 → 8.2.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.
- package/README.md +123 -322
- package/package.json +23 -5
- package/skills/gitguardex/SKILL.md +83 -3
- package/src/agents/finish.js +15 -0
- package/src/ci-init/index.js +0 -1
- package/src/cli/args.js +142 -3
- package/src/cli/commands/branch.js +294 -14
- package/src/cli/commands/claude.js +15 -0
- package/src/cli/commands/doctor.js +26 -0
- package/src/cli/commands/onboard.js +293 -0
- package/src/cli/commands/prompt.js +15 -3
- package/src/cli/commands/review.js +18 -0
- package/src/cli/commands/setup.js +8 -6
- package/src/cli/commands/status.js +12 -0
- package/src/cli/main.js +2 -0
- package/src/cli/shared-locks.js +304 -0
- package/src/context.js +21 -3
- package/src/core/runtime.js +33 -1
- package/src/doctor/index.js +58 -6
- package/src/finish/heartbeat.js +169 -0
- package/src/finish/index.js +165 -18
- package/src/finish/progress.js +316 -0
- package/src/finish/review-gate.js +453 -32
- package/src/github-api.js +59 -0
- package/src/mcp/collect.js +155 -19
- package/src/mcp/server.js +35 -19
- package/src/output/index.js +210 -0
- package/src/pr-checks.js +121 -0
- package/src/pr-review.js +581 -51
- package/src/pr.js +143 -21
- package/src/provider-binary.js +101 -0
- package/src/review-diff.js +116 -0
- package/src/review-fix.js +229 -0
- package/src/sandbox/index.js +11 -5
- package/src/scaffold/index.js +4 -16
- package/src/shared-git-state.js +566 -0
- package/templates/AGENTS.multiagent-safety.md +63 -11
- package/templates/AGENTS.multiagent-safety.min.md +1 -0
- package/templates/codex/skills/gitguardex/SKILL.md +83 -3
- package/templates/githooks/post-checkout +4 -9
- package/templates/githooks/pre-commit +17 -1
- package/templates/github/workflows/README.md +30 -23
- package/templates/scripts/agent-autofinish-watch.sh +280 -0
- package/templates/scripts/agent-branch-finish.sh +354 -23
- package/templates/scripts/agent-branch-start.sh +49 -22
- package/templates/scripts/agent-claude-stop-finish.sh +194 -0
- package/templates/scripts/agent-file-locks.py +198 -29
- package/templates/scripts/agent-preflight.sh +14 -2
- package/templates/scripts/agent-stalled-report.sh +40 -0
- package/templates/scripts/codex-agent.sh +10 -63
- package/templates/github/workflows/cr.yml +0 -46
|
@@ -1,13 +1,93 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: gitguardex
|
|
3
|
-
description: "
|
|
3
|
+
description: "Use for GitGuardex branch/worktree safety, agent or subagent collision avoidance, file ownership, and gated PR finishing. Not for code-quality review."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
Use when
|
|
6
|
+
Use when a GitGuardex-managed repository needs safe edits or multi-agent coordination.
|
|
7
7
|
|
|
8
8
|
`gx status` -> `gx doctor` -> `gx status --strict`
|
|
9
9
|
|
|
10
10
|
Bootstrap: `gx setup`
|
|
11
11
|
Ops: `gx branch start "<task>" "<agent>"`, `gx locks claim --branch "<agent-branch>" <file...>`, `gx branch finish --branch "<agent-branch>" --base <base> --via-pr --wait-for-merge --cleanup`, `gx finish --all`, `gx cleanup`
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
## Keep the default workflow lean
|
|
14
|
+
|
|
15
|
+
- Default to direct T1 work in code and tests. Do not create OpenSpec artifacts from task wording alone.
|
|
16
|
+
- OpenSpec is explicit opt-in: use `--tier T2` for a change workspace or `--tier T3` for change plus plan only when the user requests OpenSpec or the caller explicitly selects T2/T3.
|
|
17
|
+
- Treat the commit, targeted test output, and compact final handoff as the routine task record.
|
|
18
|
+
|
|
19
|
+
## Agent and subagent collaboration
|
|
20
|
+
|
|
21
|
+
Before parallel work, inspect the field with the MCP `list_agents` tool or the CLI fallback:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
gx mcp list-agents --no-prs
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
For every file a writer may touch, check ownership, create an isolated lane, then claim it:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
gx mcp who-owns path/to/file.ts
|
|
31
|
+
gx branch start "<bounded task>" "<agent>"
|
|
32
|
+
gx locks claim --branch "<agent-branch>" path/to/file.ts
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- **One owner per file.** Split writer subagents by non-overlapping files or modules. Read-only scouts may share a surface; writers may not.
|
|
36
|
+
- Give each subagent one bounded objective and a verification command. Do not ask one subagent to explore, redesign, implement, and review the same slice.
|
|
37
|
+
- Re-check `list_agents` before expanding scope. If another lane owns or is already editing a file, coordinate a handoff or choose a different slice.
|
|
38
|
+
- Return a compact handoff: conclusion, `file:line`, changed files, verification result, and blocker or next action. Do not relay full logs.
|
|
39
|
+
- Finish each writing lane through its own PR once. Never run duplicate finish commands for the same branch.
|
|
40
|
+
|
|
41
|
+
## Optional cross-machine Git lock and radar state
|
|
42
|
+
|
|
43
|
+
Local worktree locks remain the default. When agents on multiple machines use the same writable Git remote, opt into shared state per repository:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
gx locks shared-enable --remote origin
|
|
47
|
+
gx locks shared-status
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
After enablement, ordinary `claim`, `allow-delete`, `validate`, `release`, and `status` commands also use atomic remote refs under `refs/gitguardex/locks/*`. `gx mcp list-agents` combines local worktrees with remote `agent/*` branches and shared lock owners; `gx mcp who-owns` resolves the same shared owner data.
|
|
51
|
+
|
|
52
|
+
- Shared mode is explicit and fail-closed: if the remote is unreachable, rejects custom refs, or returns invalid metadata, do not keep a local-only claim or approve a commit.
|
|
53
|
+
- The remote must allow authenticated custom-ref creation, compare-and-swap updates, and deletion. GitGuardex stores validated JSON metadata in commits; it never executes remote metadata, and publishes only a hashed machine identifier.
|
|
54
|
+
- Push the agent branch when full remote-lane visibility matters. A lock still makes an unpushed branch visible as a lock-owning remote lane.
|
|
55
|
+
- Release locks before disabling shared state or abandoning a lane. Cross-machine locks are not automatically reaped because one host cannot safely prove that another host's process is dead.
|
|
56
|
+
- `gx locks shared-disable` stops shared reads and writes for this repository; it does not delete remote refs owned by any machine.
|
|
57
|
+
|
|
58
|
+
## Finish checklist in Codex
|
|
59
|
+
|
|
60
|
+
Before starting a long `gx finish`, `gx ship`, or `gx branch finish`, show this compact Markdown checklist in commentary and update it from the CLI's `[gx:finish]` transition lines:
|
|
61
|
+
|
|
62
|
+
- [ ] Prepare branch
|
|
63
|
+
- [ ] Local preflight
|
|
64
|
+
- [ ] Push and open PR
|
|
65
|
+
- [ ] AI review
|
|
66
|
+
- [ ] Review autofix
|
|
67
|
+
- [ ] CI checks
|
|
68
|
+
- [ ] Merge
|
|
69
|
+
- [ ] Cleanup
|
|
70
|
+
|
|
71
|
+
Render each state-change update as a compact card, not as a raw log dump:
|
|
72
|
+
|
|
73
|
+
### 🚀 GX Finish · `3/8`
|
|
74
|
+
`agent/...` → `main` · 🔄 **AI review** · `00:44`
|
|
75
|
+
|
|
76
|
+
- ✅ Prepare branch
|
|
77
|
+
- ✅ Local preflight
|
|
78
|
+
- ✅ Push and open PR · `#123`
|
|
79
|
+
- 🔄 AI review
|
|
80
|
+
- ⬜ Review autofix
|
|
81
|
+
- ⬜ CI checks
|
|
82
|
+
- ⬜ Merge
|
|
83
|
+
- ⬜ Cleanup
|
|
84
|
+
|
|
85
|
+
Start the command with a short initial yield, then poll the background process every 15-30 seconds. Between polls, post an updated checklist when the active stage changes; during a quiet long stage, name the active stage and elapsed time at most every 30 seconds. Never leave the user with only Codex's generic **Working** or **Waiting for background terminal** label. Do not launch a duplicate finish while the first process is still alive, and do not mark a checkbox complete until a matching `✓` line or equivalent terminal evidence arrives.
|
|
86
|
+
|
|
87
|
+
Treat `🏁 Cleanup` as finished-but-best-effort, not as unconditional success: surface any cleanup warnings beside it instead of converting it to `✅`.
|
|
88
|
+
|
|
89
|
+
Structured phase events are persisted privately under `.omx/state/finish-runs/*.jsonl` (`0700` directory, `0600` files). Prefer that JSONL stream over parsing narrative logs when exact state is needed; the visual output prints the active event-file path.
|
|
90
|
+
|
|
91
|
+
When inspecting or verifying, prefer `rtk` compact wrappers if available (`rtk git status`, `rtk grep`, `rtk test <cmd>`, and noisy gx reads like `rtk gx status` / `rtk gx doctor`). Do not wrap commands whose stdout is parsed by scripts (`--json`, `--porcelain`, exact stdout contracts) or shell-ready output (`gx prompt --exec`).
|
|
92
|
+
|
|
93
|
+
To shrink gx's own large narrative output (e.g. `gx prompt`, `gx prompt --snippet`) before it lands in your context, set `GUARDEX_COMPRESS_CMD="<stdin->stdout filter>"`; gx routes that output through the filter (terse/non-TTY mode, fail-open, JSON skipped). Unset = byte-for-byte unchanged. Confirm it is wired with `gx status` or `gx doctor` — both print a `Token compression` line and flag a configured-but-missing binary (doctor's warning is advisory and never changes its safe/unsafe exit code).
|
package/src/agents/finish.js
CHANGED
|
@@ -85,9 +85,19 @@ function buildFinishEvidence(session, finishArgs, status, result, captured = {})
|
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
// Patching process.stdout.write only catches what THIS process writes. A child
|
|
89
|
+
// spawned on 'inherit' writes straight to fd 1 and sails past the patch, so an
|
|
90
|
+
// asset that streams has to be told to pipe while a capture is in flight —
|
|
91
|
+
// otherwise the merged-PR line never reaches buildFinishEvidence and the
|
|
92
|
+
// session records MERGED with an empty prUrl. Read by src/core/runtime.js and
|
|
93
|
+
// src/finish/index.js when they pick an asset's stdio.
|
|
94
|
+
const CAPTURE_ENV = 'GUARDEX_CAPTURE_ASSET_OUTPUT';
|
|
95
|
+
|
|
88
96
|
function captureProcessOutput(fn) {
|
|
89
97
|
let stdout = '';
|
|
90
98
|
let stderr = '';
|
|
99
|
+
const previousCapture = process.env[CAPTURE_ENV];
|
|
100
|
+
process.env[CAPTURE_ENV] = '1';
|
|
91
101
|
const originalStdoutWrite = process.stdout.write;
|
|
92
102
|
const originalStderrWrite = process.stderr.write;
|
|
93
103
|
process.stdout.write = function captureStdout(chunk, encoding, callback) {
|
|
@@ -107,6 +117,11 @@ function captureProcessOutput(fn) {
|
|
|
107
117
|
} finally {
|
|
108
118
|
process.stdout.write = originalStdoutWrite;
|
|
109
119
|
process.stderr.write = originalStderrWrite;
|
|
120
|
+
if (previousCapture === undefined) {
|
|
121
|
+
delete process.env[CAPTURE_ENV];
|
|
122
|
+
} else {
|
|
123
|
+
process.env[CAPTURE_ENV] = previousCapture;
|
|
124
|
+
}
|
|
110
125
|
}
|
|
111
126
|
}
|
|
112
127
|
|
package/src/ci-init/index.js
CHANGED
|
@@ -197,7 +197,6 @@ function renderCiInitHelp() {
|
|
|
197
197
|
'Files copied (from gitguardex templates/github/workflows/):',
|
|
198
198
|
` - ci.yml PR-time CI with draft-skip + concurrency-cancel.`,
|
|
199
199
|
` - ci-full.yml Weekly cross-runtime matrix + label opt-in.`,
|
|
200
|
-
` - cr.yml AI code review with agent/* + draft skip.`,
|
|
201
200
|
` - README.md Documents the budget posture and customization knobs.`,
|
|
202
201
|
'',
|
|
203
202
|
'The command stages copied files with git add when the target is a git repo;',
|
package/src/cli/args.js
CHANGED
|
@@ -2,6 +2,7 @@ const {
|
|
|
2
2
|
path,
|
|
3
3
|
DEFAULT_SHADOW_CLEANUP_IDLE_MINUTES,
|
|
4
4
|
TARGETED_FORCEABLE_MANAGED_PATHS,
|
|
5
|
+
envFlagIsTruthy,
|
|
5
6
|
} = require('../context');
|
|
6
7
|
const { DEFAULT_NESTED_REPO_MAX_DEPTH } = require('../git');
|
|
7
8
|
|
|
@@ -293,6 +294,9 @@ function parsePrReviewArgs(rawArgs) {
|
|
|
293
294
|
post: false,
|
|
294
295
|
artifact: '',
|
|
295
296
|
timeoutMs: 10 * 60 * 1000,
|
|
297
|
+
// Apply the findings as commits on the current agent branch instead of only
|
|
298
|
+
// reporting them. Off by default: this writes to the working tree.
|
|
299
|
+
fix: false,
|
|
296
300
|
};
|
|
297
301
|
|
|
298
302
|
for (let index = 0; index < parsed.args.length; index += 1) {
|
|
@@ -319,6 +323,14 @@ function parsePrReviewArgs(rawArgs) {
|
|
|
319
323
|
options.post = false;
|
|
320
324
|
continue;
|
|
321
325
|
}
|
|
326
|
+
if (arg === '--fix') {
|
|
327
|
+
options.fix = true;
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (arg === '--no-fix') {
|
|
331
|
+
options.fix = false;
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
322
334
|
if (arg === '--artifact' || arg === '--output') {
|
|
323
335
|
options.artifact = requireValue(parsed.args, index, arg);
|
|
324
336
|
index += 1;
|
|
@@ -1140,6 +1152,14 @@ function parseMergeArgs(rawArgs) {
|
|
|
1140
1152
|
}
|
|
1141
1153
|
|
|
1142
1154
|
function parseFinishArgs(rawArgs, defaults = {}) {
|
|
1155
|
+
// `GUARDEX_AUTO_SHIP=1` (opt-in) makes a bare `gx finish` / `gx branch finish`
|
|
1156
|
+
// behave like `gx ship`: open a PR from the worktree, wait for merge, clean up,
|
|
1157
|
+
// and enforce the merge gate (clean AI review + green CI) before merging to base.
|
|
1158
|
+
// via-pr / wait-for-merge / cleanup are already the defaults, so the only delta
|
|
1159
|
+
// is flipping the gate-review default on. Explicit flags (e.g. --no-gate-review)
|
|
1160
|
+
// still override below.
|
|
1161
|
+
const autoShip = envFlagIsTruthy(process.env.GUARDEX_AUTO_SHIP);
|
|
1162
|
+
const fastMode = rawArgs.includes('--fast');
|
|
1143
1163
|
const options = {
|
|
1144
1164
|
target: process.cwd(),
|
|
1145
1165
|
base: '',
|
|
@@ -1154,11 +1174,34 @@ function parseFinishArgs(rawArgs, defaults = {}) {
|
|
|
1154
1174
|
advanceSubmodules: false,
|
|
1155
1175
|
failFast: false,
|
|
1156
1176
|
commitMessage: '',
|
|
1157
|
-
mergeMode: defaults.mergeMode || 'pr',
|
|
1158
|
-
|
|
1159
|
-
|
|
1177
|
+
mergeMode: fastMode ? 'pr' : (defaults.mergeMode || 'pr'),
|
|
1178
|
+
fastMode,
|
|
1179
|
+
agentQuiet: false,
|
|
1180
|
+
skipPreflight: fastMode,
|
|
1181
|
+
// Precedence: explicit flag (set in the loop below) > defaults (caller) >
|
|
1182
|
+
// GUARDEX_AUTO_SHIP env > hardcoded off. Note `defaults.gateReview === false`
|
|
1183
|
+
// (not undefined) also wins over the env toggle, since `??` only falls
|
|
1184
|
+
// through on null/undefined.
|
|
1185
|
+
gateReview: fastMode ? false : (defaults.gateReview ?? autoShip),
|
|
1160
1186
|
reviewProvider: defaults.reviewProvider || 'codex',
|
|
1187
|
+
// Empty = the provider's own default model.
|
|
1188
|
+
reviewModel: defaults.reviewModel || '',
|
|
1189
|
+
reviewTimeoutMs: defaults.reviewTimeoutMs,
|
|
1190
|
+
// Keep the PR draft until the review passes so GitHub cannot merge it while
|
|
1191
|
+
// the verdict is pending. `--no-gate-serial-ci` explicitly opts into overlap.
|
|
1192
|
+
gateSerialCi: defaults.gateSerialCi ?? true,
|
|
1161
1193
|
allowNoChecks: false,
|
|
1194
|
+
// Gate on "no NEW failing checks vs the base branch" instead of absolute
|
|
1195
|
+
// green, so a repo whose base is already red can still ship unattended.
|
|
1196
|
+
gateBaseline: false,
|
|
1197
|
+
// Let the gate repair its own blocking findings instead of only reporting
|
|
1198
|
+
// them. Off by default: this writes commits to the branch.
|
|
1199
|
+
gateAutofix: false,
|
|
1200
|
+
gateAutofixRounds: 1,
|
|
1201
|
+
// After a bulk `--all` finish, sweep merged-but-stranded worktree dirs whose
|
|
1202
|
+
// branch was merged out-of-band and never reaped (the post-merge "retained"
|
|
1203
|
+
// gap). Only fires for `--all`; opt out with --no-sweep-orphans.
|
|
1204
|
+
sweepOrphans: defaults.sweepOrphans ?? true,
|
|
1162
1205
|
};
|
|
1163
1206
|
|
|
1164
1207
|
for (let index = 0; index < rawArgs.length; index += 1) {
|
|
@@ -1203,10 +1246,25 @@ function parseFinishArgs(rawArgs, defaults = {}) {
|
|
|
1203
1246
|
options.all = true;
|
|
1204
1247
|
continue;
|
|
1205
1248
|
}
|
|
1249
|
+
if (arg === '--no-sweep-orphans') {
|
|
1250
|
+
options.sweepOrphans = false;
|
|
1251
|
+
continue;
|
|
1252
|
+
}
|
|
1253
|
+
if (arg === '--sweep-orphans') {
|
|
1254
|
+
options.sweepOrphans = true;
|
|
1255
|
+
continue;
|
|
1256
|
+
}
|
|
1206
1257
|
if (arg === '--dry-run') {
|
|
1207
1258
|
options.dryRun = true;
|
|
1208
1259
|
continue;
|
|
1209
1260
|
}
|
|
1261
|
+
if (arg === '--fast') {
|
|
1262
|
+
continue;
|
|
1263
|
+
}
|
|
1264
|
+
if (arg === '--agent-quiet') {
|
|
1265
|
+
options.agentQuiet = true;
|
|
1266
|
+
continue;
|
|
1267
|
+
}
|
|
1210
1268
|
if (arg === '--wait-for-merge') {
|
|
1211
1269
|
options.waitForMerge = true;
|
|
1212
1270
|
continue;
|
|
@@ -1287,6 +1345,33 @@ function parseFinishArgs(rawArgs, defaults = {}) {
|
|
|
1287
1345
|
options.allowNoChecks = true;
|
|
1288
1346
|
continue;
|
|
1289
1347
|
}
|
|
1348
|
+
if (arg === '--gate-baseline') {
|
|
1349
|
+
options.gateBaseline = true;
|
|
1350
|
+
continue;
|
|
1351
|
+
}
|
|
1352
|
+
if (arg === '--no-gate-baseline') {
|
|
1353
|
+
options.gateBaseline = false;
|
|
1354
|
+
continue;
|
|
1355
|
+
}
|
|
1356
|
+
if (arg === '--gate-autofix') {
|
|
1357
|
+
options.gateAutofix = true;
|
|
1358
|
+
continue;
|
|
1359
|
+
}
|
|
1360
|
+
if (arg === '--no-gate-autofix') {
|
|
1361
|
+
options.gateAutofix = false;
|
|
1362
|
+
continue;
|
|
1363
|
+
}
|
|
1364
|
+
if (arg === '--gate-autofix-rounds') {
|
|
1365
|
+
const raw = rawArgs[index + 1];
|
|
1366
|
+
const rounds = Number.parseInt(String(raw ?? ''), 10);
|
|
1367
|
+
if (!Number.isInteger(rounds) || rounds < 1 || rounds > 5) {
|
|
1368
|
+
throw new Error('--gate-autofix-rounds requires an integer between 1 and 5');
|
|
1369
|
+
}
|
|
1370
|
+
options.gateAutofixRounds = rounds;
|
|
1371
|
+
options.gateAutofix = true;
|
|
1372
|
+
index += 1;
|
|
1373
|
+
continue;
|
|
1374
|
+
}
|
|
1290
1375
|
if (arg === '--review-provider') {
|
|
1291
1376
|
const next = rawArgs[index + 1];
|
|
1292
1377
|
if (!next || !['codex', 'claude'].includes(next)) {
|
|
@@ -1296,9 +1381,63 @@ function parseFinishArgs(rawArgs, defaults = {}) {
|
|
|
1296
1381
|
index += 1;
|
|
1297
1382
|
continue;
|
|
1298
1383
|
}
|
|
1384
|
+
if (arg === '--review-model') {
|
|
1385
|
+
const next = String(rawArgs[index + 1] ?? '').trim();
|
|
1386
|
+
if (!next || next.startsWith('-')) {
|
|
1387
|
+
throw new Error('--review-model requires a model name (e.g. sonnet)');
|
|
1388
|
+
}
|
|
1389
|
+
options.reviewModel = next;
|
|
1390
|
+
index += 1;
|
|
1391
|
+
continue;
|
|
1392
|
+
}
|
|
1393
|
+
if (arg === '--review-timeout-ms') {
|
|
1394
|
+
const raw = rawArgs[index + 1];
|
|
1395
|
+
const timeoutMs = Number.parseInt(String(raw ?? ''), 10);
|
|
1396
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs <= 0) {
|
|
1397
|
+
throw new Error('--review-timeout-ms requires a positive integer');
|
|
1398
|
+
}
|
|
1399
|
+
options.reviewTimeoutMs = timeoutMs;
|
|
1400
|
+
index += 1;
|
|
1401
|
+
continue;
|
|
1402
|
+
}
|
|
1403
|
+
if (arg === '--gate-serial-ci') {
|
|
1404
|
+
options.gateSerialCi = true;
|
|
1405
|
+
continue;
|
|
1406
|
+
}
|
|
1407
|
+
if (arg === '--no-gate-serial-ci') {
|
|
1408
|
+
options.gateSerialCi = false;
|
|
1409
|
+
continue;
|
|
1410
|
+
}
|
|
1299
1411
|
throw new Error(`Unknown option: ${arg}`);
|
|
1300
1412
|
}
|
|
1301
1413
|
|
|
1414
|
+
if (fastMode) {
|
|
1415
|
+
const incompatibleFlag = rawArgs.find((arg) => [
|
|
1416
|
+
'--gate-review',
|
|
1417
|
+
'--gate-autofix',
|
|
1418
|
+
'--gate-autofix-rounds',
|
|
1419
|
+
'--preflight',
|
|
1420
|
+
'--direct-only',
|
|
1421
|
+
].includes(arg));
|
|
1422
|
+
const modeIndex = rawArgs.indexOf('--mode');
|
|
1423
|
+
const modeValue = modeIndex >= 0 ? rawArgs[modeIndex + 1] : '';
|
|
1424
|
+
const incompatibleMode = modeIndex >= 0 && modeValue !== 'pr'
|
|
1425
|
+
? `--mode ${modeValue || '<missing>'}`
|
|
1426
|
+
: '';
|
|
1427
|
+
const conflict = incompatibleFlag || incompatibleMode;
|
|
1428
|
+
if (conflict) {
|
|
1429
|
+
throw new Error(`--fast cannot be combined with ${conflict}`);
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
// Fast mode is an explicit per-run profile: PR/squash path, no local
|
|
1433
|
+
// preflight, and no AI review/autofix. GitHub branch protection and its
|
|
1434
|
+
// required checks remain authoritative at merge time.
|
|
1435
|
+
options.mergeMode = 'pr';
|
|
1436
|
+
options.skipPreflight = true;
|
|
1437
|
+
options.gateReview = false;
|
|
1438
|
+
options.gateAutofix = false;
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1302
1441
|
if (options.branch && !options.branch.startsWith('agent/')) {
|
|
1303
1442
|
throw new Error(`--branch must reference an agent/* branch (received: ${options.branch})`);
|
|
1304
1443
|
}
|
|
@@ -1,14 +1,201 @@
|
|
|
1
1
|
// `gx branch`, `gx pivot`, `gx ship`, `gx locks`, `gx worktree` — branch
|
|
2
2
|
// workflow surface. Pure code-motion from src/cli/main.js.
|
|
3
|
-
const {
|
|
4
|
-
|
|
3
|
+
const {
|
|
4
|
+
TOOL_NAME,
|
|
5
|
+
SHORT_TOOL_NAME,
|
|
6
|
+
path,
|
|
7
|
+
fs,
|
|
8
|
+
} = require('../../context');
|
|
9
|
+
const {
|
|
10
|
+
resolveRepoRoot,
|
|
11
|
+
resolveFinishBaseBranch,
|
|
12
|
+
currentBranchName,
|
|
13
|
+
listAgentWorktrees,
|
|
14
|
+
} = require('../../git');
|
|
5
15
|
const {
|
|
6
16
|
run,
|
|
7
17
|
extractTargetedArgs,
|
|
8
18
|
runPackageAsset,
|
|
9
19
|
invokePackageAsset,
|
|
10
20
|
} = require('../../core/runtime');
|
|
21
|
+
const { runReviewGate } = require('../../finish/review-gate');
|
|
22
|
+
const { createFinishProgress, summarizeFinishRun } = require('../../finish/progress');
|
|
23
|
+
const { autoCommitWorktreeForFinish } = require('../../finish');
|
|
11
24
|
const { finish, merge } = require('./finish');
|
|
25
|
+
const { locks } = require('../shared-locks');
|
|
26
|
+
|
|
27
|
+
const REVIEW_PROVIDERS = ['codex', 'claude'];
|
|
28
|
+
|
|
29
|
+
// Review-gate and auto-commit options are gx-level flags.
|
|
30
|
+
// agent-branch-finish.sh does not parse them (it exits 1 on the unknown
|
|
31
|
+
// argument), and its --via-pr path merges the moment the PR opens, so the shell
|
|
32
|
+
// cannot enforce the gate itself. Pull the flags out of the script's argv and
|
|
33
|
+
// honor them here.
|
|
34
|
+
function splitGateReviewFlags(args) {
|
|
35
|
+
const scriptArgs = [];
|
|
36
|
+
let fastMode = false;
|
|
37
|
+
let gateReview = false;
|
|
38
|
+
let reviewProvider;
|
|
39
|
+
let gateAutofix = false;
|
|
40
|
+
let gateAutofixRounds = 1;
|
|
41
|
+
let gateBaseline = false;
|
|
42
|
+
let gateSerialCi = true;
|
|
43
|
+
let reviewModel;
|
|
44
|
+
let reviewTimeoutMs;
|
|
45
|
+
let noAutoCommit = false;
|
|
46
|
+
let commitMessage = '';
|
|
47
|
+
let agentQuiet = false;
|
|
48
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
49
|
+
const arg = args[index];
|
|
50
|
+
if (arg === '--fast') {
|
|
51
|
+
fastMode = true;
|
|
52
|
+
} else if (arg === '--agent-quiet') {
|
|
53
|
+
agentQuiet = true;
|
|
54
|
+
} else if (arg === '--no-auto-commit') {
|
|
55
|
+
noAutoCommit = true;
|
|
56
|
+
} else if (arg === '--commit-message') {
|
|
57
|
+
commitMessage = String(args[index + 1] ?? '').trim();
|
|
58
|
+
if (!commitMessage || commitMessage.startsWith('-')) {
|
|
59
|
+
throw new Error('--commit-message requires a value');
|
|
60
|
+
}
|
|
61
|
+
index += 1;
|
|
62
|
+
} else if (arg === '--gate-review') {
|
|
63
|
+
gateReview = true;
|
|
64
|
+
} else if (arg === '--no-gate-review' || arg === '--skip-review-gate') {
|
|
65
|
+
gateReview = false;
|
|
66
|
+
} else if (arg === '--gate-serial-ci') {
|
|
67
|
+
gateSerialCi = true;
|
|
68
|
+
} else if (arg === '--no-gate-serial-ci') {
|
|
69
|
+
gateSerialCi = false;
|
|
70
|
+
} else if (arg === '--review-model') {
|
|
71
|
+
// Consume the value for the same reason as --review-provider: a bare
|
|
72
|
+
// model name left behind becomes a positional the shell script rejects.
|
|
73
|
+
reviewModel = args[index + 1] ?? '';
|
|
74
|
+
index += 1;
|
|
75
|
+
} else if (arg.startsWith('--review-model=')) {
|
|
76
|
+
reviewModel = arg.slice('--review-model='.length);
|
|
77
|
+
} else if (arg === '--review-timeout-ms' || arg.startsWith('--review-timeout-ms=')) {
|
|
78
|
+
// The shell script does not know this gx-level gate flag either.
|
|
79
|
+
reviewTimeoutMs = arg.includes('=') ? arg.slice('--review-timeout-ms='.length) : (args[index + 1] ?? '');
|
|
80
|
+
if (!arg.includes('=')) index += 1;
|
|
81
|
+
} else if (arg === '--gate-baseline') {
|
|
82
|
+
gateBaseline = true;
|
|
83
|
+
} else if (arg === '--no-gate-baseline') {
|
|
84
|
+
gateBaseline = false;
|
|
85
|
+
} else if (arg === '--gate-autofix') {
|
|
86
|
+
gateAutofix = true;
|
|
87
|
+
} else if (arg === '--no-gate-autofix') {
|
|
88
|
+
gateAutofix = false;
|
|
89
|
+
} else if (arg === '--gate-autofix-rounds' || arg.startsWith('--gate-autofix-rounds=')) {
|
|
90
|
+
// Consume the value too, for the same reason as --review-provider: a bare
|
|
91
|
+
// number left behind becomes a positional the shell script rejects.
|
|
92
|
+
let raw;
|
|
93
|
+
if (arg.includes('=')) {
|
|
94
|
+
raw = arg.slice('--gate-autofix-rounds='.length);
|
|
95
|
+
} else {
|
|
96
|
+
raw = args[index + 1];
|
|
97
|
+
index += 1;
|
|
98
|
+
}
|
|
99
|
+
const rounds = Number.parseInt(String(raw ?? ''), 10);
|
|
100
|
+
if (!Number.isInteger(rounds) || rounds < 1 || rounds > 5) {
|
|
101
|
+
throw new Error('--gate-autofix-rounds requires an integer between 1 and 5');
|
|
102
|
+
}
|
|
103
|
+
gateAutofixRounds = rounds;
|
|
104
|
+
gateAutofix = true;
|
|
105
|
+
} else if (arg === '--review-provider') {
|
|
106
|
+
// Consume the value too — leaving it behind would hand the script a bare
|
|
107
|
+
// "claude" positional and it would exit 1. A missing value becomes "" so
|
|
108
|
+
// the check below rejects it, matching args.js: falling back to the
|
|
109
|
+
// default here would gate with codex a caller who asked for claude.
|
|
110
|
+
reviewProvider = args[index + 1] ?? '';
|
|
111
|
+
index += 1;
|
|
112
|
+
} else if (arg.startsWith('--review-provider=')) {
|
|
113
|
+
reviewProvider = arg.slice('--review-provider='.length);
|
|
114
|
+
} else {
|
|
115
|
+
scriptArgs.push(arg);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (fastMode) {
|
|
120
|
+
const incompatibleFlag = args.find((arg) => [
|
|
121
|
+
'--gate-review',
|
|
122
|
+
'--gate-autofix',
|
|
123
|
+
'--gate-autofix-rounds',
|
|
124
|
+
'--preflight',
|
|
125
|
+
'--direct-only',
|
|
126
|
+
].includes(arg));
|
|
127
|
+
const modeIndex = args.indexOf('--mode');
|
|
128
|
+
const modeValue = modeIndex >= 0 ? args[modeIndex + 1] : '';
|
|
129
|
+
const incompatibleMode = modeIndex >= 0 && modeValue !== 'pr'
|
|
130
|
+
? `--mode ${modeValue || '<missing>'}`
|
|
131
|
+
: '';
|
|
132
|
+
const conflict = incompatibleFlag || incompatibleMode;
|
|
133
|
+
if (conflict) {
|
|
134
|
+
throw new Error(`--fast cannot be combined with ${conflict}`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
gateReview = false;
|
|
138
|
+
gateAutofix = false;
|
|
139
|
+
if (!scriptArgs.includes('--via-pr')) scriptArgs.push('--via-pr');
|
|
140
|
+
if (!scriptArgs.includes('--no-preflight')) scriptArgs.push('--no-preflight');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (reviewProvider !== undefined) {
|
|
144
|
+
reviewProvider = String(reviewProvider).trim().toLowerCase();
|
|
145
|
+
// Fail closed on a typo rather than silently falling back to the default: a
|
|
146
|
+
// caller who named a provider must not quietly get a different one.
|
|
147
|
+
if (!REVIEW_PROVIDERS.includes(reviewProvider)) {
|
|
148
|
+
throw new Error(`--review-provider requires a value of ${REVIEW_PROVIDERS.join('|')}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (reviewModel !== undefined) {
|
|
153
|
+
reviewModel = String(reviewModel).trim();
|
|
154
|
+
// Same fail-closed reasoning as --review-provider: a caller who named a
|
|
155
|
+
// model must not silently get the provider's default one instead.
|
|
156
|
+
if (!reviewModel || reviewModel.startsWith('-')) {
|
|
157
|
+
throw new Error('--review-model requires a model name (e.g. sonnet)');
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (reviewTimeoutMs !== undefined) {
|
|
162
|
+
reviewTimeoutMs = Number.parseInt(String(reviewTimeoutMs ?? ''), 10);
|
|
163
|
+
if (!Number.isInteger(reviewTimeoutMs) || reviewTimeoutMs <= 0) {
|
|
164
|
+
throw new Error('--review-timeout-ms requires a positive integer');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
gateReview,
|
|
170
|
+
reviewProvider,
|
|
171
|
+
reviewModel,
|
|
172
|
+
reviewTimeoutMs,
|
|
173
|
+
gateAutofix,
|
|
174
|
+
gateAutofixRounds,
|
|
175
|
+
gateBaseline,
|
|
176
|
+
gateSerialCi,
|
|
177
|
+
noAutoCommit,
|
|
178
|
+
commitMessage,
|
|
179
|
+
agentQuiet,
|
|
180
|
+
scriptArgs,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Read `--flag value` or `--flag=value` out of an argv array.
|
|
185
|
+
function readFlagValue(args, flag) {
|
|
186
|
+
const index = args.indexOf(flag);
|
|
187
|
+
if (index >= 0 && index + 1 < args.length) return args[index + 1];
|
|
188
|
+
const inline = args.find((arg) => arg.startsWith(`${flag}=`));
|
|
189
|
+
return inline ? inline.slice(flag.length + 1) : undefined;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function isLinkedAgentWorktree(worktreePath) {
|
|
193
|
+
try {
|
|
194
|
+
return fs.statSync(path.join(worktreePath, '.git')).isFile();
|
|
195
|
+
} catch {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
12
199
|
|
|
13
200
|
function branch(rawArgs) {
|
|
14
201
|
const activeCwd = process.cwd();
|
|
@@ -20,10 +207,107 @@ function branch(rawArgs) {
|
|
|
20
207
|
}
|
|
21
208
|
if (subcommand === 'finish') {
|
|
22
209
|
const { target, passthrough } = extractTargetedArgs(rest);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
210
|
+
const repoRoot = resolveRepoRoot(target);
|
|
211
|
+
const {
|
|
212
|
+
gateReview,
|
|
213
|
+
reviewProvider,
|
|
214
|
+
reviewModel,
|
|
215
|
+
reviewTimeoutMs,
|
|
216
|
+
gateAutofix,
|
|
217
|
+
gateAutofixRounds,
|
|
218
|
+
gateBaseline,
|
|
219
|
+
gateSerialCi,
|
|
220
|
+
noAutoCommit,
|
|
221
|
+
commitMessage,
|
|
222
|
+
agentQuiet,
|
|
223
|
+
scriptArgs,
|
|
224
|
+
} = splitGateReviewFlags(passthrough);
|
|
225
|
+
const finishBranch = readFlagValue(scriptArgs, '--branch') || currentBranchName(repoRoot);
|
|
226
|
+
const finishBase = resolveFinishBaseBranch(repoRoot, finishBranch, readFlagValue(scriptArgs, '--base'));
|
|
227
|
+
const finishWorktree = listAgentWorktrees(repoRoot)
|
|
228
|
+
.find((entry) => entry.branch === finishBranch)?.worktreePath || '';
|
|
229
|
+
// Keep the legacy behavior for agent branches checked out directly in the
|
|
230
|
+
// primary repository. Guardex-managed lanes are linked worktrees; limiting
|
|
231
|
+
// auto-commit to those avoids scooping primary-checkout runtime artifacts
|
|
232
|
+
// into an otherwise already-committed branch.
|
|
233
|
+
const autoCommitWorktree = finishWorktree && isLinkedAgentWorktree(finishWorktree)
|
|
234
|
+
? finishWorktree
|
|
235
|
+
: '';
|
|
236
|
+
const progress = createFinishProgress({
|
|
237
|
+
repoRoot,
|
|
238
|
+
branch: finishBranch,
|
|
239
|
+
baseBranch: finishBase,
|
|
240
|
+
quiet: agentQuiet,
|
|
26
241
|
});
|
|
242
|
+
progress.start(
|
|
243
|
+
'prepare',
|
|
244
|
+
autoCommitWorktree ? 'checking worktree and pending changes' : 'resolving branch and finish policy',
|
|
245
|
+
);
|
|
246
|
+
const commitState = autoCommitWorktree
|
|
247
|
+
? autoCommitWorktreeForFinish(repoRoot, autoCommitWorktree, finishBranch, { noAutoCommit, commitMessage })
|
|
248
|
+
: { changed: false, committed: false };
|
|
249
|
+
if (commitState.committed && !agentQuiet) {
|
|
250
|
+
console.log(`[${TOOL_NAME}] Auto-committed '${finishBranch}' before finish.`);
|
|
251
|
+
}
|
|
252
|
+
progress.complete(
|
|
253
|
+
'prepare',
|
|
254
|
+
commitState.committed ? 'pending changes auto-committed' : 'branch and base resolved',
|
|
255
|
+
);
|
|
256
|
+
// Fail-closed: runReviewGate throws on a dirty review, red CI, or a PR
|
|
257
|
+
// GitHub will not merge. Throwing here means the script never runs, so
|
|
258
|
+
// the merge never happens.
|
|
259
|
+
if (gateReview) {
|
|
260
|
+
runReviewGate({
|
|
261
|
+
repoRoot,
|
|
262
|
+
branch: finishBranch,
|
|
263
|
+
// Must match how the shell resolves --base when it is omitted, which
|
|
264
|
+
// honors branch.<name>.guardexBase. Resolving differently would gate one
|
|
265
|
+
// base and merge into another.
|
|
266
|
+
baseBranch: finishBase,
|
|
267
|
+
// review-gate.js falls back to its own default when this is undefined,
|
|
268
|
+
// which keeps a bare --gate-review behaving exactly as before.
|
|
269
|
+
options: {
|
|
270
|
+
reviewProvider,
|
|
271
|
+
reviewModel,
|
|
272
|
+
reviewTimeoutMs,
|
|
273
|
+
gateAutofix,
|
|
274
|
+
gateAutofixRounds,
|
|
275
|
+
gateBaseline,
|
|
276
|
+
gateSerialCi,
|
|
277
|
+
},
|
|
278
|
+
progress,
|
|
279
|
+
});
|
|
280
|
+
} else {
|
|
281
|
+
progress.skip('review', 'review gate disabled');
|
|
282
|
+
progress.skip('autofix', 'review gate disabled');
|
|
283
|
+
progress.skip('ci', 'review gate disabled; repository policy controls merge readiness');
|
|
284
|
+
}
|
|
285
|
+
const assetOptions = {
|
|
286
|
+
cwd: repoRoot,
|
|
287
|
+
env: {
|
|
288
|
+
GUARDEX_FINISH_ACTIVE_CWD: activeCwd,
|
|
289
|
+
GUARDEX_FINISH_CHECKLIST: '1',
|
|
290
|
+
GUARDEX_FINISH_GATE_DONE: gateReview ? '1' : '0',
|
|
291
|
+
...progress.eventEnv,
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
if (agentQuiet) {
|
|
295
|
+
const result = runPackageAsset('branchFinish', scriptArgs, { ...assetOptions, stdio: 'pipe' });
|
|
296
|
+
const summary = summarizeFinishRun({
|
|
297
|
+
eventFile: progress.eventEnv.GUARDEX_FINISH_EVENT_FILE,
|
|
298
|
+
branch: finishBranch,
|
|
299
|
+
baseBranch: finishBase,
|
|
300
|
+
stdout: result.stdout,
|
|
301
|
+
stderr: result.stderr,
|
|
302
|
+
status: result.status,
|
|
303
|
+
});
|
|
304
|
+
process.stdout.write(`[gx:finish] ${JSON.stringify(summary)}\n`);
|
|
305
|
+
if (result.status !== 0) {
|
|
306
|
+
throw new Error(`branchFinish command failed with status ${result.status}`);
|
|
307
|
+
}
|
|
308
|
+
} else {
|
|
309
|
+
invokePackageAsset('branchFinish', scriptArgs, assetOptions);
|
|
310
|
+
}
|
|
27
311
|
return;
|
|
28
312
|
}
|
|
29
313
|
if (subcommand === 'merge') return merge(rest);
|
|
@@ -90,20 +374,16 @@ function ship(rawArgs) {
|
|
|
90
374
|
ensureFlag('--cleanup');
|
|
91
375
|
// `gx ship` enforces the merge gate (clean review + green CI) by default;
|
|
92
376
|
// an explicit --no-gate-review / --skip-review-gate opts back out.
|
|
93
|
-
if (
|
|
377
|
+
if (
|
|
378
|
+
!args.includes('--fast')
|
|
379
|
+
&& !args.includes('--no-gate-review')
|
|
380
|
+
&& !args.includes('--skip-review-gate')
|
|
381
|
+
) {
|
|
94
382
|
ensureFlag('--gate-review');
|
|
95
383
|
}
|
|
96
384
|
return finish(args);
|
|
97
385
|
}
|
|
98
386
|
|
|
99
|
-
function locks(rawArgs) {
|
|
100
|
-
const { target, passthrough } = extractTargetedArgs(rawArgs);
|
|
101
|
-
const result = runPackageAsset('lockTool', passthrough, { cwd: resolveRepoRoot(target) });
|
|
102
|
-
if (result.stdout) process.stdout.write(result.stdout);
|
|
103
|
-
if (result.stderr) process.stderr.write(result.stderr);
|
|
104
|
-
process.exitCode = result.status;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
387
|
function worktree(rawArgs) {
|
|
108
388
|
const activeCwd = process.cwd();
|
|
109
389
|
const [subcommand, ...rest] = rawArgs;
|