Skip to content

Commit da09b73

Browse files
authored
Merge branch 'main' into fix/issue-1682-dotted-ts-imports
2 parents 3f08fac + cc3e263 commit da09b73

52 files changed

Lines changed: 2181 additions & 359 deletions

Some content is hidden

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

‎graph-ui/src/App.test.tsx‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* @vitest-environment jsdom */
2+
import "@testing-library/jest-dom/vitest";
3+
import { cleanup, render, screen, waitFor } from "@testing-library/react";
4+
import { afterEach, describe, expect, it, vi } from "vitest";
5+
import { App } from "./App";
6+
import { messages } from "./lib/i18n";
7+
8+
vi.mock("./components/GraphTab", () => ({ GraphTab: () => null }));
9+
vi.mock("./components/StatsTab", () => ({ StatsTab: () => null }));
10+
vi.mock("./components/ControlTab", () => ({ ControlTab: () => null }));
11+
vi.mock("./lib/i18n", async (importOriginal) => {
12+
const actual = await importOriginal<typeof import("./lib/i18n")>();
13+
return { ...actual, useUiMessages: () => messages.en };
14+
});
15+
16+
describe("App", () => {
17+
afterEach(() => {
18+
cleanup();
19+
vi.unstubAllGlobals();
20+
window.history.replaceState(null, "", "/");
21+
});
22+
23+
it("shows the serving binary version", async () => {
24+
vi.stubGlobal("fetch", vi.fn(async () =>
25+
new Response(JSON.stringify({ lang: "en", version: "0.10.8" }), {
26+
status: 200,
27+
headers: { "Content-Type": "application/json" },
28+
}),
29+
));
30+
31+
render(<App />);
32+
33+
expect(await screen.findByText("v0.10.8")).toBeVisible();
34+
});
35+
36+
it("hides the version when the config has no string version", async () => {
37+
const fetchMock = vi.fn(async () =>
38+
new Response(JSON.stringify({ lang: "en", version: 108 }), { status: 200 }),
39+
);
40+
vi.stubGlobal("fetch", fetchMock);
41+
42+
render(<App />);
43+
44+
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith("/api/ui-config"));
45+
expect(screen.queryByTitle("Server version")).not.toBeInTheDocument();
46+
});
47+
48+
it("hides the version when the config request fails", async () => {
49+
const fetchMock = vi.fn(async () => {
50+
throw new Error("offline");
51+
});
52+
vi.stubGlobal("fetch", fetchMock);
53+
54+
render(<App />);
55+
56+
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith("/api/ui-config"));
57+
expect(screen.queryByTitle("Server version")).not.toBeInTheDocument();
58+
});
59+
});

