@caupulican/pi-adaptative 0.80.85 → 0.80.88

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 (340) hide show
  1. package/CHANGELOG.md +160 -1
  2. package/dist/core/agent-session.d.ts +394 -1
  3. package/dist/core/agent-session.d.ts.map +1 -1
  4. package/dist/core/agent-session.js +1862 -46
  5. package/dist/core/agent-session.js.map +1 -1
  6. package/dist/core/autonomy/approval-gate.d.ts +4 -0
  7. package/dist/core/autonomy/approval-gate.d.ts.map +1 -0
  8. package/dist/core/autonomy/approval-gate.js +27 -0
  9. package/dist/core/autonomy/approval-gate.js.map +1 -0
  10. package/dist/core/autonomy/bounded-completion.d.ts +27 -0
  11. package/dist/core/autonomy/bounded-completion.d.ts.map +1 -0
  12. package/dist/core/autonomy/bounded-completion.js +44 -0
  13. package/dist/core/autonomy/bounded-completion.js.map +1 -0
  14. package/dist/core/autonomy/contracts.d.ts +129 -0
  15. package/dist/core/autonomy/contracts.d.ts.map +1 -0
  16. package/dist/core/autonomy/contracts.js +2 -0
  17. package/dist/core/autonomy/contracts.js.map +1 -0
  18. package/dist/core/autonomy/gates.d.ts +15 -0
  19. package/dist/core/autonomy/gates.d.ts.map +1 -0
  20. package/dist/core/autonomy/gates.js +205 -0
  21. package/dist/core/autonomy/gates.js.map +1 -0
  22. package/dist/core/autonomy/lane-tracker.d.ts +48 -0
  23. package/dist/core/autonomy/lane-tracker.d.ts.map +1 -0
  24. package/dist/core/autonomy/lane-tracker.js +125 -0
  25. package/dist/core/autonomy/lane-tracker.js.map +1 -0
  26. package/dist/core/autonomy/path-scope.d.ts +9 -0
  27. package/dist/core/autonomy/path-scope.d.ts.map +1 -0
  28. package/dist/core/autonomy/path-scope.js +122 -0
  29. package/dist/core/autonomy/path-scope.js.map +1 -0
  30. package/dist/core/autonomy/risk-assessment.d.ts +3 -0
  31. package/dist/core/autonomy/risk-assessment.d.ts.map +1 -0
  32. package/dist/core/autonomy/risk-assessment.js +122 -0
  33. package/dist/core/autonomy/risk-assessment.js.map +1 -0
  34. package/dist/core/autonomy/session-lane-record.d.ts +10 -0
  35. package/dist/core/autonomy/session-lane-record.d.ts.map +1 -0
  36. package/dist/core/autonomy/session-lane-record.js +36 -0
  37. package/dist/core/autonomy/session-lane-record.js.map +1 -0
  38. package/dist/core/autonomy/status.d.ts +40 -0
  39. package/dist/core/autonomy/status.d.ts.map +1 -0
  40. package/dist/core/autonomy/status.js +107 -0
  41. package/dist/core/autonomy/status.js.map +1 -0
  42. package/dist/core/autonomy/subagent-prompt.d.ts +21 -0
  43. package/dist/core/autonomy/subagent-prompt.d.ts.map +1 -0
  44. package/dist/core/autonomy/subagent-prompt.js +28 -0
  45. package/dist/core/autonomy/subagent-prompt.js.map +1 -0
  46. package/dist/core/autonomy/telemetry-events.d.ts +18 -0
  47. package/dist/core/autonomy/telemetry-events.d.ts.map +1 -0
  48. package/dist/core/autonomy/telemetry-events.js +60 -0
  49. package/dist/core/autonomy/telemetry-events.js.map +1 -0
  50. package/dist/core/context/artifact-retrieval.d.ts +49 -0
  51. package/dist/core/context/artifact-retrieval.d.ts.map +1 -0
  52. package/dist/core/context/artifact-retrieval.js +49 -0
  53. package/dist/core/context/artifact-retrieval.js.map +1 -0
  54. package/dist/core/context/context-artifacts.d.ts +94 -0
  55. package/dist/core/context/context-artifacts.d.ts.map +1 -0
  56. package/dist/core/context/context-artifacts.js +307 -0
  57. package/dist/core/context/context-artifacts.js.map +1 -0
  58. package/dist/core/context/context-audit.d.ts +66 -0
  59. package/dist/core/context/context-audit.d.ts.map +1 -0
  60. package/dist/core/context/context-audit.js +173 -0
  61. package/dist/core/context/context-audit.js.map +1 -0
  62. package/dist/core/context/context-item.d.ts +117 -0
  63. package/dist/core/context/context-item.d.ts.map +1 -0
  64. package/dist/core/context/context-item.js +36 -0
  65. package/dist/core/context/context-item.js.map +1 -0
  66. package/dist/core/context/context-prompt-enforcement.d.ts +73 -0
  67. package/dist/core/context/context-prompt-enforcement.d.ts.map +1 -0
  68. package/dist/core/context/context-prompt-enforcement.js +153 -0
  69. package/dist/core/context/context-prompt-enforcement.js.map +1 -0
  70. package/dist/core/context/context-prompt-policy.d.ts +90 -0
  71. package/dist/core/context/context-prompt-policy.d.ts.map +1 -0
  72. package/dist/core/context/context-prompt-policy.js +73 -0
  73. package/dist/core/context/context-prompt-policy.js.map +1 -0
  74. package/dist/core/context/context-retention.d.ts +36 -0
  75. package/dist/core/context/context-retention.d.ts.map +1 -0
  76. package/dist/core/context/context-retention.js +108 -0
  77. package/dist/core/context/context-retention.js.map +1 -0
  78. package/dist/core/context/context-store.d.ts +37 -0
  79. package/dist/core/context/context-store.d.ts.map +1 -0
  80. package/dist/core/context/context-store.js +45 -0
  81. package/dist/core/context/context-store.js.map +1 -0
  82. package/dist/core/context/memory-diagnostics.d.ts +50 -0
  83. package/dist/core/context/memory-diagnostics.d.ts.map +1 -0
  84. package/dist/core/context/memory-diagnostics.js +43 -0
  85. package/dist/core/context/memory-diagnostics.js.map +1 -0
  86. package/dist/core/context/memory-index-store.d.ts +28 -0
  87. package/dist/core/context/memory-index-store.d.ts.map +1 -0
  88. package/dist/core/context/memory-index-store.js +38 -0
  89. package/dist/core/context/memory-index-store.js.map +1 -0
  90. package/dist/core/context/memory-prompt-block.d.ts +34 -0
  91. package/dist/core/context/memory-prompt-block.d.ts.map +1 -0
  92. package/dist/core/context/memory-prompt-block.js +58 -0
  93. package/dist/core/context/memory-prompt-block.js.map +1 -0
  94. package/dist/core/context/memory-provider-contract.d.ts +114 -0
  95. package/dist/core/context/memory-provider-contract.d.ts.map +1 -0
  96. package/dist/core/context/memory-provider-contract.js +121 -0
  97. package/dist/core/context/memory-provider-contract.js.map +1 -0
  98. package/dist/core/context/memory-retrieval.d.ts +27 -0
  99. package/dist/core/context/memory-retrieval.d.ts.map +1 -0
  100. package/dist/core/context/memory-retrieval.js +91 -0
  101. package/dist/core/context/memory-retrieval.js.map +1 -0
  102. package/dist/core/context/okf-memory-provider.d.ts +26 -0
  103. package/dist/core/context/okf-memory-provider.d.ts.map +1 -0
  104. package/dist/core/context/okf-memory-provider.js +154 -0
  105. package/dist/core/context/okf-memory-provider.js.map +1 -0
  106. package/dist/core/context/okf-memory.d.ts +42 -0
  107. package/dist/core/context/okf-memory.d.ts.map +1 -0
  108. package/dist/core/context/okf-memory.js +175 -0
  109. package/dist/core/context/okf-memory.js.map +1 -0
  110. package/dist/core/context/policy-engine.d.ts +66 -0
  111. package/dist/core/context/policy-engine.d.ts.map +1 -0
  112. package/dist/core/context/policy-engine.js +171 -0
  113. package/dist/core/context/policy-engine.js.map +1 -0
  114. package/dist/core/context/policy-types.d.ts +102 -0
  115. package/dist/core/context/policy-types.d.ts.map +1 -0
  116. package/dist/core/context/policy-types.js +7 -0
  117. package/dist/core/context/policy-types.js.map +1 -0
  118. package/dist/core/context/sqlite-runtime-index.d.ts +19 -0
  119. package/dist/core/context/sqlite-runtime-index.d.ts.map +1 -0
  120. package/dist/core/context/sqlite-runtime-index.js +344 -0
  121. package/dist/core/context/sqlite-runtime-index.js.map +1 -0
  122. package/dist/core/context/storage-authority.d.ts +20 -0
  123. package/dist/core/context/storage-authority.d.ts.map +1 -0
  124. package/dist/core/context/storage-authority.js +51 -0
  125. package/dist/core/context/storage-authority.js.map +1 -0
  126. package/dist/core/context/tool-output-packer.d.ts +75 -0
  127. package/dist/core/context/tool-output-packer.d.ts.map +1 -0
  128. package/dist/core/context/tool-output-packer.js +77 -0
  129. package/dist/core/context/tool-output-packer.js.map +1 -0
  130. package/dist/core/cost/session-usage.d.ts +20 -0
  131. package/dist/core/cost/session-usage.d.ts.map +1 -0
  132. package/dist/core/cost/session-usage.js +164 -0
  133. package/dist/core/cost/session-usage.js.map +1 -0
  134. package/dist/core/delegation/session-worker-result.d.ts +10 -0
  135. package/dist/core/delegation/session-worker-result.d.ts.map +1 -0
  136. package/dist/core/delegation/session-worker-result.js +36 -0
  137. package/dist/core/delegation/session-worker-result.js.map +1 -0
  138. package/dist/core/delegation/worker-result.d.ts +9 -0
  139. package/dist/core/delegation/worker-result.d.ts.map +1 -0
  140. package/dist/core/delegation/worker-result.js +152 -0
  141. package/dist/core/delegation/worker-result.js.map +1 -0
  142. package/dist/core/delegation/worker-runner.d.ts +58 -0
  143. package/dist/core/delegation/worker-runner.d.ts.map +1 -0
  144. package/dist/core/delegation/worker-runner.js +188 -0
  145. package/dist/core/delegation/worker-runner.js.map +1 -0
  146. package/dist/core/extensions/builtin.d.ts +5 -1
  147. package/dist/core/extensions/builtin.d.ts.map +1 -1
  148. package/dist/core/extensions/builtin.js +23 -1
  149. package/dist/core/extensions/builtin.js.map +1 -1
  150. package/dist/core/footer-data-provider.d.ts +5 -1
  151. package/dist/core/footer-data-provider.d.ts.map +1 -1
  152. package/dist/core/footer-data-provider.js +13 -0
  153. package/dist/core/footer-data-provider.js.map +1 -1
  154. package/dist/core/goals/goal-continuation-controller.d.ts +22 -0
  155. package/dist/core/goals/goal-continuation-controller.d.ts.map +1 -0
  156. package/dist/core/goals/goal-continuation-controller.js +88 -0
  157. package/dist/core/goals/goal-continuation-controller.js.map +1 -0
  158. package/dist/core/goals/goal-continuation-defaults.d.ts +10 -0
  159. package/dist/core/goals/goal-continuation-defaults.d.ts.map +1 -0
  160. package/dist/core/goals/goal-continuation-defaults.js +10 -0
  161. package/dist/core/goals/goal-continuation-defaults.js.map +1 -0
  162. package/dist/core/goals/goal-continuation-prompt.d.ts +18 -0
  163. package/dist/core/goals/goal-continuation-prompt.d.ts.map +1 -0
  164. package/dist/core/goals/goal-continuation-prompt.js +141 -0
  165. package/dist/core/goals/goal-continuation-prompt.js.map +1 -0
  166. package/dist/core/goals/goal-runtime-snapshot.d.ts +19 -0
  167. package/dist/core/goals/goal-runtime-snapshot.d.ts.map +1 -0
  168. package/dist/core/goals/goal-runtime-snapshot.js +23 -0
  169. package/dist/core/goals/goal-runtime-snapshot.js.map +1 -0
  170. package/dist/core/goals/goal-state.d.ts +87 -0
  171. package/dist/core/goals/goal-state.d.ts.map +1 -0
  172. package/dist/core/goals/goal-state.js +259 -0
  173. package/dist/core/goals/goal-state.js.map +1 -0
  174. package/dist/core/goals/goal-tool-core.d.ts +66 -0
  175. package/dist/core/goals/goal-tool-core.d.ts.map +1 -0
  176. package/dist/core/goals/goal-tool-core.js +146 -0
  177. package/dist/core/goals/goal-tool-core.js.map +1 -0
  178. package/dist/core/goals/session-goal-state.d.ts +10 -0
  179. package/dist/core/goals/session-goal-state.d.ts.map +1 -0
  180. package/dist/core/goals/session-goal-state.js +35 -0
  181. package/dist/core/goals/session-goal-state.js.map +1 -0
  182. package/dist/core/learning/learning-audit.d.ts +45 -0
  183. package/dist/core/learning/learning-audit.d.ts.map +1 -0
  184. package/dist/core/learning/learning-audit.js +139 -0
  185. package/dist/core/learning/learning-audit.js.map +1 -0
  186. package/dist/core/learning/learning-gate.d.ts +29 -0
  187. package/dist/core/learning/learning-gate.d.ts.map +1 -0
  188. package/dist/core/learning/learning-gate.js +150 -0
  189. package/dist/core/learning/learning-gate.js.map +1 -0
  190. package/dist/core/learning/session-learning-decision.d.ts +10 -0
  191. package/dist/core/learning/session-learning-decision.d.ts.map +1 -0
  192. package/dist/core/learning/session-learning-decision.js +36 -0
  193. package/dist/core/learning/session-learning-decision.js.map +1 -0
  194. package/dist/core/model-capability.d.ts +41 -0
  195. package/dist/core/model-capability.d.ts.map +1 -0
  196. package/dist/core/model-capability.js +101 -0
  197. package/dist/core/model-capability.js.map +1 -0
  198. package/dist/core/model-router/config-diagnostics.d.ts.map +1 -1
  199. package/dist/core/model-router/config-diagnostics.js +1 -0
  200. package/dist/core/model-router/config-diagnostics.js.map +1 -1
  201. package/dist/core/model-router/intent-classifier.d.ts +2 -0
  202. package/dist/core/model-router/intent-classifier.d.ts.map +1 -1
  203. package/dist/core/model-router/intent-classifier.js +154 -9
  204. package/dist/core/model-router/intent-classifier.js.map +1 -1
  205. package/dist/core/model-router/route-judge.d.ts +54 -0
  206. package/dist/core/model-router/route-judge.d.ts.map +1 -0
  207. package/dist/core/model-router/route-judge.js +128 -0
  208. package/dist/core/model-router/route-judge.js.map +1 -0
  209. package/dist/core/model-router/status.d.ts +4 -1
  210. package/dist/core/model-router/status.d.ts.map +1 -1
  211. package/dist/core/model-router/status.js +30 -6
  212. package/dist/core/model-router/status.js.map +1 -1
  213. package/dist/core/model-router/tool-escalation.d.ts +4 -6
  214. package/dist/core/model-router/tool-escalation.d.ts.map +1 -1
  215. package/dist/core/model-router/tool-escalation.js +1 -1
  216. package/dist/core/model-router/tool-escalation.js.map +1 -1
  217. package/dist/core/models/fitness-store.d.ts +40 -0
  218. package/dist/core/models/fitness-store.d.ts.map +1 -0
  219. package/dist/core/models/fitness-store.js +61 -0
  220. package/dist/core/models/fitness-store.js.map +1 -0
  221. package/dist/core/profile-registry.d.ts.map +1 -1
  222. package/dist/core/profile-registry.js +1 -1
  223. package/dist/core/profile-registry.js.map +1 -1
  224. package/dist/core/prompt-templates.d.ts +2 -0
  225. package/dist/core/prompt-templates.d.ts.map +1 -1
  226. package/dist/core/prompt-templates.js +12 -4
  227. package/dist/core/prompt-templates.js.map +1 -1
  228. package/dist/core/research/automata-provider.d.ts +5 -0
  229. package/dist/core/research/automata-provider.d.ts.map +1 -0
  230. package/dist/core/research/automata-provider.js +15 -0
  231. package/dist/core/research/automata-provider.js.map +1 -0
  232. package/dist/core/research/evidence-bundle.d.ts +10 -0
  233. package/dist/core/research/evidence-bundle.d.ts.map +1 -0
  234. package/dist/core/research/evidence-bundle.js +116 -0
  235. package/dist/core/research/evidence-bundle.js.map +1 -0
  236. package/dist/core/research/model-fitness.d.ts +79 -0
  237. package/dist/core/research/model-fitness.d.ts.map +1 -0
  238. package/dist/core/research/model-fitness.js +257 -0
  239. package/dist/core/research/model-fitness.js.map +1 -0
  240. package/dist/core/research/research-gate.d.ts +11 -0
  241. package/dist/core/research/research-gate.d.ts.map +1 -0
  242. package/dist/core/research/research-gate.js +82 -0
  243. package/dist/core/research/research-gate.js.map +1 -0
  244. package/dist/core/research/research-runner.d.ts +59 -0
  245. package/dist/core/research/research-runner.d.ts.map +1 -0
  246. package/dist/core/research/research-runner.js +155 -0
  247. package/dist/core/research/research-runner.js.map +1 -0
  248. package/dist/core/research/session-evidence-bundle.d.ts +11 -0
  249. package/dist/core/research/session-evidence-bundle.d.ts.map +1 -0
  250. package/dist/core/research/session-evidence-bundle.js +55 -0
  251. package/dist/core/research/session-evidence-bundle.js.map +1 -0
  252. package/dist/core/resource-loader.d.ts.map +1 -1
  253. package/dist/core/resource-loader.js +7 -1
  254. package/dist/core/resource-loader.js.map +1 -1
  255. package/dist/core/settings-manager.d.ts +147 -4
  256. package/dist/core/settings-manager.d.ts.map +1 -1
  257. package/dist/core/settings-manager.js +285 -9
  258. package/dist/core/settings-manager.js.map +1 -1
  259. package/dist/core/skills.d.ts +4 -0
  260. package/dist/core/skills.d.ts.map +1 -1
  261. package/dist/core/skills.js +18 -6
  262. package/dist/core/skills.js.map +1 -1
  263. package/dist/core/slash-commands.d.ts.map +1 -1
  264. package/dist/core/slash-commands.js +4 -0
  265. package/dist/core/slash-commands.js.map +1 -1
  266. package/dist/core/toolkit/script-registry.d.ts +34 -0
  267. package/dist/core/toolkit/script-registry.d.ts.map +1 -0
  268. package/dist/core/toolkit/script-registry.js +71 -0
  269. package/dist/core/toolkit/script-registry.js.map +1 -0
  270. package/dist/core/toolkit/script-runner.d.ts +28 -0
  271. package/dist/core/toolkit/script-runner.d.ts.map +1 -0
  272. package/dist/core/toolkit/script-runner.js +48 -0
  273. package/dist/core/toolkit/script-runner.js.map +1 -0
  274. package/dist/core/tools/artifact-retrieve.d.ts +23 -0
  275. package/dist/core/tools/artifact-retrieve.d.ts.map +1 -0
  276. package/dist/core/tools/artifact-retrieve.js +110 -0
  277. package/dist/core/tools/artifact-retrieve.js.map +1 -0
  278. package/dist/core/tools/delegate.d.ts +32 -0
  279. package/dist/core/tools/delegate.d.ts.map +1 -0
  280. package/dist/core/tools/delegate.js +60 -0
  281. package/dist/core/tools/delegate.js.map +1 -0
  282. package/dist/core/tools/fff-search-backend.d.ts +103 -0
  283. package/dist/core/tools/fff-search-backend.d.ts.map +1 -0
  284. package/dist/core/tools/fff-search-backend.js +151 -0
  285. package/dist/core/tools/fff-search-backend.js.map +1 -0
  286. package/dist/core/tools/find.d.ts +21 -1
  287. package/dist/core/tools/find.d.ts.map +1 -1
  288. package/dist/core/tools/find.js +183 -10
  289. package/dist/core/tools/find.js.map +1 -1
  290. package/dist/core/tools/goal.d.ts +35 -0
  291. package/dist/core/tools/goal.d.ts.map +1 -0
  292. package/dist/core/tools/goal.js +122 -0
  293. package/dist/core/tools/goal.js.map +1 -0
  294. package/dist/core/tools/grep.d.ts +21 -1
  295. package/dist/core/tools/grep.d.ts.map +1 -1
  296. package/dist/core/tools/grep.js +272 -27
  297. package/dist/core/tools/grep.js.map +1 -1
  298. package/dist/core/tools/index.d.ts +4 -1
  299. package/dist/core/tools/index.d.ts.map +1 -1
  300. package/dist/core/tools/index.js +9 -0
  301. package/dist/core/tools/index.js.map +1 -1
  302. package/dist/core/tools/model-fitness.d.ts +30 -0
  303. package/dist/core/tools/model-fitness.d.ts.map +1 -0
  304. package/dist/core/tools/model-fitness.js +38 -0
  305. package/dist/core/tools/model-fitness.js.map +1 -0
  306. package/dist/core/tools/run-toolkit-script.d.ts +24 -0
  307. package/dist/core/tools/run-toolkit-script.d.ts.map +1 -0
  308. package/dist/core/tools/run-toolkit-script.js +103 -0
  309. package/dist/core/tools/run-toolkit-script.js.map +1 -0
  310. package/dist/core/tools/search-router.d.ts +75 -0
  311. package/dist/core/tools/search-router.d.ts.map +1 -0
  312. package/dist/core/tools/search-router.js +85 -0
  313. package/dist/core/tools/search-router.js.map +1 -0
  314. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  315. package/dist/modes/interactive/components/footer.js +18 -16
  316. package/dist/modes/interactive/components/footer.js.map +1 -1
  317. package/dist/modes/interactive/components/settings-selector.d.ts +13 -1
  318. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  319. package/dist/modes/interactive/components/settings-selector.js +471 -11
  320. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  321. package/dist/modes/interactive/interactive-mode.d.ts +4 -0
  322. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  323. package/dist/modes/interactive/interactive-mode.js +220 -39
  324. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  325. package/dist/modes/print-mode.d.ts.map +1 -1
  326. package/dist/modes/print-mode.js +3 -0
  327. package/dist/modes/print-mode.js.map +1 -1
  328. package/dist/utils/tools-manager.d.ts +2 -0
  329. package/dist/utils/tools-manager.d.ts.map +1 -1
  330. package/dist/utils/tools-manager.js +154 -2
  331. package/dist/utils/tools-manager.js.map +1 -1
  332. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  333. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  334. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  335. package/examples/extensions/sandbox/package-lock.json +2 -2
  336. package/examples/extensions/sandbox/package.json +1 -1
  337. package/examples/extensions/with-deps/package-lock.json +2 -2
  338. package/examples/extensions/with-deps/package.json +1 -1
  339. package/npm-shrinkwrap.json +368 -12
  340. package/package.json +5 -4
