hooks: Claude Code SessionStart briefing and PreToolUse dead-end guard, release 6.11.0 #30

Merged
tim merged 3 commits from feat/claude-code-hooks-22 into develop 2026-11-03 07:50:32 +00:00
Owner

Closes #22.

Upstream's omnimem hook reads its SQLite store directly. That does not transfer: here the store is Valkey behind the MCP server and the hook runs on the client machine. So this is a client script that talks to the server over MCP, plus two small additive server changes.

What

claude_config/hooks/omnimem_hook.py: one file, Python standard library only.

  • SessionStart injects a read-only project briefing (dead ends first). Nothing recorded for the project prints nothing.
  • PreToolUse denies an Edit/MultiEdit/Write/NotebookEdit/mutating Bash call that puts an abandoned approach in place, with the reason and what worked instead.

Answer to the issue's open question (what the matcher keys on): the text the call would put in place (new_string, content, a mutating Bash command), never old_string, matched as a whole abandoned name on word boundaries, ignoring case and separators. Read/Glob/Grep are never checked, ls vendor/kestrel-rs passes, cargo add kestrel-rs does not.

Server side (additive, defaults unchanged): warn_if_abandoned(query, project?, proposal?) and briefing(read_only=True).

Decisions to look at

  1. Deny once per session per dead end. Repeating the same call goes through. A record can be stale and a guard nobody can override would deadlock an agent that has a real reason to proceed. Cost: a determined agent gets through on the second try. Upstream denies every time; say if you want that (small change).
  2. Requires MCP_TRANSPORT=http. The deprecated SSE transport needs a different protocol and is not supported; on a server still using it the script writes a one-line hint to stderr and does nothing. The shipped docs and code default are still sse, so this is a deploy prerequisite.
  3. Auth is the existing MCP_AUTH_TOKEN bearer (works alongside OAuth via MultiAuth) from OMNIMEM_TOKEN in the environment; the script refuses to send it over cleartext http to a non-loopback host. No server auth change.
  4. Matcher is stricter than the lookup form: the existing warn_if_abandoned matches query in name, so a short command like make would match any abandoned name containing it. A hook that can block work needs word boundaries, a 3-character minimum, and project scoping (records with no project apply everywhere).
  5. Not included: upstream's session-end hook (writes a session log; transcripts are where secrets surface and the redaction and dedup it needs are unmeasured) and an installer (settings.example.json is the complete entry).

A bug the real run found

Ruff at py314 rewrote except (OSError, ValueError): in the script to the PEP 758 form except OSError, ValueError:, a SyntaxError before 3.14. The hook runs with the user's python3 (3.13 here); every test ran it with the 3.14 venv interpreter and stayed green. Found only when I ran the final script against a real server with the plain python3. Fixed with per-file-target-version = py39 for claude_config/hooks/*.py in ruff.toml, plus two tests: a source check (runs in CI) and a run under any older interpreter on the machine (skips when there is none; CI's runner has only 3.14). Both fail on the bug (verified by putting it back).

Verification

  • Against the real MCP server (bearer auth on, throwaway valkey-search): deny with the way out, the once-per-session repeat, a new session denying again, look-only Bash untouched, another project unaffected, KestrelRsLike not matched, wrong token failing open, session-start output. Under Python 3.13 and 3.12. About 30 ms per call locally.
  • pytest: 2137 passed, 1 skipped (was 2086). New: test_hook_support.py (server side), test_claude_code_hook.py (script as a subprocess against a fake MCP server on a real socket, plus the old-Python guards).
  • ruff check, ruff format --check, ASCII check, bandit on the changed files (no findings, no suppressions).
  • Also fixed: the claude_config/CLAUDE.md template still said abandoned + effort 4 auto-suppresses (removed in 6.10.0).

Not measured: the guard's effect on the dead-end benchmark. The 27% figure in the benchmark README is for the prototype; this is its production form.

🤖 Generated with Claude Code

