@narumitw/pi-subagents 2.1.4 → 3.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 (312) hide show
  1. package/README.md +187 -1228
  2. package/dist/child-communication-bridge.ts +136 -0
  3. package/dist/child-communication-bridge.ts.map +7 -0
  4. package/dist/chunks/chunk-UKTWUQRM.js +667 -0
  5. package/dist/chunks/chunk-UKTWUQRM.js.map +7 -0
  6. package/dist/index.ts +1448 -1614
  7. package/dist/index.ts.map +4 -4
  8. package/docs/design-principles.md +31 -0
  9. package/docs/ipc-transport-idea.md +103 -0
  10. package/docs/tools.md +109 -0
  11. package/package.json +11 -19
  12. package/src/broker-credentials.ts +71 -0
  13. package/src/child-communication-bridge.ts +153 -0
  14. package/src/child-communication-tools.ts +147 -0
  15. package/src/completion-renderer.ts +60 -0
  16. package/src/index.ts +1 -1
  17. package/src/message-broker.ts +628 -0
  18. package/src/model-output.ts +42 -0
  19. package/src/process.ts +688 -0
  20. package/src/runtime.ts +538 -0
  21. package/src/subagents.ts +33 -34
  22. package/src/tools.ts +416 -0
  23. package/src/types.ts +85 -0
  24. package/src/widget.ts +130 -0
  25. package/dist/chunks/auto-transport-FUUKFDIG.ts +0 -109
  26. package/dist/chunks/auto-transport-FUUKFDIG.ts.map +0 -7
  27. package/dist/chunks/capability-grant-PR72SWWS.ts +0 -19
  28. package/dist/chunks/capability-grant-PR72SWWS.ts.map +0 -7
  29. package/dist/chunks/child-peer-bridge.ts +0 -108
  30. package/dist/chunks/child-peer-bridge.ts.map +0 -7
  31. package/dist/chunks/chunk-2LMJU25E.ts +0 -1641
  32. package/dist/chunks/chunk-2LMJU25E.ts.map +0 -7
  33. package/dist/chunks/chunk-47SK2QYC.ts +0 -56
  34. package/dist/chunks/chunk-47SK2QYC.ts.map +0 -7
  35. package/dist/chunks/chunk-4AQSF7AS.ts +0 -29
  36. package/dist/chunks/chunk-4AQSF7AS.ts.map +0 -7
  37. package/dist/chunks/chunk-5DGRMKNV.ts +0 -126
  38. package/dist/chunks/chunk-5DGRMKNV.ts.map +0 -7
  39. package/dist/chunks/chunk-6H6TBBED.ts +0 -108
  40. package/dist/chunks/chunk-6H6TBBED.ts.map +0 -7
  41. package/dist/chunks/chunk-6KJ34M6S.ts +0 -159
  42. package/dist/chunks/chunk-6KJ34M6S.ts.map +0 -7
  43. package/dist/chunks/chunk-6NSJVPXX.ts +0 -480
  44. package/dist/chunks/chunk-6NSJVPXX.ts.map +0 -7
  45. package/dist/chunks/chunk-7AAJEUSL.ts +0 -238
  46. package/dist/chunks/chunk-7AAJEUSL.ts.map +0 -7
  47. package/dist/chunks/chunk-BFK2Z4ZF.ts +0 -189
  48. package/dist/chunks/chunk-BFK2Z4ZF.ts.map +0 -7
  49. package/dist/chunks/chunk-BOAXY55Y.ts +0 -153
  50. package/dist/chunks/chunk-BOAXY55Y.ts.map +0 -7
  51. package/dist/chunks/chunk-D4CR7T73.ts +0 -353
  52. package/dist/chunks/chunk-D4CR7T73.ts.map +0 -7
  53. package/dist/chunks/chunk-FEVPWRMU.ts +0 -300
  54. package/dist/chunks/chunk-FEVPWRMU.ts.map +0 -7
  55. package/dist/chunks/chunk-G6VZTCFH.ts +0 -70
  56. package/dist/chunks/chunk-G6VZTCFH.ts.map +0 -7
  57. package/dist/chunks/chunk-H3FP6DLR.ts +0 -160
  58. package/dist/chunks/chunk-H3FP6DLR.ts.map +0 -7
  59. package/dist/chunks/chunk-HA36LPCO.ts +0 -130
  60. package/dist/chunks/chunk-HA36LPCO.ts.map +0 -7
  61. package/dist/chunks/chunk-HOP5FXDT.ts +0 -27
  62. package/dist/chunks/chunk-HOP5FXDT.ts.map +0 -7
  63. package/dist/chunks/chunk-IGAMVWFN.ts +0 -139
  64. package/dist/chunks/chunk-IGAMVWFN.ts.map +0 -7
  65. package/dist/chunks/chunk-ILEQ27AL.ts +0 -45
  66. package/dist/chunks/chunk-ILEQ27AL.ts.map +0 -7
  67. package/dist/chunks/chunk-ITVWPNU4.ts +0 -819
  68. package/dist/chunks/chunk-ITVWPNU4.ts.map +0 -7
  69. package/dist/chunks/chunk-IWC32VPY.ts +0 -172
  70. package/dist/chunks/chunk-IWC32VPY.ts.map +0 -7
  71. package/dist/chunks/chunk-JSZIP73U.ts +0 -362
  72. package/dist/chunks/chunk-JSZIP73U.ts.map +0 -7
  73. package/dist/chunks/chunk-JU6LUNLP.ts +0 -380
  74. package/dist/chunks/chunk-JU6LUNLP.ts.map +0 -7
  75. package/dist/chunks/chunk-KMGKCEO4.ts +0 -38
  76. package/dist/chunks/chunk-KMGKCEO4.ts.map +0 -7
  77. package/dist/chunks/chunk-LASD73CM.ts +0 -339
  78. package/dist/chunks/chunk-LASD73CM.ts.map +0 -7
  79. package/dist/chunks/chunk-LEOYDZI3.ts +0 -22
  80. package/dist/chunks/chunk-LEOYDZI3.ts.map +0 -7
  81. package/dist/chunks/chunk-LL4LP2T7.ts +0 -737
  82. package/dist/chunks/chunk-LL4LP2T7.ts.map +0 -7
  83. package/dist/chunks/chunk-N2T5IN4X.ts +0 -18
  84. package/dist/chunks/chunk-N2T5IN4X.ts.map +0 -7
  85. package/dist/chunks/chunk-N7BLVXKK.ts +0 -73
  86. package/dist/chunks/chunk-N7BLVXKK.ts.map +0 -7
  87. package/dist/chunks/chunk-NLT67IZS.ts +0 -322
  88. package/dist/chunks/chunk-NLT67IZS.ts.map +0 -7
  89. package/dist/chunks/chunk-NTRPLF46.ts +0 -63
  90. package/dist/chunks/chunk-NTRPLF46.ts.map +0 -7
  91. package/dist/chunks/chunk-ONDTY4EL.ts +0 -91
  92. package/dist/chunks/chunk-ONDTY4EL.ts.map +0 -7
  93. package/dist/chunks/chunk-OVPHFGKO.ts +0 -1769
  94. package/dist/chunks/chunk-OVPHFGKO.ts.map +0 -7
  95. package/dist/chunks/chunk-P7OH4XMF.ts +0 -60
  96. package/dist/chunks/chunk-P7OH4XMF.ts.map +0 -7
  97. package/dist/chunks/chunk-PBZMBTNJ.ts +0 -422
  98. package/dist/chunks/chunk-PBZMBTNJ.ts.map +0 -7
  99. package/dist/chunks/chunk-PGLSFLYW.ts +0 -52
  100. package/dist/chunks/chunk-PGLSFLYW.ts.map +0 -7
  101. package/dist/chunks/chunk-RRR66UWR.ts +0 -28
  102. package/dist/chunks/chunk-RRR66UWR.ts.map +0 -7
  103. package/dist/chunks/chunk-RSUXZD6S.ts +0 -275
  104. package/dist/chunks/chunk-RSUXZD6S.ts.map +0 -7
  105. package/dist/chunks/chunk-RTCVYIZA.ts +0 -221
  106. package/dist/chunks/chunk-RTCVYIZA.ts.map +0 -7
  107. package/dist/chunks/chunk-RY2AEGMZ.ts +0 -34
  108. package/dist/chunks/chunk-RY2AEGMZ.ts.map +0 -7
  109. package/dist/chunks/chunk-SWGQLFSD.ts +0 -60
  110. package/dist/chunks/chunk-SWGQLFSD.ts.map +0 -7
  111. package/dist/chunks/chunk-TM2R67J3.ts +0 -52
  112. package/dist/chunks/chunk-TM2R67J3.ts.map +0 -7
  113. package/dist/chunks/chunk-TMZRHIIK.ts +0 -497
  114. package/dist/chunks/chunk-TMZRHIIK.ts.map +0 -7
  115. package/dist/chunks/chunk-TZ34IQ3M.ts +0 -59
  116. package/dist/chunks/chunk-TZ34IQ3M.ts.map +0 -7
  117. package/dist/chunks/chunk-UMBPMVJW.ts +0 -22
  118. package/dist/chunks/chunk-UMBPMVJW.ts.map +0 -7
  119. package/dist/chunks/chunk-VDG7LTYE.ts +0 -80
  120. package/dist/chunks/chunk-VDG7LTYE.ts.map +0 -7
  121. package/dist/chunks/chunk-X4NMONPE.ts +0 -73
  122. package/dist/chunks/chunk-X4NMONPE.ts.map +0 -7
  123. package/dist/chunks/chunk-YPJEN6NU.ts +0 -65
  124. package/dist/chunks/chunk-YPJEN6NU.ts.map +0 -7
  125. package/dist/chunks/chunk-YU53SHA7.ts +0 -328
  126. package/dist/chunks/chunk-YU53SHA7.ts.map +0 -7
  127. package/dist/chunks/completion-delivery-RSJU6BXL.ts +0 -17
  128. package/dist/chunks/completion-delivery-RSJU6BXL.ts.map +0 -7
  129. package/dist/chunks/config-status-FKGDECZ3.ts +0 -35
  130. package/dist/chunks/config-status-FKGDECZ3.ts.map +0 -7
  131. package/dist/chunks/config-ui-ABHYNGQ7.ts +0 -1212
  132. package/dist/chunks/config-ui-ABHYNGQ7.ts.map +0 -7
  133. package/dist/chunks/consult-LJU3IQY5.ts +0 -519
  134. package/dist/chunks/consult-LJU3IQY5.ts.map +0 -7
  135. package/dist/chunks/context-QDKLVQXE.ts +0 -11
  136. package/dist/chunks/context-QDKLVQXE.ts.map +0 -7
  137. package/dist/chunks/create-stateful-transport-JWC2EFYL.ts +0 -130
  138. package/dist/chunks/create-stateful-transport-JWC2EFYL.ts.map +0 -7
  139. package/dist/chunks/cwd-policy-NB6XZ5GF.ts +0 -17
  140. package/dist/chunks/cwd-policy-NB6XZ5GF.ts.map +0 -7
  141. package/dist/chunks/delegation-contract-LA56I5DT.ts +0 -24
  142. package/dist/chunks/delegation-contract-LA56I5DT.ts.map +0 -7
  143. package/dist/chunks/discovery-MIFB2U4Y.ts +0 -12
  144. package/dist/chunks/discovery-MIFB2U4Y.ts.map +0 -7
  145. package/dist/chunks/execution-Q2JZLKJA.ts +0 -4037
  146. package/dist/chunks/execution-Q2JZLKJA.ts.map +0 -7
  147. package/dist/chunks/in-process-transport-HJ6TZXC3.ts +0 -39
  148. package/dist/chunks/in-process-transport-HJ6TZXC3.ts.map +0 -7
  149. package/dist/chunks/inspect-UH2TKH6E.ts +0 -634
  150. package/dist/chunks/inspect-UH2TKH6E.ts.map +0 -7
  151. package/dist/chunks/peer-communication-KL36Z6CO.ts +0 -294
  152. package/dist/chunks/peer-communication-KL36Z6CO.ts.map +0 -7
  153. package/dist/chunks/persistence-UY3PY6E5.ts +0 -348
  154. package/dist/chunks/persistence-UY3PY6E5.ts.map +0 -7
  155. package/dist/chunks/registry-BT54L6CY.ts +0 -1445
  156. package/dist/chunks/registry-BT54L6CY.ts.map +0 -7
  157. package/dist/chunks/retained-semantic-state-GM75FZGE.ts +0 -81
  158. package/dist/chunks/retained-semantic-state-GM75FZGE.ts.map +0 -7
  159. package/dist/chunks/rpc-transport-7R7DVCEB.ts +0 -1222
  160. package/dist/chunks/rpc-transport-7R7DVCEB.ts.map +0 -7
  161. package/dist/chunks/runtime-policy-5YELCOVC.ts +0 -15
  162. package/dist/chunks/runtime-policy-5YELCOVC.ts.map +0 -7
  163. package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts +0 -19
  164. package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts.map +0 -7
  165. package/dist/chunks/spawn-idempotency-BNHOSMZW.ts +0 -13
  166. package/dist/chunks/spawn-idempotency-BNHOSMZW.ts.map +0 -7
  167. package/dist/chunks/stateful-lifecycle-JAR6K55H.ts +0 -15
  168. package/dist/chunks/stateful-lifecycle-JAR6K55H.ts.map +0 -7
  169. package/dist/chunks/subprocess-transport-VHZJRBTW.ts +0 -160
  170. package/dist/chunks/subprocess-transport-VHZJRBTW.ts.map +0 -7
  171. package/dist/chunks/usage-recording-store-CW3EH2SZ.ts +0 -164
  172. package/dist/chunks/usage-recording-store-CW3EH2SZ.ts.map +0 -7
  173. package/dist/chunks/workspace-WDKLN72J.ts +0 -11
  174. package/dist/chunks/workspace-WDKLN72J.ts.map +0 -7
  175. package/docs/async-runtime-protocol.md +0 -84
  176. package/docs/implementation-notes/pi-subagents-capability-matrix.md +0 -62
  177. package/docs/implementation-notes/pi-subagents-current-direction.md +0 -99
  178. package/docs/implementation-notes/pi-subagents-rpc-v1.md +0 -139
  179. package/docs/pi-subagents-diagrams.md +0 -183
  180. package/src/adaptive-scheduler.ts +0 -224
  181. package/src/admission-policy.ts +0 -78
  182. package/src/agent-projection.ts +0 -53
  183. package/src/agents/built-ins.ts +0 -71
  184. package/src/agents/catalog.ts +0 -241
  185. package/src/agents/discovery.ts +0 -265
  186. package/src/agents/types.ts +0 -100
  187. package/src/agents.ts +0 -51
  188. package/src/auto-transport.ts +0 -121
  189. package/src/blocking-status.ts +0 -63
  190. package/src/cached-module-loader.ts +0 -18
  191. package/src/capabilities.ts +0 -145
  192. package/src/capability-grant.ts +0 -115
  193. package/src/capability-router.ts +0 -107
  194. package/src/child-peer-bridge.ts +0 -124
  195. package/src/child-peer-tools.ts +0 -132
  196. package/src/completion-delivery.ts +0 -409
  197. package/src/completion-render.ts +0 -190
  198. package/src/completion-requirement.ts +0 -479
  199. package/src/completion-routing.ts +0 -24
  200. package/src/config-registration.ts +0 -114
  201. package/src/config-status.ts +0 -217
  202. package/src/config-ui.ts +0 -983
  203. package/src/consult-policy.ts +0 -15
  204. package/src/consult-registration.ts +0 -125
  205. package/src/consult-render.ts +0 -194
  206. package/src/consult-resources.ts +0 -51
  207. package/src/consult-tool.ts +0 -95
  208. package/src/consult.ts +0 -566
  209. package/src/context.ts +0 -126
  210. package/src/create-stateful-transport.ts +0 -157
  211. package/src/cwd-policy.ts +0 -183
  212. package/src/delegation-contract.ts +0 -463
  213. package/src/execution/budget.ts +0 -56
  214. package/src/execution/runtime-policy.ts +0 -19
  215. package/src/execution-plan.ts +0 -322
  216. package/src/execution-ui.ts +0 -259
  217. package/src/execution.ts +0 -1679
  218. package/src/in-process-transport.ts +0 -941
  219. package/src/inspect-registration.ts +0 -64
  220. package/src/inspect-render.ts +0 -334
  221. package/src/inspect-tool.ts +0 -45
  222. package/src/inspect.ts +0 -769
  223. package/src/integration-controller.ts +0 -98
  224. package/src/limits.ts +0 -75
  225. package/src/orchestration-metrics.ts +0 -116
  226. package/src/outcome.ts +0 -61
  227. package/src/panel-child-group.ts +0 -35
  228. package/src/panel-contract.ts +0 -343
  229. package/src/panel-evidence.ts +0 -59
  230. package/src/panel-execution.ts +0 -769
  231. package/src/panel-failure.ts +0 -56
  232. package/src/panel-planning.ts +0 -175
  233. package/src/panel-presets.ts +0 -3
  234. package/src/panel-prompts.ts +0 -132
  235. package/src/panel-reconciliation.ts +0 -57
  236. package/src/panel-render.ts +0 -103
  237. package/src/parallel-limit-ui.ts +0 -113
  238. package/src/params.ts +0 -278
  239. package/src/peer-communication.ts +0 -352
  240. package/src/peer-transport.ts +0 -49
  241. package/src/persistence.ts +0 -522
  242. package/src/pi-args.ts +0 -43
  243. package/src/pi-invocation.ts +0 -168
  244. package/src/process-control.ts +0 -43
  245. package/src/prompt-resources.ts +0 -38
  246. package/src/prompt-source-safety.ts +0 -48
  247. package/src/protocol.ts +0 -76
  248. package/src/registry-types.ts +0 -204
  249. package/src/registry.ts +0 -1698
  250. package/src/render-common.ts +0 -252
  251. package/src/render.ts +0 -704
  252. package/src/result-contract.ts +0 -431
  253. package/src/retained-semantic-state.ts +0 -100
  254. package/src/rpc-timeout-finalization.ts +0 -207
  255. package/src/rpc-transport-metadata.ts +0 -65
  256. package/src/rpc-transport.ts +0 -1035
  257. package/src/rpc-turn-capture.ts +0 -217
  258. package/src/runner-outcome.ts +0 -31
  259. package/src/runner-result.ts +0 -55
  260. package/src/runner-types.ts +0 -102
  261. package/src/runner-usage.ts +0 -48
  262. package/src/runner.ts +0 -866
  263. package/src/safe-text.ts +0 -67
  264. package/src/semantic-snapshot.ts +0 -214
  265. package/src/session-guidance-contract.ts +0 -399
  266. package/src/settings/inspection.ts +0 -302
  267. package/src/settings/schema.ts +0 -195
  268. package/src/settings-reader.ts +0 -215
  269. package/src/settings.ts +0 -452
  270. package/src/spawn-idempotency.ts +0 -68
  271. package/src/stateful-agent-view.ts +0 -85
  272. package/src/stateful-config.ts +0 -13
  273. package/src/stateful-guidance.ts +0 -29
  274. package/src/stateful-lifecycle.ts +0 -74
  275. package/src/stateful-limit-ui.ts +0 -249
  276. package/src/stateful-limits.ts +0 -96
  277. package/src/stateful-prompt.ts +0 -49
  278. package/src/stateful-registration.ts +0 -1320
  279. package/src/stateful-render.ts +0 -318
  280. package/src/stateful-safety.ts +0 -47
  281. package/src/stateful-tool-params.ts +0 -215
  282. package/src/stateful.ts +0 -12
  283. package/src/subagent-details.ts +0 -43
  284. package/src/subagents-extension.ts +0 -372
  285. package/src/subprocess-transport.ts +0 -145
  286. package/src/supervision.ts +0 -104
  287. package/src/task-path.ts +0 -65
  288. package/src/timeout-checkpoint.ts +0 -305
  289. package/src/timeout-finalization.ts +0 -75
  290. package/src/tool-schema-compatibility.ts +0 -73
  291. package/src/transport-types.ts +0 -74
  292. package/src/transport-ui.ts +0 -135
  293. package/src/transport.ts +0 -43
  294. package/src/turn-budget.ts +0 -109
  295. package/src/usage-format.ts +0 -42
  296. package/src/usage-recording-config.ts +0 -13
  297. package/src/usage-recording-store.ts +0 -183
  298. package/src/usage-recording.ts +0 -478
  299. package/src/verification-harness.ts +0 -516
  300. package/src/verification-policy.ts +0 -67
  301. package/src/verification-receipt.ts +0 -275
  302. package/src/verified-execution-benchmark.ts +0 -86
  303. package/src/verified-execution-contract.ts +0 -191
  304. package/src/verified-execution-schema.ts +0 -32
  305. package/src/work-item-ledger.ts +0 -1404
  306. package/src/work-item-persistence.ts +0 -254
  307. package/src/workflow-completion-controller.ts +0 -397
  308. package/src/workflow-planning.ts +0 -172
  309. package/src/workflow-tree-identity.ts +0 -289
  310. package/src/workflow-ui.ts +0 -69
  311. package/src/workflow-verification.ts +0 -296
  312. package/src/workspace.ts +0 -174
