The two hook bridges (dsh-hooks-claude, dsh-hooks-codex) would otherwise duplicate
the bulk of the protocol — Codex deliberately reimplements a SUBSET of the Claude
Code protocol (same hooks.json shape, exit-code/stdout contract, command-hook
model). This library holds the genuinely-identical primitives; each bridge owns
only what differs (per-event stdin payload, env/substitution, decision mapping).
New packages/hooks/ group; hook-protocol is a LIBRARY (no plugin, registers/injects
nothing):
- matcher: matchesMatcher(pattern, query, mode) — the one dialect axis collapsed to
a mode param (claude = literal-or-regex with pipe alternation; codex = always
unanchored regex). Match-all on absent/''/'*'; invalid regex matches nothing.
- codec: parseHookOutput(exit, stdout, stderr) → dialect-neutral HookOutput. Exit 0
→ lenient JSON; exit 2 → blocking error (stderr = reason, surfaced as
decision:'block'); other → non-blocking. Parses the CC superset
(continue/stopReason/decision/hookSpecificOutput.{permissionDecision,
additionalContext,updatedInput}/systemMessage); permissionDecision overrides the
legacy top-level decision.
- runner: runHook(bash, hook, opts, now) — runs a command hook via ctx.bash (stdin
payload + trusted-plugin env), honors timeoutSec, never throws (executor reject →
non-blocking-error HookOutput). Injected clock for testable durations.
- merge: mergeHookOutputs — most-restrictive fold (deny>ask>allow, sticky stop,
block reasons joined, context/system-messages accumulated).
- hook/* session events (declaration-merged into SessionEventMap, log-only like
compact/*) + appendHookInvoked/appendHookResult helpers.
updatedInput is parsed but NOT honored (deferred pre-tool-input-rewrite RFC); a
bridge logs+warns. 47 unit tests at per-file 100% (matcher per-mode, codec per
exit-code/field, runner plumbing w/ stub executor, merge precedence, hook/*
helpers). RFC: implemented/feature/2026-06-30-hook-protocol-lib.md.
62 lines
3.0 KiB
TypeScript
62 lines
3.0 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { Session, SessionId } from '@deepseek-ai/dsh-session'
|
|
import { appendHookInvoked, appendHookResult } from '@deepseek-ai/dsh-hook-protocol'
|
|
|
|
describe('hook/* session events', () => {
|
|
it('appendHookInvoked records a log-only hook/invoked (with matcher when present)', () => {
|
|
const session = new Session(SessionId('s'))
|
|
appendHookInvoked(session, { turn: 1, point: 'PreToolUse', dialect: 'claude', handlerId: 'h1', matcher: 'Bash' })
|
|
|
|
const ev = [...session.events].find(e => e.type === 'hook/invoked')
|
|
expect(ev?.type).toBe('hook/invoked')
|
|
if (ev?.type === 'hook/invoked') {
|
|
expect(ev.data).toMatchObject({ turn: 1, point: 'PreToolUse', dialect: 'claude', handlerId: 'h1', matcher: 'Bash' })
|
|
}
|
|
// Log-only: no surfaceOp on the event.
|
|
expect((ev as unknown as { surfaceOp?: unknown }).surfaceOp).toBeUndefined()
|
|
})
|
|
|
|
it('omits matcher when absent (match-all hook)', () => {
|
|
const session = new Session(SessionId('s'))
|
|
appendHookInvoked(session, { turn: 2, point: 'Stop', dialect: 'native', handlerId: 'h2' })
|
|
|
|
const ev = [...session.events].find(e => e.type === 'hook/invoked')
|
|
if (ev?.type === 'hook/invoked') {
|
|
expect('matcher' in ev.data).toBe(false)
|
|
}
|
|
})
|
|
|
|
it('appendHookResult records the decided outcome, omitting absent optionals', () => {
|
|
const session = new Session(SessionId('s'))
|
|
appendHookResult(session, {
|
|
turn: 1, point: 'PreToolUse', handlerId: 'h1', decision: 'deny',
|
|
exitCode: 2, stderrSummary: 'blocked', durationMs: 12,
|
|
})
|
|
const full = [...session.events].find(e => e.type === 'hook/result')
|
|
if (full?.type === 'hook/result') {
|
|
expect(full.data).toMatchObject({ turn: 1, point: 'PreToolUse', handlerId: 'h1', decision: 'deny', exitCode: 2, stderrSummary: 'blocked', durationMs: 12 })
|
|
}
|
|
|
|
// A result with no exit code / no stderr (e.g. a hook that could not run) omits both keys.
|
|
const session2 = new Session(SessionId('s2'))
|
|
appendHookResult(session2, { turn: 1, point: 'Stop', handlerId: 'h3', decision: 'allow', durationMs: 3 })
|
|
const sparse = [...session2.events].find(e => e.type === 'hook/result')
|
|
if (sparse?.type === 'hook/result') {
|
|
expect('exitCode' in sparse.data).toBe(false)
|
|
expect('stderrSummary' in sparse.data).toBe(false)
|
|
expect(sparse.data.durationMs).toBe(3)
|
|
}
|
|
})
|
|
|
|
it('an invoked/result pair correlates by handlerId', () => {
|
|
const session = new Session(SessionId('s'))
|
|
appendHookInvoked(session, { turn: 1, point: 'PreToolUse', dialect: 'claude', handlerId: 'pair-1' })
|
|
appendHookResult(session, { turn: 1, point: 'PreToolUse', handlerId: 'pair-1', decision: 'allow', exitCode: 0, durationMs: 7 })
|
|
|
|
const invoked = [...session.events].find(e => e.type === 'hook/invoked')
|
|
const result = [...session.events].find(e => e.type === 'hook/result')
|
|
expect(invoked?.type === 'hook/invoked' && invoked.data.handlerId).toBe('pair-1')
|
|
expect(result?.type === 'hook/result' && result.data.handlerId).toBe('pair-1')
|
|
})
|
|
})
|