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.

chore(pipeline): integrate upstream Python lexical resolution

Pcristin4aaa8b2ec55f
diff --cc src/pipeline/pass_calls.c
index 4becb5f95,dfbb8f7fb..79d82e044
--- a/src/pipeline/pass_calls.c
+++ b/src/pipeline/pass_calls.c
@@@ -591,11 -585,20 +591,20 @@@ static bool handle_route_registration(c
snprintf(props, sizeof(props),
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"route_registration\"}", esc_cn,
esc_fa);
- cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, route_id, "CALLS", props);
+ bool emitted = cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, route_id, "CALLS", props) > 0;
if (call->second_arg_name != NULL && call->second_arg_name[0] != '\0') {
- cbm_resolution_t hres =
- cbm_registry_resolve_handler(ctx->registry, call->second_arg_name, module_qn, imp_keys,
- imp_vals, imp_count, ctx->gbuf, lang);
+ /* Python: an app factory registers its local defs
+ * (`app.add_url_rule("/", "index", index)` inside create_app); only the
+ * lexical lookup sees a function's locals. MUST match pass_parallel.c. */
+ cbm_resolution_t hres = {0};
+ if (lang == CBM_LANG_PYTHON) {
+ hres = cbm_registry_resolve_lexical(ctx->registry, call->second_arg_name,
+ call->enclosing_func_qn, module_qn);
+ }
+ if (!hres.qualified_name) {
+ hres = cbm_registry_resolve_handler(ctx->registry, call->second_arg_name, module_qn,
+ imp_keys, imp_vals, imp_count, ctx->gbuf, lang);
+ }
if (hres.qualified_name != NULL && hres.qualified_name[0] != '\0') {
const cbm_gbuf_node_t *handler = cbm_gbuf_find_by_qn(ctx->gbuf, hres.qualified_name);
if (handler != NULL) {
@@@ -853,196 -855,6 +862,198 @@@ static const cbm_gbuf_node_t *calls_fin
return src;
}
+static CBMCallEvidence *calls_evidence_at(CBMCallEvidence *evidence, int index,
+ CBMCallEvidence *fallback) {
+ if (evidence) {
+ return &evidence[index];
+ }
+ return fallback;
+}
+
+static CBMCallEvidence *calls_evidence_new(cbm_pipeline_t *pipeline, const CBMFileResult *result) {
+ CBMCallEvidence *evidence =
+ cbm_calloc(CBM_MEM_CLASS_RESOLVE, (size_t)result->calls.count * sizeof(*evidence));
+ if (!evidence) {
+ cbm_pipeline_mark_unresolved_capture_failed(pipeline);
+ }
+ return evidence;
+}
+
+/* Report whether a service spelling handled this call, independently of
+ * whether its classified edge is CALLS and therefore covers the occurrence. */
+static bool calls_emit_service_call(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const cbm_gbuf_node_t *source_node, cbm_svc_kind_t svc,
+ const cbm_resolution_t *resolved, CBMCallEvidence *evidence) {
+ if (svc != CBM_SVC_HTTP && svc != CBM_SVC_ASYNC) {
+ return false;
+ }
+ const char *url = call->first_string_arg;
+ if (!url || !url[0]) {
+ return false;
+ }
+ bool has_url_or_topic = url[0] == '/' || strstr(url, "://") != NULL ||
+ (svc == CBM_SVC_ASYNC && strlen(url) > PAIR_LEN);
+ if (!has_url_or_topic) {
+ return false;
+ }
+ cbm_resolution_t fallback = {.qualified_name = call->callee_name,
+ .confidence = PC_SVC_PATTERN_CONF,
+ .strategy = "service_pattern",
+ .candidate_count = 0};
+ const cbm_resolution_t *res = resolved ? resolved : &fallback;
+ evidence->calls_emitted |= emit_http_async_edge(ctx, call, source_node, NULL, res, svc, false);
+ return true;
+}
+
+/* Route registration counts as handled even when its edge is not CALLS. */
+static bool calls_emit_route_call(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const cbm_gbuf_node_t *source_node, const char *module_qn,
+ const char **imp_keys, const char **imp_vals, int imp_count,
+ const char *route_mount, CBMLanguage lang,
+ CBMCallEvidence *evidence) {
+ if (cbm_service_pattern_route_method(call->callee_name) == NULL || !call->first_string_arg ||
+ call->first_string_arg[0] != '/') {
+ return false;
+ }
+ evidence->calls_emitted |= handle_route_registration(
+ ctx, call, source_node, module_qn, imp_keys, imp_vals, imp_count, route_mount, lang);
+ return true;
+}
+
+/* An external callee can still register a route or address an HTTP/async service. */
+static int calls_emit_unresolved_service(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const cbm_gbuf_node_t *source_node, const char *module_qn,
+ const char **imp_keys, const char **imp_vals,
+ int imp_count, const char *route_mount, CBMLanguage lang,
+ CBMCallEvidence *evidence) {
+ if (calls_emit_route_call(ctx, call, source_node, module_qn, imp_keys, imp_vals, imp_count,
+ route_mount, lang, evidence)) {
+ return SKIP_ONE;
+ }
+ cbm_svc_kind_t svc = cbm_service_pattern_match(call->callee_name);
+ if (svc == CBM_SVC_NONE && cbm_service_pattern_is_global_fetch(call->callee_name)) {
+ svc = CBM_SVC_HTTP;
+ }
+ return calls_emit_service_call(ctx, call, source_node, svc, NULL, evidence);
+}
+
+/* Apply the field-call policy after a registry target has been found. */
+static const cbm_gbuf_node_t *calls_field_target(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const CBMResolvedCallArray *lsp_calls,
+ CBMLanguage lang, const char *rel,
+ const cbm_gbuf_node_t *source_node,
+ const cbm_gbuf_node_t *target_node,
+ bool arrow_bound, cbm_resolution_t *res) {
+ switch (cbm_call_onto_field_policy(lang, call->callee_name, target_node->label,
+ target_node->file_path)) {
+ case CBM_FIELD_CALL_DROP:
+ return NULL;
+ case CBM_FIELD_CALL_BY_OWNER:
+ /* An arrow call was already bound this way by registry resolution. */
+ if (!arrow_bound) {
+ target_node = cbm_pipeline_c_member_call_resolve(
+ lsp_calls, NULL, ctx->gbuf, ctx->registry, ctx->project_name,
+ call->enclosing_func_qn, call->callee_name, cbm_c_member_rule_file(lang, rel), res);
+ }
+ if (!target_node || source_node->id == target_node->id) {
+ return NULL;
+ }
+ break;
+ case CBM_FIELD_CALL_KEEP:
+ break;
+ }
+ return target_node;
+}
+
+static bool calls_emit_lsp_target(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const CBMResolvedCall *lsp, bool allow_tail, CBMLanguage lang,
+ const CBMImportArray *imports, const cbm_gbuf_node_t *source_node,
+ const char *module_qn, const char **imp_keys,
+ const char **imp_vals, int imp_count, const char *route_mount,
+ CBMCallEvidence *evidence) {
+ if (!lsp) {
+ return false;
+ }
+ bool exact_external_target =
+ call->requires_lsp_resolution && cbm_pipeline_kotlin_external_target(lang, lsp->callee_qn);
+ const cbm_gbuf_node_t *target_node =
+ exact_external_target ? cbm_pipeline_lsp_target_node_strict(ctx->gbuf, ctx->project_name,
+ lsp->callee_qn, allow_tail)
+ : cbm_pipeline_lsp_target_node(ctx->gbuf, ctx->project_name,
+ lsp->callee_qn, allow_tail);
+ if (!target_node || source_node->id == target_node->id) {
+ return false;
+ }
+ cbm_resolution_t res = {0};
+ /* Keep the canonical graph QN, including project prefixes added by fallback. */
+ res.qualified_name = target_node->qualified_name;
+ res.confidence = lsp->confidence;
+ res.strategy = lsp->strategy;
+ res.candidate_count = PC_SINGLE_CANDIDATE_COUNT;
+ evidence->candidate_qn = target_node->qualified_name;
+ /* Synthetic builtins can still denote a spawn; keep the parallel pass policy. */
+ cbm_pipeline_spawn_t spawn;
+ if (cbm_pipeline_spawn_site(ctx->gbuf, lang, call, imports, &res, &spawn)) {
+ cbm_pipeline_emit_spawn(ctx->gbuf, source_node, call, &spawn);
+ return true;
+ }
+ evidence->calls_emitted |=
+ emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys,
+ imp_vals, imp_count, false, route_mount, lang);
+ return true;
+}
+
+/* Vendor receivers and cross-language name guesses cannot bind project calls.
+ * Keep the registry policy shared with the parallel resolver. */
+static cbm_resolution_t calls_registry_resolution(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const char *module_qn, const char **imp_keys,
+ const char **imp_vals, int imp_count,
+ CBMLanguage lang, bool rust_external,
+ bool php_vendor_call) {
+ cbm_resolution_t res = {0};
+ if (!rust_external && !php_vendor_call) {
- res = cbm_registry_resolve_call_lang(ctx->registry, call->callee_name, module_qn, imp_keys,
- imp_vals, imp_count, lang);
++ /* Python LEGB fallback first (MUST match pass_parallel.c). */
++ res =
++ cbm_registry_resolve_call_scoped(ctx->registry, call->callee_name, module_qn, imp_keys,
++ imp_vals, imp_count, lang, call->enclosing_func_qn);
+ /* Cross-language veto: a name-only guess never binds another
+ * language's symbol (a JS app.get must not become Python's
+ * builtins.dict.get). The vetoed answer becomes EMPTY here, before
+ * the fallbacks below, so the route/HTTP classification still runs.
+ * MUST match pass_parallel.c. */
+ if (cbm_registry_name_guess_vetoed(ctx->registry, lang, &res)) {
+ res = (cbm_resolution_t){0};
+ }
+ }
+ return res;
+}
+
+static int calls_emit_import_service(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const cbm_gbuf_node_t *source_node, const char *rel,
+ CBMLanguage lang, const CBMImportArray *imports,
+ cbm_external_import_index_t *ext_imports,
+ CBMCallEvidence *evidence) {
+ /* A callee an import binds to an external client library
+ * (`send` under `from paho.mqtt import publish as send`): spelled
+ * through its import, then classified. MUST match pass_parallel.c. */
+ char spelled[CBM_SZ_512];
+ cbm_svc_kind_t isvc =
+ cbm_pipeline_external_import_service(ext_imports, ctx->gbuf, ctx->project_name, rel, lang,
+ imports, call, spelled, sizeof(spelled));
+ if (isvc != CBM_SVC_NONE) {
+ CBMCall through = *call;
+ through.callee_name = spelled;
+ cbm_resolution_t imp_res = {.qualified_name = spelled,
+ .confidence = PC_SVC_PATTERN_CONF,
+ .strategy = "import_binding",
+ .candidate_count = 0};
+ evidence->calls_emitted |=
+ emit_http_async_edge(ctx, &through, source_node, NULL, &imp_res, isvc, false);
+ return SKIP_ONE;
+ }
+ return 0;
+}
+
/* Resolve one call and emit the appropriate edge. Returns 1 if resolved, 0 if not. */
static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBMFileResult *result,
const char *rel, const char *module_qn, const char **imp_keys,
diff --cc src/pipeline/pass_parallel.c
index eba665b0a,346d4f225..63603c7e4
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@@ -2409,10 -2415,20 +2416,20 @@@ static bool emit_route_registration(cbm
snprintf(props, sizeof(props),
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"route_registration\"}", esc_cn,
esc_rp);
- cbm_gbuf_insert_edge(gbuf, source->id, rid, "CALLS", props);
+ bool emitted = cbm_gbuf_insert_edge(gbuf, source->id, rid, "CALLS", props) > 0;
if (handler_ref && handler_ref[0] != '\0') {
- cbm_resolution_t hres = cbm_registry_resolve_handler(registry, handler_ref, module_qn, ik,
- iv, ic, main_gbuf, lang);
+ /* Python: a handler an app factory registers is usually its local def
+ * (`app.add_url_rule("/", "index", index)` inside create_app); only the
+ * lexical lookup sees a function's locals. MUST match pass_calls.c. */
+ cbm_resolution_t hres = {0};
+ if (lang == CBM_LANG_PYTHON) {
+ hres = cbm_registry_resolve_lexical(registry, handler_ref, call->enclosing_func_qn,
+ module_qn);
+ }
+ if (!hres.qualified_name) {
+ hres = cbm_registry_resolve_handler(registry, handler_ref, module_qn, ik, iv, ic,
+ main_gbuf, lang);
+ }
if (hres.qualified_name && hres.qualified_name[0] != '\0') {
const cbm_gbuf_node_t *h = cbm_gbuf_find_by_qn(main_gbuf, hres.qualified_name);
if (h) {
diff --cc src/pipeline/pipeline_internal.h
index c8ca84efc,0965e0398..90c794e59
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@@ -1585,13 -1595,8 +1595,16 @@@ int cbm_pipeline_build_fresh_semantic_m
* a kube / k8s task DEPLOYS {via:ansible} the template it applies
* (pass_ansible.c). An external Chart is keyed by its publisher URL.
* An index built before holds none of them, so it rebuilds once on
- * upgrade. */
-enum { CBM_SEMANTIC_INDEX_VERSION = 9 };
+ * upgrade.
+ * 10: combine the platform variants, stem disambiguation and infrastructure
+ * layers above with unresolved-call evidence capture and its candidate
+ * index. Both upstream version 9 and this branch's earlier version 9
+ * lack part of these semantics, so unchanged files rebuild once.
++ * 11: Python local defs and classes bind through their enclosing lexical
++ * scope, including app-factory route handlers. Published version 10
++ * predates this resolution, so unchanged files rebuild once on upgrade.
+ * The unresolved-call coverage threshold remains independent. */
- enum { CBM_SEMANTIC_INDEX_VERSION = 10 };
++enum { CBM_SEMANTIC_INDEX_VERSION = 11 };
typedef struct {
cbm_gbuf_t *gbuf;
diff --cc tests/test_pipeline.c
index 0c695d45f,4f8c656ad..ffd8c76a6
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@@ -1945,7 -1692,7 +1945,8 @@@ TEST(pipeline_incremental_drops_terrafo
if (setup_test_repo() != 0) {
FAIL("failed to create temp dir");
}
-- write_temp_file(g_tmpdir, "infra/main.tf", "module \"net\" {\n source = \"./modules/net\"\n}\n");
++ write_temp_file(g_tmpdir, "infra/main.tf",
++ "module \"net\" {\n source = \"./modules/net\"\n}\n");
write_temp_file(g_tmpdir, "infra/outputs.tf",
"output \"net_vpc\" {\n value = module.net.vpc_id\n}\n");
write_temp_file(g_tmpdir, "infra/modules/net/main.tf",
@@@ -3991,8 -3849,8 +4103,9 @@@ TEST(incremental_shell_deploy_edit_keep
}
write_temp_file(tmp, "manifests/app.yaml",
"apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: app\n");
-- write_temp_file(tmp, "hack/deploy.sh",
-- "#!/bin/sh\ncd \"$(dirname \"$0\")/..\"\nkubectl apply -f manifests/app.yaml\n");
++ write_temp_file(
++ tmp, "hack/deploy.sh",
++ "#!/bin/sh\ncd \"$(dirname \"$0\")/..\"\nkubectl apply -f manifests/app.yaml\n");
char db_path[512];
snprintf(db_path, sizeof(db_path), "%s/shinc.db", tmp);
static const char deploys[] = "SELECT COUNT(*) FROM edges WHERE type='DEPLOYS'";
@@@ -4046,8 -3904,8 +4159,7 @@@ static int incremental_spawner_delete(b
snprintf(path, sizeof(path), "%s/hack/a.sh", tmp);
(void)remove(path);
if (!legacy) {
-- *after_script =
-- k8s_inc_index(tmp, db_path) == 0 ? k8s_inc_count(db_path, processes) : -1;
++ *after_script = k8s_inc_index(tmp, db_path) == 0 ? k8s_inc_count(db_path, processes) : -1;
*route = cbm_pipeline_incremental_test_last_route();
}
snprintf(path, sizeof(path), "%s/app.py", tmp);
@@@ -4293,7 -4151,7 +4405,8 @@@ static char *incremental_env_edit(bool
return NULL;
}
write_temp_file(tmp, "Dockerfile",
-- dockerfile ? "FROM alpine:3.20\nENV OLD_D=1\n" : "FROM alpine:3.20\nENV KEEP_D=1\n");
++ dockerfile ? "FROM alpine:3.20\nENV OLD_D=1\n"
++ : "FROM alpine:3.20\nENV KEEP_D=1\n");
write_temp_file(tmp, "compose.yaml",
"services:\n web:\n image: acme/web\n environment:\n OLD_C: x\n");
write_temp_file(tmp, "compose.other.yaml",
@@@ -4502,8 -4360,8 +4615,9 @@@ static char *incremental_docker_build_k
return NULL;
}
write_temp_file(tmp, "svc/Dockerfile", "FROM golang:1.22\n");
-- write_temp_file(tmp, "build.sh",
-- "#!/bin/sh\ncd \"$(dirname \"$0\")\"\ndocker build -t ghcr.io/acme/svc:1 svc\n");
++ write_temp_file(
++ tmp, "build.sh",
++ "#!/bin/sh\ncd \"$(dirname \"$0\")\"\ndocker build -t ghcr.io/acme/svc:1 svc\n");
char db_path[512];
snprintf(db_path, sizeof(db_path), "%s/dockerbuild.db", tmp);
static const char builds[] =
@@@ -4554,10 -4412,10 +4668,12 @@@ TEST(incremental_second_build_declarer_
snprintf(tmp, sizeof(tmp), "/tmp/cbm_twodecl_XXXXXX");
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
write_temp_file(tmp, "svc/Dockerfile", "FROM golang:1.22\n");
-- write_temp_file(tmp, "a.sh", "#!/bin/sh\ncd \"$(dirname \"$0\")\"\n"
-- "docker build -t ghcr.io/acme/svc:1 svc\n");
-- write_temp_file(tmp, "b.sh", "#!/bin/sh\ncd \"$(dirname \"$0\")\"\n"
-- "docker build -t ghcr.io/acme/svc:2 svc\n");
++ write_temp_file(tmp, "a.sh",
++ "#!/bin/sh\ncd \"$(dirname \"$0\")\"\n"
++ "docker build -t ghcr.io/acme/svc:1 svc\n");
++ write_temp_file(tmp, "b.sh",
++ "#!/bin/sh\ncd \"$(dirname \"$0\")\"\n"
++ "docker build -t ghcr.io/acme/svc:2 svc\n");
char db_path[512];
snprintf(db_path, sizeof(db_path), "%s/twodecl.db", tmp);
static const char builds[] =
@@@ -5127,10 -4985,10 +5243,12 @@@ static void stem_repo(const char *tmp,
write_temp_file(tmp, "rs/foo.rs", "pub mod bar;\npub fn top() {}\n");
write_temp_file(tmp, "rs/foo/bar.rs", "pub fn inner() {}\n");
write_temp_file(tmp, "c/foo.h", "int foo_add(int a, int b);\n");
-- write_temp_file(tmp, "c/foo.c", "#include \"foo.h\"\nint foo_add(int a, int b) { return a + b; }\n");
++ write_temp_file(tmp, "c/foo.c",
++ "#include \"foo.h\"\nint foo_add(int a, int b) { return a + b; }\n");
write_temp_file(tmp, "c/foo/x.c", "int foo_x(void) { return 1; }\n");
write_temp_file(tmp, "ts/foo.test.ts", "export function stemProbeA(): number { return 1; }\n");
-- write_temp_file(tmp, "ts/foo/test/a.ts", "export function stemProbeB(): number { return 2; }\n");
++ write_temp_file(tmp, "ts/foo/test/a.ts",
++ "export function stemProbeB(): number { return 2; }\n");
write_temp_file(tmp, "go.mod", "module example.com/stem\n\ngo 1.21\n");
write_temp_file(tmp, "goapi/api.sh", "#!/bin/sh\nrun_api() {\n echo api\n}\n");
write_temp_file(tmp, "goapi/api/server.go", "package api\n\nfunc Serve() {}\n");
@@@ -5183,7 -5041,7 +5301,8 @@@ TEST(pipeline_stem_folder_collision_kee
"C stem pair Module"},
{"SELECT COUNT(*) FROM nodes WHERE label='Folder' AND qualified_name='stem.c.foo/'",
"c/foo/ Folder marked"},
-- {"SELECT COUNT(*) FROM nodes WHERE label='Module' AND qualified_name='stem.ts.foo.test' AND "
++ {"SELECT COUNT(*) FROM nodes WHERE label='Module' AND qualified_name='stem.ts.foo.test' "
++ "AND "
"file_path='ts/foo.test.ts'",
"dotted foo.test.ts Module"},
{"SELECT COUNT(*) FROM nodes WHERE label='Folder' AND qualified_name='stem.ts.foo.test/'",
@@@ -5362,8 -5220,8 +5481,9 @@@ TEST(incremental_env_join_repair_keeps_
" value: prod\n");
write_temp_file(tmp, "compose.yaml",
"services:\n web:\n image: acme/web:1\n build: ./web\n");
-- write_temp_file(tmp, "web/Dockerfile",
-- "FROM node:20\nCOPY src ./src\nCOPY lib ./lib\nCMD [\"node\", \"src/server.js\"]\n");
++ write_temp_file(
++ tmp, "web/Dockerfile",
++ "FROM node:20\nCOPY src ./src\nCOPY lib ./lib\nCMD [\"node\", \"src/server.js\"]\n");
write_temp_file(tmp, "web/src/server.js",
"const port = process.env.PORT;\nconst mode = process.env.MODE;\n");
write_temp_file(tmp, "web/lib/conf.js", "module.exports = process.env.MODE;\n");
@@@ -5373,9 -5231,9 +5493,9 @@@
snprintf(db, sizeof(db), "%s/envjoin.db", tmp);
static const char joins[] = "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id "
"WHERE e.type='CONFIGURES' AND s.label='EnvVar'";
--#define ENVJOIN_PAIR(key, file) \
-- "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON " \
-- "t.id=e.target_id WHERE e.type='CONFIGURES' AND s.label='EnvVar' AND s.qualified_name LIKE " \
++#define ENVJOIN_PAIR(key, file) \
++ "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON " \
++ "t.id=e.target_id WHERE e.type='CONFIGURES' AND s.label='EnvVar' AND s.qualified_name LIKE " \
"'%#env/web/" key "' AND t.file_path='" file "'"
static const char mode_server[] = ENVJOIN_PAIR("MODE", "web/src/server.js");
static const char mode_conf[] = ENVJOIN_PAIR("MODE", "web/lib/conf.js");
@@@ -7077,16 -6860,6 +7197,21 @@@ static int pipeline_exact_inputs_migrat
PASS();
}
+TEST(pipeline_exact_inputs_migrate_coverage_metadata_and_index_mode) {
+ return pipeline_exact_inputs_migrate_metadata_and_mode(1);
+}
+
+/* Published branch version 8 predates language doc links and diagrams.
+ * Unchanged inputs still need a rebuild, followed by an exact no-op. */
+TEST(pipeline_semantic_version_8_index_is_rebuilt_once) {
+ return pipeline_exact_inputs_migrate_metadata_and_mode(8);
+}
+
++/* Published version 10 predates Python lexical-scope call resolution. */
++TEST(pipeline_semantic_version_10_index_is_rebuilt_once) {
++ return pipeline_exact_inputs_migrate_metadata_and_mode(10);
++}
++
typedef struct {
bool published;
int rename_calls;
@@@ -9469,9 -9416,9 +9768,9 @@@ TEST(pipeline_c_call_reaches_header_fun
/* Both namesakes must exist for the probe to mean anything. */
ASSERT_GTE(fixture_node_count(s, project, "include/linux/mm.h", "put_page", "Function"), 1);
-- ASSERT_GTE(fixture_node_count(s, project, "arch/arm64/include/asm/kvm_pgtable.h", "put_page",
-- "Field"),
-- 1);
++ ASSERT_GTE(
++ fixture_node_count(s, project, "arch/arm64/include/asm/kvm_pgtable.h", "put_page", "Field"),
++ 1);
ASSERT_EQ(labelled_call_count(s, project, "mte_dump_tag_range", "put_page", "Function"), 1);
ASSERT_EQ(labelled_call_count(s, project, "recv_buffer_free", "put_page", "Function"), 1);
/* A bare C call never names the struct member. */
@@@ -9505,9 -9452,9 +9804,9 @@@ TEST(pipeline_c_call_reaches_header_fun
ASSERT_NOT_NULL(s);
ASSERT_GTE(fixture_node_count(s, project, "include/linux/mm.h", "put_page", "Function"), 1);
-- ASSERT_GTE(fixture_node_count(s, project, "arch/arm64/include/asm/kvm_pgtable.h", "put_page",
-- "Field"),
-- 1);
++ ASSERT_GTE(
++ fixture_node_count(s, project, "arch/arm64/include/asm/kvm_pgtable.h", "put_page", "Field"),
++ 1);
ASSERT_EQ(labelled_call_count(s, project, "mte_dump_tag_range", "put_page", "Function"), 1);
ASSERT_EQ(labelled_call_count(s, project, "recv_buffer_free", "put_page", "Function"), 1);
ASSERT_EQ(labelled_call_count(s, project, "mte_dump_tag_range", "put_page", "Field"), 0);
@@@ -13444,8 -13391,8 +13743,8 @@@ static int python_init_scope_pipeline_c
: body);
}
if (written && cross_walk_stage) {
-- written = python_module_write_fixture_file(root, "memo_def.py",
-- "class S:\n def m(self):\n return 1\n");
++ written = python_module_write_fixture_file(
++ root, "memo_def.py", "class S:\n def m(self):\n return 1\n");
}
if (written && !failure_stage) {
written = python_module_write_fixture_file(root, "plain.py", body) &&
@@@ -13510,8 -13457,8 +13809,10 @@@
correct = project &&
cbm_store_find_nodes_by_label(store, project, "File", &files, &file_count) ==
CBM_STORE_OK &&
-- file_count ==
-- (cross_walk_stage ? 2 : failure_stage ? 1 : 5) + (parallel ? 52 : 0);
++ file_count == (cross_walk_stage ? 2
++ : failure_stage ? 1
++ : 5) +
++ (parallel ? 52 : 0);
cbm_store_free_nodes(files, file_count);
if (project && !failure_stage) {
const char *modules[] = {"plain", "pkg"};
@@@ -14526,6 -14590,731 +14944,729 @@@ static void teardown_lang_repo(void)
g_lang_tmpdir[0] = '\0';
}
+ /* ── #1548: Python defs nested in a function body ─────────────────── */
+
+ /* The QN of `rel` in `project` ("web.create_app.health"). Route QNs
+ * ("__route__GET__/health") carry no project and are taken as they are. */
+ static void pyn_qn(char *buf, size_t cap, const char *project, const char *rel) {
+ if (strncmp(rel, "__route__", strlen("__route__")) == 0) {
+ snprintf(buf, cap, "%s", rel);
+ } else {
+ snprintf(buf, cap, "%s.%s", project, rel);
+ }
+ }
+
+ /* True iff an edge of `type` runs from node `src_rel` to node `tgt_rel`. */
+ static bool pyn_edge(cbm_store_t *s, const char *project, const char *src_rel, const char *tgt_rel,
+ const char *type) {
+ char sq[512];
+ char tq[512];
+ pyn_qn(sq, sizeof(sq), project, src_rel);
+ pyn_qn(tq, sizeof(tq), project, tgt_rel);
+ cbm_node_t src = {0};
+ cbm_node_t tgt = {0};
+ bool found = false;
+ if (cbm_store_find_node_by_qn(s, project, sq, &src) == CBM_STORE_OK &&
+ cbm_store_find_node_by_qn(s, project, tq, &tgt) == CBM_STORE_OK) {
+ cbm_edge_t *edges = NULL;
+ int ec = 0;
+ cbm_store_find_edges_by_source_type(s, src.id, type, &edges, &ec);
+ for (int i = 0; i < ec && !found; i++) {
+ found = edges[i].target_id == tgt.id;
+ }
+ if (edges) {
+ cbm_store_free_edges(edges, ec);
+ }
+ }
+ cbm_node_free_fields(&src);
+ cbm_node_free_fields(&tgt);
+ return found;
+ }
+
+ /* The fixtures of the #1548 pipeline tests. */
+ static const char pyn_web_py[] = "from flask import Flask\n"
+ "\n"
+ "def load_user(uid):\n"
+ " return uid\n"
+ "\n"
+ "def render(name):\n"
+ " return name\n"
+ "\n"
+ "def create_app():\n"
+ " app = Flask(__name__)\n"
+ "\n"
+ " @app.route(\"/users/<uid>\")\n"
+ " def show_user(uid):\n"
+ " return render(load_user(uid))\n"
+ "\n"
+ " @app.get(\"/health\")\n"
+ " def health():\n"
+ " return render(\"ok\")\n"
+ "\n"
+ " def ping():\n"
+ " return render(\"pong\")\n"
+ "\n"
+ " app.add_url_rule(\"/ping\", \"ping\", ping)\n"
+ " return app\n";
+ /* A nested def named like its method, and a local helper whose name exists
+ * nowhere else -- b.py calls that name without binding it. */
+ static const char pyn_a_py[] = "class Tree:\n"
+ " def visit(self, node):\n"
+ " def visit(n):\n"
+ " if n:\n"
+ " visit(n.child)\n"
+ "\n"
+ " visit(node)\n"
+ "\n"
+ "\n"
+ "def outer():\n"
+ " def helper_only_here():\n"
+ " return 1\n"
+ "\n"
+ " return helper_only_here()\n";
+ static const char pyn_b_py[] = "def elsewhere():\n"
+ " return helper_only_here()\n";
+ /* Two defs, each with its own local class of the same name. */
+ static const char pyn_c_py[] = "def make_forms():\n"
+ " class LocalForm:\n"
+ " def clean(self):\n"
+ " return 1\n"
+ "\n"
+ " form = LocalForm()\n"
+ " return form.clean()\n"
+ "\n"
+ "\n"
+ "def make_other_forms():\n"
+ " class LocalForm:\n"
+ " def clean(self):\n"
+ " return 2\n"
+ "\n"
+ " form = LocalForm()\n"
+ " return form.clean()\n";
+ /* A local def named like a stdlib function another module imports. */
+ static const char pyn_x_py[] = "def outer():\n"
+ " def getpass(prompt):\n"
+ " return prompt\n"
+ "\n"
+ " return getpass(\"?\")\n";
+ static const char pyn_y_py[] = "from getpass import getpass\n"
+ "\n"
+ "\n"
+ "def ask():\n"
+ " return getpass(\"password: \")\n";
+
+ /* The issue's headline: an app factory's route handlers are defs inside
+ * create_app. They were never extracted, so the HTTP layer vanished -- no
+ * HANDLES, and load_user()'s only caller was pinned on the file node. */
+ TEST(pipeline_python_app_factory_routes_issue1548) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_factory_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "web.py", pyn_web_py);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/factory.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+
+ ASSERT_TRUE(
+ pyn_edge(s, project, "web.create_app.show_user", "__route__ANY__/users/{}", "HANDLES"));
+ ASSERT_TRUE(pyn_edge(s, project, "web.create_app.health", "__route__GET__/health", "HANDLES"));
+ ASSERT_TRUE(pyn_edge(s, project, "web.create_app.show_user", "web.load_user", "CALLS"));
+ ASSERT_TRUE(pyn_edge(s, project, "web.create_app.health", "web.render", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "web.py.__file__", "web.load_user", "CALLS"));
+ /* A call-registered handler: add_url_rule names create_app's local ping. */
+ ASSERT_TRUE(pyn_edge(s, project, "web.create_app.ping", "__route__ANY__/ping", "HANDLES"));
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ PASS();
+ }
+
+ /* A def nested in a function is a local of it (Python LEGB). A bare call
+ * resolves to the local bound in the nearest enclosing function: visit(node)
+ * in Tree.visit reaches the nested visit, whose own recursion stays a
+ * self-call instead of landing on the method. And no caller outside the
+ * function can reach the local by name -- b.py's unbound helper_only_here()
+ * must not be matched to it by a short-name strategy. */
+ TEST(pipeline_python_nested_def_is_function_local_issue1548) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_local_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "a.py", pyn_a_py);
+ write_temp_file(tmp, "b.py", pyn_b_py);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/local.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+
+ ASSERT_TRUE(pyn_edge(s, project, "a.Tree.visit", "a.Tree.visit.visit", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "a.Tree.visit.visit", "a.Tree.visit", "CALLS"));
+ ASSERT_TRUE(pyn_edge(s, project, "a.outer", "a.outer.helper_only_here", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "b.elsewhere", "a.outer.helper_only_here", "CALLS"));
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ PASS();
+ }
+
+ /* Two defs each declare `class LocalForm`. Each constructor call reaches its
+ * own def's class, and a method call on the instance never reaches the other
+ * def's class: the Python LSP must not bind a local class module-wide by its
+ * short name (every same-named local class collapsed onto one). */
+ TEST(pipeline_python_local_classes_stay_in_their_def_issue1548) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_localcls_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "c.py", pyn_c_py);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/localcls.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+
+ ASSERT_TRUE(pyn_edge(s, project, "c.make_forms", "c.make_forms.LocalForm", "CALLS"));
+ ASSERT_TRUE(
+ pyn_edge(s, project, "c.make_other_forms", "c.make_other_forms.LocalForm", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "c.make_forms", "c.make_other_forms.LocalForm", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "c.make_other_forms", "c.make_forms.LocalForm", "CALLS"));
+ ASSERT_FALSE(
+ pyn_edge(s, project, "c.make_forms", "c.make_other_forms.LocalForm.clean", "CALLS"));
+ ASSERT_FALSE(
+ pyn_edge(s, project, "c.make_other_forms", "c.make_forms.LocalForm.clean", "CALLS"));
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ PASS();
+ }
+
+ /* No import can name a function's local. The symbol-name import fallback
+ * bound `from getpass import getpass` (the stdlib module, not in the project)
+ * to x.outer's local `def getpass`, and ask()'s call followed the import. */
+ TEST(pipeline_python_import_never_binds_a_local_def_issue1548) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_localimp_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "x.py", pyn_x_py);
+ write_temp_file(tmp, "y.py", pyn_y_py);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/localimp.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+
+ ASSERT_TRUE(pyn_edge(s, project, "x.outer", "x.outer.getpass", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "y.py.__file__", "x.outer.getpass", "IMPORTS"));
+ ASSERT_FALSE(pyn_edge(s, project, "y.ask", "x.outer.getpass", "CALLS"));
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ PASS();
+ }
+
+ /* The same four fixtures through the parallel path: >= 50 files and
+ * CBM_WORKERS=4 force pass_parallel.c and the cross-file Python LSP, which
+ * register, resolve and bind through their own code. */
+ TEST(pipeline_python_nested_defs_parallel_issue1548) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_nested_par_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "web.py", pyn_web_py);
+ write_temp_file(tmp, "a.py", pyn_a_py);
+ write_temp_file(tmp, "b.py", pyn_b_py);
+ write_temp_file(tmp, "c.py", pyn_c_py);
+ write_temp_file(tmp, "x.py", pyn_x_py);
+ write_temp_file(tmp, "y.py", pyn_y_py);
+ for (int i = 0; i < 52; i++) {
+ char name[64];
+ char body[128];
+ snprintf(name, sizeof(name), "filler%d.py", i);
+ snprintf(body, sizeof(body), "def filler%d():\n return %d\n", i, i);
+ write_temp_file(tmp, name, body);
+ }
+
+ char *old_workers = getenv("CBM_WORKERS");
+ char *saved = old_workers ? strdup(old_workers) : NULL;
+ cbm_setenv("CBM_WORKERS", "4", 1);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/nested_par.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+
+ ASSERT_TRUE(
+ pyn_edge(s, project, "web.create_app.show_user", "__route__ANY__/users/{}", "HANDLES"));
+ ASSERT_TRUE(pyn_edge(s, project, "web.create_app.health", "__route__GET__/health", "HANDLES"));
+ ASSERT_TRUE(pyn_edge(s, project, "web.create_app.show_user", "web.load_user", "CALLS"));
+ ASSERT_TRUE(pyn_edge(s, project, "web.create_app.ping", "__route__ANY__/ping", "HANDLES"));
+ ASSERT_TRUE(pyn_edge(s, project, "a.Tree.visit", "a.Tree.visit.visit", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "a.Tree.visit.visit", "a.Tree.visit", "CALLS"));
+ ASSERT_TRUE(pyn_edge(s, project, "a.outer", "a.outer.helper_only_here", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "b.elsewhere", "a.outer.helper_only_here", "CALLS"));
+ ASSERT_TRUE(pyn_edge(s, project, "c.make_forms", "c.make_forms.LocalForm", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "c.make_forms", "c.make_other_forms.LocalForm", "CALLS"));
+ ASSERT_FALSE(
+ pyn_edge(s, project, "c.make_forms", "c.make_other_forms.LocalForm.clean", "CALLS"));
+ ASSERT_FALSE(
+ pyn_edge(s, project, "c.make_other_forms", "c.make_forms.LocalForm.clean", "CALLS"));
+ ASSERT_TRUE(pyn_edge(s, project, "x.outer", "x.outer.getpass", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "y.py.__file__", "x.outer.getpass", "IMPORTS"));
+ ASSERT_FALSE(pyn_edge(s, project, "y.ask", "x.outer.getpass", "CALLS"));
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ if (saved) {
+ cbm_setenv("CBM_WORKERS", saved, 1);
+ free(saved);
+ } else {
+ cbm_unsetenv("CBM_WORKERS");
+ }
+ th_rmtree(tmp);
+ PASS();
+ }
+
+ /* An incremental re-index seeds its registry from the stored graph, not from
+ * extraction. It must keep a function's locals out of the short-name index
+ * exactly as the full index does: re-resolving the touched b.py must not bind
+ * its unbound helper_only_here() to a.outer's local. */
+ TEST(pipeline_python_nested_def_local_after_incremental_issue1548) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_local_incr_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "a.py", pyn_a_py);
+ write_temp_file(tmp, "b.py", pyn_b_py);
+ /* An extensionless script is Python by its shebang: its locals are locals
+ * in the incremental seed too, not only in the full index. */
+ write_temp_file(tmp, "tool",
+ "#!/usr/bin/env python3\n"
+ "def run():\n"
+ " def helper_in_tool():\n"
+ " return 1\n"
+ "\n"
+ " return helper_in_tool()\n");
+ write_temp_file(tmp, "d.py",
+ "def elsewhere_too():\n"
+ " return helper_in_tool()\n");
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/local_incr.db", tmp);
+ cbm_pipeline_t *p1 = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p1);
+ ASSERT_EQ(cbm_pipeline_run(p1), 0);
+ cbm_pipeline_free(p1);
+
+ char b_path[512];
+ snprintf(b_path, sizeof(b_path), "%s/b.py", tmp);
+ ASSERT_EQ(th_append_file(b_path, "\n# touched: re-resolve b.py incrementally\n"), 0);
+ snprintf(b_path, sizeof(b_path), "%s/d.py", tmp);
+ ASSERT_EQ(th_append_file(b_path, "\n# touched: re-resolve d.py incrementally\n"), 0);
+
+ cbm_pipeline_t *p2 = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p2);
+ ASSERT_EQ(cbm_pipeline_run(p2), 0);
+ const char *project = cbm_pipeline_project_name(p2);
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+
+ ASSERT_TRUE(pyn_edge(s, project, "a.outer", "a.outer.helper_only_here", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "b.elsewhere", "a.outer.helper_only_here", "CALLS"));
+ ASSERT_TRUE(pyn_edge(s, project, "tool.run", "tool.run.helper_in_tool", "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, "d.elsewhere_too", "tool.run.helper_in_tool", "CALLS"));
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p2);
+ th_rmtree(tmp);
+ PASS();
+ }
+
+ /* As pyn_edge, and the edge's properties also contain `fragment`. */
+ static bool pyn_edge_with(cbm_store_t *s, const char *project, const char *src_rel,
+ const char *tgt_rel, const char *type, const char *fragment) {
+ char sq[512];
+ char tq[512];
+ pyn_qn(sq, sizeof(sq), project, src_rel);
+ pyn_qn(tq, sizeof(tq), project, tgt_rel);
+ cbm_node_t src = {0};
+ cbm_node_t tgt = {0};
+ bool found = false;
+ if (cbm_store_find_node_by_qn(s, project, sq, &src) == CBM_STORE_OK &&
+ cbm_store_find_node_by_qn(s, project, tq, &tgt) == CBM_STORE_OK) {
+ cbm_edge_t *edges = NULL;
+ int ec = 0;
+ cbm_store_find_edges_by_source_type(s, src.id, type, &edges, &ec);
+ for (int i = 0; i < ec && !found; i++) {
+ found = edges[i].target_id == tgt.id && edges[i].properties_json &&
+ strstr(edges[i].properties_json, fragment);
+ }
+ if (edges) {
+ cbm_store_free_edges(edges, ec);
+ }
+ }
+ cbm_node_free_fields(&src);
+ cbm_node_free_fields(&tgt);
+ return found;
+ }
+
+ /* A local def shadows a module def of the same name (Python LEGB), in both
+ * orders of the two callers within one file: the registry's per-file resolve
+ * cache is keyed by the callee name only, so a caller-dependent answer must
+ * never come from it. The undecorated local is bound by the Python LSP itself
+ * (an lsp_* edge); a decorated one is no exact callable value, so the LSP
+ * shadows it and the registry's lexical_scope step binds it. */
+ static const char pyn_shadow_local_first_py[] = "def helper():\n"
+ " return 0\n"
+ "\n"
+ "def uses_local():\n"
+ " def helper():\n"
+ " return 1\n"
+ " return helper()\n"
+ "\n"
+ "def uses_module():\n"
+ " return helper()\n"
+ "\n"
+ "def uses_decorated():\n"
+ " @staticmethod\n"
+ " def helper():\n"
+ " return 2\n"
+ " return helper()\n";
+ static const char pyn_shadow_module_first_py[] = "def helper():\n"
+ " return 0\n"
+ "\n"
+ "def uses_module():\n"
+ " return helper()\n"
+ "\n"
+ "def uses_local():\n"
+ " if True:\n"
+ " def helper():\n"
+ " return 1\n"
+ " return helper()\n";
+
+ /* A class declared in a def is walked by the LSP under the def's QN: super()
+ * and self calls in its methods resolve through the class's own bases. */
+ static const char pyn_local_class_methods_py[] = "class Base:\n"
+ " def greet(self):\n"
+ " return 1\n"
+ "\n"
+ "def factory():\n"
+ " class Local(Base):\n"
+ " def greet(self):\n"
+ " return super().greet()\n"
+ "\n"
+ " def other(self):\n"
+ " return self.greet()\n"
+ " return Local\n";
+
+ static int pyn_assert_shadowing(cbm_store_t *s, const char *project) {
+ ASSERT_TRUE(pyn_edge_with(s, project, "sc.factory.Local.greet", "sc.Base.greet", "CALLS",
+ "\"strategy\":\"lsp_super\""));
+ ASSERT_TRUE(pyn_edge_with(s, project, "sc.factory.Local.other", "sc.factory.Local.greet",
+ "CALLS", "\"strategy\":\"lsp_"));
+ /* sa's local def is a plain statement of the body: the LSP binds it as an
+ * exact callable. sb's sits under `if True:`; at that compound statement's
+ * join the LSP invalidates what the branch bound (the def may not exist on
+ * every path), so it emits nothing and the registry's lexical step binds
+ * the local -- still never the module def. H1 (the LSP walks compound
+ * statements) may legitimately move sb to an lsp_* strategy; that PR
+ * must update this expectation deliberately. */
+ const char *files[] = {"sa", "sb"};
+ const char *local_strategy[] = {"\"strategy\":\"lsp_", "\"strategy\":\"lexical_scope\""};
+ for (int f = 0; f < 2; f++) {
+ char local_caller[64], local_def[64], module_caller[64], module_def[64];
+ snprintf(local_caller, sizeof(local_caller), "%s.uses_local", files[f]);
+ snprintf(local_def, sizeof(local_def), "%s.uses_local.helper", files[f]);
+ snprintf(module_caller, sizeof(module_caller), "%s.uses_module", files[f]);
+ snprintf(module_def, sizeof(module_def), "%s.helper", files[f]);
- ASSERT_TRUE(
- pyn_edge_with(s, project, local_caller, local_def, "CALLS", local_strategy[f]));
++ ASSERT_TRUE(pyn_edge_with(s, project, local_caller, local_def, "CALLS", local_strategy[f]));
+ ASSERT_FALSE(pyn_edge(s, project, local_caller, module_def, "CALLS"));
+ ASSERT_TRUE(pyn_edge(s, project, module_caller, module_def, "CALLS"));
+ ASSERT_FALSE(pyn_edge(s, project, module_caller, local_def, "CALLS"));
+ }
- ASSERT_TRUE(pyn_edge_with(s, project, "sa.uses_decorated", "sa.uses_decorated.helper",
- "CALLS", "\"strategy\":\"lexical_scope\""));
++ ASSERT_TRUE(pyn_edge_with(s, project, "sa.uses_decorated", "sa.uses_decorated.helper", "CALLS",
++ "\"strategy\":\"lexical_scope\""));
+ ASSERT_FALSE(pyn_edge(s, project, "sa.uses_decorated", "sa.helper", "CALLS"));
+ return 0;
+ }
+
+ static int pyn_run_shadowing(int fillers) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_shadow_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "sa.py", pyn_shadow_local_first_py);
+ write_temp_file(tmp, "sb.py", pyn_shadow_module_first_py);
+ write_temp_file(tmp, "sc.py", pyn_local_class_methods_py);
+ for (int i = 0; i < fillers; i++) {
+ char name[64];
+ char body[128];
+ snprintf(name, sizeof(name), "filler%d.py", i);
+ snprintf(body, sizeof(body), "def filler%d():\n return %d\n", i, i);
+ write_temp_file(tmp, name, body);
+ }
+ char *old_workers = getenv("CBM_WORKERS");
+ char *saved = old_workers ? strdup(old_workers) : NULL;
+ if (fillers > 0) {
+ cbm_setenv("CBM_WORKERS", "4", 1);
+ }
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/shadow.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ int rc = p ? cbm_pipeline_run(p) : -1;
+ cbm_store_t *s = rc == 0 ? cbm_store_open_path(db_path) : NULL;
+ int verdict = s ? pyn_assert_shadowing(s, cbm_pipeline_project_name(p)) : 1;
+ if (s) {
+ cbm_store_close(s);
+ }
+ if (p) {
+ cbm_pipeline_free(p);
+ }
+ if (saved) {
+ cbm_setenv("CBM_WORKERS", saved, 1);
+ free(saved);
+ } else {
+ cbm_unsetenv("CBM_WORKERS");
+ }
+ th_rmtree(tmp);
+ ASSERT_EQ(rc, 0);
+ return verdict;
+ }
+
+ TEST(pipeline_python_local_def_shadows_module_def_issue1548) {
+ return pyn_run_shadowing(0);
+ }
+
+ TEST(pipeline_python_local_def_shadows_module_def_parallel_issue1548) {
+ return pyn_run_shadowing(52);
+ }
+
+ /* L7 review of #1548: a package __init__.py function can share a submodule's QN
+ * (poetry: layouts/__init__.py `def layout` is the QN prefix of every symbol in
+ * layouts/layout.py). The function-local detection is bounded at the symbol's
+ * own file, so class Layout stays a module-level, importable class. src/ layout
+ * without a manifest: the module resolver misses `rowpkg.layout`, so the import
+ * relies on the symbol-name fallback, which skips function locals -- unbounded,
+ * Layout read as a local of layout() and its import and calls vanished. Both
+ * resolvers; the second run re-indexes incrementally after an edit of
+ * layout.py, so the incremental registry seed is held to the same bound. */
+ static const char pyn_layout_init_py[] = "def layout(name):\n"
+ " return name\n";
+ static const char pyn_layout_mod_py[] = "class Layout:\n"
+ " def __init__(self, name):\n"
+ " self.name = name\n"
+ "\n"
+ " def create(self):\n"
+ " return self.name\n";
+ static const char pyn_layout_app_py[] = "from rowpkg.layout import Layout\n"
+ "\n"
+ "def test_build():\n"
+ " return Layout(\"demo\").create()\n";
+
+ static int pyn_run_init_collision(int fillers) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_initqn_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "src/rowpkg/__init__.py", pyn_layout_init_py);
+ write_temp_file(tmp, "src/rowpkg/layout.py", pyn_layout_mod_py);
+ write_temp_file(tmp, "tests/test_app.py", pyn_layout_app_py);
+ for (int i = 0; i < fillers; i++) {
+ char name[64];
+ char body[128];
+ snprintf(name, sizeof(name), "filler%d.py", i);
+ snprintf(body, sizeof(body), "def filler%d():\n return %d\n", i, i);
+ write_temp_file(tmp, name, body);
+ }
+ char *old_workers = getenv("CBM_WORKERS");
+ char *saved = old_workers ? strdup(old_workers) : NULL;
+ if (fillers > 0) {
+ cbm_setenv("CBM_WORKERS", "4", 1);
+ }
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/initqn.db", tmp);
+ int rc[2] = {-1, -1};
+ bool imports[2] = {false, false};
+ bool calls[2] = {false, false};
+ bool method[2] = {false, false};
+ for (int run = 0; run < 2; run++) {
+ if (run == 1) {
+ char path[512];
+ snprintf(path, sizeof(path), "%s/src/rowpkg/layout.py", tmp);
+ th_append_file(path, "\n# edited: the second run re-indexes incrementally\n");
+ }
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ rc[run] = p ? cbm_pipeline_run(p) : -1;
+ cbm_store_t *s = rc[run] == 0 ? cbm_store_open_path(db_path) : NULL;
+ if (s) {
+ const char *project = cbm_pipeline_project_name(p);
+ imports[run] = pyn_edge(s, project, "tests.test_app.py.__file__",
+ "src.rowpkg.layout.Layout", "IMPORTS");
- calls[run] =
- pyn_edge(s, project, "tests.test_app.test_build", "src.rowpkg.layout.Layout",
- "CALLS");
++ calls[run] = pyn_edge(s, project, "tests.test_app.test_build",
++ "src.rowpkg.layout.Layout", "CALLS");
+ method[run] = pyn_edge(s, project, "tests.test_app.test_build",
+ "src.rowpkg.layout.Layout.create", "CALLS");
+ cbm_store_close(s);
+ }
+ if (p) {
+ cbm_pipeline_free(p);
+ }
+ }
+ if (saved) {
+ cbm_setenv("CBM_WORKERS", saved, 1);
+ free(saved);
+ } else {
+ cbm_unsetenv("CBM_WORKERS");
+ }
+ th_rmtree(tmp);
+ for (int run = 0; run < 2; run++) {
+ ASSERT_EQ(rc[run], 0);
+ ASSERT_TRUE(imports[run]); /* the class stays importable */
+ ASSERT_TRUE(calls[run]); /* Layout("demo") */
+ ASSERT_TRUE(method[run]); /* .create() through the constructed type */
+ }
+ PASS();
+ }
+
+ TEST(pipeline_python_init_function_shares_submodule_qn_issue1548) {
+ return pyn_run_init_collision(0);
+ }
+
+ TEST(pipeline_python_init_function_shares_submodule_qn_parallel_issue1548) {
+ return pyn_run_init_collision(52);
+ }
+
+ /* #1548 keeps TESTS for calls made through a test's own nested helper: the
+ * call now starts at the extracted helper def (not at the test FILE node, as
+ * before #1548), so TESTS is attributed to the enclosing test function. A
+ * helper nested in a NON-test function stays without TESTS, like a module-level
+ * helper. Both resolvers. */
+ static const char pyn_tests_prod_py[] = "def target(value):\n"
+ " return value\n"
+ "\n"
+ "def other_target(value):\n"
+ " return value\n";
+ static const char pyn_tests_test_py[] = "from pkg.prod import target, other_target\n"
+ "\n"
+ "def test_through_nested_helper():\n"
+ " def check(v):\n"
+ " return target(v)\n"
+ " assert check(1) == 1\n"
+ "\n"
+ "class TestGroup:\n"
+ " def test_method_helper(self):\n"
+ " def probe():\n"
+ " return target(2)\n"
+ " assert probe() == 2\n"
+ "\n"
+ "def make_fixture():\n"
+ " def build():\n"
+ " return other_target(3)\n"
+ " return build\n";
+
+ static int pyn_run_nested_helper_tests(int fillers) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_ntests_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "pkg/__init__.py", "");
+ write_temp_file(tmp, "pkg/prod.py", pyn_tests_prod_py);
+ write_temp_file(tmp, "tests/test_rows.py", pyn_tests_test_py);
+ for (int i = 0; i < fillers; i++) {
+ char name[64];
+ char body[128];
+ snprintf(name, sizeof(name), "filler%d.py", i);
+ snprintf(body, sizeof(body), "def filler%d():\n return %d\n", i, i);
+ write_temp_file(tmp, name, body);
+ }
+ char *old_workers = getenv("CBM_WORKERS");
+ char *saved = old_workers ? strdup(old_workers) : NULL;
+ if (fillers > 0) {
+ cbm_setenv("CBM_WORKERS", "4", 1);
+ }
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/ntests.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ int rc = p ? cbm_pipeline_run(p) : -1;
+ cbm_store_t *s = rc == 0 ? cbm_store_open_path(db_path) : NULL;
+ bool fn_tests = false, method_tests = false, helper_calls = false, fixture_tests = true;
+ if (s) {
+ const char *project = cbm_pipeline_project_name(p);
+ helper_calls = pyn_edge(s, project, "tests.test_rows.test_through_nested_helper.check",
+ "pkg.prod.target", "CALLS");
+ fn_tests = pyn_edge(s, project, "tests.test_rows.test_through_nested_helper",
+ "pkg.prod.target", "TESTS");
+ method_tests = pyn_edge(s, project, "tests.test_rows.TestGroup.test_method_helper",
+ "pkg.prod.target", "TESTS");
+ fixture_tests = pyn_edge(s, project, "tests.test_rows.make_fixture",
+ "pkg.prod.other_target", "TESTS") ||
+ pyn_edge(s, project, "tests.test_rows.make_fixture.build",
+ "pkg.prod.other_target", "TESTS");
+ cbm_store_close(s);
+ }
+ if (p) {
+ cbm_pipeline_free(p);
+ }
+ if (saved) {
+ cbm_setenv("CBM_WORKERS", saved, 1);
+ free(saved);
+ } else {
+ cbm_unsetenv("CBM_WORKERS");
+ }
+ th_rmtree(tmp);
+ ASSERT_EQ(rc, 0);
+ ASSERT_TRUE(helper_calls); /* the call starts at the nested helper (#1548) */
+ ASSERT_TRUE(fn_tests); /* ... and the enclosing test function still TESTS the target */
+ ASSERT_TRUE(method_tests); /* the same inside a test method */
+ ASSERT_FALSE(fixture_tests); /* a helper nested in a non-test function: no TESTS */
+ PASS();
+ }
+
+ TEST(pipeline_python_nested_helper_keeps_tests_issue1548) {
+ return pyn_run_nested_helper_tests(0);
+ }
+
+ TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548) {
+ return pyn_run_nested_helper_tests(52);
+ }
+
TEST(pipeline_python_project) {
/* Port of TestPipelinePythonProject */
const char *files[] = {"main.py", "utils.py"};
@@@ -24030,7 -24816,18 +25179,19 @@@ SUITE(pipeline)
RUN_TEST(pipeline_c_variants_on_sequential_and_parallel_paths_c1);
RUN_TEST(pipeline_cpp_overloads_are_listed_as_variants_c1);
RUN_TEST(pipeline_semantic_version_3_index_is_rebuilt_in_full_c1);
+ RUN_TEST(pipeline_semantic_version_5_index_is_rebuilt_in_full_c1);
+ RUN_TEST(pipeline_python_app_factory_routes_issue1548);
+ RUN_TEST(pipeline_python_nested_def_is_function_local_issue1548);
+ RUN_TEST(pipeline_python_local_classes_stay_in_their_def_issue1548);
+ RUN_TEST(pipeline_python_import_never_binds_a_local_def_issue1548);
+ RUN_TEST(pipeline_python_nested_defs_parallel_issue1548);
+ RUN_TEST(pipeline_python_nested_def_local_after_incremental_issue1548);
+ RUN_TEST(pipeline_python_local_def_shadows_module_def_issue1548);
+ RUN_TEST(pipeline_python_local_def_shadows_module_def_parallel_issue1548);
+ RUN_TEST(pipeline_python_init_function_shares_submodule_qn_issue1548);
+ RUN_TEST(pipeline_python_init_function_shares_submodule_qn_parallel_issue1548);
+ RUN_TEST(pipeline_python_nested_helper_keeps_tests_issue1548);
+ RUN_TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548);
RUN_TEST(pipeline_header_include_target_is_independent_of_registration_order);
RUN_TEST(pipeline_imports_multi_symbol_edges);
RUN_TEST(pipeline_go_cross_package_call);
@@@ -24313,8 -25110,6 +25474,9 @@@ SUITE(pipeline_semantic_manifest_repro
RUN_TEST(pipeline_source_addition_before_publication_preserves_previous_generation);
RUN_TEST(pipeline_tsconfig_mutation_before_publication_preserves_previous_generation);
RUN_TEST(pipeline_exact_inputs_migrate_coverage_metadata_and_index_mode);
+ RUN_TEST(pipeline_semantic_version_8_index_is_rebuilt_once);
++ RUN_TEST(pipeline_semantic_version_10_index_is_rebuilt_once);
+ RUN_TEST(pipeline_capture_failure_preserves_general_coverage_and_rebuilds);
RUN_TEST(pipeline_existing_artifact_refreshes_after_default_forced_full_reindex);
RUN_TEST(pipeline_full_cancel_after_predump_preserves_previous_generation);
RUN_TEST(pipeline_full_cancel_after_destination_prepare_preserves_previous_generation);