@narumitw/pi-subagents 2.1.3 → 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 (314) hide show
  1. package/README.md +181 -1250
  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 -1598
  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 -153
  179. package/docs/pi-subagents-diagrams.md +0 -183
  180. package/src/adaptive-scheduler.ts +0 -224
  181. package/src/admission-benchmark.ts +0 -95
  182. package/src/admission-policy.ts +0 -78
  183. package/src/agent-projection.ts +0 -53
  184. package/src/agents/built-ins.ts +0 -71
  185. package/src/agents/catalog.ts +0 -241
  186. package/src/agents/discovery.ts +0 -265
  187. package/src/agents/types.ts +0 -100
  188. package/src/agents.ts +0 -51
  189. package/src/async-subagent-benchmark.ts +0 -532
  190. package/src/auto-transport.ts +0 -121
  191. package/src/blocking-status.ts +0 -63
  192. package/src/cached-module-loader.ts +0 -18
  193. package/src/capabilities.ts +0 -145
  194. package/src/capability-grant.ts +0 -115
  195. package/src/capability-router.ts +0 -107
  196. package/src/child-peer-bridge.ts +0 -124
  197. package/src/child-peer-tools.ts +0 -132
  198. package/src/completion-delivery.ts +0 -409
  199. package/src/completion-render.ts +0 -190
  200. package/src/completion-requirement.ts +0 -479
  201. package/src/completion-routing.ts +0 -24
  202. package/src/config-registration.ts +0 -114
  203. package/src/config-status.ts +0 -217
  204. package/src/config-ui.ts +0 -983
  205. package/src/consult-policy.ts +0 -15
  206. package/src/consult-registration.ts +0 -125
  207. package/src/consult-render.ts +0 -194
  208. package/src/consult-resources.ts +0 -51
  209. package/src/consult-tool.ts +0 -95
  210. package/src/consult.ts +0 -566
  211. package/src/context.ts +0 -126
  212. package/src/create-stateful-transport.ts +0 -157
  213. package/src/cwd-policy.ts +0 -183
  214. package/src/delegation-contract.ts +0 -463
  215. package/src/execution/budget.ts +0 -56
  216. package/src/execution/runtime-policy.ts +0 -19
  217. package/src/execution-plan.ts +0 -322
  218. package/src/execution-ui.ts +0 -259
  219. package/src/execution.ts +0 -1679
  220. package/src/in-process-transport.ts +0 -941
  221. package/src/inspect-registration.ts +0 -64
  222. package/src/inspect-render.ts +0 -334
  223. package/src/inspect-tool.ts +0 -45
  224. package/src/inspect.ts +0 -769
  225. package/src/integration-controller.ts +0 -98
  226. package/src/limits.ts +0 -75
  227. package/src/orchestration-metrics.ts +0 -116
  228. package/src/outcome.ts +0 -61
  229. package/src/panel-child-group.ts +0 -35
  230. package/src/panel-contract.ts +0 -343
  231. package/src/panel-evidence.ts +0 -59
  232. package/src/panel-execution.ts +0 -769
  233. package/src/panel-failure.ts +0 -56
  234. package/src/panel-planning.ts +0 -175
  235. package/src/panel-presets.ts +0 -3
  236. package/src/panel-prompts.ts +0 -132
  237. package/src/panel-reconciliation.ts +0 -57
  238. package/src/panel-render.ts +0 -103
  239. package/src/parallel-limit-ui.ts +0 -113
  240. package/src/params.ts +0 -278
  241. package/src/peer-communication.ts +0 -352
  242. package/src/peer-transport.ts +0 -49
  243. package/src/persistence.ts +0 -522
  244. package/src/pi-args.ts +0 -43
  245. package/src/pi-invocation.ts +0 -168
  246. package/src/process-control.ts +0 -43
  247. package/src/prompt-resources.ts +0 -38
  248. package/src/prompt-source-safety.ts +0 -48
  249. package/src/protocol.ts +0 -76
  250. package/src/registry-types.ts +0 -204
  251. package/src/registry.ts +0 -1698
  252. package/src/render-common.ts +0 -252
  253. package/src/render.ts +0 -704
  254. package/src/result-contract.ts +0 -431
  255. package/src/retained-semantic-state.ts +0 -100
  256. package/src/rpc-timeout-finalization.ts +0 -207
  257. package/src/rpc-transport-metadata.ts +0 -65
  258. package/src/rpc-transport.ts +0 -1035
  259. package/src/rpc-turn-capture.ts +0 -217
  260. package/src/runner-outcome.ts +0 -31
  261. package/src/runner-result.ts +0 -55
  262. package/src/runner-types.ts +0 -102
  263. package/src/runner-usage.ts +0 -48
  264. package/src/runner.ts +0 -866
  265. package/src/safe-text.ts +0 -67
  266. package/src/semantic-snapshot.ts +0 -214
  267. package/src/session-guidance-contract.ts +0 -399
  268. package/src/settings/inspection.ts +0 -302
  269. package/src/settings/schema.ts +0 -195
  270. package/src/settings-reader.ts +0 -215
  271. package/src/settings.ts +0 -452
  272. package/src/spawn-idempotency.ts +0 -68
  273. package/src/stateful-agent-view.ts +0 -85
  274. package/src/stateful-config.ts +0 -13
  275. package/src/stateful-guidance.ts +0 -29
  276. package/src/stateful-lifecycle.ts +0 -74
  277. package/src/stateful-limit-ui.ts +0 -249
  278. package/src/stateful-limits.ts +0 -96
  279. package/src/stateful-prompt.ts +0 -49
  280. package/src/stateful-registration.ts +0 -1320
  281. package/src/stateful-render.ts +0 -318
  282. package/src/stateful-safety.ts +0 -47
  283. package/src/stateful-tool-params.ts +0 -215
  284. package/src/stateful.ts +0 -12
  285. package/src/subagent-details.ts +0 -43
  286. package/src/subagents-extension.ts +0 -355
  287. package/src/subprocess-transport.ts +0 -145
  288. package/src/supervision.ts +0 -104
  289. package/src/task-path.ts +0 -65
  290. package/src/timeout-checkpoint.ts +0 -305
  291. package/src/timeout-finalization.ts +0 -75
  292. package/src/tool-schema-compatibility.ts +0 -73
  293. package/src/transport-types.ts +0 -74
  294. package/src/transport-ui.ts +0 -135
  295. package/src/transport.ts +0 -43
  296. package/src/turn-budget.ts +0 -109
  297. package/src/usage-format.ts +0 -42
  298. package/src/usage-recording-config.ts +0 -13
  299. package/src/usage-recording-store.ts +0 -183
  300. package/src/usage-recording.ts +0 -478
  301. package/src/verification-harness.ts +0 -516
  302. package/src/verification-policy.ts +0 -67
  303. package/src/verification-receipt.ts +0 -275
  304. package/src/verified-execution-benchmark.ts +0 -86
  305. package/src/verified-execution-contract.ts +0 -191
  306. package/src/verified-execution-schema.ts +0 -32
  307. package/src/work-item-ledger.ts +0 -1404
  308. package/src/work-item-persistence.ts +0 -254
  309. package/src/workflow-completion-controller.ts +0 -397
  310. package/src/workflow-planning.ts +0 -172
  311. package/src/workflow-tree-identity.ts +0 -289
  312. package/src/workflow-ui.ts +0 -69
  313. package/src/workflow-verification.ts +0 -296
  314. package/src/workspace.ts +0 -174
