@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.
Files changed (97) hide show
  1. package/CONTRIBUTING.md +13 -4
  2. package/README.md +223 -1
  3. package/THIRD-PARTY-NOTICES.md +29 -0
  4. package/bin/multiagent-safety.js +9 -12
  5. package/package.json +4 -2
  6. package/skills/gitguardex/SKILL.md +28 -0
  7. package/skills/pr-lens/SKILL.md +87 -0
  8. package/src/abide.js +534 -0
  9. package/src/agents/activity.js +23 -4
  10. package/src/agents/inspect.js +15 -6
  11. package/src/agents/launch.js +6 -1
  12. package/src/agents/process-lease.js +84 -0
  13. package/src/agents/sessions.js +96 -16
  14. package/src/agents/start.js +18 -9
  15. package/src/agents/status.js +35 -14
  16. package/src/agents/supervise.js +87 -0
  17. package/src/budget/index.js +6 -1
  18. package/src/cli/args.js +71 -77
  19. package/src/cli/commands/agents.js +38 -10
  20. package/src/cli/commands/branch.js +116 -103
  21. package/src/cli/commands/claude.js +604 -50
  22. package/src/cli/commands/codex.js +359 -0
  23. package/src/cli/commands/context.js +52 -0
  24. package/src/cli/commands/mcp.js +5 -1
  25. package/src/cli/commands/pr.js +138 -3
  26. package/src/cli/commands/review.js +3 -1
  27. package/src/cli/commands/setup.js +25 -0
  28. package/src/cli/commands/status.js +128 -60
  29. package/src/cli/commands/storage.js +24 -0
  30. package/src/cli/commands/usage.js +48 -0
  31. package/src/cli/commands/watch.js +157 -47
  32. package/src/cli/commands/worktree-copy-ignored.js +38 -0
  33. package/src/cli/commands/worktree-estimate.js +69 -0
  34. package/src/cli/commands/worktree-hooks.js +62 -0
  35. package/src/cli/commands/worktree-navigation.js +71 -0
  36. package/src/cli/commands/worktree-provision.js +92 -0
  37. package/src/cli/main.js +229 -110
  38. package/src/cli/shared/gate-flags.js +131 -0
  39. package/src/cli/shared/scaffolding.js +31 -3
  40. package/src/cli/shared-locks.js +3 -1
  41. package/src/cockpit/pane-actions.js +43 -7
  42. package/src/context.js +373 -17
  43. package/src/doctor/index.js +29 -35
  44. package/src/finish/events.js +254 -0
  45. package/src/finish/index.js +322 -62
  46. package/src/finish/post-branch-finish-cleanup.js +316 -22
  47. package/src/finish/preflight.js +44 -1
  48. package/src/finish/progress.js +36 -0
  49. package/src/finish/review-gate.js +317 -127
  50. package/src/finish/review-receipt.js +156 -0
  51. package/src/finish/with-claims-lock.py +29 -0
  52. package/src/git/index.js +179 -42
  53. package/src/hooks/index.js +68 -3
  54. package/src/mcp/collect.js +61 -5
  55. package/src/mcp/server.js +4 -0
  56. package/src/output/index.js +167 -1
  57. package/src/pr-lens-publish.js +93 -0
  58. package/src/pr-lens.js +184 -0
  59. package/src/pr-respond.js +860 -0
  60. package/src/pr-review.js +519 -159
  61. package/src/pr.js +73 -14
  62. package/src/provider-binary.js +21 -0
  63. package/src/review-diff-context.js +90 -0
  64. package/src/review-fix.js +23 -17
  65. package/src/scaffold/agent-worktree-prep.js +14 -12
  66. package/src/scaffold/dependency-store.js +234 -0
  67. package/src/scaffold/index.js +13 -1
  68. package/src/scaffold/provision-approvals.js +83 -0
  69. package/src/scaffold/provision-config.js +169 -40
  70. package/src/shared-git-state.js +11 -1
  71. package/src/speckit/index.js +12 -1
  72. package/src/storage/run-artifacts.js +185 -0
  73. package/src/toolchain/index.js +322 -2
  74. package/src/worktree-copy-ignored.js +200 -0
  75. package/src/worktree-hook-worker.js +29 -0
  76. package/src/worktree-hooks.js +333 -0
  77. package/src/worktree-navigation.js +248 -0
  78. package/src/worktree-picker.js +89 -0
  79. package/src/worktree-quota.js +100 -0
  80. package/src/worktree-start.js +100 -0
  81. package/templates/AGENTS.multiagent-safety.md +8 -0
  82. package/templates/AGENTS.multiagent-safety.min.md +3 -1
  83. package/templates/codex/skills/gitguardex/SKILL.md +28 -0
  84. package/templates/codex/skills/opensrc/SKILL.md +24 -0
  85. package/templates/githooks/post-merge +16 -2
  86. package/templates/scripts/agent-autofinish-watch.sh +17 -18
  87. package/templates/scripts/agent-branch-finish.sh +180 -19
  88. package/templates/scripts/agent-branch-merge.sh +7 -32
  89. package/templates/scripts/agent-branch-start.sh +202 -43
  90. package/templates/scripts/agent-claude-stop-finish.sh +4 -27
  91. package/templates/scripts/agent-file-locks.py +257 -29
  92. package/templates/scripts/agent-preflight.sh +3 -0
  93. package/templates/scripts/agent-worktree-prune.sh +975 -63
  94. package/templates/scripts/codex-agent.sh +55 -5
  95. package/templates/scripts/lib/guardex-base-branch.sh +207 -0
  96. package/templates/scripts/openspec/init-change-workspace.sh +10 -18
  97. package/templates/scripts/review-bot-watch.sh +171 -4
