zai-org / zai-org/feedback

[Bug] UserPromptSubmit hooks 的 matcher 不生效:每条消息无条件触发所有 hooks

Open
#730 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

priority: P2 status: 待评估 type: Bug
Dominant language
No language data
Stars
22
Forks
1
PR merge metrics
No merged PRs in 30d

Description

提交前确认 · Pre-submission checklist
  • 我已搜索过现有 issue,确认这不是重复 / I searched existing issues and confirmed this isn't a duplicate.
  • 我已阅读 CONTRIBUTING.md / I've read CONTRIBUTING.md.
问题类别 · Category

工具调用 / MCP · Tool use / MCP

涉及的 Agent 框架 · Agent framework

ZCode Agent(自研)

严重程度 · Severity

影响体验 · Major (功能可用但体验受损 / works but degraded)

复现频率 · Reproducibility

必现 · Always

问题描述 · Description

在 ~/.zcode/cli/config.json 的 hooks.events.UserPromptSubmit 配置多个带精确 matcher 的 hooks(如 "^\s报数\s$"),发送任意不匹配的消息(如"你好")时,所有 hooks 仍全部被触发执行,matcher 完全没有起作用。

源码分析(zcode.cjs 0.16.9):

  1. UserPromptSubmit 的 hookRunner.run(...) 第二参数只传 {signal},未传 matchValue —— 对照 PreToolUse/PermissionRequest 等工具事件传的是 {matchValue: toolName};
  2. 匹配函数在 matchValues 为空时直接 return true(即无条件匹配所有 hooks);
  3. 单值匹配函数 Hro 本身支持正则(new RegExp(t).test(e)),只是 UserPromptSubmit 场景收不到匹配值。
复现步骤 · Steps to reproduce
  1. 在 ~/.zcode/cli/config.json 配置 hooks.events.UserPromptSubmit,挂两个 matcher 互斥的 hooks(例如 "^\s测试A\s$" 与 "^\s测试B\s$"),且 hooks.enabled: true;
  2. 新会话中发送任意不匹配的消息(如"你好");
  3. 观察两个 hook 均被触发执行(脚本有副作用即可见)。
期望表现 · Expected behavior

按 hooks 文档 "UserPromptSubmit → the prompt text":matcher 应对 prompt 文本做正则匹配,只有命中的 hook 才执行。

实际表现 · Actual behavior

所有 UserPromptSubmit hooks 对每条消息无条件触发,matcher 被完全忽略。实际事故:配置了精确触发词(如 ^报数$)的桌面自动化 hooks,结果用户每发一条消息全部触发,后台自动化流程被意外拉起多次。

ZCode 版本 · ZCode version

0.16.9(zcode doctor: zcode-cli 0.16.9, node v24.12.0)

设备 / 系统 / 浏览器 · Device / OS / Browser

Windows 11 x64 (10.0.26200),ZCode 桌面端(含内置 CLI)

截图 / 录屏 / 日志 · Screenshots / Recordings / Logs

最小复现配置:
"hooks": { "enabled": true, "events": { "UserPromptSubmit": [
{ "matcher": "^\s测试A\s$", "hooks": [ { "type": "command", "command": "echo A" } ] },
{ "matcher": "^\s测试B\s$", "hooks": [ { "type": "command", "command": "echo B" } ] }
] } }
发送"你好" → A、B 两个 hook 都执行。

备注:hook 级 "enabled": false 禁用单个 hook 是有效的(已验证,可作临时缓解)。

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by inspecting zcode.cjs 0.16.9 around the UserPromptSubmit hookRunner.run(...) call and compare its arguments with PreToolUse and PermissionRequest. Trace the matcher path, including Hro, using the provided two-hook configuration and an unmatched prompt; done means only hooks whose matcher matches the prompt text execute.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.