Opt-in custom tree-sitter queries whose matches are stored on the node (`CBM_EXTRA_QUERIES`)

Open
#3,182 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
c
Domain
backend, devtools

Research direction

Start with the prototype locations named in the issue: internal/cbm/extract_queries.c, pass_definitions.c, pass_parallel.c, and result_compact.c. Read the existing extraction and definition storage paths, then review the five extraction tests and the sequential and parallel pipeline test. Done means the proposed query matches are stored under custom on the containing node, including through both indexing routes, while invalid queries are skipped and indexing continues.

Written by the indexing model from the issue text.

Description

parsing/quality

Proposal

#3144 lists facts the extractor parses but does not store (modifiers, call counts, local variables, ...). Each one needs its own schema decision. This is a generic, opt-in alternative: the user writes tree-sitter queries, and CBM stores what they match on the node that contains the match. Nothing else in the graph changes.

How it works

  1. Set CBM_EXTRA_QUERIES to a folder with one query file per language, named after the language: java.scm, csharp.scm, python.scm, ...
  2. While indexing, CBM runs that query on the syntax tree it has already parsed (no second parse).
  3. Every match is stored on the innermost function, method, field or class that contains it, in one new node property: custom.

If the variable is not set, nothing changes.

Example

Source file Box.java:

class Box {
    private static final int MAX = 3;
    public int run(Box b) {
        int n = b.size();
        n += b.size();
        return n;
    }
    int size() { return MAX; }
}

Query file java.scm:

(modifiers ["public" "private" "static" "final"] @modifier)

(local_variable_declaration
  type: (_) @local.type
  declarator: (variable_declarator name: (identifier) @local.name))

(method_invocation object: (_) @call.receiver name: (identifier) @call.name)

Result, properties.custom of the stored nodes:

// Method run
{"call":  [{"receiver": "b", "name": "size", "line": 4},
           {"receiver": "b", "name": "size", "line": 5}],
 "local": [{"type": "int", "name": "n", "line": 4}],
 "modifier": ["public"]}

// Field MAX
{"modifier": ["private", "static", "final"]}

// Method size, class Box: no `custom` property (nothing matched inside them)

The capture name decides the shape:

  • @modifier (no dot): every captured node becomes one string in the list modifier.
  • @local.type + @local.name (same prefix, with a dot): each match becomes one object in the list local, with one key per capture and the line where it starts.
  • @_name (leading underscore): helper capture, not stored.

Behaviour and limits

  • A query that does not compile is logged once (extract.queries.invalid, with the error offset) and skipped. Indexing continues.
  • Text predicates (#eq?, #match?) are not evaluated; every structural match is stored.
  • Bounds: 512 bytes per captured text, 100,000 values per file, 1 MiB per query file.
  • Cost measured on three open-source Java/C# repositories: index time unchanged, database 2-3% larger. With the variable unset, the cost is one atomic load per file.

Implementation (prototype ready)

  • New internal/cbm/extract_queries.c: compiles each language's query once, runs it after extraction, attaches matches to the innermost definition (new tail field CBMDefinition.custom_props).
  • build_def_props in pass_definitions.c and its twin in pass_parallel.c write it as custom and count it in the buffer sizing; result_compact.c copies it, so spilled results keep it.
  • Tests: 5 extraction tests and 1 pipeline test that indexes through both the sequential and the parallel route and checks the stored property.

Questions

  1. Is a generic hook like this acceptable, alongside or instead of the per-fact items in #3144?
  2. Environment variable, or would you prefer a config file entry or an index_repository argument?
  3. Are the property name custom and the file naming by language name fine?

If the shape is OK, I will open one PR for it (Closes #<this issue>).

Dominant language
C
Stars
45.8k
Forks
3.8k
Avg merge
2d 18h
Merged PRs (30d)
108

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DeusData/codebase-memory-mcp

All issues in DeusData/codebase-memory-mcp

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.