Repository navigation
Description
The hook runs in front of the IDE. On a blocking hook the editor waits for it
before the tool executes, so this is user-visible latency on every tool call, and
hooks/ingest.py:259 already calls it "a latency budget rather than a".
Measured on this machine, five cold invocations of
scripts/agentmetry_ingest.py --hook PreToolUse with the orchestrator running:
752 ms 702 ms 780 ms 772 ms 730 ms
An agent making twenty tool calls in a row pays roughly fifteen seconds.
Where it goes
python -X importtime, ranked by cumulative cost:
422 ms agentmetry.hooks.ingest
320 ms agentmetry.core.audit.dlp
297 ms agentmetry.core.audit.dlp.scanner
253 ms agentmetry.core.config
125 ms pydantic_settings
43 ms pydantic.types
42 ms pydantic
Bare interpreter startup is ~160 ms of the total, measured separately.
So the single largest cost is pydantic and pydantic-settings, roughly 210 ms,
imported because dlp.scanner imports core.config to read a handful of
environment variables. The hook pays for a full settings model, validation
machinery and its dependency tree, to learn a DLP mode and a base URL.
Why this is worth writing down
A proposal that reached me identified the latency as a problem, estimated it at
about 100 ms, attributed it to interpreter startup, and concluded the fix is to
rewrite the hook client in Rust or Go.
The measurement disagrees on all three counts. It is 7x larger, the interpreter
is under a quarter of it, and the dominant term is an import that a rewrite is
not needed to remove. A rewrite would also buy a second language, a build matrix
across three platforms, a signing story and a release pipeline, for a project
that currently has one maintainer.
What to do, in order
- Stop importing
core.configon the hook path. The hook needs a few
values and can read them fromos.environwith the same alias names. This is
the ~210 ms, and it is a contained change. - Defer the DLP scanner import until there is content to scan. A
PreToolUsewith no scannable argument should not pay for the scanner at
all. - Re-measure, and put the number in a test. A latency budget that is not
asserted anywhere drifts back, which is the pattern this repo keeps
rediscovering in its own published claims. A test that fails when a new
top-level import lands on the hook path is the durable fix; the millisecond
count itself is machine-dependent and should not be pinned. - Only then consider a compiled client, with a measured number to justify
it rather than an assumed one.
Scope
hooks/ingest.py, core/audit/dlp/scanner.py, scripts/agentmetry_ingest.py.
No behaviour change intended: same events, same fields, same enforcement. The
test suite and the benchmark are the regression net, and neither the ruleset
files nor the fingerprint are touched.
Activity
perf(hooks): the hook cost 626 ms per tool call, a third of it pydantic
feat(policy): a rule can ask, and only where the ask reaches a human
fix(identity): the trail can say who ran the agent, and configuring i…
release: 0.9.1
feat(canary): local honeytoken decoys (rebased #491, refs #293) (#496)