package/src/cli/args.js CHANGED
@@ -5,6 +5,7 @@ const {
5
5
  envFlagIsTruthy,
6
6
  } = require('../context');
7
7
  const { DEFAULT_NESTED_REPO_MAX_DEPTH } = require('../git');
8
+ const { applyGateFlagAt } = require('./shared/gate-flags');
8
9
 
9
10
  function requireValue(rawArgs, index, flagName) {
10
11
  const value = rawArgs[index + 1];
@@ -289,7 +290,6 @@ function parsePrReviewArgs(rawArgs) {
289
290
  const parsed = parseTargetFlag(rawArgs, process.cwd());
290
291
  const options = {
291
292
  target: parsed.target,
292
- provider: 'codex',
293
293
  pr: '',
294
294
  post: false,
295
295
  artifact: '',
@@ -301,6 +301,15 @@ function parsePrReviewArgs(rawArgs) {
301
301
 
302
302
  for (let index = 0; index < parsed.args.length; index += 1) {
303
303
  const arg = parsed.args[index];
304
+ if (arg === '--pr-lens' || arg === '--no-pr-lens') {
305
+ options.prLens = arg === '--pr-lens';
306
+ continue;
307
+ }
308
+ if (arg === '--pr-lens-publish') {
309
+ options.prLensPublish = requireValue(parsed.args, index, arg);
310
+ index += 1;
311
+ continue;
312
+ }
304
313
  if (arg === '--provider') {
305
314
  const next = requireValue(parsed.args, index, '--provider');
306
315
  if (!['codex', 'claude'].includes(next)) {
@@ -323,6 +332,14 @@ function parsePrReviewArgs(rawArgs) {
323
332
  options.post = false;
324
333
  continue;
325
334
  }
335
+ if (arg === '--abide' || arg === '--no-abide') {
336
+ options.abide = arg === '--abide';
337
+ continue;
338
+ }
339
+ if (arg === '--agent-quiet') {
340
+ options.agentQuiet = true;
341
+ continue;
342
+ }
326
343
  if (arg === '--fix') {
327
344
  options.fix = true;
328
345
  continue;
@@ -377,6 +394,7 @@ function parseAgentsArgs(rawArgs) {
377
394
  dryRun: false,
378
395
  reviewIntervalSeconds: 30,
379
396
  cleanupIntervalSeconds: 60,
397
+ cleanupOnly: false,
380
398
  idleMinutes: DEFAULT_SHADOW_CLEANUP_IDLE_MINUTES,
381
399
  staleAgeMinutes: 24 * 60,
382
400
  pid: null,
@@ -487,6 +505,10 @@ function parseAgentsArgs(rawArgs) {
487
505
  index += 1;
488
506
  continue;
489
507
  }
508
+ if (arg === '--dedupe-surface') {
509
+ options.dedupeSurface = true;
510
+ continue;
511
+ }
490
512
  if (arg === '--worktree') {
491
513
  const next = rest[index + 1];
492
514
  if (!next) {
@@ -534,6 +556,10 @@ function parseAgentsArgs(rawArgs) {
534
556
  index += 1;
535
557
  continue;
536
558
  }
559
+ if (arg === '--cleanup-only') {
560
+ options.cleanupOnly = true;
561
+ continue;
562
+ }
537
563
  if (arg === '--cleanup-interval') {
538
564
  const next = rest[index + 1];
539
565
  if (!next) {
@@ -746,6 +772,9 @@ function parseAgentsArgs(rawArgs) {
746
772
  if (options.subcommand === 'set-status' && !options.activity) {
747
773
  throw new Error('gx agents set-status requires --activity <working|waiting|done|idle>');
748
774
  }
775
+ if (options.dedupeSurface && options.subcommand !== 'set-status') {
776
+ throw new Error('--dedupe-surface is only supported with `gx agents set-status`');
777
+ }
749
778
  if (
750
779
  options.json &&
751
780
  !['status', 'files', 'diff', 'locks', 'cleanup-sessions', 'finish', 'send', 'inbox'].includes(options.subcommand) &&
@@ -999,7 +1028,10 @@ function parseSyncArgs(rawArgs) {
999
1028
  return options;
1000
1029
  }
1001
1030
 
1031
+ const CLEANUP_WATCH_MAX_FAILURES_DEFAULT = 10;
1032
+
1002
1033
  function parseCleanupArgs(rawArgs) {
1034
+ const envMaxFailures = Number.parseInt(process.env.GUARDEX_CLEANUP_WATCH_MAX_FAILURES, 10);
1003
1035
  const options = {
1004
1036
  target: process.cwd(),
1005
1037
  base: '',
@@ -1009,12 +1041,23 @@ function parseCleanupArgs(rawArgs) {
1009
1041
  keepRemote: false,
1010
1042
  keepCleanWorktrees: true,
1011
1043
  includeCleanLinkedWorktrees: false,
1012
- includePrMerged: false,
1044
+ includeMergedLinkedWorktrees: false,
1045
+ // Squash/rebase merges leave the branch tip off the base's history, so
1046
+ // ancestry alone reports a merged PR's branch as unmerged and cleanup
1047
+ // never reclaims it. Consult merged PRs by default; `gh` missing or failing
1048
+ // disables the lookup (fail-closed: nothing is treated as merged).
1049
+ includePrMerged: true,
1013
1050
  idleMinutes: 0,
1014
1051
  watch: false,
1015
1052
  intervalSeconds: 60,
1016
1053
  once: false,
1017
1054
  maxBranches: 0,
1055
+ // How many consecutive prune failures trigger a non-zero exit in watch mode.
1056
+ // Overridable via --max-failures or GUARDEX_CLEANUP_WATCH_MAX_FAILURES env var.
1057
+ maxFailures:
1058
+ Number.isInteger(envMaxFailures) && envMaxFailures >= 1
1059
+ ? envMaxFailures
1060
+ : CLEANUP_WATCH_MAX_FAILURES_DEFAULT,
1018
1061
  };
1019
1062
 
1020
1063
  for (let index = 0; index < rawArgs.length; index += 1) {
@@ -1070,10 +1113,18 @@ function parseCleanupArgs(rawArgs) {
1070
1113
  options.includeCleanLinkedWorktrees = true;
1071
1114
  continue;
1072
1115
  }
1116
+ if (arg === '--include-merged-linked-worktrees') {
1117
+ options.includeMergedLinkedWorktrees = true;
1118
+ continue;
1119
+ }
1073
1120
  if (arg === '--include-pr-merged') {
1074
1121
  options.includePrMerged = true;
1075
1122
  continue;
1076
1123
  }
1124
+ if (arg === '--no-include-pr-merged') {
1125
+ options.includePrMerged = false;
1126
+ continue;
1127
+ }
1077
1128
  if (arg === '--idle-minutes') {
1078
1129
  const next = rawArgs[index + 1];
1079
1130
  if (!next) {
@@ -1121,6 +1172,19 @@ function parseCleanupArgs(rawArgs) {
1121
1172
  index += 1;
1122
1173
  continue;
1123
1174
  }
1175
+ if (arg === '--max-failures') {
1176
+ const next = rawArgs[index + 1];
1177
+ if (!next) {
1178
+ throw new Error('--max-failures requires an integer value');
1179
+ }
1180
+ const parsed = Number.parseInt(next, 10);
1181
+ if (!Number.isInteger(parsed) || parsed < 1) {
1182
+ throw new Error('--max-failures must be an integer >= 1');
1183
+ }
1184
+ options.maxFailures = parsed;
1185
+ index += 1;
1186
+ continue;
1187
+ }
1124
1188
  throw new Error(`Unknown option: ${arg}`);
1125
1189
  }
1126
1190
 
@@ -1239,7 +1303,7 @@ function parseFinishArgs(rawArgs, defaults = {}) {
1239
1303
  // (not undefined) also wins over the env toggle, since `??` only falls
1240
1304
  // through on null/undefined.
1241
1305
  gateReview: fastMode ? false : (defaults.gateReview ?? autoShip),
1242
- reviewProvider: defaults.reviewProvider || 'codex',
1306
+ reviewProvider: defaults.reviewProvider,
1243
1307
  // Empty = the provider's own default model.
1244
1308
  reviewModel: defaults.reviewModel || '',
1245
1309
  reviewTimeoutMs: defaults.reviewTimeoutMs,
@@ -1254,6 +1318,7 @@ function parseFinishArgs(rawArgs, defaults = {}) {
1254
1318
  // them. Off by default: this writes commits to the branch.
1255
1319
  gateAutofix: false,
1256
1320
  gateAutofixRounds: 1,
1321
+ gateAbide: defaults.gateAbide ?? true,
1257
1322
  // After a bulk `--all` finish, sweep merged-but-stranded worktree dirs whose
1258
1323
  // branch was merged out-of-band and never reaped (the post-merge "retained"
1259
1324
  // gap). Only fires for `--all`; opt out with --no-sweep-orphans.
@@ -1389,79 +1454,9 @@ function parseFinishArgs(rawArgs, defaults = {}) {
1389
1454
  options.skipPreflight = true;
1390
1455
  continue;
1391
1456
  }
1392
- if (arg === '--gate-review') {
1393
- options.gateReview = true;
1394
- continue;
1395
- }
1396
- if (arg === '--no-gate-review' || arg === '--skip-review-gate') {
1397
- options.gateReview = false;
1398
- continue;
1399
- }
1400
- if (arg === '--allow-no-checks') {
1401
- options.allowNoChecks = true;
1402
- continue;
1403
- }
1404
- if (arg === '--gate-baseline') {
1405
- options.gateBaseline = true;
1406
- continue;
1407
- }
1408
- if (arg === '--no-gate-baseline') {
1409
- options.gateBaseline = false;
1410
- continue;
1411
- }
1412
- if (arg === '--gate-autofix') {
1413
- options.gateAutofix = true;
1414
- continue;
1415
- }
1416
- if (arg === '--no-gate-autofix') {
1417
- options.gateAutofix = false;
1418
- continue;
1419
- }
1420
- if (arg === '--gate-autofix-rounds') {
1421
- const raw = rawArgs[index + 1];
1422
- const rounds = Number.parseInt(String(raw ?? ''), 10);
1423
- if (!Number.isInteger(rounds) || rounds < 1 || rounds > 5) {
1424
- throw new Error('--gate-autofix-rounds requires an integer between 1 and 5');
1425
- }
1426
- options.gateAutofixRounds = rounds;
1427
- options.gateAutofix = true;
1428
- index += 1;
1429
- continue;
1430
- }
1431
- if (arg === '--review-provider') {
1432
- const next = rawArgs[index + 1];
1433
- if (!next || !['codex', 'claude'].includes(next)) {
1434
- throw new Error('--review-provider requires a value of codex|claude');
1435
- }
1436
- options.reviewProvider = next;
1437
- index += 1;
1438
- continue;
1439
- }
1440
- if (arg === '--review-model') {
1441
- const next = String(rawArgs[index + 1] ?? '').trim();
1442
- if (!next || next.startsWith('-')) {
1443
- throw new Error('--review-model requires a model name (e.g. sonnet)');
1444
- }
1445
- options.reviewModel = next;
1446
- index += 1;
1447
- continue;
1448
- }
1449
- if (arg === '--review-timeout-ms') {
1450
- const raw = rawArgs[index + 1];
1451
- const timeoutMs = Number.parseInt(String(raw ?? ''), 10);
1452
- if (!Number.isInteger(timeoutMs) || timeoutMs <= 0) {
1453
- throw new Error('--review-timeout-ms requires a positive integer');
1454
- }
1455
- options.reviewTimeoutMs = timeoutMs;
1456
- index += 1;
1457
- continue;
1458
- }
1459
- if (arg === '--gate-serial-ci') {
1460
- options.gateSerialCi = true;
1461
- continue;
1462
- }
1463
- if (arg === '--no-gate-serial-ci') {
1464
- options.gateSerialCi = false;
1457
+ const consumed = applyGateFlagAt(rawArgs, index, options);
1458
+ if (consumed > 0) {
1459
+ index += consumed - 1;
1465
1460
  continue;
1466
1461
  }
1467
1462
  throw new Error(`Unknown option: ${arg}`);
@@ -1472,7 +1467,6 @@ function parseFinishArgs(rawArgs, defaults = {}) {
1472
1467
  '--gate-review',
1473
1468
  '--gate-autofix',
1474
1469
  '--gate-autofix-rounds',
1475
- '--preflight',
1476
1470
  '--direct-only',
1477
1471
  ].includes(arg));
1478
1472
  const modeIndex = rawArgs.indexOf('--mode');
@@ -36,10 +36,18 @@ function readAgentsState(repoRoot) {
36
36
  }
37
37
  }
38
38
 
39
+ function writeJsonAtomic(filePath, value) {
40
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
41
+ const tempPath = path.join(
42
+ path.dirname(filePath),
43
+ `.${path.basename(filePath)}.${process.pid}.${Date.now()}.tmp`,
44
+ );
45
+ fs.writeFileSync(tempPath, `${JSON.stringify(value, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 });
46
+ fs.renameSync(tempPath, filePath);
47
+ }
48
+
39
49
  function writeAgentsState(repoRoot, state) {
40
- const statePath = agentsStatePathForRepo(repoRoot);
41
- fs.mkdirSync(path.dirname(statePath), { recursive: true });
42
- fs.writeFileSync(statePath, `${JSON.stringify(state, null, 2)}\n`, 'utf8');
50
+ writeJsonAtomic(agentsStatePathForRepo(repoRoot), state);
43
51
  }
44
52
 
45
53
  function processAlive(pid) {
@@ -254,7 +262,15 @@ function agents(rawArgs) {
254
262
  const reviewRunning = processAlive(existingReviewPid);
255
263
  const cleanupRunning = processAlive(existingCleanupPid);
256
264
 
257
- if (reviewRunning && cleanupRunning) {
265
+ // --cleanup-only: run just the cleanup bot. The review bot spends model
266
+ // tokens on every PR; reclaiming merged lanes should not require it.
267
+ const reviewWanted = !options.cleanupOnly;
268
+ if ((reviewRunning || !reviewWanted) && cleanupRunning) {
269
+ if (!reviewWanted) {
270
+ console.log(`[${TOOL_NAME}] Cleanup bot already running (pid=${existingCleanupPid}).`);
271
+ process.exitCode = 0;
272
+ return;
273
+ }
258
274
  console.log(
259
275
  `[${TOOL_NAME}] Repo agents already running (review pid=${existingReviewPid}, cleanup pid=${existingCleanupPid}).`,
260
276
  );
@@ -271,7 +287,7 @@ function agents(rawArgs) {
271
287
  let reusedAny = false;
272
288
 
273
289
  const mainScriptPath = require.resolve('../main.js');
274
- if (!reviewRunning) {
290
+ if (reviewWanted && !reviewRunning) {
275
291
  reviewPid = spawnDetachedAgentProcess({
276
292
  command: process.execPath,
277
293
  args: [
@@ -288,7 +304,7 @@ function agents(rawArgs) {
288
304
  logPath: reviewLogPath,
289
305
  });
290
306
  startedAny = true;
291
- } else {
307
+ } else if (reviewRunning) {
292
308
  reusedAny = true;
293
309
  }
294
310
 
@@ -306,6 +322,10 @@ function agents(rawArgs) {
306
322
  '--idle-minutes',
307
323
  String(options.idleMinutes),
308
324
  '--prune-clean-worktrees',
325
+ // Agents often open lanes as sibling dirs (../repo-wt-*) instead of
326
+ // the managed agent dirs; reclaim those too once MERGED. Never
327
+ // clean-but-unmerged, detached or non-agent ones.
328
+ '--include-merged-linked-worktrees',
309
329
  ],
310
330
  cwd: repoRoot,
311
331
  logPath: cleanupLogPath,
@@ -333,7 +353,7 @@ function agents(rawArgs) {
333
353
  repoRoot,
334
354
  startedAt: new Date().toISOString(),
335
355
  review: {
336
- pid: reviewPid,
356
+ pid: reviewWanted || reviewRunning ? reviewPid : null,
337
357
  intervalSeconds: reviewIntervalSeconds,
338
358
  script: mainScriptPath,
339
359
  logPath: reviewLogPath,
@@ -348,12 +368,18 @@ function agents(rawArgs) {
348
368
  });
349
369
 
350
370
  console.log(
351
- `[${TOOL_NAME}] Started repo agents in ${repoRoot} (review pid=${reviewPid}, cleanup pid=${cleanupPid}).`,
371
+ reviewWanted
372
+ ? `[${TOOL_NAME}] Started repo agents in ${repoRoot} (review pid=${reviewPid}, cleanup pid=${cleanupPid}).`
373
+ : reviewRunning
374
+ ? `[${TOOL_NAME}] Started the cleanup bot in ${repoRoot} (cleanup pid=${cleanupPid}); the review bot already running (pid=${existingReviewPid}) is left as is.`
375
+ : `[${TOOL_NAME}] Started the cleanup bot in ${repoRoot} (cleanup pid=${cleanupPid}; review bot not started: --cleanup-only).`,
352
376
  );
353
377
  if (reusedAny && startedAny) {
354
378
  console.log(`[${TOOL_NAME}] Reused healthy bot process(es) and started only missing ones.`);
355
379
  }
356
- console.log(`[${TOOL_NAME}] Logs: ${reviewLogPath}, ${cleanupLogPath}`);
380
+ console.log(
381
+ `[${TOOL_NAME}] Logs: ${reviewWanted || reviewRunning ? `${reviewLogPath}, ` : ''}${cleanupLogPath}`,
382
+ );
357
383
  process.exitCode = 0;
358
384
  return;
359
385
  }
@@ -390,10 +416,12 @@ function agents(rawArgs) {
390
416
  return;
391
417
  }
392
418
 
393
- process.stdout.write(agentStatus.runStatusCommand(repoRoot, options));
419
+ agentStatus.writeStatusCommand(repoRoot, options);
394
420
  process.exitCode = 0;
395
421
  }
396
422
 
397
423
  module.exports = {
398
424
  agents,
425
+ writeAgentsState,
426
+ agentsStatePathForRepo,
399
427
  };