Skip to content

Commit a33cddc

Browse files
committed
Merge origin/main into fix/go-field-type-hint-owner-segment
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents afb499b + 747665d commit a33cddc

18 files changed

Lines changed: 1290 additions & 168 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/extract_calls.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,8 +4557,14 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
45574557
call.first_string_arg = route_path_with_group_prefix(ctx, node, call.callee_name,
45584558
call.first_string_arg);
45594559
/* A slashless registration's string action would only be
4560-
* guessed at (#1146): mint its Route without a handler. */
4561-
if (call.first_string_arg && call.first_string_arg[0] == '/' &&
4560+
* guessed at (#1146): mint its Route without a handler. A Go
4561+
* ServeMux pattern names its method or host before the slash
4562+
* ("POST /v1/x", #2537) and keeps its handler. */
4563+
if (call.first_string_arg &&
4564+
(call.first_string_arg[0] == '/' ||
4565+
(ctx->language == CBM_LANG_GO &&
4566+
cbm_service_pattern_mux_route_path(call.callee_name, call.first_string_arg) !=
4567+
NULL)) &&
45624568
!(laravel_uri && php_route_action_is_string(node))) {
45634569
call.second_arg_name = extract_handler_arg(ctx, args);
45644570
}

‎internal/cbm/service_patterns.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,65 @@ const char *cbm_service_pattern_route_method(const char *callee_name) {
11831183
return NULL;
11841184
}
11851185

1186+
static bool mux_is_blank(char ch) {
1187+
return ch == ' ' || ch == '\t';
1188+
}
1189+
1190+
const char *cbm_service_pattern_mux_split(const char *pattern, char *method, size_t method_sz) {
1191+
if (method && method_sz > 0) {
1192+
method[0] = '\0';
1193+
}
1194+
if (!pattern || pattern[0] == '/') {
1195+
return NULL;
1196+
}
1197+
/* METHOD: Go cuts it at the first space or tab and trims the rest. */
1198+
const char *rest = pattern;
1199+
size_t method_len = 0;
1200+
while (pattern[method_len] >= 'A' && pattern[method_len] <= 'Z') {
1201+
method_len++;
1202+
}
1203+
if (method_len > 0 && mux_is_blank(pattern[method_len])) {
1204+
rest = pattern + method_len;
1205+
while (mux_is_blank(*rest)) {
1206+
rest++;
1207+
}
1208+
} else {
1209+
method_len = 0;
1210+
}
1211+
/* HOST: everything before the path's slash, with no blank or line break. */
1212+
const char *slash = rest;
1213+
while (*slash && *slash != '/' && !mux_is_blank(*slash) && *slash != '\n' && *slash != '\r') {
1214+
slash++;
1215+
}
1216+
if (*slash != '/' || (method_len == 0 && slash == rest)) {
1217+
return NULL;
1218+
}
1219+
if (method_len > 0 && method) {
1220+
if (method_len >= method_sz) {
1221+
method[0] = '\0';
1222+
return NULL;
1223+
}
1224+
memcpy(method, pattern, method_len);
1225+
method[method_len] = '\0';
1226+
}
1227+
return slash;
1228+
}
1229+
1230+
const char *cbm_service_pattern_mux_route_path(const char *callee_name, const char *pattern) {
1231+
static const char *const mux_callees[] = {".Handle", ".HandleFunc"};
1232+
if (!callee_name || !pattern || strstr(pattern, "://") != NULL) {
1233+
return NULL;
1234+
}
1235+
size_t clen = strlen(callee_name);
1236+
for (size_t i = 0; i < sizeof(mux_callees) / sizeof(mux_callees[0]); i++) {
1237+
size_t slen = strlen(mux_callees[i]);
1238+
if (clen >= slen && strcmp(callee_name + clen - slen, mux_callees[i]) == 0) {
1239+
return cbm_service_pattern_mux_split(pattern, NULL, 0);
1240+
}
1241+
}
1242+
return NULL;
1243+
}
1244+
11861245
const char *cbm_service_pattern_broker(const char *resolved_qn) {
11871246
if (!resolved_qn) {
11881247
return NULL;

‎internal/cbm/service_patterns.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ const char *cbm_service_pattern_http_method(const char *callee_name);
8484
* Returns NULL if not a known route registration method. */
8585
const char *cbm_service_pattern_route_method(const char *callee_name);
8686

87+
/* Go 1.22+ net/http ServeMux pattern "[METHOD ][HOST]/[PATH]" (#2537):
88+
* "POST /v1/x", "GET api.example.com/v1/x", "api.example.com/v1/x". Returns a
89+
* pointer to PATH's leading slash inside `pattern`, or NULL when the pattern
90+
* names neither a method nor a host before a slash (a plain "/v1/x" is NULL
91+
* too: there is nothing to split). METHOD is a run of uppercase letters ended
92+
* by spaces or tabs; it is copied to `method` (NUL-terminated, "" when the
93+
* pattern names none) when `method` is non-NULL; a method that does not fit
94+
* `method_sz` then makes the pattern NULL. The grammar is Go's: callers gate
95+
* on the file language. */
96+
const char *cbm_service_pattern_mux_split(const char *pattern, char *method, size_t method_sz);
97+
98+
/* The path of a ServeMux registration's pattern (#2537): only a callee ending
99+
* ".Handle" or ".HandleFunc" registers one, and never with a URL ("://").
100+
* Another route-verb call names no pattern: `store.Get("users/123")` is no
101+
* host "users" + path "/123", `client.Get("https://...")` no host "https:".
102+
* The split of cbm_service_pattern_mux_split for such a registration, else
103+
* NULL. The grammar is Go's: callers gate on the file language. */
104+
const char *cbm_service_pattern_mux_route_path(const char *callee_name, const char *pattern);
105+
87106
/* Get the broker name for an async QN (e.g., "pubsub" from a Pub/Sub QN).
88107
* Returns NULL if not an async pattern. */
89108
const char *cbm_service_pattern_broker(const char *resolved_qn);

‎src/cli/cli.c‎

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10988,7 +10988,20 @@ void cbm_cli_set_client_selection_for_testing(const char *spec) {
1098810988
/* Detect the running binary's path at runtime. The return value distinguishes
1098910989
* OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback
1099010990
* used only as an install copy source. */
10991+
#ifdef CBM_CLI_ENABLE_TEST_API
10992+
static const char *g_cli_self_path_override_for_test = NULL;
10993+
void cbm_cli_set_self_path_override_for_test(const char *self_path) {
10994+
g_cli_self_path_override_for_test = self_path;
10995+
}
10996+
#endif
10997+
1099110998
static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
10999+
#ifdef CBM_CLI_ENABLE_TEST_API
11000+
if (g_cli_self_path_override_for_test) {
11001+
snprintf(buf, buf_sz, "%s", g_cli_self_path_override_for_test);
11002+
return true;
11003+
}
11004+
#endif
1099211005
buf[0] = '\0';
1099311006
bool exact = false;
1099411007
#ifdef _WIN32
@@ -11250,6 +11263,7 @@ char *cbm_build_install_plan_json(const char *home, const char *binary_path) {
1125011263

1125111264
typedef struct {
1125211265
const char *bin_target;
11266+
const char *config_binary;
1125311267
const char *bin_dir;
1125411268
const char *home;
1125511269
const char *shell_rc;
@@ -11274,6 +11288,8 @@ static int cli_install_activate(void *opaque) {
1127411288
!activation->shell_rc) {
1127511289
return CLI_TRUE;
1127611290
}
11291+
const char *config_binary =
11292+
activation->config_binary ? activation->config_binary : activation->bin_target;
1127711293
char previous_managed_binary[CLI_BUF_1K] = {0};
1127811294
bool previous_managed_binary_exact = cbm_detect_self_path(
1127911295
previous_managed_binary, sizeof(previous_managed_binary), activation->home);
@@ -11322,10 +11338,13 @@ static int cli_install_activate(void *opaque) {
1132211338
* including same-binary and non-force installs. */
1132311339
int agent_config_rc = CLI_OK;
1132411340
if (!activation->skip_config) {
11341+
const char *prev = previous_managed_binary_exact ? previous_managed_binary : NULL;
11342+
if (config_binary != activation->bin_target &&
11343+
(!prev || strcmp(prev, config_binary) == 0)) {
11344+
prev = activation->bin_target;
11345+
}
1132511346
agent_config_rc = cbm_install_agent_configs_with_previous(
11326-
activation->home, activation->bin_target,
11327-
previous_managed_binary_exact ? previous_managed_binary : NULL, activation->force,
11328-
activation->dry_run);
11347+
activation->home, config_binary, prev, activation->force, activation->dry_run);
1132911348
}
1133011349
if (agent_config_rc != CLI_OK) {
1133111350
cli_activation_transaction_finalize_committed_or_fail_stop(
@@ -11472,11 +11491,40 @@ int cbm_cmd_install(int argc, char **argv) {
1147211491
return CLI_TRUE;
1147311492
}
1147411493

11494+
char self_path[CLI_BUF_1K] = {0};
11495+
bool self_path_exact = cbm_detect_self_path(self_path, sizeof(self_path), home);
11496+
11497+
/* #1566: a binary owned by mise/Homebrew/nix is not ours to relocate. Infer
11498+
* it, and let either flag override the inference in either direction. */
11499+
bool externally_managed =
11500+
!force_binary && cli_binary_is_externally_managed(self_path, self_path_exact, bin_dir);
11501+
bool explicit_skip_binary = skip_binary;
11502+
if (externally_managed) {
11503+
skip_binary = true;
11504+
}
11505+
11506+
/* NOT stat(): on Windows it goes through the ANSI code page, so an
11507+
* extended-length or non-ASCII target reports "absent" and a non-force
11508+
* install silently overwrites bytes the user asked to keep. */
11509+
cbm_path_info_t target_status;
11510+
bool target_exists = cbm_path_info_utf8(bin_target, &target_status) == 0;
11511+
bool same_binary = cbm_same_file(self_path, bin_target);
11512+
11513+
/* #2722: when a binary is managed externally (mise, brew, nix, etc.),
11514+
* do not write the non-existent default target into agent configurations.
11515+
* Use the running managed binary path instead. */
11516+
const char *config_binary = bin_target;
11517+
if (externally_managed && self_path_exact && self_path[0]) {
11518+
config_binary = self_path;
11519+
} else if (skip_binary && !target_exists && self_path_exact && self_path[0]) {
11520+
config_binary = self_path;
11521+
}
11522+
1147511523
/* --plan: emit the machine-readable install receipt and exit WITHOUT
1147611524
* mutating anything (no config writes, no index deletion, no network) so
1147711525
* an agent can inspect exactly what install would touch first (#388). */
1147811526
if (plan) {
11479-
char *json = cbm_build_install_plan_json_options(home, bin_target, skip_config);
11527+
char *json = cbm_build_install_plan_json_options(home, config_binary, skip_config);
1148011528
if (!json) {
1148111529
(void)fprintf(stderr, "error: failed to build install plan\n");
1148211530
return CLI_TRUE;
@@ -11498,32 +11546,18 @@ int cbm_cmd_install(int argc, char **argv) {
1149811546

1149911547
printf("codebase-memory-mcp install %s\n\n", CBM_VERSION);
1150011548

11501-
char self_path[CLI_BUF_1K] = {0};
11502-
bool self_path_exact = cbm_detect_self_path(self_path, sizeof(self_path), home);
11503-
11504-
/* #1566: a binary owned by mise/Homebrew/nix is not ours to relocate. Infer
11505-
* it, and let either flag override the inference in either direction. */
11506-
bool externally_managed =
11507-
!force_binary && cli_binary_is_externally_managed(self_path, self_path_exact, bin_dir);
11508-
if (externally_managed || skip_binary) {
11549+
if (externally_managed || explicit_skip_binary) {
1150911550
const char *manager = cli_external_manager_name(self_path);
11510-
if (skip_binary) {
11551+
if (explicit_skip_binary) {
1151111552
printf("Skipping binary placement (--skip-binary); configuring agents only.\n\n");
1151211553
} else {
1151311554
printf("Binary is managed elsewhere%s%s:\n %s\n"
1151411555
"Leaving it and your PATH untouched; configuring agents only.\n"
1151511556
" (use --force-binary to install a copy into %s anyway)\n\n",
1151611557
manager ? " by " : "", manager ? manager : "", self_path, bin_dir);
1151711558
}
11518-
skip_binary = true;
1151911559
}
1152011560

11521-
/* NOT stat(): on Windows it goes through the ANSI code page, so an
11522-
* extended-length or non-ASCII target reports "absent" and a non-force
11523-
* install silently overwrites bytes the user asked to keep. */
11524-
cbm_path_info_t target_status;
11525-
bool target_exists = cbm_path_info_utf8(bin_target, &target_status) == 0;
11526-
bool same_binary = cbm_same_file(self_path, bin_target);
1152711561
bool do_copy = !skip_binary && !same_binary && (!target_exists || force);
1152811562

1152911563
/* (#607) Default: preserve existing indexes. `--reset-indexes` opts into
@@ -11690,6 +11724,7 @@ int cbm_cmd_install(int argc, char **argv) {
1169011724
#endif
1169111725
cli_install_activation_t activation = {
1169211726
.bin_target = bin_target,
11727+
.config_binary = config_binary,
1169311728
.bin_dir = bin_dir,
1169411729
.home = home,
1169511730
.shell_rc = shell_rc,

‎src/cli/cli.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ void cbm_cli_set_client_selection_for_testing(const char *spec);
250250
* not recognisably foreign. Detection must be positive evidence — see the
251251
* implementation comment. */
252252
const char *cbm_cli_external_manager_name_for_testing(const char *self_path);
253+
/* #2722: override runtime binary self-path detection for testing. */
254+
void cbm_cli_set_self_path_override_for_test(const char *self_path);
253255
/* #1558: expose the config-key table so a test can prove a key is discoverable
254256
* (`config list`/`set` both walk it). */
255257
size_t cbm_cli_config_key_count_for_testing(void);

0 commit comments

Comments
 (0)