Structured retrieval primitives over the existing graph for agent code workflows
Structured retrieval primitives for agent workflows over graphify-out/graph.json
Graphify’s static graph is useful for broad codebase understanding, edit planning, refactor planning, and blast-radius analysis. The problem I ran into is not mainly graph quality; it is the retrieval/API surface exposed to agents.
This is not a request for a new graph format, a separate planning graph, or a hardcoded “plan mode.” The request is for structured, composable retrieval primitives over the existing graphify-out/graph.json.
Problem
Today, an agent trying to answer questions like:
- “What files are involved in changing X?”
- “What is the blast radius of Y?”
- “Where should I start for this refactor?”
- “What calls this, what does it call, and what tests cover it?”
has to do brittle work itself:
- Find seed nodes from a natural-language question
- Expand around those seeds
- Distinguish strong code facts from weak semantic/context edges
- Downrank generic
uses/mentions - Prefer stronger relations like
calls,references,contains,imports,inherits, type edges, and rationale/doc links - Preserve stable node IDs when labels collide
- Roll related nodes/edges up to source files
- Retain exact source locations/spans where available
- Emit something compact enough to inspect before reading code
If this composition stays in prompt/skill instructions, every agent has to rediscover the same pattern and may parse human-readable traversal output differently.
Precision requirements
For edit/refactor workflows, this needs to be more than denser semantic retrieval. Agents need to distinguish broad hints from precise code facts.
Useful primitives might include:
find_seeds(question) -> scored nodesdefinitions(symbol_or_node_id) -> defining nodes/files/spanscallers(node_id) -> caller nodes/files/spanscallees(node_id) -> called nodes/files/spansreferences(node_id) -> non-call references, reads/writes if availableimports_exports(file_or_node_id) -> module/file dependenciesinherits_implements(node_id) -> hierarchy edgestests_for(node_id_or_file) -> linked testsfile_neighbors(file) -> symbols and edges touching a fileexpand(seed_ids, depth, relation_filters, relation_weights) -> scored nodes/edgesrank_files(nodes, edges) -> source-file rollup
The important part is that outputs preserve:
- Node IDs
- Labels
- Source files
- Source locations/spans/snippets where available
- Relation type
- Relation confidence/provenance, e.g.
AST,LSP,LLM,heuristic - Directionality
- Scores/reasons
Possible solution
Expose composable structured retrieval primitives through both CLI and MCP, with JSON output. A higher-level convenience command could exist, but the core value is the structured API surface, not a special hardcoded mode.
A useful bundled output shape might be:
{
"question": "...",
"matched_terms": [],
"seeds": [
{
"id": "...",
"label": "...",
"score": 0.0,
"source_file": "...",
"source_location": "..."
}
],
"ranked_files": [
{
"source_file": "...",
"score": 0.0,
"reasons": [],
"top_nodes": []
}
],
"edges": [
{
"source": "...",
"target": "...",
"relation": "calls",
"context": "...",
"confidence": "...",
"provenance": "...",
"score": 0.0
}
],
"follow_up_commands": []
}
Why this matters
In a Codex-run comparison against another code-review graph tool on a real codebase, Graphify looked stronger for broad planning and codebase understanding, but the raw traversal interface was harder for agents to use reliably.
The other tool was weaker as a broad planning surface, but easier for exact validation because its outputs were more structured around symbols/files/callers/callees.
Graphify does not need to become that tool. The useful change would be to expose Graphify’s richer graph through structured, composable retrieval primitives so agents can start from reliable seeds/files/edges and then read source code.
Related existing work
I found adjacent issues/PRs, but not a direct duplicate of this request:
- #897: query seed scoring / low-relevance nodes
- #830: BFS/DFS hub traversal
- #977: query exclude / no-tests filters
- #1166: file-level node summaries
- #1089 / #809: AST call handoff and LSP graph enrichment
Those are complementary. This issue is specifically about the agent-facing structured retrieval layer over the graph.
Acceptance criteria
-
No new graph representation required
-
Works over existing
graphify-out/graph.json -
Available from CLI and MCP
-
JSON output available
-
Relation filtering/weighting configurable
-
Preserves node IDs, source files, source locations, relation direction, confidence/provenance
-
Includes exact code primitives where available:
- Definitions
- Callers
- Callees
- References
- Imports/exports
- Tests
-
Includes file-level rollups for edit/refactor/blast-radius workflows
-
Documents an agent workflow for using these primitives before editing code
Disclosure
This issue was drafted with OpenAI Codex at the user’s request after empirical tool-use friction observed during a codegraph comparison. No private repo data or benchmark artifacts are included here; this is the generalized product/API gap that came out of that work.
Additional integration requirements surfaced while using Graphify 0.9.26 behind a bounded MCP facade:
- Return authoritative, machine-readable truncation metadata for query/explain/affected/path output: whether it was truncated, why, and how many characters/items were retained. Follow-up hints should name operations that are actually part of the published API instead of embedding CLI-only advice such as increasing --budget\ or calling an unavailable command.
- Provide bounded JSON forms for \ffected, \xplain, and \path, with server-owned limits. Agent-facing facades should not need to expose arbitrary client budget knobs or scrape terminal prose.
- Emit a versioned build-diagnostics JSON record or sidecar containing stage timings, semantic-cache hit/miss/bypass counts, LLM attempt outcomes, token usage/missing-usage counts, partial-chunk state, and usage-limit state. Today an embedding service must parse human-readable logs and instrument the HTTP client to recover these signals.
- Publish supported incremental sidecar operations/contracts for community-ID overlap remapping and membership signatures. These are needed to preserve labels safely without importing private clustering functions.
A small versioned schema for these retrieval and build diagnostics would remove the remaining exact-version adapters and make Graphify substantially safer to embed in MCP services.
This request matches a bounded agent-integration use case we observed with graphifyy 0.9.103. Building on the proposal and the existing comment about machine-readable truncation metadata, a versioned structured retrieval contract for CLI and agent integrations would be useful for all four existing operations: explain, affected, path, and query.
Requirements from that integration:
- Independently enforce
maxNodes,maxEdges, andmaxOutputByteson the actual serialized UTF-8 response delivered to the consumer, including its envelope. These can be server-owned limits; they need not be unrestricted agent-controlled knobs. A token budget or a limit on the input graph alone is not equivalent. - Return complete node and edge records, never byte-slice a record. Include edge endpoints as returned nodes where applicable. Preserve IDs, stored direction, relation type, and source file/location, plus confidence or provenance when available.
- Distinguish a successful result, a safely bounded partial result, and errors in machine-readable metadata. On truncation, state what limit caused it and mark the result incomplete; provide omitted counts only when known. Fail closed for malformed data or overflow that cannot safely produce a partial result. A path prefix should not be presented as a complete source-to-target path.
- Preserve the existing commands' documented matching, traversal, ordering, direction and error semantics, or explicitly version and document any differences. In particular, structured edge records for
affectedshould retain enough information to identify the traversed edge and its endpoints rather than guessing from rendered prose. - Distinguish result truncation from the underlying graph's possible extraction incompleteness: a non-truncated response should not imply that the graph contains every source relationship.
In our 0.9.103 integration, affected_nodes() exposes structured hits but not the other endpoint of each traversed edge; the CLI navigation results we inspected were prose. explain also limits its default displayed connections to 20 and reports the remainder in text, rather than using the query truncation marker our facade recognizes. A supported structured contract would avoid reconstructing edges from prose or coupling an adapter to private, version-specific traversal internals.
This is a request for retrieval over the existing graph, not for a new graph representation or a claim that structured output would fix missing extraction edges. The observations above refer to 0.9.103, not a claim about every later release.