Add a shell shim and agent instructions for cheaper graph queries - #4
Open
IT-Cru wants to merge 2 commits into
Open
Add a shell shim and agent instructions for cheaper graph queries#4IT-Cru wants to merge 2 commits into
IT-Cru wants to merge 2 commits into
Conversation
Calling an MCP tool puts its whole response into the agent's context, and graph
results carry per-node fields nobody reads - fingerprints, metric vectors, a dozen
complexity counters. An agent that wants three names pays for all of it, and a
multi-step question (schema, then search, then trace, then snippet) pays again at
every hop, because each intermediate result passes through context to reach the next
call.
That is the argument in Cloudflare's "code mode" post. Their hard part is the
sandbox; here there is nothing to build - the agent containers already have bash,
curl and jq, and CBM already exposes query_graph for projected multi-hop queries.
Adds .ddev/codebase-memory/cbm. The agent containers mount the project but have no
ddev binary and no MCP client, so this is the missing piece. It prints only the tool
result, so it pipes:
cbm search_graph --label Function \
| jq '[.results[] | {name, file_path, out_degree}] | sort_by(-.out_degree) | .[:5]'
Measured on a three-function project: 2123 bytes of tool result down to 182 (~91%).
The saving is in per-node fields, so it grows with the result set rather than staying
at 91%.
It maps --name-pattern to name_pattern, fills in --project, reuses one MCP session
across calls so a pipeline does not start a server process per invocation, and
re-initializes by itself if the container restarted.
Adds .ddev/codebase-memory/AGENT-INSTRUCTIONS.md: which tool answers which question,
call get_graph_schema first, prefer one query_graph over a chain, always pass limit.
Meant to be referenced from your own CLAUDE.md or opencode.json rather than installed
over the top - the add-on does not own those files.
Fixes get_graph_schema, which requires --project but was excluded from auto-injection
in both this shim and the host `ddev cbm` command, so `ddev cbm get_graph_schema` had
never worked - while both the README and the new instructions tell you to run it
first. An earlier test appeared to cover it but a jq fallback was masking the error
object, so it is now asserted through both paths.
On the project name: CBM derives it from the repository path, which inside the
container is always the mount point, so --project defaults to var-www-html whatever
the DDEV project is called (verified across five differently-named projects). If it
is ever wrong - a custom --name, or indexing a subdirectory - the server replies with
the names it does have, and CBM_PROJECT overrides the default.
Deliberately not done: stripping heavy fields inside the bridge would cut tokens too,
but it means returning something other than what the server said.
Also corrects the install message, which still claimed the graph UI needs an open
agent session.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The token cost of unfiltered tool responses is the reason for the shim, and it is measurable on its own. Where the idea came from is not something a reader of the README needs. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Calling an MCP tool puts its entire response into the agent's context. Graph
results carry per-node fields nobody reads — fingerprints, metric vectors, a dozen
complexity counters:
{"name":"atomic_write","in_degree":2,"out_degree":0,"complexity":0,"cognitive":0,… "fp":"0488e8e4039419ae027e7e6c02c39db2023349df014da83305cd2b540048e5bc…", "sp":"0,0,0,0,0,0,8,39,0,0,0,0,8,4,0,0,0,0,0,0,1,0,4,8,4"}An agent that wants three names pays for all of it. A multi-step question — schema,
then search, then trace, then snippet — pays again at every hop, because each
intermediate result has to pass through context to reach the next call.
Approach
Let the agent filter in a shell and keep only the answer. This needs no new runtime:
the agent containers already have
bash,curlandjq, and CBM already exposesquery_graphfor projected multi-hop queries. The only missing piece was a way toreach the graph from a shell.
.ddev/codebase-memory/cbm— agent containers mount the project but have noddevbinary and no MCP client. This prints only the tool result, so it pipes:Measured on a three-function project: 2123 bytes of tool result down to 182 (~91%).
The saving is in per-node fields, so it grows with the result set rather than staying
at 91%.
It maps
--name-pattern→name_pattern, fills in--project, reuses one MCPsession across calls (verified: 4 calls → 1 session, not four server processes), and
re-initializes transparently if the container restarted (verified by restarting
mid-test).
.ddev/codebase-memory/AGENT-INSTRUCTIONS.md— which tool answers which question,call
get_graph_schemafirst, prefer onequery_graphover a chain, always passlimit. Meant to be referenced from your ownCLAUDE.md(
@.ddev/codebase-memory/AGENT-INSTRUCTIONS.md) oropencode.jsoninstructionsarray, not installed over the top — the add-on doesn't own those files.
Bug fixed:
ddev cbm get_graph_schemanever workedget_graph_schemarequires--project, but it was excluded from auto-injection inboth the new shim and the host
ddev cbmcommand. So the one command the README —and now the agent instructions — tell you to run first was failing.
An earlier test appeared to cover it, but a
jqfallback (.node_labels // "n/a")was masking the error object, so it reported success on a failure. Now asserted
through both paths.
On the project name
CBM derives the graph project name from the repository path, which inside the
container is always the mount point — so
--projectdefaults tovar-www-htmlwhatever your DDEV project is called. Verified across five differently-named
projects, including
weird-name-99.If it's ever wrong — a custom
--name, or indexing a subdirectory — the serveranswers with the names it does have:
{"error":"project not found or not indexed","available_projects":["var-www-html"]}CBM_PROJECToverrides the default. Both documented.Deliberately not done
Stripping heavy fields inside the bridge. It would cut tokens too, but it means
returning something other than what the server said.
Tests
New bats case (6 total): the shim lists tools, resolves
--project, survives regexarguments, pipes through
jqwith"fp"absent from the output, fails loudly ona bad tool name, and serves repeated calls from one session. Plus
get_graph_schemaregressions on both the shim and the host command.
Also corrects the install message, which still claimed the graph UI needs an open
agent session.