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(pipeline): size definition properties for every field

Martin Vogel5992ea61a332
diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c
index 0f95c7989..62af154be 100644
--- a/src/pipeline/pass_definitions.c
+++ b/src/pipeline/pass_definitions.c
@@ -309,8 +309,8 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
}
size_t pos = (size_t)n;
append_json_string(buf, bufsize, &pos, "docstring", def->docstring);
- /* Right after the docstring: the buffer is sized for exactly these three
- * uncapped fields (pd_props_buf), so none can be squeezed out. */
+ /* The buffer is sized for every field below (pd_props_buf), so none can be
+ * squeezed out. */
append_json_raw(buf, bufsize, &pos, "variants", def->variants);
append_json_string(buf, bufsize, &pos, "variant_key", def->variant_key);
append_json_string(buf, bufsize, &pos, "signature", def->signature);
@@ -353,24 +353,71 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
}
}
-/* A def's properties buffer: CBM_SZ_2K for every other field plus the whole
- * serialized docstring, variants and variant_key fields, which have no length
- * cap (a field that does not fit is dropped whole). Returns `stack` for a def
- * with none of them, or when the larger buffer cannot be allocated. Twin of
- * pass_parallel.c -- keep both in sync. */
-static char *pd_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
- size_t need = *size;
- if (def->docstring && def->docstring[0]) {
- need += strlen("docstring") + def_json_escaped_len(def->docstring) + PD_JSON_FIELD_OVERHEAD;
+/* Upper bound of build_def_props' fixed prefix: the Function/Method metrics
+ * literal (about 330 B) with nine ints and seven true/false/test_role strings at
+ * their widest stays under 600 B. */
+enum { PD_PROPS_PREFIX_MAX = CBM_SZ_1K };
+
+/* Serialized size of one optional field as append_json_string emits it
+ * (0 when the field is absent). */
+static size_t pd_str_field_len(const char *key, const char *val) {
+ return val && val[0] ? strlen(key) + def_json_escaped_len(val) + PD_JSON_FIELD_OVERHEAD : 0;
+}
+
+/* Serialized size of a string array as append_json_str_array emits it. */
+static size_t pd_arr_field_len(const char *key, const char **arr) {
+ if (!arr || !arr[0]) {
+ return 0;
+ }
+ size_t n = strlen(key) + PD_JSON_FIELD_OVERHEAD;
+ for (int i = 0; arr[i]; i++) {
+ n += def_json_escaped_len(arr[i]) + PD_ESC_SPACE + (i > 0 ? SKIP_ONE : 0);
}
+ return n;
+}
+
+/* A def's properties buffer, sized so EVERY field build_def_props appends fits:
+ * the fixed metrics prefix (PD_PROPS_PREFIX_MAX) plus each optional field's
+ * whole serialized form. A field that does not fit is dropped whole, so a buffer
+ * sized for only some of them let a long signature, decorator list or parameter
+ * list squeeze out the MinHash fingerprint ("fp"), structural profile ("sp") and
+ * body tokens ("bt") behind them: no SIMILAR_TO edge and fewer
+ * SEMANTICALLY_RELATED edges for exactly the widest definitions (jellyfin's
+ * ImageController actions, 2026-10-18). fp/sp/bt are appended behind their own
+ * room checks, so both the check's reserve and the field are counted. Returns
+ * `stack` when everything fits it, or when the larger buffer cannot be
+ * allocated. Twin of pass_parallel.c -- keep both in sync. */
+static char *pd_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
+ size_t need = PD_PROPS_PREFIX_MAX + pd_str_field_len("docstring", def->docstring);
if (def->variants && def->variants[0]) {
need += strlen("variants") + strlen(def->variants) + PD_JSON_FIELD_OVERHEAD;
}
- if (def->variant_key && def->variant_key[0]) {
- need +=
- strlen("variant_key") + def_json_escaped_len(def->variant_key) + PD_JSON_FIELD_OVERHEAD;
+ need += pd_str_field_len("variant_key", def->variant_key) +
+ pd_str_field_len("signature", def->signature) +
+ pd_str_field_len("return_type", def->return_type) +
+ pd_str_field_len("parent_class", def->parent_class) +
+ pd_arr_field_len("decorators", def->decorators) +
+ pd_arr_field_len("base_classes", def->base_classes) +
+ pd_arr_field_len("param_names", def->param_names) +
+ pd_arr_field_len("param_types", def->param_types) +
+ pd_str_field_len("route_path", def->route_path) +
+ pd_str_field_len("route_method", def->route_method) +
+ pd_str_field_len("http_client", def->http_client) +
+ pd_str_field_len("http_base_url", def->http_base_url);
+ for (const char **kv = def->extra_props; kv && kv[0] && kv[SKIP_ONE]; kv += PAIR_LEN) {
+ need += pd_str_field_len(kv[0], kv[SKIP_ONE]);
+ }
+ if (def->fingerprint && def->fingerprint_k > 0) {
+ need += CBM_MINHASH_HEX_LEN + CBM_MINHASH_JSON_OVERHEAD + SKIP_ONE;
+ }
+ if (def->structural_profile) {
+ need += CBM_AST_PROFILE_BUF + pd_str_field_len("sp", def->structural_profile) + SKIP_ONE;
+ }
+ if (def->body_tokens) {
+ need += CBM_SZ_512 + pd_str_field_len("bt", def->body_tokens) + SKIP_ONE;
}
- if (need == *size) {
+ need += PD_ESC_SPACE; /* the closing '}' + NUL */
+ if (need <= *size) {
return stack;
}
char *buf = cbm_alloc(CBM_MEM_CLASS_GBUF_STRING, need);
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 2c0a9023a..ac2632ecf 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -539,8 +539,8 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
}
size_t pos = (size_t)n;
append_json_string(buf, bufsize, &pos, "docstring", def->docstring);
- /* Right after the docstring: the buffer is sized for exactly these three
- * uncapped fields (pp_props_buf), so none can be squeezed out. */
+ /* The buffer is sized for every field below (pp_props_buf), so none can be
+ * squeezed out. */
append_json_raw(buf, bufsize, &pos, "variants", def->variants);
append_json_string(buf, bufsize, &pos, "variant_key", def->variant_key);
append_json_string(buf, bufsize, &pos, "signature", def->signature);
@@ -731,24 +731,71 @@ typedef struct {
* to the response/logfile — this only throttles the stderr noise). */
enum { PP_OVERSIZED_WARN_MAX = 32 };
-/* A def's properties buffer: CBM_SZ_2K for every other field plus the whole
- * serialized docstring, variants and variant_key fields, which have no length
- * cap (a field that does not fit is dropped whole). Returns `stack` for a def
- * with none of them, or when the larger buffer cannot be allocated. Twin of
- * pass_definitions.c -- keep both in sync. */
-static char *pp_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
- size_t need = *size;
- if (def->docstring && def->docstring[0]) {
- need += strlen("docstring") + pp_json_escaped_len(def->docstring) + PP_JSON_FIELD_OVERHEAD;
+/* Upper bound of build_def_props' fixed prefix: the Function/Method metrics
+ * literal (about 330 B) with nine ints and seven true/false/test_role strings at
+ * their widest stays under 600 B. */
+enum { PP_PROPS_PREFIX_MAX = CBM_SZ_1K };
+
+/* Serialized size of one optional field as append_json_string emits it
+ * (0 when the field is absent). */
+static size_t pp_str_field_len(const char *key, const char *val) {
+ return val && val[0] ? strlen(key) + pp_json_escaped_len(val) + PP_JSON_FIELD_OVERHEAD : 0;
+}
+
+/* Serialized size of a string array as append_json_str_array emits it. */
+static size_t pp_arr_field_len(const char *key, const char **arr) {
+ if (!arr || !arr[0]) {
+ return 0;
+ }
+ size_t n = strlen(key) + PP_JSON_FIELD_OVERHEAD;
+ for (int i = 0; arr[i]; i++) {
+ n += pp_json_escaped_len(arr[i]) + PP_ESC_SPACE + (i > 0 ? SKIP_ONE : 0);
}
+ return n;
+}
+
+/* A def's properties buffer, sized so EVERY field build_def_props appends fits:
+ * the fixed metrics prefix (PP_PROPS_PREFIX_MAX) plus each optional field's
+ * whole serialized form. A field that does not fit is dropped whole, so a buffer
+ * sized for only some of them let a long signature, decorator list or parameter
+ * list squeeze out the MinHash fingerprint ("fp"), structural profile ("sp") and
+ * body tokens ("bt") behind them: no SIMILAR_TO edge and fewer
+ * SEMANTICALLY_RELATED edges for exactly the widest definitions (jellyfin's
+ * ImageController actions, 2026-10-18). fp/sp/bt are appended behind their own
+ * room checks, so both the check's reserve and the field are counted. Returns
+ * `stack` when everything fits it, or when the larger buffer cannot be
+ * allocated. Twin of pass_definitions.c -- keep both in sync. */
+static char *pp_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
+ size_t need = PP_PROPS_PREFIX_MAX + pp_str_field_len("docstring", def->docstring);
if (def->variants && def->variants[0]) {
need += strlen("variants") + strlen(def->variants) + PP_JSON_FIELD_OVERHEAD;
}
- if (def->variant_key && def->variant_key[0]) {
- need +=
- strlen("variant_key") + pp_json_escaped_len(def->variant_key) + PP_JSON_FIELD_OVERHEAD;
+ need += pp_str_field_len("variant_key", def->variant_key) +
+ pp_str_field_len("signature", def->signature) +
+ pp_str_field_len("return_type", def->return_type) +
+ pp_str_field_len("parent_class", def->parent_class) +
+ pp_arr_field_len("decorators", def->decorators) +
+ pp_arr_field_len("base_classes", def->base_classes) +
+ pp_arr_field_len("param_names", def->param_names) +
+ pp_arr_field_len("param_types", def->param_types) +
+ pp_str_field_len("route_path", def->route_path) +
+ pp_str_field_len("route_method", def->route_method) +
+ pp_str_field_len("http_client", def->http_client) +
+ pp_str_field_len("http_base_url", def->http_base_url);
+ for (const char **kv = def->extra_props; kv && kv[0] && kv[SKIP_ONE]; kv += PAIR_LEN) {
+ need += pp_str_field_len(kv[0], kv[SKIP_ONE]);
+ }
+ if (def->fingerprint && def->fingerprint_k > 0) {
+ need += CBM_MINHASH_HEX_LEN + CBM_MINHASH_JSON_OVERHEAD + SKIP_ONE;
+ }
+ if (def->structural_profile) {
+ need += CBM_AST_PROFILE_BUF + pp_str_field_len("sp", def->structural_profile) + SKIP_ONE;
+ }
+ if (def->body_tokens) {
+ need += CBM_SZ_512 + pp_str_field_len("bt", def->body_tokens) + SKIP_ONE;
}
- if (need == *size) {
+ need += PP_ESC_SPACE; /* the closing '}' + NUL */
+ if (need <= *size) {
return stack;
}
char *buf = cbm_alloc(CBM_MEM_CLASS_GBUF_STRING, need);
diff --git a/tests/test_parallel.c b/tests/test_parallel.c
index a8c0618df..fc97332e6 100644
--- a/tests/test_parallel.c
+++ b/tests/test_parallel.c
@@ -847,6 +847,91 @@ TEST(parallel_args_json_no_overflow) {
PASS();
}
+/* F14: a definition's properties buffer was sized for its docstring and variants
+ * only. A wide signature (50 annotated parameters) filled the rest of the 2 KB, and
+ * the MinHash fingerprint "fp", structural profile "sp" and body tokens "bt"
+ * appended behind it were dropped whole: no SIMILAR_TO edge and fewer
+ * SEMANTICALLY_RELATED edges for exactly the widest definitions (jellyfin's
+ * ImageController actions, 2026-10-18). `small` has the same body and is the
+ * control that the fixture produces all three at all. Both resolvers. */
+static const char f14_body[] =
+ " total = 0\n"
+ " for item in range(10):\n"
+ " if item % 3 == 0:\n"
+ " total += item * 2\n"
+ " elif item % 5 == 1:\n"
+ " total -= len(str(item))\n"
+ " else:\n"
+ " total ^= hash((item, total))\n"
+ " values = [x * x for x in range(total % 7 + 3)]\n"
+ " mapping = {str(k): v for k, v in enumerate(values)}\n"
+ " while total > 100:\n"
+ " total //= 3\n"
+ " try:\n"
+ " result = sorted(mapping.items(), key=lambda kv: kv[1])\n"
+ " except ValueError as err:\n"
+ " result = [(\"error\", str(err))]\n"
+ " return result, total, mapping\n";
+
+static bool f14_props_have(const cbm_gbuf_t *gb, const char *project, const char *name,
+ const char *venue) {
+ char *qn = cbm_pipeline_fqn_compute(project, "pkg/wide.py", name);
+ const cbm_gbuf_node_t *n = qn ? cbm_gbuf_find_by_qn(gb, qn) : NULL;
+ free(qn);
+ const char *p = n && n->properties_json ? n->properties_json : "";
+ bool fp = strstr(p, "\"fp\":\"") != NULL;
+ bool sp = strstr(p, "\"sp\":\"") != NULL;
+ bool bt = strstr(p, "\"bt\":\"") != NULL;
+ bool sig = strstr(p, "\"signature\":\"") != NULL;
+ printf(" f14 %s %s: found=%d len=%zu fp=%d sp=%d bt=%d signature=%d\n", venue, name, n != NULL,
+ strlen(p), fp, sp, bt, sig);
+ return n && fp && sp && bt && sig;
+}
+
+TEST(parallel_wide_definition_keeps_fp_sp_bt_f14) {
+ char params[CBM_SZ_4K];
+ size_t pos = 0;
+ for (int i = 0; i < 50; i++) {
+ pos += (size_t)snprintf(params + pos, sizeof(params) - pos, "%sp%02d: Dict[str, int] = None",
+ i ? ", " : "", i);
+ }
+ char src[CBM_SZ_8K];
+ snprintf(src, sizeof(src), "def small(a, b):\n%s\n\ndef wide(%s):\n%s", f14_body, params,
+ f14_body);
+ const char *project = "cbm_f14";
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_par_f14_XXXXXX");
+ if (!cbm_mkdtemp(tmpdir)) {
+ FAIL("mkdtemp failed");
+ }
+ char path[512];
+ snprintf(path, sizeof(path), "%s/pkg/wide.py", tmpdir);
+ if (th_write_file(path, src) != 0) {
+ th_rmtree(tmpdir);
+ FAIL("failed to write the F14 fixture");
+ }
+ cbm_file_info_t files[1] = {0};
+ files[0].path = path;
+ files[0].rel_path = (char *)"pkg/wide.py";
+ files[0].language = CBM_LANG_PYTHON;
+ cbm_gbuf_t *graphs[2] = {run_sequential(project, tmpdir, files, 1),
+ run_parallel(project, tmpdir, files, 1, 2)};
+ ASSERT_NOT_NULL(graphs[0]);
+ ASSERT_NOT_NULL(graphs[1]);
+ bool all = true;
+ for (int g = 0; g < 2; g++) {
+ const char *venue = g == 0 ? "sequential" : "parallel";
+ bool control = f14_props_have(graphs[g], project, "small", venue);
+ bool wide = f14_props_have(graphs[g], project, "wide", venue);
+ all = all && control && wide;
+ }
+ cbm_gbuf_free(graphs[0]);
+ cbm_gbuf_free(graphs[1]);
+ th_rmtree(tmpdir);
+ ASSERT_TRUE(all);
+ PASS();
+}
+
/* ── Graph buffer merge tests ─────────────────────────────────────── */
TEST(gbuf_shared_ids_unique) {
@@ -5346,6 +5431,7 @@ SUITE(parallel) {
RUN_TEST(parallel_post_extract_projection_spills_before_resolve);
RUN_TEST(parallel_empty_files);
RUN_TEST(parallel_args_json_no_overflow);
+ RUN_TEST(parallel_wide_definition_keeps_fp_sp_bt_f14);
/* Cleanup shared state */
parity_teardown();