@narumitw/pi-subagents 0.53.0 → 1.0.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.
Files changed (66) hide show
  1. package/README.md +85 -15
  2. package/package.json +1 -1
  3. package/src/agents/built-ins.ts +124 -0
  4. package/src/agents/catalog.ts +224 -0
  5. package/src/agents/discovery.ts +249 -0
  6. package/src/agents/types.ts +98 -0
  7. package/src/agents.ts +47 -670
  8. package/src/auto-transport.ts +2 -1
  9. package/src/automation.ts +7 -2
  10. package/src/capability-router.ts +1 -1
  11. package/src/completion-delivery.ts +82 -11
  12. package/src/config-status.ts +2 -2
  13. package/src/config-ui.ts +8 -8
  14. package/src/consult-resources.ts +1 -1
  15. package/src/consult.ts +9 -7
  16. package/src/create-stateful-transport.ts +2 -1
  17. package/src/cwd-policy.ts +1 -1
  18. package/src/execution/budget.ts +56 -0
  19. package/src/execution/runtime-policy.ts +19 -0
  20. package/src/execution-plan.ts +1 -1
  21. package/src/execution-profiles.ts +1 -1
  22. package/src/execution-ui.ts +1 -1
  23. package/src/execution.ts +269 -100
  24. package/src/in-process-transport.ts +3 -2
  25. package/src/inspect.ts +39 -8
  26. package/src/limits.ts +1 -0
  27. package/src/orchestration-metrics.ts +12 -5
  28. package/src/panel-execution.ts +1 -1
  29. package/src/panel-planning.ts +1 -1
  30. package/src/params.ts +3 -1
  31. package/src/persistence.ts +106 -7
  32. package/src/registry-types.ts +22 -5
  33. package/src/registry.ts +205 -37
  34. package/src/render.ts +1 -1
  35. package/src/retained-semantic-state.ts +1 -1
  36. package/src/rpc-transport-metadata.ts +1 -1
  37. package/src/rpc-transport.ts +2 -1
  38. package/src/runner.ts +6 -1
  39. package/src/settings/inspection.ts +275 -0
  40. package/src/settings/schema.ts +186 -0
  41. package/src/settings.ts +72 -420
  42. package/src/spawn-idempotency.ts +1 -1
  43. package/src/stateful-agent-view.ts +87 -0
  44. package/src/stateful-config.ts +1 -1
  45. package/src/stateful-guidance.ts +2 -2
  46. package/src/stateful-limits.ts +1 -1
  47. package/src/stateful-prompt.ts +2 -2
  48. package/src/stateful-render.ts +0 -1
  49. package/src/stateful-safety.ts +2 -1
  50. package/src/stateful-tool-params.ts +13 -20
  51. package/src/stateful.ts +36 -117
  52. package/src/subagents.ts +8 -9
  53. package/src/subprocess-transport.ts +2 -6
  54. package/src/transport-types.ts +1 -1
  55. package/src/transport-ui.ts +1 -1
  56. package/src/verification-harness.ts +516 -0
  57. package/src/verification-receipt.ts +275 -0
  58. package/src/verified-execution-benchmark.ts +86 -0
  59. package/src/verified-execution-contract.ts +219 -0
  60. package/src/work-item-ledger.ts +510 -37
  61. package/src/work-item-persistence.ts +31 -0
  62. package/src/workflow-completion-controller.ts +397 -0
  63. package/src/workflow-plan-compiler.ts +1 -1
  64. package/src/workflow-plan-patch.ts +1 -1
  65. package/src/workflow-planning.ts +11 -1
  66. package/src/workflow-ui.ts +1 -1
