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.

feat(docker): an image a skaffold or compose file builds DEPENDS_ON its Dockerfile

Martin Vogelcbe2b420d654
diff --git a/src/graph_buffer/graph_buffer.c b/src/graph_buffer/graph_buffer.c
index 1e25b872a..03a3bc0dd 100644
--- a/src/graph_buffer/graph_buffer.c
+++ b/src/graph_buffer/graph_buffer.c
@@ -1855,16 +1855,15 @@ int cbm_gbuf_edge_count_by_type(const cbm_gbuf_t *gb, const char *type) {
return arr ? arr->count : 0;
}
-int cbm_gbuf_delete_edges_by_type(cbm_gbuf_t *gb, const char *type) {
- if (!gb || !type) {
- return CBM_NOT_FOUND;
- }
+/* Remove the edges `drop` selects from the array and the dedup index, then
+ * rebuild the secondary indexes. */
+typedef bool (*gbuf_edge_pred_t)(const cbm_gbuf_t *gb, const cbm_gbuf_edge_t *e, const void *ud);
- /* Remove edges of the given type from array and dedup index */
+static int gbuf_delete_edges_if(cbm_gbuf_t *gb, gbuf_edge_pred_t drop, const void *ud) {
int write_idx = 0;
for (int i = 0; i < gb->edges.count; i++) {
cbm_gbuf_edge_t *e = gb->edges.items[i];
- if (strcmp(e->type, type) == 0) {
+ if (drop(gb, e, ud)) {
char key[EDGE_KEY_BUF];
make_edge_key(key, sizeof(key), e->source_id, e->target_id, e->type,
e->properties_json);
@@ -1886,6 +1885,40 @@ int cbm_gbuf_delete_edges_by_type(cbm_gbuf_t *gb, const char *type) {
return 0;
}
+static bool gbuf_edge_is_type(const cbm_gbuf_t *gb, const cbm_gbuf_edge_t *e, const void *ud) {
+ (void)gb;
+ return strcmp(e->type, (const char *)ud) == 0;
+}
+
+int cbm_gbuf_delete_edges_by_type(cbm_gbuf_t *gb, const char *type) {
+ if (!gb || !type) {
+ return CBM_NOT_FOUND;
+ }
+ return gbuf_delete_edges_if(gb, gbuf_edge_is_type, type);
+}
+
+typedef struct {
+ const char *type;
+ const char *label;
+} gbuf_type_label_t;
+
+static bool gbuf_edge_from_label(const cbm_gbuf_t *gb, const cbm_gbuf_edge_t *e, const void *ud) {
+ const gbuf_type_label_t *tl = ud;
+ if (strcmp(e->type, tl->type) != 0) {
+ return false;
+ }
+ const cbm_gbuf_node_t *src = cbm_gbuf_find_by_id(gb, e->source_id);
+ return src && src->label && strcmp(src->label, tl->label) == 0;
+}
+
+int cbm_gbuf_delete_edges_from_label(cbm_gbuf_t *gb, const char *type, const char *label) {
+ if (!gb || !type || !label) {
+ return CBM_NOT_FOUND;
+ }
+ gbuf_type_label_t tl = {type, label};
+ return gbuf_delete_edges_if(gb, gbuf_edge_from_label, &tl);
+}
+
/* ── Merge ───────────────────────────────────────────────────────── */
/* Free remap hash table entries (key = heap string, value = heap int64_t*) */
diff --git a/src/graph_buffer/graph_buffer.h b/src/graph_buffer/graph_buffer.h
index 77e9f8441..66c3377c1 100644
--- a/src/graph_buffer/graph_buffer.h
+++ b/src/graph_buffer/graph_buffer.h
@@ -159,6 +159,9 @@ int cbm_gbuf_edge_count_by_type(const cbm_gbuf_t *gb, const char *type);
/* Delete all edges of a type. */
int cbm_gbuf_delete_edges_by_type(cbm_gbuf_t *gb, const char *type);
+/* Delete the edges of `type` whose source node has label `label`. */
+int cbm_gbuf_delete_edges_from_label(cbm_gbuf_t *gb, const char *type, const char *label);
+
/* ── Vector storage (for semantic embeddings) ───────────────────── */
/* Store an int8-quantized vector for a node. The vector data is copied.
diff --git a/src/pipeline/pass_ci.c b/src/pipeline/pass_ci.c
index 2804e78d1..fcd5d63f2 100644
--- a/src/pipeline/pass_ci.c
+++ b/src/pipeline/pass_ci.c
@@ -29,6 +29,7 @@
#include "cbm.h"
#include "lang_specs.h"
+#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -50,6 +51,7 @@ typedef struct {
const char *src;
const char *rel;
cbm_ci_cmd_fn fn;
+ cbm_build_site_fn site_fn; /* a build-site walk's callback */
void *ud;
} ci_t;
@@ -680,6 +682,358 @@ static bool ci_action_path(const char *rel) {
return strcmp(base, "action.yml") == 0 || strcmp(base, "action.yaml") == 0;
}
+/* ── Build configurations (PHASE 4 P4.3) ──────────────────────────────
+ * Which Dockerfile builds which image, as a skaffold or compose file says:
+ * - skaffold: every build.artifacts[] item (top level, profiles) of a
+ * document whose apiVersion is skaffold/...: image, context (default the
+ * file's directory), docker.dockerfile (default Dockerfile, relative to
+ * the context); an artifact of another builder (jib, ko, bazel,
+ * buildpacks, custom, kaniko) is none.
+ * - compose: services.<name> with build (a context, or context /
+ * dockerfile) and image; ${X}, ${X:-d}, ${X-d} and $X from the project's
+ * .env (a variable without a value: none). */
+
+static bool ci_build_ident(const char *s, size_t n) {
+ if (n == 0 || !(isalpha((unsigned char)s[0]) || s[0] == '_')) {
+ return false;
+ }
+ for (size_t i = SKIP_ONE; i < n; i++) {
+ if (!isalnum((unsigned char)s[i]) && s[i] != '_') {
+ return false;
+ }
+ }
+ return true;
+}
+
+/* Value v[0, *len) without the quotes around it (*len shortened). */
+static const char *ci_unquote(const char *v, size_t *len) {
+ size_t l = *len;
+ if (l >= PAIR_LEN && (v[0] == '"' || v[0] == '\'') && v[l - SKIP_ONE] == v[0]) {
+ *len = l - PAIR_LEN;
+ return v + SKIP_ONE;
+ }
+ return v;
+}
+
+/* One .env line s[0, n): KEY=VALUE (an `export ` prefix, quotes around the
+ * value removed) appended to env; a comment, a blank or another line: none. */
+static void ci_dotenv_line(CBMArena *a, const char *s, size_t n, ci_env_t *env) {
+ static const char EXPORT[] = "export ";
+ size_t b = 0;
+ while (b < n && isspace((unsigned char)s[b])) {
+ b++;
+ }
+ while (n > b && isspace((unsigned char)s[n - SKIP_ONE])) {
+ n--;
+ }
+ if (n - b > sizeof(EXPORT) - SKIP_ONE &&
+ strncmp(s + b, EXPORT, sizeof(EXPORT) - SKIP_ONE) == 0) {
+ b += sizeof(EXPORT) - SKIP_ONE;
+ }
+ const char *eq = b < n && s[b] != '#' ? memchr(s + b, '=', n - b) : NULL;
+ size_t kl = eq ? (size_t)(eq - (s + b)) : 0;
+ if (!eq || !ci_build_ident(s + b, kl)) {
+ return;
+ }
+ size_t v = b + kl + SKIP_ONE;
+ while (v < n && isspace((unsigned char)s[v])) {
+ v++;
+ }
+ size_t vl = n - v;
+ const char *val = ci_unquote(s + v, &vl);
+ env->kv[PAIR_LEN * env->n] = cbm_arena_strndup(a, s + b, kl);
+ env->kv[PAIR_LEN * env->n + SKIP_ONE] = cbm_arena_strndup(a, val, vl);
+ env->n++;
+}
+
+/* KEY=VALUE pairs of a .env text (quotes removed; a later key wins). */
+static ci_env_t ci_dotenv(CBMArena *a, const char *text) {
+ size_t lines = SKIP_ONE;
+ for (const char *q = text; *q; q++) {
+ lines += *q == '\n';
+ }
+ ci_env_t env = {cbm_arena_alloc(a, lines * PAIR_LEN * sizeof(char *)), 0};
+ for (const char *p = text; env.kv && p && *p;) {
+ const char *e = strchr(p, '\n');
+ ci_dotenv_line(a, p, e ? (size_t)(e - p) : strlen(p), &env);
+ p = e ? e + SKIP_ONE : NULL;
+ }
+ return env;
+}
+
+static const char *ci_env_lookup(const ci_env_t *env, const char *name, size_t nl) {
+ for (int i = env->n - SKIP_ONE; i >= 0; i--) {
+ const char *k = env->kv[(size_t)i * PAIR_LEN];
+ if (k && strlen(k) == nl && strncmp(k, name, nl) == 0) {
+ return env->kv[(size_t)i * PAIR_LEN + SKIP_ONE];
+ }
+ }
+ return NULL;
+}
+
+/* `s` with ${X} / ${X:-d} / ${X-d} / $X expanded from env; NULL when a
+ * variable has no value (and no default). */
+static const char *ci_interp(CBMArena *a, const char *s, const ci_env_t *env) {
+ if (!s || !strchr(s, '$')) {
+ return s;
+ }
+ size_t cap = strlen(s) + SKIP_ONE;
+ for (int i = 0; i < env->n; i++) {
+ const char *v = env->kv[(size_t)i * PAIR_LEN + SKIP_ONE];
+ cap += v ? strlen(v) : 0;
+ }
+ char *out = cbm_arena_alloc(a, cap);
+ size_t len = 0;
+ for (const char *p = s; out && *p;) {
+ if (*p != '$') {
+ out[len++] = *p++;
+ continue;
+ }
+ bool braced = p[SKIP_ONE] == '{';
+ const char *name = p + (braced ? PAIR_LEN : SKIP_ONE);
+ size_t nl = 0;
+ while (isalnum((unsigned char)name[nl]) || name[nl] == '_') {
+ nl++;
+ }
+ if (nl == 0 || !ci_build_ident(name, nl)) {
+ return NULL;
+ }
+ const char *after = name + nl;
+ const char *dflt = NULL;
+ size_t dl = 0;
+ bool colon = false;
+ if (braced) {
+ const char *close = strchr(after, '}');
+ if (!close) {
+ return NULL;
+ }
+ if (after[0] == ':' && after[SKIP_ONE] == '-') {
+ colon = true;
+ dflt = after + PAIR_LEN;
+ } else if (after[0] == '-') {
+ dflt = after + SKIP_ONE;
+ } else if (after != close) {
+ return NULL;
+ }
+ dl = dflt ? (size_t)(close - dflt) : 0;
+ after = close + SKIP_ONE;
+ }
+ const char *v = ci_env_lookup(env, name, nl);
+ const char *use = v;
+ size_t ul = v ? strlen(v) : 0;
+ if (!v || (colon && !v[0])) {
+ if (!dflt) {
+ return NULL;
+ }
+ use = dflt;
+ ul = dl;
+ }
+ if (len + ul + SKIP_ONE > cap) {
+ return NULL;
+ }
+ memcpy(out + len, use, ul);
+ len += ul;
+ p = after;
+ }
+ if (out) {
+ out[len] = '\0';
+ }
+ return out;
+}
+
+/* Repository path of `rel` under repository directory `base`, normalised;
+ * NULL when it leaves the repository or is absolute. */
+static const char *ci_repo_path(CBMArena *a, const char *base, const char *rel) {
+ if (!rel || rel[0] == '/' || strstr(rel, "://")) {
+ return NULL;
+ }
+ size_t cap = strlen(base) + strlen(rel) + PAIR_LEN;
+ char *out = cbm_arena_alloc(a, cap);
+ if (!out) {
+ return NULL;
+ }
+ size_t len = 0;
+ const char *parts[PAIR_LEN] = {base, rel};
+ for (int k = 0; k < PAIR_LEN; k++) {
+ for (const char *s = parts[k]; *s;) {
+ while (*s == '/') {
+ s++;
+ }
+ const char *e = s;
+ while (*e && *e != '/') {
+ e++;
+ }
+ size_t sl = (size_t)(e - s);
+ if (sl == PAIR_LEN && s[0] == '.' && s[SKIP_ONE] == '.') {
+ if (len == 0) {
+ return NULL;
+ }
+ while (len > 0 && out[len - SKIP_ONE] != '/') {
+ len--;
+ }
+ len = len > 0 ? len - SKIP_ONE : 0;
+ } else if (sl > 0 && !(sl == SKIP_ONE && s[0] == '.')) {
+ if (len > 0) {
+ out[len++] = '/';
+ }
+ memcpy(out + len, s, sl);
+ len += sl;
+ }
+ s = e;
+ }
+ }
+ out[len] = '\0';
+ return out;
+}
+
+static void ci_build_emit(const ci_t *c, const char *via, const char *image, const char *cdir,
+ const char *dockerfile, int row) {
+ const char *df = cdir ? ci_repo_path(c->a, cdir, dockerfile) : NULL;
+ if (!image || !df) {
+ return;
+ }
+ cbm_build_site_t site = {.via = via, .image = image, .dockerfile = df, .line = row + SKIP_ONE};
+ c->site_fn(&site, c->ud);
+}
+
+static void ci_skaffold_artifact(const ci_t *c, TSNode art, const char *dir) {
+ static const char *const OTHER[] = {"jib", "ko", "bazel", "buildpacks",
+ "custom", "kaniko", NULL};
+ for (int i = 0; OTHER[i]; i++) {
+ if (!ts_node_is_null(ci_get(c, art, OTHER[i]))) {
+ return;
+ }
+ }
+ int row = 0;
+ const char *image = ci_scalar(c, ci_get(c, art, "image"), &row);
+ const char *ctx = ci_get_str(c, art, "context");
+ const char *df = ci_get_str(c, ci_get(c, art, "docker"), "dockerfile");
+ const char *cdir = ci_repo_path(c->a, dir, ctx ? ctx : ".");
+ ci_build_emit(c, "skaffold", image, cdir, df ? df : "Dockerfile", row);
+}
+
+/* Every build.artifacts list under `n` (any depth: top level, profiles). */
+static void ci_skaffold_walk(const ci_t *c, TSNode n, const char *dir, int depth) {
+ enum { CI_SKAFFOLD_DEPTH = 16 };
+ if (depth > CI_SKAFFOLD_DEPTH) {
+ return;
+ }
+ int np = 0;
+ ci_pair_t *pairs = ci_pairs(c, n, &np);
+ for (int i = 0; i < np; i++) {
+ if (strcmp(pairs[i].key, "build") == 0) {
+ int na = 0;
+ TSNode *arts = ci_items(c, ci_get(c, pairs[i].value, "artifacts"), &na);
+ for (int k = 0; k < na; k++) {
+ ci_skaffold_artifact(c, arts[k], dir);
+ }
+ }
+ ci_skaffold_walk(c, pairs[i].value, dir, depth + SKIP_ONE);
+ }
+ int ni = 0;
+ TSNode *items = ci_items(c, n, &ni);
+ for (int i = 0; i < ni; i++) {
+ ci_skaffold_walk(c, items[i], dir, depth + SKIP_ONE);
+ }
+}
+
+static void ci_compose(const ci_t *c, TSNode root, const char *dir, const ci_env_t *env) {
+ int ns = 0;
+ ci_pair_t *svcs = ci_pairs(c, ci_get(c, root, "services"), &ns);
+ for (int i = 0; i < ns; i++) {
+ TSNode svc = svcs[i].value;
+ TSNode build = ci_get(c, svc, "build");
+ int row = 0;
+ const char *image = ci_scalar(c, ci_get(c, svc, "image"), &row);
+ if (ts_node_is_null(build) || !image) {
+ continue;
+ }
+ const char *ctx = ci_scalar(c, build, NULL);
+ const char *df = "Dockerfile";
+ if (!ctx) {
+ ctx = ci_get_str(c, build, "context");
+ const char *d = ci_get_str(c, build, "dockerfile");
+ ctx = ctx ? ctx : ".";
+ df = d ? d : df;
+ }
+ ctx = ci_interp(c->a, ctx, env);
+ df = ci_interp(c->a, df, env);
+ image = ci_interp(c->a, image, env);
+ if (!ctx || !df || !image) {
+ continue;
+ }
+ ci_build_emit(c, "compose", image, ci_repo_path(c->a, dir, ctx), df, row);
+ }
+}
+
+bool cbm_build_path(const char *rel) {
+ if (!rel || !ci_yaml_ext(rel)) {
+ return false;
+ }
+ const char *slash = strrchr(rel, '/');
+ const char *base = slash ? slash + SKIP_ONE : rel;
+ if (strncmp(base, "skaffold", strlen("skaffold")) == 0) {
+ return true;
+ }
+ const char *b =
+ strncmp(base, "docker-", strlen("docker-")) == 0 ? base + strlen("docker-") : base;
+ size_t cl = strlen("compose");
+ return strncmp(b, "compose", cl) == 0 && (b[cl] == '.' || b[cl] == '-' || b[cl] == '_');
+}
+
+void cbm_build_sites(CBMArena *a, const char *rel, const char *src, size_t len, const char *dotenv,
+ cbm_build_site_fn fn, void *ud) {
+ if (!cbm_build_path(rel)) {
+ return;
+ }
+ TSParser *parser = ts_parser_new();
+ if (!parser) {
+ return;
+ }
+ TSTree *tree = NULL;
+ if (ts_parser_set_language(parser, cbm_ts_language(CBM_LANG_YAML))) {
+ tree = ts_parser_parse_string(parser, NULL, src, (uint32_t)len);
+ }
+ const char *slash = strrchr(rel, '/');
+ const char *dir = slash ? cbm_arena_strndup(a, rel, (size_t)(slash - rel)) : "";
+ const char *base = slash ? slash + SKIP_ONE : rel;
+ bool skaffold = strncmp(base, "skaffold", strlen("skaffold")) == 0;
+ ci_env_t env = {0};
+ if (!skaffold && dotenv) {
+ env = ci_dotenv(a, dotenv);
+ }
+ if (tree && dir) {
+ ci_t c = {.a = a, .src = src, .rel = rel, .site_fn = fn, .ud = ud};
+ TSNode stream = ts_tree_root_node(tree);
+ for (uint32_t i = 0; i < ts_node_named_child_count(stream); i++) {
+ TSNode doc = ts_node_named_child(stream, i);
+ if (!ci_type(doc, "document")) {
+ continue;
+ }
+ TSNode root = {0};
+ for (uint32_t k = 0; k < ts_node_named_child_count(doc); k++) {
+ TSNode x = ts_node_named_child(doc, k);
+ if (!ci_type(x, "comment")) {
+ root = x;
+ break;
+ }
+ }
+ if (skaffold) {
+ const char *api = ci_get_str(&c, root, "apiVersion");
+ if (api && strncmp(api, "skaffold/", strlen("skaffold/")) == 0) {
+ ci_skaffold_walk(&c, root, dir, 0);
+ }
+ } else {
+ ci_compose(&c, root, dir, &env);
+ }
+ }
+ }
+ if (tree) {
+ ts_tree_delete(tree);
+ }
+ ts_parser_delete(parser);
+}
+
bool cbm_ci_path(const char *rel) {
return rel && (ci_workflow_path(rel) || ci_action_path(rel));
}
diff --git a/src/pipeline/pass_docker.c b/src/pipeline/pass_docker.c
index c431450ef..896afb9dc 100644
--- a/src/pipeline/pass_docker.c
+++ b/src/pipeline/pass_docker.c
@@ -1097,6 +1097,84 @@ static char *dk_read(const char *path, size_t *len) {
return buf;
}
+typedef struct {
+ cbm_pipeline_ctx_t *ctx;
+ CBMArena *a;
+ const char *file;
+ int edges;
+} dk_builds_t;
+
+/* An Image a build configuration builds DEPENDS_ON {via:build} the
+ * Dockerfile it is built from. */
+static void dk_build_site(const cbm_build_site_t *s, void *ud) {
+ dk_builds_t *b = ud;
+ const char *mqn = cbm_fqn_module(b->a, b->ctx->project_name, s->dockerfile);
+ const cbm_gbuf_node_t *mod = mqn ? cbm_gbuf_find_by_qn(b->ctx->gbuf, mqn) : NULL;
+ if (!mod || !mod->file_path || strcmp(mod->file_path, s->dockerfile) != 0) {
+ return;
+ }
+ int64_t img = cbm_pipeline_image_node(b->ctx, s->image);
+ if (img <= 0) {
+ return;
+ }
+ char esc_file[CBM_SZ_1K];
+ char esc_img[CBM_SZ_1K];
+ cbm_json_escape(esc_file, (int)sizeof(esc_file), b->file);
+ cbm_json_escape(esc_img, (int)sizeof(esc_img), s->image);
+ char props[CBM_SZ_4K];
+ snprintf(props, sizeof(props),
+ "{\"via\":\"build\",\"tool\":\"%s\",\"file\":\"%s\",\"image\":\"%s\",\"line\":%d}",
+ s->via, esc_file, esc_img, s->line);
+ cbm_gbuf_insert_edge(b->ctx->gbuf, img, mod->id, "DEPENDS_ON", props);
+ b->edges++;
+}
+
+/* The .env of the directory of `rel` under the repository, NULL when none. */
+static char *dk_read_dotenv(const cbm_pipeline_ctx_t *ctx, const char *rel) {
+ if (!ctx->repo_path) {
+ return NULL;
+ }
+ const char *slash = strrchr(rel, '/');
+ char path[CBM_SZ_4K];
+ snprintf(path, sizeof(path), "%s/%.*s%s.env", ctx->repo_path, slash ? (int)(slash - rel) : 0,
+ rel, slash ? "/" : "");
+ size_t len = 0;
+ return dk_read(path, &len);
+}
+
+/* Every build configuration of the repository -- read from disk through the
+ * File nodes, so an incremental run (which recomputes these edges whole)
+ * sees the unchanged ones too. */
+static int dk_link_builds(cbm_pipeline_ctx_t *ctx, CBMArena *a) {
+ const cbm_gbuf_node_t **files = NULL;
+ int n = 0;
+ if (!ctx->repo_path || cbm_gbuf_find_by_label(ctx->gbuf, "File", &files, &n) != 0) {
+ return 0;
+ }
+ dk_builds_t b = {.ctx = ctx, .a = a};
+ for (int i = 0; i < n; i++) {
+ const char *rel = files[i]->file_path;
+ if (!rel || !cbm_build_path(rel)) {
+ continue;
+ }
+ char path[CBM_SZ_4K];
+ snprintf(path, sizeof(path), "%s/%s", ctx->repo_path, rel);
+ size_t len = 0;
+ char *src = dk_read(path, &len);
+ if (!src) {
+ continue;
+ }
+ char *dotenv = dk_read_dotenv(ctx, rel);
+ b.file = rel;
+ cbm_build_sites(a, rel, src, len, dotenv, dk_build_site, &b);
+ if (dotenv) {
+ cbm_free(CBM_MEM_CLASS_OTHER, dotenv);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, src);
+ }
+ return b.edges;
+}
+
static bool dk_node_before(const cbm_gbuf_node_t *a, const cbm_gbuf_node_t *b) {
int c = strcmp(a->file_path, b->file_path);
return c < 0 || (c == 0 && a->start_line < b->start_line);
@@ -1205,6 +1283,7 @@ void cbm_pipeline_pass_docker(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *fi
if (ix.gomain) {
cbm_ht_free(ix.gomain);
}
+ edges += dk_link_builds(ctx, &arena);
cbm_arena_destroy(&arena);
if (edges > 0) {
char buf[CBM_SZ_32];
diff --git a/src/pipeline/pipeline_delta.c b/src/pipeline/pipeline_delta.c
index 367fbdf13..48f5eb638 100644
--- a/src/pipeline/pipeline_delta.c
+++ b/src/pipeline/pipeline_delta.c
@@ -283,6 +283,27 @@ int cbm_delta_drop_unreferenced(cbm_store_t *store, const char *project, const c
return rc == SQLITE_DONE ? sqlite3_changes(db) : CBM_NOT_FOUND;
}
+int cbm_delta_drop_edges_from_label(cbm_store_t *store, const char *project, const char *type,
+ const char *label) {
+ sqlite3 *db = cbm_store_get_db(store);
+ if (!db) {
+ return CBM_NOT_FOUND;
+ }
+ sqlite3_stmt *stmt = NULL;
+ if (sqlite3_prepare_v2(db,
+ "DELETE FROM edges WHERE project = ?1 AND type = ?2 AND source_id IN"
+ " (SELECT id FROM nodes WHERE project = ?1 AND label = ?3)",
+ CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
+ return CBM_NOT_FOUND;
+ }
+ sqlite3_bind_text(stmt, 1, project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(stmt, 2, type, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(stmt, 3, label, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ int rc = sqlite3_step(stmt);
+ sqlite3_finalize(stmt);
+ return rc == SQLITE_DONE ? sqlite3_changes(db) : CBM_NOT_FOUND;
+}
+
/* Every project node becomes a proxy. The first cut filtered by label and
* the fail-closed patch immediately found the counterexamples (synthetic
* Decorator nodes on django; Macro is six of the kernel's 8.5M nodes) — any
@@ -448,6 +469,7 @@ typedef struct {
sqlite3 *db;
const char *project;
int64_t max_db_id;
+ const cbm_gbuf_t *gbuf;
sqlite3_stmt *node_stmt;
sqlite3_stmt *edge_stmt;
sqlite3_stmt *qn_lookup;
@@ -561,6 +583,17 @@ static void delta_patch_node(const cbm_gbuf_node_t *node, void *userdata) {
ctx->nodes++;
}
+/* An edge of the class recomputed wholesale on every run (an Image's build
+ * edges): the staging graph dropped its old set, so it is written even
+ * between pre-existing nodes. */
+static bool delta_edge_recomputed(const delta_patch_ctx_t *ctx, const cbm_gbuf_edge_t *edge) {
+ if (!ctx->gbuf || strcmp(edge->type, CBM_BUILD_EDGE_TYPE) != 0) {
+ return false;
+ }
+ const cbm_gbuf_node_t *src = cbm_gbuf_find_by_id(ctx->gbuf, edge->source_id);
+ return src && src->label && strcmp(src->label, CBM_BUILD_EDGE_SOURCE) == 0;
+}
+
static void delta_patch_edge(const cbm_gbuf_edge_t *edge, void *userdata) {
delta_patch_ctx_t *ctx = (delta_patch_ctx_t *)userdata;
if (ctx->failed) {
@@ -569,7 +602,7 @@ static void delta_patch_edge(const cbm_gbuf_edge_t *edge, void *userdata) {
int64_t src = delta_remap_get(ctx, edge->source_id);
int64_t tgt = delta_remap_get(ctx, edge->target_id);
if (src <= ctx->max_db_id && tgt <= ctx->max_db_id && src == edge->source_id &&
- tgt == edge->target_id) {
+ tgt == edge->target_id && !delta_edge_recomputed(ctx, edge)) {
return; /* both endpoints pre-existed unremapped: already recorded */
}
sqlite3_reset(ctx->edge_stmt);
@@ -635,7 +668,7 @@ int cbm_delta_patch(cbm_store_t *store, const char *project, cbm_gbuf_t *gbuf, i
/* This writes the buffer's nodes itself: settle the collision spans first,
* as the buffer's own dump and flush do. */
cbm_gbuf_settle_variants(gbuf);
- delta_patch_ctx_t ctx = {.db = db, .project = project, .max_db_id = max_db_id};
+ delta_patch_ctx_t ctx = {.db = db, .project = project, .max_db_id = max_db_id, .gbuf = gbuf};
if (sqlite3_prepare_v2(db,
"SELECT id FROM nodes WHERE project = ?1"
" AND qualified_name = ?2",
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index 75873c04b..1d90c0dd7 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -3080,6 +3080,14 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
}
}
+ /* An Image's build edges (Image -> Dockerfile, pass_docker.c) belong to
+ * the skaffold / compose file declaring them, not to either end: the K8s
+ * pass just recomputed them from every build file, so they replace the
+ * staging set wholesale. */
+ if (cbm_delta_drop_edges_from_label(staging, project, CBM_BUILD_EDGE_TYPE,
+ CBM_BUILD_EDGE_SOURCE) < 0) {
+ goto out;
+ }
cbm_clock_gettime(CLOCK_MONOTONIC, &t);
if (cbm_delta_patch(staging, project, gbuf, max_db_id, snapshot, snapshot_count) != 0) {
goto out;
@@ -3598,6 +3606,12 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
legacy_doc_load(&legacy_doc, store, project, changed_files, ci, deleted, deleted_count);
cbm_store_close(store);
+ /* An Image's build edges (Image -> Dockerfile, pass_docker.c) belong to
+ * the skaffold / compose file declaring them, not to either end: the K8s
+ * pass recomputes them all from every build file, so none is kept (nor
+ * captured below). */
+ (void)cbm_gbuf_delete_edges_from_label(existing, CBM_BUILD_EDGE_TYPE, CBM_BUILD_EDGE_SOURCE);
+
/* Snapshot inbound cross-file edges into changed files BEFORE purging, so
* the cascade delete doesn't permanently drop edges whose source lives in
* an unchanged (never-re-parsed) file. Re-linked after re-resolution. */
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index f77981f23..a6bc7a088 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -1074,6 +1074,26 @@ bool cbm_ci_path(const char *rel);
void cbm_ci_commands(CBMArena *a, const char *rel, const char *src, size_t len, cbm_ci_cmd_fn fn,
void *ud);
+/* An image a build configuration builds from a Dockerfile (pass_ci.c). */
+typedef struct {
+ const char *via; /* "skaffold" / "compose" */
+ const char *image; /* the reference, variables expanded */
+ const char *dockerfile; /* repository path */
+ int line;
+} cbm_build_site_t;
+typedef void (*cbm_build_site_fn)(const cbm_build_site_t *site, void *ud);
+
+/* Whether `rel` names a build configuration: skaffold*.yaml|yml,
+ * [docker-]compose[.x].yaml|yml. */
+bool cbm_build_path(const char *rel);
+
+/* Hand fn each image build configuration `rel` (YAML text src) declares:
+ * skaffold build.artifacts (top level and profiles; the docker builder),
+ * compose services with build and image. `dotenv` (may be NULL): the text of
+ * the compose project's .env, whose variables compose interpolates. */
+void cbm_build_sites(CBMArena *a, const char *rel, const char *src, size_t len, const char *dotenv,
+ cbm_build_site_fn fn, void *ud);
+
/* Terraform / OpenTofu modules (pass_terraform.c): a helm_release DEPLOYS its
* chart. Run at the end of the K8s pass, over the same files. */
void cbm_pipeline_pass_terraform(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
@@ -1306,6 +1326,18 @@ int cbm_delta_purge(cbm_store_t *store, const char *project, const char *const *
* on failure. */
int cbm_delta_drop_unreferenced(cbm_store_t *store, const char *project, const char *label,
const char *const *edge_types);
+/* An Image's build edges (Image -DEPENDS_ON {via:build}-> Dockerfile,
+ * pass_docker.c) belong to the build configuration declaring them, not to
+ * either end: every run recomputes them whole from all build files, the
+ * incremental routes drop the old set first and write the new one even
+ * between pre-existing nodes. */
+#define CBM_BUILD_EDGE_TYPE "DEPENDS_ON"
+#define CBM_BUILD_EDGE_SOURCE "Image"
+
+/* Delete the project's `type` edges whose source node has label `label`;
+ * returns how many, or -1 on failure. */
+int cbm_delta_drop_edges_from_label(cbm_store_t *store, const char *project, const char *type,
+ const char *label);
/* The edges that keep a file-less shared node alive. */
#define SH_PROCESS_EDGES ((const char *const[]){"SPAWNS", NULL})
#define DK_IMAGE_EDGES ((const char *const[]){"DEPENDS_ON", "USAGE", NULL})
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index 77432e750..ac37ebaa6 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -2051,6 +2051,69 @@ TEST(probe_k8s_container_images_are_images) {
PASS();
}
+/* Every DEPENDS_ON {via:build} edge as "<image> -> <dockerfile> (tool)". */
+static char *gpf_builds(const GpfFile *files, int nfiles) {
+ GpfProj lp;
+ cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
+ char *out = NULL;
+ if (store) {
+ sqlite3_stmt *st = NULL;
+ const char *sql =
+ "SELECT coalesce(group_concat(x, char(10)), '') FROM (SELECT s.name || ' -> ' || "
+ "t.file_path || ' (' || json_extract(e.properties,'$.tool') || ')' AS x FROM edges e "
+ "JOIN nodes s ON s.id=e.source_id JOIN nodes t ON t.id=e.target_id WHERE "
+ "e.project=?1 AND e.type='DEPENDS_ON' AND s.label='Image' AND "
+ "json_extract(e.properties,'$.via')='build' ORDER BY x)";
+ if (sqlite3_prepare_v2(cbm_store_get_db(store), sql, -1, &st, NULL) == SQLITE_OK) {
+ sqlite3_bind_text(st, 1, lp.project, -1, SQLITE_STATIC);
+ if (sqlite3_step(st) == SQLITE_ROW) {
+ out = strdup((const char *)sqlite3_column_text(st, 0));
+ }
+ }
+ sqlite3_finalize(st);
+ }
+ gpf_cleanup(&lp, store);
+ return out;
+}
+
+/* An image a build configuration builds DEPENDS_ON {via:build} its
+ * Dockerfile: skaffold artifacts (top level and profiles; docker.dockerfile
+ * relative to the context; a ko artifact is none), compose services with
+ * build and image, variables from the project's .env. A service without an
+ * image or a build, or a build without a Dockerfile, is none. */
+TEST(probe_build_configs_build_images_from_dockerfiles) {
+ static const GpfFile files[] = {
+ {"src/web/Dockerfile", "FROM nginx:1.27\n"},
+ {"src/api/Dockerfile.prod", "FROM golang:1.22\n"},
+ {"skaffold.yaml",
+ "apiVersion: skaffold/v4beta6\nkind: Config\nbuild:\n artifacts:\n"
+ " - image: web\n context: src/web\n"
+ " - image: api\n context: src/api\n docker:\n"
+ " dockerfile: Dockerfile.prod\n"
+ " - image: koapp\n ko:\n main: ./cmd\n"
+ "profiles:\n - name: dev\n build:\n artifacts:\n"
+ " - image: dev-web\n context: src/web\n"},
+ {".env", "REG=ghcr.io/acme\n# comment\nexport UNUSED='x'\n"},
+ {"compose.yaml", "services:\n frontend:\n image: ${REG}/frontend:${TAG:-1}\n"
+ " build:\n context: ./src/web\n"
+ " worker:\n build: ./src/api\n"
+ " db:\n image: postgres:16\n"
+ " missing:\n image: acme/missing\n build: ./nope\n"
+ " unknown:\n image: ${NOPE}/x\n build: ./src/web\n"}};
+ char *got = gpf_builds(files, 5);
+ static const char want[] = "docker.io/library/api -> src/api/Dockerfile.prod (skaffold)\n"
+ "docker.io/library/dev-web -> src/web/Dockerfile (skaffold)\n"
+ "docker.io/library/web -> src/web/Dockerfile (skaffold)\n"
+ "ghcr.io/acme/frontend -> src/web/Dockerfile (compose)";
+ if (!got || strcmp(got, want) != 0) {
+ fprintf(stderr, "builds got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
+ free(got);
+ FAIL("DEPENDS_ON {via:build} edge set");
+ }
+ free(got);
+ PASS();
+}
+
#define GPF_CM(name) "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: " name "\n"
#define GPF_KUST "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\n"
@@ -4318,6 +4381,7 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_dockerfile_variants_are_dockerfiles);
RUN_TEST(probe_dockerfile_entrypoint_runs);
RUN_TEST(probe_k8s_container_images_are_images);
+ RUN_TEST(probe_build_configs_build_images_from_dockerfiles);
RUN_TEST(probe_gitops_objects_deploy_their_units);
RUN_TEST(probe_shell_kubectl_deploys_what_it_applies);
RUN_TEST(probe_shell_helm_deploys_its_chart);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index d6a4ae3b9..9b2b4eaf5 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -3541,6 +3541,130 @@ static int incremental_dockerfile_delete(bool legacy, cbm_incremental_route_t *r
return full == 2 ? after : -1;
}
+/* Editing one Dockerfile keeps every image's build edge -- also the one
+ * between nodes that both pre-existed (Image -> the unchanged Dockerfile),
+ * which the incremental routes recompute whole. `legacy`: through the
+ * full-load route, else the closure-repair route. Returns the build edge
+ * count before / after as "full=N inc=M", or NULL; *route: the route taken. */
+static char *incremental_build_keep(bool legacy, cbm_incremental_route_t *route) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_buildkeep_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ return NULL;
+ }
+ write_temp_file(tmp, "src/web/Dockerfile", "FROM nginx:1.27\n");
+ write_temp_file(tmp, "src/api/Dockerfile", "FROM golang:1.22\n");
+ write_temp_file(tmp, "skaffold.yaml",
+ "apiVersion: skaffold/v4beta6\nkind: Config\nbuild:\n artifacts:\n"
+ " - image: web\n context: src/web\n"
+ " - image: api\n context: src/api\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/buildkeep.db", tmp);
+ static const char builds[] =
+ "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id WHERE e.type='DEPENDS_ON' "
+ "AND s.label='Image' AND json_extract(e.properties,'$.via')='build'";
+ int full = k8s_inc_index(tmp, db_path) == 0 ? k8s_inc_count(db_path, builds) : -1;
+ write_temp_file(tmp, "src/web/Dockerfile", "FROM nginx:1.27\n# edited\n");
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ if (legacy) {
+ cbm_pipeline_incremental_test_force_legacy_partial_once();
+ }
+#else
+ (void)legacy;
+#endif
+ char *out = NULL;
+ if (k8s_inc_index(tmp, db_path) == 0) {
+ char buf[64];
+ snprintf(buf, sizeof(buf), "full=%d inc=%d", full, k8s_inc_count(db_path, builds));
+ out = strdup(buf);
+ }
+ *route = cbm_pipeline_incremental_test_last_route();
+ th_rmtree(tmp);
+ return out;
+}
+
+TEST(incremental_dockerfile_edit_keeps_build_edges) {
+ cbm_incremental_route_t route = CBM_INCREMENTAL_ROUTE_NONE;
+ char *got = incremental_build_keep(false, &route);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
+ ASSERT_NOT_NULL(got);
+ ASSERT_STR_EQ(got, "full=2 inc=2");
+ free(got);
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ got = incremental_build_keep(true, &route);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);
+ ASSERT_NOT_NULL(got);
+ ASSERT_STR_EQ(got, "full=2 inc=2");
+ free(got);
+#endif
+ PASS();
+}
+
+/* A compose file's edit replaces its build edges: the renamed image keeps
+ * no edge (nor node), the new one has its edge. `legacy`: through the
+ * full-load route, else the closure-repair route. Returns "old=N new=M"
+ * after the edit, or NULL; *route: the route taken. */
+static char *incremental_compose_edit(bool legacy, cbm_incremental_route_t *route) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_composeedit_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ return NULL;
+ }
+ write_temp_file(tmp, "src/web/Dockerfile", "FROM nginx:1.27\n");
+ write_temp_file(tmp, "src/api/Dockerfile", "FROM golang:1.22\n");
+ write_temp_file(tmp, "compose.yaml",
+ "services:\n web:\n image: acme/web-old\n build: ./src/web\n"
+ " api:\n image: acme/api\n build: ./src/api\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/composeedit.db", tmp);
+ static const char old_img[] = "SELECT COUNT(*) FROM nodes WHERE label='Image' AND "
+ "name='docker.io/acme/web-old'";
+ static const char new_edge[] =
+ "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id WHERE e.type='DEPENDS_ON' "
+ "AND s.name='docker.io/acme/web-new'";
+ if (k8s_inc_index(tmp, db_path) != 0 || k8s_inc_count(db_path, old_img) != 1) {
+ th_rmtree(tmp);
+ return NULL;
+ }
+ write_temp_file(tmp, "compose.yaml",
+ "services:\n web:\n image: acme/web-new\n build: ./src/web\n"
+ " api:\n image: acme/api\n build: ./src/api\n");
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ if (legacy) {
+ cbm_pipeline_incremental_test_force_legacy_partial_once();
+ }
+#else
+ (void)legacy;
+#endif
+ char *out = NULL;
+ if (k8s_inc_index(tmp, db_path) == 0) {
+ char buf[64];
+ snprintf(buf, sizeof(buf), "old=%d new=%d", k8s_inc_count(db_path, old_img),
+ k8s_inc_count(db_path, new_edge));
+ out = strdup(buf);
+ }
+ *route = cbm_pipeline_incremental_test_last_route();
+ th_rmtree(tmp);
+ return out;
+}
+
+TEST(incremental_compose_edit_replaces_build_edges) {
+ cbm_incremental_route_t route = CBM_INCREMENTAL_ROUTE_NONE;
+ char *got = incremental_compose_edit(false, &route);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
+ ASSERT_NOT_NULL(got);
+ ASSERT_STR_EQ(got, "old=0 new=1");
+ free(got);
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ got = incremental_compose_edit(true, &route);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);
+ ASSERT_NOT_NULL(got);
+ ASSERT_STR_EQ(got, "old=0 new=1");
+ free(got);
+#endif
+ PASS();
+}
+
TEST(incremental_deleted_dockerfile_drops_its_image) {
cbm_incremental_route_t route = CBM_INCREMENTAL_ROUTE_NONE;
int left = incremental_dockerfile_delete(false, &route);
@@ -19181,6 +19305,8 @@ SUITE(pipeline_semantic_manifest_repro) {
RUN_TEST(incremental_shell_deploy_edit_keeps_its_edges);
RUN_TEST(incremental_deleted_spawner_drops_its_process);
RUN_TEST(incremental_deleted_dockerfile_drops_its_image);
+ RUN_TEST(incremental_dockerfile_edit_keeps_build_edges);
+ RUN_TEST(incremental_compose_edit_replaces_build_edges);
RUN_TEST(pipeline_sql_lineage_and_relation_isolation);
RUN_TEST(pipeline_incremental_sql_table_rename_drops_stale_lineage);
RUN_TEST(pipeline_dbt_jinja_lineage);