Skip to content

Commit a5c5b42

Browse files
committed
Merge origin/main into fix/elixir-extraction-defects
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents c0e9293 + 747665d commit a5c5b42

30 files changed

Lines changed: 1936 additions & 209 deletions

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ codebase-memory-mcp config set auto_index true
255255

256256
`auto_index` is off by default. When enabled, new projects are indexed automatically on first connection. The configurable file limit is `config set auto_index_limit 50000`. Previously indexed projects can be watched through `auto_watch` (on by default), subject to its global setting and session ownership.
257257

258-
Each initialized, long-lived MCP session can register its root project when it connects and any target from a successful `index_repository` call, including custom project names and roots in another worktree. While `auto_watch` is enabled, these watches belong to the registering sessions and remain while at least one owning session is connected; closing the last owner removes the watch. An async index registers its target only after success and while its requesting session remains connected. CLI indexing and failed MCP indexes do not register watches. Global `auto_watch` controls registration and removes all watches on the next refresh or registration request when set to `false`. `index_status` reports this in its `watch` object: `watched`, and either the poll cadence and last scan time or the `reason` it is not watched.
258+
Each initialized, long-lived MCP session can register its root project when it connects and any target from a successful `index_repository` call, including custom project names and roots in another worktree. While `auto_watch` is enabled, these watches belong to the registering sessions and remain while at least one owning session is connected; closing the last owner removes the watch. Under a daemon started with `daemon start` a watch outlives its last owning session: the project stays watched and is re-indexed in the background when it changes, until that daemon stops, the project is deleted, its directory disappears, or `auto_watch` is set to `false`. An async index registers its target only after success and while its requesting session remains connected. CLI indexing and failed MCP indexes do not register watches. Global `auto_watch` controls registration and removes all watches on the next refresh or registration request when set to `false`. `index_status` reports this in its `watch` object: `watched`, and either the poll cadence and last scan time or the `reason` it is not watched.
259259

260260
Watcher registration is controlled globally by `auto_watch` (default `true`). On the next refresh or registration request, `true` allows MCP sessions to register their root on connect and their successful `index_repository` targets; `false` removes all watches. CLI indexing and failed MCP indexes do not add watches.
261261

@@ -904,7 +904,7 @@ Tools requiring arguments accept JSON piped on stdin. Tools with only optional a
904904

905905
| Tool | Description |
906906
|------|-------------|
907-
| `index_repository` | Index a repository into the graph. A successful call from an active MCP session registers the target for auto-sync while that session remains connected, subject to the global `auto_watch` setting; CLI calls do not register watches. Waits for the whole index by default; pass `async: true` to start it in the daemon and return at once, then poll with `status: true` (see below). |
907+
| `index_repository` | Index a repository into the graph. A successful call from an active MCP session registers the target for auto-sync while that session remains connected (under `daemon start`, also after it disconnects), subject to the global `auto_watch` setting; CLI calls do not register watches. Waits for the whole index by default; pass `async: true` to start it in the daemon and return at once, then poll with `status: true` (see below). |
908908
| `list_projects` | List indexed projects with paging; use `detail: "stats"` for node/edge counts and database sizes. |
909909
| `delete_project` | Remove a project and all its graph data. |
910910
| `index_status` | Check indexing status of a project. |

