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.

perf(terraform): find a helm_release block by its QN; gate the Phase 3 resolvers

Martin Vogela48852d77eaf
diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c
index 39138e114..920b59245 100644
--- a/src/pipeline/pass_k8s.c
+++ b/src/pipeline/pass_k8s.c
@@ -3263,6 +3263,7 @@ static void k8s_link_chart_refs(k8s_dep_link_t *l, const k8s_hr_t *oci) {
return;
}
for (int i = 0; i < n; i++) {
+ atomic_fetch_add_explicit(&g_k8s_candidates, SKIP_ONE, memory_order_relaxed);
const char *p = in[i]->properties_json;
const cbm_gbuf_node_t *hr = cbm_gbuf_find_by_id(l->ctx->gbuf, in[i]->source_id);
if (p && strstr(p, "\"ref\":\"chartRef\"") && hr && hr->file_path) {
diff --git a/src/pipeline/pass_shell.c b/src/pipeline/pass_shell.c
index 1bbc5ea3f..85adae26b 100644
--- a/src/pipeline/pass_shell.c
+++ b/src/pipeline/pass_shell.c
@@ -3099,6 +3099,7 @@ static void sh_make_resolve(sh_pass_t *p, cbm_pipeline_ctx_t *ctx, sh_hits_t *h)
sh_hit_t *x = &h->items[i];
const mk_closure_t *cl = x->make ? cbm_ht_get(closures, x->path) : NULL;
for (int k = 0; cl && k < cl->n && x->tgt <= 0; k++) {
+ atomic_fetch_add_explicit(&g_sh_candidates, SKIP_ONE, memory_order_relaxed);
const char *key = mk_rule_key(&a, cl->order[k], x->ext);
const cbm_gbuf_node_t *rule = key ? cbm_ht_get(rules, key) : NULL;
x->tgt = rule ? rule->id : 0;
diff --git a/src/pipeline/pass_terraform.c b/src/pipeline/pass_terraform.c
index b0132f7a9..309f6c99c 100644
--- a/src/pipeline/pass_terraform.c
+++ b/src/pipeline/pass_terraform.c
@@ -25,15 +25,21 @@
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
#include "cbm.h"
+#include "helpers.h"
#include "lang_specs.h"
+#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+/* Blocks a helm_release lookup examined (complexity guard counter). */
+_Atomic uint64_t g_tf_candidates = 0;
+
typedef struct {
cbm_pipeline_ctx_t *ctx;
+ CBMArena *arena; /* lookup QNs */
CBMHashTable *charts; /* Chart.yaml path -> local Chart node id */
const char *src;
const char *file;
@@ -221,21 +227,16 @@ static int64_t tf_external_chart(cbm_pipeline_ctx_t *ctx, const char *name, cons
"{\"source\":\"helm\",\"external\":true}");
}
-/* The node of block `resource.helm_release.<name>` in the file. */
+/* The node of block `resource.helm_release.<name>` in the file, by the QN
+ * its definition got -- not among every block of that name: modules that
+ * share a release name (`this`) would make each lookup scan all of them. */
static int64_t tf_block_node(const tf_t *t, const char *name) {
char full[CBM_SZ_512];
snprintf(full, sizeof(full), "resource.helm_release.%s", name);
- const cbm_gbuf_node_t **nodes = NULL;
- int count = 0;
- if (cbm_gbuf_find_by_name(t->ctx->gbuf, full, &nodes, &count) != 0) {
- return 0;
- }
- for (int i = 0; i < count; i++) {
- if (nodes[i]->file_path && strcmp(nodes[i]->file_path, t->file) == 0) {
- return nodes[i]->id;
- }
- }
- return 0;
+ const char *qn = cbm_fqn_compute(t->arena, t->ctx->project_name, t->file, full);
+ const cbm_gbuf_node_t *n = qn ? cbm_gbuf_find_by_qn(t->ctx->gbuf, qn) : NULL;
+ atomic_fetch_add_explicit(&g_tf_candidates, SKIP_ONE, memory_order_relaxed);
+ return n && n->file_path && strcmp(n->file_path, t->file) == 0 ? n->id : 0;
}
static bool tf_plain_name(const char *s) {
@@ -396,6 +397,8 @@ void cbm_pipeline_pass_terraform(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
int file_count) {
TSParser *parser = NULL;
CBMHashTable *charts = NULL;
+ CBMArena arena;
+ cbm_arena_init_lazy(&arena, CBM_SZ_4K);
int edges = 0;
for (int i = 0; i < file_count; i++) {
if (files[i].language != CBM_LANG_HCL || !tf_is_module_file(files[i].rel_path)) {
@@ -416,7 +419,11 @@ void cbm_pipeline_pass_terraform(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
tree = ts_parser_parse_string(parser, NULL, src, (uint32_t)len);
}
if (tree) {
- tf_t t = {.ctx = ctx, .charts = charts, .src = src, .file = files[i].rel_path};
+ tf_t t = {.ctx = ctx,
+ .arena = &arena,
+ .charts = charts,
+ .src = src,
+ .file = files[i].rel_path};
const char *slash = strrchr(files[i].rel_path, '/');
snprintf(t.mdir, sizeof(t.mdir), "%.*s",
slash ? (int)(slash - files[i].rel_path) : 0, files[i].rel_path);
@@ -433,6 +440,7 @@ void cbm_pipeline_pass_terraform(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
if (charts) {
cbm_ht_free(charts);
}
+ cbm_arena_destroy(&arena);
if (edges > 0) {
char buf[CBM_SZ_32];
snprintf(buf, sizeof(buf), "%d", edges);
diff --git a/tests/fixtures/complexity/Bash/Makefile b/tests/fixtures/complexity/Bash/Makefile
new file mode 100644
index 000000000..518656be5
--- /dev/null
+++ b/tests/fixtures/complexity/Bash/Makefile
@@ -0,0 +1,9 @@
+include common.mk
+
+.PHONY: all deploy
+
+all: build
+ $(MAKE) deploy
+
+deploy:
+ ./deploy.sh
diff --git a/tests/fixtures/complexity/Bash/action.yml b/tests/fixtures/complexity/Bash/action.yml
new file mode 100644
index 000000000..2f517178f
--- /dev/null
+++ b/tests/fixtures/complexity/Bash/action.yml
@@ -0,0 +1,9 @@
+name: deploy
+description: build and deploy
+runs:
+ using: composite
+ steps:
+ - run: make -C "${{ github.action_path }}" all
+ shell: bash
+ - run: ${{ github.action_path }}/deploy.sh
+ shell: bash
diff --git a/tests/fixtures/complexity/Bash/charts/web/Chart.yaml b/tests/fixtures/complexity/Bash/charts/web/Chart.yaml
new file mode 100644
index 000000000..dc9e11e05
--- /dev/null
+++ b/tests/fixtures/complexity/Bash/charts/web/Chart.yaml
@@ -0,0 +1,3 @@
+apiVersion: v2
+name: web
+version: 0.1.0
diff --git a/tests/fixtures/complexity/Bash/common.mk b/tests/fixtures/complexity/Bash/common.mk
new file mode 100644
index 000000000..dc18e50aa
--- /dev/null
+++ b/tests/fixtures/complexity/Bash/common.mk
@@ -0,0 +1,2 @@
+build:
+ ./scripts/build.sh
diff --git a/tests/fixtures/complexity/Bash/deploy.sh b/tests/fixtures/complexity/Bash/deploy.sh
new file mode 100644
index 000000000..3adf2dc97
--- /dev/null
+++ b/tests/fixtures/complexity/Bash/deploy.sh
@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+set -euo pipefail
+cd "$(dirname "$0")"
+
+./scripts/build.sh
+kubectl apply -f manifests/
+helm upgrade --install web ./charts/web
diff --git a/tests/fixtures/complexity/Bash/manifests/app.yaml b/tests/fixtures/complexity/Bash/manifests/app.yaml
new file mode 100644
index 000000000..32f00c515
--- /dev/null
+++ b/tests/fixtures/complexity/Bash/manifests/app.yaml
@@ -0,0 +1,26 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+ name: web
+spec:
+ selector:
+ matchLabels:
+ app: web
+ template:
+ metadata:
+ labels:
+ app: web
+ spec:
+ containers:
+ - name: web
+ image: nginx:1.27
+---
+apiVersion: v1
+kind: Service
+metadata:
+ name: web
+spec:
+ selector:
+ app: web
+ ports:
+ - port: 80
diff --git a/tests/fixtures/complexity/Bash/scripts/build.sh b/tests/fixtures/complexity/Bash/scripts/build.sh
new file mode 100644
index 000000000..083a16db1
--- /dev/null
+++ b/tests/fixtures/complexity/Bash/scripts/build.sh
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+set -euo pipefail
+echo building
diff --git a/tests/fixtures/complexity/Go/charts/operator/Chart.yaml b/tests/fixtures/complexity/Go/charts/operator/Chart.yaml
new file mode 100644
index 000000000..a0cccb37b
--- /dev/null
+++ b/tests/fixtures/complexity/Go/charts/operator/Chart.yaml
@@ -0,0 +1,3 @@
+apiVersion: v2
+name: operator
+version: 0.1.0
diff --git a/tests/fixtures/complexity/Go/install.go b/tests/fixtures/complexity/Go/install.go
new file mode 100644
index 000000000..89c7180c8
--- /dev/null
+++ b/tests/fixtures/complexity/Go/install.go
@@ -0,0 +1,8 @@
+package op
+
+import "helm.sh/helm/v3/pkg/chart/loader"
+
+func deployOperator() error {
+ _, err := loader.Load("charts/operator")
+ return err
+}
diff --git a/tests/fixtures/complexity/HCL/charts/app/Chart.yaml b/tests/fixtures/complexity/HCL/charts/app/Chart.yaml
new file mode 100644
index 000000000..4121cad31
--- /dev/null
+++ b/tests/fixtures/complexity/HCL/charts/app/Chart.yaml
@@ -0,0 +1,3 @@
+apiVersion: v2
+name: app
+version: 0.1.0
diff --git a/tests/fixtures/complexity/HCL/main.tf b/tests/fixtures/complexity/HCL/main.tf
new file mode 100644
index 000000000..14b399a79
--- /dev/null
+++ b/tests/fixtures/complexity/HCL/main.tf
@@ -0,0 +1,10 @@
+resource "helm_release" "this" {
+ name = "app"
+ chart = "${path.module}/charts/app"
+}
+
+resource "helm_release" "ingress" {
+ name = "ingress"
+ repository = "https://kubernetes.github.io/ingress-nginx"
+ chart = "ingress-nginx"
+}
diff --git a/tests/fixtures/complexity/Kubernetes/helmrelease.yaml b/tests/fixtures/complexity/Kubernetes/helmrelease.yaml
new file mode 100644
index 000000000..33d74c55e
--- /dev/null
+++ b/tests/fixtures/complexity/Kubernetes/helmrelease.yaml
@@ -0,0 +1,8 @@
+apiVersion: helm.toolkit.fluxcd.io/v2
+kind: HelmRelease
+metadata:
+ name: web
+spec:
+ chartRef:
+ kind: OCIRepository
+ name: web-chart
diff --git a/tests/fixtures/complexity/Kubernetes/kustomization.yaml b/tests/fixtures/complexity/Kubernetes/kustomization.yaml
index b8ebf3724..aea332b3f 100644
--- a/tests/fixtures/complexity/Kubernetes/kustomization.yaml
+++ b/tests/fixtures/complexity/Kubernetes/kustomization.yaml
@@ -4,3 +4,5 @@ namespace: shop
resources:
- app.yaml
- crd.yaml
+ - ocirepository.yaml
+ - helmrelease.yaml
diff --git a/tests/fixtures/complexity/Kubernetes/ocirepository.yaml b/tests/fixtures/complexity/Kubernetes/ocirepository.yaml
new file mode 100644
index 000000000..56289128e
--- /dev/null
+++ b/tests/fixtures/complexity/Kubernetes/ocirepository.yaml
@@ -0,0 +1,9 @@
+apiVersion: source.toolkit.fluxcd.io/v1
+kind: OCIRepository
+metadata:
+ name: web-chart
+spec:
+ interval: 15m
+ ref:
+ tag: 1.2.3
+ url: oci://ghcr.io/acme/charts/web-app
diff --git a/tests/test_complexity.c b/tests/test_complexity.c
index 16c707f56..da019f527 100644
--- a/tests/test_complexity.c
+++ b/tests/test_complexity.c
@@ -67,9 +67,12 @@
* lsp_resolve.h — re-declared here to avoid pulling that header's statics). */
extern _Atomic uint64_t g_lsp_tail_lookups;
extern _Atomic uint64_t g_lsp_tail_candidates;
-/* Infra resolver candidate counters (pass_k8s.c, pass_operator.c). */
+/* Infra resolver candidate counters (pass_k8s.c, pass_operator.c,
+ * pass_shell.c, pass_terraform.c). */
extern _Atomic uint64_t g_k8s_candidates;
extern _Atomic uint64_t g_op_candidates;
+extern _Atomic uint64_t g_sh_candidates;
+extern _Atomic uint64_t g_tf_candidates;
/* ── Corpus scale ──────────────────────────────────────────────────────
* Small on purpose: the gate reads exponents from ratios, not magnitudes.
@@ -169,8 +172,9 @@ static const CxTemplate CX_TEMPLATES[] = {
};
enum { CX_TEMPLATE_COUNT = sizeof(CX_TEMPLATES) / sizeof(CX_TEMPLATES[0]) };
-/* Fixture-dir provider: tests/fixtures/complexity/<lang-name>/ — every file in
- * it is copied verbatim into each module dir. Lets a new language join the
+/* Fixture-dir provider: tests/fixtures/complexity/<lang-name>/ — every file
+ * and directory in it (dot-names aside) is copied verbatim into each module
+ * dir. Lets a new language join the
* guard by dropping fixtures, with no edit to this suite. */
static bool cx_fixture_dir_for(CBMLanguage lang, char *out, size_t cap) {
const char *name = cbm_language_name(lang);
@@ -206,6 +210,36 @@ static int cx_copy_file(const char *src, const char *dst) {
return 0;
}
+/* A fixture entry into `dst`: a file, or a directory with its contents. */
+static int cx_copy_entry(const char *src, const char *dst, bool is_dir) {
+ if (!is_dir) {
+ return cx_copy_file(src, dst);
+ }
+ if (th_mkdir_p(dst) != 0) {
+ return -1;
+ }
+ cbm_dir_t *d = cbm_opendir(src);
+ if (!d) {
+ return -1;
+ }
+ int rc = 0;
+ cbm_dirent_t *entry;
+ while ((entry = cbm_readdir(d)) != NULL) {
+ if (entry->name[0] == '.') {
+ continue;
+ }
+ char from[1024];
+ char to[1024];
+ snprintf(from, sizeof(from), "%s/%s", src, entry->name);
+ snprintf(to, sizeof(to), "%s/%s", dst, entry->name);
+ if (cx_copy_entry(from, to, entry->is_dir) != 0) {
+ rc = -1;
+ }
+ }
+ cbm_closedir(d);
+ return rc;
+}
+
/* ── Corpus builder ──────────────────────────────────────────────────── */
static int cx_build_corpus(const char *root, int k_modules) {
@@ -257,7 +291,7 @@ static int cx_build_corpus(const char *root, int k_modules) {
char src[1024];
snprintf(src, sizeof(src), "%s/%s", fixdir, entry->name);
snprintf(path, sizeof(path), "%s/%s", dir, entry->name);
- (void)cx_copy_file(src, path);
+ (void)cx_copy_entry(src, path, entry->is_dir);
}
}
cbm_closedir(d);
@@ -339,13 +373,17 @@ static void cx_pass_sink(const char *line) {
}
/* Fixture-provided infra corpora (tests/fixtures/complexity/<name>/), scoped
- * by their copy root: Kubernetes manifests with a kustomization, and a Go
- * operator (controller, API types with +groupName, an embedded CRD). An API
+ * by their copy root: Kubernetes manifests with a kustomization (and a Flux
+ * HelmRelease whose chartRef names an OCIRepository), a Go operator
+ * (controller, API types with +groupName, an embedded CRD, a chart loader), a
+ * shell deploy chain (composite CI action -> make -> scripts -> kubectl / helm
+ * of the copy's own manifests and chart), and Terraform helm_releases (one of
+ * a chart dir of the module, one of an external chart). An API
* group/kind is one identity across a repository: copies declaring the same
* one ARE joined (a CRD links to every package marking its group), so the
* fixtures' CRDs use groups no copy's Go code declares -- the guard measures
* accidental growth, not that by-design join. */
-static const char *const CX_INFRA_DIRS[] = {"fx_Kubernetes", "fx_Go"};
+static const char *const CX_INFRA_DIRS[] = {"fx_Kubernetes", "fx_Go", "fx_Bash", "fx_HCL"};
enum { CX_INFRA_COUNT = sizeof(CX_INFRA_DIRS) / sizeof(CX_INFRA_DIRS[0]) };
typedef struct {
@@ -365,6 +403,8 @@ typedef struct {
uint64_t imp_name_visits; /* same-name-group members visited by that pass */
uint64_t k8s_candidates; /* objects the K8s resolvers examined */
uint64_t op_candidates; /* Go types / dirs the operator pass examined */
+ uint64_t sh_candidates; /* paths / make rules the shell layer examined */
+ uint64_t tf_candidates; /* blocks the Terraform layer examined */
int infra_nodes[CX_INFRA_COUNT];
int infra_edges[CX_INFRA_COUNT];
double wall_s;
@@ -391,6 +431,8 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
uint64_t iv0 = atomic_load_explicit(&g_importance_name_visits, memory_order_relaxed);
uint64_t kc0 = atomic_load_explicit(&g_k8s_candidates, memory_order_relaxed);
uint64_t oc0 = atomic_load_explicit(&g_op_candidates, memory_order_relaxed);
+ uint64_t sc0 = atomic_load_explicit(&g_sh_candidates, memory_order_relaxed);
+ uint64_t fc0 = atomic_load_explicit(&g_tf_candidates, memory_order_relaxed);
atomic_store_explicit(&g_cx_pass_count, 0, memory_order_relaxed);
cbm_log_set_sink_ex(cx_pass_sink, CBM_LOG_SINK_TEE);
@@ -430,6 +472,8 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
atomic_load_explicit(&g_importance_name_visits, memory_order_relaxed) - iv0;
out->k8s_candidates = atomic_load_explicit(&g_k8s_candidates, memory_order_relaxed) - kc0;
out->op_candidates = atomic_load_explicit(&g_op_candidates, memory_order_relaxed) - oc0;
+ out->sh_candidates = atomic_load_explicit(&g_sh_candidates, memory_order_relaxed) - sc0;
+ out->tf_candidates = atomic_load_explicit(&g_tf_candidates, memory_order_relaxed) - fc0;
cbm_store_t *s = cbm_store_open_path(db_path);
if (!s) {
@@ -547,8 +591,10 @@ TEST(complexity_replicated_modules_scale_linearly) {
PASS();
}
-/* The infra resolvers (K8s references / selectors / CRDs, the Go operator's
- * controller kinds and CRD types) examine candidates by name. Independent
+/* The infra resolvers (K8s references / selectors / CRDs / chartRefs, the Go
+ * operator's controller kinds, CRD types and loaded charts, the shell layer's
+ * manifest dirs and make rules, Terraform's helm_release blocks) examine
+ * candidates by name. Independent
* copies share every name, so a candidate set that is not scoped to the
* bundle / package / module grows with the copy count: the examined count
* goes ~4x per doubling while the edges stay ~2x. Both are gated, on the
@@ -569,6 +615,16 @@ TEST(complexity_infra_resolution_is_linear) {
printf(" k8s_candidates ratio %.2f op_candidates ratio %.2f\n", kr, orr);
ASSERT_TRUE(kr >= CX_RATIO_LO && kr <= CX_RATIO_HI);
ASSERT_TRUE(orr >= CX_RATIO_LO && orr <= CX_RATIO_HI);
+ printf(" sh_candidates %llu -> %llu tf_candidates %llu -> %llu\n",
+ (unsigned long long)a->sh_candidates, (unsigned long long)b->sh_candidates,
+ (unsigned long long)a->tf_candidates, (unsigned long long)b->tf_candidates);
+ ASSERT_GT((long long)a->sh_candidates, 0);
+ ASSERT_GT((long long)a->tf_candidates, 0);
+ double sr = cx_ratio((double)b->sh_candidates, (double)a->sh_candidates);
+ double fr = cx_ratio((double)b->tf_candidates, (double)a->tf_candidates);
+ printf(" sh_candidates ratio %.2f tf_candidates ratio %.2f\n", sr, fr);
+ ASSERT_TRUE(sr >= CX_RATIO_LO && sr <= CX_RATIO_HI);
+ ASSERT_TRUE(fr >= CX_RATIO_LO && fr <= CX_RATIO_HI);
for (int i = 0; i < CX_INFRA_COUNT; i++) {
ASSERT_GT(a->infra_nodes[i], 0);
ASSERT_GT(a->infra_edges[i], 0);