Skip to content

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

Closed

Description

@blitzcrieg1

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

  1. Stop importing core.config on the hook path. The hook needs a few
    values and can read them from os.environ with the same alias names. This is
    the ~210 ms, and it is a contained change.
  2. Defer the DLP scanner import until there is content to scan. A
    PreToolUse with no scannable argument should not pay for the scanner at
    all.
  3. 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.
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions