fix(tools): limit cancellation guard to pre-execution

This commit is contained in:
Rudy Celekli committed 2026-10-14 14:17:55 -04:00
1 parent fffbe67b66
commit aacb409fbf
4 files changed
+52 -87

No files matched your search

+45 -72
View File
@@ -1,78 +1,51 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { composeCatalog, defineTool } from './catalog.js';
test('cancelled catalog calls cannot execute or return late results', async () => {
let calls = 0,
release,
block = false;
const action = defineTool({
name: 'fixture_action',
kind: 'action',
title: 'Fixture',
description: 'Offline fixture',
inputSchema: { type: 'object' },
run: async () => {
calls++;
return block
? new Promise((r) => (release = r))
: { summary: 'fixture', data: {} };
},
});
const catalog = composeCatalog({ tools: [action] });
const c = new AbortController();
c.abort();
await assert.rejects(
catalog.call('fixture_action', {}, { signal: c.signal }),
{ name: 'AbortError' },
);
assert.equal(calls, 0);
block = true;
const running = new AbortController();
const pending = catalog.call(
'fixture_action',
{},
{ signal: running.signal },
);
while (!release) await Promise.resolve();
running.abort();
release({ summary: 'late', data: {} });
await assert.rejects(pending, { name: 'AbortError' });
assert.equal(calls, 1);
});
test('cancellation during an awaited interceptor cannot publish its late result', async () => {
const controller = new AbortController();
const reason = new Error('interceptor caller canceled');
let calls = 0;
const action = defineTool({
name: 'fixture_intercepted_action',
kind: 'action',
title: 'Fixture',
description: 'Offline fixture',
inputSchema: { type: 'object' },
run: async () => {
calls++;
return { summary: 'completed action', data: {} };
},
});
const catalog = composeCatalog({
tools: [action],
interceptors: [
async (call, next) => {
const result = await next(call);
await Promise.resolve();
controller.abort(reason);
return result;
for (const intercept of [false, true]) {
test(`cancelled query refuses execution${intercept ? ' after an awaited interceptor' : ''}`, async () => {
let calls = 0;
const controller = new AbortController();
const query = defineTool({
name: 'fixture_query',
kind: 'query',
title: 'Fixture',
description: 'Offline fixture',
inputSchema: { type: 'object' },
run: async () => {
calls++;
return { summary: 'fixture', data: {} };
},
],
});
const catalog = composeCatalog({
tools: [query],
interceptors: intercept
? [
async (call, next) => {
await Promise.resolve();
controller.abort();
return next(call);
},
]
: [],
});
if (!intercept) controller.abort();
await assert.rejects(
catalog.call('fixture_query', {}, { signal: controller.signal }),
{ name: 'AbortError' },
);
assert.equal(calls, 0);
const fresh = composeCatalog({ tools: [query] });
assert.equal(
(
await fresh.call(
'fixture_query',
{},
{ signal: new AbortController().signal },
)
).summary,
'fixture',
);
assert.equal(calls, 1);
});
await assert.rejects(
catalog.call(
'fixture_intercepted_action',
{},
{ signal: controller.signal },
),
(error) => error === reason,
);
assert.equal(calls, 1, 'completed actions are not rolled back or rerun');
});
}