@fitchmultz/pi-subagents 0.0.0-stage → 0.44.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1645 -0
- package/LICENSE +21 -0
- package/README.md +1529 -2
- package/agents/context-builder.md +97 -0
- package/agents/debugger.md +51 -0
- package/agents/delegate.md +13 -0
- package/agents/fixer.md +37 -0
- package/agents/oracle.md +71 -0
- package/agents/planner.md +72 -0
- package/agents/researcher.md +51 -0
- package/agents/reviewer-claude.md +69 -0
- package/agents/reviewer-gpt.md +68 -0
- package/agents/reviewer-ponytail.md +82 -0
- package/agents/reviewer-security.md +81 -0
- package/agents/reviewer.md +69 -0
- package/agents/scout.md +66 -0
- package/agents/ui-designer.md +51 -0
- package/agents/watcher.md +26 -0
- package/agents/worker.md +46 -0
- package/agents/writer.md +36 -0
- package/dist/agents/agent-management.js +923 -0
- package/dist/agents/agent-scope.js +7 -0
- package/dist/agents/agent-selection.js +20 -0
- package/dist/agents/agent-serializer.js +100 -0
- package/dist/agents/agents.js +752 -0
- package/dist/agents/chain-serializer.js +367 -0
- package/dist/agents/frontmatter.js +21 -0
- package/dist/agents/identity.js +29 -0
- package/dist/agents/skills.js +570 -0
- package/dist/extension/build-info.js +1 -0
- package/dist/extension/compact-tools.js +112 -0
- package/dist/extension/config.js +22 -0
- package/dist/extension/control-notices.js +29 -0
- package/dist/extension/doctor.js +143 -0
- package/dist/extension/fanout-child.js +326 -0
- package/dist/extension/index.js +577 -0
- package/dist/extension/schemas.js +416 -0
- package/dist/extension/tool-input.js +20 -0
- package/dist/extension/tool-result.js +14 -0
- package/dist/history/canonical-result.js +81 -0
- package/dist/history/database.js +72 -0
- package/dist/history/index.js +194 -0
- package/dist/history/ingest.js +447 -0
- package/dist/history/queries.js +350 -0
- package/dist/history/store.js +216 -0
- package/dist/history/types.js +4 -0
- package/dist/history/worker.js +461 -0
- package/dist/intercom/intercom-bridge.js +112 -0
- package/dist/intercom/live-intercom.js +94 -0
- package/dist/intercom/result-intercom.js +373 -0
- package/dist/pi-intercom/agent-dir.js +10 -0
- package/dist/pi-intercom/broker/broker.js +426 -0
- package/dist/pi-intercom/broker/client.js +492 -0
- package/dist/pi-intercom/broker/framing.js +65 -0
- package/dist/pi-intercom/broker/paths.js +42 -0
- package/dist/pi-intercom/broker/pid.js +66 -0
- package/dist/pi-intercom/broker/spawn.js +221 -0
- package/dist/pi-intercom/config.js +102 -0
- package/dist/pi-intercom/index.js +2942 -0
- package/dist/pi-intercom/reply-tracker.js +182 -0
- package/dist/pi-intercom/session-targets.js +128 -0
- package/dist/pi-intercom/subagent-live-events.js +132 -0
- package/dist/pi-intercom/topics.js +157 -0
- package/dist/pi-intercom/types.js +163 -0
- package/dist/pi-intercom/ui/compose.js +269 -0
- package/dist/pi-intercom/ui/inline-message.js +80 -0
- package/dist/pi-intercom/ui/session-list.js +124 -0
- package/dist/runs/background/async-control.js +95 -0
- package/dist/runs/background/async-execution.js +676 -0
- package/dist/runs/background/async-job-tracker.js +383 -0
- package/dist/runs/background/async-result-file.js +58 -0
- package/dist/runs/background/async-resume.js +305 -0
- package/dist/runs/background/async-status.js +442 -0
- package/dist/runs/background/completion-dedupe.js +69 -0
- package/dist/runs/background/completion-delivery.js +245 -0
- package/dist/runs/background/notify.js +71 -0
- package/dist/runs/background/parallel-groups.js +55 -0
- package/dist/runs/background/result-watcher.js +426 -0
- package/dist/runs/background/run-id-resolver.js +64 -0
- package/dist/runs/background/run-status.js +382 -0
- package/dist/runs/background/stale-run-reconciler.js +335 -0
- package/dist/runs/background/subagent-runner-launcher.js +55 -0
- package/dist/runs/background/subagent-runner.js +1872 -0
- package/dist/runs/background/top-level-async.js +5 -0
- package/dist/runs/foreground/chain-clarify.js +1152 -0
- package/dist/runs/foreground/clarify-invocation.js +72 -0
- package/dist/runs/foreground/execution-input.js +423 -0
- package/dist/runs/foreground/foreground-control.js +817 -0
- package/dist/runs/foreground/question-control.js +77 -0
- package/dist/runs/foreground/run-async-path.js +205 -0
- package/dist/runs/foreground/subagent-executor.js +442 -0
- package/dist/runs/foreground/subagent-params.js +165 -0
- package/dist/runs/foreground/wait-run.js +139 -0
- package/dist/runs/shared/acceptance-contract.js +253 -0
- package/dist/runs/shared/acceptance-evaluation.js +256 -0
- package/dist/runs/shared/acceptance-finalization.js +280 -0
- package/dist/runs/shared/acceptance-reports.js +147 -0
- package/dist/runs/shared/acceptance.js +4 -0
- package/dist/runs/shared/chain-outputs.js +110 -0
- package/dist/runs/shared/child-attempt.js +693 -0
- package/dist/runs/shared/child-execution-cwd.js +96 -0
- package/dist/runs/shared/child-observations.js +114 -0
- package/dist/runs/shared/claude-code.js +199 -0
- package/dist/runs/shared/completion-guard.js +31 -0
- package/dist/runs/shared/dynamic-fanout.js +274 -0
- package/dist/runs/shared/history-index.js +72 -0
- package/dist/runs/shared/mcp-direct-tool-allowlist.js +334 -0
- package/dist/runs/shared/model-fallback.js +185 -0
- package/dist/runs/shared/mutating-tool-guard.js +170 -0
- package/dist/runs/shared/native-finalization.js +101 -0
- package/dist/runs/shared/native-usage.js +153 -0
- package/dist/runs/shared/nested-events.js +700 -0
- package/dist/runs/shared/nested-path.js +48 -0
- package/dist/runs/shared/nested-render.js +108 -0
- package/dist/runs/shared/parallel-utils.js +61 -0
- package/dist/runs/shared/parent-receipts.js +68 -0
- package/dist/runs/shared/parent-usage.js +121 -0
- package/dist/runs/shared/pi-args.js +310 -0
- package/dist/runs/shared/pi-spawn.js +52 -0
- package/dist/runs/shared/run-history-queries.js +45 -0
- package/dist/runs/shared/run-history.js +103 -0
- package/dist/runs/shared/run-id-ambiguity.js +4 -0
- package/dist/runs/shared/run-records.js +679 -0
- package/dist/runs/shared/session-cwd-preload.js +33 -0
- package/dist/runs/shared/single-output.js +222 -0
- package/dist/runs/shared/streaming-text.js +15 -0
- package/dist/runs/shared/structured-output.js +55 -0
- package/dist/runs/shared/subagent-control.js +179 -0
- package/dist/runs/shared/subagent-prompt-runtime.js +197 -0
- package/dist/runs/shared/subagent-tool-loop-guard.js +66 -0
- package/dist/runs/shared/supervisor-questions.js +320 -0
- package/dist/runs/shared/workflow-graph.js +214 -0
- package/dist/runs/shared/workflow-policy.js +84 -0
- package/dist/runs/shared/worktree.js +525 -0
- package/dist/shared/agent-context-policy.js +126 -0
- package/dist/shared/artifacts.js +96 -0
- package/dist/shared/atomic-json.js +13 -0
- package/dist/shared/cooperative.js +20 -0
- package/dist/shared/execution-cwd.js +14 -0
- package/dist/shared/file-coalescer.js +21 -0
- package/dist/shared/fork-context.js +49 -0
- package/dist/shared/formatters.js +85 -0
- package/dist/shared/journal-reader.js +407 -0
- package/dist/shared/lazy-tools.js +32 -0
- package/dist/shared/model-info.js +68 -0
- package/dist/shared/native-session.js +15 -0
- package/dist/shared/native-tui.js +18 -0
- package/dist/shared/native-typebox.js +20 -0
- package/dist/shared/post-exit-stdio-guard.js +197 -0
- package/dist/shared/prompt-sections.js +8 -0
- package/dist/shared/session-entries.js +52 -0
- package/dist/shared/session-identity.js +14 -0
- package/dist/shared/settings.js +254 -0
- package/dist/shared/status-format.js +111 -0
- package/dist/shared/temp-root.js +114 -0
- package/dist/shared/types.js +207 -0
- package/dist/shared/ui-mode.js +3 -0
- package/dist/shared/utils.js +334 -0
- package/dist/slash/prompt-template-bridge.js +298 -0
- package/dist/slash/slash-bridge.js +116 -0
- package/dist/slash/slash-commands.js +607 -0
- package/dist/slash/slash-live-state.js +246 -0
- package/dist/tui/action-hints.js +75 -0
- package/dist/tui/agent-history.js +197 -0
- package/dist/tui/agent-view.js +1656 -0
- package/dist/tui/render.js +1515 -0
- package/docs/intercom.md +579 -0
- package/docs/performance.md +121 -0
- package/package.json +101 -4
- package/prompts/gather-context-and-clarify.md +14 -0
- package/prompts/parallel-cleanup.md +62 -0
- package/prompts/parallel-context-build.md +56 -0
- package/prompts/parallel-handoff-plan.md +62 -0
- package/prompts/parallel-research.md +51 -0
- package/prompts/parallel-review.md +59 -0
- package/prompts/review-loop.md +42 -0
- package/scripts/build.mjs +134 -0
- package/scripts/check-compat.mjs +48 -0
- package/scripts/compat-host.mjs +19 -0
- package/scripts/linux-smoke.sh +51 -0
- package/scripts/local-install-smoke.mjs +66 -0
- package/scripts/native-package-smoke.mjs +82 -0
- package/scripts/package-smoke.mjs +205 -0
- package/scripts/prepare.mjs +67 -0
- package/scripts/real-pi-smoke.mjs +415 -0
- package/scripts/run-tests.mjs +149 -0
- package/skills/pi-intercom/SKILL.md +113 -0
- package/skills/pi-intercom/evals/evals.json +29 -0
- package/skills/pi-intercom/evals/trigger-evals.json +47 -0
- package/skills/pi-intercom/references/peer-sessions.md +69 -0
- package/skills/pi-subagents/SKILL.md +76 -0
- package/skills/pi-subagents/evals/evals.json +29 -0
- package/skills/pi-subagents/evals/trigger-evals.json +42 -0
- package/skills/pi-subagents/references/full-orchestration-guide.md +863 -0
- package/src/agents/agent-management.ts +813 -0
- package/src/agents/agent-scope.ts +7 -0
- package/src/agents/agent-selection.ts +23 -0
- package/src/agents/agent-serializer.ts +99 -0
- package/src/agents/agents.ts +916 -0
- package/src/agents/chain-serializer.ts +329 -0
- package/src/agents/frontmatter.ts +21 -0
- package/src/agents/identity.ts +30 -0
- package/src/agents/skills.ts +637 -0
- package/src/extension/build-info.ts +2 -0
- package/src/extension/compact-tools.ts +114 -0
- package/src/extension/config.ts +23 -0
- package/src/extension/control-notices.ts +47 -0
- package/src/extension/doctor.ts +171 -0
- package/src/extension/fanout-child.ts +312 -0
- package/src/extension/index.ts +649 -0
- package/src/extension/schemas.ts +444 -0
- package/src/extension/tool-input.ts +19 -0
- package/src/extension/tool-result.ts +16 -0
- package/src/history/canonical-result.ts +62 -0
- package/src/history/database.ts +59 -0
- package/src/history/index.ts +152 -0
- package/src/history/ingest.ts +332 -0
- package/src/history/queries.ts +241 -0
- package/src/history/store.ts +203 -0
- package/src/history/types.ts +166 -0
- package/src/history/worker.ts +317 -0
- package/src/intercom/intercom-bridge.ts +123 -0
- package/src/intercom/live-intercom.ts +104 -0
- package/src/intercom/result-intercom.ts +429 -0
- package/src/pi-intercom/agent-dir.ts +9 -0
- package/src/pi-intercom/broker/broker.ts +463 -0
- package/src/pi-intercom/broker/client.ts +558 -0
- package/src/pi-intercom/broker/framing.ts +77 -0
- package/src/pi-intercom/broker/paths.ts +47 -0
- package/src/pi-intercom/broker/pid.ts +59 -0
- package/src/pi-intercom/broker/spawn.ts +248 -0
- package/src/pi-intercom/config.ts +142 -0
- package/src/pi-intercom/index.ts +2929 -0
- package/src/pi-intercom/reply-tracker.ts +211 -0
- package/src/pi-intercom/session-targets.ts +172 -0
- package/src/pi-intercom/subagent-live-events.ts +133 -0
- package/src/pi-intercom/topics.ts +125 -0
- package/src/pi-intercom/types.ts +271 -0
- package/src/pi-intercom/ui/compose.ts +289 -0
- package/src/pi-intercom/ui/inline-message.ts +92 -0
- package/src/pi-intercom/ui/session-list.ts +139 -0
- package/src/runs/background/async-control.ts +89 -0
- package/src/runs/background/async-execution.ts +850 -0
- package/src/runs/background/async-job-tracker.ts +385 -0
- package/src/runs/background/async-result-file.ts +62 -0
- package/src/runs/background/async-resume.ts +333 -0
- package/src/runs/background/async-status.ts +481 -0
- package/src/runs/background/completion-dedupe.ts +79 -0
- package/src/runs/background/completion-delivery.ts +195 -0
- package/src/runs/background/notify.ts +123 -0
- package/src/runs/background/parallel-groups.ts +53 -0
- package/src/runs/background/result-watcher.ts +423 -0
- package/src/runs/background/run-id-resolver.ts +69 -0
- package/src/runs/background/run-status.ts +384 -0
- package/src/runs/background/stale-run-reconciler.ts +369 -0
- package/src/runs/background/subagent-runner-launcher.ts +56 -0
- package/src/runs/background/subagent-runner.ts +2053 -0
- package/src/runs/background/top-level-async.ts +13 -0
- package/src/runs/foreground/chain-clarify.ts +1340 -0
- package/src/runs/foreground/clarify-invocation.ts +77 -0
- package/src/runs/foreground/execution-input.ts +467 -0
- package/src/runs/foreground/foreground-control.ts +821 -0
- package/src/runs/foreground/question-control.ts +70 -0
- package/src/runs/foreground/run-async-path.ts +243 -0
- package/src/runs/foreground/subagent-executor.ts +501 -0
- package/src/runs/foreground/subagent-params.ts +320 -0
- package/src/runs/foreground/wait-run.ts +121 -0
- package/src/runs/shared/acceptance-contract.ts +257 -0
- package/src/runs/shared/acceptance-evaluation.ts +275 -0
- package/src/runs/shared/acceptance-finalization.ts +353 -0
- package/src/runs/shared/acceptance-reports.ts +152 -0
- package/src/runs/shared/acceptance.ts +33 -0
- package/src/runs/shared/chain-outputs.ts +114 -0
- package/src/runs/shared/child-attempt.ts +623 -0
- package/src/runs/shared/child-execution-cwd.ts +91 -0
- package/src/runs/shared/child-observations.ts +99 -0
- package/src/runs/shared/claude-code.ts +243 -0
- package/src/runs/shared/completion-guard.ts +35 -0
- package/src/runs/shared/dynamic-fanout.ts +295 -0
- package/src/runs/shared/history-index.ts +69 -0
- package/src/runs/shared/mcp-direct-tool-allowlist.ts +376 -0
- package/src/runs/shared/model-fallback.ts +219 -0
- package/src/runs/shared/mutating-tool-guard.ts +194 -0
- package/src/runs/shared/native-finalization.ts +128 -0
- package/src/runs/shared/native-usage.ts +113 -0
- package/src/runs/shared/nested-events.ts +752 -0
- package/src/runs/shared/nested-path.ts +52 -0
- package/src/runs/shared/nested-render.ts +119 -0
- package/src/runs/shared/parallel-utils.ts +153 -0
- package/src/runs/shared/parent-receipts.ts +59 -0
- package/src/runs/shared/parent-usage.ts +121 -0
- package/src/runs/shared/pi-args.ts +350 -0
- package/src/runs/shared/pi-spawn.ts +52 -0
- package/src/runs/shared/run-history-queries.ts +44 -0
- package/src/runs/shared/run-history.ts +93 -0
- package/src/runs/shared/run-id-ambiguity.ts +4 -0
- package/src/runs/shared/run-records.ts +607 -0
- package/src/runs/shared/session-cwd-preload.ts +27 -0
- package/src/runs/shared/single-output.ts +267 -0
- package/src/runs/shared/streaming-text.ts +18 -0
- package/src/runs/shared/structured-output.ts +68 -0
- package/src/runs/shared/subagent-control.ts +219 -0
- package/src/runs/shared/subagent-prompt-runtime.ts +201 -0
- package/src/runs/shared/subagent-tool-loop-guard.ts +89 -0
- package/src/runs/shared/supervisor-questions.ts +372 -0
- package/src/runs/shared/workflow-graph.ts +220 -0
- package/src/runs/shared/workflow-policy.ts +125 -0
- package/src/runs/shared/worktree.ts +635 -0
- package/src/shared/agent-context-policy.ts +184 -0
- package/src/shared/artifacts.ts +95 -0
- package/src/shared/atomic-json.ts +16 -0
- package/src/shared/cooperative.ts +21 -0
- package/src/shared/execution-cwd.ts +14 -0
- package/src/shared/file-coalescer.ts +29 -0
- package/src/shared/fork-context.ts +68 -0
- package/src/shared/formatters.ts +84 -0
- package/src/shared/journal-reader.ts +333 -0
- package/src/shared/lazy-tools.ts +36 -0
- package/src/shared/model-info.ts +81 -0
- package/src/shared/native-session.ts +8 -0
- package/src/shared/native-tui.ts +10 -0
- package/src/shared/native-typebox.ts +14 -0
- package/src/shared/post-exit-stdio-guard.ts +204 -0
- package/src/shared/prompt-sections.ts +10 -0
- package/src/shared/session-entries.ts +49 -0
- package/src/shared/session-identity.ts +18 -0
- package/src/shared/settings.ts +384 -0
- package/src/shared/status-format.ts +123 -0
- package/src/shared/temp-root.ts +107 -0
- package/src/shared/types.ts +1259 -0
- package/src/shared/ui-mode.ts +5 -0
- package/src/shared/utils.ts +342 -0
- package/src/slash/prompt-template-bridge.ts +408 -0
- package/src/slash/slash-bridge.ts +178 -0
- package/src/slash/slash-commands.ts +617 -0
- package/src/slash/slash-live-state.ts +292 -0
- package/src/tui/action-hints.ts +69 -0
- package/src/tui/agent-history.ts +210 -0
- package/src/tui/agent-view.ts +1323 -0
- package/src/tui/render.ts +1550 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import { readOutputPage } from "../../shared/journal-reader.ts";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { writeAtomicJson } from "../../shared/atomic-json.ts";
|
|
5
|
+
import { RESULTS_DIR, RUNNER_ERROR_LOG_FILE, type AsyncParallelGroupStatus, type AsyncResultChild, type AsyncResultTerminalState, type AsyncStatus, type NestedRunSummary, type SubagentRunMode } from "../../shared/types.ts";
|
|
6
|
+
import { normalizeParallelGroups } from "./parallel-groups.ts";
|
|
7
|
+
import { nestedSummaryFromAsyncStatus, projectNestedEvents, resolveNestedAsyncDir, writeNestedEvent, type NestedRoute } from "../shared/nested-events.ts";
|
|
8
|
+
import { isDurableRun, readAsyncResultFileIfExists } from "./async-result-file.ts";
|
|
9
|
+
import { readRunJson } from "../shared/supervisor-questions.ts";
|
|
10
|
+
import { readStatus } from "../../shared/utils.ts";
|
|
11
|
+
import type { AsyncRunRecord } from "./async-resume.ts";
|
|
12
|
+
|
|
13
|
+
export type PidLiveness = "alive" | "dead" | "unknown";
|
|
14
|
+
|
|
15
|
+
type KillFn = (pid: number, signal?: NodeJS.Signals | 0) => boolean;
|
|
16
|
+
|
|
17
|
+
interface StartedRunMetadata {
|
|
18
|
+
runId: string;
|
|
19
|
+
pid?: number;
|
|
20
|
+
sessionId?: string;
|
|
21
|
+
mode?: SubagentRunMode;
|
|
22
|
+
agents?: string[];
|
|
23
|
+
chainStepCount?: number;
|
|
24
|
+
parallelGroups?: AsyncParallelGroupStatus[];
|
|
25
|
+
startedAt?: number;
|
|
26
|
+
sessionFile?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface ReconcileAsyncRunOptions {
|
|
30
|
+
resultsDir?: string;
|
|
31
|
+
kill?: KillFn;
|
|
32
|
+
now?: () => number;
|
|
33
|
+
startedRun?: StartedRunMetadata;
|
|
34
|
+
missingStatusGraceMs?: number;
|
|
35
|
+
staleAlivePidMs?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ReconcileAsyncRunResult {
|
|
39
|
+
status: AsyncStatus | null;
|
|
40
|
+
repaired: boolean;
|
|
41
|
+
resultPath?: string;
|
|
42
|
+
message?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isNotFoundError(error: unknown): boolean {
|
|
46
|
+
return typeof error === "object"
|
|
47
|
+
&& error !== null
|
|
48
|
+
&& "code" in error
|
|
49
|
+
&& (error as NodeJS.ErrnoException).code === "ENOENT";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function appendJsonl(filePath: string, payload: object): void {
|
|
53
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
54
|
+
fs.appendFileSync(filePath, `${JSON.stringify(payload)}\n`, "utf-8");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function readStatusFile(asyncDir: string): AsyncStatus | null {
|
|
58
|
+
return readStatus(asyncDir);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface ResultRepairData {
|
|
62
|
+
state: AsyncResultTerminalState;
|
|
63
|
+
timestamp?: number;
|
|
64
|
+
results?: AsyncResultChild[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function readResultRepairData(resultPath: string): ResultRepairData | undefined {
|
|
68
|
+
const data = readAsyncResultFileIfExists(resultPath);
|
|
69
|
+
if (!data) return undefined;
|
|
70
|
+
return { state: data.terminalState, timestamp: data.timestamp, ...(Array.isArray(data.results) ? { results: data.results } : {}) };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function childState(overallState: ResultRepairData["state"], child: AsyncResultChild | undefined): "complete" | "failed" | "blocked" | "paused" {
|
|
74
|
+
if (child?.success === true) return "complete";
|
|
75
|
+
if (child?.interrupted === true) return "paused";
|
|
76
|
+
if (child?.acceptance?.status === "blocked" && (child.exitCode === 0 || child.exitCode === undefined)) return "blocked";
|
|
77
|
+
if (child?.success === false) return "failed";
|
|
78
|
+
return overallState;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function withoutLiveActivity<T extends { currentTool?: string; currentToolArgs?: string; currentToolStartedAt?: number; currentPath?: string; activityState?: string }>(value: T): T {
|
|
82
|
+
return {
|
|
83
|
+
...value,
|
|
84
|
+
activityState: undefined,
|
|
85
|
+
currentTool: undefined,
|
|
86
|
+
currentToolArgs: undefined,
|
|
87
|
+
currentToolStartedAt: undefined,
|
|
88
|
+
currentPath: undefined,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function terminalStatusFromResult(status: AsyncStatus, resultPath: string, now: number): AsyncStatus | undefined {
|
|
93
|
+
const repair = readResultRepairData(resultPath);
|
|
94
|
+
if (!repair) return undefined;
|
|
95
|
+
now = repair.timestamp ?? now;
|
|
96
|
+
const steps = (status.steps ?? []).map((step, index) => {
|
|
97
|
+
if (step.status !== "running" && step.status !== "pending") return withoutLiveActivity(step);
|
|
98
|
+
const child = repair.results?.[index];
|
|
99
|
+
if (isDurableRun(status) && step.status === "pending" && !child) return withoutLiveActivity(step);
|
|
100
|
+
const state = childState(repair.state, child);
|
|
101
|
+
return withoutLiveActivity({
|
|
102
|
+
...step,
|
|
103
|
+
status: state === "complete" ? "complete" as const : state,
|
|
104
|
+
endedAt: step.endedAt ?? now,
|
|
105
|
+
durationMs: step.startedAt !== undefined && step.durationMs === undefined ? Math.max(0, now - step.startedAt) : step.durationMs,
|
|
106
|
+
exitCode: step.exitCode ?? (state === "complete" || state === "blocked" || state === "paused" ? 0 : 1),
|
|
107
|
+
error: state === "failed" ? step.error ?? child?.error : step.error,
|
|
108
|
+
sessionFile: step.sessionFile ?? child?.sessionFile,
|
|
109
|
+
model: step.model ?? child?.model,
|
|
110
|
+
attemptedModels: step.attemptedModels ?? child?.attemptedModels,
|
|
111
|
+
modelAttempts: step.modelAttempts ?? child?.modelAttempts,
|
|
112
|
+
acceptance: step.acceptance ?? child?.acceptance,
|
|
113
|
+
agentProcessExit: step.agentProcessExit ?? child?.agentProcessExit,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
return withoutLiveActivity({
|
|
117
|
+
...status,
|
|
118
|
+
state: repair.state,
|
|
119
|
+
lastUpdate: now,
|
|
120
|
+
endedAt: status.endedAt ?? now,
|
|
121
|
+
steps,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function buildStartedStatus(asyncDir: string, startedRun: StartedRunMetadata, now: number): AsyncStatus {
|
|
126
|
+
const startedAt = startedRun.startedAt ?? now;
|
|
127
|
+
const agents = startedRun.agents?.length ? startedRun.agents : ["subagent"];
|
|
128
|
+
const chainStepCount = startedRun.chainStepCount;
|
|
129
|
+
const parallelGroups = chainStepCount !== undefined
|
|
130
|
+
? normalizeParallelGroups(startedRun.parallelGroups, agents.length, chainStepCount)
|
|
131
|
+
: [];
|
|
132
|
+
return {
|
|
133
|
+
runId: startedRun.runId || path.basename(asyncDir),
|
|
134
|
+
...(startedRun.sessionId ? { sessionId: startedRun.sessionId } : {}),
|
|
135
|
+
mode: startedRun.mode ?? "single",
|
|
136
|
+
state: "running",
|
|
137
|
+
pid: startedRun.pid,
|
|
138
|
+
startedAt,
|
|
139
|
+
lastUpdate: now,
|
|
140
|
+
currentStep: 0,
|
|
141
|
+
...(chainStepCount !== undefined ? { chainStepCount } : {}),
|
|
142
|
+
...(parallelGroups.length ? { parallelGroups } : {}),
|
|
143
|
+
steps: agents.map((agent) => ({
|
|
144
|
+
agent,
|
|
145
|
+
status: "running" as const,
|
|
146
|
+
startedAt,
|
|
147
|
+
})),
|
|
148
|
+
...(startedRun.sessionFile ? { sessionFile: startedRun.sessionFile } : {}),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function readCompletedStepOutput(asyncDir: string, index: number): string {
|
|
153
|
+
try {
|
|
154
|
+
return readOutputPage(path.join(asyncDir, `output-${index}.log`)).text.trim();
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (isNotFoundError(error)) return "";
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const RUNNER_STDERR_EXCERPT_BYTES = 16 * 1024;
|
|
162
|
+
const UNSAFE_CONTROL_CHARACTERS = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g;
|
|
163
|
+
|
|
164
|
+
function readRunnerStderr(asyncDir: string): string | undefined {
|
|
165
|
+
let fd: number | undefined;
|
|
166
|
+
try {
|
|
167
|
+
fd = fs.openSync(path.join(asyncDir, RUNNER_ERROR_LOG_FILE), "r");
|
|
168
|
+
const size = fs.fstatSync(fd).size;
|
|
169
|
+
const length = Math.min(size, RUNNER_STDERR_EXCERPT_BYTES);
|
|
170
|
+
const buffer = Buffer.alloc(length);
|
|
171
|
+
const bytesRead = fs.readSync(fd, buffer, 0, length, Math.max(0, size - length));
|
|
172
|
+
const excerpt = buffer.subarray(0, bytesRead).toString("utf-8").replace(UNSAFE_CONTROL_CHARACTERS, "�").trim();
|
|
173
|
+
if (!excerpt) return undefined;
|
|
174
|
+
return size > length ? `[runner stderr truncated to last ${length} bytes]\n${excerpt}` : excerpt;
|
|
175
|
+
} catch {
|
|
176
|
+
return undefined;
|
|
177
|
+
} finally {
|
|
178
|
+
if (fd !== undefined) {
|
|
179
|
+
try { fs.closeSync(fd); } catch {}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function buildFailedRepair(status: AsyncStatus, asyncDir: string, now: number, reason?: string): { status: AsyncStatus; result: object; message: string } {
|
|
185
|
+
const runId = status.runId || path.basename(asyncDir);
|
|
186
|
+
const pid = typeof status.pid === "number" ? status.pid : "unknown";
|
|
187
|
+
const defaultMessage = `Async runner process ${pid} exited or disappeared before writing a result. Completion is unconfirmed.`;
|
|
188
|
+
const runnerStderr = reason ? undefined : readRunnerStderr(asyncDir);
|
|
189
|
+
const message = reason ?? (runnerStderr ? `${defaultMessage}\n\nRunner stderr:\n${runnerStderr}` : defaultMessage);
|
|
190
|
+
const steps = status.steps?.length ? status.steps : [{ agent: "subagent", status: "running" as const }];
|
|
191
|
+
const repairedSteps = steps.map((step) => step.status === "running" || String(step.status) === "queued" || step.status === "pending"
|
|
192
|
+
? withoutLiveActivity({
|
|
193
|
+
...step,
|
|
194
|
+
status: "failed" as const,
|
|
195
|
+
endedAt: step.endedAt ?? now,
|
|
196
|
+
durationMs: step.startedAt !== undefined && step.durationMs === undefined ? Math.max(0, now - step.startedAt) : step.durationMs,
|
|
197
|
+
exitCode: step.exitCode ?? 1,
|
|
198
|
+
error: step.error ?? message,
|
|
199
|
+
})
|
|
200
|
+
: withoutLiveActivity(step));
|
|
201
|
+
const repairedStatus: AsyncStatus = withoutLiveActivity({
|
|
202
|
+
...status,
|
|
203
|
+
state: "failed",
|
|
204
|
+
lastUpdate: now,
|
|
205
|
+
endedAt: now,
|
|
206
|
+
steps: repairedSteps,
|
|
207
|
+
});
|
|
208
|
+
const resultAgent = repairedSteps[status.currentStep ?? 0]?.agent ?? repairedSteps[0]?.agent ?? "subagent";
|
|
209
|
+
return {
|
|
210
|
+
status: repairedStatus,
|
|
211
|
+
message,
|
|
212
|
+
result: {
|
|
213
|
+
id: runId,
|
|
214
|
+
agent: resultAgent,
|
|
215
|
+
mode: status.mode,
|
|
216
|
+
success: false,
|
|
217
|
+
state: "failed",
|
|
218
|
+
summary: message,
|
|
219
|
+
results: repairedSteps.map((step, index) => {
|
|
220
|
+
const success = step.status === "complete" || step.status === "completed";
|
|
221
|
+
return {
|
|
222
|
+
agent: step.agent,
|
|
223
|
+
output: success ? readCompletedStepOutput(asyncDir, index) : message,
|
|
224
|
+
error: success ? undefined : step.error ?? message,
|
|
225
|
+
success,
|
|
226
|
+
model: step.model,
|
|
227
|
+
attemptedModels: step.attemptedModels,
|
|
228
|
+
modelAttempts: step.modelAttempts,
|
|
229
|
+
sessionFile: step.sessionFile,
|
|
230
|
+
};
|
|
231
|
+
}),
|
|
232
|
+
exitCode: 1,
|
|
233
|
+
timestamp: now,
|
|
234
|
+
durationMs: Math.max(0, now - status.startedAt),
|
|
235
|
+
asyncDir,
|
|
236
|
+
sessionId: status.sessionId,
|
|
237
|
+
cwd: status.cwd,
|
|
238
|
+
sessionFile: status.sessionFile,
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function writeFailedRepair(asyncDir: string, status: AsyncStatus, resultPath: string, now: number, reason?: string): ReconcileAsyncRunResult {
|
|
244
|
+
const repair = buildFailedRepair(status, asyncDir, now, reason);
|
|
245
|
+
writeAtomicJson(resultPath, repair.result);
|
|
246
|
+
writeAtomicJson(path.join(asyncDir, "status.json"), repair.status);
|
|
247
|
+
appendJsonl(path.join(asyncDir, "events.jsonl"), {
|
|
248
|
+
type: "subagent.run.repaired_stale",
|
|
249
|
+
ts: now,
|
|
250
|
+
runId: repair.status.runId,
|
|
251
|
+
pid: status.pid,
|
|
252
|
+
resultPath,
|
|
253
|
+
message: repair.message,
|
|
254
|
+
});
|
|
255
|
+
return { status: repair.status, repaired: true, resultPath, message: repair.message };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function terminal(state: AsyncStatus["state"]): boolean {
|
|
259
|
+
return state === "complete" || state === "failed" || state === "blocked" || state === "paused";
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function* nestedRuns(children: NestedRunSummary[] | undefined): Generator<NestedRunSummary> {
|
|
263
|
+
for (const child of children ?? []) {
|
|
264
|
+
yield child;
|
|
265
|
+
yield* nestedRuns(child.children);
|
|
266
|
+
yield* nestedRuns(child.steps?.flatMap((step) => step.children ?? []));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function reconcileNestedAsyncDescendants(route: NestedRoute, options: ReconcileAsyncRunOptions = {}): NestedRunSummary[] {
|
|
271
|
+
const registry = projectNestedEvents(route);
|
|
272
|
+
for (const run of nestedRuns(registry.children)) {
|
|
273
|
+
if (run.state !== "running" && run.state !== "queued") continue;
|
|
274
|
+
const asyncDir = resolveNestedAsyncDir(route.rootRunId, run);
|
|
275
|
+
if (!asyncDir) continue;
|
|
276
|
+
const result = reconcileAsyncRun(asyncDir, {
|
|
277
|
+
...options,
|
|
278
|
+
resultsDir: path.join(options.resultsDir ?? RESULTS_DIR, "nested", route.rootRunId),
|
|
279
|
+
});
|
|
280
|
+
const status = result.status;
|
|
281
|
+
if (!status) continue;
|
|
282
|
+
const ts = options.now?.() ?? Date.now();
|
|
283
|
+
const child = nestedSummaryFromAsyncStatus(status, asyncDir, {
|
|
284
|
+
id: run.id, parentRunId: run.parentRunId, parentStepIndex: run.parentStepIndex,
|
|
285
|
+
depth: run.depth, path: run.path, mode: run.mode, ts,
|
|
286
|
+
});
|
|
287
|
+
child.steps?.forEach((step, index) => { step.children = run.steps?.[index]?.children; });
|
|
288
|
+
Object.assign(run, child, result.message ? { error: result.message } : {});
|
|
289
|
+
if (isDurableRun(status)) continue;
|
|
290
|
+
if (!result.repaired && !terminal(status.state)) continue;
|
|
291
|
+
writeNestedEvent(route, {
|
|
292
|
+
type: terminal(status.state) ? "subagent.nested.completed" : "subagent.nested.updated",
|
|
293
|
+
ts,
|
|
294
|
+
parentRunId: run.parentRunId,
|
|
295
|
+
parentStepIndex: run.parentStepIndex,
|
|
296
|
+
child,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
return registry.children;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function checkPidLiveness(pid: number, kill: KillFn = process.kill): PidLiveness {
|
|
303
|
+
try {
|
|
304
|
+
kill(pid, 0);
|
|
305
|
+
return "alive";
|
|
306
|
+
} catch (error) {
|
|
307
|
+
const code = typeof error === "object" && error !== null && "code" in error
|
|
308
|
+
? (error as NodeJS.ErrnoException).code
|
|
309
|
+
: undefined;
|
|
310
|
+
if (code === "ESRCH") return "dead";
|
|
311
|
+
if (code === "EPERM") return "unknown";
|
|
312
|
+
return "unknown";
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function reconcileAsyncRun(asyncDir: string, options: ReconcileAsyncRunOptions = {}, record?: Pick<AsyncRunRecord, "status" | "durable">): ReconcileAsyncRunResult {
|
|
317
|
+
const now = options.now?.() ?? Date.now();
|
|
318
|
+
const status = record ? record.status : readStatusFile(asyncDir);
|
|
319
|
+
const startedStatus = !status && options.startedRun ? buildStartedStatus(asyncDir, options.startedRun, now) : undefined;
|
|
320
|
+
const effectiveStatus = status ?? startedStatus;
|
|
321
|
+
if (!effectiveStatus) return { status: null, repaired: false };
|
|
322
|
+
|
|
323
|
+
const runId = effectiveStatus.runId || path.basename(asyncDir);
|
|
324
|
+
const durable = isDurableRun(effectiveStatus) || (record ? record.durable : isDurableRun(readRunJson<object>(path.join(asyncDir, "launch.json"))));
|
|
325
|
+
const resultPath = durable ? path.join(asyncDir, "result.json") : path.join(options.resultsDir ?? RESULTS_DIR, `${runId}.json`);
|
|
326
|
+
if (durable) {
|
|
327
|
+
// The detached owner is the only execution writer. Inspection projects evidence.
|
|
328
|
+
const final = terminalStatusFromResult(effectiveStatus, resultPath, effectiveStatus.endedAt ?? now);
|
|
329
|
+
if (final) return { status: final, repaired: false, resultPath };
|
|
330
|
+
if (typeof effectiveStatus.pid === "number" && checkPidLiveness(effectiveStatus.pid, options.kill) === "dead") {
|
|
331
|
+
const failure = buildFailedRepair(effectiveStatus, asyncDir, effectiveStatus.lastUpdate ?? effectiveStatus.startedAt);
|
|
332
|
+
return { status: failure.status, repaired: false, message: failure.message };
|
|
333
|
+
}
|
|
334
|
+
if (terminal(effectiveStatus.state)) return { status: { ...effectiveStatus, state: "running" }, repaired: false, message: "Waiting for the owner's final result. Completion is unconfirmed." };
|
|
335
|
+
return { status: effectiveStatus, repaired: false };
|
|
336
|
+
}
|
|
337
|
+
if (fs.existsSync(resultPath)) {
|
|
338
|
+
const terminalStatus = effectiveStatus.state === "running" || effectiveStatus.state === "queued"
|
|
339
|
+
? terminalStatusFromResult(effectiveStatus, resultPath, now)
|
|
340
|
+
: undefined;
|
|
341
|
+
if (terminalStatus) {
|
|
342
|
+
writeAtomicJson(path.join(asyncDir, "status.json"), terminalStatus);
|
|
343
|
+
return { status: terminalStatus, repaired: true, resultPath, message: "Existing async result file was used to repair stale running status." };
|
|
344
|
+
}
|
|
345
|
+
return { status: effectiveStatus, repaired: false, resultPath };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if ((effectiveStatus.state !== "running" && effectiveStatus.state !== "queued") || typeof effectiveStatus.pid !== "number") {
|
|
349
|
+
return { status: status ?? null, repaired: false, resultPath };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (!status) {
|
|
353
|
+
const startedAt = options.startedRun?.startedAt ?? effectiveStatus.startedAt;
|
|
354
|
+
if (now - startedAt < (options.missingStatusGraceMs ?? 1000)) {
|
|
355
|
+
return { status: null, repaired: false, resultPath };
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const liveness = checkPidLiveness(effectiveStatus.pid, options.kill);
|
|
360
|
+
if (liveness !== "dead") {
|
|
361
|
+
const staleAfterMs = options.staleAlivePidMs ?? 24 * 60 * 60 * 1000;
|
|
362
|
+
const lastUpdate = effectiveStatus.lastUpdate ?? effectiveStatus.startedAt;
|
|
363
|
+
if (now - lastUpdate <= staleAfterMs) return { status: status ?? null, repaired: false, resultPath };
|
|
364
|
+
const message = `Async runner process ${effectiveStatus.pid} still has a live PID, but status has not updated for ${now - lastUpdate}ms. Marked run failed by stale-run reconciliation because PID ownership cannot be verified.`;
|
|
365
|
+
return writeFailedRepair(asyncDir, effectiveStatus, resultPath, now, message);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return writeFailedRepair(asyncDir, effectiveStatus, resultPath, now);
|
|
369
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
5
|
+
|
|
6
|
+
const RETRY_WINDOW_MS = 30_000;
|
|
7
|
+
const RETRY_DELAY_MS = 500;
|
|
8
|
+
const MAX_STDERR_LENGTH = 64 * 1024;
|
|
9
|
+
|
|
10
|
+
function run(runnerPath: string, configPath: string, piPackageRoot?: string): Promise<{ code: number; stderr: string }> {
|
|
11
|
+
return new Promise((resolve) => {
|
|
12
|
+
const child = spawn(process.execPath, [runnerPath, configPath], {
|
|
13
|
+
stdio: ["ignore", "inherit", "pipe"],
|
|
14
|
+
env: piPackageRoot ? { ...process.env, PI_PACKAGE_DIR: process.env.PI_PACKAGE_DIR ?? piPackageRoot } : process.env,
|
|
15
|
+
});
|
|
16
|
+
let stderr = "";
|
|
17
|
+
child.stderr.on("data", (chunk: Buffer) => {
|
|
18
|
+
process.stderr.write(chunk);
|
|
19
|
+
stderr = `${stderr}${chunk.toString()}`.slice(-MAX_STDERR_LENGTH);
|
|
20
|
+
});
|
|
21
|
+
child.once("error", (error) => {
|
|
22
|
+
const stderr = error.stack ?? error.message;
|
|
23
|
+
process.stderr.write(`${stderr}\n`);
|
|
24
|
+
resolve({ code: 1, stderr });
|
|
25
|
+
});
|
|
26
|
+
child.once("close", (code) => resolve({ code: code ?? 1, stderr }));
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const [runnerPath, configPath] = process.argv.slice(2);
|
|
31
|
+
if (!runnerPath || !configPath) throw new Error("Usage: subagent-runner-launcher <runner> <config>");
|
|
32
|
+
|
|
33
|
+
let statusPath: string | undefined;
|
|
34
|
+
let piPackageRoot: string | undefined;
|
|
35
|
+
try {
|
|
36
|
+
const config = JSON.parse(fs.readFileSync(configPath, "utf-8")) as { asyncDir?: unknown; piPackageRoot?: unknown };
|
|
37
|
+
if (typeof config.piPackageRoot === "string") piPackageRoot = config.piPackageRoot;
|
|
38
|
+
if (typeof config.asyncDir === "string") statusPath = path.join(config.asyncDir, "status.json");
|
|
39
|
+
} catch {}
|
|
40
|
+
|
|
41
|
+
// ponytail: bridge short in-place extension updates; use shared update locking if longer gaps appear.
|
|
42
|
+
const deadline = Date.now() + RETRY_WINDOW_MS;
|
|
43
|
+
let announcedRetry = false;
|
|
44
|
+
while (true) {
|
|
45
|
+
const result = await run(runnerPath, configPath, piPackageRoot);
|
|
46
|
+
const failedBeforeStartup = !statusPath || !fs.existsSync(statusPath);
|
|
47
|
+
if (result.code === 0 || !failedBeforeStartup || !result.stderr.includes("ERR_MODULE_NOT_FOUND") || Date.now() >= deadline) {
|
|
48
|
+
process.exitCode = result.code;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
if (!announcedRetry) {
|
|
52
|
+
process.stderr.write("[pi-subagents] Runner dependencies are being updated; retrying startup.\n");
|
|
53
|
+
announcedRetry = true;
|
|
54
|
+
}
|
|
55
|
+
await delay(RETRY_DELAY_MS);
|
|
56
|
+
}
|