|
| 1 | +import { test } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { stripTypeScriptTypes } from 'node:module'; |
| 4 | +import { buildAsyncFn, runFunction, parsePackage, buildPackageFn, exportFn } from '@matatbread/matbot-function-tools'; |
| 5 | +import { invokeTool, parseConfig } from '@matatbread/matbot-core'; |
| 6 | +import type { MatbotMachine, Tool, ToolContext, ToolEvent } from '@matatbread/matbot-plugin-api'; |
| 7 | +import { createVmFunctionRunner, FUNCTION_SYNC_LIMIT_MS } from '../src/function-runner.ts'; |
| 8 | + |
| 9 | +// A tool_function body runs on the daemon's one event loop. 18da1564 (2026-09-15) froze every session for |
| 10 | +// 260s on a model-authored `while` whose step never advanced; it ended only because the array it grew hit |
| 11 | +// the length limit. These pin the runner that bounds such work, and the abort wiring for loops that await. |
| 12 | + |
| 13 | +const stripper = { strip: (s: string) => stripTypeScriptTypes(s) }; |
| 14 | +const runner = createVmFunctionRunner(200); |
| 15 | +const LIMIT = /synchronous work without an await/; |
| 16 | + |
| 17 | +function machineWith(tools: Tool[] = []): MatbotMachine { |
| 18 | + const reg = new Map(tools.map(t => [t.name, t])); |
| 19 | + return { |
| 20 | + tools: { |
| 21 | + list: () => [...reg.values()], |
| 22 | + resolve: (n: string) => reg.get(n) ?? null, |
| 23 | + register: (t: Tool) => { reg.set(t.name, t); }, |
| 24 | + remove: (n: string) => reg.delete(n), |
| 25 | + }, |
| 26 | + } as unknown as MatbotMachine; |
| 27 | +} |
| 28 | + |
| 29 | +const ctxFor = (signal: AbortSignal = new AbortController().signal): ToolContext => ({ |
| 30 | + callId: 'c1', session: { id: 's1' }, signal, |
| 31 | + prompt: () => Promise.reject(new Error('non-interactive')), |
| 32 | +}) as unknown as ToolContext; |
| 33 | + |
| 34 | +async function last(events: AsyncIterable<ToolEvent>): Promise<ToolEvent | undefined> { |
| 35 | + let final: ToolEvent | undefined; |
| 36 | + for await (const ev of events) final = ev; |
| 37 | + return final; |
| 38 | +} |
| 39 | + |
| 40 | +const lambda = (src: string) => buildAsyncFn(stripper, src, ['args'], runner); |
| 41 | + |
| 42 | +function pingTool(): Tool & { calls: () => number } { |
| 43 | + let calls = 0; |
| 44 | + return { |
| 45 | + name: 'ping', description: '', inputSchema: { type: 'object' }, |
| 46 | + executor: { async *execute() { calls++; yield { type: 'result', value: calls }; } }, |
| 47 | + calls: () => calls, |
| 48 | + }; |
| 49 | +} |
| 50 | +const message = (ev: ToolEvent | undefined): string => (ev?.type === 'error' ? ev.message : `not an error: ${JSON.stringify(ev)}`); |
| 51 | + |
| 52 | +test('a loop that never awaits is stopped at the limit, and the daemon carries on', async () => { |
| 53 | + const started = Date.now(); |
| 54 | + const ev = await last(runFunction(machineWith(), ctxFor(), await lambda('(args: {}): Promise<number> { let d = 1; while (d > 0) { d = d; } return d; }'), [{}])); |
| 55 | + assert.match(message(ev), LIMIT); |
| 56 | + assert.ok(Date.now() - started < 2000, `stopped near the limit, not left to run (${Date.now() - started}ms)`); |
| 57 | + assert.equal(await new Promise(r => setTimeout(() => r('alive'), 0)), 'alive', 'the event loop is free again'); |
| 58 | +}); |
| 59 | + |
| 60 | +// Not tested, because not covered: a loop AFTER an await. See createVmFunctionRunner for why the design |
| 61 | +// that covers it (a context per run, drained inside timed evaluations) was withdrawn — its timeout aborts |
| 62 | +// the process when async hooks are enabled, as they are under this very test runner. |
| 63 | + |
| 64 | +test('a body keeps the globals it had, and its input is its own data', async () => { |
| 65 | + const fn = await lambda("(args: { xs: number[] }): Promise<string> { const u = new URL('https://x.test/a?b=1'); await new Promise<void>(r => queueMicrotask(r)); return [typeof fetch, u.searchParams.get('b'), new TextEncoder().encode('hi').length, args.xs instanceof Array, await tool.ping({})].join(','); }"); |
| 66 | + assert.deepEqual(await last(runFunction(machineWith([pingTool()]), ctxFor(), fn, [{ xs: [1] }])), { type: 'result', value: 'function,1,2,true,1' }); |
| 67 | +}); |
| 68 | + |
| 69 | +test('awaiting is not work: a call slower than the limit completes', async () => { |
| 70 | + const ev = await last(runFunction(machineWith(), ctxFor(), await lambda("(args: {}): Promise<string> { await new Promise(r => setTimeout(r, 600)); return 'slow but fine'; }"), [{}])); |
| 71 | + assert.deepEqual(ev, { type: 'result', value: 'slow but fine' }); |
| 72 | +}); |
| 73 | + |
| 74 | +test('a package export is bounded too, being called inside the timed run', async () => { |
| 75 | + const spin = 'export function spin(args: {}): number { for (;;) {} }'; |
| 76 | + const spinning = await buildPackageFn(stripper, spin, parsePackage('P', spin), runner); |
| 77 | + assert.match(message(await last(runFunction(machineWith(), ctxFor(), exportFn(spinning, 'spin'), [{}]))), LIMIT); |
| 78 | + |
| 79 | + const ok = 'function double(n: number): number { return n * 2; }\nexport function twice(args: { n: number }): number { return double(args.n); }'; |
| 80 | + const working = await buildPackageFn(stripper, ok, parsePackage('P', ok), runner); |
| 81 | + assert.deepEqual(await last(runFunction(machineWith(), ctxFor(), exportFn(working, 'twice'), [{ n: 21 }])), { type: 'result', value: 42 }); |
| 82 | +}); |
| 83 | + |
| 84 | +test('a run started inside another takes its own call, and a loop there is still stopped', async () => { |
| 85 | + const inner = runner.compile([], 'return 7;'); |
| 86 | + assert.equal(await runner.compile(['inner'], 'return inner();')(inner), 7); |
| 87 | + const spinning = runner.compile([], 'for (;;) {}'); |
| 88 | + await assert.rejects(runner.compile(['inner'], 'return inner();')(spinning), LIMIT); |
| 89 | + assert.equal(await runner.compile(['a', 'b'], 'return a + b;')(2, 3), 5, 'the runner is usable after a stopped run'); |
| 90 | +}); |
| 91 | + |
| 92 | +test('with no runner the body runs directly, as it did before', async () => { |
| 93 | + const fn = await buildAsyncFn(stripper, "(args: {}): Promise<string> { return 'direct'; }", ['args']); |
| 94 | + assert.deepEqual(await last(runFunction(machineWith(), ctxFor(), fn, [{}])), { type: 'result', value: 'direct' }); |
| 95 | +}); |
| 96 | + |
| 97 | +test('aborting ends the call, and a body looping over tool calls then stops itself', async () => { |
| 98 | + // Awaiting a tool each iteration is what lets it die: after the abort that call is refused and throws. |
| 99 | + // A body awaiting only timers would keep running unseen — nothing in-process can stop it — though the |
| 100 | + // call still ends on the abort. |
| 101 | + const ping = pingTool(); |
| 102 | + const ac = new AbortController(); |
| 103 | + const fn = await lambda('(args: {}): Promise<void> { for (;;) { await tool.ping({}); await new Promise(r => setTimeout(r, 5)); } }'); |
| 104 | + setTimeout(() => ac.abort(), 50); |
| 105 | + const started = Date.now(); |
| 106 | + assert.match(message(await last(runFunction(machineWith([ping]), ctxFor(ac.signal), fn, [{}]))), /Cancelled/); |
| 107 | + assert.ok(Date.now() - started < 1000, 'returned promptly after the abort'); |
| 108 | + const atAbort = ping.calls(); |
| 109 | + await new Promise(r => setTimeout(r, 60)); |
| 110 | + assert.equal(ping.calls(), atAbort, 'no tool was started after the abort'); |
| 111 | +}); |
| 112 | + |
| 113 | +test('a tool call on a cancelled call is refused before the tool starts', async () => { |
| 114 | + let started = false; |
| 115 | + const probe: Tool = { |
| 116 | + name: 'probe', description: '', inputSchema: { type: 'object' }, |
| 117 | + executor: { async *execute() { started = true; yield { type: 'result', value: 1 }; } }, |
| 118 | + }; |
| 119 | + const ac = new AbortController(); |
| 120 | + ac.abort(); |
| 121 | + assert.throws(() => invokeTool(machineWith([probe]), 'probe', {}, { session: { id: 's1' } as never, signal: ac.signal }), /was not started: the call was cancelled/); |
| 122 | + assert.equal(started, false); |
| 123 | +}); |
| 124 | + |
| 125 | +test('function_timeout_ms comes from matbot.yaml: absent is the default, 0 is a value, a typo is refused', () => { |
| 126 | + assert.equal(FUNCTION_SYNC_LIMIT_MS, 10_000); |
| 127 | + assert.equal(parseConfig('ephemeral: true\n').functionTimeoutMs, undefined, 'absent ⇒ the host default applies'); |
| 128 | + assert.equal(parseConfig('function_timeout_ms: 2500\n').functionTimeoutMs, 2500); |
| 129 | + // 0 must survive parsing as a value: it is what turns the runner off, not what leaves it at the default. |
| 130 | + assert.equal(parseConfig('function_timeout_ms: 0\n').functionTimeoutMs, 0); |
| 131 | + for (const bad of ['-1', '1.5', 'soon']) { |
| 132 | + assert.throws(() => parseConfig(`function_timeout_ms: ${bad}\n`), /function_timeout_ms/, bad); |
| 133 | + } |
| 134 | +}); |
0 commit comments