@h-rig/cli 0.0.6-alpha.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.
Files changed (47) hide show
  1. package/README.md +30 -0
  2. package/dist/bin/build-rig-binaries.js +107 -0
  3. package/dist/bin/rig.js +9330 -0
  4. package/dist/src/commands/_authority-runs.js +110 -0
  5. package/dist/src/commands/_connection-state.js +123 -0
  6. package/dist/src/commands/_doctor-checks.js +501 -0
  7. package/dist/src/commands/_operator-view.js +322 -0
  8. package/dist/src/commands/_parsers.js +107 -0
  9. package/dist/src/commands/_paths.js +50 -0
  10. package/dist/src/commands/_pi-install.js +184 -0
  11. package/dist/src/commands/_policy.js +79 -0
  12. package/dist/src/commands/_preflight.js +460 -0
  13. package/dist/src/commands/_probes.js +13 -0
  14. package/dist/src/commands/_run-driver-helpers.js +289 -0
  15. package/dist/src/commands/_server-client.js +364 -0
  16. package/dist/src/commands/_snapshot-upload.js +313 -0
  17. package/dist/src/commands/_task-picker.js +48 -0
  18. package/dist/src/commands/agent.js +497 -0
  19. package/dist/src/commands/browser.js +890 -0
  20. package/dist/src/commands/connect.js +180 -0
  21. package/dist/src/commands/dist.js +402 -0
  22. package/dist/src/commands/doctor.js +511 -0
  23. package/dist/src/commands/github.js +276 -0
  24. package/dist/src/commands/inbox.js +160 -0
  25. package/dist/src/commands/init.js +1254 -0
  26. package/dist/src/commands/inspect.js +174 -0
  27. package/dist/src/commands/inspector.js +256 -0
  28. package/dist/src/commands/plugin.js +167 -0
  29. package/dist/src/commands/profile-and-review.js +178 -0
  30. package/dist/src/commands/queue.js +197 -0
  31. package/dist/src/commands/remote.js +507 -0
  32. package/dist/src/commands/repo-git-harness.js +221 -0
  33. package/dist/src/commands/run.js +753 -0
  34. package/dist/src/commands/server.js +368 -0
  35. package/dist/src/commands/setup.js +681 -0
  36. package/dist/src/commands/task-report-bug.js +1083 -0
  37. package/dist/src/commands/task-run-driver.js +1933 -0
  38. package/dist/src/commands/task.js +1325 -0
  39. package/dist/src/commands/test.js +39 -0
  40. package/dist/src/commands/workspace.js +123 -0
  41. package/dist/src/commands.js +9012 -0
  42. package/dist/src/index.js +9348 -0
  43. package/dist/src/launcher.js +131 -0
  44. package/dist/src/report-bug.js +260 -0
  45. package/dist/src/runner.js +272 -0
  46. package/dist/src/withMutedConsole.js +42 -0
  47. package/package.json +31 -0
