Skip to content

Native ZCode Agent does not trigger hooks configured in ~/.zcode/cli/config.json #32

Closed as not planned

Description

@laozhouxingke

Problem

When using ZCode Agent (the native built-in agent), lifecycle hooks configured under hooks.events in ~/.zcode/cli/config.json are never triggered.

Config used

{
  "hooks": {
    "enabled": true,
    "timeoutMs": 60000,
    "maxOutputBytes": 32768,
    "events": {
      "UserPromptSubmit": [
        {
          "matcher": "",
          "hooks": [
            {
              "type": "command",
              "command": "node",
              "args": ["/path/to/hook.js"]
            }
          ]
        }
      ]
    }
  }
}

The hook script writes to a debug log on entry (before any stdin read), so if it were invoked at all, the log would appear. After multiple ZCode Agent sessions with prompts and tool use, the log file is never written.

What was also tried

  • Registering hooks via the plugin system (~/.zcode/cli/plugins/) — same result, hooks not triggered for ZCode Agent sessions
  • Hook command executes correctly when invoked manually via PowerShell

Expected behavior

Hooks in hooks.events should fire for ZCode Agent sessions (SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, Stop, etc.) the same way they work for external CLI subagents.

Question

Is this a known limitation? Is there a supported way to receive lifecycle events from ZCode Agent sessions externally (e.g., a local HTTP/WebSocket API, IPC, or log file)?

Activity

  1. github-actions commented on Jun 22, 2026

    @github-actions

    👋 感谢你的反馈,我们已经收到。

    • 维护者会在 3 个工作日内 给出首次响应。
    • 状态保持为 status: 待评估,你可以随时补充信息。
    • 信息不全时我们会打上 needs: 更多信息 标签并 @ 你。

    👋 Thanks — we've received your issue.

    • A maintainer will respond within 3 business days.
    • Status stays at status: 待评估 (Triage); feel free to add context.
    • If we need more details, we'll add needs: 更多信息 and ping you.
  2. Krutov-mcp commented on Jul 19, 2026

    @Krutov-mcp

    Adding to the original report — I ran into the same core issue (hooks not firing), but was able to get them working after changing the format. Leaving notes in case useful.

    Diagnosis

    When using exec form with a separate args field (as described in Claude Code docs and Discussion #9), ZCode marks the entire config.json as invalid and no hooks are registered at all:

    event: config.file.invalid
    diagnosticMessage: "hooks.events.UserPromptSubmit.0.hooks.0: Unrecognized key: \"args\""
    diagnosticCode: config_file_invalid
    

    Found in ZCode logs at ~/.zcode/cli/log/zcode-YYYY-MM-DD.jsonl.

    The args field appears to no longer be recognized by the validator (schema?). Notable behavior: a single invalid entry disables the entire hooks section, including any other valid hooks.

    Workaround — shell form only

    Using a single command string with quoted paths (instead of args) makes everything work end-to-end:

    {
      "hooks": {
        "enabled": true,
        "events": {
          "UserPromptSubmit": [{
            "hooks": [{
              "type": "command",
              "command": "\"C:\\Program Files\\nodejs\\node.exe\" \"C:\\Users\\<user>\\.zcode\\hooks\\my-hook.js\"",
              "timeoutMs": 5000
            }]
          }]
        }
      }
    }

    The node script reads JSON from stdin and returns {hookSpecificOutput: {hookEventName, additionalContext}} — additionalContext is injected into the model context as documented.

    Events verified working (empirically, 2026-07-20)

    Desktop build on Windows 10:

    • ✅ SessionStart
    • ✅ UserPromptSubmit — additionalContext injection confirmed
    • ✅ Stop — fires only after the model fully completes its response (not on user interruption)
    • ✅ PreToolUse with matcher: "Bash" — permissionDecision: "deny" blocks the tool call

    Possible doc discrepancy

    Discussion #9 by @GoooIce mentions args as part of the schema (based on decompile analysis, Jun 14 2026). On my current build (Jul 20 2026), args is rejected with config.file.invalid. Either (a) args was removed/restricted in a build between those dates, or (b) the desktop client validator is stricter than the embedded runtime. Would be useful to know which format is intended canonical.

    Working 3-hook setup (for reference)

    Deployed three production hooks (node.exe + JS script, shell form as above):

    1. UserPromptSubmit → workflow-trigger reminder (L-Z069-like pattern)
    2. Stop → verify-before-complete checklist (block if "done" without test markers)
    3. PreToolUse(Bash) → block destructive commands (rm -rf, git push -f, curl|sh)

    All three work end-to-end after switching to shell form. Hook logs are written, additionalContext shows up in model context, permissionDecision: "deny" properly blocks tool calls.

    Hope this helps clarify what works and what doesn't right now.

  3. panda920 commented on Aug 17, 2026

    @panda920
    Collaborator

    这条和 #167 是同一个问题,已合并到 #167 统一跟进。

    共同根因:hooks 在配置/插件里注册成功、UI 也显示已加载,但运行时多个事件(Stop、PreToolUse、SubagentStart/Stop 等)从不触发,导致依赖 hook 的插件(dcg、SAP 等)整体失效。

    合并是为了让研发看到完整上下文,而不是同一件事被拆成 4 条各自跟进。你的反馈没有丢——我已经在 #167 里把这几条一起列了出来,后续进展都会更新在那边,建议去 #167 点一下 Subscribe。

    如果你认为这其实是另一个问题,直接回复本条,我们会重新拆开。


    This is the same problem as #167, and is now tracked there.

    Shared root cause: Hooks register successfully in config/plugins and show as loaded in the UI, but several events (Stop, PreToolUse, SubagentStart/Stop, …) never fire at runtime, which breaks any plugin built on hooks (dcg, SAP, and others).

    We're consolidating so the team sees one complete picture instead of the same thing split across 4 issues. Nothing is lost — your report is listed in #167, all updates will go there, and you're welcome to subscribe to it.

    If you think this is actually a different problem, reply here and we'll split it back out.

  4. added
    duplicate
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicatepriority: P2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions