Repository navigation
Add token-budgeted packs and a stdio MCP server - #7
Conversation
Agents can export ranked source with pack --budget, and Claude Code or Cursor can call the same build, query, and pack operations over stdio. Call resolution and packing no longer duplicate or mis-attach symbols. Co-authored-by: Gan shihao <yniantongtian-oss@users.noreply.github.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8940d2bf8b
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| module_level = [node_id for node_id in local if "." not in qualname(node_id)] | ||
| if len(module_level) == 1: | ||
| return module_level |
There was a problem hiding this comment.
Resolve enclosing function scopes before module-level names
When a nested function calls a sibling defined in an enclosing function, this fallback can select a different module-level function instead. For example, with both module-level helper and outer.helper, a call to helper() inside outer.run now resolves exclusively to the module-level symbol, removing the actual call edge that the previous implementation included. Walk enclosing function scopes before falling back to module-level names so closure calls retain their correct targets and graph-derived ranking.
Useful? React with 👍 / 👎.
| later = scored_nodes[index + 1 :] | ||
| if span is not None and any( | ||
| self._strictly_contains(span, inner) | ||
| for other_id, _other_score in later | ||
| if (inner := self._node_span(other_id)) is not None |
There was a problem hiding this comment.
Avoid scanning all later candidates when too little budget remains
When the remaining budget cannot accommodate any oversized block, each candidate still scans all later candidates for contained spans before reaching the minimum-budget check. With disjoint function spans this produces quadratic work, including after an ordinary pack leaves a small unusable remainder. Exercising this packing loop with 8,000 disjoint candidates and a 10-token budget performed 32,004,000 span lookups and took about 24 seconds, despite returning no context. Check the minimum truncation budget before this search and avoid repeated full-tail scans when determining whether a container has a later descendant.
Useful? React with 👍 / 👎.
What
contextgraph pack --budget Nexports ranked source that fits an approximate token budget. Each item records rank, score, path, and line span; the source itself appears once incontext.contextgraph-ai[mcp]addscontextgraph mcp, a stdio server on the official MCP Python SDK. Tools:build_graph,query_context, andpack_context..mcp.json) and Cursor (.cursor/mcp.json) config.Why
Coding agents need a small, local, ranked slice of a repository instead of a whole file pasted twice. The same ranker now backs the CLI and the MCP tools, so an editor and a pipe see one behavior. The server does not execute indexed code and does not open a port.
Bug fixes found while wiring this
self.method()stays on the enclosing class. Ambiguous names are left unresolved.scanincludes parse and load messages.packandbundlesend index progress to stderr and print unwrapped JSON, so stdout can be piped.How verified
ruff checkandruff format --checkonsrc,tests, andexamplespytest --cov=contextgraph(86% coverage, gate is 80%)python -m buildandpython -m twine check dist/*contextgraph --helpandcontextgraph pack graph --project <repo> --budget 200python examples/backend_demo.py