oira666_pi-subagent 0.5.13 → 0.5.14
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/docs/performance-audit.md +59 -0
- package/extension/progress-details.ts +134 -0
- package/extension/progress-preview.ts +92 -0
- package/extension/progress.ts +3 -44
- package/package.json +1 -1
- package/runner/attempt.ts +5 -23
- package/runner/events.ts +3 -23
- package/runner/jsonl-framer.ts +48 -0
- package/runner/message-index.ts +61 -0
- package/runner/parallel.ts +26 -10
- package/runner/progress-coalescer.ts +44 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Main-process CPU audit
|
|
2
|
+
|
|
3
|
+
This audit found reproducible excess work in the extension. It does not establish what saturated the reported Pi process. No CPU profile of that process was captured.
|
|
4
|
+
|
|
5
|
+
## Fixed
|
|
6
|
+
|
|
7
|
+
### Message deduplication
|
|
8
|
+
|
|
9
|
+
`runner/events.ts` previously serialized each earlier message while checking every new message. For N distinct messages without IDs, it computed N × (N + 1) / 2 equality keys. Tool output and thinking content made those keys expensive.
|
|
10
|
+
|
|
11
|
+
`runner/message-index.ts` now indexes finalized messages by SHA256 fingerprint. Each distinct message is serialized once. Matching fingerprints still receive an exact comparison, preserving the existing equality rules. A weak reference to the history owns the index, and serialized transcript copies are not retained.
|
|
12
|
+
|
|
13
|
+
The index assumes finalized messages remain immutable and histories are append-only. New arrays and shortened arrays are reindexed. Replacing an existing array element in place is not supported.
|
|
14
|
+
|
|
15
|
+
`test/message-dedup-index.test.ts` verifies that 200 resumed messages plus 400 new messages require 600 serializations, not repeated scans of the history. It also covers legacy events, equality compatibility, usage accounting and nested-progress cleanup.
|
|
16
|
+
|
|
17
|
+
### Fragmented stdout records
|
|
18
|
+
|
|
19
|
+
`runner/attempt.ts` previously appended each stdout chunk to a buffer and split the entire buffer on every arrival. A large JSON record arriving in small chunks repeatedly scanned and copied the accumulated prefix.
|
|
20
|
+
|
|
21
|
+
`runner/jsonl-framer.ts` now scans only newly decoded fragments and joins them when a complete record arrives. Tests cover fragmented UTF-8, CRLF, Unicode separators inside JSON, multiple records, and unterminated final records. The scan-count test checks linear growth for a 2 MiB record.
|
|
22
|
+
|
|
23
|
+
A single record still requires memory proportional to its size, and `JSON.parse` still runs synchronously. This patch does not impose a new record-size limit.
|
|
24
|
+
|
|
25
|
+
### Batch progress updates
|
|
26
|
+
|
|
27
|
+
`runner/parallel.ts` previously built and forwarded a whole-batch snapshot on every child update. Nested progress then repeated usage aggregation, serialization and rendering at each ancestor.
|
|
28
|
+
|
|
29
|
+
`runner/progress-coalescer.ts` now limits routine batch updates to one per 100 ms. Incoming results update immediately; snapshot construction waits until emission. Initial progress is immediate, heartbeats share the limiter, and completion flushes pending state. Abort and failure cancel queued callbacks.
|
|
30
|
+
|
|
31
|
+
Tests cover bursts, simultaneous siblings, final flushing, heartbeat behavior, abort, failure and late sibling updates. A two-child integration test sends 200 message events without constructing a batch snapshot for each event.
|
|
32
|
+
|
|
33
|
+
### Progress payloads
|
|
34
|
+
|
|
35
|
+
`extension/progress-details.ts` now constructs an explicit wire projection rather than spreading full results and messages. It excludes ordinary transcript messages, tool-result bodies, arbitrary metadata, active-tool arguments, and recursive usage trees. Completed delegation results retain only the structure needed for tree rendering and accounting.
|
|
36
|
+
|
|
37
|
+
Activity logs retain at most six entries. Tool arguments include only bounded display fields such as command, path or pattern, never write content or edit replacements. Individual argument strings are capped at 256 characters; error previews at 1,000 characters. Completed output previews use at most the final 2,048 characters, reduced to six lines of at most 240 characters each. Running output text is omitted.
|
|
38
|
+
|
|
39
|
+
Task text, call IDs, names and usage identities remain exact because current consumers use them for structural matching, accounting and steering. No descendant-count or tree-depth cap is introduced. These changes reduce repeated payloads; they do not impose a hard record-size limit.
|
|
40
|
+
|
|
41
|
+
The projection accepts compact saved details with no message array. Full final results, saved transcripts and resume data are unchanged. Pi's own `message_end` and `agent_end` records can still contain large messages; this patch only controls the extension's `subagent_progress` records.
|
|
42
|
+
|
|
43
|
+
Payload tests verify that growing discarded data to two million characters does not increase progress size, that previews are bounded, and that projection does not mutate source data. Compatibility tests cover trees, legacy calls, signature fallback, usage, identities, steering paths and compact saved results.
|
|
44
|
+
|
|
45
|
+
## Remaining work
|
|
46
|
+
|
|
47
|
+
Profile a long-running, nested workload before expanding this patch. In particular:
|
|
48
|
+
|
|
49
|
+
- `runner/single.ts` still builds each child's live details for every accepted semantic event. Batch coalescing limits downstream work, not this per-child cost.
|
|
50
|
+
- Progress still recursively projects the structural tree, and `runner/protocol.ts` serializes that projection again to detect changes. Large delegation trees and long task prompts can remain expensive even after body payloads are removed.
|
|
51
|
+
- `render.ts` builds tree nodes on repeated collapsed renders, and `ui/tree-build.ts` scans histories to build nested children and previews. Consider width/content caching with correct invalidation if a profile attributes substantial CPU here.
|
|
52
|
+
- `runner/attempt.ts` clears and allocates an inactivity timer on each streaming delta. A mutable deadline and one timer would avoid that churn, but must preserve retry grace and active-tool exemptions.
|
|
53
|
+
- Session header/settings reads load whole transcripts. Registry and budget operations use synchronous filesystem work. These can block the event loop, though disk waits alone do not explain sustained CPU saturation.
|
|
54
|
+
|
|
55
|
+
For a follow-up measurement, record parent-process CPU and event-loop delay with representative transcript sizes and nesting. Compare progress enabled and disabled, and interactive and non-interactive runs. Capture a Node CPU profile of the parent to distinguish JSON processing, tree rendering and filesystem work from Pi's own costs.
|
|
56
|
+
|
|
57
|
+
## Validation
|
|
58
|
+
|
|
59
|
+
`npm run check` passed after the changes: TypeScript, formatting, file-size checks and all 447 tests. These regression checks demonstrate reduced repeated work; they are not a production CPU measurement.
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type SingleResult,
|
|
3
|
+
type SubagentDetails,
|
|
4
|
+
type SubagentUsageSummary,
|
|
5
|
+
type UsageStats,
|
|
6
|
+
emptyUsage,
|
|
7
|
+
aggregateUsage,
|
|
8
|
+
getFinalOutput,
|
|
9
|
+
isSubagentDetails,
|
|
10
|
+
isSubagentToolName,
|
|
11
|
+
} from "../types.js";
|
|
12
|
+
import { isRecord, recordArray } from "../types/records.js";
|
|
13
|
+
import type { TranscriptMessage } from "../types/transcript.js";
|
|
14
|
+
import { headPreview, outputPreview, progressLog, PROGRESS_ERROR_CHARS } from "./progress-preview.js";
|
|
15
|
+
|
|
16
|
+
function usage(value: UsageStats | undefined): UsageStats {
|
|
17
|
+
const result = emptyUsage();
|
|
18
|
+
for (const key of Object.keys(result) as Array<keyof UsageStats>) {
|
|
19
|
+
if (typeof value?.[key] === "number") result[key] = value[key];
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function summary(value: SubagentUsageSummary | undefined): SubagentUsageSummary | undefined {
|
|
25
|
+
if (!value) return undefined;
|
|
26
|
+
return {
|
|
27
|
+
subagentCount: value.subagentCount,
|
|
28
|
+
subagentIds: value.subagentIds?.filter((id) => typeof id === "string"),
|
|
29
|
+
inputTokens: value.inputTokens,
|
|
30
|
+
outputTokens: value.outputTokens,
|
|
31
|
+
cacheReadTokens: value.cacheReadTokens,
|
|
32
|
+
cacheWriteTokens: value.cacheWriteTokens,
|
|
33
|
+
costUsd: value.costUsd,
|
|
34
|
+
turns: value.turns,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function strings(value: Record<string, unknown>, keys: string[]): Record<string, string> {
|
|
39
|
+
return Object.fromEntries(keys.flatMap((key) => (typeof value[key] === "string" ? [[key, value[key]]] : [])));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** These tasks are structural matching keys, not tool-argument previews. Keep them exact. */
|
|
43
|
+
function delegationArguments(value: unknown): Record<string, unknown> {
|
|
44
|
+
const args = isRecord(value) ? value : {};
|
|
45
|
+
const result: Record<string, unknown> = {};
|
|
46
|
+
if (Array.isArray(args.tasks)) {
|
|
47
|
+
result.tasks = recordArray(args.tasks).map((task) => strings(task, ["agent", "name", "intelligence", "task"]));
|
|
48
|
+
}
|
|
49
|
+
const resume = (item: Record<string, unknown>) => strings(item, ["subagent", "name", "task", "prompt"]);
|
|
50
|
+
if (Array.isArray(args.resumes)) result.resumes = recordArray(args.resumes).map(resume);
|
|
51
|
+
else if (isRecord(args.resumes)) result.resumes = resume(args.resumes);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function messagesForProgress(value: unknown): TranscriptMessage[] {
|
|
56
|
+
const messages: TranscriptMessage[] = [];
|
|
57
|
+
for (const message of recordArray(value)) {
|
|
58
|
+
if (message.role === "assistant") {
|
|
59
|
+
const content = recordArray(message.content)
|
|
60
|
+
.filter((part) => part.type === "toolCall" && isSubagentToolName(part.name))
|
|
61
|
+
.map((part) => ({
|
|
62
|
+
type: "toolCall",
|
|
63
|
+
...strings(part, ["name", "id", "toolCallId"]),
|
|
64
|
+
arguments: delegationArguments(part.arguments),
|
|
65
|
+
}));
|
|
66
|
+
if (content.length) messages.push({ role: "assistant", content });
|
|
67
|
+
} else if (message.role === "toolResult" && isSubagentToolName(message.toolName)) {
|
|
68
|
+
// Invalid details cannot supply tree/accounting information. Never forward
|
|
69
|
+
// their original content as a fallback, since it can include arbitrary blobs.
|
|
70
|
+
if (!isSubagentDetails(message.details)) continue;
|
|
71
|
+
messages.push({
|
|
72
|
+
role: "toolResult",
|
|
73
|
+
...strings(message, ["toolName", "toolCallId"]),
|
|
74
|
+
isError: message.isError === true,
|
|
75
|
+
details: slimDetailsForProgress(message.details),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return messages;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function resultForProgress(result: SingleResult): SingleResult {
|
|
83
|
+
const liveNestedSubagents = result.liveNestedSubagents
|
|
84
|
+
? Object.fromEntries(
|
|
85
|
+
Object.entries(result.liveNestedSubagents)
|
|
86
|
+
.filter(([, details]) => isSubagentDetails(details))
|
|
87
|
+
.map(([id, details]) => [id, slimDetailsForProgress(details)]),
|
|
88
|
+
)
|
|
89
|
+
: undefined;
|
|
90
|
+
return {
|
|
91
|
+
agent: result.agent,
|
|
92
|
+
agentSource: result.agentSource,
|
|
93
|
+
task: result.task,
|
|
94
|
+
name: result.name,
|
|
95
|
+
intelligence: result.intelligence,
|
|
96
|
+
exitCode: result.exitCode,
|
|
97
|
+
stopReason: result.stopReason,
|
|
98
|
+
model: result.model,
|
|
99
|
+
startedAt: result.startedAt,
|
|
100
|
+
lastActionAt: result.lastActionAt,
|
|
101
|
+
// Identity references preserve usage-count deduplication across resumes.
|
|
102
|
+
budget: result.budget ? { directory: result.budget.directory } : undefined,
|
|
103
|
+
sessionDir: result.sessionDir,
|
|
104
|
+
sessionId: result.sessionId,
|
|
105
|
+
usage: usage(result.usage),
|
|
106
|
+
subtreeUsageSummary: summary(result.subtreeUsageSummary),
|
|
107
|
+
priorDescendantUsageSummary: summary(result.priorDescendantUsageSummary),
|
|
108
|
+
messages: messagesForProgress(result.messages),
|
|
109
|
+
liveNestedSubagents,
|
|
110
|
+
liveLog: progressLog(result.liveLog),
|
|
111
|
+
stderr: typeof result.stderr === "string" ? result.stderr.slice(-PROGRESS_ERROR_CHARS) : "",
|
|
112
|
+
errorMessage: headPreview(result.errorMessage, PROGRESS_ERROR_CHARS),
|
|
113
|
+
finalOutput:
|
|
114
|
+
result.exitCode === -1 ? undefined : outputPreview(getFinalOutput(result.messages, result.finalOutput)),
|
|
115
|
+
// Empty compatibility fields, never a copy of process state or tool arguments.
|
|
116
|
+
toolCalls: {},
|
|
117
|
+
completedTurns: 0,
|
|
118
|
+
turnInProgress: false,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Transient wire projection only. Never use this in durable results or saved transcripts. */
|
|
123
|
+
export function slimDetailsForProgress(details: SubagentDetails): SubagentDetails {
|
|
124
|
+
return {
|
|
125
|
+
mode: details.mode,
|
|
126
|
+
delegationMode: details.delegationMode,
|
|
127
|
+
projectAgentsDir: null,
|
|
128
|
+
results: details.results.map(resultForProgress),
|
|
129
|
+
usageSummary: summary(details.usageSummary),
|
|
130
|
+
aggregatedUsage: usage(details.aggregatedUsage ?? aggregateUsage(details.results)),
|
|
131
|
+
aggregatedToolCalls: {},
|
|
132
|
+
usageTree: [],
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { MAX_LIVE_LOG_ENTRIES, type LiveLogEntry } from "../types.js";
|
|
2
|
+
import { isRecord, recordArray } from "../types/records.js";
|
|
3
|
+
|
|
4
|
+
export const PROGRESS_ARGUMENT_CHARS = 256;
|
|
5
|
+
export const PROGRESS_ERROR_CHARS = 1000;
|
|
6
|
+
export const PROGRESS_OUTPUT_CHARS = 2048;
|
|
7
|
+
|
|
8
|
+
export function headPreview(value: unknown, limit: number): string | undefined {
|
|
9
|
+
if (typeof value !== "string") return undefined;
|
|
10
|
+
return value.length <= limit ? value : `${value.slice(0, limit - 1)}…`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Slice before splitting so preview construction never copies an entire large answer. */
|
|
14
|
+
export function outputPreview(value: string): string {
|
|
15
|
+
return value
|
|
16
|
+
.slice(-PROGRESS_OUTPUT_CHARS)
|
|
17
|
+
.replace(/\r\n?/g, "\n")
|
|
18
|
+
.split("\n")
|
|
19
|
+
.filter((line) => line.trim())
|
|
20
|
+
.slice(-6)
|
|
21
|
+
.map((line) => headPreview(line, 240))
|
|
22
|
+
.join("\n");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function argumentPreview(toolName: string, value: unknown): Record<string, unknown> {
|
|
26
|
+
const args = isRecord(value) ? value : {};
|
|
27
|
+
const strings = (keys: string[]) =>
|
|
28
|
+
Object.fromEntries(
|
|
29
|
+
keys.flatMap((key) => {
|
|
30
|
+
const text = headPreview(args[key], PROGRESS_ARGUMENT_CHARS);
|
|
31
|
+
return text === undefined ? [] : [[key, text]];
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
switch (toolName) {
|
|
35
|
+
case "bash":
|
|
36
|
+
return strings(["command"]);
|
|
37
|
+
case "read":
|
|
38
|
+
case "write":
|
|
39
|
+
case "edit":
|
|
40
|
+
return strings(["path", "file_path"]);
|
|
41
|
+
case "grep":
|
|
42
|
+
case "find":
|
|
43
|
+
return strings(["pattern", "path"]);
|
|
44
|
+
case "subagent":
|
|
45
|
+
case "subagents":
|
|
46
|
+
return {
|
|
47
|
+
tasks: recordArray(args.tasks)
|
|
48
|
+
.slice(0, MAX_LIVE_LOG_ENTRIES)
|
|
49
|
+
.map((task) => ({ agent: headPreview(task.agent, PROGRESS_ARGUMENT_CHARS) })),
|
|
50
|
+
};
|
|
51
|
+
case "resume_subagents":
|
|
52
|
+
return {
|
|
53
|
+
resumes: recordArray(Array.isArray(args.resumes) ? args.resumes : [args.resumes])
|
|
54
|
+
.slice(0, MAX_LIVE_LOG_ENTRIES)
|
|
55
|
+
.map((resume) => ({
|
|
56
|
+
subagent: headPreview(resume.subagent, PROGRESS_ARGUMENT_CHARS),
|
|
57
|
+
name: headPreview(resume.name, PROGRESS_ARGUMENT_CHARS),
|
|
58
|
+
})),
|
|
59
|
+
};
|
|
60
|
+
default:
|
|
61
|
+
return {};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function progressLog(value: unknown): LiveLogEntry[] {
|
|
66
|
+
const result: LiveLogEntry[] = [];
|
|
67
|
+
for (const entry of recordArray(value).slice(-MAX_LIVE_LOG_ENTRIES)) {
|
|
68
|
+
const at = typeof entry.at === "number" ? entry.at : undefined;
|
|
69
|
+
const toolName = headPreview(entry.toolName, PROGRESS_ARGUMENT_CHARS) ?? "unknown tool";
|
|
70
|
+
switch (entry.kind) {
|
|
71
|
+
case "turn_start":
|
|
72
|
+
result.push({ kind: "turn_start", at });
|
|
73
|
+
break;
|
|
74
|
+
case "turn_end":
|
|
75
|
+
result.push({
|
|
76
|
+
kind: "turn_end",
|
|
77
|
+
at,
|
|
78
|
+
turn: typeof entry.turn === "number" ? entry.turn : 0,
|
|
79
|
+
inputTokens: typeof entry.inputTokens === "number" ? entry.inputTokens : 0,
|
|
80
|
+
outputTokens: typeof entry.outputTokens === "number" ? entry.outputTokens : 0,
|
|
81
|
+
});
|
|
82
|
+
break;
|
|
83
|
+
case "tool_start":
|
|
84
|
+
result.push({ kind: "tool_start", at, toolName, args: argumentPreview(toolName, entry.args) });
|
|
85
|
+
break;
|
|
86
|
+
case "tool_end":
|
|
87
|
+
result.push({ kind: "tool_end", at, toolName });
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
}
|
package/extension/progress.ts
CHANGED
|
@@ -1,53 +1,12 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type SubagentDetails, isSubagentDetails } from "../types.js";
|
|
2
|
+
import { slimDetailsForProgress } from "./progress-details.js";
|
|
3
|
+
export { slimDetailsForProgress } from "./progress-details.js";
|
|
2
4
|
import { updateLatestBroadcastTargets } from "./broadcast.js";
|
|
3
5
|
import type { ProgressUpdate, SessionContext } from "./contracts.js";
|
|
4
6
|
import { updateCombinedUsageStatus } from "./runtime.js";
|
|
5
7
|
import type { ExtensionState } from "./state.js";
|
|
6
8
|
import { collectLiveUsageSummary } from "./usage.js";
|
|
7
9
|
|
|
8
|
-
export function slimDetailsForProgress(details: SubagentDetails): SubagentDetails {
|
|
9
|
-
const slimResult = (result: SingleResult): SingleResult => {
|
|
10
|
-
const slimMessages = result.messages
|
|
11
|
-
.map((message) => {
|
|
12
|
-
if (message?.role === "assistant" && Array.isArray(message.content)) {
|
|
13
|
-
const subagentCalls = message.content.filter(
|
|
14
|
-
(part) => part?.type === "toolCall" && isSubagentToolName(part?.name),
|
|
15
|
-
);
|
|
16
|
-
return subagentCalls.length > 0 ? { ...message, content: subagentCalls } : null;
|
|
17
|
-
}
|
|
18
|
-
if (message?.role === "toolResult" && isSubagentToolName(message.toolName)) {
|
|
19
|
-
return isSubagentDetails(message.details)
|
|
20
|
-
? { ...message, details: slimDetailsForProgress(message.details) }
|
|
21
|
-
: message;
|
|
22
|
-
}
|
|
23
|
-
return null;
|
|
24
|
-
})
|
|
25
|
-
.filter(Boolean) as SingleResult["messages"];
|
|
26
|
-
|
|
27
|
-
const liveNestedSubagents = result.liveNestedSubagents
|
|
28
|
-
? Object.fromEntries(
|
|
29
|
-
Object.entries(result.liveNestedSubagents).map(([nestedToolCallId, nested]) => [
|
|
30
|
-
nestedToolCallId,
|
|
31
|
-
slimDetailsForProgress(nested),
|
|
32
|
-
]),
|
|
33
|
-
)
|
|
34
|
-
: undefined;
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
...result,
|
|
38
|
-
messages: slimMessages,
|
|
39
|
-
stderr: result.stderr ? result.stderr.slice(-1000) : "",
|
|
40
|
-
liveLog: [...(result.liveLog ?? [])],
|
|
41
|
-
liveNestedSubagents,
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
...details,
|
|
47
|
-
results: details.results.map(slimResult),
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
10
|
export function emitNestedProgressToParent(state: ExtensionState, toolCallId: string, details: SubagentDetails): void {
|
|
52
11
|
if (state.currentDepth <= 0) return;
|
|
53
12
|
try {
|
package/package.json
CHANGED
package/runner/attempt.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import {
|
|
2
|
+
import { JsonlFramer } from "./jsonl-framer.js";
|
|
3
3
|
import { isResultError, type SingleResult } from "../types.js";
|
|
4
4
|
import { SUBAGENT_SESSION_ROOT_ENV } from "../resume.js";
|
|
5
5
|
import { SUBAGENT_NAMES_FILE_ENV } from "../names.js";
|
|
@@ -93,8 +93,6 @@ export async function runAttempt(
|
|
|
93
93
|
}),
|
|
94
94
|
});
|
|
95
95
|
|
|
96
|
-
let buffer = "";
|
|
97
|
-
const stdoutDecoder = new StringDecoder("utf8");
|
|
98
96
|
let abortHandler: (() => void) | undefined;
|
|
99
97
|
const promptRequestId = `pi-subagent-${process.pid}-${Date.now()}-${attempt}`;
|
|
100
98
|
let steeringRequest = 0;
|
|
@@ -230,18 +228,9 @@ export async function runAttempt(
|
|
|
230
228
|
}, startupTimeoutMs);
|
|
231
229
|
}
|
|
232
230
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
buffer = lines.pop() || "";
|
|
237
|
-
for (let line of lines) {
|
|
238
|
-
if (line.endsWith("\r")) line = line.slice(0, -1);
|
|
239
|
-
flushLine(line);
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
proc.stdout.on("end", () => {
|
|
243
|
-
buffer += stdoutDecoder.end();
|
|
244
|
-
});
|
|
231
|
+
const stdoutFramer = new JsonlFramer(flushLine);
|
|
232
|
+
proc.stdout.on("data", (chunk: Buffer) => stdoutFramer.write(chunk));
|
|
233
|
+
proc.stdout.on("end", () => stdoutFramer.end());
|
|
245
234
|
|
|
246
235
|
let stderrBuffer = "";
|
|
247
236
|
const flushStderrLine = (line: string) => {
|
|
@@ -288,15 +277,8 @@ export async function runAttempt(
|
|
|
288
277
|
return code !== null && code !== 0 ? code : 1;
|
|
289
278
|
};
|
|
290
279
|
|
|
291
|
-
const flushRemainingStdout = () => {
|
|
292
|
-
if (!buffer.trim()) return;
|
|
293
|
-
const line = buffer.endsWith("\r") ? buffer.slice(0, -1) : buffer;
|
|
294
|
-
buffer = "";
|
|
295
|
-
flushLine(line);
|
|
296
|
-
};
|
|
297
|
-
|
|
298
280
|
proc.on("close", (code, exitSignal) => {
|
|
299
|
-
|
|
281
|
+
stdoutFramer.end();
|
|
300
282
|
flushRemainingStderr();
|
|
301
283
|
if (!state.resolved) doResolve(classifyUnexpectedExit(code, exitSignal));
|
|
302
284
|
});
|
package/runner/events.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
isSubagentToolName,
|
|
8
8
|
} from "../types.js";
|
|
9
9
|
import { isRecord } from "../types/records.js";
|
|
10
|
+
import { appendUniqueMessage } from "./message-index.js";
|
|
10
11
|
|
|
11
12
|
const SEMANTIC_EVENT_TYPES = new Set([
|
|
12
13
|
"message_end",
|
|
@@ -39,26 +40,6 @@ function pushLiveLog(result: SingleResult, entry: LiveLogEntry): void {
|
|
|
39
40
|
if (result.liveLog.length > MAX_LIVE_LOG_ENTRIES) result.liveLog.shift();
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
function messageDedupKey(value: unknown): string {
|
|
43
|
-
const message = isRecord(value) ? value : {};
|
|
44
|
-
if (typeof message.id === "string") return `id:${message.id}`;
|
|
45
|
-
return JSON.stringify({
|
|
46
|
-
role: message.role,
|
|
47
|
-
provider: message.provider,
|
|
48
|
-
model: message.model,
|
|
49
|
-
stopReason: message.stopReason,
|
|
50
|
-
toolCallId: message.toolCallId,
|
|
51
|
-
toolName: message.toolName,
|
|
52
|
-
content: message.content,
|
|
53
|
-
usage: message.usage,
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function hasMessage(result: SingleResult, message: TranscriptMessage): boolean {
|
|
58
|
-
const key = messageDedupKey(message);
|
|
59
|
-
return result.messages.some((existing) => messageDedupKey(existing) === key);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
43
|
function clearNestedProgress(result: SingleResult, message: TranscriptMessage): void {
|
|
63
44
|
if (isSubagentToolName(message.toolName) && typeof message.toolCallId === "string") {
|
|
64
45
|
delete result.liveNestedSubagents?.[message.toolCallId];
|
|
@@ -85,9 +66,8 @@ export function processRpcEvent(event: Record<string, unknown>, result: SingleRe
|
|
|
85
66
|
// Current RPC emits tool results as message_end; retain tool_result_end below
|
|
86
67
|
// only for older child streams. Both must retire transient nested snapshots.
|
|
87
68
|
if (msg.role === "toolResult") clearNestedProgress(result, msg);
|
|
88
|
-
if (hasMessage(result, msg)) return true;
|
|
89
69
|
// Preserve legacy and extension message shapes, not just current SDK roles.
|
|
90
|
-
result.messages
|
|
70
|
+
if (!appendUniqueMessage(result.messages, msg)) return true;
|
|
91
71
|
|
|
92
72
|
if (msg.role === "assistant") {
|
|
93
73
|
result.usage.turns++;
|
|
@@ -112,7 +92,7 @@ export function processRpcEvent(event: Record<string, unknown>, result: SingleRe
|
|
|
112
92
|
|
|
113
93
|
if (event.type === "tool_result_end" && isTranscriptMessage(event.message)) {
|
|
114
94
|
const msg = event.message;
|
|
115
|
-
|
|
95
|
+
appendUniqueMessage(result.messages, msg);
|
|
116
96
|
clearNestedProgress(result, msg);
|
|
117
97
|
return true;
|
|
118
98
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { StringDecoder } from "node:string_decoder";
|
|
2
|
+
|
|
3
|
+
/** Decode UTF-8 and scan only new fragments for LF, joining once per complete record. */
|
|
4
|
+
export class JsonlFramer {
|
|
5
|
+
private readonly decoder = new StringDecoder("utf8");
|
|
6
|
+
private fragments: string[] = [];
|
|
7
|
+
private ended = false;
|
|
8
|
+
|
|
9
|
+
constructor(private readonly onLine: (line: string) => void) {}
|
|
10
|
+
|
|
11
|
+
write(chunk: Buffer): void {
|
|
12
|
+
if (!this.ended) this.scan(this.decoder.write(chunk));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Flush decoder bytes and the final unterminated record exactly once. */
|
|
16
|
+
end(): void {
|
|
17
|
+
if (this.ended) return;
|
|
18
|
+
this.ended = true;
|
|
19
|
+
this.scan(this.decoder.end());
|
|
20
|
+
if (this.fragments.length === 0) return;
|
|
21
|
+
const line = this.fragments.join("");
|
|
22
|
+
this.fragments = [];
|
|
23
|
+
if (line.trim()) this.emit(line);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private scan(text: string): void {
|
|
27
|
+
let start = 0;
|
|
28
|
+
while (start < text.length) {
|
|
29
|
+
const newline = text.indexOf("\n", start);
|
|
30
|
+
if (newline === -1) {
|
|
31
|
+
this.fragments.push(text.slice(start));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
let line = text.slice(start, newline);
|
|
35
|
+
if (this.fragments.length > 0) {
|
|
36
|
+
this.fragments.push(line);
|
|
37
|
+
line = this.fragments.join("");
|
|
38
|
+
this.fragments = [];
|
|
39
|
+
}
|
|
40
|
+
this.emit(line);
|
|
41
|
+
start = newline + 1;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private emit(line: string): void {
|
|
46
|
+
this.onLine(line.endsWith("\r") ? line.slice(0, -1) : line);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isRecord } from "../types/records.js";
|
|
3
|
+
import type { TranscriptMessage } from "../types/transcript.js";
|
|
4
|
+
|
|
5
|
+
interface MessageIndex {
|
|
6
|
+
length: number;
|
|
7
|
+
buckets: Map<string, TranscriptMessage[]>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Finalized messages are immutable; histories may be resumed, replaced, or appended to.
|
|
11
|
+
// Weak keys keep the index out of durable results and release it with the history.
|
|
12
|
+
const indexes = new WeakMap<TranscriptMessage[], MessageIndex>();
|
|
13
|
+
|
|
14
|
+
function messageDedupKey(value: unknown): string {
|
|
15
|
+
const message = isRecord(value) ? value : {};
|
|
16
|
+
if (typeof message.id === "string") return `id:${message.id}`;
|
|
17
|
+
return JSON.stringify({
|
|
18
|
+
role: message.role,
|
|
19
|
+
provider: message.provider,
|
|
20
|
+
model: message.model,
|
|
21
|
+
stopReason: message.stopReason,
|
|
22
|
+
toolCallId: message.toolCallId,
|
|
23
|
+
toolName: message.toolName,
|
|
24
|
+
content: message.content,
|
|
25
|
+
usage: message.usage,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function fingerprint(key: string): string {
|
|
30
|
+
// Preserve distinct UTF-16 strings, including lone surrogates in legacy ids.
|
|
31
|
+
return createHash("sha256").update(key, "utf16le").digest("hex");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function indexMessage(index: MessageIndex, message: TranscriptMessage, hash: string): void {
|
|
35
|
+
const bucket = index.buckets.get(hash);
|
|
36
|
+
if (bucket) bucket.push(message);
|
|
37
|
+
else index.buckets.set(hash, [message]);
|
|
38
|
+
index.length++;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Append once using the historical equality key, without retaining serialized content. */
|
|
42
|
+
export function appendUniqueMessage(messages: TranscriptMessage[], message: TranscriptMessage): boolean {
|
|
43
|
+
let index = indexes.get(messages);
|
|
44
|
+
if (!index || index.length > messages.length) {
|
|
45
|
+
index = { length: 0, buckets: new Map() };
|
|
46
|
+
indexes.set(messages, index);
|
|
47
|
+
}
|
|
48
|
+
// Seed resumed histories once and catch up with external append-only accumulation.
|
|
49
|
+
while (index.length < messages.length) {
|
|
50
|
+
const existing = messages[index.length];
|
|
51
|
+
indexMessage(index, existing, fingerprint(messageDedupKey(existing)));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const key = messageDedupKey(message);
|
|
55
|
+
const hash = fingerprint(key);
|
|
56
|
+
// Verify matching fingerprints so even a hash collision cannot change equality.
|
|
57
|
+
if (index.buckets.get(hash)?.some((existing) => messageDedupKey(existing) === key)) return false;
|
|
58
|
+
messages.push(message);
|
|
59
|
+
indexMessage(index, message, hash);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
package/runner/parallel.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type { DetailsFactory, OnUpdateCallback, RunningSubagentHandle, RunAgentO
|
|
|
16
16
|
import { configuredNonNegativeInt } from "./constants.js";
|
|
17
17
|
import { sessionDirExists } from "./files.js";
|
|
18
18
|
import { runAgentSubprocess } from "./single.js";
|
|
19
|
+
import { ProgressCoalescer } from "./progress-coalescer.js";
|
|
19
20
|
export async function executeParallelSubprocess(
|
|
20
21
|
tasks: Array<{ agent: string; task: string; intelligence?: string }>,
|
|
21
22
|
agents: AgentConfig[],
|
|
@@ -111,6 +112,9 @@ export async function executeParallelSubprocess(
|
|
|
111
112
|
liveLog: [],
|
|
112
113
|
}),
|
|
113
114
|
intelligence,
|
|
115
|
+
...(extras?.resumeSettings
|
|
116
|
+
? { model: extras.resumeSettings[index].model, thinking: extras.resumeSettings[index].thinking }
|
|
117
|
+
: {}),
|
|
114
118
|
};
|
|
115
119
|
});
|
|
116
120
|
|
|
@@ -129,21 +133,31 @@ export async function executeParallelSubprocess(
|
|
|
129
133
|
});
|
|
130
134
|
};
|
|
131
135
|
|
|
136
|
+
const progress = onUpdate ? new ProgressCoalescer(emitProgress) : undefined;
|
|
132
137
|
let heartbeat: NodeJS.Timeout | undefined;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
heartbeat =
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
138
|
+
const stopProgress = () => {
|
|
139
|
+
if (heartbeat) clearInterval(heartbeat);
|
|
140
|
+
heartbeat = undefined;
|
|
141
|
+
progress?.dispose();
|
|
142
|
+
};
|
|
139
143
|
|
|
140
144
|
let results: SingleResult[];
|
|
141
145
|
try {
|
|
146
|
+
if (progress) {
|
|
147
|
+
progress.request(); // Initial snapshot is immediate, including already-aborted calls.
|
|
148
|
+
if (signal?.aborted) stopProgress();
|
|
149
|
+
else {
|
|
150
|
+
heartbeat = setInterval(() => {
|
|
151
|
+
if (allResults.some((r) => r.exitCode === -1)) progress.request();
|
|
152
|
+
}, PARALLEL_HEARTBEAT_MS);
|
|
153
|
+
signal?.addEventListener("abort", stopProgress, { once: true });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
142
156
|
results = await mapConcurrent(tasks, maxConcurrency, async (t, index) => {
|
|
143
157
|
const previousResult = resumeResults?.[index];
|
|
144
158
|
if (previousResult && isResultSuccess(previousResult)) {
|
|
145
159
|
allResults[index] = previousResult;
|
|
146
|
-
|
|
160
|
+
progress?.request();
|
|
147
161
|
return previousResult;
|
|
148
162
|
}
|
|
149
163
|
const savedSessionDir = previousResult?.sessionDir;
|
|
@@ -181,7 +195,7 @@ export async function executeParallelSubprocess(
|
|
|
181
195
|
onUpdate: (partial) => {
|
|
182
196
|
if (partial.details?.results[0]) {
|
|
183
197
|
allResults[index] = partial.details.results[0];
|
|
184
|
-
|
|
198
|
+
progress?.request();
|
|
185
199
|
}
|
|
186
200
|
},
|
|
187
201
|
makeDetails,
|
|
@@ -190,11 +204,13 @@ export async function executeParallelSubprocess(
|
|
|
190
204
|
onTaskDone?.(index, t);
|
|
191
205
|
}
|
|
192
206
|
allResults[index] = result;
|
|
193
|
-
|
|
207
|
+
progress?.request();
|
|
194
208
|
return result;
|
|
195
209
|
});
|
|
210
|
+
progress?.flush(); // Publish final results before disposing the limiter.
|
|
196
211
|
} finally {
|
|
197
|
-
|
|
212
|
+
signal?.removeEventListener("abort", stopProgress);
|
|
213
|
+
stopProgress();
|
|
198
214
|
}
|
|
199
215
|
|
|
200
216
|
const successCount = results.filter(isResultSuccess).length;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** Batch-wide leading/trailing throttle. The callback reads current state only when emitted. */
|
|
2
|
+
export class ProgressCoalescer {
|
|
3
|
+
private timer: ReturnType<typeof setTimeout> | undefined;
|
|
4
|
+
private lastEmittedAt: number | undefined;
|
|
5
|
+
private pending = false;
|
|
6
|
+
private disposed = false;
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
private readonly emit: () => void,
|
|
10
|
+
private readonly intervalMs = 100,
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
request(): void {
|
|
14
|
+
if (this.disposed) return;
|
|
15
|
+
this.pending = true;
|
|
16
|
+
const remaining =
|
|
17
|
+
this.lastEmittedAt === undefined ? 0 : Math.max(0, this.intervalMs - (Date.now() - this.lastEmittedAt));
|
|
18
|
+
if (remaining === 0) {
|
|
19
|
+
this.flush();
|
|
20
|
+
} else if (this.timer === undefined) {
|
|
21
|
+
this.timer = setTimeout(() => {
|
|
22
|
+
this.timer = undefined;
|
|
23
|
+
this.flush();
|
|
24
|
+
}, remaining);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Completion may bypass the interval to publish the latest pending state. */
|
|
29
|
+
flush(): void {
|
|
30
|
+
if (this.disposed || !this.pending) return;
|
|
31
|
+
if (this.timer !== undefined) clearTimeout(this.timer);
|
|
32
|
+
this.timer = undefined;
|
|
33
|
+
this.pending = false;
|
|
34
|
+
this.lastEmittedAt = Date.now();
|
|
35
|
+
this.emit();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
dispose(): void {
|
|
39
|
+
this.disposed = true;
|
|
40
|
+
if (this.timer !== undefined) clearTimeout(this.timer);
|
|
41
|
+
this.timer = undefined;
|
|
42
|
+
this.pending = false;
|
|
43
|
+
}
|
|
44
|
+
}
|