‎graph-ui/src/App.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,24 @@ function routeUrl(tab: TabId, project: string | null): string {
3333
export function App() {
3434
const t = useUiMessages();
3535
const [route, setRoute] = useState<RouteState>(readRoute);
36+
const [version, setVersion] = useState<string | null>(null);
3637
const { tab: activeTab, project: selectedProject } = route;
3738

39+
useEffect(() => {
40+
let cancelled = false;
41+
void fetch("/api/ui-config")
42+
.then((response) => (response.ok ? response.json() : null))
43+
.then((config) => {
44+
if (!cancelled && typeof config?.version === "string" && config.version) {
45+
setVersion(config.version);
46+
}
47+
})
48+
.catch(() => {});
49+
return () => {
50+
cancelled = true;
51+
};
52+
}, []);
53+
3854
/* Normalize the URL on first load so it always carries the current route. */
3955
useEffect(() => {
4056
const initial = readRoute();
@@ -73,6 +89,14 @@ export function App() {
7389
<span className="text-[13px] font-semibold text-foreground/90 tracking-tight">
7490
Codebase Memory
7591
</span>
92+
{version && (
93+
<span
94+
className="translate-y-px text-[10px] font-mono text-foreground/30"
95+
title="Server version"
96+
>
97+
{version.startsWith("v") ? version : `v${version}`}
98+
</span>
99+
)}
76100
</div>
77101

78102
{/* Tabs inline in header */}

‎internal/cbm/cbm.c‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/* Full declaration set for the same CBMArena, and it must precede cbm.h:
2+
* internal/cbm/arena.h declares a subset and the two share the CBM_ARENA_H
3+
* guard, so whichever is included first is the one this file sees. */
4+
#include "foundation/arena.h" // cbm_arena_init_sized
15
#include "cbm.h"
26
#include "arena.h" // CBMArena, cbm_arena_init/alloc/strdup/destroy
37
#include "helpers.h"
@@ -1186,11 +1190,27 @@ CBMFileResult *cbm_extract_file(const char *source, int source_len, CBMLanguage
11861190
return r;
11871191
}
11881192

1189-
CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLanguage language,
1190-
const char *project, const char *rel_path,
1191-
int64_t timeout_micros, const char **extra_defines,
1192-
const char **include_paths, const CBMMacroTable *macro_table,
1193-
const CBMReturnTypeTable *return_type_table) {
1193+
/* Initial block for the per-file traversal scratch arena, chosen by measuring
1194+
* arena_grow on a 14k-file TypeScript tree: it fires on one file in 12,000 at
1195+
* both this size and at 1 MB, and on most files at 256 KB, where the two
1196+
* channel walks alone are exactly 262144 bytes. 512 KB therefore buys the same
1197+
* growth behaviour as 1 MB for half the resident block per worker. It is also
1198+
* exactly MI_LARGE_MAX_OBJ_SIZE in the vendored mimalloc
1199+
* (vendored/mimalloc/include/mimalloc/types.h:426, MI_LARGE_PAGE_SIZE/8 with
1200+
* MI_ENABLE_LARGE_PAGES defaulting to 1 at :115 and not overridden here), so
1201+
* the block is still bin-allocated from a large page. Growth is not free at
1202+
* this size for the same reason: arena_grow doubles to 1 MiB, which is above
1203+
* that bound and so a singleton OS allocation. One file in twelve thousand
1204+
* pays it, which is why the cost is accepted. */
1205+
enum { CBM_EXTRACT_SCRATCH_BLOCK = CBM_SZ_512 * CBM_SZ_1K };
1206+
1207+
static CBMFileResult *extract_file_ex_body(const char *source, int source_len, CBMLanguage language,
1208+
const char *project, const char *rel_path,
1209+
int64_t timeout_micros, const char **extra_defines,
1210+
const char **include_paths,
1211+
const CBMMacroTable *macro_table,
1212+
const CBMReturnTypeTable *return_type_table,
1213+
CBMArena *scratch) {
11941214
// Allocate result on heap (arena inside for all string data)
11951215
enum { SINGLE = 1 };
11961216
CBMFileResult *result = (CBMFileResult *)calloc(SINGLE, sizeof(CBMFileResult));
@@ -1301,6 +1321,7 @@ CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLangua
13011321
// Build extraction context
13021322
CBMExtractCtx ctx = {
13031323
.arena = a,
1324+
.scratch = scratch,
13041325
.result = result,
13051326
.source = source,
13061327
.source_len = source_len,
@@ -1429,6 +1450,7 @@ CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLangua
14291450
// Build context for expanded source — extract only calls via unified extractor
14301451
CBMExtractCtx pp_ctx = {
14311452
.arena = a,
1453+
.scratch = scratch,
14321454
.result = result,
14331455
.source = expanded,
14341456
.source_len = expanded_len,
@@ -1665,6 +1687,26 @@ CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLangua
16651687
return result;
16661688
}
16671689

1690+
/* Public entry. Owns the traversal scratch arena for the whole of one file's
1691+
* extraction: created here, handed to the body as ctx->scratch, destroyed on
1692+
* the way out. The body has seven early returns, so bracketing it in a wrapper
1693+
* is what keeps that to one create and one destroy. If the arena cannot be
1694+
* created, the body is handed NULL and the traversal stacks fall back to the
1695+
* result arena, which is what shipped before #1997. */
1696+
CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLanguage language,
1697+
const char *project, const char *rel_path,
1698+
int64_t timeout_micros, const char **extra_defines,
1699+
const char **include_paths, const CBMMacroTable *macro_table,
1700+
const CBMReturnTypeTable *return_type_table) {
1701+
CBMArena scratch;
1702+
cbm_arena_init_sized(&scratch, CBM_EXTRACT_SCRATCH_BLOCK);
1703+
CBMFileResult *result = extract_file_ex_body(
1704+
source, source_len, language, project, rel_path, timeout_micros, extra_defines,
1705+
include_paths, macro_table, return_type_table, scratch.nblocks > 0 ? &scratch : NULL);
1706+
cbm_arena_destroy(&scratch);
1707+
return result;
1708+
}
1709+
16681710
void cbm_free_result(CBMFileResult *result) {
16691711
if (!result) {
16701712
return;

‎internal/cbm/cbm.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ typedef struct {
269269
// pass_lsp_cross.c. Default false.
270270
bool requires_lsp_resolution; // synthetic semantic candidate (for example an implicit
271271
// C++ operator). Never fall back to textual resolution.
272+
bool callee_is_locally_bound; // bare call foo() whose callee identifier is bound as a
273+
// parameter of an enclosing function, so it cannot be the
274+
// module-level foo. Python only today. Read by the
275+
// weak-local-binding guard. Default false.
272276
} CBMCall;
273277

274278
typedef struct {
@@ -292,6 +296,10 @@ typedef struct {
292296
uint32_t site_start_byte; // exact reference-token span; end > start when present
293297
uint32_t site_end_byte; // exclusive byte offset in the source file
294298
CBMSourceOrigin source_origin; // raw source or C-family preprocessed buffer
299+
bool is_member_access; // token is the member half of a selector/attribute
300+
// (Go x.f — field_identifier). The extractor strips
301+
// the receiver, so this is the only surviving record
302+
// of selector shape (#1962). Default false.
295303
} CBMUsage;
296304

297305
typedef struct {
@@ -303,6 +311,9 @@ typedef struct {
303311
const char *var_name; // variable name
304312
const char *enclosing_func_qn; // QN of enclosing function
305313
bool is_write; // true = write, false = read
314+
bool is_member_access; // var_name is the field half of a selector/member LHS
315+
// (`t.err = x` → "err"); the receiver is stripped here,
316+
// so this is the only record of selector shape (#1962)
306317
} CBMReadWrite;
307318

308319
typedef struct {
@@ -582,6 +593,13 @@ typedef struct {
582593

583594
typedef struct {
584595
CBMArena *arena;
596+
/* Scratch for AST traversal, owned by the cbm_extract_file_ex call that
597+
* built this context and destroyed when it returns. Nothing a
598+
* CBMFileResult points at may be allocated here: `arena` is the result's
599+
* own, and it outlives extraction by the whole pipeline (#1997). NULL in a
600+
* context built without one, in which case the stacks fall back to
601+
* `arena`. */
602+
CBMArena *scratch;
585603
CBMFileResult *result;
586604
const char *source;
587605
int source_len;

‎internal/cbm/extract_calls.c‎

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
enum { LEAN_MAX_PARENT_DEPTH = 20 };
2222
/* Max positional args to scan for URL/string. */
2323
enum { MAX_POSITIONAL_SCAN = 3 };
24-
/* Max positional args to scan for handler ref. */
25-
enum { MAX_HANDLER_SCAN = 4 };
2624
/* Max string arg length before rejection. */
2725
enum { MAX_STRING_ARG_LEN = CBM_SZ_512 };
2826
/* Min printable ASCII (space). */
@@ -2349,8 +2347,17 @@ static const char *normalize_string_handler(CBMArena *a, const char *raw) {
23492347
}
23502348

23512349
static const char *extract_handler_arg(CBMExtractCtx *ctx, TSNode args) {
2350+
/* The LAST eligible argument wins, and every argument is examined.
2351+
* Express, Fastify, gin and Laravel all put middleware between the route
2352+
* path and the handler, so the first function-shaped argument is usually a
2353+
* middleware. Taking the first one pointed the HANDLES edge at the
2354+
* middleware; stopping the scan early missed the handler outright when the
2355+
* middleware was written inline, because an arrow function matches none of
2356+
* the kinds below. Nothing that follows a handler matches them either — an
2357+
* options argument is an object node — so the last match is the handler. */
2358+
const char *handler = NULL;
23522359
uint32_t nc = ts_node_named_child_count(args);
2353-
for (uint32_t ai = HANDLER_START_IDX; ai < nc && ai < MAX_HANDLER_SCAN; ai++) {
2360+
for (uint32_t ai = HANDLER_START_IDX; ai < nc; ai++) {
23542361
TSNode arg2 = ts_node_named_child(args, ai);
23552362
/* PHP wraps each argument in an `argument` node — unwrap to the value. */
23562363
if (strcmp(ts_node_type(arg2), "argument") == 0 && ts_node_named_child_count(arg2) > 0) {
@@ -2362,17 +2369,18 @@ static const char *extract_handler_arg(CBMExtractCtx *ctx, TSNode args) {
23622369
if (strcmp(ak2, "identifier") == 0 || strcmp(ak2, "member_expression") == 0 ||
23632370
strcmp(ak2, "selector_expression") == 0 || strcmp(ak2, "attribute") == 0 ||
23642371
strcmp(ak2, "field_expression") == 0 || strcmp(ak2, "name") == 0) {
2365-
return cbm_node_text(ctx->arena, arg2, ctx->source);
2372+
handler = cbm_node_text(ctx->arena, arg2, ctx->source);
2373+
continue;
23662374
}
23672375
if (is_string_like(ak2)) {
23682376
const char *h =
23692377
normalize_string_handler(ctx->arena, cbm_node_text(ctx->arena, arg2, ctx->source));
23702378
if (h && h[0]) {
2371-
return h;
2379+
handler = h;
23722380
}
23732381
}
23742382
}
2375-
return NULL;
2383+
return handler;
23762384
}
23772385

23782386
// Extract JSX component refs (uppercase tags) as CALLS edges.
@@ -2965,6 +2973,41 @@ static bool python_receiver_is_exempt(CBMExtractCtx *ctx, TSNode receiver) {
29652973
return false;
29662974
}
29672975

2976+
/* Name bound by one Python parameter node, or NULL when the shape binds none.
2977+
* Covers every binding form a `parameters` / `lambda_parameters` list produces:
2978+
* a bare `identifier`, the `name` field of default/typed parameters, and the
2979+
* identifier under a `*args` / `**kwargs` splat. A shape with no identifier (the
2980+
* bare `*` keyword separator) yields NULL and simply matches nothing. */
2981+
/* True when the callee of a BARE Python call `foo()` is bound as a parameter of
2982+
* an enclosing function or lambda -- the bare-call counterpart of
2983+
* python_receiver_is_exempt above.
2984+
*
2985+
* A parameter binding shadows any module-level `foo` for the whole body, so
2986+
* resolving such a call to a project Function/Method by short name alone
2987+
* fabricates the edge BY CONSTRUCTION: `def _run_with_heavy_slot(run): run()`
2988+
* must not bind an unrelated `SatoriLive.run`. Unlike a receiver type this is
2989+
* decidable from the AST outright, with no flow analysis and no list of
2990+
* "generic-looking" callee names -- Python forbids `global` on a parameter, and
2991+
* a parameter is in scope for the entire body regardless of position.
2992+
*
2993+
* The answer is CARRIED BY THE WALK rather than recomputed here. The unified
2994+
* walk binds a def's or lambda's parameters when it opens that scope and
2995+
* unwinds them when it closes it, so this is an O(1) map lookup. Deciding it
2996+
* by ascending the tree instead -- with ts_node_parent() or with a copied walk
2997+
* cursor -- costs O(depth) per call, and since every level of f(f(f(...))) is
2998+
* itself a bare call, that is quadratic across the file: the 30,000-deep
2999+
* fixture in tests/test_stack_overflow.c turned it into a hang rather than a
3000+
* slowdown. An O(1) lookup needs no hop cap, so unlike a capped walk this can
3001+
* no longer fail open on deep-but-ordinary code.
3002+
*
3003+
* It still answers false if the walk's own tracking hit an allocation failure,
3004+
* which costs a suppression and never a true edge. */
3005+
static bool python_callee_is_bound_parameter(CBMExtractCtx *ctx, WalkState *state,
3006+
TSNode callee_ident) {
3007+
const char *callee_name = cbm_node_text(ctx->arena, callee_ident, ctx->source);
3008+
return cbm_walk_python_param_is_bound(state, callee_name);
3009+
}
3010+
29683011
static bool is_objectscript_language(CBMLanguage language) {
29693012
return language == CBM_LANG_OBJECTSCRIPT_UDL || language == CBM_LANG_OBJECTSCRIPT_ROUTINE;
29703013
}
@@ -3589,11 +3632,18 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
35893632
// (`accelerator.print()` must not bind MockAccelerator.print).
35903633
// Imported receivers stay unflagged: module.function() is Python's
35913634
// canonical cross-file call and the import map resolves it.
3635+
// A BARE Python call foo() whose callee is bound as a parameter of an
3636+
// enclosing scope cannot be the module-level foo, so short-name
3637+
// resolution would fabricate the edge (`def f(run): run()` must not
3638+
// bind SatoriLive.run). Distinct from is_method: there is no receiver
3639+
// here, so the weak-member guard cannot see this class at all.
35923640
if (ctx->language == CBM_LANG_PYTHON && strcmp(ts_node_type(node), "call") == 0) {
35933641
TSNode fn = ts_node_child_by_field_name(node, TS_FIELD("function"));
35943642
if (!ts_node_is_null(fn) && strcmp(ts_node_type(fn), "attribute") == 0) {
35953643
TSNode obj = ts_node_child_by_field_name(fn, TS_FIELD("object"));
35963644
call.is_method = !python_receiver_is_exempt(ctx, obj);
3645+
} else if (!ts_node_is_null(fn) && strcmp(ts_node_type(fn), "identifier") == 0) {
3646+
call.callee_is_locally_bound = python_callee_is_bound_parameter(ctx, state, fn);
35973647
}
35983648
}
35993649
// TS/JS/TSX receiver-aware guard (#592/#606 direction; same intent

0 commit comments

Comments
 (0)