@selesai/code 0.1.5 → 0.1.6
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/dist/config.d.ts +21 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +71 -4
- package/dist/config.js.map +1 -1
- package/dist/core/package-manager.d.ts +1 -0
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +6 -1
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +3 -2
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/settings-manager.d.ts +1 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/extensions/pi-intercom/LICENSE +21 -0
- package/dist/extensions/pi-intercom/README.md +484 -0
- package/dist/extensions/pi-intercom/broker/broker.ts +345 -0
- package/dist/extensions/pi-intercom/broker/client.ts +535 -0
- package/dist/extensions/pi-intercom/broker/framing.ts +57 -0
- package/dist/extensions/pi-intercom/broker/paths.ts +20 -0
- package/dist/extensions/pi-intercom/broker/spawn.ts +307 -0
- package/dist/extensions/pi-intercom/config.ts +108 -0
- package/dist/extensions/pi-intercom/index.ts +1778 -0
- package/dist/extensions/pi-intercom/package.json +38 -0
- package/dist/extensions/pi-intercom/reply-tracker.ts +102 -0
- package/dist/extensions/pi-intercom/skills/pi-intercom/SKILL.md +512 -0
- package/dist/extensions/pi-intercom/types.ts +46 -0
- package/dist/extensions/pi-intercom/ui/compose.ts +139 -0
- package/dist/extensions/pi-intercom/ui/inline-message.ts +76 -0
- package/dist/extensions/pi-intercom/ui/session-list.ts +162 -0
- package/dist/extensions/pi-rewind-hook/README.md +245 -0
- package/dist/extensions/pi-rewind-hook/index.ts +1445 -0
- package/dist/extensions/pi-rewind-hook/package.json +29 -0
- package/dist/extensions/pi-subagents/README.md +1290 -0
- package/dist/extensions/pi-subagents/agents/context-builder.md +46 -0
- package/dist/extensions/pi-subagents/agents/delegate.md +12 -0
- package/dist/extensions/pi-subagents/agents/oracle.md +73 -0
- package/dist/extensions/pi-subagents/agents/planner.md +55 -0
- package/dist/extensions/pi-subagents/agents/researcher.md +52 -0
- package/dist/extensions/pi-subagents/agents/reviewer.md +79 -0
- package/dist/extensions/pi-subagents/agents/scout.md +50 -0
- package/dist/extensions/pi-subagents/agents/worker.md +55 -0
- package/dist/extensions/pi-subagents/package.json +83 -0
- package/dist/extensions/pi-subagents/prompts/gather-context-and-clarify.md +13 -0
- package/dist/extensions/pi-subagents/prompts/parallel-cleanup.md +59 -0
- package/dist/extensions/pi-subagents/prompts/parallel-context-build.md +55 -0
- package/dist/extensions/pi-subagents/prompts/parallel-handoff-plan.md +61 -0
- package/dist/extensions/pi-subagents/prompts/parallel-research.md +50 -0
- package/dist/extensions/pi-subagents/prompts/parallel-review.md +54 -0
- package/dist/extensions/pi-subagents/prompts/review-loop.md +41 -0
- package/dist/extensions/pi-subagents/skills/pi-subagents/SKILL.md +855 -0
- package/dist/extensions/pi-subagents/src/agents/agent-management.ts +875 -0
- package/dist/extensions/pi-subagents/src/agents/agent-scope.ts +6 -0
- package/dist/extensions/pi-subagents/src/agents/agent-selection.ts +25 -0
- package/dist/extensions/pi-subagents/src/agents/agent-serializer.ts +110 -0
- package/dist/extensions/pi-subagents/src/agents/agents.ts +1379 -0
- package/dist/extensions/pi-subagents/src/agents/chain-serializer.ts +252 -0
- package/dist/extensions/pi-subagents/src/agents/frontmatter.ts +93 -0
- package/dist/extensions/pi-subagents/src/agents/identity.ts +30 -0
- package/dist/extensions/pi-subagents/src/agents/proactive-skills.ts +191 -0
- package/dist/extensions/pi-subagents/src/agents/skills.ts +729 -0
- package/dist/extensions/pi-subagents/src/extension/config.ts +16 -0
- package/dist/extensions/pi-subagents/src/extension/control-notices.ts +92 -0
- package/dist/extensions/pi-subagents/src/extension/doctor.ts +220 -0
- package/dist/extensions/pi-subagents/src/extension/fanout-child.ts +171 -0
- package/dist/extensions/pi-subagents/src/extension/index.ts +627 -0
- package/dist/extensions/pi-subagents/src/extension/schemas.ts +265 -0
- package/dist/extensions/pi-subagents/src/intercom/intercom-bridge.ts +412 -0
- package/dist/extensions/pi-subagents/src/intercom/result-intercom.ts +377 -0
- package/dist/extensions/pi-subagents/src/profiles/profiles.ts +637 -0
- package/dist/extensions/pi-subagents/src/runs/background/async-execution.ts +916 -0
- package/dist/extensions/pi-subagents/src/runs/background/async-job-tracker.ts +355 -0
- package/dist/extensions/pi-subagents/src/runs/background/async-resume.ts +391 -0
- package/dist/extensions/pi-subagents/src/runs/background/async-status.ts +338 -0
- package/dist/extensions/pi-subagents/src/runs/background/chain-append.ts +282 -0
- package/dist/extensions/pi-subagents/src/runs/background/chain-root-attachment.ts +161 -0
- package/dist/extensions/pi-subagents/src/runs/background/completion-dedupe.ts +63 -0
- package/dist/extensions/pi-subagents/src/runs/background/control-channel.ts +177 -0
- package/dist/extensions/pi-subagents/src/runs/background/notify.ts +108 -0
- package/dist/extensions/pi-subagents/src/runs/background/parallel-groups.ts +45 -0
- package/dist/extensions/pi-subagents/src/runs/background/result-watcher.ts +316 -0
- package/dist/extensions/pi-subagents/src/runs/background/run-id-resolver.ts +83 -0
- package/dist/extensions/pi-subagents/src/runs/background/run-status.ts +273 -0
- package/dist/extensions/pi-subagents/src/runs/background/stale-run-reconciler.ts +341 -0
- package/dist/extensions/pi-subagents/src/runs/background/subagent-runner.ts +2458 -0
- package/dist/extensions/pi-subagents/src/runs/background/top-level-async.ts +13 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/chain-clarify.ts +1333 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/chain-execution.ts +1217 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/execution.ts +1092 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/subagent-executor.ts +3125 -0
- package/dist/extensions/pi-subagents/src/runs/shared/acceptance.ts +856 -0
- package/dist/extensions/pi-subagents/src/runs/shared/chain-outputs.ts +116 -0
- package/dist/extensions/pi-subagents/src/runs/shared/completion-guard.ts +147 -0
- package/dist/extensions/pi-subagents/src/runs/shared/dynamic-fanout.ts +295 -0
- package/dist/extensions/pi-subagents/src/runs/shared/long-running-guard.ts +175 -0
- package/dist/extensions/pi-subagents/src/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
- package/dist/extensions/pi-subagents/src/runs/shared/model-fallback.ts +141 -0
- package/dist/extensions/pi-subagents/src/runs/shared/nested-events.ts +819 -0
- package/dist/extensions/pi-subagents/src/runs/shared/nested-path.ts +52 -0
- package/dist/extensions/pi-subagents/src/runs/shared/nested-render.ts +115 -0
- package/dist/extensions/pi-subagents/src/runs/shared/parallel-utils.ts +149 -0
- package/dist/extensions/pi-subagents/src/runs/shared/pi-args.ts +240 -0
- package/dist/extensions/pi-subagents/src/runs/shared/pi-spawn.ts +115 -0
- package/dist/extensions/pi-subagents/src/runs/shared/run-history.ts +60 -0
- package/dist/extensions/pi-subagents/src/runs/shared/single-output.ts +178 -0
- package/dist/extensions/pi-subagents/src/runs/shared/structured-output.ts +77 -0
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-control.ts +223 -0
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-prompt-runtime.ts +222 -0
- package/dist/extensions/pi-subagents/src/runs/shared/workflow-graph.ts +206 -0
- package/dist/extensions/pi-subagents/src/runs/shared/worktree.ts +577 -0
- package/dist/extensions/pi-subagents/src/shared/artifacts.ts +98 -0
- package/dist/extensions/pi-subagents/src/shared/atomic-json.ts +73 -0
- package/dist/extensions/pi-subagents/src/shared/file-coalescer.ts +40 -0
- package/dist/extensions/pi-subagents/src/shared/fork-context.ts +76 -0
- package/dist/extensions/pi-subagents/src/shared/formatters.ts +133 -0
- package/dist/extensions/pi-subagents/src/shared/jsonl-writer.ts +81 -0
- package/dist/extensions/pi-subagents/src/shared/model-info.ts +78 -0
- package/dist/extensions/pi-subagents/src/shared/post-exit-stdio-guard.ts +85 -0
- package/dist/extensions/pi-subagents/src/shared/session-identity.ts +10 -0
- package/dist/extensions/pi-subagents/src/shared/session-tokens.ts +44 -0
- package/dist/extensions/pi-subagents/src/shared/settings.ts +448 -0
- package/dist/extensions/pi-subagents/src/shared/status-format.ts +49 -0
- package/dist/extensions/pi-subagents/src/shared/types.ts +1064 -0
- package/dist/extensions/pi-subagents/src/shared/utils.ts +469 -0
- package/dist/extensions/pi-subagents/src/slash/prompt-template-bridge.ts +420 -0
- package/dist/extensions/pi-subagents/src/slash/slash-bridge.ts +176 -0
- package/dist/extensions/pi-subagents/src/slash/slash-commands.ts +1165 -0
- package/dist/extensions/pi-subagents/src/slash/slash-live-state.ts +292 -0
- package/dist/extensions/pi-subagents/src/tui/render-helpers.ts +80 -0
- package/dist/extensions/pi-subagents/src/tui/render.ts +1728 -0
- package/dist/extensions/quick.ts +630 -0
- package/dist/modes/index.d.ts +1 -1
- package/dist/modes/index.d.ts.map +1 -1
- package/dist/modes/index.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +4 -3
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +3 -2
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { writeAtomicJson } from "../../shared/atomic-json.ts";
|
|
4
|
+
import { RESULTS_DIR, type AsyncParallelGroupStatus, type AsyncStatus, type NestedRunSummary, type SubagentRunMode } from "../../shared/types.ts";
|
|
5
|
+
import { normalizeParallelGroups } from "./parallel-groups.ts";
|
|
6
|
+
import { nestedSummaryFromAsyncStatus, projectNestedEvents, resolveNestedAsyncDir, writeNestedEvent, type NestedRoute } from "../shared/nested-events.ts";
|
|
7
|
+
|
|
8
|
+
export type PidLiveness = "alive" | "dead" | "unknown";
|
|
9
|
+
|
|
10
|
+
type KillFn = (pid: number, signal?: NodeJS.Signals | 0) => boolean;
|
|
11
|
+
|
|
12
|
+
interface StartedRunMetadata {
|
|
13
|
+
runId: string;
|
|
14
|
+
pid?: number;
|
|
15
|
+
sessionId?: string;
|
|
16
|
+
mode?: SubagentRunMode;
|
|
17
|
+
agents?: string[];
|
|
18
|
+
chainStepCount?: number;
|
|
19
|
+
parallelGroups?: AsyncParallelGroupStatus[];
|
|
20
|
+
startedAt?: number;
|
|
21
|
+
sessionFile?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface ReconcileAsyncRunOptions {
|
|
25
|
+
resultsDir?: string;
|
|
26
|
+
kill?: KillFn;
|
|
27
|
+
now?: () => number;
|
|
28
|
+
startedRun?: StartedRunMetadata;
|
|
29
|
+
missingStatusGraceMs?: number;
|
|
30
|
+
staleAlivePidMs?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface ReconcileAsyncRunResult {
|
|
34
|
+
status: AsyncStatus | null;
|
|
35
|
+
repaired: boolean;
|
|
36
|
+
resultPath?: string;
|
|
37
|
+
message?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getErrorMessage(error: unknown): string {
|
|
41
|
+
return error instanceof Error ? error.message : String(error);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isNotFoundError(error: unknown): boolean {
|
|
45
|
+
return typeof error === "object"
|
|
46
|
+
&& error !== null
|
|
47
|
+
&& "code" in error
|
|
48
|
+
&& (error as NodeJS.ErrnoException).code === "ENOENT";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function appendJsonlBestEffort(filePath: string, payload: object): void {
|
|
52
|
+
try {
|
|
53
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
54
|
+
fs.appendFileSync(filePath, `${JSON.stringify(payload)}\n`, "utf-8");
|
|
55
|
+
} catch {
|
|
56
|
+
// Repair status/result writes are the important path. A broken or full
|
|
57
|
+
// diagnostic event log must not make stale-run reconciliation fail.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function readStatusFile(asyncDir: string): AsyncStatus | null {
|
|
62
|
+
const statusPath = path.join(asyncDir, "status.json");
|
|
63
|
+
let content: string;
|
|
64
|
+
try {
|
|
65
|
+
content = fs.readFileSync(statusPath, "utf-8");
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (isNotFoundError(error)) return null;
|
|
68
|
+
throw new Error(`Failed to read async status file '${statusPath}': ${getErrorMessage(error)}`, {
|
|
69
|
+
cause: error instanceof Error ? error : undefined,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
return JSON.parse(content) as AsyncStatus;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
throw new Error(`Failed to parse async status file '${statusPath}': ${getErrorMessage(error)}`, {
|
|
76
|
+
cause: error instanceof Error ? error : undefined,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface ResultChildOutcome {
|
|
82
|
+
agent?: string;
|
|
83
|
+
success?: boolean;
|
|
84
|
+
error?: string;
|
|
85
|
+
sessionFile?: string;
|
|
86
|
+
model?: string;
|
|
87
|
+
attemptedModels?: string[];
|
|
88
|
+
modelAttempts?: NonNullable<AsyncStatus["steps"]>[number]["modelAttempts"];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface ResultRepairData {
|
|
92
|
+
state: "complete" | "failed" | "paused";
|
|
93
|
+
results?: ResultChildOutcome[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function readResultRepairData(resultPath: string): ResultRepairData | undefined {
|
|
97
|
+
try {
|
|
98
|
+
const data = JSON.parse(fs.readFileSync(resultPath, "utf-8")) as { success?: boolean; state?: string; exitCode?: number; results?: ResultChildOutcome[] };
|
|
99
|
+
const state = data.success ? "complete" : data.state === "paused" || data.exitCode === 0 ? "paused" : "failed";
|
|
100
|
+
return { state, ...(Array.isArray(data.results) ? { results: data.results } : {}) };
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (isNotFoundError(error)) return undefined;
|
|
103
|
+
throw new Error(`Failed to read async result file '${resultPath}': ${getErrorMessage(error)}`, {
|
|
104
|
+
cause: error instanceof Error ? error : undefined,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function childState(overallState: ResultRepairData["state"], child: ResultChildOutcome | undefined): "complete" | "failed" | "paused" {
|
|
110
|
+
if (child?.success === true) return "complete";
|
|
111
|
+
if (child?.success === false) return "failed";
|
|
112
|
+
return overallState;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function terminalStatusFromResult(status: AsyncStatus, resultPath: string, now: number): AsyncStatus | undefined {
|
|
116
|
+
const repair = readResultRepairData(resultPath);
|
|
117
|
+
if (!repair) return undefined;
|
|
118
|
+
const steps = (status.steps ?? []).map((step, index) => {
|
|
119
|
+
if (step.status !== "running" && step.status !== "pending") return step;
|
|
120
|
+
const child = repair.results?.[index];
|
|
121
|
+
const state = childState(repair.state, child);
|
|
122
|
+
return {
|
|
123
|
+
...step,
|
|
124
|
+
status: state === "complete" ? "complete" as const : state,
|
|
125
|
+
endedAt: step.endedAt ?? now,
|
|
126
|
+
durationMs: step.startedAt !== undefined && step.durationMs === undefined ? Math.max(0, now - step.startedAt) : step.durationMs,
|
|
127
|
+
exitCode: step.exitCode ?? (state === "complete" || state === "paused" ? 0 : 1),
|
|
128
|
+
error: state === "failed" ? step.error ?? child?.error : step.error,
|
|
129
|
+
sessionFile: step.sessionFile ?? child?.sessionFile,
|
|
130
|
+
model: step.model ?? child?.model,
|
|
131
|
+
attemptedModels: step.attemptedModels ?? child?.attemptedModels,
|
|
132
|
+
modelAttempts: step.modelAttempts ?? child?.modelAttempts,
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
return {
|
|
136
|
+
...status,
|
|
137
|
+
state: repair.state,
|
|
138
|
+
activityState: undefined,
|
|
139
|
+
lastUpdate: now,
|
|
140
|
+
endedAt: status.endedAt ?? now,
|
|
141
|
+
steps,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function buildStartedStatus(asyncDir: string, startedRun: StartedRunMetadata, now: number): AsyncStatus {
|
|
146
|
+
const startedAt = startedRun.startedAt ?? now;
|
|
147
|
+
const agents = startedRun.agents?.length ? startedRun.agents : ["subagent"];
|
|
148
|
+
const chainStepCount = startedRun.chainStepCount;
|
|
149
|
+
const parallelGroups = chainStepCount !== undefined
|
|
150
|
+
? normalizeParallelGroups(startedRun.parallelGroups, agents.length, chainStepCount)
|
|
151
|
+
: [];
|
|
152
|
+
return {
|
|
153
|
+
runId: startedRun.runId || path.basename(asyncDir),
|
|
154
|
+
...(startedRun.sessionId ? { sessionId: startedRun.sessionId } : {}),
|
|
155
|
+
mode: startedRun.mode ?? "single",
|
|
156
|
+
state: "running",
|
|
157
|
+
pid: startedRun.pid,
|
|
158
|
+
startedAt,
|
|
159
|
+
lastUpdate: now,
|
|
160
|
+
currentStep: 0,
|
|
161
|
+
...(chainStepCount !== undefined ? { chainStepCount } : {}),
|
|
162
|
+
...(parallelGroups.length ? { parallelGroups } : {}),
|
|
163
|
+
steps: agents.map((agent) => ({
|
|
164
|
+
agent,
|
|
165
|
+
status: "running" as const,
|
|
166
|
+
startedAt,
|
|
167
|
+
})),
|
|
168
|
+
...(startedRun.sessionFile ? { sessionFile: startedRun.sessionFile } : {}),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function buildFailedRepair(status: AsyncStatus, asyncDir: string, now: number, reason?: string): { status: AsyncStatus; result: object; message: string } {
|
|
173
|
+
const runId = status.runId || path.basename(asyncDir);
|
|
174
|
+
const pid = typeof status.pid === "number" ? status.pid : "unknown";
|
|
175
|
+
const message = reason ?? `Async runner process ${pid} exited or disappeared before writing a result. Marked run failed by stale-run reconciliation.`;
|
|
176
|
+
const steps = status.steps?.length ? status.steps : [{ agent: "subagent", status: "running" as const }];
|
|
177
|
+
const repairedSteps = steps.map((step) => step.status === "running" || step.status === "pending"
|
|
178
|
+
? {
|
|
179
|
+
...step,
|
|
180
|
+
status: "failed" as const,
|
|
181
|
+
activityState: undefined,
|
|
182
|
+
endedAt: step.endedAt ?? now,
|
|
183
|
+
durationMs: step.startedAt !== undefined && step.durationMs === undefined ? Math.max(0, now - step.startedAt) : step.durationMs,
|
|
184
|
+
exitCode: step.exitCode ?? 1,
|
|
185
|
+
error: step.error ?? message,
|
|
186
|
+
}
|
|
187
|
+
: step);
|
|
188
|
+
const repairedStatus: AsyncStatus = {
|
|
189
|
+
...status,
|
|
190
|
+
state: "failed",
|
|
191
|
+
activityState: undefined,
|
|
192
|
+
lastUpdate: now,
|
|
193
|
+
endedAt: now,
|
|
194
|
+
steps: repairedSteps,
|
|
195
|
+
};
|
|
196
|
+
const resultAgent = repairedSteps[status.currentStep ?? 0]?.agent ?? repairedSteps[0]?.agent ?? "subagent";
|
|
197
|
+
return {
|
|
198
|
+
status: repairedStatus,
|
|
199
|
+
message,
|
|
200
|
+
result: {
|
|
201
|
+
id: runId,
|
|
202
|
+
agent: resultAgent,
|
|
203
|
+
mode: status.mode,
|
|
204
|
+
success: false,
|
|
205
|
+
state: "failed",
|
|
206
|
+
summary: message,
|
|
207
|
+
results: repairedSteps.map((step) => ({
|
|
208
|
+
agent: step.agent,
|
|
209
|
+
output: step.status === "complete" || step.status === "completed" ? "" : message,
|
|
210
|
+
error: step.status === "complete" || step.status === "completed" ? undefined : step.error ?? message,
|
|
211
|
+
success: step.status === "complete" || step.status === "completed",
|
|
212
|
+
model: step.model,
|
|
213
|
+
attemptedModels: step.attemptedModels,
|
|
214
|
+
modelAttempts: step.modelAttempts,
|
|
215
|
+
sessionFile: step.sessionFile,
|
|
216
|
+
})),
|
|
217
|
+
exitCode: 1,
|
|
218
|
+
timestamp: now,
|
|
219
|
+
durationMs: Math.max(0, now - status.startedAt),
|
|
220
|
+
asyncDir,
|
|
221
|
+
sessionId: status.sessionId,
|
|
222
|
+
sessionFile: status.sessionFile,
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function writeFailedRepair(asyncDir: string, status: AsyncStatus, resultPath: string, now: number, reason?: string): ReconcileAsyncRunResult {
|
|
228
|
+
const repair = buildFailedRepair(status, asyncDir, now, reason);
|
|
229
|
+
writeAtomicJson(resultPath, repair.result);
|
|
230
|
+
writeAtomicJson(path.join(asyncDir, "status.json"), repair.status);
|
|
231
|
+
appendJsonlBestEffort(path.join(asyncDir, "events.jsonl"), {
|
|
232
|
+
type: "subagent.run.repaired_stale",
|
|
233
|
+
ts: now,
|
|
234
|
+
runId: repair.status.runId,
|
|
235
|
+
pid: status.pid,
|
|
236
|
+
resultPath,
|
|
237
|
+
message: repair.message,
|
|
238
|
+
});
|
|
239
|
+
return { status: repair.status, repaired: true, resultPath, message: repair.message };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function terminal(state: AsyncStatus["state"]): boolean {
|
|
243
|
+
return state === "complete" || state === "failed" || state === "paused";
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function* nestedRuns(children: NestedRunSummary[] | undefined): Generator<NestedRunSummary> {
|
|
247
|
+
for (const child of children ?? []) {
|
|
248
|
+
yield child;
|
|
249
|
+
yield* nestedRuns(child.children);
|
|
250
|
+
yield* nestedRuns(child.steps?.flatMap((step) => step.children ?? []));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function reconcileNestedAsyncDescendants(route: NestedRoute, options: ReconcileAsyncRunOptions = {}): void {
|
|
255
|
+
const registry = projectNestedEvents(route);
|
|
256
|
+
for (const run of nestedRuns(registry.children)) {
|
|
257
|
+
if (run.state !== "running" && run.state !== "queued") continue;
|
|
258
|
+
const asyncDir = resolveNestedAsyncDir(route.rootRunId, run);
|
|
259
|
+
if (!asyncDir) continue;
|
|
260
|
+
const result = reconcileAsyncRun(asyncDir, {
|
|
261
|
+
...options,
|
|
262
|
+
resultsDir: path.join(options.resultsDir ?? RESULTS_DIR, "nested", route.rootRunId),
|
|
263
|
+
});
|
|
264
|
+
const status = result.status;
|
|
265
|
+
if (!status) continue;
|
|
266
|
+
if (!result.repaired && !terminal(status.state)) continue;
|
|
267
|
+
const ts = options.now?.() ?? Date.now();
|
|
268
|
+
writeNestedEvent(route, {
|
|
269
|
+
type: terminal(status.state) ? "subagent.nested.completed" : "subagent.nested.updated",
|
|
270
|
+
ts,
|
|
271
|
+
parentRunId: run.parentRunId,
|
|
272
|
+
parentStepIndex: run.parentStepIndex,
|
|
273
|
+
child: nestedSummaryFromAsyncStatus(status, asyncDir, {
|
|
274
|
+
id: run.id,
|
|
275
|
+
parentRunId: run.parentRunId,
|
|
276
|
+
parentStepIndex: run.parentStepIndex,
|
|
277
|
+
depth: run.depth,
|
|
278
|
+
path: run.path,
|
|
279
|
+
mode: run.mode,
|
|
280
|
+
ts,
|
|
281
|
+
}),
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function checkPidLiveness(pid: number, kill: KillFn = process.kill): PidLiveness {
|
|
287
|
+
try {
|
|
288
|
+
kill(pid, 0);
|
|
289
|
+
return "alive";
|
|
290
|
+
} catch (error) {
|
|
291
|
+
const code = typeof error === "object" && error !== null && "code" in error
|
|
292
|
+
? (error as NodeJS.ErrnoException).code
|
|
293
|
+
: undefined;
|
|
294
|
+
if (code === "ESRCH") return "dead";
|
|
295
|
+
if (code === "EPERM") return "unknown";
|
|
296
|
+
return "unknown";
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function reconcileAsyncRun(asyncDir: string, options: ReconcileAsyncRunOptions = {}): ReconcileAsyncRunResult {
|
|
301
|
+
const now = options.now?.() ?? Date.now();
|
|
302
|
+
const status = readStatusFile(asyncDir);
|
|
303
|
+
const startedStatus = !status && options.startedRun ? buildStartedStatus(asyncDir, options.startedRun, now) : undefined;
|
|
304
|
+
const effectiveStatus = status ?? startedStatus;
|
|
305
|
+
if (!effectiveStatus) return { status: null, repaired: false };
|
|
306
|
+
|
|
307
|
+
const runId = effectiveStatus.runId || path.basename(asyncDir);
|
|
308
|
+
const resultPath = path.join(options.resultsDir ?? RESULTS_DIR, `${runId}.json`);
|
|
309
|
+
if (fs.existsSync(resultPath)) {
|
|
310
|
+
const terminalStatus = effectiveStatus.state === "running" || effectiveStatus.state === "queued"
|
|
311
|
+
? terminalStatusFromResult(effectiveStatus, resultPath, now)
|
|
312
|
+
: undefined;
|
|
313
|
+
if (terminalStatus) {
|
|
314
|
+
writeAtomicJson(path.join(asyncDir, "status.json"), terminalStatus);
|
|
315
|
+
return { status: terminalStatus, repaired: true, resultPath, message: "Existing async result file was used to repair stale running status." };
|
|
316
|
+
}
|
|
317
|
+
return { status: effectiveStatus, repaired: false, resultPath };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (effectiveStatus.state !== "running" || typeof effectiveStatus.pid !== "number") {
|
|
321
|
+
return { status: status ?? null, repaired: false, resultPath };
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (!status) {
|
|
325
|
+
const startedAt = options.startedRun?.startedAt ?? effectiveStatus.startedAt;
|
|
326
|
+
if (now - startedAt < (options.missingStatusGraceMs ?? 1000)) {
|
|
327
|
+
return { status: null, repaired: false, resultPath };
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const liveness = checkPidLiveness(effectiveStatus.pid, options.kill);
|
|
332
|
+
if (liveness !== "dead") {
|
|
333
|
+
const staleAfterMs = options.staleAlivePidMs ?? 24 * 60 * 60 * 1000;
|
|
334
|
+
const lastUpdate = effectiveStatus.lastUpdate ?? effectiveStatus.startedAt;
|
|
335
|
+
if (now - lastUpdate <= staleAfterMs) return { status: status ?? null, repaired: false, resultPath };
|
|
336
|
+
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.`;
|
|
337
|
+
return writeFailedRepair(asyncDir, effectiveStatus, resultPath, now, message);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return writeFailedRepair(asyncDir, effectiveStatus, resultPath, now);
|
|
341
|
+
}
|