Skip to content

perf(mcp): slim SERVER_INSTRUCTIONS ~35%; pin marker literals + size budget with a test - #1529

Open
zyrill wants to merge 1 commit into
colbymchenry:mainfrom
zyrill:perf/slim-server-instructions
Open

perf(mcp): slim SERVER_INSTRUCTIONS ~35%; pin marker literals + size budget with a test#1529
zyrill wants to merge 1 commit into
colbymchenry:mainfrom
zyrill:perf/slim-server-instructions

Conversation

@zyrill

@zyrill zyrill commented Aug 8, 2026

Copy link
Copy Markdown

Why

The initialize instructions land in the agent's system prompt on every session of every MCP client — the header of server-instructions.ts itself says "Keep it tight. The agent reads this every session — long instructions burn tokens." Measured on v1.5.0, SERVER_INSTRUCTIONS is 5,890 chars (~1.5k tokens), and much of that is repetition: the explore-over-Read directive appears ~6 times and the return-shape description ("verbatim line-numbered source + call path + blast radius") ~4 times.

For context on how this compounds: we run codegraph in a multi-agent repo where every session, subagent, and cache rewrite re-pays the block — it was the single largest non-configurable item left after dieting everything else in our always-on context.

What

Rewrote the block to 3,814 chars (−35%), removing only repetition — every operative rule survives:

  • explore-first / while-editing guidance, flow-by-endpoints querying (incl. the mutateElement renderScene example), treat-returned-source-as-Read, overloaded-name behavior
  • all three staleness banners with their exact literal wording (agents pattern-match on these), the mis-slice/omit semantics and shifted-line-numbers caveat, and the "Already sent earlier in this conversation" dedup-pointer semantics
  • the not-indexed → stop-for-session rule, codegraph init etiquette (mention, don't run), best-effort cross-file resolution, no-correctness-validation
  • the merged "How to query" content folded into the tool section; "Anti-patterns" and a new compact "Staleness signals" section keep the rest

SERVER_INSTRUCTIONS_NO_ROOT_INDEX is untouched (already tight).

Guarding against re-inflation

New __tests__/server-instructions.test.ts (matches the existing root-__tests__/ vitest layout):

  • pins the four literal marker strings agents match on, so a future rewording can't silently break them
  • asserts no hidden tool name (anything beyond codegraph_explore) leaks into the text, per the header's own rule
  • enforces a 4,000-char size budget on SERVER_INSTRUCTIONS (1,500 for the no-root variant) — the same ratchet pattern that keeps our own agent contract from regrowing

Testing

  • npx vitest run __tests__/server-instructions.test.ts — 3/3 (the size case fails on v1.5.0's text, passes after the rewrite)
  • npm run build — clean

🤖 Generated with Claude Code

…rkers + size budget with a test

The initialize instructions are injected into the system prompt of EVERY
session of EVERY MCP client — the file's own header asks to keep them tight,
but the explore-over-Read directive was stated ~6 times and the tool's
return-shape description ~4 times. This rewrite removes only repetition:

- every operative rule survives (explore-first, flow-by-endpoints,
  treat-returned-source-as-Read, all three staleness banners with their exact
  literal wording, the already-sent-dedup pointer semantics, the
  not-indexed/stop + 'codegraph init' etiquette, best-effort resolution,
  no-correctness-validation);
- the four literal marker strings agents pattern-match on are now pinned by
  __tests__/server-instructions.test.ts, alongside a 4000-char size budget so
  the block cannot silently re-inflate, and a guard that no hidden tool name
  (anything beyond codegraph_explore) leaks into the text.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant