@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
|
@@ -1,6 +1,24 @@
|
|
|
1
|
-
import type { AgentRegistry } from "./registry.js";
|
|
1
|
+
import type { AgentRegistry, ManagedAgent } from "./registry.js";
|
|
2
2
|
import type { WorkspaceManager } from "./workspace.js";
|
|
3
3
|
|
|
4
|
+
export async function cleanupPersistedWorkspaces(
|
|
5
|
+
agents: readonly ManagedAgent[],
|
|
6
|
+
workspaceManager: WorkspaceManager,
|
|
7
|
+
): Promise<number> {
|
|
8
|
+
const cleanup = (
|
|
9
|
+
workspaceManager as WorkspaceManager & {
|
|
10
|
+
cleanupPersisted?: (ownerId: string, cwd: string) => Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
).cleanupPersisted?.bind(workspaceManager);
|
|
13
|
+
if (!cleanup) return 0;
|
|
14
|
+
const results = await Promise.allSettled(
|
|
15
|
+
agents
|
|
16
|
+
.filter((agent) => agent.workspaceMode === "worktree")
|
|
17
|
+
.map((agent) => cleanup(agent.id, agent.cwd)),
|
|
18
|
+
);
|
|
19
|
+
return results.filter((result) => result.status === "rejected").length;
|
|
20
|
+
}
|
|
21
|
+
|
|
4
22
|
export async function disposeStatefulRuntime(
|
|
5
23
|
registry: AgentRegistry | undefined,
|
|
6
24
|
workspaceManager: WorkspaceManager,
|
|
@@ -19,13 +37,38 @@ export async function disposeStatefulRuntime(
|
|
|
19
37
|
return errors;
|
|
20
38
|
}
|
|
21
39
|
|
|
40
|
+
export async function waitForOwnedSpawn<T>(
|
|
41
|
+
promise: Promise<T>,
|
|
42
|
+
signal: AbortSignal | undefined,
|
|
43
|
+
): Promise<T> {
|
|
44
|
+
if (!signal) return promise;
|
|
45
|
+
if (signal.aborted) throw ownedSpawnAbortError();
|
|
46
|
+
let abortHandler: (() => void) | undefined;
|
|
47
|
+
try {
|
|
48
|
+
return await Promise.race([
|
|
49
|
+
promise,
|
|
50
|
+
new Promise<T>((_resolve, reject) => {
|
|
51
|
+
abortHandler = () => reject(ownedSpawnAbortError());
|
|
52
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
53
|
+
if (signal.aborted) abortHandler();
|
|
54
|
+
}),
|
|
55
|
+
]);
|
|
56
|
+
} finally {
|
|
57
|
+
if (abortHandler) signal.removeEventListener("abort", abortHandler);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
22
61
|
export function assertCurrentSpawn(
|
|
23
62
|
signal: AbortSignal | undefined,
|
|
24
63
|
generation: number,
|
|
25
64
|
currentGeneration: number,
|
|
26
65
|
): void {
|
|
27
66
|
if (!signal?.aborted && generation === currentGeneration) return;
|
|
67
|
+
throw ownedSpawnAbortError();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function ownedSpawnAbortError(): Error {
|
|
28
71
|
const error = new Error("Subagent spawn owner was replaced or aborted");
|
|
29
72
|
error.name = "AbortError";
|
|
30
|
-
|
|
73
|
+
return error;
|
|
31
74
|
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { projectAgentRecords } from "./agent-projection.js";
|
|
3
|
+
import type { ManagedAgent } from "./registry.js";
|
|
4
|
+
import { safeTerminalLine as safeTerminalText } from "./safe-text.js";
|
|
5
|
+
import { inspectStatefulLimitSettings, updateStatefulLimitSetting } from "./settings.js";
|
|
6
|
+
import type { StatefulSubagentRuntimeStatus } from "./stateful.js";
|
|
7
|
+
import {
|
|
8
|
+
isValidStatefulLimit,
|
|
9
|
+
resolveStatefulLimits,
|
|
10
|
+
STATEFUL_LIMIT_DEFINITIONS,
|
|
11
|
+
type StatefulLimitField,
|
|
12
|
+
type StatefulLimits,
|
|
13
|
+
statefulLimitDefinition,
|
|
14
|
+
} from "./stateful-limits.js";
|
|
15
|
+
|
|
16
|
+
export interface StatefulLimitRuntime {
|
|
17
|
+
getRuntimeStatus(): StatefulSubagentRuntimeStatus;
|
|
18
|
+
listAgents(includeClosed?: boolean): ManagedAgent[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface StatefulLimitApplyOptions {
|
|
22
|
+
signal: AbortSignal;
|
|
23
|
+
isCurrent(): boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function statefulLimitListScreen(runtime: StatefulLimitRuntime) {
|
|
27
|
+
const inspected = inspectStatefulLimitSettings();
|
|
28
|
+
const current = runtime.getRuntimeStatus().limits;
|
|
29
|
+
return {
|
|
30
|
+
kind: "actions" as const,
|
|
31
|
+
title: "Detached Agent Limits",
|
|
32
|
+
lines: [
|
|
33
|
+
"These limits apply after /reload or the next Pi session.",
|
|
34
|
+
"Reloading can interrupt retained detached work.",
|
|
35
|
+
...(inspected.error
|
|
36
|
+
? [
|
|
37
|
+
`Settings cannot be edited: ${safeTerminalText(inspected.error)}`,
|
|
38
|
+
`Repair ${safeTerminalText(inspected.path)} and retry.`,
|
|
39
|
+
]
|
|
40
|
+
: []),
|
|
41
|
+
],
|
|
42
|
+
items: [
|
|
43
|
+
...(inspected.values
|
|
44
|
+
? STATEFUL_LIMIT_DEFINITIONS.map((definition) => {
|
|
45
|
+
const configured = inspected.values?.[definition.field];
|
|
46
|
+
return {
|
|
47
|
+
id: definition.field,
|
|
48
|
+
label: definition.label,
|
|
49
|
+
description: `Current ${current[definition.field]} · configured ${configured?.value ?? "unavailable"} (${configured?.source ?? "unknown"})`,
|
|
50
|
+
action: "pick-stateful-limit" as const,
|
|
51
|
+
};
|
|
52
|
+
})
|
|
53
|
+
: []),
|
|
54
|
+
{ id: "back", label: "Back", action: "back" as const },
|
|
55
|
+
],
|
|
56
|
+
hint: "back" as const,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function statefulLimitInputScreen(field: StatefulLimitField, runtime: StatefulLimitRuntime) {
|
|
61
|
+
const definition = statefulLimitDefinition(field);
|
|
62
|
+
const inspected = inspectStatefulLimitSettings();
|
|
63
|
+
const configured = inspected.values?.[field];
|
|
64
|
+
return {
|
|
65
|
+
kind: "input" as const,
|
|
66
|
+
title: definition.label,
|
|
67
|
+
lines: [
|
|
68
|
+
definition.description,
|
|
69
|
+
`Current session: ${runtime.getRuntimeStatus().limits[field]}`,
|
|
70
|
+
`Configured after reload: ${configured?.value ?? "unavailable"} (${configured?.source ?? "unknown"})`,
|
|
71
|
+
`Allowed: whole numbers ${definition.minimum === 0 ? "0 or greater" : "1 or greater"}`,
|
|
72
|
+
`Read from: ${safeTerminalText(inspected.path)}`,
|
|
73
|
+
...(inspected.writePath !== inspected.path
|
|
74
|
+
? [`Saves to: ${safeTerminalText(inspected.writePath)}`]
|
|
75
|
+
: []),
|
|
76
|
+
],
|
|
77
|
+
placeholder: "Enter a whole number",
|
|
78
|
+
action: "set-stateful-limit" as const,
|
|
79
|
+
hint: "back" as const,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function applyStatefulLimitSetting(
|
|
84
|
+
field: StatefulLimitField,
|
|
85
|
+
value: string | undefined,
|
|
86
|
+
ctx: ExtensionCommandContext,
|
|
87
|
+
runtime: StatefulLimitRuntime,
|
|
88
|
+
options: StatefulLimitApplyOptions,
|
|
89
|
+
) {
|
|
90
|
+
const next = parseStatefulLimit(field, value);
|
|
91
|
+
if (next === undefined) {
|
|
92
|
+
notifyValidationError(field, ctx);
|
|
93
|
+
return { kind: "rejected" as const };
|
|
94
|
+
}
|
|
95
|
+
const inspected = inspectStatefulLimitSettings();
|
|
96
|
+
if (inspected.error || !inspected.values) {
|
|
97
|
+
if (options.isCurrent() && !options.signal.aborted) {
|
|
98
|
+
ctx.ui.notify(
|
|
99
|
+
`Subagent settings cannot be edited: ${safeTerminalText(inspected.error ?? "settings are unavailable")}`,
|
|
100
|
+
"error",
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return { kind: "rejected" as const };
|
|
104
|
+
}
|
|
105
|
+
const expected = configuredValues(inspected.values);
|
|
106
|
+
if (next === expected[field]) return { kind: "back" as const };
|
|
107
|
+
|
|
108
|
+
const beforeAgents = runtime.listAgents();
|
|
109
|
+
const affectedBefore = projectedRemovedAgentIds(beforeAgents, expected, {
|
|
110
|
+
...expected,
|
|
111
|
+
[field]: next,
|
|
112
|
+
});
|
|
113
|
+
if (affectedBefore.length > 0) {
|
|
114
|
+
const confirmed = await ctx.ui.confirm(
|
|
115
|
+
`Lower ${statefulLimitDefinition(field).label}?`,
|
|
116
|
+
[
|
|
117
|
+
`This configured value would omit ${affectedBefore.length} currently retained agent record${affectedBefore.length === 1 ? "" : "s"} from projected recovery after reload.`,
|
|
118
|
+
"No agent is closed now, and this menu will not reload Pi.",
|
|
119
|
+
"A later state rewrite can make omitted records unavailable for recovery.",
|
|
120
|
+
].join("\n\n"),
|
|
121
|
+
{ signal: options.signal },
|
|
122
|
+
);
|
|
123
|
+
if (options.signal.aborted || !options.isCurrent()) return { kind: "close" as const };
|
|
124
|
+
if (!confirmed) return { kind: "rejected" as const };
|
|
125
|
+
const affectedAfter = projectedRemovedAgentIds(runtime.listAgents(), expected, {
|
|
126
|
+
...expected,
|
|
127
|
+
[field]: next,
|
|
128
|
+
});
|
|
129
|
+
if (!sameIds(affectedBefore, affectedAfter)) {
|
|
130
|
+
ctx.ui.notify("Detached agents changed while confirming; review the limit again.", "warning");
|
|
131
|
+
return { kind: "rejected" as const };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (options.signal.aborted || !options.isCurrent()) return { kind: "close" as const };
|
|
136
|
+
try {
|
|
137
|
+
updateStatefulLimitSetting(field, next, expected);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (options.isCurrent() && !options.signal.aborted) {
|
|
140
|
+
ctx.ui.notify(
|
|
141
|
+
`Detached limit was not saved; the previous setting is unchanged: ${formatError(error)}`,
|
|
142
|
+
"error",
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return { kind: "rejected" as const };
|
|
146
|
+
}
|
|
147
|
+
if (options.signal.aborted || !options.isCurrent()) return { kind: "close" as const };
|
|
148
|
+
ctx.ui.notify(
|
|
149
|
+
`Saved ${statefulLimitDefinition(field).label.toLowerCase()}: ${next}. Applies after /reload; clear retained agents before reloading if their work must not be interrupted.`,
|
|
150
|
+
"info",
|
|
151
|
+
);
|
|
152
|
+
return { kind: "back" as const };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function formatDetachedLimitSummary(status: StatefulSubagentRuntimeStatus): string {
|
|
156
|
+
return [
|
|
157
|
+
`${status.limits.maxAgents} retained`,
|
|
158
|
+
`${status.limits.maxActiveTurns} active turns`,
|
|
159
|
+
`${status.limits.maxChildrenPerAgent} children`,
|
|
160
|
+
`depth ${status.limits.maxDepth}`,
|
|
161
|
+
`${status.limits.maxStoredAgents} stored`,
|
|
162
|
+
].join(" · ");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function formatConfiguredDetachedLimitDivergence(
|
|
166
|
+
status: StatefulSubagentRuntimeStatus,
|
|
167
|
+
values: NonNullable<ReturnType<typeof inspectStatefulLimitSettings>["values"]>,
|
|
168
|
+
): string | undefined {
|
|
169
|
+
const changed = STATEFUL_LIMIT_DEFINITIONS.flatMap((definition) => {
|
|
170
|
+
const configured = values[definition.field].value;
|
|
171
|
+
return configured === status.limits[definition.field]
|
|
172
|
+
? []
|
|
173
|
+
: [`${definition.label.toLowerCase()} ${configured}`];
|
|
174
|
+
});
|
|
175
|
+
return changed.length > 0 ? `Configured after reload: ${changed.join(" · ")}` : undefined;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function formatConfiguredDetachedLimits(
|
|
179
|
+
values: NonNullable<ReturnType<typeof inspectStatefulLimitSettings>["values"]>,
|
|
180
|
+
): string {
|
|
181
|
+
return STATEFUL_LIMIT_DEFINITIONS.map((definition) => {
|
|
182
|
+
const snapshot = values[definition.field];
|
|
183
|
+
return `${definition.label.toLowerCase()} ${snapshot.value} (${snapshot.source})`;
|
|
184
|
+
}).join(" · ");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function formatEmptyStatefulRuntime(status: StatefulSubagentRuntimeStatus): string {
|
|
188
|
+
if (!status.enabled) return "Stateful subagents are disabled in user settings.";
|
|
189
|
+
if (!status.initialized) return "Stateful subagents are not initialized for this session.";
|
|
190
|
+
return "No current-session subagents.";
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function parseStatefulLimit(
|
|
194
|
+
field: StatefulLimitField,
|
|
195
|
+
value: string | undefined,
|
|
196
|
+
): number | undefined {
|
|
197
|
+
const normalized = value?.trim() ?? "";
|
|
198
|
+
if (!/^\d+$/u.test(normalized)) return undefined;
|
|
199
|
+
const parsed = Number(normalized);
|
|
200
|
+
return isValidStatefulLimit(field, parsed) ? parsed : undefined;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function notifyValidationError(field: StatefulLimitField, ctx: ExtensionCommandContext): void {
|
|
204
|
+
const definition = statefulLimitDefinition(field);
|
|
205
|
+
ctx.ui.notify(
|
|
206
|
+
`${definition.label} must be a safe whole number ${definition.minimum === 0 ? "0 or greater" : "1 or greater"}.`,
|
|
207
|
+
"warning",
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function configuredValues(
|
|
212
|
+
values: NonNullable<ReturnType<typeof inspectStatefulLimitSettings>["values"]>,
|
|
213
|
+
): StatefulLimits {
|
|
214
|
+
return resolveStatefulLimits(
|
|
215
|
+
Object.fromEntries(Object.entries(values).map(([field, snapshot]) => [field, snapshot.value])),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function projectedRemovedAgentIds(
|
|
220
|
+
agents: readonly ManagedAgent[],
|
|
221
|
+
current: StatefulLimits,
|
|
222
|
+
next: StatefulLimits,
|
|
223
|
+
): string[] {
|
|
224
|
+
const restorable = agents.filter(
|
|
225
|
+
(agent) => agent.state !== "closed" && agent.workspaceMode !== "worktree",
|
|
226
|
+
);
|
|
227
|
+
const before = projectForReload(restorable, current);
|
|
228
|
+
const afterIds = new Set(projectForReload(restorable, next).map((agent) => agent.id));
|
|
229
|
+
return before.filter((agent) => !afterIds.has(agent.id)).map((agent) => agent.id);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function projectForReload(agents: readonly ManagedAgent[], limits: StatefulLimits): ManagedAgent[] {
|
|
233
|
+
const stored = projectAgentRecords(agents, { maxAgents: limits.maxStoredAgents });
|
|
234
|
+
return projectAgentRecords(stored, {
|
|
235
|
+
maxAgents: limits.maxAgents,
|
|
236
|
+
maxDepth: limits.maxDepth,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function sameIds(left: readonly string[], right: readonly string[]): boolean {
|
|
241
|
+
return left.length === right.length && left.every((id, index) => id === right[index]);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function formatError(error: unknown): string {
|
|
245
|
+
return safeTerminalText(error instanceof Error ? error.message : String(error));
|
|
246
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { SubagentRuntimeSettings } from "./agents.js";
|
|
2
|
+
|
|
3
|
+
export const STATEFUL_LIMIT_FIELDS = [
|
|
4
|
+
"maxAgents",
|
|
5
|
+
"maxActiveTurns",
|
|
6
|
+
"maxChildrenPerAgent",
|
|
7
|
+
"maxDepth",
|
|
8
|
+
"maxStoredAgents",
|
|
9
|
+
] as const;
|
|
10
|
+
|
|
11
|
+
export type StatefulLimitField = (typeof STATEFUL_LIMIT_FIELDS)[number];
|
|
12
|
+
|
|
13
|
+
export interface StatefulLimits {
|
|
14
|
+
maxAgents: number;
|
|
15
|
+
maxActiveTurns: number;
|
|
16
|
+
maxChildrenPerAgent: number;
|
|
17
|
+
maxDepth: number;
|
|
18
|
+
maxStoredAgents: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface StatefulLimitDefinition {
|
|
22
|
+
field: StatefulLimitField;
|
|
23
|
+
label: string;
|
|
24
|
+
description: string;
|
|
25
|
+
defaultValue: number;
|
|
26
|
+
minimum: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const STATEFUL_LIMIT_DEFINITIONS: readonly StatefulLimitDefinition[] = [
|
|
30
|
+
{
|
|
31
|
+
field: "maxAgents",
|
|
32
|
+
label: "Retained agents",
|
|
33
|
+
description: "Running, queued, and reusable idle detached agents",
|
|
34
|
+
defaultValue: 16,
|
|
35
|
+
minimum: 1,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
field: "maxActiveTurns",
|
|
39
|
+
label: "Active turns",
|
|
40
|
+
description: "Detached agent turns that may run at the same time",
|
|
41
|
+
defaultValue: 4,
|
|
42
|
+
minimum: 1,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
field: "maxChildrenPerAgent",
|
|
46
|
+
label: "Children per agent",
|
|
47
|
+
description: "Direct child agents retained beneath one parent",
|
|
48
|
+
defaultValue: 8,
|
|
49
|
+
minimum: 1,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
field: "maxDepth",
|
|
53
|
+
label: "Agent tree depth",
|
|
54
|
+
description: "Nested child levels below a root agent",
|
|
55
|
+
defaultValue: 3,
|
|
56
|
+
minimum: 0,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
field: "maxStoredAgents",
|
|
60
|
+
label: "Stored agents",
|
|
61
|
+
description: "Detached agent records kept per session on disk",
|
|
62
|
+
defaultValue: 50,
|
|
63
|
+
minimum: 1,
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
const definitionsByField = new Map(
|
|
68
|
+
STATEFUL_LIMIT_DEFINITIONS.map((definition) => [definition.field, definition]),
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
export function statefulLimitDefinition(field: StatefulLimitField): StatefulLimitDefinition {
|
|
72
|
+
const definition = definitionsByField.get(field);
|
|
73
|
+
if (!definition) throw new Error(`Unknown stateful limit: ${field}`);
|
|
74
|
+
return definition;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function isStatefulLimitField(value: string): value is StatefulLimitField {
|
|
78
|
+
return (STATEFUL_LIMIT_FIELDS as readonly string[]).includes(value);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function isValidStatefulLimit(field: StatefulLimitField, value: unknown): value is number {
|
|
82
|
+
return (
|
|
83
|
+
typeof value === "number" &&
|
|
84
|
+
Number.isSafeInteger(value) &&
|
|
85
|
+
value >= statefulLimitDefinition(field).minimum
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function resolveStatefulLimits(settings?: SubagentRuntimeSettings): StatefulLimits {
|
|
90
|
+
return Object.fromEntries(
|
|
91
|
+
STATEFUL_LIMIT_DEFINITIONS.map((definition) => [
|
|
92
|
+
definition.field,
|
|
93
|
+
settings?.[definition.field] ?? definition.defaultValue,
|
|
94
|
+
]),
|
|
95
|
+
) as unknown as StatefulLimits;
|
|
96
|
+
}
|
package/src/stateful-prompt.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import type { AgentConfig } from "./agents.js";
|
|
2
2
|
import { redactPrivateText } from "./context.js";
|
|
3
|
+
import { appendDelegationContract } from "./delegation-contract.js";
|
|
3
4
|
import { resolveDefaultSubagentTimeoutMs } from "./execution.js";
|
|
4
5
|
import { DEFAULT_MAX_CONTEXT_BYTES, truncateUtf8 } from "./limits.js";
|
|
5
6
|
import type { ManagedAgent } from "./registry.js";
|
|
7
|
+
import { appendResultInstruction } from "./result-contract.js";
|
|
6
8
|
|
|
7
9
|
export function buildStatefulTurnPrompt(
|
|
8
|
-
record: Pick<
|
|
10
|
+
record: Pick<
|
|
11
|
+
ManagedAgent,
|
|
12
|
+
"context" | "history" | "mailbox" | "currentMailboxMessageIds" | "contract" | "resultFormat"
|
|
13
|
+
>,
|
|
9
14
|
task: string,
|
|
10
15
|
maxBytes = DEFAULT_MAX_CONTEXT_BYTES,
|
|
11
16
|
): { text: string; truncated: boolean } {
|
|
@@ -30,7 +35,11 @@ export function buildStatefulTurnPrompt(
|
|
|
30
35
|
]
|
|
31
36
|
.filter(Boolean)
|
|
32
37
|
.join("\n\n---\n\n");
|
|
33
|
-
|
|
38
|
+
const contracted = appendDelegationContract(context, record.contract, maxBytes);
|
|
39
|
+
return truncateUtf8(
|
|
40
|
+
appendResultInstruction(contracted.text, record.resultFormat, maxBytes),
|
|
41
|
+
maxBytes,
|
|
42
|
+
);
|
|
34
43
|
}
|
|
35
44
|
|
|
36
45
|
export function resolveStatefulTurnTimeout(
|
package/src/stateful-render.ts
CHANGED
|
@@ -43,6 +43,10 @@ function renderStatefulCall(tool: StatefulRenderTool, args: Record<string, unkno
|
|
|
43
43
|
safeLine(args.workspaceMode, "shared", 64),
|
|
44
44
|
];
|
|
45
45
|
if (typeof args.thinkingLevel === "string") metadata.push(`thinking:${args.thinkingLevel}`);
|
|
46
|
+
if (typeof args.timeoutMs === "number") metadata.push(`timeout:${args.timeoutMs}ms`);
|
|
47
|
+
if (typeof args.idleTimeoutMs === "number") metadata.push(`idle:${args.idleTimeoutMs}ms`);
|
|
48
|
+
if (typeof args.maxTurns === "number") metadata.push(`turns:${args.maxTurns}`);
|
|
49
|
+
if (typeof args.maxToolCalls === "number") metadata.push(`tools:${args.maxToolCalls}`);
|
|
46
50
|
return new Text(
|
|
47
51
|
[
|
|
48
52
|
toolHeader(theme, "subagent_spawn", args.agent, metadata),
|
|
@@ -53,9 +57,14 @@ function renderStatefulCall(tool: StatefulRenderTool, args: Record<string, unkno
|
|
|
53
57
|
);
|
|
54
58
|
}
|
|
55
59
|
if (tool === "send") {
|
|
60
|
+
const metadata = ["follow-up"];
|
|
61
|
+
if (typeof args.timeoutMs === "number") metadata.push(`timeout:${args.timeoutMs}ms`);
|
|
62
|
+
if (typeof args.idleTimeoutMs === "number") metadata.push(`idle:${args.idleTimeoutMs}ms`);
|
|
63
|
+
if (typeof args.maxTurns === "number") metadata.push(`turns:${args.maxTurns}`);
|
|
64
|
+
if (typeof args.maxToolCalls === "number") metadata.push(`tools:${args.maxToolCalls}`);
|
|
56
65
|
return new Text(
|
|
57
66
|
[
|
|
58
|
-
toolHeader(theme, "subagent_send", args.agentId,
|
|
67
|
+
toolHeader(theme, "subagent_send", args.agentId, metadata),
|
|
59
68
|
` ${theme.fg("dim", safeLine(args.task, "...", 2 * 1024))}`,
|
|
60
69
|
].join("\n"),
|
|
61
70
|
0,
|
|
@@ -112,12 +121,43 @@ function renderAgentResult(
|
|
|
112
121
|
`${statusBadge(theme, lifecycleStatus(state))} · ${theme.fg("accent", safeLine(agent.id, "agent", 256))} · ${theme.fg("toolOutput", safeLine(agent.agent, "subagent", 256))} · ${theme.fg("muted", state)}`,
|
|
113
122
|
];
|
|
114
123
|
const thinking = stringValue(agent.thinkingLevel);
|
|
124
|
+
const timeout =
|
|
125
|
+
typeof agent.currentTimeoutMs === "number"
|
|
126
|
+
? agent.currentTimeoutMs
|
|
127
|
+
: typeof agent.timeoutMs === "number"
|
|
128
|
+
? agent.timeoutMs
|
|
129
|
+
: 0;
|
|
130
|
+
const idleTimeout =
|
|
131
|
+
typeof agent.currentIdleTimeoutMs === "number"
|
|
132
|
+
? agent.currentIdleTimeoutMs
|
|
133
|
+
: typeof agent.idleTimeoutMs === "number"
|
|
134
|
+
? agent.idleTimeoutMs
|
|
135
|
+
: 0;
|
|
136
|
+
const maxTurns =
|
|
137
|
+
typeof agent.currentMaxTurns === "number"
|
|
138
|
+
? agent.currentMaxTurns
|
|
139
|
+
: typeof agent.maxTurns === "number"
|
|
140
|
+
? agent.maxTurns
|
|
141
|
+
: 0;
|
|
142
|
+
const maxToolCalls =
|
|
143
|
+
typeof agent.currentMaxToolCalls === "number"
|
|
144
|
+
? agent.currentMaxToolCalls
|
|
145
|
+
: typeof agent.maxToolCalls === "number"
|
|
146
|
+
? agent.maxToolCalls
|
|
147
|
+
: 0;
|
|
115
148
|
const unread = typeof agent.unreadMessages === "number" ? agent.unreadMessages : 0;
|
|
116
|
-
if (thinking || unread > 0) {
|
|
149
|
+
if (thinking || timeout || idleTimeout || maxTurns || maxToolCalls || unread > 0) {
|
|
117
150
|
lines.push(
|
|
118
151
|
theme.fg(
|
|
119
152
|
"dim",
|
|
120
|
-
[
|
|
153
|
+
[
|
|
154
|
+
thinking && `thinking:${safeLine(thinking, "", 128)}`,
|
|
155
|
+
timeout && `timeout:${timeout}ms`,
|
|
156
|
+
idleTimeout && `idle:${idleTimeout}ms`,
|
|
157
|
+
maxTurns && `turns:${maxTurns}`,
|
|
158
|
+
maxToolCalls && `tools:${maxToolCalls}`,
|
|
159
|
+
unread > 0 && `unread:${unread}`,
|
|
160
|
+
]
|
|
121
161
|
.filter(Boolean)
|
|
122
162
|
.join(" · "),
|
|
123
163
|
),
|
|
@@ -237,6 +277,11 @@ function lifecycleStatus(state: string): RenderStatus {
|
|
|
237
277
|
return "running";
|
|
238
278
|
case "idle":
|
|
239
279
|
return "idle";
|
|
280
|
+
case "blocked":
|
|
281
|
+
case "needs-input":
|
|
282
|
+
case "abstained":
|
|
283
|
+
case "stale":
|
|
284
|
+
return "warning";
|
|
240
285
|
case "failed":
|
|
241
286
|
return "failed";
|
|
242
287
|
case "interrupted":
|