Skip to content

Commit ca1f750

Browse files
committed
Merge origin/main into fix/swift-init-extraction
Signed-off-by: Martin Vogel <[email protected]>
2 parents 97628c0 + a33878c commit ca1f750

50 files changed

Lines changed: 2720 additions & 160 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.claude-plugin/marketplace.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "codebase-memory",
3+
"description": "The codebase-memory-mcp code knowledge graph server, as a Claude Code plugin.",
4+
"owner": {
5+
"name": "DeusData",
6+
"url": "https://github.com/DeusData"
7+
},
8+
"plugins": [
9+
{
10+
"name": "codebase-memory",
11+
"source": "./plugin",
12+
"description": "Codebase knowledge graph for AI agents: sub-ms queries, 99% fewer tokens."
13+
}
14+
]
15+
}

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Thumbs.db
3333

3434
# MCP config (user-local, generated by install command)
3535
.mcp.json
36+
# ...except the Claude Code plugin's server definition, which is published
37+
!plugin/.mcp.json
3638

3739
# MCP Registry auth tokens
3840
.mcpregistry_*

‎README.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,30 @@ Working in a clone? Use `.` in place of the flake URL, e.g. `nix run .#codebase-
578578
You: "Install this MCP server: https://github.com/DeusData/codebase-memory-mcp"
579579
```
580580

581+
### Install via Claude Code plugin
582+
583+
This repository is also a Claude Code plugin marketplace, so you can find and
584+
add the server from inside Claude Code:
585+
586+
```bash
587+
claude plugin marketplace add DeusData/codebase-memory-mcp
588+
claude plugin install codebase-memory@codebase-memory
589+
```
590+
591+
The plugin registers the MCP server only. It starts the server with
592+
`npx -y codebase-memory-mcp@<version>`, pinned to the current release; the first
593+
start downloads that release's binary through the npm package, which verifies
594+
it against the release checksums. The skill, the three graph agents and the
595+
context hooks are not part of the plugin: they come from
596+
`codebase-memory-mcp install`.
597+
598+
Use one or the other. `install` registers the same server for Claude Code, so
599+
with both you run it twice, under two tool namespaces
600+
(`mcp__codebase-memory-mcp__*` and
601+
`mcp__plugin_codebase-memory_codebase-memory-mcp__*`). Choose `install` for the
602+
full Claude Code setup; choose the plugin to add just the server without
603+
running an installer.
604+
581605
### Build from Source
582606

583607
<details>

‎docs/CONFIGURATION.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ root because they are too broad or too sensitive to index as a unit:
245245
- a credential directory at any depth — `.ssh`, `.aws`, `.gnupg`, `.kube`,
246246
`.docker`, `.netrc`, `.git-credentials`, `.password-store`, macOS `Keychains`.
247247

248+
A single top-level directory can still be indexed as one root on purpose, for
249+
example a container or build machine that keeps its project in `/work`, `/app` or
250+
`/src`, by approving that exact path in a terminal:
251+
`codebase-memory-mcp allow-root --approve-broad /work`. The approval is recorded
252+
like any other allowed root, listed as `(approved broad)` by `allow-root --list`,
253+
and covers no other top-level directory; as with any recorded root, indexing is
254+
then confined to the recorded roots. Filesystem, drive and share roots cannot be
255+
approved.
256+
248257
Two limits are worth stating plainly. This constrains *scope*, not
249258
*sensitivity*: inside a root that is allowed, every file the process can read may
250259
be indexed and later returned. And the credential list is a denylist, so it

