Skip to content

Commit 5e596c7

Browse files
committed
Merge main into fix/issue-686-fiber-group-v2 for the release batch
Conflict in internal/cbm/extract_calls.c with main's DeusData#1146 slashless Laravel routes (DeusData#2471): both helper sets kept (this branch's route_prefix_compose/route_path_with_prefix, main's php_plain_string_value/php_first_arg_value/php_group_array_prefix). At the call site main's slashless-URI step runs first, then this branch's shared route_path_with_group_prefix replaces main's inline DeusData#952 composition (identical composition and guard), then main's string-action handler rule. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents 1d0a9de + bf93f0b commit 5e596c7

16 files changed

Lines changed: 1070 additions & 71 deletions

‎docs/CONFIGURATION.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,33 @@ be indexed and later returned. And the credential list is a denylist, so it
236236
raises the cost of a mistake rather than closing the class — a directory it does
237237
not name is permitted.
238238

239+
### Shared / HPC filesystems
240+
241+
On clusters and other shared machines, home directories often live on a network
242+
filesystem (NFS, Lustre, GPFS, SMB/CIFS), and `~/.cache` is sometimes a symlink
243+
into a shared tree. Three rules keep CBM working there:
244+
245+
- **Put the cache and the rendezvous on local storage.** Set `CBM_CACHE_DIR` (indexes,
246+
`_config.db`) and, if needed, `CBM_RUNTIME_DIR` (the daemon rendezvous) to a
247+
local disk you own, for example `/local/scratch/$USER/cbm` or `/tmp/$USER-cbm`.
248+
Set the same values in your MCP client's environment and your shell.
249+
- **Do not route the cache into another account's tree.** A symlinked `~/.cache` is
250+
resolved and allowed, but every directory on the *resolved* path must be owned
251+
by you or by root. If a parent such as `/shared/fs` belongs to another account,
252+
that account can rename or replace anything below it, so CBM refuses the path
253+
and names the directory and its owner:
254+
255+
```text
256+
... (cache-private) - '/shared/fs' is owned by uid 1234 (alice), not by you (uid 1000) or root; ...
257+
```
258+
259+
This check has no override. Choose a location with a safe ancestry instead.
260+
- **Avoid network filesystems for the cache.** The indexes are SQLite databases in
261+
WAL mode, which needs shared-memory locking that network filesystems do not
262+
provide reliably. On Linux, CBM logs one `daemon.cache_root_network_fs` warning
263+
at startup when the cache root is on NFS, Lustre, GPFS, CIFS or SMB2. The cache
264+
still works there, but it can be slow, and a database can be corrupted.
265+
239266
## 5. Agent and Editor Integration Files
240267

241268
The `install` command can also write MCP entries and instruction blocks into agent/editor config files such as Claude Code, Codex, Gemini, VS Code, Cursor, Zed, and others.

‎internal/cbm/cbm.c‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,26 @@ static void cbm_mark_pp_error_rows(TSNode n, uint8_t *rows, uint32_t row_count,
14341434
}
14351435
}
14361436

