forked from nicobailon/pi-subagents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubagent-runner.ts
More file actions
834 lines (766 loc) · 25.6 KB
/
subagent-runner.ts
File metadata and controls
834 lines (766 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
import { spawn, spawnSync } from "node:child_process";
import * as fs from "node:fs";
import { createRequire } from "node:module";
import * as os from "node:os";
import * as path from "node:path";
import { pathToFileURL } from "node:url";
import { appendJsonl, getArtifactPaths } from "./artifacts.js";
import { getPiSpawnCommand } from "./pi-spawn.js";
import { persistSingleOutput } from "./single-output.js";
import {
type ArtifactConfig,
type ArtifactPaths,
DEFAULT_MAX_OUTPUT,
type MaxOutputConfig,
truncateOutput,
getSubagentDepthEnv,
} from "./types.js";
import {
type RunnerSubagentStep as SubagentStep,
type RunnerStep,
isParallelGroup,
flattenSteps,
mapConcurrent,
aggregateParallelOutputs,
MAX_PARALLEL_CONCURRENCY,
} from "./parallel-utils.js";
interface SubagentRunConfig {
id: string;
steps: RunnerStep[];
resultPath: string;
cwd: string;
placeholder: string;
taskIndex?: number;
totalTasks?: number;
maxOutput?: MaxOutputConfig;
artifactsDir?: string;
artifactConfig?: Partial<ArtifactConfig>;
share?: boolean;
sessionDir?: string;
asyncDir: string;
sessionId?: string | null;
piPackageRoot?: string;
}
interface StepResult {
agent: string;
output: string;
success: boolean;
skipped?: boolean;
artifactPaths?: ArtifactPaths;
truncated?: boolean;
}
const require = createRequire(import.meta.url);
function findLatestSessionFile(sessionDir: string): string | null {
try {
const files = fs
.readdirSync(sessionDir)
.filter((f) => f.endsWith(".jsonl"))
.map((f) => path.join(sessionDir, f));
if (files.length === 0) return null;
files.sort((a, b) => fs.statSync(b).mtimeMs - fs.statSync(a).mtimeMs);
return files[0] ?? null;
} catch {
return null;
}
}
interface TokenUsage {
input: number;
output: number;
total: number;
}
function parseSessionTokens(sessionDir: string): TokenUsage | null {
const sessionFile = findLatestSessionFile(sessionDir);
if (!sessionFile) return null;
try {
const content = fs.readFileSync(sessionFile, "utf-8");
let input = 0;
let output = 0;
for (const line of content.split("\n")) {
if (!line.trim()) continue;
try {
const entry = JSON.parse(line);
if (entry.usage) {
input += entry.usage.inputTokens ?? entry.usage.input ?? 0;
output += entry.usage.outputTokens ?? entry.usage.output ?? 0;
}
} catch {}
}
return { input, output, total: input + output };
} catch {
return null;
}
}
function runPiStreaming(
args: string[],
cwd: string,
outputFile: string,
env?: Record<string, string | undefined>,
piPackageRoot?: string,
): Promise<{ stdout: string; exitCode: number | null }> {
return new Promise((resolve) => {
const outputStream = fs.createWriteStream(outputFile, { flags: "w" });
const spawnEnv = { ...process.env, ...(env ?? {}), ...getSubagentDepthEnv() };
const spawnSpec = getPiSpawnCommand(args, piPackageRoot ? { piPackageRoot } : undefined);
const child = spawn(spawnSpec.command, spawnSpec.args, { cwd, stdio: ["ignore", "pipe", "pipe"], env: spawnEnv });
let stdout = "";
child.stdout.on("data", (chunk: Buffer) => {
const text = chunk.toString();
stdout += text;
outputStream.write(text);
});
child.stderr.on("data", (chunk: Buffer) => {
outputStream.write(chunk.toString());
});
child.on("close", (exitCode) => {
outputStream.end();
resolve({ stdout, exitCode });
});
child.on("error", () => {
outputStream.end();
resolve({ stdout, exitCode: 1 });
});
});
}
function resolvePiPackageRootFallback(): string {
// Try to resolve the main entry point and walk up to find the package root
const entryPoint = require.resolve("@mariozechner/pi-coding-agent");
// Entry point is typically /path/to/dist/index.js, so go up to find package root
let dir = path.dirname(entryPoint);
while (dir !== path.dirname(dir)) {
const pkgJsonPath = path.join(dir, "package.json");
try {
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
if (pkg.name === "@mariozechner/pi-coding-agent") return dir;
} catch {}
dir = path.dirname(dir);
}
throw new Error("Could not resolve @mariozechner/pi-coding-agent package root");
}
async function exportSessionHtml(sessionFile: string, outputDir: string, piPackageRoot?: string): Promise<string> {
const pkgRoot = piPackageRoot ?? resolvePiPackageRootFallback();
const exportModulePath = path.join(pkgRoot, "dist", "core", "export-html", "index.js");
const moduleUrl = pathToFileURL(exportModulePath).href;
const mod = await import(moduleUrl);
const exportFromFile = (mod as { exportFromFile?: (inputPath: string, options?: { outputPath?: string }) => string })
.exportFromFile;
if (typeof exportFromFile !== "function") {
throw new Error("exportFromFile not available");
}
const outputPath = path.join(outputDir, `${path.basename(sessionFile, ".jsonl")}.html`);
return exportFromFile(sessionFile, { outputPath });
}
function createShareLink(htmlPath: string): { shareUrl: string; gistUrl: string } | { error: string } {
try {
const auth = spawnSync("gh", ["auth", "status"], { encoding: "utf-8" });
if (auth.status !== 0) {
return { error: "GitHub CLI is not logged in. Run 'gh auth login' first." };
}
} catch {
return { error: "GitHub CLI (gh) is not installed." };
}
try {
const result = spawnSync("gh", ["gist", "create", htmlPath], { encoding: "utf-8" });
if (result.status !== 0) {
const err = (result.stderr || "").trim() || "Failed to create gist.";
return { error: err };
}
const gistUrl = (result.stdout || "").trim();
const gistId = gistUrl.split("/").pop();
if (!gistId) return { error: "Failed to parse gist ID." };
const shareUrl = `https://shittycodingagent.ai/session/?${gistId}`;
return { shareUrl, gistUrl };
} catch (err) {
return { error: String(err) };
}
}
function writeJson(filePath: string, payload: object): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, JSON.stringify(payload, null, 2), "utf-8");
}
function formatDuration(ms: number): string {
if (ms < 1000) return `${ms}ms`;
if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`;
const minutes = Math.floor(ms / 60000);
const seconds = Math.floor((ms % 60000) / 1000);
return `${minutes}m${seconds}s`;
}
function writeRunLog(
logPath: string,
input: {
id: string;
mode: "single" | "chain";
cwd: string;
startedAt: number;
endedAt: number;
steps: Array<{
agent: string;
status: string;
durationMs?: number;
}>;
summary: string;
truncated: boolean;
artifactsDir?: string;
sessionFile?: string;
shareUrl?: string;
shareError?: string;
},
): void {
const lines: string[] = [];
lines.push(`# Subagent run ${input.id}`);
lines.push("");
lines.push(`- **Mode:** ${input.mode}`);
lines.push(`- **CWD:** ${input.cwd}`);
lines.push(`- **Started:** ${new Date(input.startedAt).toISOString()}`);
lines.push(`- **Ended:** ${new Date(input.endedAt).toISOString()}`);
lines.push(`- **Duration:** ${formatDuration(input.endedAt - input.startedAt)}`);
if (input.sessionFile) lines.push(`- **Session:** ${input.sessionFile}`);
if (input.shareUrl) lines.push(`- **Share:** ${input.shareUrl}`);
if (input.shareError) lines.push(`- **Share error:** ${input.shareError}`);
if (input.artifactsDir) lines.push(`- **Artifacts:** ${input.artifactsDir}`);
lines.push("");
lines.push("## Steps");
lines.push("| Step | Agent | Status | Duration |");
lines.push("| --- | --- | --- | --- |");
input.steps.forEach((step, i) => {
const duration = step.durationMs !== undefined ? formatDuration(step.durationMs) : "-";
lines.push(`| ${i + 1} | ${step.agent} | ${step.status} | ${duration} |`);
});
lines.push("");
lines.push("## Summary");
if (input.truncated) {
lines.push("_Output truncated_");
lines.push("");
}
lines.push(input.summary.trim() || "(no output)");
lines.push("");
fs.writeFileSync(logPath, lines.join("\n"), "utf-8");
}
/** Context for running a single step */
interface SingleStepContext {
previousOutput: string;
placeholder: string;
cwd: string;
sessionEnabled: boolean;
sessionDir?: string;
artifactsDir?: string;
artifactConfig?: Partial<ArtifactConfig>;
id: string;
flatIndex: number;
flatStepCount: number;
outputFile: string;
piPackageRoot?: string;
}
/** Run a single pi agent step, returning output and metadata */
async function runSingleStep(
step: SubagentStep,
ctx: SingleStepContext,
): Promise<{ agent: string; output: string; exitCode: number | null; artifactPaths?: ArtifactPaths }> {
const args = ["-p"];
if (!ctx.sessionEnabled) {
args.push("--no-session");
}
if (ctx.sessionDir) {
try { fs.mkdirSync(ctx.sessionDir, { recursive: true }); } catch {}
args.push("--session-dir", ctx.sessionDir);
}
if (step.model) args.push("--models", step.model);
const toolExtensionPaths: string[] = [];
if (step.tools?.length) {
const builtinTools: string[] = [];
for (const tool of step.tools) {
if (tool.includes("/") || tool.endsWith(".ts") || tool.endsWith(".js")) {
toolExtensionPaths.push(tool);
} else {
builtinTools.push(tool);
}
}
if (builtinTools.length > 0) args.push("--tools", builtinTools.join(","));
}
if (step.extensions !== undefined) {
args.push("--no-extensions");
for (const extPath of step.extensions) args.push("--extension", extPath);
} else {
for (const extPath of toolExtensionPaths) args.push("--extension", extPath);
}
let tmpDir: string | null = null;
if (step.systemPrompt) {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-subagent-"));
const promptPath = path.join(tmpDir, "prompt.md");
fs.writeFileSync(promptPath, step.systemPrompt);
args.push("--append-system-prompt", promptPath);
}
const placeholderRegex = new RegExp(ctx.placeholder.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g");
const task = step.task.replace(placeholderRegex, () => ctx.previousOutput);
const TASK_ARG_LIMIT = 8000;
if (task.length > TASK_ARG_LIMIT) {
if (!tmpDir) tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-subagent-"));
const taskFilePath = path.join(tmpDir, "task.md");
fs.writeFileSync(taskFilePath, `Task: ${task}`, { mode: 0o600 });
args.push(`@${taskFilePath}`);
} else {
args.push(`Task: ${task}`);
}
let artifactPaths: ArtifactPaths | undefined;
if (ctx.artifactsDir && ctx.artifactConfig?.enabled !== false) {
const index = ctx.flatStepCount > 1 ? ctx.flatIndex : undefined;
artifactPaths = getArtifactPaths(ctx.artifactsDir, ctx.id, step.agent, index);
fs.mkdirSync(ctx.artifactsDir, { recursive: true });
if (ctx.artifactConfig?.includeInput !== false) {
fs.writeFileSync(artifactPaths.inputPath, `# Task for ${step.agent}\n\n${task}`, "utf-8");
}
}
const mcpEnv: Record<string, string | undefined> = {};
if (step.mcpDirectTools?.length) {
mcpEnv.MCP_DIRECT_TOOLS = step.mcpDirectTools.join(",");
} else {
mcpEnv.MCP_DIRECT_TOOLS = "__none__";
}
const result = await runPiStreaming(args, step.cwd ?? ctx.cwd, ctx.outputFile, mcpEnv, ctx.piPackageRoot);
if (tmpDir) {
try { fs.rmSync(tmpDir, { recursive: true }); } catch {}
}
const output = (result.stdout || "").trim();
let outputForSummary = output;
if (step.outputPath && result.exitCode === 0) {
const persisted = persistSingleOutput(step.outputPath, output);
if (persisted.savedPath) {
outputForSummary = output
? `${output}\n\n📄 Output saved to: ${persisted.savedPath}`
: `📄 Output saved to: ${persisted.savedPath}`;
} else if (persisted.error) {
outputForSummary = output
? `${output}\n\n⚠️ Failed to save output to: ${step.outputPath}\n${persisted.error}`
: `⚠️ Failed to save output to: ${step.outputPath}\n${persisted.error}`;
}
}
if (artifactPaths && ctx.artifactConfig?.enabled !== false) {
if (ctx.artifactConfig?.includeOutput !== false) {
fs.writeFileSync(artifactPaths.outputPath, output, "utf-8");
}
if (ctx.artifactConfig?.includeMetadata !== false) {
fs.writeFileSync(
artifactPaths.metadataPath,
JSON.stringify({
runId: ctx.id,
agent: step.agent,
task,
exitCode: result.exitCode,
skills: step.skills,
timestamp: Date.now(),
}, null, 2),
"utf-8",
);
}
}
return { agent: step.agent, output: outputForSummary, exitCode: result.exitCode, artifactPaths };
}
async function runSubagent(config: SubagentRunConfig): Promise<void> {
const { id, steps, resultPath, cwd, placeholder, taskIndex, totalTasks, maxOutput, artifactsDir, artifactConfig } =
config;
let previousOutput = "";
const results: StepResult[] = [];
const overallStartTime = Date.now();
const shareEnabled = config.share === true;
const sessionEnabled = Boolean(config.sessionDir) || shareEnabled;
const asyncDir = config.asyncDir;
const statusPath = path.join(asyncDir, "status.json");
const eventsPath = path.join(asyncDir, "events.jsonl");
const logPath = path.join(asyncDir, `subagent-log-${id}.md`);
let previousCumulativeTokens: TokenUsage = { input: 0, output: 0, total: 0 };
// Flatten steps for status tracking (parallel groups expand to individual entries)
const flatSteps = flattenSteps(steps);
const statusPayload: {
runId: string;
mode: "single" | "chain";
state: "queued" | "running" | "complete" | "failed";
startedAt: number;
endedAt?: number;
lastUpdate: number;
pid: number;
cwd: string;
currentStep: number;
steps: Array<{
agent: string;
status: "pending" | "running" | "complete" | "failed";
startedAt?: number;
endedAt?: number;
durationMs?: number;
exitCode?: number | null;
error?: string;
tokens?: TokenUsage;
skills?: string[];
}>;
artifactsDir?: string;
sessionDir?: string;
outputFile?: string;
totalTokens?: TokenUsage;
sessionFile?: string;
shareUrl?: string;
gistUrl?: string;
shareError?: string;
error?: string;
} = {
runId: id,
mode: flatSteps.length > 1 ? "chain" : "single",
state: "running",
startedAt: overallStartTime,
lastUpdate: overallStartTime,
pid: process.pid,
cwd,
currentStep: 0,
steps: flatSteps.map((step) => ({ agent: step.agent, status: "pending", skills: step.skills })),
artifactsDir,
sessionDir: config.sessionDir,
outputFile: path.join(asyncDir, "output-0.log"),
};
fs.mkdirSync(asyncDir, { recursive: true });
writeJson(statusPath, statusPayload);
appendJsonl(
eventsPath,
JSON.stringify({
type: "subagent.run.started",
ts: overallStartTime,
runId: id,
mode: statusPayload.mode,
cwd,
pid: process.pid,
}),
);
// Track the flat index into statusPayload.steps across sequential + parallel steps
let flatIndex = 0;
for (let stepIndex = 0; stepIndex < steps.length; stepIndex++) {
const step = steps[stepIndex];
if (isParallelGroup(step)) {
// === PARALLEL STEP GROUP ===
const group = step;
const concurrency = group.concurrency ?? MAX_PARALLEL_CONCURRENCY;
const failFast = group.failFast ?? false;
const groupStartFlatIndex = flatIndex;
let aborted = false;
// Mark all tasks in the group as running
const groupStartTime = Date.now();
for (let t = 0; t < group.parallel.length; t++) {
const fi = groupStartFlatIndex + t;
statusPayload.steps[fi].status = "running";
statusPayload.steps[fi].startedAt = groupStartTime;
}
statusPayload.currentStep = groupStartFlatIndex;
statusPayload.lastUpdate = groupStartTime;
statusPayload.outputFile = path.join(asyncDir, `output-${groupStartFlatIndex}.log`);
writeJson(statusPath, statusPayload);
appendJsonl(eventsPath, JSON.stringify({
type: "subagent.parallel.started",
ts: groupStartTime,
runId: id,
stepIndex,
agents: group.parallel.map((t) => t.agent),
count: group.parallel.length,
}));
const parallelResults = await mapConcurrent(
group.parallel,
concurrency,
async (task, taskIdx) => {
if (aborted && failFast) {
return { agent: task.agent, output: "(skipped — fail-fast)", exitCode: -1 as number | null, skipped: true };
}
const fi = groupStartFlatIndex + taskIdx;
const taskStartTime = Date.now();
appendJsonl(eventsPath, JSON.stringify({
type: "subagent.step.started", ts: taskStartTime, runId: id, stepIndex: fi, agent: task.agent,
}));
// Each parallel task gets its own session subdirectory to avoid conflicts
const taskSessionDir = config.sessionDir
? path.join(config.sessionDir, `parallel-${taskIdx}`)
: undefined;
const singleResult = await runSingleStep(task, {
previousOutput, placeholder, cwd, sessionEnabled,
sessionDir: taskSessionDir,
artifactsDir, artifactConfig, id,
flatIndex: fi, flatStepCount: flatSteps.length,
outputFile: path.join(asyncDir, `output-${fi}.log`),
piPackageRoot: config.piPackageRoot,
});
const taskEndTime = Date.now();
const taskDuration = taskEndTime - taskStartTime;
statusPayload.steps[fi].status = singleResult.exitCode === 0 ? "complete" : "failed";
statusPayload.steps[fi].endedAt = taskEndTime;
statusPayload.steps[fi].durationMs = taskDuration;
statusPayload.steps[fi].exitCode = singleResult.exitCode;
statusPayload.lastUpdate = taskEndTime;
writeJson(statusPath, statusPayload);
appendJsonl(eventsPath, JSON.stringify({
type: singleResult.exitCode === 0 ? "subagent.step.completed" : "subagent.step.failed",
ts: taskEndTime, runId: id, stepIndex: fi, agent: task.agent,
exitCode: singleResult.exitCode, durationMs: taskDuration,
}));
if (singleResult.exitCode !== 0 && failFast) aborted = true;
return { ...singleResult, skipped: false };
},
);
flatIndex += group.parallel.length;
// Aggregate token usage from parallel task session dirs
if (config.sessionDir) {
for (let t = 0; t < group.parallel.length; t++) {
const taskSessionDir = path.join(config.sessionDir, `parallel-${t}`);
const taskTokens = parseSessionTokens(taskSessionDir);
if (taskTokens) {
const fi = groupStartFlatIndex + t;
statusPayload.steps[fi].tokens = taskTokens;
previousCumulativeTokens = {
input: previousCumulativeTokens.input + taskTokens.input,
output: previousCumulativeTokens.output + taskTokens.output,
total: previousCumulativeTokens.total + taskTokens.total,
};
}
}
statusPayload.totalTokens = { ...previousCumulativeTokens };
statusPayload.lastUpdate = Date.now();
writeJson(statusPath, statusPayload);
}
// Collect results
for (const pr of parallelResults) {
results.push({
agent: pr.agent,
output: pr.output,
success: pr.exitCode === 0,
skipped: pr.skipped,
artifactPaths: pr.artifactPaths,
});
}
// Aggregate parallel outputs for {previous}
previousOutput = aggregateParallelOutputs(
parallelResults.map((r) => ({ agent: r.agent, output: r.output, exitCode: r.exitCode })),
);
appendJsonl(eventsPath, JSON.stringify({
type: "subagent.parallel.completed",
ts: Date.now(),
runId: id,
stepIndex,
success: parallelResults.every((r) => r.exitCode === 0 || r.exitCode === -1),
}));
// If any parallel task failed (not skipped), stop the chain
if (parallelResults.some((r) => r.exitCode !== 0 && r.exitCode !== -1)) {
break;
}
} else {
// === SEQUENTIAL STEP ===
const seqStep = step as SubagentStep;
const stepStartTime = Date.now();
statusPayload.currentStep = flatIndex;
statusPayload.steps[flatIndex].status = "running";
statusPayload.steps[flatIndex].skills = seqStep.skills;
statusPayload.steps[flatIndex].startedAt = stepStartTime;
statusPayload.lastUpdate = stepStartTime;
statusPayload.outputFile = path.join(asyncDir, `output-${flatIndex}.log`);
writeJson(statusPath, statusPayload);
appendJsonl(eventsPath, JSON.stringify({
type: "subagent.step.started",
ts: stepStartTime,
runId: id,
stepIndex: flatIndex,
agent: seqStep.agent,
}));
const singleResult = await runSingleStep(seqStep, {
previousOutput, placeholder, cwd, sessionEnabled,
sessionDir: config.sessionDir,
artifactsDir, artifactConfig, id,
flatIndex, flatStepCount: flatSteps.length,
outputFile: path.join(asyncDir, `output-${flatIndex}.log`),
piPackageRoot: config.piPackageRoot,
});
previousOutput = singleResult.output;
results.push({
agent: singleResult.agent,
output: singleResult.output,
success: singleResult.exitCode === 0,
artifactPaths: singleResult.artifactPaths,
});
const cumulativeTokens = config.sessionDir ? parseSessionTokens(config.sessionDir) : null;
const stepTokens: TokenUsage | null = cumulativeTokens
? {
input: cumulativeTokens.input - previousCumulativeTokens.input,
output: cumulativeTokens.output - previousCumulativeTokens.output,
total: cumulativeTokens.total - previousCumulativeTokens.total,
}
: null;
if (cumulativeTokens) {
previousCumulativeTokens = cumulativeTokens;
}
const stepEndTime = Date.now();
statusPayload.steps[flatIndex].status = singleResult.exitCode === 0 ? "complete" : "failed";
statusPayload.steps[flatIndex].endedAt = stepEndTime;
statusPayload.steps[flatIndex].durationMs = stepEndTime - stepStartTime;
statusPayload.steps[flatIndex].exitCode = singleResult.exitCode;
if (stepTokens) {
statusPayload.steps[flatIndex].tokens = stepTokens;
statusPayload.totalTokens = { ...previousCumulativeTokens };
}
statusPayload.lastUpdate = stepEndTime;
writeJson(statusPath, statusPayload);
appendJsonl(eventsPath, JSON.stringify({
type: singleResult.exitCode === 0 ? "subagent.step.completed" : "subagent.step.failed",
ts: stepEndTime,
runId: id,
stepIndex: flatIndex,
agent: seqStep.agent,
exitCode: singleResult.exitCode,
durationMs: stepEndTime - stepStartTime,
tokens: stepTokens,
}));
flatIndex++;
if (singleResult.exitCode !== 0) {
break;
}
}
}
let summary = results.map((r) => `${r.agent}:\n${r.output}`).join("\n\n");
let truncated = false;
if (maxOutput) {
const config = { ...DEFAULT_MAX_OUTPUT, ...maxOutput };
const lastArtifactPath = results[results.length - 1]?.artifactPaths?.outputPath;
const truncResult = truncateOutput(summary, config, lastArtifactPath);
if (truncResult.truncated) {
summary = truncResult.text;
truncated = true;
}
}
const agentName = flatSteps.length === 1
? flatSteps[0].agent
: `chain:${flatSteps.map((s) => s.agent).join("->")}`;
let sessionFile: string | undefined;
let shareUrl: string | undefined;
let gistUrl: string | undefined;
let shareError: string | undefined;
if (shareEnabled && config.sessionDir) {
sessionFile = findLatestSessionFile(config.sessionDir) ?? undefined;
if (sessionFile) {
try {
const htmlPath = await exportSessionHtml(sessionFile, config.sessionDir, config.piPackageRoot);
const share = createShareLink(htmlPath);
if ("error" in share) shareError = share.error;
else {
shareUrl = share.shareUrl;
gistUrl = share.gistUrl;
}
} catch (err) {
shareError = String(err);
}
} else {
shareError = "Session file not found.";
}
}
const runEndedAt = Date.now();
statusPayload.state = results.every((r) => r.success) ? "complete" : "failed";
statusPayload.endedAt = runEndedAt;
statusPayload.lastUpdate = runEndedAt;
statusPayload.sessionFile = sessionFile;
statusPayload.shareUrl = shareUrl;
statusPayload.gistUrl = gistUrl;
statusPayload.shareError = shareError;
if (statusPayload.state === "failed") {
const failedStep = statusPayload.steps.find((s) => s.status === "failed");
if (failedStep?.agent) {
statusPayload.error = `Step failed: ${failedStep.agent}`;
}
}
writeJson(statusPath, statusPayload);
appendJsonl(
eventsPath,
JSON.stringify({
type: "subagent.run.completed",
ts: runEndedAt,
runId: id,
status: statusPayload.state,
durationMs: runEndedAt - overallStartTime,
}),
);
writeRunLog(logPath, {
id,
mode: statusPayload.mode,
cwd,
startedAt: overallStartTime,
endedAt: runEndedAt,
steps: statusPayload.steps.map((step) => ({
agent: step.agent,
status: step.status,
durationMs: step.durationMs,
})),
summary,
truncated,
artifactsDir,
sessionFile,
shareUrl,
shareError,
});
try {
fs.mkdirSync(path.dirname(resultPath), { recursive: true });
fs.writeFileSync(
resultPath,
JSON.stringify({
id,
agent: agentName,
success: results.every((r) => r.success),
summary,
results: results.map((r) => ({
agent: r.agent,
output: r.output,
success: r.success,
skipped: r.skipped || undefined,
artifactPaths: r.artifactPaths,
truncated: r.truncated,
})),
exitCode: results.every((r) => r.success) ? 0 : 1,
timestamp: runEndedAt,
durationMs: runEndedAt - overallStartTime,
truncated,
artifactsDir,
cwd,
asyncDir,
sessionId: config.sessionId,
sessionFile,
shareUrl,
gistUrl,
shareError,
...(taskIndex !== undefined && { taskIndex }),
...(totalTasks !== undefined && { totalTasks }),
}),
);
} catch (err) {
console.error(`Failed to write result file ${resultPath}:`, err);
}
}
const configArg = process.argv[2];
if (configArg) {
try {
const configJson = fs.readFileSync(configArg, "utf-8");
const config = JSON.parse(configJson) as SubagentRunConfig;
try {
fs.unlinkSync(configArg);
} catch {}
runSubagent(config).catch((runErr) => {
console.error("Subagent runner error:", runErr);
process.exit(1);
});
} catch (err) {
console.error("Subagent runner error:", err);
process.exit(1);
}
} else {
let input = "";
process.stdin.setEncoding("utf-8");
process.stdin.on("data", (chunk) => {
input += chunk;
});
process.stdin.on("end", () => {
try {
const config = JSON.parse(input) as SubagentRunConfig;
runSubagent(config).catch((runErr) => {
console.error("Subagent runner error:", runErr);
process.exit(1);
});
} catch (err) {
console.error("Subagent runner error:", err);
process.exit(1);
}
});
}