@amodalai/runtime 0.2.0 → 0.2.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 (209) hide show
  1. package/dist/src/__fixtures__/README.md +4 -0
  2. package/dist/src/__fixtures__/e2e.test.d.ts +6 -0
  3. package/dist/src/__fixtures__/e2e.test.js +211 -0
  4. package/dist/src/__fixtures__/e2e.test.js.map +1 -0
  5. package/dist/src/__fixtures__/smoke-agent/automations/delivery-callback-test.json +9 -0
  6. package/dist/src/__fixtures__/smoke-agent/connections/mock-mcp/spec.json +1 -1
  7. package/dist/src/__fixtures__/smoke.test.js +715 -29
  8. package/dist/src/__fixtures__/smoke.test.js.map +1 -1
  9. package/dist/src/__fixtures__/test-env.d.ts +27 -0
  10. package/dist/src/__fixtures__/test-env.js +64 -0
  11. package/dist/src/__fixtures__/test-env.js.map +1 -0
  12. package/dist/src/__fixtures__/test-helpers.d.ts +30 -0
  13. package/dist/src/__fixtures__/test-helpers.js +120 -0
  14. package/dist/src/__fixtures__/test-helpers.js.map +1 -0
  15. package/dist/src/agent/agent-types.d.ts +22 -0
  16. package/dist/src/agent/agent-types.js.map +1 -1
  17. package/dist/src/agent/automation-bridge.d.ts +9 -0
  18. package/dist/src/agent/automation-bridge.js +26 -0
  19. package/dist/src/agent/automation-bridge.js.map +1 -1
  20. package/dist/src/agent/automation-bridge.test.js +63 -0
  21. package/dist/src/agent/automation-bridge.test.js.map +1 -1
  22. package/dist/src/agent/local-server.d.ts +0 -7
  23. package/dist/src/agent/local-server.js +230 -86
  24. package/dist/src/agent/local-server.js.map +1 -1
  25. package/dist/src/agent/local-server.test.js +14 -8
  26. package/dist/src/agent/local-server.test.js.map +1 -1
  27. package/dist/src/agent/loop-types.d.ts +81 -2
  28. package/dist/src/agent/loop-types.js +4 -0
  29. package/dist/src/agent/loop-types.js.map +1 -1
  30. package/dist/src/agent/loop.js +16 -3
  31. package/dist/src/agent/loop.js.map +1 -1
  32. package/dist/src/agent/loop.test.js +572 -8
  33. package/dist/src/agent/loop.test.js.map +1 -1
  34. package/dist/src/agent/proactive/delivery-router.d.ts +68 -0
  35. package/dist/src/agent/proactive/delivery-router.js +337 -0
  36. package/dist/src/agent/proactive/delivery-router.js.map +1 -0
  37. package/dist/src/agent/proactive/delivery-router.test.d.ts +6 -0
  38. package/dist/src/agent/proactive/delivery-router.test.js +455 -0
  39. package/dist/src/agent/proactive/delivery-router.test.js.map +1 -0
  40. package/dist/src/agent/proactive/proactive-runner.d.ts +23 -1
  41. package/dist/src/agent/proactive/proactive-runner.js +42 -10
  42. package/dist/src/agent/proactive/proactive-runner.js.map +1 -1
  43. package/dist/src/agent/proactive/proactive-runner.test.js +0 -2
  44. package/dist/src/agent/proactive/proactive-runner.test.js.map +1 -1
  45. package/dist/src/agent/routes/admin-chat-abort.test.d.ts +6 -0
  46. package/dist/src/agent/routes/admin-chat-abort.test.js +206 -0
  47. package/dist/src/agent/routes/admin-chat-abort.test.js.map +1 -0
  48. package/dist/src/agent/routes/admin-chat.js +0 -2
  49. package/dist/src/agent/routes/admin-chat.js.map +1 -1
  50. package/dist/src/agent/routes/task.test.js +0 -2
  51. package/dist/src/agent/routes/task.test.js.map +1 -1
  52. package/dist/src/agent/snapshot-server.js +0 -2
  53. package/dist/src/agent/snapshot-server.js.map +1 -1
  54. package/dist/src/agent/states/compacting.js +5 -3
  55. package/dist/src/agent/states/compacting.js.map +1 -1
  56. package/dist/src/agent/states/confirming.js +3 -0
  57. package/dist/src/agent/states/confirming.js.map +1 -1
  58. package/dist/src/agent/states/dispatching.js +45 -1
  59. package/dist/src/agent/states/dispatching.js.map +1 -1
  60. package/dist/src/agent/states/executing.js +225 -81
  61. package/dist/src/agent/states/executing.js.map +1 -1
  62. package/dist/src/agent/states/streaming.js +14 -0
  63. package/dist/src/agent/states/streaming.js.map +1 -1
  64. package/dist/src/agent/states/thinking.d.ts +1 -1
  65. package/dist/src/agent/states/thinking.js +246 -29
  66. package/dist/src/agent/states/thinking.js.map +1 -1
  67. package/dist/src/agent/token-estimate.d.ts +20 -6
  68. package/dist/src/agent/token-estimate.js +24 -3
  69. package/dist/src/agent/token-estimate.js.map +1 -1
  70. package/dist/src/agent/token-estimate.test.d.ts +6 -0
  71. package/dist/src/agent/token-estimate.test.js +44 -0
  72. package/dist/src/agent/token-estimate.test.js.map +1 -0
  73. package/dist/src/api/create-agent.js +0 -3
  74. package/dist/src/api/create-agent.js.map +1 -1
  75. package/dist/src/api/types.d.ts +0 -2
  76. package/dist/src/env-ref.d.ts +13 -0
  77. package/dist/src/env-ref.js +31 -0
  78. package/dist/src/env-ref.js.map +1 -0
  79. package/dist/src/env-ref.test.d.ts +6 -0
  80. package/dist/src/env-ref.test.js +34 -0
  81. package/dist/src/env-ref.test.js.map +1 -0
  82. package/dist/src/errors.d.ts +15 -0
  83. package/dist/src/errors.js +22 -0
  84. package/dist/src/errors.js.map +1 -1
  85. package/dist/src/errors.test.js +2 -2
  86. package/dist/src/errors.test.js.map +1 -1
  87. package/dist/src/events/event-bus.d.ts +54 -0
  88. package/dist/src/events/event-bus.js +84 -0
  89. package/dist/src/events/event-bus.js.map +1 -0
  90. package/dist/src/events/event-bus.test.d.ts +6 -0
  91. package/dist/src/events/event-bus.test.js +112 -0
  92. package/dist/src/events/event-bus.test.js.map +1 -0
  93. package/dist/src/events/events-route.d.ts +36 -0
  94. package/dist/src/events/events-route.js +80 -0
  95. package/dist/src/events/events-route.js.map +1 -0
  96. package/dist/src/events/events-route.test.d.ts +6 -0
  97. package/dist/src/events/events-route.test.js +134 -0
  98. package/dist/src/events/events-route.test.js.map +1 -0
  99. package/dist/src/events/store-event-wrapper.d.ts +19 -0
  100. package/dist/src/events/store-event-wrapper.js +57 -0
  101. package/dist/src/events/store-event-wrapper.js.map +1 -0
  102. package/dist/src/events/store-event-wrapper.test.d.ts +6 -0
  103. package/dist/src/events/store-event-wrapper.test.js +91 -0
  104. package/dist/src/events/store-event-wrapper.test.js.map +1 -0
  105. package/dist/src/middleware/auth.d.ts +0 -2
  106. package/dist/src/middleware/auth.js.map +1 -1
  107. package/dist/src/providers/search-provider.d.ts +64 -0
  108. package/dist/src/providers/search-provider.js +174 -0
  109. package/dist/src/providers/search-provider.js.map +1 -0
  110. package/dist/src/providers/types.d.ts +8 -0
  111. package/dist/src/routes/ai-stream.d.ts +15 -0
  112. package/dist/src/routes/ai-stream.js +9 -0
  113. package/dist/src/routes/ai-stream.js.map +1 -1
  114. package/dist/src/routes/chat-stream.d.ts +6 -0
  115. package/dist/src/routes/chat-stream.js +2 -0
  116. package/dist/src/routes/chat-stream.js.map +1 -1
  117. package/dist/src/routes/chat.d.ts +6 -0
  118. package/dist/src/routes/chat.js +2 -0
  119. package/dist/src/routes/chat.js.map +1 -1
  120. package/dist/src/routes/session-resolver.d.ts +5 -0
  121. package/dist/src/routes/session-resolver.js +1 -15
  122. package/dist/src/routes/session-resolver.js.map +1 -1
  123. package/dist/src/routes/session-resolver.test.js +7 -6
  124. package/dist/src/routes/session-resolver.test.js.map +1 -1
  125. package/dist/src/server.d.ts +6 -0
  126. package/dist/src/server.js +2 -0
  127. package/dist/src/server.js.map +1 -1
  128. package/dist/src/session/drizzle-session-store.d.ts +56 -0
  129. package/dist/src/session/drizzle-session-store.js +203 -0
  130. package/dist/src/session/drizzle-session-store.js.map +1 -0
  131. package/dist/src/session/manager.d.ts +6 -3
  132. package/dist/src/session/manager.js +46 -16
  133. package/dist/src/session/manager.js.map +1 -1
  134. package/dist/src/session/manager.test.js +12 -18
  135. package/dist/src/session/manager.test.js.map +1 -1
  136. package/dist/src/session/pglite-session-store.d.ts +23 -0
  137. package/dist/src/session/pglite-session-store.js +70 -0
  138. package/dist/src/session/pglite-session-store.js.map +1 -0
  139. package/dist/src/session/postgres-session-store.d.ts +44 -0
  140. package/dist/src/session/postgres-session-store.js +138 -0
  141. package/dist/src/session/postgres-session-store.js.map +1 -0
  142. package/dist/src/session/session-builder.d.ts +0 -2
  143. package/dist/src/session/session-builder.js +22 -2
  144. package/dist/src/session/session-builder.js.map +1 -1
  145. package/dist/src/session/session-builder.test.js +0 -2
  146. package/dist/src/session/session-builder.test.js.map +1 -1
  147. package/dist/src/session/session-store-selector.d.ts +49 -0
  148. package/dist/src/session/session-store-selector.js +60 -0
  149. package/dist/src/session/session-store-selector.js.map +1 -0
  150. package/dist/src/session/session-store-selector.test.d.ts +6 -0
  151. package/dist/src/session/session-store-selector.test.js +79 -0
  152. package/dist/src/session/session-store-selector.test.js.map +1 -0
  153. package/dist/src/session/store.d.ts +146 -32
  154. package/dist/src/session/store.js +126 -138
  155. package/dist/src/session/store.js.map +1 -1
  156. package/dist/src/session/store.test.js +385 -107
  157. package/dist/src/session/store.test.js.map +1 -1
  158. package/dist/src/session/tool-context-factory.d.ts +3 -2
  159. package/dist/src/session/tool-context-factory.js +1 -2
  160. package/dist/src/session/tool-context-factory.js.map +1 -1
  161. package/dist/src/session/tool-context-factory.test.js +1 -4
  162. package/dist/src/session/tool-context-factory.test.js.map +1 -1
  163. package/dist/src/session/types.d.ts +13 -6
  164. package/dist/src/stores/schema.d.ts +0 -34
  165. package/dist/src/stores/schema.js +6 -4
  166. package/dist/src/stores/schema.js.map +1 -1
  167. package/dist/src/tools/admin-file-tools.d.ts +29 -0
  168. package/dist/src/tools/admin-file-tools.js +525 -11
  169. package/dist/src/tools/admin-file-tools.js.map +1 -1
  170. package/dist/src/tools/admin-file-tools.test.js +373 -4
  171. package/dist/src/tools/admin-file-tools.test.js.map +1 -1
  172. package/dist/src/tools/custom-tool-adapter.test.js +0 -1
  173. package/dist/src/tools/custom-tool-adapter.test.js.map +1 -1
  174. package/dist/src/tools/dispatch-tool.d.ts +4 -4
  175. package/dist/src/tools/fetch-url-tool.d.ts +23 -0
  176. package/dist/src/tools/fetch-url-tool.js +333 -0
  177. package/dist/src/tools/fetch-url-tool.js.map +1 -0
  178. package/dist/src/tools/fetch-url-tool.test.d.ts +6 -0
  179. package/dist/src/tools/fetch-url-tool.test.js +228 -0
  180. package/dist/src/tools/fetch-url-tool.test.js.map +1 -0
  181. package/dist/src/tools/mcp-tool-adapter.test.js +0 -1
  182. package/dist/src/tools/mcp-tool-adapter.test.js.map +1 -1
  183. package/dist/src/tools/registry.test.js +0 -1
  184. package/dist/src/tools/registry.test.js.map +1 -1
  185. package/dist/src/tools/request-tool.test.js +0 -1
  186. package/dist/src/tools/request-tool.test.js.map +1 -1
  187. package/dist/src/tools/store-tools.test.js +0 -1
  188. package/dist/src/tools/store-tools.test.js.map +1 -1
  189. package/dist/src/tools/types.d.ts +20 -2
  190. package/dist/src/tools/web-search-tool.d.ts +31 -0
  191. package/dist/src/tools/web-search-tool.js +170 -0
  192. package/dist/src/tools/web-search-tool.js.map +1 -0
  193. package/dist/src/tools/web-search-tool.test.d.ts +6 -0
  194. package/dist/src/tools/web-search-tool.test.js +153 -0
  195. package/dist/src/tools/web-search-tool.test.js.map +1 -0
  196. package/dist/src/tools/web-tools-shared.d.ts +21 -0
  197. package/dist/src/tools/web-tools-shared.js +32 -0
  198. package/dist/src/tools/web-tools-shared.js.map +1 -0
  199. package/dist/src/types.d.ts +20 -0
  200. package/dist/src/types.js +13 -0
  201. package/dist/src/types.js.map +1 -1
  202. package/dist/tsconfig.tsbuildinfo +1 -1
  203. package/package.json +17 -3
  204. package/dist/src/agent/session-store.d.ts +0 -71
  205. package/dist/src/agent/session-store.js +0 -151
  206. package/dist/src/agent/session-store.js.map +0 -1
  207. package/dist/src/session/admin-file-tools.d.ts +0 -136
  208. package/dist/src/session/admin-file-tools.js +0 -240
  209. package/dist/src/session/admin-file-tools.js.map +0 -1
