feat: extend agent-sdk-runner with canUseTool for AskUserQuestion interception

Test harness at test/helpers/agent-sdk-runner.ts gains an optional
`canUseTool` callback parameter. When a test supplies it, the harness
flips `permissionMode` from `bypassPermissions` (overlay-harness default)
to `default` so the SDK actually invokes the callback on every tool use,
and auto-adds `AskUserQuestion` to `allowedTools` so Claude can fire it
at all.

Exports a `passThroughNonAskUserQuestion` helper so tests that only want
to intercept AskUserQuestion can auto-allow every other tool with one
line: `return passThroughNonAskUserQuestion(toolName, input)`.

This is the foundation for D14 — every future interactive-skill E2E test
can now assert on AskUserQuestion shape and routing. Previous E2E tests
at `test/skill-e2e.test.ts` explicitly instructed the model to skip
AskUserQuestion ("non-interactive run") which meant no test could actually
verify the question content or routing.

6 new unit tests in test/agent-sdk-runner.test.ts cover:
- permissionMode flips to 'default' when canUseTool supplied
- permissionMode stays 'bypassPermissions' when canUseTool absent
- canUseTool callback reaches the SDK options
- AskUserQuestion auto-added to allowedTools when canUseTool supplied
- AskUserQuestion NOT added when canUseTool absent
- passThroughNonAskUserQuestion helper returns allow+updatedInput

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-04-23 23:40:50 -07:00
parent 5e4895c90a
commit 28b14fbf0c
2 changed files with 156 additions and 5 deletions

View File

@@ -366,6 +366,101 @@ describe('runAgentSdkTest — options propagation', () => {
});
});
// ---------------------------------------------------------------------------
// canUseTool extension (D10 CEO / D4 eng)
// ---------------------------------------------------------------------------
describe('runAgentSdkTest — canUseTool extension', () => {
test('permissionMode flips to "default" when canUseTool is supplied', async () => {
freshSem();
const stub: StubConfig = {
streams: [[systemInit(), assistantTurn([{ type: 'text', text: 'ok' }]), resultSuccess()]],
calls: [],
};
await runAgentSdkTest({
...BASE_OPTS,
queryProvider: makeStubProvider(stub),
canUseTool: async (_toolName, input) => ({ behavior: 'allow', updatedInput: input }),
});
const opts = stub.calls[0]!.options!;
expect(opts.permissionMode).toBe('default');
expect(opts.allowDangerouslySkipPermissions).toBe(false);
});
test('permissionMode stays "bypassPermissions" when canUseTool is NOT supplied', async () => {
freshSem();
const stub: StubConfig = {
streams: [[systemInit(), assistantTurn([{ type: 'text', text: 'ok' }]), resultSuccess()]],
calls: [],
};
await runAgentSdkTest({
...BASE_OPTS,
queryProvider: makeStubProvider(stub),
});
const opts = stub.calls[0]!.options!;
expect(opts.permissionMode).toBe('bypassPermissions');
expect(opts.allowDangerouslySkipPermissions).toBe(true);
});
test('canUseTool callback reaches the SDK options', async () => {
freshSem();
const stub: StubConfig = {
streams: [[systemInit(), assistantTurn([{ type: 'text', text: 'ok' }]), resultSuccess()]],
calls: [],
};
const cb = async (_toolName: string, input: Record<string, unknown>) => ({
behavior: 'allow' as const,
updatedInput: input,
});
await runAgentSdkTest({
...BASE_OPTS,
queryProvider: makeStubProvider(stub),
canUseTool: cb,
});
const opts = stub.calls[0]!.options! as Options & { canUseTool?: unknown };
expect(typeof opts.canUseTool).toBe('function');
});
test('AskUserQuestion is auto-added to allowedTools when canUseTool is supplied', async () => {
freshSem();
const stub: StubConfig = {
streams: [[systemInit(), assistantTurn([{ type: 'text', text: 'ok' }]), resultSuccess()]],
calls: [],
};
await runAgentSdkTest({
...BASE_OPTS,
allowedTools: ['Read', 'Grep'], // explicitly omits AskUserQuestion
queryProvider: makeStubProvider(stub),
canUseTool: async (_toolName, input) => ({ behavior: 'allow', updatedInput: input }),
});
const opts = stub.calls[0]!.options!;
expect(opts.allowedTools).toContain('AskUserQuestion');
expect(opts.tools).toContain('AskUserQuestion');
});
test('AskUserQuestion is NOT auto-added when canUseTool is absent', async () => {
freshSem();
const stub: StubConfig = {
streams: [[systemInit(), assistantTurn([{ type: 'text', text: 'ok' }]), resultSuccess()]],
calls: [],
};
await runAgentSdkTest({
...BASE_OPTS,
allowedTools: ['Read', 'Grep'],
queryProvider: makeStubProvider(stub),
});
const opts = stub.calls[0]!.options!;
expect(opts.allowedTools).not.toContain('AskUserQuestion');
});
test('passThroughNonAskUserQuestion helper returns allow+updatedInput', async () => {
const { passThroughNonAskUserQuestion } = await import('../test/helpers/agent-sdk-runner');
const result = passThroughNonAskUserQuestion('Read', { file_path: '/tmp/x' });
expect(result.behavior).toBe('allow');
expect(result.updatedInput).toEqual({ file_path: '/tmp/x' });
});
});
// ---------------------------------------------------------------------------
// Rate-limit retry (three shapes)
// ---------------------------------------------------------------------------