@narumitw/pi-subagents 0.49.2 → 0.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +313 -53
- package/package.json +11 -8
- package/src/adaptive-scheduler.ts +196 -0
- package/src/admission-benchmark.ts +95 -0
- package/src/admission-policy.ts +78 -0
- package/src/agent-projection.ts +53 -0
- package/src/agents.ts +58 -1
- package/src/auto-transport.ts +114 -0
- package/src/blocking-status.ts +63 -0
- package/src/capabilities.ts +145 -0
- package/src/capability-grant.ts +115 -0
- package/src/capability-router.ts +107 -0
- package/src/completion-delivery.ts +257 -0
- package/src/config-status.ts +221 -0
- package/src/config-ui.ts +215 -236
- package/src/consult-resources.ts +4 -27
- package/src/consult.ts +9 -1
- package/src/create-stateful-transport.ts +55 -0
- package/src/delegation-contract.ts +417 -0
- package/src/execution-plan.ts +322 -0
- package/src/execution-profiles.ts +95 -0
- package/src/execution-ui.ts +320 -0
- package/src/execution.ts +848 -158
- package/src/in-process-transport.ts +269 -25
- package/src/inspect-render.ts +101 -1
- package/src/inspect.ts +296 -3
- package/src/integration-controller.ts +98 -0
- package/src/limits.ts +3 -0
- package/src/orchestration-metrics.ts +78 -0
- package/src/outcome.ts +61 -0
- package/src/panel-child-group.ts +35 -0
- package/src/panel-contract.ts +343 -0
- package/src/panel-evidence.ts +59 -0
- package/src/panel-execution.ts +772 -0
- package/src/panel-failure.ts +56 -0
- package/src/panel-planning.ts +175 -0
- package/src/panel-prompts.ts +132 -0
- package/src/panel-reconciliation.ts +57 -0
- package/src/panel-render.ts +103 -0
- package/src/parallel-limit-ui.ts +112 -0
- package/src/params.ts +172 -3
- package/src/persistence.ts +182 -32
- package/src/prompt-resources.ts +38 -0
- package/src/registry-types.ts +175 -0
- package/src/registry.ts +466 -143
- package/src/render.ts +72 -6
- package/src/result-contract.ts +416 -0
- package/src/retained-semantic-state.ts +100 -0
- package/src/rpc-timeout-finalization.ts +207 -0
- package/src/rpc-transport-metadata.ts +65 -0
- package/src/rpc-transport.ts +990 -0
- package/src/rpc-turn-capture.ts +142 -0
- package/src/runner-result.ts +55 -0
- package/src/runner-usage.ts +48 -0
- package/src/runner.ts +325 -73
- package/src/semantic-snapshot.ts +214 -0
- package/src/settings.ts +254 -35
- package/src/spawn-idempotency.ts +61 -0
- package/src/stateful-config.ts +13 -0
- package/src/stateful-guidance.ts +1 -0
- package/src/stateful-lifecycle.ts +45 -2
- package/src/stateful-limit-ui.ts +246 -0
- package/src/stateful-limits.ts +96 -0
- package/src/stateful-prompt.ts +11 -2
- package/src/stateful-render.ts +48 -3
- package/src/stateful.ts +467 -357
- package/src/subagents.ts +114 -46
- package/src/subprocess-transport.ts +64 -5
- package/src/supervision.ts +103 -0
- package/src/timeout-checkpoint.ts +305 -0
- package/src/timeout-finalization.ts +75 -0
- package/src/transport-types.ts +68 -0
- package/src/transport-ui.ts +169 -0
- package/src/transport.ts +16 -4
- package/src/turn-budget.ts +109 -0
- package/src/verification-policy.ts +17 -0
- package/src/work-item-ledger.ts +682 -0
- package/src/work-item-persistence.ts +218 -0
- package/src/workflow-planning.ts +150 -0
- package/src/workflow-ui.ts +61 -0
- package/src/workspace.ts +69 -12
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { redactPrivateText } from "./context.js";
|
|
2
|
+
import { DEFAULT_MAX_CONTEXT_BYTES, truncateUtf8 } from "./limits.js";
|
|
3
|
+
|
|
4
|
+
export const TIMEOUT_CHECKPOINT_VERSION = "pi-subagents:checkpoint:v1" as const;
|
|
5
|
+
export const TURN_TERMINATION_VERSION = "pi-subagents:termination:v1" as const;
|
|
6
|
+
|
|
7
|
+
const DEFAULT_CHECKPOINT_BYTES = 16 * 1024;
|
|
8
|
+
const MAX_ITEMS = 10;
|
|
9
|
+
const MAX_ITEM_BYTES = 2 * 1024;
|
|
10
|
+
const MAX_PENDING_TOOLS = 50;
|
|
11
|
+
const MUTATING_TOOLS = new Set(["bash", "edit", "write"]);
|
|
12
|
+
const FILE_TOOLS = new Set(["edit", "write"]);
|
|
13
|
+
|
|
14
|
+
export type TurnTerminationReason =
|
|
15
|
+
| "work_timeout"
|
|
16
|
+
| "idle_timeout"
|
|
17
|
+
| "turn_limit"
|
|
18
|
+
| "tool_call_limit"
|
|
19
|
+
| "orchestration_timeout";
|
|
20
|
+
|
|
21
|
+
export interface CompletedToolEvidence {
|
|
22
|
+
toolName: string;
|
|
23
|
+
output: string;
|
|
24
|
+
isError: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface TimeoutCheckpoint {
|
|
28
|
+
version: typeof TIMEOUT_CHECKPOINT_VERSION;
|
|
29
|
+
task: string;
|
|
30
|
+
partialOutput?: string;
|
|
31
|
+
assistantNotes: string[];
|
|
32
|
+
completedTools: CompletedToolEvidence[];
|
|
33
|
+
changedFiles: string[];
|
|
34
|
+
sideEffectsMayHaveOccurred: boolean;
|
|
35
|
+
truncated: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface TurnFinalizationReport {
|
|
39
|
+
attempted: boolean;
|
|
40
|
+
status: "completed" | "failed" | "timed_out" | "skipped";
|
|
41
|
+
durationMs: number;
|
|
42
|
+
error?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface TurnTerminationReport {
|
|
46
|
+
version: typeof TURN_TERMINATION_VERSION;
|
|
47
|
+
reason: TurnTerminationReason;
|
|
48
|
+
limit: number;
|
|
49
|
+
checkpoint: TimeoutCheckpoint;
|
|
50
|
+
finalization: TurnFinalizationReport;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface TimeoutProgressJournalOptions {
|
|
54
|
+
maxBytes?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface PendingToolCall {
|
|
58
|
+
name: string;
|
|
59
|
+
path?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class TimeoutProgressJournal {
|
|
63
|
+
private readonly assistantNotes: string[] = [];
|
|
64
|
+
private readonly completedTools: CompletedToolEvidence[] = [];
|
|
65
|
+
private readonly changedFiles = new Set<string>();
|
|
66
|
+
private readonly pendingTools = new Map<string, PendingToolCall>();
|
|
67
|
+
private readonly maxBytes: number;
|
|
68
|
+
private sideEffectsMayHaveOccurred = false;
|
|
69
|
+
private truncated = false;
|
|
70
|
+
|
|
71
|
+
constructor(options: TimeoutProgressJournalOptions = {}) {
|
|
72
|
+
const requested = options.maxBytes ?? DEFAULT_CHECKPOINT_BYTES;
|
|
73
|
+
if (
|
|
74
|
+
!Number.isSafeInteger(requested) ||
|
|
75
|
+
requested < 512 ||
|
|
76
|
+
requested > DEFAULT_MAX_CONTEXT_BYTES
|
|
77
|
+
) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
`Timeout checkpoint limit must be an integer between 512 and ${DEFAULT_MAX_CONTEXT_BYTES}`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
this.maxBytes = requested;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
recordAssistantText(text: string): void {
|
|
86
|
+
const bounded = boundedPrivate(text, MAX_ITEM_BYTES);
|
|
87
|
+
if (!bounded) return;
|
|
88
|
+
this.assistantNotes.push(bounded);
|
|
89
|
+
this.trimItems(this.assistantNotes);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
recordToolCall(id: string, name: string, args: Record<string, unknown> = {}): void {
|
|
93
|
+
const toolName = boundedPrivate(name, 256);
|
|
94
|
+
if (!toolName) return;
|
|
95
|
+
const path = toolPath(args);
|
|
96
|
+
this.pendingTools.set(id, { name: toolName, path });
|
|
97
|
+
while (this.pendingTools.size > MAX_PENDING_TOOLS) {
|
|
98
|
+
const oldest = this.pendingTools.keys().next().value;
|
|
99
|
+
if (typeof oldest !== "string") break;
|
|
100
|
+
this.pendingTools.delete(oldest);
|
|
101
|
+
this.truncated = true;
|
|
102
|
+
}
|
|
103
|
+
if (MUTATING_TOOLS.has(toolName)) this.sideEffectsMayHaveOccurred = true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
recordToolResult(
|
|
107
|
+
id: string,
|
|
108
|
+
name: string,
|
|
109
|
+
result: { content?: unknown; isError?: unknown },
|
|
110
|
+
): void {
|
|
111
|
+
const pending = this.pendingTools.get(id);
|
|
112
|
+
this.pendingTools.delete(id);
|
|
113
|
+
const toolName = boundedPrivate(pending?.name || name, 256);
|
|
114
|
+
if (!toolName) return;
|
|
115
|
+
if (pending?.path && FILE_TOOLS.has(toolName)) this.changedFiles.add(pending.path);
|
|
116
|
+
const output = boundedPrivate(textContent(result.content), MAX_ITEM_BYTES);
|
|
117
|
+
this.completedTools.push({
|
|
118
|
+
toolName,
|
|
119
|
+
output: output || "(no text output)",
|
|
120
|
+
isError: result.isError === true,
|
|
121
|
+
});
|
|
122
|
+
this.trimItems(this.completedTools);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
checkpoint(task: string, partialOutput?: string): TimeoutCheckpoint {
|
|
126
|
+
const checkpoint: TimeoutCheckpoint = {
|
|
127
|
+
version: TIMEOUT_CHECKPOINT_VERSION,
|
|
128
|
+
task: boundedPrivate(task, 4 * 1024),
|
|
129
|
+
partialOutput: partialOutput ? boundedPrivate(partialOutput, 4 * 1024) : undefined,
|
|
130
|
+
assistantNotes: [...this.assistantNotes],
|
|
131
|
+
completedTools: this.completedTools.map((item) => ({ ...item })),
|
|
132
|
+
changedFiles: [...this.changedFiles].slice(-MAX_ITEMS),
|
|
133
|
+
sideEffectsMayHaveOccurred: this.sideEffectsMayHaveOccurred,
|
|
134
|
+
truncated: this.truncated || this.changedFiles.size > MAX_ITEMS,
|
|
135
|
+
};
|
|
136
|
+
return fitCheckpoint(checkpoint, this.maxBytes);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private trimItems(items: unknown[]): void {
|
|
140
|
+
if (items.length <= MAX_ITEMS) return;
|
|
141
|
+
items.splice(0, items.length - MAX_ITEMS);
|
|
142
|
+
this.truncated = true;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function formatTurnTerminationMessage(
|
|
147
|
+
reason: TurnTerminationReason,
|
|
148
|
+
limit: number,
|
|
149
|
+
prefix = "Subagent",
|
|
150
|
+
): string {
|
|
151
|
+
switch (reason) {
|
|
152
|
+
case "work_timeout":
|
|
153
|
+
return `${prefix} timed out after ${limit}ms`;
|
|
154
|
+
case "orchestration_timeout":
|
|
155
|
+
return `${prefix} orchestration deadline expired after ${limit}ms`;
|
|
156
|
+
case "idle_timeout":
|
|
157
|
+
return `${prefix} made no completed progress for ${limit}ms`;
|
|
158
|
+
case "turn_limit":
|
|
159
|
+
return `${prefix} reached the ${limit}-turn limit before producing a final answer`;
|
|
160
|
+
case "tool_call_limit":
|
|
161
|
+
return `${prefix} exceeded the ${limit}-tool-call limit`;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function copyTurnTerminationReport(report: TurnTerminationReport): TurnTerminationReport {
|
|
166
|
+
return {
|
|
167
|
+
...report,
|
|
168
|
+
checkpoint: {
|
|
169
|
+
...report.checkpoint,
|
|
170
|
+
assistantNotes: [...report.checkpoint.assistantNotes],
|
|
171
|
+
completedTools: report.checkpoint.completedTools.map((item) => ({ ...item })),
|
|
172
|
+
changedFiles: [...report.checkpoint.changedFiles],
|
|
173
|
+
},
|
|
174
|
+
finalization: { ...report.finalization },
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function formatTimeoutCheckpoint(checkpoint: TimeoutCheckpoint): string {
|
|
179
|
+
const sections = [
|
|
180
|
+
checkpoint.partialOutput ? `Partial output:\n${checkpoint.partialOutput}` : undefined,
|
|
181
|
+
checkpoint.assistantNotes.length > 0
|
|
182
|
+
? `Assistant checkpoints:\n${checkpoint.assistantNotes.map((note) => `- ${note}`).join("\n")}`
|
|
183
|
+
: undefined,
|
|
184
|
+
checkpoint.completedTools.length > 0
|
|
185
|
+
? `Completed tool evidence:\n${checkpoint.completedTools
|
|
186
|
+
.map((item) => `- ${item.toolName}${item.isError ? " (error)" : ""}: ${item.output}`)
|
|
187
|
+
.join("\n")}`
|
|
188
|
+
: undefined,
|
|
189
|
+
checkpoint.changedFiles.length > 0
|
|
190
|
+
? `Changed files: ${checkpoint.changedFiles.join(", ")}`
|
|
191
|
+
: undefined,
|
|
192
|
+
checkpoint.sideEffectsMayHaveOccurred
|
|
193
|
+
? "Side effects may have occurred before termination."
|
|
194
|
+
: undefined,
|
|
195
|
+
checkpoint.truncated ? "Checkpoint evidence was truncated." : undefined,
|
|
196
|
+
].filter((value): value is string => Boolean(value));
|
|
197
|
+
return truncateUtf8(
|
|
198
|
+
sections.join("\n\n") || "No completed evidence was captured.",
|
|
199
|
+
DEFAULT_MAX_CONTEXT_BYTES,
|
|
200
|
+
).text;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function journalMessages(
|
|
204
|
+
journal: TimeoutProgressJournal,
|
|
205
|
+
messages: readonly unknown[],
|
|
206
|
+
): void {
|
|
207
|
+
for (const message of messages) {
|
|
208
|
+
if (!message || typeof message !== "object" || Array.isArray(message)) continue;
|
|
209
|
+
const value = message as Record<string, unknown>;
|
|
210
|
+
if (value.role === "assistant" && Array.isArray(value.content)) {
|
|
211
|
+
for (const part of value.content) {
|
|
212
|
+
if (!part || typeof part !== "object" || Array.isArray(part)) continue;
|
|
213
|
+
const item = part as Record<string, unknown>;
|
|
214
|
+
if (item.type === "text" && typeof item.text === "string") {
|
|
215
|
+
journal.recordAssistantText(item.text);
|
|
216
|
+
} else if (
|
|
217
|
+
item.type === "toolCall" &&
|
|
218
|
+
typeof item.id === "string" &&
|
|
219
|
+
typeof item.name === "string"
|
|
220
|
+
) {
|
|
221
|
+
journal.recordToolCall(
|
|
222
|
+
item.id,
|
|
223
|
+
item.name,
|
|
224
|
+
isRecord(item.arguments) ? item.arguments : {},
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} else if (value.role === "toolResult") {
|
|
229
|
+
journal.recordToolResult(
|
|
230
|
+
typeof value.toolCallId === "string" ? value.toolCallId : "",
|
|
231
|
+
typeof value.toolName === "string" ? value.toolName : "tool",
|
|
232
|
+
{ content: value.content, isError: value.isError },
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function fitCheckpoint(checkpoint: TimeoutCheckpoint, maxBytes: number): TimeoutCheckpoint {
|
|
239
|
+
const copy: TimeoutCheckpoint = {
|
|
240
|
+
...checkpoint,
|
|
241
|
+
assistantNotes: [...checkpoint.assistantNotes],
|
|
242
|
+
completedTools: checkpoint.completedTools.map((item) => ({ ...item })),
|
|
243
|
+
changedFiles: [...checkpoint.changedFiles],
|
|
244
|
+
};
|
|
245
|
+
while (serializedBytes(copy) > maxBytes) {
|
|
246
|
+
copy.truncated = true;
|
|
247
|
+
if (copy.assistantNotes.length > 0) copy.assistantNotes.shift();
|
|
248
|
+
else if (copy.completedTools.length > 0) copy.completedTools.shift();
|
|
249
|
+
else if (copy.changedFiles.length > 0) copy.changedFiles.shift();
|
|
250
|
+
else if (copy.partialOutput) {
|
|
251
|
+
copy.partialOutput =
|
|
252
|
+
shrinkCheckpointText(copy.partialOutput, Math.floor(maxBytes / 4), 128) || undefined;
|
|
253
|
+
} else if (copy.task) {
|
|
254
|
+
copy.task = shrinkCheckpointText(copy.task, Math.floor(maxBytes / 8), 64);
|
|
255
|
+
} else break;
|
|
256
|
+
}
|
|
257
|
+
return copy;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function shrinkCheckpointText(
|
|
261
|
+
value: string,
|
|
262
|
+
preferredMaxBytes: number,
|
|
263
|
+
minimumBytes: number,
|
|
264
|
+
): string {
|
|
265
|
+
const currentBytes = Buffer.byteLength(value, "utf8");
|
|
266
|
+
if (currentBytes <= minimumBytes) return "";
|
|
267
|
+
const nextMaxBytes = Math.min(
|
|
268
|
+
currentBytes - 1,
|
|
269
|
+
preferredMaxBytes,
|
|
270
|
+
Math.max(minimumBytes, Math.floor(currentBytes / 2)),
|
|
271
|
+
);
|
|
272
|
+
return truncateUtf8(value, nextMaxBytes).text;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function serializedBytes(value: unknown): number {
|
|
276
|
+
return Buffer.byteLength(JSON.stringify(value), "utf8");
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function boundedPrivate(value: string, maxBytes: number): string {
|
|
280
|
+
return truncateUtf8(redactPrivateText(value).trim(), maxBytes).text;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function textContent(value: unknown): string {
|
|
284
|
+
if (typeof value === "string") return value;
|
|
285
|
+
if (!Array.isArray(value)) return "";
|
|
286
|
+
return value
|
|
287
|
+
.flatMap((part) => {
|
|
288
|
+
if (!part || typeof part !== "object" || Array.isArray(part)) return [];
|
|
289
|
+
const item = part as Record<string, unknown>;
|
|
290
|
+
return item.type === "text" && typeof item.text === "string" ? [item.text] : [];
|
|
291
|
+
})
|
|
292
|
+
.join("\n");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function toolPath(args: Record<string, unknown>): string | undefined {
|
|
296
|
+
for (const key of ["path", "file_path"]) {
|
|
297
|
+
const value = args[key];
|
|
298
|
+
if (typeof value === "string" && value.trim()) return boundedPrivate(value, 1024);
|
|
299
|
+
}
|
|
300
|
+
return undefined;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
304
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
305
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { redactPrivateText } from "./context.js";
|
|
2
|
+
import { DEFAULT_MAX_CONTEXT_BYTES, truncateUtf8 } from "./limits.js";
|
|
3
|
+
import { appendResultInstruction, type SubagentResultFormat } from "./result-contract.js";
|
|
4
|
+
import type { RecentActivityItem } from "./runner.js";
|
|
5
|
+
import {
|
|
6
|
+
formatTimeoutCheckpoint,
|
|
7
|
+
type TimeoutCheckpoint,
|
|
8
|
+
type TurnTerminationReason,
|
|
9
|
+
} from "./timeout-checkpoint.js";
|
|
10
|
+
|
|
11
|
+
export const DEFAULT_TIMEOUT_FINALIZATION_MS = 45_000;
|
|
12
|
+
|
|
13
|
+
export interface TimeoutFinalizationEvidence {
|
|
14
|
+
task: string;
|
|
15
|
+
partialOutput?: string;
|
|
16
|
+
recentActivity?: readonly RecentActivityItem[];
|
|
17
|
+
checkpoint?: TimeoutCheckpoint;
|
|
18
|
+
terminationReason?: TurnTerminationReason;
|
|
19
|
+
resultFormat?: SubagentResultFormat;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function resolveTimeoutFinalizationMs(workTimeoutMs: number, override?: number): number {
|
|
23
|
+
const requested = override ?? Math.min(workTimeoutMs, DEFAULT_TIMEOUT_FINALIZATION_MS);
|
|
24
|
+
if (!Number.isFinite(requested) || requested < 1) {
|
|
25
|
+
throw new Error("Timeout finalization deadline must be a positive finite number");
|
|
26
|
+
}
|
|
27
|
+
return Math.min(Math.floor(requested), DEFAULT_TIMEOUT_FINALIZATION_MS);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function buildTimeoutFinalizationPrompt(
|
|
31
|
+
evidence: TimeoutFinalizationEvidence,
|
|
32
|
+
maxBytes = DEFAULT_MAX_CONTEXT_BYTES,
|
|
33
|
+
): string {
|
|
34
|
+
const activity = (evidence.recentActivity ?? [])
|
|
35
|
+
.slice(-10)
|
|
36
|
+
.map((item) =>
|
|
37
|
+
item.type === "text"
|
|
38
|
+
? `- Assistant note: ${redactPrivateText(item.text)}`
|
|
39
|
+
: `- Tool activity: ${redactPrivateText(item.name)}`,
|
|
40
|
+
)
|
|
41
|
+
.join("\n");
|
|
42
|
+
const prompt = [
|
|
43
|
+
terminationHeading(evidence.terminationReason),
|
|
44
|
+
"Do not continue investigating, call tools, modify files, or retry the original task.",
|
|
45
|
+
"Return a concise summary of only verified findings and completed evidence already available.",
|
|
46
|
+
"Explicitly label unfinished or unverified areas.",
|
|
47
|
+
`Original task:\n${redactPrivateText(evidence.task)}`,
|
|
48
|
+
evidence.partialOutput
|
|
49
|
+
? `Partial assistant output:\n${redactPrivateText(evidence.partialOutput)}`
|
|
50
|
+
: "Partial assistant output: (none)",
|
|
51
|
+
evidence.checkpoint
|
|
52
|
+
? `Deterministic progress checkpoint:\n${formatTimeoutCheckpoint(evidence.checkpoint)}`
|
|
53
|
+
: undefined,
|
|
54
|
+
activity ? `Recent bounded activity:\n${activity}` : "Recent bounded activity: (none)",
|
|
55
|
+
]
|
|
56
|
+
.filter((value): value is string => Boolean(value))
|
|
57
|
+
.join("\n\n");
|
|
58
|
+
return truncateUtf8(appendResultInstruction(prompt, evidence.resultFormat, maxBytes), maxBytes)
|
|
59
|
+
.text;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function terminationHeading(reason: TurnTerminationReason | undefined): string {
|
|
63
|
+
switch (reason) {
|
|
64
|
+
case "idle_timeout":
|
|
65
|
+
return "The idle deadline expired and the active work was aborted.";
|
|
66
|
+
case "turn_limit":
|
|
67
|
+
return "The assistant-turn budget was exhausted and the active work was aborted.";
|
|
68
|
+
case "tool_call_limit":
|
|
69
|
+
return "The tool-call budget was exhausted and the active work was aborted.";
|
|
70
|
+
case "orchestration_timeout":
|
|
71
|
+
return "The orchestration deadline expired and the active work was aborted.";
|
|
72
|
+
default:
|
|
73
|
+
return "Work deadline expired and the active work was aborted.";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { SubagentThinkingLevel } from "./agents.js";
|
|
2
|
+
|
|
3
|
+
export const PI_SUBAGENTS_RPC_PROTOCOL = "pi-subagents:v1" as const;
|
|
4
|
+
|
|
5
|
+
export type EffectiveSubagentTransportKind = "subprocess" | "in-process" | "rpc";
|
|
6
|
+
|
|
7
|
+
export type TransportProgressPhase =
|
|
8
|
+
| "queued"
|
|
9
|
+
| "starting"
|
|
10
|
+
| "ready"
|
|
11
|
+
| "accepted"
|
|
12
|
+
| "running"
|
|
13
|
+
| "finalizing"
|
|
14
|
+
| "retrying"
|
|
15
|
+
| "compacting"
|
|
16
|
+
| "settled"
|
|
17
|
+
| "failed"
|
|
18
|
+
| "interrupted";
|
|
19
|
+
|
|
20
|
+
export interface TransportUsage {
|
|
21
|
+
input: number;
|
|
22
|
+
output: number;
|
|
23
|
+
cacheRead: number;
|
|
24
|
+
cacheWrite: number;
|
|
25
|
+
totalTokens: number;
|
|
26
|
+
cost: number;
|
|
27
|
+
turns: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface TransportTiming {
|
|
31
|
+
queuedAt?: number;
|
|
32
|
+
startedAt?: number;
|
|
33
|
+
transportStartedAt?: number;
|
|
34
|
+
readyAt?: number;
|
|
35
|
+
promptAcceptedAt?: number;
|
|
36
|
+
firstActivityAt?: number;
|
|
37
|
+
settledAt?: number;
|
|
38
|
+
completionDeliveredAt?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface TransportTelemetry {
|
|
42
|
+
transport?: EffectiveSubagentTransportKind;
|
|
43
|
+
selectionReason?: string;
|
|
44
|
+
protocol?: typeof PI_SUBAGENTS_RPC_PROTOCOL;
|
|
45
|
+
phase: TransportProgressPhase;
|
|
46
|
+
queuePosition?: number;
|
|
47
|
+
updatedAt: number;
|
|
48
|
+
timing: TransportTiming;
|
|
49
|
+
provider?: string;
|
|
50
|
+
model?: string;
|
|
51
|
+
thinkingLevel?: SubagentThinkingLevel;
|
|
52
|
+
usage?: TransportUsage;
|
|
53
|
+
failurePhase?: TransportProgressPhase;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type TransportProgressCallback = (progress: TransportTelemetry) => void;
|
|
57
|
+
|
|
58
|
+
export function emptyTransportUsage(): TransportUsage {
|
|
59
|
+
return {
|
|
60
|
+
input: 0,
|
|
61
|
+
output: 0,
|
|
62
|
+
cacheRead: 0,
|
|
63
|
+
cacheWrite: 0,
|
|
64
|
+
totalTokens: 0,
|
|
65
|
+
cost: 0,
|
|
66
|
+
turns: 0,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { SubagentTransportKind } from "./agents.js";
|
|
3
|
+
import { safeTerminalLine as safeTerminalText } from "./safe-text.js";
|
|
4
|
+
import { inspectStatefulTransportSettings, updateStatefulTransportSetting } from "./settings.js";
|
|
5
|
+
import type { StatefulSubagentRuntimeStatus } from "./stateful.js";
|
|
6
|
+
|
|
7
|
+
const TRANSPORT_OPTIONS: Array<{
|
|
8
|
+
value: SubagentTransportKind;
|
|
9
|
+
label: string;
|
|
10
|
+
description: string;
|
|
11
|
+
}> = [
|
|
12
|
+
{
|
|
13
|
+
value: "subprocess",
|
|
14
|
+
label: "Fresh subprocess",
|
|
15
|
+
description: "Compatibility path with a fresh isolated Pi process for every turn.",
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
value: "in-process",
|
|
19
|
+
label: "In process",
|
|
20
|
+
description:
|
|
21
|
+
"Lowest follow-up overhead for built-in tools, with shared memory and crash boundary.",
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
value: "rpc",
|
|
25
|
+
label: "Persistent RPC process",
|
|
26
|
+
description: "Retain native history in one isolated Pi process per active retained agent.",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
value: "auto",
|
|
30
|
+
label: "Automatic",
|
|
31
|
+
description:
|
|
32
|
+
"Read-only built-ins use in-process, write-capable built-ins use RPC, and custom tools use subprocess.",
|
|
33
|
+
},
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
export interface TransportUiRuntime {
|
|
37
|
+
getRuntimeStatus(): StatefulSubagentRuntimeStatus;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function transportSettingsScreen(runtime: TransportUiRuntime) {
|
|
41
|
+
const configured = inspectStatefulTransportSettings();
|
|
42
|
+
const current = runtime.getRuntimeStatus();
|
|
43
|
+
return {
|
|
44
|
+
kind: "actions" as const,
|
|
45
|
+
title: configured.error ? "Detached Transport · Read only" : "Detached Transport",
|
|
46
|
+
lines: [
|
|
47
|
+
`Current session: ${transportLabel(current.transport)}`,
|
|
48
|
+
`Configured after reload: ${transportLabel(configured.value)} (${configured.source})`,
|
|
49
|
+
"Transport isolation is not a filesystem or network sandbox.",
|
|
50
|
+
"RPC v1 disables child extensions and supports built-in Pi tools only.",
|
|
51
|
+
...(configured.error
|
|
52
|
+
? [
|
|
53
|
+
`Settings cannot be edited: ${safeTerminalText(configured.error)}`,
|
|
54
|
+
`Repair ${safeTerminalText(configured.path)} and retry.`,
|
|
55
|
+
]
|
|
56
|
+
: []),
|
|
57
|
+
],
|
|
58
|
+
items: [
|
|
59
|
+
...(configured.error
|
|
60
|
+
? []
|
|
61
|
+
: TRANSPORT_OPTIONS.map((option) => ({
|
|
62
|
+
id: option.value,
|
|
63
|
+
label: option.label,
|
|
64
|
+
description: option.description,
|
|
65
|
+
action: "set-transport" as const,
|
|
66
|
+
}))),
|
|
67
|
+
{ id: "back", label: "Back", action: "back" as const },
|
|
68
|
+
],
|
|
69
|
+
hint: "back" as const,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function applyTransportSetting(
|
|
74
|
+
value: string,
|
|
75
|
+
ctx: ExtensionCommandContext,
|
|
76
|
+
runtime: TransportUiRuntime,
|
|
77
|
+
signal: AbortSignal,
|
|
78
|
+
isCurrent: () => boolean,
|
|
79
|
+
) {
|
|
80
|
+
if (!isTransport(value)) return { kind: "rejected" as const };
|
|
81
|
+
const before = inspectStatefulTransportSettings();
|
|
82
|
+
if (before.error) return { kind: "rejected" as const };
|
|
83
|
+
if (value === before.value) return { kind: "stay" as const };
|
|
84
|
+
const status = runtime.getRuntimeStatus();
|
|
85
|
+
if (status.retainedAgents > 0) {
|
|
86
|
+
ctx.ui.notify(
|
|
87
|
+
`Cannot change transport while ${status.retainedAgents} detached agent${status.retainedAgents === 1 ? " is" : "s are"} retained. Clear Current agents first.`,
|
|
88
|
+
"warning",
|
|
89
|
+
);
|
|
90
|
+
return { kind: "rejected" as const };
|
|
91
|
+
}
|
|
92
|
+
const option = TRANSPORT_OPTIONS.find((candidate) => candidate.value === value);
|
|
93
|
+
const confirmed = await ctx.ui.confirm(
|
|
94
|
+
`Use ${transportLabel(value)} after reload?`,
|
|
95
|
+
`${option?.description ?? ""}\n\nThis saves the setting but does not reload Pi automatically.`,
|
|
96
|
+
{ signal },
|
|
97
|
+
);
|
|
98
|
+
if (signal.aborted || !isCurrent()) return { kind: "close" as const };
|
|
99
|
+
if (!confirmed) return { kind: "rejected" as const };
|
|
100
|
+
const after = inspectStatefulTransportSettings();
|
|
101
|
+
if (after.error || after.value !== before.value || after.source !== before.source) {
|
|
102
|
+
ctx.ui.notify("Transport settings changed while confirming; review again.", "warning");
|
|
103
|
+
return { kind: "rejected" as const };
|
|
104
|
+
}
|
|
105
|
+
if (runtime.getRuntimeStatus().retainedAgents > 0) {
|
|
106
|
+
ctx.ui.notify(
|
|
107
|
+
"Detached agents appeared while confirming; clear them before changing transport.",
|
|
108
|
+
"warning",
|
|
109
|
+
);
|
|
110
|
+
return { kind: "rejected" as const };
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
updateStatefulTransportSetting(value);
|
|
114
|
+
ctx.ui.notify(`Saved ${transportLabel(value)}. Run /reload when ready.`, "info");
|
|
115
|
+
return { kind: "stay" as const };
|
|
116
|
+
} catch (error) {
|
|
117
|
+
ctx.ui.notify(
|
|
118
|
+
`Transport was not saved; the previous setting remains: ${safeTerminalText(error instanceof Error ? error.message : String(error))}`,
|
|
119
|
+
"error",
|
|
120
|
+
);
|
|
121
|
+
return { kind: "rejected" as const };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function responsivenessSetupScreen(runtime: TransportUiRuntime) {
|
|
126
|
+
const current = runtime.getRuntimeStatus();
|
|
127
|
+
const configured = inspectStatefulTransportSettings();
|
|
128
|
+
return {
|
|
129
|
+
kind: "actions" as const,
|
|
130
|
+
title: "Responsiveness Setup",
|
|
131
|
+
lines: [
|
|
132
|
+
`Current transport: ${transportLabel(current.transport)}`,
|
|
133
|
+
`Configured transport: ${transportLabel(configured.value)}`,
|
|
134
|
+
"Transport, completion delivery, and thinking defaults are separate explicit choices.",
|
|
135
|
+
"Use Automatic to reduce retained follow-up startup while keeping custom-tool compatibility.",
|
|
136
|
+
],
|
|
137
|
+
items: [
|
|
138
|
+
{
|
|
139
|
+
id: "auto",
|
|
140
|
+
label: "Preview Automatic transport",
|
|
141
|
+
description: "Choose a deterministic transport before each retained agent starts",
|
|
142
|
+
action: "set-transport" as const,
|
|
143
|
+
},
|
|
144
|
+
{ id: "transport", label: "Compare all transports", to: "transport" as const },
|
|
145
|
+
{
|
|
146
|
+
id: "completion",
|
|
147
|
+
label: "Completion delivery",
|
|
148
|
+
description: "Choose separately whether an idle root resumes for synthesis",
|
|
149
|
+
to: "settings" as const,
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
id: "thinking",
|
|
153
|
+
label: "Thinking profiles",
|
|
154
|
+
description: "Preview explicit Fast, Balanced, or Deep per-agent defaults",
|
|
155
|
+
to: "execution-profiles" as const,
|
|
156
|
+
},
|
|
157
|
+
{ id: "back", label: "Back", action: "back" as const },
|
|
158
|
+
],
|
|
159
|
+
hint: "back" as const,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function transportLabel(value: SubagentTransportKind): string {
|
|
164
|
+
return TRANSPORT_OPTIONS.find((option) => option.value === value)?.label ?? value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function isTransport(value: string): value is SubagentTransportKind {
|
|
168
|
+
return ["subprocess", "in-process", "rpc", "auto"].includes(value);
|
|
169
|
+
}
|
package/src/transport.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { ManagedAgent, TurnOutcome } from "./registry.js";
|
|
2
|
+
import type { TransportProgressCallback } from "./transport-types.js";
|
|
2
3
|
|
|
3
4
|
export interface SubagentTransport {
|
|
4
|
-
readonly kind: "subprocess" | "in-process" | "fake";
|
|
5
|
-
runTurn(
|
|
5
|
+
readonly kind: "subprocess" | "in-process" | "rpc" | "auto" | "fake";
|
|
6
|
+
runTurn(
|
|
7
|
+
agent: ManagedAgent,
|
|
8
|
+
task: string,
|
|
9
|
+
signal: AbortSignal,
|
|
10
|
+
onProgress?: TransportProgressCallback,
|
|
11
|
+
): Promise<TurnOutcome>;
|
|
6
12
|
release?(agent: ManagedAgent): Promise<void>;
|
|
7
13
|
shutdown?(): Promise<void>;
|
|
8
14
|
}
|
|
@@ -11,6 +17,7 @@ export type AgentTurnRunner = (
|
|
|
11
17
|
agent: ManagedAgent,
|
|
12
18
|
task: string,
|
|
13
19
|
signal: AbortSignal,
|
|
20
|
+
onProgress?: TransportProgressCallback,
|
|
14
21
|
) => Promise<TurnOutcome>;
|
|
15
22
|
|
|
16
23
|
export class FunctionTransport implements SubagentTransport {
|
|
@@ -18,8 +25,13 @@ export class FunctionTransport implements SubagentTransport {
|
|
|
18
25
|
|
|
19
26
|
constructor(private readonly runner: AgentTurnRunner) {}
|
|
20
27
|
|
|
21
|
-
runTurn(
|
|
22
|
-
|
|
28
|
+
runTurn(
|
|
29
|
+
agent: ManagedAgent,
|
|
30
|
+
task: string,
|
|
31
|
+
signal: AbortSignal,
|
|
32
|
+
onProgress?: TransportProgressCallback,
|
|
33
|
+
): Promise<TurnOutcome> {
|
|
34
|
+
return this.runner(agent, task, signal, onProgress);
|
|
23
35
|
}
|
|
24
36
|
}
|
|
25
37
|
|