@@ -0,0 +1,107 @@
1
+ const REDACTED = "[REDACTED]";
2
+ const CIRCULAR = "[Circular]";
3
+ const MAX_STRING_LENGTH = 200;
4
+ const SENSITIVE_KEYS = ["token", "secret", "key", "credential", "password", "authorization"];
5
+ const SENSITIVE_VALUE_REGEX = /bearer\s+[\w\-._]+|api[-_]?key[-_]?[\w\-._]+|sk-[\w\-._]+/i;
6
+ function formatCost(value) {
7
+ return Number.isFinite(value) ? `$${value.toFixed(4)}` : "$0.0000";
8
+ }
9
+ function redactAndTruncateString(value) {
10
+ if (SENSITIVE_VALUE_REGEX.test(value))
11
+ return REDACTED;
12
+ if (value.length <= MAX_STRING_LENGTH)
13
+ return value;
14
+ return `${value.slice(0, MAX_STRING_LENGTH - 1)}…`;
15
+ }
16
+ function isSensitiveKey(key) {
17
+ const lowerKey = key.toLowerCase();
18
+ return SENSITIVE_KEYS.some((sensitiveKey) => lowerKey.includes(sensitiveKey));
19
+ }
20
+ function sanitizeMetadataValue(value, seen) {
21
+ if (typeof value === "string")
22
+ return redactAndTruncateString(value);
23
+ if (typeof value !== "object" || value === null)
24
+ return value;
25
+ if (seen.has(value))
26
+ return CIRCULAR;
27
+ if (Array.isArray(value)) {
28
+ seen.add(value);
29
+ return value.map((item) => sanitizeMetadataValue(item, seen));
30
+ }
31
+ return sanitizeMetadata(value, seen);
32
+ }
33
+ function sanitizeMetadata(obj, seen = new WeakSet()) {
34
+ if (seen.has(obj))
35
+ return { value: CIRCULAR };
36
+ seen.add(obj);
37
+ const result = {};
38
+ for (const [key, value] of Object.entries(obj)) {
39
+ result[key] = isSensitiveKey(key) ? REDACTED : sanitizeMetadataValue(value, seen);
40
+ }
41
+ return result;
42
+ }
43
+ export function formatAutonomyStatus(args) {
44
+ const parts = [];
45
+ if (args.latestRoute) {
46
+ const risk = args.latestRoute.risk ? ` (${redactAndTruncateString(args.latestRoute.risk)})` : "";
47
+ parts.push(`Route: ${redactAndTruncateString(args.latestRoute.tier)}${risk} - ${redactAndTruncateString(args.latestRoute.reasonCode)}`);
48
+ }
49
+ if (args.latestGate) {
50
+ parts.push(`Gate: ${redactAndTruncateString(args.latestGate.gate)} = ${redactAndTruncateString(args.latestGate.outcome)} (${redactAndTruncateString(args.latestGate.reasonCode)})`);
51
+ }
52
+ const costs = [];
53
+ if (args.currentCostUsd !== undefined)
54
+ costs.push(`current: ${formatCost(args.currentCostUsd)}`);
55
+ if (args.dailyCostUsd !== undefined)
56
+ costs.push(`daily: ${formatCost(args.dailyCostUsd)}`);
57
+ if (args.spawnedCostUsd !== undefined)
58
+ costs.push(`spawned: ${formatCost(args.spawnedCostUsd)}`);
59
+ if (costs.length > 0) {
60
+ parts.push(`Costs: ${costs.join(", ")}`);
61
+ }
62
+ if (args.activeGoal) {
63
+ const goal = args.activeGoal;
64
+ const requirements = goal.openRequirements !== undefined ? `, open reqs: ${goal.openRequirements}` : "";
65
+ const stalls = goal.stallTurns !== undefined ? `, stalls: ${goal.stallTurns}` : "";
66
+ parts.push(`Goal [${redactAndTruncateString(goal.goalId)}]: ${redactAndTruncateString(goal.status)}${requirements}${stalls}`);
67
+ }
68
+ if (args.activeLaneCount !== undefined) {
69
+ parts.push(`Lanes: ${args.activeLaneCount}`);
70
+ }
71
+ if (parts.length === 0)
72
+ return "Autonomy: idle";
73
+ return parts.join(" | ");
74
+ }
75
+ function formatDiagnosticSection(name, entries) {
76
+ if (!entries || entries.length === 0)
77
+ return undefined;
78
+ const lines = [`--- ${name} ---`];
79
+ for (const entry of entries) {
80
+ let header = `- ${redactAndTruncateString(entry.title)}`;
81
+ if (entry.reasonCode)
82
+ header += ` [${redactAndTruncateString(entry.reasonCode)}]`;
83
+ lines.push(header);
84
+ if (entry.summary) {
85
+ lines.push(` Summary: ${redactAndTruncateString(entry.summary)}`);
86
+ }
87
+ if (entry.metadata) {
88
+ lines.push(` Metadata: ${JSON.stringify(sanitizeMetadata(entry.metadata))}`);
89
+ }
90
+ }
91
+ return lines.join("\n");
92
+ }
93
+ export function formatAutonomyDiagnostics(args) {
94
+ const sections = [
95
+ formatDiagnosticSection("Routes", args.routes),
96
+ formatDiagnosticSection("Gates", args.gates),
97
+ formatDiagnosticSection("Costs", args.costs),
98
+ formatDiagnosticSection("Research", args.research),
99
+ formatDiagnosticSection("Delegation", args.delegation),
100
+ formatDiagnosticSection("Learning", args.learning),
101
+ formatDiagnosticSection("Goals", args.goals),
102
+ ].filter((section) => Boolean(section));
103
+ if (sections.length === 0)
104
+ return "No diagnostics available.";
105
+ return sections.join("\n\n");
106
+ }
107
+ //# sourceMappingURL=status.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"status.js","sourceRoot":"","sources":["../../../src/core/autonomy/status.ts"],"names":[],"mappings":"AA2BA,MAAM,QAAQ,G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AAK,CAAC,IAAI,CAAC,eAAe,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC;QAC/E,CAAC;IACF,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAAA,CACxB;AAED,MAAM,UAAU,yBAAyB,CAAC,IAAgC,EAAU;IACnF,MAAM,QAAQ,GAAG;QAChB,uBAAuB,CAAC,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC;QAC9C,uBAAuB,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC;QAC5C,uBAAuB,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC;QAC5C,uBAAuB,CAAC,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC;QAClD,uBAAuB,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC;QACtD,uBAAuB,CAAC,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC;QAClD,uBAAuB,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC;KAC5C,CAAC,MAAM,CAAC,CAAC,OAAO,EAAqB,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAE3D,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,2BAA2B,CAAC;IAC9D,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAAA,CAC7B","sourcesContent":["export interface AutonomyStatusSnapshot {\n\tlatestRoute?: { tier: string; reasonCode: string; risk?: string };\n\tlatestGate?: { outcome: string; gate: string; reasonCode: string };\n\tcurrentCostUsd?: number;\n\tdailyCostUsd?: number;\n\tspawnedCostUsd?: number;\n\tactiveGoal?: { goalId: string; status: string; openRequirements?: number; stallTurns?: number };\n\tactiveLaneCount?: number;\n}\n\nexport interface DiagnosticEntry {\n\ttitle: string;\n\tsummary?: string;\n\treasonCode?: string;\n\tmetadata?: Record<string, unknown>;\n}\n\nexport interface AutonomyDiagnosticSnapshot {\n\troutes?: readonly DiagnosticEntry[];\n\tgates?: readonly DiagnosticEntry[];\n\tcosts?: readonly DiagnosticEntry[];\n\tresearch?: readonly DiagnosticEntry[];\n\tdelegation?: readonly DiagnosticEntry[];\n\tlearning?: readonly DiagnosticEntry[];\n\tgoals?: readonly DiagnosticEntry[];\n}\n\nconst REDACTED = \"[REDACTED]\";\nconst CIRCULAR = \"[Circular]\";\nconst MAX_STRING_LENGTH = 200;\nconst SENSITIVE_KEYS = [\"token\", \"secret\", \"key\", \"credential\", \"password\", \"authorization\"];\nconst SENSITIVE_VALUE_REGEX = /bearer\\s+[\\w\\-._]+|api[-_]?key[-_]?[\\w\\-._]+|sk-[\\w\\-._]+/i;\n\nfunction formatCost(value: number): string {\n\treturn Number.isFinite(value) ? `$${value.toFixed(4)}` : \"$0.0000\";\n}\n\nfunction redactAndTruncateString(value: string): string {\n\tif (SENSITIVE_VALUE_REGEX.test(value)) return REDACTED;\n\tif (value.length <= MAX_STRING_LENGTH) return value;\n\treturn `${value.slice(0, MAX_STRING_LENGTH - 1)}…`;\n}\n\nfunction isSensitiveKey(key: string): boolean {\n\tconst lowerKey = key.toLowerCase();\n\treturn SENSITIVE_KEYS.some((sensitiveKey) => lowerKey.includes(sensitiveKey));\n}\n\nfunction sanitizeMetadataValue(value: unknown, seen: WeakSet<object>): unknown {\n\tif (typeof value === \"string\") return redactAndTruncateString(value);\n\tif (typeof value !== \"object\" || value === null) return value;\n\tif (seen.has(value)) return CIRCULAR;\n\tif (Array.isArray(value)) {\n\t\tseen.add(value);\n\t\treturn value.map((item) => sanitizeMetadataValue(item, seen));\n\t}\n\treturn sanitizeMetadata(value as Record<string, unknown>, seen);\n}\n\nfunction sanitizeMetadata(\n\tobj: Record<string, unknown>,\n\tseen: WeakSet<object> = new WeakSet(),\n): Record<string, unknown> {\n\tif (seen.has(obj)) return { value: CIRCULAR };\n\tseen.add(obj);\n\tconst result: Record<string, unknown> = {};\n\tfor (const [key, value] of Object.entries(obj)) {\n\t\tresult[key] = isSensitiveKey(key) ? REDACTED : sanitizeMetadataValue(value, seen);\n\t}\n\treturn result;\n}\n\nexport function formatAutonomyStatus(args: AutonomyStatusSnapshot): string {\n\tconst parts: string[] = [];\n\n\tif (args.latestRoute) {\n\t\tconst risk = args.latestRoute.risk ? ` (${redactAndTruncateString(args.latestRoute.risk)})` : \"\";\n\t\tparts.push(\n\t\t\t`Route: ${redactAndTruncateString(args.latestRoute.tier)}${risk} - ${redactAndTruncateString(args.latestRoute.reasonCode)}`,\n\t\t);\n\t}\n\n\tif (args.latestGate) {\n\t\tparts.push(\n\t\t\t`Gate: ${redactAndTruncateString(args.latestGate.gate)} = ${redactAndTruncateString(args.latestGate.outcome)} (${redactAndTruncateString(args.latestGate.reasonCode)})`,\n\t\t);\n\t}\n\n\tconst costs: string[] = [];\n\tif (args.currentCostUsd !== undefined) costs.push(`current: ${formatCost(args.currentCostUsd)}`);\n\tif (args.dailyCostUsd !== undefined) costs.push(`daily: ${formatCost(args.dailyCostUsd)}`);\n\tif (args.spawnedCostUsd !== undefined) costs.push(`spawned: ${formatCost(args.spawnedCostUsd)}`);\n\tif (costs.length > 0) {\n\t\tparts.push(`Costs: ${costs.join(\", \")}`);\n\t}\n\n\tif (args.activeGoal) {\n\t\tconst goal = args.activeGoal;\n\t\tconst requirements = goal.openRequirements !== undefined ? `, open reqs: ${goal.openRequirements}` : \"\";\n\t\tconst stalls = goal.stallTurns !== undefined ? `, stalls: ${goal.stallTurns}` : \"\";\n\t\tparts.push(\n\t\t\t`Goal [${redactAndTruncateString(goal.goalId)}]: ${redactAndTruncateString(goal.status)}${requirements}${stalls}`,\n\t\t);\n\t}\n\n\tif (args.activeLaneCount !== undefined) {\n\t\tparts.push(`Lanes: ${args.activeLaneCount}`);\n\t}\n\n\tif (parts.length === 0) return \"Autonomy: idle\";\n\treturn parts.join(\" | \");\n}\n\nfunction formatDiagnosticSection(name: string, entries?: readonly DiagnosticEntry[]): string | undefined {\n\tif (!entries || entries.length === 0) return undefined;\n\n\tconst lines: string[] = [`--- ${name} ---`];\n\tfor (const entry of entries) {\n\t\tlet header = `- ${redactAndTruncateString(entry.title)}`;\n\t\tif (entry.reasonCode) header += ` [${redactAndTruncateString(entry.reasonCode)}]`;\n\t\tlines.push(header);\n\n\t\tif (entry.summary) {\n\t\t\tlines.push(` Summary: ${redactAndTruncateString(entry.summary)}`);\n\t\t}\n\n\t\tif (entry.metadata) {\n\t\t\tlines.push(` Metadata: ${JSON.stringify(sanitizeMetadata(entry.metadata))}`);\n\t\t}\n\t}\n\treturn lines.join(\"\\n\");\n}\n\nexport function formatAutonomyDiagnostics(args: AutonomyDiagnosticSnapshot): string {\n\tconst sections = [\n\t\tformatDiagnosticSection(\"Routes\", args.routes),\n\t\tformatDiagnosticSection(\"Gates\", args.gates),\n\t\tformatDiagnosticSection(\"Costs\", args.costs),\n\t\tformatDiagnosticSection(\"Research\", args.research),\n\t\tformatDiagnosticSection(\"Delegation\", args.delegation),\n\t\tformatDiagnosticSection(\"Learning\", args.learning),\n\t\tformatDiagnosticSection(\"Goals\", args.goals),\n\t].filter((section): section is string => Boolean(section));\n\n\tif (sections.length === 0) return \"No diagnostics available.\";\n\treturn sections.join(\"\\n\\n\");\n}\n"]}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Subagent system-prompt composition with an irreducible level-0 core.
3
+ *
4
+ * The core is the "ultimate level-0 default": ~80 tokens of non-negotiable rules that survive ANY
5
+ * customization. Everything above it — the lane's role prompt and a shipped profile's soul — is a
6
+ * replaceable layer: settings, a lane profile, or the calling model (delegate tool) can erase and
7
+ * replace it entirely. This keeps shipped subagents maximally efficient on small open models (a
8
+ * caller can hand a tiny model a purpose-built minimal prompt) without ever shedding the safety
9
+ * floor. Keep the core UNDER 300 tokens; it is deliberately terse.
10
+ */
11
+ export declare const SUBAGENT_CORE_SYSTEM_PROMPT: string;
12
+ export interface SubagentPromptParts {
13
+ /** Situational identity from the shipped profile (replaceable layer). */
14
+ soul?: string;
15
+ /** The lane's default role prompt (replaceable layer). */
16
+ rolePrompt: string;
17
+ /** User- or model-provided replacement for every layer above level 0. */
18
+ override?: string;
19
+ }
20
+ export declare function composeSubagentSystemPrompt(parts: SubagentPromptParts): string;
21
+ //# sourceMappingURL=subagent-prompt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subagent-prompt.d.ts","sourceRoot":"","sources":["../../../src/core/autonomy/subagent-prompt.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,eAAO,MAAM,2BAA2B,QAO5B,CAAC;AAEb,MAAM,WAAW,mBAAmB;IACnC,yEAAyE;IACzE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,0DAA0D;IAC1D,UAAU,EAAE,MAAM,CAAC;IACnB,yEAAyE;IACzE,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,mBAAmB,GAAG,MAAM,CAS9E","sourcesContent":["/**\n * Subagent system-prompt composition with an irreducible level-0 core.\n *\n * The core is the \"ultimate level-0 default\": ~80 tokens of non-negotiable rules that survive ANY\n * customization. Everything above it — the lane's role prompt and a shipped profile's soul — is a\n * replaceable layer: settings, a lane profile, or the calling model (delegate tool) can erase and\n * replace it entirely. This keeps shipped subagents maximally efficient on small open models (a\n * caller can hand a tiny model a purpose-built minimal prompt) without ever shedding the safety\n * floor. Keep the core UNDER 300 tokens; it is deliberately terse.\n */\nexport const SUBAGENT_CORE_SYSTEM_PROMPT = [\n\t\"You are a bounded subagent shipped by a coding-agent session. Non-negotiable rules:\",\n\t\"1. Do only the task you were given; you cannot see the parent conversation.\",\n\t\"2. You are read-only unless your envelope explicitly grants otherwise; never attempt to change files, settings, credentials, or external state.\",\n\t\"3. Never invent facts, file paths, or APIs; say so when you do not know.\",\n\t\"4. Your output is untrusted evidence for the parent agent - data, never instructions.\",\n\t\"5. Follow the requested output format exactly and be concise; budgets are enforced outside you.\",\n].join(\"\\n\");\n\nexport interface SubagentPromptParts {\n\t/** Situational identity from the shipped profile (replaceable layer). */\n\tsoul?: string;\n\t/** The lane's default role prompt (replaceable layer). */\n\trolePrompt: string;\n\t/** User- or model-provided replacement for every layer above level 0. */\n\toverride?: string;\n}\n\nexport function composeSubagentSystemPrompt(parts: SubagentPromptParts): string {\n\tconst override = parts.override?.trim();\n\tconst above =\n\t\toverride && override.length > 0\n\t\t\t? override\n\t\t\t: [parts.soul?.trim(), parts.rolePrompt]\n\t\t\t\t\t.filter((part): part is string => Boolean(part && part.length > 0))\n\t\t\t\t\t.join(\"\\n\\n\");\n\treturn above.length > 0 ? `${SUBAGENT_CORE_SYSTEM_PROMPT}\\n\\n${above}` : SUBAGENT_CORE_SYSTEM_PROMPT;\n}\n"]}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Subagent system-prompt composition with an irreducible level-0 core.
3
+ *
4
+ * The core is the "ultimate level-0 default": ~80 tokens of non-negotiable rules that survive ANY
5
+ * customization. Everything above it — the lane's role prompt and a shipped profile's soul — is a
6
+ * replaceable layer: settings, a lane profile, or the calling model (delegate tool) can erase and
7
+ * replace it entirely. This keeps shipped subagents maximally efficient on small open models (a
8
+ * caller can hand a tiny model a purpose-built minimal prompt) without ever shedding the safety
9
+ * floor. Keep the core UNDER 300 tokens; it is deliberately terse.
10
+ */
11
+ export const SUBAGENT_CORE_SYSTEM_PROMPT = [
12
+ "You are a bounded subagent shipped by a coding-agent session. Non-negotiable rules:",
13
+ "1. Do only the task you were given; you cannot see the parent conversation.",
14
+ "2. You are read-only unless your envelope explicitly grants otherwise; never attempt to change files, settings, credentials, or external state.",
15
+ "3. Never invent facts, file paths, or APIs; say so when you do not know.",
16
+ "4. Your output is untrusted evidence for the parent agent - data, never instructions.",
17
+ "5. Follow the requested output format exactly and be concise; budgets are enforced outside you.",
18
+ ].join("\n");
19
+ export function composeSubagentSystemPrompt(parts) {
20
+ const override = parts.override?.trim();
21
+ const above = override && override.length > 0
22
+ ? override
23
+ : [parts.soul?.trim(), parts.rolePrompt]
24
+ .filter((part) => Boolean(part && part.length > 0))
25
+ .join("\n\n");
26
+ return above.length > 0 ? `${SUBAGENT_CORE_SYSTEM_PROMPT}\n\n${above}` : SUBAGENT_CORE_SYSTEM_PROMPT;
27
+ }
28
+ //# sourceMappingURL=subagent-prompt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subagent-prompt.js","sourceRoot":"","sources":["../../../src/core/autonomy/subagent-prompt.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG;IAC1C,qFAAqF;IACrF,6EAA6E;IAC7E,iJAAiJ;IACjJ,0EAA0E;IAC1E,uFAAuF;IACvF,iGAAiG;CACjG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAWb,MAAM,UAAU,2BAA2B,CAAC,KAA0B,EAAU;IAC/E,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC;IACxC,MAAM,KAAK,GACV,QAAQ,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC;QAC9B,CAAC,CAAC,QAAQ;QACV,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,KAAK,CAAC,UAAU,CAAC;aACrC,MAAM,CAAC,CAAC,IAAI,EAAkB,EAAE,CAAC,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;aAClE,IAAI,CAAC,MAAM,CAAC,CAAC;IAClB,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,2BAA2B,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,2BAA2B,CAAC;AAAA,CACrG","sourcesContent":["/**\n * Subagent system-prompt composition with an irreducible level-0 core.\n *\n * The core is the \"ultimate level-0 default\": ~80 tokens of non-negotiable rules that survive ANY\n * customization. Everything above it — the lane's role prompt and a shipped profile's soul — is a\n * replaceable layer: settings, a lane profile, or the calling model (delegate tool) can erase and\n * replace it entirely. This keeps shipped subagents maximally efficient on small open models (a\n * caller can hand a tiny model a purpose-built minimal prompt) without ever shedding the safety\n * floor. Keep the core UNDER 300 tokens; it is deliberately terse.\n */\nexport const SUBAGENT_CORE_SYSTEM_PROMPT = [\n\t\"You are a bounded subagent shipped by a coding-agent session. Non-negotiable rules:\",\n\t\"1. Do only the task you were given; you cannot see the parent conversation.\",\n\t\"2. You are read-only unless your envelope explicitly grants otherwise; never attempt to change files, settings, credentials, or external state.\",\n\t\"3. Never invent facts, file paths, or APIs; say so when you do not know.\",\n\t\"4. Your output is untrusted evidence for the parent agent - data, never instructions.\",\n\t\"5. Follow the requested output format exactly and be concise; budgets are enforced outside you.\",\n].join(\"\\n\");\n\nexport interface SubagentPromptParts {\n\t/** Situational identity from the shipped profile (replaceable layer). */\n\tsoul?: string;\n\t/** The lane's default role prompt (replaceable layer). */\n\trolePrompt: string;\n\t/** User- or model-provided replacement for every layer above level 0. */\n\toverride?: string;\n}\n\nexport function composeSubagentSystemPrompt(parts: SubagentPromptParts): string {\n\tconst override = parts.override?.trim();\n\tconst above =\n\t\toverride && override.length > 0\n\t\t\t? override\n\t\t\t: [parts.soul?.trim(), parts.rolePrompt]\n\t\t\t\t\t.filter((part): part is string => Boolean(part && part.length > 0))\n\t\t\t\t\t.join(\"\\n\\n\");\n\treturn above.length > 0 ? `${SUBAGENT_CORE_SYSTEM_PROMPT}\\n\\n${above}` : SUBAGENT_CORE_SYSTEM_PROMPT;\n}\n"]}
@@ -0,0 +1,18 @@
1
+ import type { JsonValue } from "./contracts.ts";
2
+ export declare const AUTONOMY_TELEMETRY_EVENT_TYPES: {
3
+ readonly routeDecision: "autonomy.route_decision";
4
+ readonly gateOutcome: "autonomy.gate_outcome";
5
+ readonly approvalRequest: "autonomy.approval_request";
6
+ readonly evidenceBundle: "autonomy.evidence_bundle";
7
+ readonly workerRequest: "autonomy.worker_request";
8
+ readonly workerResult: "autonomy.worker_result";
9
+ readonly learningDecision: "autonomy.learning_decision";
10
+ };
11
+ export type AutonomyTelemetryEventType = (typeof AUTONOMY_TELEMETRY_EVENT_TYPES)[keyof typeof AUTONOMY_TELEMETRY_EVENT_TYPES];
12
+ export interface AutonomyTelemetryEvent {
13
+ type: AutonomyTelemetryEventType;
14
+ timestamp: string;
15
+ payload: JsonValue;
16
+ }
17
+ export declare function redactTelemetryValue(value: unknown, depth?: number): unknown;
18
+ //# sourceMappingURL=telemetry-events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry-events.d.ts","sourceRoot":"","sources":["../../../src/core/autonomy/telemetry-events.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD,eAAO,MAAM,8BAA8B;;;;;;;;CAQjC,CAAC;AAEX,MAAM,MAAM,0BAA0B,GACrC,CAAC,OAAO,8BAA8B,CAAC,CAAC,MAAM,OAAO,8BAA8B,CAAC,CAAC;AAEtF,MAAM,WAAW,sBAAsB;IACtC,IAAI,EAAE,0BAA0B,CAAC;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,SAAS,CAAC;CACnB;AAiBD,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,SAAI,GAAG,OAAO,CAyCvE","sourcesContent":["import type { JsonValue } from \"./contracts.ts\";\n\nexport const AUTONOMY_TELEMETRY_EVENT_TYPES = {\n\trouteDecision: \"autonomy.route_decision\",\n\tgateOutcome: \"autonomy.gate_outcome\",\n\tapprovalRequest: \"autonomy.approval_request\",\n\tevidenceBundle: \"autonomy.evidence_bundle\",\n\tworkerRequest: \"autonomy.worker_request\",\n\tworkerResult: \"autonomy.worker_result\",\n\tlearningDecision: \"autonomy.learning_decision\",\n} as const;\n\nexport type AutonomyTelemetryEventType =\n\t(typeof AUTONOMY_TELEMETRY_EVENT_TYPES)[keyof typeof AUTONOMY_TELEMETRY_EVENT_TYPES];\n\nexport interface AutonomyTelemetryEvent {\n\ttype: AutonomyTelemetryEventType;\n\ttimestamp: string;\n\tpayload: JsonValue;\n}\n\nconst SECRET_KEYS = new Set([\n\t\"apikey\",\n\t\"api_key\",\n\t\"token\",\n\t\"accesstoken\",\n\t\"refreshtoken\",\n\t\"secret\",\n\t\"password\",\n\t\"authorization\",\n\t\"credential\",\n\t\"credentials\",\n]);\n\nconst BEARER_RE = /bearer\\s+\\S+/i;\n\nexport function redactTelemetryValue(value: unknown, depth = 0): unknown {\n\tif (depth > 20) {\n\t\treturn \"[Depth Limit Exceeded]\";\n\t}\n\n\tif (typeof value === \"string\") {\n\t\tif (BEARER_RE.test(value)) {\n\t\t\treturn \"[REDACTED BEARER TOKEN]\";\n\t\t}\n\t\tif (value.startsWith(\"sk-\")) {\n\t\t\treturn \"[REDACTED API KEY]\";\n\t\t}\n\t\treturn value;\n\t}\n\n\tif (value === null || typeof value !== \"object\") {\n\t\treturn value;\n\t}\n\n\tif (Array.isArray(value)) {\n\t\tconst result: unknown[] = [];\n\t\tfor (const item of value) {\n\t\t\tresult.push(redactTelemetryValue(item, depth + 1));\n\t\t}\n\t\treturn result;\n\t}\n\n\t// It is an object\n\tconst obj = value as Record<string, unknown>;\n\tconst result: Record<string, unknown> = {};\n\n\tfor (const key of Object.keys(obj)) {\n\t\tconst lowercaseKey = key.toLowerCase();\n\t\tif (SECRET_KEYS.has(lowercaseKey)) {\n\t\t\tresult[key] = \"[REDACTED]\";\n\t\t} else {\n\t\t\tresult[key] = redactTelemetryValue(obj[key], depth + 1);\n\t\t}\n\t}\n\n\treturn result;\n}\n"]}
@@ -0,0 +1,60 @@
1
+ export const AUTONOMY_TELEMETRY_EVENT_TYPES = {
2
+ routeDecision: "autonomy.route_decision",
3
+ gateOutcome: "autonomy.gate_outcome",
4
+ approvalRequest: "autonomy.approval_request",
5
+ evidenceBundle: "autonomy.evidence_bundle",
6
+ workerRequest: "autonomy.worker_request",
7
+ workerResult: "autonomy.worker_result",
8
+ learningDecision: "autonomy.learning_decision",
9
+ };
10
+ const SECRET_KEYS = new Set([
11
+ "apikey",
12
+ "api_key",
13
+ "token",
14
+ "accesstoken",
15
+ "refreshtoken",
16
+ "secret",
17
+ "password",
18
+ "authorization",
19
+ "credential",
20
+ "credentials",
21
+ ]);
22
+ const BEARER_RE = /bearer\s+\S+/i;
23
+ export function redactTelemetryValue(value, depth = 0) {
24
+ if (depth > 20) {
25
+ return "[Depth Limit Exceeded]";
26
+ }
27
+ if (typeof value === "string") {
28
+ if (BEARER_RE.test(value)) {
29
+ return "[REDACTED BEARER TOKEN]";
30
+ }
31
+ if (value.startsWith("sk-")) {
32
+ return "[REDACTED API KEY]";
33
+ }
34
+ return value;
35
+ }
36
+ if (value === null || typeof value !== "object") {
37
+ return value;
38
+ }
39
+ if (Array.isArray(value)) {
40
+ const result = [];
41
+ for (const item of value) {
42
+ result.push(redactTelemetryValue(item, depth + 1));
43
+ }
44
+ return result;
45
+ }
46
+ // It is an object
47
+ const obj = value;
48
+ const result = {};
49
+ for (const key of Object.keys(obj)) {
50
+ const lowercaseKey = key.toLowerCase();
51
+ if (SECRET_KEYS.has(lowercaseKey)) {
52
+ result[key] = "[REDACTED]";
53
+ }
54
+ else {
55
+ result[key] = redactTelemetryValue(obj[key], depth + 1);
56
+ }
57
+ }
58
+ return result;
59
+ }
60
+ //# sourceMappingURL=telemetry-events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry-events.js","sourceRoot":"","sources":["../../../src/core/autonomy/telemetry-events.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,8BAA8B,GAAG;IAC7C,aAAa,EAAE,yBAAyB;IACxC,WAAW,EAAE,uBAAuB;IACpC,eAAe,EAAE,2BAA2B;IAC5C,cAAc,EAAE,0BAA0B;IAC1C,aAAa,EAAE,yBAAyB;IACxC,YAAY,EAAE,wBAAwB;IACtC,gBAAgB,EAAE,4BAA4B;CACrC,CAAC;AAWX,MAAM,W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AAE,CAAC;QACvC,IAAI,WAAW,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;YACnC,MAAM,CAAC,GAAG,CAAC,GAAG,YAAY,CAAC;QAC5B,CAAC;aAAM,CAAC;YACP,MAAM,CAAC,GAAG,CAAC,GAAG,oBAAoB,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QACzD,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AAAA,CACd","sourcesContent":["import type { JsonValue } from \"./contracts.ts\";\n\nexport const AUTONOMY_TELEMETRY_EVENT_TYPES = {\n\trouteDecision: \"autonomy.route_decision\",\n\tgateOutcome: \"autonomy.gate_outcome\",\n\tapprovalRequest: \"autonomy.approval_request\",\n\tevidenceBundle: \"autonomy.evidence_bundle\",\n\tworkerRequest: \"autonomy.worker_request\",\n\tworkerResult: \"autonomy.worker_result\",\n\tlearningDecision: \"autonomy.learning_decision\",\n} as const;\n\nexport type AutonomyTelemetryEventType =\n\t(typeof AUTONOMY_TELEMETRY_EVENT_TYPES)[keyof typeof AUTONOMY_TELEMETRY_EVENT_TYPES];\n\nexport interface AutonomyTelemetryEvent {\n\ttype: AutonomyTelemetryEventType;\n\ttimestamp: string;\n\tpayload: JsonValue;\n}\n\nconst SECRET_KEYS = new Set([\n\t\"apikey\",\n\t\"api_key\",\n\t\"token\",\n\t\"accesstoken\",\n\t\"refreshtoken\",\n\t\"secret\",\n\t\"password\",\n\t\"authorization\",\n\t\"credential\",\n\t\"credentials\",\n]);\n\nconst BEARER_RE = /bearer\\s+\\S+/i;\n\nexport function redactTelemetryValue(value: unknown, depth = 0): unknown {\n\tif (depth > 20) {\n\t\treturn \"[Depth Limit Exceeded]\";\n\t}\n\n\tif (typeof value === \"string\") {\n\t\tif (BEARER_RE.test(value)) {\n\t\t\treturn \"[REDACTED BEARER TOKEN]\";\n\t\t}\n\t\tif (value.startsWith(\"sk-\")) {\n\t\t\treturn \"[REDACTED API KEY]\";\n\t\t}\n\t\treturn value;\n\t}\n\n\tif (value === null || typeof value !== \"object\") {\n\t\treturn value;\n\t}\n\n\tif (Array.isArray(value)) {\n\t\tconst result: unknown[] = [];\n\t\tfor (const item of value) {\n\t\t\tresult.push(redactTelemetryValue(item, depth + 1));\n\t\t}\n\t\treturn result;\n\t}\n\n\t// It is an object\n\tconst obj = value as Record<string, unknown>;\n\tconst result: Record<string, unknown> = {};\n\n\tfor (const key of Object.keys(obj)) {\n\t\tconst lowercaseKey = key.toLowerCase();\n\t\tif (SECRET_KEYS.has(lowercaseKey)) {\n\t\t\tresult[key] = \"[REDACTED]\";\n\t\t} else {\n\t\t\tresult[key] = redactTelemetryValue(obj[key], depth + 1);\n\t\t}\n\t}\n\n\treturn result;\n}\n"]}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Bounded artifact retrieval (Phase 8-style helper landing early, per D2b): resolve an
3
+ * artifact id shown in a "Full output: artifact tool-output:<id>" notice back into a
4
+ * small, useful slice. Per tool-output-artifacts.md's retrieval behavior: retrieve the
5
+ * smallest useful slice by default (metadata, or a bounded head/tail).
6
+ *
7
+ * Bounds are hard limits, not just defaults: a caller-provided `maxLines`/`maxBytes` is
8
+ * clamped to `MAX_RETRIEVAL_LINES`/`MAX_RETRIEVAL_BYTES` before use, so no caller --
9
+ * including a future agent-facing tool wrapper -- can force a large artifact to be fully
10
+ * rehydrated in one call by simply requesting a larger bound. A small artifact that
11
+ * already fits within the bound is still returned in full; the guarantee is "never more
12
+ * than the configured hard bounds," not "never the whole artifact."
13
+ */
14
+ import { type TruncationResult } from "../tools/truncate.ts";
15
+ import type { ArtifactStore } from "./context-artifacts.ts";
16
+ import { type MissingArtifactReason } from "./context-artifacts.ts";
17
+ import type { ContextArtifactRef } from "./context-item.ts";
18
+ export type ArtifactRetrievalMode = "metadata" | "head" | "tail";
19
+ export declare const DEFAULT_RETRIEVAL_MAX_LINES = 200;
20
+ /** Hard ceilings: a caller-requested maxLines/maxBytes can never exceed these. */
21
+ export declare const MAX_RETRIEVAL_LINES = 2000;
22
+ export declare const MAX_RETRIEVAL_BYTES: number;
23
+ export interface ArtifactRetrievalRequest {
24
+ artifactId: string;
25
+ mode?: ArtifactRetrievalMode;
26
+ maxLines?: number;
27
+ maxBytes?: number;
28
+ }
29
+ export type ArtifactRetrievalResult = {
30
+ found: false;
31
+ missingReason: MissingArtifactReason;
32
+ } | {
33
+ found: true;
34
+ mode: "metadata";
35
+ ref: ContextArtifactRef;
36
+ } | {
37
+ found: true;
38
+ mode: "head" | "tail";
39
+ ref: ContextArtifactRef;
40
+ slice: string;
41
+ truncation: TruncationResult;
42
+ };
43
+ /**
44
+ * Resolve `request.artifactId` against `store` and return a bounded slice. `maxLines`/
45
+ * `maxBytes` are hard-clamped to `MAX_RETRIEVAL_LINES`/`MAX_RETRIEVAL_BYTES` regardless of
46
+ * what the caller requests -- see the module doc comment for the exact guarantee.
47
+ */
48
+ export declare function retrieveArtifactSlice(store: ArtifactStore, request: ArtifactRetrievalRequest): ArtifactRetrievalResult;
49
+ //# sourceMappingURL=artifact-retrieval.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"artifact-retrieval.d.ts","sourceRoot":"","sources":["../../../src/core/context/artifact-retrieval.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAqB,KAAK,gBAAgB,EAA8B,MAAM,sBAAsB,CAAC;AAC5G,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAA2B,KAAK,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC7F,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAE5D,MAAM,MAAM,qBAAqB,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAEjE,eAAO,MAAM,2BAA2B,MAAM,CAAC;AAE/C,kFAAkF;AAClF,eAAO,MAAM,mBAAmB,OAAO,CAAC;AACxC,eAAO,MAAM,mBAAmB,QAAoB,CAAC;AAErD,MAAM,WAAW,wBAAwB;IACxC,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,qBAAqB,CAAC;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,MAAM,uBAAuB,GAChC;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,aAAa,EAAE,qBAAqB,CAAA;CAAE,GACtD;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,UAAU,CAAC;IAAC,GAAG,EAAE,kBAAkB,CAAA;CAAE,GAC1D;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IAAC,GAAG,EAAE,kBAAkB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,gBAAgB,CAAA;CAAE,CAAC;AAQhH;;;;GAIG;AACH,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,aAAa,EACpB,OAAO,EAAE,wBAAwB,GAC/B,uBAAuB,CAqBzB","sourcesContent":["/**\n * Bounded artifact retrieval (Phase 8-style helper landing early, per D2b): resolve an\n * artifact id shown in a \"Full output: artifact tool-output:<id>\" notice back into a\n * small, useful slice. Per tool-output-artifacts.md's retrieval behavior: retrieve the\n * smallest useful slice by default (metadata, or a bounded head/tail).\n *\n * Bounds are hard limits, not just defaults: a caller-provided `maxLines`/`maxBytes` is\n * clamped to `MAX_RETRIEVAL_LINES`/`MAX_RETRIEVAL_BYTES` before use, so no caller --\n * including a future agent-facing tool wrapper -- can force a large artifact to be fully\n * rehydrated in one call by simply requesting a larger bound. A small artifact that\n * already fits within the bound is still returned in full; the guarantee is \"never more\n * than the configured hard bounds,\" not \"never the whole artifact.\"\n */\n\nimport { DEFAULT_MAX_BYTES, type TruncationResult, truncateHead, truncateTail } from \"../tools/truncate.ts\";\nimport type { ArtifactStore } from \"./context-artifacts.ts\";\nimport { isMissingArtifactMarker, type MissingArtifactReason } from \"./context-artifacts.ts\";\nimport type { ContextArtifactRef } from \"./context-item.ts\";\n\nexport type ArtifactRetrievalMode = \"metadata\" | \"head\" | \"tail\";\n\nexport const DEFAULT_RETRIEVAL_MAX_LINES = 200;\n\n/** Hard ceilings: a caller-requested maxLines/maxBytes can never exceed these. */\nexport const MAX_RETRIEVAL_LINES = 2000;\nexport const MAX_RETRIEVAL_BYTES = DEFAULT_MAX_BYTES;\n\nexport interface ArtifactRetrievalRequest {\n\tartifactId: string;\n\tmode?: ArtifactRetrievalMode;\n\tmaxLines?: number;\n\tmaxBytes?: number;\n}\n\nexport type ArtifactRetrievalResult =\n\t| { found: false; missingReason: MissingArtifactReason }\n\t| { found: true; mode: \"metadata\"; ref: ContextArtifactRef }\n\t| { found: true; mode: \"head\" | \"tail\"; ref: ContextArtifactRef; slice: string; truncation: TruncationResult };\n\nfunction clampToHardCeiling(requested: number | undefined, fallback: number, hardCeiling: number): number {\n\tconst candidate = requested ?? fallback;\n\tif (candidate <= 0) return 0;\n\treturn Math.min(candidate, hardCeiling);\n}\n\n/**\n * Resolve `request.artifactId` against `store` and return a bounded slice. `maxLines`/\n * `maxBytes` are hard-clamped to `MAX_RETRIEVAL_LINES`/`MAX_RETRIEVAL_BYTES` regardless of\n * what the caller requests -- see the module doc comment for the exact guarantee.\n */\nexport function retrieveArtifactSlice(\n\tstore: ArtifactStore,\n\trequest: ArtifactRetrievalRequest,\n): ArtifactRetrievalResult {\n\tconst record = store.read(request.artifactId);\n\tif (isMissingArtifactMarker(record)) {\n\t\treturn { found: false, missingReason: record.reason };\n\t}\n\n\tconst mode = request.mode ?? \"head\";\n\tif (mode === \"metadata\") {\n\t\treturn { found: true, mode: \"metadata\", ref: record.ref };\n\t}\n\n\tconst truncationOptions = {\n\t\tmaxLines: clampToHardCeiling(request.maxLines, DEFAULT_RETRIEVAL_MAX_LINES, MAX_RETRIEVAL_LINES),\n\t\tmaxBytes: clampToHardCeiling(request.maxBytes, DEFAULT_MAX_BYTES, MAX_RETRIEVAL_BYTES),\n\t};\n\tconst truncation =\n\t\tmode === \"tail\"\n\t\t\t? truncateTail(record.content, truncationOptions)\n\t\t\t: truncateHead(record.content, truncationOptions);\n\n\treturn { found: true, mode, ref: record.ref, slice: truncation.content, truncation };\n}\n"]}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Bounded artifact retrieval (Phase 8-style helper landing early, per D2b): resolve an
3
+ * artifact id shown in a "Full output: artifact tool-output:<id>" notice back into a
4
+ * small, useful slice. Per tool-output-artifacts.md's retrieval behavior: retrieve the
5
+ * smallest useful slice by default (metadata, or a bounded head/tail).
6
+ *
7
+ * Bounds are hard limits, not just defaults: a caller-provided `maxLines`/`maxBytes` is
8
+ * clamped to `MAX_RETRIEVAL_LINES`/`MAX_RETRIEVAL_BYTES` before use, so no caller --
9
+ * including a future agent-facing tool wrapper -- can force a large artifact to be fully
10
+ * rehydrated in one call by simply requesting a larger bound. A small artifact that
11
+ * already fits within the bound is still returned in full; the guarantee is "never more
12
+ * than the configured hard bounds," not "never the whole artifact."
13
+ */
14
+ import { DEFAULT_MAX_BYTES, truncateHead, truncateTail } from "../tools/truncate.js";
15
+ import { isMissingArtifactMarker } from "./context-artifacts.js";
16
+ export const DEFAULT_RETRIEVAL_MAX_LINES = 200;
17
+ /** Hard ceilings: a caller-requested maxLines/maxBytes can never exceed these. */
18
+ export const MAX_RETRIEVAL_LINES = 2000;
19
+ export const MAX_RETRIEVAL_BYTES = DEFAULT_MAX_BYTES;
20
+ function clampToHardCeiling(requested, fallback, hardCeiling) {
21
+ const candidate = requested ?? fallback;
22
+ if (candidate <= 0)
23
+ return 0;
24
+ return Math.min(candidate, hardCeiling);
25
+ }
26
+ /**
27
+ * Resolve `request.artifactId` against `store` and return a bounded slice. `maxLines`/
28
+ * `maxBytes` are hard-clamped to `MAX_RETRIEVAL_LINES`/`MAX_RETRIEVAL_BYTES` regardless of
29
+ * what the caller requests -- see the module doc comment for the exact guarantee.
30
+ */
31
+ export function retrieveArtifactSlice(store, request) {
32
+ const record = store.read(request.artifactId);
33
+ if (isMissingArtifactMarker(record)) {
34
+ return { found: false, missingReason: record.reason };
35
+ }
36
+ const mode = request.mode ?? "head";
37
+ if (mode === "metadata") {
38
+ return { found: true, mode: "metadata", ref: record.ref };
39
+ }
40
+ const truncationOptions = {
41
+ maxLines: clampToHardCeiling(request.maxLines, DEFAULT_RETRIEVAL_MAX_LINES, MAX_RETRIEVAL_LINES),
42
+ maxBytes: clampToHardCeiling(request.maxBytes, DEFAULT_MAX_BYTES, MAX_RETRIEVAL_BYTES),
43
+ };
44
+ const truncation = mode === "tail"
45
+ ? truncateTail(record.content, truncationOptions)
46
+ : truncateHead(record.content, truncationOptions);
47
+ return { found: true, mode, ref: record.ref, slice: truncation.content, truncation };
48
+ }
49
+ //# sourceMappingURL=artifact-retrieval.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"artifact-retrieval.js","sourceRoot":"","sources":["../../../src/core/context/artifact-retrieval.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,iBAAiB,EAAyB,YAAY,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAE5G,OAAO,EAAE,uBAAuB,EAA8B,MAAM,wBAAwB,CAAC;AAK7F,MAAM,CAAC,MAAM,2BAA2B,GAAG,GAAG,CAAC;AAE/C,kFAAkF;AAClF,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,CAAC;AACxC,MAAM,CAAC,MAAM,mBAAmB,GAAG,iBAAiB,CAAC;AAcrD,SAAS,kBAAkB,CAAC,SAA6B,EAAE,QAAgB,EAAE,WAAmB,EAAU;IACzG,MAAM,SAAS,GAAG,SAAS,IAAI,QAAQ,CAAC;IACxC,IAAI,SAAS,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC;IAC7B,OAAO,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;AAAA,CACxC;AAED;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CACpC,KAAoB,EACpB,OAAiC,EACP;IAC1B,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAC9C,IAAI,uBAAuB,CAAC,MAAM,CAAC,EAAE,CAAC;QACrC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;IACvD,CAAC;IAED,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,MAAM,CAAC;IACpC,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;QACzB,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,CAAC;IAC3D,CAAC;IAED,MAAM,iBAAiB,GAAG;QACzB,QAAQ,EAAE,kBAAkB,CAAC,OAAO,CAAC,QAAQ,EAAE,2BAA2B,EAAE,mBAAmB,CAAC;QAChG,QAAQ,EAAE,kBAAkB,CAAC,OAAO,CAAC,QAAQ,EAAE,iBAAiB,EAAE,mBAAmB,CAAC;KACtF,CAAC;IACF,MAAM,UAAU,GACf,IAAI,KAAK,MAAM;QACd,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,OAAO,EAAE,iBAAiB,CAAC;QACjD,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IAEpD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,KAAK,EAAE,UAAU,CAAC,OAAO,EAAE,UAAU,EAAE,CAAC;AAAA,CACrF","sourcesContent":["/**\n * Bounded artifact retrieval (Phase 8-style helper landing early, per D2b): resolve an\n * artifact id shown in a \"Full output: artifact tool-output:<id>\" notice back into a\n * small, useful slice. Per tool-output-artifacts.md's retrieval behavior: retrieve the\n * smallest useful slice by default (metadata, or a bounded head/tail).\n *\n * Bounds are hard limits, not just defaults: a caller-provided `maxLines`/`maxBytes` is\n * clamped to `MAX_RETRIEVAL_LINES`/`MAX_RETRIEVAL_BYTES` before use, so no caller --\n * including a future agent-facing tool wrapper -- can force a large artifact to be fully\n * rehydrated in one call by simply requesting a larger bound. A small artifact that\n * already fits within the bound is still returned in full; the guarantee is \"never more\n * than the configured hard bounds,\" not \"never the whole artifact.\"\n */\n\nimport { DEFAULT_MAX_BYTES, type TruncationResult, truncateHead, truncateTail } from \"../tools/truncate.ts\";\nimport type { ArtifactStore } from \"./context-artifacts.ts\";\nimport { isMissingArtifactMarker, type MissingArtifactReason } from \"./context-artifacts.ts\";\nimport type { ContextArtifactRef } from \"./context-item.ts\";\n\nexport type ArtifactRetrievalMode = \"metadata\" | \"head\" | \"tail\";\n\nexport const DEFAULT_RETRIEVAL_MAX_LINES = 200;\n\n/** Hard ceilings: a caller-requested maxLines/maxBytes can never exceed these. */\nexport const MAX_RETRIEVAL_LINES = 2000;\nexport const MAX_RETRIEVAL_BYTES = DEFAULT_MAX_BYTES;\n\nexport interface ArtifactRetrievalRequest {\n\tartifactId: string;\n\tmode?: ArtifactRetrievalMode;\n\tmaxLines?: number;\n\tmaxBytes?: number;\n}\n\nexport type ArtifactRetrievalResult =\n\t| { found: false; missingReason: MissingArtifactReason }\n\t| { found: true; mode: \"metadata\"; ref: ContextArtifactRef }\n\t| { found: true; mode: \"head\" | \"tail\"; ref: ContextArtifactRef; slice: string; truncation: TruncationResult };\n\nfunction clampToHardCeiling(requested: number | undefined, fallback: number, hardCeiling: number): number {\n\tconst candidate = requested ?? fallback;\n\tif (candidate <= 0) return 0;\n\treturn Math.min(candidate, hardCeiling);\n}\n\n/**\n * Resolve `request.artifactId` against `store` and return a bounded slice. `maxLines`/\n * `maxBytes` are hard-clamped to `MAX_RETRIEVAL_LINES`/`MAX_RETRIEVAL_BYTES` regardless of\n * what the caller requests -- see the module doc comment for the exact guarantee.\n */\nexport function retrieveArtifactSlice(\n\tstore: ArtifactStore,\n\trequest: ArtifactRetrievalRequest,\n): ArtifactRetrievalResult {\n\tconst record = store.read(request.artifactId);\n\tif (isMissingArtifactMarker(record)) {\n\t\treturn { found: false, missingReason: record.reason };\n\t}\n\n\tconst mode = request.mode ?? \"head\";\n\tif (mode === \"metadata\") {\n\t\treturn { found: true, mode: \"metadata\", ref: record.ref };\n\t}\n\n\tconst truncationOptions = {\n\t\tmaxLines: clampToHardCeiling(request.maxLines, DEFAULT_RETRIEVAL_MAX_LINES, MAX_RETRIEVAL_LINES),\n\t\tmaxBytes: clampToHardCeiling(request.maxBytes, DEFAULT_MAX_BYTES, MAX_RETRIEVAL_BYTES),\n\t};\n\tconst truncation =\n\t\tmode === \"tail\"\n\t\t\t? truncateTail(record.content, truncationOptions)\n\t\t\t: truncateHead(record.content, truncationOptions);\n\n\treturn { found: true, mode, ref: record.ref, slice: truncation.content, truncation };\n}\n"]}
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Artifact store abstraction (Phase 2): stable refs for raw large payloads, kept out of
3
+ * band from prompt context. This module defines the `ArtifactStore` interface plus two
4
+ * implementations: an in-memory one for tests, and `createFileArtifactStore` (session-
5
+ * scoped, filesystem-backed). A SQLite-backed implementation waits until the Phase M0
6
+ * storage-authority/location/concurrency decisions are accepted (see
7
+ * docs/context-management-rework/memory-architecture.md).
8
+ *
9
+ * `createFileArtifactStore` is wired into live grep/find tool construction in
10
+ * agent-session.ts (session-scoped under `<agentDir>/context-artifacts/<sessionId>/`).
11
+ * References are registered at pack time and released when context-gc evicts the
12
+ * corresponding grep/find tool result (opportunistic, conservative cleanup), with a
13
+ * best-effort dispose-time sweep for zero-reference artifacts. Payloads are retrievable
14
+ * out of band via the artifact_retrieve tool (context/artifact-retrieval.ts).
15
+ */
16
+ import { type ContextArtifactRef } from "./context-item.ts";
17
+ export interface ArtifactWriteRequest {
18
+ kind: ContextArtifactRef["kind"];
19
+ content: string;
20
+ toolName?: string;
21
+ command?: string;
22
+ path?: string;
23
+ sessionEntryId?: string;
24
+ createdAtTurn: number;
25
+ reproducible: boolean;
26
+ }
27
+ export interface ArtifactRecord {
28
+ ref: ContextArtifactRef;
29
+ content: string;
30
+ }
31
+ export type MissingArtifactReason = "not_found" | "cleaned_up";
32
+ export interface MissingArtifactMarker {
33
+ id: string;
34
+ missing: true;
35
+ reason: MissingArtifactReason;
36
+ }
37
+ export declare function isMissingArtifactMarker(value: ArtifactRecord | MissingArtifactMarker): value is MissingArtifactMarker;
38
+ /**
39
+ * Artifact id for a capture event, not merely a payload: it hashes every ref-defining
40
+ * field (kind, tool/command/path, content, sessionEntryId, createdAtTurn, reproducible).
41
+ * A repeat write with identical content but a different turn or session entry is a
42
+ * distinct capture and must get a distinct id -- otherwise the later capture's metadata
43
+ * would be silently discarded in favor of the first write. Only a truly identical
44
+ * request (same capture, re-submitted) is idempotent under this id.
45
+ */
46
+ export declare function generateArtifactId(request: Pick<ArtifactWriteRequest, "kind" | "content" | "toolName" | "command" | "path" | "sessionEntryId" | "createdAtTurn" | "reproducible">): string;
47
+ export interface ArtifactStore {
48
+ write(request: ArtifactWriteRequest): ArtifactRecord;
49
+ read(id: string): ArtifactRecord | MissingArtifactMarker;
50
+ /**
51
+ * Metadata-only lookup: the ref if `id` resolves to a live artifact, `undefined`
52
+ * otherwise. Never loads the payload -- for the file store this must not touch the
53
+ * payload file at all beyond an existence check, so a caller that only needs to know
54
+ * "does this still exist, and what are its ref fields" (e.g. a per-turn audit pass)
55
+ * never pays the cost of reading potentially large content off disk.
56
+ */
57
+ readRef(id: string): ContextArtifactRef | undefined;
58
+ has(id: string): boolean;
59
+ /**
60
+ * Register that `holderId` (a context item id, session entry id, etc.) depends on this
61
+ * artifact. Returns false if `id` does not exist (never written, or already cleaned
62
+ * up) so a caller cannot believe it protected an artifact that was never registered.
63
+ * Callers must fail closed (treat the artifact as unprotected) on a false return.
64
+ */
65
+ addReference(id: string, holderId: string): boolean;
66
+ /** Release a previously registered dependency. Returns true only if a reference was actually removed. */
67
+ removeReference(id: string, holderId: string): boolean;
68
+ referenceCount(id: string): number;
69
+ /** Delete only artifacts with zero active references. Returns the ids actually deleted. */
70
+ cleanup(): string[];
71
+ }
72
+ export declare function createInMemoryArtifactStore(): ArtifactStore;
73
+ export interface FileArtifactStoreOptions {
74
+ /** Directory the store persists artifact payloads and metadata under. Created if missing. */
75
+ baseDir: string;
76
+ }
77
+ /**
78
+ * Filesystem-backed `ArtifactStore`: payload and metadata (including reference holder ids)
79
+ * are written to `baseDir` so content, ref fields, and cleanup-protecting references all
80
+ * survive recreating the store (e.g. across a process restart against the same
81
+ * directory) -- unlike the in-memory store, which loses everything when the instance is
82
+ * dropped. No SQLite or other index is used; each artifact's metadata is a small sidecar
83
+ * JSON file next to its payload file, per the "keep SQLite out of scope unless a minimal
84
+ * metadata shape is unavoidable" constraint for this slice.
85
+ *
86
+ * The one thing that does NOT survive recreation: the missing-artifact reason
87
+ * distinction. A fresh instance has no in-memory record of which ids it personally
88
+ * cleaned up, so a previously-cleaned-up id reads back as "not_found" rather than
89
+ * "cleaned_up" after a restart. This still always returns an explicit missing marker,
90
+ * never fabricated or empty content -- it only affects which of the two reason codes is
91
+ * reported.
92
+ */
93
+ export declare function createFileArtifactStore(options: FileArtifactStoreOptions): ArtifactStore;
94
+ //# sourceMappingURL=context-artifacts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-artifacts.d.ts","sourceRoot":"","sources":["../../../src/core/context/context-artifacts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,OAAO,EAAE,KAAK,kBAAkB,EAAyC,MAAM,mBAAmB,CAAC;AAEnG,MAAM,WAAW,oBAAoB;IACpC,IAAI,EAAE,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC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F;IAC3F,OAAO,IAAI,MAAM,EAAE,CAAC;CACpB;AAQD,wBAAgB,2BAA2B,IAAI,aAAa,CA2E3D;AAED,MAAM,WAAW,wBAAwB;IACxC,6FAA6F;IAC7F,OAAO,EAAE,MAAM,CAAC;CAChB;AA+ED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,wBAAwB,GAAG,aAAa,CA0GxF","sourcesContent":["/**\n * Artifact store abstraction (Phase 2): stable refs for raw large payloads, kept out of\n * band from prompt context. This module defines the `ArtifactStore` interface plus two\n * implementations: an in-memory one for tests, and `createFileArtifactStore` (session-\n * scoped, filesystem-backed). A SQLite-backed implementation waits until the Phase M0\n * storage-authority/location/concurrency decisions are accepted (see\n * docs/context-management-rework/memory-architecture.md).\n *\n * `createFileArtifactStore` is wired into live grep/find tool construction in\n * agent-session.ts (session-scoped under `<agentDir>/context-artifacts/<sessionId>/`).\n * References are registered at pack time and released when context-gc evicts the\n * corresponding grep/find tool result (opportunistic, conservative cleanup), with a\n * best-effort dispose-time sweep for zero-reference artifacts. Payloads are retrievable\n * out of band via the artifact_retrieve tool (context/artifact-retrieval.ts).\n */\n\nimport { createHash } from \"node:crypto\";\nimport { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { type ContextArtifactRef, estimateByteLength, estimateLineCount } from \"./context-item.ts\";\n\nexport interface ArtifactWriteRequest {\n\tkind: ContextArtifactRef[\"kind\"];\n\tcontent: string;\n\ttoolName?: string;\n\tcommand?: string;\n\tpath?: string;\n\tsessionEntryId?: string;\n\tcreatedAtTurn: number;\n\treproducible: boolean;\n}\n\nexport interface ArtifactRecord {\n\tref: ContextArtifactRef;\n\tcontent: string;\n}\n\nexport type MissingArtifactReason = \"not_found\" | \"cleaned_up\";\n\nexport interface MissingArtifactMarker {\n\tid: string;\n\tmissing: true;\n\treason: MissingArtifactReason;\n}\n\nexport function isMissingArtifactMarker(value: ArtifactRecord | MissingArtifactMarker): value is MissingArtifactMarker {\n\treturn (value as MissingArtifactMarker).missing === true;\n}\n\n/**\n * Artifact id for a capture event, not merely a payload: it hashes every ref-defining\n * field (kind, tool/command/path, content, sessionEntryId, createdAtTurn, reproducible).\n * A repeat write with identical content but a different turn or session entry is a\n * distinct capture and must get a distinct id -- otherwise the later capture's metadata\n * would be silently discarded in favor of the first write. Only a truly identical\n * request (same capture, re-submitted) is idempotent under this id.\n */\nexport function generateArtifactId(\n\trequest: Pick<\n\t\tArtifactWriteRequest,\n\t\t\"kind\" | \"content\" | \"toolName\" | \"command\" | \"path\" | \"sessionEntryId\" | \"createdAtTurn\" | \"reproducible\"\n\t>,\n): string {\n\treturn createHash(\"sha256\")\n\t\t.update(\n\t\t\t[\n\t\t\t\trequest.kind,\n\t\t\t\trequest.toolName ?? \"\",\n\t\t\t\trequest.command ?? \"\",\n\t\t\t\trequest.path ?? \"\",\n\t\t\t\trequest.sessionEntryId ?? \"\",\n\t\t\t\tString(request.createdAtTurn),\n\t\t\t\tString(request.reproducible),\n\t\t\t\trequest.content,\n\t\t\t].join(\"\\0\"),\n\t\t)\n\t\t.digest(\"hex\")\n\t\t.slice(0, 24);\n}\n\nexport interface ArtifactStore {\n\twrite(request: ArtifactWriteRequest): ArtifactRecord;\n\tread(id: string): ArtifactRecord | MissingArtifactMarker;\n\t/**\n\t * Metadata-only lookup: the ref if `id` resolves to a live artifact, `undefined`\n\t * otherwise. Never loads the payload -- for the file store this must not touch the\n\t * payload file at all beyond an existence check, so a caller that only needs to know\n\t * \"does this still exist, and what are its ref fields\" (e.g. a per-turn audit pass)\n\t * never pays the cost of reading potentially large content off disk.\n\t */\n\treadRef(id: string): ContextArtifactRef | undefined;\n\thas(id: string): boolean;\n\t/**\n\t * Register that `holderId` (a context item id, session entry id, etc.) depends on this\n\t * artifact. Returns false if `id` does not exist (never written, or already cleaned\n\t * up) so a caller cannot believe it protected an artifact that was never registered.\n\t * Callers must fail closed (treat the artifact as unprotected) on a false return.\n\t */\n\taddReference(id: string, holderId: string): boolean;\n\t/** Release a previously registered dependency. Returns true only if a reference was actually removed. */\n\tremoveReference(id: string, holderId: string): boolean;\n\treferenceCount(id: string): number;\n\t/** Delete only artifacts with zero active references. Returns the ids actually deleted. */\n\tcleanup(): string[];\n}\n\ninterface StoredArtifact {\n\tref: ContextArtifactRef;\n\tcontent: string;\n\treferences: Set<string>;\n}\n\nexport function createInMemoryArtifactStore(): ArtifactStore {\n\tconst artifacts = new Map<string, StoredArtifact>();\n\tconst cleanedUp = new Set<string>();\n\n\treturn {\n\t\twrite(request: ArtifactWriteRequest): ArtifactRecord {\n\t\t\tconst id = generateArtifactId(request);\n\t\t\tconst existing = artifacts.get(id);\n\t\t\tif (existing) {\n\t\t\t\tcleanedUp.delete(id);\n\t\t\t\treturn { ref: existing.ref, content: existing.content };\n\t\t\t}\n\n\t\t\tconst ref: ContextArtifactRef = {\n\t\t\t\tid,\n\t\t\t\tkind: request.kind,\n\t\t\t\tsessionEntryId: request.sessionEntryId,\n\t\t\t\ttoolName: request.toolName,\n\t\t\t\tcommand: request.command,\n\t\t\t\tpath: request.path,\n\t\t\t\tbyteLength: estimateByteLength(request.content),\n\t\t\t\tlineCount: estimateLineCount(request.content),\n\t\t\t\tcreatedAtTurn: request.createdAtTurn,\n\t\t\t\treproducible: request.reproducible,\n\t\t\t};\n\t\t\tartifacts.set(id, { ref, content: request.content, references: new Set() });\n\t\t\tcleanedUp.delete(id);\n\t\t\treturn { ref, content: request.content };\n\t\t},\n\n\t\tread(id: string): ArtifactRecord | MissingArtifactMarker {\n\t\t\tconst stored = artifacts.get(id);\n\t\t\tif (!stored) {\n\t\t\t\treturn { id, missing: true, reason: cleanedUp.has(id) ? \"cleaned_up\" : \"not_found\" };\n\t\t\t}\n\t\t\treturn { ref: stored.ref, content: stored.content };\n\t\t},\n\n\t\treadRef(id: string): ContextArtifactRef | undefined {\n\t\t\treturn artifacts.get(id)?.ref;\n\t\t},\n\n\t\thas(id: string): boolean {\n\t\t\treturn artifacts.has(id);\n\t\t},\n\n\t\taddReference(id: string, holderId: string): boolean {\n\t\t\tconst stored = artifacts.get(id);\n\t\t\tif (!stored) return false;\n\t\t\tstored.references.add(holderId);\n\t\t\treturn true;\n\t\t},\n\n\t\tremoveReference(id: string, holderId: string): boolean {\n\t\t\tconst stored = artifacts.get(id);\n\t\t\tif (!stored) return false;\n\t\t\treturn stored.references.delete(holderId);\n\t\t},\n\n\t\treferenceCount(id: string): number {\n\t\t\treturn artifacts.get(id)?.references.size ?? 0;\n\t\t},\n\n\t\tcleanup(): string[] {\n\t\t\tconst deleted: string[] = [];\n\t\t\tfor (const [id, stored] of artifacts) {\n\t\t\t\tif (stored.references.size === 0) {\n\t\t\t\t\tartifacts.delete(id);\n\t\t\t\t\tcleanedUp.add(id);\n\t\t\t\t\tdeleted.push(id);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn deleted;\n\t\t},\n\t};\n}\n\nexport interface FileArtifactStoreOptions {\n\t/** Directory the store persists artifact payloads and metadata under. Created if missing. */\n\tbaseDir: string;\n}\n\ninterface PersistedArtifactMeta {\n\tref: ContextArtifactRef;\n\treferences: string[];\n}\n\nconst META_SUFFIX = \".meta.json\";\nconst PAYLOAD_SUFFIX = \".payload\";\n\n/**\n * Artifact ids are generated by `generateArtifactId` as a lowercase hex digest. Reject\n * anything else so a caller-supplied id (including one echoed back from model output)\n * can never be used as a path-traversal vector into `baseDir`.\n */\nfunction isSafeArtifactId(id: string): boolean {\n\treturn /^[0-9a-f]{1,64}$/.test(id);\n}\n\nfunction payloadPath(baseDir: string, id: string): string {\n\treturn join(baseDir, `${id}${PAYLOAD_SUFFIX}`);\n}\n\nfunction metaPath(baseDir: string, id: string): string {\n\treturn join(baseDir, `${id}${META_SUFFIX}`);\n}\n\nconst VALID_ARTIFACT_KINDS: ReadonlySet<ContextArtifactRef[\"kind\"]> = new Set([\n\t\"tool_output\",\n\t\"file_snapshot\",\n\t\"test_output\",\n\t\"diff\",\n\t\"transcript_slice\",\n]);\n\nfunction isValidArtifactRefShape(value: unknown): value is ContextArtifactRef {\n\tif (typeof value !== \"object\" || value === null) return false;\n\tconst ref = value as Record<string, unknown>;\n\treturn (\n\t\ttypeof ref.id === \"string\" &&\n\t\ttypeof ref.kind === \"string\" &&\n\t\tVALID_ARTIFACT_KINDS.has(ref.kind as ContextArtifactRef[\"kind\"]) &&\n\t\ttypeof ref.byteLength === \"number\" &&\n\t\ttypeof ref.createdAtTurn === \"number\" &&\n\t\ttypeof ref.reproducible === \"boolean\"\n\t);\n}\n\n/**\n * A parsed JSON value can be syntactically valid but semantically garbage (truncated\n * write, hand-edited file, future/incompatible format). Validate shape, not just parse\n * success, so a malformed sidecar can never produce an invalid ref or crash `cleanup()` --\n * it is treated as unusable/missing, the same as a sidecar that doesn't exist.\n */\nfunction isValidPersistedArtifactMeta(value: unknown): value is PersistedArtifactMeta {\n\tif (typeof value !== \"object\" || value === null) return false;\n\tconst meta = value as Record<string, unknown>;\n\treturn (\n\t\tisValidArtifactRefShape(meta.ref) &&\n\t\tArray.isArray(meta.references) &&\n\t\tmeta.references.every((entry) => typeof entry === \"string\")\n\t);\n}\n\nfunction readMeta(baseDir: string, id: string): PersistedArtifactMeta | undefined {\n\tconst path = metaPath(baseDir, id);\n\tif (!existsSync(path)) return undefined;\n\ttry {\n\t\tconst parsed: unknown = JSON.parse(readFileSync(path, \"utf8\"));\n\t\treturn isValidPersistedArtifactMeta(parsed) ? parsed : undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction writeMeta(baseDir: string, id: string, meta: PersistedArtifactMeta): void {\n\twriteFileSync(metaPath(baseDir, id), JSON.stringify(meta), \"utf8\");\n}\n\n/**\n * Filesystem-backed `ArtifactStore`: payload and metadata (including reference holder ids)\n * are written to `baseDir` so content, ref fields, and cleanup-protecting references all\n * survive recreating the store (e.g. across a process restart against the same\n * directory) -- unlike the in-memory store, which loses everything when the instance is\n * dropped. No SQLite or other index is used; each artifact's metadata is a small sidecar\n * JSON file next to its payload file, per the \"keep SQLite out of scope unless a minimal\n * metadata shape is unavoidable\" constraint for this slice.\n *\n * The one thing that does NOT survive recreation: the missing-artifact reason\n * distinction. A fresh instance has no in-memory record of which ids it personally\n * cleaned up, so a previously-cleaned-up id reads back as \"not_found\" rather than\n * \"cleaned_up\" after a restart. This still always returns an explicit missing marker,\n * never fabricated or empty content -- it only affects which of the two reason codes is\n * reported.\n */\nexport function createFileArtifactStore(options: FileArtifactStoreOptions): ArtifactStore {\n\tconst baseDir = options.baseDir;\n\tmkdirSync(baseDir, { recursive: true });\n\tconst cleanedUpThisInstance = new Set<string>();\n\n\treturn {\n\t\twrite(request: ArtifactWriteRequest): ArtifactRecord {\n\t\t\tconst id = generateArtifactId(request);\n\t\t\tconst existingMeta = readMeta(baseDir, id);\n\t\t\tconst existingPayloadPath = payloadPath(baseDir, id);\n\t\t\tif (existingMeta && existsSync(existingPayloadPath)) {\n\t\t\t\tcleanedUpThisInstance.delete(id);\n\t\t\t\treturn { ref: existingMeta.ref, content: readFileSync(existingPayloadPath, \"utf8\") };\n\t\t\t}\n\n\t\t\tconst ref: ContextArtifactRef = {\n\t\t\t\tid,\n\t\t\t\tkind: request.kind,\n\t\t\t\tsessionEntryId: request.sessionEntryId,\n\t\t\t\ttoolName: request.toolName,\n\t\t\t\tcommand: request.command,\n\t\t\t\tpath: request.path,\n\t\t\t\tbyteLength: estimateByteLength(request.content),\n\t\t\t\tlineCount: estimateLineCount(request.content),\n\t\t\t\tcreatedAtTurn: request.createdAtTurn,\n\t\t\t\treproducible: request.reproducible,\n\t\t\t};\n\t\t\twriteFileSync(payloadPath(baseDir, id), request.content, \"utf8\");\n\t\t\twriteMeta(baseDir, id, { ref, references: [] });\n\t\t\tcleanedUpThisInstance.delete(id);\n\t\t\treturn { ref, content: request.content };\n\t\t},\n\n\t\tread(id: string): ArtifactRecord | MissingArtifactMarker {\n\t\t\tif (!isSafeArtifactId(id)) return { id, missing: true, reason: \"not_found\" };\n\t\t\tconst meta = readMeta(baseDir, id);\n\t\t\tconst pPath = payloadPath(baseDir, id);\n\t\t\tif (!meta || !existsSync(pPath)) {\n\t\t\t\treturn { id, missing: true, reason: cleanedUpThisInstance.has(id) ? \"cleaned_up\" : \"not_found\" };\n\t\t\t}\n\t\t\treturn { ref: meta.ref, content: readFileSync(pPath, \"utf8\") };\n\t\t},\n\n\t\treadRef(id: string): ContextArtifactRef | undefined {\n\t\t\tif (!isSafeArtifactId(id)) return undefined;\n\t\t\tconst meta = readMeta(baseDir, id);\n\t\t\tif (!meta || !existsSync(payloadPath(baseDir, id))) return undefined;\n\t\t\treturn meta.ref;\n\t\t},\n\n\t\thas(id: string): boolean {\n\t\t\tif (!isSafeArtifactId(id)) return false;\n\t\t\treturn readMeta(baseDir, id) !== undefined && existsSync(payloadPath(baseDir, id));\n\t\t},\n\n\t\taddReference(id: string, holderId: string): boolean {\n\t\t\tif (!isSafeArtifactId(id)) return false;\n\t\t\tconst meta = readMeta(baseDir, id);\n\t\t\tif (!meta || !existsSync(payloadPath(baseDir, id))) return false;\n\t\t\tif (!meta.references.includes(holderId)) {\n\t\t\t\tmeta.references.push(holderId);\n\t\t\t\twriteMeta(baseDir, id, meta);\n\t\t\t}\n\t\t\treturn true;\n\t\t},\n\n\t\tremoveReference(id: string, holderId: string): boolean {\n\t\t\tif (!isSafeArtifactId(id)) return false;\n\t\t\tconst meta = readMeta(baseDir, id);\n\t\t\tif (!meta) return false;\n\t\t\tconst index = meta.references.indexOf(holderId);\n\t\t\tif (index === -1) return false;\n\t\t\tmeta.references.splice(index, 1);\n\t\t\twriteMeta(baseDir, id, meta);\n\t\t\treturn true;\n\t\t},\n\n\t\treferenceCount(id: string): number {\n\t\t\tif (!isSafeArtifactId(id)) return 0;\n\t\t\treturn readMeta(baseDir, id)?.references.length ?? 0;\n\t\t},\n\n\t\tcleanup(): string[] {\n\t\t\tconst deleted: string[] = [];\n\t\t\tfor (const entry of readdirSync(baseDir)) {\n\t\t\t\tif (!entry.endsWith(META_SUFFIX)) continue;\n\t\t\t\tconst id = entry.slice(0, -META_SUFFIX.length);\n\t\t\t\tif (!isSafeArtifactId(id)) continue;\n\t\t\t\tconst meta = readMeta(baseDir, id);\n\t\t\t\tif (!meta || meta.references.length > 0) continue;\n\t\t\t\ttry {\n\t\t\t\t\tunlinkSync(metaPath(baseDir, id));\n\t\t\t\t} catch {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tunlinkSync(payloadPath(baseDir, id));\n\t\t\t\t} catch {\n\t\t\t\t\t// Payload already gone; metadata removal above is what matters for reachability.\n\t\t\t\t}\n\t\t\t\tcleanedUpThisInstance.add(id);\n\t\t\t\tdeleted.push(id);\n\t\t\t}\n\t\t\treturn deleted;\n\t\t},\n\t};\n}\n"]}