@qlairoslabs/acp-client 0.12.1 → 0.13.0-alpha.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 (467) hide show
  1. package/CHANGELOG-consumer-fixes.md +17 -10
  2. package/CHANGELOG-controller-interface.md +258 -0
  3. package/CHANGELOG-metering.md +29 -0
  4. package/CHANGELOG-operation-lifecycle.md +15 -1
  5. package/CONSUMER_GUIDE.md +845 -6
  6. package/MIGRATION-0.10-to-0.12.md +4 -1
  7. package/MIGRATION-0.12-to-0.13.md +275 -0
  8. package/README.md +17 -16
  9. package/package.json +4 -2
  10. package/src/acp-client-handlers.ts +48 -0
  11. package/src/activity.ts +3 -0
  12. package/src/adapters.ts +25 -0
  13. package/src/agent/adoption/provider-attachment.ts +33 -0
  14. package/src/agent/adoption/provider-session-delete.ts +1 -1
  15. package/src/agent/adoption/provider-sessions.ts +3 -2
  16. package/src/agent/append-assistant-message.ts +91 -0
  17. package/src/agent/config.ts +54 -6
  18. package/src/agent/conversation-manager.ts +94 -158
  19. package/src/agent/manager-settings.ts +1 -0
  20. package/src/agent/native-channel-lifecycle.ts +111 -0
  21. package/src/agent/persisted-lifecycle.ts +44 -4
  22. package/src/agent/persistence-policy/context.ts +13 -0
  23. package/src/agent/persistence-policy/resolver.ts +7 -0
  24. package/src/agent/persistence-policy/types.ts +1 -0
  25. package/src/agent/record-capture.ts +1 -0
  26. package/src/agent-adapter-registry.ts +33 -7
  27. package/src/agent-conversation-contracts.ts +14 -1
  28. package/src/agent-conversation-errors.ts +8 -0
  29. package/src/agent-conversation-owner.ts +9 -4
  30. package/src/agent-session-config.ts +135 -0
  31. package/src/agent-session.ts +439 -934
  32. package/src/agent.ts +73 -0
  33. package/src/capabilities.ts +2 -0
  34. package/src/checkpointing/recovery.ts +3 -0
  35. package/src/cli/index.ts +3 -3
  36. package/src/cli/managed.ts +2 -2
  37. package/src/client.ts +52 -94
  38. package/src/connection-attempt.ts +95 -0
  39. package/src/controller-connection.ts +114 -0
  40. package/src/controller.ts +156 -148
  41. package/src/conversation-controller/capabilities.ts +88 -0
  42. package/src/conversation-controller/contracts.ts +208 -0
  43. package/src/conversation-controller/factory.ts +39 -0
  44. package/src/conversation-controller/guards.ts +98 -0
  45. package/src/conversation-controller/index.ts +20 -0
  46. package/src/conversation-startup.ts +35 -0
  47. package/src/conversation-turns/backend-turns.ts +373 -0
  48. package/src/conversation-turns/clear-operations.ts +37 -0
  49. package/src/conversation-turns/context.ts +135 -0
  50. package/src/conversation-turns/index.ts +4 -0
  51. package/src/conversation-turns/record-helpers.ts +75 -0
  52. package/src/conversation-turns/recording.ts +235 -0
  53. package/src/conversation-turns/run-turn.ts +367 -0
  54. package/src/conversation-turns/turn-run.ts +57 -0
  55. package/src/errors.ts +41 -0
  56. package/src/handoff-options.ts +81 -0
  57. package/src/identity/checkpoint-backed.ts +1 -1
  58. package/src/index.ts +58 -3
  59. package/src/internal-inference/coordination/manager.ts +11 -0
  60. package/src/internal-inference/runtime/harness-runtime.ts +4 -4
  61. package/src/launcher.ts +35 -2
  62. package/src/media/parts.ts +5 -0
  63. package/src/message-status.ts +83 -0
  64. package/src/metering/engine.ts +39 -64
  65. package/src/metering/measurement-provenance.ts +36 -0
  66. package/src/metering/native/durability.ts +115 -0
  67. package/src/metering/native/ledger.ts +104 -0
  68. package/src/metering/native/operation.ts +85 -0
  69. package/src/metering/native/projection.ts +114 -0
  70. package/src/metering/native/proof.ts +51 -0
  71. package/src/metering/native/requirements.ts +10 -0
  72. package/src/metering/native/retirement.ts +92 -0
  73. package/src/metering/native/source.ts +90 -0
  74. package/src/metering/record.ts +38 -1
  75. package/src/metering/state.ts +13 -3
  76. package/src/native/approvals.ts +169 -0
  77. package/src/native/capabilities.ts +17 -0
  78. package/src/native/channel-changes.ts +211 -0
  79. package/src/native/connection.ts +56 -0
  80. package/src/native/controller.ts +591 -0
  81. package/src/native/delegation-sender.ts +26 -0
  82. package/src/native/errors.ts +35 -0
  83. package/src/native/functions.ts +156 -0
  84. package/src/native/host-config.ts +59 -0
  85. package/src/native/host.ts +909 -0
  86. package/src/native/item-receipts.ts +15 -0
  87. package/src/native/metering.ts +12 -0
  88. package/src/native/records.ts +166 -0
  89. package/src/native/recovery.ts +186 -0
  90. package/src/native/reminders/fixtures/schema21.sqlite.gz +0 -0
  91. package/src/native/reminders/idle.ts +70 -0
  92. package/src/native/reminders/limits.ts +19 -0
  93. package/src/native/reminders/notifications.ts +29 -0
  94. package/src/native/reminders/reminders.test.ts +553 -0
  95. package/src/native/reminders/routine-config.typecheck.ts +13 -0
  96. package/src/native/reminders/routine-guard.ts +66 -0
  97. package/src/native/reminders/routine-schedule.ts +25 -0
  98. package/src/native/reminders/routine-wire.ts +21 -0
  99. package/src/native/reminders/schedule.ts +97 -0
  100. package/src/native/reminders/service.ts +280 -0
  101. package/src/native/reminders/supervisor.ts +91 -0
  102. package/src/native/reminders/sync.ts +301 -0
  103. package/src/native/reminders/wire.ts +197 -0
  104. package/src/native/reminders/wire.typecheck.ts +13 -0
  105. package/src/native/submission-withdrawal.ts +65 -0
  106. package/src/native/usage.ts +15 -0
  107. package/src/native/worker-delivery.ts +61 -0
  108. package/src/native-harness/.res/README.md +1 -0
  109. package/src/native-harness/.res/architecture/README.md +21 -0
  110. package/src/native-harness/.res/architecture/context-and-data.md +31 -0
  111. package/src/native-harness/.res/architecture/execution.md +42 -0
  112. package/src/native-harness/.res/architecture/overview.md +44 -0
  113. package/src/native-harness/.res/architecture/persistence.md +56 -0
  114. package/src/native-harness/.res/architecture/suspend-and-resume.md +45 -0
  115. package/src/native-harness/.res/architecture/technical-architecture.md +50 -0
  116. package/src/native-harness/.res/decisions/0001-basic-loop.md +11 -0
  117. package/src/native-harness/.res/decisions/0002-channel-input.md +17 -0
  118. package/src/native-harness/.res/decisions/0003-inference-inspection.md +14 -0
  119. package/src/native-harness/.res/decisions/0004-shared-context.md +21 -0
  120. package/src/native-harness/.res/decisions/0005-notice-inbox.md +20 -0
  121. package/src/native-harness/.res/decisions/0006-explicit-notices.md +12 -0
  122. package/src/native-harness/.res/decisions/0007-channel-output.md +12 -0
  123. package/src/native-harness/.res/decisions/0008-browser-client.md +16 -0
  124. package/src/native-harness/.res/decisions/0009-scripted-model.md +18 -0
  125. package/src/native-harness/.res/decisions/0010-inprocess-protocol.md +19 -0
  126. package/src/native-harness/.res/decisions/0011-stdio-transport.md +21 -0
  127. package/src/native-harness/.res/decisions/0012-session-storage-contract.md +37 -0
  128. package/src/native-harness/.res/decisions/0013-durable-delivery-contract.md +64 -0
  129. package/src/native-harness/.res/decisions/0014-channel-controls-contract.md +73 -0
  130. package/src/native-harness/.res/decisions/0015-session-file-contract.md +72 -0
  131. package/src/native-harness/.res/decisions/0016-configuration-permissions.md +159 -0
  132. package/src/native-harness/.res/decisions/0017-openrouter-managed-config.md +69 -0
  133. package/src/native-harness/.res/decisions/0018-tool-dispatch-evidence.md +78 -0
  134. package/src/native-harness/.res/decisions/0019-model-execution.md +188 -0
  135. package/src/native-harness/.res/decisions/0020-startup-channel-changes.md +71 -0
  136. package/src/native-harness/.res/decisions/0021-tools-and-host-callbacks.md +100 -0
  137. package/src/native-harness/.res/decisions/0022-operator-controls.md +115 -0
  138. package/src/native-harness/.res/decisions/0023-long-running-mock.md +82 -0
  139. package/src/native-harness/.res/decisions/0024-callback-policy-names.md +46 -0
  140. package/src/native-harness/.res/decisions/0025-external-notices.md +364 -0
  141. package/src/native-harness/.res/decisions/0026-bounded-context.md +81 -0
  142. package/src/native-harness/.res/decisions/0027-development-instructions.md +22 -0
  143. package/src/native-harness/.res/decisions/0028-debug-tools.md +16 -0
  144. package/src/native-harness/.res/decisions/0029-bounded-model-call-pages.md +37 -0
  145. package/src/native-harness/.res/decisions/0030-timeline-retrieval.md +145 -0
  146. package/src/native-harness/.res/decisions/0031-one-off-reminders.md +173 -0
  147. package/src/native-harness/.res/docs/11-15.md +148 -0
  148. package/src/native-harness/.res/docs/BUSINESS_RULES.md +111 -0
  149. package/src/native-harness/.res/docs/WORK.md +65 -0
  150. package/src/native-harness/.res/docs/_/11-16-persistence/LOG.md +312 -0
  151. package/src/native-harness/.res/docs/_/11-16-persistence/PLAN.md +77 -0
  152. package/src/native-harness/.res/docs/_/11-16-persistence/SPEC.md +313 -0
  153. package/src/native-harness/.res/docs/_/11-19-channel-controls/LOG.md +160 -0
  154. package/src/native-harness/.res/docs/_/11-19-channel-controls/PLAN.md +89 -0
  155. package/src/native-harness/.res/docs/_/11-19-channel-controls/SPEC.md +135 -0
  156. package/src/native-harness/.res/docs/_/11-19-config-permissions/LOG.md +143 -0
  157. package/src/native-harness/.res/docs/_/11-19-config-permissions/PLAN.md +93 -0
  158. package/src/native-harness/.res/docs/_/11-19-config-permissions/SPEC.md +181 -0
  159. package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/11-20-LOG.md +433 -0
  160. package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/PLAN.md +60 -0
  161. package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/PROTOCOL.md +141 -0
  162. package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/SPEC.md +168 -0
  163. package/src/native-harness/.res/docs/attachments/0001-DESIGN/SPEC.md +111 -0
  164. package/src/native-harness/.res/docs/bounded-context/0001-DESIGN/PLAN.md +129 -0
  165. package/src/native-harness/.res/docs/bounded-context/0001-DESIGN/SPEC.md +135 -0
  166. package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/LOG.md +437 -0
  167. package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/PLAN.md +127 -0
  168. package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/SPEC.md +203 -0
  169. package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/LOG.md +292 -0
  170. package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/PLAN.md +91 -0
  171. package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/SPEC.md +187 -0
  172. package/src/native-harness/.res/docs/model-execution/0001-DESIGN/LOG.md +148 -0
  173. package/src/native-harness/.res/docs/model-execution/0001-DESIGN/PLAN.md +95 -0
  174. package/src/native-harness/.res/docs/model-execution/0001-DESIGN/SPEC.md +200 -0
  175. package/src/native-harness/.res/docs/model-execution/0002-AMENDMENT-accounting/SPEC.md +80 -0
  176. package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/LOG.md +206 -0
  177. package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/PLAN.md +106 -0
  178. package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/SPEC.md +297 -0
  179. package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/LOG.md +41 -0
  180. package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/PLAN.md +36 -0
  181. package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/SPEC.md +84 -0
  182. package/src/native-harness/.res/docs/shell-execution/0001-DESIGN/SPEC.md +147 -0
  183. package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/LOG.md +15 -0
  184. package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/PLAN.md +107 -0
  185. package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/SPEC.md +114 -0
  186. package/src/native-harness/.res/docs/todos/11-19-TODO-auto_review-approve.md +24 -0
  187. package/src/native-harness/.res/docs/todos/11-20-TODO-user-entered-tool-code.md +23 -0
  188. package/src/native-harness/.res/docs/todos/11-21-TODO-node-resource-coordination.md +103 -0
  189. package/src/native-harness/.res/docs/todos/11-22-TODO-harness-m1-sequence.md +50 -0
  190. package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/LOG.md +337 -0
  191. package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/PLAN.md +76 -0
  192. package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/SPEC.md +204 -0
  193. package/src/native-harness/.res/docs/tools-and-callbacks/0002-AMENDMENT-callback-policy/LOG.md +73 -0
  194. package/src/native-harness/.res/docs/tools-and-callbacks/0002-AMENDMENT-callback-policy/SPEC.md +64 -0
  195. package/src/native-harness/.res/docs/tools-and-callbacks/0003-DESIGN-attachment-tools/SPEC.md +58 -0
  196. package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/LOG.md +45 -0
  197. package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/PLAN.md +108 -0
  198. package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/SPEC.md +169 -0
  199. package/src/native-harness/.res/docs/web-tools/0001-DESIGN/SPEC.md +128 -0
  200. package/src/native-harness/.res/marketing-draft.md +22 -0
  201. package/src/native-harness/.res/sandbox/1-simple-harness/README.md +285 -0
  202. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-1.md +19 -0
  203. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-10.md +138 -0
  204. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-11.md +148 -0
  205. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-12.md +172 -0
  206. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-13.md +194 -0
  207. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-14.md +233 -0
  208. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-15.md +249 -0
  209. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-16.md +265 -0
  210. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-17.md +279 -0
  211. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-18.md +293 -0
  212. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-19.md +307 -0
  213. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-2.md +29 -0
  214. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-20.md +317 -0
  215. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-21.md +327 -0
  216. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-3.md +40 -0
  217. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-4.md +59 -0
  218. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-5.md +69 -0
  219. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-6.md +79 -0
  220. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-7.md +89 -0
  221. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-8.md +102 -0
  222. package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-9.md +116 -0
  223. package/src/native-harness/.res/sandbox/1-simple-harness/conversation.jsonl +49 -0
  224. package/src/native-harness/.res/sandbox/1-simple-harness/doc.md +87 -0
  225. package/src/native-harness/.res/sandbox/1-simple-harness/harness.py +289 -0
  226. package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy 2.py +221 -0
  227. package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy 3.py +287 -0
  228. package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy.py +165 -0
  229. package/src/native-harness/.res/sandbox/1-simple-harness/state.json +22 -0
  230. package/src/native-harness/.res/sandbox/1-simple-harness/system-prompt.md +2 -0
  231. package/src/native-harness/AGENTS.md +70 -0
  232. package/src/native-harness/ARCHITECTURE.md +249 -0
  233. package/src/native-harness/README.md +726 -0
  234. package/src/native-harness/bun.lock +26 -0
  235. package/src/native-harness/package.json +24 -0
  236. package/src/native-harness/src/capabilities/context/build.ts +59 -0
  237. package/src/native-harness/src/capabilities/context/instructions/agent-demo.md +11 -0
  238. package/src/native-harness/src/capabilities/context/instructions/agent.md +5 -0
  239. package/src/native-harness/src/capabilities/context/instructions/harness.md +7 -0
  240. package/src/native-harness/src/capabilities/context/load-instructions.ts +22 -0
  241. package/src/native-harness/src/capabilities/context/session.ts +92 -0
  242. package/src/native-harness/src/capabilities/model/attempt.ts +27 -0
  243. package/src/native-harness/src/capabilities/model/openrouter-stream.ts +72 -0
  244. package/src/native-harness/src/capabilities/model/openrouter.ts +62 -0
  245. package/src/native-harness/src/capabilities/model/runner.ts +116 -0
  246. package/src/native-harness/src/capabilities/model/scenario-attempt.ts +50 -0
  247. package/src/native-harness/src/capabilities/model/scenario-file.ts +125 -0
  248. package/src/native-harness/src/capabilities/model/scripted-error.ts +6 -0
  249. package/src/native-harness/src/capabilities/model/scripted.ts +81 -0
  250. package/src/native-harness/src/capabilities/model/usage.ts +15 -0
  251. package/src/native-harness/src/capabilities/tools/builtin/current-time.ts +28 -0
  252. package/src/native-harness/src/capabilities/tools/builtin/notify-channel.ts +17 -0
  253. package/src/native-harness/src/capabilities/tools/debug/index.ts +13 -0
  254. package/src/native-harness/src/capabilities/tools/debug/result.ts +12 -0
  255. package/src/native-harness/src/capabilities/tools/debug/test-echo.ts +16 -0
  256. package/src/native-harness/src/capabilities/tools/debug/test-fail.ts +14 -0
  257. package/src/native-harness/src/capabilities/tools/debug/test-wait.ts +19 -0
  258. package/src/native-harness/src/capabilities/tools/reminders/index.ts +75 -0
  259. package/src/native-harness/src/capabilities/tools/runtime/catalog.ts +103 -0
  260. package/src/native-harness/src/capabilities/tools/runtime/functions.ts +130 -0
  261. package/src/native-harness/src/capabilities/tools/runtime/invocations.ts +48 -0
  262. package/src/native-harness/src/capabilities/tools/testing/echo.ts +18 -0
  263. package/src/native-harness/src/capabilities/tools/testing/record.ts +47 -0
  264. package/src/native-harness/src/capabilities/tools/timeline/index.ts +41 -0
  265. package/src/native-harness/src/client.ts +18 -0
  266. package/src/native-harness/src/contracts/agent.ts +105 -0
  267. package/src/native-harness/src/contracts/config-permissions.ts +47 -0
  268. package/src/native-harness/src/contracts/context.ts +112 -0
  269. package/src/native-harness/src/contracts/events.ts +47 -0
  270. package/src/native-harness/src/contracts/input-limits.ts +7 -0
  271. package/src/native-harness/src/contracts/inspection.ts +22 -0
  272. package/src/native-harness/src/contracts/loop.ts +31 -0
  273. package/src/native-harness/src/contracts/model-execution.ts +83 -0
  274. package/src/native-harness/src/contracts/operator-controls.ts +106 -0
  275. package/src/native-harness/src/contracts/output.ts +13 -0
  276. package/src/native-harness/src/contracts/protocol.ts +320 -0
  277. package/src/native-harness/src/contracts/reminders.ts +132 -0
  278. package/src/native-harness/src/contracts/scenario.ts +42 -0
  279. package/src/native-harness/src/contracts/session-file.ts +19 -0
  280. package/src/native-harness/src/contracts/session.ts +184 -0
  281. package/src/native-harness/src/contracts/storage.ts +596 -0
  282. package/src/native-harness/src/contracts/timeline-retrieval.ts +40 -0
  283. package/src/native-harness/src/contracts/tools.ts +133 -0
  284. package/src/native-harness/src/governance/permissions/gate.ts +129 -0
  285. package/src/native-harness/src/governance/policy/resolve.ts +32 -0
  286. package/src/native-harness/src/index.ts +318 -0
  287. package/src/native-harness/src/interfaces/cli/chat.ts +83 -0
  288. package/src/native-harness/src/interfaces/cli/development.ts +145 -0
  289. package/src/native-harness/src/interfaces/cli/format-reply.ts +27 -0
  290. package/src/native-harness/src/interfaces/cli/listeners.ts +74 -0
  291. package/src/native-harness/src/interfaces/cli/main.ts +47 -0
  292. package/src/native-harness/src/interfaces/cli/scenario.ts +37 -0
  293. package/src/native-harness/src/interfaces/cli/serve.ts +21 -0
  294. package/src/native-harness/src/interfaces/cli/start.ts +106 -0
  295. package/src/native-harness/src/interfaces/client.ts +225 -0
  296. package/src/native-harness/src/interfaces/inspector/index.html +283 -0
  297. package/src/native-harness/src/interfaces/inspector/observe.ts +35 -0
  298. package/src/native-harness/src/interfaces/inspector/server.ts +56 -0
  299. package/src/native-harness/src/interfaces/inspector/state.ts +92 -0
  300. package/src/native-harness/src/interfaces/protocol/channel-changes.ts +153 -0
  301. package/src/native-harness/src/interfaces/protocol/framing.ts +47 -0
  302. package/src/native-harness/src/interfaces/protocol/functions.ts +87 -0
  303. package/src/native-harness/src/interfaces/protocol/in-process.ts +67 -0
  304. package/src/native-harness/src/interfaces/protocol/live-queue.ts +55 -0
  305. package/src/native-harness/src/interfaces/protocol/load.ts +60 -0
  306. package/src/native-harness/src/interfaces/protocol/local.ts +120 -0
  307. package/src/native-harness/src/interfaces/protocol/operator.ts +120 -0
  308. package/src/native-harness/src/interfaces/protocol/output.ts +59 -0
  309. package/src/native-harness/src/interfaces/protocol/server.ts +517 -0
  310. package/src/native-harness/src/interfaces/protocol/stdio-client.ts +119 -0
  311. package/src/native-harness/src/interfaces/protocol/stdio-server.ts +82 -0
  312. package/src/native-harness/src/interfaces/protocol/subscribe.ts +41 -0
  313. package/src/native-harness/src/interfaces/protocol/transfers.ts +146 -0
  314. package/src/native-harness/src/interfaces/web-chat/index.html +94 -0
  315. package/src/native-harness/src/interfaces/web-chat/server.ts +34 -0
  316. package/src/native-harness/src/kernel/channels.ts +405 -0
  317. package/src/native-harness/src/kernel/loop.ts +45 -0
  318. package/src/native-harness/src/runtime/config/development.ts +23 -0
  319. package/src/native-harness/src/runtime/config/managed.ts +2 -0
  320. package/src/native-harness/src/runtime/config/paths.ts +27 -0
  321. package/src/native-harness/src/runtime/reminder-timer.ts +42 -0
  322. package/src/native-harness/src/runtime/reminder-validation.ts +104 -0
  323. package/src/native-harness/src/runtime/reminders.ts +66 -0
  324. package/src/native-harness/src/state/storage/archive.ts +239 -0
  325. package/src/native-harness/src/state/storage/engine.ts +1013 -0
  326. package/src/native-harness/src/state/storage/inference.ts +124 -0
  327. package/src/native-harness/src/state/storage/memory-reminders.ts +78 -0
  328. package/src/native-harness/src/state/storage/memory.ts +341 -0
  329. package/src/native-harness/src/state/storage/policy-metadata.ts +32 -0
  330. package/src/native-harness/src/state/storage/reminder-archive.ts +63 -0
  331. package/src/native-harness/src/state/storage/reminder-change.ts +92 -0
  332. package/src/native-harness/src/state/storage/reminder-tables.ts +27 -0
  333. package/src/native-harness/src/state/storage/retrieval.ts +122 -0
  334. package/src/native-harness/src/state/storage/script-identity.ts +7 -0
  335. package/src/native-harness/src/state/storage/session-file.ts +85 -0
  336. package/src/native-harness/src/state/storage/sqlite-reminders.ts +134 -0
  337. package/src/native-harness/src/state/storage/sqlite.ts +625 -0
  338. package/src/native-harness/src/testing.ts +190 -0
  339. package/src/native-harness/src/util/config.ts +58 -0
  340. package/src/native-harness/src/util/json.ts +41 -0
  341. package/src/native-harness/src/util/message-position.ts +3 -0
  342. package/src/native-harness/src/util/reminder-schedule.ts +44 -0
  343. package/src/native-harness/src/util/schema.ts +40 -0
  344. package/src/native-harness/src/util/timeline-text.ts +72 -0
  345. package/src/native-harness/src/util/timeline-tool-names.ts +3 -0
  346. package/src/native-harness/test/agent.test.ts +130 -0
  347. package/src/native-harness/test/approval-gate.test.ts +255 -0
  348. package/src/native-harness/test/approval-protocol.test.ts +94 -0
  349. package/src/native-harness/test/boundaries.test.ts +18 -0
  350. package/src/native-harness/test/channel-controls.test.ts +322 -0
  351. package/src/native-harness/test/client-abort.test.ts +136 -0
  352. package/src/native-harness/test/config-policy.test.ts +301 -0
  353. package/src/native-harness/test/conformance/storage.ts +1141 -0
  354. package/src/native-harness/test/context.test.ts +177 -0
  355. package/src/native-harness/test/crash.test.ts +187 -0
  356. package/src/native-harness/test/debug-tools.test.ts +67 -0
  357. package/src/native-harness/test/development-cli.test.ts +67 -0
  358. package/src/native-harness/test/development.test.ts +171 -0
  359. package/src/native-harness/test/dispatch-evidence.test.ts +58 -0
  360. package/src/native-harness/test/e2e.test.ts +186 -0
  361. package/src/native-harness/test/external-notices.test.ts +101 -0
  362. package/src/native-harness/test/fixtures/approval-e2e.jsonl +4 -0
  363. package/src/native-harness/test/fixtures/crash-scenario.jsonl +5 -0
  364. package/src/native-harness/test/fixtures/development-lifecycle.jsonl +10 -0
  365. package/src/native-harness/test/fixtures/echo-scenario.jsonl +3 -0
  366. package/src/native-harness/test/fixtures/format2-schema.ts +27 -0
  367. package/src/native-harness/test/fixtures/format3-schema.ts +16 -0
  368. package/src/native-harness/test/fixtures/hold-scenario.jsonl +4 -0
  369. package/src/native-harness/test/fixtures/model-e2e.jsonl +3 -0
  370. package/src/native-harness/test/fixtures/storage-owner.ts +20 -0
  371. package/src/native-harness/test/format-reply.test.ts +10 -0
  372. package/src/native-harness/test/helpers/operator-view.ts +41 -0
  373. package/src/native-harness/test/helpers/scenario.ts +9 -0
  374. package/src/native-harness/test/helpers/session.ts +38 -0
  375. package/src/native-harness/test/helpers/stdio.ts +46 -0
  376. package/src/native-harness/test/inspector.test.ts +77 -0
  377. package/src/native-harness/test/listeners.test.ts +49 -0
  378. package/src/native-harness/test/loop.test.ts +88 -0
  379. package/src/native-harness/test/model-channel-double.test.ts +87 -0
  380. package/src/native-harness/test/model-crash.test.ts +62 -0
  381. package/src/native-harness/test/model-delivery-regressions.test.ts +55 -0
  382. package/src/native-harness/test/model-e2e.test.ts +71 -0
  383. package/src/native-harness/test/model-final-filter.test.ts +30 -0
  384. package/src/native-harness/test/model-large-reply.test.ts +28 -0
  385. package/src/native-harness/test/model-live-queue.test.ts +33 -0
  386. package/src/native-harness/test/model-runner.test.ts +117 -0
  387. package/src/native-harness/test/model-storage.test.ts +99 -0
  388. package/src/native-harness/test/model-stream-adapter.test.ts +26 -0
  389. package/src/native-harness/test/model-stream-public.test.ts +77 -0
  390. package/src/native-harness/test/notices.test.ts +64 -0
  391. package/src/native-harness/test/openrouter-config.test.ts +218 -0
  392. package/src/native-harness/test/operator-controls.test.ts +132 -0
  393. package/src/native-harness/test/output.test.ts +44 -0
  394. package/src/native-harness/test/owner-config.test.ts +64 -0
  395. package/src/native-harness/test/permissions-e2e.test.ts +68 -0
  396. package/src/native-harness/test/persistence.test.ts +236 -0
  397. package/src/native-harness/test/portability.test.ts +337 -0
  398. package/src/native-harness/test/protocol.test.ts +160 -0
  399. package/src/native-harness/test/reminder-clock.test.ts +61 -0
  400. package/src/native-harness/test/reminder-stdio.test.ts +140 -0
  401. package/src/native-harness/test/reminder-storage.test.ts +124 -0
  402. package/src/native-harness/test/scenario-jsonl.test.ts +112 -0
  403. package/src/native-harness/test/scripted.test.ts +92 -0
  404. package/src/native-harness/test/session-file.test.ts +51 -0
  405. package/src/native-harness/test/startup-channel-changes.test.ts +256 -0
  406. package/src/native-harness/test/stdio.test.ts +132 -0
  407. package/src/native-harness/test/storage-memory.test.ts +4 -0
  408. package/src/native-harness/test/storage-sqlite.test.ts +420 -0
  409. package/src/native-harness/test/suspension.test.ts +430 -0
  410. package/src/native-harness/test/timeline-retrieval.test.ts +211 -0
  411. package/src/native-harness/test/tools-catalog.test.ts +468 -0
  412. package/src/native-harness/test/tools-protocol.test.ts +418 -0
  413. package/src/native-harness/test/transfers.test.ts +94 -0
  414. package/src/native-harness/test/web-chat.test.ts +35 -0
  415. package/src/native-harness/tsconfig.json +20 -0
  416. package/src/observation/agent-event-bus.ts +44 -18
  417. package/src/observation/index.ts +1 -0
  418. package/src/operations/contracts.ts +11 -3
  419. package/src/operations/errors.ts +5 -0
  420. package/src/operations/stale-policy.ts +1 -1
  421. package/src/operations/supervisor.ts +34 -10
  422. package/src/orchestrator/acp-delivery.ts +68 -0
  423. package/src/orchestrator/assignments.ts +27 -0
  424. package/src/orchestrator/busy.ts +15 -0
  425. package/src/orchestrator/config.ts +41 -0
  426. package/src/orchestrator/consumer-api.ts +24 -0
  427. package/src/orchestrator/contracts.ts +71 -0
  428. package/src/orchestrator/durable-assignments.ts +111 -0
  429. package/src/orchestrator/native-delegation.ts +103 -0
  430. package/src/orchestrator/orchestrator.ts +318 -0
  431. package/src/orchestrator/permissions.ts +25 -0
  432. package/src/orchestrator/policy.ts +44 -0
  433. package/src/orchestrator/pool.ts +123 -0
  434. package/src/orchestrator/tool-descriptions.ts +11 -0
  435. package/src/permissions/acp.ts +18 -0
  436. package/src/permissions/arrival.ts +13 -0
  437. package/src/permissions/consumer-config.ts +19 -0
  438. package/src/permissions/conversation.ts +280 -0
  439. package/src/permissions/matcher.ts +85 -0
  440. package/src/permissions/outcome.ts +15 -0
  441. package/src/permissions/policy.ts +81 -0
  442. package/src/permissions/request.ts +26 -0
  443. package/src/persistence/activity-projection.ts +1 -0
  444. package/src/persistence/backend.ts +34 -1
  445. package/src/persistence/channels.ts +23 -0
  446. package/src/persistence/index.ts +4 -0
  447. package/src/persistence/protocol-method.ts +5 -0
  448. package/src/persistence/reminder-wake.ts +49 -0
  449. package/src/persistence/reminders.ts +385 -0
  450. package/src/persistence/sqlite-backend.ts +84 -6
  451. package/src/persistence/sqlite-channels.ts +65 -0
  452. package/src/persistence/sqlite-reminders.ts +85 -0
  453. package/src/persistence/sqlite-schema.ts +108 -3
  454. package/src/persistence/sqlite-worker-delivery.ts +374 -0
  455. package/src/persistence/sqlite-workers.ts +59 -0
  456. package/src/persistence/worker-delivery-contract.ts +104 -0
  457. package/src/persistence/worker-terminal-reason.ts +29 -0
  458. package/src/persistence/workers.ts +19 -0
  459. package/src/provider-notices.ts +137 -0
  460. package/src/repl/commands.ts +2 -2
  461. package/src/steering/combine.ts +3 -1
  462. package/src/steering/contracts.ts +11 -2
  463. package/src/steering/controller.ts +3 -2
  464. package/src/transport/acp-transport.ts +24 -14
  465. package/src/types.ts +15 -12
  466. package/src/updates.ts +14 -2
  467. package/src/wire-log.ts +1 -1
