Skip to content

perf(hooks): the hook cost 626 ms per tool call, a third of it pydantic - #207

Merged
blitzcrieg1 merged 1 commit into
masterfrom
perf/hook-import-latency
Oct 2, 2026
Merged

blitzcrieg1 merged 1 commit into
masterfrom
perf/hook-import-latency

Conversation

@blitzcrieg1

Copy link
Copy Markdown
Owner

Closes #171.

The number

Cold end-to-end hook runs (scripts/agentmetry_ingest.py --hook PreToolUse, a fresh interpreter each time, Claude Code payload on stdin, against a stub ingest server so the live orchestrator is never touched):

median min
before 626 ms 616 ms
after 376 ms 357 ms

Importing agentmetry.hooks.ingest went from about 420 ms to about 206 ms. A bare interpreter is about 74 ms on this machine.

(A first measurement against a closed port read 1146 ms. That was Windows retrying a refused localhost connection for about a second, not the hook, so the stub server is what the numbers above use.)

What it was

As the issue said, dlp.scanner and tool_policy.evaluator both imported core.config to read five settings (dlp_mode, dlp_rules_path, dlp_pii, tool_policy_mode, tool_policy_path), and that import is pydantic plus pydantic-settings: about 220 ms on every tool call.

The fix

  • core/audit/policy_settings.py, stdlib only. Where core.config is already loaded (the orchestrator, the test suite), it reads the real settings object, so server behaviour and every monkeypatched test are unchanged. Where it isn't (the hook), it resolves as Settings does: environment first, matched case-insensitively; then apps/orchestrator/.env, with export, quotes and inline comments; then the same defaults; with pydantic's boolean vocabulary. A typo like AGENTMETRY_DLP_PII=maybe keeps the default in the hook, where Settings would refuse to start, because the hook must never fail a tool call.
  • libyaml for the manifests (CSafeLoader, falling back to SafeLoader). Same SafeConstructor, so the same objects; a test checks both shipped manifests and that unsafe tags are still rejected. About 16 ms more, over 15 runs each.

What keeps it fixed

  • test_hook_import_cost.py fails if pydantic, pydantic-settings or core.config comes back onto the hook's import path, or if any third-party module beyond yaml lands there. The allowlist names its reason. Both guards fail against the old code. The millisecond count is not pinned, as the issue asked.
  • Parity: 18 cases compare policy_settings.resolve() with a real Settings(_env_file=...), covering environment, .env forms, and environment-over-.env precedence.

A gap this exposed

Every enforcement test ran the hook in-process, where core.config is loaded, so the path a real hook takes was never exercised. test_hook_real_process.py runs the hook as Claude Code does, as a separate process against a stub server, and checks:

  • tool policy block denies rm -rf, and the event is recorded denied;
  • log mode records without denying;
  • DLP block denies AWS's example key;
  • an ordinary command passes in block mode.

With resolve() made to ignore the environment, two of the four fail.

Also changed

test_dlp_scanner.py patched scanner.settings, a module attribute that no longer exists. It now monkeypatches the real settings object, which the scanner reads.

Not done, on purpose

The issue's step 4 (a compiled client) isn't justified by these numbers. Of the 376 ms left: interpreter startup is 74, the hook's import is about 206 (mostly urllib for the POST, YAML, and the frozen traits.py compiling its patterns), and runtime is about 85.

Verification

  • pytest -q: 1404 passed, 1 skipped
  • ruff check agentmetry tests: clean
  • benchmark: all cases behaved as recorded
  • ruleset fingerprint 15846a0915769d4a, unchanged. No frozen file touched.

🤖 Generated with Claude Code

The hook runs in front of every tool call, and on a blocking hook the agent
waits for it (#171). The DLP scanner and tool-policy evaluator imported
core.config to read five settings, which pulled in pydantic and
pydantic-settings: about 220 ms on every call to learn two modes, two
manifest paths and a boolean.

They now read those through core/audit/policy_settings.py, stdlib only.
Where core.config is already loaded (the orchestrator, the test suite) it
reads the real settings object, so nothing there changes. In the hook it
resolves them as Settings does: environment case-insensitively, then the
orchestrator's .env, then the same defaults. A parity test checks the two
agree across eighteen environment and .env forms. The manifests are also
parsed with libyaml where installed: same objects, about 16 ms more.

Seven cold runs each against a stub ingest server: median 626 ms before,
376 ms after. Importing the hook went from ~420 ms to ~206 ms. The figure is
machine-dependent and not pinned; the cause is. test_hook_import_cost.py
fails if pydantic or core.config returns to the hook's import path, or if
any third-party module beyond YAML lands on it. Both guards fail against
the old code.

Every enforcement test ran the hook in-process, where core.config is
loaded, so the path a real hook takes was untested. test_hook_real_process.py
runs it as a separate process, as Claude Code does, and checks block mode
still denies rm -rf and an AWS key. With the resolver made to ignore the
environment, two of its four tests fail.

Fingerprint unchanged (15846a0915769d4a).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@blitzcrieg1
blitzcrieg1 force-pushed the perf/hook-import-latency branch from 7dbdf04 to 6d55ef9 Compare October 2, 2026 16:36
@blitzcrieg1
blitzcrieg1 merged commit 13d943d into master Oct 2, 2026
10 checks passed
@blitzcrieg1
blitzcrieg1 deleted the perf/hook-import-latency branch October 2, 2026 16:38
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hooks: the hook costs 730ms, and 210ms of it is importing pydantic to read env vars

1 participant