Skip to content

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.

fix(extraction): replace fixed traversal stacks with growable arena-allocated stacks (#217)

Ahmed Mohammedaa6df6b90628
diff --git a/Makefile.cbm b/Makefile.cbm
index 6bc1eb122..0eb8d6e37 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -326,7 +326,9 @@ TEST_YAML_SRCS = tests/test_yaml.c
TEST_SIMHASH_SRCS = tests/test_simhash.c
-ALL_TEST_SRCS = $(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_INTEGRATION_SRCS)
+TEST_STACK_OVERFLOW_SRCS = tests/test_stack_overflow.c
+
+ALL_TEST_SRCS = $(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
# ── Build directories ────────────────────────────────────────────
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index 2bd69d004..ef6bb9baa 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -4,6 +4,7 @@
#include "lang_specs.h"
#include "extract_unified.h"
#include "foundation/constants.h"
+#include "ts_node_stack.h"
#include "tree_sitter/api.h" // TSNode, ts_node_*
#include <stdint.h> // uint32_t
#include <string.h>
@@ -396,14 +397,13 @@ static const char *extract_first_string_arg(CBMExtractCtx *ctx, TSNode args) {
}
// Walk AST for call nodes (iterative)
-#define CALLS_STACK_CAP CBM_SZ_512
static void walk_calls(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
- TSNode stack[CALLS_STACK_CAP];
- int top = 0;
- stack[top++] = root;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, root);
- while (top > 0) {
- TSNode node = stack[--top];
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
const char *kind = ts_node_type(node);
if (cbm_kind_in_set(node, spec->call_node_types)) {
@@ -430,8 +430,8 @@ static void walk_calls(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec)
uint32_t count = ts_node_child_count(node);
enum { LAST_IDX_OFFSET = 1 };
- for (int i = (int)(count - LAST_IDX_OFFSET); i >= 0 && top < CALLS_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)(count - LAST_IDX_OFFSET); i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
diff --git a/internal/cbm/extract_channels.c b/internal/cbm/extract_channels.c
index 61bf1d7bd..5db1396da 100644
--- a/internal/cbm/extract_channels.c
+++ b/internal/cbm/extract_channels.c
@@ -24,14 +24,15 @@
#include "arena.h"
#include "helpers.h"
#include "foundation/constants.h"
+#include "ts_node_stack.h"
#include "tree_sitter/api.h"
#include <stdint.h>
#include <string.h>
enum {
CHAN_CONST_CAP = 256, /* max tracked identifiers per file */
- CHAN_STACK_CAP = 4096, /* tree-walk stack */
CHAN_IDENT_MAX = 128, /* max identifier length tracked */
+ CHAN_STACK_CAP = 4096, /* traversal stack depth per walk */
CHAN_DIR_UNKNOWN = -1, /* unrecognized method → no channel */
};
@@ -93,15 +94,18 @@ static const char *literal_from_first_child(CBMExtractCtx *ctx, TSNode node) {
/* ── Constant resolution table ──────────────────────────────────── */
-/* Walk the whole tree once and collect constant string bindings so later
- * passes can resolve bare-identifier channel arguments. */
+/* Walk the whole tree once and collect `const IDENT = "value"` bindings so
+ * later passes can resolve bare-identifier channel arguments. Only scalar
+ * string literals are tracked — template literals and expressions are left
+ * unresolved. This is a flat lookup; scope boundaries are ignored (a single
+ * const table per file is sufficient for the common Socket.IO pattern). */
static void scan_string_consts_js(CBMExtractCtx *ctx, chan_const_table_t *tbl) {
- TSNode stack[CHAN_STACK_CAP];
- int top = 0;
- stack[top++] = ctx->root;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
- while (top > 0 && tbl->count < CHAN_CONST_CAP) {
- TSNode node = stack[--top];
+ while (stack.count > 0 && tbl->count < CHAN_CONST_CAP) {
+ TSNode node = ts_nstack_pop(&stack);
const char *kind = ts_node_type(node);
if (strcmp(kind, "variable_declarator") == 0) {
@@ -125,8 +129,8 @@ static void scan_string_consts_js(CBMExtractCtx *ctx, chan_const_table_t *tbl) {
}
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
@@ -370,18 +374,19 @@ static void extract_channels_js(CBMExtractCtx *ctx) {
chan_const_table_t consts = {0};
scan_string_consts_js(ctx, &consts);
- TSNode stack[CHAN_STACK_CAP];
- int top = 0;
- stack[top++] = ctx->root;
+ /* Second pass: walk the tree looking for call_expression nodes. */
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
- while (top > 0) {
- TSNode node = stack[--top];
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
if (strcmp(ts_node_type(node), "call_expression") == 0) {
js_process_call(ctx, node, &consts);
}
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index b40d9f551..c9acd6c86 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -3,6 +3,7 @@
#include "helpers.h"
#include "lang_specs.h"
#include "foundation/constants.h"
+#include "ts_node_stack.h"
#include "simhash/minhash.h"
#include "semantic/ast_profile.h"
#include "tree_sitter/api.h" // TSNode, ts_node_*
@@ -2299,15 +2300,14 @@ static TSNode emit_elixir_module_class(CBMExtractCtx *ctx, TSNode cur) {
// Process Elixir call nodes iteratively — handles defmodule/def/defp/defmacro
// without recursion between extract_elixir_call ↔ extract_elixir_module_def.
-#define ELIXIR_STACK_CAP CBM_SZ_64
static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {
(void)spec;
- TSNode stack[ELIXIR_STACK_CAP];
- int top = 0;
- stack[top++] = node;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_64);
+ ts_nstack_push(&stack, ctx->arena, node);
- while (top > 0) {
- TSNode cur = stack[--top];
+ while (stack.count > 0) {
+ TSNode cur = ts_nstack_pop(&stack);
CBMArena *a = ctx->arena;
if (ts_node_child_count(cur) == 0) {
@@ -2329,10 +2329,10 @@ static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
TSNode do_block = emit_elixir_module_class(ctx, cur);
if (!ts_node_is_null(do_block)) {
uint32_t dbc = ts_node_child_count(do_block);
- for (int di = (int)dbc - SKIP_CHAR; di >= 0 && top < ELIXIR_STACK_CAP; di--) {
+ for (int di = (int)dbc - SKIP_CHAR; di >= 0; di--) {
TSNode dchild = ts_node_child(do_block, (uint32_t)di);
if (!ts_node_is_null(dchild) && strcmp(ts_node_type(dchild), "call") == 0) {
- stack[top++] = dchild;
+ ts_nstack_push(&stack, ctx->arena, dchild);
}
}
}
@@ -3051,14 +3051,13 @@ static void extract_var_names(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
// Iterative variable walker for config languages with nested structure.
// Used by YAML, TOML, INI, JSON.
-#define VAR_WALK_STACK_CAP CBM_SZ_256
static void walk_variables_iter(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
- TSNode stack[VAR_WALK_STACK_CAP];
- int top = 0;
- stack[top++] = root;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_256);
+ ts_nstack_push(&stack, ctx->arena, root);
- while (top > 0) {
- TSNode node = stack[--top];
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
uint32_t count = ts_node_child_count(node);
for (int i = (int)count - SKIP_CHAR; i >= 0; i--) {
TSNode child = ts_node_child(node, (uint32_t)i);
@@ -3077,9 +3076,7 @@ static void walk_variables_iter(CBMExtractCtx *ctx, TSNode root, const CBMLangSp
strcmp(ck, "section") == 0 || strcmp(ck, "object") == 0 ||
strcmp(ck, "array") == 0 || strcmp(ck, "pair") == 0 || strcmp(ck, "element") == 0 ||
strcmp(ck, "content") == 0) {
- if (top < VAR_WALK_STACK_CAP) {
- stack[top++] = child;
- }
+ ts_nstack_push(&stack, ctx->arena, child);
}
}
}
@@ -3275,13 +3272,13 @@ typedef struct {
// Push nested class nodes from a class body container onto the defs stack.
// Iteratively walks into wrapper nodes (field_declaration, template_declaration).
static void push_nested_class_nodes(TSNode body, const CBMLangSpec *spec, walk_defs_frame_t *stack,
- int *top, const char *enclosing_qn) {
- TSNode nc_stack[NESTED_CLASS_STACK_CAP];
- int nc_top = 0;
- nc_stack[nc_top++] = body;
+ int *top, const char *enclosing_qn, CBMArena *arena) {
+ TSNodeStack nc_stack;
+ ts_nstack_init(&nc_stack, arena, NESTED_CLASS_STACK_CAP);
+ ts_nstack_push(&nc_stack, arena, body);
- while (nc_top > 0) {
- TSNode cur = nc_stack[--nc_top];
+ while (nc_stack.count > 0) {
+ TSNode cur = ts_nstack_pop(&nc_stack);
uint32_t nc = ts_node_child_count(cur);
for (int i = (int)nc - SKIP_CHAR; i >= 0; i--) {
TSNode child = ts_node_child(cur, (uint32_t)i);
@@ -3293,9 +3290,7 @@ static void push_nested_class_nodes(TSNode body, const CBMLangSpec *spec, walk_d
const char *ck = ts_node_type(child);
if (strcmp(ck, "field_declaration") == 0 ||
strcmp(ck, "template_declaration") == 0 || strcmp(ck, "declaration") == 0) {
- if (nc_top < NESTED_CLASS_STACK_CAP) {
- nc_stack[nc_top++] = child;
- }
+ ts_nstack_push(&nc_stack, arena, child);
}
}
}
@@ -3342,7 +3337,7 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const char
// Push nested class children from a class body container onto the walk stack.
static void push_class_body_children(TSNode node, const CBMLangSpec *spec, walk_defs_frame_t *stack,
- int *top, const char *new_enclosing) {
+ int *top, const char *new_enclosing, CBMArena *arena) {
uint32_t nc = ts_node_child_count(node);
for (uint32_t ci = 0; ci < nc; ci++) {
TSNode child = ts_node_child(node, ci);
@@ -3350,7 +3345,7 @@ static void push_class_body_children(TSNode node, const CBMLangSpec *spec, walk_
if (strcmp(ck, "field_declaration_list") == 0 || strcmp(ck, "class_body") == 0 ||
strcmp(ck, "declaration_list") == 0 || strcmp(ck, "body") == 0 ||
strcmp(ck, "block") == 0 || strcmp(ck, "suite") == 0) {
- push_nested_class_nodes(child, spec, stack, top, new_enclosing);
+ push_nested_class_nodes(child, spec, stack, top, new_enclosing, arena);
return;
}
}
@@ -3394,7 +3389,7 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
if (cbm_kind_in_set(node, spec->class_node_types)) {
extract_class_def(ctx, node, spec);
const char *new_enclosing = compute_class_qn(ctx, node, frame.enclosing_class_qn);
- push_class_body_children(node, spec, stack, &top, new_enclosing);
+ push_class_body_children(node, spec, stack, &top, new_enclosing, ctx->arena);
continue;
}
diff --git a/internal/cbm/extract_env_accesses.c b/internal/cbm/extract_env_accesses.c
index 215b3a004..03e7c0c10 100644
--- a/internal/cbm/extract_env_accesses.c
+++ b/internal/cbm/extract_env_accesses.c
@@ -4,6 +4,7 @@
#include "lang_specs.h"
#include "extract_unified.h"
#include "foundation/constants.h"
+#include "ts_node_stack.h"
#include "tree_sitter/api.h" // TSNode, ts_node_*
#include <stdint.h> // uint32_t
#include <string.h>
@@ -137,14 +138,13 @@ static bool is_env_var_name(const char *s) {
}
// Iterative env access walker — explicit stack
-#define ENV_STACK_CAP 4096
static void walk_env_accesses(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
- TSNode stack[ENV_STACK_CAP];
- int top = 0;
- stack[top++] = root;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, 4096);
+ ts_nstack_push(&stack, ctx->arena, root);
- while (top > 0) {
- TSNode node = stack[--top];
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
const char *kind = ts_node_type(node);
const char *env_key = NULL;
@@ -165,8 +165,8 @@ static void walk_env_accesses(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec
uint32_t count = ts_node_child_count(node);
enum { LAST_IDX = 1 };
- for (int i = (int)count - LAST_IDX; i >= 0 && top < ENV_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - LAST_IDX; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
diff --git a/internal/cbm/extract_imports.c b/internal/cbm/extract_imports.c
index 239a19408..961244180 100644
--- a/internal/cbm/extract_imports.c
+++ b/internal/cbm/extract_imports.c
@@ -3,6 +3,7 @@
#include "helpers.h"
#include "tree_sitter/api.h" // TSNode, ts_node_*
#include "foundation/constants.h"
+#include "ts_node_stack.h"
#include <stdint.h> // uint32_t
#include <string.h>
#include <ctype.h>
@@ -411,14 +412,13 @@ static bool process_commonjs_require(CBMExtractCtx *ctx, TSNode call) {
return true;
}
-#define ES_IMPORT_STACK_CAP CBM_SZ_512
static void walk_es_imports(CBMExtractCtx *ctx, TSNode root) {
- TSNode stack[ES_IMPORT_STACK_CAP];
- int top = 0;
- stack[top++] = root;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, root);
- while (top > 0) {
- TSNode node = stack[--top];
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
const char *kind = ts_node_type(node);
bool push_children = true;
@@ -436,8 +436,8 @@ static void walk_es_imports(CBMExtractCtx *ctx, TSNode root) {
if (push_children) {
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0 && top < ES_IMPORT_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
@@ -820,14 +820,13 @@ static void process_wolfram_needs(CBMExtractCtx *ctx, TSNode node) {
}
}
-#define WOLFRAM_IMPORT_STACK_CAP CBM_SZ_512
static void walk_wolfram_imports(CBMExtractCtx *ctx, TSNode root) {
- TSNode stack[WOLFRAM_IMPORT_STACK_CAP];
- int top = 0;
- stack[top++] = root;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, root);
- while (top > 0) {
- TSNode node = stack[--top];
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
const char *kind = ts_node_type(node);
if (strcmp(kind, "get_top") == 0) {
@@ -837,8 +836,8 @@ static void walk_wolfram_imports(CBMExtractCtx *ctx, TSNode root) {
}
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0 && top < WOLFRAM_IMPORT_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
diff --git a/internal/cbm/extract_semantic.c b/internal/cbm/extract_semantic.c
index 05f4a0e92..17e1ebe47 100644
--- a/internal/cbm/extract_semantic.c
+++ b/internal/cbm/extract_semantic.c
@@ -5,6 +5,7 @@
#include "extract_unified.h"
#include "tree_sitter/api.h" // TSNode, ts_node_*
#include "foundation/constants.h"
+#include "ts_node_stack.h"
enum { MAX_EXCEPTION_NAME_LEN = 100, LAST_IDX = 1 };
#include <stdint.h> // uint32_t
@@ -102,17 +103,16 @@ static void process_throw_node(CBMExtractCtx *ctx, TSNode node, const CBMLangSpe
}
// Iterative throw walker
-#define THROWS_STACK_CAP CBM_SZ_512
static void walk_throws(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
- TSNode stack[THROWS_STACK_CAP];
- int top = 0;
- stack[top++] = root;
- while (top > 0) {
- TSNode node = stack[--top];
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
process_throw_node(ctx, node, spec);
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - LAST_IDX; i >= 0 && top < THROWS_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - LAST_IDX; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
@@ -144,19 +144,18 @@ static void try_emit_assignment_write(CBMExtractCtx *ctx, TSNode node, const cha
}
}
-#define READWRITE_STACK_CAP CBM_SZ_512
static void walk_readwrites(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
- TSNode stack[READWRITE_STACK_CAP];
- int top = 0;
- stack[top++] = root;
- while (top > 0) {
- TSNode node = stack[--top];
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
if (cbm_kind_in_set(node, spec->assignment_node_types)) {
try_emit_assignment_write(ctx, node, cbm_enclosing_func_qn_cached(ctx, node));
}
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - LAST_IDX; i >= 0 && top < READWRITE_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - LAST_IDX; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
diff --git a/internal/cbm/extract_type_assigns.c b/internal/cbm/extract_type_assigns.c
index 74ead85f5..bd7328eda 100644
--- a/internal/cbm/extract_type_assigns.c
+++ b/internal/cbm/extract_type_assigns.c
@@ -5,6 +5,7 @@
#include "extract_unified.h"
#include "tree_sitter/api.h" // TSNode, ts_node_*
#include "foundation/constants.h"
+#include "ts_node_stack.h"
#include <stdint.h> // uint32_t
#include <string.h>
#include <ctype.h>
@@ -164,18 +165,17 @@ static void process_type_assign_node(CBMExtractCtx *ctx, TSNode node, const CBML
}
// Walk AST for assignment patterns where RHS is a constructor call.
-#define TYPE_ASSIGN_STACK_CAP 4096
static void walk_type_assigns(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
- TSNode stack[TYPE_ASSIGN_STACK_CAP];
- int top = 0;
- stack[top++] = root;
- while (top > 0) {
- TSNode node = stack[--top];
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, 4096);
+ ts_nstack_push(&stack, ctx->arena, root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
process_type_assign_node(ctx, node, spec, cbm_enclosing_func_qn_cached(ctx, node));
enum { LAST_IDX = 1 };
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - LAST_IDX; i >= 0 && top < TYPE_ASSIGN_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - LAST_IDX; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
diff --git a/internal/cbm/extract_type_refs.c b/internal/cbm/extract_type_refs.c
index 87b105473..3960d1e1a 100644
--- a/internal/cbm/extract_type_refs.c
+++ b/internal/cbm/extract_type_refs.c
@@ -5,6 +5,7 @@
#include "extract_unified.h"
#include "tree_sitter/api.h" // TSNode, ts_node_*
#include "foundation/constants.h"
+#include "ts_node_stack.h"
enum { MIN_TYPE_LEN = 1 };
@@ -242,17 +243,16 @@ static void process_body_type_ref(CBMExtractCtx *ctx, TSNode node, const char *f
}
// Walk function body for type references (casts, type assertions, local var types, generics).
-#define BODY_TYPE_REF_STACK_CAP 4096
static void walk_body_type_refs(CBMExtractCtx *ctx, TSNode root, const char *func_qn) {
- TSNode stack[BODY_TYPE_REF_STACK_CAP];
- int top = 0;
- stack[top++] = root;
- while (top > 0) {
- TSNode node = stack[--top];
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, 4096);
+ ts_nstack_push(&stack, ctx->arena, root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
process_body_type_ref(ctx, node, func_qn);
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0 && top < BODY_TYPE_REF_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
@@ -283,26 +283,25 @@ static void process_func_type_refs(CBMExtractCtx *ctx, TSNode node) {
}
}
-#define TYPE_REF_STACK_CAP 4096
static void walk_type_refs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
if (!spec->function_node_types || !spec->function_node_types[0]) {
return;
}
- TSNode stack[TYPE_REF_STACK_CAP];
- int top = 0;
- stack[top++] = root;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, 4096);
+ ts_nstack_push(&stack, ctx->arena, root);
- while (top > 0) {
- TSNode node = stack[--top];
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
if (cbm_kind_in_set(node, spec->function_node_types)) {
process_func_type_refs(ctx, node);
continue;
}
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0 && top < TYPE_REF_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
diff --git a/internal/cbm/extract_usages.c b/internal/cbm/extract_usages.c
index 329f32b56..e52345f3c 100644
--- a/internal/cbm/extract_usages.c
+++ b/internal/cbm/extract_usages.c
@@ -4,6 +4,7 @@
#include "extract_unified.h"
#include "tree_sitter/api.h" // TSNode, ts_node_*
#include "foundation/constants.h"
+#include "ts_node_stack.h"
enum { MAX_PARENT_DEPTH = 10, LAST_IDX = 1 };
#include <stdint.h> // uint32_t
@@ -106,18 +107,17 @@ static void try_emit_usage(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
}
// Iterative usage walker — explicit stack
-#define USAGES_STACK_CAP 4096
static void walk_usages(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
- TSNode stack[USAGES_STACK_CAP];
- int top = 0;
- stack[top++] = root;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, 4096);
+ ts_nstack_push(&stack, ctx->arena, root);
- while (top > 0) {
- TSNode node = stack[--top];
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
try_emit_usage(ctx, node, spec);
uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - LAST_IDX; i >= 0 && top < USAGES_STACK_CAP; i--) {
- stack[top++] = ts_node_child(node, (uint32_t)i);
+ for (int i = (int)count - LAST_IDX; i >= 0; i--) {
+ ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
}
}
}
diff --git a/internal/cbm/ts_node_stack.h b/internal/cbm/ts_node_stack.h
new file mode 100644
index 000000000..db6588e6d
--- /dev/null
+++ b/internal/cbm/ts_node_stack.h
@@ -0,0 +1,53 @@
+/*
+ * ts_node_stack.h — Growable TSNode stack for AST traversal.
+ *
+ * Replaces fixed-size TSNode stack[] arrays that silently drop AST subtrees
+ * when the stack overflows (GitHub issue #199).
+ *
+ * Uses the arena allocator for zero-fragmentation growth: old blocks are
+ * abandoned (freed when the arena is destroyed at end of file extraction).
+ * Initial capacity matches the previous fixed caps so small files allocate
+ * no extra memory.
+ */
+#ifndef CBM_TS_NODE_STACK_H
+#define CBM_TS_NODE_STACK_H
+
+#include "arena.h"
+#include "tree_sitter/api.h"
+#include <string.h> /* memcpy */
+
+typedef struct {
+ TSNode *items;
+ int count;
+ int cap;
+} TSNodeStack;
+
+/* Initialize a stack with the given initial capacity, arena-allocated. */
+static inline void ts_nstack_init(TSNodeStack *s, CBMArena *arena, int initial_cap) {
+ s->items = (TSNode *)cbm_arena_alloc(arena, (size_t)initial_cap * sizeof(TSNode));
+ s->count = 0;
+ s->cap = s->items ? initial_cap : 0;
+}
+
+/* Push a node onto the stack, growing 2x if needed. */
+static inline void ts_nstack_push(TSNodeStack *s, CBMArena *arena, TSNode node) {
+ if (s->count >= s->cap) {
+ int new_cap = s->cap ? s->cap * 2 : 512;
+ TSNode *new_items = (TSNode *)cbm_arena_alloc(arena, (size_t)new_cap * sizeof(TSNode));
+ if (!new_items) return; /* OOM: best-effort, stop growing */
+ if (s->items && s->count > 0) {
+ memcpy(new_items, s->items, (size_t)s->count * sizeof(TSNode));
+ }
+ /* Old s->items is abandoned in the arena — freed on arena_destroy. */
+ s->items = new_items;
+ s->cap = new_cap;
+ }
+ s->items[s->count++] = node;
+}
+
+/* Pop a node from the stack. Caller must check s->count > 0. */
+static inline TSNode ts_nstack_pop(TSNodeStack *s) {
+ return s->items[--s->count];
+}
+
+#endif /* CBM_TS_NODE_STACK_H */
diff --git a/tests/test_main.c b/tests/test_main.c
index 6796f8c33..5a9257360 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -57,6 +57,7 @@ extern void suite_yaml(void);
extern void suite_integration(void);
extern void suite_incremental(void);
extern void suite_simhash(void);
+extern void suite_stack_overflow(void);
int main(void) {
printf("\n codebase-memory-mcp C test suite\n");
@@ -154,6 +155,9 @@ int main(void) {
/* SimHash / SIMILAR_TO */
RUN_SUITE(simhash);
+ /* Stack overflow regression (GitHub #199) */
+ RUN_SUITE(stack_overflow);
+
/* Integration (end-to-end) */
RUN_SUITE(integration);
RUN_SUITE(incremental);
diff --git a/tests/test_stack_overflow.c b/tests/test_stack_overflow.c
new file mode 100644
index 000000000..86c1eca2f
--- /dev/null
+++ b/tests/test_stack_overflow.c
@@ -0,0 +1,320 @@
+/*
+ * test_stack_overflow.c — Regression tests for GitHub issue #199.
+ *
+ * Verifies that extraction functions do NOT silently drop AST nodes
+ * when files exceed the fixed traversal stack capacity (512 for calls,
+ * 256 for variables, 64 for Elixir, etc.).
+ *
+ * These tests generate source code with more call sites / definitions /
+ * imports than the stack cap, then assert the extraction count matches
+ * the expected total. Before the fix, counts plateau at the cap.
+ */
+#include "test_framework.h"
+#include "cbm.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* ── Helpers ───────────────────────────────────────────────────── */
+
+static CBMFileResult *extract(const char *src, CBMLanguage lang, const char *proj,
+ const char *path) {
+ CBMFileResult *r = cbm_extract_file(src, (int)strlen(src), lang, proj, path, 0, NULL, NULL);
+ return r;
+}
+
+
+/* ═══════════════════════════════════════════════════════════════════
+ * Test: JavaScript calls exceeding 512 stack cap
+ *
+ * Generates a JS function with 600 unique function calls.
+ * Before fix: walk_calls() stops at ~512 due to CALLS_STACK_CAP.
+ * After fix: all 600 calls are extracted.
+ * ═══════════════════════════════════════════════════════════════════ */
+
+TEST(js_calls_exceed_512) {
+ const int CALL_COUNT = 600;
+ /* Generate calls spread across many small functions to create wide AST.
+ * Each function has ~20 calls, 30 functions = 600 calls total.
+ * The DFS stack must hold sibling function nodes simultaneously. */
+ const int FUNCS = 30;
+ const int CALLS_PER = CALL_COUNT / FUNCS;
+ size_t buf_sz = 256 + (size_t)CALL_COUNT * 48;
+ char *src = malloc(buf_sz);
+ ASSERT_NOT_NULL(src);
+
+ char *p = src;
+ for (int f = 0; f < FUNCS; f++) {
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "function handler_%d() {\n", f);
+ for (int c = 0; c < CALLS_PER; c++) {
+ int idx = f * CALLS_PER + c;
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), " func_%d();\n", idx);
+ }
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "}\n");
+ }
+
+ CBMFileResult *r = extract(src, CBM_LANG_JAVASCRIPT, "test", "many_calls.js");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ /* Count calls that match our generated pattern */
+ int matched = 0;
+ for (int i = 0; i < r->calls.count; i++) {
+ if (strncmp(r->calls.items[i].callee_name, "func_", 5) == 0) {
+ matched++;
+ }
+ }
+
+ printf(" calls extracted: %d / %d expected\n", matched, CALL_COUNT);
+ ASSERT_EQ(matched, CALL_COUNT);
+
+ cbm_free_result(r);
+ free(src);
+ PASS();
+}
+
+/* ═══════════════════════════════════════════════════════════════════
+ * Test: Python calls exceeding 512 stack cap
+ *
+ * Same test in Python syntax to verify language-independence.
+ * ═══════════════════════════════════════════════════════════════════ */
+
+TEST(python_calls_exceed_512) {
+ const int CALL_COUNT = 600;
+ size_t buf_sz = 256 + (size_t)CALL_COUNT * 32;
+ char *src = malloc(buf_sz);
+ ASSERT_NOT_NULL(src);
+
+ char *p = src;
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "def main():\n");
+ for (int i = 0; i < CALL_COUNT; i++) {
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), " func_%d()\n", i);
+ }
+
+ CBMFileResult *r = extract(src, CBM_LANG_PYTHON, "test", "many_calls.py");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ int matched = 0;
+ for (int i = 0; i < r->calls.count; i++) {
+ if (strncmp(r->calls.items[i].callee_name, "func_", 5) == 0) {
+ matched++;
+ }
+ }
+
+ printf(" calls extracted: %d / %d expected\n", matched, CALL_COUNT);
+ ASSERT_EQ(matched, CALL_COUNT);
+
+ cbm_free_result(r);
+ free(src);
+ PASS();
+}
+
+/* ═══════════════════════════════════════════════════════════════════
+ * Test: Go calls at exactly 1024 (well past 512 cap)
+ *
+ * Larger count to ensure the fix handles 2x overflow gracefully.
+ * ═══════════════════════════════════════════════════════════════════ */
+
+TEST(go_calls_exceed_1024) {
+ const int CALL_COUNT = 1024;
+ size_t buf_sz = 256 + (size_t)CALL_COUNT * 40;
+ char *src = malloc(buf_sz);
+ ASSERT_NOT_NULL(src);
+
+ char *p = src;
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "package main\n\nfunc main() {\n");
+ for (int i = 0; i < CALL_COUNT; i++) {
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "\tfunc_%d()\n", i);
+ }
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "}\n");
+
+ CBMFileResult *r = extract(src, CBM_LANG_GO, "test", "many_calls.go");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ int matched = 0;
+ for (int i = 0; i < r->calls.count; i++) {
+ if (strncmp(r->calls.items[i].callee_name, "func_", 5) == 0) {
+ matched++;
+ }
+ }
+
+ printf(" calls extracted: %d / %d expected\n", matched, CALL_COUNT);
+ ASSERT_EQ(matched, CALL_COUNT);
+
+ cbm_free_result(r);
+ free(src);
+ PASS();
+}
+
+/* ═══════════════════════════════════════════════════════════════════
+ * Test: Express-style route file (original reporter's scenario)
+ *
+ * ~150 route definitions — the actual use case from issue #199.
+ * Each route has a handler call, so both defs and calls matter.
+ * ═══════════════════════════════════════════════════════════════════ */
+
+TEST(express_routes_exceed_512) {
+ const int ROUTE_COUNT = 150;
+ /* Each route: app.get('/route_NNN', handler_NNN); */
+ size_t buf_sz = 512 + (size_t)ROUTE_COUNT * 80;
+ char *src = malloc(buf_sz);
+ ASSERT_NOT_NULL(src);
+
+ char *p = src;
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "const express = require('express');\n");
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "const app = express();\n\n");
+ for (int i = 0; i < ROUTE_COUNT; i++) {
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "app.get('/route_%d', handler_%d);\n", i, i);
+ }
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "\napp.listen(3000);\n");
+
+ CBMFileResult *r = extract(src, CBM_LANG_JAVASCRIPT, "test", "routes.js");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ /* Each app.get() is a call — count calls containing "get" */
+ int get_calls = 0;
+ for (int i = 0; i < r->calls.count; i++) {
+ if (strstr(r->calls.items[i].callee_name, "get") != NULL) {
+ get_calls++;
+ }
+ }
+
+ printf(" route calls extracted: %d / %d expected\n", get_calls, ROUTE_COUNT);
+ ASSERT_EQ(get_calls, ROUTE_COUNT);
+
+ cbm_free_result(r);
+ free(src);
+ PASS();
+}
+
+/* ═══════════════════════════════════════════════════════════════════
+ * Test: ES imports exceeding 512 (walk_es_imports uses same cap)
+ *
+ * Generate a TS file with 600 import statements.
+ * ═══════════════════════════════════════════════════════════════════ */
+
+TEST(ts_imports_exceed_512) {
+ const int IMPORT_COUNT = 600;
+ size_t buf_sz = 256 + (size_t)IMPORT_COUNT * 64;
+ char *src = malloc(buf_sz);
+ ASSERT_NOT_NULL(src);
+
+ char *p = src;
+ for (int i = 0; i < IMPORT_COUNT; i++) {
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "import { mod_%d } from './module_%d';\n", i, i);
+ }
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "console.log('done');\n");
+
+ CBMFileResult *r = extract(src, CBM_LANG_TYPESCRIPT, "test", "many_imports.ts");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ printf(" imports extracted: %d / %d expected\n", r->imports.count, IMPORT_COUNT);
+ ASSERT_GTE(r->imports.count, IMPORT_COUNT);
+
+ cbm_free_result(r);
+ free(src);
+ PASS();
+}
+
+/* ═══════════════════════════════════════════════════════════════════
+ * Test: Deeply nested calls (tests stack depth, not just breadth)
+ *
+ * Generates nested function calls: a(b(c(d(e(...)))))
+ * A deep call chain can overflow the stack even with fewer total nodes.
+ * ═══════════════════════════════════════════════════════════════════ */
+
+TEST(js_deeply_nested_calls) {
+ const int DEPTH = 200;
+ /* Build: outermost( level_0( level_1( ... level_199() ... ))) */
+ size_t buf_sz = 256 + (size_t)DEPTH * 32;
+ char *src = malloc(buf_sz);
+ ASSERT_NOT_NULL(src);
+
+ char *p = src;
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "function main() {\n ");
+ for (int i = 0; i < DEPTH; i++) {
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "level_%d(", i);
+ }
+ /* Close all parens */
+ for (int i = 0; i < DEPTH; i++) {
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), ")");
+ }
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), ";\n}\n");
+
+ CBMFileResult *r = extract(src, CBM_LANG_JAVASCRIPT, "test", "nested_calls.js");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ int matched = 0;
+ for (int i = 0; i < r->calls.count; i++) {
+ if (strncmp(r->calls.items[i].callee_name, "level_", 6) == 0) {
+ matched++;
+ }
+ }
+
+ printf(" nested calls extracted: %d / %d expected\n", matched, DEPTH);
+ ASSERT_EQ(matched, DEPTH);
+
+ cbm_free_result(r);
+ free(src);
+ PASS();
+}
+
+/* ═══════════════════════════════════════════════════════════════════
+ * Test: YAML variables exceeding 256 (walk_variables_iter cap)
+ *
+ * Generate a YAML file with 300 top-level keys.
+ * ═══════════════════════════════════════════════════════════════════ */
+
+TEST(yaml_vars_exceed_256) {
+ const int VAR_COUNT = 300;
+ size_t buf_sz = (size_t)VAR_COUNT * 32;
+ char *src = malloc(buf_sz);
+ ASSERT_NOT_NULL(src);
+
+ char *p = src;
+ for (int i = 0; i < VAR_COUNT; i++) {
+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "key_%d: value_%d\n", i, i);
+ }
+
+ CBMFileResult *r = extract(src, CBM_LANG_YAML, "test", "many_keys.yaml");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ int var_count = 0;
+ for (int i = 0; i < r->defs.count; i++) {
+ if (strcmp(r->defs.items[i].label, "Variable") == 0) {
+ var_count++;
+ }
+ }
+
+ printf(" YAML vars extracted: %d / %d expected\n", var_count, VAR_COUNT);
+ ASSERT_EQ(var_count, VAR_COUNT);
+
+ cbm_free_result(r);
+ free(src);
+ PASS();
+}
+
+/* ═══════════════════════════════════════════════════════════════════
+ * Suite registration
+ * ═══════════════════════════════════════════════════════════════════ */
+
+SUITE(stack_overflow) {
+ cbm_init();
+
+ RUN_TEST(js_calls_exceed_512);
+ RUN_TEST(python_calls_exceed_512);
+ RUN_TEST(go_calls_exceed_1024);
+ RUN_TEST(express_routes_exceed_512);
+ RUN_TEST(ts_imports_exceed_512);
+ RUN_TEST(js_deeply_nested_calls);
+ RUN_TEST(yaml_vars_exceed_256);
+
+ cbm_shutdown();
+}