Skip to content

perf: drop the dead guard config lookup and hoist constants out of the per-call path - #120

Merged
iap merged 3 commits into
mainfrom
fix/guard-dead-config-and-hotpath
Oct 9, 2026
Merged

iap merged 3 commits into
mainfrom
fix/guard-dead-config-and-hotpath

Conversation

@iap

@iap iap commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Problem

Two related problems in _plugin_pre_tool_call, which runs on every tool
call
.

1. The approval opt-out was unreachable (#118). The guard read its opt-out
like this:

from hermes_cli.config import load_config
cfg = load_config()
approval_enabled = cfg.get("builder", {}).get("guard_approval_enabled", True)

builder is not a known Hermes config root — _KNOWN_ROOT_KEYS is built from
DEFAULT_CONFIG plus _EXTRA_KNOWN_ROOT_KEYS, and neither contains builder.
So cfg.get("builder", {}) always returned {}, approval_enabled was always
True, and the silent-block branch below it could never run.

The supported mechanism already exists: PluginContext.get_config reads
plugins.entries.<plugin_id>.settings.<key>, and plugins is a known root
(hermes_cli/config.py). Rewiring to ctx would need the hook to close over a
value captured at register() time.

2. Per-call work that could not change. The hook rebuilt its constants on
every invocation — a realpath over the three protected paths, both pattern
tuples, 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 real
approval prompt and which fails closed when there is no approving human.

Hoist the constants and _is_dangerous to module level so they are computed
once at import. re moves to a module-level import alongside os.

Silently blocking protected writes was never reachable, so no user-visible
behavior changes. If that opt-out is wanted later, ctx.get_config is the
supported path.

Measurements

Per-call cost on the real hook, median of 400:

call before this change
safe write_file 0.180 ms
terminal echo 0.131 ms
blocked terminal 0.134 ms
protected-path write 0.358 ms
non-terminal (read_file) 0.120 ms

Of that, the per-call realpath was 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.config
import sitting on the write_file/patch path, the same cold-import shape as
the plugin-load deadline in #116.

Verified after the change that a guard call does not put hermes_cli.config in
sys.modules (NOT_LOADED).

Tests

  • test_guard_does_not_read_an_unknown_config_root — asserts the guard source
    contains no load_config / approval_enabled (comments excluded) and that a
    protected write still returns approve. Mutation-checked: reintroducing the
    lookup fails the test.
  • Existing guard tests pass unchanged; 31 must-block and 16 must-allow command
    forms were re-verified directly against the hook after the refactor, plus the
    protected-path and sibling-path cases (config.yaml.bak and
    hermes-agent-notes/ still allowed).

Gates: pytest tests/ -q — 263 passed, 1 skipped; verify.py — all checks
passed; ruff check . — clean; ruff format --check . — 35 files formatted.

Closes #118

RetriggerConfidence 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.

  • The guard reuses fixed checks and keeps protected writes on the approval path.

Reviews (3) · Last reviewed commit: "test: use tmp_path and skip where symlin..." · Reviewed by Greptile

`_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
@github-actions github-actions Bot added the bug Something isn't working label Oct 9, 2026
Comment thread __init__.py Resolved
Comment thread tests/test_coverage.py Resolved
`_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.
Comment thread tests/test_coverage.py Resolved
Comment thread tests/test_coverage.py Resolved
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.
@iap
iap merged commit 6a63ceb into main Oct 9, 2026
8 checks passed
@iap
iap deleted the fix/guard-dead-config-and-hotpath branch October 9, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

guard_approval_enabled is unreadable: "builder" is not a known config root, making the opt-out and the block fallback dead code

1 participant