fix(hooks): remove speculative regex runtime
This commit is contained in:
@@ -66,11 +66,11 @@ async function waitFor(predicate: () => boolean, timeout = 5000, interval = 10):
|
||||
}
|
||||
|
||||
describe('hooks-codex bridge', () => {
|
||||
it('a PreToolUse hook (exit 2) honors a Rust-regex inline flag matcher', async () => {
|
||||
it('a PreToolUse hook (exit 2) denies a tool the regex matcher matches as a substring', async () => {
|
||||
const dir = configDir()
|
||||
const deny = script(dir, 'deny.sh', '#!/usr/bin/env bash\necho "codex blocked it" >&2\nexit 2\n')
|
||||
// `(?i)` is accepted by Rust regex but rejected by JavaScript RegExp.
|
||||
writeHooks(dir, { PreToolUse: [{ matcher: '(?i)^bash$', hooks: [{ type: 'command', command: deny }] }] })
|
||||
// Codex regex matcher: "Bash" is /Bash/ — matches the tool name "Bash".
|
||||
writeHooks(dir, { PreToolUse: [{ matcher: 'Bash', hooks: [{ type: 'command', command: deny }] }] })
|
||||
|
||||
const adapter = new MockAdapter([toolCallResponse('c1', 'Bash', { command: 'ls' }), textResponse('done')])
|
||||
const ctx = await harness(dir, adapter)
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { parseCodexConfig as parseRawCodexConfig, CODEX_EVENTS } from '@deepseek-ai/dsh-hooks-codex/src/config.ts'
|
||||
|
||||
const matcherSets: Array<ReturnType<typeof parseRawCodexConfig>['matchers']> = []
|
||||
afterEach(() => { for (const matchers of matcherSets.splice(0)) matchers.dispose() })
|
||||
|
||||
function parseCodexConfig(...args: Parameters<typeof parseRawCodexConfig>): ReturnType<typeof parseRawCodexConfig> {
|
||||
const result = parseRawCodexConfig(...args)
|
||||
matcherSets.push(result.matchers)
|
||||
return result
|
||||
}
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { parseCodexConfig, CODEX_EVENTS } from '@deepseek-ai/dsh-hooks-codex/src/config.ts'
|
||||
|
||||
describe('parseCodexConfig', () => {
|
||||
it('honors only the five bridge-supported Codex events, dropping the rest', () => {
|
||||
@@ -70,10 +61,9 @@ describe('parseCodexConfig', () => {
|
||||
expect('matcher' in config.Stop![0]!).toBe(false)
|
||||
})
|
||||
|
||||
it('keeps a valid Rust-regex matcher when present', () => {
|
||||
const { config, matchers } = parseCodexConfig({ PreToolUse: [{ matcher: '(?i)^bash$', hooks: [{ type: 'command', command: 'b.sh' }] }] })
|
||||
expect(config.PreToolUse![0]!.matcher).toBe('(?i)^bash$')
|
||||
expect(matchers.matches('(?i)^bash$', 'BASH')).toBe(true)
|
||||
it('keeps a matcher when present', () => {
|
||||
const { config } = parseCodexConfig({ PreToolUse: [{ matcher: '^Bash$', hooks: [{ type: 'command', command: 'b.sh' }] }] })
|
||||
expect(config.PreToolUse![0]!.matcher).toBe('^Bash$')
|
||||
})
|
||||
|
||||
it('rejects an invalid regex matcher with its event name', () => {
|
||||
@@ -82,12 +72,6 @@ describe('parseCodexConfig', () => {
|
||||
})).toThrow('invalid codex regex matcher "[" on event "PreToolUse"')
|
||||
})
|
||||
|
||||
it('rejects JavaScript-only regex syntax that Codex cannot execute', () => {
|
||||
expect(() => parseCodexConfig({
|
||||
PreToolUse: [{ matcher: '(?=Bash)', hooks: [{ type: 'command', command: 's.sh' }] }],
|
||||
})).toThrow('invalid codex regex matcher "(?=Bash)" on event "PreToolUse"')
|
||||
})
|
||||
|
||||
it('discards matcher fields on events without matcher subjects before validation', () => {
|
||||
const { config } = parseCodexConfig({
|
||||
UserPromptSubmit: [{ matcher: '[', hooks: [{ type: 'command', command: 'prompt.sh' }] }],
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { Context } from 'cordis'
|
||||
import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local'
|
||||
import LocalSubprocessService from '@deepseek-ai/dsh-subprocess-local'
|
||||
|
||||
const matcherLifecycle = vi.hoisted(() => {
|
||||
const registry = {
|
||||
matches: vi.fn(() => true),
|
||||
diagnostic: vi.fn<(matcher: string | undefined) => string | undefined>(() => undefined),
|
||||
dispose: vi.fn<() => void>(),
|
||||
}
|
||||
return {
|
||||
registry,
|
||||
compileMatchers: vi.fn(() => registry),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@deepseek-ai/dsh-hook-protocol', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@deepseek-ai/dsh-hook-protocol')>()
|
||||
return { ...actual, compileMatchers: matcherLifecycle.compileMatchers }
|
||||
})
|
||||
|
||||
const dirs: string[] = []
|
||||
afterEach(() => {
|
||||
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true })
|
||||
vi.clearAllMocks()
|
||||
matcherLifecycle.registry.diagnostic.mockReturnValue(undefined)
|
||||
})
|
||||
|
||||
describe('hooks-codex matcher lifecycle', () => {
|
||||
it('disposes the compiled set when one event-specific diagnostic rejects the config', async () => {
|
||||
const { parseCodexConfig } = await import('@deepseek-ai/dsh-hooks-codex/src/config.ts')
|
||||
matcherLifecycle.registry.diagnostic.mockImplementation((matcher: string | undefined) => (
|
||||
matcher === '[' ? 'invalid codex regex matcher "["' : undefined
|
||||
))
|
||||
|
||||
expect(() => parseCodexConfig({
|
||||
PreToolUse: [
|
||||
{ matcher: '(?i)^bash$', hooks: [{ type: 'command', command: 'first' }] },
|
||||
{ matcher: '[', hooks: [{ type: 'command', command: 'second' }] },
|
||||
],
|
||||
})).toThrow('invalid codex regex matcher "[" on event "PreToolUse"')
|
||||
|
||||
expect(matcherLifecycle.compileMatchers).toHaveBeenCalledExactlyOnceWith(
|
||||
new Set(['(?i)^bash$', '[']),
|
||||
'codex',
|
||||
)
|
||||
expect(matcherLifecycle.registry.dispose).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('gives the loaded config one matcher registry and disposes it on plugin teardown', async () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'dsh-hooks-codex-matchers-'))
|
||||
dirs.push(dir)
|
||||
const configPath = join(dir, 'hooks.json')
|
||||
writeFileSync(configPath, JSON.stringify({ hooks: {
|
||||
PreToolUse: [{ matcher: '(?i)^bash$', hooks: [{ type: 'command', command: 'true' }] }],
|
||||
PostToolUse: [{ matcher: '(?i)^bash$', hooks: [{ type: 'command', command: 'true' }] }],
|
||||
} }))
|
||||
|
||||
const HooksCodex = await import('@deepseek-ai/dsh-hooks-codex')
|
||||
const ctx = new Context()
|
||||
await ctx.plugin(LocalSubprocessService)
|
||||
await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 })
|
||||
const fiber = await ctx.plugin(HooksCodex, { configPath, model: 'm' })
|
||||
|
||||
expect(matcherLifecycle.compileMatchers).toHaveBeenCalledExactlyOnceWith(new Set([
|
||||
'(?i)^bash$',
|
||||
]), 'codex')
|
||||
expect(matcherLifecycle.registry.diagnostic).toHaveBeenCalledTimes(2)
|
||||
expect(matcherLifecycle.registry.dispose).not.toHaveBeenCalled()
|
||||
|
||||
await fiber.dispose()
|
||||
expect(matcherLifecycle.registry.dispose).toHaveBeenCalledOnce()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user