@imdeadpool/guardex 7.0.43 → 7.1.1

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 (68) hide show
  1. package/README.md +35 -0
  2. package/package.json +4 -1
  3. package/scripts/prepublish-bump-version.js +191 -0
  4. package/skills/gx-act/SKILL.md +82 -0
  5. package/src/agents/activity.js +279 -0
  6. package/src/agents/inspect.js +17 -4
  7. package/src/agents/launch.js +10 -1
  8. package/src/agents/status.js +9 -6
  9. package/src/budget/index.js +2 -1
  10. package/src/cli/args.js +118 -5
  11. package/src/cli/commands/agents.js +381 -0
  12. package/src/cli/commands/bootstrap.js +92 -0
  13. package/src/cli/commands/branch.js +127 -0
  14. package/src/cli/commands/claude.js +674 -0
  15. package/src/cli/commands/doctor.js +268 -0
  16. package/src/cli/commands/finish.js +26 -0
  17. package/src/cli/commands/mcp.js +122 -0
  18. package/src/cli/commands/misc.js +304 -0
  19. package/src/cli/commands/pr.js +439 -0
  20. package/src/cli/commands/prompt.js +92 -0
  21. package/src/cli/commands/release.js +305 -0
  22. package/src/cli/commands/report.js +244 -0
  23. package/src/cli/commands/review.js +32 -0
  24. package/src/cli/commands/setup.js +242 -0
  25. package/src/cli/commands/status.js +338 -0
  26. package/src/cli/commands/watch.js +234 -0
  27. package/src/cli/main.js +68 -3726
  28. package/src/cli/shared/repo-env.js +161 -0
  29. package/src/cli/shared/sandbox.js +417 -0
  30. package/src/cli/shared/scaffolding.js +535 -0
  31. package/src/cli/shared/toolchain-shims.js +420 -0
  32. package/src/context.js +229 -11
  33. package/src/core/runtime.js +6 -1
  34. package/src/doctor/index.js +42 -13
  35. package/src/finish/index.js +147 -5
  36. package/src/finish/preflight.js +177 -0
  37. package/src/finish/review-gate.js +182 -0
  38. package/src/git/index.js +446 -4
  39. package/src/hooks/index.js +0 -64
  40. package/src/mcp/collect.js +394 -0
  41. package/src/mcp/server.js +163 -0
  42. package/src/output/index.js +67 -1
  43. package/src/pr-review.js +23 -0
  44. package/src/pr.js +381 -0
  45. package/src/sandbox/index.js +13 -2
  46. package/src/scaffold/agent-worktree-prep.js +220 -0
  47. package/src/scaffold/index.js +108 -10
  48. package/src/scaffold/provision-config.js +314 -0
  49. package/src/speckit/index.js +226 -0
  50. package/src/terminal/index.js +1 -76
  51. package/src/terminal/kitty.js +20 -0
  52. package/src/terminal/tmux.js +9 -1
  53. package/src/toolchain/index.js +20 -0
  54. package/templates/AGENTS.monorepo-apps.md +26 -0
  55. package/templates/AGENTS.multiagent-safety.md +68 -344
  56. package/templates/AGENTS.multiagent-safety.min.md +11 -0
  57. package/templates/codex/skills/gx-act/SKILL.md +82 -0
  58. package/templates/githooks/pre-commit +22 -19
  59. package/templates/scripts/agent-branch-finish.sh +17 -33
  60. package/templates/scripts/agent-branch-merge.sh +4 -1
  61. package/templates/scripts/agent-branch-start.sh +115 -6
  62. package/templates/scripts/agent-file-locks.py +245 -50
  63. package/templates/scripts/agent-preflight.sh +31 -5
  64. package/templates/scripts/agent-worktree-prune.sh +1 -1
  65. package/templates/scripts/codex-agent.sh +0 -91
  66. package/src/agents/detect.js +0 -160
  67. package/src/cockpit/keybindings.js +0 -224
  68. package/src/cockpit/layout.js +0 -224
@@ -32,6 +32,48 @@ function supportsAnsiColors() {
32
32
  return Boolean(process.stdout.isTTY) && process.env.TERM !== 'dumb';
33
33
  }
34
34
 
