@namzu/sdk 61.4.0 → 62.0.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 (302) hide show
  1. package/CHANGELOG.md +55 -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/acp/permission.d.ts +8 -0
  6. package/dist/bridge/acp/permission.d.ts.map +1 -1
  7. package/dist/bridge/acp/permission.js.map +1 -1
  8. package/dist/bridge/acp/server.d.ts.map +1 -1
  9. package/dist/bridge/acp/server.js +8 -1
  10. package/dist/bridge/acp/server.js.map +1 -1
  11. package/dist/constants/tools/index.d.ts +2 -0
  12. package/dist/constants/tools/index.d.ts.map +1 -1
  13. package/dist/constants/tools/index.js +23 -0
  14. package/dist/constants/tools/index.js.map +1 -1
  15. package/dist/eval/harness-protection.js +1 -1
  16. package/dist/eval/harness-protection.js.map +1 -1
  17. package/dist/learning/cycle.d.ts +5 -3
  18. package/dist/learning/cycle.d.ts.map +1 -1
  19. package/dist/learning/cycle.js +11 -13
  20. package/dist/learning/cycle.js.map +1 -1
  21. package/dist/learning/guidance.d.ts +74 -0
  22. package/dist/learning/guidance.d.ts.map +1 -0
  23. package/dist/learning/guidance.js +259 -0
  24. package/dist/learning/guidance.js.map +1 -0
  25. package/dist/learning/skill.d.ts +2 -2
  26. package/dist/learning/skill.d.ts.map +1 -1
  27. package/dist/learning/skill.js +6 -6
  28. package/dist/learning/skill.js.map +1 -1
  29. package/dist/learning/store.d.ts.map +1 -1
  30. package/dist/learning/store.js +6 -6
  31. package/dist/learning/store.js.map +1 -1
  32. package/dist/pals/communication/ingress-schema.d.ts +8 -8
  33. package/dist/pals/communication/schema.d.ts +2 -2
  34. package/dist/peers/client.d.ts +2 -3
  35. package/dist/peers/client.d.ts.map +1 -1
  36. package/dist/peers/client.js +2 -3
  37. package/dist/peers/client.js.map +1 -1
  38. package/dist/peers/endpoint.d.ts +1 -2
  39. package/dist/peers/endpoint.d.ts.map +1 -1
  40. package/dist/peers/endpoint.js +1 -2
  41. package/dist/peers/endpoint.js.map +1 -1
  42. package/dist/peers/protocol.d.ts.map +1 -1
  43. package/dist/peers/protocol.js +1 -0
  44. package/dist/peers/protocol.js.map +1 -1
  45. package/dist/prompt/coding-agent-doctrine.d.ts +4 -4
  46. package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
  47. package/dist/prompt/coding-agent-doctrine.js +5 -3
  48. package/dist/prompt/coding-agent-doctrine.js.map +1 -1
  49. package/dist/prompt/index.d.ts +0 -4
  50. package/dist/prompt/index.d.ts.map +1 -1
  51. package/dist/prompt/index.js +0 -2
  52. package/dist/prompt/index.js.map +1 -1
  53. package/dist/provider/fallback.d.ts.map +1 -1
  54. package/dist/provider/fallback.js +3 -0
  55. package/dist/provider/fallback.js.map +1 -1
  56. package/dist/provider/idle-timeout.d.ts.map +1 -1
  57. package/dist/provider/idle-timeout.js +5 -0
  58. package/dist/provider/idle-timeout.js.map +1 -1
  59. package/dist/provider/model-window-suffix.d.ts.map +1 -1
  60. package/dist/provider/model-window-suffix.js +1 -0
  61. package/dist/provider/model-window-suffix.js.map +1 -1
  62. package/dist/provider/retry.d.ts.map +1 -1
  63. package/dist/provider/retry.js +5 -0
  64. package/dist/provider/retry.js.map +1 -1
  65. package/dist/provider/step-stopped.d.ts +19 -0
  66. package/dist/provider/step-stopped.d.ts.map +1 -0
  67. package/dist/provider/step-stopped.js +25 -0
  68. package/dist/provider/step-stopped.js.map +1 -0
  69. package/dist/provider/token-budget.d.ts.map +1 -1
  70. package/dist/provider/token-budget.js +6 -1
  71. package/dist/provider/token-budget.js.map +1 -1
  72. package/dist/public-runtime.d.ts +3 -14
  73. package/dist/public-runtime.d.ts.map +1 -1
  74. package/dist/public-runtime.js +3 -14
  75. package/dist/public-runtime.js.map +1 -1
  76. package/dist/public-tools.d.ts +0 -2
  77. package/dist/public-tools.d.ts.map +1 -1
  78. package/dist/public-tools.js +0 -2
  79. package/dist/public-tools.js.map +1 -1
  80. package/dist/public-types.d.ts +3 -21
  81. package/dist/public-types.d.ts.map +1 -1
  82. package/dist/runtime/query/iteration/index.d.ts +17 -0
  83. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  84. package/dist/runtime/query/iteration/index.js +168 -5
  85. package/dist/runtime/query/iteration/index.js.map +1 -1
  86. package/dist/runtime/query/iteration/phases/per-call-review.d.ts.map +1 -1
  87. package/dist/runtime/query/iteration/phases/per-call-review.js +4 -5
  88. package/dist/runtime/query/iteration/phases/per-call-review.js.map +1 -1
  89. package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
  90. package/dist/runtime/query/iteration/phases/tool-review.js +6 -3
  91. package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
  92. package/dist/runtime/query/iteration/stream-turn.d.ts +9 -1
  93. package/dist/runtime/query/iteration/stream-turn.d.ts.map +1 -1
  94. package/dist/runtime/query/iteration/stream-turn.js +48 -2
  95. package/dist/runtime/query/iteration/stream-turn.js.map +1 -1
  96. package/dist/runtime/query/resume-pending.d.ts.map +1 -1
  97. package/dist/runtime/query/resume-pending.js +7 -4
  98. package/dist/runtime/query/resume-pending.js.map +1 -1
  99. package/dist/runtime/query/review-policy.d.ts +40 -8
  100. package/dist/runtime/query/review-policy.d.ts.map +1 -1
  101. package/dist/runtime/query/review-policy.js +139 -24
  102. package/dist/runtime/query/review-policy.js.map +1 -1
  103. package/dist/runtime/query/turn/completion-audit.d.ts +32 -0
  104. package/dist/runtime/query/turn/completion-audit.d.ts.map +1 -0
  105. package/dist/runtime/query/turn/completion-audit.js +56 -0
  106. package/dist/runtime/query/turn/completion-audit.js.map +1 -0
  107. package/dist/runtime/query/turn/stall-monitor.d.ts +78 -0
  108. package/dist/runtime/query/turn/stall-monitor.d.ts.map +1 -0
  109. package/dist/runtime/query/turn/stall-monitor.js +101 -0
  110. package/dist/runtime/query/turn/stall-monitor.js.map +1 -0
  111. package/dist/session/paths.d.ts +1 -2
  112. package/dist/session/paths.d.ts.map +1 -1
  113. package/dist/session/paths.js +1 -4
  114. package/dist/session/paths.js.map +1 -1
  115. package/dist/tools/builtins/bash.d.ts +2 -1
  116. package/dist/tools/builtins/bash.d.ts.map +1 -1
  117. package/dist/tools/builtins/bash.js +5 -2
  118. package/dist/tools/builtins/bash.js.map +1 -1
  119. package/dist/tools/builtins/content-fingerprint.d.ts +13 -0
  120. package/dist/tools/builtins/content-fingerprint.d.ts.map +1 -1
  121. package/dist/tools/builtins/content-fingerprint.js +25 -0
  122. package/dist/tools/builtins/content-fingerprint.js.map +1 -1
  123. package/dist/tools/builtins/edit.d.ts.map +1 -1
  124. package/dist/tools/builtins/edit.js +7 -2
  125. package/dist/tools/builtins/edit.js.map +1 -1
  126. package/dist/tools/builtins/shell-read-credit.d.ts +15 -0
  127. package/dist/tools/builtins/shell-read-credit.d.ts.map +1 -0
  128. package/dist/tools/builtins/shell-read-credit.js +161 -0
  129. package/dist/tools/builtins/shell-read-credit.js.map +1 -0
  130. package/dist/tools/paths.d.ts +15 -2
  131. package/dist/tools/paths.d.ts.map +1 -1
  132. package/dist/tools/paths.js +17 -2
  133. package/dist/tools/paths.js.map +1 -1
  134. package/dist/turn/evidence-recall.d.ts +1 -1
  135. package/dist/turn/evidence-recall.d.ts.map +1 -1
  136. package/dist/turn/evidence-recall.js +5 -14
  137. package/dist/turn/evidence-recall.js.map +1 -1
  138. package/dist/types/acp/index.d.ts +6 -0
  139. package/dist/types/acp/index.d.ts.map +1 -1
  140. package/dist/types/agent/base.d.ts +4 -0
  141. package/dist/types/agent/base.d.ts.map +1 -1
  142. package/dist/types/hitl/index.d.ts +8 -0
  143. package/dist/types/hitl/index.d.ts.map +1 -1
  144. package/dist/types/hitl/index.js.map +1 -1
  145. package/dist/types/ids/index.d.ts +1 -1
  146. package/dist/types/message/index.d.ts +1 -1
  147. package/dist/types/message/index.d.ts.map +1 -1
  148. package/dist/types/message/index.js +1 -0
  149. package/dist/types/message/index.js.map +1 -1
  150. package/dist/types/provider/interface.d.ts +12 -0
  151. package/dist/types/provider/interface.d.ts.map +1 -1
  152. package/dist/types/session/config.d.ts +25 -0
  153. package/dist/types/session/config.d.ts.map +1 -1
  154. package/dist/types/session/records.d.ts.map +1 -1
  155. package/dist/types/session/records.js +2 -0
  156. package/dist/types/session/records.js.map +1 -1
  157. package/dist/types/session/turn.d.ts +1 -1
  158. package/dist/types/session/turn.d.ts.map +1 -1
  159. package/dist/types/session/turn.js.map +1 -1
  160. package/package.json +1 -1
  161. package/src/agents/QueryAgent.ts +2 -0
  162. package/src/bridge/acp/permission.ts +8 -0
  163. package/src/bridge/acp/server.ts +7 -1
  164. package/src/constants/tools/index.ts +27 -0
  165. package/src/eval/harness-protection.ts +1 -1
  166. package/src/learning/cycle.ts +15 -16
  167. package/src/learning/guidance.ts +339 -0
  168. package/src/learning/skill.ts +7 -7
  169. package/src/learning/store.ts +5 -6
  170. package/src/peers/client.ts +2 -3
  171. package/src/peers/endpoint.ts +1 -2
  172. package/src/peers/protocol.ts +1 -0
  173. package/src/prompt/coding-agent-doctrine.ts +5 -3
  174. package/src/prompt/index.ts +0 -4
  175. package/src/provider/fallback.ts +7 -0
  176. package/src/provider/idle-timeout.ts +8 -0
  177. package/src/provider/model-window-suffix.ts +1 -0
  178. package/src/provider/retry.ts +8 -0
  179. package/src/provider/step-stopped.ts +27 -0
  180. package/src/provider/token-budget.ts +6 -1
  181. package/src/public-runtime.ts +6 -28
  182. package/src/public-tools.ts +0 -3
  183. package/src/public-types.ts +5 -137
  184. package/src/runtime/query/iteration/index.ts +207 -5
  185. package/src/runtime/query/iteration/phases/per-call-review.ts +7 -5
  186. package/src/runtime/query/iteration/phases/tool-review.ts +5 -3
  187. package/src/runtime/query/iteration/stream-turn.ts +51 -1
  188. package/src/runtime/query/resume-pending.ts +11 -4
  189. package/src/runtime/query/review-policy.ts +157 -24
  190. package/src/runtime/query/turn/completion-audit.ts +71 -0
  191. package/src/runtime/query/turn/stall-monitor.ts +158 -0
  192. package/src/session/paths.ts +2 -6
  193. package/src/tools/builtins/bash.ts +6 -2
  194. package/src/tools/builtins/content-fingerprint.ts +25 -0
  195. package/src/tools/builtins/edit.ts +11 -2
  196. package/src/tools/builtins/shell-read-credit.ts +159 -0
  197. package/src/tools/paths.ts +18 -2
  198. package/src/turn/evidence-recall.ts +7 -15
  199. package/src/types/acp/index.ts +6 -0
  200. package/src/types/agent/base.ts +4 -0
  201. package/src/types/hitl/index.ts +8 -0
  202. package/src/types/ids/index.ts +1 -1
  203. package/src/types/message/index.ts +1 -0
  204. package/src/types/provider/interface.ts +13 -0
  205. package/src/types/session/config.ts +27 -0
  206. package/src/types/session/records.ts +2 -0
  207. package/src/types/session/turn.ts +2 -1
  208. package/dist/learning/deprecated.d.ts +0 -86
  209. package/dist/learning/deprecated.d.ts.map +0 -1
  210. package/dist/learning/deprecated.js +0 -22
  211. package/dist/learning/deprecated.js.map +0 -1
  212. package/dist/manager/resident/activity.d.ts +0 -74
  213. package/dist/manager/resident/activity.d.ts.map +0 -1
  214. package/dist/manager/resident/activity.js +0 -125
  215. package/dist/manager/resident/activity.js.map +0 -1
  216. package/dist/manager/resident/agenda.d.ts +0 -167
  217. package/dist/manager/resident/agenda.d.ts.map +0 -1
  218. package/dist/manager/resident/agenda.js +0 -588
  219. package/dist/manager/resident/agenda.js.map +0 -1
  220. package/dist/manager/resident/consumption.d.ts +0 -98
  221. package/dist/manager/resident/consumption.d.ts.map +0 -1
  222. package/dist/manager/resident/consumption.js +0 -237
  223. package/dist/manager/resident/consumption.js.map +0 -1
  224. package/dist/manager/resident/delivery-window.d.ts +0 -28
  225. package/dist/manager/resident/delivery-window.d.ts.map +0 -1
  226. package/dist/manager/resident/delivery-window.js +0 -80
  227. package/dist/manager/resident/delivery-window.js.map +0 -1
  228. package/dist/manager/resident/evidence-recall.d.ts +0 -30
  229. package/dist/manager/resident/evidence-recall.d.ts.map +0 -1
  230. package/dist/manager/resident/evidence-recall.js +0 -297
  231. package/dist/manager/resident/evidence-recall.js.map +0 -1
  232. package/dist/manager/resident/history-disk.d.ts +0 -10
  233. package/dist/manager/resident/history-disk.d.ts.map +0 -1
  234. package/dist/manager/resident/history-disk.js +0 -50
  235. package/dist/manager/resident/history-disk.js.map +0 -1
  236. package/dist/manager/resident/history.d.ts +0 -115
  237. package/dist/manager/resident/history.d.ts.map +0 -1
  238. package/dist/manager/resident/history.js +0 -203
  239. package/dist/manager/resident/history.js.map +0 -1
  240. package/dist/manager/resident/host.d.ts +0 -112
  241. package/dist/manager/resident/host.d.ts.map +0 -1
  242. package/dist/manager/resident/host.js +0 -254
  243. package/dist/manager/resident/host.js.map +0 -1
  244. package/dist/manager/resident/initiative.d.ts +0 -86
  245. package/dist/manager/resident/initiative.d.ts.map +0 -1
  246. package/dist/manager/resident/initiative.js +0 -131
  247. package/dist/manager/resident/initiative.js.map +0 -1
  248. package/dist/manager/resident/loop.d.ts +0 -49
  249. package/dist/manager/resident/loop.d.ts.map +0 -1
  250. package/dist/manager/resident/loop.js +0 -75
  251. package/dist/manager/resident/loop.js.map +0 -1
  252. package/dist/manager/resident/outbox.d.ts +0 -124
  253. package/dist/manager/resident/outbox.d.ts.map +0 -1
  254. package/dist/manager/resident/outbox.js +0 -216
  255. package/dist/manager/resident/outbox.js.map +0 -1
  256. package/dist/manager/resident/proposal.d.ts +0 -65
  257. package/dist/manager/resident/proposal.d.ts.map +0 -1
  258. package/dist/manager/resident/proposal.js +0 -77
  259. package/dist/manager/resident/proposal.js.map +0 -1
  260. package/dist/manager/resident/store.d.ts +0 -108
  261. package/dist/manager/resident/store.d.ts.map +0 -1
  262. package/dist/manager/resident/store.js +0 -205
  263. package/dist/manager/resident/store.js.map +0 -1
  264. package/dist/manager/resident/tool-evidence.d.ts +0 -112
  265. package/dist/manager/resident/tool-evidence.d.ts.map +0 -1
  266. package/dist/manager/resident/tool-evidence.js +0 -289
  267. package/dist/manager/resident/tool-evidence.js.map +0 -1
  268. package/dist/prompt/resident-learning.d.ts +0 -29
  269. package/dist/prompt/resident-learning.d.ts.map +0 -1
  270. package/dist/prompt/resident-learning.js +0 -126
  271. package/dist/prompt/resident-learning.js.map +0 -1
  272. package/dist/prompt/resident-step.d.ts +0 -42
  273. package/dist/prompt/resident-step.d.ts.map +0 -1
  274. package/dist/prompt/resident-step.js +0 -137
  275. package/dist/prompt/resident-step.js.map +0 -1
  276. package/dist/tools/resident-history.d.ts +0 -12
  277. package/dist/tools/resident-history.d.ts.map +0 -1
  278. package/dist/tools/resident-history.js +0 -82
  279. package/dist/tools/resident-history.js.map +0 -1
  280. package/dist/tools/resident-tool-evidence.d.ts +0 -9
  281. package/dist/tools/resident-tool-evidence.d.ts.map +0 -1
  282. package/dist/tools/resident-tool-evidence.js +0 -61
  283. package/dist/tools/resident-tool-evidence.js.map +0 -1
  284. package/src/learning/deprecated.ts +0 -124
  285. package/src/manager/resident/activity.ts +0 -211
  286. package/src/manager/resident/agenda.ts +0 -937
  287. package/src/manager/resident/consumption.ts +0 -335
  288. package/src/manager/resident/delivery-window.ts +0 -99
  289. package/src/manager/resident/evidence-recall.ts +0 -369
  290. package/src/manager/resident/history-disk.ts +0 -63
  291. package/src/manager/resident/history.ts +0 -348
  292. package/src/manager/resident/host.ts +0 -390
  293. package/src/manager/resident/initiative.ts +0 -231
  294. package/src/manager/resident/loop.ts +0 -119
  295. package/src/manager/resident/outbox.ts +0 -341
  296. package/src/manager/resident/proposal.ts +0 -119
  297. package/src/manager/resident/store.ts +0 -279
  298. package/src/manager/resident/tool-evidence.ts +0 -455
  299. package/src/prompt/resident-learning.ts +0 -152
  300. package/src/prompt/resident-step.ts +0 -182
  301. package/src/tools/resident-history.ts +0 -88
  302. package/src/tools/resident-tool-evidence.ts +0 -72
@@ -1,348 +0,0 @@
1
- import { z } from 'zod'
2
- import type { ResidentAgendaState } from './agenda.js'
3
- import { ResidentHistoryPageLimit, type ResidentHistoryReadBudget } from './history-disk.js'
4
-
5
- const SCAN_BYTES = 8 * 1024 * 1024
6
- const SCAN_REVISIONS = 32
7
- const TEXT_PAGE_CHARS = 6_000
8
-
9
- /**
10
- * @experimental Host-bound historical scope; the upper revision never advances implicitly.
11
- *
12
- * @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.
13
- */
14
- export interface ResidentHistoryScope {
15
- readonly tenantId: string
16
- readonly agentKey: string
17
- readonly pursuitId: string
18
- readonly throughRevision: number
19
- }
20
-
21
- /**
22
- * @experimental Revision plus part identifies retained text within one pursuit.
23
- *
24
- * @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.
25
- */
26
- export interface ResidentHistoryAddress {
27
- readonly revision: number
28
- readonly part: number
29
- }
30
-
31
- /**
32
- * @experimental Summary or accepted wake input from a settled historical step.
33
- *
34
- * @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.
35
- */
36
- export interface ResidentHistoryMatch extends ResidentHistoryAddress {
37
- readonly step: number
38
- readonly claimId: string
39
- readonly kind: 'wait' | 'complete' | 'blocked'
40
- readonly source: 'summary' | 'wake'
41
- readonly excerpt: string
42
- readonly matchingParts: readonly number[]
43
- }
44
-
45
- /**
46
- * @experimental Empty query browses recent steps; cursor is the next inclusive revision.
47
- *
48
- * @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.
49
- */
50
- export interface ResidentHistorySearchOptions {
51
- /** Optional per-call ceiling, 1 byte–8 MiB. Defaults to 8 MiB. */
52
- readonly maxReadBytes?: number
53
- readonly query?: string
54
- readonly cursor?: number
55
- readonly limit?: number
56
- }
57
-
58
- /**
59
- * @experimental Counts describe this page, not the whole history.
60
- *
61
- * @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.
62
- */
63
- export interface ResidentHistorySearchResult {
64
- readonly matches: readonly ResidentHistoryMatch[]
65
- readonly nextCursor: number | null
66
- readonly scannedRevisions: number
67
- readonly scannedBytes: number
68
- readonly unavailableRevisions: readonly number[]
69
- readonly incomplete: boolean
70
- }
71
-
72
- /**
73
- * @experimental Use the returned nextOffset to continue exact Unicode-safe text.
74
- *
75
- * @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.
76
- */
77
- export interface ResidentHistoryReadOptions extends ResidentHistoryAddress {
78
- readonly offset?: number
79
- /** Optional per-call ceiling, 1 byte–8 MiB. Defaults to 8 MiB. */
80
- readonly maxReadBytes?: number
81
- }
82
-
83
- /**
84
- * @experimental A recorded claim remains historical evidence, not proof of current validity.
85
- *
86
- * @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.
87
- */
88
- export interface ResidentHistoryText extends ResidentHistoryAddress {
89
- readonly step: number
90
- readonly claimId: string
91
- readonly kind: 'wait' | 'complete' | 'blocked'
92
- readonly source: 'summary' | 'wake'
93
- readonly receivedAt?: number
94
- readonly text: string
95
- readonly offset: number
96
- readonly totalChars: number
97
- readonly nextOffset: number | null
98
- }
99
-
100
- /**
101
- * @experimental Null with unavailable revisions means unreadable evidence, not proven absence.
102
- *
103
- * @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.
104
- */
105
- export interface ResidentHistoryReadResult {
106
- readonly entry: ResidentHistoryText | null
107
- readonly scannedRevisions: number
108
- readonly scannedBytes: number
109
- readonly unavailableRevisions: readonly number[]
110
- }
111
-
112
- /**
113
- * @experimental A backend must enforce its bound scope and bounded reads.
114
- *
115
- * @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.
116
- */
117
- export interface ResidentHistorySource {
118
- readonly scope: ResidentHistoryScope
119
- search(
120
- options?: ResidentHistorySearchOptions,
121
- signal?: AbortSignal,
122
- ): Promise<ResidentHistorySearchResult>
123
- read(
124
- options: ResidentHistoryReadOptions,
125
- signal?: AbortSignal,
126
- ): Promise<ResidentHistoryReadResult>
127
- }
128
-
129
- type LoadRevision = (
130
- revision: number,
131
- budget: ResidentHistoryReadBudget,
132
- ) => Promise<ResidentAgendaState>
133
- interface Part {
134
- source: 'summary' | 'wake'
135
- text: string
136
- receivedAt?: number
137
- }
138
- interface Episode {
139
- step: number
140
- claimId: string
141
- kind: 'wait' | 'complete' | 'blocked'
142
- parts: Part[]
143
- }
144
-
145
- function episode(
146
- before: ResidentAgendaState | null,
147
- after: ResidentAgendaState | null,
148
- id: string,
149
- ): Episode | null {
150
- const previous = before?.pursuits.find((p) => p.id === id)?.state
151
- const current = after?.pursuits.find((p) => p.id === id)?.state
152
- if (
153
- !before ||
154
- !after ||
155
- !previous ||
156
- !current ||
157
- previous.phase !== 'running' ||
158
- !previous.claimId ||
159
- current.phase === 'running' ||
160
- current.claimId !== null ||
161
- current.summary === null ||
162
- current.stepsAdmitted !== previous.stepsAdmitted ||
163
- current.revision !== previous.revision + 1
164
- )
165
- return null
166
- return {
167
- step: current.stepsAdmitted,
168
- claimId: previous.claimId,
169
- kind: current.phase === 'waiting' ? 'wait' : current.phase,
170
- parts: [
171
- { source: 'summary', text: current.summary },
172
- ...(previous.wakeEvidence ?? []).map((entry) => ({
173
- source: 'wake' as const,
174
- text: entry.reason,
175
- receivedAt: entry.receivedAt,
176
- })),
177
- ],
178
- }
179
- }
180
-
181
- function boundary(text: string, offset: number): boolean {
182
- const current = text.charCodeAt(offset)
183
- const previous = text.charCodeAt(offset - 1)
184
- return !(current >= 0xdc00 && current <= 0xdfff && previous >= 0xd800 && previous <= 0xdbff)
185
- }
186
-
187
- function textSlice(text: string, offset: number, maxChars: number): string {
188
- let end = Math.min(text.length, offset + maxChars)
189
- if (!boundary(text, end)) end--
190
- return text.slice(offset, end)
191
- }
192
-
193
- /** Internal disk adapter. Other backends may implement ResidentHistorySource directly. */
194
- export function createResidentHistorySource(
195
- scopeInput: ResidentHistoryScope,
196
- identity: { identity: string; objective: string },
197
- load: LoadRevision,
198
- ): ResidentHistorySource {
199
- const scope = Object.freeze({ ...scopeInput })
200
- const expected = { ...identity }
201
- const revisionSchema = z.number().int().min(1).max(scope.throughRevision).safe()
202
- function page(signal: AbortSignal | undefined, requestedBytes: number | undefined) {
203
- const limit = z
204
- .number()
205
- .int()
206
- .min(1)
207
- .max(SCAN_BYTES)
208
- .parse(requestedBytes ?? SCAN_BYTES)
209
- const budget: ResidentHistoryReadBudget = { remaining: limit, bytesRead: 0, signal }
210
- const unavailable = new Set<number>()
211
- let scanned = 0
212
- const cache = new Map<number, ResidentAgendaState | null>()
213
- return {
214
- budget,
215
- async load(revision: number) {
216
- signal?.throwIfAborted()
217
- if (cache.has(revision)) return cache.get(revision) ?? null
218
- if (scanned >= SCAN_REVISIONS) throw new ResidentHistoryPageLimit()
219
- scanned++
220
- let record: ResidentAgendaState | null = null
221
- try {
222
- record = await load(revision, budget)
223
- const pursuit = record.pursuits.find((p) => p.id === scope.pursuitId)
224
- if (
225
- record.revision !== revision ||
226
- record.tenantId !== scope.tenantId ||
227
- record.agentKey !== scope.agentKey ||
228
- (pursuit &&
229
- (pursuit.state.identity !== expected.identity ||
230
- pursuit.state.objective !== expected.objective))
231
- )
232
- throw new Error('History identity does not match its bound pursuit.')
233
- } catch (error) {
234
- signal?.throwIfAborted()
235
- if (error instanceof ResidentHistoryPageLimit) throw error
236
- unavailable.add(revision)
237
- record = null
238
- }
239
- // Adjacent revisions are reused on the next scan iteration, without
240
- // keeping the whole archive in memory.
241
- if (cache.size >= 2) cache.delete(cache.keys().next().value as number)
242
- cache.set(revision, record)
243
- return record
244
- },
245
- counts: () => ({
246
- scannedRevisions: scanned,
247
- scannedBytes: budget.bytesRead,
248
- unavailableRevisions: [...unavailable],
249
- }),
250
- }
251
- }
252
- return Object.freeze({
253
- scope,
254
- async search(options: ResidentHistorySearchOptions = {}, signal?: AbortSignal) {
255
- signal?.throwIfAborted()
256
- const query = z
257
- .string()
258
- .max(256)
259
- .parse(options.query ?? '')
260
- const limit = z
261
- .number()
262
- .int()
263
- .min(1)
264
- .max(8)
265
- .parse(options.limit ?? 5)
266
- let revision = revisionSchema.parse(options.cursor ?? scope.throughRevision)
267
- const reader = page(signal, options.maxReadBytes)
268
- const matches: ResidentHistoryMatch[] = []
269
- while (revision >= 2 && matches.length < limit) {
270
- let found: Episode | null
271
- try {
272
- const after = await reader.load(revision)
273
- const before = await reader.load(revision - 1)
274
- found = episode(before, after, scope.pursuitId)
275
- } catch (error) {
276
- if (!(error instanceof ResidentHistoryPageLimit)) throw error
277
- break
278
- }
279
- if (found) {
280
- const matchingParts = found.parts.flatMap((part, index) =>
281
- part.text.includes(query) ? [index] : [],
282
- )
283
- const part = matchingParts[0]
284
- const matched = part === undefined ? undefined : found.parts[part]
285
- if (matched && part !== undefined) {
286
- let start = Math.max(0, matched.text.indexOf(query) - 120)
287
- if (!boundary(matched.text, start)) start--
288
- matches.push({
289
- revision,
290
- part,
291
- step: found.step,
292
- claimId: found.claimId,
293
- kind: found.kind,
294
- source: matched.source,
295
- excerpt: textSlice(matched.text, start, 512),
296
- matchingParts,
297
- })
298
- }
299
- }
300
- revision--
301
- }
302
- const nextCursor = revision >= 2 ? revision : null
303
- const counts = reader.counts()
304
- return {
305
- matches,
306
- nextCursor,
307
- ...counts,
308
- incomplete: nextCursor !== null || counts.unavailableRevisions.length > 0,
309
- }
310
- },
311
- async read(options: ResidentHistoryReadOptions, signal?: AbortSignal) {
312
- const revision = revisionSchema.parse(options.revision)
313
- const part = z.number().int().min(0).max(16).parse(options.part)
314
- const offset = z
315
- .number()
316
- .int()
317
- .nonnegative()
318
- .safe()
319
- .parse(options.offset ?? 0)
320
- const reader = page(signal, options.maxReadBytes)
321
- const after = await reader.load(revision)
322
- const before = revision >= 2 ? await reader.load(revision - 1) : null
323
- const found = episode(before, after, scope.pursuitId)
324
- const selected = found?.parts[part]
325
- if (!selected || !found) return { entry: null, ...reader.counts() }
326
- if (offset > selected.text.length || !boundary(selected.text, offset))
327
- throw new Error('Invalid retained text offset; use nextOffset from the previous page.')
328
- const text = textSlice(selected.text, offset, TEXT_PAGE_CHARS)
329
- const end = offset + text.length
330
- return {
331
- entry: {
332
- revision,
333
- part,
334
- step: found.step,
335
- claimId: found.claimId,
336
- kind: found.kind,
337
- source: selected.source,
338
- ...(selected.receivedAt !== undefined ? { receivedAt: selected.receivedAt } : {}),
339
- text,
340
- offset,
341
- totalChars: selected.text.length,
342
- nextOffset: end < selected.text.length ? end : null,
343
- },
344
- ...reader.counts(),
345
- }
346
- },
347
- })
348
- }