Repository navigation
perf(hooks): the hook cost 626 ms per tool call, a third of it pydantic - #207
Merged
Merged
Conversation
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
force-pushed
the
perf/hook-import-latency
branch
from
October 2, 2026 16:36
7dbdf04 to
6d55ef9
Compare
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
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):Importing
agentmetry.hooks.ingestwent 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.scannerandtool_policy.evaluatorboth importedcore.configto 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. Wherecore.configis already loaded (the orchestrator, the test suite), it reads the realsettingsobject, so server behaviour and every monkeypatched test are unchanged. Where it isn't (the hook), it resolves asSettingsdoes: environment first, matched case-insensitively; thenapps/orchestrator/.env, withexport, quotes and inline comments; then the same defaults; with pydantic's boolean vocabulary. A typo likeAGENTMETRY_DLP_PII=maybekeeps the default in the hook, whereSettingswould refuse to start, because the hook must never fail a tool call.CSafeLoader, falling back toSafeLoader). SameSafeConstructor, 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.pyfails if pydantic, pydantic-settings orcore.configcomes back onto the hook's import path, or if any third-party module beyondyamllands there. The allowlist names its reason. Both guards fail against the old code. The millisecond count is not pinned, as the issue asked.policy_settings.resolve()with a realSettings(_env_file=...), covering environment,.envforms, and environment-over-.envprecedence.A gap this exposed
Every enforcement test ran the hook in-process, where
core.configis loaded, so the path a real hook takes was never exercised.test_hook_real_process.pyruns the hook as Claude Code does, as a separate process against a stub server, and checks:blockdeniesrm -rf, and the event is recordeddenied;logmode records without denying;blockdenies AWS's example key;With
resolve()made to ignore the environment, two of the four fail.Also changed
test_dlp_scanner.pypatchedscanner.settings, a module attribute that no longer exists. It now monkeypatches the realsettingsobject, 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
urllibfor the POST, YAML, and the frozentraits.pycompiling its patterns), and runtime is about 85.Verification
pytest -q: 1404 passed, 1 skippedruff check agentmetry tests: cleanbenchmark: all cases behaved as recorded15846a0915769d4a, unchanged. No frozen file touched.🤖 Generated with Claude Code