@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
|
@@ -54,26 +54,35 @@ function captureWorktreeIdentity(worktreePath, runner = run) {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
function
|
|
58
|
-
if (!plan?.worktreeIdentity) return
|
|
57
|
+
function worktreeIdentityState(plan, runner = run) {
|
|
58
|
+
if (!plan?.worktreeIdentity) return 'unknown';
|
|
59
59
|
try {
|
|
60
60
|
const gitDirResult = runner(
|
|
61
61
|
'git',
|
|
62
62
|
['-C', plan.worktreePath, 'rev-parse', '--absolute-git-dir'],
|
|
63
63
|
{ cwd: plan.worktreePath }
|
|
64
64
|
);
|
|
65
|
-
if (gitDirResult.status !== 0) return
|
|
65
|
+
if (gitDirResult.status !== 0 || !String(gitDirResult.stdout || '').trim()) return 'unknown';
|
|
66
66
|
const gitDir = path.resolve(String(gitDirResult.stdout || '').trim());
|
|
67
|
-
if (gitDir !== plan.worktreeIdentity.gitDir) return
|
|
67
|
+
if (gitDir !== plan.worktreeIdentity.gitDir) return 'changed';
|
|
68
68
|
return (
|
|
69
69
|
fs.readFileSync(path.join(gitDir, 'gitguardex-finish-cleanup-id'), 'utf8').trim() ===
|
|
70
70
|
plan.worktreeIdentity.token
|
|
71
|
-
);
|
|
71
|
+
) ? 'same' : 'changed';
|
|
72
72
|
} catch {
|
|
73
|
-
return
|
|
73
|
+
return 'unknown';
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
function worktreeIdentityMatches(plan, runner = run) {
|
|
78
|
+
return worktreeIdentityState(plan, runner) === 'same';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function worktreeMissing(worktreePath) {
|
|
82
|
+
try { fs.lstatSync(worktreePath); return false; }
|
|
83
|
+
catch (error) { return error.code === 'ENOENT'; }
|
|
84
|
+
}
|
|
85
|
+
|
|
77
86
|
function resolveSharedRepoRoot(target) {
|
|
78
87
|
const worktreeRoot = resolveRepoRoot(target);
|
|
79
88
|
const commonDir = run('git', ['-C', worktreeRoot, 'rev-parse', '--git-common-dir'], {
|
|
@@ -155,6 +164,12 @@ function probeLiveProcessInWorktree(worktreePath, options = {}) {
|
|
|
155
164
|
return { supported: true, active };
|
|
156
165
|
}
|
|
157
166
|
|
|
167
|
+
function probeWorktreeOwnership(worktreePath, options = {}) {
|
|
168
|
+
const lease = require('../agents/process-lease').probeLeases(worktreePath);
|
|
169
|
+
if (lease.active) return lease;
|
|
170
|
+
return (options.probe || probeLiveProcessInWorktree)(worktreePath, options);
|
|
171
|
+
}
|
|
172
|
+
|
|
158
173
|
function hasLiveProcessInWorktree(
|
|
159
174
|
worktreePath,
|
|
160
175
|
procRoot = process.platform === 'linux' ? '/proc' : '',
|
|
@@ -191,12 +206,247 @@ function prepareBranchFinishCleanup(argv, activeCwd) {
|
|
|
191
206
|
}
|
|
192
207
|
}
|
|
193
208
|
|
|
194
|
-
function
|
|
209
|
+
function commonGitDirectory(repoRoot) {
|
|
210
|
+
const result = run('git', ['-C', repoRoot, 'rev-parse', '--git-common-dir']);
|
|
211
|
+
if (result.status !== 0) throw new Error('Cannot resolve cleanup queue directory');
|
|
212
|
+
return fs.realpathSync(path.resolve(repoRoot, result.stdout.trim()));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function cleanupQueueDirectory(repoRoot, create = false) {
|
|
216
|
+
const directory = path.join(commonGitDirectory(repoRoot), 'gitguardex-pending-cleanup');
|
|
217
|
+
if (create) {
|
|
218
|
+
try {
|
|
219
|
+
fs.mkdirSync(directory, { mode: 0o700 });
|
|
220
|
+
} catch (error) {
|
|
221
|
+
if (error.code !== 'EEXIST') throw error;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const stat = fs.lstatSync(directory);
|
|
225
|
+
if (
|
|
226
|
+
!stat.isDirectory() ||
|
|
227
|
+
stat.isSymbolicLink() ||
|
|
228
|
+
stat.mode & 0o077 ||
|
|
229
|
+
(process.getuid && stat.uid !== process.getuid())
|
|
230
|
+
)
|
|
231
|
+
throw new Error('Unsafe cleanup queue directory');
|
|
232
|
+
return directory;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function readPrivateJob(file) {
|
|
236
|
+
const fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
|
|
237
|
+
try {
|
|
238
|
+
const stat = fs.fstatSync(fd);
|
|
239
|
+
if (
|
|
240
|
+
!stat.isFile() ||
|
|
241
|
+
stat.nlink !== 1 ||
|
|
242
|
+
stat.size > 16384 ||
|
|
243
|
+
stat.mode & 0o077 ||
|
|
244
|
+
(process.getuid && stat.uid !== process.getuid())
|
|
245
|
+
)
|
|
246
|
+
throw new Error('Unsafe cleanup job');
|
|
247
|
+
return JSON.parse(fs.readFileSync(fd, 'utf8'));
|
|
248
|
+
} finally {
|
|
249
|
+
fs.closeSync(fd);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function validCleanupPlan(plan) {
|
|
254
|
+
try {
|
|
255
|
+
const common = commonGitDirectory(plan.repoRoot);
|
|
256
|
+
return (
|
|
257
|
+
fs.realpathSync(plan.repoRoot) === plan.repoRoot &&
|
|
258
|
+
path.resolve(plan.worktreePath) === plan.worktreePath &&
|
|
259
|
+
isManagedAgentWorktree(plan.repoRoot, plan.worktreePath) &&
|
|
260
|
+
pathContains(path.join(common, 'worktrees'), plan.worktreeIdentity.gitDir) &&
|
|
261
|
+
plan.worktreeIdentity.gitDir !== path.join(common, 'worktrees') &&
|
|
262
|
+
/^[a-f0-9-]{36}$/.test(plan.worktreeIdentity.token) &&
|
|
263
|
+
/^[a-f0-9]{40,64}$/.test(plan.expectedHead)
|
|
264
|
+
);
|
|
265
|
+
} catch {
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Persist only after a successful finish, pinning the post-finish detached HEAD.
|
|
271
|
+
// Pending jobs live outside the worktree and survive worker exits/reboots.
|
|
272
|
+
function persistFinishedCleanup(plan) {
|
|
273
|
+
try {
|
|
274
|
+
if (!plan || !worktreeIdentityMatches(plan)) return null;
|
|
275
|
+
const head = run('git', ['-C', plan.worktreePath, 'rev-parse', 'HEAD']);
|
|
276
|
+
const branch = run('git', ['-C', plan.worktreePath, 'symbolic-ref', '-q', 'HEAD']);
|
|
277
|
+
if (head.status !== 0 || branch.status !== 1) return null;
|
|
278
|
+
const pending = {
|
|
279
|
+
repoRoot: fs.realpathSync(plan.repoRoot),
|
|
280
|
+
worktreePath: plan.worktreePath,
|
|
281
|
+
worktreeIdentity: plan.worktreeIdentity,
|
|
282
|
+
expectedHead: head.stdout.trim()
|
|
283
|
+
};
|
|
284
|
+
if (!validCleanupPlan(pending)) return null;
|
|
285
|
+
const jobFile = path.join(
|
|
286
|
+
cleanupQueueDirectory(pending.repoRoot, true),
|
|
287
|
+
pending.worktreeIdentity.token + '.json'
|
|
288
|
+
);
|
|
289
|
+
try {
|
|
290
|
+
fs.writeFileSync(jobFile, JSON.stringify(pending), { flag: 'wx', mode: 0o600 });
|
|
291
|
+
} catch (error) {
|
|
292
|
+
if (error.code !== 'EEXIST') throw error;
|
|
293
|
+
// Never rebind an existing job to new work at the same path.
|
|
294
|
+
if (JSON.stringify(readPrivateJob(jobFile)) !== JSON.stringify(pending)) return null;
|
|
295
|
+
}
|
|
296
|
+
return { ...pending, jobFile };
|
|
297
|
+
} catch (error) {
|
|
298
|
+
console.error(`[${TOOL_NAME}] Warning: could not persist finished cleanup: ${error.message}`);
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function retireCleanupJob(plan) {
|
|
304
|
+
if (!plan.jobFile) return;
|
|
305
|
+
try {
|
|
306
|
+
const expected = path.join(
|
|
307
|
+
cleanupQueueDirectory(plan.repoRoot),
|
|
308
|
+
plan.worktreeIdentity.token + '.json'
|
|
309
|
+
);
|
|
310
|
+
if (plan.jobFile === expected) fs.unlinkSync(expected);
|
|
311
|
+
} catch (error) {
|
|
312
|
+
if (error.code !== 'ENOENT') throw error;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function retryPendingFinishedCleanup(repoRoot, options = {}) {
|
|
317
|
+
let directory;
|
|
318
|
+
try {
|
|
319
|
+
directory = cleanupQueueDirectory(repoRoot);
|
|
320
|
+
} catch (error) {
|
|
321
|
+
if (error.code !== 'ENOENT')
|
|
322
|
+
console.error(`[${TOOL_NAME}] Warning: cleanup queue unavailable: ${error.message}`);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
// Bounded opportunistic pass; long-lived sessions are handled by workers.
|
|
326
|
+
let attempted = 0;
|
|
327
|
+
const jobs = fs.readdirSync(directory).filter((name) => /^[a-f0-9-]{36}\.json$/.test(name))
|
|
328
|
+
.map((name) => {
|
|
329
|
+
try { return { name, mtime: fs.lstatSync(path.join(directory, name)).mtimeMs }; }
|
|
330
|
+
catch { return null; }
|
|
331
|
+
}).filter(Boolean).sort((a, b) => a.mtime - b.mtime || a.name.localeCompare(b.name));
|
|
332
|
+
for (const { name } of jobs) {
|
|
333
|
+
if (attempted >= 16) break;
|
|
334
|
+
try {
|
|
335
|
+
const jobFile = path.join(directory, name);
|
|
336
|
+
const plan = { ...readPrivateJob(jobFile), jobFile };
|
|
337
|
+
if (
|
|
338
|
+
!validCleanupPlan(plan) ||
|
|
339
|
+
commonGitDirectory(plan.repoRoot) !== commonGitDirectory(repoRoot) ||
|
|
340
|
+
name !== plan.worktreeIdentity.token + '.json'
|
|
341
|
+
)
|
|
342
|
+
continue;
|
|
343
|
+
attempted++;
|
|
344
|
+
// Rotate blocked jobs durably so later jobs are not starved.
|
|
345
|
+
const fd = fs.openSync(jobFile, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
|
|
346
|
+
try {
|
|
347
|
+
if (fs.fstatSync(fd).nlink !== 1) continue;
|
|
348
|
+
const now = new Date();
|
|
349
|
+
fs.futimesSync(fd, now, now);
|
|
350
|
+
} finally { fs.closeSync(fd); }
|
|
351
|
+
const identity = worktreeIdentityState(plan);
|
|
352
|
+
if (worktreeMissing(plan.worktreePath) || identity === 'changed') {
|
|
353
|
+
retireCleanupJob(plan);
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (identity !== 'same') continue;
|
|
357
|
+
const head = run('git', ['-C', plan.worktreePath, 'rev-parse', 'HEAD']);
|
|
358
|
+
if (head.status === 0 && head.stdout.trim() !== plan.expectedHead) {
|
|
359
|
+
retireCleanupJob(plan);
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (cleanupFinishedDetachedWorktree(plan)) retireCleanupJob(plan);
|
|
363
|
+
else if (options.schedule) scheduleFinishedDetachedWorktreeCleanup(plan);
|
|
364
|
+
} catch (error) {
|
|
365
|
+
console.error(`[${TOOL_NAME}] Warning: skipped cleanup job: ${error.message}`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function cleanupFinishedDetachedWorktree(input) {
|
|
371
|
+
const plan = input?.expectedHead ? input : persistFinishedCleanup(input);
|
|
372
|
+
if (!validCleanupPlan(plan)) return false;
|
|
373
|
+
if (!worktreeIdentityMatches(plan) || probeWorktreeOwnership(plan.worktreePath).active)
|
|
374
|
+
return false;
|
|
375
|
+
try {
|
|
376
|
+
require('../worktree-hooks').runLifecycleHookSync(plan.repoRoot, 'pre-remove', {
|
|
377
|
+
worktreePath: plan.worktreePath,
|
|
378
|
+
branch: plan.branch
|
|
379
|
+
});
|
|
380
|
+
} catch (error) {
|
|
381
|
+
console.error(`[${TOOL_NAME}] pre-remove preserved finished worktree: ${error.message}`);
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
// Same OS mutex as claims writers and prune; never release anyone's claims.
|
|
385
|
+
const result = run(
|
|
386
|
+
'python3',
|
|
387
|
+
[
|
|
388
|
+
path.join(__dirname, 'with-claims-lock.py'),
|
|
389
|
+
'--try-lock',
|
|
390
|
+
path.join(commonGitDirectory(plan.repoRoot), 'agent-file-locks.lock'),
|
|
391
|
+
process.execPath,
|
|
392
|
+
__filename,
|
|
393
|
+
'--remove-locked',
|
|
394
|
+
Buffer.from(JSON.stringify(plan)).toString('base64url')
|
|
395
|
+
],
|
|
396
|
+
{ cwd: plan.repoRoot }
|
|
397
|
+
);
|
|
398
|
+
if (result.status === 0) {
|
|
399
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
400
|
+
retireCleanupJob(plan);
|
|
401
|
+
try {
|
|
402
|
+
require('../worktree-hooks').runLifecycleHookSync(plan.repoRoot, 'post-remove', {
|
|
403
|
+
worktreePath: plan.worktreePath,
|
|
404
|
+
branch: plan.branch
|
|
405
|
+
});
|
|
406
|
+
} catch (error) {
|
|
407
|
+
console.error(`[${TOOL_NAME}] Worktree removed; post-remove hook failed: ${error.message}`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return result.status === 0;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function removeFinishedWorktreeLocked(plan) {
|
|
414
|
+
if (!validCleanupPlan(plan)) return false;
|
|
195
415
|
if (!plan || !worktreeIdentityMatches(plan)) return false;
|
|
196
416
|
|
|
197
417
|
try {
|
|
198
418
|
process.chdir(plan.repoRoot);
|
|
199
|
-
if (
|
|
419
|
+
if (fs.realpathSync(plan.worktreePath) !== plan.worktreePath) return false;
|
|
420
|
+
const registered = run('git', ['-C', plan.repoRoot, 'worktree', 'list', '--porcelain', '-z']);
|
|
421
|
+
if (registered.status !== 0) return false;
|
|
422
|
+
let found = false;
|
|
423
|
+
for (const record of registered.stdout.split('\0\0')) {
|
|
424
|
+
const fields = record.split('\0');
|
|
425
|
+
const entry = fields.find((field) => field.startsWith('worktree '));
|
|
426
|
+
if (!entry) continue;
|
|
427
|
+
const candidate = path.resolve(entry.slice(9));
|
|
428
|
+
if (candidate === plan.worktreePath) {
|
|
429
|
+
found = true;
|
|
430
|
+
if (fields.some((field) => field === 'locked' || field.startsWith('locked '))) return false;
|
|
431
|
+
} else if (pathContains(plan.worktreePath, candidate)) return false;
|
|
432
|
+
}
|
|
433
|
+
if (!found) return false;
|
|
434
|
+
const claimsFile = path.join(plan.worktreePath, '.omx/state/agent-file-locks.json');
|
|
435
|
+
try {
|
|
436
|
+
const claims = JSON.parse(fs.readFileSync(claimsFile, 'utf8'));
|
|
437
|
+
if (
|
|
438
|
+
!claims?.locks ||
|
|
439
|
+
typeof claims.locks !== 'object' ||
|
|
440
|
+
Array.isArray(claims.locks) ||
|
|
441
|
+
Object.keys(claims.locks).length
|
|
442
|
+
)
|
|
443
|
+
return false;
|
|
444
|
+
} catch (error) {
|
|
445
|
+
if (error.code !== 'ENOENT') return false;
|
|
446
|
+
}
|
|
447
|
+
const commit = run('git', ['-C', plan.worktreePath, 'rev-parse', 'HEAD']);
|
|
448
|
+
if (commit.status !== 0 || commit.stdout.trim() !== plan.expectedHead) return false;
|
|
449
|
+
if (probeWorktreeOwnership(plan.worktreePath).active) return false;
|
|
200
450
|
if (!worktreeIdentityMatches(plan)) return false;
|
|
201
451
|
const status = run(
|
|
202
452
|
'git',
|
|
@@ -225,7 +475,6 @@ function cleanupFinishedDetachedWorktree(plan) {
|
|
|
225
475
|
);
|
|
226
476
|
return false;
|
|
227
477
|
}
|
|
228
|
-
run('git', ['-C', plan.repoRoot, 'worktree', 'prune'], { cwd: plan.repoRoot });
|
|
229
478
|
console.log(
|
|
230
479
|
`[${TOOL_NAME}] Removed finished detached worktree after finish worker exit: ${plan.worktreePath}`
|
|
231
480
|
);
|
|
@@ -238,17 +487,19 @@ function cleanupFinishedDetachedWorktree(plan) {
|
|
|
238
487
|
}
|
|
239
488
|
}
|
|
240
489
|
|
|
241
|
-
function scheduleFinishedDetachedWorktreeCleanup(
|
|
242
|
-
if (!
|
|
490
|
+
function scheduleFinishedDetachedWorktreeCleanup(input, options = {}) {
|
|
491
|
+
if (!input || !fs.existsSync(input.worktreePath)) return false;
|
|
243
492
|
|
|
244
493
|
try {
|
|
494
|
+
const plan = input.jobFile ? input : persistFinishedCleanup(input);
|
|
495
|
+
if (!plan || !validCleanupPlan(plan)) return false;
|
|
245
496
|
const runner = options.runner || run;
|
|
246
497
|
const worktreeIdentity =
|
|
247
498
|
plan.worktreeIdentity || captureWorktreeIdentity(plan.worktreePath, runner);
|
|
248
499
|
if (!worktreeIdentity) return false;
|
|
249
500
|
const cleanupPlan = { ...plan, worktreeIdentity };
|
|
250
501
|
if (!worktreeIdentityMatches(cleanupPlan, runner)) return false;
|
|
251
|
-
const probe =
|
|
502
|
+
const probe = probeWorktreeOwnership(plan.worktreePath, options);
|
|
252
503
|
if (!probe.supported) {
|
|
253
504
|
console.error(
|
|
254
505
|
`[${TOOL_NAME}] Warning: cannot safely monitor the finished worktree; deferred cleanup was not started: ${plan.worktreePath}`
|
|
@@ -257,8 +508,16 @@ function scheduleFinishedDetachedWorktreeCleanup(plan, options = {}) {
|
|
|
257
508
|
}
|
|
258
509
|
const payload = Buffer.from(JSON.stringify(cleanupPlan), 'utf8').toString('base64url');
|
|
259
510
|
const child = (options.spawn || cp.spawn)(
|
|
260
|
-
|
|
261
|
-
[
|
|
511
|
+
'python3',
|
|
512
|
+
[
|
|
513
|
+
path.join(__dirname, 'with-claims-lock.py'),
|
|
514
|
+
'--try-lock',
|
|
515
|
+
path.join(cleanupQueueDirectory(plan.repoRoot), plan.worktreeIdentity.token + '.lock'),
|
|
516
|
+
process.execPath,
|
|
517
|
+
__filename,
|
|
518
|
+
'--deferred-worker',
|
|
519
|
+
payload
|
|
520
|
+
],
|
|
262
521
|
{
|
|
263
522
|
cwd: plan.repoRoot,
|
|
264
523
|
detached: true,
|
|
@@ -284,17 +543,40 @@ function scheduleFinishedDetachedWorktreeCleanup(plan, options = {}) {
|
|
|
284
543
|
}
|
|
285
544
|
|
|
286
545
|
async function runDeferredCleanupWorker(plan, options = {}) {
|
|
287
|
-
const attempts = options.attempts ??
|
|
546
|
+
const attempts = options.attempts ?? 360;
|
|
288
547
|
const intervalMs = options.intervalMs ?? 1000;
|
|
289
548
|
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
290
|
-
if (
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
549
|
+
if (worktreeMissing(plan.worktreePath)) {
|
|
550
|
+
retireCleanupJob(plan);
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
const identity = worktreeIdentityState(plan, options.runner || run);
|
|
554
|
+
if (identity === 'changed') {
|
|
555
|
+
retireCleanupJob(plan);
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
if (plan.expectedHead) {
|
|
559
|
+
const head = (options.runner || run)('git', ['-C', plan.worktreePath, 'rev-parse', 'HEAD']);
|
|
560
|
+
if (head.status === 0 && head.stdout.trim() !== plan.expectedHead) {
|
|
561
|
+
retireCleanupJob(plan);
|
|
562
|
+
return false;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const probe = probeWorktreeOwnership(plan.worktreePath, options);
|
|
566
|
+
if (
|
|
567
|
+
identity === 'same' &&
|
|
568
|
+
probe.supported &&
|
|
569
|
+
!probe.active &&
|
|
570
|
+
(options.cleanup || cleanupFinishedDetachedWorktree)(plan)
|
|
571
|
+
) {
|
|
572
|
+
retireCleanupJob(plan);
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
575
|
+
if (attempt + 1 < attempts) {
|
|
576
|
+
await new Promise((resolve) =>
|
|
577
|
+
setTimeout(resolve, Math.min(intervalMs * (attempt + 1), 30_000))
|
|
578
|
+
);
|
|
296
579
|
}
|
|
297
|
-
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
298
580
|
}
|
|
299
581
|
return false;
|
|
300
582
|
}
|
|
@@ -306,12 +588,24 @@ module.exports = {
|
|
|
306
588
|
hasLiveProcessInWorktree,
|
|
307
589
|
isManagedAgentWorktree,
|
|
308
590
|
probeLiveProcessInWorktree,
|
|
591
|
+
probeWorktreeOwnership,
|
|
309
592
|
prepareBranchFinishCleanup,
|
|
593
|
+
persistFinishedCleanup,
|
|
594
|
+
retryPendingFinishedCleanup,
|
|
310
595
|
runDeferredCleanupWorker,
|
|
311
596
|
scheduleFinishedDetachedWorktreeCleanup,
|
|
312
597
|
worktreeIdentityMatches
|
|
313
598
|
};
|
|
314
599
|
|
|
600
|
+
if (require.main === module && process.argv[2] === '--remove-locked') {
|
|
601
|
+
try {
|
|
602
|
+
const plan = JSON.parse(Buffer.from(process.argv[3] || '', 'base64url').toString('utf8'));
|
|
603
|
+
process.exitCode = removeFinishedWorktreeLocked(plan) ? 0 : 1;
|
|
604
|
+
} catch {
|
|
605
|
+
process.exitCode = 1;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
315
609
|
if (require.main === module && process.argv[2] === '--deferred-worker') {
|
|
316
610
|
try {
|
|
317
611
|
const plan = JSON.parse(Buffer.from(process.argv[3] || '', 'base64url').toString('utf8'));
|
package/src/finish/preflight.js
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
const fs = require('fs');
|
|
13
13
|
const path = require('path');
|
|
14
14
|
const { spawnSync } = require('child_process');
|
|
15
|
+
const { run, runPackageAsset } = require('../core/runtime');
|
|
15
16
|
|
|
16
17
|
const PREFLIGHT_SCRIPTS = ['typecheck', 'lint'];
|
|
17
18
|
|
|
@@ -137,12 +138,16 @@ function runPreflight(repoRoot, worktreePath, branch, baseBranch, options = {})
|
|
|
137
138
|
const ok = !result.error && result.status === 0;
|
|
138
139
|
ran.push({ label, ok, status: result.status, cmd, args });
|
|
139
140
|
if (!ok) {
|
|
141
|
+
let errorMessage = result.error ? result.error.message : null;
|
|
142
|
+
if (result.error?.code === 'ETIMEDOUT') {
|
|
143
|
+
errorMessage = `${label} timed out after 5 min. Run with --skip-preflight to bypass, or check for a hung tsc/eslint process.`;
|
|
144
|
+
}
|
|
140
145
|
failures.push({
|
|
141
146
|
label,
|
|
142
147
|
status: result.status,
|
|
143
148
|
stdout: stdout.slice(-2000),
|
|
144
149
|
stderr: stderr.slice(-2000),
|
|
145
|
-
error:
|
|
150
|
+
error: errorMessage,
|
|
146
151
|
});
|
|
147
152
|
}
|
|
148
153
|
}
|
|
@@ -168,10 +173,48 @@ function summarizePreflight(result) {
|
|
|
168
173
|
return `[preflight] ${result.status} — ${tail}`;
|
|
169
174
|
}
|
|
170
175
|
|
|
176
|
+
function branchPreflightDuringCi(repoRoot, args, { activeCwd, agentQuiet, progress }) {
|
|
177
|
+
return ({ reviewedHeadSha, reviewedBaseSha }) => {
|
|
178
|
+
// A repo that opts into disabled-Actions fallback may require a *trusted
|
|
179
|
+
// base* preflight instead. Leave that decision to the final finish.
|
|
180
|
+
const fallback = run(
|
|
181
|
+
'git',
|
|
182
|
+
['config', '--local', '--type=bool', '--get', 'multiagent.ciFallbackWhenDisabled'],
|
|
183
|
+
{ cwd: repoRoot }
|
|
184
|
+
);
|
|
185
|
+
if (fallback.status !== 1 && String(fallback.stdout || '').trim() !== 'false') return;
|
|
186
|
+
const result = runPackageAsset('branchFinish', args, {
|
|
187
|
+
cwd: repoRoot,
|
|
188
|
+
stdio: agentQuiet ? 'pipe' : 'inherit',
|
|
189
|
+
env: {
|
|
190
|
+
GUARDEX_FINISH_ACTIVE_CWD: activeCwd,
|
|
191
|
+
GUARDEX_FINISH_CHECKLIST: '1',
|
|
192
|
+
GUARDEX_FINISH_GATE_DONE: '1',
|
|
193
|
+
GUARDEX_FINISH_PREFLIGHT_ONLY: '1',
|
|
194
|
+
GUARDEX_FINISH_REQUIRE_PREFLIGHT: '0',
|
|
195
|
+
GUARDEX_FINISH_REVIEWED_HEAD: reviewedHeadSha,
|
|
196
|
+
GUARDEX_FINISH_REVIEWED_BASE: reviewedBaseSha,
|
|
197
|
+
...progress.eventEnv
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
if (result.status !== 0) {
|
|
201
|
+
const detail = String(result.stderr || '')
|
|
202
|
+
.trim()
|
|
203
|
+
.split('\n')
|
|
204
|
+
.slice(-8)
|
|
205
|
+
.join('\n');
|
|
206
|
+
throw new Error(
|
|
207
|
+
`Preflight failed before the CI wait. Refusing to merge.${detail ? '\n' + detail : ''}`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
171
213
|
module.exports = {
|
|
172
214
|
detectAppPackages,
|
|
173
215
|
detectTouchedDirs,
|
|
174
216
|
pickPackageManager,
|
|
175
217
|
runPreflight,
|
|
176
218
|
summarizePreflight,
|
|
219
|
+
branchPreflightDuringCi,
|
|
177
220
|
};
|
package/src/finish/progress.js
CHANGED
|
@@ -130,6 +130,9 @@ function createFinishProgress({
|
|
|
130
130
|
: heartbeat;
|
|
131
131
|
const clock = typeof now === 'function' ? now : Date.now;
|
|
132
132
|
const eventStream = persistEvents ? createEventStream(repoRoot, branch, baseBranch) : null;
|
|
133
|
+
if (quiet && eventStream) {
|
|
134
|
+
sink(`[gx:finish] ${JSON.stringify({ runId: eventStream.runId, eventFile: eventStream.relativePath })}`);
|
|
135
|
+
}
|
|
133
136
|
const stageMap = new Map(STAGES.map(([id, label], index) => [id, {
|
|
134
137
|
index: index + 1,
|
|
135
138
|
label,
|
|
@@ -267,6 +270,26 @@ function readFinishEvents(filePath) {
|
|
|
267
270
|
}
|
|
268
271
|
}
|
|
269
272
|
|
|
273
|
+
function persistFinishOutput(eventFile, stdout, stderr) {
|
|
274
|
+
const name = path.basename(eventFile);
|
|
275
|
+
if (!/^finish-[a-z0-9]+-[0-9]+-[a-f0-9]{8}\.jsonl$/.test(name)) {
|
|
276
|
+
throw new Error('unsafe finish log name');
|
|
277
|
+
}
|
|
278
|
+
const root = path.resolve(path.dirname(eventFile), '../../..');
|
|
279
|
+
const directory = createPrivateDirectory(root, ['.omx', 'state', 'finish-runs']);
|
|
280
|
+
if (path.resolve(eventFile) !== path.join(directory, name)) throw new Error('unsafe finish log path');
|
|
281
|
+
const file = path.join(directory, `${name}.output.log`);
|
|
282
|
+
const fd = fs.openSync(file, fs.constants.O_WRONLY | fs.constants.O_CREAT
|
|
283
|
+
| fs.constants.O_EXCL | fs.constants.O_NOFOLLOW, 0o600);
|
|
284
|
+
try {
|
|
285
|
+
fs.fchmodSync(fd, 0o600);
|
|
286
|
+
fs.writeFileSync(fd, `--- stdout ---\n${stdout}\n--- stderr ---\n${stderr}`);
|
|
287
|
+
} finally {
|
|
288
|
+
fs.closeSync(fd);
|
|
289
|
+
}
|
|
290
|
+
return file;
|
|
291
|
+
}
|
|
292
|
+
|
|
270
293
|
/** Build one bounded machine-readable result from a captured finish run. */
|
|
271
294
|
function summarizeFinishRun({
|
|
272
295
|
eventFile,
|
|
@@ -296,6 +319,19 @@ function summarizeFinishRun({
|
|
|
296
319
|
stages,
|
|
297
320
|
};
|
|
298
321
|
if (pr) result.pr = pr;
|
|
322
|
+
const warnings = combined.split('\n').map((line) => line.trim())
|
|
323
|
+
.filter((line) => /\bwarn(?:ing)?\b|⚠|retained|manual (?:cleanup|repair)/i.test(line));
|
|
324
|
+
if (warnings.length) {
|
|
325
|
+
result.warningCount = warnings.length;
|
|
326
|
+
result.warnings = warnings.slice(-10).map((line) => line.slice(0, 500));
|
|
327
|
+
}
|
|
328
|
+
if (eventFile && (status !== 0 || warnings.length)) {
|
|
329
|
+
try {
|
|
330
|
+
result.logFile = persistFinishOutput(eventFile, stdout, stderr);
|
|
331
|
+
} catch (_error) {
|
|
332
|
+
result.logWarning = 'Could not persist full finish output; bounded diagnostics follow';
|
|
333
|
+
}
|
|
334
|
+
}
|
|
299
335
|
if (status !== 0) {
|
|
300
336
|
const lines = combined.split('\n').map((line) => line.trim()).filter(Boolean);
|
|
301
337
|
const tail = lines.slice(-12).join('\n');
|