@caupulican/pi-adaptative 0.81.39 → 0.81.41

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 (884) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/README.md +2 -0
  3. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts +2 -0
  4. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts.map +1 -1
  5. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js +3 -0
  6. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js.map +1 -1
  7. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.ts +4 -0
  8. package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts +9 -0
  9. package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts.map +1 -1
  10. package/dist/bundled-resources/extensions/tmux-agent-manager/index.js +290 -107
  11. package/dist/bundled-resources/extensions/tmux-agent-manager/index.js.map +1 -1
  12. package/dist/bundled-resources/extensions/tmux-agent-manager/index.ts +357 -122
  13. package/dist/bundled-resources/skills/tool-call-repair/references/repair-catalogue.md +2 -2
  14. package/dist/cli/args.d.ts +4 -0
  15. package/dist/cli/args.d.ts.map +1 -1
  16. package/dist/cli/args.js +31 -1
  17. package/dist/cli/args.js.map +1 -1
  18. package/dist/core/agent-directory-layout.d.ts +15 -0
  19. package/dist/core/agent-directory-layout.d.ts.map +1 -0
  20. package/dist/core/agent-directory-layout.js +72 -0
  21. package/dist/core/agent-directory-layout.js.map +1 -0
  22. package/dist/core/agent-paths.d.ts +25 -2
  23. package/dist/core/agent-paths.d.ts.map +1 -1
  24. package/dist/core/agent-paths.js +79 -4
  25. package/dist/core/agent-paths.js.map +1 -1
  26. package/dist/core/agent-session-contracts.d.ts +293 -0
  27. package/dist/core/agent-session-contracts.d.ts.map +1 -0
  28. package/dist/core/agent-session-contracts.js +26 -0
  29. package/dist/core/agent-session-contracts.js.map +1 -0
  30. package/dist/core/agent-session-runtime.d.ts +52 -8
  31. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  32. package/dist/core/agent-session-runtime.js +400 -79
  33. package/dist/core/agent-session-runtime.js.map +1 -1
  34. package/dist/core/agent-session-services.d.ts +1 -0
  35. package/dist/core/agent-session-services.d.ts.map +1 -1
  36. package/dist/core/agent-session-services.js +1 -0
  37. package/dist/core/agent-session-services.js.map +1 -1
  38. package/dist/core/agent-session.d.ts +74 -518
  39. package/dist/core/agent-session.d.ts.map +1 -1
  40. package/dist/core/agent-session.js +370 -1432
  41. package/dist/core/agent-session.js.map +1 -1
  42. package/dist/core/auth-storage.d.ts.map +1 -1
  43. package/dist/core/auth-storage.js +3 -1
  44. package/dist/core/auth-storage.js.map +1 -1
  45. package/dist/core/autonomy/approval-gate.d.ts +3 -2
  46. package/dist/core/autonomy/approval-gate.d.ts.map +1 -1
  47. package/dist/core/autonomy/approval-gate.js +6 -28
  48. package/dist/core/autonomy/approval-gate.js.map +1 -1
  49. package/dist/core/autonomy/bounded-completion.d.ts +1 -1
  50. package/dist/core/autonomy/bounded-completion.d.ts.map +1 -1
  51. package/dist/core/autonomy/bounded-completion.js +1 -1
  52. package/dist/core/autonomy/bounded-completion.js.map +1 -1
  53. package/dist/core/autonomy/contracts.d.ts +20 -12
  54. package/dist/core/autonomy/contracts.d.ts.map +1 -1
  55. package/dist/core/autonomy/contracts.js.map +1 -1
  56. package/dist/core/autonomy/foreground-envelope.d.ts +8 -2
  57. package/dist/core/autonomy/foreground-envelope.d.ts.map +1 -1
  58. package/dist/core/autonomy/foreground-envelope.js +8 -25
  59. package/dist/core/autonomy/foreground-envelope.js.map +1 -1
  60. package/dist/core/autonomy/gates.d.ts.map +1 -1
  61. package/dist/core/autonomy/gates.js +8 -10
  62. package/dist/core/autonomy/gates.js.map +1 -1
  63. package/dist/core/autonomy/lane-private-paths.d.ts +2 -0
  64. package/dist/core/autonomy/lane-private-paths.d.ts.map +1 -0
  65. package/dist/core/autonomy/lane-private-paths.js +16 -0
  66. package/dist/core/autonomy/lane-private-paths.js.map +1 -0
  67. package/dist/core/autonomy/lane-tool-surface.d.ts +10 -0
  68. package/dist/core/autonomy/lane-tool-surface.d.ts.map +1 -1
  69. package/dist/core/autonomy/lane-tool-surface.js +48 -9
  70. package/dist/core/autonomy/lane-tool-surface.js.map +1 -1
  71. package/dist/core/autonomy/lane-tracker.d.ts +14 -0
  72. package/dist/core/autonomy/lane-tracker.d.ts.map +1 -1
  73. package/dist/core/autonomy/lane-tracker.js +38 -0
  74. package/dist/core/autonomy/lane-tracker.js.map +1 -1
  75. package/dist/core/autonomy/risk-assessment.d.ts +1 -2
  76. package/dist/core/autonomy/risk-assessment.d.ts.map +1 -1
  77. package/dist/core/autonomy/risk-assessment.js +1 -123
  78. package/dist/core/autonomy/risk-assessment.js.map +1 -1
  79. package/dist/core/autonomy/session-lane-record.d.ts +4 -4
  80. package/dist/core/autonomy/session-lane-record.d.ts.map +1 -1
  81. package/dist/core/autonomy/session-lane-record.js +16 -29
  82. package/dist/core/autonomy/session-lane-record.js.map +1 -1
  83. package/dist/core/autonomy/telemetry-events.d.ts +1 -1
  84. package/dist/core/autonomy/telemetry-events.d.ts.map +1 -1
  85. package/dist/core/autonomy/telemetry-events.js +1 -1
  86. package/dist/core/autonomy/telemetry-events.js.map +1 -1
  87. package/dist/core/autonomy-telemetry.d.ts +7 -7
  88. package/dist/core/autonomy-telemetry.d.ts.map +1 -1
  89. package/dist/core/autonomy-telemetry.js +9 -9
  90. package/dist/core/autonomy-telemetry.js.map +1 -1
  91. package/dist/core/background-lane-controller.d.ts +56 -236
  92. package/dist/core/background-lane-controller.d.ts.map +1 -1
  93. package/dist/core/background-lane-controller.js +141 -1273
  94. package/dist/core/background-lane-controller.js.map +1 -1
  95. package/dist/core/bedrock-sso-login.d.ts +16 -0
  96. package/dist/core/bedrock-sso-login.d.ts.map +1 -0
  97. package/dist/core/bedrock-sso-login.js +98 -0
  98. package/dist/core/bedrock-sso-login.js.map +1 -0
  99. package/dist/core/capability-contract.d.ts +4 -0
  100. package/dist/core/capability-contract.d.ts.map +1 -0
  101. package/dist/core/capability-contract.js +28 -0
  102. package/dist/core/capability-contract.js.map +1 -0
  103. package/dist/core/compaction-controller.d.ts +90 -0
  104. package/dist/core/compaction-controller.d.ts.map +1 -0
  105. package/dist/core/compaction-controller.js +452 -0
  106. package/dist/core/compaction-controller.js.map +1 -0
  107. package/dist/core/compaction-support.d.ts +13 -17
  108. package/dist/core/compaction-support.d.ts.map +1 -1
  109. package/dist/core/compaction-support.js +3 -3
  110. package/dist/core/compaction-support.js.map +1 -1
  111. package/dist/core/context/context-artifacts.d.ts +6 -2
  112. package/dist/core/context/context-artifacts.d.ts.map +1 -1
  113. package/dist/core/context/context-artifacts.js +34 -2
  114. package/dist/core/context/context-artifacts.js.map +1 -1
  115. package/dist/core/context/context-store-retention.d.ts +8 -2
  116. package/dist/core/context/context-store-retention.d.ts.map +1 -1
  117. package/dist/core/context/context-store-retention.js +158 -35
  118. package/dist/core/context/context-store-retention.js.map +1 -1
  119. package/dist/core/context/okf-memory.d.ts.map +1 -1
  120. package/dist/core/context/okf-memory.js +9 -11
  121. package/dist/core/context/okf-memory.js.map +1 -1
  122. package/dist/core/context/sqlite-runtime-index.d.ts.map +1 -1
  123. package/dist/core/context/sqlite-runtime-index.js +6 -8
  124. package/dist/core/context/sqlite-runtime-index.js.map +1 -1
  125. package/dist/core/context-gc.d.ts +3 -0
  126. package/dist/core/context-gc.d.ts.map +1 -1
  127. package/dist/core/context-gc.js +10 -5
  128. package/dist/core/context-gc.js.map +1 -1
  129. package/dist/core/context-pipeline.d.ts +1 -1
  130. package/dist/core/context-pipeline.d.ts.map +1 -1
  131. package/dist/core/context-pipeline.js +16 -37
  132. package/dist/core/context-pipeline.js.map +1 -1
  133. package/dist/core/cost/cost-summary.d.ts.map +1 -1
  134. package/dist/core/cost/cost-summary.js +5 -3
  135. package/dist/core/cost/cost-summary.js.map +1 -1
  136. package/dist/core/cost/daily-usage.d.ts.map +1 -1
  137. package/dist/core/cost/daily-usage.js +4 -5
  138. package/dist/core/cost/daily-usage.js.map +1 -1
  139. package/dist/core/default-tool-surface.d.ts.map +1 -1
  140. package/dist/core/default-tool-surface.js +2 -0
  141. package/dist/core/default-tool-surface.js.map +1 -1
  142. package/dist/core/delegation/managed-lane-controller.d.ts +34 -0
  143. package/dist/core/delegation/managed-lane-controller.d.ts.map +1 -0
  144. package/dist/core/delegation/managed-lane-controller.js +217 -0
  145. package/dist/core/delegation/managed-lane-controller.js.map +1 -0
  146. package/dist/core/delegation/session-worker-claim.d.ts +44 -0
  147. package/dist/core/delegation/session-worker-claim.d.ts.map +1 -0
  148. package/dist/core/delegation/session-worker-claim.js +98 -0
  149. package/dist/core/delegation/session-worker-claim.js.map +1 -0
  150. package/dist/core/delegation/worker-actions.d.ts +7 -5
  151. package/dist/core/delegation/worker-actions.d.ts.map +1 -1
  152. package/dist/core/delegation/worker-actions.js +20 -4
  153. package/dist/core/delegation/worker-actions.js.map +1 -1
  154. package/dist/core/delegation/{worker-result.d.ts → worker-claim.d.ts} +13 -13
  155. package/dist/core/delegation/worker-claim.d.ts.map +1 -0
  156. package/dist/core/delegation/{worker-result.js → worker-claim.js} +61 -51
  157. package/dist/core/delegation/worker-claim.js.map +1 -0
  158. package/dist/core/delegation/worker-delegation-controller.d.ts +110 -0
  159. package/dist/core/delegation/worker-delegation-controller.d.ts.map +1 -0
  160. package/dist/core/delegation/worker-delegation-controller.js +861 -0
  161. package/dist/core/delegation/worker-delegation-controller.js.map +1 -0
  162. package/dist/core/delegation/worker-delegation-request.d.ts +11 -0
  163. package/dist/core/delegation/worker-delegation-request.d.ts.map +1 -0
  164. package/dist/core/delegation/worker-delegation-request.js +2 -0
  165. package/dist/core/delegation/worker-delegation-request.js.map +1 -0
  166. package/dist/core/delegation/worker-dispatch-scheduler.d.ts +40 -0
  167. package/dist/core/delegation/worker-dispatch-scheduler.d.ts.map +1 -0
  168. package/dist/core/delegation/worker-dispatch-scheduler.js +93 -0
  169. package/dist/core/delegation/worker-dispatch-scheduler.js.map +1 -0
  170. package/dist/core/delegation/worker-execution-policy.d.ts +62 -0
  171. package/dist/core/delegation/worker-execution-policy.d.ts.map +1 -0
  172. package/dist/core/delegation/worker-execution-policy.js +160 -0
  173. package/dist/core/delegation/worker-execution-policy.js.map +1 -0
  174. package/dist/core/delegation/worker-lane-projection.d.ts +14 -0
  175. package/dist/core/delegation/worker-lane-projection.d.ts.map +1 -0
  176. package/dist/core/delegation/worker-lane-projection.js +100 -0
  177. package/dist/core/delegation/worker-lane-projection.js.map +1 -0
  178. package/dist/core/delegation/worker-lifecycle.d.ts +97 -0
  179. package/dist/core/delegation/worker-lifecycle.d.ts.map +1 -0
  180. package/dist/core/delegation/worker-lifecycle.js +339 -0
  181. package/dist/core/delegation/worker-lifecycle.js.map +1 -0
  182. package/dist/core/delegation/worker-notification-coordinator.d.ts +36 -0
  183. package/dist/core/delegation/worker-notification-coordinator.d.ts.map +1 -0
  184. package/dist/core/delegation/worker-notification-coordinator.js +90 -0
  185. package/dist/core/delegation/worker-notification-coordinator.js.map +1 -0
  186. package/dist/core/delegation/worker-profile-resolver.d.ts +55 -0
  187. package/dist/core/delegation/worker-profile-resolver.d.ts.map +1 -0
  188. package/dist/core/delegation/worker-profile-resolver.js +113 -0
  189. package/dist/core/delegation/worker-profile-resolver.js.map +1 -0
  190. package/dist/core/delegation/worker-runner.d.ts +25 -14
  191. package/dist/core/delegation/worker-runner.d.ts.map +1 -1
  192. package/dist/core/delegation/worker-runner.js +122 -53
  193. package/dist/core/delegation/worker-runner.js.map +1 -1
  194. package/dist/core/delegation/worker-task-label.d.ts +3 -0
  195. package/dist/core/delegation/worker-task-label.d.ts.map +1 -0
  196. package/dist/core/delegation/worker-task-label.js +11 -0
  197. package/dist/core/delegation/worker-task-label.js.map +1 -0
  198. package/dist/core/doctor.d.ts +8 -0
  199. package/dist/core/doctor.d.ts.map +1 -1
  200. package/dist/core/doctor.js +32 -1
  201. package/dist/core/doctor.js.map +1 -1
  202. package/dist/core/extension-import-authority.d.ts +6 -0
  203. package/dist/core/extension-import-authority.d.ts.map +1 -0
  204. package/dist/core/extension-import-authority.js +10 -0
  205. package/dist/core/extension-import-authority.js.map +1 -0
  206. package/dist/core/extensions/index.d.ts +1 -1
  207. package/dist/core/extensions/index.d.ts.map +1 -1
  208. package/dist/core/extensions/index.js.map +1 -1
  209. package/dist/core/extensions/loader.d.ts +5 -1
  210. package/dist/core/extensions/loader.d.ts.map +1 -1
  211. package/dist/core/extensions/loader.js +52 -18
  212. package/dist/core/extensions/loader.js.map +1 -1
  213. package/dist/core/extensions/runner.d.ts +3 -0
  214. package/dist/core/extensions/runner.d.ts.map +1 -1
  215. package/dist/core/extensions/runner.js +20 -5
  216. package/dist/core/extensions/runner.js.map +1 -1
  217. package/dist/core/extensions/stale-context.d.ts +2 -0
  218. package/dist/core/extensions/stale-context.d.ts.map +1 -0
  219. package/dist/core/extensions/stale-context.js +2 -0
  220. package/dist/core/extensions/stale-context.js.map +1 -0
  221. package/dist/core/extensions/storage.d.ts +8 -0
  222. package/dist/core/extensions/storage.d.ts.map +1 -0
  223. package/dist/core/extensions/storage.js +28 -0
  224. package/dist/core/extensions/storage.js.map +1 -0
  225. package/dist/core/extensions/types.d.ts +78 -21
  226. package/dist/core/extensions/types.d.ts.map +1 -1
  227. package/dist/core/extensions/types.js.map +1 -1
  228. package/dist/core/failure-corpus.d.ts +1 -15
  229. package/dist/core/failure-corpus.d.ts.map +1 -1
  230. package/dist/core/failure-corpus.js +9 -37
  231. package/dist/core/failure-corpus.js.map +1 -1
  232. package/dist/core/foreground-recovery-controller.d.ts +53 -0
  233. package/dist/core/foreground-recovery-controller.d.ts.map +1 -0
  234. package/dist/core/foreground-recovery-controller.js +111 -0
  235. package/dist/core/foreground-recovery-controller.js.map +1 -0
  236. package/dist/core/gateways/channel-provider.d.ts +3 -0
  237. package/dist/core/gateways/channel-provider.d.ts.map +1 -1
  238. package/dist/core/gateways/channel-provider.js +31 -6
  239. package/dist/core/gateways/channel-provider.js.map +1 -1
  240. package/dist/core/goal-loop-controller.d.ts +14 -9
  241. package/dist/core/goal-loop-controller.d.ts.map +1 -1
  242. package/dist/core/goal-loop-controller.js +47 -75
  243. package/dist/core/goal-loop-controller.js.map +1 -1
  244. package/dist/core/goals/compact-goal-context.d.ts +11 -0
  245. package/dist/core/goals/compact-goal-context.d.ts.map +1 -0
  246. package/dist/core/goals/compact-goal-context.js +59 -0
  247. package/dist/core/goals/compact-goal-context.js.map +1 -0
  248. package/dist/core/goals/goal-acceptance.d.ts +11 -0
  249. package/dist/core/goals/goal-acceptance.d.ts.map +1 -0
  250. package/dist/core/goals/goal-acceptance.js +27 -0
  251. package/dist/core/goals/goal-acceptance.js.map +1 -0
  252. package/dist/core/goals/goal-auto-continue-controller.d.ts +28 -0
  253. package/dist/core/goals/goal-auto-continue-controller.d.ts.map +1 -0
  254. package/dist/core/goals/goal-auto-continue-controller.js +90 -0
  255. package/dist/core/goals/goal-auto-continue-controller.js.map +1 -0
  256. package/dist/core/goals/goal-continuation-controller.d.ts +2 -2
  257. package/dist/core/goals/goal-continuation-controller.d.ts.map +1 -1
  258. package/dist/core/goals/goal-continuation-controller.js +52 -11
  259. package/dist/core/goals/goal-continuation-controller.js.map +1 -1
  260. package/dist/core/goals/goal-continuation-defaults.d.ts +4 -29
  261. package/dist/core/goals/goal-continuation-defaults.d.ts.map +1 -1
  262. package/dist/core/goals/goal-continuation-defaults.js +4 -32
  263. package/dist/core/goals/goal-continuation-defaults.js.map +1 -1
  264. package/dist/core/goals/goal-continuation-prompt.d.ts +8 -18
  265. package/dist/core/goals/goal-continuation-prompt.d.ts.map +1 -1
  266. package/dist/core/goals/goal-continuation-prompt.js +9 -179
  267. package/dist/core/goals/goal-continuation-prompt.js.map +1 -1
  268. package/dist/core/goals/goal-lifecycle.d.ts +43 -0
  269. package/dist/core/goals/goal-lifecycle.d.ts.map +1 -0
  270. package/dist/core/goals/goal-lifecycle.js +102 -0
  271. package/dist/core/goals/goal-lifecycle.js.map +1 -0
  272. package/dist/core/goals/goal-record.d.ts +17 -0
  273. package/dist/core/goals/goal-record.d.ts.map +1 -0
  274. package/dist/core/goals/goal-record.js +31 -0
  275. package/dist/core/goals/goal-record.js.map +1 -0
  276. package/dist/core/goals/goal-runtime-snapshot.d.ts +11 -9
  277. package/dist/core/goals/goal-runtime-snapshot.d.ts.map +1 -1
  278. package/dist/core/goals/goal-runtime-snapshot.js +10 -8
  279. package/dist/core/goals/goal-runtime-snapshot.js.map +1 -1
  280. package/dist/core/goals/goal-session-controller.d.ts +42 -0
  281. package/dist/core/goals/goal-session-controller.d.ts.map +1 -0
  282. package/dist/core/goals/goal-session-controller.js +128 -0
  283. package/dist/core/goals/goal-session-controller.js.map +1 -0
  284. package/dist/core/goals/goal-state.d.ts +46 -25
  285. package/dist/core/goals/goal-state.d.ts.map +1 -1
  286. package/dist/core/goals/goal-state.js +91 -24
  287. package/dist/core/goals/goal-state.js.map +1 -1
  288. package/dist/core/goals/goal-tool-core.d.ts +9 -10
  289. package/dist/core/goals/goal-tool-core.d.ts.map +1 -1
  290. package/dist/core/goals/goal-tool-core.js +60 -75
  291. package/dist/core/goals/goal-tool-core.js.map +1 -1
  292. package/dist/core/goals/session-goal-state.d.ts +20 -12
  293. package/dist/core/goals/session-goal-state.d.ts.map +1 -1
  294. package/dist/core/goals/session-goal-state.js +133 -33
  295. package/dist/core/goals/session-goal-state.js.map +1 -1
  296. package/dist/core/human-input-controller.d.ts +47 -0
  297. package/dist/core/human-input-controller.d.ts.map +1 -0
  298. package/dist/core/human-input-controller.js +181 -0
  299. package/dist/core/human-input-controller.js.map +1 -0
  300. package/dist/core/human-input.d.ts +100 -0
  301. package/dist/core/human-input.d.ts.map +1 -0
  302. package/dist/core/human-input.js +362 -0
  303. package/dist/core/human-input.js.map +1 -0
  304. package/dist/core/index.d.ts +9 -1
  305. package/dist/core/index.d.ts.map +1 -1
  306. package/dist/core/index.js +9 -1
  307. package/dist/core/index.js.map +1 -1
  308. package/dist/core/{telemetry.d.ts → install-telemetry.d.ts} +1 -1
  309. package/dist/core/install-telemetry.d.ts.map +1 -0
  310. package/dist/core/{telemetry.js → install-telemetry.js} +1 -1
  311. package/dist/core/install-telemetry.js.map +1 -0
  312. package/dist/core/keybindings.d.ts +17 -1
  313. package/dist/core/keybindings.d.ts.map +1 -1
  314. package/dist/core/keybindings.js +24 -6
  315. package/dist/core/keybindings.js.map +1 -1
  316. package/dist/core/learning/learning-audit.d.ts +2 -4
  317. package/dist/core/learning/learning-audit.d.ts.map +1 -1
  318. package/dist/core/learning/learning-audit.js +9 -29
  319. package/dist/core/learning/learning-audit.js.map +1 -1
  320. package/dist/core/learning/learning-gate.d.ts.map +1 -1
  321. package/dist/core/learning/learning-gate.js +1 -6
  322. package/dist/core/learning/learning-gate.js.map +1 -1
  323. package/dist/core/learning/session-learning-decision.d.ts +2 -4
  324. package/dist/core/learning/session-learning-decision.d.ts.map +1 -1
  325. package/dist/core/learning/session-learning-decision.js +9 -29
  326. package/dist/core/learning/session-learning-decision.js.map +1 -1
  327. package/dist/core/local-runtime-controller.d.ts +1 -1
  328. package/dist/core/local-runtime-controller.d.ts.map +1 -1
  329. package/dist/core/local-runtime-controller.js.map +1 -1
  330. package/dist/core/memory-controller.d.ts.map +1 -1
  331. package/dist/core/memory-controller.js +4 -4
  332. package/dist/core/memory-controller.js.map +1 -1
  333. package/dist/core/model-capability.d.ts +0 -19
  334. package/dist/core/model-capability.d.ts.map +1 -1
  335. package/dist/core/model-capability.js +1 -19
  336. package/dist/core/model-capability.js.map +1 -1
  337. package/dist/core/model-registry.d.ts +6 -1
  338. package/dist/core/model-registry.d.ts.map +1 -1
  339. package/dist/core/model-registry.js +13 -5
  340. package/dist/core/model-registry.js.map +1 -1
  341. package/dist/core/model-resolver.d.ts +17 -2
  342. package/dist/core/model-resolver.d.ts.map +1 -1
  343. package/dist/core/model-resolver.js +38 -25
  344. package/dist/core/model-resolver.js.map +1 -1
  345. package/dist/core/model-router/intent-classifier.d.ts +1 -4
  346. package/dist/core/model-router/intent-classifier.d.ts.map +1 -1
  347. package/dist/core/model-router/intent-classifier.js +1 -156
  348. package/dist/core/model-router/intent-classifier.js.map +1 -1
  349. package/dist/core/model-router-controller.d.ts +1 -1
  350. package/dist/core/model-router-controller.d.ts.map +1 -1
  351. package/dist/core/model-router-controller.js +2 -2
  352. package/dist/core/model-router-controller.js.map +1 -1
  353. package/dist/core/model-selection-controller.d.ts +5 -5
  354. package/dist/core/model-selection-controller.d.ts.map +1 -1
  355. package/dist/core/model-selection-controller.js.map +1 -1
  356. package/dist/core/model-tool-protocol.d.ts +23 -0
  357. package/dist/core/model-tool-protocol.d.ts.map +1 -0
  358. package/dist/core/model-tool-protocol.js +43 -0
  359. package/dist/core/model-tool-protocol.js.map +1 -0
  360. package/dist/core/models/adaptation-store.d.ts +4 -6
  361. package/dist/core/models/adaptation-store.d.ts.map +1 -1
  362. package/dist/core/models/adaptation-store.js +95 -83
  363. package/dist/core/models/adaptation-store.js.map +1 -1
  364. package/dist/core/models/fitness-store.d.ts +2 -12
  365. package/dist/core/models/fitness-store.d.ts.map +1 -1
  366. package/dist/core/models/fitness-store.js +84 -57
  367. package/dist/core/models/fitness-store.js.map +1 -1
  368. package/dist/core/models/host-state-store.d.ts +34 -0
  369. package/dist/core/models/host-state-store.d.ts.map +1 -0
  370. package/dist/core/models/host-state-store.js +86 -0
  371. package/dist/core/models/host-state-store.js.map +1 -0
  372. package/dist/core/orchestration/agent-resume.d.ts +24 -0
  373. package/dist/core/orchestration/agent-resume.d.ts.map +1 -0
  374. package/dist/core/orchestration/agent-resume.js +90 -0
  375. package/dist/core/orchestration/agent-resume.js.map +1 -0
  376. package/dist/core/orchestration/capability-gateway.d.ts +61 -0
  377. package/dist/core/orchestration/capability-gateway.d.ts.map +1 -0
  378. package/dist/core/orchestration/capability-gateway.js +152 -0
  379. package/dist/core/orchestration/capability-gateway.js.map +1 -0
  380. package/dist/core/orchestration/contracts.d.ts +343 -0
  381. package/dist/core/orchestration/contracts.d.ts.map +1 -0
  382. package/dist/core/orchestration/contracts.js +135 -0
  383. package/dist/core/orchestration/contracts.js.map +1 -0
  384. package/dist/core/orchestration/delegation-ledger.d.ts +56 -0
  385. package/dist/core/orchestration/delegation-ledger.d.ts.map +1 -0
  386. package/dist/core/orchestration/delegation-ledger.js +200 -0
  387. package/dist/core/orchestration/delegation-ledger.js.map +1 -0
  388. package/dist/core/orchestration/event-store.d.ts +83 -0
  389. package/dist/core/orchestration/event-store.d.ts.map +1 -0
  390. package/dist/core/orchestration/event-store.js +560 -0
  391. package/dist/core/orchestration/event-store.js.map +1 -0
  392. package/dist/core/orchestration/lane-tool-manifests.d.ts +9 -0
  393. package/dist/core/orchestration/lane-tool-manifests.d.ts.map +1 -0
  394. package/dist/core/orchestration/lane-tool-manifests.js +37 -0
  395. package/dist/core/orchestration/lane-tool-manifests.js.map +1 -0
  396. package/dist/core/orchestration/model-binding.d.ts +15 -0
  397. package/dist/core/orchestration/model-binding.d.ts.map +1 -0
  398. package/dist/core/orchestration/model-binding.js +29 -0
  399. package/dist/core/orchestration/model-binding.js.map +1 -0
  400. package/dist/core/orchestration/policy-compiler.d.ts +49 -0
  401. package/dist/core/orchestration/policy-compiler.d.ts.map +1 -0
  402. package/dist/core/orchestration/policy-compiler.js +191 -0
  403. package/dist/core/orchestration/policy-compiler.js.map +1 -0
  404. package/dist/core/orchestration/policy-gate.d.ts +18 -0
  405. package/dist/core/orchestration/policy-gate.d.ts.map +1 -0
  406. package/dist/core/orchestration/policy-gate.js +24 -0
  407. package/dist/core/orchestration/policy-gate.js.map +1 -0
  408. package/dist/core/orchestration/profile-registry.d.ts +51 -0
  409. package/dist/core/orchestration/profile-registry.d.ts.map +1 -0
  410. package/dist/core/orchestration/profile-registry.js +393 -0
  411. package/dist/core/orchestration/profile-registry.js.map +1 -0
  412. package/dist/core/orchestration/profile-store.d.ts +31 -0
  413. package/dist/core/orchestration/profile-store.d.ts.map +1 -0
  414. package/dist/core/orchestration/profile-store.js +136 -0
  415. package/dist/core/orchestration/profile-store.js.map +1 -0
  416. package/dist/core/orchestration/risk-budget.d.ts +9 -0
  417. package/dist/core/orchestration/risk-budget.d.ts.map +1 -0
  418. package/dist/core/orchestration/risk-budget.js +48 -0
  419. package/dist/core/orchestration/risk-budget.js.map +1 -0
  420. package/dist/core/orchestration/task-runtime.d.ts +168 -0
  421. package/dist/core/orchestration/task-runtime.d.ts.map +1 -0
  422. package/dist/core/orchestration/task-runtime.js +1600 -0
  423. package/dist/core/orchestration/task-runtime.js.map +1 -0
  424. package/dist/core/orchestration/work-state-projection.d.ts +32 -0
  425. package/dist/core/orchestration/work-state-projection.d.ts.map +1 -0
  426. package/dist/core/orchestration/work-state-projection.js +116 -0
  427. package/dist/core/orchestration/work-state-projection.js.map +1 -0
  428. package/dist/core/orchestration/worker-execution-contract.d.ts +21 -0
  429. package/dist/core/orchestration/worker-execution-contract.d.ts.map +1 -0
  430. package/dist/core/orchestration/worker-execution-contract.js +179 -0
  431. package/dist/core/orchestration/worker-execution-contract.js.map +1 -0
  432. package/dist/core/orchestration/worker-result-adapter.d.ts +21 -0
  433. package/dist/core/orchestration/worker-result-adapter.d.ts.map +1 -0
  434. package/dist/core/orchestration/worker-result-adapter.js +109 -0
  435. package/dist/core/orchestration/worker-result-adapter.js.map +1 -0
  436. package/dist/core/package-manager.d.ts.map +1 -1
  437. package/dist/core/package-manager.js +20 -3
  438. package/dist/core/package-manager.js.map +1 -1
  439. package/dist/core/process-identity.d.ts +11 -0
  440. package/dist/core/process-identity.d.ts.map +1 -0
  441. package/dist/core/process-identity.js +26 -0
  442. package/dist/core/process-identity.js.map +1 -0
  443. package/dist/core/process-matrix/codes.d.ts +14 -3
  444. package/dist/core/process-matrix/codes.d.ts.map +1 -1
  445. package/dist/core/process-matrix/codes.js.map +1 -1
  446. package/dist/core/process-matrix/resume-launcher.d.ts +26 -0
  447. package/dist/core/process-matrix/resume-launcher.d.ts.map +1 -0
  448. package/dist/core/process-matrix/resume-launcher.js +62 -0
  449. package/dist/core/process-matrix/resume-launcher.js.map +1 -0
  450. package/dist/core/process-matrix/runtime.d.ts +37 -22
  451. package/dist/core/process-matrix/runtime.d.ts.map +1 -1
  452. package/dist/core/process-matrix/runtime.js +399 -76
  453. package/dist/core/process-matrix/runtime.js.map +1 -1
  454. package/dist/core/process-matrix/store.d.ts +5 -2
  455. package/dist/core/process-matrix/store.d.ts.map +1 -1
  456. package/dist/core/process-matrix/store.js +123 -14
  457. package/dist/core/process-matrix/store.js.map +1 -1
  458. package/dist/core/process-matrix/supervisor.d.ts +10 -7
  459. package/dist/core/process-matrix/supervisor.d.ts.map +1 -1
  460. package/dist/core/process-matrix/supervisor.js +45 -14
  461. package/dist/core/process-matrix/supervisor.js.map +1 -1
  462. package/dist/core/profile-filter-controller.d.ts +8 -1
  463. package/dist/core/profile-filter-controller.d.ts.map +1 -1
  464. package/dist/core/profile-filter-controller.js +23 -5
  465. package/dist/core/profile-filter-controller.js.map +1 -1
  466. package/dist/core/profile-registry.d.ts.map +1 -1
  467. package/dist/core/profile-registry.js +6 -8
  468. package/dist/core/profile-registry.js.map +1 -1
  469. package/dist/core/prompt-templates.d.ts +1 -0
  470. package/dist/core/prompt-templates.d.ts.map +1 -1
  471. package/dist/core/prompt-templates.js +18 -29
  472. package/dist/core/prompt-templates.js.map +1 -1
  473. package/dist/core/reflection-controller.d.ts +5 -5
  474. package/dist/core/reflection-controller.d.ts.map +1 -1
  475. package/dist/core/reflection-controller.js +5 -2
  476. package/dist/core/reflection-controller.js.map +1 -1
  477. package/dist/core/reload-blockers.d.ts.map +1 -1
  478. package/dist/core/reload-blockers.js +6 -8
  479. package/dist/core/reload-blockers.js.map +1 -1
  480. package/dist/core/request-auth.d.ts +7 -0
  481. package/dist/core/request-auth.d.ts.map +1 -0
  482. package/dist/core/request-auth.js +8 -0
  483. package/dist/core/request-auth.js.map +1 -0
  484. package/dist/core/research/evidence-bundle.d.ts.map +1 -1
  485. package/dist/core/research/evidence-bundle.js +1 -6
  486. package/dist/core/research/evidence-bundle.js.map +1 -1
  487. package/dist/core/research/lane-model-resolver.d.ts +33 -0
  488. package/dist/core/research/lane-model-resolver.d.ts.map +1 -0
  489. package/dist/core/research/lane-model-resolver.js +65 -0
  490. package/dist/core/research/lane-model-resolver.js.map +1 -0
  491. package/dist/core/research/model-fitness-controller.d.ts +38 -0
  492. package/dist/core/research/model-fitness-controller.d.ts.map +1 -0
  493. package/dist/core/research/model-fitness-controller.js +106 -0
  494. package/dist/core/research/model-fitness-controller.js.map +1 -0
  495. package/dist/core/research/model-fitness.d.ts +1 -1
  496. package/dist/core/research/model-fitness.d.ts.map +1 -1
  497. package/dist/core/research/model-fitness.js +4 -4
  498. package/dist/core/research/model-fitness.js.map +1 -1
  499. package/dist/core/research/research-gate.d.ts.map +1 -1
  500. package/dist/core/research/research-gate.js +7 -7
  501. package/dist/core/research/research-gate.js.map +1 -1
  502. package/dist/core/research/research-lane-controller.d.ts +62 -0
  503. package/dist/core/research/research-lane-controller.d.ts.map +1 -0
  504. package/dist/core/research/research-lane-controller.js +292 -0
  505. package/dist/core/research/research-lane-controller.js.map +1 -0
  506. package/dist/core/research/session-evidence-bundle.d.ts +2 -4
  507. package/dist/core/research/session-evidence-bundle.d.ts.map +1 -1
  508. package/dist/core/research/session-evidence-bundle.js +10 -46
  509. package/dist/core/research/session-evidence-bundle.js.map +1 -1
  510. package/dist/core/resource-loader.d.ts.map +1 -1
  511. package/dist/core/resource-loader.js +23 -8
  512. package/dist/core/resource-loader.js.map +1 -1
  513. package/dist/core/risk-classifier.d.ts +6 -0
  514. package/dist/core/risk-classifier.d.ts.map +1 -0
  515. package/dist/core/risk-classifier.js +233 -0
  516. package/dist/core/risk-classifier.js.map +1 -0
  517. package/dist/core/runtime-builder.d.ts +41 -23
  518. package/dist/core/runtime-builder.d.ts.map +1 -1
  519. package/dist/core/runtime-builder.js +365 -253
  520. package/dist/core/runtime-builder.js.map +1 -1
  521. package/dist/core/sdk.d.ts +6 -3
  522. package/dist/core/sdk.d.ts.map +1 -1
  523. package/dist/core/sdk.js +68 -25
  524. package/dist/core/sdk.js.map +1 -1
  525. package/dist/core/secrets/credential-exposure-guard.d.ts +9 -0
  526. package/dist/core/secrets/credential-exposure-guard.d.ts.map +1 -0
  527. package/dist/core/secrets/credential-exposure-guard.js +241 -0
  528. package/dist/core/secrets/credential-exposure-guard.js.map +1 -0
  529. package/dist/core/secrets/secret-dotenv.d.ts +27 -0
  530. package/dist/core/secrets/secret-dotenv.d.ts.map +1 -0
  531. package/dist/core/secrets/secret-dotenv.js +183 -0
  532. package/dist/core/secrets/secret-dotenv.js.map +1 -0
  533. package/dist/core/secrets/secret-vault-format.d.ts +112 -0
  534. package/dist/core/secrets/secret-vault-format.d.ts.map +1 -0
  535. package/dist/core/secrets/secret-vault-format.js +397 -0
  536. package/dist/core/secrets/secret-vault-format.js.map +1 -0
  537. package/dist/core/secrets/secret-vault.d.ts +72 -0
  538. package/dist/core/secrets/secret-vault.d.ts.map +1 -0
  539. package/dist/core/secrets/secret-vault.js +671 -0
  540. package/dist/core/secrets/secret-vault.js.map +1 -0
  541. package/dist/core/session-analytics.d.ts +4 -23
  542. package/dist/core/session-analytics.d.ts.map +1 -1
  543. package/dist/core/session-analytics.js +67 -70
  544. package/dist/core/session-analytics.js.map +1 -1
  545. package/dist/core/session-image-store.d.ts +33 -0
  546. package/dist/core/session-image-store.d.ts.map +1 -0
  547. package/dist/core/session-image-store.js +283 -0
  548. package/dist/core/session-image-store.js.map +1 -0
  549. package/dist/core/session-role.d.ts +11 -4
  550. package/dist/core/session-role.d.ts.map +1 -1
  551. package/dist/core/session-role.js +24 -5
  552. package/dist/core/session-role.js.map +1 -1
  553. package/dist/core/session-snapshot.d.ts +34 -0
  554. package/dist/core/session-snapshot.d.ts.map +1 -0
  555. package/dist/core/session-snapshot.js +68 -0
  556. package/dist/core/session-snapshot.js.map +1 -0
  557. package/dist/core/session-supervision-runtime.d.ts +31 -0
  558. package/dist/core/session-supervision-runtime.d.ts.map +1 -0
  559. package/dist/core/session-supervision-runtime.js +97 -0
  560. package/dist/core/session-supervision-runtime.js.map +1 -0
  561. package/dist/core/session-tree-navigator.d.ts +4 -6
  562. package/dist/core/session-tree-navigator.d.ts.map +1 -1
  563. package/dist/core/session-tree-navigator.js +4 -1
  564. package/dist/core/session-tree-navigator.js.map +1 -1
  565. package/dist/core/settings-manager.d.ts +10 -5
  566. package/dist/core/settings-manager.d.ts.map +1 -1
  567. package/dist/core/settings-manager.js +81 -29
  568. package/dist/core/settings-manager.js.map +1 -1
  569. package/dist/core/slash-commands.d.ts.map +1 -1
  570. package/dist/core/slash-commands.js +2 -2
  571. package/dist/core/slash-commands.js.map +1 -1
  572. package/dist/core/tasks/session-task-state.d.ts +2 -4
  573. package/dist/core/tasks/session-task-state.d.ts.map +1 -1
  574. package/dist/core/tasks/session-task-state.js +10 -28
  575. package/dist/core/tasks/session-task-state.js.map +1 -1
  576. package/dist/core/tasks/task-projection.d.ts +20 -0
  577. package/dist/core/tasks/task-projection.d.ts.map +1 -0
  578. package/dist/core/tasks/task-projection.js +47 -0
  579. package/dist/core/tasks/task-projection.js.map +1 -0
  580. package/dist/core/tasks/task-state.d.ts +4 -0
  581. package/dist/core/tasks/task-state.d.ts.map +1 -1
  582. package/dist/core/tasks/task-state.js +28 -11
  583. package/dist/core/tasks/task-state.js.map +1 -1
  584. package/dist/core/tool-capability-policy.d.ts +13 -0
  585. package/dist/core/tool-capability-policy.d.ts.map +1 -0
  586. package/dist/core/tool-capability-policy.js +86 -0
  587. package/dist/core/tool-capability-policy.js.map +1 -0
  588. package/dist/core/tool-gate-controller.d.ts.map +1 -1
  589. package/dist/core/tool-gate-controller.js +8 -2
  590. package/dist/core/tool-gate-controller.js.map +1 -1
  591. package/dist/core/tool-protocol-controller.d.ts +89 -0
  592. package/dist/core/tool-protocol-controller.d.ts.map +1 -0
  593. package/dist/core/tool-protocol-controller.js +512 -0
  594. package/dist/core/tool-protocol-controller.js.map +1 -0
  595. package/dist/core/tool-recovery-log-records.d.ts +2 -3
  596. package/dist/core/tool-recovery-log-records.d.ts.map +1 -1
  597. package/dist/core/tool-recovery-log-records.js +9 -35
  598. package/dist/core/tool-recovery-log-records.js.map +1 -1
  599. package/dist/core/tool-recovery-stats.d.ts +21 -1
  600. package/dist/core/tool-recovery-stats.d.ts.map +1 -1
  601. package/dist/core/tool-recovery-stats.js.map +1 -1
  602. package/dist/core/tool-selection/tool-performance-store.d.ts +3 -7
  603. package/dist/core/tool-selection/tool-performance-store.d.ts.map +1 -1
  604. package/dist/core/tool-selection/tool-performance-store.js +97 -138
  605. package/dist/core/tool-selection/tool-performance-store.js.map +1 -1
  606. package/dist/core/tools/ask-question.d.ts +124 -0
  607. package/dist/core/tools/ask-question.d.ts.map +1 -0
  608. package/dist/core/tools/ask-question.js +693 -0
  609. package/dist/core/tools/ask-question.js.map +1 -0
  610. package/dist/core/tools/bash.d.ts +5 -0
  611. package/dist/core/tools/bash.d.ts.map +1 -1
  612. package/dist/core/tools/bash.js +59 -5
  613. package/dist/core/tools/bash.js.map +1 -1
  614. package/dist/core/tools/delegate-status.d.ts +31 -3
  615. package/dist/core/tools/delegate-status.d.ts.map +1 -1
  616. package/dist/core/tools/delegate-status.js +148 -29
  617. package/dist/core/tools/delegate-status.js.map +1 -1
  618. package/dist/core/tools/delegate.d.ts +15 -13
  619. package/dist/core/tools/delegate.d.ts.map +1 -1
  620. package/dist/core/tools/delegate.js +106 -23
  621. package/dist/core/tools/delegate.js.map +1 -1
  622. package/dist/core/tools/extensionify.d.ts.map +1 -1
  623. package/dist/core/tools/extensionify.js +1 -0
  624. package/dist/core/tools/extensionify.js.map +1 -1
  625. package/dist/core/tools/git-filter.d.ts +1 -8
  626. package/dist/core/tools/git-filter.d.ts.map +1 -1
  627. package/dist/core/tools/git-filter.js +2 -63
  628. package/dist/core/tools/git-filter.js.map +1 -1
  629. package/dist/core/tools/goal.d.ts +10 -8
  630. package/dist/core/tools/goal.d.ts.map +1 -1
  631. package/dist/core/tools/goal.js +106 -28
  632. package/dist/core/tools/goal.js.map +1 -1
  633. package/dist/core/tools/index.d.ts +1 -0
  634. package/dist/core/tools/index.d.ts.map +1 -1
  635. package/dist/core/tools/index.js +1 -0
  636. package/dist/core/tools/index.js.map +1 -1
  637. package/dist/core/tools/model-fitness.d.ts.map +1 -1
  638. package/dist/core/tools/model-fitness.js +2 -2
  639. package/dist/core/tools/model-fitness.js.map +1 -1
  640. package/dist/core/tools/orchestration-panel.d.ts +40 -0
  641. package/dist/core/tools/orchestration-panel.d.ts.map +1 -0
  642. package/dist/core/tools/orchestration-panel.js +124 -0
  643. package/dist/core/tools/orchestration-panel.js.map +1 -0
  644. package/dist/core/tools/output-accumulator.d.ts +11 -0
  645. package/dist/core/tools/output-accumulator.d.ts.map +1 -1
  646. package/dist/core/tools/output-accumulator.js +36 -3
  647. package/dist/core/tools/output-accumulator.js.map +1 -1
  648. package/dist/core/tools/python.d.ts +2 -0
  649. package/dist/core/tools/python.d.ts.map +1 -1
  650. package/dist/core/tools/python.js +3 -1
  651. package/dist/core/tools/python.js.map +1 -1
  652. package/dist/core/tools/read.d.ts.map +1 -1
  653. package/dist/core/tools/read.js +1 -1
  654. package/dist/core/tools/read.js.map +1 -1
  655. package/dist/core/tools/run-process.d.ts +26 -0
  656. package/dist/core/tools/run-process.d.ts.map +1 -0
  657. package/dist/core/tools/run-process.js +138 -0
  658. package/dist/core/tools/run-process.js.map +1 -0
  659. package/dist/core/tools/search-command-guard.d.ts +14 -0
  660. package/dist/core/tools/search-command-guard.d.ts.map +1 -0
  661. package/dist/core/tools/search-command-guard.js +333 -0
  662. package/dist/core/tools/search-command-guard.js.map +1 -0
  663. package/dist/core/tools/secret-store.d.ts +38 -0
  664. package/dist/core/tools/secret-store.d.ts.map +1 -0
  665. package/dist/core/tools/secret-store.js +412 -0
  666. package/dist/core/tools/secret-store.js.map +1 -0
  667. package/dist/core/tools/shell-command-parser.d.ts +16 -0
  668. package/dist/core/tools/shell-command-parser.d.ts.map +1 -0
  669. package/dist/core/tools/shell-command-parser.js +113 -0
  670. package/dist/core/tools/shell-command-parser.js.map +1 -0
  671. package/dist/core/tools/task-steps.d.ts +2 -0
  672. package/dist/core/tools/task-steps.d.ts.map +1 -1
  673. package/dist/core/tools/task-steps.js +99 -0
  674. package/dist/core/tools/task-steps.js.map +1 -1
  675. package/dist/core/tools/tmux-dispatch.d.ts +3 -5
  676. package/dist/core/tools/tmux-dispatch.d.ts.map +1 -1
  677. package/dist/core/tools/tmux-dispatch.js +4 -4
  678. package/dist/core/tools/tmux-dispatch.js.map +1 -1
  679. package/dist/core/tools/windows-shell-engine.d.ts.map +1 -1
  680. package/dist/core/tools/windows-shell-engine.js +4 -6
  681. package/dist/core/tools/windows-shell-engine.js.map +1 -1
  682. package/dist/core/util/atomic-file.d.ts +6 -2
  683. package/dist/core/util/atomic-file.d.ts.map +1 -1
  684. package/dist/core/util/atomic-file.js +4 -4
  685. package/dist/core/util/atomic-file.js.map +1 -1
  686. package/dist/core/util/bounded-jsonl.d.ts +10 -0
  687. package/dist/core/util/bounded-jsonl.d.ts.map +1 -0
  688. package/dist/core/util/bounded-jsonl.js +75 -0
  689. package/dist/core/util/bounded-jsonl.js.map +1 -0
  690. package/dist/core/util/value-guards.d.ts +5 -0
  691. package/dist/core/util/value-guards.d.ts.map +1 -0
  692. package/dist/core/util/value-guards.js +12 -0
  693. package/dist/core/util/value-guards.js.map +1 -0
  694. package/dist/core/worktree-sync/store.d.ts +3 -3
  695. package/dist/core/worktree-sync/store.d.ts.map +1 -1
  696. package/dist/core/worktree-sync/store.js +10 -5
  697. package/dist/core/worktree-sync/store.js.map +1 -1
  698. package/dist/index.d.ts +15 -4
  699. package/dist/index.d.ts.map +1 -1
  700. package/dist/index.js +14 -3
  701. package/dist/index.js.map +1 -1
  702. package/dist/main.d.ts.map +1 -1
  703. package/dist/main.js +149 -51
  704. package/dist/main.js.map +1 -1
  705. package/dist/migrations.d.ts +4 -6
  706. package/dist/migrations.d.ts.map +1 -1
  707. package/dist/migrations.js +186 -28
  708. package/dist/migrations.js.map +1 -1
  709. package/dist/modes/interactive/auth-dialogs-controller.d.ts +4 -0
  710. package/dist/modes/interactive/auth-dialogs-controller.d.ts.map +1 -1
  711. package/dist/modes/interactive/auth-dialogs-controller.js +60 -1
  712. package/dist/modes/interactive/auth-dialogs-controller.js.map +1 -1
  713. package/dist/modes/interactive/clipboard-input.d.ts +7 -1
  714. package/dist/modes/interactive/clipboard-input.d.ts.map +1 -1
  715. package/dist/modes/interactive/clipboard-input.js +44 -6
  716. package/dist/modes/interactive/clipboard-input.js.map +1 -1
  717. package/dist/modes/interactive/components/activity-lane.d.ts +55 -0
  718. package/dist/modes/interactive/components/activity-lane.d.ts.map +1 -0
  719. package/dist/modes/interactive/components/activity-lane.js +304 -0
  720. package/dist/modes/interactive/components/activity-lane.js.map +1 -0
  721. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  722. package/dist/modes/interactive/components/assistant-message.js +19 -10
  723. package/dist/modes/interactive/components/assistant-message.js.map +1 -1
  724. package/dist/modes/interactive/components/expandable-text.d.ts +12 -0
  725. package/dist/modes/interactive/components/expandable-text.d.ts.map +1 -0
  726. package/dist/modes/interactive/components/expandable-text.js +17 -0
  727. package/dist/modes/interactive/components/expandable-text.js.map +1 -0
  728. package/dist/modes/interactive/components/extension-editor.d.ts +15 -1
  729. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -1
  730. package/dist/modes/interactive/components/extension-editor.js +30 -21
  731. package/dist/modes/interactive/components/extension-editor.js.map +1 -1
  732. package/dist/modes/interactive/components/extension-input.d.ts +1 -0
  733. package/dist/modes/interactive/components/extension-input.d.ts.map +1 -1
  734. package/dist/modes/interactive/components/extension-input.js +6 -1
  735. package/dist/modes/interactive/components/extension-input.js.map +1 -1
  736. package/dist/modes/interactive/components/scoped-models-selector.d.ts +2 -2
  737. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
  738. package/dist/modes/interactive/components/scoped-models-selector.js +23 -14
  739. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
  740. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  741. package/dist/modes/interactive/components/settings-selector.js +6 -6
  742. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  743. package/dist/modes/interactive/components/tool-execution.d.ts +19 -11
  744. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  745. package/dist/modes/interactive/components/tool-execution.js +53 -20
  746. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  747. package/dist/modes/interactive/components/tool-group.d.ts.map +1 -1
  748. package/dist/modes/interactive/components/tool-group.js +3 -0
  749. package/dist/modes/interactive/components/tool-group.js.map +1 -1
  750. package/dist/modes/interactive/components/usage-action-selector.d.ts +16 -0
  751. package/dist/modes/interactive/components/usage-action-selector.d.ts.map +1 -0
  752. package/dist/modes/interactive/components/usage-action-selector.js +46 -0
  753. package/dist/modes/interactive/components/usage-action-selector.js.map +1 -0
  754. package/dist/modes/interactive/config-backup.d.ts.map +1 -1
  755. package/dist/modes/interactive/config-backup.js +5 -4
  756. package/dist/modes/interactive/config-backup.js.map +1 -1
  757. package/dist/modes/interactive/extension-ui-host.d.ts +14 -0
  758. package/dist/modes/interactive/extension-ui-host.d.ts.map +1 -1
  759. package/dist/modes/interactive/extension-ui-host.js +77 -15
  760. package/dist/modes/interactive/extension-ui-host.js.map +1 -1
  761. package/dist/modes/interactive/external-editor.d.ts +15 -0
  762. package/dist/modes/interactive/external-editor.d.ts.map +1 -1
  763. package/dist/modes/interactive/external-editor.js +27 -19
  764. package/dist/modes/interactive/external-editor.js.map +1 -1
  765. package/dist/modes/interactive/history-reload-math.d.ts +6 -2
  766. package/dist/modes/interactive/history-reload-math.d.ts.map +1 -1
  767. package/dist/modes/interactive/history-reload-math.js +10 -5
  768. package/dist/modes/interactive/history-reload-math.js.map +1 -1
  769. package/dist/modes/interactive/interactive-event-controller.d.ts +74 -0
  770. package/dist/modes/interactive/interactive-event-controller.d.ts.map +1 -0
  771. package/dist/modes/interactive/interactive-event-controller.js +328 -0
  772. package/dist/modes/interactive/interactive-event-controller.js.map +1 -0
  773. package/dist/modes/interactive/interactive-mode.d.ts +22 -24
  774. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  775. package/dist/modes/interactive/interactive-mode.js +245 -595
  776. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  777. package/dist/modes/interactive/key-handlers.d.ts.map +1 -1
  778. package/dist/modes/interactive/key-handlers.js +1 -1
  779. package/dist/modes/interactive/key-handlers.js.map +1 -1
  780. package/dist/modes/interactive/loaded-resources-view.d.ts +20 -0
  781. package/dist/modes/interactive/loaded-resources-view.d.ts.map +1 -0
  782. package/dist/modes/interactive/loaded-resources-view.js +147 -0
  783. package/dist/modes/interactive/loaded-resources-view.js.map +1 -0
  784. package/dist/modes/interactive/profile-menu-controller.d.ts.map +1 -1
  785. package/dist/modes/interactive/profile-menu-controller.js +7 -7
  786. package/dist/modes/interactive/profile-menu-controller.js.map +1 -1
  787. package/dist/modes/interactive/report-commands.d.ts.map +1 -1
  788. package/dist/modes/interactive/report-commands.js +21 -0
  789. package/dist/modes/interactive/report-commands.js.map +1 -1
  790. package/dist/modes/interactive/resource-display.d.ts.map +1 -1
  791. package/dist/modes/interactive/resource-display.js +8 -2
  792. package/dist/modes/interactive/resource-display.js.map +1 -1
  793. package/dist/modes/interactive/resource-shell-commands.d.ts +1 -5
  794. package/dist/modes/interactive/resource-shell-commands.d.ts.map +1 -1
  795. package/dist/modes/interactive/resource-shell-commands.js +0 -5
  796. package/dist/modes/interactive/resource-shell-commands.js.map +1 -1
  797. package/dist/modes/interactive/session-flow-commands.d.ts +27 -8
  798. package/dist/modes/interactive/session-flow-commands.d.ts.map +1 -1
  799. package/dist/modes/interactive/session-flow-commands.js +150 -64
  800. package/dist/modes/interactive/session-flow-commands.js.map +1 -1
  801. package/dist/modes/interactive/session-io-commands.d.ts.map +1 -1
  802. package/dist/modes/interactive/session-io-commands.js +16 -6
  803. package/dist/modes/interactive/session-io-commands.js.map +1 -1
  804. package/dist/modes/interactive/session-replacement-errors.d.ts +7 -0
  805. package/dist/modes/interactive/session-replacement-errors.d.ts.map +1 -0
  806. package/dist/modes/interactive/session-replacement-errors.js +15 -0
  807. package/dist/modes/interactive/session-replacement-errors.js.map +1 -0
  808. package/dist/modes/interactive/startup-checks.d.ts.map +1 -1
  809. package/dist/modes/interactive/startup-checks.js +1 -1
  810. package/dist/modes/interactive/startup-checks.js.map +1 -1
  811. package/dist/modes/interactive/usage-commands.d.ts +34 -0
  812. package/dist/modes/interactive/usage-commands.d.ts.map +1 -0
  813. package/dist/modes/interactive/usage-commands.js +225 -0
  814. package/dist/modes/interactive/usage-commands.js.map +1 -0
  815. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  816. package/dist/modes/rpc/rpc-mode.js +82 -23
  817. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  818. package/dist/modes/rpc/rpc-types.d.ts +15 -0
  819. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  820. package/dist/modes/rpc/rpc-types.js +138 -1
  821. package/dist/modes/rpc/rpc-types.js.map +1 -1
  822. package/dist/utils/clipboard-image.d.ts.map +1 -1
  823. package/dist/utils/clipboard-image.js +2 -13
  824. package/dist/utils/clipboard-image.js.map +1 -1
  825. package/dist/utils/clipboard-native.d.ts +1 -0
  826. package/dist/utils/clipboard-native.d.ts.map +1 -1
  827. package/dist/utils/clipboard-native.js.map +1 -1
  828. package/dist/utils/clipboard.d.ts +1 -0
  829. package/dist/utils/clipboard.d.ts.map +1 -1
  830. package/dist/utils/clipboard.js +52 -1
  831. package/dist/utils/clipboard.js.map +1 -1
  832. package/dist/utils/platform.d.ts +3 -0
  833. package/dist/utils/platform.d.ts.map +1 -0
  834. package/dist/utils/platform.js +8 -0
  835. package/dist/utils/platform.js.map +1 -0
  836. package/dist/utils/shell.d.ts.map +1 -1
  837. package/dist/utils/shell.js +5 -3
  838. package/dist/utils/shell.js.map +1 -1
  839. package/dist/utils/version-check.d.ts.map +1 -1
  840. package/dist/utils/version-check.js +3 -1
  841. package/dist/utils/version-check.js.map +1 -1
  842. package/dist/utils/work-directory.d.ts +11 -0
  843. package/dist/utils/work-directory.d.ts.map +1 -1
  844. package/dist/utils/work-directory.js +30 -9
  845. package/dist/utils/work-directory.js.map +1 -1
  846. package/docs/UI_POLISH_ORCHESTRATION.md +38 -0
  847. package/docs/UI_QUALITY_ORCHESTRATION.md +28 -0
  848. package/docs/docs.json +4 -0
  849. package/docs/extensions.md +62 -2
  850. package/docs/harness-architecture.md +156 -0
  851. package/docs/keybindings.md +11 -1
  852. package/docs/process-matrix.md +59 -26
  853. package/docs/providers.md +9 -1
  854. package/docs/rpc.md +45 -2
  855. package/docs/sessions.md +15 -0
  856. package/docs/settings.md +125 -11
  857. package/docs/task-steps.md +8 -0
  858. package/docs/telemetry.md +19 -0
  859. package/docs/tmux-agent-manager.md +10 -3
  860. package/docs/ui-reference/README.md +9 -0
  861. package/docs/ui-reference/orchestration-after.txt +14 -0
  862. package/docs/ui-reference/orchestration-before.txt +13 -0
  863. package/docs/ui-reference/orchestration-reference.txt +16 -0
  864. package/docs/usage.md +22 -2
  865. package/docs/work-directory.md +19 -8
  866. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  867. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  868. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  869. package/examples/extensions/question.ts +34 -245
  870. package/examples/extensions/questionnaire.ts +41 -407
  871. package/examples/extensions/sandbox/package-lock.json +2 -2
  872. package/examples/extensions/sandbox/package.json +1 -1
  873. package/examples/extensions/with-deps/package-lock.json +2 -2
  874. package/examples/extensions/with-deps/package.json +1 -1
  875. package/npm-shrinkwrap.json +13 -13
  876. package/package.json +4 -4
  877. package/dist/core/delegation/session-worker-result.d.ts +0 -46
  878. package/dist/core/delegation/session-worker-result.d.ts.map +0 -1
  879. package/dist/core/delegation/session-worker-result.js +0 -113
  880. package/dist/core/delegation/session-worker-result.js.map +0 -1
  881. package/dist/core/delegation/worker-result.d.ts.map +0 -1
  882. package/dist/core/delegation/worker-result.js.map +0 -1
  883. package/dist/core/telemetry.d.ts.map +0 -1
  884. package/dist/core/telemetry.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"context-artifacts.js","sourceRoot":"","sources":["../../../src/core/context/context-artifacts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACtG,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAA2B,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AA0BnG,MAAM,UAAU,uBAAuB,CAAC,KAA6C,EAAkC;IACtH,OAAQ,KAA+B,CAAC,OAAO,KAAK,IAAI,CAAC;AAAA,CACzD;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CACjC,OAGC,EACQ;IACT,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;IAClC,KAAK,MAAM,KAAK,IAAI;QACnB,OAAO,CAAC,IAAI;QACZ,OAAO,CAAC,QAAQ,IAAI,EAAE;QACtB,OAAO,CAAC,OAAO,IAAI,EAAE;QACrB,OAAO,CAAC,IAAI,IAAI,EAAE;QAClB,OAAO,CAAC,cAAc,IAAI,EAAE;QAC5B,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC;QAC7B,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC;KAC5B,EAAE,CAAC;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,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AM,CAAC;oBACR,SAAS;gBACV,CAAC;gBACD,IAAI,CAAC;oBACJ,UAAU,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;gBACtC,CAAC;gBAAC,MAAM,CAAC;oBACR,iFAAiF;gBAClF,CAAC;gBACD,qBAAqB,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;gBAC9B,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAClB,CAAC;YACD,OAAO,OAAO,CAAC;QAAA,CACf;KACD,CAAC;AAAA,CACF","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\tconst hash = createHash(\"sha256\");\n\tfor (const field of [\n\t\trequest.kind,\n\t\trequest.toolName ?? \"\",\n\t\trequest.command ?? \"\",\n\t\trequest.path ?? \"\",\n\t\trequest.sessionEntryId ?? \"\",\n\t\tString(request.createdAtTurn),\n\t\tString(request.reproducible),\n\t]) {\n\t\thash.update(field).update(\"\\0\");\n\t}\n\thash.update(request.content);\n\treturn hash.digest(\"hex\").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"]}
