hooks.e2e.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process'
  2. import { Readable, Writable } from 'node:stream'
  3. import { mkdtemp, rm, writeFile, access } from 'node:fs/promises'
  4. import { tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import { fileURLToPath } from 'node:url'
  7. import { afterEach, describe, expect, it } from 'vitest'
  8. import {
  9. ClientSideConnection,
  10. ndJsonStream,
  11. PROTOCOL_VERSION,
  12. type Agent as AcpAgent,
  13. type Client,
  14. type RequestPermissionRequest,
  15. type RequestPermissionResponse,
  16. type SessionNotification,
  17. } from '@agentclientprotocol/sdk'
  18. /**
  19. * With-key e2e for the Claude hook bridge. The process-level `./hooks.json` is
  20. * resolved from a temporary launch cwd and blocks all PreToolUse calls; a real
  21. * model is asked to write there, and absence of the file proves interception.
  22. * The test owns and disposes the ACP subprocess.
  23. */
  24. const binScript = fileURLToPath(new URL('../../../packages/ui/acp-agent/src/bin.ts', import.meta.url))
  25. const configPath = fileURLToPath(new URL('../cordis.yml', import.meta.url))
  26. const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
  27. const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  28. interface Spawned {
  29. child: ChildProcessWithoutNullStreams
  30. client: ClientSideConnection
  31. updates: SessionNotification['update'][]
  32. stderr: string[]
  33. }
  34. function spawnAcpAgent(cwd: string): Spawned {
  35. const child = spawn(
  36. process.execPath,
  37. ['--import', tsxLoader, binScript, '--config', configPath],
  38. { cwd, env: { ...process.env, TSX_TSCONFIG_PATH: repoTsconfig, DSH_PERMISSION_MODE: 'danger-full-access' }, stdio: ['pipe', 'pipe', 'pipe'] },
  39. )
  40. const stderr: string[] = []
  41. child.stderr.setEncoding('utf8')
  42. child.stderr.on('data', (chunk: string) => stderr.push(chunk))
  43. const updates: SessionNotification['update'][] = []
  44. const stream = ndJsonStream(
  45. Writable.toWeb(child.stdin) as WritableStream<Uint8Array>,
  46. Readable.toWeb(child.stdout) as ReadableStream<Uint8Array>,
  47. )
  48. const makeClient = (_agent: AcpAgent): Client => ({
  49. sessionUpdate(params: SessionNotification): Promise<void> {
  50. updates.push(params.update)
  51. return Promise.resolve()
  52. },
  53. requestPermission(_params: RequestPermissionRequest): Promise<RequestPermissionResponse> {
  54. return Promise.resolve({ outcome: { outcome: 'cancelled' } })
  55. },
  56. })
  57. const client = new ClientSideConnection(makeClient, stream)
  58. return { child, client, updates, stderr }
  59. }
  60. let spawned: Spawned | undefined
  61. let workdir: string | undefined
  62. afterEach(async () => {
  63. if (spawned) {
  64. spawned.child.kill('SIGKILL')
  65. spawned = undefined
  66. }
  67. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  68. workdir = undefined
  69. })
  70. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('acp-agent e2e: a PreToolUse hook blocks bash (real model)', () => {
  71. it('denies every bash command, so the requested file is never written (verified on disk)', async () => {
  72. workdir = await mkdtemp(join(tmpdir(), 'acp-hooks-e2e-'))
  73. // `configPath` is process-relative, so placing the match-all hook in the
  74. // launch cwd selects it; hook commands themselves run in the session cwd.
  75. await writeFile(join(workdir, 'hooks.json'), JSON.stringify({
  76. hooks: { PreToolUse: [{ hooks: [{ type: 'command', command: 'echo "bash blocked by policy" >&2; exit 2' }] }] },
  77. }))
  78. spawned = spawnAcpAgent(workdir)
  79. const { client, updates } = spawned
  80. await client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  81. const { sessionId } = await client.newSession({ cwd: workdir, mcpServers: [] })
  82. const res = await client.prompt({
  83. sessionId,
  84. prompt: [{ type: 'text', text: 'Use the bash tool to write the exact text HOOK_FAIL into a file named proof.txt in the current directory. Then stop.' }],
  85. })
  86. // The turn completes normally (the block is a tool-result error fed back to
  87. // the model, not a turn failure).
  88. expect(['end_turn', 'max_tokens']).toContain(res.stopReason)
  89. // Verify that the denied hook left no filesystem effect.
  90. await expect(access(join(workdir, 'proof.txt'))).rejects.toThrow()
  91. // A blocked call is still streamed with the hook's reason as an error.
  92. const toolCalls = updates.filter(u => u.sessionUpdate === 'tool_call' || u.sessionUpdate === 'tool_call_update')
  93. expect(toolCalls.length).toBeGreaterThan(0)
  94. }, 180_000)
  95. })