feat(hooks): record HookError codes on hook denials and blocks

A PreToolUse deny now records HookError / HOOK_DENIED and a PostToolUse
block records HookError / HOOK_BLOCKED through the guard and
post-execute info contract. Model-facing reasons are unchanged.

Refs #7869
This commit is contained in:
sjw1231 committed 2026-11-09 14:50:04 +08:00
1 parent 8421590853
commit 98e08f2fa2
15 files changed
+47 -14

No files matched your search

@@ -39,8 +39,8 @@
en: 7f9239eaea5a945c
zh: 9ba6a2ea955d4934
/deepseek-ai-dsh-hook-protocol/understand-the-implementation/source-map:
en: 0d8956a6d8608ee0
zh: 9f554284c4c0a36e
en: 60f13bbc4bd35669
zh: a523f557a26dfade
/deepseek-ai-dsh-hook-protocol/further-exploration:
en: 8b585ce73c2be2cc
zh: c0270fd001e1352b
@@ -88,6 +88,7 @@ The [hook-protocol-lib Agent Note](../../../.agents/notes/archived/feature/2026-
| [`src/runner.ts`](src/runner.ts) | `runHook` execution through `ctx.shell` and `DEFAULT_HOOK_TIMEOUT_MS` |
| [`src/codec.ts`](src/codec.ts) | Exit-code and structured-stdout decoding into `HookOutput` |
| [`src/merge.ts`](src/merge.ts) | Most-restrictive merge and the `MergedHookOutcome` type |
| [`src/errors.ts`](src/errors.ts) | `HookError` name with `HOOK_DENIED` and `HOOK_BLOCKED` codes recorded on denied and blocked tool calls |
| [`src/events.ts`](src/events.ts) | `hook/*` event declaration, append helpers, stderr summary |
| [`src/detached.ts`](src/detached.ts) | Detached-run quiescence tracking |
| [`src/types.ts`](src/types.ts) | `HookOutput`, `MatcherGroup`, `CommandHook`, and the `hook/*` payload types |
@@ -88,6 +88,7 @@ kind: "package-library"
| [`src/runner.ts`](src/runner.ts) | 通过 `ctx.shell` 的 `runHook` 执行与 `DEFAULT_HOOK_TIMEOUT_MS` |
| [`src/codec.ts`](src/codec.ts) | 退出码与结构化 stdout 解码为 `HookOutput` |
| [`src/merge.ts`](src/merge.ts) | 最严格合并与 `MergedHookOutcome` 类型 |
| [`src/errors.ts`](src/errors.ts) | 被拒绝和被阻塞的工具调用所记录的 `HookError` 名称及 `HOOK_DENIED`、`HOOK_BLOCKED` code |
| [`src/events.ts`](src/events.ts) | `hook/*` 事件声明、追加辅助函数、stderr 摘要 |
| [`src/detached.ts`](src/detached.ts) | 脱离运行的完全停稳跟踪 |
| [`src/types.ts`](src/types.ts) | `HookOutput`、`MatcherGroup`、`CommandHook` 与 `hook/*` 载荷类型 |
@@ -0,0 +1,10 @@
/** Structured error identity that hook-backed tool policies record on denied and blocked calls. @module */
/** Error name shared by every hook policy failure. */
export const HOOK_ERROR_NAME = 'HookError'
/** Code for a call a `PreToolUse` hook denied before dispatch; the model should not retry the call unchanged. */
export const HOOK_DENIED = 'HOOK_DENIED'
/** Code for a result a `PostToolUse` hook blocked after dispatch; the hook's feedback replaces the result. */
export const HOOK_BLOCKED = 'HOOK_BLOCKED'
@@ -13,6 +13,7 @@ export type {
MatcherGroup,
MatcherMode,
} from './types.ts'
export { HOOK_BLOCKED, HOOK_DENIED, HOOK_ERROR_NAME } from './errors.ts'
export { matcherDiagnostic, matchesMatcher, selectHookGroups } from './matcher.ts'
export { parseHookOutput } from './codec.ts'
export { DEFAULT_HOOK_TIMEOUT_MS, runHook } from './runner.ts'
@@ -72,8 +72,8 @@
en: 82be8d95921ef639
zh: 11c185068ec18932
/deepseek-ai-dsh-hooks-claude-code/model-experience/blocked-prompt-or-tool-outcome/what-the-model-sees:
en: 1313b662532a2bda
zh: 4002d38540286a94
en: fe02a31c04fd91d1
zh: 39e55aad9940eae4
/deepseek-ai-dsh-hooks-claude-code/model-experience/blocked-prompt-or-tool-outcome/token-effect:
en: 7c13e10d2a0e3151
zh: 54ffd88bca4fce3a
@@ -155,7 +155,7 @@ Append-only; newly visible content follows the reusable request prefix and does
#### What the model sees
Provider-supplied reasons pass through verbatim. When absent, a denied tool becomes `Error: blocked by PreToolUse hook`, blocked post-tool feedback is exactly `blocked by PostToolUse hook`, and a blocking stop adds steering exactly `continue: blocked by Stop hook`; a blocked prompt is discarded with no model-visible message, ending the turn as `blocked`. `systemMessage` and `updatedInput` are logged or warned but are not model-visible in this implementation.
Provider-supplied reasons pass through verbatim. When absent, a denied tool becomes `Error: blocked by PreToolUse hook`, blocked post-tool feedback is exactly `blocked by PostToolUse hook`, and a blocking stop adds steering exactly `continue: blocked by Stop hook`; a blocked prompt is discarded with no model-visible message, ending the turn as `blocked`. Denied tool calls record `HookError` with `HOOK_DENIED` on `tool/result`, and blocked post-tool results record `HookError` with `HOOK_BLOCKED`. `systemMessage` and `updatedInput` are logged or warned but are not model-visible in this implementation.
#### Token effect
@@ -155,7 +155,7 @@ hook 不返回上下文时没有成本。Hook 文本取决于数据,会被记
#### 模型看到什么
提供方提供的原因逐字传递。缺失原因时,已拒绝工具变为 `Error: blocked by PreToolUse hook`,已阻塞工具后反馈精确为 `blocked by PostToolUse hook`,阻塞 stop 则精确添加 steering `continue: blocked by Stop hook`;已阻塞提示词不会产生任何模型可见消息,而是以 `blocked` 结束该轮次。`systemMessage` 与 `updatedInput` 会被记录或警告,但在此实现中对模型不可见。
提供方提供的原因逐字传递。缺失原因时,已拒绝工具变为 `Error: blocked by PreToolUse hook`,已阻塞工具后反馈精确为 `blocked by PostToolUse hook`,阻塞 stop 则精确添加 steering `continue: blocked by Stop hook`;已阻塞提示词不会产生任何模型可见消息,而是以 `blocked` 结束该轮次。已拒绝的工具调用在 `tool/result` 上记录 `HookError` 与 `HOOK_DENIED`,已阻塞的工具后结果记录 `HookError` 与 `HOOK_BLOCKED`。`systemMessage` 与 `updatedInput` 会被记录或警告,但在此实现中对模型不可见。
#### Token 影响
@@ -30,6 +30,9 @@ import {
createDetachedRuns,
DEFAULT_HOOK_TIMEOUT_MS,
DEFAULT_STDERR_SUMMARY_MAX_CHARS,
HOOK_BLOCKED,
HOOK_DENIED,
HOOK_ERROR_NAME,
mergeHookOutputs,
runHook,
selectHookGroups,
@@ -243,7 +246,7 @@ export function apply(ctx: Context, config: Config): void {
ctx.on('tools/pre-execute', async (exec, next): Promise<PreToolDecision> => {
const turn = lastTurn(ctx, exec.agent)
const merged = await runPoint('PreToolUse', exec.name, preToolPayload(exec), { ...exec.agent ? { agent: exec.agent } : {}, turn, signal: exec.signal })
if (merged.decision === 'deny') return { kind: 'deny', reason: merged.reason ?? 'blocked by PreToolUse hook' }
if (merged.decision === 'deny') return { kind: 'deny', reason: merged.reason ?? 'blocked by PreToolUse hook', info: { name: HOOK_ERROR_NAME, code: HOOK_DENIED } }
if (merged.decision === 'ask') return { kind: 'ask', ...merged.reason !== undefined ? { reason: merged.reason } : {} }
return next()
})
@@ -254,7 +257,12 @@ export function apply(ctx: Context, config: Config): void {
const merged = await runPoint('PostToolUse', exec.name, postToolPayload(exec, result), { ...exec.agent ? { agent: exec.agent } : {}, turn, signal: exec.signal })
const context = contextFrom(merged)
if (merged.decision === 'deny') {
return { kind: 'block', feedback: [{ type: 'text', text: merged.reason ?? 'blocked by PostToolUse hook' }], ...context ? { additionalContexts: [context] } : {} }
return {
kind: 'block',
feedback: [{ type: 'text', text: merged.reason ?? 'blocked by PostToolUse hook' }],
info: { name: HOOK_ERROR_NAME, code: HOOK_BLOCKED },
...context ? { additionalContexts: [context] } : {},
}
}
// Our hooks did not block. DELEGATE so a later listener can still block/replace,
// then fold our context onto its decision (a downstream block carries it too).
@@ -288,6 +288,7 @@ export function defineCoverageCases(group: CoverageGroup): void {
await waitForIdle(ctx, agent)
const result = events(agent).find(e => e.type === 'tool/result')
expect(result?.type === 'tool/result' && result.data.message.content.some(b => b.type === 'text' && b.text.includes('blocked by PreToolUse hook'))).toBe(true)
expect(result?.type === 'tool/result' && result.data.error).toEqual({ name: 'HookError', code: 'HOOK_DENIED' })
})
it('PostToolUse deny with EMPTY stderr + no context uses the default feedback', async () => {
@@ -302,6 +303,7 @@ export function defineCoverageCases(group: CoverageGroup): void {
await waitForIdle(ctx, agent)
const result = events(agent).find(e => e.type === 'tool/result')
expect(result?.type === 'tool/result' && result.data.message.content.some(b => b.type === 'text' && b.text.includes('blocked by PostToolUse hook'))).toBe(true)
expect(result?.type === 'tool/result' && result.data.error).toEqual({ name: 'HookError', code: 'HOOK_BLOCKED' })
})
it('SubagentStop with no registered child runs the hook cleanly (fire-and-forget)', async () => {
@@ -72,8 +72,8 @@
en: 82be8d95921ef639
zh: 11c185068ec18932
/deepseek-ai-dsh-hooks-codex/model-experience/blocked-prompt-or-tool-outcome/what-the-model-sees:
en: fd44027de2255f3f
zh: d880eb4d2d3f6af8
en: ca7de360c3a5f25e
zh: 639c09fef35e4a2d
/deepseek-ai-dsh-hooks-codex/model-experience/blocked-prompt-or-tool-outcome/token-effect:
en: 7c13e10d2a0e3151
zh: 54ffd88bca4fce3a
+1 -1
View File
@@ -151,7 +151,7 @@ Append-only; newly visible content follows the reusable request prefix and does
#### What the model sees
Provider-supplied reasons pass through verbatim. When absent, a denied tool becomes `Error: blocked by PreToolUse hook`, blocked post-tool feedback is exactly `blocked by PostToolUse hook`, and a blocking stop adds steering exactly `continue: blocked by Stop hook`; a blocked prompt is discarded with no model-visible message, ending the turn as `blocked`. Codex `systemMessage` is not surfaced.
Provider-supplied reasons pass through verbatim. When absent, a denied tool becomes `Error: blocked by PreToolUse hook`, blocked post-tool feedback is exactly `blocked by PostToolUse hook`, and a blocking stop adds steering exactly `continue: blocked by Stop hook`; a blocked prompt is discarded with no model-visible message, ending the turn as `blocked`. Denied tool calls record `HookError` with `HOOK_DENIED` on `tool/result`, and blocked post-tool results record `HookError` with `HOOK_BLOCKED`. Codex `systemMessage` is not surfaced.
#### Token effect
@@ -151,7 +151,7 @@ hook 不返回上下文时没有成本。Hook 文本取决于数据,会被记
#### 模型看到什么
提供方提供的原因逐字传递。缺失原因时,已拒绝工具变为 `Error: blocked by PreToolUse hook`,已阻塞工具后反馈精确为 `blocked by PostToolUse hook`,阻塞 stop 则精确添加 steering `continue: blocked by Stop hook`;已阻塞提示词不会产生任何模型可见消息,而是以 `blocked` 结束该轮次。Codex `systemMessage` 不会呈现。
提供方提供的原因逐字传递。缺失原因时,已拒绝工具变为 `Error: blocked by PreToolUse hook`,已阻塞工具后反馈精确为 `blocked by PostToolUse hook`,阻塞 stop 则精确添加 steering `continue: blocked by Stop hook`;已阻塞提示词不会产生任何模型可见消息,而是以 `blocked` 结束该轮次。已拒绝的工具调用在 `tool/result` 上记录 `HookError` 与 `HOOK_DENIED`,已阻塞的工具后结果记录 `HookError` 与 `HOOK_BLOCKED`。Codex `systemMessage` 不会呈现。
#### Token 影响
+10 -2
View File
@@ -33,6 +33,9 @@ import {
createDetachedRuns,
DEFAULT_HOOK_TIMEOUT_MS,
DEFAULT_STDERR_SUMMARY_MAX_CHARS,
HOOK_BLOCKED,
HOOK_DENIED,
HOOK_ERROR_NAME,
mergeHookOutputs,
runHook,
selectHookGroups,
@@ -228,7 +231,7 @@ export function apply(ctx: Context, config: Config): void {
const turn = lastTurn(ctx, exec.agent)
const merged = await runPoint('PreToolUse', exec.name, preToolPayload(ctx, exec, model), { ...exec.agent ? { agent: exec.agent } : {}, turn, signal: exec.signal })
/* jscpd:ignore-end */
if (merged.decision === 'deny') return { kind: 'deny', reason: merged.reason ?? 'blocked by PreToolUse hook' }
if (merged.decision === 'deny') return { kind: 'deny', reason: merged.reason ?? 'blocked by PreToolUse hook', info: { name: HOOK_ERROR_NAME, code: HOOK_DENIED } }
return next()
})
@@ -239,7 +242,12 @@ export function apply(ctx: Context, config: Config): void {
const merged = await runPoint('PostToolUse', exec.name, postToolPayload(ctx, exec, result, model), { ...exec.agent ? { agent: exec.agent } : {}, turn, signal: exec.signal })
const context = contextFrom(merged)
if (merged.decision === 'deny') {
return { kind: 'block', feedback: [{ type: 'text', text: merged.reason ?? 'blocked by PostToolUse hook' }], ...context ? { additionalContexts: [context] } : {} }
return {
kind: 'block',
feedback: [{ type: 'text', text: merged.reason ?? 'blocked by PostToolUse hook' }],
info: { name: HOOK_ERROR_NAME, code: HOOK_BLOCKED },
...context ? { additionalContexts: [context] } : {},
}
}
// Context alone is not a veto: DELEGATE, then fold our context onto the
// downstream decision (a downstream block carries it too).
@@ -235,6 +235,7 @@ export function defineCoverageCases(groups: CoverageGroup | readonly CoverageGro
const r = events(agent).find(e => e.type === 'tool/result')
expect(r?.type === 'tool/result' && r.data.message.isError).toBe(true)
expect(r?.type === 'tool/result' && r.data.message.content.some(b => b.type === 'text' && b.text.includes('blocked by PostToolUse hook'))).toBe(true)
expect(r?.type === 'tool/result' && r.data.error).toEqual({ name: 'HookError', code: 'HOOK_BLOCKED' })
})
it('PostToolUse additionalContext (clean exit) is attached after the result', async () => {
@@ -437,6 +438,7 @@ export function defineCoverageCases(groups: CoverageGroup | readonly CoverageGro
agent.followup(createUserMessage({ content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } })); await waitForIdle(ctx, agent)
const r = events(agent).find(e => e.type === 'tool/result')
expect(r?.type === 'tool/result' && r.data.message.content.some(b => b.type === 'text' && b.text.includes('blocked by PreToolUse hook'))).toBe(true)
expect(r?.type === 'tool/result' && r.data.error).toEqual({ name: 'HookError', code: 'HOOK_DENIED' })
})
it('PostToolUse block AND additionalContext are surfaced together', async () => {