Closes #22. Upstream's `omnimem hook` reads its SQLite store directly. That does not transfer: here the store is Valkey behind the MCP server and the hook runs on the client machine. So this is a client script that talks to the server over MCP, plus two small additive server changes. ## What `claude_config/hooks/omnimem_hook.py`: one file, Python standard library only. - **SessionStart** injects a read-only project briefing (dead ends first). Nothing recorded for the project prints nothing. - **PreToolUse** denies an `Edit`/`MultiEdit`/`Write`/`NotebookEdit`/mutating `Bash` call that puts an abandoned approach in place, with the reason and what worked instead. Answer to the issue's open question (what the matcher keys on): the text the call would put in place (`new_string`, `content`, a mutating Bash command), never `old_string`, matched as a whole abandoned name on word boundaries, ignoring case and separators. `Read`/`Glob`/`Grep` are never checked, `ls vendor/kestrel-rs` passes, `cargo add kestrel-rs` does not. Server side (additive, defaults unchanged): `warn_if_abandoned(query, project?, proposal?)` and `briefing(read_only=True)`. ## Decisions to look at 1. **Deny once per session per dead end.** Repeating the same call goes through. A record can be stale and a guard nobody can override would deadlock an agent that has a real reason to proceed. Cost: a determined agent gets through on the second try. Upstream denies every time; say if you want that (small change). 2. **Requires `MCP_TRANSPORT=http`.** The deprecated SSE transport needs a different protocol and is not supported; on a server still using it the script writes a one-line hint to stderr and does nothing. The shipped docs and code default are still `sse`, so this is a deploy prerequisite. 3. **Auth** is the existing `MCP_AUTH_TOKEN` bearer (works alongside OAuth via MultiAuth) from `OMNIMEM_TOKEN` in the environment; the script refuses to send it over cleartext http to a non-loopback host. No server auth change. 4. **Matcher is stricter than the lookup form**: the existing `warn_if_abandoned` matches `query in name`, so a short command like `make` would match any abandoned name containing it. A hook that can block work needs word boundaries, a 3-character minimum, and project scoping (records with no project apply everywhere). 5. **Not included**: upstream's `session-end` hook (writes a session log; transcripts are where secrets surface and the redaction and dedup it needs are unmeasured) and an installer (`settings.example.json` is the complete entry). ## A bug the real run found Ruff at `py314` rewrote `except (OSError, ValueError):` in the script to the PEP 758 form `except OSError, ValueError:`, a SyntaxError before 3.14. The hook runs with the user's `python3` (3.13 here); every test ran it with the 3.14 venv interpreter and stayed green. Found only when I ran the final script against a real server with the plain `python3`. Fixed with `per-file-target-version = py39` for `claude_config/hooks/*.py` in `ruff.toml`, plus two tests: a source check (runs in CI) and a run under any older interpreter on the machine (skips when there is none; CI's runner has only 3.14). Both fail on the bug (verified by putting it back). ## Verification - Against the **real MCP server** (bearer auth on, throwaway valkey-search): deny with the way out, the once-per-session repeat, a new session denying again, look-only Bash untouched, another project unaffected, `KestrelRsLike` not matched, wrong token failing open, session-start output. Under Python 3.13 and 3.12. About 30 ms per call locally. - `pytest`: 2137 passed, 1 skipped (was 2086). New: `test_hook_support.py` (server side), `test_claude_code_hook.py` (script as a subprocess against a fake MCP server on a real socket, plus the old-Python guards). - `ruff check`, `ruff format --check`, ASCII check, bandit on the changed files (no findings, no suppressions). - Also fixed: the `claude_config/CLAUDE.md` template still said abandoned + effort 4 auto-suppresses (removed in 6.10.0). Not measured: the guard's effect on the dead-end benchmark. The 27% figure in the benchmark README is for the prototype; this is its production form. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Support for the Claude Code hooks, both additive:
- warn_if_abandoned(query, project, proposal): proposal=True matches a
  whole abandoned name on word boundaries (case and separators ignored),
  never `query in name`, ignores names under three characters, and keeps
  other projects' records out. A match carries the breakthrough, the way
  out, when the memory recorded one. The default call is unchanged.
- briefing(read_only=True) skips the writes: the maintenance counter and
  run, and the auto skill scan.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude_config/hooks/omnimem_hook.py, one stdlib-only file that talks to
the server over MCP (streamable HTTP, bearer token from the environment,
never over cleartext to another host). SessionStart injects a read-only
briefing with the graveyard first; PreToolUse denies a mutating tool call
that puts an abandoned approach in place and says what worked instead,
once per session so a stale record cannot deadlock the agent. Fails open
on every error. Tested against a fake MCP server on a real socket and
against the real server.

ruff.toml formats the hook for py39: at py314 the formatter rewrote
`except (A, B):` to the 3.14-only form, which broke the script on the
python3 the user has. A source test and an older-interpreter test guard it.

Also fixes the claude_config/CLAUDE.md template, which still described the
auto-suppression removed in 6.10.0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
release 6.11.0: Claude Code hooks
All checks were successful
Lint / Ruff (pull_request) Successful in 11s
Lint / ASCII only (pull_request) Successful in 2s
Security Scans / Bandit, dependency audit, secret scan (pull_request) Successful in 1m43s
Security Scans / Test Coverage (pull_request) Successful in 1m32s
f3277d53c0
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
tim merged commit aadaaa3f54 into develop 2026-11-03 07:50:32 +00:00
tim deleted branch feat/claude-code-hooks-22 2026-11-03 07:50:32 +00:00
Sign in to join this conversation.
No reviewers
No labels
upstream-feature
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
tim/omnimem!30
No description provided.