@@ -97,7 +97,6 @@ function makeMockContext(overrides) {
97
97
  logger,
98
98
  signal: new AbortController().signal,
99
99
  sessionId: 'test-session',
100
- tenantId: 'test-tenant',
101
100
  user: { roles: ['user'] },
102
101
  systemPrompt: 'You are a helpful assistant.',
103
102
  messages: [],
@@ -108,6 +107,8 @@ function makeMockContext(overrides) {
108
107
  config: { ...DEFAULT_LOOP_CONFIG },
109
108
  compactionFailures: 0,
110
109
  preExecutionCache: new Map(),
110
+ confirmedCallIds: new Set(),
111
+ disabledToolsUntilTurn: new Map(),
111
112
  waitForConfirmation: vi.fn().mockResolvedValue(true),
112
113
  buildToolContext: vi.fn().mockReturnValue({
113
114
  request: vi.fn(),
@@ -117,7 +118,6 @@ function makeMockContext(overrides) {
117
118
  user: { roles: [] },
118
119
  signal: new AbortController().signal,
119
120
  sessionId: 'test-session',
120
- tenantId: 'test-tenant',
121
121
  }),
122
122
  ...overrides,
123
123
  };
@@ -192,12 +192,14 @@ describe('handleThinking (via transition)', () => {
192
192
  expect(errorEvents.length).toBe(1);
193
193
  });
194
194
  it('detects loops with similar (not identical) parameters', async () => {
195
- // Build messages where the same tool is called with slightly different params
195
+ // Build messages where the same tool is called with slightly different
196
+ // params. Use a non-pagination key (retry_count) — pagination keys
197
+ // (offset/limit/page/cursor) are treated as iteration, not loops.
196
198
  const messages = [];
197
199
  for (let i = 0; i < 8; i++) {
198
200
  messages.push({
199
201
  role: 'assistant',
200
- content: [{ type: 'tool-call', toolCallId: `c${i}`, toolName: 'search_api', input: { query: 'test', page: i } }],
202
+ content: [{ type: 'tool-call', toolCallId: `c${i}`, toolName: 'search_api', input: { query: 'test', retry_count: i } }],
201
203
  });
202
204
  messages.push({
203
205
  role: 'tool',
@@ -212,6 +214,195 @@ describe('handleThinking (via transition)', () => {
212
214
  expect(result.next.reason).toBe('loop_detected');
213
215
  }
214
216
  });
217
+ it('does NOT detect pagination as a loop (offset/limit/page variants)', async () => {
218
+ // Agent walking a long file in chunks is legitimate iteration — same
219
+ // tool, same path, different offset. Must not trip the loop detector.
220
+ const messages = [];
221
+ for (let i = 0; i < 8; i++) {
222
+ messages.push({
223
+ role: 'assistant',
224
+ content: [{
225
+ type: 'tool-call',
226
+ toolCallId: `c${i}`,
227
+ toolName: 'read_repo_file',
228
+ input: { path: 'knowledge/big.md', offset: 1 + i * 2000, limit: 2000 },
229
+ }],
230
+ });
231
+ messages.push({
232
+ role: 'tool',
233
+ content: [{ type: 'tool-result', toolCallId: `c${i}`, toolName: 'read_repo_file', output: { type: 'text', value: 'chunk' } }],
234
+ });
235
+ }
236
+ const ctx = makeMockContext();
237
+ const result = await transition({ type: 'thinking', messages }, ctx);
238
+ // Should NOT be done — the loop detector should have skipped these.
239
+ expect(result.next.type).not.toBe('done');
240
+ });
241
+ it('replaces old tool results with summarizer output when hook is set', async () => {
242
+ // Build 20 tool-result messages so clearing triggers (threshold=15 by default)
243
+ const messages = [];
244
+ for (let i = 0; i < 20; i++) {
245
+ messages.push({
246
+ role: 'tool',
247
+ content: [{
248
+ type: 'tool-result',
249
+ toolCallId: `c${i}`,
250
+ toolName: 'search_api',
251
+ output: { type: 'text', value: `result body ${i} with lots of content` },
252
+ }],
253
+ });
254
+ }
255
+ const summarizer = vi.fn().mockResolvedValue('found 3 matching records');
256
+ const ctx = makeMockContext({ summarizeToolResult: summarizer });
257
+ await transition({ type: 'thinking', messages }, ctx);
258
+ // Summarizer should have been called for the cleared (non-kept) messages.
259
+ // threshold=15, keepRecent=5 → 15 cleared (all but the last 5).
260
+ expect(summarizer).toHaveBeenCalled();
261
+ expect(summarizer.mock.calls.length).toBe(15);
262
+ // Verify the summary is wired through — the messages passed to streamText
263
+ // should include the summary text in a cleared marker.
264
+ const streamTextCall = vi.mocked(ctx.provider.streamText).mock.calls[0][0];
265
+ const passedMessages = streamTextCall.messages;
266
+ const clearedMsg = passedMessages[0];
267
+ if (clearedMsg.role === 'tool' && Array.isArray(clearedMsg.content)) {
268
+ const part = clearedMsg.content[0];
269
+ if ('output' in part && part.output && typeof part.output === 'object' && 'value' in part.output) {
270
+ expect(String(part.output.value)).toContain('found 3 matching records');
271
+ expect(String(part.output.value)).toContain('search_api');
272
+ }
273
+ }
274
+ });
275
+ it('falls back to static marker when summarizer throws', async () => {
276
+ const messages = [];
277
+ for (let i = 0; i < 20; i++) {
278
+ messages.push({
279
+ role: 'tool',
280
+ content: [{
281
+ type: 'tool-result',
282
+ toolCallId: `c${i}`,
283
+ toolName: 'flaky_tool',
284
+ output: { type: 'text', value: `body ${i}` },
285
+ }],
286
+ });
287
+ }
288
+ const summarizer = vi.fn().mockRejectedValue(new Error('haiku unavailable'));
289
+ const ctx = makeMockContext({ summarizeToolResult: summarizer });
290
+ await transition({ type: 'thinking', messages }, ctx);
291
+ // Summarizer was called but threw; we should still proceed with static marker
292
+ expect(summarizer).toHaveBeenCalled();
293
+ const streamTextCall = vi.mocked(ctx.provider.streamText).mock.calls[0][0];
294
+ const clearedMsg = streamTextCall.messages[0];
295
+ if (clearedMsg.role === 'tool' && Array.isArray(clearedMsg.content)) {
296
+ const part = clearedMsg.content[0];
297
+ if ('output' in part && part.output && typeof part.output === 'object' && 'value' in part.output) {
298
+ expect(String(part.output.value)).toContain('Tool result cleared');
299
+ }
300
+ }
301
+ // The failure should have been logged
302
+ expect(ctx.logger.warn).toHaveBeenCalledWith('tool_result_summarization_failed', expect.objectContaining({ tool: 'flaky_tool' }));
303
+ });
304
+ it('skips already-cleared messages (idempotent)', async () => {
305
+ // Already-cleared messages keep their original toolCallId (otherwise
306
+ // providers reject "orphaned" assistant tool-calls). Detection is by
307
+ // output-value prefix: "[Tool result cleared..." or "[Summary of ...".
308
+ const messages = [];
309
+ for (let i = 0; i < 20; i++) {
310
+ messages.push({
311
+ role: 'tool',
312
+ content: [{
313
+ type: 'tool-result',
314
+ toolCallId: `c${i}`,
315
+ toolName: 'search_api',
316
+ // First 15 are already-cleared (marker prefix); last 5 are fresh
317
+ output: {
318
+ type: 'text',
319
+ value: i < 15 ? '[Tool result cleared to save context space]' : `body ${i}`,
320
+ },
321
+ }],
322
+ });
323
+ }
324
+ const summarizer = vi.fn().mockResolvedValue('summary');
325
+ const ctx = makeMockContext({ summarizeToolResult: summarizer });
326
+ await transition({ type: 'thinking', messages }, ctx);
327
+ // Clearing kicks in (20 > 15), but all 15 candidates are already cleared.
328
+ // Summarizer should NOT be called for already-cleared messages.
329
+ expect(summarizer).not.toHaveBeenCalled();
330
+ });
331
+ it('escalates at loopEscalationThreshold: stronger warn + removes looping tool', async () => {
332
+ // Build messages with 5 tool calls so count hits escalation (default=5)
333
+ // but not hard-stop (default=8).
334
+ const messages = [];
335
+ for (let i = 0; i < 5; i++) {
336
+ messages.push({
337
+ role: 'assistant',
338
+ content: [{ type: 'tool-call', toolCallId: `c${i}`, toolName: 'stuck_api', input: { q: 'same' } }],
339
+ });
340
+ messages.push({
341
+ role: 'tool',
342
+ content: [{ type: 'tool-result', toolCallId: `c${i}`, toolName: 'stuck_api', output: { type: 'text', value: 'no progress' } }],
343
+ });
344
+ }
345
+ // Registry has both the looping tool and another tool
346
+ const stuckTool = makeMockToolDef({ description: 'Stuck tool' });
347
+ const otherTool = makeMockToolDef({ description: 'Other tool' });
348
+ const registry = makeMockRegistry({ stuck_api: stuckTool, other_tool: otherTool });
349
+ const ctx = makeMockContext({ toolRegistry: registry });
350
+ const result = await transition({ type: 'thinking', messages }, ctx);
351
+ // Should still stream (not hard-stop)
352
+ expect(result.next.type).toBe('streaming');
353
+ // The escalation-level warn should have been logged
354
+ expect(ctx.logger.warn).toHaveBeenCalledWith('agent_loop_escalation', expect.objectContaining({ tool: 'stuck_api', count: 5 }));
355
+ // The looping tool should be EXCLUDED from this turn's tool set
356
+ const streamTextCall = vi.mocked(ctx.provider.streamText).mock.calls[0][0];
357
+ const passedTools = streamTextCall.tools;
358
+ expect(passedTools['stuck_api']).toBeUndefined();
359
+ expect(passedTools['other_tool']).toBeDefined();
360
+ // Escalation message should be appended
361
+ const lastMsg = streamTextCall.messages[streamTextCall.messages.length - 1];
362
+ expect(lastMsg.role).toBe('user');
363
+ if (typeof lastMsg.content === 'string') {
364
+ expect(lastMsg.content).toContain('temporarily disabled');
365
+ expect(lastMsg.content).toContain('stuck_api');
366
+ }
367
+ // The looping tool should be registered in the cooldown map, not just
368
+ // filtered once-off — so subsequent turns also skip it.
369
+ expect(ctx.disabledToolsUntilTurn.has('stuck_api')).toBe(true);
370
+ });
371
+ it('escalation cooldown keeps tool disabled across subsequent turns', async () => {
372
+ // Simulate a session where escalation fires at turn 5 with default
373
+ // cooldown of 3, then the agent moves on. The looping tool should be
374
+ // excluded from turns 5-7 and return at turn 8.
375
+ const stuckTool = makeMockToolDef({ description: 'Stuck tool' });
376
+ const otherTool = makeMockToolDef({ description: 'Other tool' });
377
+ const registry = makeMockRegistry({ stuck_api: stuckTool, other_tool: otherTool });
378
+ const ctx = makeMockContext({ toolRegistry: registry });
379
+ // Pre-populate the cooldown as if escalation fired at turn 5
380
+ ctx.turnCount = 4; // next turn will be 5
381
+ ctx.disabledToolsUntilTurn.set('stuck_api', 8); // disable until turn 8
382
+ // Turn 5: tool still disabled
383
+ await transition({ type: 'thinking', messages: [] }, ctx);
384
+ let streamTextCall = vi.mocked(ctx.provider.streamText).mock.calls[0][0];
385
+ let passedTools = streamTextCall.tools;
386
+ expect(passedTools['stuck_api']).toBeUndefined();
387
+ expect(passedTools['other_tool']).toBeDefined();
388
+ // Turn 6: still disabled
389
+ await transition({ type: 'thinking', messages: [] }, ctx);
390
+ streamTextCall = vi.mocked(ctx.provider.streamText).mock.calls[1][0];
391
+ passedTools = streamTextCall.tools;
392
+ expect(passedTools['stuck_api']).toBeUndefined();
393
+ // Turn 7: still disabled (turnCount=7, untilTurn=8)
394
+ await transition({ type: 'thinking', messages: [] }, ctx);
395
+ streamTextCall = vi.mocked(ctx.provider.streamText).mock.calls[2][0];
396
+ passedTools = streamTextCall.tools;
397
+ expect(passedTools['stuck_api']).toBeUndefined();
398
+ // Turn 8: cooldown expired — tool back in the set
399
+ await transition({ type: 'thinking', messages: [] }, ctx);
400
+ streamTextCall = vi.mocked(ctx.provider.streamText).mock.calls[3][0];
401
+ passedTools = streamTextCall.tools;
402
+ expect(passedTools['stuck_api']).toBeDefined();
403
+ // Map should be cleaned up after expiry
404
+ expect(ctx.disabledToolsUntilTurn.has('stuck_api')).toBe(false);
405
+ });
215
406
  it('injects warning when tool called 3+ times', async () => {
216
407
  const messages = [];
217
408
  for (let i = 0; i < 3; i++) {
@@ -231,7 +422,7 @@ describe('handleThinking (via transition)', () => {
231
422
  expect(result.next.type).toBe('streaming');
232
423
  const streamTextCall = vi.mocked(ctx.provider.streamText).mock.calls[0][0];
233
424
  const lastMsg = streamTextCall.messages[streamTextCall.messages.length - 1];
234
- expect(lastMsg.role).toBe('system');
425
+ expect(lastMsg.role).toBe('user');
235
426
  if (typeof lastMsg.content === 'string') {
236
427
  expect(lastMsg.content).toContain('flaky_api');
237
428
  expect(lastMsg.content).toContain('3 times');
@@ -364,6 +555,86 @@ describe('handleExecuting (via transition)', () => {
364
555
  expect(startEvents.length).toBe(1);
365
556
  expect(resultEvents.length).toBe(1);
366
557
  });
558
+ it('routes requiresConfirmation tools through CONFIRMING on first pass', async () => {
559
+ const destructiveTool = makeMockToolDef({
560
+ execute: vi.fn().mockResolvedValue({ deleted: true }),
561
+ requiresConfirmation: true,
562
+ });
563
+ const registry = makeMockRegistry({ delete_repo: destructiveTool });
564
+ const ctx = makeMockContext({ toolRegistry: registry });
565
+ const state = {
566
+ type: 'executing',
567
+ queue: [],
568
+ current: { toolCallId: 'call-danger', toolName: 'delete_repo', args: { name: 'foo' } },
569
+ results: [],
570
+ };
571
+ const result = await transition(state, ctx);
572
+ expect(result.next.type).toBe('confirming');
573
+ expect(destructiveTool.execute).not.toHaveBeenCalled();
574
+ // ConfirmationRequired SSE event should be emitted
575
+ const confirmEvents = result.effects.filter((e) => e.type === SSEEventType.ConfirmationRequired);
576
+ expect(confirmEvents.length).toBe(1);
577
+ });
578
+ it('executes requiresConfirmation tools after approval (no re-confirm loop)', async () => {
579
+ const destructiveTool = makeMockToolDef({
580
+ execute: vi.fn().mockResolvedValue({ deleted: true }),
581
+ requiresConfirmation: true,
582
+ });
583
+ const registry = makeMockRegistry({ delete_repo: destructiveTool });
584
+ // Pre-populate confirmedCallIds as if CONFIRMING already approved this call
585
+ const ctx = makeMockContext({ toolRegistry: registry });
586
+ ctx.confirmedCallIds.add('call-approved');
587
+ const state = {
588
+ type: 'executing',
589
+ queue: [],
590
+ current: { toolCallId: 'call-approved', toolName: 'delete_repo', args: { name: 'foo' } },
591
+ results: [],
592
+ };
593
+ const result = await transition(state, ctx);
594
+ // Should execute this time, not route back to CONFIRMING
595
+ expect(result.next.type).toBe('thinking');
596
+ expect(destructiveTool.execute).toHaveBeenCalledTimes(1);
597
+ });
598
+ it('connection tool does NOT re-prompt after confirmedCallIds marks the call', async () => {
599
+ // Regression test for the latent infinite-loop bug: a connection tool
600
+ // whose ACL gate returns requiresConfirmation=true used to re-route back
601
+ // to CONFIRMING on every pass, since the permission checker has no
602
+ // notion of "already approved." confirmedCallIds fixes this.
603
+ const connectionTool = makeMockToolDef({
604
+ execute: vi.fn().mockResolvedValue({ ok: true }),
605
+ metadata: { category: 'connection', connection: 'github' },
606
+ });
607
+ const registry = makeMockRegistry({ request: connectionTool });
608
+ const ctx = makeMockContext({
609
+ toolRegistry: registry,
610
+ permissionChecker: {
611
+ check: vi.fn().mockReturnValue({
612
+ allowed: true,
613
+ requiresConfirmation: true,
614
+ reason: 'Write to github requires confirmation',
615
+ }),
616
+ },
617
+ });
618
+ // Simulate: CONFIRMING has already approved this call
619
+ ctx.confirmedCallIds.add('call-gh-write');
620
+ const state = {
621
+ type: 'executing',
622
+ queue: [],
623
+ current: {
624
+ toolCallId: 'call-gh-write',
625
+ toolName: 'request',
626
+ args: { method: 'POST', endpoint: '/repos/foo', intent: 'confirmed_write' },
627
+ },
628
+ results: [],
629
+ };
630
+ const result = await transition(state, ctx);
631
+ // Should execute the connection tool instead of re-routing to CONFIRMING
632
+ expect(result.next.type).toBe('thinking');
633
+ expect(connectionTool.execute).toHaveBeenCalledTimes(1);
634
+ // No ConfirmationRequired SSE event should have been emitted
635
+ const confirmEvents = result.effects.filter((e) => e.type === SSEEventType.ConfirmationRequired);
636
+ expect(confirmEvents.length).toBe(0);
637
+ });
367
638
  it('transitions to compacting when context exceeds threshold', async () => {
368
639
  const tool = makeMockToolDef({
369
640
  // Return a large result to inflate context
@@ -540,6 +811,161 @@ describe('handleExecuting (via transition)', () => {
540
811
  // The cached result should be used — tool.execute should NOT be called again
541
812
  expect(readTool.execute).not.toHaveBeenCalled();
542
813
  });
814
+ // -------------------------------------------------------------------------
815
+ // Parallel tool calls: batch contiguous read-only calls
816
+ // -------------------------------------------------------------------------
817
+ it('batches contiguous read-only calls and runs them concurrently', async () => {
818
+ // Two read-only tools. Each sleeps before resolving. If batched in
819
+ // parallel, total wall time ≈ max(sleep). If sequential, ≈ sum(sleep).
820
+ const sleep = (ms) => new Promise((r) => setTimeout(() => r('ok'), ms));
821
+ const readA = makeMockToolDef({
822
+ readOnly: true,
823
+ execute: vi.fn(() => sleep(50)),
824
+ });
825
+ const readB = makeMockToolDef({
826
+ readOnly: true,
827
+ execute: vi.fn(() => sleep(50)),
828
+ });
829
+ const registry = makeMockRegistry({ read_a: readA, read_b: readB });
830
+ const ctx = makeMockContext({ toolRegistry: registry });
831
+ const state = {
832
+ type: 'executing',
833
+ queue: [{ toolCallId: 'call-b', toolName: 'read_b', args: {} }],
834
+ current: { toolCallId: 'call-a', toolName: 'read_a', args: {} },
835
+ results: [],
836
+ };
837
+ const startedAt = Date.now();
838
+ const result = await transition(state, ctx);
839
+ const elapsed = Date.now() - startedAt;
840
+ // Both executed
841
+ expect(readA.execute).toHaveBeenCalledTimes(1);
842
+ expect(readB.execute).toHaveBeenCalledTimes(1);
843
+ // Parallel: should finish in roughly one sleep, well under the sum
844
+ expect(elapsed).toBeLessThan(90);
845
+ // Batch drained the queue and transitioned to thinking in one step
846
+ expect(result.next.type).toBe('thinking');
847
+ // Both results appended to messages
848
+ const toolMessages = ctx.messages.filter((m) => m.role === 'tool');
849
+ expect(toolMessages).toHaveLength(2);
850
+ // Per-call SSE events emitted (2 start + 2 result)
851
+ const starts = result.effects.filter((e) => e.type === SSEEventType.ToolCallStart);
852
+ const results = result.effects.filter((e) => e.type === SSEEventType.ToolCallResult);
853
+ expect(starts).toHaveLength(2);
854
+ expect(results).toHaveLength(2);
855
+ });
856
+ it('stops batching at the first write (non-readOnly) tool', async () => {
857
+ const readTool = makeMockToolDef({ readOnly: true, execute: vi.fn().mockResolvedValue('r') });
858
+ const writeTool = makeMockToolDef({ readOnly: false, execute: vi.fn().mockResolvedValue('w') });
859
+ const registry = makeMockRegistry({ read: readTool, write: writeTool });
860
+ const ctx = makeMockContext({ toolRegistry: registry });
861
+ const state = {
862
+ type: 'executing',
863
+ queue: [
864
+ { toolCallId: 'call-read-2', toolName: 'read', args: {} },
865
+ { toolCallId: 'call-write', toolName: 'write', args: {} },
866
+ { toolCallId: 'call-read-3', toolName: 'read', args: {} },
867
+ ],
868
+ current: { toolCallId: 'call-read-1', toolName: 'read', args: {} },
869
+ results: [],
870
+ };
871
+ const result = await transition(state, ctx);
872
+ // Batched the two leading reads; the write stopped the batch
873
+ expect(readTool.execute).toHaveBeenCalledTimes(2);
874
+ expect(writeTool.execute).not.toHaveBeenCalled();
875
+ // Next state should process the write sequentially
876
+ expect(result.next.type).toBe('executing');
877
+ if (result.next.type === 'executing') {
878
+ expect(result.next.current.toolCallId).toBe('call-write');
879
+ expect(result.next.queue).toHaveLength(1);
880
+ expect(result.next.queue[0].toolCallId).toBe('call-read-3');
881
+ }
882
+ });
883
+ it('does not batch when the current call is a write', async () => {
884
+ const writeTool = makeMockToolDef({ readOnly: false, execute: vi.fn().mockResolvedValue('w') });
885
+ const readTool = makeMockToolDef({ readOnly: true, execute: vi.fn().mockResolvedValue('r') });
886
+ const registry = makeMockRegistry({ write: writeTool, read: readTool });
887
+ const ctx = makeMockContext({ toolRegistry: registry });
888
+ const state = {
889
+ type: 'executing',
890
+ queue: [{ toolCallId: 'call-read', toolName: 'read', args: {} }],
891
+ current: { toolCallId: 'call-write', toolName: 'write', args: {} },
892
+ results: [],
893
+ };
894
+ const result = await transition(state, ctx);
895
+ // Only write executes; read stays in queue for the next transition
896
+ expect(writeTool.execute).toHaveBeenCalledTimes(1);
897
+ expect(readTool.execute).not.toHaveBeenCalled();
898
+ expect(result.next.type).toBe('executing');
899
+ if (result.next.type === 'executing') {
900
+ expect(result.next.current.toolCallId).toBe('call-read');
901
+ }
902
+ });
903
+ it('does not batch connection tools (any call could transition to CONFIRMING)', async () => {
904
+ const connRead = makeMockToolDef({
905
+ readOnly: true,
906
+ execute: vi.fn().mockResolvedValue('x'),
907
+ metadata: { category: 'connection', connection: 'github' },
908
+ });
909
+ const registry = makeMockRegistry({ request: connRead });
910
+ const ctx = makeMockContext({ toolRegistry: registry });
911
+ const state = {
912
+ type: 'executing',
913
+ queue: [{ toolCallId: 'call-2', toolName: 'request', args: { method: 'GET', endpoint: '/x' } }],
914
+ current: { toolCallId: 'call-1', toolName: 'request', args: { method: 'GET', endpoint: '/y' } },
915
+ results: [],
916
+ };
917
+ const result = await transition(state, ctx);
918
+ // Only the first ran; the second stays queued for its own ACL check
919
+ expect(connRead.execute).toHaveBeenCalledTimes(1);
920
+ expect(result.next.type).toBe('executing');
921
+ });
922
+ it('does not batch read-only tools that require confirmation', async () => {
923
+ const gated = makeMockToolDef({
924
+ readOnly: true,
925
+ requiresConfirmation: true,
926
+ execute: vi.fn().mockResolvedValue('x'),
927
+ });
928
+ const registry = makeMockRegistry({ gated });
929
+ const ctx = makeMockContext({ toolRegistry: registry });
930
+ const state = {
931
+ type: 'executing',
932
+ queue: [{ toolCallId: 'call-2', toolName: 'gated', args: {} }],
933
+ current: { toolCallId: 'call-1', toolName: 'gated', args: {} },
934
+ results: [],
935
+ };
936
+ const result = await transition(state, ctx);
937
+ // Routes to CONFIRMING, no execution yet
938
+ expect(result.next.type).toBe('confirming');
939
+ expect(gated.execute).not.toHaveBeenCalled();
940
+ });
941
+ it('a failure in one batched call does not block the others', async () => {
942
+ const good = makeMockToolDef({ readOnly: true, execute: vi.fn().mockResolvedValue('ok') });
943
+ const bad = makeMockToolDef({
944
+ readOnly: true,
945
+ execute: vi.fn().mockRejectedValue(new Error('boom')),
946
+ });
947
+ const registry = makeMockRegistry({ good, bad });
948
+ const ctx = makeMockContext({ toolRegistry: registry });
949
+ const state = {
950
+ type: 'executing',
951
+ queue: [
952
+ { toolCallId: 'call-bad', toolName: 'bad', args: {} },
953
+ { toolCallId: 'call-good-2', toolName: 'good', args: {} },
954
+ ],
955
+ current: { toolCallId: 'call-good-1', toolName: 'good', args: {} },
956
+ results: [],
957
+ };
958
+ const result = await transition(state, ctx);
959
+ // All three tried, failure surfaced as an error tool-result for the bad one
960
+ expect(good.execute).toHaveBeenCalledTimes(2);
961
+ expect(bad.execute).toHaveBeenCalledTimes(1);
962
+ expect(result.next.type).toBe('thinking');
963
+ const toolMessages = ctx.messages.filter((m) => m.role === 'tool');
964
+ expect(toolMessages).toHaveLength(3);
965
+ // Error result is present in the SSE stream
966
+ const errorEvents = result.effects.filter((e) => e.type === SSEEventType.ToolCallResult && e.status === 'error');
967
+ expect(errorEvents).toHaveLength(1);
968
+ });
543
969
  });
544
970
  describe('handleConfirming (via transition)', () => {
545
971
  it('approved confirmation resumes executing', async () => {
@@ -571,6 +997,30 @@ describe('handleConfirming (via transition)', () => {
571
997
  // A denial message should have been appended
572
998
  expect(ctx.messages.length).toBeGreaterThan(0);
573
999
  });
1000
+ it('approved confirmation marks the callId in ctx.confirmedCallIds', async () => {
1001
+ const ctx = makeMockContext({
1002
+ waitForConfirmation: vi.fn().mockResolvedValue(true),
1003
+ });
1004
+ const state = {
1005
+ type: 'confirming',
1006
+ call: { toolCallId: 'call-XYZ', toolName: 'delete_item', args: { id: '123' } },
1007
+ remainingQueue: [],
1008
+ };
1009
+ await transition(state, ctx);
1010
+ expect(ctx.confirmedCallIds.has('call-XYZ')).toBe(true);
1011
+ });
1012
+ it('denied confirmation does NOT mark callId as confirmed', async () => {
1013
+ const ctx = makeMockContext({
1014
+ waitForConfirmation: vi.fn().mockResolvedValue(false),
1015
+ });
1016
+ const state = {
1017
+ type: 'confirming',
1018
+ call: { toolCallId: 'call-DENIED', toolName: 'delete_item', args: { id: '123' } },
1019
+ remainingQueue: [],
1020
+ };
1021
+ await transition(state, ctx);
1022
+ expect(ctx.confirmedCallIds.has('call-DENIED')).toBe(false);
1023
+ });
574
1024
  it('intercepts dispatch_task and transitions to DISPATCHING', async () => {
575
1025
  const dispatchTool = {
576
1026
  description: 'Dispatch sub-task',
@@ -659,9 +1109,10 @@ describe('handleCompacting (via transition)', () => {
659
1109
  if (result.next.type === 'thinking') {
660
1110
  // Should have fewer messages (summary + recent turns)
661
1111
  expect(result.next.messages.length).toBeLessThan(messages.length);
662
- // First message should be the system summary
1112
+ // First message should be the compaction summary (user role, not system,
1113
+ // because Anthropic rejects system messages after user/assistant turns)
663
1114
  const firstMsg = result.next.messages[0];
664
- expect(firstMsg.role).toBe('system');
1115
+ expect(firstMsg.role).toBe('user');
665
1116
  const firstContent = firstMsg.content;
666
1117
  expect(typeof firstContent === 'string' && firstContent.includes('Conversation Summary')).toBe(true);
667
1118
  }
@@ -837,6 +1288,50 @@ describe('handleDispatching (via transition)', () => {
837
1288
  agent: 'broken-agent',
838
1289
  }));
839
1290
  });
1291
+ it('propagates parent remaining token budget to child', async () => {
1292
+ // Parent has 100 budget, 90 already used → child should get 10.
1293
+ // Child's first mock turn yields 150 tokens, which exceeds the child's
1294
+ // 10-token budget; the child's outer loop catches it on the next check
1295
+ // and stops with budget_exceeded. Parent usage reflects the child's
1296
+ // consumed tokens once the child merges back.
1297
+ const parentCtx = makeMockContext({
1298
+ maxSessionTokens: 100,
1299
+ usage: { inputTokens: 60, outputTokens: 30, totalTokens: 90 },
1300
+ });
1301
+ const result = await transition({
1302
+ type: 'dispatching',
1303
+ task: { agentName: 'starved-child', toolSubset: [], prompt: 'Do lots of work' },
1304
+ toolCallId: 'tc-starved',
1305
+ queue: [],
1306
+ results: [],
1307
+ }, parentCtx);
1308
+ // Parent should resume (doesn't crash on child budget exhaustion)
1309
+ expect(result.next.type).toBe('thinking');
1310
+ // Child should have stopped early — its merged-back token usage should
1311
+ // not massively exceed the original budget, because the check fires on
1312
+ // the next outer loop iteration after the first 150-token turn.
1313
+ // (It won't be zero — the first turn runs fully and merges — but it
1314
+ // won't compound across many turns.)
1315
+ expect(parentCtx.usage.totalTokens).toBeLessThanOrEqual(90 + 150 + 150);
1316
+ });
1317
+ it('child inherits unlimited budget when parent has no cap', async () => {
1318
+ // No maxSessionTokens on parent → child should also have no cap (undefined).
1319
+ // The child runs through normal termination (model_stop), not budget.
1320
+ const parentCtx = makeMockContext({
1321
+ // maxSessionTokens intentionally omitted
1322
+ usage: { inputTokens: 500, outputTokens: 500, totalTokens: 1000 },
1323
+ });
1324
+ const result = await transition({
1325
+ type: 'dispatching',
1326
+ task: { agentName: 'unbounded-child', toolSubset: [], prompt: 'Go' },
1327
+ toolCallId: 'tc-unbounded',
1328
+ queue: [],
1329
+ results: [],
1330
+ }, parentCtx);
1331
+ expect(result.next.type).toBe('thinking');
1332
+ // No agent_loop_budget_exceeded log should have fired for the child
1333
+ expect(parentCtx.logger.warn).not.toHaveBeenCalledWith('agent_loop_budget_exceeded', expect.anything());
1334
+ });
840
1335
  });
841
1336
  // ---------------------------------------------------------------------------
842
1337
  // 3. Integration: runAgent() full flow
@@ -974,6 +1469,76 @@ describe('runAgent', () => {
974
1469
  // Turn count should not exceed maxTurns
975
1470
  expect(ctx.turnCount).toBeLessThanOrEqual(3);
976
1471
  });
1472
+ it('token budget terminates the loop with reason=budget_exceeded', async () => {
1473
+ const tool = makeMockToolDef();
1474
+ const registry = makeMockRegistry({ loop_tool: tool });
1475
+ // Each turn yields 15 tokens (10 in + 5 out). With maxSessionTokens=30, the loop
1476
+ // should stop after the 2nd turn pushes cumulative usage past the cap.
1477
+ const provider = {
1478
+ model: 'test-model',
1479
+ provider: 'test',
1480
+ languageModel: {},
1481
+ streamText: vi.fn(() => makeMockStream([
1482
+ { type: 'tool-call', toolCallId: `c-${Date.now()}`, toolName: 'loop_tool', args: {} },
1483
+ { type: 'finish', usage: makeUsage({ inputTokens: 10, outputTokens: 5, totalTokens: 15 }) },
1484
+ ], '')),
1485
+ generateText: vi.fn(),
1486
+ };
1487
+ const ctx = makeMockContext({
1488
+ provider,
1489
+ toolRegistry: registry,
1490
+ maxTurns: 100,
1491
+ maxSessionTokens: 30,
1492
+ });
1493
+ const events = [];
1494
+ for await (const event of runAgent({
1495
+ messages: [{ role: 'user', content: 'Burn tokens' }],
1496
+ context: ctx,
1497
+ })) {
1498
+ events.push(event);
1499
+ }
1500
+ // Should have terminated on budget, not max_turns
1501
+ const doneEvent = events[events.length - 1];
1502
+ expect(doneEvent.type).toBe(SSEEventType.Done);
1503
+ expect(ctx.turnCount).toBeLessThan(100);
1504
+ expect(ctx.usage.totalTokens).toBeGreaterThanOrEqual(30);
1505
+ });
1506
+ it('undefined maxSessionTokens means no budget cap', async () => {
1507
+ // Same infinite-tool-call provider as max_turns test, but with no
1508
+ // maxSessionTokens set and a small maxTurns to bound the test. Verifies that
1509
+ // undefined budget doesn't accidentally trip the check.
1510
+ const tool = makeMockToolDef();
1511
+ const registry = makeMockRegistry({ loop_tool: tool });
1512
+ const provider = {
1513
+ model: 'test-model',
1514
+ provider: 'test',
1515
+ languageModel: {},
1516
+ streamText: vi.fn(() => makeMockStream([
1517
+ { type: 'tool-call', toolCallId: `c-${Date.now()}`, toolName: 'loop_tool', args: {} },
1518
+ { type: 'finish', usage: makeUsage({ inputTokens: 100, outputTokens: 100, totalTokens: 200 }) },
1519
+ ], '')),
1520
+ generateText: vi.fn(),
1521
+ };
1522
+ const ctx = makeMockContext({
1523
+ provider,
1524
+ toolRegistry: registry,
1525
+ maxTurns: 2,
1526
+ // maxSessionTokens intentionally omitted
1527
+ });
1528
+ const events = [];
1529
+ for await (const event of runAgent({
1530
+ messages: [{ role: 'user', content: 'Run' }],
1531
+ context: ctx,
1532
+ })) {
1533
+ events.push(event);
1534
+ }
1535
+ // Should stop on max_turns, budget check should not interfere
1536
+ const doneEvent = events[events.length - 1];
1537
+ expect(doneEvent.type).toBe(SSEEventType.Done);
1538
+ expect(ctx.usage.totalTokens).toBeGreaterThan(0);
1539
+ // No budget-exceeded log should have been emitted
1540
+ // (positive assertion: we reached max_turns, accumulating tokens beyond any tiny cap)
1541
+ });
977
1542
  it('done event always includes usage regardless of reason (G2)', async () => {
978
1543
  // Abort immediately
979
1544
  const abortController = new AbortController();
@@ -1019,7 +1584,6 @@ describe('runAgent', () => {
1019
1584
  }
1020
1585
  expect(ctx.logger.info).toHaveBeenCalledWith('agent_loop_start', expect.objectContaining({
1021
1586
  session: 'test-session',
1022
- tenant: 'test-tenant',
1023
1587
  }));
1024
1588
  expect(ctx.logger.info).toHaveBeenCalledWith('agent_loop_done', expect.objectContaining({
1025
1589
  session: 'test-session',