@terreno/ai 57.9.0 → 57.10.1

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 (412) hide show
  1. package/README.md +1 -0
  2. package/bunfig.toml +1 -1
  3. package/dist/aiApp.d.ts +8 -2
  4. package/dist/aiApp.js +12 -6
  5. package/dist/harness/adminScreens.d.ts +6 -0
  6. package/dist/harness/adminScreens.js +11 -0
  7. package/dist/harness/agentLoop.d.ts +61 -0
  8. package/dist/harness/agentLoop.js +798 -0
  9. package/dist/harness/agentTaskNames.d.ts +4 -0
  10. package/dist/harness/agentTaskNames.js +7 -0
  11. package/dist/harness/approvalPolicy.d.ts +3 -0
  12. package/dist/harness/approvalPolicy.js +23 -0
  13. package/dist/harness/approvals.d.ts +101 -0
  14. package/dist/harness/approvals.js +468 -0
  15. package/dist/harness/commit.d.ts +238 -0
  16. package/dist/harness/commit.js +678 -0
  17. package/dist/harness/conversation.d.ts +75 -0
  18. package/dist/harness/conversation.js +397 -0
  19. package/dist/harness/defineAgent.d.ts +9 -0
  20. package/dist/harness/defineAgent.js +65 -0
  21. package/dist/harness/defineTask.d.ts +11 -0
  22. package/dist/harness/defineTask.js +59 -0
  23. package/dist/harness/defineTool.d.ts +7 -0
  24. package/dist/harness/defineTool.js +36 -0
  25. package/dist/harness/definitionError.d.ts +8 -0
  26. package/dist/harness/definitionError.js +15 -0
  27. package/dist/harness/errors.d.ts +127 -0
  28. package/dist/harness/errors.js +101 -0
  29. package/dist/harness/eventHub.d.ts +45 -0
  30. package/dist/harness/eventHub.js +135 -0
  31. package/dist/harness/events.d.ts +95 -0
  32. package/dist/harness/events.js +267 -0
  33. package/dist/harness/executionEnv.d.ts +31 -0
  34. package/dist/harness/executionEnv.js +2 -0
  35. package/dist/harness/extensions.d.ts +82 -0
  36. package/dist/harness/extensions.js +274 -0
  37. package/dist/harness/harness.d.ts +163 -0
  38. package/dist/harness/harness.js +520 -0
  39. package/dist/harness/harnessApp.d.ts +44 -0
  40. package/dist/harness/harnessApp.js +62 -0
  41. package/dist/harness/internalRuntime.d.ts +16 -0
  42. package/dist/harness/internalRuntime.js +18 -0
  43. package/dist/harness/leases.d.ts +35 -0
  44. package/dist/harness/leases.js +86 -0
  45. package/dist/harness/memo.d.ts +24 -0
  46. package/dist/harness/memo.js +78 -0
  47. package/dist/harness/modelCall.d.ts +45 -0
  48. package/dist/harness/modelCall.js +147 -0
  49. package/dist/harness/models/harnessApproval.d.ts +2 -0
  50. package/dist/harness/models/harnessApproval.js +100 -0
  51. package/dist/harness/models/harnessConversation.d.ts +2 -0
  52. package/dist/harness/models/harnessConversation.js +117 -0
  53. package/dist/harness/models/harnessEvent.d.ts +3 -0
  54. package/dist/harness/models/harnessEvent.js +73 -0
  55. package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
  56. package/dist/harness/models/harnessInboxEvent.js +58 -0
  57. package/dist/harness/models/harnessMemo.d.ts +2 -0
  58. package/dist/harness/models/harnessMemo.js +42 -0
  59. package/dist/harness/models/harnessMessage.d.ts +2 -0
  60. package/dist/harness/models/harnessMessage.js +73 -0
  61. package/dist/harness/models/harnessOwner.d.ts +2 -0
  62. package/dist/harness/models/harnessOwner.js +36 -0
  63. package/dist/harness/models/harnessTask.d.ts +2 -0
  64. package/dist/harness/models/harnessTask.js +186 -0
  65. package/dist/harness/ownership.d.ts +89 -0
  66. package/dist/harness/ownership.js +438 -0
  67. package/dist/harness/registry.d.ts +29 -0
  68. package/dist/harness/registry.js +93 -0
  69. package/dist/harness/retryBackoff.d.ts +31 -0
  70. package/dist/harness/retryBackoff.js +50 -0
  71. package/dist/harness/routes/approvals.d.ts +13 -0
  72. package/dist/harness/routes/approvals.js +93 -0
  73. package/dist/harness/routes/conversations.d.ts +11 -0
  74. package/dist/harness/routes/conversations.js +66 -0
  75. package/dist/harness/routes/events.d.ts +19 -0
  76. package/dist/harness/routes/events.js +215 -0
  77. package/dist/harness/routes/tasks.d.ts +11 -0
  78. package/dist/harness/routes/tasks.js +108 -0
  79. package/dist/harness/runners/inProcessRunner.d.ts +57 -0
  80. package/dist/harness/runners/inProcessRunner.js +196 -0
  81. package/dist/harness/runtime.d.ts +34 -0
  82. package/dist/harness/runtime.js +551 -0
  83. package/dist/harness/subagent.d.ts +53 -0
  84. package/dist/harness/subagent.js +134 -0
  85. package/dist/harness/suspend.d.ts +8 -0
  86. package/dist/harness/suspend.js +16 -0
  87. package/dist/harness/transaction.d.ts +3 -0
  88. package/dist/harness/transaction.js +22 -0
  89. package/dist/harness/waits.d.ts +100 -0
  90. package/dist/harness/waits.js +214 -0
  91. package/dist/index.d.ts +19 -1
  92. package/dist/index.js +45 -2
  93. package/dist/models/aiDataset.d.ts +2 -0
  94. package/dist/models/aiDataset.js +67 -0
  95. package/dist/models/aiRequest.js +1 -1
  96. package/dist/models/gptHistory.js +73 -0
  97. package/dist/observability/adminScreens.d.ts +5 -0
  98. package/dist/observability/adminScreens.js +32 -0
  99. package/dist/observability/compileTemplate.d.ts +1 -0
  100. package/dist/observability/compileTemplate.js +13 -0
  101. package/dist/observability/datasetImport.d.ts +11 -0
  102. package/dist/observability/datasetImport.js +185 -0
  103. package/dist/observability/evaluate.d.ts +31 -0
  104. package/dist/observability/evaluate.js +200 -0
  105. package/dist/observability/evaluatorTemplates.d.ts +17 -0
  106. package/dist/observability/evaluatorTemplates.js +111 -0
  107. package/dist/observability/jsonPath.d.ts +2 -0
  108. package/dist/observability/jsonPath.js +57 -0
  109. package/dist/observability/local/datasetStore.d.ts +87 -0
  110. package/dist/observability/local/datasetStore.js +326 -0
  111. package/dist/observability/local/evaluatorStore.d.ts +44 -0
  112. package/dist/observability/local/evaluatorStore.js +182 -0
  113. package/dist/observability/local/experimentRunner.d.ts +96 -0
  114. package/dist/observability/local/experimentRunner.js +419 -0
  115. package/dist/observability/local/localPlugin.d.ts +22 -0
  116. package/dist/observability/local/localPlugin.js +76 -0
  117. package/dist/observability/local/models/obsDataset.d.ts +2 -0
  118. package/dist/observability/local/models/obsDataset.js +37 -0
  119. package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
  120. package/dist/observability/local/models/obsDatasetItem.js +76 -0
  121. package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
  122. package/dist/observability/local/models/obsEvaluator.js +98 -0
  123. package/dist/observability/local/models/obsExperiment.d.ts +2 -0
  124. package/dist/observability/local/models/obsExperiment.js +87 -0
  125. package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
  126. package/dist/observability/local/models/obsExperimentItem.js +45 -0
  127. package/dist/observability/local/models/obsPrompt.d.ts +2 -0
  128. package/dist/observability/local/models/obsPrompt.js +42 -0
  129. package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
  130. package/dist/observability/local/models/obsPromptLabel.js +39 -0
  131. package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
  132. package/dist/observability/local/models/obsPromptVersion.js +96 -0
  133. package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
  134. package/dist/observability/local/models/obsReviewItem.js +75 -0
  135. package/dist/observability/local/models/obsScore.d.ts +2 -0
  136. package/dist/observability/local/models/obsScore.js +58 -0
  137. package/dist/observability/local/models/obsSpan.d.ts +2 -0
  138. package/dist/observability/local/models/obsSpan.js +64 -0
  139. package/dist/observability/local/models/obsTrace.d.ts +2 -0
  140. package/dist/observability/local/models/obsTrace.js +93 -0
  141. package/dist/observability/local/promptStore.d.ts +180 -0
  142. package/dist/observability/local/promptStore.js +444 -0
  143. package/dist/observability/local/reviewStore.d.ts +50 -0
  144. package/dist/observability/local/reviewStore.js +203 -0
  145. package/dist/observability/local/traceStore.d.ts +103 -0
  146. package/dist/observability/local/traceStore.js +325 -0
  147. package/dist/observability/observabilityApp.d.ts +24 -0
  148. package/dist/observability/observabilityApp.js +157 -0
  149. package/dist/observability/observabilityAppRegistry.d.ts +14 -0
  150. package/dist/observability/observabilityAppRegistry.js +16 -0
  151. package/dist/observability/permissions.d.ts +7 -0
  152. package/dist/observability/permissions.js +60 -0
  153. package/dist/observability/reviewPanels.d.ts +18 -0
  154. package/dist/observability/reviewPanels.js +46 -0
  155. package/dist/observability/routes/datasets.d.ts +7 -0
  156. package/dist/observability/routes/datasets.js +149 -0
  157. package/dist/observability/routes/evaluators.d.ts +7 -0
  158. package/dist/observability/routes/evaluators.js +123 -0
  159. package/dist/observability/routes/experiments.d.ts +7 -0
  160. package/dist/observability/routes/experiments.js +95 -0
  161. package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
  162. package/dist/observability/routes/observabilityRouteAccess.js +67 -0
  163. package/dist/observability/routes/prompts.d.ts +12 -0
  164. package/dist/observability/routes/prompts.js +172 -0
  165. package/dist/observability/routes/requestAiService.d.ts +13 -0
  166. package/dist/observability/routes/requestAiService.js +21 -0
  167. package/dist/observability/routes/review.d.ts +7 -0
  168. package/dist/observability/routes/review.js +79 -0
  169. package/dist/observability/routes/status.d.ts +5 -0
  170. package/dist/observability/routes/status.js +31 -0
  171. package/dist/observability/routes/testMultiStage.d.ts +9 -0
  172. package/dist/observability/routes/testMultiStage.js +67 -0
  173. package/dist/observability/routes/traces.d.ts +7 -0
  174. package/dist/observability/routes/traces.js +129 -0
  175. package/dist/observability/schemaValidation.d.ts +5 -0
  176. package/dist/observability/schemaValidation.js +26 -0
  177. package/dist/observability/sopThresholds.d.ts +2 -0
  178. package/dist/observability/sopThresholds.js +21 -0
  179. package/dist/observability/status.d.ts +23 -0
  180. package/dist/observability/status.js +34 -0
  181. package/dist/observability/testMultiStageSchemas.d.ts +10 -0
  182. package/dist/observability/testMultiStageSchemas.js +79 -0
  183. package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
  184. package/dist/observability/testMultiStageWorkflow.js +218 -0
  185. package/dist/observability/types.d.ts +159 -0
  186. package/dist/observability/types.js +67 -0
  187. package/dist/routes/files.js +18 -7
  188. package/dist/routes/gpt.js +79 -689
  189. package/dist/routes/gptActions.d.ts +9 -0
  190. package/dist/routes/gptActions.js +212 -0
  191. package/dist/routes/gptDatasets.d.ts +5 -0
  192. package/dist/routes/gptDatasets.js +94 -0
  193. package/dist/routes/gptHistories.d.ts +3 -1
  194. package/dist/routes/gptHistories.js +188 -2
  195. package/dist/service/aiDatasets.d.ts +57 -0
  196. package/dist/service/aiDatasets.js +253 -0
  197. package/dist/service/aiService.d.ts +50 -1
  198. package/dist/service/aiService.js +426 -77
  199. package/dist/service/askFiles.d.ts +49 -0
  200. package/dist/service/askFiles.js +162 -0
  201. package/dist/service/asks.d.ts +76 -0
  202. package/dist/service/asks.js +284 -0
  203. package/dist/service/chatTurn.d.ts +128 -0
  204. package/dist/service/chatTurn.js +1674 -0
  205. package/dist/service/fileStorage.d.ts +4 -0
  206. package/dist/service/fileStorage.js +7 -2
  207. package/dist/service/fillActionIds.d.ts +6 -0
  208. package/dist/service/fillActionIds.js +79 -0
  209. package/dist/service/jsonFenceModel.d.ts +7 -0
  210. package/dist/service/jsonFenceModel.js +45 -0
  211. package/dist/service/prompts.d.ts +52 -0
  212. package/dist/service/prompts.js +96 -1
  213. package/dist/service/sanitizeHtml.d.ts +11 -0
  214. package/dist/service/sanitizeHtml.js +108 -0
  215. package/dist/types/harness.d.ts +934 -0
  216. package/dist/types/harness.js +133 -0
  217. package/dist/types/index.d.ts +192 -14
  218. package/dist/types/index.js +2 -0
  219. package/dist/types/observability.d.ts +288 -0
  220. package/dist/types/observability.js +2 -0
  221. package/package.json +17 -5
  222. package/src/aiApp.test.ts +207 -1
  223. package/src/aiApp.ts +19 -6
  224. package/src/harness/adminScreens.test.ts +34 -0
  225. package/src/harness/adminScreens.ts +11 -0
  226. package/src/harness/agentLoop.test.ts +1034 -0
  227. package/src/harness/agentLoop.ts +1082 -0
  228. package/src/harness/agentRecovery.test.ts +380 -0
  229. package/src/harness/agentTaskNames.ts +4 -0
  230. package/src/harness/approvalPolicy.ts +28 -0
  231. package/src/harness/approvals.test.ts +1094 -0
  232. package/src/harness/approvals.ts +644 -0
  233. package/src/harness/commit.ts +1049 -0
  234. package/src/harness/conversation.ts +503 -0
  235. package/src/harness/defineAgent.test.ts +92 -0
  236. package/src/harness/defineAgent.ts +82 -0
  237. package/src/harness/defineTask.test.ts +59 -0
  238. package/src/harness/defineTask.ts +76 -0
  239. package/src/harness/defineTool.ts +45 -0
  240. package/src/harness/definitionError.ts +13 -0
  241. package/src/harness/errors.test.ts +90 -0
  242. package/src/harness/errors.ts +119 -0
  243. package/src/harness/eventHub.test.ts +124 -0
  244. package/src/harness/eventHub.ts +159 -0
  245. package/src/harness/events.test.ts +757 -0
  246. package/src/harness/events.ts +384 -0
  247. package/src/harness/executionEnv.ts +34 -0
  248. package/src/harness/extensions.test.ts +1218 -0
  249. package/src/harness/extensions.ts +382 -0
  250. package/src/harness/harness.test.ts +693 -0
  251. package/src/harness/harness.ts +767 -0
  252. package/src/harness/harnessApp.ts +76 -0
  253. package/src/harness/internalRuntime.ts +44 -0
  254. package/src/harness/leases.ts +132 -0
  255. package/src/harness/memo.ts +107 -0
  256. package/src/harness/modelCall.test.ts +134 -0
  257. package/src/harness/modelCall.ts +200 -0
  258. package/src/harness/models/harnessApproval.ts +109 -0
  259. package/src/harness/models/harnessConversation.ts +142 -0
  260. package/src/harness/models/harnessEvent.ts +92 -0
  261. package/src/harness/models/harnessInboxEvent.ts +73 -0
  262. package/src/harness/models/harnessMemo.ts +43 -0
  263. package/src/harness/models/harnessMessage.ts +85 -0
  264. package/src/harness/models/harnessModels.test.ts +60 -0
  265. package/src/harness/models/harnessOwner.ts +40 -0
  266. package/src/harness/models/harnessTask.ts +201 -0
  267. package/src/harness/ownership.test.ts +1087 -0
  268. package/src/harness/ownership.ts +618 -0
  269. package/src/harness/recovery.test.ts +912 -0
  270. package/src/harness/registry.ts +124 -0
  271. package/src/harness/retries.test.ts +292 -0
  272. package/src/harness/retryBackoff.test.ts +100 -0
  273. package/src/harness/retryBackoff.ts +77 -0
  274. package/src/harness/routes/approvals.ts +123 -0
  275. package/src/harness/routes/conversations.test.ts +518 -0
  276. package/src/harness/routes/conversations.ts +102 -0
  277. package/src/harness/routes/events.ts +280 -0
  278. package/src/harness/routes/tasks.test.ts +198 -0
  279. package/src/harness/routes/tasks.ts +146 -0
  280. package/src/harness/runners/inProcessRunner.test.ts +402 -0
  281. package/src/harness/runners/inProcessRunner.ts +220 -0
  282. package/src/harness/runtime.ts +714 -0
  283. package/src/harness/subagent.test.ts +696 -0
  284. package/src/harness/subagent.ts +192 -0
  285. package/src/harness/suspend.ts +13 -0
  286. package/src/harness/transaction.ts +17 -0
  287. package/src/harness/versioning.test.ts +288 -0
  288. package/src/harness/waits.test.ts +693 -0
  289. package/src/harness/waits.ts +356 -0
  290. package/src/index.test.ts +13 -0
  291. package/src/index.ts +70 -0
  292. package/src/isolated/adminScreensConfig.isolated.ts +143 -0
  293. package/src/models/aiDataset.test.ts +34 -0
  294. package/src/models/aiDataset.ts +78 -0
  295. package/src/models/aiRequest.ts +2 -1
  296. package/src/models/gptHistory.test.ts +156 -0
  297. package/src/models/gptHistory.ts +89 -1
  298. package/src/observability/adminScreens.test.ts +71 -0
  299. package/src/observability/adminScreens.ts +42 -0
  300. package/src/observability/compileTemplate.test.ts +14 -0
  301. package/src/observability/compileTemplate.ts +10 -0
  302. package/src/observability/datasetImport.test.ts +95 -0
  303. package/src/observability/datasetImport.ts +220 -0
  304. package/src/observability/evaluate.test.ts +549 -0
  305. package/src/observability/evaluate.ts +254 -0
  306. package/src/observability/evaluatorTemplates.ts +124 -0
  307. package/src/observability/jsonPath.test.ts +52 -0
  308. package/src/observability/jsonPath.ts +60 -0
  309. package/src/observability/local/datasetStore.test.ts +230 -0
  310. package/src/observability/local/datasetStore.ts +452 -0
  311. package/src/observability/local/evaluatorStore.test.ts +138 -0
  312. package/src/observability/local/evaluatorStore.ts +224 -0
  313. package/src/observability/local/experimentRunner.test.ts +489 -0
  314. package/src/observability/local/experimentRunner.ts +548 -0
  315. package/src/observability/local/localPlugin.ts +88 -0
  316. package/src/observability/local/models/obsDataset.ts +38 -0
  317. package/src/observability/local/models/obsDatasetItem.ts +80 -0
  318. package/src/observability/local/models/obsEvaluator.ts +102 -0
  319. package/src/observability/local/models/obsExperiment.ts +91 -0
  320. package/src/observability/local/models/obsExperimentItem.ts +52 -0
  321. package/src/observability/local/models/obsPrompt.ts +43 -0
  322. package/src/observability/local/models/obsPromptLabel.ts +43 -0
  323. package/src/observability/local/models/obsPromptVersion.ts +107 -0
  324. package/src/observability/local/models/obsReviewItem.ts +79 -0
  325. package/src/observability/local/models/obsScore.ts +59 -0
  326. package/src/observability/local/models/obsSpan.ts +65 -0
  327. package/src/observability/local/models/obsTrace.ts +94 -0
  328. package/src/observability/local/obsModels.test.ts +119 -0
  329. package/src/observability/local/promptStore.test.ts +351 -0
  330. package/src/observability/local/promptStore.ts +665 -0
  331. package/src/observability/local/reviewStore.test.ts +164 -0
  332. package/src/observability/local/reviewStore.ts +264 -0
  333. package/src/observability/local/traceStore.test.ts +264 -0
  334. package/src/observability/local/traceStore.ts +440 -0
  335. package/src/observability/observabilityApp.test.ts +92 -0
  336. package/src/observability/observabilityApp.ts +185 -0
  337. package/src/observability/observabilityAppRegistry.test.ts +30 -0
  338. package/src/observability/observabilityAppRegistry.ts +35 -0
  339. package/src/observability/permissions.test.ts +96 -0
  340. package/src/observability/permissions.ts +78 -0
  341. package/src/observability/reviewPanels.test.ts +35 -0
  342. package/src/observability/reviewPanels.ts +64 -0
  343. package/src/observability/routes/datasets.test.ts +176 -0
  344. package/src/observability/routes/datasets.ts +252 -0
  345. package/src/observability/routes/evaluators.test.ts +119 -0
  346. package/src/observability/routes/evaluators.ts +207 -0
  347. package/src/observability/routes/experiments.test.ts +282 -0
  348. package/src/observability/routes/experiments.ts +152 -0
  349. package/src/observability/routes/observabilityRbac.test.ts +767 -0
  350. package/src/observability/routes/observabilityRouteAccess.ts +106 -0
  351. package/src/observability/routes/prompts.test.ts +320 -0
  352. package/src/observability/routes/prompts.ts +263 -0
  353. package/src/observability/routes/requestAiService.ts +36 -0
  354. package/src/observability/routes/review.test.ts +120 -0
  355. package/src/observability/routes/review.ts +125 -0
  356. package/src/observability/routes/status.test.ts +116 -0
  357. package/src/observability/routes/status.ts +47 -0
  358. package/src/observability/routes/testMultiStage.test.ts +214 -0
  359. package/src/observability/routes/testMultiStage.ts +96 -0
  360. package/src/observability/routes/traces.test.ts +167 -0
  361. package/src/observability/routes/traces.ts +172 -0
  362. package/src/observability/schemaValidation.ts +29 -0
  363. package/src/observability/sopThresholds.ts +20 -0
  364. package/src/observability/status.test.ts +38 -0
  365. package/src/observability/status.ts +56 -0
  366. package/src/observability/testMultiStageSchemas.ts +95 -0
  367. package/src/observability/testMultiStageWorkflow.test.ts +114 -0
  368. package/src/observability/testMultiStageWorkflow.ts +307 -0
  369. package/src/observability/types.ts +262 -0
  370. package/src/routes/files.test.ts +63 -9
  371. package/src/routes/files.ts +21 -7
  372. package/src/routes/gpt.test.ts +3279 -0
  373. package/src/routes/gpt.ts +91 -816
  374. package/src/routes/gptActions.test.ts +241 -0
  375. package/src/routes/gptActions.ts +255 -0
  376. package/src/routes/gptApprovals.test.ts +762 -0
  377. package/src/routes/gptDatasets.test.ts +86 -0
  378. package/src/routes/gptDatasets.ts +104 -0
  379. package/src/routes/gptFiles.test.ts +589 -0
  380. package/src/routes/gptHistories.test.ts +933 -0
  381. package/src/routes/gptHistories.ts +266 -5
  382. package/src/routes/routes.test.ts +222 -2
  383. package/src/service/aiDatasets.test.ts +123 -0
  384. package/src/service/aiDatasets.ts +316 -0
  385. package/src/service/aiService.test.ts +593 -0
  386. package/src/service/aiService.ts +568 -111
  387. package/src/service/askFiles.test.ts +96 -0
  388. package/src/service/askFiles.ts +222 -0
  389. package/src/service/asks.test.ts +946 -0
  390. package/src/service/asks.ts +423 -0
  391. package/src/service/chatTurn.ts +2287 -0
  392. package/src/service/fileStorage.test.ts +11 -0
  393. package/src/service/fileStorage.ts +10 -2
  394. package/src/service/fillActionIds.test.ts +65 -0
  395. package/src/service/fillActionIds.ts +98 -0
  396. package/src/service/jsonFenceModel.test.ts +47 -0
  397. package/src/service/jsonFenceModel.ts +53 -0
  398. package/src/service/prompts.test.ts +21 -0
  399. package/src/service/prompts.ts +147 -0
  400. package/src/service/sanitizeHtml.test.ts +61 -0
  401. package/src/service/sanitizeHtml.ts +109 -0
  402. package/src/tests/bunSetup.test.ts +7 -4
  403. package/src/tests/bunSetup.ts +7 -7
  404. package/src/tests/chatHarness.ts +478 -0
  405. package/src/tests/generateStream.ts +49 -0
  406. package/src/tests/harnessErrors.ts +22 -0
  407. package/src/tests/harnessStreaming.ts +227 -0
  408. package/src/tests/helpers.ts +9 -2
  409. package/src/types/harness.ts +1095 -0
  410. package/src/types/index.ts +209 -13
  411. package/src/types/observability.ts +344 -0
  412. package/src/types/sanitize-html.d.ts +25 -0
