Skip to content

perf: share one tools/list across manifest-reading rules - #172

Open
abu-tahir-0 wants to merge 1 commit into
patsypppe:mainfrom
abu-tahir-0:perf/manifest-cache
Open

abu-tahir-0 wants to merge 1 commit into
patsypppe:mainfrom
abu-tahir-0:perf/manifest-cache

Conversation

@abu-tahir-0

Copy link
Copy Markdown

Problem

Every HTTP rule gets its own Probe, so each rule that only reads the tool manifest fetched tools/list again. A full scan of the conformant fixture made 62 tools/list requests, most of them the same reply. Closes #151.

Solution

  • run_scan creates one ManifestCache per scan and attaches it to the probes it builds. Probe.manifest_cache is deliberately not copied by with_token() or new_connection(): those exist to compare this principal or connection against another, so they must ask again.
  • Probe.manifest() returns the shared reply; without a scan-attached cache it is exactly tools_list().
  • Rules that only read the manifest call manifest(): naming, ordering, schema, envelope tools-declare-input-schema / tools-are-named, the style and manifest-budget rules, and snapshot.fetch_tools (so the injection/scope rules and the report's manifest hash reuse it too).
  • Rules that test the request itself -- determinism, headers, capability negotiation, rate limiting -- keep calling tools_list() and make every call they want. The determinism rule still makes its twenty.
  • The cache holds the lock across the request, so rules arriving together produce one call rather than a thundering herd. Transport errors and 401/429/5xx are not cached: those statuses are never FAILs, and the next rule deserves its own attempt. Replies are keyed by wire (current vs legacy session).

Testing

  • New tests/harness/test_manifest_cache.py (4 tests): seven manifest-reading rules make exactly one tools/list; the determinism rule still makes 21 (its 20 plus the report's manifest hash); the cache reuses a usable reply but retries an unusable one; a probe with no scan cache keeps asking.
  • pytest tests/harness/test_manifest_cache.py — 4 passed.
  • pytest tests -m unit -n auto — 396 passed, 4 failures (test_population.py::test_only_current_servers_are_graded, two test_graybox.py cases, test_triage.py::test_a_baseline_never_hides_what_was_fixed). The same suites fail on main under -n auto with a different subset each run; they pass individually except the known test_population and test_fleet_service cases recorded in the repo. Nothing here touches their assertions or the fingerprints they compare.
  • ruff check . — clean; mypy — success (56 files).

Measurements

Conformant fixture, JSON-RPC methods counted in dispatch: 62 → 45 tools/list, 122 → 105 total requests. benchmarks/servers/pysdk_demo.py (mcp 2.3.0, loopback), median of five scans after warm-up, same 68 findings / 7 FAILs: 38.6 ms → 33.2 ms. Loopback has no network latency, so the saved work shows up mostly as 17 fewer roundtrips on a remote server. Recorded in benchmarks/README.md.

Impact

A remote scan no longer pays seventeen extra roundtrips just to read one manifest, while every rule that asserts on the request still sees a real reply.

Every HTTP rule built its own probe, so each rule that only reads the
tool manifest fetched it again. A scan now carries a ManifestCache and
those rules call probe.manifest(); rules that test the request itself
keep calling tools_list() and still make every call they want.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: cache the tool manifest across rules in HTTP scans

1 participant