Skip to content

feat(context): adaptive depth + centrality budgeting + L3 domain-graph for cgis context #220

Open

Description

@zaebee

Follow-ups for the GraphRAG prompt compiler (#19, shipped in #219), capturing design extensions raised by @kehansama on #19. v1 ships a fixed --depth 2, INTERNAL-only neighbours, source snippet, and a lightweight <domain> block; the items below are the next rungs that are specific to the cgis context compiler.

Note: @kehansama's broader insights already have homes — #25 (Prompt-as-Memory / multi-dimensional change memory + negative examples), #16 (DuckDB analytical layer / centrality), #60 (hybrid vector-graph retrieval). This issue intentionally scopes ONLY the compiler-side work and points at those rather than duplicating them.

1. Adaptive depth (leaf vs hub)

Replace the fixed depth with a strategy keyed on the focal node's position in the graph: a bug-fix in a leaf wants depth 1 (immediate neighbours); a refactor of a core abstraction wants depth 3+. @kehansama's heuristic:

depth = max(1, min(5, round(log(out_degree + 1))))

Cheap — one out-degree query before traversal. Self-contained to context_service.

2. Centrality-ranked token budgeting (consumes #16)

For hub nodes the subgraph can still explode. Rank subgraph nodes by betweenness/degree centrality and prune low-centrality branches when the estimated token count crosses a budget — keeping structurally-important context, dropping noise. The centrality computation is exactly what #16 (DuckDB layer) provides; this issue is just the budget-pruning consumer in the compiler. Blocked on #16.

3. Full L3 domain-graph in the <domain> block

v1 surfaces only the focal node's own domain tags. Extend <domain> to the domain-to-domain dependency sub-graph ("this function is part of the auth boundary; see also the permission model") so semantic tasks ("add authentication to this API") get why it matters, not just what connects to what. Builds on the quotient/#186 domain-graph + #47 ontology work.

4. Optional --format mermaid

Reuse MermaidCompiler for a human/visual rendering of the same subgraph, behind a flag — keeping the lean XML lists the default.


Credit: insights from @kehansama's review on #19. Meta-memory / RAG-eval (TruLens, context-effectiveness scoring, negative examples) tracked on #25, not here.

Activity

  1. zaebee commented on Jun 13, 2026

    @zaebee
    OwnerAuthor

    Note for the centrality-budgeting item: betweenness centrality lands here (reassigned from #231). Rationale: betweenness is all-pairs-shortest-paths, impractical as a whole-graph SQL query — but the pruning operates on the small ego-graph the PromptCompiler (#19) already builds, so a plain-Python Brandes' over that subgraph is cheap and exact. The DuckDB layer (#16/#233) provides whole-graph PageRank for the cheaper 'rank by importance' variant if a SQL-side approximation is preferred.

  2. zaebee commented on Oct 2, 2026

    @zaebee
    OwnerAuthor

    Absorbing #60 (hybrid vector-graph retrieval) as the semantic rung after the lexical one (#206), and the context-compression measurement from #36 (CGIS context bytes vs. naive file dump).


    Generated by Claude Code

  3. zaebee commented on Oct 2, 2026

    @zaebee
    OwnerAuthor

    Prior art for "centrality budgeting", from colbymchenry/codegraph (MIT). Its codegraph_explore is the closest thing to what this issue describes (src/mcp/tools.ts):

    • Entry is free text, not an FQN. Seeds come from exact symbol names in the query, camelCase segments (damped ×0.6), stem-prefix matches and FTS5 bm25. File paths named in the query are pinned and funded first. Today cgis context needs a known FQN, so an agent has to find first.
    • Ranking is a random walk with restart over the candidate subgraph (calls, references, extends, imports…), with the restart mass spread over the seeds (computeGraphRelevance, ~L3998). A file with a text match but no graph mass is dropped. The 1–2 most central matching files get a bigger budget.
    • The budget scales with repo size (getExploreOutputBudget, L287–380): 13K chars below 150 files, up to 24K above 500 (kept under the host's ~25K inline tool-result limit). It is split across files in proportion to relevance, with at most 70% per file. Siblings off the main path are reduced to skeletons.
    • Output: line-numbered source grouped by file, the call path among the symbols the query names, a blast-radius footer (locations only), and "files not shown" pointers.

    Suggested split: keep this issue for adaptive depth plus RWR-based budgeting, add an optional free-text query entry point, and measure with the agent A/B in #543 (their "sufficiency" metric shows whether the budget went to the right files).

    Notes: research/competitor-codegraph.md (idea 1).


    Generated by Claude Code

  4. added this to the Agent UX (MCP) milestone on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    could-haveNice for AI agents and UXenhancementNew feature or request

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions