Skip to content

feat(hooks): tool_pattern 正则工具匹配器(对齐 Claude Code / Codex matcher) - #565

Merged
Qiyuanqiii merged 2 commits into
mainfrom
feat/hook-tool-matchers
Nov 12, 2026
Merged

Qiyuanqiii merged 2 commits into
mainfrom
feat/hook-tool-matchers

Conversation

@Qiyuanqiii

Copy link
Copy Markdown
Member

改动背景

继续对齐 Claude Code 与 Codex 的 hooks 能力:两者的工具事件 matcher 都支持正则(Claude Code 为非锚定 JS 正则、Codex 为正则字符串),而本项目的 hook 只能按 tool_names 精确列表过滤且上限 64 条。真实工具家族会让精确列表失效——MCP server 的工具名是动态的(mcp_github_create_issue、mcp_github_create_pr、…),一个已审查插件想禁止整个写家族,要么逐一枚举、要么耗尽整个列表预算。

本 PR 堆叠在 #564(其又堆叠在 #563)之上,base 为 feat/user-prompt-submit-hooks;请按 #563 → #564 → 本 PR 顺序合并,base 会随前序合并自动重定向到 main。

关键设计

  • 每条声明一个过滤器,两种形式二选一:tool_names 精确列表 或 单条 tool_pattern Go 正则(对工具名非锚定匹配,与竞品的实际匹配行为一致),声明两者同时存在会被拒绝;^(Bash|Edit)$ 这类交替可覆盖任意并集。
  • 有界且线性时间:pattern 上限 256 字节、拒绝 NUL 与控制字符、必须可编译为 Go regexp——Go regexp 是线性时间引擎,恶意或粗糙的 pattern 不可能在 hook 边界引发灾难性回溯;编译发生在注册表替换时(loader 每边界刷新都会重新编译),编译失败 fail-closed。
  • 同代次契约:pattern 属于声明指纹的一部分,修改即推进 registry generation(与其它声明编辑一致);插件诊断投影(HookDeclarationView)同步暴露 tool_pattern,操作者视图渲染真实过滤器而非空列表。
  • 非工具事件不变:user_prompt_submit、run/session/compaction/subagent/checkpoint 边界没有工具名,两种过滤器均按既有规则拒绝。
  • 契约链全量同步:Go 投影 → docs/openapi.json 再生成 → web/src/api/schema.d.ts(npm run generate:api)→ 前端 exact-keys 校验(允许空串、与 Go validText 同样要求 trim 一致、非空时互斥 + 工具事件限定)→ 声明规则面板显示 /pattern/。

验证方式

  • go vet、gofmt、go test -race -count=1 ./internal/hooks/ 通过。
  • internal/hooks:校验(互斥/超长/不可编译/NUL/非工具事件)、非锚定匹配(命中/不命中/无关工具)、generation 随 pattern 变化推进、Replace 时编译失败关闭。
  • internal/httpapi Hook/Extension 子集通过;internal/plugins 全套通过。
  • 前端:npm run generate:api + check:api(契约一致性)、typecheck 通过、plugin-lifecycle 相关 vitest 24/24 通过。
  • 文档:docs/extensions.md 双语 hook 契约补充过滤器规则;新增 ADR 0199。

Extend tool-event hook declarations with an optional tool_pattern: a single
Go regular expression (unanchored against the tool name, at most 256 bytes,
compiled at registry replacement and stored beside the sorted registrations)
that replaces the exact tool_names list. Both external harnesses match tools
with regular expressions; dynamic MCP tool families cannot be expressed as a
64-entry exact list. The pattern participates in the registry generation
fingerprint, fails closed when it does not compile, and rejects a byte order
mark so the web parser and the Go projection agree on every byte.

The plugin diagnostics projection exposes the declared pattern, the OpenAPI
snapshot and the generated TypeScript schema are regenerated with a
maxLength constraint, and the web hook diagnostics parser and declared-rules
view learn the new field.
@coderabbitai

coderabbitai Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@Qiyuanqiii
Qiyuanqiii changed the base branch from feat/user-prompt-submit-hooks to main November 12, 2026 03:15
@Qiyuanqiii
Qiyuanqiii merged commit f74720d into main Nov 12, 2026
19 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants