|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import type { Provider, ProviderResult, ProviderRunOpts } from '../providers/types.js'; |
| 3 | +import { ToolRegistry } from '../tools/registry.js'; |
| 4 | +import type { ToolHandler } from '../types.js'; |
| 5 | +import { RuntimeHost } from './host.js'; |
| 6 | + |
| 7 | +class ScriptedProvider implements Provider { |
| 8 | + readonly name = 'scripted'; |
| 9 | + constructor(private readonly results: ProviderResult[]) {} |
| 10 | + async runTurn(_opts: ProviderRunOpts): Promise<ProviderResult> { |
| 11 | + const result = this.results.shift(); |
| 12 | + if (!result) throw new Error('no scripted result'); |
| 13 | + return result; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +const usage = { inputTokens: 1, outputTokens: 1, reasoningTokens: 0, cacheReadTokens: 0 }; |
| 18 | + |
| 19 | +function writeThenDone(): ProviderResult[] { |
| 20 | + return [ |
| 21 | + { |
| 22 | + content: [ |
| 23 | + { |
| 24 | + type: 'tool_use', |
| 25 | + id: 'write-1', |
| 26 | + name: 'Write', |
| 27 | + input: { file_path: 'x', content: 'x' }, |
| 28 | + }, |
| 29 | + ], |
| 30 | + stopReason: 'tool_use', |
| 31 | + usage, |
| 32 | + }, |
| 33 | + { |
| 34 | + content: [{ type: 'text', text: 'done' }], |
| 35 | + stopReason: 'end_turn', |
| 36 | + usage, |
| 37 | + }, |
| 38 | + ]; |
| 39 | +} |
| 40 | + |
| 41 | +describe('RuntimeHost', () => { |
| 42 | + it('fails closed when the host omits policy and approval', async () => { |
| 43 | + let executions = 0; |
| 44 | + const write: ToolHandler = { |
| 45 | + name: 'Write', |
| 46 | + definition: { name: 'Write', description: 'test', inputSchema: {} }, |
| 47 | + execute: async () => { |
| 48 | + executions++; |
| 49 | + return { content: 'wrote' }; |
| 50 | + }, |
| 51 | + }; |
| 52 | + const host = new RuntimeHost({ |
| 53 | + provider: new ScriptedProvider(writeThenDone()), |
| 54 | + tools: new ToolRegistry([write]), |
| 55 | + cwd: '/tmp', |
| 56 | + }); |
| 57 | + |
| 58 | + const result = await host.run({ |
| 59 | + systemPrompt: '', |
| 60 | + userMessage: 'write', |
| 61 | + model: 'deepseek-chat', |
| 62 | + systemReminders: false, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(executions).toBe(0); |
| 66 | + expect(result.history.flatMap((message) => message.content)).toContainEqual( |
| 67 | + expect.objectContaining({ type: 'tool_result', is_error: true }), |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('keeps host policy while accepting an explicit turn mode override', async () => { |
| 72 | + let executions = 0; |
| 73 | + const write: ToolHandler = { |
| 74 | + name: 'Write', |
| 75 | + definition: { name: 'Write', description: 'test', inputSchema: {} }, |
| 76 | + execute: async () => { |
| 77 | + executions++; |
| 78 | + return { content: 'wrote' }; |
| 79 | + }, |
| 80 | + }; |
| 81 | + const host = new RuntimeHost({ |
| 82 | + provider: new ScriptedProvider(writeThenDone()), |
| 83 | + tools: new ToolRegistry([write]), |
| 84 | + cwd: '/tmp', |
| 85 | + mode: 'default', |
| 86 | + }); |
| 87 | + |
| 88 | + await host.run({ |
| 89 | + systemPrompt: '', |
| 90 | + userMessage: 'write', |
| 91 | + model: 'deepseek-chat', |
| 92 | + systemReminders: false, |
| 93 | + modeOverride: 'bypassPermissions', |
| 94 | + }); |
| 95 | + |
| 96 | + expect(executions).toBe(1); |
| 97 | + expect(host.mode).toBe('default'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('requires a cwd at either boundary', () => { |
| 101 | + const host = new RuntimeHost({ |
| 102 | + provider: new ScriptedProvider([]), |
| 103 | + tools: new ToolRegistry(), |
| 104 | + }); |
| 105 | + expect(() => host.run({ systemPrompt: '', userMessage: 'x', model: 'deepseek-chat' })).toThrow( |
| 106 | + /requires cwd/, |
| 107 | + ); |
| 108 | + }); |
| 109 | +}); |
0 commit comments