The two bridge plugins that run a user's existing Claude Code / Codex hook
config on the harness's typed interception seams, built on the shared
dsh-hook-protocol library. A bridge is a faithfulness adapter, not a power
tool: anything it does a native cordis plugin does more powerfully — the
bridge exists only to run UNMODIFIED external hooks.
- dsh-hooks-claude: CC dialect. Seven hook points (SessionStart,
UserPromptSubmit, PreToolUse, PostToolUse, Stop, SubagentStart,
SubagentStop), CC per-event stdin payloads, env + ${CLAUDE_PLUGIN_ROOT}/
${CLAUDE_PROJECT_DIR} substitution, literal-or-regex matcher.
- dsh-hooks-codex: Codex dialect — a deliberate subset. Five hook points,
always-regex matcher, snake_case payloads (turn_id/model, no trailing
newline), no env/substitution, block-only decisions.
Both map the neutral merged outcome onto the seam's typed Decision and stamp
an explicit {kind:'plugin'} source on injected context (so it is never
mislabeled as a user prompt). Config parse-failure is contained; only command
hooks run. updatedInput is logged+warned (input rewrite deferred); the Stop
loop-guard is deferred (TODO).
Tests: per-file 100% — config-parse unit branches + per-seam mappings
end-to-end through the REAL loop + REAL bash + REAL shell scripts (scripted
mock model only) + a real-Loader export-shape guard. A keyless ACP snapshot
scenario (hook-prompt-block) proves a UserPromptSubmit hook blocks a prompt
end-to-end (rejected turn -> ACP cancelled, hook/* events in the log); a
with-key e2e (hooks.e2e.ts) proves a PreToolUse hook blocks real bash
(verified on disk). The snapshot normalizer now scrubs hook/result.durationMs.
RFC: docs/rfc/implemented/feature/2026-06-30-hook-bridges.md
67 lines
3.3 KiB
TypeScript
67 lines
3.3 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { parseClaudeConfig, substituteCommand } from '@deepseek-ai/dsh-hooks-claude/src/config.ts'
|
|
|
|
describe('substituteCommand', () => {
|
|
it('replaces CLAUDE_PLUGIN_ROOT and CLAUDE_PROJECT_DIR (all occurrences)', () => {
|
|
expect(substituteCommand('${CLAUDE_PLUGIN_ROOT}/x.sh', { pluginRoot: '/p' })).toBe('/p/x.sh')
|
|
expect(substituteCommand('${CLAUDE_PROJECT_DIR}/a ${CLAUDE_PROJECT_DIR}/b', { projectDir: '/proj' })).toBe('/proj/a /proj/b')
|
|
expect(substituteCommand('${CLAUDE_PLUGIN_ROOT}-${CLAUDE_PROJECT_DIR}', { pluginRoot: '/p', projectDir: '/d' })).toBe('/p-/d')
|
|
})
|
|
it('leaves the command untouched when no vars are supplied', () => {
|
|
expect(substituteCommand('${CLAUDE_PLUGIN_ROOT}/x', {})).toBe('${CLAUDE_PLUGIN_ROOT}/x')
|
|
})
|
|
})
|
|
|
|
describe('parseClaudeConfig', () => {
|
|
it('parses a bare event map and a settings-style { hooks: … } wrapper identically', () => {
|
|
const groups = { PreToolUse: [{ matcher: 'Bash', hooks: [{ type: 'command', command: 'x.sh' }] }] }
|
|
const bare = parseClaudeConfig(groups)
|
|
const wrapped = parseClaudeConfig({ hooks: groups })
|
|
expect(bare.config).toEqual(wrapped.config)
|
|
expect(bare.config.PreToolUse).toEqual([{ matcher: 'Bash', hooks: [{ command: 'x.sh' }] }])
|
|
})
|
|
|
|
it('carries timeout → timeoutSec and substitutes the command', () => {
|
|
const { config } = parseClaudeConfig(
|
|
{ Stop: [{ hooks: [{ type: 'command', command: '${CLAUDE_PLUGIN_ROOT}/s.sh', timeout: 30 }] }] },
|
|
{ pluginRoot: '/p' },
|
|
)
|
|
expect(config.Stop).toEqual([{ hooks: [{ command: '/p/s.sh', timeoutSec: 30 }] }])
|
|
})
|
|
|
|
it('skips non-command hooks (recorded) and keeps the command ones in the same group', () => {
|
|
const { config, skipped } = parseClaudeConfig({
|
|
PreToolUse: [{ hooks: [
|
|
{ type: 'prompt', prompt: 'hi' },
|
|
{ type: 'command', command: 'ok.sh' },
|
|
{ type: 'http', url: 'http://x' },
|
|
] }],
|
|
})
|
|
expect(config.PreToolUse).toEqual([{ hooks: [{ command: 'ok.sh' }] }])
|
|
expect(skipped).toEqual([{ event: 'PreToolUse', type: 'prompt' }, { event: 'PreToolUse', type: 'http' }])
|
|
})
|
|
|
|
it('treats a hook with no `type` as a command (CC default)', () => {
|
|
const { config } = parseClaudeConfig({ Stop: [{ hooks: [{ command: 'd.sh' }] }] })
|
|
expect(config.Stop).toEqual([{ hooks: [{ command: 'd.sh' }] }])
|
|
})
|
|
|
|
it('drops malformed entries without throwing: non-array groups, non-object group/hook, missing command, empty groups', () => {
|
|
expect(parseClaudeConfig({ PreToolUse: 'nope' }).config).toEqual({})
|
|
expect(parseClaudeConfig({ PreToolUse: [42, { hooks: 'no' }, { hooks: [7, { type: 'command' }] }] }).config).toEqual({})
|
|
// a group whose only hook lacks a command string drops the whole (empty) group
|
|
expect(parseClaudeConfig({ Stop: [{ hooks: [{ type: 'command', command: 5 }] }] }).config).toEqual({})
|
|
})
|
|
|
|
it('returns empty for a non-object / null / array top level', () => {
|
|
expect(parseClaudeConfig(null).config).toEqual({})
|
|
expect(parseClaudeConfig(42).config).toEqual({})
|
|
expect(parseClaudeConfig([1, 2]).config).toEqual({})
|
|
})
|
|
|
|
it('omits the matcher key when the group has none (match-all)', () => {
|
|
const { config } = parseClaudeConfig({ Stop: [{ hooks: [{ type: 'command', command: 's.sh' }] }] })
|
|
expect('matcher' in config.Stop![0]!).toBe(false)
|
|
})
|
|
})
|