@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
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
const { spawn: spawnProcess } = require('node:child_process');
|
|
2
|
+
const fs = require('node:fs');
|
|
3
|
+
|
|
4
|
+
const FRAMES = ['◐', '◓', '◑', '◒'];
|
|
5
|
+
const DEFAULT_INTERVAL_MS = 15_000;
|
|
6
|
+
|
|
7
|
+
function formatElapsed(elapsedMs) {
|
|
8
|
+
const numericElapsed = Number(elapsedMs);
|
|
9
|
+
const totalSeconds = Number.isFinite(numericElapsed)
|
|
10
|
+
? Math.max(0, Math.floor(numericElapsed / 1_000))
|
|
11
|
+
: 0;
|
|
12
|
+
const seconds = totalSeconds % 60;
|
|
13
|
+
const totalMinutes = Math.floor(totalSeconds / 60);
|
|
14
|
+
const minutes = totalMinutes % 60;
|
|
15
|
+
const hours = Math.floor(totalMinutes / 60);
|
|
16
|
+
const twoDigits = (value) => String(value).padStart(2, '0');
|
|
17
|
+
return hours > 0
|
|
18
|
+
? `${hours}:${twoDigits(minutes)}:${twoDigits(seconds)}`
|
|
19
|
+
: `${twoDigits(minutes)}:${twoDigits(seconds)}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function renderHeartbeat({ index, total, label, detail, elapsedMs, frame = 0 }) {
|
|
23
|
+
const symbol = FRAMES[Math.abs(Number(frame) || 0) % FRAMES.length];
|
|
24
|
+
const normalizedDetail = String(detail || '').trim();
|
|
25
|
+
return `[gx:finish] ├─ ${symbol} ${index}/${total} ${label}`
|
|
26
|
+
+ `${normalizedDetail ? ` · ${normalizedDetail}` : ''}`
|
|
27
|
+
+ ` · ⏱ ${formatElapsed(elapsedMs)}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function processIsAlive(pid) {
|
|
31
|
+
try {
|
|
32
|
+
process.kill(pid, 0);
|
|
33
|
+
return true;
|
|
34
|
+
} catch (_error) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function normalizePositiveInteger(value, fallback) {
|
|
40
|
+
const parsed = Number(value);
|
|
41
|
+
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : fallback;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function runWorker(rawConfig) {
|
|
45
|
+
let config;
|
|
46
|
+
try {
|
|
47
|
+
config = JSON.parse(rawConfig);
|
|
48
|
+
} catch (_error) {
|
|
49
|
+
process.exitCode = 1;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const parentPid = normalizePositiveInteger(config.parentPid, 0);
|
|
54
|
+
const intervalMs = normalizePositiveInteger(config.intervalMs, DEFAULT_INTERVAL_MS);
|
|
55
|
+
const startAt = Number.isFinite(Number(config.startAt)) ? Number(config.startAt) : Date.now();
|
|
56
|
+
if (parentPid === 0) {
|
|
57
|
+
process.exitCode = 1;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let lastSlot = 0;
|
|
62
|
+
const timer = setInterval(() => {
|
|
63
|
+
if (!processIsAlive(parentPid)) {
|
|
64
|
+
clearInterval(timer);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const elapsedMs = Math.max(0, Date.now() - startAt);
|
|
68
|
+
const slot = Math.floor(elapsedMs / intervalMs);
|
|
69
|
+
if (slot <= lastSlot) return;
|
|
70
|
+
lastSlot = slot;
|
|
71
|
+
process.stderr.write(`${renderHeartbeat({ ...config, elapsedMs, frame: slot - 1 })}\n`);
|
|
72
|
+
if (config.eventFd === 3 && config.event) {
|
|
73
|
+
try {
|
|
74
|
+
fs.writeSync(config.eventFd, `${JSON.stringify({
|
|
75
|
+
...config.event,
|
|
76
|
+
timestamp: new Date().toISOString(),
|
|
77
|
+
kind: 'heartbeat',
|
|
78
|
+
stage: config.stage,
|
|
79
|
+
state: 'running',
|
|
80
|
+
index: config.index,
|
|
81
|
+
total: config.total,
|
|
82
|
+
label: config.label,
|
|
83
|
+
detail: config.detail,
|
|
84
|
+
elapsedMs,
|
|
85
|
+
})}\n`);
|
|
86
|
+
} catch (_error) {
|
|
87
|
+
// Structured progress is best-effort, just like terminal progress.
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}, Math.min(intervalMs, 1_000));
|
|
91
|
+
|
|
92
|
+
const stop = () => {
|
|
93
|
+
clearInterval(timer);
|
|
94
|
+
process.exit(0);
|
|
95
|
+
};
|
|
96
|
+
process.once('SIGINT', stop);
|
|
97
|
+
process.once('SIGTERM', stop);
|
|
98
|
+
process.once('SIGHUP', stop);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function startHeartbeat({
|
|
102
|
+
stage,
|
|
103
|
+
index,
|
|
104
|
+
total,
|
|
105
|
+
label,
|
|
106
|
+
detail = '',
|
|
107
|
+
intervalMs = DEFAULT_INTERVAL_MS,
|
|
108
|
+
startAt = Date.now(),
|
|
109
|
+
eventDescriptor,
|
|
110
|
+
event,
|
|
111
|
+
spawn = spawnProcess,
|
|
112
|
+
} = {}) {
|
|
113
|
+
const config = {
|
|
114
|
+
parentPid: process.pid,
|
|
115
|
+
stage: String(stage || ''),
|
|
116
|
+
index: normalizePositiveInteger(index, 0),
|
|
117
|
+
total: normalizePositiveInteger(total, 0),
|
|
118
|
+
label: String(label || ''),
|
|
119
|
+
detail: String(detail || ''),
|
|
120
|
+
intervalMs: normalizePositiveInteger(intervalMs, DEFAULT_INTERVAL_MS),
|
|
121
|
+
startAt: Number.isFinite(Number(startAt)) ? Number(startAt) : Date.now(),
|
|
122
|
+
};
|
|
123
|
+
const stdio = ['ignore', 'ignore', 'inherit'];
|
|
124
|
+
if (Number.isInteger(eventDescriptor) && eventDescriptor >= 0 && event) {
|
|
125
|
+
config.eventFd = 3;
|
|
126
|
+
config.event = {
|
|
127
|
+
schemaVersion: Number(event.schemaVersion) || 1,
|
|
128
|
+
runId: String(event.runId || ''),
|
|
129
|
+
branch: String(event.branch || ''),
|
|
130
|
+
baseBranch: String(event.baseBranch || ''),
|
|
131
|
+
};
|
|
132
|
+
stdio.push(eventDescriptor);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let child;
|
|
136
|
+
try {
|
|
137
|
+
child = spawn(process.execPath, [__filename, '--worker', JSON.stringify(config)], {
|
|
138
|
+
stdio,
|
|
139
|
+
});
|
|
140
|
+
child.on('error', () => {});
|
|
141
|
+
child.unref();
|
|
142
|
+
} catch (_error) {
|
|
143
|
+
return () => {};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let stopped = false;
|
|
147
|
+
return () => {
|
|
148
|
+
if (stopped) return;
|
|
149
|
+
stopped = true;
|
|
150
|
+
if (child.exitCode === null && !child.killed) {
|
|
151
|
+
try {
|
|
152
|
+
child.kill('SIGTERM');
|
|
153
|
+
} catch (_error) {
|
|
154
|
+
// Progress is observability only; never fail finish while stopping it.
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (require.main === module && process.argv[2] === '--worker') {
|
|
161
|
+
runWorker(process.argv[3] || '');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
module.exports = {
|
|
165
|
+
formatElapsed,
|
|
166
|
+
renderHeartbeat,
|
|
167
|
+
runWorker,
|
|
168
|
+
startHeartbeat,
|
|
169
|
+
};
|
package/src/finish/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
|
-
const { TOOL_NAME, LOCK_FILE_RELATIVE, path, fs } = require('../context');
|
|
2
|
+
const { TOOL_NAME, SHORT_TOOL_NAME, LOCK_FILE_RELATIVE, path, fs } = require('../context');
|
|
3
3
|
const { isTerseMode } = require('../output');
|
|
4
|
-
const { run, runPackageAsset } = require('../core/runtime');
|
|
4
|
+
const { run, runPackageAsset, assetStdio } = require('../core/runtime');
|
|
5
5
|
const {
|
|
6
6
|
resolveRepoRoot,
|
|
7
7
|
uniquePreserveOrder,
|
|
@@ -31,6 +31,7 @@ const {
|
|
|
31
31
|
const submoduleModule = require('../submodule');
|
|
32
32
|
const { runPreflight, summarizePreflight } = require('./preflight');
|
|
33
33
|
const { runReviewGate } = require('./review-gate');
|
|
34
|
+
const { createFinishProgress, summarizeFinishRun } = require('./progress');
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
37
|
* Options recognized by {@link autoCommitWorktreeForFinish} and the public
|
|
@@ -52,8 +53,17 @@ const { runReviewGate } = require('./review-gate');
|
|
|
52
53
|
* @property {string} [branch] Single branch to finish (skips discovery).
|
|
53
54
|
* @property {string} [base] Explicit base branch override.
|
|
54
55
|
* @property {string} [target] Path inside the target repo (defaults to cwd).
|
|
56
|
+
* @property {boolean} [agentQuiet] Capture narrative finish output and emit compact JSON summaries.
|
|
55
57
|
*/
|
|
56
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Whether the caller explicitly requested that the legacy branch-finish script
|
|
61
|
+
* skip its full repository-defined preflight.
|
|
62
|
+
*/
|
|
63
|
+
function shouldSkipBranchPreflight(options = {}) {
|
|
64
|
+
return options.skipPreflight === true;
|
|
65
|
+
}
|
|
66
|
+
|
|
57
67
|
/**
|
|
58
68
|
* Outcome of an auto-commit attempt for a single branch.
|
|
59
69
|
*
|
|
@@ -201,6 +211,30 @@ function autoCommitWorktreeForFinish(repoRoot, worktreePath, branch, options) {
|
|
|
201
211
|
* @throws {Error} When the underlying prune subprocess exits non-zero or the watch sleep fails.
|
|
202
212
|
*/
|
|
203
213
|
function cleanup(rawArgs) {
|
|
214
|
+
if (rawArgs.some((arg) => arg === '--help' || arg === '-h') || (rawArgs.length === 1 && rawArgs[0] === 'help')) {
|
|
215
|
+
console.log(`USAGE: ${SHORT_TOOL_NAME} cleanup [options]
|
|
216
|
+
|
|
217
|
+
Prune merged or stale agent branches and worktrees.
|
|
218
|
+
|
|
219
|
+
OPTIONS
|
|
220
|
+
--target <path> Target repository (default: current directory)
|
|
221
|
+
--base <branch> Base branch used to determine merged branches
|
|
222
|
+
--branch <agent/*> Limit cleanup to one agent branch
|
|
223
|
+
--dry-run Print actions without changing branches or worktrees
|
|
224
|
+
--force-dirty Allow cleanup of dirty worktrees
|
|
225
|
+
--keep-remote Keep remote agent branches
|
|
226
|
+
--keep-clean-worktrees Keep clean worktrees instead of pruning them
|
|
227
|
+
--include-pr-merged Treat branches from merged PRs as merged
|
|
228
|
+
--idle-minutes <n> Only consider worktrees idle for at least n minutes
|
|
229
|
+
--watch Repeat cleanup cycles (defaults idle threshold to 60)
|
|
230
|
+
--interval <seconds> Watch interval in seconds (minimum: 5; default: 60)
|
|
231
|
+
--once Run one cleanup cycle when used with --watch
|
|
232
|
+
--max-branches <n> Limit branches processed per cycle
|
|
233
|
+
-h, --help Show this help`);
|
|
234
|
+
process.exitCode = 0;
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
204
238
|
const activeCwd = process.cwd();
|
|
205
239
|
const options = parseCleanupArgs(rawArgs);
|
|
206
240
|
const repoRoot = resolveRepoRoot(options.target);
|
|
@@ -324,6 +358,20 @@ function merge(rawArgs) {
|
|
|
324
358
|
* @returns {void}
|
|
325
359
|
* @throws {Error} When `--branch` references an unknown ref, or when any branch fails to finish (after the loop completes).
|
|
326
360
|
*/
|
|
361
|
+
/**
|
|
362
|
+
* Decide whether a finish run should sweep merged-but-stranded worktree dirs
|
|
363
|
+
* after the per-lane loop. Only for bulk `--all`, never on a dry run, only when
|
|
364
|
+
* every lane succeeded (failed === 0), and honoring the --no-sweep-orphans
|
|
365
|
+
* opt-out. Pure so the guard can be tested without the gh/PR finish flow.
|
|
366
|
+
*
|
|
367
|
+
* @param {{all?: boolean, sweepOrphans?: boolean, dryRun?: boolean}} options
|
|
368
|
+
* @param {number} failed Count of lanes that failed to finish.
|
|
369
|
+
* @returns {boolean}
|
|
370
|
+
*/
|
|
371
|
+
function shouldSweepOrphans(options, failed) {
|
|
372
|
+
return Boolean(options.all && options.sweepOrphans && !options.dryRun && failed === 0);
|
|
373
|
+
}
|
|
374
|
+
|
|
327
375
|
function finish(rawArgs, defaults = {}) {
|
|
328
376
|
const activeCwd = process.cwd();
|
|
329
377
|
const options = parseFinishArgs(rawArgs, defaults);
|
|
@@ -371,10 +419,17 @@ function finish(rawArgs, defaults = {}) {
|
|
|
371
419
|
let succeeded = 0;
|
|
372
420
|
let failed = 0;
|
|
373
421
|
let autoCommitted = 0;
|
|
374
|
-
const terse = isTerseMode();
|
|
422
|
+
const terse = isTerseMode() || options.agentQuiet;
|
|
375
423
|
|
|
376
424
|
for (const candidate of candidates) {
|
|
377
425
|
const { branch, baseBranch, worktreePath } = candidate;
|
|
426
|
+
const progress = createFinishProgress({
|
|
427
|
+
repoRoot,
|
|
428
|
+
branch,
|
|
429
|
+
baseBranch,
|
|
430
|
+
persistEvents: !options.dryRun,
|
|
431
|
+
quiet: options.agentQuiet,
|
|
432
|
+
});
|
|
378
433
|
// In terse mode, defer the "Finishing X -> Y" line until we know whether
|
|
379
434
|
// we also need to announce an auto-commit, then emit a single combined
|
|
380
435
|
// line per branch. Keep branch + base + worktree path so agents still see
|
|
@@ -386,6 +441,7 @@ function finish(rawArgs, defaults = {}) {
|
|
|
386
441
|
}
|
|
387
442
|
|
|
388
443
|
try {
|
|
444
|
+
progress.start('prepare', worktreePath ? 'checking worktree and pending changes' : 'checking branch');
|
|
389
445
|
let commitState = { changed: false, committed: false };
|
|
390
446
|
if (worktreePath) {
|
|
391
447
|
commitState = autoCommitWorktreeForFinish(repoRoot, worktreePath, branch, options);
|
|
@@ -395,9 +451,11 @@ function finish(rawArgs, defaults = {}) {
|
|
|
395
451
|
const suffix = commitState.committed
|
|
396
452
|
? ' [auto-committed]'
|
|
397
453
|
: (commitState.changed && commitState.dryRun ? ' [dry-run: would auto-commit]' : '');
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
454
|
+
if (!options.agentQuiet) {
|
|
455
|
+
console.log(
|
|
456
|
+
`[${TOOL_NAME}] Finishing '${branch}' -> '${baseBranch}'${worktreePath ? ` (${worktreePath})` : ''}${suffix}`,
|
|
457
|
+
);
|
|
458
|
+
}
|
|
401
459
|
if (commitState.committed) {
|
|
402
460
|
autoCommitted += 1;
|
|
403
461
|
}
|
|
@@ -407,6 +465,12 @@ function finish(rawArgs, defaults = {}) {
|
|
|
407
465
|
} else if (commitState.changed && commitState.dryRun) {
|
|
408
466
|
console.log(`[${TOOL_NAME}] [dry-run] Would auto-commit pending changes on '${branch}'.`);
|
|
409
467
|
}
|
|
468
|
+
progress.complete(
|
|
469
|
+
'prepare',
|
|
470
|
+
commitState.committed
|
|
471
|
+
? 'pending changes auto-committed'
|
|
472
|
+
: (commitState.changed && commitState.dryRun ? 'would auto-commit pending changes' : 'branch ready'),
|
|
473
|
+
);
|
|
410
474
|
|
|
411
475
|
if (options.advanceSubmodules && worktreePath) {
|
|
412
476
|
const gitmodulesPath = path.join(worktreePath, '.gitmodules');
|
|
@@ -455,6 +519,13 @@ function finish(rawArgs, defaults = {}) {
|
|
|
455
519
|
finishArgs.push(options.parentGitlinkCommit ? '--parent-gitlink-commit' : '--no-parent-gitlink-commit');
|
|
456
520
|
|
|
457
521
|
if (options.dryRun) {
|
|
522
|
+
progress.skip('preflight', 'dry run');
|
|
523
|
+
progress.skip('pr', 'dry run');
|
|
524
|
+
progress.skip('review', 'dry run');
|
|
525
|
+
progress.skip('autofix', 'dry run');
|
|
526
|
+
progress.skip('ci', 'dry run');
|
|
527
|
+
progress.skip('merge', 'dry run');
|
|
528
|
+
progress.skip('cleanup', 'dry run');
|
|
458
529
|
console.log(`[${TOOL_NAME}] [dry-run] Would run: gx branch finish ${finishArgs.join(' ')}`);
|
|
459
530
|
succeeded += 1;
|
|
460
531
|
continue;
|
|
@@ -463,11 +534,13 @@ function finish(rawArgs, defaults = {}) {
|
|
|
463
534
|
// Preflight: typecheck + lint touched workspace packages before opening
|
|
464
535
|
// a PR. Only enforced for PR-mode finishes; bypass with --skip-preflight.
|
|
465
536
|
if (options.mergeMode === 'pr' && !options.skipPreflight) {
|
|
537
|
+
progress.start('preflight', 'running targeted local verification');
|
|
466
538
|
const preflight = runPreflight(repoRoot, worktreePath, branch, baseBranch, {
|
|
467
539
|
verbose: !terse,
|
|
468
540
|
});
|
|
469
|
-
console.log(`[${TOOL_NAME}] ${summarizePreflight(preflight)}`);
|
|
541
|
+
if (!options.agentQuiet) console.log(`[${TOOL_NAME}] ${summarizePreflight(preflight)}`);
|
|
470
542
|
if (preflight.status === 'failed') {
|
|
543
|
+
progress.fail('preflight', `${preflight.failures.length} script(s) failed`);
|
|
471
544
|
for (const f of preflight.failures) {
|
|
472
545
|
console.error(`[${TOOL_NAME}] preflight failure: ${f.label} (exit ${f.status})`);
|
|
473
546
|
if (f.stderr && f.stderr.trim()) {
|
|
@@ -480,25 +553,66 @@ function finish(rawArgs, defaults = {}) {
|
|
|
480
553
|
`preflight failed for ${preflight.failures.length} script(s). Fix the failures or rerun with --skip-preflight to bypass.`,
|
|
481
554
|
);
|
|
482
555
|
}
|
|
556
|
+
progress.complete('preflight', preflight.status);
|
|
557
|
+
} else {
|
|
558
|
+
progress.skip(
|
|
559
|
+
'preflight',
|
|
560
|
+
options.skipPreflight ? 'disabled by flag' : 'not a PR finish',
|
|
561
|
+
);
|
|
483
562
|
}
|
|
484
563
|
|
|
485
564
|
// Opt-in merge gate (--gate-review / gx ship): enforce a clean AI review +
|
|
486
565
|
// green CI + GitHub-mergeable verdict BEFORE the shell merge runs. Throws on
|
|
487
566
|
// failure, which the catch below turns into a finish failure (no merge).
|
|
488
567
|
if (options.mergeMode === 'pr' && options.gateReview) {
|
|
489
|
-
runReviewGate({
|
|
568
|
+
runReviewGate({
|
|
569
|
+
repoRoot, worktreePath, branch, baseBranch, options, progress,
|
|
570
|
+
});
|
|
571
|
+
} else {
|
|
572
|
+
progress.skip('review', 'review gate disabled');
|
|
573
|
+
progress.skip('autofix', 'review gate disabled');
|
|
574
|
+
progress.skip('ci', 'review gate disabled; repository policy controls merge readiness');
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
if (shouldSkipBranchPreflight(options)) {
|
|
578
|
+
finishArgs.push('--no-preflight');
|
|
490
579
|
}
|
|
491
580
|
|
|
581
|
+
// Streamed, not piped: the script can sit for minutes waiting on the PR
|
|
582
|
+
// merge, and buffering means the operator sees nothing until it exits —
|
|
583
|
+
// and sees NOTHING AT ALL if the process is killed while waiting, since
|
|
584
|
+
// the buffer dies with it. Streaming cannot interleave two branches'
|
|
585
|
+
// output, because lanes run sequentially here.
|
|
586
|
+
//
|
|
587
|
+
// The exception is `gx agents finish --json`, which reads this script's
|
|
588
|
+
// output through a process.stdout.write patch to recover the merged-PR
|
|
589
|
+
// URL — a child on 'inherit' writes past that patch. assetStdio owns that
|
|
590
|
+
// decision for every caller, so this path and invokePackageAsset cannot
|
|
591
|
+
// drift apart.
|
|
492
592
|
const finishResult = runPackageAsset('branchFinish', finishArgs, {
|
|
493
593
|
cwd: repoRoot,
|
|
494
|
-
stdio: 'pipe',
|
|
495
|
-
env: {
|
|
594
|
+
stdio: options.agentQuiet ? 'pipe' : assetStdio('branchFinish'),
|
|
595
|
+
env: {
|
|
596
|
+
GUARDEX_FINISH_ACTIVE_CWD: activeCwd,
|
|
597
|
+
GUARDEX_FINISH_CHECKLIST: '1',
|
|
598
|
+
GUARDEX_FINISH_GATE_DONE: options.gateReview ? '1' : '0',
|
|
599
|
+
...progress.eventEnv,
|
|
600
|
+
},
|
|
496
601
|
});
|
|
497
|
-
if (
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
602
|
+
if (options.agentQuiet) {
|
|
603
|
+
const summary = summarizeFinishRun({
|
|
604
|
+
eventFile: progress.eventEnv.GUARDEX_FINISH_EVENT_FILE,
|
|
605
|
+
branch,
|
|
606
|
+
baseBranch,
|
|
607
|
+
stdout: finishResult.stdout,
|
|
608
|
+
stderr: finishResult.stderr,
|
|
609
|
+
status: finishResult.status,
|
|
610
|
+
});
|
|
611
|
+
process.stdout.write(`[gx:finish] ${JSON.stringify(summary)}\n`);
|
|
612
|
+
} else {
|
|
613
|
+
// Null under 'inherit'; kept so an explicit pipe still prints.
|
|
614
|
+
if (finishResult.stdout) process.stdout.write(finishResult.stdout);
|
|
615
|
+
if (finishResult.stderr) process.stderr.write(finishResult.stderr);
|
|
502
616
|
}
|
|
503
617
|
if (finishResult.status !== 0) {
|
|
504
618
|
throw new Error(`agent-branch-finish exited with status ${finishResult.status}`);
|
|
@@ -514,9 +628,40 @@ function finish(rawArgs, defaults = {}) {
|
|
|
514
628
|
}
|
|
515
629
|
}
|
|
516
630
|
|
|
517
|
-
|
|
518
|
-
`[
|
|
519
|
-
|
|
631
|
+
if (options.agentQuiet) {
|
|
632
|
+
process.stdout.write(`[gx:finish] ${JSON.stringify({
|
|
633
|
+
total: candidates.length,
|
|
634
|
+
success: succeeded,
|
|
635
|
+
failed,
|
|
636
|
+
autoCommitted,
|
|
637
|
+
})}\n`);
|
|
638
|
+
} else {
|
|
639
|
+
console.log(
|
|
640
|
+
`[${TOOL_NAME}] Finish summary: total=${candidates.length}, success=${succeeded}, failed=${failed}, autoCommitted=${autoCommitted}`,
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Bulk `--all` finish self-cleans: sweep merged-but-stranded worktree dirs
|
|
645
|
+
// whose branch was merged out-of-band and never reaped (the post-merge
|
|
646
|
+
// "retained for now" gap in agent-branch-finish.sh). Only when every lane
|
|
647
|
+
// succeeded, never on a dry run, and opt-out via --no-sweep-orphans. The
|
|
648
|
+
// sweep is best-effort: a failure warns but does not fail the finish.
|
|
649
|
+
if (shouldSweepOrphans(options, failed)) {
|
|
650
|
+
const baseForSweep = options.base || candidates[0].baseBranch;
|
|
651
|
+
const sweepArgs = ['--include-pr-merged', '--delete-branches'];
|
|
652
|
+
if (baseForSweep) {
|
|
653
|
+
sweepArgs.push('--base', baseForSweep);
|
|
654
|
+
}
|
|
655
|
+
if (!options.agentQuiet) console.log(`[${TOOL_NAME}] Sweeping merged-but-stranded worktrees...`);
|
|
656
|
+
const sweep = runPackageAsset('worktreePrune', sweepArgs, {
|
|
657
|
+
cwd: repoRoot,
|
|
658
|
+
stdio: options.agentQuiet ? 'pipe' : 'inherit',
|
|
659
|
+
env: { GUARDEX_PRUNE_ACTIVE_CWD: activeCwd },
|
|
660
|
+
});
|
|
661
|
+
if (sweep.status !== 0) {
|
|
662
|
+
console.error(`[${TOOL_NAME}] Warning: orphan sweep failed (non-fatal).`);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
520
665
|
|
|
521
666
|
if (failed > 0) {
|
|
522
667
|
throw new Error('finish command failed for one or more agent branches');
|
|
@@ -701,4 +846,6 @@ module.exports = {
|
|
|
701
846
|
finish,
|
|
702
847
|
sync,
|
|
703
848
|
autoCommitWorktreeForFinish,
|
|
849
|
+
shouldSweepOrphans,
|
|
850
|
+
shouldSkipBranchPreflight,
|
|
704
851
|
};
|