@afaik-lab/weigh 0.0.0 → 0.6.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 (349) hide show
  1. package/.env.example +49 -0
  2. package/LICENSE +373 -0
  3. package/README.md +103 -1
  4. package/dist/agent.d.ts +83 -0
  5. package/dist/agent.d.ts.map +1 -0
  6. package/dist/agent.js +204 -0
  7. package/dist/agent.js.map +1 -0
  8. package/dist/ceilings.d.ts +32 -0
  9. package/dist/ceilings.d.ts.map +1 -0
  10. package/dist/ceilings.js +61 -0
  11. package/dist/ceilings.js.map +1 -0
  12. package/dist/cli.d.ts +3 -0
  13. package/dist/cli.d.ts.map +1 -0
  14. package/dist/cli.js +530 -0
  15. package/dist/cli.js.map +1 -0
  16. package/dist/config-cli.d.ts +15 -0
  17. package/dist/config-cli.d.ts.map +1 -0
  18. package/dist/config-cli.js +141 -0
  19. package/dist/config-cli.js.map +1 -0
  20. package/dist/config.d.ts +198 -0
  21. package/dist/config.d.ts.map +1 -0
  22. package/dist/config.js +373 -0
  23. package/dist/config.js.map +1 -0
  24. package/dist/context-cli.d.ts +35 -0
  25. package/dist/context-cli.d.ts.map +1 -0
  26. package/dist/context-cli.js +251 -0
  27. package/dist/context-cli.js.map +1 -0
  28. package/dist/context.d.ts +118 -0
  29. package/dist/context.d.ts.map +1 -0
  30. package/dist/context.js +159 -0
  31. package/dist/context.js.map +1 -0
  32. package/dist/corrections/correction-set.d.ts +67 -0
  33. package/dist/corrections/correction-set.d.ts.map +1 -0
  34. package/dist/corrections/correction-set.js +104 -0
  35. package/dist/corrections/correction-set.js.map +1 -0
  36. package/dist/corrections/correction.d.ts +74 -0
  37. package/dist/corrections/correction.d.ts.map +1 -0
  38. package/dist/corrections/correction.js +154 -0
  39. package/dist/corrections/correction.js.map +1 -0
  40. package/dist/corrections/follow-up.d.ts +69 -0
  41. package/dist/corrections/follow-up.d.ts.map +1 -0
  42. package/dist/corrections/follow-up.js +217 -0
  43. package/dist/corrections/follow-up.js.map +1 -0
  44. package/dist/corrections/index.d.ts +31 -0
  45. package/dist/corrections/index.d.ts.map +1 -0
  46. package/dist/corrections/index.js +31 -0
  47. package/dist/corrections/index.js.map +1 -0
  48. package/dist/corrections/ledger.d.ts +78 -0
  49. package/dist/corrections/ledger.d.ts.map +1 -0
  50. package/dist/corrections/ledger.js +117 -0
  51. package/dist/corrections/ledger.js.map +1 -0
  52. package/dist/corrections/promotion.d.ts +119 -0
  53. package/dist/corrections/promotion.d.ts.map +1 -0
  54. package/dist/corrections/promotion.js +221 -0
  55. package/dist/corrections/promotion.js.map +1 -0
  56. package/dist/corrections/schema.d.ts +125 -0
  57. package/dist/corrections/schema.d.ts.map +1 -0
  58. package/dist/corrections/schema.js +100 -0
  59. package/dist/corrections/schema.js.map +1 -0
  60. package/dist/corrections-cli.d.ts +28 -0
  61. package/dist/corrections-cli.d.ts.map +1 -0
  62. package/dist/corrections-cli.js +157 -0
  63. package/dist/corrections-cli.js.map +1 -0
  64. package/dist/diff.d.ts +105 -0
  65. package/dist/diff.d.ts.map +1 -0
  66. package/dist/diff.js +202 -0
  67. package/dist/diff.js.map +1 -0
  68. package/dist/doctor.d.ts +38 -0
  69. package/dist/doctor.d.ts.map +1 -0
  70. package/dist/doctor.js +197 -0
  71. package/dist/doctor.js.map +1 -0
  72. package/dist/events.d.ts +21 -0
  73. package/dist/events.d.ts.map +1 -0
  74. package/dist/events.js +5 -0
  75. package/dist/events.js.map +1 -0
  76. package/dist/findings.d.ts +510 -0
  77. package/dist/findings.d.ts.map +1 -0
  78. package/dist/findings.js +645 -0
  79. package/dist/findings.js.map +1 -0
  80. package/dist/follow-up-cli.d.ts +88 -0
  81. package/dist/follow-up-cli.d.ts.map +1 -0
  82. package/dist/follow-up-cli.js +208 -0
  83. package/dist/follow-up-cli.js.map +1 -0
  84. package/dist/harness.d.ts +64 -0
  85. package/dist/harness.d.ts.map +1 -0
  86. package/dist/harness.js +71 -0
  87. package/dist/harness.js.map +1 -0
  88. package/dist/help/command-help.d.ts +9 -0
  89. package/dist/help/command-help.d.ts.map +1 -0
  90. package/dist/help/command-help.js +21 -0
  91. package/dist/help/command-help.js.map +1 -0
  92. package/dist/help/commands/answer.d.ts +3 -0
  93. package/dist/help/commands/answer.d.ts.map +1 -0
  94. package/dist/help/commands/answer.js +39 -0
  95. package/dist/help/commands/answer.js.map +1 -0
  96. package/dist/help/commands/config.d.ts +3 -0
  97. package/dist/help/commands/config.d.ts.map +1 -0
  98. package/dist/help/commands/config.js +25 -0
  99. package/dist/help/commands/config.js.map +1 -0
  100. package/dist/help/commands/context.d.ts +3 -0
  101. package/dist/help/commands/context.d.ts.map +1 -0
  102. package/dist/help/commands/context.js +44 -0
  103. package/dist/help/commands/context.js.map +1 -0
  104. package/dist/help/commands/corrections.d.ts +3 -0
  105. package/dist/help/commands/corrections.d.ts.map +1 -0
  106. package/dist/help/commands/corrections.js +30 -0
  107. package/dist/help/commands/corrections.js.map +1 -0
  108. package/dist/help/commands/doctor.d.ts +3 -0
  109. package/dist/help/commands/doctor.d.ts.map +1 -0
  110. package/dist/help/commands/doctor.js +12 -0
  111. package/dist/help/commands/doctor.js.map +1 -0
  112. package/dist/help/commands/index.d.ts +4 -0
  113. package/dist/help/commands/index.d.ts.map +1 -0
  114. package/dist/help/commands/index.js +27 -0
  115. package/dist/help/commands/index.js.map +1 -0
  116. package/dist/help/commands/memory.d.ts +3 -0
  117. package/dist/help/commands/memory.d.ts.map +1 -0
  118. package/dist/help/commands/memory.js +33 -0
  119. package/dist/help/commands/memory.js.map +1 -0
  120. package/dist/help/commands/models.d.ts +3 -0
  121. package/dist/help/commands/models.d.ts.map +1 -0
  122. package/dist/help/commands/models.js +17 -0
  123. package/dist/help/commands/models.js.map +1 -0
  124. package/dist/help/commands/options.d.ts +4 -0
  125. package/dist/help/commands/options.d.ts.map +1 -0
  126. package/dist/help/commands/options.js +13 -0
  127. package/dist/help/commands/options.js.map +1 -0
  128. package/dist/help/commands/review.d.ts +3 -0
  129. package/dist/help/commands/review.d.ts.map +1 -0
  130. package/dist/help/commands/review.js +50 -0
  131. package/dist/help/commands/review.js.map +1 -0
  132. package/dist/help/commands/sessions.d.ts +3 -0
  133. package/dist/help/commands/sessions.d.ts.map +1 -0
  134. package/dist/help/commands/sessions.js +23 -0
  135. package/dist/help/commands/sessions.js.map +1 -0
  136. package/dist/help/commands/show.d.ts +3 -0
  137. package/dist/help/commands/show.d.ts.map +1 -0
  138. package/dist/help/commands/show.js +18 -0
  139. package/dist/help/commands/show.js.map +1 -0
  140. package/dist/help/help.d.ts +16 -0
  141. package/dist/help/help.d.ts.map +1 -0
  142. package/dist/help/help.js +68 -0
  143. package/dist/help/help.js.map +1 -0
  144. package/dist/help/index.d.ts +4 -0
  145. package/dist/help/index.d.ts.map +1 -0
  146. package/dist/help/index.js +7 -0
  147. package/dist/help/index.js.map +1 -0
  148. package/dist/index.d.ts +80 -0
  149. package/dist/index.d.ts.map +1 -0
  150. package/dist/index.js +51 -0
  151. package/dist/index.js.map +1 -0
  152. package/dist/knowledge/block.d.ts +65 -0
  153. package/dist/knowledge/block.d.ts.map +1 -0
  154. package/dist/knowledge/block.js +89 -0
  155. package/dist/knowledge/block.js.map +1 -0
  156. package/dist/knowledge/claim.d.ts +143 -0
  157. package/dist/knowledge/claim.d.ts.map +1 -0
  158. package/dist/knowledge/claim.js +303 -0
  159. package/dist/knowledge/claim.js.map +1 -0
  160. package/dist/knowledge/index.d.ts +32 -0
  161. package/dist/knowledge/index.d.ts.map +1 -0
  162. package/dist/knowledge/index.js +29 -0
  163. package/dist/knowledge/index.js.map +1 -0
  164. package/dist/knowledge/novelty.d.ts +65 -0
  165. package/dist/knowledge/novelty.d.ts.map +1 -0
  166. package/dist/knowledge/novelty.js +100 -0
  167. package/dist/knowledge/novelty.js.map +1 -0
  168. package/dist/knowledge/recall.d.ts +134 -0
  169. package/dist/knowledge/recall.d.ts.map +1 -0
  170. package/dist/knowledge/recall.js +229 -0
  171. package/dist/knowledge/recall.js.map +1 -0
  172. package/dist/knowledge/recalled-claim.d.ts +35 -0
  173. package/dist/knowledge/recalled-claim.d.ts.map +1 -0
  174. package/dist/knowledge/recalled-claim.js +67 -0
  175. package/dist/knowledge/recalled-claim.js.map +1 -0
  176. package/dist/knowledge/recalled.d.ts +70 -0
  177. package/dist/knowledge/recalled.d.ts.map +1 -0
  178. package/dist/knowledge/recalled.js +149 -0
  179. package/dist/knowledge/recalled.js.map +1 -0
  180. package/dist/knowledge/schema.d.ts +83 -0
  181. package/dist/knowledge/schema.d.ts.map +1 -0
  182. package/dist/knowledge/schema.js +96 -0
  183. package/dist/knowledge/schema.js.map +1 -0
  184. package/dist/knowledge/staleness.d.ts +48 -0
  185. package/dist/knowledge/staleness.d.ts.map +1 -0
  186. package/dist/knowledge/staleness.js +85 -0
  187. package/dist/knowledge/staleness.js.map +1 -0
  188. package/dist/knowledge/store.d.ts +100 -0
  189. package/dist/knowledge/store.d.ts.map +1 -0
  190. package/dist/knowledge/store.js +184 -0
  191. package/dist/knowledge/store.js.map +1 -0
  192. package/dist/memory/compose-file.d.ts +14 -0
  193. package/dist/memory/compose-file.d.ts.map +1 -0
  194. package/dist/memory/compose-file.js +25 -0
  195. package/dist/memory/compose-file.js.map +1 -0
  196. package/dist/memory/index.d.ts +43 -0
  197. package/dist/memory/index.d.ts.map +1 -0
  198. package/dist/memory/index.js +119 -0
  199. package/dist/memory/index.js.map +1 -0
  200. package/dist/memory/oss.d.ts +62 -0
  201. package/dist/memory/oss.d.ts.map +1 -0
  202. package/dist/memory/oss.js +230 -0
  203. package/dist/memory/oss.js.map +1 -0
  204. package/dist/memory/platform.d.ts +37 -0
  205. package/dist/memory/platform.d.ts.map +1 -0
  206. package/dist/memory/platform.js +131 -0
  207. package/dist/memory/platform.js.map +1 -0
  208. package/dist/memory/scope.d.ts +49 -0
  209. package/dist/memory/scope.d.ts.map +1 -0
  210. package/dist/memory/scope.js +70 -0
  211. package/dist/memory/scope.js.map +1 -0
  212. package/dist/memory/types.d.ts +113 -0
  213. package/dist/memory/types.d.ts.map +1 -0
  214. package/dist/memory/types.js +18 -0
  215. package/dist/memory/types.js.map +1 -0
  216. package/dist/memory-cli.d.ts +17 -0
  217. package/dist/memory-cli.d.ts.map +1 -0
  218. package/dist/memory-cli.js +107 -0
  219. package/dist/memory-cli.js.map +1 -0
  220. package/dist/models-cli.d.ts +12 -0
  221. package/dist/models-cli.d.ts.map +1 -0
  222. package/dist/models-cli.js +54 -0
  223. package/dist/models-cli.js.map +1 -0
  224. package/dist/models.d.ts +111 -0
  225. package/dist/models.d.ts.map +1 -0
  226. package/dist/models.js +228 -0
  227. package/dist/models.js.map +1 -0
  228. package/dist/paths.d.ts +35 -0
  229. package/dist/paths.d.ts.map +1 -0
  230. package/dist/paths.js +65 -0
  231. package/dist/paths.js.map +1 -0
  232. package/dist/profile.d.ts +28 -0
  233. package/dist/profile.d.ts.map +1 -0
  234. package/dist/profile.js +154 -0
  235. package/dist/profile.js.map +1 -0
  236. package/dist/progress.d.ts +70 -0
  237. package/dist/progress.d.ts.map +1 -0
  238. package/dist/progress.js +132 -0
  239. package/dist/progress.js.map +1 -0
  240. package/dist/quiet.d.ts +2 -0
  241. package/dist/quiet.d.ts.map +1 -0
  242. package/dist/quiet.js +21 -0
  243. package/dist/quiet.js.map +1 -0
  244. package/dist/reconcile/continuation.d.ts +132 -0
  245. package/dist/reconcile/continuation.d.ts.map +1 -0
  246. package/dist/reconcile/continuation.js +306 -0
  247. package/dist/reconcile/continuation.js.map +1 -0
  248. package/dist/reconcile/index.d.ts +26 -0
  249. package/dist/reconcile/index.d.ts.map +1 -0
  250. package/dist/reconcile/index.js +28 -0
  251. package/dist/reconcile/index.js.map +1 -0
  252. package/dist/reconcile/outcome-set.d.ts +53 -0
  253. package/dist/reconcile/outcome-set.d.ts.map +1 -0
  254. package/dist/reconcile/outcome-set.js +124 -0
  255. package/dist/reconcile/outcome-set.js.map +1 -0
  256. package/dist/reconcile/outcome.d.ts +56 -0
  257. package/dist/reconcile/outcome.d.ts.map +1 -0
  258. package/dist/reconcile/outcome.js +60 -0
  259. package/dist/reconcile/outcome.js.map +1 -0
  260. package/dist/reconcile/reconciliation.d.ts +49 -0
  261. package/dist/reconcile/reconciliation.d.ts.map +1 -0
  262. package/dist/reconcile/reconciliation.js +88 -0
  263. package/dist/reconcile/reconciliation.js.map +1 -0
  264. package/dist/reconcile/schema.d.ts +157 -0
  265. package/dist/reconcile/schema.d.ts.map +1 -0
  266. package/dist/reconcile/schema.js +97 -0
  267. package/dist/reconcile/schema.js.map +1 -0
  268. package/dist/record/index.d.ts +23 -0
  269. package/dist/record/index.d.ts.map +1 -0
  270. package/dist/record/index.js +25 -0
  271. package/dist/record/index.js.map +1 -0
  272. package/dist/record/review-record.d.ts +28 -0
  273. package/dist/record/review-record.d.ts.map +1 -0
  274. package/dist/record/review-record.js +78 -0
  275. package/dist/record/review-record.js.map +1 -0
  276. package/dist/record/schema.d.ts +153 -0
  277. package/dist/record/schema.d.ts.map +1 -0
  278. package/dist/record/schema.js +87 -0
  279. package/dist/record/schema.js.map +1 -0
  280. package/dist/record/store.d.ts +83 -0
  281. package/dist/record/store.d.ts.map +1 -0
  282. package/dist/record/store.js +162 -0
  283. package/dist/record/store.js.map +1 -0
  284. package/dist/record/stored-review.d.ts +36 -0
  285. package/dist/record/stored-review.d.ts.map +1 -0
  286. package/dist/record/stored-review.js +90 -0
  287. package/dist/record/stored-review.js.map +1 -0
  288. package/dist/refute.d.ts +318 -0
  289. package/dist/refute.d.ts.map +1 -0
  290. package/dist/refute.js +513 -0
  291. package/dist/refute.js.map +1 -0
  292. package/dist/render.d.ts +212 -0
  293. package/dist/render.d.ts.map +1 -0
  294. package/dist/render.js +544 -0
  295. package/dist/render.js.map +1 -0
  296. package/dist/review-cli.d.ts +181 -0
  297. package/dist/review-cli.d.ts.map +1 -0
  298. package/dist/review-cli.js +598 -0
  299. package/dist/review-cli.js.map +1 -0
  300. package/dist/reviewer.d.ts +297 -0
  301. package/dist/reviewer.d.ts.map +1 -0
  302. package/dist/reviewer.js +584 -0
  303. package/dist/reviewer.js.map +1 -0
  304. package/dist/session-cli.d.ts +17 -0
  305. package/dist/session-cli.d.ts.map +1 -0
  306. package/dist/session-cli.js +62 -0
  307. package/dist/session-cli.js.map +1 -0
  308. package/dist/session.d.ts +36 -0
  309. package/dist/session.d.ts.map +1 -0
  310. package/dist/session.js +105 -0
  311. package/dist/session.js.map +1 -0
  312. package/dist/settings.d.ts +68 -0
  313. package/dist/settings.d.ts.map +1 -0
  314. package/dist/settings.js +175 -0
  315. package/dist/settings.js.map +1 -0
  316. package/dist/show-cli.d.ts +39 -0
  317. package/dist/show-cli.d.ts.map +1 -0
  318. package/dist/show-cli.js +56 -0
  319. package/dist/show-cli.js.map +1 -0
  320. package/dist/stdout.d.ts +28 -0
  321. package/dist/stdout.d.ts.map +1 -0
  322. package/dist/stdout.js +44 -0
  323. package/dist/stdout.js.map +1 -0
  324. package/dist/target.d.ts +58 -0
  325. package/dist/target.d.ts.map +1 -0
  326. package/dist/target.js +96 -0
  327. package/dist/target.js.map +1 -0
  328. package/dist/tools/index.d.ts +93 -0
  329. package/dist/tools/index.d.ts.map +1 -0
  330. package/dist/tools/index.js +146 -0
  331. package/dist/tools/index.js.map +1 -0
  332. package/dist/tools/root.d.ts +62 -0
  333. package/dist/tools/root.d.ts.map +1 -0
  334. package/dist/tools/root.js +51 -0
  335. package/dist/tools/root.js.map +1 -0
  336. package/dist/usage.d.ts +75 -0
  337. package/dist/usage.d.ts.map +1 -0
  338. package/dist/usage.js +100 -0
  339. package/dist/usage.js.map +1 -0
  340. package/dist/version.d.ts +14 -0
  341. package/dist/version.d.ts.map +1 -0
  342. package/dist/version.js +26 -0
  343. package/dist/version.js.map +1 -0
  344. package/dist/workspace.d.ts +48 -0
  345. package/dist/workspace.d.ts.map +1 -0
  346. package/dist/workspace.js +126 -0
  347. package/dist/workspace.js.map +1 -0
  348. package/docker-compose.yml +61 -0
  349. package/package.json +72 -1