@@ -1,217 +0,0 @@
1
- import { DEFAULT_MAX_OUTPUT_BYTES, truncateUtf8 } from "./limits.js";
2
- import { boundedPrivateText } from "./safe-text.js";
3
- import { journalMessages, TimeoutProgressJournal } from "./timeout-checkpoint.js";
4
- import { emptyTransportUsage, type TransportUsage } from "./transport-types.js";
5
- import type { TurnBudgetMonitor } from "./turn-budget.js";
6
-
7
- export interface RpcTurnCapture {
8
- output: string;
9
- partial: string;
10
- stopReason?: string;
11
- error?: string;
12
- provider?: string;
13
- model?: string;
14
- usage: TransportUsage;
15
- inFlightUsage?: TransportUsage;
16
- firstActivityAt?: number;
17
- journal: TimeoutProgressJournal;
18
- }
19
-
20
- export function createRpcTurnCapture(): RpcTurnCapture {
21
- return {
22
- output: "",
23
- partial: "",
24
- usage: emptyTransportUsage(),
25
- journal: new TimeoutProgressJournal(),
26
- };
27
- }
28
-
29
- export function captureRpcEvent(event: unknown, capture: RpcTurnCapture): boolean {
30
- if (!isRecord(event)) return false;
31
- const previousUsage = snapshotRpcUsage(capture);
32
- if (event.type === "tool_execution_start") {
33
- capture.journal.recordToolCall(
34
- typeof event.toolCallId === "string" ? event.toolCallId : "",
35
- typeof event.toolName === "string" ? event.toolName : "tool",
36
- isRecord(event.args) ? event.args : {},
37
- );
38
- }
39
- if (event.type === "tool_execution_end") {
40
- const result = isRecord(event.result) ? event.result : {};
41
- capture.journal.recordToolResult(
42
- typeof event.toolCallId === "string" ? event.toolCallId : "",
43
- typeof event.toolName === "string" ? event.toolName : "tool",
44
- { content: result.content, isError: event.isError },
45
- );
46
- }
47
- if (event.type === "message_start" && isRecord(event.message)) {
48
- if (event.message.role === "assistant") commitRpcInFlightUsage(capture);
49
- }
50
- if (event.type === "message_update") {
51
- const usage = normalizeUsage(event.usage);
52
- if (usage) capture.inFlightUsage = usage;
53
- const delta = event.assistantMessageEvent;
54
- if (isRecord(delta) && delta.type === "text_delta" && typeof delta.delta === "string") {
55
- capture.partial = truncateUtf8(
56
- `${capture.partial}${delta.delta}`,
57
- DEFAULT_MAX_OUTPUT_BYTES,
58
- ).text;
59
- }
60
- }
61
- if (event.type === "message_end" && isRecord(event.message)) {
62
- const candidate = event.message;
63
- if (candidate.role !== "toolResult") journalMessages(capture.journal, [candidate]);
64
- if (candidate.role === "assistant") captureAssistantEnd(candidate, capture);
65
- }
66
- return !sameUsage(previousUsage, snapshotRpcUsage(capture));
67
- }
68
-
69
- export function snapshotRpcUsage(capture: RpcTurnCapture): TransportUsage {
70
- const usage = { ...capture.usage };
71
- if (capture.inFlightUsage) addUsage(usage, capture.inFlightUsage);
72
- return usage;
73
- }
74
-
75
- export function commitRpcInFlightUsage(capture: RpcTurnCapture): boolean {
76
- if (!capture.inFlightUsage) return false;
77
- addUsage(capture.usage, capture.inFlightUsage);
78
- capture.inFlightUsage = undefined;
79
- return true;
80
- }
81
-
82
- function captureAssistantEnd(candidate: Record<string, unknown>, capture: RpcTurnCapture): void {
83
- capture.output = truncateUtf8(
84
- assistantText(candidate.content) || capture.partial,
85
- DEFAULT_MAX_OUTPUT_BYTES,
86
- ).text;
87
- capture.partial = capture.output;
88
- capture.stopReason = typeof candidate.stopReason === "string" ? candidate.stopReason : undefined;
89
- capture.error =
90
- typeof candidate.errorMessage === "string"
91
- ? boundedPrivateText(candidate.errorMessage, 4 * 1024)
92
- : undefined;
93
- capture.provider =
94
- typeof candidate.provider === "string"
95
- ? boundedPrivateText(candidate.provider, 256)
96
- : undefined;
97
- const responseModel =
98
- typeof candidate.responseModel === "string"
99
- ? candidate.responseModel
100
- : typeof candidate.model === "string"
101
- ? candidate.model
102
- : undefined;
103
- capture.model = responseModel ? boundedPrivateText(responseModel, 256) : undefined;
104
- const finalUsage = normalizeUsage(candidate.usage);
105
- const completedUsage = finalUsage ?? capture.inFlightUsage;
106
- if (completedUsage) addUsage(capture.usage, completedUsage);
107
- capture.inFlightUsage = undefined;
108
- capture.usage.turns = addUsageValue(capture.usage.turns, 1);
109
- }
110
-
111
- export function observeRpcBudgetEvent(event: unknown, monitor: TurnBudgetMonitor): void {
112
- if (!isRecord(event)) return;
113
- if (event.type === "tool_execution_end") monitor.recordActivity();
114
- if (event.type !== "message_end" || !isRecord(event.message)) return;
115
- const message = event.message;
116
- if (message.role === "toolResult") {
117
- monitor.recordActivity();
118
- return;
119
- }
120
- if (message.role !== "assistant") return;
121
- monitor.recordToolCalls(assistantToolCallCount(message.content));
122
- monitor.recordAssistantTurn(
123
- typeof message.stopReason === "string" ? message.stopReason : undefined,
124
- );
125
- }
126
-
127
- function assistantToolCallCount(content: unknown): number {
128
- if (!Array.isArray(content)) return 0;
129
- return content.filter((part) => isRecord(part) && part.type === "toolCall").length;
130
- }
131
-
132
- function normalizeUsage(value: unknown): TransportUsage | undefined {
133
- if (!isRecord(value)) return undefined;
134
- const input = safeNonNegative(value.input);
135
- const output = safeNonNegative(value.output);
136
- const cacheRead = safeNonNegative(value.cacheRead);
137
- const cacheWrite = safeNonNegative(value.cacheWrite);
138
- const reportedTotal = safeNonNegative(value.totalTokens);
139
- const cost = isRecord(value.cost) ? safeNonNegative(value.cost.total) : undefined;
140
- if (
141
- input === undefined &&
142
- output === undefined &&
143
- cacheRead === undefined &&
144
- cacheWrite === undefined &&
145
- reportedTotal === undefined &&
146
- cost === undefined
147
- ) {
148
- return undefined;
149
- }
150
- const components = [input, output, cacheRead, cacheWrite].reduce<number>(
151
- (total, current) => addUsageValue(total, current ?? 0),
152
- 0,
153
- );
154
- return {
155
- input: input ?? 0,
156
- output: output ?? 0,
157
- cacheRead: cacheRead ?? 0,
158
- cacheWrite: cacheWrite ?? 0,
159
- totalTokens: reportedTotal ?? components,
160
- cost: cost ?? 0,
161
- turns: 0,
162
- };
163
- }
164
-
165
- function addUsage(target: TransportUsage, value: TransportUsage): void {
166
- for (const key of [
167
- "input",
168
- "output",
169
- "cacheRead",
170
- "cacheWrite",
171
- "totalTokens",
172
- "cost",
173
- "turns",
174
- ] as const) {
175
- target[key] = addUsageValue(target[key], value[key]);
176
- }
177
- }
178
-
179
- function addUsageValue(current: number, addition: number): number {
180
- return Math.min(Number.MAX_SAFE_INTEGER, current + addition);
181
- }
182
-
183
- function safeNonNegative(value: unknown): number | undefined {
184
- return typeof value === "number" &&
185
- Number.isFinite(value) &&
186
- value >= 0 &&
187
- value <= Number.MAX_SAFE_INTEGER
188
- ? value
189
- : undefined;
190
- }
191
-
192
- function sameUsage(left: TransportUsage, right: TransportUsage): boolean {
193
- return (
194
- left.input === right.input &&
195
- left.output === right.output &&
196
- left.cacheRead === right.cacheRead &&
197
- left.cacheWrite === right.cacheWrite &&
198
- left.totalTokens === right.totalTokens &&
199
- left.cost === right.cost &&
200
- left.turns === right.turns
201
- );
202
- }
203
-
204
- function assistantText(value: unknown): string {
205
- if (typeof value === "string") return value;
206
- if (!Array.isArray(value)) return "";
207
- return value
208
- .flatMap((part) => {
209
- if (!isRecord(part)) return [];
210
- return part.type === "text" && typeof part.text === "string" ? [part.text] : [];
211
- })
212
- .join("\n");
213
- }
214
-
215
- function isRecord(value: unknown): value is Record<string, unknown> {
216
- return Boolean(value && typeof value === "object" && !Array.isArray(value));
217
- }
@@ -1,31 +0,0 @@
1
- import {
2
- formatResultFailure as formatBaseResultFailure,
3
- getResultFinalOutput as getBaseResultFinalOutput,
4
- isResultError as isBaseResultError,
5
- } from "./runner-result.js";
6
- import type { SingleResult } from "./runner-types.js";
7
-
8
- export function getResultFinalOutput(result: SingleResult): string {
9
- return getBaseResultFinalOutput(result);
10
- }
11
-
12
- export function isResultError(result: SingleResult): boolean {
13
- return (
14
- isBaseResultError(result) ||
15
- result.resultContractInvalid === true ||
16
- (result.outcome !== undefined &&
17
- result.outcome.status !== "completed" &&
18
- result.outcome.status !== "partial")
19
- );
20
- }
21
-
22
- export function formatResultFailure(result: SingleResult): string {
23
- const contractError = result.resultContractInvalid
24
- ? `Subagent returned an invalid ${result.resultFormat ?? "structured"} result contract`
25
- : result.outcome && !["completed", "partial"].includes(result.outcome.status)
26
- ? `Subagent outcome ${result.outcome.status}${result.outcome.reasonCode ? ` (${result.outcome.reasonCode})` : ""}; recovery: ${result.outcome.recoveryActions.join(", ") || "none"}`
27
- : undefined;
28
- return contractError
29
- ? formatBaseResultFailure({ ...result, errorMessage: contractError })
30
- : formatBaseResultFailure(result);
31
- }
@@ -1,55 +0,0 @@
1
- import type { Message } from "@earendil-works/pi-ai";
2
- import { DEFAULT_MAX_CONTEXT_BYTES, truncateUtf8 } from "./limits.js";
3
- import type { SingleResult } from "./runner-types.js";
4
- import { formatTimeoutCheckpoint } from "./timeout-checkpoint.js";
5
-
6
- export function getFinalOutput(messages: Message[]): string {
7
- for (let i = messages.length - 1; i >= 0; i--) {
8
- const msg = messages[i];
9
- if (msg?.role === "assistant") {
10
- const text = msg.content
11
- .filter((part) => part.type === "text")
12
- .map((part) => part.text)
13
- .join("\n");
14
- if (text) return text;
15
- }
16
- }
17
- return "";
18
- }
19
-
20
- export function getResultFinalOutput(result: SingleResult): string {
21
- return result.finalOutput ?? getFinalOutput(result.messages);
22
- }
23
-
24
- export function isResultError(result: SingleResult): boolean {
25
- return (
26
- (result.exitCode !== 0 && result.exitCode !== -1) ||
27
- result.timedOut === true ||
28
- result.stopReason === "timeout" ||
29
- result.stopReason === "error" ||
30
- result.stopReason === "aborted"
31
- );
32
- }
33
-
34
- export function formatResultFailure(result: SingleResult): string {
35
- const error = result.errorMessage || result.stderr.trim();
36
- const output = getResultFinalOutput(result);
37
- const sections = [error];
38
- if (result.timeoutSummary) sections.push(`Timed-out work summary:\n${result.timeoutSummary}`);
39
- else if (output) sections.push(`Partial output:\n${output}`);
40
- if (result.partialOutput && result.partialOutput !== output) {
41
- sections.push(`Partial output before finalization:\n${result.partialOutput}`);
42
- }
43
- if (result.termination && !result.timeoutSummary) {
44
- sections.push(
45
- `Termination checkpoint:\n${formatTimeoutCheckpoint(result.termination.checkpoint)}`,
46
- );
47
- }
48
- if (result.timeoutSummaryError) {
49
- sections.push(`Summary finalization failed: ${result.timeoutSummaryError}`);
50
- }
51
- return truncateUtf8(
52
- sections.filter(Boolean).join("\n\n") || "(no output)",
53
- DEFAULT_MAX_CONTEXT_BYTES,
54
- ).text;
55
- }
@@ -1,102 +0,0 @@
1
- import type { Message } from "@earendil-works/pi-ai";
2
- import type { AgentSource, SubagentThinkingLevel } from "./agents/types.js";
3
- import type { CapabilityGrant } from "./capability-grant.js";
4
- import type { TargetPolicyAudit } from "./cwd-policy.js";
5
- import type { DelegationContract } from "./delegation-contract.js";
6
- import type { ExecutionPlan } from "./execution-plan.js";
7
- import type { ClassifiedSubagentOutcome } from "./outcome.js";
8
- import type { AnyStructuredSubagentResult, SubagentResultFormat } from "./result-contract.js";
9
- import type { UsageStats } from "./runner-usage.js";
10
- import type { TurnTerminationReport } from "./timeout-checkpoint.js";
11
- import type { TurnLimits } from "./turn-budget.js";
12
-
13
- export type RecentActivityItem =
14
- | { type: "text"; text: string }
15
- | { type: "toolCall"; name: string; args: Record<string, unknown> };
16
-
17
- export interface SingleResult {
18
- agent: string;
19
- agentSource: AgentSource | "unknown";
20
- task: string;
21
- exitCode: number;
22
- messages: Message[];
23
- stderr: string;
24
- usage: UsageStats;
25
- model?: string;
26
- actualProvider?: string;
27
- actualModel?: string;
28
- recentActivity?: RecentActivityItem[];
29
- recentActivityTotal?: number;
30
- thinkingLevel?: SubagentThinkingLevel;
31
- stopReason?: string;
32
- errorMessage?: string;
33
- step?: number;
34
- finalOutput?: string;
35
- partialOutput?: string;
36
- timeoutSummary?: string;
37
- timeoutSummaryError?: string;
38
- termination?: TurnTerminationReport;
39
- timedOut?: boolean;
40
- timeoutMs?: number;
41
- aborted?: boolean;
42
- truncated?: boolean;
43
- malformedEvents?: number;
44
- launchFailed?: boolean;
45
- processStarted?: boolean;
46
- target?: TargetPolicyAudit;
47
- policy?: {
48
- inherited: string[];
49
- overridden: string[];
50
- unsupported: string[];
51
- };
52
- contract?: DelegationContract;
53
- resultFormat?: SubagentResultFormat;
54
- structuredResult?: AnyStructuredSubagentResult;
55
- resultContractInvalid?: boolean;
56
- outcome?: ClassifiedSubagentOutcome;
57
- attemptCount?: number;
58
- hedged?: boolean;
59
- executionPlan?: ExecutionPlan;
60
- capabilityGrant?: CapabilityGrant;
61
- }
62
-
63
- export interface ChildLaunchPolicy {
64
- tools?: string[];
65
- disableExtensions?: boolean;
66
- disableSkills?: boolean;
67
- disablePromptTemplates?: boolean;
68
- disableContextFiles?: boolean;
69
- projectTrust?: boolean;
70
- baseSystemPrompt?: string;
71
- appendSystemPromptPaths?: string[];
72
- /** Package-owned explicit child extensions loaded even when unrelated extensions are disabled. */
73
- extensionPaths?: string[];
74
- /** Package-owned tools added to the child allowlist without changing delegated execution tools. */
75
- additionalTools?: string[];
76
- /** Ephemeral child-process environment consumed and cleared by a package-owned bridge. */
77
- env?: NodeJS.ProcessEnv;
78
- /** Internal timeout recovery control; omitted means enabled. */
79
- finalizeOnTimeout?: boolean;
80
- /** Internal hard deadline for the summary attempt. */
81
- timeoutFinalizationMs?: number;
82
- /** Optional stateful result contract retained during timeout finalization. */
83
- timeoutResultFormat?: SubagentResultFormat;
84
- /** Optional non-wall-clock limits for this turn. */
85
- turnLimits?: TurnLimits;
86
- /** Override the timeout reason when an orchestration deadline caps this child. */
87
- workTimeoutReason?: "work_timeout" | "orchestration_timeout";
88
- /** Public limit value reported when the effective child timeout is only the remaining budget. */
89
- workTimeoutReportLimit?: number;
90
- /** Absolute blocking-workflow deadline that also caps model finalization. */
91
- orchestrationDeadlineAt?: number;
92
- /** Completion contract requested for this turn. */
93
- resultFormat?: SubagentResultFormat;
94
- /** Normalized request contract retained in result details. */
95
- contract?: DelegationContract;
96
- /** Original task summary shown in result details when the executed prompt has contract metadata. */
97
- displayTask?: string;
98
- /** Immutable audit or enforcement decision made before launch. */
99
- executionPlan?: ExecutionPlan;
100
- /** Executor-owned authority lifetime bound to the accepted plan generation. */
101
- capabilityGrant?: CapabilityGrant;
102
- }
@@ -1,48 +0,0 @@
1
- export interface UsageStats {
2
- input: number;
3
- output: number;
4
- cacheRead: number;
5
- cacheWrite: number;
6
- cost: number;
7
- costInput?: number;
8
- costOutput?: number;
9
- costCacheRead?: number;
10
- costCacheWrite?: number;
11
- totalTokens?: number;
12
- contextTokens: number;
13
- turns: number;
14
- }
15
-
16
- const MAX_USAGE_VALUE = Number.MAX_SAFE_INTEGER;
17
-
18
- export function protocolUsageCount(value: unknown): number {
19
- return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : 0;
20
- }
21
-
22
- export function protocolUsageCost(value: unknown): number {
23
- return typeof value === "number" && Number.isFinite(value) && value >= 0
24
- ? Math.min(value, MAX_USAGE_VALUE)
25
- : 0;
26
- }
27
-
28
- export function addUsageValue(current: number, addition: number): number {
29
- return Math.min(MAX_USAGE_VALUE, current + addition);
30
- }
31
-
32
- export function mergeUsageStats(target: UsageStats, addition: UsageStats): void {
33
- for (const key of ["input", "output", "cacheRead", "cacheWrite", "cost", "turns"] as const) {
34
- target[key] = addUsageValue(target[key], addition[key]);
35
- }
36
- for (const key of [
37
- "costInput",
38
- "costOutput",
39
- "costCacheRead",
40
- "costCacheWrite",
41
- "totalTokens",
42
- ] as const) {
43
- if (addition[key] !== undefined) {
44
- target[key] = addUsageValue(target[key] ?? 0, addition[key]);
45
- }
46
- }
47
- target.contextTokens = addition.contextTokens || target.contextTokens;
48
- }