@@ -0,0 +1,553 @@
1
+ import { mkdtemp, rm } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { createAgentConversationId, createAgentSessionId, type AgentConversationId, type AgentSessionId } from "../../agent-ids.ts";
5
+ import { Agent } from "../../agent.ts";
6
+ import { AgentConversation } from "../../agent-conversation.ts";
7
+ import { protectPendingRetirement } from "../../agent/native-channel-lifecycle.ts";
8
+ import { SqliteAgentPersistence } from "../../persistence/sqlite-backend.ts";
9
+ import { deriveReminderWake } from "../../persistence/reminder-wake.ts";
10
+ import { nativeRig } from "../../../test/native/support.ts";
11
+ import { expect, test } from "bun:test";
12
+ import { Database } from "bun:sqlite";
13
+ import { createMemoryReminderPersistence } from "../../persistence/reminders.ts";
14
+ import { createSqliteReminderPersistence } from "../../persistence/sqlite-reminders.ts";
15
+ import { migrateSqliteAgentSchema } from "../../persistence/sqlite-schema.ts";
16
+ import { normalizeReminderSchedule, reminderExpiryPolicy, resolveReminderExpiry } from "./schedule.ts";
17
+ import { ReminderService, reminderRequest, type ReminderEvent } from "./service.ts";
18
+
19
+ function setup() {
20
+ const database = new Database(":memory:");
21
+ database.exec("PRAGMA foreign_keys = ON");
22
+ migrateSqliteAgentSchema(database);
23
+ const run = async <T>(work: () => T) => work();
24
+ const repository = createSqliteReminderPersistence(database, run, run);
25
+ let now = 1_000_000;
26
+ const service = new ReminderService({ repository, now: () => now });
27
+ const channel = service.conversation("channel", true, () => {});
28
+ return { database, repository, channel, advance: () => { now += 10_000; } };
29
+ }
30
+
31
+ test("exact lead survives a possibly sent create and retry", async () => {
32
+ const { database, repository, channel, advance } = setup();
33
+ try {
34
+ const item = await channel.create({ text: "deploy", schedule: { in: 120_000 } });
35
+ const first = (await repository.markMaybeSent(item.id, 1))!;
36
+ expect(first.acceptedAt).toBe(1_000_000);
37
+ expect(await repository.wake()).toEqual({ reported: null, effectiveAt: 1_120_000 });
38
+ advance();
39
+ await channel.update(item.id, { text: "check deploy" });
40
+ const replay = (await repository.markMaybeSent(item.id, 1))!;
41
+ expect(JSON.stringify(reminderRequest(replay))).toBe(JSON.stringify(reminderRequest(first)));
42
+ const second = (await repository.markMaybeSent(item.id, 2))!;
43
+ expect(second.op).toBe("update");
44
+ await repository.acknowledge(item.id, 1, { confirmed: first.reminder });
45
+ expect((await repository.list())[0]?.changes.map(({ state }) => state)).toEqual(["applied", "maybe_sent"]);
46
+ await repository.acknowledge(item.id, 2, { confirmed: second.reminder });
47
+ expect((await repository.list())[0]?.changes).toEqual([]);
48
+ await channel.pause(item.id);
49
+ expect((await repository.list())[0]?.changes[0]?.changeId).toBe(3);
50
+ } finally {
51
+ database.close();
52
+ }
53
+ });
54
+
55
+ test("suspension removes local validation wakes and resuming restores them", async () => {
56
+ const { database, repository, channel } = setup();
57
+ try {
58
+ const item = await channel.create({ text: "once", schedule: { in: 180_000 } });
59
+ const [row] = await repository.list();
60
+ expect(item.expiry).toEqual({ kind: "absolute", at: 1_540_000, graceMs: 360_000 });
61
+ expect(reminderRequest(row!.changes[0]!).payload).not.toHaveProperty("expiresAfter");
62
+ await repository.setConversationSuspended("channel", true);
63
+ expect((await repository.wake()).effectiveAt).toBeNull();
64
+ await repository.setConversationSuspended("channel", false);
65
+ expect((await repository.wake()).effectiveAt).toBe(1_120_000);
66
+ await repository.deleteConversation("channel");
67
+ expect((await repository.wake()).effectiveAt).toBeNull();
68
+ expect(await channel.list()).toEqual([]);
69
+ } finally {
70
+ database.close();
71
+ }
72
+ });
73
+
74
+ test("only a new session replaces the deadline fence", async () => {
75
+ const { database, repository } = setup();
76
+ try {
77
+ const deadline = { sessionId: "one", revision: 10, nextDueAt: 42 };
78
+ expect(await repository.updateDeadline(deadline)).toBe("accepted");
79
+ expect(await repository.updateDeadline({ ...deadline, revision: 9 })).toBe("stale");
80
+ expect(await repository.updateDeadline({ ...deadline, nextDueAt: 43 })).toBe("mismatch");
81
+ expect((await repository.wake()).effectiveAt).toBe(42);
82
+ const replacement = { sessionId: "two", revision: 1, nextDueAt: null };
83
+ expect(await repository.updateDeadline(replacement)).toBe("stale");
84
+ expect(await repository.updateDeadline(replacement, true)).toBe("accepted");
85
+ expect(await repository.updateDeadline(deadline)).toBe("stale");
86
+ expect((await repository.wake()).effectiveAt).toBeNull();
87
+ } finally {
88
+ database.close();
89
+ }
90
+ });
91
+
92
+ test("SQLite upgrades a populated schema 21 database in place", async () => {
93
+ const directory = await mkdtemp(join(tmpdir(), "reminder-v21-"));
94
+ const path = join(directory, "agent.sqlite");
95
+ const agentSessionId = "ags-7f45e629-3a70-4668-93c6-4e88e922c1dd" as AgentSessionId;
96
+ const id = "agc-8a7f0a36-5972-4bb1-b64b-9751a490b61b" as AgentConversationId;
97
+ try {
98
+ // This file was created and populated using the backend from f0eb7d8.
99
+ const fixture = Bun.file(new URL("./fixtures/schema21.sqlite.gz", import.meta.url));
100
+ await Bun.write(path, Bun.gunzipSync(new Uint8Array(await fixture.arrayBuffer())));
101
+ {
102
+ using database = new Database(path);
103
+ expect(database.query("SELECT version FROM schema_migrations").all()).toEqual([{ version: 21 }]);
104
+ expect(database.query("SELECT name FROM sqlite_master WHERE name = 'reminder_changes'").get()).toBeNull();
105
+ }
106
+ const backend = await SqliteAgentPersistence.open({ path, agentSessionId });
107
+ try {
108
+ expect(await backend.conversations.get(id)).toMatchObject({ title: "Existing schema 21 conversation", controllerKind: "native" });
109
+ expect(await backend.reminders.list()).toEqual([]);
110
+ using database = new Database(path);
111
+ expect(database.query("SELECT version FROM schema_migrations ORDER BY version").all())
112
+ .toEqual([{ version: 21 }, { version: 22 }]);
113
+ } finally {
114
+ await backend.close();
115
+ }
116
+ } finally {
117
+ await rm(directory, { recursive: true, force: true });
118
+ }
119
+ });
120
+
121
+ test("lead, repeat and grace boundaries", () => {
122
+ expect(() => normalizeReminderSchedule({ in: 119_999 }, 0)).toThrow();
123
+ expect(normalizeReminderSchedule({ in: 120_000 }, 0)).toEqual({ at: 120_000 });
124
+ expect(() => normalizeReminderSchedule({ every: 599_999 }, 0)).toThrow();
125
+ expect(normalizeReminderSchedule({ every: 600_000 }, 0)).toEqual({ every: 600_000, startAt: 600_000 });
126
+ expect(resolveReminderExpiry({ at: 120_000 }, { kind: "default" }, 0)).toEqual({
127
+ kind: "absolute", at: 420_000, graceMs: 300_000,
128
+ });
129
+ expect(resolveReminderExpiry({ at: 86_400_000 }, { kind: "default" }, 0)).toEqual({
130
+ kind: "absolute", at: 172_800_000, graceMs: 86_400_000,
131
+ });
132
+ });
133
+
134
+ test("memory allows live confirmation and rejects offline create", async () => {
135
+ const repository = createMemoryReminderPersistence();
136
+ let running = false;
137
+ const service = new ReminderService({
138
+ repository, running: () => running, now: () => 0,
139
+ live: async () => ({ confirmed: (await repository.list())[0]!.changes[0]!.reminder }),
140
+ });
141
+ const channel = service.conversation("channel", true, () => {});
142
+ const input = { text: "check", schedule: { in: 120_000 } };
143
+ await expect(channel.create(input)).rejects.toMatchObject({ code: "reminders_unavailable" });
144
+ running = true;
145
+ expect((await channel.create(input)).state).toBe("confirmed");
146
+ });
147
+
148
+ test("deleting a conversation with a reminders-only backend removes its wake in the transaction", async () => {
149
+ const { database, repository, channel } = setup();
150
+ try {
151
+ const { deleteConversationWithChannel } = await import("../../agent/native-channel-lifecycle.ts");
152
+ await channel.create({ text: "deploy", schedule: { in: 120_000 } });
153
+ let transaction = false;
154
+ let deleted = false;
155
+ const scope = {
156
+ reminders: repository,
157
+ conversations: {
158
+ get: async () => ({ controllerKind: "native" }),
159
+ delete: async () => {
160
+ expect(transaction).toBe(true);
161
+ expect((await repository.wake()).effectiveAt).toBeNull();
162
+ deleted = true;
163
+ },
164
+ },
165
+ };
166
+ const persistence = {
167
+ ...scope,
168
+ capabilities: { channels: false, reminders: true },
169
+ transaction: async (work: (scoped: typeof scope) => Promise<unknown>) => {
170
+ transaction = true;
171
+ return work(scope);
172
+ },
173
+ };
174
+ await deleteConversationWithChannel(
175
+ { persistence, nativeHarness: null } as unknown as Parameters<typeof deleteConversationWithChannel>[0],
176
+ "channel" as AgentConversationId,
177
+ { providerSession: "keep" } as unknown as Parameters<typeof deleteConversationWithChannel>[2],
178
+ );
179
+ expect(deleted).toBe(true);
180
+ expect(await repository.list()).toEqual([]);
181
+ } finally {
182
+ database.close();
183
+ }
184
+ });
185
+
186
+ test("never-sent edits coalesce and a cancelled create never reuses a version", async () => {
187
+ const { database, repository, channel } = setup();
188
+ try {
189
+ const item = await channel.create({ text: "first", schedule: { in: 120_000 } });
190
+ await channel.update(item.id, { text: "second" });
191
+ expect((await repository.list())[0]?.changes.map(({ op, changeId }) => [op, changeId])).toEqual([["create", 2]]);
192
+ await channel.cancel(item.id);
193
+ expect(await channel.list()).toEqual([]);
194
+ expect((await repository.list())[0]?.highWater).toBe(3);
195
+ expect((await repository.wake()).effectiveAt).toBeNull();
196
+ } finally {
197
+ database.close();
198
+ }
199
+ });
200
+
201
+ test("schedule edits preserve expiry policy and resume preserves an overdue schedule", async () => {
202
+ const { database, channel, advance } = setup();
203
+ try {
204
+ const item = await channel.create({ text: "check", schedule: { in: 120_000 }, expiresAfter: 300_000 });
205
+ const moved = await channel.update(item.id, { schedule: { in: 130_000 } });
206
+ expect(moved.expiry).toEqual({ kind: "absolute", at: 1_430_000, graceMs: 300_000 });
207
+ for (let step = 0; step < 15; step++) advance();
208
+ await channel.pause(item.id);
209
+ const resumed = await channel.resume(item.id);
210
+ expect(resumed.schedule).toEqual(moved.schedule);
211
+ expect(resumed.expiry).toEqual(moved.expiry);
212
+ } finally {
213
+ database.close();
214
+ }
215
+ });
216
+
217
+ test("blocked entries stay visible and correcting or discarding them preserves the clock", async () => {
218
+ const { database, repository, channel } = setup();
219
+ try {
220
+ const item = await channel.create({ text: "check", schedule: { in: 120_000 } });
221
+ await repository.markMaybeSent(item.id, 1);
222
+ await repository.acknowledge(item.id, 1, { reason: "invalid_policy" });
223
+ expect(await channel.get(item.id)).toMatchObject({ state: "blocked", reason: "invalid_policy" });
224
+ expect((await repository.wake()).effectiveAt).toBeNull();
225
+ await expect(channel.update(item.id, { text: "corrected" })).rejects.toMatchObject({ code: "not_found" });
226
+ await channel.discardBlocked(item.id);
227
+ expect((await repository.list())[0]?.highWater).toBe(1);
228
+ expect(await channel.get(item.id)).toBeUndefined();
229
+ const corrected = await channel.create({ text: "corrected", schedule: { in: 120_000 } });
230
+ expect(corrected.id).not.toBe(item.id);
231
+ } finally {
232
+ database.close();
233
+ }
234
+ });
235
+
236
+ test("authorization is checked before journaling and one-offs have no local cap", async () => {
237
+ const { database, repository } = setup();
238
+ try {
239
+ const service = new ReminderService({
240
+ repository,
241
+ now: () => 0,
242
+ authorize: (id) => id !== "denied",
243
+ });
244
+ const input = { text: "check", schedule: { in: 120_000 } };
245
+ await expect(service.conversation("denied", true, () => {}).create(input))
246
+ .rejects.toMatchObject({ code: "unauthorized" });
247
+ await expect(service.conversation("acp", false, () => {}).create(input))
248
+ .rejects.toMatchObject({ code: "reminders_unsupported" });
249
+ const channel = service.conversation("channel", true, () => {});
250
+ for (let i = 0; i < 20; i++) await channel.create(input);
251
+ await channel.create(input);
252
+ expect(await repository.list()).toHaveLength(21);
253
+ const first = await service.list({ limit: 13 });
254
+ const second = await service.list({ after: first.nextCursor!, limit: 13 });
255
+ expect(new Set([...first.items, ...second.items].map(({ id }) => id)).size).toBe(21);
256
+ expect(second.nextCursor).toBeNull();
257
+ } finally {
258
+ database.close();
259
+ }
260
+ });
261
+
262
+ test("a maybe-sent change still contributes after recomputing the wake", async () => {
263
+ const { database, repository, channel } = setup();
264
+ try {
265
+ const item = await channel.create({ text: "check", schedule: { in: 180_000 } });
266
+ await repository.markMaybeSent(item.id, 1);
267
+ await repository.updateDeadline({ sessionId: "one", revision: 1, nextDueAt: null });
268
+ expect((await repository.wake()).effectiveAt).toBe(1_120_000);
269
+ } finally {
270
+ database.close();
271
+ }
272
+ });
273
+
274
+ test("a paused change has no validation wake and resume restores it", async () => {
275
+ const { database, repository, channel } = setup();
276
+ try {
277
+ const item = await channel.create({ text: "check", schedule: { in: 180_000 } });
278
+ await channel.pause(item.id);
279
+ expect((await repository.wake()).effectiveAt).toBeNull();
280
+ await channel.resume(item.id);
281
+ expect((await repository.wake()).effectiveAt).toBe(1_120_000);
282
+ } finally {
283
+ database.close();
284
+ }
285
+ });
286
+
287
+ test("validation wakes use the earlier of launch deadline and due time", () => {
288
+ const change = {
289
+ op: "update", state: "pending", acceptedAt: 200_000,
290
+ paused: false, suspended: false, retiring: false,
291
+ } as const;
292
+ expect(deriveReminderWake(null, [{ ...change, dueAt: 210_000 }], 120_000)).toBe(210_000);
293
+ expect(deriveReminderWake(null, [{ ...change, dueAt: 400_000 }], 120_000)).toBe(320_000);
294
+ });
295
+
296
+ test("text edits, pause and resume do not drift default expiry", async () => {
297
+ const { database, channel, advance } = setup();
298
+ try {
299
+ const item = await channel.create({ text: "check", schedule: { in: 600_000 } });
300
+ expect(item.expiry).toEqual({ kind: "absolute", at: 2_800_000, graceMs: 1_200_000 });
301
+ advance();
302
+ expect((await channel.update(item.id, { text: "edited" })).expiry).toEqual(item.expiry);
303
+ advance();
304
+ expect((await channel.pause(item.id)).expiry).toEqual(item.expiry);
305
+ advance();
306
+ expect((await channel.resume(item.id)).expiry).toEqual(item.expiry);
307
+ } finally {
308
+ database.close();
309
+ }
310
+ });
311
+
312
+ test("explicit repeating anchors and expiry lengths enforce acceptance boundaries", () => {
313
+ for (const startAt of [99_999, 100_000, 699_999]) {
314
+ expect(() => normalizeReminderSchedule({ every: 600_000, startAt }, 100_000))
315
+ .toThrow(expect.objectContaining({ code: "invalid_schedule" }));
316
+ }
317
+ expect(normalizeReminderSchedule({ every: 600_000, startAt: 700_000 }, 100_000))
318
+ .toEqual({ every: 600_000, startAt: 700_000 });
319
+ for (const duration of [-1, 0]) {
320
+ expect(() => reminderExpiryPolicy(duration)).toThrow(expect.objectContaining({ code: "invalid_input" }));
321
+ }
322
+ expect(() => reminderExpiryPolicy(299_999)).toThrow();
323
+ expect(reminderExpiryPolicy(300_000)).toEqual({ kind: "duration", milliseconds: 300_000 });
324
+ expect(() => reminderExpiryPolicy(86_400_001)).toThrow();
325
+ });
326
+
327
+ test("terminal reminders reject changes and accept a repeat cancel without writing", async () => {
328
+ const { database, repository, channel } = setup();
329
+ try {
330
+ for (const status of ["cancelled", "completed", "expired"] as const) {
331
+ const item = await channel.create({ text: "check", schedule: { in: 180_000 } });
332
+ await repository.markMaybeSent(item.id, 1);
333
+ await repository.acknowledge(item.id, 1, { confirmed: { ...item, status } });
334
+ for (const mutate of [channel.pause, channel.resume, (id: string) => channel.update(id, { text: "edited" })]) {
335
+ await expect(mutate(item.id)).rejects.toMatchObject({ code: "conflict" });
336
+ await expect(mutate("missing")).rejects.toMatchObject({ code: "not_found" });
337
+ }
338
+ const before = await repository.list();
339
+ expect(await channel.cancel(item.id)).toMatchObject({ status, state: "confirmed" });
340
+ expect(await repository.list()).toEqual(before);
341
+ }
342
+ } finally {
343
+ database.close();
344
+ }
345
+ });
346
+
347
+ async function withSqlite(work: (backend: SqliteAgentPersistence, path: string) => Promise<void>) {
348
+ const directory = await mkdtemp(join(tmpdir(), "reminder-sqlite-"));
349
+ const path = join(directory, "agent.sqlite");
350
+ try {
351
+ const backend = await SqliteAgentPersistence.open({ path, agentSessionId: createAgentSessionId() });
352
+ try {
353
+ await work(backend, path);
354
+ } finally {
355
+ await backend.close();
356
+ }
357
+ } finally {
358
+ await rm(directory, { recursive: true, force: true });
359
+ }
360
+ }
361
+
362
+ test("SQLite channel desire sets the suspension fence in its transaction", async () => {
363
+ await withSqlite(async (backend, path) => {
364
+ const service = new ReminderService({ persistence: backend, now: () => 0 });
365
+ await service.conversation("channel", true, () => {}).create({ text: "check", schedule: { in: 180_000 } });
366
+ const channels = protectPendingRetirement(backend, backend.channels);
367
+ using database = new Database(path);
368
+ database.exec(`CREATE TRIGGER reject_suspension BEFORE INSERT ON reminder_suspensions
369
+ BEGIN SELECT RAISE(ABORT, 'suspension rejected'); END`);
370
+ await expect(channels.desire("channel", { lifecycle: "suspended" })).rejects.toThrow("suspension rejected");
371
+ expect(await channels.pendingChange("channel")).toBeUndefined();
372
+ database.exec("DROP TRIGGER reject_suspension");
373
+ await channels.desire("channel", { lifecycle: "suspended" });
374
+ expect((await backend.reminders.wake()).effectiveAt).toBeNull();
375
+ await channels.desire("channel", { lifecycle: "open" });
376
+ expect((await backend.reminders.wake()).effectiveAt).toBe(120_000);
377
+ });
378
+ });
379
+
380
+ test("SQLite channel confirmation sets the suspension fence in its transaction", async () => {
381
+ await withSqlite(async (backend, path) => {
382
+ const service = new ReminderService({ persistence: backend, now: () => 0 });
383
+ await service.conversation("channel", true, () => {}).create({ text: "check", schedule: { in: 180_000 } });
384
+ const channels = protectPendingRetirement(backend, backend.channels);
385
+ const pending = await backend.channels.desire("channel", { lifecycle: "suspended" });
386
+ using database = new Database(path);
387
+ database.exec(`CREATE TRIGGER reject_suspension BEFORE INSERT ON reminder_suspensions
388
+ BEGIN SELECT RAISE(ABORT, 'suspension rejected'); END`);
389
+ await expect(channels.confirmChange("channel", pending.version)).rejects.toThrow("suspension rejected");
390
+ expect(await channels.pendingChange("channel")).toEqual(pending);
391
+ database.exec("DROP TRIGGER reject_suspension");
392
+ expect(await channels.confirmChange("channel", pending.version)).toBe(true);
393
+ expect((await backend.reminders.wake()).effectiveAt).toBeNull();
394
+ const resumed = await backend.channels.desire("channel", { lifecycle: "open" });
395
+ expect(await channels.confirmChange("channel", resumed.version)).toBe(true);
396
+ expect((await backend.reminders.wake()).effectiveAt).toBe(120_000);
397
+ });
398
+ });
399
+
400
+ test("SQLite rolls back reminder deletion and wake changes when conversation deletion fails", async () => {
401
+ await withSqlite(async (backend, path) => {
402
+ const { deleteConversationWithChannel } = await import("../../agent/native-channel-lifecycle.ts");
403
+ const id = createAgentConversationId();
404
+ await backend.conversations.create({
405
+ agentSessionId: backend.identityRecord.agentSessionId, agentConversationId: id, controllerKind: "native",
406
+ });
407
+ const channel = new ReminderService({ persistence: backend, now: () => 0 }).conversation(id, true, () => {});
408
+ const first = await channel.create({ text: "confirmed", schedule: { in: 180_000 } });
409
+ await backend.reminders.markMaybeSent(first.id, 1);
410
+ await backend.reminders.acknowledge(first.id, 1, { confirmed: first });
411
+ await channel.create({ text: "pending", schedule: { in: 180_000 } });
412
+ const before = await backend.reminders.list();
413
+ using database = new Database(path);
414
+ database.exec(`CREATE TRIGGER reject_conversation_delete BEFORE UPDATE OF deleted_at ON conversations
415
+ BEGIN SELECT RAISE(ABORT, 'conversation deletion rejected'); END`);
416
+ const agent = { persistence: backend, nativeHarness: null } as unknown as Agent;
417
+ await expect(deleteConversationWithChannel(agent, id, {})).rejects.toThrow("conversation deletion rejected");
418
+ expect((await backend.conversations.get(id))?.deletedAt).toBeUndefined();
419
+ expect(await backend.channels.pendingChange(id)).toBeUndefined();
420
+ expect(await backend.reminders.list()).toEqual(before);
421
+ expect((await backend.reminders.wake()).effectiveAt).toBe(120_000);
422
+ database.exec("DROP TRIGGER reject_conversation_delete");
423
+ await deleteConversationWithChannel(agent, id, { deletedAt: "2026-01-01T00:00:00.000Z" });
424
+ expect(await backend.conversations.get(id)).toMatchObject({ deletedAt: "2026-01-01T00:00:00.000Z" });
425
+ expect(await backend.reminders.list()).toEqual([]);
426
+ expect((await backend.reminders.wake()).effectiveAt).toBeNull();
427
+ });
428
+ });
429
+
430
+ test("reminder events reach observers after the journal commits", async () => {
431
+ await withSqlite(async (backend, path) => {
432
+ using observer = new Database(path, { readonly: true });
433
+ const events: Array<{ kind: ReminderEvent["kind"]; status: string; stored: string | undefined }> = [];
434
+ let block = false;
435
+ const service = new ReminderService({
436
+ persistence: backend, running: () => true, now: () => 0,
437
+ live: async ({ reminderId, changeId }) => {
438
+ if (block) return { reason: "invalid_policy" };
439
+ const journal = (await backend.reminders.list()).find(({ id }) => id === reminderId);
440
+ return { confirmed: journal!.changes.find((change) => change.changeId === changeId)!.reminder };
441
+ },
442
+ });
443
+ const channel = service.conversation("channel", true, ({ kind, reminder }) => {
444
+ const change = observer.query<{ value: string }, [string]>(
445
+ "SELECT value FROM reminder_changes WHERE id = ? ORDER BY change_id DESC LIMIT 1",
446
+ ).get(reminder.id);
447
+ const cache = observer.query<{ value: string }, [string]>("SELECT value FROM reminder_cache WHERE id = ?").get(reminder.id);
448
+ events.push({ kind, status: reminder.status, stored: change ? JSON.parse(change.value).state : cache && JSON.parse(cache.value).status });
449
+ });
450
+ const item = await channel.create({ text: "check", schedule: { in: 180_000 } });
451
+ block = true;
452
+ await channel.update(item.id, { text: "edited" });
453
+ block = false;
454
+ await channel.cancel(item.id);
455
+ expect(events).toEqual([
456
+ { kind: "reminder_intent_recorded", status: "active", stored: "maybe_sent" },
457
+ { kind: "reminder_intent_confirmed", status: "active", stored: "active" },
458
+ { kind: "reminder_intent_recorded", status: "active", stored: "maybe_sent" },
459
+ { kind: "reminder_intent_blocked", status: "active", stored: "blocked" },
460
+ { kind: "reminder_intent_recorded", status: "cancelled", stored: "maybe_sent" },
461
+ { kind: "reminder_intent_confirmed", status: "cancelled", stored: "cancelled" },
462
+ ]);
463
+ });
464
+ });
465
+
466
+ test("owned native and ACP conversations expose their reminder support and share agent state", async () => {
467
+ const rig = await nativeRig();
468
+ try {
469
+ const agent = await rig.open({
470
+ adapters: { main: { kind: "native" }, acp: { command: ["unused"] } },
471
+ conversationDefaults: { providerStart: "on_first_use" },
472
+ });
473
+ const native = await agent.conversations.create();
474
+ const acp = await agent.conversations.create({ adapterId: "acp" });
475
+ const events: string[] = [];
476
+ native.subscribe(({ event }) => events.push(event.kind));
477
+ expect(native.conversationCapabilities.reminders).toBe(true);
478
+ const input = { text: "check", schedule: { in: 180_000 } };
479
+ const item = await native.reminders.create(input);
480
+ expect(await native.reminders.get(item.id)).toEqual(item);
481
+ expect((await agent.reminders.list()).items).toContainEqual(item);
482
+ expect(events).toContain("reminder_intent_recorded");
483
+ expect(acp.conversationCapabilities.reminders).toBe(false);
484
+ await expect(acp.reminders.create(input)).rejects.toMatchObject({ code: "reminders_unsupported" });
485
+ expect(rig.launches).toEqual([]);
486
+ } finally {
487
+ await rig.clean();
488
+ }
489
+ });
490
+
491
+ test("a native conversation without an owner rejects reminder access with a typed error", async () => {
492
+ const rig = await nativeRig();
493
+ try {
494
+ const agent = await rig.open();
495
+ const id = createAgentConversationId();
496
+ const conversation = await AgentConversation.create({
497
+ adapter: { command: ["unused"] },
498
+ adapterId: "native",
499
+ agentConversationId: id,
500
+ native: { host: agent.nativeHarness!, channelId: id },
501
+ });
502
+ try {
503
+ expect(conversation.conversationCapabilities.reminders).toBe(true);
504
+ expect(() => conversation.reminders).toThrow(expect.objectContaining({ code: "reminders_unavailable" }));
505
+ } finally {
506
+ await conversation.stop();
507
+ }
508
+ } finally {
509
+ await rig.clean();
510
+ }
511
+ });
512
+
513
+ test("an unreleased R7 schema 22 fails fast with a migration diagnostic", () => {
514
+ using database = new Database(":memory:");
515
+ database.exec(`CREATE TABLE schema_migrations(version INTEGER); INSERT INTO schema_migrations VALUES (22);
516
+ CREATE TABLE reminder_changes(id TEXT, change_id INTEGER, value TEXT);
517
+ CREATE TABLE reminder_cache(id TEXT, value TEXT)`);
518
+ expect(() => migrateSqliteAgentSchema(database)).toThrow("unreleased R7 build");
519
+ });
520
+
521
+ test("schema 22 reads a pre-routine one-off journal JSON row unchanged", async () => {
522
+ const { database, repository } = setup();
523
+ try {
524
+ database.query("INSERT INTO reminder_journals VALUES (?, ?, ?)").run("legacy", "channel", 1);
525
+ database.query("INSERT INTO reminder_changes VALUES (?, ?, ?, ?)").run("legacy", 1, 1, JSON.stringify({
526
+ op: "create", changeId: 1, stagingOrder: 1, acceptedAt: 0, state: "pending",
527
+ reminder: { id: "legacy", conversationId: "channel", text: "old", status: "active",
528
+ schedule: { at: 180_000 }, scheduleAcceptedAt: 0, expiry: { at: 540_000, graceMs: 360_000 },
529
+ expiryPolicy: { kind: "default" } },
530
+ }));
531
+ expect(database.query("SELECT MAX(version) AS version FROM schema_migrations").get()).toEqual({ version: 22 });
532
+ const [row] = await repository.list();
533
+ expect(reminderRequest(row!.changes[0]!)).toEqual({
534
+ reminderId: "legacy", changeId: 1, op: "create",
535
+ payload: { acceptedAt: new Date(0).toISOString(), channel: "channel", text: "old", schedule: { at: new Date(180_000).toISOString() } },
536
+ });
537
+ } finally { database.close(); }
538
+ });
539
+
540
+ test("memory exhausted discard is conversation-scoped and preserves change ID high water", async () => {
541
+ const repo = createMemoryReminderPersistence();
542
+ const reminder = { id: "frozen", conversationId: "owner", text: "old", schedule: { at: 180_000 },
543
+ status: "active", expiry: null, expiryPolicy: { kind: "unlimited" }, scheduleAcceptedAt: 0 } as const;
544
+ const change = (await repo.enqueue({ reminder, op: "create", acceptedAt: 0 }))!;
545
+ const entry = reminderRequest(change);
546
+ await repo.markMaybeSent(reminder.id, change.changeId, entry);
547
+ await repo.setStartupRetry({ sessionId: "session", entries: [entry], exhausted: true });
548
+ expect(await repo.discardExhausted(reminder.id, "other")).toBe(false);
549
+ expect((await repo.wake()).startupRetry?.entries).toEqual([entry]);
550
+ expect(await repo.discardExhausted(reminder.id, "owner")).toBe(true);
551
+ expect((await repo.list())[0]).toMatchObject({ highWater: change.changeId, changes: [] });
552
+ expect(await repo.enqueue({ reminder, op: "update", acceptedAt: 1 })).toMatchObject({ changeId: change.changeId + 1 });
553
+ });
@@ -0,0 +1,13 @@
1
+ import type { AgentConfig } from "../../agent/config.ts";
2
+ import type { ProtocolConfig } from "../../native-harness/src/client.ts";
3
+
4
+ type Config = NonNullable<AgentConfig["harness"]>["config"];
5
+ type PublicLimit = NonNullable<Config["limits"]>["unattendedTurns"];
6
+ type WireLimit = NonNullable<ProtocolConfig["limits"]>["unattendedTurns"];
7
+
8
+ export const numericLimit = 3 satisfies PublicLimit;
9
+ export const unlimitedLimit = "unlimited" satisfies PublicLimit;
10
+ // @ts-expect-error The public sentinel is case-sensitive.
11
+ export const wrongSpelling = "Unlimited" satisfies PublicLimit;
12
+ // @ts-expect-error The public sentinel cannot be sent on the wire.
13
+ export const wireSentinel = "unlimited" satisfies WireLimit;
@@ -0,0 +1,66 @@
1
+ import type { ReminderChange } from "../../persistence/reminders.ts";
2
+ import { HarnessIncompatibleError } from "../errors.ts";
3
+ import type { ReminderWireOpen, RoutineConfig, RoutineConfigInput } from "./routine-wire.ts";
4
+ import { isRoutineSchedule } from "./routine-schedule.ts";
5
+ import { ReminderError } from "./schedule.ts";
6
+
7
+ /**
8
+ * Executable startup stages the first column and holds the second.
9
+ *
10
+ * Schedule | Startup | Post-open live
11
+ * One-off | All changes | Overflow
12
+ * Routine | Cancel, pause, text, grace, missed/overlap | Create, resume, replacement
13
+ *
14
+ * Every change after a held entry for the same ID follows it live. A cancel
15
+ * can erase an entirely unsent create locally. Old-calendar work can run
16
+ * before a replacement, and a new routine can be due before its follow-up pause.
17
+ */
18
+ export function activatesRoutine(change: Pick<ReminderChange, "op" | "fields" | "reminder">): boolean {
19
+ return isRoutineSchedule(change.reminder.schedule) && (
20
+ change.op === "create" || (change.op === "update" &&
21
+ (change.fields?.includes("schedule") === true ||
22
+ (change.fields?.includes("status") === true && change.reminder.status === "active")))
23
+ );
24
+ }
25
+
26
+ export class RoutineAdmission {
27
+ advertised = false;
28
+ supported = false;
29
+ private effective?: number;
30
+ private bound?: number;
31
+
32
+ initialize(routines: unknown, unattendedTurnGuard: unknown, reminders: boolean): void {
33
+ this.advertised = reminders && routines === true;
34
+ this.supported = this.advertised && unattendedTurnGuard === true;
35
+ this.effective = undefined;
36
+ if (!this.supported) this.bound = undefined;
37
+ }
38
+
39
+ get available(): boolean { return this.supported && this.effective !== undefined; }
40
+
41
+ unavailable(): string | undefined {
42
+ if (!this.supported) return "The harness must advertise routines and unattendedTurnGuard";
43
+ if (this.effective === undefined) return "An executable open must confirm an enabled unattended-turn guard";
44
+ }
45
+
46
+ resetBinding(): void { this.bound = undefined; }
47
+
48
+ configForOpen(config: RoutineConfigInput | undefined): RoutineConfig | undefined {
49
+ if (!config) return;
50
+ const { limits, ...rest } = config;
51
+ const configured = limits?.unattendedTurns;
52
+ if (configured === undefined) return rest;
53
+ const value = configured === "unlimited" ? Number.MAX_SAFE_INTEGER : configured;
54
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) {
55
+ throw new ReminderError("invalid_config", 'unattendedTurns must be a positive safe integer or "unlimited"');
56
+ }
57
+ if (!this.supported) throw new HarnessIncompatibleError("The native harness must support routines and unattendedTurnGuard to configure unattendedTurns");
58
+ return this.bound === value ? rest : { ...rest, limits: { unattendedTurns: value } };
59
+ }
60
+
61
+ opened(opened: ReminderWireOpen): void {
62
+ const value = opened.unattendedTurnGuard?.maxConsecutiveTurns;
63
+ this.effective = this.supported && typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
64
+ this.bound = this.effective;
65
+ }
66
+ }
@@ -0,0 +1,25 @@
1
+ export type RoutineBound<Instant> =
2
+ | { readonly count: number; readonly until?: never }
3
+ | { readonly until: Instant; readonly count?: never }
4
+ | { readonly count?: never; readonly until?: never };
5
+
6
+ export type RoutineSchedule<Instant, Start = { readonly startAt?: Instant }> = RoutineBound<Instant> & (
7
+ | ({ readonly every: number } & Start)
8
+ | { readonly cron: string; readonly timezone: string }
9
+ );
10
+
11
+ export type MissedRuns = "coalesce" | "skip" | "each";
12
+ export type RoutineOverlap = "skip" | "queue";
13
+
14
+ export function isRoutineSchedule(schedule: unknown): schedule is RoutineSchedule<number> {
15
+ return !!schedule && typeof schedule === "object" && ("every" in schedule || "cron" in schedule);
16
+ }
17
+
18
+ export function hasRoutineCronShape(expression: string): boolean {
19
+ const fields = expression.trim().split(/\s+/);
20
+ return fields.length === 5 && fields.every(field => /^[\d*,\/-]+$/.test(field));
21
+ }
22
+
23
+ export function validInstant(value: unknown): value is number {
24
+ return typeof value === "number" && Number.isSafeInteger(value) && Math.abs(value) <= 8_640_000_000_000_000;
25
+ }