@namzu/sdk 61.4.0 → 62.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (338) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/dist/agents/QueryAgent.d.ts.map +1 -1
  3. package/dist/agents/QueryAgent.js +2 -0
  4. package/dist/agents/QueryAgent.js.map +1 -1
  5. package/dist/bridge/a2a/mapper.d.ts.map +1 -1
  6. package/dist/bridge/a2a/mapper.js +2 -0
  7. package/dist/bridge/a2a/mapper.js.map +1 -1
  8. package/dist/bridge/acp/permission.d.ts +8 -0
  9. package/dist/bridge/acp/permission.d.ts.map +1 -1
  10. package/dist/bridge/acp/permission.js.map +1 -1
  11. package/dist/bridge/acp/server.d.ts.map +1 -1
  12. package/dist/bridge/acp/server.js +8 -1
  13. package/dist/bridge/acp/server.js.map +1 -1
  14. package/dist/bridge/sse/mapper.d.ts.map +1 -1
  15. package/dist/bridge/sse/mapper.js +13 -0
  16. package/dist/bridge/sse/mapper.js.map +1 -1
  17. package/dist/constants/tools/index.d.ts +2 -0
  18. package/dist/constants/tools/index.d.ts.map +1 -1
  19. package/dist/constants/tools/index.js +23 -0
  20. package/dist/constants/tools/index.js.map +1 -1
  21. package/dist/contracts/session/api.d.ts +1 -1
  22. package/dist/contracts/session/api.d.ts.map +1 -1
  23. package/dist/eval/harness-protection.js +1 -1
  24. package/dist/eval/harness-protection.js.map +1 -1
  25. package/dist/learning/cycle.d.ts +5 -3
  26. package/dist/learning/cycle.d.ts.map +1 -1
  27. package/dist/learning/cycle.js +11 -13
  28. package/dist/learning/cycle.js.map +1 -1
  29. package/dist/learning/guidance.d.ts +74 -0
  30. package/dist/learning/guidance.d.ts.map +1 -0
  31. package/dist/learning/guidance.js +259 -0
  32. package/dist/learning/guidance.js.map +1 -0
  33. package/dist/learning/skill.d.ts +2 -2
  34. package/dist/learning/skill.d.ts.map +1 -1
  35. package/dist/learning/skill.js +6 -6
  36. package/dist/learning/skill.js.map +1 -1
  37. package/dist/learning/store.d.ts.map +1 -1
  38. package/dist/learning/store.js +6 -6
  39. package/dist/learning/store.js.map +1 -1
  40. package/dist/pals/communication/ingress-schema.d.ts +8 -8
  41. package/dist/pals/communication/schema.d.ts +2 -2
  42. package/dist/peers/client.d.ts +2 -3
  43. package/dist/peers/client.d.ts.map +1 -1
  44. package/dist/peers/client.js +2 -3
  45. package/dist/peers/client.js.map +1 -1
  46. package/dist/peers/endpoint.d.ts +1 -2
  47. package/dist/peers/endpoint.d.ts.map +1 -1
  48. package/dist/peers/endpoint.js +1 -2
  49. package/dist/peers/endpoint.js.map +1 -1
  50. package/dist/peers/protocol.d.ts.map +1 -1
  51. package/dist/peers/protocol.js +1 -0
  52. package/dist/peers/protocol.js.map +1 -1
  53. package/dist/prompt/coding-agent-doctrine.d.ts +4 -4
  54. package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
  55. package/dist/prompt/coding-agent-doctrine.js +5 -3
  56. package/dist/prompt/coding-agent-doctrine.js.map +1 -1
  57. package/dist/prompt/index.d.ts +0 -4
  58. package/dist/prompt/index.d.ts.map +1 -1
  59. package/dist/prompt/index.js +0 -2
  60. package/dist/prompt/index.js.map +1 -1
  61. package/dist/provider/fallback.d.ts.map +1 -1
  62. package/dist/provider/fallback.js +3 -0
  63. package/dist/provider/fallback.js.map +1 -1
  64. package/dist/provider/idle-timeout.d.ts.map +1 -1
  65. package/dist/provider/idle-timeout.js +5 -0
  66. package/dist/provider/idle-timeout.js.map +1 -1
  67. package/dist/provider/model-window-suffix.d.ts.map +1 -1
  68. package/dist/provider/model-window-suffix.js +1 -0
  69. package/dist/provider/model-window-suffix.js.map +1 -1
  70. package/dist/provider/retry.d.ts.map +1 -1
  71. package/dist/provider/retry.js +5 -0
  72. package/dist/provider/retry.js.map +1 -1
  73. package/dist/provider/step-stopped.d.ts +19 -0
  74. package/dist/provider/step-stopped.d.ts.map +1 -0
  75. package/dist/provider/step-stopped.js +25 -0
  76. package/dist/provider/step-stopped.js.map +1 -0
  77. package/dist/provider/token-budget.d.ts.map +1 -1
  78. package/dist/provider/token-budget.js +6 -1
  79. package/dist/provider/token-budget.js.map +1 -1
  80. package/dist/public-runtime.d.ts +4 -15
  81. package/dist/public-runtime.d.ts.map +1 -1
  82. package/dist/public-runtime.js +4 -15
  83. package/dist/public-runtime.js.map +1 -1
  84. package/dist/public-tools.d.ts +0 -2
  85. package/dist/public-tools.d.ts.map +1 -1
  86. package/dist/public-tools.js +0 -2
  87. package/dist/public-tools.js.map +1 -1
  88. package/dist/public-types.d.ts +4 -22
  89. package/dist/public-types.d.ts.map +1 -1
  90. package/dist/runtime/query/iteration/index.d.ts +17 -0
  91. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  92. package/dist/runtime/query/iteration/index.js +176 -6
  93. package/dist/runtime/query/iteration/index.js.map +1 -1
  94. package/dist/runtime/query/iteration/phases/per-call-review.d.ts.map +1 -1
  95. package/dist/runtime/query/iteration/phases/per-call-review.js +4 -5
  96. package/dist/runtime/query/iteration/phases/per-call-review.js.map +1 -1
  97. package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
  98. package/dist/runtime/query/iteration/phases/tool-review.js +6 -3
  99. package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
  100. package/dist/runtime/query/iteration/recorded-context.d.ts +1 -1
  101. package/dist/runtime/query/iteration/recorded-context.d.ts.map +1 -1
  102. package/dist/runtime/query/iteration/recorded-context.js +2 -1
  103. package/dist/runtime/query/iteration/recorded-context.js.map +1 -1
  104. package/dist/runtime/query/iteration/stream-turn.d.ts +9 -1
  105. package/dist/runtime/query/iteration/stream-turn.d.ts.map +1 -1
  106. package/dist/runtime/query/iteration/stream-turn.js +48 -2
  107. package/dist/runtime/query/iteration/stream-turn.js.map +1 -1
  108. package/dist/runtime/query/resume-pending.d.ts.map +1 -1
  109. package/dist/runtime/query/resume-pending.js +7 -4
  110. package/dist/runtime/query/resume-pending.js.map +1 -1
  111. package/dist/runtime/query/review-policy.d.ts +40 -8
  112. package/dist/runtime/query/review-policy.d.ts.map +1 -1
  113. package/dist/runtime/query/review-policy.js +139 -24
  114. package/dist/runtime/query/review-policy.js.map +1 -1
  115. package/dist/runtime/query/turn/completion-audit.d.ts +32 -0
  116. package/dist/runtime/query/turn/completion-audit.d.ts.map +1 -0
  117. package/dist/runtime/query/turn/completion-audit.js +56 -0
  118. package/dist/runtime/query/turn/completion-audit.js.map +1 -0
  119. package/dist/runtime/query/turn/stall-monitor.d.ts +78 -0
  120. package/dist/runtime/query/turn/stall-monitor.d.ts.map +1 -0
  121. package/dist/runtime/query/turn/stall-monitor.js +101 -0
  122. package/dist/runtime/query/turn/stall-monitor.js.map +1 -0
  123. package/dist/session/paths.d.ts +1 -2
  124. package/dist/session/paths.d.ts.map +1 -1
  125. package/dist/session/paths.js +1 -4
  126. package/dist/session/paths.js.map +1 -1
  127. package/dist/store/index.d.ts +2 -0
  128. package/dist/store/index.d.ts.map +1 -1
  129. package/dist/store/index.js +1 -0
  130. package/dist/store/index.js.map +1 -1
  131. package/dist/tools/builtins/bash.d.ts +2 -1
  132. package/dist/tools/builtins/bash.d.ts.map +1 -1
  133. package/dist/tools/builtins/bash.js +5 -2
  134. package/dist/tools/builtins/bash.js.map +1 -1
  135. package/dist/tools/builtins/content-fingerprint.d.ts +13 -0
  136. package/dist/tools/builtins/content-fingerprint.d.ts.map +1 -1
  137. package/dist/tools/builtins/content-fingerprint.js +25 -0
  138. package/dist/tools/builtins/content-fingerprint.js.map +1 -1
  139. package/dist/tools/builtins/edit.d.ts.map +1 -1
  140. package/dist/tools/builtins/edit.js +7 -2
  141. package/dist/tools/builtins/edit.js.map +1 -1
  142. package/dist/tools/builtins/shell-read-credit.d.ts +15 -0
  143. package/dist/tools/builtins/shell-read-credit.d.ts.map +1 -0
  144. package/dist/tools/builtins/shell-read-credit.js +161 -0
  145. package/dist/tools/builtins/shell-read-credit.js.map +1 -0
  146. package/dist/tools/paths.d.ts +15 -2
  147. package/dist/tools/paths.d.ts.map +1 -1
  148. package/dist/tools/paths.js +17 -2
  149. package/dist/tools/paths.js.map +1 -1
  150. package/dist/turn/evidence-recall.d.ts +1 -1
  151. package/dist/turn/evidence-recall.d.ts.map +1 -1
  152. package/dist/turn/evidence-recall.js +5 -14
  153. package/dist/turn/evidence-recall.js.map +1 -1
  154. package/dist/turn/memory-recall.d.ts.map +1 -1
  155. package/dist/turn/memory-recall.js +18 -4
  156. package/dist/turn/memory-recall.js.map +1 -1
  157. package/dist/turn/reporter.d.ts.map +1 -1
  158. package/dist/turn/reporter.js +5 -0
  159. package/dist/turn/reporter.js.map +1 -1
  160. package/dist/types/acp/index.d.ts +6 -0
  161. package/dist/types/acp/index.d.ts.map +1 -1
  162. package/dist/types/agent/base.d.ts +4 -0
  163. package/dist/types/agent/base.d.ts.map +1 -1
  164. package/dist/types/hitl/index.d.ts +8 -0
  165. package/dist/types/hitl/index.d.ts.map +1 -1
  166. package/dist/types/hitl/index.js.map +1 -1
  167. package/dist/types/ids/index.d.ts +1 -1
  168. package/dist/types/message/index.d.ts +1 -1
  169. package/dist/types/message/index.d.ts.map +1 -1
  170. package/dist/types/message/index.js +1 -0
  171. package/dist/types/message/index.js.map +1 -1
  172. package/dist/types/provider/interface.d.ts +12 -0
  173. package/dist/types/provider/interface.d.ts.map +1 -1
  174. package/dist/types/session/config.d.ts +25 -0
  175. package/dist/types/session/config.d.ts.map +1 -1
  176. package/dist/types/session/events.d.ts +19 -0
  177. package/dist/types/session/events.d.ts.map +1 -1
  178. package/dist/types/session/events.js.map +1 -1
  179. package/dist/types/session/prepare-step.d.ts +16 -0
  180. package/dist/types/session/prepare-step.d.ts.map +1 -1
  181. package/dist/types/session/records.d.ts.map +1 -1
  182. package/dist/types/session/records.js +3 -0
  183. package/dist/types/session/records.js.map +1 -1
  184. package/dist/types/session/turn.d.ts +1 -1
  185. package/dist/types/session/turn.d.ts.map +1 -1
  186. package/dist/types/session/turn.js.map +1 -1
  187. package/package.json +1 -1
  188. package/src/agents/QueryAgent.ts +2 -0
  189. package/src/bridge/a2a/mapper.ts +2 -0
  190. package/src/bridge/acp/permission.ts +8 -0
  191. package/src/bridge/acp/server.ts +7 -1
  192. package/src/bridge/sse/mapper.ts +13 -0
  193. package/src/constants/tools/index.ts +27 -0
  194. package/src/contracts/session/api.ts +1 -0
  195. package/src/eval/harness-protection.ts +1 -1
  196. package/src/learning/cycle.ts +15 -16
  197. package/src/learning/guidance.ts +339 -0
  198. package/src/learning/skill.ts +7 -7
  199. package/src/learning/store.ts +5 -6
  200. package/src/peers/client.ts +2 -3
  201. package/src/peers/endpoint.ts +1 -2
  202. package/src/peers/protocol.ts +1 -0
  203. package/src/prompt/coding-agent-doctrine.ts +5 -3
  204. package/src/prompt/index.ts +0 -4
  205. package/src/provider/fallback.ts +7 -0
  206. package/src/provider/idle-timeout.ts +8 -0
  207. package/src/provider/model-window-suffix.ts +1 -0
  208. package/src/provider/retry.ts +8 -0
  209. package/src/provider/step-stopped.ts +27 -0
  210. package/src/provider/token-budget.ts +6 -1
  211. package/src/public-runtime.ts +7 -28
  212. package/src/public-tools.ts +0 -3
  213. package/src/public-types.ts +6 -137
  214. package/src/runtime/query/iteration/index.ts +218 -6
  215. package/src/runtime/query/iteration/phases/per-call-review.ts +7 -5
  216. package/src/runtime/query/iteration/phases/tool-review.ts +5 -3
  217. package/src/runtime/query/iteration/recorded-context.ts +2 -0
  218. package/src/runtime/query/iteration/stream-turn.ts +51 -1
  219. package/src/runtime/query/resume-pending.ts +11 -4
  220. package/src/runtime/query/review-policy.ts +157 -24
  221. package/src/runtime/query/turn/completion-audit.ts +71 -0
  222. package/src/runtime/query/turn/stall-monitor.ts +158 -0
  223. package/src/session/paths.ts +2 -6
  224. package/src/store/index.ts +2 -0
  225. package/src/tools/builtins/bash.ts +6 -2
  226. package/src/tools/builtins/content-fingerprint.ts +25 -0
  227. package/src/tools/builtins/edit.ts +11 -2
  228. package/src/tools/builtins/shell-read-credit.ts +159 -0
  229. package/src/tools/paths.ts +18 -2
  230. package/src/turn/evidence-recall.ts +7 -15
  231. package/src/turn/memory-recall.ts +26 -7
  232. package/src/turn/reporter.ts +5 -0
  233. package/src/types/acp/index.ts +6 -0
  234. package/src/types/agent/base.ts +4 -0
  235. package/src/types/hitl/index.ts +8 -0
  236. package/src/types/ids/index.ts +1 -1
  237. package/src/types/message/index.ts +1 -0
  238. package/src/types/provider/interface.ts +13 -0
  239. package/src/types/session/config.ts +27 -0
  240. package/src/types/session/events.ts +19 -0
  241. package/src/types/session/prepare-step.ts +17 -0
  242. package/src/types/session/records.ts +3 -0
  243. package/src/types/session/turn.ts +2 -1
  244. package/dist/learning/deprecated.d.ts +0 -86
  245. package/dist/learning/deprecated.d.ts.map +0 -1
  246. package/dist/learning/deprecated.js +0 -22
  247. package/dist/learning/deprecated.js.map +0 -1
  248. package/dist/manager/resident/activity.d.ts +0 -74
  249. package/dist/manager/resident/activity.d.ts.map +0 -1
  250. package/dist/manager/resident/activity.js +0 -125
  251. package/dist/manager/resident/activity.js.map +0 -1
  252. package/dist/manager/resident/agenda.d.ts +0 -167
  253. package/dist/manager/resident/agenda.d.ts.map +0 -1
  254. package/dist/manager/resident/agenda.js +0 -588
  255. package/dist/manager/resident/agenda.js.map +0 -1
  256. package/dist/manager/resident/consumption.d.ts +0 -98
  257. package/dist/manager/resident/consumption.d.ts.map +0 -1
  258. package/dist/manager/resident/consumption.js +0 -237
  259. package/dist/manager/resident/consumption.js.map +0 -1
  260. package/dist/manager/resident/delivery-window.d.ts +0 -28
  261. package/dist/manager/resident/delivery-window.d.ts.map +0 -1
  262. package/dist/manager/resident/delivery-window.js +0 -80
  263. package/dist/manager/resident/delivery-window.js.map +0 -1
  264. package/dist/manager/resident/evidence-recall.d.ts +0 -30
  265. package/dist/manager/resident/evidence-recall.d.ts.map +0 -1
  266. package/dist/manager/resident/evidence-recall.js +0 -297
  267. package/dist/manager/resident/evidence-recall.js.map +0 -1
  268. package/dist/manager/resident/history-disk.d.ts +0 -10
  269. package/dist/manager/resident/history-disk.d.ts.map +0 -1
  270. package/dist/manager/resident/history-disk.js +0 -50
  271. package/dist/manager/resident/history-disk.js.map +0 -1
  272. package/dist/manager/resident/history.d.ts +0 -115
  273. package/dist/manager/resident/history.d.ts.map +0 -1
  274. package/dist/manager/resident/history.js +0 -203
  275. package/dist/manager/resident/history.js.map +0 -1
  276. package/dist/manager/resident/host.d.ts +0 -112
  277. package/dist/manager/resident/host.d.ts.map +0 -1
  278. package/dist/manager/resident/host.js +0 -254
  279. package/dist/manager/resident/host.js.map +0 -1
  280. package/dist/manager/resident/initiative.d.ts +0 -86
  281. package/dist/manager/resident/initiative.d.ts.map +0 -1
  282. package/dist/manager/resident/initiative.js +0 -131
  283. package/dist/manager/resident/initiative.js.map +0 -1
  284. package/dist/manager/resident/loop.d.ts +0 -49
  285. package/dist/manager/resident/loop.d.ts.map +0 -1
  286. package/dist/manager/resident/loop.js +0 -75
  287. package/dist/manager/resident/loop.js.map +0 -1
  288. package/dist/manager/resident/outbox.d.ts +0 -124
  289. package/dist/manager/resident/outbox.d.ts.map +0 -1
  290. package/dist/manager/resident/outbox.js +0 -216
  291. package/dist/manager/resident/outbox.js.map +0 -1
  292. package/dist/manager/resident/proposal.d.ts +0 -65
  293. package/dist/manager/resident/proposal.d.ts.map +0 -1
  294. package/dist/manager/resident/proposal.js +0 -77
  295. package/dist/manager/resident/proposal.js.map +0 -1
  296. package/dist/manager/resident/store.d.ts +0 -108
  297. package/dist/manager/resident/store.d.ts.map +0 -1
  298. package/dist/manager/resident/store.js +0 -205
  299. package/dist/manager/resident/store.js.map +0 -1
  300. package/dist/manager/resident/tool-evidence.d.ts +0 -112
  301. package/dist/manager/resident/tool-evidence.d.ts.map +0 -1
  302. package/dist/manager/resident/tool-evidence.js +0 -289
  303. package/dist/manager/resident/tool-evidence.js.map +0 -1
  304. package/dist/prompt/resident-learning.d.ts +0 -29
  305. package/dist/prompt/resident-learning.d.ts.map +0 -1
  306. package/dist/prompt/resident-learning.js +0 -126
  307. package/dist/prompt/resident-learning.js.map +0 -1
  308. package/dist/prompt/resident-step.d.ts +0 -42
  309. package/dist/prompt/resident-step.d.ts.map +0 -1
  310. package/dist/prompt/resident-step.js +0 -137
  311. package/dist/prompt/resident-step.js.map +0 -1
  312. package/dist/tools/resident-history.d.ts +0 -12
  313. package/dist/tools/resident-history.d.ts.map +0 -1
  314. package/dist/tools/resident-history.js +0 -82
  315. package/dist/tools/resident-history.js.map +0 -1
  316. package/dist/tools/resident-tool-evidence.d.ts +0 -9
  317. package/dist/tools/resident-tool-evidence.d.ts.map +0 -1
  318. package/dist/tools/resident-tool-evidence.js +0 -61
  319. package/dist/tools/resident-tool-evidence.js.map +0 -1
  320. package/src/learning/deprecated.ts +0 -124
  321. package/src/manager/resident/activity.ts +0 -211
  322. package/src/manager/resident/agenda.ts +0 -937
  323. package/src/manager/resident/consumption.ts +0 -335
  324. package/src/manager/resident/delivery-window.ts +0 -99
  325. package/src/manager/resident/evidence-recall.ts +0 -369
  326. package/src/manager/resident/history-disk.ts +0 -63
  327. package/src/manager/resident/history.ts +0 -348
  328. package/src/manager/resident/host.ts +0 -390
  329. package/src/manager/resident/initiative.ts +0 -231
  330. package/src/manager/resident/loop.ts +0 -119
  331. package/src/manager/resident/outbox.ts +0 -341
  332. package/src/manager/resident/proposal.ts +0 -119
  333. package/src/manager/resident/store.ts +0 -279
  334. package/src/manager/resident/tool-evidence.ts +0 -455
  335. package/src/prompt/resident-learning.ts +0 -152
  336. package/src/prompt/resident-step.ts +0 -182
  337. package/src/tools/resident-history.ts +0 -88
  338. package/src/tools/resident-tool-evidence.ts +0 -72