@@ -0,0 +1,1933 @@
1
+ // @bun
2
+ // packages/cli/src/commands/task-run-driver.ts
3
+ import { copyFileSync, existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2, statSync } from "fs";
4
+ import { resolve as resolve4 } from "path";
5
+ import { spawn, spawnSync } from "child_process";
6
+ import { createInterface as createLineInterface } from "readline";
7
+
8
+ // packages/cli/src/runner.ts
9
+ import { EventBus } from "@rig/runtime/control-plane/runtime/events";
10
+ import { CliError } from "@rig/runtime/control-plane/errors";
11
+ import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
12
+ import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
13
+ import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
14
+ import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
15
+ import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
16
+ function formatCommand(parts) {
17
+ return parts.map((part) => /[^a-zA-Z0-9_./:-]/.test(part) ? JSON.stringify(part) : part).join(" ");
18
+ }
19
+
20
+ // packages/cli/src/commands/task-run-driver.ts
21
+ import { loadConfig } from "@rig/core/load-config";
22
+ import {
23
+ listAgentRuntimes,
24
+ startRuntimeSnapshotSidecar
25
+ } from "@rig/runtime/control-plane/runtime/isolation";
26
+ import {
27
+ finalizeTaskRunSnapshot,
28
+ resolveTaskRunSnapshotSidecar
29
+ } from "@rig/runtime/control-plane/runtime/snapshot/task-run";
30
+ import { loadRuntimeSnapshotConfig } from "@rig/runtime/control-plane/runtime/guard";
31
+ import {
32
+ buildClaudeLogsFromRecord,
33
+ flushPendingClaudeToolUseLogs
34
+ } from "@rig/runtime/control-plane/provider/claude-stream-records";
35
+ import {
36
+ buildCodexLogsFromRecord,
37
+ extractCodexAssistantMessageText,
38
+ flushPendingCodexToolUseLogs,
39
+ isCodexExecRecord
40
+ } from "@rig/runtime/control-plane/provider/codex-exec-records";
41
+ import { resolvePreferredShellBinary } from "@rig/runtime/control-plane/native/run-ops";
42
+ import { readAuthorityRun as readAuthorityRun3, readJsonFile, resolveTaskArtifactDirs } from "@rig/runtime/control-plane/authority-files";
43
+ import {
44
+ buildTaskRunLifecycleComment,
45
+ updateConfiguredTaskSourceTask
46
+ } from "@rig/runtime/control-plane/tasks/source-lifecycle";
47
+ import {
48
+ closeIssueAfterMergedPr,
49
+ commitRunChanges,
50
+ runPrAutomation
51
+ } from "@rig/runtime/control-plane/native/pr-automation";
52
+
53
+ // packages/cli/src/commands/_run-driver-helpers.ts
54
+ import { readFileSync } from "fs";
55
+ import { resolve as resolve3 } from "path";
56
+ import {
57
+ appendJsonlRecord,
58
+ readAuthorityRun as readAuthorityRun2,
59
+ resolveAuthorityRunDir as resolveAuthorityRunDir2,
60
+ writeJsonFile as writeJsonFile2
61
+ } from "@rig/runtime/control-plane/authority-files";
62
+ import { taskLookup } from "@rig/runtime/control-plane/native/task-ops";
63
+ import { buildProviderTaskRunInstructionLines } from "@rig/runtime/control-plane/provider/runtime-instructions";
64
+ import { loadRigTaskRunSkillBody } from "@rig/runtime/control-plane/provider/rig-task-run-skill";
65
+
66
+ // packages/cli/src/commands/_authority-runs.ts
67
+ import { existsSync } from "fs";
68
+ import { resolve as resolve2 } from "path";
69
+ import {
70
+ readAuthorityRun,
71
+ readJsonlFile,
72
+ resolveAuthorityRunDir,
73
+ writeJsonFile
74
+ } from "@rig/runtime/control-plane/authority-files";
75
+
76
+ // packages/cli/src/commands/_paths.ts
77
+ import { resolve } from "path";
78
+ import { resolveMonorepoRoot } from "@rig/runtime/control-plane/native/utils";
79
+ function resolveControlPlaneMonorepoRoot(projectRoot) {
80
+ return resolveMonorepoRoot(projectRoot);
81
+ }
82
+ function resolveControlPlaneTaskConfigPath(projectRoot) {
83
+ return resolve(resolveControlPlaneMonorepoRoot(projectRoot), ".rig", "task-config.json");
84
+ }
85
+
86
+ // packages/cli/src/commands/_authority-runs.ts
87
+ var RIG_WORKSPACE_ID = "rig-local-workspace";
88
+ function normalizeRuntimeAdapter(value) {
89
+ const normalized = value?.trim().toLowerCase();
90
+ if (!normalized) {
91
+ return "pi";
92
+ }
93
+ if (normalized === "codex" || normalized === "codex-cli" || normalized === "codex-app-server" || normalized === "gpt-codex") {
94
+ return "codex";
95
+ }
96
+ if (normalized === "pi" || normalized === "rig-pi" || normalized === "@rig/pi") {
97
+ return "pi";
98
+ }
99
+ return "claude-code";
100
+ }
101
+ function readLatestBeadRecord(projectRoot, taskId) {
102
+ const issuesPath = resolve2(resolveControlPlaneMonorepoRoot(projectRoot), ".beads", "issues.jsonl");
103
+ if (!existsSync(issuesPath)) {
104
+ return null;
105
+ }
106
+ let latest = null;
107
+ for (const issue of readJsonlFile(issuesPath)) {
108
+ if (!issue || typeof issue !== "object") {
109
+ continue;
110
+ }
111
+ const record = issue;
112
+ if (record.id === taskId) {
113
+ latest = record;
114
+ }
115
+ }
116
+ return latest;
117
+ }
118
+ function resolveTaskTitleForAuthorityRun(projectRoot, taskId) {
119
+ try {
120
+ const record = readLatestBeadRecord(projectRoot, taskId);
121
+ const title = record && typeof record.title === "string" ? record.title.trim() : "";
122
+ if (title) {
123
+ return title;
124
+ }
125
+ } catch {}
126
+ return null;
127
+ }
128
+ function upsertAgentAuthorityRun(projectRoot, input) {
129
+ const current = readAuthorityRun(projectRoot, input.runId);
130
+ const existing = current;
131
+ const createdAt = existing?.createdAt ?? input.createdAt;
132
+ const runtimeMode = typeof existing?.runtimeMode === "string" ? existing.runtimeMode : "full-access";
133
+ const interactionMode = typeof existing?.interactionMode === "string" ? existing.interactionMode : "default";
134
+ const runMode = typeof existing?.runMode === "string" ? existing.runMode : "autonomous";
135
+ const runtimeAdapter = normalizeRuntimeAdapter(input.runtimeAdapter ?? existing?.runtimeAdapter ?? "claude-code");
136
+ const title = resolveTaskTitleForAuthorityRun(projectRoot, input.taskId) ?? input.taskId;
137
+ const next = {
138
+ runId: input.runId,
139
+ projectRoot,
140
+ workspaceId: existing?.workspaceId ?? RIG_WORKSPACE_ID,
141
+ taskId: input.taskId,
142
+ threadId: existing?.threadId ?? null,
143
+ mode: "local",
144
+ runtimeAdapter,
145
+ status: input.status,
146
+ createdAt,
147
+ startedAt: input.startedAt ?? existing?.startedAt ?? null,
148
+ completedAt: input.completedAt ?? existing?.completedAt ?? null,
149
+ endpointId: existing?.endpointId ?? null,
150
+ hostId: existing?.hostId ?? null,
151
+ worktreePath: input.worktreePath ?? existing?.worktreePath ?? null,
152
+ artifactRoot: input.artifactRoot ?? existing?.artifactRoot ?? null,
153
+ logRoot: input.logRoot ?? existing?.logRoot ?? null,
154
+ sessionPath: input.sessionPath ?? existing?.sessionPath ?? null,
155
+ sessionLogPath: input.sessionLogPath ?? existing?.sessionLogPath ?? null,
156
+ pid: input.pid ?? existing?.pid ?? null,
157
+ updatedAt: input.createdAt,
158
+ title,
159
+ model: typeof existing?.model === "string" ? existing.model : null,
160
+ runtimeMode,
161
+ interactionMode,
162
+ runMode,
163
+ initialPrompt: typeof existing?.initialPrompt === "string" ? existing.initialPrompt : null
164
+ };
165
+ if (input.errorText !== undefined) {
166
+ next.errorText = input.errorText;
167
+ } else if ("errorText" in next) {
168
+ delete next.errorText;
169
+ }
170
+ writeJsonFile(resolve2(resolveAuthorityRunDir(projectRoot, input.runId), "run.json"), next);
171
+ return next;
172
+ }
173
+
174
+ // packages/cli/src/commands/_run-driver-helpers.ts
175
+ function patchAuthorityRun(projectRoot, runId, patch) {
176
+ const current = readAuthorityRun2(projectRoot, runId);
177
+ if (!current) {
178
+ throw new CliError2(`Run not found: ${runId}`, 2);
179
+ }
180
+ const next = {
181
+ ...current,
182
+ ...patch,
183
+ updatedAt: new Date().toISOString()
184
+ };
185
+ writeJsonFile2(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "run.json"), next);
186
+ return next;
187
+ }
188
+ function touchAuthorityRun(projectRoot, runId) {
189
+ const current = readAuthorityRun2(projectRoot, runId);
190
+ if (!current) {
191
+ return;
192
+ }
193
+ writeJsonFile2(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "run.json"), {
194
+ ...current,
195
+ updatedAt: new Date().toISOString()
196
+ });
197
+ }
198
+ function appendRunTimeline(projectRoot, runId, value) {
199
+ appendJsonlRecord(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "timeline.jsonl"), value);
200
+ touchAuthorityRun(projectRoot, runId);
201
+ }
202
+ function appendRunLog(projectRoot, runId, value) {
203
+ appendJsonlRecord(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "logs.jsonl"), value);
204
+ touchAuthorityRun(projectRoot, runId);
205
+ }
206
+ function appendRunAction(projectRoot, runId, value) {
207
+ appendJsonlRecord(resolve3(resolveAuthorityRunDir2(projectRoot, runId), "timeline.jsonl"), {
208
+ id: value.id,
209
+ type: "action",
210
+ actionType: value.actionType,
211
+ title: value.title,
212
+ detail: value.detail ?? null,
213
+ state: value.state,
214
+ createdAt: value.startedAt,
215
+ completedAt: value.completedAt ?? null,
216
+ payload: value.payload ?? {}
217
+ });
218
+ }
219
+ function startRunAction(projectRoot, runId, input) {
220
+ const startedAt = new Date().toISOString();
221
+ appendRunAction(projectRoot, runId, {
222
+ id: input.actionId,
223
+ actionType: input.actionType,
224
+ title: input.title,
225
+ detail: input.detail,
226
+ state: "running",
227
+ startedAt,
228
+ payload: input.payload
229
+ });
230
+ emitServerRunEvent({ type: "timeline", runId });
231
+ return {
232
+ startedAt,
233
+ complete: (detail, payload) => {
234
+ appendRunAction(projectRoot, runId, {
235
+ id: input.actionId,
236
+ actionType: input.actionType,
237
+ title: input.title,
238
+ detail: detail ?? input.detail ?? null,
239
+ state: "completed",
240
+ startedAt,
241
+ completedAt: new Date().toISOString(),
242
+ payload: payload ?? input.payload
243
+ });
244
+ emitServerRunEvent({ type: "timeline", runId });
245
+ },
246
+ fail: (detail, payload) => {
247
+ appendRunAction(projectRoot, runId, {
248
+ id: input.actionId,
249
+ actionType: input.actionType,
250
+ title: input.title,
251
+ detail,
252
+ state: "failed",
253
+ startedAt,
254
+ completedAt: new Date().toISOString(),
255
+ payload: payload ?? input.payload
256
+ });
257
+ emitServerRunEvent({ type: "timeline", runId });
258
+ }
259
+ };
260
+ }
261
+ function emitServerRunEvent(event) {
262
+ console.log(`__RIG_RUN_EVENT__${JSON.stringify({ ...event, at: new Date().toISOString() })}`);
263
+ }
264
+ function buildRunPrompt(input) {
265
+ const initialPrompt = input.initialPrompt?.trim() || null;
266
+ if (!input.taskId) {
267
+ return initialPrompt ?? input.fallbackTitle?.trim() ?? "Continue the requested Rig run and complete the work.";
268
+ }
269
+ const issues = (() => {
270
+ try {
271
+ return taskLookup(input.projectRoot, input.taskId);
272
+ } catch {
273
+ return "";
274
+ }
275
+ })();
276
+ const scopeText = (() => {
277
+ try {
278
+ const parsed = JSON.parse(readFileSync(resolveControlPlaneTaskConfigPath(input.projectRoot), "utf8"));
279
+ const entry = parsed[input.taskId] ?? {};
280
+ const scope = Array.isArray(entry.scope) ? entry.scope.filter((item) => typeof item === "string") : [];
281
+ const validation = Array.isArray(entry.validation) ? entry.validation.filter((item) => typeof item === "string") : [];
282
+ const lines = [];
283
+ if (scope.length > 0)
284
+ lines.push(`Scope:
285
+ - ${scope.join(`
286
+ - `)}`);
287
+ if (validation.length > 0)
288
+ lines.push(`Validation:
289
+ - ${validation.join(`
290
+ - `)}`);
291
+ return lines.join(`
292
+
293
+ `);
294
+ } catch {
295
+ return "";
296
+ }
297
+ })();
298
+ const bead = readLatestBeadRecord(input.projectRoot, input.taskId);
299
+ const sourceTask = input.sourceTask ?? null;
300
+ const sourceDescription = firstPromptString(sourceTask?.description, sourceTask?.body);
301
+ const sourceAcceptance = firstPromptString(sourceTask?.acceptanceCriteria, sourceTask?.acceptance_criteria);
302
+ const sourceValidation = uniqueStrings([...sourceTask?.validation ?? [], ...sourceTask?.validators ?? []]);
303
+ const title = (bead && typeof bead === "object" && typeof bead.title === "string" ? bead.title : null) ?? firstPromptString(sourceTask?.title) ?? input.fallbackTitle ?? issues ?? input.taskId;
304
+ const description = (bead && typeof bead === "object" && typeof bead.description === "string" ? bead.description : null) ?? sourceDescription ?? input.fallbackDescription ?? "";
305
+ const acceptance = bead && typeof bead === "object" && typeof bead.acceptance_criteria === "string" ? bead.acceptance_criteria : sourceAcceptance ?? input.fallbackAcceptanceCriteria ?? "";
306
+ const sourceContractLines = renderSourceTaskContract(sourceTask, sourceValidation);
307
+ const providerLines = buildProviderTaskRunInstructionLines(input.runtimeAdapter);
308
+ const skillBody = loadRigTaskRunSkillBody();
309
+ return [
310
+ `You are working on task ${input.taskId}: ${title}.`,
311
+ description ? `Description:
312
+ ${description}` : null,
313
+ acceptance ? `Acceptance criteria:
314
+ ${acceptance}` : null,
315
+ sourceContractLines.length > 0 ? `Source task contract:
316
+ ${sourceContractLines.join(`
317
+ `)}` : null,
318
+ scopeText || renderSourceScopeValidation(sourceTask, sourceValidation) || null,
319
+ initialPrompt ? `Additional operator guidance:
320
+ ${initialPrompt}` : null,
321
+ providerLines.length > 0 ? `Provider-specific runtime tooling:
322
+ ${providerLines.join(" ")}` : null,
323
+ skillBody || null
324
+ ].filter((value) => Boolean(value)).join(`
325
+
326
+ `);
327
+ }
328
+ function firstPromptString(...values) {
329
+ for (const value of values) {
330
+ const trimmed = typeof value === "string" ? value.trim() : "";
331
+ if (trimmed.length > 0) {
332
+ return trimmed;
333
+ }
334
+ }
335
+ return null;
336
+ }
337
+ function uniqueStrings(values) {
338
+ return Array.from(new Set(values.map((value) => value.trim()).filter(Boolean)));
339
+ }
340
+ function renderSourceTaskContract(task, validation) {
341
+ if (!task) {
342
+ return [];
343
+ }
344
+ const lines = [];
345
+ const sourceIssueId = firstPromptString(task.sourceIssueId);
346
+ if (sourceIssueId)
347
+ lines.push(`Source issue: ${sourceIssueId}`);
348
+ const status = firstPromptString(task.status);
349
+ if (status)
350
+ lines.push(`Source status: ${status}`);
351
+ const issueType = firstPromptString(task.issueType);
352
+ if (issueType)
353
+ lines.push(`Issue type: ${issueType}`);
354
+ const role = firstPromptString(task.role);
355
+ if (role)
356
+ lines.push(`Role: ${role}`);
357
+ const externalRef = firstPromptString(task.externalRef);
358
+ if (externalRef)
359
+ lines.push(`External ref: ${externalRef}`);
360
+ const labels = uniqueStrings(task.labels ?? []);
361
+ if (labels.length > 0)
362
+ lines.push(`Labels:
363
+ - ${labels.join(`
364
+ - `)}`);
365
+ if (validation.length > 0)
366
+ lines.push(`Validators:
367
+ - ${validation.join(`
368
+ - `)}`);
369
+ return lines;
370
+ }
371
+ function renderSourceScopeValidation(task, validation) {
372
+ if (!task) {
373
+ return "";
374
+ }
375
+ const lines = [];
376
+ const scope = uniqueStrings(task.scope ?? []);
377
+ if (scope.length > 0)
378
+ lines.push(`Scope:
379
+ - ${scope.join(`
380
+ - `)}`);
381
+ if (validation.length > 0)
382
+ lines.push(`Validation:
383
+ - ${validation.join(`
384
+ - `)}`);
385
+ return lines.join(`
386
+
387
+ `);
388
+ }
389
+
390
+ // packages/cli/src/commands/task-run-driver.ts
391
+ var PI_CANONICAL_RUN_STAGES = [
392
+ "Connect",
393
+ "GitHub/task sync",
394
+ "Prepare workspace",
395
+ "Launch Pi",
396
+ "Plan",
397
+ "Implement",
398
+ "Validate",
399
+ "Commit",
400
+ "Open PR",
401
+ "Review/CI",
402
+ "Merge",
403
+ "Complete"
404
+ ];
405
+ function canonicalPiRunStages() {
406
+ return [...PI_CANONICAL_RUN_STAGES];
407
+ }
408
+ function buildPiRigBridgeEnv(input) {
409
+ return {
410
+ RIG_PROJECT_ROOT: input.projectRoot,
411
+ PROJECT_RIG_ROOT: input.projectRoot,
412
+ RIG_RUN_ID: input.runId,
413
+ RIG_SERVER_RUN_ID: input.runId,
414
+ RIG_TASK_ID: input.taskId,
415
+ RIG_RUNTIME_ADAPTER: "pi",
416
+ ...input.serverUrl ? { RIG_SERVER_URL: input.serverUrl } : {},
417
+ ...input.authToken ? { RIG_AUTH_TOKEN: input.authToken } : {},
418
+ ...input.githubToken ? { RIG_GITHUB_TOKEN: input.githubToken } : {}
419
+ };
420
+ }
421
+ function runGitSync(cwd, args, input) {
422
+ const result = spawnSync("git", [...args], {
423
+ cwd,
424
+ input,
425
+ encoding: "utf8",
426
+ stdio: input === undefined ? ["ignore", "pipe", "pipe"] : ["pipe", "pipe", "pipe"]
427
+ });
428
+ return {
429
+ ok: result.status === 0,
430
+ stdout: result.stdout || "",
431
+ stderr: result.stderr || (result.error instanceof Error ? result.error.message : "")
432
+ };
433
+ }
434
+ function copyUntrackedDirtyFiles(sourceRoot, targetRoot) {
435
+ const listed = runGitSync(sourceRoot, ["ls-files", "--others", "--exclude-standard", "-z"]);
436
+ if (!listed.ok || !listed.stdout)
437
+ return 0;
438
+ let copied = 0;
439
+ for (const relativePath of listed.stdout.split("\x00").filter(Boolean)) {
440
+ const sourcePath = resolve4(sourceRoot, relativePath);
441
+ const targetPath = resolve4(targetRoot, relativePath);
442
+ try {
443
+ if (!statSync(sourcePath).isFile())
444
+ continue;
445
+ mkdirSync(resolve4(targetPath, ".."), { recursive: true });
446
+ copyFileSync(sourcePath, targetPath);
447
+ copied += 1;
448
+ } catch {}
449
+ }
450
+ return copied;
451
+ }
452
+ function applyDirtyBaselineSnapshot(input) {
453
+ const diff = runGitSync(input.sourceRoot, ["diff", "--binary", "HEAD"]);
454
+ if (!diff.ok) {
455
+ return { applied: false, copiedUntracked: 0, detail: diff.stderr || "git diff failed" };
456
+ }
457
+ let applied = false;
458
+ if (diff.stdout.trim()) {
459
+ const apply = runGitSync(input.targetRoot, ["apply", "--whitespace=nowarn", "-"], diff.stdout);
460
+ if (!apply.ok) {
461
+ return { applied: false, copiedUntracked: 0, detail: apply.stderr || "git apply failed" };
462
+ }
463
+ applied = true;
464
+ }
465
+ const copiedUntracked = copyUntrackedDirtyFiles(input.sourceRoot, input.targetRoot);
466
+ return {
467
+ applied: applied || copiedUntracked > 0,
468
+ copiedUntracked,
469
+ detail: applied || copiedUntracked > 0 ? `Applied dirty baseline snapshot (${copiedUntracked} untracked files).` : "No dirty baseline changes to apply."
470
+ };
471
+ }
472
+ function buildTaskRunReviewEnv(config) {
473
+ const review = config?.review;
474
+ return {
475
+ ...review?.mode ? { AI_REVIEW_MODE: review.mode } : {},
476
+ ...review?.provider ? { AI_REVIEW_PROVIDER: review.provider } : {}
477
+ };
478
+ }
479
+ function positiveInt(value, fallback) {
480
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : fallback;
481
+ }
482
+ function resolveTaskRunAutomationLimits(config, env = process.env) {
483
+ const configuredValidationAttempts = positiveInt(config?.automation?.maxValidationAttempts, 30);
484
+ const configuredPrFixIterations = positiveInt(config?.automation?.maxPrFixIterations, 30);
485
+ return {
486
+ maxValidationAttempts: parsePositiveInt(env.RIG_TASK_RUN_MAX_ATTEMPTS, "RIG_TASK_RUN_MAX_ATTEMPTS", configuredValidationAttempts),
487
+ maxPrFixIterations: configuredPrFixIterations
488
+ };
489
+ }
490
+ function classifyPlanningNeed(input) {
491
+ const planning = input.config?.planning ?? {};
492
+ const mode = planning.mode ?? "auto";
493
+ const labels = new Set((input.sourceTask?.labels ?? []).map((label) => label.trim().toLowerCase()).filter(Boolean));
494
+ const requireLabels = planning.requireForLabels ?? ["needs-plan", "epic", "large", "risk:high"];
495
+ const skipLabels = planning.skipForLabels ?? ["no-plan", "small", "trivial"];
496
+ const bodyLength = `${input.sourceTask?.description ?? ""}
497
+ ${input.sourceTask?.body ?? ""}
498
+ ${input.sourceTask?.acceptanceCriteria ?? input.sourceTask?.acceptance_criteria ?? ""}`.trim().length;
499
+ const forcedLabel = requireLabels.find((label) => labels.has(label.toLowerCase()));
500
+ const skippedLabel = skipLabels.find((label) => labels.has(label.toLowerCase()));
501
+ const highRisk = labels.has("risk:high") || labels.has("security") || labels.has("migration");
502
+ const size = labels.has("large") || bodyLength > 4000 ? "large" : bodyLength > 1000 ? "medium" : "small";
503
+ const risk = highRisk ? "high" : labels.has("risk:medium") || size !== "small" ? "medium" : "low";
504
+ if (mode === "always") {
505
+ return { planningRequired: true, size, risk, reason: "planning.mode=always", policy: "always" };
506
+ }
507
+ if (mode === "off") {
508
+ return { planningRequired: false, size, risk, reason: "planning.mode=off", policy: "off" };
509
+ }
510
+ if (forcedLabel) {
511
+ return { planningRequired: true, size, risk, reason: `label:${forcedLabel}`, policy: "auto" };
512
+ }
513
+ if (skippedLabel && !highRisk) {
514
+ return { planningRequired: false, size, risk, reason: `label:${skippedLabel}`, policy: "auto" };
515
+ }
516
+ const planningRequired = size !== "small" || risk === "high";
517
+ return {
518
+ planningRequired,
519
+ size,
520
+ risk,
521
+ reason: planningRequired ? `auto:${size}/${risk}` : "auto:small/low",
522
+ policy: "auto"
523
+ };
524
+ }
525
+ function buildPiValidationRetrySteeringPrompt(input) {
526
+ return [
527
+ `Retry attempt ${input.attempt + 1}/${input.maxAttempts} for task ${input.taskId}.`,
528
+ "The previous attempt failed Rig validation. Continue from the existing runtime workspace and PR state instead of starting over.",
529
+ "",
530
+ `Latest blocker: ${input.failureDetail}`,
531
+ ...input.validationOutput?.trim() ? ["", "Validation output:", input.validationOutput.trim()] : [],
532
+ "",
533
+ "Fix the concrete failure, rerun the relevant validation, and only report completion when the task is accepted."
534
+ ].join(`
535
+ `);
536
+ }
537
+ function classifyValidationRetryOutcome(input) {
538
+ return {
539
+ stage: "Validate failed",
540
+ status: input.attempt >= input.maxAttempts ? "needs_attention" : "retry",
541
+ failureDetail: input.failureDetail
542
+ };
543
+ }
544
+ function configuredIssueUpdatesMode(config) {
545
+ const github = config?.github;
546
+ const mode = github && typeof github === "object" && !Array.isArray(github) ? github.issueUpdates : null;
547
+ return mode === "off" || mode === "minimal" || mode === "lifecycle" ? mode : "lifecycle";
548
+ }
549
+ function shouldWriteDriverIssueUpdate(config, status) {
550
+ const mode = configuredIssueUpdatesMode(config);
551
+ if (mode === "off")
552
+ return false;
553
+ if (mode === "lifecycle")
554
+ return true;
555
+ return ["closed", "failed", "needs_attention"].includes(status);
556
+ }
557
+ async function loadTaskRunAutomationConfig(projectRoot) {
558
+ try {
559
+ return await loadConfig(projectRoot);
560
+ } catch {
561
+ return null;
562
+ }
563
+ }
564
+ function taskRunStageLogId(runId, stage) {
565
+ return `log:${runId}:stage:${stage.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
566
+ }
567
+ function appendPiStageLog(input) {
568
+ appendRunLog(input.projectRoot, input.runId, {
569
+ id: taskRunStageLogId(input.runId, input.stage),
570
+ title: input.stage,
571
+ detail: input.detail ?? null,
572
+ tone: input.tone ?? "info",
573
+ status: input.status ?? "running",
574
+ createdAt: new Date().toISOString()
575
+ });
576
+ emitServerRunEvent({ type: "log", runId: input.runId, title: input.stage });
577
+ }
578
+ function createCommandRunner(binary) {
579
+ return async (args, options) => {
580
+ const child = spawn(binary, [...args], {
581
+ cwd: options?.cwd,
582
+ stdio: ["ignore", "pipe", "pipe"]
583
+ });
584
+ const stdoutChunks = [];
585
+ const stderrChunks = [];
586
+ child.stdout.on("data", (chunk) => stdoutChunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))));
587
+ child.stderr.on("data", (chunk) => stderrChunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))));
588
+ return await new Promise((resolve5) => {
589
+ child.once("error", (error) => resolve5({ exitCode: 1, stderr: error.message }));
590
+ child.once("close", (code) => resolve5({
591
+ exitCode: code ?? 1,
592
+ stdout: Buffer.concat(stdoutChunks).toString("utf8"),
593
+ stderr: Buffer.concat(stderrChunks).toString("utf8")
594
+ }));
595
+ });
596
+ };
597
+ }
598
+ async function resolvePostValidationBranch(input) {
599
+ if (!input.configuredBranch.startsWith("task-")) {
600
+ return input.configuredBranch;
601
+ }
602
+ const current = await input.gitCommand(["rev-parse", "--abbrev-ref", "HEAD"], { cwd: input.workspace });
603
+ const currentBranch = current.stdout?.trim();
604
+ if (current.exitCode === 0 && currentBranch && currentBranch !== "HEAD") {
605
+ return currentBranch;
606
+ }
607
+ return input.configuredBranch;
608
+ }
609
+ async function runTaskRunPostValidationLifecycle(input) {
610
+ const taskId = input.taskId?.trim();
611
+ if (!taskId) {
612
+ return { status: "skipped" };
613
+ }
614
+ const config = input.config ?? {};
615
+ const prMode = config.pr?.mode ?? "auto";
616
+ if (prMode === "off" || prMode === "ask") {
617
+ input.appendStage?.("Open PR", prMode === "off" ? "PR automation disabled by pr.mode=off." : "PR creation awaiting operator approval by pr.mode=ask.", "skipped", "info");
618
+ input.appendStage?.("Complete", "Validation completed; no PR was opened and the issue was left open.", "completed", "info");
619
+ return { status: "skipped" };
620
+ }
621
+ const workspace = input.runtimeWorkspace || input.projectRoot;
622
+ const ghCommand = input.ghCommand ?? createCommandRunner("gh");
623
+ const gitCommand = input.gitCommand ?? createCommandRunner("git");
624
+ const configuredBranch = input.branch || `rig/${input.runtimeTaskId}-${input.runId}`;
625
+ const branch = await resolvePostValidationBranch({
626
+ workspace,
627
+ configuredBranch,
628
+ gitCommand
629
+ });
630
+ const prAutomation = input.prAutomation ?? runPrAutomation;
631
+ const updateTaskSource = input.updateTaskSource ?? updateConfiguredTaskSourceTask;
632
+ const stage = input.appendStage ?? (() => {
633
+ return;
634
+ });
635
+ const steerPi = input.steerPi ?? (async (message) => {
636
+ appendRunLog(input.projectRoot, input.runId, {
637
+ id: `log:${input.runId}:pr-steering:${Date.now()}`,
638
+ title: "Steering Pi from PR feedback",
639
+ detail: message,
640
+ tone: "info",
641
+ status: "reviewing",
642
+ createdAt: new Date().toISOString()
643
+ });
644
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Steering Pi from PR feedback" });
645
+ });
646
+ stage("Commit", `Committing changes in ${workspace}.`, "running", "tool");
647
+ const commit = await commitRunChanges({
648
+ cwd: workspace,
649
+ message: `rig: complete task ${taskId}`,
650
+ command: gitCommand
651
+ });
652
+ stage("Commit", commit.committed ? "Committed run workspace changes." : "No workspace changes to commit.", "completed", "tool");
653
+ stage("Open PR", `Opening PR from ${branch}.`, "running", "tool");
654
+ const pr = await prAutomation({
655
+ projectRoot: workspace,
656
+ taskId,
657
+ runId: input.runId,
658
+ branch,
659
+ config,
660
+ sourceTask: input.sourceTask,
661
+ uploadedSnapshot: input.uploadedSnapshot,
662
+ command: ghCommand,
663
+ gitCommand,
664
+ steerPi,
665
+ lifecycle: {
666
+ onPrOpened: async ({ prUrl }) => {
667
+ stage("Open PR", prUrl, "completed", "tool");
668
+ try {
669
+ if (shouldWriteDriverIssueUpdate(config, "under_review")) {
670
+ await updateTaskSource(input.projectRoot, {
671
+ taskId,
672
+ sourceTask: runSourceTaskIdentity(input.sourceTask),
673
+ update: {
674
+ status: "under_review",
675
+ comment: buildTaskRunLifecycleComment({
676
+ runId: input.runId,
677
+ status: "under_review",
678
+ summary: "Rig opened a pull request for this task.",
679
+ runtimeWorkspace: input.runtimeWorkspace ?? null
680
+ })
681
+ }
682
+ });
683
+ }
684
+ } catch (error) {
685
+ appendRunLog(input.projectRoot, input.runId, {
686
+ id: `log:${input.runId}:task-source-under-review-update`,
687
+ title: "Task source PR status update failed",
688
+ detail: error instanceof Error ? error.message : String(error),
689
+ tone: "error",
690
+ status: "reviewing",
691
+ createdAt: new Date().toISOString()
692
+ });
693
+ }
694
+ },
695
+ onReviewCiStarted: ({ prUrl, iteration }) => stage("Review/CI", `${prUrl} (iteration ${iteration})`, "running", "info"),
696
+ onFeedback: async ({ feedback }) => {
697
+ stage("Review/CI", feedback.join(`
698
+ `), "reviewing", "error");
699
+ if (shouldWriteDriverIssueUpdate(config, "ci_fixing")) {
700
+ await updateTaskSource(input.projectRoot, {
701
+ taskId,
702
+ sourceTask: runSourceTaskIdentity(input.sourceTask),
703
+ update: {
704
+ status: "ci_fixing",
705
+ comment: buildTaskRunLifecycleComment({
706
+ runId: input.runId,
707
+ status: "ci_fixing",
708
+ summary: "Rig is fixing CI/review feedback for this task.",
709
+ runtimeWorkspace: input.runtimeWorkspace ?? null
710
+ })
711
+ }
712
+ }).catch(() => {
713
+ return;
714
+ });
715
+ }
716
+ },
717
+ onMergeStarted: async ({ prUrl }) => {
718
+ stage("Merge", prUrl, "running", "tool");
719
+ if (shouldWriteDriverIssueUpdate(config, "merging")) {
720
+ await updateTaskSource(input.projectRoot, {
721
+ taskId,
722
+ sourceTask: runSourceTaskIdentity(input.sourceTask),
723
+ update: {
724
+ status: "merging",
725
+ comment: buildTaskRunLifecycleComment({
726
+ runId: input.runId,
727
+ status: "merging",
728
+ summary: "Rig is merging the pull request for this task.",
729
+ runtimeWorkspace: input.runtimeWorkspace ?? null
730
+ })
731
+ }
732
+ }).catch(() => {
733
+ return;
734
+ });
735
+ }
736
+ },
737
+ onMerged: ({ prUrl }) => stage("Merge", prUrl, "completed", "tool")
738
+ }
739
+ });
740
+ if (pr.status === "opened" && pr.prUrl) {
741
+ stage("Complete", `PR ready without merge: ${pr.prUrl}`, "completed", "info");
742
+ return { status: "completed", pr };
743
+ }
744
+ if (pr.status !== "merged" || !pr.prUrl) {
745
+ const detail = pr.actionableFeedback.join(`
746
+ `) || "PR automation did not merge the PR.";
747
+ stage("Needs attention", detail, "needs_attention", "error");
748
+ if (shouldWriteDriverIssueUpdate(config, "needs_attention")) {
749
+ await updateTaskSource(input.projectRoot, {
750
+ taskId,
751
+ sourceTask: runSourceTaskIdentity(input.sourceTask),
752
+ update: {
753
+ status: "needs_attention",
754
+ comment: buildTaskRunLifecycleComment({
755
+ runId: input.runId,
756
+ status: "needs_attention",
757
+ summary: "Rig needs operator attention before this task can proceed.",
758
+ runtimeWorkspace: input.runtimeWorkspace ?? null,
759
+ errorText: detail
760
+ })
761
+ }
762
+ }).catch(() => {
763
+ return;
764
+ });
765
+ }
766
+ return { status: "needs_attention", pr };
767
+ }
768
+ if (shouldWriteDriverIssueUpdate(config, "closed")) {
769
+ await closeIssueAfterMergedPr({
770
+ projectRoot: input.projectRoot,
771
+ taskId,
772
+ runId: input.runId,
773
+ prUrl: pr.prUrl,
774
+ sourceTask: input.sourceTask,
775
+ updateTaskSource: async (projectRoot, closeInput) => updateTaskSource(projectRoot, closeInput)
776
+ });
777
+ }
778
+ stage("Complete", `PR merged and issue closed: ${pr.prUrl}`, "completed", "info");
779
+ return { status: "completed", pr };
780
+ }
781
+ function summarizeValidationFailure(projectRoot, taskId) {
782
+ if (!taskId) {
783
+ return null;
784
+ }
785
+ for (const artifactDir of resolveTaskArtifactDirs(projectRoot, taskId)) {
786
+ const summary = readJsonFile(resolve4(artifactDir, "validation-summary.json"), null);
787
+ if (!summary || summary.status !== "fail") {
788
+ continue;
789
+ }
790
+ const failedCategories = Array.isArray(summary.categories) ? summary.categories.filter((entry) => String(entry?.status ?? "") !== "pass").map((entry) => String(entry?.category ?? "").trim()).filter(Boolean) : [];
791
+ if (failedCategories.length > 0) {
792
+ return `Task validation failed: ${failedCategories.join(", ")}`;
793
+ }
794
+ return "Task validation failed.";
795
+ }
796
+ return null;
797
+ }
798
+ function classifyCompletionVerificationLine(line) {
799
+ const trimmed = line.trim();
800
+ if (/^=== Completion Verification: /.test(trimmed)) {
801
+ return { isCompletionLine: true, isVerifierReview: false };
802
+ }
803
+ if (/^\[\d+\/\d+\]/.test(trimmed)) {
804
+ return {
805
+ isCompletionLine: true,
806
+ isVerifierReview: /Verifier review/i.test(trimmed)
807
+ };
808
+ }
809
+ return { isCompletionLine: false, isVerifierReview: false };
810
+ }
811
+ function createRunLogIdFactory(runId) {
812
+ let sequence = 0;
813
+ return () => {
814
+ sequence += 1;
815
+ return `log:${runId}:${Date.now()}:${sequence}`;
816
+ };
817
+ }
818
+ function summarizeTaskRunProcessFailure(input) {
819
+ const fallback = `Task run failed (${String(input.exitCode ?? "unknown")}${input.signal ? ` ${input.signal}` : ""})`;
820
+ const stderrLines = input.stderrLines ?? [];
821
+ const meaningfulLines = [...stderrLines].map((line) => line.trim()).filter(Boolean).filter((line) => !/^Task run failed \([^)]*\)$/i.test(line));
822
+ const taskSourceFailure = meaningfulLines.find((line) => /failed to update task source/i.test(line));
823
+ if (taskSourceFailure) {
824
+ return `Task source update failed: ${taskSourceFailure}`;
825
+ }
826
+ const moduleFailure = meaningfulLines.find((line) => /cannot find module/i.test(line));
827
+ if (moduleFailure) {
828
+ return `Runtime module resolution failed: ${moduleFailure}`;
829
+ }
830
+ for (const line of [...meaningfulLines].reverse()) {
831
+ const trimmed = line.trim();
832
+ if (/server-managed task run cannot finish successfully/i.test(trimmed)) {
833
+ return `Task source remained open: ${trimmed}`;
834
+ }
835
+ if (/no api key found/i.test(trimmed)) {
836
+ return `Missing provider credentials: ${trimmed}`;
837
+ }
838
+ if (/no claude credentials found/i.test(trimmed)) {
839
+ return `Missing provider credentials: ${trimmed}`;
840
+ }
841
+ if (/unauthorized|authentication failed|invalid api key|api key is invalid/i.test(trimmed)) {
842
+ return `Provider authentication failed: ${trimmed}`;
843
+ }
844
+ if (/command not found|no such file or directory|enoent/i.test(trimmed)) {
845
+ return `Missing runtime executable: ${trimmed}`;
846
+ }
847
+ if (/bubblewrap|\bbwrap\b|sandbox-exec|sandbox/i.test(trimmed) && /missing|not found|failed|denied/i.test(trimmed)) {
848
+ return `Sandbox backend unavailable: ${trimmed}`;
849
+ }
850
+ }
851
+ return fallback;
852
+ }
853
+ function parsePositiveInt(value, option, fallback) {
854
+ if (!value) {
855
+ return fallback;
856
+ }
857
+ const parsed = Number.parseInt(value, 10);
858
+ if (!Number.isFinite(parsed) || parsed <= 0) {
859
+ throw new CliError2(`Invalid ${option} value: ${value}`);
860
+ }
861
+ return parsed;
862
+ }
863
+ function readTaskRunAcceptedArtifactState(input) {
864
+ if (!input.taskId || !input.workspaceDir) {
865
+ return { accepted: false, reason: null };
866
+ }
867
+ const artifactDir = resolve4(input.workspaceDir, "artifacts", input.taskId);
868
+ const reviewStatusPath = resolve4(artifactDir, "review-status.txt");
869
+ const taskResultPath = resolve4(artifactDir, "task-result.json");
870
+ const reviewStatus = readTaskRunReviewStatus(reviewStatusPath);
871
+ if (reviewStatus !== "APPROVED") {
872
+ return { accepted: false, reason: null };
873
+ }
874
+ const taskResult = readJsonFile(taskResultPath, null);
875
+ if (taskResult?.status !== "completed") {
876
+ return { accepted: false, reason: null };
877
+ }
878
+ if (typeof input.artifactNotBeforeMs === "number" && Number.isFinite(input.artifactNotBeforeMs)) {
879
+ try {
880
+ const reviewStatusMtime = statSync(reviewStatusPath).mtimeMs;
881
+ if (reviewStatusMtime + 1000 < input.artifactNotBeforeMs) {
882
+ return { accepted: false, reason: null };
883
+ }
884
+ } catch {
885
+ return { accepted: false, reason: null };
886
+ }
887
+ }
888
+ return {
889
+ accepted: true,
890
+ reason: `task-result.json is completed and review-status.txt is APPROVED for task ${input.taskId}`
891
+ };
892
+ }
893
+ function resolvePiAcceptedArtifactExitGraceMs(env = process.env) {
894
+ const raw = env.RIG_PI_ACCEPTED_ARTIFACT_EXIT_GRACE_MS;
895
+ if (raw === undefined || raw.trim() === "")
896
+ return 30000;
897
+ const parsed = Number.parseInt(raw, 10);
898
+ if (!Number.isFinite(parsed) || parsed < 0)
899
+ return 30000;
900
+ return parsed;
901
+ }
902
+ function resolveTaskRunRetryContext(input) {
903
+ if (!input.taskId || !input.workspaceDir) {
904
+ return { shouldRetry: false, failureDetail: null, nextPrompt: null };
905
+ }
906
+ const artifactDir = resolve4(input.workspaceDir, "artifacts", input.taskId);
907
+ const reviewStatePath = resolve4(artifactDir, "review-state.json");
908
+ const reviewFeedbackPath = resolve4(artifactDir, "review-feedback.md");
909
+ const reviewStatusPath = resolve4(artifactDir, "review-status.txt");
910
+ const failedApproachesPath = resolve4(input.workspaceDir, ".rig", "state", "failed_approaches.md");
911
+ const validationSummaryPath = resolve4(artifactDir, "validation-summary.json");
912
+ const reviewState = readJsonFile(reviewStatePath, null);
913
+ const reviewStatus = readTaskRunReviewStatus(reviewStatusPath);
914
+ const reviewRejected = isTaskRunReviewRejected(reviewState);
915
+ if (reviewStatus === "APPROVED") {
916
+ return { shouldRetry: false, failureDetail: null, nextPrompt: null };
917
+ }
918
+ if (reviewStatus !== "REJECTED" && !reviewRejected) {
919
+ return { shouldRetry: false, failureDetail: null, nextPrompt: null };
920
+ }
921
+ const failureDetail = summarizeTaskRunReviewFailure(reviewState);
922
+ if (input.attempt >= input.maxAttempts) {
923
+ return { shouldRetry: false, failureDetail, nextPrompt: null };
924
+ }
925
+ return {
926
+ shouldRetry: true,
927
+ failureDetail,
928
+ nextPrompt: [
929
+ input.basePrompt,
930
+ "",
931
+ `Retry attempt ${input.attempt + 1}/${input.maxAttempts} for task ${input.taskId}.`,
932
+ "The previous attempt did not satisfy completion verification. Continue from the existing runtime workspace and PR state instead of starting over.",
933
+ "",
934
+ `Latest blocker: ${failureDetail}`,
935
+ "",
936
+ "Inspect these artifacts first:",
937
+ `- ${reviewStatusPath}`,
938
+ `- ${reviewStatePath}`,
939
+ `- ${reviewFeedbackPath}`,
940
+ `- ${failedApproachesPath}`,
941
+ `- ${validationSummaryPath}`,
942
+ "",
943
+ "Resolve the concrete issues recorded there, refresh the task artifacts if needed, rerun validation and completion verification, and do not stop until the task is actually accepted. When Greptile review is required, keep iterating until the review verdict is APPROVE with no unresolved comments."
944
+ ].join(`
945
+ `)
946
+ };
947
+ }
948
+ function summarizeTaskRunReviewFailure(reviewState) {
949
+ if (!reviewState) {
950
+ return "Completion verification rejected the task. Read review-feedback.md for required fixes.";
951
+ }
952
+ const reasons = [
953
+ ...reviewState.local_reasons ?? [],
954
+ ...reviewState.ai_reasons ?? [],
955
+ ...reviewState.ai_warnings ?? []
956
+ ].map((value) => value.trim()).filter(Boolean);
957
+ if (reasons.length > 0) {
958
+ return reasons[0];
959
+ }
960
+ if (reviewState.mode === "required" && reviewState.verdict && reviewState.verdict !== "APPROVE") {
961
+ return `[AI Review] Required mode needs a completed Greptile approval; current verdict is ${reviewState.verdict}.`;
962
+ }
963
+ return "Completion verification rejected the task. Read review-feedback.md for required fixes.";
964
+ }
965
+ function readTaskRunReviewStatus(reviewStatusPath) {
966
+ if (!existsSync2(reviewStatusPath)) {
967
+ return null;
968
+ }
969
+ try {
970
+ const status = readFileSync2(reviewStatusPath, "utf8").trim().toUpperCase();
971
+ return status === "APPROVED" || status === "REJECTED" ? status : null;
972
+ } catch {
973
+ return null;
974
+ }
975
+ }
976
+ function isTaskRunReviewRejected(reviewState) {
977
+ if (!reviewState) {
978
+ return false;
979
+ }
980
+ if (reviewState.approved === false) {
981
+ return true;
982
+ }
983
+ return reviewState.mode === "required" && !!reviewState.verdict && reviewState.verdict !== "APPROVE";
984
+ }
985
+ function runSourceTaskIdentity(sourceTask) {
986
+ return sourceTask;
987
+ }
988
+ async function updateTaskSourceAfterDriverRun(projectRoot, runId, taskId, sourceTask, status, summary, input, updateTaskSource = updateConfiguredTaskSourceTask) {
989
+ if (!taskId)
990
+ return;
991
+ const config = await loadTaskRunAutomationConfig(projectRoot);
992
+ if (!shouldWriteDriverIssueUpdate(config, status))
993
+ return;
994
+ const result = await updateTaskSource(projectRoot, {
995
+ taskId,
996
+ sourceTask: runSourceTaskIdentity(sourceTask),
997
+ update: {
998
+ status,
999
+ comment: buildTaskRunLifecycleComment({
1000
+ runId,
1001
+ status,
1002
+ summary,
1003
+ runtimeWorkspace: input.latestRuntimeWorkspace,
1004
+ logsDir: input.latestLogsDir,
1005
+ sessionDir: input.latestSessionDir,
1006
+ errorText: input.errorText
1007
+ })
1008
+ }
1009
+ });
1010
+ if (!result.updated && (result.source === "plugin" || result.sourceKind)) {
1011
+ throw new Error(`Configured task source${result.sourceKind ? ` (${result.sourceKind})` : ""} did not accept lifecycle update for ${result.taskId}.`);
1012
+ }
1013
+ }
1014
+ function readRunSourceTaskContract(projectRoot, runId, taskId) {
1015
+ const run = readAuthorityRun3(projectRoot, runId);
1016
+ const raw = run?.sourceTask;
1017
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
1018
+ return null;
1019
+ }
1020
+ const record = raw;
1021
+ const id = typeof record.id === "string" && record.id.trim() ? record.id.trim() : taskId?.trim();
1022
+ if (!id) {
1023
+ return null;
1024
+ }
1025
+ return {
1026
+ id,
1027
+ ...optionalStringField(record, "title"),
1028
+ ...optionalStringField(record, "description"),
1029
+ ...optionalStringField(record, "body"),
1030
+ ...optionalStringField(record, "acceptanceCriteria"),
1031
+ ...optionalStringField(record, "acceptance_criteria"),
1032
+ ...optionalStringField(record, "sourceIssueId"),
1033
+ ...optionalStringField(record, "status"),
1034
+ ...optionalStringField(record, "issueType"),
1035
+ ...optionalStringField(record, "role"),
1036
+ ...optionalStringField(record, "externalRef"),
1037
+ scope: stringArrayField(record, "scope"),
1038
+ validation: stringArrayField(record, "validation"),
1039
+ validators: stringArrayField(record, "validators"),
1040
+ labels: stringArrayField(record, "labels")
1041
+ };
1042
+ }
1043
+ function optionalStringField(record, key) {
1044
+ const value = record[String(key)];
1045
+ return typeof value === "string" ? { [key]: value } : {};
1046
+ }
1047
+ function stringArrayField(record, key) {
1048
+ const value = record[key];
1049
+ return Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
1050
+ }
1051
+ async function executeRigOwnedTaskRun(context, input) {
1052
+ const runtimeTaskId = input.taskId?.trim() || `adhoc-${input.runId}`;
1053
+ const sourceTask = readRunSourceTaskContract(context.projectRoot, input.runId, input.taskId);
1054
+ const prompt = buildRunPrompt({
1055
+ projectRoot: context.projectRoot,
1056
+ taskId: input.taskId,
1057
+ fallbackTitle: input.title,
1058
+ fallbackDescription: sourceTask?.description ?? sourceTask?.body ?? null,
1059
+ fallbackAcceptanceCriteria: sourceTask?.acceptanceCriteria ?? sourceTask?.acceptance_criteria ?? null,
1060
+ sourceTask,
1061
+ initialPrompt: input.initialPrompt,
1062
+ runtimeAdapter: input.runtimeAdapter
1063
+ });
1064
+ const startedAt = new Date().toISOString();
1065
+ const hostAgentBinary = await resolvePreferredShellBinary(context.projectRoot);
1066
+ const baseAgentArgs = input.runtimeAdapter === "codex" ? [
1067
+ "__rig_codex_app_server_task_run__",
1068
+ "--runtime-mode",
1069
+ input.runtimeMode,
1070
+ "--interaction-mode",
1071
+ input.interactionMode,
1072
+ ...input.model ? ["--model", input.model] : [],
1073
+ "--prompt"
1074
+ ] : input.runtimeAdapter === "pi" ? [
1075
+ "--print",
1076
+ "--verbose",
1077
+ "--mode",
1078
+ "json",
1079
+ "--no-session",
1080
+ ...input.model ? ["--model", input.model] : []
1081
+ ] : [
1082
+ "--print",
1083
+ "--verbose",
1084
+ "--output-format",
1085
+ "stream-json",
1086
+ "--dangerously-skip-permissions"
1087
+ ];
1088
+ const buildHostAgentCommand = (attemptPrompt) => input.runtimeAdapter === "codex" ? [hostAgentBinary, ...baseAgentArgs, attemptPrompt] : [hostAgentBinary, ...baseAgentArgs];
1089
+ const hostAgentCommand = buildHostAgentCommand(prompt);
1090
+ const hostAgentCommandText = formatCommand(hostAgentCommand);
1091
+ const provisioningAction = startRunAction(context.projectRoot, input.runId, {
1092
+ actionId: `action:${input.runId}:runtime-provision`,
1093
+ actionType: "engine.runtime.provision",
1094
+ title: "Provision dedicated runtime",
1095
+ detail: input.taskId ? `Preparing isolated worktree for ${input.taskId}` : "Preparing isolated runtime",
1096
+ payload: {
1097
+ hostAgentCommand,
1098
+ formattedCommand: hostAgentCommandText,
1099
+ taskId: input.taskId ?? null,
1100
+ mode: "worktree"
1101
+ }
1102
+ });
1103
+ upsertAgentAuthorityRun(context.projectRoot, {
1104
+ runId: input.runId,
1105
+ taskId: runtimeTaskId,
1106
+ createdAt: startedAt,
1107
+ runtimeAdapter: input.runtimeAdapter,
1108
+ status: "created"
1109
+ });
1110
+ patchAuthorityRun(context.projectRoot, input.runId, {
1111
+ status: "preparing",
1112
+ startedAt,
1113
+ completedAt: null,
1114
+ errorText: null,
1115
+ artifactRoot: null,
1116
+ runtimeAdapter: input.runtimeAdapter,
1117
+ runtimeMode: input.runtimeMode,
1118
+ interactionMode: input.interactionMode,
1119
+ runMode: input.runtimeMode === "full-access" ? "autonomous" : "interactive",
1120
+ initialPrompt: input.initialPrompt ?? null,
1121
+ title: input.taskId ? undefined : input.title ?? `Run ${input.runId}`,
1122
+ pid: process.pid
1123
+ });
1124
+ emitServerRunEvent({
1125
+ type: "status",
1126
+ runId: input.runId,
1127
+ status: "preparing",
1128
+ detail: input.taskId ?? input.title ?? runtimeTaskId
1129
+ });
1130
+ appendRunLog(context.projectRoot, input.runId, {
1131
+ id: `log:${input.runId}:start`,
1132
+ title: "Rig task run started",
1133
+ detail: input.taskId ?? input.title ?? runtimeTaskId,
1134
+ tone: "info",
1135
+ status: "preparing",
1136
+ createdAt: startedAt
1137
+ });
1138
+ appendRunLog(context.projectRoot, input.runId, {
1139
+ id: `log:${input.runId}:wrapper-command`,
1140
+ title: "Agent wrapper command",
1141
+ detail: hostAgentCommandText,
1142
+ tone: "tool",
1143
+ status: "preparing",
1144
+ payload: {
1145
+ command: hostAgentCommand,
1146
+ formattedCommand: hostAgentCommandText
1147
+ },
1148
+ createdAt: new Date().toISOString()
1149
+ });
1150
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Agent wrapper command" });
1151
+ const loadedAutomationConfig = await loadTaskRunAutomationConfig(context.projectRoot);
1152
+ const automationConfig = input.prMode ? { ...loadedAutomationConfig ?? {}, pr: { ...loadedAutomationConfig?.pr ?? {}, mode: input.prMode } } : loadedAutomationConfig;
1153
+ const planningClassification = classifyPlanningNeed({ config: automationConfig, sourceTask });
1154
+ patchAuthorityRun(context.projectRoot, input.runId, { planning: planningClassification });
1155
+ if (input.runtimeAdapter === "pi") {
1156
+ for (const stage of ["Connect", "GitHub/task sync", "Prepare workspace", "Launch Pi", "Plan", "Implement"]) {
1157
+ appendPiStageLog({
1158
+ projectRoot: context.projectRoot,
1159
+ runId: input.runId,
1160
+ stage,
1161
+ detail: stage === "Launch Pi" ? "Pi runtime bridge starting with pi-rig environment." : stage === "Plan" ? `${planningClassification.planningRequired ? "recorded" : "skipped"} (${planningClassification.reason}; size=${planningClassification.size}; risk=${planningClassification.risk})` : stage === "Implement" ? "Pi implementation pass is running." : null,
1162
+ status: stage === "Implement" || stage === "Launch Pi" ? "running" : "completed"
1163
+ });
1164
+ }
1165
+ }
1166
+ appendRunTimeline(context.projectRoot, input.runId, {
1167
+ id: `message:${input.runId}:user`,
1168
+ type: "user_message",
1169
+ text: prompt,
1170
+ createdAt: startedAt,
1171
+ state: "completed",
1172
+ completedAt: startedAt
1173
+ });
1174
+ emitServerRunEvent({ type: "timeline", runId: input.runId });
1175
+ await context.emitEvent("command.started", {
1176
+ command: [
1177
+ "rig",
1178
+ "server",
1179
+ "task-run",
1180
+ ...input.taskId ? ["--task", input.taskId] : [],
1181
+ ...input.title ? ["--title", input.title] : []
1182
+ ],
1183
+ formattedCommand: input.taskId ? `rig server task-run --task ${input.taskId}` : `rig server task-run --title ${JSON.stringify(input.title ?? `Run ${input.runId}`)}`,
1184
+ startedAt,
1185
+ dryRun: false
1186
+ });
1187
+ const assistantMessageId = `message:${input.runId}:assistant`;
1188
+ let assistantText = "";
1189
+ const pendingClaudeToolUses = new Map;
1190
+ const pendingCodexToolUses = new Map;
1191
+ let launchAction;
1192
+ let verificationAction;
1193
+ let reviewAction;
1194
+ let verificationStarted = false;
1195
+ let reviewStarted = false;
1196
+ let latestRuntimeWorkspace = null;
1197
+ let latestSessionDir = null;
1198
+ let latestLogsDir = null;
1199
+ let latestProviderCommand = null;
1200
+ let latestRuntimeBranch = null;
1201
+ let snapshotSidecarPromise = null;
1202
+ let dirtyBaselineApplied = false;
1203
+ const childEnv = {
1204
+ ...process.env,
1205
+ ...input.runtimeAdapter === "pi" ? buildPiRigBridgeEnv({
1206
+ projectRoot: context.projectRoot,
1207
+ runId: input.runId,
1208
+ taskId: runtimeTaskId,
1209
+ serverUrl: process.env.RIG_SERVER_URL ?? null,
1210
+ authToken: process.env.RIG_AUTH_TOKEN ?? null,
1211
+ githubToken: process.env.RIG_GITHUB_TOKEN ?? process.env.GITHUB_TOKEN ?? process.env.GH_TOKEN ?? null
1212
+ }) : {
1213
+ PROJECT_RIG_ROOT: context.projectRoot,
1214
+ RIG_TASK_ID: runtimeTaskId,
1215
+ RIG_RUNTIME_ADAPTER: input.runtimeAdapter,
1216
+ RIG_SERVER_RUN_ID: input.runId
1217
+ },
1218
+ ...sourceTask ? { RIG_SOURCE_TASK_JSON: JSON.stringify(sourceTask) } : {}
1219
+ };
1220
+ Object.assign(childEnv, buildTaskRunReviewEnv(automationConfig));
1221
+ const automationLimits = resolveTaskRunAutomationLimits(automationConfig);
1222
+ const maxAttempts = automationLimits.maxValidationAttempts;
1223
+ const promoteToValidating = (detail) => {
1224
+ if (verificationStarted)
1225
+ return;
1226
+ verificationStarted = true;
1227
+ patchAuthorityRun(context.projectRoot, input.runId, { status: "validating" });
1228
+ emitServerRunEvent({ type: "status", runId: input.runId, status: "validating", detail: detail ?? null });
1229
+ verificationAction = startRunAction(context.projectRoot, input.runId, {
1230
+ actionId: `action:${input.runId}:completion-verification`,
1231
+ actionType: "completion-verification",
1232
+ title: "Run completion verification",
1233
+ detail: detail ?? "Rig completion-verification hook is running."
1234
+ });
1235
+ };
1236
+ const promoteToReviewing = (detail) => {
1237
+ if (reviewStarted)
1238
+ return;
1239
+ reviewStarted = true;
1240
+ patchAuthorityRun(context.projectRoot, input.runId, { status: "reviewing" });
1241
+ emitServerRunEvent({ type: "status", runId: input.runId, status: "reviewing", detail: detail ?? null });
1242
+ reviewAction = startRunAction(context.projectRoot, input.runId, {
1243
+ actionId: `action:${input.runId}:review-closeout`,
1244
+ actionType: "run.review",
1245
+ title: "Finalize run review",
1246
+ detail: detail ?? "Verifier review is running."
1247
+ });
1248
+ };
1249
+ const handleWrapperEvent = (rawPayload) => {
1250
+ let event = null;
1251
+ try {
1252
+ event = JSON.parse(rawPayload);
1253
+ } catch {
1254
+ return false;
1255
+ }
1256
+ if (!event?.type)
1257
+ return false;
1258
+ const payload = event.payload ?? {};
1259
+ if (event.type === "runtime.provision.completed") {
1260
+ latestRuntimeWorkspace = typeof payload.workspaceDir === "string" ? payload.workspaceDir : latestRuntimeWorkspace;
1261
+ latestSessionDir = typeof payload.sessionDir === "string" ? payload.sessionDir : latestSessionDir;
1262
+ latestLogsDir = typeof payload.logsDir === "string" ? payload.logsDir : latestLogsDir;
1263
+ const runtimeId = typeof payload.runtimeId === "string" ? payload.runtimeId : null;
1264
+ latestRuntimeBranch = runtimeId;
1265
+ provisioningAction.complete(latestRuntimeWorkspace ?? "Runtime ready.", {
1266
+ runtimeId: runtimeId ?? null,
1267
+ workspaceDir: latestRuntimeWorkspace,
1268
+ sessionDir: latestSessionDir,
1269
+ logsDir: latestLogsDir,
1270
+ contextFile: payload.contextFile ?? null
1271
+ });
1272
+ patchAuthorityRun(context.projectRoot, input.runId, {
1273
+ status: "running",
1274
+ worktreePath: latestRuntimeWorkspace,
1275
+ artifactRoot: latestRuntimeWorkspace && input.taskId ? resolve4(latestRuntimeWorkspace, "artifacts", input.taskId) : null,
1276
+ logRoot: latestLogsDir,
1277
+ sessionPath: latestSessionDir ? resolve4(latestSessionDir, "session.json") : null,
1278
+ sessionLogPath: latestLogsDir ? resolve4(latestLogsDir, "agent-stdout.log") : null,
1279
+ branch: runtimeId
1280
+ });
1281
+ if (!dirtyBaselineApplied && input.baselineMode === "dirty-snapshot" && latestRuntimeWorkspace) {
1282
+ dirtyBaselineApplied = true;
1283
+ const dirty = applyDirtyBaselineSnapshot({ sourceRoot: context.projectRoot, targetRoot: latestRuntimeWorkspace });
1284
+ appendRunLog(context.projectRoot, input.runId, {
1285
+ id: `log:${input.runId}:dirty-baseline`,
1286
+ title: "Dirty baseline snapshot",
1287
+ detail: dirty.detail,
1288
+ tone: dirty.applied ? "tool" : "info",
1289
+ status: dirty.applied ? "completed" : "skipped",
1290
+ createdAt: new Date().toISOString()
1291
+ });
1292
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Dirty baseline snapshot" });
1293
+ }
1294
+ if (!snapshotSidecarPromise && runtimeId && loadRuntimeSnapshotConfig(context.projectRoot).enabled) {
1295
+ snapshotSidecarPromise = (async () => {
1296
+ const { sidecar, error } = await resolveTaskRunSnapshotSidecar({
1297
+ projectRoot: context.projectRoot,
1298
+ runtimeId,
1299
+ workspaceDir: latestRuntimeWorkspace,
1300
+ listRuntimes: listAgentRuntimes,
1301
+ startSidecar: (runtime) => startRuntimeSnapshotSidecar(runtime, { instanceId: `${runtime.id}-run-${input.runId}` })
1302
+ });
1303
+ if (!sidecar) {
1304
+ appendRunLog(context.projectRoot, input.runId, {
1305
+ id: `log:${input.runId}:snapshot-unavailable`,
1306
+ title: "Runtime snapshotting unavailable",
1307
+ detail: error ?? `Could not locate runtime metadata for ${runtimeId}.`,
1308
+ tone: "error",
1309
+ status: "running",
1310
+ createdAt: new Date().toISOString()
1311
+ });
1312
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Runtime snapshotting unavailable" });
1313
+ return null;
1314
+ }
1315
+ return sidecar;
1316
+ })();
1317
+ }
1318
+ emitServerRunEvent({
1319
+ type: "status",
1320
+ runId: input.runId,
1321
+ status: "running",
1322
+ detail: latestRuntimeWorkspace
1323
+ });
1324
+ return true;
1325
+ }
1326
+ if (event.type === "runtime.provision.failed") {
1327
+ const detail = typeof payload.error === "string" ? payload.error : "Runtime provisioning failed.";
1328
+ provisioningAction.fail(detail, payload);
1329
+ emitServerRunEvent({ type: "failed", runId: input.runId, error: detail });
1330
+ return true;
1331
+ }
1332
+ if (event.type === "provider.launch") {
1333
+ const command = Array.isArray(payload.command) ? payload.command.filter((value) => typeof value === "string") : [];
1334
+ latestProviderCommand = command.length > 0 ? command : null;
1335
+ const commandText = command.length > 0 ? formatCommand(command) : String(payload.provider ?? "provider");
1336
+ launchAction = startRunAction(context.projectRoot, input.runId, {
1337
+ actionId: `action:${input.runId}:provider-launch`,
1338
+ actionType: "agent.launch",
1339
+ title: "Launch agent runtime",
1340
+ detail: commandText,
1341
+ payload: {
1342
+ runtimeAdapter: input.runtimeAdapter,
1343
+ runtimeMode: input.runtimeMode,
1344
+ interactionMode: input.interactionMode,
1345
+ workspaceDir: payload.workspaceDir ?? latestRuntimeWorkspace,
1346
+ command,
1347
+ formattedCommand: commandText
1348
+ }
1349
+ });
1350
+ appendRunLog(context.projectRoot, input.runId, {
1351
+ id: `log:${input.runId}:provider-command`,
1352
+ title: "Agent launch command",
1353
+ detail: commandText,
1354
+ tone: "tool",
1355
+ status: "running",
1356
+ payload: {
1357
+ command,
1358
+ formattedCommand: commandText
1359
+ },
1360
+ createdAt: new Date().toISOString()
1361
+ });
1362
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Agent launch command" });
1363
+ return true;
1364
+ }
1365
+ if (event.type === "provider.completed") {
1366
+ const exitCode = typeof payload.exitCode === "number" ? payload.exitCode : null;
1367
+ if (launchAction) {
1368
+ if (exitCode === 0) {
1369
+ launchAction.complete("Agent runtime finished.");
1370
+ } else {
1371
+ launchAction.fail(`Agent runtime failed (${String(exitCode ?? "unknown")})`, payload);
1372
+ }
1373
+ }
1374
+ return true;
1375
+ }
1376
+ return false;
1377
+ };
1378
+ const nextRunLogId = createRunLogIdFactory(input.runId);
1379
+ const handleAgentStdoutLine = (line) => {
1380
+ const trimmed = line.trim();
1381
+ if (!trimmed)
1382
+ return;
1383
+ if (trimmed.startsWith("__RIG_WRAPPER_EVENT__")) {
1384
+ if (handleWrapperEvent(trimmed.slice("__RIG_WRAPPER_EVENT__".length))) {
1385
+ emitServerRunEvent({ type: "timeline", runId: input.runId });
1386
+ return;
1387
+ }
1388
+ }
1389
+ const completionLine = classifyCompletionVerificationLine(trimmed);
1390
+ if (completionLine.isCompletionLine) {
1391
+ promoteToValidating(trimmed);
1392
+ if (completionLine.isVerifierReview) {
1393
+ if (verificationAction) {
1394
+ verificationAction.complete("Completion verification checks finished.");
1395
+ }
1396
+ promoteToReviewing(trimmed);
1397
+ }
1398
+ appendRunLog(context.projectRoot, input.runId, {
1399
+ id: nextRunLogId(),
1400
+ title: verificationStarted ? "Completion verification" : "Agent output",
1401
+ detail: trimmed,
1402
+ tone: "info",
1403
+ status: reviewStarted ? "reviewing" : verificationStarted ? "validating" : "running",
1404
+ createdAt: new Date().toISOString()
1405
+ });
1406
+ emitServerRunEvent({ type: "log", runId: input.runId, title: verificationStarted ? "Completion verification" : "Agent output" });
1407
+ return;
1408
+ }
1409
+ try {
1410
+ const record = JSON.parse(trimmed);
1411
+ const liveLogStatus = reviewStarted ? "reviewing" : verificationStarted ? "validating" : "running";
1412
+ const providerLogs = input.runtimeAdapter === "codex" ? buildCodexLogsFromRecord({
1413
+ runId: input.runId,
1414
+ record,
1415
+ createdAtFallback: new Date().toISOString(),
1416
+ status: liveLogStatus,
1417
+ pendingToolUses: pendingCodexToolUses
1418
+ }) : buildClaudeLogsFromRecord({
1419
+ runId: input.runId,
1420
+ record,
1421
+ createdAtFallback: new Date().toISOString(),
1422
+ status: liveLogStatus,
1423
+ pendingToolUses: pendingClaudeToolUses
1424
+ });
1425
+ if (providerLogs.length > 0) {
1426
+ for (const providerLog of providerLogs) {
1427
+ appendRunLog(context.projectRoot, input.runId, providerLog);
1428
+ emitServerRunEvent({ type: "log", runId: input.runId, title: providerLog.title });
1429
+ }
1430
+ }
1431
+ if (input.runtimeAdapter === "codex") {
1432
+ const nextAssistantText = extractCodexAssistantMessageText(record);
1433
+ if (nextAssistantText) {
1434
+ assistantText = nextAssistantText;
1435
+ appendRunTimeline(context.projectRoot, input.runId, {
1436
+ id: assistantMessageId,
1437
+ type: "assistant_message",
1438
+ text: assistantText,
1439
+ state: "streaming",
1440
+ createdAt: new Date().toISOString()
1441
+ });
1442
+ emitServerRunEvent({ type: "timeline", runId: input.runId });
1443
+ return;
1444
+ }
1445
+ if (providerLogs.length > 0 || isCodexExecRecord(record)) {
1446
+ return;
1447
+ }
1448
+ }
1449
+ if (record.type === "message_update") {
1450
+ const assistantMessageEvent = record.assistantMessageEvent && typeof record.assistantMessageEvent === "object" ? record.assistantMessageEvent : null;
1451
+ if (assistantMessageEvent?.type === "text_delta" && typeof assistantMessageEvent.delta === "string") {
1452
+ assistantText += assistantMessageEvent.delta;
1453
+ appendRunTimeline(context.projectRoot, input.runId, {
1454
+ id: assistantMessageId,
1455
+ type: "assistant_message",
1456
+ text: assistantText,
1457
+ state: "streaming",
1458
+ createdAt: new Date().toISOString()
1459
+ });
1460
+ emitServerRunEvent({ type: "timeline", runId: input.runId });
1461
+ return;
1462
+ }
1463
+ }
1464
+ if (record.type === "stream_event") {
1465
+ const event = record.event && typeof record.event === "object" ? record.event : null;
1466
+ const delta = event?.delta && typeof event.delta === "object" ? event.delta : null;
1467
+ if (delta?.type === "text_delta" && typeof delta.text === "string") {
1468
+ assistantText += delta.text;
1469
+ appendRunTimeline(context.projectRoot, input.runId, {
1470
+ id: assistantMessageId,
1471
+ type: "assistant_message",
1472
+ text: assistantText,
1473
+ state: "streaming",
1474
+ createdAt: new Date().toISOString()
1475
+ });
1476
+ emitServerRunEvent({ type: "timeline", runId: input.runId });
1477
+ return;
1478
+ }
1479
+ }
1480
+ if (record.type === "assistant") {
1481
+ const message = record.message && typeof record.message === "object" ? record.message : record;
1482
+ const content = Array.isArray(message.content) ? message.content : [];
1483
+ const nextText = content.map((entry) => entry && typeof entry === "object" && entry.type === "text" ? String(entry.text ?? "") : "").join("");
1484
+ if (nextText.length > assistantText.length) {
1485
+ assistantText = nextText;
1486
+ appendRunTimeline(context.projectRoot, input.runId, {
1487
+ id: assistantMessageId,
1488
+ type: "assistant_message",
1489
+ text: assistantText,
1490
+ state: "streaming",
1491
+ createdAt: new Date().toISOString()
1492
+ });
1493
+ emitServerRunEvent({ type: "timeline", runId: input.runId });
1494
+ }
1495
+ return;
1496
+ }
1497
+ if (providerLogs.length > 0) {
1498
+ return;
1499
+ }
1500
+ } catch {}
1501
+ appendRunLog(context.projectRoot, input.runId, {
1502
+ id: nextRunLogId(),
1503
+ title: verificationStarted ? "Completion verification" : "Agent output",
1504
+ detail: trimmed,
1505
+ tone: "info",
1506
+ status: reviewStarted ? "reviewing" : verificationStarted ? "validating" : "running",
1507
+ createdAt: new Date().toISOString()
1508
+ });
1509
+ emitServerRunEvent({ type: "log", runId: input.runId, title: verificationStarted ? "Completion verification" : "Agent output" });
1510
+ };
1511
+ let currentPrompt = prompt;
1512
+ let exit = null;
1513
+ let reviewFailureDetail = null;
1514
+ const stderrLines = [];
1515
+ const piAcceptedArtifactExitGraceMs = resolvePiAcceptedArtifactExitGraceMs(childEnv);
1516
+ for (let attempt = 1;attempt <= maxAttempts; attempt += 1) {
1517
+ const attemptHostAgentCommand = buildHostAgentCommand(currentPrompt);
1518
+ const child = spawn(attemptHostAgentCommand[0], attemptHostAgentCommand.slice(1), {
1519
+ cwd: context.projectRoot,
1520
+ env: childEnv,
1521
+ stdio: ["pipe", "pipe", "pipe"]
1522
+ });
1523
+ const forwardSigterm = () => {
1524
+ try {
1525
+ child.kill("SIGTERM");
1526
+ } catch {}
1527
+ };
1528
+ process.once("SIGTERM", forwardSigterm);
1529
+ if (input.runtimeAdapter === "claude-code" || input.runtimeAdapter === "pi") {
1530
+ child.stdin.write(currentPrompt);
1531
+ child.stdin.end();
1532
+ } else {
1533
+ child.stdin.end();
1534
+ }
1535
+ const stdout = createLineInterface({ input: child.stdout });
1536
+ stdout.on("line", handleAgentStdoutLine);
1537
+ const stderr = createLineInterface({ input: child.stderr });
1538
+ stderr.on("line", (line) => {
1539
+ const trimmed = line.trim();
1540
+ if (!trimmed)
1541
+ return;
1542
+ stderrLines.push(trimmed);
1543
+ appendRunLog(context.projectRoot, input.runId, {
1544
+ id: nextRunLogId(),
1545
+ title: "Agent stderr",
1546
+ detail: trimmed,
1547
+ tone: "error",
1548
+ status: reviewStarted ? "reviewing" : verificationStarted ? "validating" : "running",
1549
+ createdAt: new Date().toISOString()
1550
+ });
1551
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Agent stderr" });
1552
+ });
1553
+ let acceptedArtifactExit = false;
1554
+ let acceptedArtifactObservedAt = null;
1555
+ let acceptedArtifactPollTimer = null;
1556
+ let acceptedArtifactKillTimer = null;
1557
+ const attemptExit = await new Promise((resolve5) => {
1558
+ let settled = false;
1559
+ const settle = (result) => {
1560
+ if (settled)
1561
+ return;
1562
+ settled = true;
1563
+ if (acceptedArtifactPollTimer)
1564
+ clearInterval(acceptedArtifactPollTimer);
1565
+ resolve5(result);
1566
+ };
1567
+ const pollAcceptedArtifacts = () => {
1568
+ const artifactState = readTaskRunAcceptedArtifactState({
1569
+ taskId: input.taskId ?? runtimeTaskId,
1570
+ workspaceDir: latestRuntimeWorkspace,
1571
+ artifactNotBeforeMs: Date.parse(startedAt)
1572
+ });
1573
+ if (!artifactState.accepted) {
1574
+ acceptedArtifactObservedAt = null;
1575
+ return;
1576
+ }
1577
+ acceptedArtifactObservedAt ??= Date.now();
1578
+ if (Date.now() - acceptedArtifactObservedAt < piAcceptedArtifactExitGraceMs || acceptedArtifactExit) {
1579
+ return;
1580
+ }
1581
+ acceptedArtifactExit = true;
1582
+ const detail = [
1583
+ artifactState.reason ?? "Rig accepted task artifacts are present.",
1584
+ `Pi has not exited after ${piAcceptedArtifactExitGraceMs}ms; terminating the stale Pi process so Rig can continue PR/issue closeout.`
1585
+ ].join(" ");
1586
+ appendRunLog(context.projectRoot, input.runId, {
1587
+ id: `log:${input.runId}:pi-accepted-artifacts-exit`,
1588
+ title: "Pi accepted artifacts; continuing closeout",
1589
+ detail,
1590
+ tone: "info",
1591
+ status: "running",
1592
+ createdAt: new Date().toISOString()
1593
+ });
1594
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Pi accepted artifacts; continuing closeout" });
1595
+ try {
1596
+ child.kill("SIGTERM");
1597
+ } catch {}
1598
+ acceptedArtifactKillTimer = setTimeout(() => {
1599
+ try {
1600
+ child.kill("SIGKILL");
1601
+ } catch {}
1602
+ settle({ code: 0, signal: "SIGKILL" });
1603
+ }, 5000);
1604
+ };
1605
+ child.once("error", (error) => {
1606
+ if (acceptedArtifactKillTimer)
1607
+ clearTimeout(acceptedArtifactKillTimer);
1608
+ console.error(`[rig] agent process error: ${error.message}`);
1609
+ settle({ code: 1, signal: null, error });
1610
+ });
1611
+ child.once("close", (code, signal) => {
1612
+ if (acceptedArtifactKillTimer)
1613
+ clearTimeout(acceptedArtifactKillTimer);
1614
+ settle(acceptedArtifactExit ? { code: 0, signal: signal ?? "SIGTERM" } : { code, signal });
1615
+ });
1616
+ if (input.runtimeAdapter === "pi") {
1617
+ acceptedArtifactPollTimer = setInterval(pollAcceptedArtifacts, 5000);
1618
+ acceptedArtifactPollTimer.unref?.();
1619
+ }
1620
+ });
1621
+ await finalizeTaskRunSnapshot({
1622
+ snapshotSidecarPromise,
1623
+ latestProviderCommand,
1624
+ hostAgentCommand: attemptHostAgentCommand,
1625
+ exit: attemptExit
1626
+ });
1627
+ const pendingLogs = input.runtimeAdapter === "codex" ? flushPendingCodexToolUseLogs({
1628
+ runId: input.runId,
1629
+ status: typeof attemptExit.code === "number" && attemptExit.code === 0 ? "completed" : "failed",
1630
+ pendingToolUses: pendingCodexToolUses
1631
+ }) : flushPendingClaudeToolUseLogs({
1632
+ runId: input.runId,
1633
+ status: typeof attemptExit.code === "number" && attemptExit.code === 0 ? "completed" : "failed",
1634
+ pendingToolUses: pendingClaudeToolUses
1635
+ });
1636
+ for (const pendingLog of pendingLogs) {
1637
+ appendRunLog(context.projectRoot, input.runId, pendingLog);
1638
+ emitServerRunEvent({ type: "log", runId: input.runId, title: pendingLog.title });
1639
+ }
1640
+ process.off("SIGTERM", forwardSigterm);
1641
+ if (attemptExit.error) {
1642
+ throw new CliError2(`Task run failed to start: ${attemptExit.error.message}`, 1);
1643
+ }
1644
+ const retryContext = resolveTaskRunRetryContext({
1645
+ projectRoot: context.projectRoot,
1646
+ taskId: input.taskId ?? null,
1647
+ workspaceDir: latestRuntimeWorkspace,
1648
+ attempt,
1649
+ maxAttempts,
1650
+ basePrompt: prompt
1651
+ });
1652
+ if (retryContext.shouldRetry && retryContext.nextPrompt) {
1653
+ const failureDetail = retryContext.failureDetail ?? summarizeValidationFailure(context.projectRoot, input.taskId ?? null) ?? "Rig validation failed.";
1654
+ const retrySteering = buildPiValidationRetrySteeringPrompt({
1655
+ taskId: input.taskId ?? runtimeTaskId,
1656
+ attempt,
1657
+ maxAttempts,
1658
+ failureDetail,
1659
+ validationOutput: summarizeValidationFailure(context.projectRoot, input.taskId ?? null)
1660
+ });
1661
+ currentPrompt = [retryContext.nextPrompt, "", "Rig Pi steering message:", retrySteering].join(`
1662
+ `);
1663
+ reviewFailureDetail = failureDetail;
1664
+ patchAuthorityRun(context.projectRoot, input.runId, {
1665
+ status: "validating",
1666
+ completedAt: null,
1667
+ errorText: null
1668
+ });
1669
+ appendPiStageLog({
1670
+ projectRoot: context.projectRoot,
1671
+ runId: input.runId,
1672
+ stage: "Validate",
1673
+ detail: failureDetail,
1674
+ tone: "error",
1675
+ status: "validating"
1676
+ });
1677
+ appendRunLog(context.projectRoot, input.runId, {
1678
+ id: `log:${input.runId}:retry:${attempt + 1}`,
1679
+ title: `Steering Pi to retry validation (attempt ${attempt + 1}/${maxAttempts})`,
1680
+ detail: retrySteering,
1681
+ tone: "info",
1682
+ status: "running",
1683
+ createdAt: new Date().toISOString()
1684
+ });
1685
+ emitServerRunEvent({
1686
+ type: "status",
1687
+ runId: input.runId,
1688
+ status: "validating",
1689
+ detail: failureDetail
1690
+ });
1691
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Validate failed" });
1692
+ emitServerRunEvent({ type: "log", runId: input.runId, title: `Steering Pi to retry validation (attempt ${attempt + 1}/${maxAttempts})` });
1693
+ continue;
1694
+ }
1695
+ exit = attemptExit;
1696
+ reviewFailureDetail = retryContext.failureDetail;
1697
+ break;
1698
+ }
1699
+ if (!exit) {
1700
+ throw new CliError2(`Task run for ${runtimeTaskId} exhausted ${maxAttempts} attempts without a terminal result.`, 1);
1701
+ }
1702
+ if (exit.error) {
1703
+ throw new CliError2(`Task run failed to start: ${exit.error.message}`, 1);
1704
+ }
1705
+ if (assistantText.trim()) {
1706
+ appendRunTimeline(context.projectRoot, input.runId, {
1707
+ id: assistantMessageId,
1708
+ type: "assistant_message",
1709
+ text: assistantText,
1710
+ state: "completed",
1711
+ createdAt: new Date().toISOString(),
1712
+ completedAt: new Date().toISOString()
1713
+ });
1714
+ emitServerRunEvent({ type: "timeline", runId: input.runId });
1715
+ }
1716
+ if (exit.code !== 0 || reviewFailureDetail) {
1717
+ const failedAt = new Date().toISOString();
1718
+ const failureDetail = reviewFailureDetail ?? summarizeValidationFailure(context.projectRoot, input.taskId ?? null) ?? summarizeTaskRunProcessFailure({
1719
+ exitCode: exit.code,
1720
+ signal: exit.signal,
1721
+ stderrLines
1722
+ });
1723
+ if (verificationAction && !reviewStarted) {
1724
+ verificationAction.fail(failureDetail);
1725
+ }
1726
+ if (reviewAction) {
1727
+ reviewAction.fail(failureDetail);
1728
+ }
1729
+ let terminalFailureDetail = failureDetail;
1730
+ const terminalRunStatus = reviewFailureDetail ? "needs_attention" : "failed";
1731
+ try {
1732
+ await updateTaskSourceAfterDriverRun(context.projectRoot, input.runId, input.taskId, sourceTask, "failed", terminalRunStatus === "needs_attention" ? "Rig task run needs attention." : "Rig task run failed.", {
1733
+ latestRuntimeWorkspace,
1734
+ latestLogsDir,
1735
+ latestSessionDir,
1736
+ errorText: failureDetail
1737
+ });
1738
+ } catch (error) {
1739
+ terminalFailureDetail = `${failureDetail}
1740
+ Failed to update task source for ${input.taskId ?? runtimeTaskId} to failed: ${error instanceof Error ? error.message : String(error)}`;
1741
+ }
1742
+ patchAuthorityRun(context.projectRoot, input.runId, {
1743
+ status: terminalRunStatus,
1744
+ completedAt: failedAt,
1745
+ errorText: terminalFailureDetail
1746
+ });
1747
+ appendRunLog(context.projectRoot, input.runId, {
1748
+ id: `log:${input.runId}:${terminalRunStatus}`,
1749
+ title: terminalRunStatus === "needs_attention" ? "Task run needs attention" : "Task run failed",
1750
+ detail: terminalFailureDetail,
1751
+ tone: "error",
1752
+ status: terminalRunStatus,
1753
+ createdAt: failedAt
1754
+ });
1755
+ emitServerRunEvent({
1756
+ type: "failed",
1757
+ runId: input.runId,
1758
+ error: terminalFailureDetail
1759
+ });
1760
+ throw new CliError2(terminalFailureDetail, exit.code ?? 1);
1761
+ }
1762
+ const runPiPrFeedbackFix = async (message) => {
1763
+ appendPiStageLog({
1764
+ projectRoot: context.projectRoot,
1765
+ runId: input.runId,
1766
+ stage: "Implement",
1767
+ detail: "Pi is applying PR review/CI feedback.",
1768
+ status: "running"
1769
+ });
1770
+ appendRunLog(context.projectRoot, input.runId, {
1771
+ id: `log:${input.runId}:pr-feedback-pi:${Date.now()}`,
1772
+ title: "Steering Pi from PR feedback",
1773
+ detail: message,
1774
+ tone: "info",
1775
+ status: "reviewing",
1776
+ createdAt: new Date().toISOString()
1777
+ });
1778
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Steering Pi from PR feedback" });
1779
+ const feedbackCommand = buildHostAgentCommand(message);
1780
+ const child = spawn(feedbackCommand[0], feedbackCommand.slice(1), {
1781
+ cwd: latestRuntimeWorkspace ?? context.projectRoot,
1782
+ env: childEnv,
1783
+ stdio: ["pipe", "pipe", "pipe"]
1784
+ });
1785
+ if (input.runtimeAdapter === "claude-code" || input.runtimeAdapter === "pi") {
1786
+ child.stdin.write(message);
1787
+ }
1788
+ child.stdin.end();
1789
+ const stdout = createLineInterface({ input: child.stdout });
1790
+ stdout.on("line", (line) => {
1791
+ const trimmed = line.trim();
1792
+ if (!trimmed)
1793
+ return;
1794
+ appendRunLog(context.projectRoot, input.runId, {
1795
+ id: nextRunLogId(),
1796
+ title: "Pi PR feedback fix output",
1797
+ detail: trimmed,
1798
+ tone: "info",
1799
+ status: "reviewing",
1800
+ createdAt: new Date().toISOString()
1801
+ });
1802
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Pi PR feedback fix output" });
1803
+ });
1804
+ const stderr = createLineInterface({ input: child.stderr });
1805
+ stderr.on("line", (line) => {
1806
+ const trimmed = line.trim();
1807
+ if (!trimmed)
1808
+ return;
1809
+ appendRunLog(context.projectRoot, input.runId, {
1810
+ id: nextRunLogId(),
1811
+ title: "Pi PR feedback fix stderr",
1812
+ detail: trimmed,
1813
+ tone: "error",
1814
+ status: "reviewing",
1815
+ createdAt: new Date().toISOString()
1816
+ });
1817
+ emitServerRunEvent({ type: "log", runId: input.runId, title: "Pi PR feedback fix stderr" });
1818
+ });
1819
+ const exitCode = await new Promise((resolve5) => {
1820
+ child.once("error", () => resolve5(1));
1821
+ child.once("close", (code) => resolve5(code ?? 1));
1822
+ });
1823
+ if (exitCode !== 0) {
1824
+ throw new Error(`Pi PR feedback fix failed with exit code ${exitCode}.`);
1825
+ }
1826
+ };
1827
+ try {
1828
+ appendPiStageLog({
1829
+ projectRoot: context.projectRoot,
1830
+ runId: input.runId,
1831
+ stage: "Validate",
1832
+ detail: "Rig validation accepted the task run.",
1833
+ status: "completed"
1834
+ });
1835
+ const postValidation = await runTaskRunPostValidationLifecycle({
1836
+ projectRoot: context.projectRoot,
1837
+ runId: input.runId,
1838
+ taskId: input.taskId,
1839
+ runtimeTaskId,
1840
+ runtimeWorkspace: latestRuntimeWorkspace,
1841
+ branch: latestRuntimeBranch,
1842
+ config: automationConfig,
1843
+ sourceTask,
1844
+ steerPi: input.runtimeAdapter === "pi" ? runPiPrFeedbackFix : undefined,
1845
+ appendStage: (stage, detail, status, tone) => appendPiStageLog({
1846
+ projectRoot: context.projectRoot,
1847
+ runId: input.runId,
1848
+ stage,
1849
+ detail,
1850
+ status,
1851
+ tone
1852
+ })
1853
+ });
1854
+ if (postValidation.status === "needs_attention") {
1855
+ const failureDetail = postValidation.pr?.actionableFeedback.join(`
1856
+ `) || "PR automation needs attention.";
1857
+ const failedAt = new Date().toISOString();
1858
+ patchAuthorityRun(context.projectRoot, input.runId, {
1859
+ status: "needs_attention",
1860
+ completedAt: failedAt,
1861
+ errorText: failureDetail
1862
+ });
1863
+ emitServerRunEvent({ type: "failed", runId: input.runId, error: failureDetail });
1864
+ throw new CliError2(failureDetail, 1);
1865
+ }
1866
+ } catch (error) {
1867
+ if (error instanceof CliError2) {
1868
+ throw error;
1869
+ }
1870
+ const failureDetail = `Failed to complete PR/issue closeout for ${input.taskId ?? runtimeTaskId}: ${error instanceof Error ? error.message : String(error)}`;
1871
+ const failedAt = new Date().toISOString();
1872
+ patchAuthorityRun(context.projectRoot, input.runId, {
1873
+ status: "failed",
1874
+ completedAt: failedAt,
1875
+ errorText: failureDetail
1876
+ });
1877
+ appendRunLog(context.projectRoot, input.runId, {
1878
+ id: `log:${input.runId}:pr-closeout-failed`,
1879
+ title: "PR/issue closeout failed",
1880
+ detail: failureDetail,
1881
+ tone: "error",
1882
+ status: "failed",
1883
+ createdAt: failedAt
1884
+ });
1885
+ emitServerRunEvent({ type: "failed", runId: input.runId, error: failureDetail });
1886
+ throw new CliError2(failureDetail, 1);
1887
+ }
1888
+ if (verificationAction && !reviewStarted) {
1889
+ verificationAction.complete("Completion verification checks finished.");
1890
+ }
1891
+ if (!reviewAction) {
1892
+ promoteToReviewing("Task finished; finalizing run state.");
1893
+ }
1894
+ if (reviewAction) {
1895
+ reviewAction.complete("Run marked completed.");
1896
+ }
1897
+ patchAuthorityRun(context.projectRoot, input.runId, {
1898
+ status: "completed",
1899
+ completedAt: new Date().toISOString()
1900
+ });
1901
+ emitServerRunEvent({ type: "completed", runId: input.runId });
1902
+ await context.emitEvent("command.finished", {
1903
+ command: [
1904
+ "rig",
1905
+ "server",
1906
+ "task-run",
1907
+ ...input.taskId ? ["--task", input.taskId] : [],
1908
+ ...input.title ? ["--title", input.title] : []
1909
+ ],
1910
+ formattedCommand: input.taskId ? `rig server task-run --task ${input.taskId}` : `rig server task-run --title ${JSON.stringify(input.title ?? `Run ${input.runId}`)}`,
1911
+ exitCode: 0,
1912
+ durationMs: 0,
1913
+ startedAt,
1914
+ finishedAt: new Date().toISOString()
1915
+ });
1916
+ }
1917
+ export {
1918
+ taskRunStageLogId,
1919
+ summarizeTaskRunProcessFailure,
1920
+ runTaskRunPostValidationLifecycle,
1921
+ resolveTaskRunAutomationLimits,
1922
+ readTaskRunAcceptedArtifactState,
1923
+ executeRigOwnedTaskRun,
1924
+ createRunLogIdFactory,
1925
+ classifyValidationRetryOutcome,
1926
+ classifyPlanningNeed,
1927
+ classifyCompletionVerificationLine,
1928
+ canonicalPiRunStages,
1929
+ buildTaskRunReviewEnv,
1930
+ buildPiValidationRetrySteeringPrompt,
1931
+ buildPiRigBridgeEnv,
1932
+ applyDirtyBaselineSnapshot
1933
+ };