‎internal/cbm/cbm.h‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,10 @@ typedef struct {
298298
* are bound to the owning result's source identity before cross-file use. */
299299
uint32_t test_name_start_byte, test_name_end_byte;
300300
uint32_t test_body_start_byte, test_body_end_byte;
301-
/* Documents only: facts a document definition adds to its node's
302-
* properties, as a NULL-terminated key, value, key, value, ... list (an
303-
* ADR's id, status, date, deciders, title). Keys are fixed literals; the
301+
/* Facts a definition adds to its node's properties, as a NULL-terminated
302+
* key, value, key, value, ... list: a document's (an ADR's id, status,
303+
* date, deciders, title), or CBM_PROP_GENERATED on a member the compiler
304+
* writes and the source never spells. Keys are fixed literals; the
304305
* values are escaped where the properties are written. Tail field:
305306
* zero-init stays valid. */
306307
const char **extra_props;
@@ -321,6 +322,13 @@ typedef struct {
321322
const char *alias_target;
322323
} CBMDefinition;
323324

325+
/* extra_props key of a generated member, one the compiler writes and the
326+
* source never spells: a Java record's implicit accessor carries
327+
* `generated: record`. The node keeps the property, and the pipeline registers
328+
* such a definition by qualified name only, so no short-name guess can land on
329+
* it (pipeline.h cbm_def_props_generated). */
330+
#define CBM_PROP_GENERATED "generated"
331+
324332
/* Argument captured from a call expression */
325333
typedef struct {
326334
const char *expr; // raw expression text ("payload.info", "MY_URL", "'hello'")

‎internal/cbm/extract_defs.c‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
230230
const CBMLangSpec *spec);
231231
static TSNode find_class_body(TSNode class_node, CBMLanguage lang);
232232
static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *class_qn);
233+
static void extract_record_accessors(CBMExtractCtx *ctx, TSNode node, const char *class_qn);
233234
static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
234235

235236
// --- Helpers ---
@@ -8326,6 +8327,10 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
83268327
extract_enum_members(ctx, node, class_qn);
83278328
}
83288329

8330+
if (ctx->language == CBM_LANG_JAVA && strcmp(kind, "record_declaration") == 0) {
8331+
extract_record_accessors(ctx, node, class_qn);
8332+
}
8333+
83298334
// Extract methods inside the class
83308335
int methods_from = ctx->result->defs.count;
83318336
extract_class_methods(ctx, node, class_qn, spec);
@@ -9657,6 +9662,121 @@ static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *cl
96579662
}
96589663
}
96599664