1
+ {"version":3,"file":"context-artifacts.js","sourceRoot":"","sources":["../../../src/core/context/context-artifacts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACtG,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAA2B,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AA0BnG,MAAM,UAAU,uBAAuB,CAAC,KAA6C,EAAkC;IACtH,OAAQ,KAA+B,CAAC,OAAO,KAAK,IAAI,CAAC;AAAA,CACzD;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CACjC,OAGC,EACQ;IACT,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;IAClC,KAAK,MAAM,KAAK,IAAI;QACnB,OAAO,CAAC,IAAI;QACZ,O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ClD,IAAI,CAAC;oBACJ,UAAU,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;gBACnC,CAAC;gBAAC,MAAM,CAAC;oBACR,SAAS;gBACV,CAAC;gBACD,IAAI,CAAC;oBACJ,UAAU,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;gBACtC,CAAC;gBAAC,MAAM,CAAC;oBACR,iFAAiF;gBAClF,CAAC;gBACD,qBAAqB,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;gBAC9B,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAClB,CAAC;YACD,OAAO,OAAO,CAAC;QAAA,CACf;KACD,CAAC;AAAA,CACF","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>/work/context/sessions/<sessionId>/artifacts/`).\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\tconst hash = createHash(\"sha256\");\n\tfor (const field of [\n\t\trequest.kind,\n\t\trequest.toolName ?? \"\",\n\t\trequest.command ?? \"\",\n\t\trequest.path ?? \"\",\n\t\trequest.sessionEntryId ?? \"\",\n\t\tString(request.createdAtTurn),\n\t\tString(request.reproducible),\n\t]) {\n\t\thash.update(field).update(\"\\0\");\n\t}\n\thash.update(request.content);\n\treturn hash.digest(\"hex\").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/** Leased session work directory where payloads and metadata live. Created on first use. */\n\tbaseDir: string;\n\t/** Migrate an existing store before probing it; must remain zero-write when no legacy data exists. */\n\tprepareBaseDir?: () => void;\n\t/** Acquire the owning lease immediately before the first real store access. */\n\tacquireBaseDir?: () => 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\tconst cleanedUpThisInstance = new Set<string>();\n\tlet acquiredBaseDir: string | undefined;\n\tlet preparedBaseDir = false;\n\tconst ensureBaseDir = (): string => {\n\t\tpreparedBaseDir = true;\n\t\tacquiredBaseDir ??= options.acquireBaseDir?.() ?? baseDir;\n\t\tmkdirSync(acquiredBaseDir, { recursive: true });\n\t\treturn acquiredBaseDir;\n\t};\n\tconst ensureExistingBaseDir = (): boolean => {\n\t\tif (!preparedBaseDir) {\n\t\t\toptions.prepareBaseDir?.();\n\t\t\tpreparedBaseDir = true;\n\t\t}\n\t\tif (!existsSync(baseDir)) return false;\n\t\tensureBaseDir();\n\t\treturn true;\n\t};\n\n\treturn {\n\t\twrite(request: ArtifactWriteRequest): ArtifactRecord {\n\t\t\tensureBaseDir();\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\tif (!ensureExistingBaseDir()) 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\tif (!ensureExistingBaseDir()) 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\tif (!ensureExistingBaseDir()) 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\tif (!ensureExistingBaseDir()) 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\tif (!ensureExistingBaseDir()) 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\tif (!ensureExistingBaseDir()) 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\tif (!ensureExistingBaseDir()) return deleted;\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"]}
@@ -1,3 +1,4 @@
1
+ export type ContextStoreKind = "gc" | "artifacts";
1
2
  export interface ContextStoreRetentionOptions {
2
3
  maxAgeMs?: number;
3
4
  maxSessionDirs?: number;
@@ -9,7 +10,12 @@ export interface ContextStoreRetentionLease {
9
10
  readonly artifactsDir: string;
10
11
  release(): void;
11
12
  }
12
- export declare function pruneContextStores(agentDir: string, tenant: "gc" | "artifacts", options?: ContextStoreRetentionOptions): string[];
13
- export declare function getContextStoreDir(agentDir: string, tenant: "gc" | "artifacts", sessionId: string): string;
13
+ /**
14
+ * Move recognized context payloads from both historical layouts into the single leased session
15
+ * namespace. Unknown files, symlinks, active runs, and conflicting destinations are left untouched.
16
+ */
17
+ export declare function migrateLegacyContextStores(agentDir: string, onlySessionId?: string): void;
18
+ export declare function pruneContextStores(agentDir: string, options?: ContextStoreRetentionOptions): string[];
19
+ export declare function getContextStoreDir(agentDir: string, kind: ContextStoreKind, sessionId: string): string;
14
20
  export declare function acquireContextStoreRetention(agentDir: string, sessionId: string, options?: ContextStoreRetentionOptions): ContextStoreRetentionLease;
15
21
  //# sourceMappingURL=context-store-retention.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"context-store-retention.d.ts","sourceRoot":"","sources":["../../../src/core/context/context-store-retention.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,4BAA4B;IAC5C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,GAAG,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,0BAA0B;IAC1C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,OAAO,IAAI,IAAI,CAAC;CAChB;AAWD,wBAAgB,kBAAkB,CACjC,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,IAAI,GAAG,WAAW,EAC1B,OAAO,GAAE,4BAAiC,GACxC,MAAM,EAAE,CAEV;AAED,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAG,WAAW,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAE1G;AAED,wBAAgB,4BAA4B,CAC3C,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,4BAAiC,GACxC,0BAA0B,CA4B5B","sourcesContent":["import {\n\tacquireWorkRun,\n\tgetWorkRunDir,\n\tpruneWorkTenant,\n\ttype WorkRetentionOptions,\n\ttype WorkRunLease,\n} from \"../../utils/work-directory.ts\";\n\nconst DEFAULT_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;\nconst DEFAULT_MAX_SESSION_DIRS = 64;\nconst DEFAULT_MAX_TOTAL_BYTES = 512 * 1024 * 1024;\n\nexport interface ContextStoreRetentionOptions {\n\tmaxAgeMs?: number;\n\tmaxSessionDirs?: number;\n\tmaxTotalBytes?: number;\n\tnow?: number;\n}\n\nexport interface ContextStoreRetentionLease {\n\treadonly gcDir: string;\n\treadonly artifactsDir: string;\n\trelease(): void;\n}\n\nfunction toWorkRetention(options: ContextStoreRetentionOptions): WorkRetentionOptions {\n\treturn {\n\t\tmaxAgeMs: options.maxAgeMs ?? DEFAULT_MAX_AGE_MS,\n\t\tmaxRuns: options.maxSessionDirs ?? DEFAULT_MAX_SESSION_DIRS,\n\t\tmaxTotalBytes: options.maxTotalBytes ?? DEFAULT_MAX_TOTAL_BYTES,\n\t\tnow: options.now,\n\t};\n}\n\nexport function pruneContextStores(\n\tagentDir: string,\n\ttenant: \"gc\" | \"artifacts\",\n\toptions: ContextStoreRetentionOptions = {},\n): string[] {\n\treturn pruneWorkTenant(agentDir, \"context\", tenant, toWorkRetention(options));\n}\n\nexport function getContextStoreDir(agentDir: string, tenant: \"gc\" | \"artifacts\", sessionId: string): string {\n\treturn getWorkRunDir(agentDir, \"context\", tenant, sessionId);\n}\n\nexport function acquireContextStoreRetention(\n\tagentDir: string,\n\tsessionId: string,\n\toptions: ContextStoreRetentionOptions = {},\n): ContextStoreRetentionLease {\n\tconst retention = toWorkRetention(options);\n\tconst leases: WorkRunLease[] = [];\n\ttry {\n\t\tconst gc = acquireWorkRun({ agentDir, category: \"context\", tenant: \"gc\", runId: sessionId, retention });\n\t\tleases.push(gc);\n\t\tconst artifacts = acquireWorkRun({\n\t\t\tagentDir,\n\t\t\tcategory: \"context\",\n\t\t\ttenant: \"artifacts\",\n\t\t\trunId: sessionId,\n\t\t\tretention,\n\t\t});\n\t\tleases.push(artifacts);\n\t\tlet released = false;\n\t\treturn {\n\t\t\tgcDir: gc.path,\n\t\t\tartifactsDir: artifacts.path,\n\t\t\trelease(): void {\n\t\t\t\tif (released) return;\n\t\t\t\treleased = true;\n\t\t\t\tfor (const lease of leases) lease.release();\n\t\t\t},\n\t\t};\n\t} catch (error) {\n\t\tfor (const lease of leases) lease.release();\n\t\tthrow error;\n\t}\n}\n"]}
1
+ {"version":3,"file":"context-store-retention.d.ts","sourceRoot":"","sources":["../../../src/core/context/context-store-retention.ts"],"names":[],"mappings":"AAmBA,MAAM,MAAM,gBAAgB,GAAG,IAAI,GAAG,WAAW,CAAC;AAElD,MAAM,WAAW,4BAA4B;IAC5C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,GAAG,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,0BAA0B;IAC1C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,OAAO,IAAI,IAAI,CAAC;CAChB;AA0GD;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAyBzF;AAED,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,GAAE,4BAAiC,GAAG,MAAM,EAAE,CAEzG;AAED,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAEtG;AAED,wBAAgB,4BAA4B,CAC3C,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,4BAAiC,GACxC,0BAA0B,CAmB5B","sourcesContent":["import { existsSync, lstatSync, mkdirSync, readdirSync, renameSync, rmdirSync, unlinkSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport {\n\tacquireWorkRun,\n\tgetWorkRunDir,\n\tgetWorkTenantDir,\n\thasActiveWorkRunLease,\n\tpruneWorkTenant,\n\ttype WorkRetentionOptions,\n} from \"../../utils/work-directory.ts\";\n\nconst DEFAULT_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;\nconst DEFAULT_MAX_SESSION_DIRS = 64;\nconst DEFAULT_MAX_TOTAL_BYTES = 512 * 1024 * 1024;\nconst MAX_LEGACY_SESSION_DIRS = 10_000;\nconst MAX_LEGACY_FILES_PER_SESSION = 100_000;\nconst CONTEXT_WORK_TENANT = \"sessions\";\nconst WORK_RUN_MANIFEST_FILE = \".pi-work-run.json\";\n\nexport type ContextStoreKind = \"gc\" | \"artifacts\";\n\nexport interface ContextStoreRetentionOptions {\n\tmaxAgeMs?: number;\n\tmaxSessionDirs?: number;\n\tmaxTotalBytes?: number;\n\tnow?: number;\n}\n\nexport interface ContextStoreRetentionLease {\n\treadonly gcDir: string;\n\treadonly artifactsDir: string;\n\trelease(): void;\n}\n\ninterface LegacyContextStore {\n\troot: string;\n\tkind: ContextStoreKind;\n\tmanagedRun: boolean;\n}\n\nfunction toWorkRetention(options: ContextStoreRetentionOptions): WorkRetentionOptions {\n\treturn {\n\t\tmaxAgeMs: options.maxAgeMs ?? DEFAULT_MAX_AGE_MS,\n\t\tmaxRuns: options.maxSessionDirs ?? DEFAULT_MAX_SESSION_DIRS,\n\t\tmaxTotalBytes: options.maxTotalBytes ?? DEFAULT_MAX_TOTAL_BYTES,\n\t\tnow: options.now,\n\t};\n}\n\nfunction legacyContextStores(agentDir: string): LegacyContextStore[] {\n\treturn [\n\t\t{ root: join(agentDir, \"context-gc\"), kind: \"gc\", managedRun: false },\n\t\t{ root: join(agentDir, \"context-artifacts\"), kind: \"artifacts\", managedRun: false },\n\t\t{ root: getWorkTenantDir(agentDir, \"context\", \"gc\"), kind: \"gc\", managedRun: true },\n\t\t{ root: getWorkTenantDir(agentDir, \"context\", \"artifacts\"), kind: \"artifacts\", managedRun: true },\n\t];\n}\n\nfunction isRecognizedPayload(kind: ContextStoreKind, name: string): boolean {\n\tif (kind === \"gc\") return /^[0-9a-f]{1,64}\\.txt$/.test(name);\n\treturn /^[0-9a-f]{1,64}(?:\\.payload|\\.meta\\.json)$/.test(name);\n}\n\nfunction isRealDirectory(path: string): boolean {\n\ttry {\n\t\tconst stats = lstatSync(path);\n\t\treturn stats.isDirectory() && !stats.isSymbolicLink();\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nfunction recognizedPayloads(sourceDir: string, kind: ContextStoreKind): string[] | undefined {\n\ttry {\n\t\tconst entries = readdirSync(sourceDir, { withFileTypes: true });\n\t\tif (entries.length > MAX_LEGACY_FILES_PER_SESSION) return undefined;\n\t\treturn entries\n\t\t\t.filter((entry) => entry.isFile() && !entry.isSymbolicLink() && isRecognizedPayload(kind, entry.name))\n\t\t\t.map((entry) => entry.name);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction removeLegacyRunIfEmpty(sourceDir: string, managedRun: boolean): void {\n\ttry {\n\t\trmdirSync(join(sourceDir, \".leases\"));\n\t} catch {}\n\tif (managedRun) {\n\t\ttry {\n\t\t\tconst entries = readdirSync(sourceDir);\n\t\t\tif (entries.length === 1 && entries[0] === WORK_RUN_MANIFEST_FILE) {\n\t\t\t\tunlinkSync(join(sourceDir, WORK_RUN_MANIFEST_FILE));\n\t\t\t}\n\t\t} catch {}\n\t}\n\ttry {\n\t\trmdirSync(sourceDir);\n\t} catch {}\n}\n\nfunction migrateLegacySessionStore(agentDir: string, sessionId: string, store: LegacyContextStore): void {\n\tconst sourceDir = join(store.root, sessionId);\n\tif (!isRealDirectory(sourceDir)) return;\n\tif (store.managedRun && hasActiveWorkRunLease(sourceDir)) return;\n\tconst payloads = recognizedPayloads(sourceDir, store.kind);\n\tif (!payloads) return;\n\n\tif (payloads.length > 0) {\n\t\tlet lease: ReturnType<typeof acquireWorkRun> | undefined;\n\t\ttry {\n\t\t\tlease = acquireWorkRun({\n\t\t\t\tagentDir,\n\t\t\t\tcategory: \"context\",\n\t\t\t\ttenant: CONTEXT_WORK_TENANT,\n\t\t\t\trunId: sessionId,\n\t\t\t\tretention: false,\n\t\t\t});\n\t\t\tconst targetDir = join(lease.path, store.kind);\n\t\t\tmkdirSync(targetDir, { recursive: true });\n\t\t\tfor (const name of payloads) {\n\t\t\t\tconst sourcePath = join(sourceDir, name);\n\t\t\t\tconst targetPath = join(targetDir, name);\n\t\t\t\tif (existsSync(targetPath)) continue;\n\t\t\t\ttry {\n\t\t\t\t\trenameSync(sourcePath, targetPath);\n\t\t\t\t} catch {}\n\t\t\t}\n\t\t} catch {\n\t\t\treturn;\n\t\t} finally {\n\t\t\tlease?.release();\n\t\t}\n\t}\n\n\tremoveLegacyRunIfEmpty(sourceDir, store.managedRun);\n}\n\n/**\n * Move recognized context payloads from both historical layouts into the single leased session\n * namespace. Unknown files, symlinks, active runs, and conflicting destinations are left untouched.\n */\nexport function migrateLegacyContextStores(agentDir: string, onlySessionId?: string): void {\n\tfor (const store of legacyContextStores(agentDir)) {\n\t\tlet sessionIds: string[];\n\t\tif (onlySessionId) {\n\t\t\tsessionIds = [onlySessionId];\n\t\t} else {\n\t\t\ttry {\n\t\t\t\tsessionIds = readdirSync(store.root, { withFileTypes: true })\n\t\t\t\t\t.slice(0, MAX_LEGACY_SESSION_DIRS)\n\t\t\t\t\t.filter((entry) => entry.isDirectory() && !entry.isSymbolicLink())\n\t\t\t\t\t.map((entry) => entry.name);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\tfor (const sessionId of sessionIds) {\n\t\t\tmigrateLegacySessionStore(agentDir, sessionId, store);\n\t\t}\n\t\ttry {\n\t\t\trmdirSync(store.root);\n\t\t} catch {}\n\t}\n\ttry {\n\t\trmdirSync(join(agentDir, \"work\", \"context\"));\n\t} catch {}\n}\n\nexport function pruneContextStores(agentDir: string, options: ContextStoreRetentionOptions = {}): string[] {\n\treturn pruneWorkTenant(agentDir, \"context\", CONTEXT_WORK_TENANT, toWorkRetention(options));\n}\n\nexport function getContextStoreDir(agentDir: string, kind: ContextStoreKind, sessionId: string): string {\n\treturn join(getWorkRunDir(agentDir, \"context\", CONTEXT_WORK_TENANT, sessionId), kind);\n}\n\nexport function acquireContextStoreRetention(\n\tagentDir: string,\n\tsessionId: string,\n\toptions: ContextStoreRetentionOptions = {},\n): ContextStoreRetentionLease {\n\tmigrateLegacyContextStores(agentDir, sessionId);\n\tconst lease = acquireWorkRun({\n\t\tagentDir,\n\t\tcategory: \"context\",\n\t\ttenant: CONTEXT_WORK_TENANT,\n\t\trunId: sessionId,\n\t\tretention: toWorkRetention(options),\n\t});\n\tlet released = false;\n\treturn {\n\t\tgcDir: join(lease.path, \"gc\"),\n\t\tartifactsDir: join(lease.path, \"artifacts\"),\n\t\trelease(): void {\n\t\t\tif (released) return;\n\t\t\treleased = true;\n\t\t\tlease.release();\n\t\t},\n\t};\n}\n"]}
@@ -1,7 +1,13 @@
1
- import { acquireWorkRun, getWorkRunDir, pruneWorkTenant, } from "../../utils/work-directory.js";
1
+ import { existsSync, lstatSync, mkdirSync, readdirSync, renameSync, rmdirSync, unlinkSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { acquireWorkRun, getWorkRunDir, getWorkTenantDir, hasActiveWorkRunLease, pruneWorkTenant, } from "../../utils/work-directory.js";
2
4
  const DEFAULT_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
3
5
  const DEFAULT_MAX_SESSION_DIRS = 64;
4
6
  const DEFAULT_MAX_TOTAL_BYTES = 512 * 1024 * 1024;
7
+ const MAX_LEGACY_SESSION_DIRS = 10_000;
8
+ const MAX_LEGACY_FILES_PER_SESSION = 100_000;
9
+ const CONTEXT_WORK_TENANT = "sessions";
10
+ const WORK_RUN_MANIFEST_FILE = ".pi-work-run.json";
5
11
  function toWorkRetention(options) {
6
12
  return {
7
13
  maxAgeMs: options.maxAgeMs ?? DEFAULT_MAX_AGE_MS,
@@ -10,43 +16,160 @@ function toWorkRetention(options) {
10
16
  now: options.now,
11
17
  };
12
18
  }
13
- export function pruneContextStores(agentDir, tenant, options = {}) {
14
- return pruneWorkTenant(agentDir, "context", tenant, toWorkRetention(options));
19
+ function legacyContextStores(agentDir) {
20
+ return [
21
+ { root: join(agentDir, "context-gc"), kind: "gc", managedRun: false },
22
+ { root: join(agentDir, "context-artifacts"), kind: "artifacts", managedRun: false },
23
+ { root: getWorkTenantDir(agentDir, "context", "gc"), kind: "gc", managedRun: true },
24
+ { root: getWorkTenantDir(agentDir, "context", "artifacts"), kind: "artifacts", managedRun: true },
25
+ ];
15
26
  }
16
- export function getContextStoreDir(agentDir, tenant, sessionId) {
17
- return getWorkRunDir(agentDir, "context", tenant, sessionId);
27
+ function isRecognizedPayload(kind, name) {
28
+ if (kind === "gc")
29
+ return /^[0-9a-f]{1,64}\.txt$/.test(name);
30
+ return /^[0-9a-f]{1,64}(?:\.payload|\.meta\.json)$/.test(name);
18
31
  }
19
- export function acquireContextStoreRetention(agentDir, sessionId, options = {}) {
20
- const retention = toWorkRetention(options);
21
- const leases = [];
32
+ function isRealDirectory(path) {
22
33
  try {
23
- const gc = acquireWorkRun({ agentDir, category: "context", tenant: "gc", runId: sessionId, retention });
24
- leases.push(gc);
25
- const artifacts = acquireWorkRun({
26
- agentDir,
27
- category: "context",
28
- tenant: "artifacts",
29
- runId: sessionId,
30
- retention,
31
- });
32
- leases.push(artifacts);
33
- let released = false;
34
- return {
35
- gcDir: gc.path,
36
- artifactsDir: artifacts.path,
37
- release() {
38
- if (released)
39
- return;
40
- released = true;
41
- for (const lease of leases)
42
- lease.release();
43
- },
44
- };
45
- }
46
- catch (error) {
47
- for (const lease of leases)
48
- lease.release();
49
- throw error;
34
+ const stats = lstatSync(path);
35
+ return stats.isDirectory() && !stats.isSymbolicLink();
36
+ }
37
+ catch {
38
+ return false;
39
+ }
40
+ }
41
+ function recognizedPayloads(sourceDir, kind) {
42
+ try {
43
+ const entries = readdirSync(sourceDir, { withFileTypes: true });
44
+ if (entries.length > MAX_LEGACY_FILES_PER_SESSION)
45
+ return undefined;
46
+ return entries
47
+ .filter((entry) => entry.isFile() && !entry.isSymbolicLink() && isRecognizedPayload(kind, entry.name))
48
+ .map((entry) => entry.name);
49
+ }
50
+ catch {
51
+ return undefined;
52
+ }
53
+ }
54
+ function removeLegacyRunIfEmpty(sourceDir, managedRun) {
55
+ try {
56
+ rmdirSync(join(sourceDir, ".leases"));
57
+ }
58
+ catch { }
59
+ if (managedRun) {
60
+ try {
61
+ const entries = readdirSync(sourceDir);
62
+ if (entries.length === 1 && entries[0] === WORK_RUN_MANIFEST_FILE) {
63
+ unlinkSync(join(sourceDir, WORK_RUN_MANIFEST_FILE));
64
+ }
65
+ }
66
+ catch { }
67
+ }
68
+ try {
69
+ rmdirSync(sourceDir);
50
70
  }
71
+ catch { }
72
+ }
73
+ function migrateLegacySessionStore(agentDir, sessionId, store) {
74
+ const sourceDir = join(store.root, sessionId);
75
+ if (!isRealDirectory(sourceDir))
76
+ return;
77
+ if (store.managedRun && hasActiveWorkRunLease(sourceDir))
78
+ return;
79
+ const payloads = recognizedPayloads(sourceDir, store.kind);
80
+ if (!payloads)
81
+ return;
82
+ if (payloads.length > 0) {
83
+ let lease;
84
+ try {
85
+ lease = acquireWorkRun({
86
+ agentDir,
87
+ category: "context",
88
+ tenant: CONTEXT_WORK_TENANT,
89
+ runId: sessionId,
90
+ retention: false,
91
+ });
92
+ const targetDir = join(lease.path, store.kind);
93
+ mkdirSync(targetDir, { recursive: true });
94
+ for (const name of payloads) {
95
+ const sourcePath = join(sourceDir, name);
96
+ const targetPath = join(targetDir, name);
97
+ if (existsSync(targetPath))
98
+ continue;
99
+ try {
100
+ renameSync(sourcePath, targetPath);
101
+ }
102
+ catch { }
103
+ }
104
+ }
105
+ catch {
106
+ return;
107
+ }
108
+ finally {
109
+ lease?.release();
110
+ }
111
+ }
112
+ removeLegacyRunIfEmpty(sourceDir, store.managedRun);
113
+ }
114
+ /**
115
+ * Move recognized context payloads from both historical layouts into the single leased session
116
+ * namespace. Unknown files, symlinks, active runs, and conflicting destinations are left untouched.
117
+ */
118
+ export function migrateLegacyContextStores(agentDir, onlySessionId) {
119
+ for (const store of legacyContextStores(agentDir)) {
120
+ let sessionIds;
121
+ if (onlySessionId) {
122
+ sessionIds = [onlySessionId];
123
+ }
124
+ else {
125
+ try {
126
+ sessionIds = readdirSync(store.root, { withFileTypes: true })
127
+ .slice(0, MAX_LEGACY_SESSION_DIRS)
128
+ .filter((entry) => entry.isDirectory() && !entry.isSymbolicLink())
129
+ .map((entry) => entry.name);
130
+ }
131
+ catch {
132
+ continue;
133
+ }
134
+ }
135
+ for (const sessionId of sessionIds) {
136
+ migrateLegacySessionStore(agentDir, sessionId, store);
137
+ }
138
+ try {
139
+ rmdirSync(store.root);
140
+ }
141
+ catch { }
142
+ }
143
+ try {
144
+ rmdirSync(join(agentDir, "work", "context"));
145
+ }
146
+ catch { }
147
+ }
148
+ export function pruneContextStores(agentDir, options = {}) {
149
+ return pruneWorkTenant(agentDir, "context", CONTEXT_WORK_TENANT, toWorkRetention(options));
150
+ }
151
+ export function getContextStoreDir(agentDir, kind, sessionId) {
152
+ return join(getWorkRunDir(agentDir, "context", CONTEXT_WORK_TENANT, sessionId), kind);
153
+ }
154
+ export function acquireContextStoreRetention(agentDir, sessionId, options = {}) {
155
+ migrateLegacyContextStores(agentDir, sessionId);
156
+ const lease = acquireWorkRun({
157
+ agentDir,
158
+ category: "context",
159
+ tenant: CONTEXT_WORK_TENANT,
160
+ runId: sessionId,
161
+ retention: toWorkRetention(options),
162
+ });
163
+ let released = false;
164
+ return {
165
+ gcDir: join(lease.path, "gc"),
166
+ artifactsDir: join(lease.path, "artifacts"),
167
+ release() {
168
+ if (released)
169
+ return;
170
+ released = true;
171
+ lease.release();
172
+ },
173
+ };
51
174
  }
52
175
  //# sourceMappingURL=context-store-retention.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"context-store-retention.js","sourceRoot":"","sources":["../../../src/core/context/context-store-retention.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EACd,aAAa,EACb,eAAe,GAGf,MAAM,+BAA+B,CAAC;AAEvC,MAAM,kBAAkB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACpD,MAAM,wBAAwB,GAAG,EAAE,CAAC;AACpC,MAAM,uBAAuB,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AAelD,SAAS,eAAe,CAAC,OAAqC,EAAwB;IACrF,OAAO;QACN,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,kBAAkB;QAChD,OAAO,EAAE,OAAO,CAAC,cAAc,IAAI,wBAAwB;QAC3D,aAAa,EAAE,OAAO,CAAC,aAAa,IAAI,uBAAuB;QAC/D,GAAG,EAAE,OAAO,CAAC,GAAG;KAChB,CAAC;AAAA,CACF;AAED,MAAM,UAAU,kBAAkB,CACjC,QAAgB,EAChB,MAA0B,EAC1B,OAAO,GAAiC,EA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,MAAM,KAAK,IAAI,MAAM;YAAE,KAAK,CAAC,OAAO,EAAE,CAAC;QAC5C,MAAM,KAAK,CAAC;IACb,CAAC;AAAA,CACD","sourcesContent":["import {\n\tacquireWorkRun,\n\tgetWorkRunDir,\n\tpruneWorkTenant,\n\ttype WorkRetentionOptions,\n\ttype WorkRunLease,\n} from \"../../utils/work-directory.ts\";\n\nconst DEFAULT_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;\nconst DEFAULT_MAX_SESSION_DIRS = 64;\nconst DEFAULT_MAX_TOTAL_BYTES = 512 * 1024 * 1024;\n\nexport interface ContextStoreRetentionOptions {\n\tmaxAgeMs?: number;\n\tmaxSessionDirs?: number;\n\tmaxTotalBytes?: number;\n\tnow?: number;\n}\n\nexport interface ContextStoreRetentionLease {\n\treadonly gcDir: string;\n\treadonly artifactsDir: string;\n\trelease(): void;\n}\n\nfunction toWorkRetention(options: ContextStoreRetentionOptions): WorkRetentionOptions {\n\treturn {\n\t\tmaxAgeMs: options.maxAgeMs ?? DEFAULT_MAX_AGE_MS,\n\t\tmaxRuns: options.maxSessionDirs ?? DEFAULT_MAX_SESSION_DIRS,\n\t\tmaxTotalBytes: options.maxTotalBytes ?? DEFAULT_MAX_TOTAL_BYTES,\n\t\tnow: options.now,\n\t};\n}\n\nexport function pruneContextStores(\n\tagentDir: string,\n\ttenant: \"gc\" | \"artifacts\",\n\toptions: ContextStoreRetentionOptions = {},\n): string[] {\n\treturn pruneWorkTenant(agentDir, \"context\", tenant, toWorkRetention(options));\n}\n\nexport function getContextStoreDir(agentDir: string, tenant: \"gc\" | \"artifacts\", sessionId: string): string {\n\treturn getWorkRunDir(agentDir, \"context\", tenant, sessionId);\n}\n\nexport function acquireContextStoreRetention(\n\tagentDir: string,\n\tsessionId: string,\n\toptions: ContextStoreRetentionOptions = {},\n): ContextStoreRetentionLease {\n\tconst retention = toWorkRetention(options);\n\tconst leases: WorkRunLease[] = [];\n\ttry {\n\t\tconst gc = acquireWorkRun({ agentDir, category: \"context\", tenant: \"gc\", runId: sessionId, retention });\n\t\tleases.push(gc);\n\t\tconst artifacts = acquireWorkRun({\n\t\t\tagentDir,\n\t\t\tcategory: \"context\",\n\t\t\ttenant: \"artifacts\",\n\t\t\trunId: sessionId,\n\t\t\tretention,\n\t\t});\n\t\tleases.push(artifacts);\n\t\tlet released = false;\n\t\treturn {\n\t\t\tgcDir: gc.path,\n\t\t\tartifactsDir: artifacts.path,\n\t\t\trelease(): void {\n\t\t\t\tif (released) return;\n\t\t\t\treleased = true;\n\t\t\t\tfor (const lease of leases) lease.release();\n\t\t\t},\n\t\t};\n\t} catch (error) {\n\t\tfor (const lease of leases) lease.release();\n\t\tthrow error;\n\t}\n}\n"]}
1
+ {"version":3,"file":"context-store-retention.js","sourceRoot":"","sources":["../../../src/core/context/context-store-retention.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC3G,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EACN,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,qBAAqB,EACrB,eAAe,GAEf,MAAM,+BAA+B,CAAC;AAEvC,MAAM,kBAAkB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACpD,MAAM,wBAAwB,GAAG,EAAE,CAAC;AACpC,MAAM,uBAAuB,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AAClD,MAAM,uBAAuB,GAAG,MAAM,CAAC;AACvC,MAAM,4BAA4B,GAAG,OAAO,CAAC;AAC7C,MAAM,mBAAmB,GAAG,UAAU,CAAC;AACvC,MAAM,sBAAsB,GAAG,mBAAmB,CAAC;AAuBnD,SAAS,eAAe,CAAC,OAAqC,EAAwB;IACrF,OAAO;QACN,QAAQ,EAAE,OAAO,CAAC,QAAQ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EAAY;IAC1G,OAAO,eAAe,CAAC,QAAQ,EAAE,SAAS,EAAE,mBAAmB,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC;AAAA,CAC3F;AAED,MAAM,UAAU,kBAAkB,CAAC,QAAgB,EAAE,IAAsB,EAAE,SAAiB,EAAU;IACvG,OAAO,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE,SAAS,EAAE,mBAAmB,EAAE,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC;AAAA,CACtF;AAED,MAAM,UAAU,4BAA4B,CAC3C,QAAgB,EAChB,SAAiB,EACjB,OAAO,GAAiC,EAAE,EACb;IAC7B,0BAA0B,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;IAChD,MAAM,KAAK,GAAG,cAAc,CAAC;QAC5B,QAAQ;QACR,QAAQ,EAAE,SAAS;QACnB,MAAM,EAAE,mBAAmB;QAC3B,KAAK,EAAE,SAAS;QAChB,SAAS,EAAE,eAAe,CAAC,OAAO,CAAC;KACnC,CAAC,CAAC;IACH,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,OAAO;QACN,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC;QAC7B,YAAY,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,WAAW,CAAC;QAC3C,OAAO,GAAS;YACf,IAAI,QAAQ;gBAAE,OAAO;YACrB,QAAQ,GAAG,IAAI,CAAC;YAChB,KAAK,CAAC,OAAO,EAAE,CAAC;QAAA,CAChB;KACD,CAAC;AAAA,CACF","sourcesContent":["import { existsSync, lstatSync, mkdirSync, readdirSync, renameSync, rmdirSync, unlinkSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport {\n\tacquireWorkRun,\n\tgetWorkRunDir,\n\tgetWorkTenantDir,\n\thasActiveWorkRunLease,\n\tpruneWorkTenant,\n\ttype WorkRetentionOptions,\n} from \"../../utils/work-directory.ts\";\n\nconst DEFAULT_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;\nconst DEFAULT_MAX_SESSION_DIRS = 64;\nconst DEFAULT_MAX_TOTAL_BYTES = 512 * 1024 * 1024;\nconst MAX_LEGACY_SESSION_DIRS = 10_000;\nconst MAX_LEGACY_FILES_PER_SESSION = 100_000;\nconst CONTEXT_WORK_TENANT = \"sessions\";\nconst WORK_RUN_MANIFEST_FILE = \".pi-work-run.json\";\n\nexport type ContextStoreKind = \"gc\" | \"artifacts\";\n\nexport interface ContextStoreRetentionOptions {\n\tmaxAgeMs?: number;\n\tmaxSessionDirs?: number;\n\tmaxTotalBytes?: number;\n\tnow?: number;\n}\n\nexport interface ContextStoreRetentionLease {\n\treadonly gcDir: string;\n\treadonly artifactsDir: string;\n\trelease(): void;\n}\n\ninterface LegacyContextStore {\n\troot: string;\n\tkind: ContextStoreKind;\n\tmanagedRun: boolean;\n}\n\nfunction toWorkRetention(options: ContextStoreRetentionOptions): WorkRetentionOptions {\n\treturn {\n\t\tmaxAgeMs: options.maxAgeMs ?? DEFAULT_MAX_AGE_MS,\n\t\tmaxRuns: options.maxSessionDirs ?? DEFAULT_MAX_SESSION_DIRS,\n\t\tmaxTotalBytes: options.maxTotalBytes ?? DEFAULT_MAX_TOTAL_BYTES,\n\t\tnow: options.now,\n\t};\n}\n\nfunction legacyContextStores(agentDir: string): LegacyContextStore[] {\n\treturn [\n\t\t{ root: join(agentDir, \"context-gc\"), kind: \"gc\", managedRun: false },\n\t\t{ root: join(agentDir, \"context-artifacts\"), kind: \"artifacts\", managedRun: false },\n\t\t{ root: getWorkTenantDir(agentDir, \"context\", \"gc\"), kind: \"gc\", managedRun: true },\n\t\t{ root: getWorkTenantDir(agentDir, \"context\", \"artifacts\"), kind: \"artifacts\", managedRun: true },\n\t];\n}\n\nfunction isRecognizedPayload(kind: ContextStoreKind, name: string): boolean {\n\tif (kind === \"gc\") return /^[0-9a-f]{1,64}\\.txt$/.test(name);\n\treturn /^[0-9a-f]{1,64}(?:\\.payload|\\.meta\\.json)$/.test(name);\n}\n\nfunction isRealDirectory(path: string): boolean {\n\ttry {\n\t\tconst stats = lstatSync(path);\n\t\treturn stats.isDirectory() && !stats.isSymbolicLink();\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nfunction recognizedPayloads(sourceDir: string, kind: ContextStoreKind): string[] | undefined {\n\ttry {\n\t\tconst entries = readdirSync(sourceDir, { withFileTypes: true });\n\t\tif (entries.length > MAX_LEGACY_FILES_PER_SESSION) return undefined;\n\t\treturn entries\n\t\t\t.filter((entry) => entry.isFile() && !entry.isSymbolicLink() && isRecognizedPayload(kind, entry.name))\n\t\t\t.map((entry) => entry.name);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction removeLegacyRunIfEmpty(sourceDir: string, managedRun: boolean): void {\n\ttry {\n\t\trmdirSync(join(sourceDir, \".leases\"));\n\t} catch {}\n\tif (managedRun) {\n\t\ttry {\n\t\t\tconst entries = readdirSync(sourceDir);\n\t\t\tif (entries.length === 1 && entries[0] === WORK_RUN_MANIFEST_FILE) {\n\t\t\t\tunlinkSync(join(sourceDir, WORK_RUN_MANIFEST_FILE));\n\t\t\t}\n\t\t} catch {}\n\t}\n\ttry {\n\t\trmdirSync(sourceDir);\n\t} catch {}\n}\n\nfunction migrateLegacySessionStore(agentDir: string, sessionId: string, store: LegacyContextStore): void {\n\tconst sourceDir = join(store.root, sessionId);\n\tif (!isRealDirectory(sourceDir)) return;\n\tif (store.managedRun && hasActiveWorkRunLease(sourceDir)) return;\n\tconst payloads = recognizedPayloads(sourceDir, store.kind);\n\tif (!payloads) return;\n\n\tif (payloads.length > 0) {\n\t\tlet lease: ReturnType<typeof acquireWorkRun> | undefined;\n\t\ttry {\n\t\t\tlease = acquireWorkRun({\n\t\t\t\tagentDir,\n\t\t\t\tcategory: \"context\",\n\t\t\t\ttenant: CONTEXT_WORK_TENANT,\n\t\t\t\trunId: sessionId,\n\t\t\t\tretention: false,\n\t\t\t});\n\t\t\tconst targetDir = join(lease.path, store.kind);\n\t\t\tmkdirSync(targetDir, { recursive: true });\n\t\t\tfor (const name of payloads) {\n\t\t\t\tconst sourcePath = join(sourceDir, name);\n\t\t\t\tconst targetPath = join(targetDir, name);\n\t\t\t\tif (existsSync(targetPath)) continue;\n\t\t\t\ttry {\n\t\t\t\t\trenameSync(sourcePath, targetPath);\n\t\t\t\t} catch {}\n\t\t\t}\n\t\t} catch {\n\t\t\treturn;\n\t\t} finally {\n\t\t\tlease?.release();\n\t\t}\n\t}\n\n\tremoveLegacyRunIfEmpty(sourceDir, store.managedRun);\n}\n\n/**\n * Move recognized context payloads from both historical layouts into the single leased session\n * namespace. Unknown files, symlinks, active runs, and conflicting destinations are left untouched.\n */\nexport function migrateLegacyContextStores(agentDir: string, onlySessionId?: string): void {\n\tfor (const store of legacyContextStores(agentDir)) {\n\t\tlet sessionIds: string[];\n\t\tif (onlySessionId) {\n\t\t\tsessionIds = [onlySessionId];\n\t\t} else {\n\t\t\ttry {\n\t\t\t\tsessionIds = readdirSync(store.root, { withFileTypes: true })\n\t\t\t\t\t.slice(0, MAX_LEGACY_SESSION_DIRS)\n\t\t\t\t\t.filter((entry) => entry.isDirectory() && !entry.isSymbolicLink())\n\t\t\t\t\t.map((entry) => entry.name);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\tfor (const sessionId of sessionIds) {\n\t\t\tmigrateLegacySessionStore(agentDir, sessionId, store);\n\t\t}\n\t\ttry {\n\t\t\trmdirSync(store.root);\n\t\t} catch {}\n\t}\n\ttry {\n\t\trmdirSync(join(agentDir, \"work\", \"context\"));\n\t} catch {}\n}\n\nexport function pruneContextStores(agentDir: string, options: ContextStoreRetentionOptions = {}): string[] {\n\treturn pruneWorkTenant(agentDir, \"context\", CONTEXT_WORK_TENANT, toWorkRetention(options));\n}\n\nexport function getContextStoreDir(agentDir: string, kind: ContextStoreKind, sessionId: string): string {\n\treturn join(getWorkRunDir(agentDir, \"context\", CONTEXT_WORK_TENANT, sessionId), kind);\n}\n\nexport function acquireContextStoreRetention(\n\tagentDir: string,\n\tsessionId: string,\n\toptions: ContextStoreRetentionOptions = {},\n): ContextStoreRetentionLease {\n\tmigrateLegacyContextStores(agentDir, sessionId);\n\tconst lease = acquireWorkRun({\n\t\tagentDir,\n\t\tcategory: \"context\",\n\t\ttenant: CONTEXT_WORK_TENANT,\n\t\trunId: sessionId,\n\t\tretention: toWorkRetention(options),\n\t});\n\tlet released = false;\n\treturn {\n\t\tgcDir: join(lease.path, \"gc\"),\n\t\tartifactsDir: join(lease.path, \"artifacts\"),\n\t\trelease(): void {\n\t\t\tif (released) return;\n\t\t\treleased = true;\n\t\t\tlease.release();\n\t\t},\n\t};\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"okf-memory.d.ts","sourceRoot":"","sources":["../../../src/core/context/okf-memory.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAEX,UAAU,EAGV,kBAAkB,EAClB,MAAM,+BAA+B,CAAC;AAEvC,eAAO,MAAM,kBAAkB,WAAW,CAAC;AAE3C,eAAO,MAAM,gBAAgB,mBAAmB,CAAC;AAEjD,eAAO,MAAM,YAAY,qOAWf,CAAC;AAEX,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AAEtD,MAAM,MAAM,uBAAuB,GAChC,qBAAqB,GACrB,qBAAqB,GACrB,cAAc,GACd,cAAc,GACd,cAAc,GACd,eAAe,GACf,qBAAqB,GACrB,YAAY,GACZ,eAAe,GACf,mBAAmB,GACnB,uBAAuB,CAAC;AAE3B,MAAM,WAAW,mBAAmB;IACnC,IAAI,EAAE,uBAAuB,CAAC;IAC9B,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,qBAAqB;IACrC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,uBAAuB;IACvC,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,mBAAmB,EAAE,CAAC;CACnC;AAED,MAAM,WAAW,sBAAsB;IACtC,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,WAAW,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB;AAED,MAAM,WAAW,8BAA8B;IAC9C,IAAI,EAAE,UAAU,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,MAAM,gCAAgC,GACzC,4BAA4B,GAC5B,sCAAsC,GACtC,6BAA6B,CAAC;AAkEjC,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,GAAG,uBAAuB,CA6EpH;AAED,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,sBAAsB,GAAG,MAAM,CAc7E;AAED,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,kBAAkB,CAYhG;AAED,wBAAgB,+BAA+B,CAC9C,OAAO,EAAE,8BAA8B,GACrC,gCAAgC,EAAE,CAQpC","sourcesContent":["import { createHash } from \"node:crypto\";\nimport { stringify } from \"yaml\";\nimport { parseFrontmatter } from \"../../utils/frontmatter.ts\";\nimport type { MemoryScope } from \"./context-item.ts\";\nimport type {\n\tMemoryEvidenceRef,\n\tMemoryItem,\n\tMemoryItemKind,\n\tMemoryRef,\n\tMemoryWriteRequest,\n} from \"./memory-provider-contract.ts\";\n\nexport const PI_OKF_PROVIDER_ID = \"pi-okf\";\n\nexport const PI_OKF_AUTHORITY = \"durable_memory\";\n\nexport const PI_OKF_TYPES = [\n\t\"Design Decision\",\n\t\"Architecture Concept\",\n\t\"Project Rule Candidate\",\n\t\"Implementation Note\",\n\t\"Debugging Finding\",\n\t\"Invalidated Assumption\",\n\t\"Tooling Playbook\",\n\t\"External Reference\",\n\t\"User Preference\",\n\t\"Capability Doc\",\n] as const;\n\nexport type PiOkfType = (typeof PI_OKF_TYPES)[number];\n\nexport type OkfMemoryDiagnosticCode =\n\t| \"missing_frontmatter\"\n\t| \"invalid_frontmatter\"\n\t| \"invalid_yaml\"\n\t| \"missing_type\"\n\t| \"unknown_type\"\n\t| \"missing_title\"\n\t| \"missing_description\"\n\t| \"missing_pi\"\n\t| \"invalid_scope\"\n\t| \"invalid_authority\"\n\t| \"invalid_evidence_refs\";\n\nexport interface OkfMemoryDiagnostic {\n\tcode: OkfMemoryDiagnosticCode;\n\tmessage: string;\n}\n\nexport interface ParseOkfMemoryOptions {\n\tproviderId?: string;\n\turi?: string;\n\tfallbackId?: string;\n}\n\nexport interface ParsedOkfMemoryDocument {\n\titem?: MemoryItem;\n\tbody: string;\n\tdiagnostics: OkfMemoryDiagnostic[];\n}\n\nexport interface OkfMemoryDocumentInput {\n\ttype: PiOkfType;\n\ttitle: string;\n\tdescription: string;\n\tscope: MemoryScope;\n\tbody: string;\n\ttags?: string[];\n\ttimestamp?: string;\n\tevidenceRefs?: string[];\n}\n\nexport interface OkfProjectRulePromotionRequest {\n\titem: MemoryItem;\n\tapprovalId?: string;\n\ttrustedConfigPath?: string;\n}\n\nexport type OkfProjectRulePromotionRejection =\n\t| \"not_project_rule_candidate\"\n\t| \"missing_explicit_promotion_authority\"\n\t| \"stale_or_conflicting_memory\";\n\nconst OKF_TYPE_TO_MEMORY_KIND = new Map<PiOkfType, MemoryItemKind>([\n\t[\"Design Decision\", \"design_decision\"],\n\t[\"Architecture Concept\", \"architecture_concept\"],\n\t[\"Project Rule Candidate\", \"project_rule_candidate\"],\n\t[\"Implementation Note\", \"fact\"],\n\t[\"Debugging Finding\", \"debugging_finding\"],\n\t[\"Invalidated Assumption\", \"invalidated_assumption\"],\n\t[\"Tooling Playbook\", \"procedure\"],\n\t[\"External Reference\", \"reference\"],\n\t[\"User Preference\", \"user_preference\"],\n\t[\"Capability Doc\", \"reference\"],\n]);\n\nconst VALID_SCOPES: readonly MemoryScope[] = [\"session\", \"project\", \"user\", \"global\"];\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction stringField(record: Record<string, unknown>, key: string): string | undefined {\n\tconst value = record[key];\n\treturn typeof value === \"string\" && value.trim().length > 0 ? value : undefined;\n}\n\nfunction stringArrayField(record: Record<string, unknown>, key: string): string[] | undefined {\n\tconst value = record[key];\n\tif (value === undefined) return undefined;\n\treturn Array.isArray(value) && value.every((entry) => typeof entry === \"string\") ? value : undefined;\n}\n\nfunction memoryItemKindForOkfType(type: string): MemoryItemKind | undefined {\n\treturn OKF_TYPE_TO_MEMORY_KIND.get(type as PiOkfType);\n}\n\nfunction memoryIdForDocument(\n\tproviderId: string,\n\turi: string | undefined,\n\ttitle: string,\n\ttimestamp: string | undefined,\n): string {\n\treturn createHash(\"sha256\")\n\t\t.update([providerId, uri ?? \"\", title, timestamp ?? \"\"].join(\"\\0\"))\n\t\t.digest(\"hex\")\n\t\t.slice(0, 24);\n}\n\nfunction evidenceRefsFromOkf(providerId: string, refs: string[] | undefined): MemoryEvidenceRef[] {\n\treturn (refs ?? []).map((ref) => ({ type: \"external\", id: ref, providerId, description: \"OKF evidence_ref\" }));\n}\n\nfunction buildMemoryRef(\n\tproviderId: string,\n\tid: string,\n\tscope: MemoryScope,\n\tkind: MemoryItemKind,\n\turi?: string,\n): MemoryRef {\n\treturn { providerId, itemId: id, scope, kind, uri };\n}\n\nfunction validScope(value: string | undefined): MemoryScope | undefined {\n\treturn value !== undefined && VALID_SCOPES.includes(value as MemoryScope) ? (value as MemoryScope) : undefined;\n}\n\nexport function parseOkfMemoryDocument(content: string, options: ParseOkfMemoryOptions = {}): ParsedOkfMemoryDocument {\n\tlet frontmatter: Record<string, unknown>;\n\tlet body: string;\n\ttry {\n\t\tconst parsed = parseFrontmatter(content);\n\t\tfrontmatter = parsed.frontmatter;\n\t\tbody = parsed.body;\n\t} catch (error) {\n\t\treturn {\n\t\t\tbody: content,\n\t\t\tdiagnostics: [{ code: \"invalid_yaml\", message: error instanceof Error ? error.message : String(error) }],\n\t\t};\n\t}\n\n\tconst diagnostics: OkfMemoryDiagnostic[] = [];\n\tif (!isRecord(frontmatter) || Object.keys(frontmatter).length === 0) {\n\t\treturn { body, diagnostics: [{ code: \"missing_frontmatter\", message: \"OKF memory requires YAML frontmatter.\" }] };\n\t}\n\n\tconst type = stringField(frontmatter, \"type\");\n\tif (type === undefined) diagnostics.push({ code: \"missing_type\", message: \"OKF memory requires a type.\" });\n\tconst kind = type === undefined ? undefined : memoryItemKindForOkfType(type);\n\tif (type !== undefined && kind === undefined)\n\t\tdiagnostics.push({ code: \"unknown_type\", message: `Unknown Pi OKF type: ${type}` });\n\n\tconst title = stringField(frontmatter, \"title\");\n\tif (title === undefined) diagnostics.push({ code: \"missing_title\", message: \"OKF memory requires a title.\" });\n\tconst description = stringField(frontmatter, \"description\");\n\tif (description === undefined)\n\t\tdiagnostics.push({ code: \"missing_description\", message: \"OKF memory requires a description.\" });\n\n\tconst pi = frontmatter.pi;\n\tif (!isRecord(pi)) diagnostics.push({ code: \"missing_pi\", message: \"OKF memory requires a pi block.\" });\n\tconst scope = isRecord(pi) ? validScope(stringField(pi, \"scope\")) : undefined;\n\tif (isRecord(pi) && scope === undefined)\n\t\tdiagnostics.push({ code: \"invalid_scope\", message: \"pi.scope must be session, project, user, or global.\" });\n\tconst authority = isRecord(pi) ? stringField(pi, \"authority\") : undefined;\n\tif (isRecord(pi) && authority !== PI_OKF_AUTHORITY) {\n\t\tdiagnostics.push({ code: \"invalid_authority\", message: `pi.authority must be ${PI_OKF_AUTHORITY}.` });\n\t}\n\tconst evidenceRefs = isRecord(pi) ? stringArrayField(pi, \"evidence_refs\") : undefined;\n\tif (isRecord(pi) && pi.evidence_refs !== undefined && evidenceRefs === undefined) {\n\t\tdiagnostics.push({ code: \"invalid_evidence_refs\", message: \"pi.evidence_refs must be a string array.\" });\n\t}\n\n\tif (\n\t\tdiagnostics.length > 0 ||\n\t\tkind === undefined ||\n\t\ttitle === undefined ||\n\t\tdescription === undefined ||\n\t\tscope === undefined\n\t) {\n\t\treturn { body, diagnostics };\n\t}\n\n\tconst providerId = options.providerId ?? PI_OKF_PROVIDER_ID;\n\tconst timestamp = stringField(frontmatter, \"timestamp\");\n\tconst id = options.fallbackId ?? memoryIdForDocument(providerId, options.uri, title, timestamp);\n\tconst ref = buildMemoryRef(providerId, id, scope, kind, options.uri);\n\treturn {\n\t\tbody,\n\t\tdiagnostics,\n\t\titem: {\n\t\t\tid,\n\t\t\tproviderId,\n\t\t\tsource: \"pi_native\",\n\t\t\tkind,\n\t\t\tscope,\n\t\t\tdurability: \"durable\",\n\t\t\ttitle,\n\t\t\tsummary: description,\n\t\t\tcontent: body.length > 0 ? body : undefined,\n\t\t\trefs: [ref],\n\t\t\tevidenceRefs: evidenceRefsFromOkf(providerId, evidenceRefs),\n\t\t\ttimestamp,\n\t\t},\n\t};\n}\n\nexport function formatOkfMemoryDocument(input: OkfMemoryDocumentInput): string {\n\tconst frontmatter = {\n\t\ttype: input.type,\n\t\ttitle: input.title,\n\t\tdescription: input.description,\n\t\ttags: input.tags,\n\t\ttimestamp: input.timestamp,\n\t\tpi: {\n\t\t\tscope: input.scope,\n\t\t\tauthority: PI_OKF_AUTHORITY,\n\t\t\tevidence_refs: input.evidenceRefs ?? [],\n\t\t},\n\t};\n\treturn `---\\n${stringify(frontmatter).trim()}\\n---\\n\\n${input.body.trim()}\\n`;\n}\n\nexport function okfMemoryItemToWriteRequest(item: MemoryItem, reason: string): MemoryWriteRequest {\n\treturn {\n\t\tproviderId: item.providerId,\n\t\tscope: item.scope,\n\t\tkind: item.kind,\n\t\ttitle: item.title,\n\t\tsummary: item.summary,\n\t\tcontent: item.content,\n\t\tevidenceRefs: item.evidenceRefs,\n\t\tsensitivity: \"normal\",\n\t\treason,\n\t};\n}\n\nexport function validateOkfProjectRulePromotion(\n\trequest: OkfProjectRulePromotionRequest,\n): OkfProjectRulePromotionRejection[] {\n\tconst rejections: OkfProjectRulePromotionRejection[] = [];\n\tif (request.item.kind !== \"project_rule_candidate\") rejections.push(\"not_project_rule_candidate\");\n\tif (request.approvalId === undefined && request.trustedConfigPath === undefined) {\n\t\trejections.push(\"missing_explicit_promotion_authority\");\n\t}\n\tif (request.item.stale || request.item.conflict !== undefined) rejections.push(\"stale_or_conflicting_memory\");\n\treturn rejections;\n}\n"]}
1
+ {"version":3,"file":"okf-memory.d.ts","sourceRoot":"","sources":["../../../src/core/context/okf-memory.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAEX,UAAU,EAGV,kBAAkB,EAClB,MAAM,+BAA+B,CAAC;AAEvC,eAAO,MAAM,kBAAkB,WAAW,CAAC;AAE3C,eAAO,MAAM,gBAAgB,mBAAmB,CAAC;AAEjD,eAAO,MAAM,YAAY,qOAWf,CAAC;AAEX,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AAEtD,MAAM,MAAM,uBAAuB,GAChC,qBAAqB,GACrB,qBAAqB,GACrB,cAAc,GACd,cAAc,GACd,cAAc,GACd,eAAe,GACf,qBAAqB,GACrB,YAAY,GACZ,eAAe,GACf,mBAAmB,GACnB,uBAAuB,CAAC;AAE3B,MAAM,WAAW,mBAAmB;IACnC,IAAI,EAAE,uBAAuB,CAAC;IAC9B,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,qBAAqB;IACrC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,uBAAuB;IACvC,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,mBAAmB,EAAE,CAAC;CACnC;AAED,MAAM,WAAW,sBAAsB;IACtC,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,WAAW,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB;AAED,MAAM,WAAW,8BAA8B;IAC9C,IAAI,EAAE,UAAU,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,MAAM,gCAAgC,GACzC,4BAA4B,GAC5B,sCAAsC,GACtC,6BAA6B,CAAC;AA8DjC,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,GAAG,uBAAuB,CA6EpH;AAED,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,sBAAsB,GAAG,MAAM,CAc7E;AAED,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,kBAAkB,CAYhG;AAED,wBAAgB,+BAA+B,CAC9C,OAAO,EAAE,8BAA8B,GACrC,gCAAgC,EAAE,CAQpC","sourcesContent":["import { createHash } from \"node:crypto\";\nimport { stringify } from \"yaml\";\nimport { parseFrontmatter } from \"../../utils/frontmatter.ts\";\nimport { isRecordObject } from \"../util/value-guards.ts\";\nimport type { MemoryScope } from \"./context-item.ts\";\nimport type {\n\tMemoryEvidenceRef,\n\tMemoryItem,\n\tMemoryItemKind,\n\tMemoryRef,\n\tMemoryWriteRequest,\n} from \"./memory-provider-contract.ts\";\n\nexport const PI_OKF_PROVIDER_ID = \"pi-okf\";\n\nexport const PI_OKF_AUTHORITY = \"durable_memory\";\n\nexport const PI_OKF_TYPES = [\n\t\"Design Decision\",\n\t\"Architecture Concept\",\n\t\"Project Rule Candidate\",\n\t\"Implementation Note\",\n\t\"Debugging Finding\",\n\t\"Invalidated Assumption\",\n\t\"Tooling Playbook\",\n\t\"External Reference\",\n\t\"User Preference\",\n\t\"Capability Doc\",\n] as const;\n\nexport type PiOkfType = (typeof PI_OKF_TYPES)[number];\n\nexport type OkfMemoryDiagnosticCode =\n\t| \"missing_frontmatter\"\n\t| \"invalid_frontmatter\"\n\t| \"invalid_yaml\"\n\t| \"missing_type\"\n\t| \"unknown_type\"\n\t| \"missing_title\"\n\t| \"missing_description\"\n\t| \"missing_pi\"\n\t| \"invalid_scope\"\n\t| \"invalid_authority\"\n\t| \"invalid_evidence_refs\";\n\nexport interface OkfMemoryDiagnostic {\n\tcode: OkfMemoryDiagnosticCode;\n\tmessage: string;\n}\n\nexport interface ParseOkfMemoryOptions {\n\tproviderId?: string;\n\turi?: string;\n\tfallbackId?: string;\n}\n\nexport interface ParsedOkfMemoryDocument {\n\titem?: MemoryItem;\n\tbody: string;\n\tdiagnostics: OkfMemoryDiagnostic[];\n}\n\nexport interface OkfMemoryDocumentInput {\n\ttype: PiOkfType;\n\ttitle: string;\n\tdescription: string;\n\tscope: MemoryScope;\n\tbody: string;\n\ttags?: string[];\n\ttimestamp?: string;\n\tevidenceRefs?: string[];\n}\n\nexport interface OkfProjectRulePromotionRequest {\n\titem: MemoryItem;\n\tapprovalId?: string;\n\ttrustedConfigPath?: string;\n}\n\nexport type OkfProjectRulePromotionRejection =\n\t| \"not_project_rule_candidate\"\n\t| \"missing_explicit_promotion_authority\"\n\t| \"stale_or_conflicting_memory\";\n\nconst OKF_TYPE_TO_MEMORY_KIND = new Map<PiOkfType, MemoryItemKind>([\n\t[\"Design Decision\", \"design_decision\"],\n\t[\"Architecture Concept\", \"architecture_concept\"],\n\t[\"Project Rule Candidate\", \"project_rule_candidate\"],\n\t[\"Implementation Note\", \"fact\"],\n\t[\"Debugging Finding\", \"debugging_finding\"],\n\t[\"Invalidated Assumption\", \"invalidated_assumption\"],\n\t[\"Tooling Playbook\", \"procedure\"],\n\t[\"External Reference\", \"reference\"],\n\t[\"User Preference\", \"user_preference\"],\n\t[\"Capability Doc\", \"reference\"],\n]);\n\nconst VALID_SCOPES: readonly MemoryScope[] = [\"session\", \"project\", \"user\", \"global\"];\n\nfunction stringField(record: Record<string, unknown>, key: string): string | undefined {\n\tconst value = record[key];\n\treturn typeof value === \"string\" && value.trim().length > 0 ? value : undefined;\n}\n\nfunction stringArrayField(record: Record<string, unknown>, key: string): string[] | undefined {\n\tconst value = record[key];\n\tif (value === undefined) return undefined;\n\treturn Array.isArray(value) && value.every((entry) => typeof entry === \"string\") ? value : undefined;\n}\n\nfunction memoryItemKindForOkfType(type: string): MemoryItemKind | undefined {\n\treturn OKF_TYPE_TO_MEMORY_KIND.get(type as PiOkfType);\n}\n\nfunction memoryIdForDocument(\n\tproviderId: string,\n\turi: string | undefined,\n\ttitle: string,\n\ttimestamp: string | undefined,\n): string {\n\treturn createHash(\"sha256\")\n\t\t.update([providerId, uri ?? \"\", title, timestamp ?? \"\"].join(\"\\0\"))\n\t\t.digest(\"hex\")\n\t\t.slice(0, 24);\n}\n\nfunction evidenceRefsFromOkf(providerId: string, refs: string[] | undefined): MemoryEvidenceRef[] {\n\treturn (refs ?? []).map((ref) => ({ type: \"external\", id: ref, providerId, description: \"OKF evidence_ref\" }));\n}\n\nfunction buildMemoryRef(\n\tproviderId: string,\n\tid: string,\n\tscope: MemoryScope,\n\tkind: MemoryItemKind,\n\turi?: string,\n): MemoryRef {\n\treturn { providerId, itemId: id, scope, kind, uri };\n}\n\nfunction validScope(value: string | undefined): MemoryScope | undefined {\n\treturn value !== undefined && VALID_SCOPES.includes(value as MemoryScope) ? (value as MemoryScope) : undefined;\n}\n\nexport function parseOkfMemoryDocument(content: string, options: ParseOkfMemoryOptions = {}): ParsedOkfMemoryDocument {\n\tlet frontmatter: Record<string, unknown>;\n\tlet body: string;\n\ttry {\n\t\tconst parsed = parseFrontmatter(content);\n\t\tfrontmatter = parsed.frontmatter;\n\t\tbody = parsed.body;\n\t} catch (error) {\n\t\treturn {\n\t\t\tbody: content,\n\t\t\tdiagnostics: [{ code: \"invalid_yaml\", message: error instanceof Error ? error.message : String(error) }],\n\t\t};\n\t}\n\n\tconst diagnostics: OkfMemoryDiagnostic[] = [];\n\tif (!isRecordObject(frontmatter) || Object.keys(frontmatter).length === 0) {\n\t\treturn { body, diagnostics: [{ code: \"missing_frontmatter\", message: \"OKF memory requires YAML frontmatter.\" }] };\n\t}\n\n\tconst type = stringField(frontmatter, \"type\");\n\tif (type === undefined) diagnostics.push({ code: \"missing_type\", message: \"OKF memory requires a type.\" });\n\tconst kind = type === undefined ? undefined : memoryItemKindForOkfType(type);\n\tif (type !== undefined && kind === undefined)\n\t\tdiagnostics.push({ code: \"unknown_type\", message: `Unknown Pi OKF type: ${type}` });\n\n\tconst title = stringField(frontmatter, \"title\");\n\tif (title === undefined) diagnostics.push({ code: \"missing_title\", message: \"OKF memory requires a title.\" });\n\tconst description = stringField(frontmatter, \"description\");\n\tif (description === undefined)\n\t\tdiagnostics.push({ code: \"missing_description\", message: \"OKF memory requires a description.\" });\n\n\tconst pi = frontmatter.pi;\n\tif (!isRecordObject(pi)) diagnostics.push({ code: \"missing_pi\", message: \"OKF memory requires a pi block.\" });\n\tconst scope = isRecordObject(pi) ? validScope(stringField(pi, \"scope\")) : undefined;\n\tif (isRecordObject(pi) && scope === undefined)\n\t\tdiagnostics.push({ code: \"invalid_scope\", message: \"pi.scope must be session, project, user, or global.\" });\n\tconst authority = isRecordObject(pi) ? stringField(pi, \"authority\") : undefined;\n\tif (isRecordObject(pi) && authority !== PI_OKF_AUTHORITY) {\n\t\tdiagnostics.push({ code: \"invalid_authority\", message: `pi.authority must be ${PI_OKF_AUTHORITY}.` });\n\t}\n\tconst evidenceRefs = isRecordObject(pi) ? stringArrayField(pi, \"evidence_refs\") : undefined;\n\tif (isRecordObject(pi) && pi.evidence_refs !== undefined && evidenceRefs === undefined) {\n\t\tdiagnostics.push({ code: \"invalid_evidence_refs\", message: \"pi.evidence_refs must be a string array.\" });\n\t}\n\n\tif (\n\t\tdiagnostics.length > 0 ||\n\t\tkind === undefined ||\n\t\ttitle === undefined ||\n\t\tdescription === undefined ||\n\t\tscope === undefined\n\t) {\n\t\treturn { body, diagnostics };\n\t}\n\n\tconst providerId = options.providerId ?? PI_OKF_PROVIDER_ID;\n\tconst timestamp = stringField(frontmatter, \"timestamp\");\n\tconst id = options.fallbackId ?? memoryIdForDocument(providerId, options.uri, title, timestamp);\n\tconst ref = buildMemoryRef(providerId, id, scope, kind, options.uri);\n\treturn {\n\t\tbody,\n\t\tdiagnostics,\n\t\titem: {\n\t\t\tid,\n\t\t\tproviderId,\n\t\t\tsource: \"pi_native\",\n\t\t\tkind,\n\t\t\tscope,\n\t\t\tdurability: \"durable\",\n\t\t\ttitle,\n\t\t\tsummary: description,\n\t\t\tcontent: body.length > 0 ? body : undefined,\n\t\t\trefs: [ref],\n\t\t\tevidenceRefs: evidenceRefsFromOkf(providerId, evidenceRefs),\n\t\t\ttimestamp,\n\t\t},\n\t};\n}\n\nexport function formatOkfMemoryDocument(input: OkfMemoryDocumentInput): string {\n\tconst frontmatter = {\n\t\ttype: input.type,\n\t\ttitle: input.title,\n\t\tdescription: input.description,\n\t\ttags: input.tags,\n\t\ttimestamp: input.timestamp,\n\t\tpi: {\n\t\t\tscope: input.scope,\n\t\t\tauthority: PI_OKF_AUTHORITY,\n\t\t\tevidence_refs: input.evidenceRefs ?? [],\n\t\t},\n\t};\n\treturn `---\\n${stringify(frontmatter).trim()}\\n---\\n\\n${input.body.trim()}\\n`;\n}\n\nexport function okfMemoryItemToWriteRequest(item: MemoryItem, reason: string): MemoryWriteRequest {\n\treturn {\n\t\tproviderId: item.providerId,\n\t\tscope: item.scope,\n\t\tkind: item.kind,\n\t\ttitle: item.title,\n\t\tsummary: item.summary,\n\t\tcontent: item.content,\n\t\tevidenceRefs: item.evidenceRefs,\n\t\tsensitivity: \"normal\",\n\t\treason,\n\t};\n}\n\nexport function validateOkfProjectRulePromotion(\n\trequest: OkfProjectRulePromotionRequest,\n): OkfProjectRulePromotionRejection[] {\n\tconst rejections: OkfProjectRulePromotionRejection[] = [];\n\tif (request.item.kind !== \"project_rule_candidate\") rejections.push(\"not_project_rule_candidate\");\n\tif (request.approvalId === undefined && request.trustedConfigPath === undefined) {\n\t\trejections.push(\"missing_explicit_promotion_authority\");\n\t}\n\tif (request.item.stale || request.item.conflict !== undefined) rejections.push(\"stale_or_conflicting_memory\");\n\treturn rejections;\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { stringify } from "yaml";
3
3
  import { parseFrontmatter } from "../../utils/frontmatter.js";
4
+ import { isRecordObject } from "../util/value-guards.js";
4
5
  export const PI_OKF_PROVIDER_ID = "pi-okf";
5
6
  export const PI_OKF_AUTHORITY = "durable_memory";
6
7
  export const PI_OKF_TYPES = [
@@ -28,9 +29,6 @@ const OKF_TYPE_TO_MEMORY_KIND = new Map([
28
29
  ["Capability Doc", "reference"],
29
30
  ]);
30
31
  const VALID_SCOPES = ["session", "project", "user", "global"];
31
- function isRecord(value) {
32
- return typeof value === "object" && value !== null && !Array.isArray(value);
33
- }
34
32
  function stringField(record, key) {
35
33
  const value = record[key];
36
34
  return typeof value === "string" && value.trim().length > 0 ? value : undefined;
@@ -74,7 +72,7 @@ export function parseOkfMemoryDocument(content, options = {}) {
74
72
  };
75
73
  }
76
74
  const diagnostics = [];
77
- if (!isRecord(frontmatter) || Object.keys(frontmatter).length === 0) {
75
+ if (!isRecordObject(frontmatter) || Object.keys(frontmatter).length === 0) {
78
76
  return { body, diagnostics: [{ code: "missing_frontmatter", message: "OKF memory requires YAML frontmatter." }] };
79
77
  }
80
78
  const type = stringField(frontmatter, "type");
@@ -90,17 +88,17 @@ export function parseOkfMemoryDocument(content, options = {}) {
90
88
  if (description === undefined)
91
89
  diagnostics.push({ code: "missing_description", message: "OKF memory requires a description." });
92
90
  const pi = frontmatter.pi;
93
- if (!isRecord(pi))
91
+ if (!isRecordObject(pi))
94
92
  diagnostics.push({ code: "missing_pi", message: "OKF memory requires a pi block." });
95
- const scope = isRecord(pi) ? validScope(stringField(pi, "scope")) : undefined;
96
- if (isRecord(pi) && scope === undefined)
93
+ const scope = isRecordObject(pi) ? validScope(stringField(pi, "scope")) : undefined;
94
+ if (isRecordObject(pi) && scope === undefined)
97
95
  diagnostics.push({ code: "invalid_scope", message: "pi.scope must be session, project, user, or global." });
98
- const authority = isRecord(pi) ? stringField(pi, "authority") : undefined;
99
- if (isRecord(pi) && authority !== PI_OKF_AUTHORITY) {
96
+ const authority = isRecordObject(pi) ? stringField(pi, "authority") : undefined;
97
+ if (isRecordObject(pi) && authority !== PI_OKF_AUTHORITY) {
100
98
  diagnostics.push({ code: "invalid_authority", message: `pi.authority must be ${PI_OKF_AUTHORITY}.` });
101
99
  }
102
- const evidenceRefs = isRecord(pi) ? stringArrayField(pi, "evidence_refs") : undefined;
103
- if (isRecord(pi) && pi.evidence_refs !== undefined && evidenceRefs === undefined) {
100
+ const evidenceRefs = isRecordObject(pi) ? stringArrayField(pi, "evidence_refs") : undefined;
101
+ if (isRecordObject(pi) && pi.evidence_refs !== undefined && evidenceRefs === undefined) {
104
102
  diagnostics.push({ code: "invalid_evidence_refs", message: "pi.evidence_refs must be a string array." });
105
103
  }
106
104
  if (diagnostics.length > 0 ||