@@ -1,335 +0,0 @@
1
- import { z } from 'zod'
2
- import type {
3
- ResidentActivityScope,
4
- ResidentActivitySource,
5
- ResidentAdmission,
6
- ResidentSettlement,
7
- } from './activity.js'
8
-
9
- const count = z.number().int().nonnegative().safe()
10
- const uuidKey = (id: string) => id.toLowerCase()
11
- const admissionSchema = z.object({
12
- revision: count.positive(),
13
- pursuitRevision: count.positive(),
14
- pursuitId: z.string().uuid(),
15
- claimId: z.string().uuid(),
16
- step: count.positive(),
17
- objective: z.string().min(1).max(8000),
18
- })
19
- const settlementSchema = z.object({
20
- revision: count.positive(),
21
- pursuitId: z.string().uuid(),
22
- claimId: z.string().uuid(),
23
- outcome: z.enum(['wait', 'complete', 'blocked']),
24
- })
25
- const receiptSchema = z
26
- .object({
27
- /** The resident step's turn: one receipt per turn, whose session holds its log. */
28
- sessionId: z.string().uuid(),
29
- turnId: z.string().uuid(),
30
- /** Own usage includes retry and side-call spend; never add cache buckets again. */
31
- ownTokens: count.nullable(),
32
- treeTokens: count.nullable(),
33
- ownCostUsd: z.number().nonnegative().finite().nullable(),
34
- unpricedOwnTokens: count.nullable(),
35
- usageFinal: z.boolean(),
36
- cleanup: z.enum(['confirmed', 'unconfirmed', 'unknown']),
37
- verification: z.enum(['recorded', 'unconfigured', 'unconfirmed']),
38
- })
39
- .superRefine((receipt, ctx) => {
40
- if (
41
- receipt.ownTokens !== null &&
42
- receipt.treeTokens !== null &&
43
- receipt.treeTokens < receipt.ownTokens
44
- )
45
- ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Tree usage is below own usage.' })
46
- if (
47
- receipt.ownTokens !== null &&
48
- receipt.unpricedOwnTokens !== null &&
49
- receipt.unpricedOwnTokens > receipt.ownTokens
50
- )
51
- ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Unpriced usage exceeds own usage.' })
52
- })
53
-
54
- /**
55
- * @experimental Host-authenticated cumulative root receipt. Cost covers this root only.
56
- *
57
- * @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
58
- */
59
- export type ResidentConsumptionReceipt = z.infer<typeof receiptSchema>
60
-
61
- /**
62
- * @experimental A host resolves only authoritative admitted claims and bounds each read.
63
- *
64
- * @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
65
- */
66
- export interface ResidentConsumptionResolver {
67
- /** Worst-case bytes read per resolution; reserved before calling the adapter. */
68
- readonly maxReadBytes: number
69
- resolve(
70
- admission: ResidentAdmission,
71
- signal?: AbortSignal,
72
- ): Promise<ResidentConsumptionReceipt | null>
73
- }
74
-
75
- /**
76
- * @experimental Bounds the entire inspection, not just each page. No inference or writes.
77
- *
78
- * @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
79
- */
80
- export interface ResidentConsumptionOptions {
81
- readonly cursor?: number
82
- readonly maxRevisions?: number
83
- readonly maxHistoryBytes?: number
84
- readonly maxReceiptBytes?: number
85
- }
86
-
87
- /**
88
- * @experimental Missing and deferred receipts never establish zero usage.
89
- *
90
- * @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
91
- */
92
- export interface ResidentConsumptionAttempt extends ResidentAdmission {
93
- readonly settlement: ResidentSettlement | null
94
- readonly receipt: ResidentConsumptionReceipt | null
95
- readonly receiptStatus: 'available' | 'missing' | 'invalid' | 'deferred' | 'duplicate-turn'
96
- }
97
-
98
- /**
99
- * @experimental An evidence projection, not a bill or an enforced spending cap.
100
- *
101
- * @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
102
- */
103
- export interface ResidentConsumptionReport {
104
- readonly scope: ResidentActivityScope
105
- readonly fromRevision: number
106
- readonly throughRevision: number
107
- readonly nextCursor: number | null
108
- readonly historyComplete: boolean
109
- readonly unavailableRevisions: readonly number[]
110
- readonly historyBytes: number
111
- readonly receiptBytesReserved: number
112
- readonly archivedPursuits: readonly string[]
113
- readonly attempts: readonly ResidentConsumptionAttempt[]
114
- readonly recorded: {
115
- readonly ownTokens: number
116
- readonly treeTokens: number
117
- readonly ownCostUsd: number
118
- readonly unpricedOwnTokens: number
119
- }
120
- readonly unknown: {
121
- readonly ownUsageAttempts: number
122
- readonly treeUsageAttempts: number
123
- readonly ownPriceAttempts: number
124
- }
125
- /** Requires complete history and final receipts; says nothing about external billing. */
126
- readonly usageComplete: boolean
127
- }
128
-
129
- /**
130
- * @experimental Inspect cumulative root receipts once per admitted claim. Historical
131
- * archive/reconcile events never erase consumption. Tree tokens are a separate total:
132
- * adding them to own tokens would count the root twice. Descendant prices are unknown.
133
- *
134
- * @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
135
- */
136
- export async function inspectResidentConsumption(
137
- source: ResidentActivitySource,
138
- resolver: ResidentConsumptionResolver,
139
- options: ResidentConsumptionOptions = {},
140
- signal?: AbortSignal,
141
- ): Promise<ResidentConsumptionReport> {
142
- const scope = z
143
- .object({
144
- tenantId: z.string().uuid(),
145
- agentKey: z.string().min(1).max(200),
146
- throughRevision: count.positive(),
147
- })
148
- .parse(source.scope)
149
- const from = z
150
- .number()
151
- .int()
152
- .positive()
153
- .max(scope.throughRevision)
154
- .parse(options.cursor ?? 1)
155
- const maxRevisions = z
156
- .number()
157
- .int()
158
- .positive()
159
- .max(4096)
160
- .parse(options.maxRevisions ?? 256)
161
- const maxHistoryBytes = z
162
- .number()
163
- .int()
164
- .positive()
165
- .max(8 * 1024 * 1024)
166
- .parse(options.maxHistoryBytes ?? 8 * 1024 * 1024)
167
- const maxReceiptBytes = z
168
- .number()
169
- .int()
170
- .nonnegative()
171
- .max(64 * 1024 * 1024)
172
- .parse(options.maxReceiptBytes ?? 16 * 1024 * 1024)
173
- const resolutionBytes = z
174
- .number()
175
- .int()
176
- .positive()
177
- .max(1024 * 1024)
178
- .parse(resolver.maxReadBytes)
179
- const admissions = new Map<string, ResidentAdmission>()
180
- const settlements = new Map<string, ResidentSettlement>()
181
- const unavailable = new Set<number>()
182
- const archived = new Map<string, string>()
183
- let cursor: number | null = from
184
- let through = from - 1
185
- let historyBytes = 0
186
- while (cursor !== null && through - from + 1 < maxRevisions && historyBytes < maxHistoryBytes) {
187
- signal?.throwIfAborted()
188
- const page = await source.read(
189
- {
190
- cursor,
191
- maxRevisions: Math.min(32, maxRevisions - (through - from + 1)),
192
- maxReadBytes: maxHistoryBytes - historyBytes,
193
- },
194
- signal,
195
- )
196
- signal?.throwIfAborted()
197
- if (
198
- page.scope.tenantId !== scope.tenantId ||
199
- page.scope.agentKey !== scope.agentKey ||
200
- page.scope.throughRevision !== scope.throughRevision ||
201
- page.fromRevision !== cursor ||
202
- !Number.isSafeInteger(page.throughRevision) ||
203
- page.throughRevision < cursor - 1 ||
204
- page.throughRevision > Math.min(scope.throughRevision, from + maxRevisions - 1) ||
205
- !Number.isSafeInteger(page.scannedBytes) ||
206
- page.scannedBytes < 0 ||
207
- page.scannedBytes > maxHistoryBytes - historyBytes ||
208
- page.nextCursor !==
209
- (page.throughRevision === scope.throughRevision ? null : page.throughRevision + 1)
210
- )
211
- throw new Error('Invalid resident activity page.')
212
- if (
213
- page.admissions.length > 32 ||
214
- page.settlements.length > 32 ||
215
- page.archivedPursuits.length > 1024
216
- )
217
- throw new Error('Resident activity page exceeds event bounds.')
218
- if (page.unavailableRevisions.length > 33) throw new Error('Invalid unavailable revision list.')
219
- for (const revision of page.unavailableRevisions) {
220
- count.positive().parse(revision)
221
- if (revision < Math.max(1, cursor - 1) || revision > page.throughRevision)
222
- throw new Error('Unavailable revision is outside the page.')
223
- unavailable.add(revision)
224
- }
225
- for (const raw of page.admissions) {
226
- const admission = Object.freeze(admissionSchema.parse(raw))
227
- if (admission.revision < cursor || admission.revision > page.throughRevision)
228
- throw new Error('Resident admission is outside its history page.')
229
- if (admissions.has(uuidKey(admission.claimId)))
230
- throw new Error('Duplicate resident admission.')
231
- admissions.set(uuidKey(admission.claimId), admission)
232
- }
233
- for (const raw of page.settlements) {
234
- const settlement = Object.freeze(settlementSchema.parse(raw))
235
- if (settlement.revision < cursor || settlement.revision > page.throughRevision)
236
- throw new Error('Resident settlement is outside its history page.')
237
- if (settlements.has(uuidKey(settlement.claimId)))
238
- throw new Error('Duplicate resident settlement.')
239
- settlements.set(uuidKey(settlement.claimId), settlement)
240
- }
241
- for (const id of page.archivedPursuits) archived.set(uuidKey(z.string().uuid().parse(id)), id)
242
- historyBytes += page.scannedBytes
243
- through = page.throughRevision
244
- if (page.nextCursor === cursor) break
245
- cursor = page.nextCursor
246
- }
247
- const attempts: ResidentConsumptionAttempt[] = []
248
- let receiptBytesReserved = 0
249
- for (const admission of admissions.values()) {
250
- signal?.throwIfAborted()
251
- const settlement = settlements.get(uuidKey(admission.claimId))
252
- if (
253
- settlement &&
254
- (uuidKey(settlement.pursuitId) !== uuidKey(admission.pursuitId) ||
255
- settlement.revision <= admission.revision)
256
- )
257
- throw new Error('Resident settlement does not follow its admitted claim.')
258
- let receipt: ResidentConsumptionReceipt | null = null
259
- let receiptStatus: ResidentConsumptionAttempt['receiptStatus'] = 'deferred'
260
- if (receiptBytesReserved + resolutionBytes <= maxReceiptBytes) {
261
- receiptBytesReserved += resolutionBytes
262
- try {
263
- const value = await resolver.resolve(admission, signal)
264
- signal?.throwIfAborted()
265
- receipt = value === null ? null : receiptSchema.parse(value)
266
- receiptStatus = receipt ? 'available' : 'missing'
267
- } catch {
268
- signal?.throwIfAborted()
269
- receiptStatus = 'invalid'
270
- }
271
- }
272
- attempts.push({
273
- ...admission,
274
- settlement: settlements.get(uuidKey(admission.claimId)) ?? null,
275
- receipt,
276
- receiptStatus,
277
- })
278
- }
279
- // A copied root receipt must not authenticate two claims. Exclude BOTH records.
280
- const turnCounts = new Map<string, number>()
281
- for (const { receipt } of attempts)
282
- if (receipt)
283
- turnCounts.set(uuidKey(receipt.turnId), (turnCounts.get(uuidKey(receipt.turnId)) ?? 0) + 1)
284
- const checked = attempts.map((attempt) =>
285
- attempt.receipt && (turnCounts.get(uuidKey(attempt.receipt.turnId)) ?? 0) > 1
286
- ? { ...attempt, receipt: null, receiptStatus: 'duplicate-turn' as const }
287
- : attempt,
288
- )
289
- const recorded = { ownTokens: 0, treeTokens: 0, ownCostUsd: 0, unpricedOwnTokens: 0 }
290
- const unknown = { ownUsageAttempts: 0, treeUsageAttempts: 0, ownPriceAttempts: 0 }
291
- const add = (a: number, b: number) => {
292
- const result = a + b
293
- if (!Number.isSafeInteger(result))
294
- throw new Error('Resident consumption exceeds the safe integer range.')
295
- return result
296
- }
297
- let priceCorrection = 0
298
- for (const { receipt } of checked) {
299
- if (!receipt?.usageFinal || receipt.ownTokens === null) unknown.ownUsageAttempts++
300
- if (!receipt?.usageFinal || receipt.treeTokens === null) unknown.treeUsageAttempts++
301
- if (
302
- !receipt?.usageFinal ||
303
- receipt.ownCostUsd === null ||
304
- receipt.unpricedOwnTokens === null ||
305
- receipt.unpricedOwnTokens > 0
306
- )
307
- unknown.ownPriceAttempts++
308
- recorded.ownTokens = add(recorded.ownTokens, receipt?.ownTokens ?? 0)
309
- recorded.treeTokens = add(recorded.treeTokens, receipt?.treeTokens ?? 0)
310
- recorded.unpricedOwnTokens = add(recorded.unpricedOwnTokens, receipt?.unpricedOwnTokens ?? 0)
311
- // Compensated addition limits drift; provider prices are still estimates, not a bill.
312
- const y = (receipt?.ownCostUsd ?? 0) - priceCorrection
313
- const next = recorded.ownCostUsd + y
314
- priceCorrection = next - recorded.ownCostUsd - y
315
- if (!Number.isFinite(next)) throw new Error('Resident cost exceeds the supported range.')
316
- recorded.ownCostUsd = next
317
- }
318
- const historyComplete = from === 1 && cursor === null && unavailable.size === 0
319
- return {
320
- scope,
321
- fromRevision: from,
322
- throughRevision: through,
323
- nextCursor: cursor,
324
- historyComplete,
325
- unavailableRevisions: [...unavailable],
326
- historyBytes,
327
- receiptBytesReserved,
328
- archivedPursuits: [...archived.values()],
329
- attempts: checked,
330
- recorded,
331
- unknown,
332
- usageComplete:
333
- historyComplete && unknown.ownUsageAttempts === 0 && unknown.treeUsageAttempts === 0,
334
- }
335
- }
@@ -1,99 +0,0 @@
1
- import type { ResidentDeliveryGate } from './outbox.js'
2
-
3
- /**
4
- * @experimental A daily allowed delivery window in an explicit named time zone.
5
- *
6
- * @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
7
- */
8
- export interface ResidentDeliveryWindowConfig {
9
- readonly timeZone: string
10
- /** Inclusive local minute of the day, from 0 through 1439. */
11
- readonly startMinute: number
12
- /** Exclusive local minute of the day; less than startMinute means overnight. */
13
- readonly endMinute: number
14
- }
15
-
16
- const minuteMs = 60_000
17
- const searchMinutes = 72 * 60
18
-
19
- /**
20
- * @experimental Admit delivery within a daily local-time window, without model
21
- * calls. The start is inclusive and the end exclusive; equal boundaries are
22
- * rejected. Omit this gate when no time restriction is wanted.
23
- *
24
- * Future admission searches actual UTC minute boundaries, so nonexistent local
25
- * times are skipped and repeated times can open the window twice. The search
26
- * is bounded to 72 hours and the Date range; no opening returns nextCheckAt:null.
27
- * Historic time zones with second-based offsets may defer up to one extra minute.
28
- * This gate does not schedule a timer, authorize a destination or send a message.
29
- *
30
- * @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
31
- */
32
- export function createResidentDeliveryWindow(
33
- config: ResidentDeliveryWindowConfig,
34
- ): ResidentDeliveryGate {
35
- if (
36
- !config ||
37
- typeof config.timeZone !== 'string' ||
38
- config.timeZone.length === 0 ||
39
- config.timeZone !== config.timeZone.trim() ||
40
- /^[+-]/.test(config.timeZone) ||
41
- !Number.isInteger(config.startMinute) ||
42
- config.startMinute < 0 ||
43
- config.startMinute > 1439 ||
44
- !Number.isInteger(config.endMinute) ||
45
- config.endMinute < 0 ||
46
- config.endMinute > 1439 ||
47
- config.startMinute === config.endMinute
48
- ) {
49
- throw new TypeError('Delivery window requires a named time zone and distinct minutes 0–1439.')
50
- }
51
-
52
- let formatter: Intl.DateTimeFormat
53
- try {
54
- formatter = new Intl.DateTimeFormat('en-US', {
55
- timeZone: config.timeZone,
56
- hour: '2-digit',
57
- minute: '2-digit',
58
- hourCycle: 'h23',
59
- })
60
- } catch {
61
- throw new TypeError('Delivery window requires a valid named time zone.')
62
- }
63
- const { startMinute, endMinute } = config
64
- const timeZone = formatter.resolvedOptions().timeZone
65
- const allowed = (at: number): boolean => {
66
- const parts = formatter.formatToParts(at)
67
- const hour = Number(parts.find((part) => part.type === 'hour')?.value)
68
- const minute = Number(parts.find((part) => part.type === 'minute')?.value)
69
- const localMinute = hour * 60 + minute
70
- return startMinute < endMinute
71
- ? localMinute >= startMinute && localMinute < endMinute
72
- : localMinute >= startMinute || localMinute < endMinute
73
- }
74
-
75
- return (_message, now) => {
76
- if (!Number.isFinite(now) || !Number.isFinite(new Date(now).getTime())) {
77
- throw new TypeError('Delivery window requires a finite timestamp within the Date range.')
78
- }
79
- if (allowed(now)) return { allow: true }
80
-
81
- const firstMinute = (Math.floor(now / minuteMs) + 1) * minuteMs
82
- for (let offset = 0; offset < searchMinutes; offset++) {
83
- const candidate = firstMinute + offset * minuteMs
84
- if (!Number.isFinite(new Date(candidate).getTime())) break
85
- if (allowed(candidate)) {
86
- return {
87
- allow: false,
88
- nextCheckAt: candidate,
89
- reason: `Outside the allowed daily delivery window in ${timeZone}.`,
90
- }
91
- }
92
- }
93
- return {
94
- allow: false,
95
- nextCheckAt: null,
96
- reason: `No allowed delivery minute found within the next 72 hours and Date range in ${timeZone}.`,
97
- }
98
- }
99
- }