@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
@@ -1,4 +1,5 @@
1
- import { discoverAgents, type SubagentSettings } from "./agents.js";
1
+ import { discoverAgents } from "./agents/discovery.js";
2
+ import type { SubagentSettings } from "./agents/types.js";
2
3
  import type { ManagedAgent, TurnOutcome } from "./registry.js";
3
4
  import { isWriteCapable } from "./stateful-safety.js";
4
5
  import type { SubagentTransport } from "./transport.js";
package/src/automation.ts CHANGED
@@ -9,7 +9,9 @@ import {
9
9
  } from "@earendil-works/pi-coding-agent";
10
10
  import { Text } from "@earendil-works/pi-tui";
11
11
  import { type Static, Type } from "typebox";
12
- import { discoverAgents, getBuiltInAgent, type SubagentSettings } from "./agents.js";
12
+ import { getBuiltInAgent } from "./agents/built-ins.js";
13
+ import { discoverAgents } from "./agents/discovery.js";
14
+ import type { SubagentSettings } from "./agents/types.js";
13
15
  import {
14
16
  AutomationRequestSchema,
15
17
  parseAutomationRequest,
@@ -39,7 +41,10 @@ import {
39
41
  type SubagentDetails,
40
42
  } from "./runner.js";
41
43
  import { boundedPrivateText } from "./safe-text.js";
42
- import { DEFAULT_DELEGATION_CWD_POLICY, resolveBlockingMaxParallelTasks } from "./settings.js";
44
+ import {
45
+ DEFAULT_DELEGATION_CWD_POLICY,
46
+ resolveBlockingMaxParallelTasks,
47
+ } from "./settings/inspection.js";
43
48
  import {
44
49
  type CompiledWorkflowPlan,
45
50
  compileWorkflowPlan,
@@ -1,4 +1,4 @@
1
- import { type AgentConfig, resolveAgentToolNames } from "./agents.js";
1
+ import { type AgentConfig, resolveAgentToolNames } from "./agents/types.js";
2
2
  import type { CapabilityHint } from "./capabilities.js";
3
3
 
4
4
  export interface CapabilityRouteRequest {
@@ -1,18 +1,20 @@
1
1
  import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import type { CompletionDelivery } from "./agents.js";
2
+ import type { CompletionDelivery } from "./agents/types.js";
3
3
  import { redactPrivateText } from "./context.js";
4
- import { DEFAULT_MAX_CONTEXT_BYTES, truncateUtf8 } from "./limits.js";
4
+ import { DEFAULT_MAX_CONTEXT_BYTES, MAX_TOOL_MESSAGE_BYTES, truncateUtf8 } from "./limits.js";
5
5
  import type { AgentTurnCompletion, ManagedAgent } from "./registry.js";
6
6
  import { safeTerminalText } from "./safe-text.js";
7
7
  import { PI_SUBAGENTS_RPC_PROTOCOL } from "./transport-types.js";
8
8
 
9
- const MAX_TOOL_MESSAGE_BYTES = 2 * 1024;
10
9
  const MAX_COMPLETION_ERROR_BYTES = 512;
11
10
  const MAX_COMPLETIONS_PER_MESSAGE = 16;
12
11
  const COMPLETION_BATCH_DELAY_MS = 10;
13
12
 
14
13
  interface CompletionMetadata {
15
14
  protocol: typeof PI_SUBAGENTS_RPC_PROTOCOL;
15
+ completionId: string;
16
+ runId: string;
17
+ generation: number;
16
18
  agentId: string;
17
19
  agent: string;
18
20
  state: string;
@@ -39,13 +41,15 @@ type CompletionPi = Pick<ExtensionAPI, "sendMessage">;
39
41
 
40
42
  export interface CompletionDeliveryBrokerOptions {
41
43
  onDeliveryError?: (error: unknown) => void;
42
- onDelivered?: (completions: readonly AgentTurnCompletion[], deliveredAt: number) => void;
44
+ onAcknowledged?: (completions: readonly AgentTurnCompletion[], acknowledgedAt: number) => void;
43
45
  now?: () => number;
44
46
  }
45
47
 
46
48
  /** Owns bounded completion batching and at most one idle-root wake for one parent session. */
47
49
  export class CompletionDeliveryBroker {
48
50
  private pending: AgentTurnCompletion[] = [];
51
+ private readonly knownCompletionIds = new Set<string>();
52
+ private awaitingParentAck: AgentTurnCompletion[] = [];
49
53
  private flushTimer?: NodeJS.Timeout;
50
54
  private wakeInFlight = false;
51
55
  private closed = false;
@@ -58,7 +62,8 @@ export class CompletionDeliveryBroker {
58
62
  ) {}
59
63
 
60
64
  enqueue(completion: AgentTurnCompletion): void {
61
- if (this.closed) return;
65
+ if (this.closed || this.knownCompletionIds.has(completion.completionId)) return;
66
+ this.knownCompletionIds.add(completion.completionId);
62
67
  this.pending.push(completion);
63
68
  this.scheduleFlush();
64
69
  }
@@ -73,6 +78,14 @@ export class CompletionDeliveryBroker {
73
78
  this.scheduleFlush();
74
79
  }
75
80
 
81
+ onParentContext(messages: readonly unknown[]): void {
82
+ this.acknowledgeVisible(completionIdsFromContext(messages));
83
+ if (this.awaitingParentAck.length > 0) {
84
+ this.pending = [...this.awaitingParentAck.splice(0), ...this.pending];
85
+ }
86
+ this.scheduleFlush();
87
+ }
88
+
76
89
  onParentSettled(): void {
77
90
  this.wakeInFlight = false;
78
91
  this.scheduleFlush();
@@ -89,18 +102,21 @@ export class CompletionDeliveryBroker {
89
102
  let canWake = this.shouldWakeRoot();
90
103
  for (let index = 0; index < batches.length; index++) {
91
104
  const triggerTurn = canWake && index === batches.length - 1;
92
- const message = buildCompletionMessage(batches[index]);
105
+ const batch = batches[index];
106
+ const message = buildCompletionMessage(batch);
93
107
  if (triggerTurn) this.wakeInFlight = true;
108
+ this.awaitingParentAck.push(...batch);
94
109
  try {
95
110
  this.pi.sendMessage(message, { deliverAs: "steer", triggerTurn });
96
- this.notifyDelivered(batches[index]);
97
111
  } catch (primaryError) {
112
+ this.removeAwaiting(batch);
98
113
  if (triggerTurn) this.wakeInFlight = false;
99
114
  canWake = false;
115
+ this.awaitingParentAck.push(...batch);
100
116
  try {
101
117
  this.pi.sendMessage(message, { deliverAs: "nextTurn", triggerTurn: false });
102
- this.notifyDelivered(batches[index]);
103
118
  } catch (fallbackError) {
119
+ this.removeAwaiting(batch);
104
120
  this.pending = [...batches.slice(index).flat(), ...this.pending];
105
121
  try {
106
122
  this.options.onDeliveryError?.(
@@ -123,6 +139,8 @@ export class CompletionDeliveryBroker {
123
139
  if (this.flushTimer) clearTimeout(this.flushTimer);
124
140
  this.flushTimer = undefined;
125
141
  this.pending = [];
142
+ this.awaitingParentAck = [];
143
+ this.knownCompletionIds.clear();
126
144
  }
127
145
 
128
146
  private scheduleFlush(): void {
@@ -133,14 +151,34 @@ export class CompletionDeliveryBroker {
133
151
  }, COMPLETION_BATCH_DELAY_MS);
134
152
  }
135
153
 
136
- private notifyDelivered(completions: readonly AgentTurnCompletion[]): void {
154
+ private acknowledgeVisible(visibleIds: ReadonlySet<string>): void {
155
+ if (visibleIds.size === 0) return;
156
+ const completions = [...this.awaitingParentAck, ...this.pending].filter((completion) =>
157
+ visibleIds.has(completion.completionId),
158
+ );
159
+ if (completions.length === 0) return;
160
+ const acknowledgedIds = new Set(completions.map((completion) => completion.completionId));
161
+ this.awaitingParentAck = this.awaitingParentAck.filter(
162
+ (completion) => !acknowledgedIds.has(completion.completionId),
163
+ );
164
+ this.pending = this.pending.filter(
165
+ (completion) => !acknowledgedIds.has(completion.completionId),
166
+ );
167
+ for (const completion of completions) this.knownCompletionIds.delete(completion.completionId);
137
168
  try {
138
- this.options.onDelivered?.(completions, (this.options.now ?? Date.now)());
169
+ this.options.onAcknowledged?.(completions, (this.options.now ?? Date.now)());
139
170
  } catch {
140
- // Delivery already succeeded, so observer failures cannot requeue it.
171
+ // Context assembly already observed the message, so observer failures cannot retract it.
141
172
  }
142
173
  }
143
174
 
175
+ private removeAwaiting(completions: readonly AgentTurnCompletion[]): void {
176
+ const removed = new Set(completions.map((completion) => completion.completionId));
177
+ this.awaitingParentAck = this.awaitingParentAck.filter(
178
+ (completion) => !removed.has(completion.completionId),
179
+ );
180
+ }
181
+
144
182
  private isRootIdle(): boolean {
145
183
  try {
146
184
  return this.ctx.isIdle();
@@ -159,6 +197,33 @@ export class CompletionDeliveryBroker {
159
197
  }
160
198
  }
161
199
 
200
+ function completionIdsFromContext(messages: readonly unknown[]): Set<string> {
201
+ const ids = new Set<string>();
202
+ for (const message of messages) {
203
+ if (!message || typeof message !== "object" || Array.isArray(message)) continue;
204
+ const record = message as Record<string, unknown>;
205
+ if (record.role !== "custom" || record.customType !== "pi-subagent-completion") continue;
206
+ const details = record.details;
207
+ if (!details || typeof details !== "object" || Array.isArray(details)) continue;
208
+ const metadata = details as Record<string, unknown>;
209
+ if (
210
+ metadata.protocol === PI_SUBAGENTS_RPC_PROTOCOL &&
211
+ typeof metadata.completionId === "string"
212
+ ) {
213
+ ids.add(metadata.completionId);
214
+ }
215
+ if (!Array.isArray(metadata.completions)) continue;
216
+ for (const completion of metadata.completions) {
217
+ if (!completion || typeof completion !== "object" || Array.isArray(completion)) continue;
218
+ const item = completion as Record<string, unknown>;
219
+ if (item.protocol === PI_SUBAGENTS_RPC_PROTOCOL && typeof item.completionId === "string") {
220
+ ids.add(item.completionId);
221
+ }
222
+ }
223
+ }
224
+ return ids;
225
+ }
226
+
162
227
  function chunkCompletions(completions: AgentTurnCompletion[]): AgentTurnCompletion[][] {
163
228
  const batches: AgentTurnCompletion[][] = [];
164
229
  for (let index = 0; index < completions.length; index += MAX_COMPLETIONS_PER_MESSAGE) {
@@ -204,6 +269,9 @@ function buildCompletionMessage(completions: AgentTurnCompletion[]): CompletionM
204
269
  function completionMetadata(completion: AgentTurnCompletion): CompletionMetadata {
205
270
  return {
206
271
  protocol: PI_SUBAGENTS_RPC_PROTOCOL,
272
+ completionId: completion.completionId,
273
+ runId: completion.runId,
274
+ generation: completion.generation,
207
275
  agentId: completion.agent.id,
208
276
  agent: completion.agent.agent,
209
277
  state: completion.agent.state,
@@ -234,6 +302,9 @@ export function buildDetachedCompletionMessage(completion: AgentTurnCompletion):
234
302
  [
235
303
  "Message Type: SUBAGENT_COMPLETION",
236
304
  `Protocol: ${PI_SUBAGENTS_RPC_PROTOCOL}`,
305
+ `Completion ID: ${completion.completionId}`,
306
+ `Run ID: ${completion.runId}`,
307
+ `Generation: ${completion.generation}`,
237
308
  `Agent ID: ${completion.agent.id}`,
238
309
  `Agent: ${agentName}`,
239
310
  `Task: ${task}`,
@@ -4,11 +4,11 @@ import type {
4
4
  ConsultationCwdPolicy,
5
5
  ConsultResourcePolicy,
6
6
  DelegationCwdPolicy,
7
- } from "./agents.js";
7
+ } from "./agents/types.js";
8
8
  import type { SubagentSettingsRuntime } from "./config-ui.js";
9
9
  import { safeTerminalLine as safeTerminalText } from "./safe-text.js";
10
+ import type { DelegationWorkflow } from "./settings/inspection.js";
10
11
  import {
11
- type DelegationWorkflow,
12
12
  inspectBlockingParallelLimitSettings,
13
13
  inspectCompletionDeliverySettings,
14
14
  inspectConsultResourceSettings,
package/src/config-ui.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
- import {
3
- type CompletionDelivery,
4
- type ConsultationCwdPolicy,
5
- type ConsultResourcePolicy,
6
- type DelegationCwdPolicy,
7
- discoverAgents,
8
- } from "./agents.js";
2
+ import { discoverAgents } from "./agents/discovery.js";
3
+ import type {
4
+ CompletionDelivery,
5
+ ConsultationCwdPolicy,
6
+ ConsultResourcePolicy,
7
+ DelegationCwdPolicy,
8
+ } from "./agents/types.js";
9
9
  import {
10
10
  completionLabel,
11
11
  consultationCwdLabel,
@@ -38,8 +38,8 @@ import {
38
38
  } from "./parallel-limit-ui.js";
39
39
  import type { ManagedAgent } from "./registry.js";
40
40
  import { safeTerminalLine as safeTerminalText } from "./safe-text.js";
41
+ import type { DelegationWorkflow } from "./settings/inspection.js";
41
42
  import {
42
- type DelegationWorkflow,
43
43
  hasOwn,
44
44
  inspectBlockingParallelLimitSettings,
45
45
  inspectCompletionDeliverySettings,
@@ -1,4 +1,4 @@
1
- import type { ConsultResourcePolicy } from "./agents.js";
1
+ import type { ConsultResourcePolicy } from "./agents/types.js";
2
2
  import { DEFAULT_MAX_CONTEXT_BYTES, truncateUtf8 } from "./limits.js";
3
3
  import { resolvePiPromptResources } from "./prompt-resources.js";
4
4
  import type { ChildLaunchPolicy } from "./runner.js";
package/src/consult.ts CHANGED
@@ -8,17 +8,16 @@ import {
8
8
  type ToolDefinition,
9
9
  } from "@earendil-works/pi-coding-agent";
10
10
  import { type Static, Type } from "typebox";
11
+ import { DEFAULT_AGENT_CATALOG_MAX_ITEMS } from "./agents/catalog.js";
12
+ import { type AgentDiscoveryResult, discoverAgents } from "./agents/discovery.js";
11
13
  import {
12
14
  type AgentConfig,
13
- type AgentDiscoveryResult,
14
15
  type AgentScope,
15
16
  type ConsultResourcePolicy,
16
- DEFAULT_AGENT_CATALOG_MAX_ITEMS,
17
- discoverAgents,
18
17
  isThinkingLevel,
19
18
  type SubagentSettings,
20
19
  THINKING_LEVELS,
21
- } from "./agents.js";
20
+ } from "./agents/types.js";
22
21
  import { resolveConsultTools } from "./consult-policy.js";
23
22
  import { renderConsultCall, renderConsultResult } from "./consult-render.js";
24
23
  import { resolveConsultResourceLaunchPolicy } from "./consult-resources.js";
@@ -27,7 +26,10 @@ import {
27
26
  type ResolvedSubagentTarget,
28
27
  resolveSubagentTarget,
29
28
  } from "./cwd-policy.js";
30
- import { assertSubagentDepthAllowed, resolveDefaultSubagentTimeoutMs } from "./execution.js";
29
+ import {
30
+ assertSubagentDepthAllowed,
31
+ resolveDefaultSubagentTimeoutMs,
32
+ } from "./execution/runtime-policy.js";
31
33
  import {
32
34
  DEFAULT_MAX_CONTEXT_BYTES,
33
35
  DEFAULT_MAX_STDERR_BYTES,
@@ -45,8 +47,8 @@ import { boundedPrivateText, boundText, safeDisplayPath, safeTerminalLine } from
45
47
  import {
46
48
  DEFAULT_CONSULT_RESOURCE_POLICY,
47
49
  DEFAULT_CONSULTATION_CWD_POLICY,
48
- resolveSubagentThinkingLevel,
49
- } from "./settings.js";
50
+ } from "./settings/inspection.js";
51
+ import { resolveSubagentThinkingLevel } from "./settings.js";
50
52
 
51
53
  const ConsultScopeSchema = StringEnum(["user", "project", "both"] as const, {
52
54
  default: "user",
@@ -1,5 +1,6 @@
1
1
  import type { ModelRegistry } from "@earendil-works/pi-coding-agent";
2
- import { discoverAgents, type SubagentSettings, type SubagentTransportKind } from "./agents.js";
2
+ import { discoverAgents } from "./agents/discovery.js";
3
+ import type { SubagentSettings, SubagentTransportKind } from "./agents/types.js";
3
4
  import { AutoTransport } from "./auto-transport.js";
4
5
  import {
5
6
  type ChildSessionFactory,
package/src/cwd-policy.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as path from "node:path";
3
3
  import { getAgentDir, ProjectTrustStore } from "@earendil-works/pi-coding-agent";
4
- import type { ConsultationCwdPolicy, DelegationCwdPolicy } from "./agents.js";
4
+ import type { ConsultationCwdPolicy, DelegationCwdPolicy } from "./agents/types.js";
5
5
  import { safeTerminalLine } from "./safe-text.js";
6
6
 
7
7
  export type TargetBoundary = "current-workspace" | "external";
@@ -0,0 +1,56 @@
1
+ import type { AgentConfig } from "../agents/types.js";
2
+ import type { TurnLimits } from "../turn-budget.js";
3
+
4
+ export interface ExecutionBudget {
5
+ timeoutMs: number;
6
+ workTimeoutReason: "work_timeout" | "orchestration_timeout";
7
+ workTimeoutReportLimit: number;
8
+ }
9
+
10
+ export function resolveConfiguredTimeout(
11
+ agents: readonly AgentConfig[],
12
+ agentName: string,
13
+ localTimeoutMs: number | undefined,
14
+ topLevelTimeoutMs: number | undefined,
15
+ defaultTimeoutMs: number,
16
+ ): number {
17
+ return (
18
+ localTimeoutMs ??
19
+ topLevelTimeoutMs ??
20
+ agents.find((agent) => agent.name === agentName)?.timeoutMs ??
21
+ defaultTimeoutMs
22
+ );
23
+ }
24
+
25
+ export function mergeTurnLimits(local: TurnLimits | undefined, fallback: TurnLimits): TurnLimits {
26
+ return {
27
+ idleTimeoutMs: local?.idleTimeoutMs ?? fallback.idleTimeoutMs,
28
+ maxTurns: local?.maxTurns ?? fallback.maxTurns,
29
+ maxToolCalls: local?.maxToolCalls ?? fallback.maxToolCalls,
30
+ };
31
+ }
32
+
33
+ export function calculateExecutionBudget(input: {
34
+ requestedTimeoutMs: number;
35
+ orchestrationDeadline?: number;
36
+ totalTimeoutMs?: number;
37
+ now: number;
38
+ }): ExecutionBudget | undefined {
39
+ if (input.orchestrationDeadline === undefined) {
40
+ return {
41
+ timeoutMs: input.requestedTimeoutMs,
42
+ workTimeoutReason: "work_timeout",
43
+ workTimeoutReportLimit: input.requestedTimeoutMs,
44
+ };
45
+ }
46
+ const remaining = Math.floor(input.orchestrationDeadline - input.now);
47
+ if (remaining < 1) return undefined;
48
+ const orchestrationLimited = remaining < input.requestedTimeoutMs;
49
+ return {
50
+ timeoutMs: Math.min(input.requestedTimeoutMs, remaining),
51
+ workTimeoutReason: orchestrationLimited ? "orchestration_timeout" : "work_timeout",
52
+ workTimeoutReportLimit: orchestrationLimited
53
+ ? Math.floor(input.totalTimeoutMs as number)
54
+ : input.requestedTimeoutMs,
55
+ };
56
+ }
@@ -0,0 +1,19 @@
1
+ export const FALLBACK_TIMEOUT_MS = 10 * 60 * 1000;
2
+
3
+ export function parsePositiveInteger(value: string | undefined): number | undefined {
4
+ if (!value) return undefined;
5
+ const parsed = Number.parseInt(value, 10);
6
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
7
+ }
8
+
9
+ export function resolveDefaultSubagentTimeoutMs(): number {
10
+ return parsePositiveInteger(process.env.PI_SUBAGENT_TIMEOUT_MS) ?? FALLBACK_TIMEOUT_MS;
11
+ }
12
+
13
+ export function assertSubagentDepthAllowed(): void {
14
+ const depth = Number.parseInt(process.env.PI_SUBAGENT_DEPTH ?? "0", 10) || 0;
15
+ const maxDepth = parsePositiveInteger(process.env.PI_SUBAGENT_MAX_DEPTH) ?? 1;
16
+ if (depth >= maxDepth) {
17
+ throw new Error(`Subagent recursion depth limit reached (${maxDepth})`);
18
+ }
19
+ }
@@ -8,7 +8,7 @@ import {
8
8
  type AgentSource,
9
9
  resolveAgentToolNames,
10
10
  type SubagentThinkingLevel,
11
- } from "./agents.js";
11
+ } from "./agents/types.js";
12
12
  import type { TargetPolicyAudit } from "./cwd-policy.js";
13
13
  import type { DelegationContract } from "./delegation-contract.js";
14
14
  import type { SubagentResultFormat } from "./result-contract.js";
@@ -1,4 +1,4 @@
1
- import type { SubagentThinkingLevel } from "./agents.js";
1
+ import type { SubagentThinkingLevel } from "./agents/types.js";
2
2
  import { readSubagentSettings, updateAgentSettingsPatch } from "./settings.js";
3
3
 
4
4
  export const EXECUTION_PROFILES = ["fast", "balanced", "deep"] as const;
@@ -1,5 +1,5 @@
1
1
  import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
- import type { AgentConfig, SubagentThinkingLevel } from "./agents.js";
2
+ import type { AgentConfig, SubagentThinkingLevel } from "./agents/types.js";
3
3
  import {
4
4
  applyExecutionProfile,
5
5
  EXECUTION_PROFILES,