Skip to content

fix(s04, s05): register log_hook before permission_hook so denied calls get logged - #660

Open
shuhuNB515 wants to merge 1 commit into
shareAI-lab:mainfrom
shuhuNB515:fix/hooks-log-order
Open

shuhuNB515 wants to merge 1 commit into
shareAI-lab:mainfrom
shuhuNB515:fix/hooks-log-order

Conversation

@shuhuNB515

Copy link
Copy Markdown

Fixes #654

Problem

s04_hooks/code.py (and the inherited copy in s05_todo_write/code.py) registers:

register_hook("PreToolUse", permission_hook)
register_hook("PreToolUse", log_hook)

trigger_hooks runs callbacks in registration order and returns on the first non-None result (code.py:142-147). So for any call that permission_hook denies, log_hook — whose docstring says "log every tool call" — never runs. The denied calls, exactly the ones you want in a log when debugging permission behavior, produced no [HOOK] output at all.

Fix

Swap the registration order in both chapters and add a two-line comment recording why:

# log_hook returns None and never blocks; registering it first lets the
# [HOOK] line fire for denied calls too, so the log covers every call.
register_hook("PreToolUse", log_hook)
register_hook("PreToolUse", permission_hook)

log_hook returns None, so it can't influence the permission decision — only the log coverage changes. This also doubles as a small illustration of the hook-ordering semantics the chapter teaches. (The alternative — rewording the docstring to "every allowed call" — is fine too; I picked the reorder since the log is most useful when it sees denials.)

Verification

  • Loaded the real modules: a deny-listed bash call now prints [HOOK] bash(...) before being denied, the denial verdict is byte-identical, an allowed call still passes through with None, and the registration order is ["log_hook", "permission_hook"] in both s04 and s05.
  • python3 -m pytest tests/ — 503 passed, 118 subtests passed.
  • Embedded sources in web/src/data/generated/versions.json (s04 + s05) synced, as asserted by tests/test_web_scenarios.py.

Disclosed AI assistance: found and fixed during an AI-assisted review; I verified the behavior before/after and stand behind the change.

log_hook's docstring promises 'log every tool call', but it was
registered after permission_hook, and trigger_hooks short-circuits on
the first non-None result. Every denied call therefore produced no
[HOOK] line -- the calls most worth logging left no trace.

Register log_hook first: it returns None so it never blocks, the
permission outcome is unchanged, and denied calls now get logged too.
A short comment records the ordering intent. Same change in s04 and
s05 (s05 inherits the hooks chapter's setup), plus the matching
embedded sources in the web registry.

Fixes shareAI-lab#654

This branch has not been deployed

No deployments
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.

s04/s05: log_hook claims to "log every tool call" but registered after permission_hook, so denied calls are never logged

1 participant