package/dist/agent.js ADDED
@@ -0,0 +1,204 @@
1
+ // This Source Code Form is subject to the terms of the Mozilla Public
2
+ // License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
+ import { query } from '@anthropic-ai/claude-agent-sdk';
5
+ import { harnessOptions } from './harness.js';
6
+ import { refreshCatalogue } from './models.js';
7
+ import { Spend } from './usage.js';
8
+ import { capture, createMemoryStore, formatMemories, recall, resolveScope } from './memory/index.js';
9
+ /** Recall keys off the newest user turn, not the whole replayed history. */
10
+ function latestUserText(input) {
11
+ if (typeof input === 'string')
12
+ return input;
13
+ for (let i = input.length - 1; i >= 0; i--) {
14
+ if (input[i].role === 'user')
15
+ return input[i].content;
16
+ }
17
+ return '';
18
+ }
19
+ /**
20
+ * Replays a conversation as the SDK's streaming input.
21
+ *
22
+ * The SDK takes either one prompt or an async iterable of user messages, and
23
+ * has no notion of a replayed assistant turn — it keeps its own transcript.
24
+ * A resumed Weigh session therefore arrives as the history rendered into a
25
+ * single opening message rather than as alternating turns. Lossier than
26
+ * `resume`, and deliberate: Weigh's transcripts are its own, readable without
27
+ * the SDK, and are what `weigh sessions` operates on.
28
+ */
29
+ function asPrompt(input) {
30
+ if (typeof input === 'string')
31
+ return input;
32
+ if (input.length === 1)
33
+ return input[0].content;
34
+ return input.map((m) => `${m.role}: ${m.content}`).join('\n\n');
35
+ }
36
+ export async function runAgent(config, input, options) {
37
+ const startedAt = Date.now();
38
+ const repoRoot = options?.repoRoot ?? process.cwd();
39
+ const store = options?.memory ?? createMemoryStore(config.memory);
40
+ // Two scopes, because reading and writing want different ones. A memory is
41
+ // written under the run that produced it, so it can be traced back; it is
42
+ // recalled across every run, because that is the whole point of the long-term
43
+ // axis. One scope for both would narrow recall to the session that was still
44
+ // being typed.
45
+ const scope = resolveScope(config, options?.runId);
46
+ const recallScope = resolveScope(config);
47
+ const query_ = latestUserText(input);
48
+ // A backend that failed on recall will fail identically on capture, so one
49
+ // warning per run is enough — note the failure and skip the second attempt.
50
+ let memoryDegraded = false;
51
+ const onWarn = (message) => {
52
+ memoryDegraded = true;
53
+ options?.onEvent?.({ type: 'warning', message });
54
+ };
55
+ // Recall first — the memories become part of the instructions, so this has to
56
+ // settle before the model call goes out.
57
+ const memories = await recall(store, query_, recallScope, {
58
+ topK: config.memory.topK,
59
+ ...(config.memory.threshold !== undefined && { threshold: config.memory.threshold }),
60
+ onWarn,
61
+ });
62
+ if (store.enabled) {
63
+ options?.onEvent?.({ type: 'memory_recall', count: memories.length, backend: store.backend });
64
+ }
65
+ const instructions = [formatMemories(memories), config.systemPrompt.replace('{repoRoot}', repoRoot)]
66
+ .filter(Boolean)
67
+ .join('\n\n');
68
+ const controller = new AbortController();
69
+ const onAbort = () => controller.abort();
70
+ options?.signal?.addEventListener('abort', onAbort);
71
+ if (options?.signal?.aborted)
72
+ controller.abort();
73
+ let text = '';
74
+ let result;
75
+ const seen = new Set();
76
+ const run = query({
77
+ prompt: asPrompt(input),
78
+ options: {
79
+ ...harnessOptions({
80
+ root: repoRoot,
81
+ instructions,
82
+ model: config.model,
83
+ effort: config.effort,
84
+ maxSteps: config.maxSteps,
85
+ maxCost: config.maxCost,
86
+ hooks: {
87
+ ...(options?.onEvent && {
88
+ onCall: (name, detail) => options.onEvent({ type: 'tool_call', name, callId: '', args: detail ? { detail } : {} }),
89
+ }),
90
+ },
91
+ }),
92
+ // The controller is what actually cancels: without handing it to the
93
+ // SDK, aborting only stops memory capture afterwards while the stream
94
+ // runs to completion. `harnessOptions` has no field for it because a
95
+ // run's isolation and its lifetime are separate concerns.
96
+ abortController: controller,
97
+ },
98
+ });
99
+ // Free: answered before the stream is consumed, on a subprocess that exists
100
+ // either way. Keeps a short model name pointing at the current model.
101
+ refreshCatalogue(run);
102
+ try {
103
+ for await (const message of run) {
104
+ if (message.type === 'assistant') {
105
+ for (const block of message.message.content) {
106
+ // Assistant messages arrive whole and are re-sent as the turn grows,
107
+ // so a naive append prints the same paragraph several times. Emit
108
+ // each block once, keyed on what it says.
109
+ if (block.type === 'text') {
110
+ if (seen.has(block.text))
111
+ continue;
112
+ seen.add(block.text);
113
+ text += block.text;
114
+ options?.onEvent?.({ type: 'text', delta: block.text });
115
+ }
116
+ else if (block.type === 'thinking' && block.thinking) {
117
+ if (seen.has(block.thinking))
118
+ continue;
119
+ seen.add(block.thinking);
120
+ options?.onEvent?.({ type: 'reasoning', delta: block.thinking });
121
+ }
122
+ }
123
+ options?.onEvent?.({ type: 'turn_end' });
124
+ }
125
+ else if (message.type === 'user') {
126
+ // A user message mid-run is a tool result being handed back.
127
+ for (const block of message.message.content ?? []) {
128
+ if (typeof block === 'string' || block.type !== 'tool_result')
129
+ continue;
130
+ const out = typeof block.content === 'string' ? block.content : JSON.stringify(block.content);
131
+ options?.onEvent?.({
132
+ type: 'tool_result',
133
+ name: 'tool',
134
+ callId: block.tool_use_id ?? '',
135
+ output: out.length > 200 ? out.slice(0, 200) + '...' : out,
136
+ });
137
+ }
138
+ }
139
+ else if (message.type === 'result') {
140
+ result = message;
141
+ if (message.subtype === 'success' && !text)
142
+ text = message.result;
143
+ }
144
+ }
145
+ }
146
+ finally {
147
+ options?.signal?.removeEventListener('abort', onAbort);
148
+ }
149
+ // Capture after the run so Mem0 extracts from a completed exchange. Awaited,
150
+ // not fire-and-forget: a CLI process exits the moment this returns.
151
+ if (config.memory.autoCapture && !options?.signal?.aborted && !memoryDegraded) {
152
+ const written = await capture(store, [
153
+ { role: 'user', content: query_ },
154
+ { role: 'assistant', content: text },
155
+ ], scope, { metadata: { model: config.model, repoRoot }, onWarn });
156
+ if (store.enabled) {
157
+ options?.onEvent?.({ type: 'memory_capture', count: written.length, backend: store.backend });
158
+ }
159
+ }
160
+ // `Spend.fromResult`, the same accounting a review reports, rather than
161
+ // `result.usage`. That field covers the main loop alone: the SDK makes
162
+ // auxiliary calls of its own — measured at 80k input tokens through Haiku on
163
+ // one run — and reading it here produced figures like 10 input tokens for a
164
+ // run that had just read a six-hundred-line file. It also carries the cost,
165
+ // which was absent entirely.
166
+ const usage = result ? Spend.fromResult(result) : null;
167
+ const durationMs = Date.now() - startedAt;
168
+ options?.onEvent?.({ type: 'done', usage, durationMs });
169
+ return { text, usage, output: result, durationMs, memories };
170
+ }
171
+ /**
172
+ * Retry on 429/5xx, and only while no tool has executed yet.
173
+ *
174
+ * The guard predates the read-only tool surface, when replaying a run could
175
+ * repeat a write. Nothing in the request mutates now, so what it buys is
176
+ * narrower and still worth having: a run that has already read half a
177
+ * repository has already been paid for, and replaying it from the prompt
178
+ * spends that again for the same answer.
179
+ */
180
+ export async function runAgentWithRetry(config, input, options) {
181
+ for (let attempt = 0, max = options?.maxRetries ?? 3; attempt <= max; attempt++) {
182
+ let toolCallsMade = 0;
183
+ const wrappedOptions = {
184
+ ...options,
185
+ onEvent: (event) => {
186
+ if (event.type === 'tool_call')
187
+ toolCallsMade++;
188
+ options?.onEvent?.(event);
189
+ },
190
+ };
191
+ try {
192
+ return await runAgent(config, input, wrappedOptions);
193
+ }
194
+ catch (err) {
195
+ const s = err?.status ?? err?.statusCode;
196
+ const retryable = s === 429 || (s >= 500 && s < 600);
197
+ if (!retryable || attempt === max || toolCallsMade > 0)
198
+ throw err;
199
+ await new Promise((r) => setTimeout(r, Math.min(1000 * 2 ** attempt, 30000)));
200
+ }
201
+ }
202
+ throw new Error('Unreachable');
203
+ }
204
+ //# sourceMappingURL=agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,sEAAsE;AACtE,4DAA4D;AAE5D,OAAO,EAAE,KAAK,EAAwC,MAAM,gCAAgC,CAAC;AAE7F,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,KAAK,EAAoB,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE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AAC;QACF,IAAI,CAAC;YACH,OAAO,MAAM,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,cAAc,CAAC,CAAC;QACvD,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,MAAM,CAAC,GAAG,GAAG,EAAE,MAAM,IAAI,GAAG,EAAE,UAAU,CAAC;YACzC,MAAM,SAAS,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC;YACrD,IAAI,CAAC,SAAS,IAAI,OAAO,KAAK,GAAG,IAAI,aAAa,GAAG,CAAC;gBAAE,MAAM,GAAG,CAAC;YAClE,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,IAAI,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QAChF,CAAC;IACH,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;AACjC,CAAC"}
@@ -0,0 +1,32 @@
1
+ /** Why a run stopped before it finished, when it did. */
2
+ export interface StoppedReason {
3
+ reason: 'cost' | 'steps' | 'error';
4
+ /** A sentence, ending in a full stop, naming the fix where there is one. */
5
+ detail: string;
6
+ }
7
+ /**
8
+ * Reads the ceilings back out of a run that hit one.
9
+ *
10
+ * Here rather than on the reviewer because there are two callers now, and
11
+ * because of how the SDK reports this. A ceiling arrives two different ways —
12
+ * `maxTurns` as an `error_max_turns` result, `maxBudgetUsd` as a throw out of
13
+ * the iterator with no code on it — so recognising one means matching a
14
+ * message against a regular expression. That is a thing to own in one place.
15
+ * Copied into a second caller it rots quietly: the SDK rewords the message,
16
+ * one copy is updated, and the other starts reporting a ceiling as a fault.
17
+ */
18
+ export declare class Ceilings {
19
+ private readonly _maxSteps;
20
+ private readonly _maxCost;
21
+ constructor(_maxSteps: number, _maxCost: number);
22
+ /** The ceiling behind a thrown iteration, or undefined if it was a fault. */
23
+ fromError(err: unknown): StoppedReason | undefined;
24
+ /** Why a result that is not a success is not a success. */
25
+ fromResult(result: {
26
+ subtype: string;
27
+ errors?: readonly string[];
28
+ }): StoppedReason;
29
+ private _cost;
30
+ private _steps;
31
+ }
32
+ //# sourceMappingURL=ceilings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ceilings.d.ts","sourceRoot":"","sources":["../src/ceilings.ts"],"names":[],"mappings":"AAIA,yDAAyD;AACzD,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;IACnC,4EAA4E;IAC5E,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;GAUG;AACH,qBAAa,QAAQ;IAEjB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAF3B,YACmB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAC/B;IAEJ,6EAA6E;IAC7E,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,aAAa,GAAG,SAAS,CAKjD;IAED,2DAA2D;IAC3D,UAAU,CAAC,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,GAAG,aAAa,CAcjF;IAED,OAAO,CAAC,KAAK;IAUb,OAAO,CAAC,MAAM;CAMf"}
@@ -0,0 +1,61 @@
1
+ // This Source Code Form is subject to the terms of the Mozilla Public
2
+ // License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
+ /**
5
+ * Reads the ceilings back out of a run that hit one.
6
+ *
7
+ * Here rather than on the reviewer because there are two callers now, and
8
+ * because of how the SDK reports this. A ceiling arrives two different ways —
9
+ * `maxTurns` as an `error_max_turns` result, `maxBudgetUsd` as a throw out of
10
+ * the iterator with no code on it — so recognising one means matching a
11
+ * message against a regular expression. That is a thing to own in one place.
12
+ * Copied into a second caller it rots quietly: the SDK rewords the message,
13
+ * one copy is updated, and the other starts reporting a ceiling as a fault.
14
+ */
15
+ export class Ceilings {
16
+ _maxSteps;
17
+ _maxCost;
18
+ constructor(_maxSteps, _maxCost) {
19
+ this._maxSteps = _maxSteps;
20
+ this._maxCost = _maxCost;
21
+ }
22
+ /** The ceiling behind a thrown iteration, or undefined if it was a fault. */
23
+ fromError(err) {
24
+ const message = err instanceof Error ? err.message : String(err);
25
+ if (/maximum budget|max budget/i.test(message))
26
+ return this._cost();
27
+ if (/maximum (number of )?turns|max turns/i.test(message))
28
+ return this._steps();
29
+ return undefined;
30
+ }
31
+ /** Why a result that is not a success is not a success. */
32
+ fromResult(result) {
33
+ switch (result.subtype) {
34
+ case 'error_max_turns':
35
+ return this._steps();
36
+ case 'error_max_budget_usd':
37
+ return this._cost();
38
+ default:
39
+ return {
40
+ reason: 'error',
41
+ detail: `the run ended as ${result.subtype}${result.errors?.length ? ` — ${result.errors[0]}` : ''}.`,
42
+ };
43
+ }
44
+ }
45
+ _cost() {
46
+ return {
47
+ reason: 'cost',
48
+ // Rounded, because this number can be arithmetic rather than a figure
49
+ // someone typed: what is left of a ceiling after a first call is a
50
+ // float, and `$0.8765432109999999` reads as a bug in the tool.
51
+ detail: `it reached the ceiling of $${this._maxCost.toFixed(2)}. Raise --max-cost to let it run longer.`,
52
+ };
53
+ }
54
+ _steps() {
55
+ return {
56
+ reason: 'steps',
57
+ detail: `it reached the ceiling of ${this._maxSteps} steps. Raise --max-steps to let it run longer.`,
58
+ };
59
+ }
60
+ }
61
+ //# sourceMappingURL=ceilings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ceilings.js","sourceRoot":"","sources":["../src/ceilings.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,sEAAsE;AACtE,4DAA4D;AAS5D;;;;;;;;;;GAUG;AACH,MAAM,OAAO,QAAQ;IAEA,SAAS;IAC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ACrG,CAAC;IACJ,CAAC;CACF"}
package/dist/cli.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ import './quiet.js';
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAeA,OAAO,YAAY,CAAC"}