@@ -1,74 +0,0 @@
1
- import type { AgentRegistry, ManagedAgent } from "./registry.js";
2
- import type { WorkspaceManager } from "./workspace.js";
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
-
22
- export async function disposeStatefulRuntime(
23
- registry: AgentRegistry | undefined,
24
- workspaceManager: WorkspaceManager,
25
- ): Promise<unknown[]> {
26
- const errors: unknown[] = [];
27
- try {
28
- await registry?.shutdown();
29
- } catch (error) {
30
- errors.push(error);
31
- }
32
- try {
33
- await workspaceManager.cleanupAll();
34
- } catch (error) {
35
- errors.push(error);
36
- }
37
- return errors;
38
- }
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
-
61
- export function assertCurrentSpawn(
62
- signal: AbortSignal | undefined,
63
- generation: number,
64
- currentGeneration: number,
65
- ): void {
66
- if (!signal?.aborted && generation === currentGeneration) return;
67
- throw ownedSpawnAbortError();
68
- }
69
-
70
- function ownedSpawnAbortError(): Error {
71
- const error = new Error("Subagent spawn owner was replaced or aborted");
72
- error.name = "AbortError";
73
- return error;
74
- }
@@ -1,249 +0,0 @@
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: "Background Agent Limits",
32
- lines: [
33
- "These advanced limits apply after /reload or the next Pi session.",
34
- "Reloading can interrupt subagents saved for follow-up.",
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]} · after reload: ${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
- `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
- `After reload, this value could exclude ${affectedBefore.length} saved subagent record${affectedBefore.length === 1 ? "" : "s"} from recovery.`,
118
- "No subagent is removed now, and this menu will not reload Pi.",
119
- "A later state save can make excluded 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(
131
- "Current subagents changed while confirming; review the limit again.",
132
- "warning",
133
- );
134
- return { kind: "rejected" as const };
135
- }
136
- }
137
-
138
- if (options.signal.aborted || !options.isCurrent()) return { kind: "close" as const };
139
- try {
140
- updateStatefulLimitSetting(field, next, expected);
141
- } catch (error) {
142
- if (options.isCurrent() && !options.signal.aborted) {
143
- ctx.ui.notify(
144
- `Background-agent limit was not saved; the previous setting is unchanged: ${formatError(error)}`,
145
- "error",
146
- );
147
- }
148
- return { kind: "rejected" as const };
149
- }
150
- if (options.signal.aborted || !options.isCurrent()) return { kind: "close" as const };
151
- ctx.ui.notify(
152
- `Saved ${statefulLimitDefinition(field).label.toLowerCase()}: ${next}. Applies after /reload; clear current subagents first if their work must not be interrupted.`,
153
- "info",
154
- );
155
- return { kind: "back" as const };
156
- }
157
-
158
- export function formatDetachedLimitSummary(status: StatefulSubagentRuntimeStatus): string {
159
- return [
160
- `${status.limits.maxAgents} saved`,
161
- `${status.limits.maxActiveTurns} working at once`,
162
- `${status.limits.maxChildrenPerAgent} children each`,
163
- `${status.limits.maxDepth} nested levels`,
164
- `${status.limits.maxStoredAgents} stored records`,
165
- ].join(" · ");
166
- }
167
-
168
- export function formatConfiguredDetachedLimitDivergence(
169
- status: StatefulSubagentRuntimeStatus,
170
- values: NonNullable<ReturnType<typeof inspectStatefulLimitSettings>["values"]>,
171
- ): string | undefined {
172
- const changed = STATEFUL_LIMIT_DEFINITIONS.flatMap((definition) => {
173
- const configured = values[definition.field].value;
174
- return configured === status.limits[definition.field]
175
- ? []
176
- : [`${definition.label.toLowerCase()} ${configured}`];
177
- });
178
- return changed.length > 0 ? `After /reload: ${changed.join(" · ")}` : undefined;
179
- }
180
-
181
- export function formatConfiguredDetachedLimits(
182
- values: NonNullable<ReturnType<typeof inspectStatefulLimitSettings>["values"]>,
183
- ): string {
184
- return STATEFUL_LIMIT_DEFINITIONS.map((definition) => {
185
- const snapshot = values[definition.field];
186
- return `${definition.label.toLowerCase()} ${snapshot.value} (${snapshot.source})`;
187
- }).join(" · ");
188
- }
189
-
190
- export function formatEmptyStatefulRuntime(status: StatefulSubagentRuntimeStatus): string {
191
- if (!status.enabled) return "Background subagents are disabled in How subagents run.";
192
- if (!status.initialized) return "Background subagents have not started in this session.";
193
- return "No subagents are working or saved for follow-up in this session.";
194
- }
195
-
196
- function parseStatefulLimit(
197
- field: StatefulLimitField,
198
- value: string | undefined,
199
- ): number | undefined {
200
- const normalized = value?.trim() ?? "";
201
- if (!/^\d+$/u.test(normalized)) return undefined;
202
- const parsed = Number(normalized);
203
- return isValidStatefulLimit(field, parsed) ? parsed : undefined;
204
- }
205
-
206
- function notifyValidationError(field: StatefulLimitField, ctx: ExtensionCommandContext): void {
207
- const definition = statefulLimitDefinition(field);
208
- ctx.ui.notify(
209
- `${definition.label} must be a safe whole number ${definition.minimum === 0 ? "0 or greater" : "1 or greater"}.`,
210
- "warning",
211
- );
212
- }
213
-
214
- function configuredValues(
215
- values: NonNullable<ReturnType<typeof inspectStatefulLimitSettings>["values"]>,
216
- ): StatefulLimits {
217
- return resolveStatefulLimits(
218
- Object.fromEntries(Object.entries(values).map(([field, snapshot]) => [field, snapshot.value])),
219
- );
220
- }
221
-
222
- function projectedRemovedAgentIds(
223
- agents: readonly ManagedAgent[],
224
- current: StatefulLimits,
225
- next: StatefulLimits,
226
- ): string[] {
227
- const restorable = agents.filter(
228
- (agent) => agent.state !== "closed" && agent.workspaceMode !== "worktree",
229
- );
230
- const before = projectForReload(restorable, current);
231
- const afterIds = new Set(projectForReload(restorable, next).map((agent) => agent.id));
232
- return before.filter((agent) => !afterIds.has(agent.id)).map((agent) => agent.id);
233
- }
234
-
235
- function projectForReload(agents: readonly ManagedAgent[], limits: StatefulLimits): ManagedAgent[] {
236
- const stored = projectAgentRecords(agents, { maxAgents: limits.maxStoredAgents });
237
- return projectAgentRecords(stored, {
238
- maxAgents: limits.maxAgents,
239
- maxDepth: limits.maxDepth,
240
- });
241
- }
242
-
243
- function sameIds(left: readonly string[], right: readonly string[]): boolean {
244
- return left.length === right.length && left.every((id, index) => id === right[index]);
245
- }
246
-
247
- function formatError(error: unknown): string {
248
- return safeTerminalText(error instanceof Error ? error.message : String(error));
249
- }
@@ -1,96 +0,0 @@
1
- import type { SubagentRuntimeSettings } from "./agents/types.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: "Subagents saved for follow-up",
33
- description: "Working, queued, and reusable background subagents kept in one session",
34
- defaultValue: 16,
35
- minimum: 1,
36
- },
37
- {
38
- field: "maxActiveTurns",
39
- label: "Subagents working at once",
40
- description: "Background subagent turns that may run at the same time",
41
- defaultValue: 4,
42
- minimum: 1,
43
- },
44
- {
45
- field: "maxChildrenPerAgent",
46
- label: "Direct children per subagent",
47
- description: "Subagents that one parent subagent may keep for follow-up",
48
- defaultValue: 8,
49
- minimum: 1,
50
- },
51
- {
52
- field: "maxDepth",
53
- label: "Nested subagent levels",
54
- description: "Child levels allowed below a top-level subagent",
55
- defaultValue: 3,
56
- minimum: 0,
57
- },
58
- {
59
- field: "maxStoredAgents",
60
- label: "Stored subagent records",
61
- description: "Background subagent records kept on disk for each session",
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
- }
@@ -1,49 +0,0 @@
1
- import type { AgentConfig } from "./agents/types.js";
2
- import { redactPrivateText } from "./context.js";
3
- import { appendDelegationContract } from "./delegation-contract.js";
4
- import { resolveDefaultSubagentTimeoutMs } from "./execution/runtime-policy.js";
5
- import { DEFAULT_MAX_CONTEXT_BYTES, truncateUtf8 } from "./limits.js";
6
- import type { ManagedAgent } from "./registry.js";
7
- import { appendResultInstruction } from "./result-contract.js";
8
-
9
- export function buildStatefulTurnPrompt(
10
- record: Pick<
11
- ManagedAgent,
12
- "context" | "history" | "mailbox" | "currentMailboxMessageIds" | "contract" | "resultFormat"
13
- >,
14
- task: string,
15
- maxBytes = DEFAULT_MAX_CONTEXT_BYTES,
16
- ): { text: string; truncated: boolean } {
17
- const previous = record.history
18
- .map((turn) => {
19
- const redactedTask = redactPrivateText(turn.task);
20
- const redactedOutput = redactPrivateText(turn.output);
21
- return `Task: ${redactedTask}\nOutput: ${redactedOutput}`;
22
- })
23
- .join("\n\n");
24
- const currentMessageIds = new Set(record.currentMailboxMessageIds ?? []);
25
- const messages = record.mailbox
26
- .filter((message) => currentMessageIds.has(message.id))
27
- .slice(-20)
28
- .map((message) => `From ${message.senderId}: ${redactPrivateText(message.content)}`)
29
- .join("\n");
30
- const context = [
31
- `Current task:\n${redactPrivateText(task)}`,
32
- messages ? `Mailbox messages:\n${messages}` : "",
33
- previous ? `Prior subagent turns:\n${previous}` : "",
34
- record.context ? `Parent context:\n${redactPrivateText(record.context)}` : "",
35
- ]
36
- .filter(Boolean)
37
- .join("\n\n---\n\n");
38
- const contracted = appendDelegationContract(context, record.contract, maxBytes);
39
- return truncateUtf8(
40
- appendResultInstruction(contracted.text, record.resultFormat, maxBytes),
41
- maxBytes,
42
- );
43
- }
44
-
45
- export function resolveStatefulTurnTimeout(
46
- agent: Pick<AgentConfig, "timeoutMs"> | undefined,
47
- ): number {
48
- return agent?.timeoutMs ?? resolveDefaultSubagentTimeoutMs();
49
- }