‎docs/CONFIGURATION.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Current keys:
8989
| `auto_index_limit` | `50000` | Maximum file count allowed for automatic indexing of a new project. |
9090
| `auto_watch` | `true` | Global switch for MCP session watch registration. On the next refresh or registration request, `true` allows session roots and successful `index_repository` targets to be watched; `false` removes all watches. The background watcher can still run with no registered projects. |
9191
| `doc_candidates` | `auto` | Score documentation sections against code to store doc↔code candidates (the possible targets of a section, and the sections that may describe a function or file, each with a judged probability). Unset (`auto`), the index mode decides: on for `full` indexes (the default mode), off for `moderate` and `fast`. Set `true` or `false` to choose for every mode; `config reset doc_candidates` returns to `auto`. The scoring grows with sections × functions: under a second up to ~100k functions, ~15 s at ~400k, ~3 minutes on the Linux kernel. Off stores no candidates and leaves the rest of the graph unchanged; on in `fast` mode stores candidates without adding `fast`'s skipped `SIMILAR_TO` / `SEMANTICALLY_RELATED` edges. `index_repository` also takes `doc_candidates` for a single index, which wins over this setting. Applies from the next index. |
92-
| `watcher_enabled` | `true` | Master switch for the background watcher subsystem. Set `false` to stop the watcher from starting at all — no poll thread and no project registration. Reindex manually with `index_repository` when disabled. Active MCP sessions can register their root project and successful explicit index targets; `index_status` reports the current state in its `watch` object. |
92+
| `watcher_enabled` | `true` | Master switch for the background watcher subsystem. Set `false` to stop the watcher from starting at all — no poll thread and no project registration. Reindex manually with `index_repository` when disabled. Active MCP sessions can register their root project and successful explicit index targets; under `daemon start` those watches outlive the session until the daemon stops, the project is deleted, its directory disappears, or `auto_watch` is set to `false`. `index_status` reports the current state in its `watch` object. |
9393
| `watch_non_git` | `false` | Also poll project roots that are **not git repositories**. By default the watcher only follows git projects, so a project indexed from a plain directory is never refreshed after its first index — reindex it manually with `index_repository`. Set `true` to poll such roots on the same adaptive cadence with a file-tree scan: the indexer's own discovery walk (same skip lists, `.gitignore` and `.cbmignore` rules) hashed over each file's path, size and mtime. Any change reindexes once; paths the indexer skips (including cbm's own `.codebase-memory/` output) never trigger. The first poll after the daemon starts reindexes each such project once, since nothing records which tree state the index holds. The scan walks the whole tree every poll, so it costs more than git polling on very large trees. Read once when the daemon starts, like `watcher_enabled`. |
9494
| `index_max_files` | `off` | Optional maximum number of accepted source files in one discovery run. |
9595
| `index_max_source_mb` | `off` | Optional maximum accepted source size in MiB in one discovery run. |

‎internal/cbm/cbm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3924,7 +3924,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
39243924
uint64_t lsp_start = now_ns();
39253925
if (!result->lsp_skipped) {
39263926
if (language == CBM_LANG_GO) {
3927-
cbm_run_go_lsp(a, result, source, source_len, root);
3927+
cbm_run_go_lsp(a, result, source, source_len, root, rel_path);
39283928
}
39293929
if (language == CBM_LANG_C || language == CBM_LANG_CPP || language == CBM_LANG_CUDA) {
39303930
if (!cbm_run_c_lsp_with_test_owners(a, result, source, source_len, root,

‎internal/cbm/extract_calls.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4633,8 +4633,14 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
46334633
call.first_string_arg = route_path_with_group_prefix(ctx, node, call.callee_name,
46344634
call.first_string_arg);
46354635
/* A slashless registration's string action would only be
4636-
* guessed at (#1146): mint its Route without a handler. */
4637-
if (call.first_string_arg && call.first_string_arg[0] == '/' &&
4636+
* guessed at (#1146): mint its Route without a handler. A Go
4637+
* ServeMux pattern names its method or host before the slash
4638+
* ("POST /v1/x", #2537) and keeps its handler. */
4639+
if (call.first_string_arg &&
4640+
(call.first_string_arg[0] == '/' ||
4641+
(ctx->language == CBM_LANG_GO &&
4642+
cbm_service_pattern_mux_route_path(call.callee_name, call.first_string_arg) !=
4643+
NULL)) &&
46384644
!(laravel_uri && php_route_action_is_string(node))) {
46394645
call.second_arg_name = extract_handler_arg(ctx, args);
46404646
}

‎internal/cbm/extract_defs.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7061,6 +7061,24 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
70617061
}
70627062
}
70637063

7064+
/* Go allows any number of init() functions per package — even several in
7065+
* one file — and they all run at start-up. On the flat QN they all
7066+
* collided and the graph upsert kept ONE node per package, silently
7067+
* dropping the rest (#1910). Disambiguate with the #495 cfg-twin pattern:
7068+
* fold the file basename and the init's per-file ordinal into the QN.
7069+
* The ordinal (not the line) keeps the QN stable when code is edited
7070+
* above the function — the QN is node identity, and a line suffix would
7071+
* churn nodes on every unrelated insertion. Nothing ever joins on the
7072+
* plain QN — calling init explicitly is illegal in Go — and the
7073+
* call-scope side (compute_func_qn in extract_unified.c) and the Go LSP
7074+
* (process_function in lsp/go_lsp.c) name it through the same
7075+
* cbm_go_init_qn, so init-body calls keep their attribution. */
7076+
if (ctx->language == CBM_LANG_GO && strcmp(def.label, "Function") == 0 &&
7077+
strcmp(name, "init") == 0) {
7078+
def.qualified_name =
7079+
cbm_go_init_qn(a, def.qualified_name, ctx->rel_path, node, ctx->source);
7080+
}
7081+
70647082
// C++/CUDA: out-of-line method definition (`Foo::bar` in a .cc/.cpp). The
70657083
// class body in the header is declaration-only, so without this the
70667084
// definition is recorded as a free Function. Promote it to a Method whose QN

‎internal/cbm/extract_unified.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,23 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
11381138
}
11391139
}
11401140

1141+
/* Go init(): the def extractor folds the file basename and the init's
1142+
* per-file ordinal into the QN so every init in a package survives the
1143+
* upsert (#1910, the #495 cfg-twin pattern; ordinal, not line, so the QN
1144+
* is stable under edits above the function). Name it through the same
1145+
* cbm_go_init_qn, or init-body calls fall back to File-node attribution
1146+
* (calls_find_source). */
1147+
if (ctx->language == CBM_LANG_GO && strcmp(name, "init") == 0 &&
1148+
strcmp(ts_node_type(node), "function_declaration") == 0) {
1149+
const char *base_qn = cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path,
1150+
name, ctx->language);
1151+
/* NULL only on allocation failure: fall through to the flat QN, as
1152+
* the receiver branch above does. */
1153+
if (base_qn) {
1154+
return cbm_go_init_qn(ctx->arena, base_qn, ctx->rel_path, node, ctx->source);
1155+
}
1156+
}
1157+
11411158
/* Nix: a binding's own attrpath contributes scope (`a.b.fn = …`), and the def
11421159
* extractor bakes it into the def QN. Compose it identically here — otherwise
11431160
* an in-body call sources to a QN one or more segments short of the def, and

‎internal/cbm/helpers.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,65 @@ int cbm_classify_string(const char *str, int len) {
24172417
return NOT_FOUND;
24182418
}
24192419

2420+
/* 1-based source-order ordinal of a top-level Go `init` function_declaration
2421+
* among the file's `init`s. Unlike a line number, the ordinal is stable under
2422+
* edits above the function — QNs are node identity, and a line-based suffix
2423+
* would churn nodes on every unrelated insertion (the #495 cfg suffix and the
2424+
* GoogleTest derivation are both edit-stable for the same reason).
2425+
* One cursor pass over the top-level declarations: ts_node_named_child(root, i)
2426+
* is O(i), so an indexed loop is O(n^2) per init on a wide file, and generated
2427+
* Go (.pb.go, tables) carries thousands of top-level declarations next to its
2428+
* init(). */
2429+
static int go_init_ordinal(TSNode fn_node, const char *source) {
2430+
TSNode root = fn_node;
2431+
for (TSNode p = ts_node_parent(root); !ts_node_is_null(p); p = ts_node_parent(p)) {
2432+
root = p;
2433+
}
2434+
int ordinal = 0;
2435+
TSTreeCursor cur = ts_tree_cursor_new(root);
2436+
if (ts_tree_cursor_goto_first_child(&cur)) {
2437+
do {
2438+
TSNode child = ts_tree_cursor_current_node(&cur);
2439+
if (strcmp(ts_node_type(child), "function_declaration") != 0) {
2440+
continue;
2441+
}
2442+
TSNode name = ts_node_child_by_field_name(child, TS_FIELD("name"));
2443+
if (ts_node_is_null(name)) {
2444+
continue;
2445+
}
2446+
uint32_t start = ts_node_start_byte(name);
2447+
if (ts_node_end_byte(name) - start != 4 || memcmp(source + start, "init", 4) != 0) {
2448+
continue;
2449+
}
2450+
ordinal++;
2451+
if (ts_node_eq(child, fn_node)) {
2452+
break;
2453+
}
2454+
} while (ts_tree_cursor_goto_next_sibling(&cur));
2455+
}
2456+
ts_tree_cursor_delete(&cur);
2457+
return ordinal;
2458+
}
2459+
2460+
/* Qualified name of a Go `init` function (#1910). Go allows any number of
2461+
* init() per package — even several in one file — so the flat `pkg.init`
2462+
* collides and the graph upsert keeps one node. The QN folds in the file
2463+
* basename and the init's per-file ordinal: `pkg.init#file.go:N`. Every side
2464+
* that names an init calls this one function — the definition extractor, the
2465+
* call-scope side (compute_func_qn) and the Go LSP's enclosing-function QN —
2466+
* so the three cannot drift. A NULL base QN or path keeps base_qn as is. */
2467+
const char *cbm_go_init_qn(CBMArena *a, const char *base_qn, const char *rel_path, TSNode fn_node,
2468+
const char *source) {
2469+
if (!base_qn || !rel_path) {
2470+
return base_qn;
2471+
}
2472+
const char *base = strrchr(rel_path, '/');
2473+
base = base ? base + 1 : rel_path;
2474+
const char *qn =
2475+
cbm_arena_sprintf(a, "%s#%s:%d", base_qn, base, go_init_ordinal(fn_node, source));
2476+
return qn ? qn : base_qn;
2477+
}
2478+
24202479
/* Flatten a JS/TS `template_string` node into plain text (issue #1006).
24212480
* String fragments are kept verbatim; each ${...} substitution becomes the
24222481
* "{}" placeholder so client URLs built from template literals share the

‎internal/cbm/helpers.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ bool cbm_is_module_level(TSNode node, CBMLanguage lang);
307307
// node being classified.
308308
bool cbm_is_module_level_p(TSNode parent, CBMLanguage lang);
309309

310+
/* QN of a Go `init` function_declaration (#1910): base_qn + "#<file basename>:<N>",
311+
* N = its 1-based ordinal among the file's top-level `init`s. The ONE formula
312+
* for the def extractor, compute_func_qn and the Go LSP's enclosing-function
313+
* QN. A NULL base_qn or rel_path returns base_qn unchanged. */
314+
const char *cbm_go_init_qn(CBMArena *a, const char *base_qn, const char *rel_path, TSNode fn_node,
315+
const char *source);
316+
310317
// --- FQN computation ---
311318

312319
// Compute qualified name: project.rel_path_parts.name

0 commit comments

Comments
 (0)