1437+
/* The ONE line counter for a source buffer (#1967). Every caller that asks
1438+
* "how many lines does this file have" uses it, so the coverage report never
1439+
* holds two answers for the same buffer.
1440+
*
1441+
* Convention: a '\n' TERMINATES a line; it does not open a new one. So the
1442+
* count is the number of '\n' that have at least one byte after them, plus
1443+
* one. "a\nb" and "a\nb\n" both have 2 lines (the trailing newline adds no
1444+
* phantom empty line, matching what an editor shows); "a\nb" without a final
1445+
* newline still counts its last line. An empty buffer counts as 1 line, because
1446+
* tree-sitter still reports row 0 for it and 1-based line maps index line 1. */
1447+
uint32_t cbm_source_line_count(const char *src, int src_len) {
1448+
uint32_t n = 1;
1449+
for (int i = 0; i + 1 < src_len; i++) {
1450+
if (src[i] == '\n') {
1451+
n++;
1452+
}
1453+
}
1454+
return n;
1455+
}
1456+
14371457
/* Recovery subtraction (#963): tree-sitter error recovery plus the
14381458
* ERROR-descending def walker often still extract constructs INSIDE a failed
14391459
* region (verified: a function in an #ifdef-split ERROR region and even a
@@ -1451,12 +1471,7 @@ static void cbm_mark_pp_error_rows(TSNode n, uint8_t *rows, uint32_t row_count,
14511471
* Now the uncovered gaps are reported instead, and a gap holding only blank,
14521472
* comment or preprocessor lines is not a miss at all. */
14531473
static uint32_t *cbm_line_offsets(const char *src, int src_len, uint32_t *out_lines) {
1454-
uint32_t lines = 1;
1455-
for (int i = 0; i < src_len; i++) {
1456-
if (src[i] == '\n') {
1457-
lines++;
1458-
}
1459-
}
1474+
uint32_t lines = cbm_source_line_count(src, src_len);
14601475
uint32_t *offs =
14611476
(uint32_t *)cbm_alloc(CBM_MEM_CLASS_EXTRACT, (size_t)(lines + 1) * sizeof(uint32_t));
14621477
if (!offs) {
@@ -2061,18 +2076,6 @@ static void cbm_refine_regions_with_pp_lines(cbm_error_regions_t *regs, const ui
20612076
* this repo covers 25.5% of its file, and the next widest 3.9%. */
20622077
#define CBM_UNUSABLE_PCT 80
20632078

2064-
/* Number of 1-based lines in `src`. A file that does not end with a newline
2065-
* still has a last line, so the count is separators plus one. */
2066-
static uint32_t cbm_count_lines(const char *src, int src_len) {
2067-
uint32_t n = 1;
2068-
for (int i = 0; i < src_len; i++) {
2069-
if (src[i] == '\n' && i + 1 < src_len) {
2070-
n++;
2071-
}
2072-
}
2073-
return n;
2074-
}
2075-
20762079
/* Serialize collected regions as "start-end,start-end,...", with a trailing
20772080
* ",+<N>" when the cap threw N ranges away.
20782081
*
@@ -2868,12 +2871,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
28682871
* total loss (root is ERROR), because then it vouches for
28692872
* nothing and there is no refinement to make. */
28702873
if (strcmp(ts_node_type(pp_root), "ERROR") != 0) {
2871-
uint32_t orig_lines = 1;
2872-
for (int ci = 0; ci < source_len; ci++) {
2873-
if (source[ci] == '\n') {
2874-
orig_lines++;
2875-
}
2876-
}
2874+
uint32_t orig_lines = cbm_source_line_count(source, source_len);
28772875
uint8_t *map = (uint8_t *)cbm_arena_alloc(a, (size_t)orig_lines + 2);
28782876
int exp_lines = preprocessed->expanded_line_count;
28792877
uint8_t *bad_rows =
@@ -3069,7 +3067,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
30693067
* so the report can say "read the source" instead. See
30703068
* parse_unusable in cbm.h for which files land here and why. */
30713069
if (regs.count == 1 && regs.dropped == 0) {
3072-
uint32_t total = cbm_count_lines(source, source_len);
3070+
uint32_t total = cbm_source_line_count(source, source_len);
30733071
uint32_t span = regs.ends[0] - regs.starts[0] + 1;
30743072
if (total > 0 && span * 100 >= total * CBM_UNUSABLE_PCT) {
30753073
result->parse_unusable = true;

‎internal/cbm/cbm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,11 @@ uint64_t cbm_usage_field_lookup_test_work(void);
10051005
uint64_t cbm_usage_slow_parent_fallback_test_count(void);
10061006
#endif
10071007

1008+
// Number of 1-based lines in a source buffer. The single line-count convention
1009+
// for coverage reporting (#1967): a trailing '\n' ends the last line and opens
1010+
// no new one; an empty buffer counts as 1 line. See the definition in cbm.c.
1011+
uint32_t cbm_source_line_count(const char *src, int src_len);
1012+
10081013
// Toggle C/C++ preprocessor Macro-node extraction (#375). The pipeline enables
10091014
// it only for full/advanced index modes (it dominates extraction on macro-dense
10101015
// codebases). Default ON. Set before extraction; read-only during.

‎internal/cbm/extract_calls.c‎

Lines changed: 154 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,59 @@ static const char *route_path_with_prefix(CBMArena *a, const char *prefix, const
18051805
: cbm_arena_strndup(a, prefix, strlen(prefix));
18061806
}
18071807

1808+
/* The value of a plain PHP string literal ('...' or "..." with no
1809+
* interpolation or escapes), "" for an empty one; NULL for anything else. */
1810+
static const char *php_plain_string_value(CBMArena *a, TSNode node, const char *source) {
1811+
const char *kind = ts_node_type(node);
1812+
if (strcmp(kind, "string") != 0 && strcmp(kind, "encapsed_string") != 0) {
1813+
return NULL;
1814+
}
1815+
uint32_t nc = ts_node_named_child_count(node);
1816+
for (uint32_t i = 0; i < nc; i++) {
1817+
if (strcmp(ts_node_type(ts_node_named_child(node, i)), "string_content") != 0) {
1818+
return NULL;
1819+
}
1820+
}
1821+
return nc == 0 ? "" : cbm_node_text(a, ts_node_named_child(node, 0), source);
1822+
}
1823+
1824+
/* The first argument's value node of a PHP call (unwrapping `argument`). */
1825+
static TSNode php_first_arg_value(TSNode call_node) {
1826+
TSNode none = {0};
1827+
TSNode args = ts_node_child_by_field_name(call_node, TS_FIELD("arguments"));
1828+
if (ts_node_is_null(args) || ts_node_named_child_count(args) == 0) {
1829+
return none;
1830+
}
1831+
TSNode arg = ts_node_named_child(args, 0);
1832+
if (strcmp(ts_node_type(arg), "argument") == 0) {
1833+
return ts_node_named_child_count(arg) == SKIP_ONE ? ts_node_named_child(arg, 0) : none;
1834+
}
1835+
return arg;
1836+
}
1837+
1838+
/* The literal 'prefix' of an attribute-array group (#1146):
1839+
* Route::group(['prefix' => 'v1/autocomplete', 'as' => ...], function () {...}),
1840+
* the form Firefly III's whole API is written in. */
1841+
static const char *php_group_array_prefix(CBMArena *a, TSNode group_call, const char *source) {
1842+
TSNode attrs = php_first_arg_value(group_call);
1843+
if (ts_node_is_null(attrs) || strcmp(ts_node_type(attrs), "array_creation_expression") != 0) {
1844+
return NULL;
1845+
}
1846+
uint32_t nc = ts_node_named_child_count(attrs);
1847+
for (uint32_t i = 0; i < nc; i++) {
1848+
TSNode el = ts_node_named_child(attrs, i);
1849+
if (strcmp(ts_node_type(el), "array_element_initializer") != 0 ||
1850+
ts_node_named_child_count(el) != PAIR_LEN) {
1851+
continue;
1852+
}
1853+
const char *key = php_plain_string_value(a, ts_node_named_child(el, 0), source);
1854+
if (key && strcmp(key, "prefix") == 0) {
1855+
return php_plain_string_value(a, ts_node_named_child(el, SKIP_ONE), source);
1856+
}
1857+
}
1858+
return NULL;
1859+
}
1860+
18081861
static const char *php_group_prefix_for_call(CBMArena *a, TSNode node, const char *source) {
18091862
const char *parts[PHP_PREFIX_PARTS_MAX] = {NULL};
18101863
int part_count = 0;
@@ -1827,7 +1880,14 @@ static const char *php_group_prefix_for_call(CBMArena *a, TSNode node, const cha
18271880
strcmp(ts_node_type(p), "scoped_call_expression") == 0)) {
18281881
TSNode gname = ts_node_child_by_field_name(p, TS_FIELD("name"));
18291882
char *gtxt = ts_node_is_null(gname) ? NULL : cbm_node_text(a, gname, source);
1830-
if (gtxt && strcmp(gtxt, "group") == 0) {
1883+
const char *array_prefix = gtxt && strcmp(gtxt, "group") == 0
1884+
? php_group_array_prefix(a, p, source)
1885+
: NULL;
1886+
if (array_prefix) {
1887+
if (part_count < PHP_PREFIX_PARTS_MAX) {
1888+
parts[part_count++] = array_prefix; /* inner-first */
1889+
}
1890+
} else if (gtxt && strcmp(gtxt, "group") == 0) {
18311891
/* Scan the receiver chain for prefix('...'). */
18321892
TSNode recv = ts_node_child_by_field_name(p, TS_FIELD("object"));
18331893
for (int hops = 0; hops < PHP_GROUP_WALK_MAX && !ts_node_is_null(recv);
@@ -2051,6 +2111,81 @@ static const char *route_path_with_group_prefix(CBMExtractCtx *ctx, TSNode node,
20512111
return (gp && gp[0]) ? route_path_with_prefix(ctx->arena, gp, path) : path;
20522112
}
20532113

2114+
/* A Laravel route registrar receiver (#1146): the Route facade — also at the
2115+
* root of a registrar chain, Route::middleware('auth')->get(...) — or the
2116+
* $router instance route files are loaded with (RouteServiceProvider, Lumen).
2117+
* The same literal `Route` scope the #952 callee qualification accepts. */
2118+
static bool php_is_laravel_registrar(CBMArena *a, TSNode call_node, const char *source) {
2119+
TSNode cur = call_node;
2120+
for (int hops = 0; hops < PHP_GROUP_WALK_MAX && !ts_node_is_null(cur); hops++) {
2121+
const char *kind = ts_node_type(cur);
2122+
if (strcmp(kind, "scoped_call_expression") == 0) {
2123+
TSNode scope = ts_node_child_by_field_name(cur, TS_FIELD("scope"));
2124+
const char *st = ts_node_is_null(scope) ? NULL : cbm_node_text(a, scope, source);
2125+
return st && strcmp(st, "Route") == 0;
2126+
}
2127+
if (strcmp(kind, "variable_name") == 0) {
2128+
const char *vt = cbm_node_text(a, cur, source);
2129+
return vt && strcmp(vt, "$router") == 0;
2130+
}
2131+
if (strcmp(kind, "member_call_expression") != 0) {
2132+
return false;
2133+
}
2134+
cur = ts_node_child_by_field_name(cur, TS_FIELD("object"));
2135+
}
2136+
return false;
2137+
}
2138+
2139+
/* Laravel accepts a route URI with or without its leading slash and trims
2140+
* '/' from both ends: Route::get('users', ...) serves /users, and '' is the
2141+
* group root. The route passes only mint a Route for a path that starts with
2142+
* '/', so give a recognised registration's literal URI its slash here, where
2143+
* the first argument is still at hand (#1146). Returns NULL — leave the call
2144+
* alone — for any other call, any non-literal URI, or one already slashed:
2145+
* `$request->get('name')`, `Cache::get('key')` and a collection's get('k')
2146+
* keep their argument. */
2147+
static const char *php_laravel_route_uri(CBMArena *a, TSNode call_node, const char *source) {
2148+
static const char *const verbs[] = {"get", "post", "put", "patch", "delete", NULL};
2149+
TSNode mname = ts_node_child_by_field_name(call_node, TS_FIELD("name"));
2150+
const char *verb = ts_node_is_null(mname) ? NULL : cbm_node_text(a, mname, source);
2151+
bool is_verb = false;
2152+
for (int i = 0; verb && verbs[i] && !is_verb; i++) {
2153+
is_verb = strcmp(verb, verbs[i]) == 0;
2154+
}
2155+
if (!is_verb || !php_is_laravel_registrar(a, call_node, source)) {
2156+
return NULL;
2157+
}
2158+
TSNode arg0 = php_first_arg_value(call_node);
2159+
const char *uri = ts_node_is_null(arg0) ? NULL : php_plain_string_value(a, arg0, source);
2160+
if (!uri || uri[0] == '/') {
2161+
return NULL;
2162+
}
2163+
size_t len = strlen(uri);
2164+
while (len > 0 && uri[len - SKIP_ONE] == '/') {
2165+
len--;
2166+
}
2167+
return cbm_arena_sprintf(a, "/%.*s", (int)len, uri);
2168+
}
2169+
2170+
/* Is a Laravel registration's action (its last argument) a string — 'index'
2171+
* inside a Route::controller() group, 'UserController@show'? Such an action
2172+
* does not say which class owns the method, so resolving it could only guess a
2173+
* same-named function anywhere in the repo. */
2174+
static bool php_route_action_is_string(TSNode call_node) {
2175+
TSNode args = ts_node_child_by_field_name(call_node, TS_FIELD("arguments"));
2176+
uint32_t nc = ts_node_is_null(args) ? 0 : ts_node_named_child_count(args);
2177+
if (nc < PAIR_LEN) {
2178+
return false;
2179+
}
2180+
TSNode action = ts_node_named_child(args, nc - SKIP_ONE);
2181+
if (strcmp(ts_node_type(action), "argument") == 0 &&
2182+
ts_node_named_child_count(action) == SKIP_ONE) {
2183+
action = ts_node_named_child(action, 0);
2184+
}
2185+
const char *kind = ts_node_type(action);
2186+
return strcmp(kind, "string") == 0 || strcmp(kind, "encapsed_string") == 0;
2187+
}
2188+
20542189
static bool is_nested_verilog_call_wrapper(CBMLanguage lang, TSNode node) {
20552190
if (lang != CBM_LANG_VERILOG || strcmp(ts_node_type(node), "subroutine_call") != 0) {
20562191
return false;
@@ -4148,10 +4283,24 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
41484283
args = swift_call_args(node);
41494284
}
41504285
if (!ts_node_is_null(args)) {
4151-
call.first_string_arg = route_path_with_group_prefix(
4152-
ctx, node, call.callee_name,
4153-
extract_url_or_topic_arg(ctx, args, call.callee_name));
4154-
if (call.first_string_arg && call.first_string_arg[0] == '/') {
4286+
call.first_string_arg = extract_url_or_topic_arg(ctx, args, call.callee_name);
4287+
/* #1146: a Laravel registration's slashless URI ('users', '')
4288+
* becomes '/users' / '/' so it mints and composes below. */
4289+
const char *laravel_uri = NULL;
4290+
if (ctx->language == CBM_LANG_PHP && call.callee_name &&
4291+
cbm_service_pattern_route_method(call.callee_name) != NULL) {
4292+
laravel_uri = php_laravel_route_uri(ctx->arena, node, ctx->source);
4293+
if (laravel_uri) {
4294+
call.first_string_arg = laravel_uri;
4295+
}
4296+
}
4297+
/* Laravel prefix()->group() (#952) and Go x.Group("/p") (#686). */
4298+
call.first_string_arg = route_path_with_group_prefix(ctx, node, call.callee_name,
4299+
call.first_string_arg);
4300+
/* A slashless registration's string action would only be
4301+
* guessed at (#1146): mint its Route without a handler. */
4302+
if (call.first_string_arg && call.first_string_arg[0] == '/' &&
4303+
!(laravel_uri && php_route_action_is_string(node))) {
41554304
call.second_arg_name = extract_handler_arg(ctx, args);
41564305
}
41574306
if (ctx->language == CBM_LANG_OBJECTSCRIPT_UDL ||

‎internal/cbm/extract_defs.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2987,7 +2987,11 @@ static bool try_drf_action_decorator(CBMArena *a, TSNode dchild, const char *sou
29872987
}
29882988

29892989
// Try to extract a route from a single decorator call node.
2990-
// Returns true if a route method was found (even with fallback path "/").
2990+
// Returns true if a route method was found. The fallback path "/" applies only
2991+
// to a zero-argument call (`@app.route()`): a call that HAS arguments but none
2992+
// path-shaped is not a route -- unittest.mock's `@patch("subprocess.run")`
2993+
// shares its name with the HTTP verb and used to mint a PATCH "/" handler for
2994+
// every mocked test function (distilled from PR #1245).
29912995
static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char *source,
29922996
const char **out_path, const char **out_method) {
29932997
TSNode fn = ts_node_child_by_field_name(dchild, TS_FIELD("function"));
@@ -3012,6 +3016,9 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char
30123016
*out_method = method;
30133017
return true;
30143018
}
3019+
if (ts_node_named_child_count(args) > 0) {
3020+
return false;
3021+
}
30153022
}
30163023
*out_path = "/";
30173024
*out_method = method;

‎internal/cbm/service_patterns.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,11 @@ static const method_suffix_t route_reg_suffixes[] = {
577577
{".add_route", "ANY"},
578578
{".add_api_route", "ANY"},
579579
{".add_api_websocket_route", "ANY"},
580+
/* FastAPI/Starlette WebSocket decorators (`@router.websocket('/ws')`,
581+
* `@app.websocket_route('/events')`): not an HTTP verb, so ANY — mirrors
582+
* .add_api_websocket_route above (#1245). */
583+
{".websocket", "ANY"},
584+
{".websocket_route", "ANY"},
580585
{NULL, NULL},
581586
};
582587

0 commit comments

Comments
 (0)