@@ -0,0 +1,275 @@
1
+ import type {
2
+ CompletionDelivery,
3
+ ConsultationCwdPolicy,
4
+ ConsultResourcePolicy,
5
+ DelegationCwdPolicy,
6
+ SubagentSettings,
7
+ SubagentTransportKind,
8
+ } from "../agents/types.js";
9
+ import { DEFAULT_MAX_PARALLEL_TASKS } from "../limits.js";
10
+ import {
11
+ resolveStatefulLimits,
12
+ STATEFUL_LIMIT_FIELDS,
13
+ type StatefulLimitField,
14
+ } from "../stateful-limits.js";
15
+ import { hasOwn, isPlainObject } from "./schema.js";
16
+
17
+ const DEFAULT_COMPLETION_DELIVERY: CompletionDelivery = "next-turn";
18
+ export const DEFAULT_CONSULT_RESOURCE_POLICY: ConsultResourcePolicy = "project-context";
19
+ export const DEFAULT_CONSULTATION_CWD_POLICY: ConsultationCwdPolicy = "anywhere";
20
+ export const DEFAULT_DELEGATION_CWD_POLICY: DelegationCwdPolicy = "trusted-targets";
21
+
22
+ export type DelegationWorkflow = "all" | "async-only" | "blocking-only" | "disabled";
23
+
24
+ type SettingsSource = "default" | "user settings";
25
+
26
+ export interface InspectedSubagentSettingsDocument {
27
+ path: string;
28
+ raw?: Record<string, unknown>;
29
+ settings?: SubagentSettings;
30
+ error?: string;
31
+ }
32
+
33
+ export interface DelegationWorkflowSettingsSnapshot {
34
+ path: string;
35
+ value: DelegationWorkflow;
36
+ source: SettingsSource;
37
+ error?: string;
38
+ }
39
+
40
+ export interface CompletionDeliverySettingsSnapshot {
41
+ path: string;
42
+ value: CompletionDelivery;
43
+ source: SettingsSource;
44
+ error?: string;
45
+ }
46
+
47
+ export interface StatefulTransportSettingsSnapshot {
48
+ path: string;
49
+ value: SubagentTransportKind;
50
+ source: SettingsSource;
51
+ error?: string;
52
+ }
53
+
54
+ export interface BlockingParallelLimitSettingsSnapshot {
55
+ path: string;
56
+ value: number;
57
+ source: SettingsSource;
58
+ error?: string;
59
+ }
60
+
61
+ export interface StatefulLimitFieldSnapshot {
62
+ value: number;
63
+ source: SettingsSource;
64
+ }
65
+
66
+ export interface StatefulLimitSettingsSnapshot {
67
+ path: string;
68
+ writePath: string;
69
+ values?: Record<StatefulLimitField, StatefulLimitFieldSnapshot>;
70
+ error?: string;
71
+ }
72
+
73
+ export interface ConsultResourceSettingsSnapshot {
74
+ path: string;
75
+ value: ConsultResourcePolicy;
76
+ source: SettingsSource;
77
+ error?: string;
78
+ }
79
+
80
+ export interface CwdPolicyFieldSnapshot<T> {
81
+ value: T;
82
+ source: SettingsSource;
83
+ }
84
+
85
+ export interface CwdPolicySettingsSnapshot {
86
+ path: string;
87
+ consultation: CwdPolicyFieldSnapshot<ConsultationCwdPolicy>;
88
+ delegation: CwdPolicyFieldSnapshot<DelegationCwdPolicy>;
89
+ error?: string;
90
+ }
91
+
92
+ export interface SubagentSettingsSnapshot {
93
+ path: string;
94
+ settings?: SubagentSettings;
95
+ source: SettingsSource;
96
+ error?: string;
97
+ }
98
+
99
+ export function resolveDelegationWorkflow(
100
+ blockingEnabled: boolean,
101
+ statefulEnabled: boolean,
102
+ ): DelegationWorkflow {
103
+ if (blockingEnabled && statefulEnabled) return "all";
104
+ if (statefulEnabled) return "async-only";
105
+ if (blockingEnabled) return "blocking-only";
106
+ return "disabled";
107
+ }
108
+
109
+ export function resolveBlockingMaxParallelTasks(settings?: SubagentSettings): number {
110
+ return settings?.blocking?.maxParallelTasks ?? DEFAULT_MAX_PARALLEL_TASKS;
111
+ }
112
+
113
+ export function buildSubagentSettingsSnapshot(
114
+ inspected: InspectedSubagentSettingsDocument,
115
+ ): SubagentSettingsSnapshot {
116
+ return {
117
+ path: inspected.path,
118
+ settings: inspected.settings,
119
+ source: inspected.settings ? "user settings" : "default",
120
+ ...(inspected.error ? { error: inspected.error } : {}),
121
+ };
122
+ }
123
+
124
+ export function buildConsultResourceSettingsSnapshot(
125
+ inspected: InspectedSubagentSettingsDocument,
126
+ ): ConsultResourceSettingsSnapshot {
127
+ if (!inspected.raw || !inspected.settings) {
128
+ return {
129
+ path: inspected.path,
130
+ value: DEFAULT_CONSULT_RESOURCE_POLICY,
131
+ source: "default",
132
+ ...(inspected.error ? { error: inspected.error } : {}),
133
+ };
134
+ }
135
+ const explicit =
136
+ isPlainObject(inspected.raw.consult) && hasOwn(inspected.raw.consult, "resources");
137
+ return {
138
+ path: inspected.path,
139
+ value: inspected.settings.consult?.resources ?? DEFAULT_CONSULT_RESOURCE_POLICY,
140
+ source: explicit ? "user settings" : "default",
141
+ };
142
+ }
143
+
144
+ export function buildCwdPolicySettingsSnapshot(
145
+ inspected: InspectedSubagentSettingsDocument,
146
+ ): CwdPolicySettingsSnapshot {
147
+ if (!inspected.raw || !inspected.settings) {
148
+ return {
149
+ path: inspected.path,
150
+ consultation: { value: DEFAULT_CONSULTATION_CWD_POLICY, source: "default" },
151
+ delegation: { value: DEFAULT_DELEGATION_CWD_POLICY, source: "default" },
152
+ ...(inspected.error ? { error: inspected.error } : {}),
153
+ };
154
+ }
155
+ const rawPolicy = isPlainObject(inspected.raw.cwdPolicy) ? inspected.raw.cwdPolicy : undefined;
156
+ return {
157
+ path: inspected.path,
158
+ consultation: {
159
+ value: inspected.settings.cwdPolicy?.consultation ?? DEFAULT_CONSULTATION_CWD_POLICY,
160
+ source: rawPolicy && hasOwn(rawPolicy, "consultation") ? "user settings" : "default",
161
+ },
162
+ delegation: {
163
+ value: inspected.settings.cwdPolicy?.delegation ?? DEFAULT_DELEGATION_CWD_POLICY,
164
+ source: rawPolicy && hasOwn(rawPolicy, "delegation") ? "user settings" : "default",
165
+ },
166
+ };
167
+ }
168
+
169
+ export function buildDelegationWorkflowSettingsSnapshot(
170
+ inspected: InspectedSubagentSettingsDocument,
171
+ ): DelegationWorkflowSettingsSnapshot {
172
+ if (!inspected.raw || !inspected.settings) {
173
+ return {
174
+ path: inspected.path,
175
+ value: "all",
176
+ source: "default",
177
+ ...(inspected.error ? { error: inspected.error } : {}),
178
+ };
179
+ }
180
+ const explicit =
181
+ (isPlainObject(inspected.raw.blocking) && hasOwn(inspected.raw.blocking, "enabled")) ||
182
+ (isPlainObject(inspected.raw.stateful) && hasOwn(inspected.raw.stateful, "enabled"));
183
+ return {
184
+ path: inspected.path,
185
+ value: resolveDelegationWorkflow(
186
+ inspected.settings.blocking?.enabled !== false,
187
+ inspected.settings.stateful?.enabled !== false,
188
+ ),
189
+ source: explicit ? "user settings" : "default",
190
+ };
191
+ }
192
+
193
+ export function buildCompletionDeliverySettingsSnapshot(
194
+ inspected: InspectedSubagentSettingsDocument,
195
+ ): CompletionDeliverySettingsSnapshot {
196
+ if (!inspected.raw || !inspected.settings) {
197
+ return {
198
+ path: inspected.path,
199
+ value: DEFAULT_COMPLETION_DELIVERY,
200
+ source: "default",
201
+ ...(inspected.error ? { error: inspected.error } : {}),
202
+ };
203
+ }
204
+ const explicit =
205
+ isPlainObject(inspected.raw.stateful) && hasOwn(inspected.raw.stateful, "completionDelivery");
206
+ return {
207
+ path: inspected.path,
208
+ value: inspected.settings.stateful?.completionDelivery ?? DEFAULT_COMPLETION_DELIVERY,
209
+ source: explicit ? "user settings" : "default",
210
+ };
211
+ }
212
+
213
+ export function buildStatefulTransportSettingsSnapshot(
214
+ inspected: InspectedSubagentSettingsDocument,
215
+ ): StatefulTransportSettingsSnapshot {
216
+ if (!inspected.raw || !inspected.settings) {
217
+ return {
218
+ path: inspected.path,
219
+ value: "subprocess",
220
+ source: "default",
221
+ ...(inspected.error ? { error: inspected.error } : {}),
222
+ };
223
+ }
224
+ const explicit =
225
+ isPlainObject(inspected.raw.stateful) && hasOwn(inspected.raw.stateful, "transport");
226
+ return {
227
+ path: inspected.path,
228
+ value: inspected.settings.stateful?.transport ?? "subprocess",
229
+ source: explicit ? "user settings" : "default",
230
+ };
231
+ }
232
+
233
+ export function buildBlockingParallelLimitSettingsSnapshot(
234
+ inspected: InspectedSubagentSettingsDocument,
235
+ ): BlockingParallelLimitSettingsSnapshot {
236
+ if (!inspected.raw || !inspected.settings) {
237
+ return {
238
+ path: inspected.path,
239
+ value: DEFAULT_MAX_PARALLEL_TASKS,
240
+ source: "default",
241
+ ...(inspected.error ? { error: inspected.error } : {}),
242
+ };
243
+ }
244
+ const explicit =
245
+ isPlainObject(inspected.raw.blocking) && hasOwn(inspected.raw.blocking, "maxParallelTasks");
246
+ return {
247
+ path: inspected.path,
248
+ value: resolveBlockingMaxParallelTasks(inspected.settings),
249
+ source: explicit ? "user settings" : "default",
250
+ };
251
+ }
252
+
253
+ export function buildStatefulLimitSettingsSnapshot(
254
+ inspected: InspectedSubagentSettingsDocument,
255
+ writePath: string,
256
+ ): StatefulLimitSettingsSnapshot {
257
+ if (inspected.error) {
258
+ return { path: inspected.path, writePath, error: inspected.error };
259
+ }
260
+ const resolved = resolveStatefulLimits(inspected.settings?.stateful);
261
+ const rawStateful = isPlainObject(inspected.raw?.stateful) ? inspected.raw.stateful : undefined;
262
+ return {
263
+ path: inspected.path,
264
+ writePath,
265
+ values: Object.fromEntries(
266
+ STATEFUL_LIMIT_FIELDS.map((field) => [
267
+ field,
268
+ {
269
+ value: resolved[field],
270
+ source: rawStateful && hasOwn(rawStateful, field) ? "user settings" : "default",
271
+ },
272
+ ]),
273
+ ) as unknown as Record<StatefulLimitField, StatefulLimitFieldSnapshot>,
274
+ };
275
+ }
@@ -0,0 +1,186 @@
1
+ import {
2
+ CONSULT_RESOURCE_POLICIES,
3
+ CONSULTATION_CWD_POLICIES,
4
+ type ConsultationCwdPolicy,
5
+ type ConsultResourcePolicy,
6
+ DELEGATION_CWD_POLICIES,
7
+ type DelegationCwdPolicy,
8
+ isThinkingLevel,
9
+ type SubagentAgentConfig,
10
+ type SubagentSettings,
11
+ } from "../agents/types.js";
12
+ import { MAX_CONFIGURABLE_PARALLEL_TASKS, MAX_SUBAGENT_TIMEOUT_MS } from "../limits.js";
13
+ import { isValidStatefulLimit, STATEFUL_LIMIT_FIELDS } from "../stateful-limits.js";
14
+
15
+ export function hasOwn(obj: object, key: PropertyKey): boolean {
16
+ return Object.hasOwn(obj, key);
17
+ }
18
+
19
+ export function isPlainObject(value: unknown): value is Record<string, unknown> {
20
+ return typeof value === "object" && value !== null && !Array.isArray(value);
21
+ }
22
+
23
+ function isStringArray(value: unknown): value is string[] {
24
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
25
+ }
26
+
27
+ export function isPositiveNumber(value: unknown): value is number {
28
+ return typeof value === "number" && Number.isFinite(value) && value >= 1;
29
+ }
30
+
31
+ export function isPositiveInteger(value: unknown): value is number {
32
+ return isPositiveNumber(value) && Number.isSafeInteger(value);
33
+ }
34
+
35
+ export function normalizeAgentSettings(value: unknown): SubagentAgentConfig | undefined {
36
+ if (!isPlainObject(value)) return undefined;
37
+
38
+ const config: SubagentAgentConfig = {};
39
+ let hasKnownField = false;
40
+
41
+ if (hasOwn(value, "tools")) {
42
+ if (!isStringArray(value.tools)) return undefined;
43
+ config.tools = value.tools;
44
+ hasKnownField = true;
45
+ }
46
+
47
+ if (hasOwn(value, "model")) {
48
+ if (value.model !== null && typeof value.model !== "string") return undefined;
49
+ config.model = value.model;
50
+ hasKnownField = true;
51
+ }
52
+
53
+ if (hasOwn(value, "thinkingLevel")) {
54
+ if (value.thinkingLevel !== null && !isThinkingLevel(value.thinkingLevel)) return undefined;
55
+ config.thinkingLevel = value.thinkingLevel;
56
+ hasKnownField = true;
57
+ }
58
+
59
+ if (hasOwn(value, "timeoutMs")) {
60
+ if (
61
+ value.timeoutMs !== null &&
62
+ (!isPositiveNumber(value.timeoutMs) || value.timeoutMs > MAX_SUBAGENT_TIMEOUT_MS)
63
+ ) {
64
+ return undefined;
65
+ }
66
+ config.timeoutMs = value.timeoutMs;
67
+ hasKnownField = true;
68
+ }
69
+
70
+ return hasKnownField ? config : undefined;
71
+ }
72
+
73
+ export function normalizeSubagentSettings(value: unknown): SubagentSettings | undefined {
74
+ if (!isPlainObject(value)) return undefined;
75
+ const settings: SubagentSettings = {};
76
+ if (hasOwn(value, "agents")) {
77
+ if (!isPlainObject(value.agents)) return undefined;
78
+ const agents: Record<string, SubagentAgentConfig> = {};
79
+ for (const [name, rawConfig] of Object.entries(value.agents)) {
80
+ const config = normalizeAgentSettings(rawConfig);
81
+ if (config) agents[name] = config;
82
+ }
83
+ if (Object.keys(agents).length > 0) settings.agents = agents;
84
+ }
85
+ if (hasOwn(value, "blocking")) {
86
+ if (!isPlainObject(value.blocking)) return undefined;
87
+ const blocking: NonNullable<SubagentSettings["blocking"]> = {};
88
+ if (hasOwn(value.blocking, "enabled")) {
89
+ if (typeof value.blocking.enabled !== "boolean") return undefined;
90
+ blocking.enabled = value.blocking.enabled;
91
+ }
92
+ if (hasOwn(value.blocking, "maxParallelTasks")) {
93
+ if (
94
+ !isPositiveInteger(value.blocking.maxParallelTasks) ||
95
+ value.blocking.maxParallelTasks > MAX_CONFIGURABLE_PARALLEL_TASKS
96
+ ) {
97
+ return undefined;
98
+ }
99
+ blocking.maxParallelTasks = value.blocking.maxParallelTasks;
100
+ }
101
+ settings.blocking = blocking;
102
+ }
103
+ if (hasOwn(value, "stateful")) {
104
+ if (!isPlainObject(value.stateful)) return undefined;
105
+ const runtime: NonNullable<SubagentSettings["stateful"]> = {};
106
+ if (hasOwn(value.stateful, "transport")) {
107
+ if (
108
+ value.stateful.transport !== "subprocess" &&
109
+ value.stateful.transport !== "in-process" &&
110
+ value.stateful.transport !== "rpc" &&
111
+ value.stateful.transport !== "auto"
112
+ ) {
113
+ return undefined;
114
+ }
115
+ runtime.transport = value.stateful.transport;
116
+ }
117
+ if (hasOwn(value.stateful, "completionDelivery")) {
118
+ if (
119
+ value.stateful.completionDelivery !== "next-turn" &&
120
+ value.stateful.completionDelivery !== "auto-resume"
121
+ ) {
122
+ return undefined;
123
+ }
124
+ runtime.completionDelivery = value.stateful.completionDelivery;
125
+ }
126
+ for (const key of STATEFUL_LIMIT_FIELDS) {
127
+ if (hasOwn(value.stateful, key)) {
128
+ if (!isValidStatefulLimit(key, value.stateful[key])) return undefined;
129
+ runtime[key] = value.stateful[key];
130
+ }
131
+ }
132
+ for (const key of ["maxMailboxMessages", "maxMailboxMessageBytes", "idleTtlMs"] as const) {
133
+ if (hasOwn(value.stateful, key)) {
134
+ if (!isPositiveInteger(value.stateful[key])) return undefined;
135
+ runtime[key] = value.stateful[key];
136
+ }
137
+ }
138
+ if (hasOwn(value.stateful, "retentionDays")) {
139
+ if (!isPositiveNumber(value.stateful.retentionDays)) return undefined;
140
+ runtime.retentionDays = value.stateful.retentionDays;
141
+ }
142
+ if (hasOwn(value.stateful, "enabled")) {
143
+ if (typeof value.stateful.enabled !== "boolean") return undefined;
144
+ runtime.enabled = value.stateful.enabled;
145
+ }
146
+ settings.stateful = runtime;
147
+ }
148
+ if (hasOwn(value, "consult")) {
149
+ if (!isPlainObject(value.consult)) return undefined;
150
+ const consult: NonNullable<SubagentSettings["consult"]> = {};
151
+ if (hasOwn(value.consult, "resources")) {
152
+ if (
153
+ typeof value.consult.resources !== "string" ||
154
+ !CONSULT_RESOURCE_POLICIES.includes(value.consult.resources as ConsultResourcePolicy)
155
+ ) {
156
+ return undefined;
157
+ }
158
+ consult.resources = value.consult.resources as ConsultResourcePolicy;
159
+ }
160
+ settings.consult = consult;
161
+ }
162
+ if (hasOwn(value, "cwdPolicy")) {
163
+ if (!isPlainObject(value.cwdPolicy)) return undefined;
164
+ const cwdPolicy: NonNullable<SubagentSettings["cwdPolicy"]> = {};
165
+ if (hasOwn(value.cwdPolicy, "consultation")) {
166
+ if (
167
+ typeof value.cwdPolicy.consultation !== "string" ||
168
+ !CONSULTATION_CWD_POLICIES.includes(value.cwdPolicy.consultation as ConsultationCwdPolicy)
169
+ ) {
170
+ return undefined;
171
+ }
172
+ cwdPolicy.consultation = value.cwdPolicy.consultation as ConsultationCwdPolicy;
173
+ }
174
+ if (hasOwn(value.cwdPolicy, "delegation")) {
175
+ if (
176
+ typeof value.cwdPolicy.delegation !== "string" ||
177
+ !DELEGATION_CWD_POLICIES.includes(value.cwdPolicy.delegation as DelegationCwdPolicy)
178
+ ) {
179
+ return undefined;
180
+ }
181
+ cwdPolicy.delegation = value.cwdPolicy.delegation as DelegationCwdPolicy;
182
+ }
183
+ settings.cwdPolicy = cwdPolicy;
184
+ }
185
+ return settings;
186
+ }