35
+ // envTruthy returns true when an env var is set to a truthy-ish string. Used
36
+ // by isTerseMode so callers can flip terse / verbose without parsing flags.
37
+ function envTruthy(name) {
38
+ const value = String(process.env[name] || '').trim().toLowerCase();
39
+ return value === '1' || value === 'true' || value === 'yes' || value === 'on';
40
+ }
41
+
42
+ // argvHasFlag scans the current process argv for a literal flag. This avoids
43
+ // re-parsing every command's argument table just to learn whether the caller
44
+ // asked for verbose / terse output.
45
+ function argvHasFlag(flag) {
46
+ const argv = process.argv || [];
47
+ for (let index = 2; index < argv.length; index += 1) {
48
+ if (argv[index] === flag) {
49
+ return true;
50
+ }
51
+ }
52
+ return false;
53
+ }
54
+
55
+ // isTerseMode collapses noisy human-friendly narration when stdout is being
56
+ // piped (typical for AI coding agents that read CLI output into a conversation
57
+ // transcript). Explicit verbose flags / env vars always win so operators can
58
+ // recover full output. Decorative blank lines, banners, and "[OK] default
59
+ // chosen" confirmations should be gated through this helper; errors, blockers,
60
+ // PR URLs, branch names, and file paths must stay visible in both modes.
61
+ function isTerseMode() {
62
+ if (envTruthy('GUARDEX_VERBOSE')) {
63
+ return false;
64
+ }
65
+ if (argvHasFlag('--verbose')) {
66
+ return false;
67
+ }
68
+ if (envTruthy('GUARDEX_TERSE')) {
69
+ return true;
70
+ }
71
+ if (argvHasFlag('--terse')) {
72
+ return true;
73
+ }
74
+ return !process.stdout.isTTY;
75
+ }
76
+
35
77
  function colorize(text, colorCode) {
36
78
  if (!supportsAnsiColors()) {
37
79
  return text;
@@ -231,7 +273,9 @@ function getInvokedCliName() {
231
273
 
232
274
  function printToolLogsSummary(options = {}) {
233
275
  const invoked = options.invokedBasename || getInvokedCliName();
234
- const compact = Boolean(options.compact);
276
+ // Terse mode collapses the full help tree into a single hint line so agents
277
+ // reading non-TTY output don't pay for the decorative banners.
278
+ const compact = Boolean(options.compact) || isTerseMode();
235
279
 
236
280
  if (compact) {
237
281
  const helpLine = `Try '${invoked} help' for commands, or '${invoked} status --verbose' for full service details.`;
@@ -320,6 +364,27 @@ function usage(options = {}) {
320
364
  const { outsideGitRepo = false } = options;
321
365
  const invoked = options.invokedBasename || getInvokedCliName();
322
366
 
367
+ // In terse mode (default when stdout is non-TTY, e.g. agents piping output),
368
+ // drop the long NOTES / VERSION / QUICKSTART / REPO TOGGLE sections and emit
369
+ // just usage + command catalog. Errors and the outside-git-repo hint stay so
370
+ // agents still see actionable next steps.
371
+ if (isTerseMode()) {
372
+ const groupedCommandLinesTerse = groupedCommandCatalogLines(' ', {
373
+ colorizeLabel: (text) => text,
374
+ })
375
+ .map((line) => (line == null ? '' : line))
376
+ .join('\n');
377
+ console.log(`USAGE: ${invoked} <command> [options]
378
+ COMMANDS
379
+ ${groupedCommandLinesTerse}`);
380
+ if (outsideGitRepo) {
381
+ console.log(
382
+ `[${TOOL_NAME}] No git repository detected. Re-run from a repo root or pass --target <path>.`,
383
+ );
384
+ }
385
+ return;
386
+ }
387
+
323
388
  const groupedCommandLines = groupedCommandCatalogLines(' ', {
324
389
  colorizeLabel: (text) => colorize(text, '1;36'),
325
390
  })
@@ -578,6 +643,7 @@ function printAutoFinishSummary(summary, options = {}) {
578
643
  module.exports = {
579
644
  runtimeVersion,
580
645
  supportsAnsiColors,
646
+ isTerseMode,
581
647
  colorize,
582
648
  doctorOutputColorCode,
583
649
  colorizeDoctorOutput,
package/src/pr-review.js CHANGED
@@ -153,6 +153,12 @@ function runProviderReview(provider, diff, repoRoot, timeoutMs, runner = run) {
153
153
  if (result.status !== 0) {
154
154
  throw new Error(`${provider} review failed${result.stderr ? `\n${result.stderr.trim()}` : ''}`);
155
155
  }
156
+ // A compliant provider always emits the findings JSON object (empty array when
157
+ // nothing is wrong). Empty stdout means the review did NOT run — fail closed so
158
+ // a silent no-op is never mistaken for "clean" by the merge gate.
159
+ if (!(result.stdout || '').trim()) {
160
+ throw new Error(`${provider} review returned no output (review did not run)`);
161
+ }
156
162
  return normalizeFindings(result.stdout || '');
157
163
  }
158
164
 
@@ -218,6 +224,22 @@ function runPrReview(options, deps = {}) {
218
224
  return { posted: true, artifactPath: '', findings };
219
225
  }
220
226
 
227
+ /**
228
+ * Decide whether a set of review findings clears the merge gate. Blocks on any
229
+ * finding whose severity is in `blockSeverities` (high + critical by default;
230
+ * low/medium are advisory). Pure + synchronous so it is trivially testable.
231
+ *
232
+ * Fail-closed semantics live in the CALLER: an absent/empty `findings` here is
233
+ * treated as clean, so the caller must independently treat a review that did
234
+ * NOT run (provider threw / timed out) as a block, never as clean.
235
+ */
236
+ function evaluateReviewGate(findings, { blockSeverities = ['high', 'critical'] } = {}) {
237
+ const block = new Set(blockSeverities.map((s) => String(s).toLowerCase()));
238
+ const list = Array.isArray(findings) ? findings : [];
239
+ const blocking = list.filter((f) => f && block.has(String(f.severity).toLowerCase()));
240
+ return { clean: blocking.length === 0, blocking };
241
+ }
242
+
221
243
  function printPrReviewResult(result) {
222
244
  if (result.posted) {
223
245
  console.log(`${TOOL_PREFIX} Posted PR review with ${result.findings.length} finding(s).`);
@@ -237,5 +259,6 @@ module.exports = {
237
259
  normalizeFindings,
238
260
  renderMarkdownReview,
239
261
  runPrReview,
262
+ evaluateReviewGate,
240
263
  printPrReviewResult,
241
264
  };
package/src/pr.js ADDED
@@ -0,0 +1,381 @@
1
+ // PR-from-worktree helpers.
2
+ //
3
+ // Centralizes the operations that the agent + worktree flow needs to drive a
4
+ // pull request: detecting the PR for the current branch, opening one
5
+ // idempotently, pushing + syncing, enabling auto-merge, and polling CI / merge
6
+ // state. The agent-branch-finish.sh script historically owned most of this;
7
+ // these helpers expose the same primitives to the JS CLI so `gx pr ...` can be
8
+ // driven without invoking the shell flow.
9
+
10
+ const { GH_BIN } = require('./context');
11
+ const { run } = require('./core/runtime');
12
+ const {
13
+ resolveRepoRoot,
14
+ currentBranchName,
15
+ hasOriginRemote,
16
+ detectDefaultBaseBranch,
17
+ } = require('./git');
18
+
19
+ const DEFAULT_POLL_INTERVAL_MS = 5_000;
20
+ const DEFAULT_POLL_TIMEOUT_MS = 10 * 60 * 1000;
21
+
22
+ class PrError extends Error {
23
+ constructor(message, { code = 'pr-error', cause = null } = {}) {
24
+ super(message);
25
+ this.name = 'PrError';
26
+ this.code = code;
27
+ if (cause) this.cause = cause;
28
+ }
29
+ }
30
+
31
+ function ghAvailable() {
32
+ const result = run(GH_BIN, ['--version'], { allowFailure: true });
33
+ return result.status === 0;
34
+ }
35
+
36
+ function ghAuthStatus() {
37
+ const result = run(GH_BIN, ['auth', 'status'], { allowFailure: true });
38
+ return {
39
+ authenticated: result.status === 0,
40
+ output: ((result.stdout || '') + (result.stderr || '')).trim(),
41
+ };
42
+ }
43
+
44
+ function ghJson(repoRoot, args) {
45
+ const result = run(GH_BIN, args, { cwd: repoRoot, allowFailure: true });
46
+ if (result.status !== 0) {
47
+ return { ok: false, error: (result.stderr || result.stdout || '').trim(), data: null };
48
+ }
49
+ const raw = (result.stdout || '').trim();
50
+ if (!raw) return { ok: true, data: null, error: null };
51
+ try {
52
+ return { ok: true, data: JSON.parse(raw), error: null };
53
+ } catch (error) {
54
+ return { ok: false, data: null, error: `gh JSON parse: ${error.message}` };
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Resolve the PR for `branch` if any exists on the origin remote.
60
+ * Returns null when no PR is open (or any are merged / closed only).
61
+ */
62
+ function findOpenPrForBranch(repoRoot, branch) {
63
+ const result = ghJson(repoRoot, [
64
+ 'pr', 'list',
65
+ '--head', branch,
66
+ '--state', 'open',
67
+ '--json', 'number,url,state,isDraft,mergeable,mergeStateStatus,reviewDecision,headRefName,baseRefName,title,statusCheckRollup',
68
+ '--limit', '5',
69
+ ]);
70
+ if (!result.ok) {
71
+ throw new PrError(`gh pr list failed: ${result.error}`, { code: 'gh-list-failed' });
72
+ }
73
+ const items = Array.isArray(result.data) ? result.data : [];
74
+ if (items.length === 0) return null;
75
+ return items[0];
76
+ }
77
+
78
+ /**
79
+ * Resolve the most recent PR for `branch` regardless of state. Useful when
80
+ * the agent has already merged a previous PR for this branch.
81
+ */
82
+ function findLatestPrForBranch(repoRoot, branch) {
83
+ const result = ghJson(repoRoot, [
84
+ 'pr', 'list',
85
+ '--head', branch,
86
+ '--state', 'all',
87
+ '--json', 'number,url,state,isDraft,mergedAt,headRefName,baseRefName,title',
88
+ '--limit', '5',
89
+ ]);
90
+ if (!result.ok) {
91
+ throw new PrError(`gh pr list failed: ${result.error}`, { code: 'gh-list-failed' });
92
+ }
93
+ const items = Array.isArray(result.data) ? result.data : [];
94
+ if (items.length === 0) return null;
95
+ return items[0];
96
+ }
97
+
98
+ /**
99
+ * List ALL open PRs for the repo on origin in a single gh call, so callers can
100
+ * correlate many branches to their PRs without one gh round-trip per branch.
101
+ * Best-effort: never throws. Returns `{ prs, error }` so callers can tell a
102
+ * genuine "no open PRs" (`error: null`) from a failed lookup (gh missing /
103
+ * unauthenticated / offline → `error` set, `prs: []`).
104
+ * NB: `limit` (default 100) bounds correlation COVERAGE, not just payload size —
105
+ * a repo with more than `limit` open PRs may leave some lanes showing pr:null.
106
+ */
107
+ function listOpenPrsForRepo(repoRoot, { limit = 100 } = {}) {
108
+ const result = ghJson(repoRoot, [
109
+ 'pr', 'list',
110
+ '--state', 'open',
111
+ '--json', 'number,url,state,isDraft,mergeable,mergeStateStatus,reviewDecision,headRefName,baseRefName,title',
112
+ '--limit', String(limit),
113
+ ]);
114
+ if (!result.ok) return { prs: [], error: result.error || 'gh pr list failed' };
115
+ if (!Array.isArray(result.data)) return { prs: [], error: null };
116
+ return { prs: result.data, error: null };
117
+ }
118
+
119
+ function pushBranch(repoRoot, branch, { setUpstream = true } = {}) {
120
+ const args = ['push'];
121
+ if (setUpstream) args.push('-u');
122
+ args.push('origin', branch);
123
+ const result = run('git', args, { cwd: repoRoot, allowFailure: true });
124
+ return {
125
+ ok: result.status === 0,
126
+ output: ((result.stdout || '') + (result.stderr || '')).trim(),
127
+ };
128
+ }
129
+
130
+ function defaultPrTitleFromCommit(repoRoot, branch) {
131
+ const result = run('git', ['log', '-1', '--pretty=%s', branch], {
132
+ cwd: repoRoot, allowFailure: true,
133
+ });
134
+ if (result.status !== 0) return branch;
135
+ const subject = (result.stdout || '').trim();
136
+ return subject || branch;
137
+ }
138
+
139
+ function defaultPrBodyFromCommits(repoRoot, branch, baseBranch) {
140
+ const result = run('git', [
141
+ 'log',
142
+ `${baseBranch}..${branch}`,
143
+ '--pretty=format:- %s',
144
+ ], { cwd: repoRoot, allowFailure: true });
145
+ const list = (result.stdout || '').trim();
146
+ return [
147
+ '## Summary',
148
+ list || '- (no commits between base and branch yet)',
149
+ '',
150
+ '## Test plan',
151
+ '- [ ] verified locally',
152
+ ].join('\n');
153
+ }
154
+
155
+ /**
156
+ * Create (or fetch existing) PR for the current worktree branch. Idempotent:
157
+ * if an open PR is already associated with the branch, returns it without
158
+ * creating a duplicate.
159
+ *
160
+ * @param {object} options
161
+ * @param {string} options.repoRoot
162
+ * @param {string} options.branch
163
+ * @param {string} [options.base]
164
+ * @param {string} [options.title]
165
+ * @param {string} [options.body]
166
+ * @param {boolean} [options.draft]
167
+ * @param {boolean} [options.push]
168
+ */
169
+ function openPullRequest(options) {
170
+ const repoRoot = options.repoRoot;
171
+ const branch = options.branch;
172
+ if (!repoRoot) throw new PrError('repoRoot required', { code: 'bad-arg' });
173
+ if (!branch) throw new PrError('branch required', { code: 'bad-arg' });
174
+
175
+ if (!ghAvailable()) {
176
+ throw new PrError('gh CLI not installed. Install from https://cli.github.com/', {
177
+ code: 'gh-missing',
178
+ });
179
+ }
180
+ const auth = ghAuthStatus();
181
+ if (!auth.authenticated) {
182
+ throw new PrError(
183
+ `GitHub CLI is not authenticated. Run 'gh auth login' or set GITHUB_TOKEN.\n${auth.output}`,
184
+ { code: 'gh-unauthenticated' },
185
+ );
186
+ }
187
+ if (!hasOriginRemote(repoRoot)) {
188
+ throw new PrError('No `origin` remote configured for this repo', {
189
+ code: 'no-origin',
190
+ });
191
+ }
192
+
193
+ // Try to find an existing open PR first.
194
+ const existing = findOpenPrForBranch(repoRoot, branch);
195
+ if (existing) {
196
+ return { created: false, pr: existing };
197
+ }
198
+
199
+ if (options.push !== false) {
200
+ const push = pushBranch(repoRoot, branch);
201
+ if (!push.ok) {
202
+ throw new PrError(`git push failed:\n${push.output}`, { code: 'push-failed' });
203
+ }
204
+ }
205
+
206
+ const base = options.base || detectDefaultBaseBranch(repoRoot);
207
+ const title = options.title || defaultPrTitleFromCommit(repoRoot, branch);
208
+ const body = options.body || defaultPrBodyFromCommits(repoRoot, branch, base);
209
+
210
+ const args = [
211
+ 'pr', 'create',
212
+ '--base', base,
213
+ '--head', branch,
214
+ '--title', title,
215
+ '--body', body,
216
+ ];
217
+ if (options.draft !== false) args.push('--draft');
218
+
219
+ const result = run(GH_BIN, args, { cwd: repoRoot, allowFailure: true });
220
+ if (result.status !== 0) {
221
+ throw new PrError(
222
+ `gh pr create failed:\n${(result.stderr || result.stdout || '').trim()}`,
223
+ { code: 'gh-create-failed' },
224
+ );
225
+ }
226
+
227
+ const created = findOpenPrForBranch(repoRoot, branch);
228
+ if (!created) {
229
+ throw new PrError('PR appears to have been created but could not be located on origin', {
230
+ code: 'pr-lookup-failed',
231
+ });
232
+ }
233
+ return { created: true, pr: created };
234
+ }
235
+
236
+ /**
237
+ * Fetch the live status of the PR for `branch`. Returns null when no open PR
238
+ * exists. Status checks are summarized.
239
+ */
240
+ function getPullRequestStatus(repoRoot, branch) {
241
+ const pr = findOpenPrForBranch(repoRoot, branch);
242
+ if (!pr) return null;
243
+
244
+ const checks = Array.isArray(pr.statusCheckRollup) ? pr.statusCheckRollup : [];
245
+ const summary = checks.reduce(
246
+ (acc, check) => {
247
+ const state = String(check?.conclusion || check?.status || '').toUpperCase();
248
+ if (state === 'SUCCESS') acc.success += 1;
249
+ else if (state === 'FAILURE' || state === 'ERROR' || state === 'TIMED_OUT') acc.failed += 1;
250
+ else if (state === 'PENDING' || state === 'IN_PROGRESS' || state === 'QUEUED') acc.pending += 1;
251
+ else if (state === 'CANCELLED') acc.cancelled += 1;
252
+ else acc.other += 1;
253
+ return acc;
254
+ },
255
+ { success: 0, failed: 0, pending: 0, cancelled: 0, other: 0, total: checks.length },
256
+ );
257
+
258
+ return {
259
+ number: pr.number,
260
+ url: pr.url,
261
+ state: pr.state,
262
+ isDraft: pr.isDraft,
263
+ mergeable: pr.mergeable,
264
+ mergeStateStatus: pr.mergeStateStatus,
265
+ reviewDecision: pr.reviewDecision,
266
+ title: pr.title,
267
+ head: pr.headRefName,
268
+ base: pr.baseRefName,
269
+ checks: summary,
270
+ };
271
+ }
272
+
273
+ function enableAutoMerge(repoRoot, prNumber, { strategy = 'squash' } = {}) {
274
+ const flag = strategy === 'merge'
275
+ ? '--merge'
276
+ : strategy === 'rebase' ? '--rebase' : '--squash';
277
+ const result = run(GH_BIN, [
278
+ 'pr', 'merge', String(prNumber),
279
+ '--auto', flag, '--delete-branch',
280
+ ], { cwd: repoRoot, allowFailure: true });
281
+ return {
282
+ ok: result.status === 0,
283
+ output: ((result.stdout || '') + (result.stderr || '')).trim(),
284
+ };
285
+ }
286
+
287
+ function markPullRequestReady(repoRoot, prNumber) {
288
+ const result = run(GH_BIN, ['pr', 'ready', String(prNumber)], {
289
+ cwd: repoRoot, allowFailure: true,
290
+ });
291
+ return {
292
+ ok: result.status === 0,
293
+ output: ((result.stdout || '') + (result.stderr || '')).trim(),
294
+ };
295
+ }
296
+
297
+ /**
298
+ * Watch a PR's CI + merge state until it merges, fails, or times out.
299
+ *
300
+ * Returns a structured result; does not throw on non-success states. Used by
301
+ * `gx pr watch`. `onEvent` is called for every poll tick with the current
302
+ * status snapshot, so callers can render progress.
303
+ */
304
+ async function watchPullRequest(options) {
305
+ const {
306
+ repoRoot,
307
+ branch,
308
+ intervalMs = DEFAULT_POLL_INTERVAL_MS,
309
+ timeoutMs = DEFAULT_POLL_TIMEOUT_MS,
310
+ onEvent = () => {},
311
+ } = options;
312
+
313
+ const deadline = Date.now() + timeoutMs;
314
+
315
+ // eslint-disable-next-line no-constant-condition
316
+ while (true) {
317
+ // Check open PRs first.
318
+ const open = findOpenPrForBranch(repoRoot, branch);
319
+ if (!open) {
320
+ const latest = findLatestPrForBranch(repoRoot, branch);
321
+ if (latest && latest.state === 'MERGED') {
322
+ onEvent({ phase: 'merged', pr: latest });
323
+ return { status: 'merged', pr: latest };
324
+ }
325
+ if (latest && latest.state === 'CLOSED') {
326
+ onEvent({ phase: 'closed', pr: latest });
327
+ return { status: 'closed', pr: latest };
328
+ }
329
+ onEvent({ phase: 'no-pr', pr: null });
330
+ return { status: 'no-pr', pr: null };
331
+ }
332
+
333
+ const snapshot = getPullRequestStatus(repoRoot, branch);
334
+ onEvent({ phase: 'polling', pr: snapshot });
335
+
336
+ if (snapshot && snapshot.checks.failed > 0) {
337
+ return { status: 'checks-failed', pr: snapshot };
338
+ }
339
+ if (
340
+ snapshot
341
+ && !snapshot.isDraft
342
+ && snapshot.mergeable === 'MERGEABLE'
343
+ && snapshot.checks.pending === 0
344
+ && snapshot.checks.failed === 0
345
+ && snapshot.checks.total > 0
346
+ ) {
347
+ // CI is green and PR is non-draft + mergeable; the merge itself will be
348
+ // driven by auto-merge or a separate `gh pr merge` invocation.
349
+ onEvent({ phase: 'ready', pr: snapshot });
350
+ }
351
+
352
+ if (Date.now() >= deadline) {
353
+ return { status: 'timeout', pr: snapshot };
354
+ }
355
+ await new Promise((resolve) => setTimeout(resolve, intervalMs));
356
+ }
357
+ }
358
+
359
+ function resolveRepoAndBranch(target) {
360
+ const repoRoot = resolveRepoRoot(target || process.cwd());
361
+ const branch = currentBranchName(repoRoot);
362
+ return { repoRoot, branch };
363
+ }
364
+
365
+ module.exports = {
366
+ PrError,
367
+ ghAvailable,
368
+ ghAuthStatus,
369
+ findOpenPrForBranch,
370
+ findLatestPrForBranch,
371
+ listOpenPrsForRepo,
372
+ pushBranch,
373
+ openPullRequest,
374
+ getPullRequestStatus,
375
+ enableAutoMerge,
376
+ markPullRequestReady,
377
+ watchPullRequest,
378
+ resolveRepoAndBranch,
379
+ defaultPrTitleFromCommit,
380
+ defaultPrBodyFromCommits,
381
+ };
@@ -13,6 +13,14 @@ const {
13
13
  gitRefExists,
14
14
  ensureRepoBranch,
15
15
  } = require('../git');
16
+ const { prepareAgentWorktree } = require('../scaffold/agent-worktree-prep');
17
+
18
+ function formatWorktreePrepOps(operations) {
19
+ if (!operations || operations.length === 0) return '';
20
+ return operations
21
+ .map((op) => `[agent-branch-start] worktree-prep ${op.status} ${op.file}${op.note ? ' — ' + op.note : ''}`)
22
+ .join('\n') + '\n';
23
+ }
16
24
 
17
25
  function hasGuardexBootstrapFiles(repoRoot) {
18
26
  const required = [
@@ -195,6 +203,7 @@ function startProtectedBaseSandboxFallback(blocked, sandboxSuffix) {
195
203
  }
196
204
  }
197
205
 
206
+ const prepOps = prepareAgentWorktree(blocked.repoRoot, selectedWorktreePath);
198
207
  return {
199
208
  metadata: {
200
209
  branch: selectedBranch,
@@ -202,7 +211,8 @@ function startProtectedBaseSandboxFallback(blocked, sandboxSuffix) {
202
211
  },
203
212
  stdout:
204
213
  `[agent-branch-start] Created branch: ${selectedBranch}\n` +
205
- `[agent-branch-start] Worktree: ${selectedWorktreePath}\n`,
214
+ `[agent-branch-start] Worktree: ${selectedWorktreePath}\n` +
215
+ formatWorktreePrepOps(prepOps),
206
216
  stderr: addResult.stderr || '',
207
217
  };
208
218
  }
@@ -246,9 +256,10 @@ function startProtectedBaseSandbox(blocked, { taskName, sandboxSuffix }) {
246
256
  return startProtectedBaseSandboxFallback(blocked, sandboxSuffix);
247
257
  }
248
258
 
259
+ const prepOps = prepareAgentWorktree(blocked.repoRoot, worktreePath);
249
260
  return {
250
261
  metadata,
251
- stdout: startResult.stdout || '',
262
+ stdout: (startResult.stdout || '') + formatWorktreePrepOps(prepOps),
252
263
  stderr: startResult.stderr || '',
253
264
  };
254
265
  }