Skip to content

perf(mcp): canonicalize the tool catalog once per digest profile - #11298

Merged
morluto merged 5 commits into
mainfrom
perf/mcp-doctor-test
Nov 15, 2026
Merged

morluto merged 5 commits into
mainfrom
perf/mcp-doctor-test

Conversation

@morluto

@morluto morluto commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Summary

tests/boundary/mcp/mcpDoctor.test.ts is CPU-bound (54 s user CPU for a 64 s run). A CPU profile of the test worker attributes 4.6 s of its 11 s busy time to createCatalogDigests, and canonicalize alone takes 2.25 s. digestCatalogs in src/contracts/catalog/catalogIdentity.ts canonicalized the full tool catalog twice per profile: once for tools_sha256, and again inside the {cli, tools, prompts} document for combined_sha256. The combined document is now built from the canonical tool and prompt texts.

Problem and expected behavior

Every scoped or custom-budget catalog identity (doctor runs, family-scoped servers, tests) computes digests on first use; only the default identity has recorded digests. Expected: the same digests, with the catalog canonicalized once.

Change and scope

RFC 8785 (and the canonicalize package's implementation, node_modules/canonicalize/lib/canonicalize.js) serializes an object as { + members sorted by key, each "key":<canonical value>, joined by , + }. The combined document's keys cli < prompts < tools are fixed and its values are never undefined, so {"cli":C(cli),"prompts":C(prompts),"tools":C(tools)} is byte-identical to C({cli, tools, prompts}). digestCatalogs canonicalizes tools and prompts once and hashes the composed text.

Not included: the other measured worker hotspot, the MCP client's per-call ajv output-validator compilation (2.9 s), which #10960 addresses. The doctor's child MCP servers are killed before writing CPU profiles, so their share was not attributed.

Scope: not applicable.

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/contracts/catalog/catalogIdentity.ts digestCatalogs.
  • Contribution scope fit and linked maintainer decision: not applicable.
  • CLI and MCP/tool-catalog contract: digest values unchanged (verified below).
  • Provider, bridge, target-format, or platform compatibility: none.
  • Evidence, artifact, provenance, or reconstruction contract: none.
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): none.
  • Generated metadata, package, or installation impact: recorded catalog digests unchanged.

Expected effects (check known effects; use documentation-only on its own).
Unchecked items do not establish absence; leave uncertainties visible.

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated: tests/boundary/mcp/serverIdentity.test.ts adds "composes the combined digest exactly as whole-document canonicalization", which checks digestPresentedCatalogs (the exported digest step after presentation; digestCatalogs presents the catalog and delegates to it) against sha256(canonicalize(...)) for escaped and non-ASCII strings, unsorted keys, and empty catalogs under both profiles.
  • Base reproduction or other evidence: with the built module, the composed form and a whole-document canonicalize variant give identical combined_sha256 for both profiles (off 0613528d…85d1, compact d8c9204b…5eb1). Warm createCatalogDigests for both profiles: ~240 ms → ~168 ms. The old-formula stand-in also re-parsed the texts, so that figure slightly overstates the saving.
  • User-visible CLI/MCP output (if applicable): none.
  • Remaining proof gaps: single runs under shared machine load.
npm run test:focused -- tests/boundary/mcp/mcpDoctor.test.ts before after
elapsed (wall) 64.18 s 49.43 s
CPU (user + sys) 67.48 s 53.65 s
peak RSS (maximum resident set size) 1.17 GB 0.92 GB

The file-level difference is larger than the digest saving alone explains, so part of it is load variance; the per-call A/B above is the controlled measurement.

Validation performed

  • npm run test:focused -- tests/boundary/mcp/mcpDoctor.test.ts tests/boundary/mcp/serverIdentity.test.ts — 44 passed; after adding the composition test, serverIdentity.test.ts — 12 passed.
  • npm run test:focused -- tests/boundary/mcp/mcpDoctor.test.ts — before 33 passed in 64.2 s; after 33 passed in 49.4 s.
  • digest A/B script over dist/catalogIdentity.js — identical digests (above).
  • pre-push hook (check:fast, architecture guards) — passed.
  • After rebasing onto main (refactor(verify): keep each counted guard's baseline in its own file #11316, refactor(mcp): derive every advertised tool through one owner #11268): npm run check:fast — passed; npm run test:focused -- tests/boundary/mcp/serverIdentity.test.ts — 12 passed; briefs/local-gate.sh (merge with main, oxfmt, check:fast, guards, affected tests, advertised schemas) — passed.
  • Merged current main (merge 219e031; refactor(contracts): own CLI command names, catalog identity and schema presentation in contracts #11425 moved src/catalogIdentity.ts to src/contracts/catalog/catalogIdentity.ts, and this change moved with it), commit written without the pre-commit lint-staged pass so it carries no unrelated autofixes; pre-push hook ran; the only conflicts were serverIdentity.test.ts imports (kept main's paths and this PR's createHash, canonicalize and digestPresentedCatalogs imports). After the merge: npm run check:fast pass; npm run test:focused -- tests/boundary/mcp/serverIdentity.test.ts 12 passed; briefs/local-gate.sh 11298 perf/mcp-doctor-test pass.

Compatibility, safety, and release

  • Breaking changes or migration steps: none; digests are byte-identical.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: none.

Author readiness

Human owner:

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-15T15:45:04.471649Z
We couldn’t complete this review. Comment @codex review to try again.
254ec18 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

[Low impact] The PR appears safe to merge; no actionable issues remain from this review.

Summary

The PR reuses canonical tool and prompt text when building the combined catalog digest.

  • Catalog digests reuse canonical text for combined identity hashes.

Reviews (6) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

@morluto

morluto commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

@greptileai please review the latest commit (cd8b6c9). It rebases onto main after #11316 and #11268; resolving the conflict with #11268's presentation step, digestCatalogs now presents the catalog and delegates to an exported digestPresentedCatalogs, which the whole-document canonicalization test calls.

@morluto
morluto force-pushed the perf/mcp-doctor-test branch from dc517b6 to c323042 Compare November 15, 2026 21:22
@morluto
morluto merged commit e77793e into main Nov 15, 2026
13 of 15 checks passed
@morluto
morluto deleted the perf/mcp-doctor-test branch November 15, 2026 22:30
@morluto morluto added area: mcp MCP server, transport, and tool registration performance Performance regressions, scaling limits, and bounded-resource work labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: mcp MCP server, transport, and tool registration performance Performance regressions, scaling limits, and bounded-resource work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant