Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(ts): keep uncapped signature walks linear
Martin Vogel060b56a1be59
diff --git a/internal/cbm/lsp/ts_lsp.c b/internal/cbm/lsp/ts_lsp.cindex a5e601872..b7d966c63 100644--- a/internal/cbm/lsp/ts_lsp.c+++ b/internal/cbm/lsp/ts_lsp.c@@ -20,7 +20,7 @@* cbm_batch_ts_lsp_cross.** Conventions (mirrors c_lsp / go_lsp):- * - All allocations go through ctx->arena; no malloc / free.+ * - Persistent results use ctx->arena; temporary walk/index buffers use tracked allocations.* - ts_emit_resolved_call requires a non-NULL enclosing_func_qn — calls outside any* function (module top-level) are not emitted.* - eval_depth caps at TS_LSP_MAX_EVAL_DEPTH to defend against pathological recursion.@@ -29,6 +29,9 @@#include "ts_lsp.h"#include "../../../src/foundation/platform.h"#include "../helpers.h"+#include "../../../src/foundation/log.h"+#include "../../../src/foundation/mem_core.h"+#include <limits.h>#include <stdatomic.h>#include <stdio.h>#include <stdlib.h>@@ -61,6 +64,7 @@ void cbm_ts_full_registry_builds_reset(void) {static _Thread_local long g_ts_type_budget = -1;static _Thread_local bool g_ts_type_budget_warned;#ifdef CBM_ENABLE_TEST_SEAMS+static _Thread_local unsigned long g_ts_signature_lookup_work;static _Thread_local int g_ts_max_member_lookup_steps;static _Thread_local int g_ts_max_method_lookup_steps;#endif@@ -175,6 +179,9 @@ long cbm_ts_lsp_test_budget_remaining(void) {bool cbm_ts_lsp_test_budget_warned(void) {return g_ts_type_budget_warned;}+unsigned long cbm_ts_lsp_test_signature_lookup_work(void) {+ return g_ts_signature_lookup_work;+}int cbm_ts_lsp_test_max_member_lookup_steps(void) {return g_ts_max_member_lookup_steps;}@@ -3957,6 +3964,7 @@ void ts_lsp_init(TSLSPContext *ctx, CBMArena *arena, const char *source, int souif (!ctx)return;#ifdef CBM_ENABLE_TEST_SEAMS+ g_ts_signature_lookup_work = 0;g_ts_max_member_lookup_steps = 0;g_ts_max_method_lookup_steps = 0;#endif@@ -4814,6 +4822,22 @@ static void register_file_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMFileRes// return walk short of returns it had never reached (#2388).enum { TS_WALK_STACK_INLINE = 256 };+static int ts_walk_pending_count(int top, uint32_t count, const char *walker) {+ if (top < 0 || count > (uint32_t)(INT_MAX - top)) {+ char pending[24];+ snprintf(pending, sizeof(pending), "%llu", (unsigned long long)count + (unsigned)top);+ cbm_log_error("extract.walk_stack_alloc_failed", "walker", walker, "pending", pending);+ return -1;+ }+ return top + (int)count;+}++#ifdef CBM_ENABLE_TEST_SEAMS+int cbm_ts_lsp_test_walk_pending_count(int top, uint32_t count) {+ return ts_walk_pending_count(top, count, "ts_lsp_test");+}+#endif+// Push every child of n in cursor order (one O(N) pass). The walk pops the stack,// so children are visited last-child first: the order these walks have always// used. False means the stack could not grow; cbm_walk_stack_reserve has logged it.@@ -4822,8 +4846,9 @@ static bool ts_walk_push_children(TSNode **stack, int *cap, int *top, TSNode n,uint32_t count = ts_node_child_count(n);if (count == 0)return true;- if (!cbm_walk_stack_reserve((void **)stack, cap, *top + (int)count, sizeof(TSNode), inline_buf,- walker))+ int need = ts_walk_pending_count(*top, count, walker);+ if (need < 0 ||+ !cbm_walk_stack_reserve((void **)stack, cap, need, sizeof(TSNode), inline_buf, walker))return false;TSTreeCursor cursor = ts_tree_cursor_new(n);if (ts_tree_cursor_goto_first_child(&cursor)) {@@ -4839,7 +4864,9 @@ static bool ts_walk_push_children(TSNode **stack, int *cap, int *top, TSNode n,static bool ts_walk_push(TSNode **stack, int *cap, int *top, TSNode n, const TSNode *inline_buf,const char *walker) {- if (!cbm_walk_stack_reserve((void **)stack, cap, *top + 1, sizeof(TSNode), inline_buf, walker))+ int need = ts_walk_pending_count(*top, 1, walker);+ if (need < 0 ||+ !cbm_walk_stack_reserve((void **)stack, cap, need, sizeof(TSNode), inline_buf, walker))return false;(*stack)[(*top)++] = n;return true;@@ -4891,6 +4918,89 @@ static const CBMType *infer_return_type_from_body(TSLSPContext *ctx, TSNode bodyreturn found;}+// These two AST passes patch the FIRST matching local registration, even when+// the general registry index prefers the last duplicate. Do not follow fallback+// or copy its entries into the overlay. The snapshot lives only across these+// passes, which mutate signatures but never append or rename registered funcs.+typedef struct {+ CBMRegisteredFunc **slots;+ size_t capacity;+} TSSignatureIndex;++static void ts_signature_work(void) {+#ifdef CBM_ENABLE_TEST_SEAMS+ g_ts_signature_lookup_work++;+#endif+}++static size_t ts_signature_slot(const TSSignatureIndex *index, const char *qn) {+ uint64_t hash = 0xcbf29ce484222325ULL;+ for (const unsigned char *p = (const unsigned char *)qn; *p; p++) {+ hash ^= *p;+ hash *= 0x100000001b3ULL;+ }+ size_t slot = (size_t)hash & (index->capacity - 1);+ for (;;) {+ ts_signature_work();+ if (!index->slots[slot] || strcmp(index->slots[slot]->qualified_name, qn) == 0)+ return slot;+ slot = (slot + 1) & (index->capacity - 1);+ }+}++static TSSignatureIndex ts_signature_index(CBMTypeRegistry *reg) {+ TSSignatureIndex index = {0};+ if (reg->func_count <= 0 || (size_t)reg->func_count > SIZE_MAX / 2)+ return index;+ size_t capacity = 16;+ while (capacity < (size_t)reg->func_count * 2) {+ if (capacity > SIZE_MAX / 2)+ return index;+ capacity *= 2;+ }+ if (capacity > SIZE_MAX / sizeof(*index.slots))+ return index;+ index.slots = cbm_alloc(CBM_MEM_CLASS_EXTRACT, capacity * sizeof(*index.slots));+ if (!index.slots)+ return index; // Preserve the original lookup on allocation failure.+ index.capacity = capacity;+ memset(index.slots, 0, capacity * sizeof(*index.slots));+ for (int i = 0; i < reg->func_count; i++) {+ ts_signature_work();+ CBMRegisteredFunc *func = ®->funcs[i];+ if (!func->qualified_name)+ continue;+ size_t slot = ts_signature_slot(&index, func->qualified_name);+ if (!index.slots[slot])+ index.slots[slot] = func;+ }+ return index;+}++static CBMRegisteredFunc *ts_signature_func(CBMTypeRegistry *reg, const TSSignatureIndex *index,+ const char *qn) {+ if (!qn)+ return NULL;+ if (index->slots)+ return index->slots[ts_signature_slot(index, qn)];+ for (int i = 0; i < reg->func_count; i++) {+ ts_signature_work();+ if (reg->funcs[i].qualified_name && strcmp(reg->funcs[i].qualified_name, qn) == 0)+ return ®->funcs[i];+ }+ return NULL;+}++#ifdef CBM_ENABLE_TEST_SEAMS+int cbm_ts_lsp_test_signature_func_index(CBMTypeRegistry *reg, const char *qn) {+ TSSignatureIndex index = ts_signature_index(reg);+ CBMRegisteredFunc *func = ts_signature_func(reg, &index, qn);+ int result = func ? (int)(func - reg->funcs) : -1;+ cbm_free(CBM_MEM_CLASS_EXTRACT, index.slots);+ return result;+}+#endif+// Rebuild a function's CBMRegisteredFunc signature from the AST directly. extract_defs// only handles type_identifier / generic_type / predefined_type for params, so function// types (`(x: Foo) => void`), object types, tuple types etc. are missing from@@ -4898,7 +5008,8 @@ static const CBMType *infer_return_type_from_body(TSLSPContext *ctx, TSNode body//// Called after `register_file_defs` so the func entries already exist; we only mutate// in place.-static void rebuild_signatures_from_ast(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *reg) {+static void rebuild_signatures_from_ast(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *reg,+ const TSSignatureIndex *index) {if (ts_node_is_null(root) || !reg || !ctx->module_qn)return;@@ -5039,23 +5150,17 @@ static void rebuild_signatures_from_ast(TSLSPContext *ctx, TSNode root, CBMTypeR// Build new signature and patch in registry.const CBMType *rets[2] = {ret, NULL};const CBMType *new_sig = cbm_type_func(ctx->arena, p_names, p_types, rets);- for (int fi = 0; fi < reg->func_count; fi++) {- if (!reg->funcs[fi].qualified_name)- continue;- if (strcmp(reg->funcs[fi].qualified_name, fqn) != 0)- continue;+ CBMRegisteredFunc *func = ts_signature_func(reg, index, fqn);+ if (func) {// Preserve the ret type if AST gave UNKNOWN but the existing reg sig had one.- if (cbm_type_is_unknown(ret) && reg->funcs[fi].signature &&- reg->funcs[fi].signature->kind == CBM_TYPE_FUNC &&- reg->funcs[fi].signature->data.func.return_types &&- reg->funcs[fi].signature->data.func.return_types[0] &&- !cbm_type_is_unknown(reg->funcs[fi].signature->data.func.return_types[0])) {- const CBMType *preserved[2] = {reg->funcs[fi].signature->data.func.return_types[0],- NULL};+ if (cbm_type_is_unknown(ret) && func->signature &&+ func->signature->kind == CBM_TYPE_FUNC && func->signature->data.func.return_types &&+ func->signature->data.func.return_types[0] &&+ !cbm_type_is_unknown(func->signature->data.func.return_types[0])) {+ const CBMType *preserved[2] = {func->signature->data.func.return_types[0], NULL};new_sig = cbm_type_func(ctx->arena, p_names, p_types, preserved);}- reg->funcs[fi].signature = new_sig;- break;+ func->signature = new_sig;}}cbm_walk_stack_release(stack, inline_stack);@@ -5064,7 +5169,8 @@ static void rebuild_signatures_from_ast(TSLSPContext *ctx, TSNode root, CBMTypeR// Convert NAMED references to TYPE_PARAM for any function whose declaration has a// `type_parameters` block. Without this, generic functions like `function id<T>(x: T): T`// register T as NAMED("module.T") instead of TYPE_PARAM("T"), defeating substitution.-static void convert_signature_type_params(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *reg) {+static void convert_signature_type_params(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *reg,+ const TSSignatureIndex *index) {if (ts_node_is_null(root) || !reg || !ctx->module_qn)return;@@ -5171,17 +5277,14 @@ static void convert_signature_type_params(TSLSPContext *ctx, TSNode root, CBMTyp}// Patch the registered func's signature: rewrite NAMED → TYPE_PARAM.- for (int fi = 0; fi < reg->func_count; fi++) {- if (!reg->funcs[fi].qualified_name)- continue;- if (strcmp(reg->funcs[fi].qualified_name, fqn) != 0)- continue;- const CBMType *old_sig = reg->funcs[fi].signature;+ CBMRegisteredFunc *func = ts_signature_func(reg, index, fqn);+ if (func) {+ const CBMType *old_sig = func->signature;if (!old_sig)- break;+ continue;const CBMType *new_sig = cbm_type_substitute(ctx->arena, old_sig, names, args);if (new_sig)- reg->funcs[fi].signature = new_sig;+ func->signature = new_sig;// Also store the type_param_names for later overload-by-types use.const char **stored = (const char **)cbm_arena_alloc(ctx->arena, (size_t)(tpc + 1) * sizeof(const char *));@@ -5189,14 +5292,22 @@ static void convert_signature_type_params(TSLSPContext *ctx, TSNode root, CBMTypfor (int ti = 0; ti < tpc; ti++)stored[ti] = names[ti];stored[tpc] = NULL;- reg->funcs[fi].type_param_names = stored;+ func->type_param_names = stored;}- break;}}cbm_walk_stack_release(stack, inline_stack);}+// Build once after shape registration, then release before later passes may+// grow the registry. This keeps per-file lookup storage out of the result arena.+static void refine_ast_signatures(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *reg) {+ TSSignatureIndex index = ts_signature_index(reg);+ rebuild_signatures_from_ast(ctx, root, reg, &index);+ convert_signature_type_params(ctx, root, reg, &index);+ cbm_free(CBM_MEM_CLASS_EXTRACT, index.slots);+}+// Parse a JSDoc comment block for `@param {T} name` and `@returns {T}` tags. For each// match, returns the type as a parsed CBMType. Used in `js_mode` to infer signatures// for JavaScript files where there are no inline type annotations.@@ -5839,8 +5950,7 @@ void cbm_run_ts_lsp(CBMArena *arena, CBMFileResult *result, const char *source,}ast_sweep_shapes(&ctx, root, ®);- rebuild_signatures_from_ast(&ctx, root, ®);- convert_signature_type_params(&ctx, root, ®);+ refine_ast_signatures(&ctx, root, ®);apply_jsdoc_signatures(&ctx, root, ®);infer_implicit_returns(&ctx, root, ®);@@ -6093,8 +6203,7 @@ void cbm_run_ts_lsp_cross_with_registry(CBMArena *arena, const char *source, int}ast_sweep_shapes(&ctx, root, &overlay);- rebuild_signatures_from_ast(&ctx, root, &overlay);- convert_signature_type_params(&ctx, root, &overlay);+ refine_ast_signatures(&ctx, root, &overlay);apply_jsdoc_signatures(&ctx, root, &overlay);infer_implicit_returns(&ctx, root, &overlay);@@ -6309,8 +6418,7 @@ void cbm_run_ts_lsp_cross(CBMArena *arena, const char *source, int source_len,}ast_sweep_shapes(&ctx, root, ®);- rebuild_signatures_from_ast(&ctx, root, ®);- convert_signature_type_params(&ctx, root, ®);+ refine_ast_signatures(&ctx, root, ®);apply_jsdoc_signatures(&ctx, root, ®);infer_implicit_returns(&ctx, root, ®);diff --git a/internal/cbm/lsp/ts_lsp.h b/internal/cbm/lsp/ts_lsp.hindex 1096bea90..fc67895b3 100644--- a/internal/cbm/lsp/ts_lsp.h+++ b/internal/cbm/lsp/ts_lsp.h@@ -72,6 +72,11 @@ typedef struct {// for the calling thread, valid after a cbm_run_ts_lsp on the same thread.long cbm_ts_lsp_test_budget_remaining(void);bool cbm_ts_lsp_test_budget_warned(void);+// Registry entries/probes visited by the two AST signature passes in the last run.+unsigned long cbm_ts_lsp_test_signature_lookup_work(void);+// Inspect signature-index selection and checked walker addition without huge ASTs.+int cbm_ts_lsp_test_signature_func_index(CBMTypeRegistry *reg, const char *qn);+int cbm_ts_lsp_test_walk_pending_count(int top, uint32_t count);// Maximum recursive calls in one member/method lookup during the last TS LSP run,// including attempts rejected by the depth or visited-type guard.int cbm_ts_lsp_test_max_member_lookup_steps(void);diff --git a/tests/test_ts_lsp.c b/tests/test_ts_lsp.cindex 04f8875a0..b21e946d0 100644--- a/tests/test_ts_lsp.c+++ b/tests/test_ts_lsp.c@@ -26,6 +26,7 @@#include "../src/foundation/compat.h"#include "../src/pipeline/lsp_resolve.h"#include "lsp/ts_lsp.h"+#include <limits.h>/* ── Helpers ───────────────────────────────────────────────────────────────── */@@ -3448,6 +3449,66 @@ TEST(tslsp_stress_many_classes) {PASS();}+TEST(tslsp_signature_walk_keeps_first_local_registration) {+ CBMRegisteredFunc base_funcs[] = {{.qualified_name = "test.base.only"}};+ CBMTypeRegistry base = {.funcs = base_funcs, .func_count = 1, .read_only = true};+ CBMRegisteredFunc funcs[] = {+ {.qualified_name = NULL},+ {.qualified_name = "test.main.identity"},+ {.qualified_name = "test.main.identity"},+ {.qualified_name = "test.main.Box.identity"},+ };+ CBMTypeRegistry overlay = {.funcs = funcs, .func_count = 4, .fallback = &base};+ ASSERT_EQ(cbm_ts_lsp_test_signature_func_index(&overlay, "test.main.identity"), 1);+ ASSERT_EQ(cbm_ts_lsp_test_signature_func_index(&overlay, "test.main.Box.identity"), 3);+ ASSERT_EQ(cbm_ts_lsp_test_signature_func_index(&overlay, "test.base.only"), -1);+ ASSERT_EQ(cbm_ts_lsp_test_signature_func_index(&overlay, "test.missing"), -1);+ ASSERT_EQ(overlay.func_count, 4);+ ASSERT_EQ(base.func_count, 1);+ PASS();+}++TEST(tslsp_walk_pending_count_rejects_overflow) {+ ASSERT_EQ(cbm_ts_lsp_test_walk_pending_count(255, 300), 555);+ ASSERT_EQ(cbm_ts_lsp_test_walk_pending_count(INT_MAX - 1, 1), INT_MAX);+ ASSERT_EQ(cbm_ts_lsp_test_walk_pending_count(INT_MAX, 1), -1);+ ASSERT_EQ(cbm_ts_lsp_test_walk_pending_count(1, (uint32_t)INT_MAX), -1);+ ASSERT_EQ(cbm_ts_lsp_test_walk_pending_count(0, UINT32_MAX), -1);+ PASS();+}++/* Count actual registry work, independent of machine speed. Both inputs exceed+ * the old walk cap, and every declaration exercises rebuild and generic rewrite. */+TEST(tslsp_signature_walk_lookup_scales_linearly) {+ unsigned long work[2] = {0};+ for (int scale = 0; scale < 2; scale++) {+ const int count = 300 << scale;+ const size_t capacity = (size_t)count * 160 + 512;+ char *source = (char *)malloc(capacity);+ ASSERT_NOT_NULL(source);+ size_t used = 0;+ for (int i = 0; i < count; i++) {+ used += (size_t)snprintf(source + used, capacity - used,+ "export function identity%d<T>(x: T): T { return x; }\n", i);+ }+ snprintf(source + used, capacity - used,+ "class Box { use(): void {} }\n"+ "function go() { const b = identity%d(new Box()); b.use(); }\n",+ count - 1);+ CBMFileResult *r = extract_ts(source);+ free(source);+ ASSERT_NOT_NULL(r);+ work[scale] = cbm_ts_lsp_test_signature_lookup_work();+ ASSERT_GTE(require_resolved(r, ".go", "use"), 0);+ cbm_free_result(r);+ }+ printf(" signature lookup work: 300=%lu 600=%lu\n", work[0], work[1]);+ ASSERT_GT(work[0], 0);+ /* Allow 25% above ideal doubling for hash-bucket distribution. */+ ASSERT_LT(work[1], work[0] * 5 / 2);+ PASS();+}+TEST(tslsp_stress_declarations_past_stack_cap) {/* 300 padding functions, then the generic pair that needs the AST walk.* The walk used to hold 256 nodes at most, so every top-level declaration@@ -5261,6 +5322,9 @@ SUITE(ts_lsp) {/* Stress tests */RUN_TEST(tslsp_stress_many_classes);+ RUN_TEST(tslsp_signature_walk_keeps_first_local_registration);+ RUN_TEST(tslsp_walk_pending_count_rejects_overflow);+ RUN_TEST(tslsp_signature_walk_lookup_scales_linearly);RUN_TEST(tslsp_stress_declarations_past_stack_cap);RUN_TEST(tslsp_stress_return_past_stack_cap);RUN_TEST(tslsp_stress_deep_inheritance);