Repository navigation
Description
keyfence install-hooks claude-code and keyfence doctor treat the hook as installed once keyfence's entry is in settings.json. They do not check whether Claude Code applies that file. Claude Code ignores the whole file when any PreToolUse entry in it cannot be loaded. keyfence then reports a hook and 42 deny rules that are not in force, and nothing tells the user.
Claude Code's side
Checked with claude doctor, which reads the settings files without signing in, on Claude Code 2.1.317. A file with one PreToolUse entry it cannot load gets this:
.claude/settings.json › hooks.PreToolUse.0: Hook matcher must be an object; received a string — a
PreToolUse/PermissionRequest hook that cannot be loaded may be what guards the permissions declared
beside it, so nothing it sits in is applied until the entry is fixed or removed.
The same "nothing it sits in is applied" text appears for:
PreToolUse entry |
claude doctor |
|---|---|
"x" |
Hook matcher must be an object; received a string |
{"hooks": "y"} |
Hook matcher "hooks" must be an array of hook entries |
{"matcher": "Read", "hooks": [null]} |
Hook entry must be an object; received null |
{"matcher": 5, "hooks": [{"type": "command", "command": "true"}]} |
Invalid hook matcher (matcher: … expected string, received number) |
{"matcher": "Read", "hooks": [{"type": "command"}]} |
Invalid command hook (command: … expected string, received undefined) |
Other bad values cost only themselves, and keyfence already handles those correctly:
UserPromptSubmitentry: a malformed entry is reported asmatcher ignored; only that entry is dropped.denyvalue: a non-string value is reported asNon-string value in deny array was removed; only that value is dropped.
The settings docs describe the same split. A value the schema rejects is a Settings Error: in an interactive session Claude Code offers to continue without the broken settings, and a -p run skips them silently.
keyfence's side
On main at 247c418, the last two shapes in the table install without complaint. The first three end in a traceback, which #164 turns into a successful install. In a project directory:
$ printf '{"hooks":{"PreToolUse":[{"matcher":5,"hooks":[{"type":"command","command":"true"}]}]}}' > .claude/settings.json
$ keyfence install-hooks claude-code --project
Hook installed in .../.claude/settings.json for this project.
...
$ keyfence doctor
ok Claude Code hook: installed (project)
ok Claude Code prompt hook: installed (project)
$ claude doctor
... hooks.PreToolUse.0: Invalid hook matcher ... nothing it sits in is applied ...
The user sees a hook and deny rules that Claude Code is not applying. In a keyfence session, the Python hook does not run and the .env deny rules are off.
doctor has a related gap. It shows info Claude Code hook: not installed for a file it cannot read at all (invalid JSON, or a section of the wrong type after #165). It does not say why, and a user who knows the hook is in the file has nothing to act on.
Scope
#164, with the change requested in its review, makes install-hooks stop on the three entry shapes keyfence already parses: an entry that is not an object, hooks that is not a list, and a hook that is not an object. This issue covers the rest: entries that are structurally fine for keyfence but that Claude Code's schema rejects, such as the matcher and command cases above. keyfence cannot mirror Claude Code's whole schema, and the schema changes between releases, so a fix should not try to.
Possible directions
- Point at the authority. After
install-hooks claude-code, and indoctorwhen a Claude Code hook is found, print a line sayingclaude doctorconfirms whether Claude Code applies the file. This is cheap and always correct, but it relies on the user running it. - Ask Claude Code. When
claudeis onPATH,doctorrunsclaude doctorand reports aFAILif the settings file it checked is listed under "Invalid settings". In my runs it finished in a few seconds without a login. The output is plain text with no stable format, so a parse failure must degrade to the hint above, never to a falseok. - Say why
doctorcannot read a file. For an unreadable file, replacenot installedwithcannot read <path>: <reason>. This changes the current invalid-JSON output, which Read a malformed settings file in doctor like one that is not JSON #165 left alone on purpose.
Acceptance criteria
- With a
PreToolUseentry that Claude Code rejects,keyfence doctordoes not printokfor the Claude Code hook in that scope. -
keyfence install-hooks claude-codeeither refuses such a file or says after install that Claude Code may not apply it, and how to check. - For a settings file it cannot read,
keyfence doctornames the file and the reason. - If
claude doctoris used, a missingclaudebinary, a timeout or unrecognised output leads to the hint, never to a falseokor a traceback. - Tests cover the
matcherandcommandshapes above, plus aUserPromptSubmitentry that is malformed, which must not trigger the warning because Claude Code drops only that entry.
Activity