Repository navigation
perf: drop the dead guard config lookup and hoist constants out of the per-call path - #120
Merged
Merged
Conversation
`_plugin_pre_tool_call` rebuilt its constants on every invocation: a realpath over the three protected Hermes paths, both pattern tuples, and a regex compile, all to produce the same answer every time. The hook runs on every tool call, so that work was repeated indefinitely. Move them to module level, where they are computed once at import. `_is_dangerous` becomes a module-level function for the same reason. Behavior is unchanged. Measured on the real hook: 0.180 ms median for a safe write, 0.131 ms for a terminal echo, 0.120 ms for a non-terminal tool. The per-call realpath alone was 0.091 ms. The absolute numbers are small, so this is a correctness-of-structure fix rather than a measurable speedup. Closes #118
`_HERMES_CORE` held `realpath` values resolved once at import, while each write resolved its own target fresh. Replacing a protected path with a symlink pointing outside the root after import therefore made the two disagree, and the guard returned None instead of escalating (Greptile P1 on #120). Keep the path strings in `_HERMES_CORE_PATHS` and resolve each through `os.path.realpath()` inside the write/patch branch on every call. The regex and pattern tuples stay hoisted; those are stateless. Restores the pre-#120 behavior while keeping the constant construction out of the per-call path.
The symlink guard test built its fixture with `tempfile.mkdtemp` and never removed it, leaking a directory per run. It also called `os.symlink` unconditionally, which raises on Windows without admin or Developer Mode. Switch to the `tmp_path` fixture and skip on OSError, matching the pattern `conftest.py` already uses for the same platform limitation. Mutation-checked after the rewrite: the frozen-root mutant still fails the test and the real code still passes, so the rewrite did not weaken the binding.
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two related problems in
_plugin_pre_tool_call, which runs on every toolcall.
1. The approval opt-out was unreachable (#118). The guard read its opt-out
like this:
builderis not a known Hermes config root —_KNOWN_ROOT_KEYSis built fromDEFAULT_CONFIGplus_EXTRA_KNOWN_ROOT_KEYS, and neither containsbuilder.So
cfg.get("builder", {})always returned{},approval_enabledwas alwaysTrue, and the silent-block branch below it could never run.The supported mechanism already exists:
PluginContext.get_configreadsplugins.entries.<plugin_id>.settings.<key>, andpluginsis a known root(
hermes_cli/config.py). Rewiring toctxwould need the hook to close over avalue captured at
register()time.2. Per-call work that could not change. The hook rebuilt its constants on
every invocation — a
realpathover the three protected paths, both patterntuples, and a regex compile — to produce the same answer each time.
Fix
Delete the dead lookup and its unreachable branch; protected-path writes now
always escalate to
approve, which the dispatch layer turns into a realapproval prompt and which fails closed when there is no approving human.
Hoist the constants and
_is_dangerousto module level so they are computedonce at import.
removes to a module-level import alongsideos.Silently blocking protected writes was never reachable, so no user-visible
behavior changes. If that opt-out is wanted later,
ctx.get_configis thesupported path.
Measurements
Per-call cost on the real hook, median of 400:
write_fileechoread_file)Of that, the per-call
realpathwas 0.091 ms and the regex compile 0.001 ms.The absolute numbers are small, so this is a structural fix rather than a
measurable speedup — the real cost of problem 2 was the
hermes_cli.configimport sitting on the
write_file/patchpath, the same cold-import shape asthe plugin-load deadline in #116.
Verified after the change that a guard call does not put
hermes_cli.configinsys.modules(NOT_LOADED).Tests
test_guard_does_not_read_an_unknown_config_root— asserts the guard sourcecontains no
load_config/approval_enabled(comments excluded) and that aprotected write still returns
approve. Mutation-checked: reintroducing thelookup fails the test.
forms were re-verified directly against the hook after the refactor, plus the
protected-path and sibling-path cases (
config.yaml.bakandhermes-agent-notes/still allowed).Gates:
pytest tests/ -q— 263 passed, 1 skipped;verify.py— all checkspassed;
ruff check .— clean;ruff format --check .— 35 files formatted.Closes #118

Confidence Score: 5/5
The PR appears safe to merge; both numbered findings are fixed and no new blocking issue remains.Summary
The PR removes the unused config lookup and moves fixed guard patterns to module scope. Protected paths still resolve on each write.
Reviews (3) · Last reviewed commit: "test: use tmp_path and skip where symlin..." · Reviewed by Greptile