@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,431 +0,0 @@
1
- import { redactPrivateText } from "./context.js";
2
- import { DEFAULT_MAX_OUTPUT_BYTES, truncateUtf8 } from "./limits.js";
3
-
4
- export const SUBAGENT_RESULT_FORMATS = ["text", "structured-v1", "structured-v2"] as const;
5
- export type SubagentResultFormat = (typeof SUBAGENT_RESULT_FORMATS)[number];
6
-
7
- export interface StructuredSubagentResult {
8
- version: "pi-subagents:result:v1";
9
- summary: string;
10
- evidence: string[];
11
- changes: string[];
12
- verification: string[];
13
- risks: string[];
14
- }
15
-
16
- export const SUBAGENT_OUTCOME_STATUSES = [
17
- "completed",
18
- "partial",
19
- "blocked",
20
- "needs-input",
21
- "abstained",
22
- "failed",
23
- "interrupted",
24
- "stale",
25
- "contract-invalid",
26
- ] as const;
27
- export type SubagentOutcomeStatus = (typeof SUBAGENT_OUTCOME_STATUSES)[number];
28
-
29
- export const SUBAGENT_CLAIM_CLASSIFICATIONS = ["observed", "inferred", "unverified"] as const;
30
- export type SubagentClaimClassification = (typeof SUBAGENT_CLAIM_CLASSIFICATIONS)[number];
31
-
32
- export interface EvidenceBackedClaim {
33
- claim: string;
34
- classification: SubagentClaimClassification;
35
- evidence: string[];
36
- }
37
-
38
- export interface SubagentArtifactReference {
39
- id: string;
40
- kind: string;
41
- version?: string;
42
- location?: string;
43
- digest?: string;
44
- }
45
-
46
- export interface SubagentChangeReference {
47
- path: string;
48
- summary: string;
49
- }
50
-
51
- export interface SubagentVerificationReference {
52
- status: "passed" | "failed" | "not-run";
53
- summary: string;
54
- command?: string;
55
- evidence?: string[];
56
- }
57
-
58
- export interface SubagentResultProvenance {
59
- taskId?: string;
60
- taskGeneration?: number;
61
- executionPlanId?: string;
62
- cancellationLineage?: string[];
63
- inputArtifacts?: string[];
64
- repositoryGeneration?: string;
65
- }
66
-
67
- export interface StructuredSubagentResultV2 {
68
- version: "pi-subagents:result:v2";
69
- status: SubagentOutcomeStatus;
70
- reasonCode?: string;
71
- summary: string;
72
- claims: EvidenceBackedClaim[];
73
- artifacts: SubagentArtifactReference[];
74
- changes: SubagentChangeReference[];
75
- verification: SubagentVerificationReference[];
76
- limitations: string[];
77
- unresolvedDependencies: string[];
78
- provenance?: SubagentResultProvenance;
79
- }
80
-
81
- export type AnyStructuredSubagentResult = StructuredSubagentResult | StructuredSubagentResultV2;
82
-
83
- export interface ResultContractRuntimeMetadata {
84
- truncated?: boolean;
85
- usage?: {
86
- input: number;
87
- output: number;
88
- cost: number;
89
- turns: number;
90
- };
91
- }
92
-
93
- export interface ResultContractEnvelope<T extends AnyStructuredSubagentResult> {
94
- result: T;
95
- truncated?: boolean;
96
- usage?: ResultContractRuntimeMetadata["usage"];
97
- }
98
-
99
- const MAX_FIELD_BYTES = 8 * 1024;
100
- const MAX_ITEMS = 50;
101
- const MAX_REASON_CODE_BYTES = 256;
102
- const MAX_ARTIFACT_IDENTIFIER_BYTES = 256;
103
-
104
- export function structuredResultInstruction(format: SubagentResultFormat | undefined): string {
105
- if (format === "structured-v1") {
106
- return [
107
- "Return the final answer as one JSON object and no surrounding prose.",
108
- 'Use exactly version "pi-subagents:result:v1" and fields summary, evidence, changes, verification, and risks.',
109
- "summary must be a string and the other fields must be arrays of strings.",
110
- ].join(" ");
111
- }
112
- if (format === "structured-v2") {
113
- const minimumResult = JSON.stringify({
114
- version: "pi-subagents:result:v2",
115
- status: "completed",
116
- summary: "Concise outcome",
117
- claims: [],
118
- artifacts: [],
119
- changes: [],
120
- verification: [],
121
- limitations: [],
122
- unresolvedDependencies: [],
123
- });
124
- return [
125
- "Return the final answer as one JSON object and no surrounding prose.",
126
- "Minimum valid result:",
127
- minimumResult,
128
- "Keep every top-level field shown, including empty arrays.",
129
- `status must be one of ${SUBAGENT_OUTCOME_STATUSES.join(", ")}.`,
130
- 'Claim item shape: {"claim":"Observed fact","classification":"observed","evidence":["path:line"]}.',
131
- 'Artifact item shape: {"id":"artifact-id","kind":"file","version":"optional","location":"optional","digest":"optional"}.',
132
- 'Change item shape: {"path":"path/to/file","summary":"What changed"}.',
133
- 'Verification item shape: {"status":"passed","summary":"What was checked","command":"optional","evidence":["optional"]}.',
134
- "Claim classification must be observed, inferred, or unverified; verification status must be passed, failed, or not-run.",
135
- "Use optional reasonCode for non-completed outcomes and optional provenance for taskId, inputArtifacts, and repositoryGeneration; executor-owned generation and plan identity are stamped after parsing.",
136
- ].join("\n");
137
- }
138
- return "";
139
- }
140
-
141
- export function appendResultInstruction(
142
- prompt: string,
143
- format: SubagentResultFormat | undefined,
144
- maxBytes = DEFAULT_MAX_OUTPUT_BYTES,
145
- ): string {
146
- const instruction = structuredResultInstruction(format);
147
- if (!instruction) return prompt;
148
- const suffix = `\n\nResult contract:\n${instruction}`;
149
- const suffixBytes = Buffer.byteLength(suffix, "utf8");
150
- const boundedPrompt = truncateUtf8(prompt, Math.max(0, maxBytes - suffixBytes)).text;
151
- return `${boundedPrompt}${suffix}`;
152
- }
153
-
154
- export function parseStructuredSubagentResult(text: string): StructuredSubagentResult | undefined {
155
- const value = parseJsonObject(text);
156
- if (
157
- value?.version !== "pi-subagents:result:v1" ||
158
- !hasOnlyKeys(value, ["version", "summary", "evidence", "changes", "verification", "risks"]) ||
159
- typeof value.summary !== "string"
160
- ) {
161
- return undefined;
162
- }
163
- const evidence = stringArray(value.evidence);
164
- const changes = stringArray(value.changes);
165
- const verification = stringArray(value.verification);
166
- const risks = stringArray(value.risks);
167
- if (!evidence || !changes || !verification || !risks) return undefined;
168
- return {
169
- version: "pi-subagents:result:v1",
170
- summary: bounded(value.summary),
171
- evidence,
172
- changes,
173
- verification,
174
- risks,
175
- };
176
- }
177
-
178
- export function parseStructuredSubagentResultV2(
179
- text: string,
180
- ): StructuredSubagentResultV2 | undefined {
181
- const value = parseJsonObject(text);
182
- if (
183
- value?.version !== "pi-subagents:result:v2" ||
184
- !hasOnlyKeys(value, [
185
- "version",
186
- "status",
187
- "reasonCode",
188
- "summary",
189
- "claims",
190
- "artifacts",
191
- "changes",
192
- "verification",
193
- "limitations",
194
- "unresolvedDependencies",
195
- "provenance",
196
- ]) ||
197
- typeof value.status !== "string" ||
198
- !SUBAGENT_OUTCOME_STATUSES.includes(value.status as SubagentOutcomeStatus) ||
199
- typeof value.summary !== "string"
200
- ) {
201
- return undefined;
202
- }
203
- const claims = objectArray(value.claims, parseClaim);
204
- const artifacts = objectArray(value.artifacts, parseArtifact);
205
- const changes = objectArray(value.changes, parseChange);
206
- const verification = objectArray(value.verification, parseVerification);
207
- const limitations = stringArray(value.limitations);
208
- const unresolvedDependencies = stringArray(value.unresolvedDependencies);
209
- if (
210
- !claims ||
211
- !artifacts ||
212
- new Set(artifacts.map((artifact) => artifact.id)).size !== artifacts.length ||
213
- !changes ||
214
- !verification ||
215
- !limitations ||
216
- !unresolvedDependencies
217
- ) {
218
- return undefined;
219
- }
220
- const reasonCode = optionalBoundedString(value.reasonCode, MAX_REASON_CODE_BYTES);
221
- if (reasonCode === false) return undefined;
222
- const provenance = parseProvenance(value.provenance);
223
- if (provenance === false) return undefined;
224
- return {
225
- version: "pi-subagents:result:v2",
226
- status: value.status as SubagentOutcomeStatus,
227
- ...(reasonCode === undefined ? {} : { reasonCode }),
228
- summary: bounded(value.summary),
229
- claims,
230
- artifacts,
231
- changes,
232
- verification,
233
- limitations,
234
- unresolvedDependencies,
235
- ...(provenance === undefined ? {} : { provenance }),
236
- };
237
- }
238
-
239
- export function parseAnyStructuredSubagentResult(
240
- text: string,
241
- format: SubagentResultFormat | undefined,
242
- ): AnyStructuredSubagentResult | undefined {
243
- if (format === "structured-v2") return parseStructuredSubagentResultV2(text);
244
- if (format === "structured-v1") return parseStructuredSubagentResult(text);
245
- return undefined;
246
- }
247
-
248
- export function resultContractEnvelope<T extends AnyStructuredSubagentResult>(
249
- result: T,
250
- metadata: ResultContractRuntimeMetadata = {},
251
- ): ResultContractEnvelope<T> {
252
- return {
253
- result,
254
- ...(metadata.truncated === undefined ? {} : { truncated: metadata.truncated }),
255
- ...(metadata.usage === undefined ? {} : { usage: { ...metadata.usage } }),
256
- };
257
- }
258
-
259
- function parseClaim(value: Record<string, unknown>): EvidenceBackedClaim | undefined {
260
- if (
261
- typeof value.claim !== "string" ||
262
- typeof value.classification !== "string" ||
263
- !SUBAGENT_CLAIM_CLASSIFICATIONS.includes(value.classification as SubagentClaimClassification)
264
- ) {
265
- return undefined;
266
- }
267
- const evidence = stringArray(value.evidence);
268
- if (!evidence) return undefined;
269
- return {
270
- claim: bounded(value.claim),
271
- classification: value.classification as SubagentClaimClassification,
272
- evidence,
273
- };
274
- }
275
-
276
- function parseArtifact(value: Record<string, unknown>): SubagentArtifactReference | undefined {
277
- if (typeof value.id !== "string" || typeof value.kind !== "string") return undefined;
278
- const id = truncateUtf8(redactPrivateText(value.id), MAX_ARTIFACT_IDENTIFIER_BYTES).text.trim();
279
- const kind = truncateUtf8(
280
- redactPrivateText(value.kind),
281
- MAX_ARTIFACT_IDENTIFIER_BYTES,
282
- ).text.trim();
283
- if (!/^[A-Za-z0-9][A-Za-z0-9._:-]*$/u.test(id) || !kind) return undefined;
284
- const version = optionalTrimmedBoundedString(value.version, MAX_ARTIFACT_IDENTIFIER_BYTES);
285
- const location = optionalTrimmedBoundedString(value.location);
286
- const digest = optionalTrimmedBoundedString(value.digest);
287
- if (version === false || location === false || digest === false) return undefined;
288
- return {
289
- id,
290
- kind,
291
- ...(version === undefined ? {} : { version }),
292
- ...(location === undefined ? {} : { location }),
293
- ...(digest === undefined ? {} : { digest }),
294
- };
295
- }
296
-
297
- function parseChange(value: Record<string, unknown>): SubagentChangeReference | undefined {
298
- if (typeof value.path !== "string" || typeof value.summary !== "string") return undefined;
299
- return { path: bounded(value.path), summary: bounded(value.summary) };
300
- }
301
-
302
- function parseVerification(
303
- value: Record<string, unknown>,
304
- ): SubagentVerificationReference | undefined {
305
- if (
306
- typeof value.status !== "string" ||
307
- !(["passed", "failed", "not-run"] as const).includes(
308
- value.status as SubagentVerificationReference["status"],
309
- ) ||
310
- typeof value.summary !== "string"
311
- ) {
312
- return undefined;
313
- }
314
- const command = optionalBoundedString(value.command);
315
- const evidence = value.evidence === undefined ? undefined : stringArray(value.evidence);
316
- if (command === false || (evidence === undefined && value.evidence !== undefined))
317
- return undefined;
318
- return {
319
- status: value.status as SubagentVerificationReference["status"],
320
- summary: bounded(value.summary),
321
- ...(command === undefined ? {} : { command }),
322
- ...(evidence === undefined ? {} : { evidence }),
323
- };
324
- }
325
-
326
- function parseProvenance(value: unknown): SubagentResultProvenance | undefined | false {
327
- if (value === undefined) return undefined;
328
- if (!isPlainObject(value)) return false;
329
- const taskId = optionalBoundedString(value.taskId);
330
- const repositoryGeneration = optionalBoundedString(value.repositoryGeneration);
331
- const taskGeneration =
332
- value.taskGeneration === undefined
333
- ? undefined
334
- : Number.isSafeInteger(value.taskGeneration) && Number(value.taskGeneration) >= 0
335
- ? Number(value.taskGeneration)
336
- : false;
337
- const executionPlanId = optionalBoundedString(value.executionPlanId);
338
- const cancellationLineage =
339
- value.cancellationLineage === undefined ? undefined : stringArray(value.cancellationLineage);
340
- const inputArtifacts =
341
- value.inputArtifacts === undefined ? undefined : stringArray(value.inputArtifacts);
342
- if (
343
- taskId === false ||
344
- repositoryGeneration === false ||
345
- taskGeneration === false ||
346
- executionPlanId === false ||
347
- (cancellationLineage === undefined && value.cancellationLineage !== undefined) ||
348
- (inputArtifacts === undefined && value.inputArtifacts !== undefined)
349
- ) {
350
- return false;
351
- }
352
- return {
353
- ...(taskId === undefined ? {} : { taskId }),
354
- ...(taskGeneration === undefined ? {} : { taskGeneration }),
355
- ...(executionPlanId === undefined ? {} : { executionPlanId }),
356
- ...(cancellationLineage === undefined ? {} : { cancellationLineage }),
357
- ...(inputArtifacts === undefined ? {} : { inputArtifacts }),
358
- ...(repositoryGeneration === undefined ? {} : { repositoryGeneration }),
359
- };
360
- }
361
-
362
- function parseJsonObject(text: string): Record<string, unknown> | undefined {
363
- if (Buffer.byteLength(text, "utf8") > DEFAULT_MAX_OUTPUT_BYTES) return undefined;
364
- const source = unwrapJsonFence(text).trim();
365
- if (!source.startsWith("{") || !source.endsWith("}")) return undefined;
366
- let parsed: unknown;
367
- try {
368
- parsed = JSON.parse(source);
369
- } catch {
370
- return undefined;
371
- }
372
- return isPlainObject(parsed) ? parsed : undefined;
373
- }
374
-
375
- function objectArray<T>(
376
- value: unknown,
377
- parse: (entry: Record<string, unknown>) => T | undefined,
378
- ): T[] | undefined {
379
- if (!Array.isArray(value) || value.length > MAX_ITEMS) return undefined;
380
- const result: T[] = [];
381
- for (const item of value) {
382
- if (!isPlainObject(item)) return undefined;
383
- const parsed = parse(item);
384
- if (!parsed) return undefined;
385
- result.push(parsed);
386
- }
387
- return result;
388
- }
389
-
390
- function stringArray(value: unknown): string[] | undefined {
391
- if (!Array.isArray(value) || value.length > MAX_ITEMS) return undefined;
392
- if (!value.every((item) => typeof item === "string")) return undefined;
393
- return value.map((item) => bounded(item));
394
- }
395
-
396
- function optionalTrimmedBoundedString(
397
- value: unknown,
398
- maxBytes = MAX_FIELD_BYTES,
399
- ): string | undefined | false {
400
- const boundedValue = optionalBoundedString(value, maxBytes);
401
- if (boundedValue === undefined || boundedValue === false) return boundedValue;
402
- const trimmed = boundedValue.trim();
403
- return trimmed || false;
404
- }
405
-
406
- function optionalBoundedString(
407
- value: unknown,
408
- maxBytes = MAX_FIELD_BYTES,
409
- ): string | undefined | false {
410
- if (value === undefined) return undefined;
411
- if (typeof value !== "string") return false;
412
- return truncateUtf8(redactPrivateText(value), maxBytes).text;
413
- }
414
-
415
- function bounded(value: string): string {
416
- return truncateUtf8(redactPrivateText(value), MAX_FIELD_BYTES).text;
417
- }
418
-
419
- function hasOnlyKeys(value: Record<string, unknown>, allowed: readonly string[]): boolean {
420
- return Object.keys(value).every((key) => allowed.includes(key));
421
- }
422
-
423
- function isPlainObject(value: unknown): value is Record<string, unknown> {
424
- return Boolean(value) && typeof value === "object" && !Array.isArray(value);
425
- }
426
-
427
- function unwrapJsonFence(value: string): string {
428
- const trimmed = value.trim();
429
- const match = /^```(?:json)?\s*\n([\s\S]*?)\n```$/iu.exec(trimmed);
430
- return match?.[1] ?? trimmed;
431
- }
@@ -1,100 +0,0 @@
1
- import * as path from "node:path";
2
- import { getAgentDir } from "@earendil-works/pi-coding-agent";
3
- import type { AgentConfig, SubagentThinkingLevel, SubagentTransportKind } from "./agents/types.js";
4
- import type { TargetPolicyAudit } from "./cwd-policy.js";
5
- import type { DelegationContract } from "./delegation-contract.js";
6
- import {
7
- acknowledgeExecutionPlan,
8
- createExecutionPlan,
9
- type ExecutionPlan,
10
- resolveContractTools,
11
- } from "./execution-plan.js";
12
- import type { SubagentResultFormat } from "./result-contract.js";
13
- import {
14
- captureRepositoryGeneration,
15
- captureSemanticResourceGeneration,
16
- captureSemanticSnapshot,
17
- type SemanticSnapshot,
18
- } from "./semantic-snapshot.js";
19
-
20
- export interface RetainedSemanticStateInput {
21
- agent: AgentConfig;
22
- contract?: DelegationContract;
23
- target: TargetPolicyAudit;
24
- cwd: string;
25
- workspaceMode: "shared" | "worktree";
26
- transport: SubagentTransportKind;
27
- resultFormat: SubagentResultFormat;
28
- thinkingLevel?: SubagentThinkingLevel;
29
- timeoutMs?: number;
30
- taskGeneration?: number;
31
- cancellationLineage?: string[];
32
- }
33
-
34
- export interface RetainedSemanticState {
35
- executionPlan: ExecutionPlan;
36
- semanticSnapshot: SemanticSnapshot;
37
- }
38
-
39
- export async function buildRetainedSemanticState(
40
- input: RetainedSemanticStateInput,
41
- ): Promise<RetainedSemanticState> {
42
- const executionPlan = createExecutionPlan({
43
- contract: input.contract,
44
- agent: input.agent,
45
- effectiveTools: resolveContractTools(input.agent.tools, input.contract),
46
- target: input.target,
47
- workspaceMode: input.workspaceMode,
48
- transport: input.transport,
49
- resultFormat: input.resultFormat,
50
- model: input.agent.model,
51
- thinkingLevel: input.thinkingLevel ?? input.agent.thinkingLevel,
52
- timeoutMs: input.timeoutMs ?? input.agent.timeoutMs,
53
- taskGeneration: input.taskGeneration,
54
- cancellationLineage: input.cancellationLineage,
55
- });
56
- const acknowledgement = acknowledgeExecutionPlan(executionPlan);
57
- if (acknowledgement.status === "rejected") {
58
- throw new Error(`Execution plan rejected: ${JSON.stringify(acknowledgement)}`);
59
- }
60
- const agentDir = getAgentDir();
61
- const [repository, resourceGeneration] = await Promise.all([
62
- captureRepositoryGeneration(input.cwd),
63
- captureSemanticResourceGeneration([
64
- path.join(agentDir, "skills"),
65
- path.join(agentDir, "prompts"),
66
- path.join(agentDir, "SYSTEM.md"),
67
- path.join(agentDir, "APPEND_SYSTEM.md"),
68
- path.join(input.cwd, ".pi", "skills"),
69
- path.join(input.cwd, ".pi", "prompts"),
70
- path.join(input.cwd, "AGENTS.md"),
71
- path.join(input.cwd, "CLAUDE.md"),
72
- path.join(input.cwd, "SYSTEM.md"),
73
- path.join(input.cwd, "APPEND_SYSTEM.md"),
74
- ]),
75
- ]);
76
- return {
77
- executionPlan,
78
- semanticSnapshot: captureSemanticSnapshot({
79
- agentName: input.agent.name,
80
- agentManifest: {
81
- manifest: input.agent.capabilityManifest,
82
- delegationContract: input.contract,
83
- resourceGeneration,
84
- },
85
- rolePrompt: input.agent.systemPrompt,
86
- tools: executionPlan.effectiveTools,
87
- model: executionPlan.model,
88
- thinkingLevel: executionPlan.thinkingLevel,
89
- transport: input.transport,
90
- trust: {
91
- kind: input.target.trust.kind,
92
- projectTrusted: input.target.trust.projectTrusted,
93
- },
94
- repository,
95
- artifacts: {},
96
- workflowGeneration: 0,
97
- schedulerPolicy: "retained-fifo-v1",
98
- }),
99
- };
100
- }