9665+
/* Whether the record body declares this accessor itself (JLS §8.10.3 lets a
9666+
* record override any of them), so the implicit one is not emitted twice. */
9667+
static bool record_declares_accessor(CBMExtractCtx *ctx, TSNode node, const char *comp_name) {
9668+
TSNode body = find_class_body(node, ctx->language);
9669+
if (ts_node_is_null(body)) {
9670+
return false;
9671+
}
9672+
uint32_t mc = ts_node_named_child_count(body);
9673+
for (uint32_t mi = 0; mi < mc; mi++) {
9674+
TSNode member = ts_node_named_child(body, mi);
9675+
if (strcmp(ts_node_type(member), "method_declaration") != 0) {
9676+
continue;
9677+
}
9678+
TSNode mname = ts_node_child_by_field_name(member, TS_FIELD("name"));
9679+
if (ts_node_is_null(mname)) {
9680+
continue;
9681+
}
9682+
char *name = cbm_node_text(ctx->arena, mname, ctx->source);
9683+
if (!name || strcmp(name, comp_name) != 0) {
9684+
continue;
9685+
}
9686+
/* Same name, no parameters — that is the accessor. */
9687+
TSNode params = ts_node_child_by_field_name(member, TS_FIELD("parameters"));
9688+
if (ts_node_is_null(params)) {
9689+
return true;
9690+
}
9691+
/* Comments are named extras, not parameters. */
9692+
bool has_parameters = false;
9693+
uint32_t pc = ts_node_named_child_count(params);
9694+
for (uint32_t pi = 0; pi < pc; pi++) {
9695+
if (!ts_node_is_extra(ts_node_named_child(params, pi))) {
9696+
has_parameters = true;
9697+
break;
9698+
}
9699+
}
9700+
if (!has_parameters) {
9701+
return true;
9702+
}
9703+
}
9704+
return false;
9705+
}
9706+
9707+
/* Java records: the compiler writes one accessor per component, so `p.x()`
9708+
* calls a method that appears nowhere in the source. Without a definition for
9709+
* it the call has nothing to resolve to and falls through to the project-wide
9710+
* name registry, which binds it to the first `x` in the tree — typically an
9711+
* unrelated class's private field. Emitting the accessors makes the record's
9712+
* own API visible, exactly as the language defines it (JLS §8.10.3). */
9713+
static void extract_record_accessors(CBMExtractCtx *ctx, TSNode node, const char *class_qn) {
9714+
CBMArena *a = ctx->arena;
9715+
TSNode params = ts_node_child_by_field_name(node, TS_FIELD("parameters"));
9716+
if (ts_node_is_null(params)) {
9717+
return;
9718+
}
9719+
uint32_t pc = ts_node_named_child_count(params);
9720+
for (uint32_t pi = 0; pi < pc; pi++) {
9721+
TSNode comp = ts_node_named_child(params, pi);
9722+
bool spread = strcmp(ts_node_type(comp), "spread_parameter") == 0;
9723+
if (!spread && strcmp(ts_node_type(comp), "formal_parameter") != 0) {
9724+
continue;
9725+
}
9726+
TSNode cname = ts_node_child_by_field_name(comp, TS_FIELD("name"));
9727+
TSNode ctype = ts_node_child_by_field_name(comp, TS_FIELD("type"));
9728+
if (spread) {
9729+
/* Java varargs have an unnamed type child and a nested declarator. */
9730+
TSNode decl = cbm_find_child_by_kind(comp, "variable_declarator");
9731+
if (ts_node_is_null(decl)) {
9732+
continue;
9733+
}
9734+
cname = ts_node_child_by_field_name(decl, TS_FIELD("name"));
9735+
uint32_t nc = ts_node_named_child_count(comp);
9736+
for (uint32_t ci = 0; ci < nc; ci++) {
9737+
TSNode child = ts_node_named_child(comp, ci);
9738+
if (!ts_node_is_extra(child) && strcmp(ts_node_type(child), "modifiers") != 0) {
9739+
ctype = child;
9740+
break;
9741+
}
9742+
}
9743+
}
9744+
if (ts_node_is_null(cname)) {
9745+
continue;
9746+
}
9747+
char *comp_name = cbm_node_text(a, cname, ctx->source);
9748+
if (!comp_name || !comp_name[0] || record_declares_accessor(ctx, node, comp_name)) {
9749+
continue;
9750+
}
9751+
CBMDefinition adef;
9752+
memset(&adef, 0, sizeof(adef));
9753+
adef.name = comp_name;
9754+
adef.qualified_name = cbm_arena_sprintf(a, "%s.%s", class_qn, comp_name);
9755+
adef.label = "Method";
9756+
adef.file_path = ctx->rel_path;
9757+
adef.start_line = ts_node_start_point(comp).row + TS_LINE_OFFSET;
9758+
adef.end_line = ts_node_end_point(comp).row + TS_LINE_OFFSET;
9759+
adef.lines = 1;
9760+
adef.parent_class = class_qn;
9761+
adef.is_exported = true;
9762+
if (!ts_node_is_null(ctype)) {
9763+
const char *type = cbm_node_text(a, ctype, ctx->source);
9764+
adef.return_type = spread && type ? cbm_arena_sprintf(a, "%s[]", type) : type;
9765+
}
9766+
/* The compiler writes this member and the source never spells it: its
9767+
* node says so, and the pipeline registers it by qualified name only,
9768+
* so no short-name guess can land on it. */
9769+
const char **kv = (const char **)cbm_arena_alloc(a, CBM_SZ_3 * sizeof(char *));
9770+
if (kv) {
9771+
kv[0] = CBM_PROP_GENERATED;
9772+
kv[SKIP_ONE] = "record";
9773+
kv[PAIR_LEN] = NULL;
9774+
}
9775+
adef.extra_props = kv;
9776+
cbm_defs_push(&ctx->result->defs, a, adef);
9777+
}
9778+
}
9779+
96609780
/* Resolve the identifier node from a destructure pattern child.
96619781
* pair_pattern → value field; shorthand/identifier → itself; others → first named child. */
96629782
static TSNode destructure_ident(TSNode pat_child) {

‎internal/cbm/extract_usages.c‎

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,9 +1789,25 @@ static uint32_t active_lexical_scope_id(const WalkState *state) {
17891789
static uint32_t lexical_ancestor_of_kind(const WalkState *state, uint32_t start_id,
17901790
bool want_function, bool want_block);
17911791

1792-
static bool python_default_value_reference(TSNode node) {
1793-
for (TSNode parent = ts_node_parent(node); !ts_node_is_null(parent);
1794-
parent = ts_node_parent(parent)) {
1792+
/* One step up: on the occurrence cursor when there is one, else with
1793+
* ts_node_parent, which descends from the ROOT, so a climb costs O(depth^2). */
1794+
static TSNode usage_parent_step(TSTreeCursor *cursor, TSNode current) {
1795+
if (cursor) {
1796+
return ts_tree_cursor_goto_parent(cursor) ? ts_tree_cursor_current_node(cursor)
1797+
: (TSNode){0};
1798+
}
1799+
usage_slow_parent_fallback_test_note();
1800+
return ts_node_parent(current);
1801+
}
1802+
1803+
/* Asked for every Python usage, and a deep expression puts O(depth) parents
1804+
* above each one. A tree without errors climbs them on the walk cursor, like
1805+
* python_scope_directive_kind; a tree with errors keeps ts_node_parent. */
1806+
static bool python_default_value_reference(CBMExtractCtx *ctx, WalkState *state, TSNode node) {
1807+
TSTreeCursor *cursor =
1808+
ts_node_has_error(ctx->root) ? NULL : reset_occurrence_cursor(state, node);
1809+
for (TSNode parent = usage_parent_step(cursor, node); !ts_node_is_null(parent);
1810+
parent = usage_parent_step(cursor, parent)) {
17951811
const char *kind = ts_node_type(parent);
17961812
/* A nested executable scope inside the default owns its own lookups;
17971813
* only a direct default expression is evaluated in the declaring
@@ -1812,10 +1828,10 @@ static bool python_default_value_reference(TSNode node) {
18121828
return false;
18131829
}
18141830

1815-
static uint32_t usage_lexical_scope_id_for_node(CBMExtractCtx *ctx, const WalkState *state,
1816-
TSNode node) {
1831+
static uint32_t usage_lexical_scope_id_for_node(CBMExtractCtx *ctx, WalkState *state, TSNode node) {
18171832
uint32_t scope_id = active_lexical_scope_id(state);
1818-
if (!ctx || ctx->language != CBM_LANG_PYTHON || !python_default_value_reference(node)) {
1833+
if (!ctx || ctx->language != CBM_LANG_PYTHON ||
1834+
!python_default_value_reference(ctx, state, node)) {
18191835
return scope_id;
18201836
}
18211837
uint32_t function_id = lexical_ancestor_of_kind(state, scope_id, true, false);
@@ -1891,15 +1907,43 @@ static bool ensure_lexical_binding_capacity(WalkState *state) {
18911907
}
18921908

18931909
static bool lexical_ancestor_kind(TSNode node, const char *kind) {
1894-
for (TSNode parent = ts_node_parent(node); !ts_node_is_null(parent);
1895-
parent = ts_node_parent(parent)) {
1910+
for (TSNode parent = usage_parent_step(NULL, node); !ts_node_is_null(parent);
1911+
parent = usage_parent_step(NULL, parent)) {
18961912
if (strcmp(ts_node_type(parent), kind) == 0) {
18971913
return true;
18981914
}
18991915
}
19001916
return false;
19011917
}
19021918

1919+
/* Is `node` a name listed by a Python global/nonlocal statement? The grammar
1920+
* makes those names direct children of the statement, so in a tree without
1921+
* errors one parent step on the walk cursor answers it. The full climb above
1922+
* steps with ts_node_parent, which descends from the ROOT each time: O(depth^2)
1923+
* per identifier, asked for every Python identifier, so one deep arithmetic
1924+
* expression went cubic. Error recovery can put a name under an ERROR child of
1925+
* the statement, so a tree with errors keeps the full climb. */
1926+
static CBMPythonDirectiveKind python_scope_directive_kind(CBMExtractCtx *ctx, WalkState *state,
1927+
TSNode node) {
1928+
if (ts_node_has_error(ctx->root)) {
1929+
if (lexical_ancestor_kind(node, "global_statement")) {
1930+
return CBM_PYTHON_DIRECTIVE_GLOBAL;
1931+
}
1932+
return lexical_ancestor_kind(node, "nonlocal_statement") ? CBM_PYTHON_DIRECTIVE_NONLOCAL
1933+
: 0;
1934+
}
1935+
TSNode parent;
1936+
const char *field;
1937+
if (!occurrence_parent(reset_occurrence_cursor(state, node), node, &parent, &field)) {
1938+
return 0;
1939+
}
1940+
const char *kind = ts_node_type(parent);
1941+
if (strcmp(kind, "global_statement") == 0) {
1942+
return CBM_PYTHON_DIRECTIVE_GLOBAL;
1943+
}
1944+
return strcmp(kind, "nonlocal_statement") == 0 ? CBM_PYTHON_DIRECTIVE_NONLOCAL : 0;
1945+
}
1946+
19031947
static TSNode declared_function_name_owner(TSNode node, const CBMLangSpec *spec) {
19041948
for (TSNode parent = ts_node_parent(node); !ts_node_is_null(parent);
19051949
parent = ts_node_parent(parent)) {
@@ -2246,13 +2290,11 @@ static void record_lexical_binding(CBMExtractCtx *ctx, WalkState *state, TSNode
22462290
TSNode function_declaration = declared_function_name_owner(node, spec);
22472291
TSNode class_declaration = declared_class_name_owner(node, spec);
22482292

2249-
if (ctx->language == CBM_LANG_PYTHON && (lexical_ancestor_kind(node, "global_statement") ||
2250-
lexical_ancestor_kind(node, "nonlocal_statement"))) {
2293+
CBMPythonDirectiveKind scope_directive =
2294+
ctx->language == CBM_LANG_PYTHON ? python_scope_directive_kind(ctx, state, node) : 0;
2295+
if (scope_directive) {
22512296
uint32_t function_id = lexical_ancestor_of_kind(state, current_id, true, false);
2252-
CBMPythonDirectiveKind directive = lexical_ancestor_kind(node, "global_statement")
2253-
? CBM_PYTHON_DIRECTIVE_GLOBAL
2254-
: CBM_PYTHON_DIRECTIVE_NONLOCAL;
2255-
record_python_directive(state, function_id, name, directive);
2297+
record_python_directive(state, function_id, name, scope_directive);
22562298
return;
22572299
}
22582300

@@ -2719,8 +2761,7 @@ void handle_usages(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Wal
27192761
}
27202762

27212763
bool python_scope_directive =
2722-
ctx->language == CBM_LANG_PYTHON && (lexical_ancestor_kind(node, "global_statement") ||
2723-
lexical_ancestor_kind(node, "nonlocal_statement"));
2764+
ctx->language == CBM_LANG_PYTHON && python_scope_directive_kind(ctx, state, node) != 0;
27242765
if (python_scope_directive || is_binding_occurrence(ctx, node, spec, state)) {
27252766
char *binding_name = reference_name(ctx, node);
27262767
if (binding_name && binding_name[0] && !cbm_is_keyword(binding_name, ctx->language)) {

0 commit comments

Comments
 (0)