@@ -0,0 +1,1218 @@
1
+ import {
2
+ afterEach,
3
+ beforeAll,
4
+ beforeEach,
5
+ describe,
6
+ expect,
7
+ it,
8
+ mock,
9
+ setDefaultTimeout,
10
+ } from "bun:test";
11
+ import {z} from "@terreno/api";
12
+ import type {LanguageModel} from "ai";
13
+ import {DateTime, Duration} from "luxon";
14
+ import type mongoose from "mongoose";
15
+ import {createLocalObservabilityPlugin} from "../observability/local/localPlugin";
16
+ import {registerObsSpan} from "../observability/local/models/obsSpan";
17
+ import {registerObsTrace} from "../observability/local/models/obsTrace";
18
+ import {generateToStream} from "../tests/generateStream";
19
+ import {harnessErrorMatching} from "../tests/harnessErrors";
20
+ import type {
21
+ AnyHarnessToolDefinition,
22
+ HarnessAgentDefinition,
23
+ HarnessTaskDocument,
24
+ } from "../types/harness";
25
+ import type {ObsSpanModel, ObsTraceModel} from "../types/observability";
26
+ import type {HarnessModels} from "./commit";
27
+ import {errorMessage} from "./errors";
28
+ import {
29
+ AGENT_TOOL_TASK_NAME,
30
+ AGENT_TURN_TASK_NAME,
31
+ defineAgent,
32
+ defineExtension,
33
+ defineTask,
34
+ defineTool,
35
+ Harness,
36
+ HarnessCommitConflictError,
37
+ type HarnessModelRef,
38
+ type HarnessRegistryEntry,
39
+ hook,
40
+ InProcessRunner,
41
+ section,
42
+ wrapTool,
43
+ } from "./harness";
44
+ import {createMemo} from "./memo";
45
+ import {registerHarnessConversation} from "./models/harnessConversation";
46
+ import {registerHarnessMemo} from "./models/harnessMemo";
47
+ import {registerHarnessMessage} from "./models/harnessMessage";
48
+ import {registerHarnessOwner} from "./models/harnessOwner";
49
+ import {registerHarnessTask} from "./models/harnessTask";
50
+
51
+ interface ScriptedToolCall {
52
+ id: string;
53
+ input: Record<string, unknown>;
54
+ name: string;
55
+ }
56
+
57
+ interface ScriptStep {
58
+ text?: string;
59
+ toolCalls?: ScriptedToolCall[];
60
+ }
61
+
62
+ interface GenerateCall {
63
+ prompt: Array<{content: unknown; role: string}>;
64
+ tools?: Array<{name: string}>;
65
+ }
66
+
67
+ /** A LanguageModelV2 mock that answers each request with the next scripted step. */
68
+ const scriptedModel = (steps: ScriptStep[]) => {
69
+ const calls: GenerateCall[] = [];
70
+ const model = {
71
+ doGenerate: mock(async (options: GenerateCall) => {
72
+ calls.push(JSON.parse(JSON.stringify(options)));
73
+ const step = steps.shift();
74
+ if (!step) {
75
+ throw new Error("script exhausted");
76
+ }
77
+ const toolCalls = step.toolCalls ?? [];
78
+ return {
79
+ content: [
80
+ ...(step.text ? [{text: step.text, type: "text" as const}] : []),
81
+ ...toolCalls.map((call) => ({
82
+ input: JSON.stringify(call.input),
83
+ toolCallId: call.id,
84
+ toolName: call.name,
85
+ type: "tool-call" as const,
86
+ })),
87
+ ],
88
+ finishReason: toolCalls.length > 0 ? ("tool-calls" as const) : ("stop" as const),
89
+ usage: {inputTokens: 10, outputTokens: 2, totalTokens: 12},
90
+ warnings: [],
91
+ };
92
+ }),
93
+ doStream: mock(async (options: GenerateCall) =>
94
+ generateToStream(await model.doGenerate(options))
95
+ ),
96
+ modelId: "mock-model",
97
+ provider: "mock",
98
+ specificationVersion: "v2" as const,
99
+ supportedUrls: {},
100
+ };
101
+ return {calls, model};
102
+ };
103
+
104
+ /** Plain JSON copy; matchers must never walk mongoose documents (they recurse forever). */
105
+ const plain = (value: unknown): unknown => JSON.parse(JSON.stringify(value));
106
+
107
+ const MODEL: HarnessModelRef = {modelId: "mock-model", provider: "mock"};
108
+
109
+ // Whole turns on a real replica set; the first test also pays for index creation.
110
+ setDefaultTimeout(20_000);
111
+
112
+ const TaskModel = registerHarnessTask();
113
+ const OwnerModel = registerHarnessOwner();
114
+ const ConversationModel = registerHarnessConversation();
115
+ const MessageModel = registerHarnessMessage();
116
+ const MemoModel = registerHarnessMemo();
117
+ let SpanModel: ObsSpanModel;
118
+ let TraceModel: ObsTraceModel;
119
+
120
+ const openHarnesses: Harness[] = [];
121
+
122
+ const openHarness = async ({
123
+ model,
124
+ registry,
125
+ }: {
126
+ model?: ReturnType<typeof scriptedModel>["model"];
127
+ registry: ReadonlyArray<HarnessRegistryEntry>;
128
+ }): Promise<Harness> => {
129
+ const harness = await Harness.open({
130
+ models: () => model as unknown as LanguageModel,
131
+ registry,
132
+ runner: new InProcessRunner({pollInterval: {milliseconds: 20}}),
133
+ testHooks: {random: () => 0},
134
+ });
135
+ openHarnesses.push(harness);
136
+ await harness.start();
137
+ return harness;
138
+ };
139
+
140
+ const lookupTool = defineTool({
141
+ description: "Look up a patient's chart by id",
142
+ execute: async ({patientId}: {patientId: string}) => ({chart: `chart-${patientId}`}),
143
+ name: "lookup",
144
+ parameters: z.object({patientId: z.string()}),
145
+ });
146
+
147
+ const agentWith = (
148
+ overrides: Partial<Parameters<typeof defineAgent>[0]> = {}
149
+ ): HarnessAgentDefinition =>
150
+ defineAgent({
151
+ instructions: "Answer the clinician.",
152
+ model: MODEL,
153
+ modelRetry: {backoffMs: 1, maxBackoffMs: 2},
154
+ name: "test.clinician",
155
+ tools: [lookupTool],
156
+ ...overrides,
157
+ });
158
+
159
+ /** Run one turn of `agent` (with `extensions` registered) and return everything it wrote. */
160
+ const runTurn = async ({
161
+ agent,
162
+ extensions,
163
+ steps,
164
+ }: {
165
+ agent: HarnessAgentDefinition;
166
+ extensions: ReadonlyArray<HarnessRegistryEntry>;
167
+ steps: ScriptStep[];
168
+ }) => {
169
+ const scripted = scriptedModel(steps);
170
+ const harness = await openHarness({model: scripted.model, registry: [agent, ...extensions]});
171
+ const conversation = await harness.createConversation({agent});
172
+ const turn = await conversation.submit({content: "Check patient p7", requestId: "r1"});
173
+ const done = await harness.waitForTask(turn._id, {timeout: {seconds: 10}});
174
+ const messages = await MessageModel.find({conversationId: conversation.id}).sort({seq: 1});
175
+ return {calls: scripted.calls, conversation, done, harness, messages};
176
+ };
177
+
178
+ const toolResultOf = (
179
+ messages: ReadonlyArray<{parts: unknown[]; role: string; status?: string}>
180
+ ): {output: unknown; status?: string} => {
181
+ const tool = messages.find(({role}) => role === "tool");
182
+ return {
183
+ output: (tool?.parts[0] as {output?: unknown} | undefined)?.output,
184
+ status: tool?.status,
185
+ };
186
+ };
187
+
188
+ /** The tool result the model was sent on its second request. */
189
+ const toolResultSeen = (calls: GenerateCall[]): unknown =>
190
+ calls[1]?.prompt.find(({role}) => role === "tool")?.content;
191
+
192
+ const lookupCall: ScriptStep = {
193
+ toolCalls: [{id: "call-1", input: {patientId: "p7"}, name: "lookup"}],
194
+ };
195
+
196
+ const llmSpans = (task: HarnessTaskDocument) =>
197
+ SpanModel.find({kind: "LLM", traceId: task.traceId}).sort({_id: 1});
198
+
199
+ describe("Extensions, hooks, wraps, and memos", () => {
200
+ beforeAll(async () => {
201
+ // The shared preload's beforeEach imports this lazily under a 5 s hook timeout; a
202
+ // cold first import can exceed it, so load it here first.
203
+ await import("../langfuseClient");
204
+ createLocalObservabilityPlugin();
205
+ SpanModel = registerObsSpan();
206
+ TraceModel = registerObsTrace();
207
+ });
208
+
209
+ beforeEach(async () => {
210
+ await Promise.all([
211
+ TaskModel.deleteMany({}),
212
+ OwnerModel.deleteMany({}),
213
+ ConversationModel.deleteMany({}),
214
+ MessageModel.deleteMany({}),
215
+ MemoModel.deleteMany({}),
216
+ SpanModel.deleteMany({}),
217
+ TraceModel.deleteMany({}),
218
+ ]);
219
+ });
220
+
221
+ afterEach(async () => {
222
+ await Promise.all(openHarnesses.splice(0).map((harness) => harness.stop()));
223
+ });
224
+
225
+ describe("definitions", () => {
226
+ it("validates extensions, sections, hooks, and wraps", () => {
227
+ expect(() => defineExtension({name: " "})).toThrow(
228
+ harnessErrorMatching("definitionInvalid", "defineExtension: name is required")
229
+ );
230
+ expect(() => section("", () => "x")).toThrow(
231
+ harnessErrorMatching("definitionInvalid", "section: name is required")
232
+ );
233
+ expect(() => section("s", "x" as never)).toThrow(
234
+ harnessErrorMatching("definitionInvalid", "section(s): build must be a function")
235
+ );
236
+ expect(() => hook("onEverything" as never, () => undefined)).toThrow(
237
+ harnessErrorMatching(
238
+ "definitionInvalid",
239
+ 'hook: kind must be one of afterTool, beforeModelRequest, beforeTool, not "onEverything"'
240
+ )
241
+ );
242
+ expect(() => hook("beforeTool", 3 as never)).toThrow(
243
+ harnessErrorMatching("definitionInvalid", "hook(beforeTool): handler must be a function")
244
+ );
245
+ expect(() => wrapTool("", (tool) => tool)).toThrow(
246
+ harnessErrorMatching("definitionInvalid", "wrapTool: a tool or tool name is required")
247
+ );
248
+ expect(() => wrapTool("lookup", null as never)).toThrow(
249
+ harnessErrorMatching("definitionInvalid", "wrapTool(lookup): wrap must be a function")
250
+ );
251
+ expect(wrapTool(lookupTool, (tool) => tool).toolName).toBe("lookup");
252
+ expect(() =>
253
+ defineExtension({name: "x", sections: [section("a", () => ""), section("a", () => "")]})
254
+ ).toThrow(
255
+ harnessErrorMatching(
256
+ "definitionInvalid",
257
+ 'defineExtension(x): section "a" is listed more than once'
258
+ )
259
+ );
260
+ expect(() => defineExtension({name: "x", sections: [{} as never]})).toThrow(
261
+ harnessErrorMatching(
262
+ "definitionInvalid",
263
+ "defineExtension(x): every section must come from section()"
264
+ )
265
+ );
266
+ expect(() => defineExtension({name: "x", tools: [lookupTool, lookupTool]})).toThrow(
267
+ harnessErrorMatching(
268
+ "definitionInvalid",
269
+ 'defineExtension(x): tool "lookup" is listed more than once'
270
+ )
271
+ );
272
+ expect(() => defineExtension({name: "x", tools: [{} as never]})).toThrow(
273
+ harnessErrorMatching(
274
+ "definitionInvalid",
275
+ "defineExtension(x): every tool must come from defineTool"
276
+ )
277
+ );
278
+ expect(() => defineExtension({hooks: [{} as never], name: "x"})).toThrow(
279
+ harnessErrorMatching(
280
+ "definitionInvalid",
281
+ "defineExtension(x): every hook must come from hook()"
282
+ )
283
+ );
284
+ expect(() => defineExtension({name: "x", wraps: [{} as never]})).toThrow(
285
+ harnessErrorMatching(
286
+ "definitionInvalid",
287
+ "defineExtension(x): every wrap must come from wrapTool()"
288
+ )
289
+ );
290
+ const extension = defineExtension({name: "clinic"});
291
+ expect(Object.isFrozen(extension)).toBe(true);
292
+ expect(agentWith({extensions: [extension, "audit"]}).extensions).toEqual(["clinic", "audit"]);
293
+ expect(() => agentWith({extensions: ["clinic", extension]})).toThrow(
294
+ harnessErrorMatching(
295
+ "definitionInvalid",
296
+ "defineAgent(test.clinician): an extension is listed more than once"
297
+ )
298
+ );
299
+ expect(() => agentWith({extensions: [{} as never]})).toThrow(
300
+ harnessErrorMatching(
301
+ "definitionInvalid",
302
+ "defineAgent(test.clinician): every extension must be a defineExtension result or its name"
303
+ )
304
+ );
305
+ });
306
+
307
+ it("rejects duplicate extension names and agents that use unregistered extensions", async () => {
308
+ const clinic = defineExtension({name: "clinic"});
309
+ await expect(
310
+ Harness.open({registry: [clinic, defineExtension({name: "clinic"})]})
311
+ ).rejects.toThrow(
312
+ harnessErrorMatching(
313
+ "configInvalid",
314
+ 'Harness registry lists extension "clinic" more than once'
315
+ )
316
+ );
317
+ await expect(
318
+ Harness.open({
319
+ models: () => ({}) as LanguageModel,
320
+ registry: [agentWith({extensions: ["clinic"]})],
321
+ })
322
+ ).rejects.toThrow(
323
+ harnessErrorMatching(
324
+ "configInvalid",
325
+ 'Agent "test.clinician" uses extension "clinic", which is not in this harness registry'
326
+ )
327
+ );
328
+ });
329
+ });
330
+
331
+ describe("beforeTool", () => {
332
+ it("blocks a call: the tool never runs and the model gets the reason as an error result (AC6)", async () => {
333
+ const execute = mock(async () => ({chart: "should not run"}));
334
+ const guarded = defineTool({...lookupTool, execute});
335
+ const policy = defineExtension({
336
+ hooks: [
337
+ hook("beforeTool", (call) =>
338
+ call.toolName === "lookup" ? {block: "Chart lookups need sign-off"} : undefined
339
+ ),
340
+ ],
341
+ name: "policy",
342
+ });
343
+ const {calls, done, messages} = await runTurn({
344
+ agent: agentWith({extensions: [policy], tools: [guarded]}),
345
+ extensions: [policy],
346
+ steps: [lookupCall, {text: "I cannot look that up."}],
347
+ });
348
+
349
+ expect(done.status).toBe("completed");
350
+ expect(execute).not.toHaveBeenCalled();
351
+ expect(toolResultOf(messages)).toEqual({
352
+ output: "Chart lookups need sign-off",
353
+ status: "error",
354
+ });
355
+ expect(toolResultSeen(calls)).toEqual([
356
+ {
357
+ output: {type: "error-text", value: "Chart lookups need sign-off"},
358
+ toolCallId: "call-1",
359
+ toolName: "lookup",
360
+ type: "tool-result",
361
+ },
362
+ ]);
363
+ const toolTask = await TaskModel.findExactlyOne({name: AGENT_TOOL_TASK_NAME});
364
+ expect(plain(toolTask.outcome)).toEqual({
365
+ error: "Chart lookups need sign-off",
366
+ status: "failed",
367
+ });
368
+ });
369
+
370
+ it("rewrites arguments in extension order, and re-validates them", async () => {
371
+ const seen: unknown[] = [];
372
+ const recorder = defineTool({
373
+ ...lookupTool,
374
+ execute: async (args: {patientId: string}) => {
375
+ seen.push(args);
376
+ return {chart: `chart-${args.patientId}`};
377
+ },
378
+ });
379
+ const first = defineExtension({
380
+ hooks: [
381
+ hook("beforeTool", (call) => ({
382
+ args: {patientId: `${(call.args as {patientId: string}).patientId}-a`},
383
+ })),
384
+ ],
385
+ name: "first",
386
+ });
387
+ const second = defineExtension({
388
+ hooks: [
389
+ hook("beforeTool", () => undefined),
390
+ hook("beforeTool", (call) => ({
391
+ args: {patientId: `${(call.args as {patientId: string}).patientId}-b`},
392
+ })),
393
+ ],
394
+ name: "second",
395
+ });
396
+ const {messages} = await runTurn({
397
+ agent: agentWith({extensions: [first, second], tools: [recorder]}),
398
+ extensions: [first, second],
399
+ steps: [lookupCall, {text: "done"}],
400
+ });
401
+ expect(seen).toEqual([{patientId: "p7-a-b"}]);
402
+ expect(toolResultOf(messages)).toEqual({output: {chart: "chart-p7-a-b"}, status: "ok"});
403
+ });
404
+
405
+ it("reports rewritten arguments that fail the tool's schema as an error result", async () => {
406
+ const breaker = defineExtension({
407
+ hooks: [hook("beforeTool", () => ({args: {patientId: 7}}))],
408
+ name: "breaker",
409
+ });
410
+ const {messages} = await runTurn({
411
+ agent: agentWith({extensions: [breaker]}),
412
+ extensions: [breaker],
413
+ steps: [lookupCall, {text: "done"}],
414
+ });
415
+ const result = toolResultOf(messages);
416
+ expect(result.status).toBe("error");
417
+ expect(String(result.output)).toStartWith(
418
+ 'Invalid arguments for tool "lookup" after beforeTool hooks:'
419
+ );
420
+ });
421
+
422
+ it("turns a throwing tool hook into an error result naming the extension", async () => {
423
+ const broken = defineExtension({
424
+ hooks: [
425
+ hook("beforeTool", () => {
426
+ throw new Error("policy store down");
427
+ }),
428
+ ],
429
+ name: "broken",
430
+ });
431
+ const {done, messages} = await runTurn({
432
+ agent: agentWith({extensions: [broken]}),
433
+ extensions: [broken],
434
+ steps: [lookupCall, {text: "done"}],
435
+ });
436
+ expect(done.status).toBe("completed");
437
+ expect(toolResultOf(messages)).toEqual({
438
+ output: 'Extension "broken" beforeTool hook failed: policy store down',
439
+ status: "error",
440
+ });
441
+ });
442
+
443
+ it("rejects a hook that returns something other than undefined, {block}, or {args}", async () => {
444
+ const odd = defineExtension({
445
+ hooks: [hook("beforeTool", () => ({allow: true}) as never)],
446
+ name: "odd",
447
+ });
448
+ const {messages} = await runTurn({
449
+ agent: agentWith({extensions: [odd]}),
450
+ extensions: [odd],
451
+ steps: [lookupCall, {text: "done"}],
452
+ });
453
+ expect(toolResultOf(messages)).toEqual({
454
+ output:
455
+ 'Tool "lookup" failed: Extension "odd" beforeTool hook must return undefined, {block}, or {args}',
456
+ status: "error",
457
+ });
458
+ });
459
+ });
460
+
461
+ describe("afterTool", () => {
462
+ it("rewrites the result the model sees, in extension order", async () => {
463
+ const redact = defineExtension({
464
+ hooks: [
465
+ hook("afterTool", (call, result) => ({
466
+ ...(result as Record<string, unknown>),
467
+ redactedBy: call.toolName,
468
+ })),
469
+ hook("afterTool", () => undefined),
470
+ ],
471
+ name: "redact",
472
+ });
473
+ const stamp = defineExtension({
474
+ hooks: [
475
+ hook("afterTool", (_call, result) => ({
476
+ ...(result as Record<string, unknown>),
477
+ stamped: true,
478
+ })),
479
+ ],
480
+ name: "stamp",
481
+ });
482
+ const {calls, messages} = await runTurn({
483
+ agent: agentWith({extensions: [redact, stamp]}),
484
+ extensions: [redact, stamp],
485
+ steps: [lookupCall, {text: "done"}],
486
+ });
487
+ const rewritten = {chart: "chart-p7", redactedBy: "lookup", stamped: true};
488
+ expect(toolResultOf(messages)).toEqual({output: rewritten, status: "ok"});
489
+ expect(toolResultSeen(calls)).toEqual([
490
+ {
491
+ output: {type: "json", value: rewritten},
492
+ toolCallId: "call-1",
493
+ toolName: "lookup",
494
+ type: "tool-result",
495
+ },
496
+ ]);
497
+ });
498
+
499
+ it("does not run when the tool throws, and a throwing afterTool fails the call", async () => {
500
+ const afterTool = mock(() => undefined);
501
+ const failing = defineTool({
502
+ ...lookupTool,
503
+ execute: async () => {
504
+ throw new Error("EHR timeout");
505
+ },
506
+ });
507
+ const watcher = defineExtension({hooks: [hook("afterTool", afterTool)], name: "watcher"});
508
+ const first = await runTurn({
509
+ agent: agentWith({extensions: [watcher], tools: [failing]}),
510
+ extensions: [watcher],
511
+ steps: [lookupCall, {text: "done"}],
512
+ });
513
+ expect(afterTool).not.toHaveBeenCalled();
514
+ expect(toolResultOf(first.messages)).toEqual({
515
+ output: 'Tool "lookup" failed: EHR timeout',
516
+ status: "error",
517
+ });
518
+ });
519
+
520
+ it("reports a throwing afterTool hook as the call's error", async () => {
521
+ const broken = defineExtension({
522
+ hooks: [
523
+ hook("afterTool", () => {
524
+ throw new Error("redactor crashed");
525
+ }),
526
+ ],
527
+ name: "broken",
528
+ });
529
+ const {messages} = await runTurn({
530
+ agent: agentWith({extensions: [broken]}),
531
+ extensions: [broken],
532
+ steps: [lookupCall, {text: "done"}],
533
+ });
534
+ expect(toolResultOf(messages)).toEqual({
535
+ output: 'Extension "broken" afterTool hook failed: redactor crashed',
536
+ status: "error",
537
+ });
538
+ });
539
+ });
540
+
541
+ describe("beforeModelRequest and sections", () => {
542
+ it("sends the rewritten request and records which extensions rewrote it", async () => {
543
+ const seenSystems: string[] = [];
544
+ const seenApis: Array<{taskId: string; turnTaskId: string}> = [];
545
+ const rewrite = defineExtension({
546
+ hooks: [
547
+ hook("beforeModelRequest", (request, api) => {
548
+ seenSystems.push(request.system);
549
+ seenApis.push({taskId: api.taskId, turnTaskId: api.turnTaskId});
550
+ return {
551
+ messages: [...request.messages, {content: "Be terse.", role: "user"}],
552
+ system: `${request.system}\n\nRespond in English.`,
553
+ };
554
+ }),
555
+ ],
556
+ name: "rewrite",
557
+ });
558
+ const passive = defineExtension({
559
+ hooks: [hook("beforeModelRequest", () => undefined)],
560
+ name: "passive",
561
+ });
562
+ const {calls, done, messages} = await runTurn({
563
+ agent: agentWith({extensions: [rewrite, passive], tools: []}),
564
+ extensions: [rewrite, passive],
565
+ steps: [{text: "ok"}],
566
+ });
567
+ expect(done.status).toBe("completed");
568
+ expect(seenSystems).toEqual(["Answer the clinician."]);
569
+ expect(seenApis).toEqual([{taskId: String(done._id), turnTaskId: String(done._id)}]);
570
+ expect(calls[0]?.prompt).toEqual([
571
+ {content: "Answer the clinician.\n\nRespond in English.", role: "system"},
572
+ {content: [{text: "Check patient p7", type: "text"}], role: "user"},
573
+ {content: [{text: "Be terse.", type: "text"}], role: "user"},
574
+ ]);
575
+ const [llm] = await llmSpans(done);
576
+ expect(plain(llm?.input)).toMatchObject({rewrittenBy: ["rewrite"]});
577
+ // Rewritten messages are named by hash, since the transcript slice is no longer what was sent.
578
+ expect((plain(llm?.input) as {messages: {hash?: string}}).messages.hash).toMatch(
579
+ /^[0-9a-f]{64}$/
580
+ );
581
+ // The rewritten system prompt differs from the instructions, so it is in the transcript.
582
+ const prompt = messages.find(({role}) => role === "system");
583
+ expect(plain(prompt?.parts)).toMatchObject([
584
+ {sections: [], text: "Answer the clinician.\n\nRespond in English.", type: "system-prompt"},
585
+ ]);
586
+ });
587
+
588
+ it("fails the turn when beforeModelRequest throws or returns a malformed request", async () => {
589
+ const broken = defineExtension({
590
+ hooks: [
591
+ hook("beforeModelRequest", () => {
592
+ throw new Error("quota service down");
593
+ }),
594
+ ],
595
+ name: "broken",
596
+ });
597
+ const first = await runTurn({
598
+ agent: agentWith({extensions: [broken]}),
599
+ extensions: [broken],
600
+ steps: [{text: "never sent"}],
601
+ });
602
+ expect(first.done.status).toBe("failed");
603
+ expect(first.done.outcome?.error).toBe(
604
+ 'Extension "broken" beforeModelRequest hook failed: quota service down'
605
+ );
606
+ expect(first.calls).toHaveLength(0);
607
+ const reloaded = await first.harness.conversation(first.conversation.id);
608
+ expect(reloaded.document.status).toBe("idle");
609
+
610
+ await Promise.all(openHarnesses.splice(0).map((harness) => harness.stop()));
611
+ const malformed = defineExtension({
612
+ hooks: [hook("beforeModelRequest", () => ({system: "only"}) as never)],
613
+ name: "malformed",
614
+ });
615
+ const second = await runTurn({
616
+ agent: agentWith({extensions: [malformed], name: "test.other"}),
617
+ extensions: [malformed],
618
+ steps: [{text: "never sent"}],
619
+ });
620
+ expect(second.done.outcome?.error).toBe(
621
+ 'Extension "malformed" beforeModelRequest hook must return {system, messages} or undefined'
622
+ );
623
+ });
624
+
625
+ it("rebuilds sections before every request and records each changed prompt (AC6)", async () => {
626
+ let build = 0;
627
+ const inputs: Array<{agentName: string; messageCount: number; step: number}> = [];
628
+ const clinic = defineExtension({
629
+ name: "clinic",
630
+ sections: [
631
+ section("vitals", (input) => {
632
+ build += 1;
633
+ inputs.push({
634
+ agentName: input.agentName,
635
+ messageCount: input.messages.length,
636
+ step: input.step,
637
+ });
638
+ return build === 1 ? "Vitals: pending" : "Vitals: BP 120/80";
639
+ }),
640
+ section("empty", () => undefined),
641
+ ],
642
+ });
643
+ const policy = defineExtension({
644
+ name: "policy",
645
+ sections: [section("rules", async () => "Never prescribe.")],
646
+ });
647
+ const {calls, done, messages} = await runTurn({
648
+ agent: agentWith({extensions: [clinic, policy]}),
649
+ extensions: [clinic, policy],
650
+ steps: [lookupCall, lookupCall, {text: "BP is fine."}],
651
+ });
652
+ expect(done.status).toBe("completed");
653
+ expect(inputs).toEqual([
654
+ {agentName: "test.clinician", messageCount: 1, step: 1},
655
+ {agentName: "test.clinician", messageCount: 3, step: 2},
656
+ {agentName: "test.clinician", messageCount: 5, step: 3},
657
+ ]);
658
+ const systems = calls.map((call) => call.prompt[0]);
659
+ expect(systems).toEqual([
660
+ {content: "Answer the clinician.\n\nVitals: pending\n\nNever prescribe.", role: "system"},
661
+ {content: "Answer the clinician.\n\nVitals: BP 120/80\n\nNever prescribe.", role: "system"},
662
+ {content: "Answer the clinician.\n\nVitals: BP 120/80\n\nNever prescribe.", role: "system"},
663
+ ]);
664
+ // Recorded prompts are never replayed to the model as messages.
665
+ for (const call of calls) {
666
+ expect(call.prompt.filter(({role}) => role === "system")).toHaveLength(1);
667
+ }
668
+
669
+ // Two distinct prompts: two records, each just before the answer it produced.
670
+ expect(messages.map(({role}) => role)).toEqual([
671
+ "user",
672
+ "system",
673
+ "assistant",
674
+ "tool",
675
+ "system",
676
+ "assistant",
677
+ "tool",
678
+ "assistant",
679
+ ]);
680
+ const records = messages
681
+ .filter(({role}) => role === "system")
682
+ .map(({parts, seq}) => ({part: plain(parts[0]) as Record<string, unknown>, seq}));
683
+ expect(records.map(({part}) => part.text)).toEqual([
684
+ "Answer the clinician.\n\nVitals: pending\n\nNever prescribe.",
685
+ "Answer the clinician.\n\nVitals: BP 120/80\n\nNever prescribe.",
686
+ ]);
687
+ expect(records[0]?.part.sections).toEqual([
688
+ {extension: "clinic", name: "vitals"},
689
+ {extension: "policy", name: "rules"},
690
+ ]);
691
+
692
+ // Each LLM span names the prompt it sent by hash and transcript seq.
693
+ const spans = (await llmSpans(done)).map(
694
+ (span) => (span.input as {system: {hash: string; messageSeq?: number}}).system
695
+ );
696
+ expect(spans.map(({messageSeq}) => messageSeq)).toEqual([
697
+ records[0]?.seq,
698
+ records[1]?.seq,
699
+ records[1]?.seq,
700
+ ]);
701
+ expect(spans[0]?.hash).toBe(records[0]?.part.hash as string);
702
+ expect(spans[1]?.hash).toBe(records[1]?.part.hash as string);
703
+ expect(spans[2]?.hash).toBe(spans[1]?.hash as string);
704
+ });
705
+
706
+ it("records nothing when the prompt is just the agent's instructions", async () => {
707
+ const silent = defineExtension({name: "silent", sections: [section("none", () => " ")]});
708
+ const {done, messages} = await runTurn({
709
+ agent: agentWith({extensions: [silent], tools: []}),
710
+ extensions: [silent],
711
+ steps: [{text: "ok"}],
712
+ });
713
+ expect(messages.map(({role}) => role)).toEqual(["user", "assistant"]);
714
+ const [llm] = await llmSpans(done);
715
+ const input = plain(llm?.input) as {system: {messageSeq?: number; sections: unknown[]}};
716
+ expect(input.system.sections).toEqual([]);
717
+ expect(input.system.messageSeq).toBeUndefined();
718
+ });
719
+
720
+ it("fails the turn when a section throws", async () => {
721
+ const broken = defineExtension({
722
+ name: "broken",
723
+ sections: [
724
+ section("vitals", () => {
725
+ throw new Error("monitor offline");
726
+ }),
727
+ ],
728
+ });
729
+ const {calls, done} = await runTurn({
730
+ agent: agentWith({extensions: [broken]}),
731
+ extensions: [broken],
732
+ steps: [{text: "never sent"}],
733
+ });
734
+ expect(done.outcome?.error).toBe(
735
+ 'Extension "broken" section "vitals" failed: monitor offline'
736
+ );
737
+ expect(calls).toHaveLength(0);
738
+ });
739
+ });
740
+
741
+ describe("tools and wraps", () => {
742
+ it("lets a later extension override a tool and wraps whichever tool wins (AC6)", async () => {
743
+ const ran: string[] = [];
744
+ const fromAgent = defineTool({
745
+ ...lookupTool,
746
+ execute: async () => {
747
+ ran.push("agent");
748
+ return "agent";
749
+ },
750
+ });
751
+ const fromClinic = defineTool({
752
+ ...lookupTool,
753
+ execute: async () => {
754
+ ran.push("clinic");
755
+ return "clinic";
756
+ },
757
+ });
758
+ const fromOverride = defineTool({
759
+ ...lookupTool,
760
+ execute: async () => {
761
+ ran.push("override");
762
+ return "override";
763
+ },
764
+ });
765
+ const extra = defineTool({
766
+ description: "Page the on-call nurse",
767
+ execute: async () => "paged",
768
+ name: "page",
769
+ parameters: z.object({}),
770
+ });
771
+ const clinic = defineExtension({name: "clinic", tools: [fromClinic, extra]});
772
+ const audited = (tag: string) => (tool: AnyHarnessToolDefinition) =>
773
+ defineTool({
774
+ ...tool,
775
+ execute: async (args, api) => {
776
+ ran.push(`${tag}:before`);
777
+ return {[tag]: await tool.execute(args, api)};
778
+ },
779
+ });
780
+ // The wrapping extension comes before the overriding one, yet wraps its tool.
781
+ const audit = defineExtension({
782
+ name: "audit",
783
+ wraps: [wrapTool("lookup", audited("audit")), wrapTool("missing", audited("never"))],
784
+ });
785
+ const override = defineExtension({
786
+ name: "override",
787
+ tools: [fromOverride],
788
+ wraps: [wrapTool(lookupTool, audited("outer"))],
789
+ });
790
+ const {calls, conversation, messages} = await runTurn({
791
+ agent: agentWith({extensions: [clinic, audit, override], tools: [fromAgent]}),
792
+ extensions: [clinic, audit, override],
793
+ steps: [
794
+ {
795
+ toolCalls: [
796
+ {id: "call-1", input: {patientId: "p7"}, name: "lookup"},
797
+ {id: "call-2", input: {}, name: "page"},
798
+ ],
799
+ },
800
+ {text: "done"},
801
+ ],
802
+ });
803
+ expect(plain(conversation.document.agent.tools)).toEqual(["lookup", "page"]);
804
+ expect(plain(conversation.document.agent.extensions)).toEqual([
805
+ "clinic",
806
+ "audit",
807
+ "override",
808
+ ]);
809
+ expect(calls[0]?.tools?.map(({name}) => name)).toEqual(["lookup", "page"]);
810
+ // The first extension's wrap is innermost.
811
+ expect(ran).toEqual(["outer:before", "audit:before", "override"]);
812
+ expect(toolResultOf(messages)).toEqual({
813
+ output: {outer: {audit: "override"}},
814
+ status: "ok",
815
+ });
816
+ // A tool only an extension provides is callable too.
817
+ expect(toolResultOf(messages.filter(({role}) => role === "tool").slice(1))).toEqual({
818
+ output: "paged",
819
+ status: "ok",
820
+ });
821
+ });
822
+
823
+ it("refuses a conversation whose wrap does not return a tool of the same name", async () => {
824
+ const bad = defineExtension({
825
+ name: "bad",
826
+ wraps: [wrapTool("lookup", (tool) => ({...tool, name: "renamed"}))],
827
+ });
828
+ const throwing = defineExtension({
829
+ name: "throwing",
830
+ wraps: [
831
+ wrapTool("lookup", () => {
832
+ throw new Error("no audit sink");
833
+ }),
834
+ ],
835
+ });
836
+ const agent = agentWith({extensions: [bad]});
837
+ const harness = await openHarness({
838
+ model: scriptedModel([]).model,
839
+ registry: [agent, bad, throwing],
840
+ });
841
+ // Snapshotting the conversation resolves its tools, so a bad wrap fails there.
842
+ await expect(harness.createConversation({agent})).rejects.toThrow(
843
+ harnessErrorMatching(
844
+ "definitionInvalid",
845
+ 'Extension "bad" wrap of tool "lookup" must return a defineTool tool named "lookup"'
846
+ )
847
+ );
848
+ await expect(harness.createConversation({agent, extensions: ["throwing"]})).rejects.toThrow(
849
+ 'Extension "throwing" wrap of tool "lookup" failed: no audit sink'
850
+ );
851
+ });
852
+
853
+ it("reports a wrap that fails while the tool call resolves its tool as the call's error", async () => {
854
+ let resolutions = 0;
855
+ const flaky = defineExtension({
856
+ name: "flaky",
857
+ wraps: [
858
+ wrapTool("lookup", (tool) => {
859
+ resolutions += 1;
860
+ // Resolutions: snapshot, request, tool fan-out, the tool call, next request. Fail the
861
+ // fan-out and the call so the call itself reports the wrap's error.
862
+ if (resolutions === 3 || resolutions === 4) {
863
+ throw new Error("audit sink blipped");
864
+ }
865
+ return tool;
866
+ }),
867
+ ],
868
+ });
869
+ const {done, messages} = await runTurn({
870
+ agent: agentWith({extensions: [flaky]}),
871
+ extensions: [flaky],
872
+ steps: [lookupCall, {text: "done"}],
873
+ });
874
+ expect(done.status).toBe("completed");
875
+ expect(toolResultOf(messages)).toEqual({
876
+ output: 'Extension "flaky" wrap of tool "lookup" failed: audit sink blipped',
877
+ status: "error",
878
+ });
879
+ });
880
+
881
+ it("fails a turn whose conversation names an extension this process does not register", async () => {
882
+ const clinic = defineExtension({name: "clinic"});
883
+ const agent = agentWith({tools: []});
884
+ const before = await openHarness({model: scriptedModel([]).model, registry: [agent, clinic]});
885
+ const conversation = await before.createConversation({agent, extensions: ["clinic"]});
886
+ await before.stop();
887
+ openHarnesses.splice(0);
888
+
889
+ const scripted = scriptedModel([{text: "never sent"}]);
890
+ const after = await openHarness({model: scripted.model, registry: [agent]});
891
+ const turn = await (await after.conversation(conversation.id)).submit({
892
+ content: "hi",
893
+ requestId: "r1",
894
+ });
895
+ const done = await after.waitForTask(turn._id, {timeout: {seconds: 10}});
896
+ expect(done.outcome?.error).toBe('Extension "clinic" is not in this harness registry');
897
+ expect(scripted.calls).toHaveLength(0);
898
+ });
899
+
900
+ it("uses a conversation's own extension list and refuses unregistered names", async () => {
901
+ const clinic = defineExtension({name: "clinic", sections: [section("s", () => "Clinic.")]});
902
+ const scripted = scriptedModel([{text: "ok"}]);
903
+ const agent = agentWith({tools: []});
904
+ const harness = await openHarness({model: scripted.model, registry: [agent, clinic]});
905
+ await expect(harness.createConversation({agent, extensions: ["ghost"]})).rejects.toThrow(
906
+ harnessErrorMatching("notRegistered", 'Extension "ghost" is not in this harness registry')
907
+ );
908
+ await expect(harness.createConversation({agent, extensions: [3 as never]})).rejects.toThrow(
909
+ harnessErrorMatching(
910
+ "definitionInvalid",
911
+ "createConversation: every extension must be a defineExtension result or its name"
912
+ )
913
+ );
914
+ await expect(
915
+ harness.createConversation({agent, extensions: [clinic, "clinic"]})
916
+ ).rejects.toThrow(
917
+ harnessErrorMatching(
918
+ "invalidRequest",
919
+ "createConversation: an extension is listed more than once"
920
+ )
921
+ );
922
+ const conversation = await harness.createConversation({agent, extensions: [clinic]});
923
+ expect(plain(conversation.document.agent.extensions)).toEqual(["clinic"]);
924
+ const turn = await conversation.submit({content: "hi", requestId: "r1"});
925
+ await harness.waitForTask(turn._id, {timeout: {seconds: 10}});
926
+ expect(scripted.calls[0]?.prompt[0]).toEqual({
927
+ content: "Answer the clinician.\n\nClinic.",
928
+ role: "system",
929
+ });
930
+ });
931
+ });
932
+
933
+ describe("memos", () => {
934
+ it("scopes tool-hook memos to the turn task so a decision is made once per call", async () => {
935
+ const decide = mock(() => "approved");
936
+ const seenApis: Array<{taskId: string; turnTaskId: string}> = [];
937
+ const gatekeeper = defineExtension({
938
+ hooks: [
939
+ hook("beforeTool", async (call, api) => {
940
+ seenApis.push({taskId: api.taskId, turnTaskId: api.turnTaskId});
941
+ const key = `decision:${call.toolCallId}`;
942
+ const existing = await api.memo<string>(key);
943
+ const decision = existing ?? (await api.memo(key, decide()));
944
+ return decision === "approved" ? undefined : {block: "denied"};
945
+ }),
946
+ ],
947
+ name: "gatekeeper",
948
+ });
949
+ const {done, messages} = await runTurn({
950
+ agent: agentWith({extensions: [gatekeeper]}),
951
+ extensions: [gatekeeper],
952
+ steps: [lookupCall, {text: "done"}],
953
+ });
954
+ expect(toolResultOf(messages).status).toBe("ok");
955
+ expect(decide).toHaveBeenCalledTimes(1);
956
+ const rows = await MemoModel.find({});
957
+ expect(rows.map(({key, taskId, value}) => ({key, taskId: String(taskId), value}))).toEqual([
958
+ {key: "decision:call-1", taskId: String(done._id), value: "approved"},
959
+ ]);
960
+ expect(done.name).toBe(AGENT_TURN_TASK_NAME);
961
+ const toolTask = await TaskModel.findExactlyOne({name: AGENT_TOOL_TASK_NAME});
962
+ expect(seenApis).toEqual([{taskId: String(toolTask._id), turnTaskId: String(done._id)}]);
963
+ });
964
+
965
+ it("keeps the first write under concurrent writers, and reads it back", async () => {
966
+ const results: unknown[] = [];
967
+ const task = defineTask<Record<string, never>, unknown, unknown>({
968
+ initial: () => ({phase: "decide"}),
969
+ name: "test.memoRace",
970
+ phases: {
971
+ decide: {
972
+ run: async (_task, rt) => {
973
+ expect(await rt.memo("choice")).toBeUndefined();
974
+ const writes = await Promise.all(
975
+ ["a", "b", "c", "d", "e"].map((value) => rt.memo("choice", {value}))
976
+ );
977
+ results.push(...writes);
978
+ results.push(await rt.memo("choice"));
979
+ expect(await rt.memo("nothing", null)).toBeNull();
980
+ await rt.commit({terminal: {result: writes[0], status: "completed"}});
981
+ },
982
+ },
983
+ },
984
+ version: 1,
985
+ });
986
+ const harness = await openHarness({registry: [task]});
987
+ const created = await harness.createTask(task, {});
988
+ const done = await harness.waitForTask(created._id, {timeout: {seconds: 10}});
989
+ expect(done.status).toBe("completed");
990
+ expect(new Set(results.map((value) => JSON.stringify(value))).size).toBe(1);
991
+ expect(await MemoModel.countDocuments({key: "choice", taskId: created._id})).toBe(1);
992
+ const row = await MemoModel.findExactlyOne({key: "choice", taskId: created._id});
993
+ expect(plain(row.value)).toEqual(plain(done.outcome?.result));
994
+ });
995
+
996
+ it("rejects blank keys, non-JSON values, and writes after the phase committed", async () => {
997
+ const errors: string[] = [];
998
+ const lateRead: unknown[] = [];
999
+ const capture = async (work: () => Promise<unknown>): Promise<void> => {
1000
+ try {
1001
+ await work();
1002
+ } catch (error: unknown) {
1003
+ errors.push(errorMessage(error));
1004
+ }
1005
+ };
1006
+ const task = defineTask<Record<string, never>, unknown, unknown>({
1007
+ initial: () => ({phase: "decide"}),
1008
+ name: "test.memoMisuse",
1009
+ phases: {
1010
+ decide: {
1011
+ run: async (_task, rt) => {
1012
+ await capture(() => rt.memo(" "));
1013
+ await capture(() => rt.memo("big", 10n));
1014
+ await capture(() => rt.memo("fn", () => 1));
1015
+ await rt.commit({terminal: {status: "completed"}});
1016
+ await capture(() => rt.memo("late", 1));
1017
+ lateRead.push(await rt.memo("late"));
1018
+ },
1019
+ },
1020
+ },
1021
+ version: 1,
1022
+ });
1023
+ const harness = await openHarness({registry: [task]});
1024
+ const created = await harness.createTask(task, {});
1025
+ await harness.waitForTask(created._id, {timeout: {seconds: 10}});
1026
+ expect(errors).toEqual([
1027
+ "memo requires a non-empty key",
1028
+ 'memo "big": value is not JSON-serializable (JSON.stringify cannot serialize BigInt.)',
1029
+ 'memo "fn": value is not JSON-serializable',
1030
+ 'test.memoMisuse@1: rt.memo called after the phase "decide" committed or started waiting',
1031
+ ]);
1032
+ expect(lateRead).toEqual([undefined]);
1033
+ });
1034
+
1035
+ describe("fenced writers", () => {
1036
+ /** Start `count` tasks whose phase hangs, and return their running rows. */
1037
+ const hangTasks = async (count: number) => {
1038
+ let release: () => void = () => {};
1039
+ const hung = new Promise<void>((resolve) => {
1040
+ release = resolve;
1041
+ });
1042
+ const task = defineTask<{n: number}, unknown, unknown>({
1043
+ initial: () => ({phase: "hold"}),
1044
+ name: "test.memoHold",
1045
+ phases: {
1046
+ hold: {
1047
+ run: async (_task, rt) => {
1048
+ await hung;
1049
+ await rt.commit({terminal: {status: "completed"}});
1050
+ },
1051
+ },
1052
+ },
1053
+ version: 1,
1054
+ });
1055
+ const harness = await openHarness({registry: [task]});
1056
+ const ids = [];
1057
+ for (let n = 0; n < count; n++) {
1058
+ ids.push((await harness.createTask(task, {n}))._id);
1059
+ }
1060
+ // The in-process runner runs one task at a time: wait for the first to hold its
1061
+ // lease, then give the others a running lease of their own, as another runner would.
1062
+ for (let i = 0; i < 300; i++) {
1063
+ if ((await TaskModel.countDocuments({_id: ids[0], status: "running"})) > 0) {
1064
+ break;
1065
+ }
1066
+ await new Promise((resolve) => setTimeout(resolve, 10));
1067
+ }
1068
+ for (const [n, id] of ids.slice(1).entries()) {
1069
+ await TaskModel.updateOne(
1070
+ {_id: id, status: "pending"},
1071
+ {
1072
+ $set: {
1073
+ lease: {
1074
+ acquiredAt: new Date(),
1075
+ expiresAt: DateTime.now().plus({minutes: 5}).toJSDate(),
1076
+ owner: "other-runner",
1077
+ token: `other-${n}`,
1078
+ },
1079
+ status: "running",
1080
+ },
1081
+ }
1082
+ );
1083
+ }
1084
+ const rows = await TaskModel.find({_id: {$in: ids}, status: "running"}).sort({created: 1});
1085
+ expect(rows).toHaveLength(count);
1086
+ return {release, rows};
1087
+ };
1088
+
1089
+ const memoFor = (
1090
+ row: HarnessTaskDocument,
1091
+ scopeTaskId: mongoose.Types.ObjectId,
1092
+ token = row.lease?.token
1093
+ ) =>
1094
+ createMemo({
1095
+ assertWritable: () => {},
1096
+ fence: {phase: row.phase, taskId: row._id, token},
1097
+ lease: {
1098
+ duration: Duration.fromObject({seconds: 30}),
1099
+ heartbeat: Duration.fromObject({seconds: 1}),
1100
+ owner: "test",
1101
+ },
1102
+ models: {memo: MemoModel, task: TaskModel} as unknown as HarnessModels,
1103
+ scopeTaskId,
1104
+ });
1105
+
1106
+ it("lets exactly one of two independent runs win a key (first write wins)", async () => {
1107
+ const {release, rows} = await hangTasks(2);
1108
+ try {
1109
+ const [first, second] = rows as [HarnessTaskDocument, HarnessTaskDocument];
1110
+ // Two runs fencing different task rows race on one shared key, so neither queues
1111
+ // behind the other's lease write and the unique index decides.
1112
+ const scope = first._id;
1113
+ const results = await Promise.all([
1114
+ memoFor(first, scope)("route", {by: "first"}),
1115
+ memoFor(second, scope)("route", {by: "second"}),
1116
+ ]);
1117
+ expect(plain(results[0])).toEqual(plain(results[1]));
1118
+ expect(await MemoModel.countDocuments({key: "route", taskId: scope})).toBe(1);
1119
+ expect(plain(await memoFor(second, scope)("route", {by: "later"}))).toEqual(
1120
+ plain(results[0])
1121
+ );
1122
+ expect(plain(await memoFor(first, scope)("empty", {}))).toEqual({});
1123
+ } finally {
1124
+ release();
1125
+ }
1126
+ });
1127
+
1128
+ it("refuses a write from a run whose lease token is stale", async () => {
1129
+ const {release, rows} = await hangTasks(1);
1130
+ try {
1131
+ const [row] = rows as [HarnessTaskDocument];
1132
+ const stale = memoFor(row, row._id, "stale-token")("choice", "a");
1133
+ await expect(stale).rejects.toBeInstanceOf(HarnessCommitConflictError);
1134
+ await expect(stale).rejects.toThrow(
1135
+ harnessErrorMatching("commitConflict", `Harness commit for task ${row._id}`)
1136
+ );
1137
+ expect(await MemoModel.countDocuments({})).toBe(0);
1138
+ expect(await memoFor(row, row._id)("choice", "b")).toBe("b");
1139
+ } finally {
1140
+ release();
1141
+ }
1142
+ });
1143
+ });
1144
+ });
1145
+
1146
+ describe("memos across a crash", () => {
1147
+ it("returns the first process's decision to the process that resumes the phase (AC6)", async () => {
1148
+ let release: () => void = () => {};
1149
+ const hung = new Promise<void>((resolve) => {
1150
+ release = resolve;
1151
+ });
1152
+ let started: () => void = () => {};
1153
+ const isStarted = new Promise<void>((resolve) => {
1154
+ started = resolve;
1155
+ });
1156
+ const decisions: unknown[] = [];
1157
+ let runs = 0;
1158
+ const task = defineTask<Record<string, never>, unknown, unknown>({
1159
+ initial: () => ({phase: "decide"}),
1160
+ name: "test.memoCrash",
1161
+ phases: {
1162
+ decide: {
1163
+ replay: "safe",
1164
+ run: async (_task, rt) => {
1165
+ runs += 1;
1166
+ const decision = await rt.memo("route", {choice: `run-${runs}`});
1167
+ decisions.push(decision);
1168
+ if (runs === 1) {
1169
+ started();
1170
+ await hung;
1171
+ }
1172
+ await rt.commit({terminal: {result: decision, status: "completed"}});
1173
+ },
1174
+ },
1175
+ },
1176
+ version: 1,
1177
+ });
1178
+ const processes: Array<{die: () => void; harness: Harness}> = [];
1179
+ const openProcess = async (ownerId: string) => {
1180
+ let isDead = false;
1181
+ const harness = await Harness.open({
1182
+ registry: [task],
1183
+ runner: new InProcessRunner({
1184
+ heartbeatInterval: {milliseconds: 50},
1185
+ leaseDuration: {milliseconds: 300},
1186
+ ownerId,
1187
+ pollInterval: {milliseconds: 20},
1188
+ }),
1189
+ testHooks: {isHeartbeatSuspended: () => isDead},
1190
+ });
1191
+ const handle = {
1192
+ die: () => {
1193
+ isDead = true;
1194
+ },
1195
+ harness,
1196
+ };
1197
+ processes.push(handle);
1198
+ await harness.start();
1199
+ return handle;
1200
+ };
1201
+ try {
1202
+ const first = await openProcess("process-a");
1203
+ const created = await first.harness.createTask(task, {});
1204
+ await isStarted;
1205
+ first.die();
1206
+ const second = await openProcess("process-b");
1207
+ const done = await second.harness.waitForTask(created._id, {timeout: {seconds: 15}});
1208
+ expect(done.status).toBe("completed");
1209
+ expect(plain(done.outcome?.result)).toEqual({choice: "run-1"});
1210
+ expect(plain(decisions)).toEqual([{choice: "run-1"}, {choice: "run-1"}]);
1211
+ expect(await MemoModel.countDocuments({taskId: created._id})).toBe(1);
1212
+ } finally {
1213
+ release();
1214
+ await Promise.all(processes.map(({harness}) => harness.stop()));
1215
+ }
1216
+ });
1217
+ });
1218
+ });