Skip to content

Commit 9c83689

Browse files
committed
Merge origin/main into fix/permanent-daemon-watch
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents ed17097 + a3831c9 commit 9c83689

18 files changed

Lines changed: 1124 additions & 97 deletions

‎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/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)) {

‎src/discover/discover.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ CBMLanguage cbm_disambiguate_frm(const char *path);
5858
* CBM_LANG_RESCRIPT. On read failure, defaults to CBM_LANG_RESCRIPT. */
5959
CBMLanguage cbm_disambiguate_res(const char *path);
6060

61+
/* Disambiguate .d files by reading first 4KB of content.
62+
* Returns CBM_LANG_COUNT (not source) for a make-style dependency file written
63+
* by rustc/cargo, gcc/clang -MD or CMake ("target ...: prereq ..."), otherwise
64+
* CBM_LANG_DLANG. On read failure or any doubt, defaults to CBM_LANG_DLANG. */
65+
CBMLanguage cbm_disambiguate_d(const char *path);
66+
6167
/* Disambiguate .inc files by reading first 4KB of content.
6268
* Returns CBM_LANG_OBJECTSCRIPT_ROUTINE if it looks like an ObjectScript
6369
* include (a "ROUTINE <Uppercase>" header), otherwise CBM_LANG_BITBAKE.

‎src/discover/language.c‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,107 @@ CBMLanguage cbm_disambiguate_res(const char *path) {
14361436
return lang_res_bytes(head, n);
14371437
}
14381438

1439+
/* A make rule's targets end at a ':' followed by whitespace or the end of the
1440+
* file, which skips drive-letter colons ("C:/", "C:\"). */
1441+
static bool is_rule_colon(const char *p, bool whole_file) {
1442+
char next = p[SKIP_ONE];
1443+
return *p == ':' && (next == ' ' || next == '\t' || next == '\r' || next == '\n' ||
1444+
(next == '\0' && whole_file));
1445+
}
1446+
1447+
/* Bytes of D whitespace at p, or 0: space, tab, vertical tab, form feed, CR
1448+
* and the UTF-8 line and paragraph separators U+2028/U+2029, all of which split
1449+
* D tokens. '\n' ends the line and is the caller's. */
1450+
static size_t d_space_len(const char *p) {
1451+
if (*p == ' ' || *p == '\t' || *p == '\v' || *p == '\f' || *p == '\r') {
1452+
return SKIP_ONE;
1453+
}
1454+
if (strncmp(p, "\xE2\x80\xA8", SLEN("\xE2\x80\xA8")) == 0 ||
1455+
strncmp(p, "\xE2\x80\xA9", SLEN("\xE2\x80\xA9")) == 0) {
1456+
return SLEN("\xE2\x80\xA8");
1457+
}
1458+
return 0;
1459+
}
1460+
1461+
/* True if the line at p is a make rule "target...: prereq..." whose targets all
1462+
* look like paths (contain '/', '\' or '.'); a backslash-escaped space stays
1463+
* inside its target. D source fails this: "public:", "@safe:", "extern(C):"
1464+
* and "import a.b : c" all have a non-path word or D punctuation before the
1465+
* colon. An attribute ('@', as in "@1.0:") or a comment between tokens
1466+
* ("public/+ +/:") is D even with a '.' or '/' before the colon, so a target
1467+
* holding either is never dep-info. */
1468+
static bool is_dep_rule_line(const char *p, bool whole_file) {
1469+
bool in_target = false;
1470+
bool path_like = false;
1471+
bool any_target = false;
1472+
for (; *p && *p != '\n'; p++) {
1473+
if (is_rule_colon(p, whole_file)) {
1474+
return in_target ? path_like : any_target;
1475+
}
1476+
size_t space = d_space_len(p);
1477+
if (space) {
1478+
if (in_target && !path_like) {
1479+
return false;
1480+
}
1481+
in_target = false;
1482+
path_like = false;
1483+
p += space - SKIP_ONE;
1484+
continue;
1485+
}
1486+
if (strchr("(){};=\"',@", *p)) {
1487+
return false;
1488+
}
1489+
if (*p == '/' && (p[SKIP_ONE] == '*' || p[SKIP_ONE] == '+' || p[SKIP_ONE] == '/')) {
1490+
return false;
1491+
}
1492+
in_target = true;
1493+
any_target = true;
1494+
path_like = path_like || *p == '/' || *p == '\\' || *p == '.';
1495+
if (*p == '\\' && p[SKIP_ONE] == ' ') {
1496+
p++; /* escaped space: part of this target */
1497+
}
1498+
}
1499+
return false;
1500+
}
1501+
1502+
/* Disambiguate .d files: shared by D source and make-style dependency files
1503+
* written by rustc/cargo (target/<profile>/deps/<crate>-<hash>.d), gcc/clang -MD
1504+
* and CMake (<object>.o.d). Those are "target: prereq ..." rules, often tens of
1505+
* KB on one line, which the D grammar parses slowly into nothing but a module
1506+
* node, so they are reported as unsupported (CBM_LANG_COUNT). Defaults to D on
1507+
* any doubt (preserves existing behaviour). `n` is the count of head bytes buf
1508+
* was made from. */
1509+
static CBMLanguage lang_d_text(const char *buf, size_t n) {
1510+
/* A colon at the end of buf only ends the rule if nothing was cut off. */
1511+
bool whole_file = n < LANG_PROBE_HEAD && strlen(buf) == n;
1512+
1513+
const char *p = buf;
1514+
if (strncmp(p, "\xEF\xBB\xBF", SLEN("\xEF\xBB\xBF")) == 0) {
1515+
p += SLEN("\xEF\xBB\xBF");
1516+
}
1517+
while (*p && isspace((unsigned char)*p)) {
1518+
p++;
1519+
}
1520+
/* A shebang or a leading D comment (including the DUB single-file
1521+
* "/+ dub.sdl:" recipe) is D; dep-info never starts that way. */
1522+
if (*p == '#' ||
1523+
(p[0] == '/' && (p[SKIP_ONE] == '/' || p[SKIP_ONE] == '*' || p[SKIP_ONE] == '+'))) {
1524+
return CBM_LANG_DLANG;
1525+
}
1526+
return is_dep_rule_line(p, whole_file) ? CBM_LANG_COUNT : CBM_LANG_DLANG;
1527+
}
1528+
1529+
CBMLanguage cbm_disambiguate_d(const char *path) {
1530+
unsigned char head[LANG_PROBE_HEAD];
1531+
size_t n = 0;
1532+
if (!lang_read_head(path, head, sizeof(head), &n)) {
1533+
return CBM_LANG_DLANG;
1534+
}
1535+
char buf[LANG_PROBE_HEAD + SKIP_ONE];
1536+
lang_head_text(buf, LANG_PROBE_HEAD, head, n);
1537+
return lang_d_text(buf, n);
1538+
}
1539+
14391540
/* Disambiguate .cls files: shared by InterSystems ObjectScript UDL, Salesforce
14401541
* Apex and Visual Basic 6 class modules (#721). ObjectScript class files begin
14411542
* with a line of the form "Class <UppercasePackage>..."; VB6 class modules
@@ -1611,6 +1712,13 @@ static const char *lang_probe_extension(const char *filename) {
16111712
return (dot && lang_name_in(dot, PROBED)) ? dot : NULL;
16121713
}
16131714

1715+
/* .d is shared by D and make-style dep-info (cargo, gcc -MD). Probe only while
1716+
* .d maps to D, so a user override to another language wins. */
1717+
static bool lang_probe_d(CBMLanguage lang, const char *filename) {
1718+
const char *dot = strrchr(filename, '.');
1719+
return lang == CBM_LANG_DLANG && dot && strcmp(dot, ".d") == 0;
1720+
}
1721+
16141722
/* "<Export generator=" in the first line-probe bytes: an ObjectScript Studio
16151723
* export, which is XML by name. */
16161724
static bool lang_objectscript_export(const unsigned char *head, size_t n) {
@@ -1633,6 +1741,9 @@ size_t cbm_language_probe_bytes_with(const cbm_userconfig_t *config, const char
16331741
if (ext) {
16341742
return strcmp(ext, ".cfc") == 0 ? LANG_PROBE_CFC : LANG_PROBE_HEAD;
16351743
}
1744+
if (lang_probe_d(lang, filename)) {
1745+
return LANG_PROBE_HEAD;
1746+
}
16361747
return lang == CBM_LANG_XML ? LANG_PROBE_LINE : 0;
16371748
}
16381749

@@ -1676,6 +1787,11 @@ CBMLanguage cbm_language_classify_with(const cbm_userconfig_t *config, const cha
16761787
/* .res: ReScript, or a binary Godot / Windows resource (#2176) */
16771788
lang = readable ? lang_res_bytes(head, head_len) : CBM_LANG_RESCRIPT;
16781789
}
1790+
} else if (lang_probe_d(lang, filename)) {
1791+
/* D, or a make-style dep-info file */
1792+
char buf[LANG_PROBE_HEAD + SKIP_ONE];
1793+
lang_head_text(buf, LANG_PROBE_HEAD, head, head_len);
1794+
lang = readable ? lang_d_text(buf, head_len) : CBM_LANG_DLANG;
16791795
}
16801796
if (lang == CBM_LANG_XML && readable && lang_objectscript_export(head, head_len)) {
16811797
return CBM_LANG_OBJECTSCRIPT_EXPORT;

0 commit comments

Comments
 (0)