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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
feat(compose): a compose service is a Resource with its image, build and dependencies
Martin Vogelccf28b676c30
diff --git a/src/pipeline/pass_ci.c b/src/pipeline/pass_ci.cindex fcd5d63f2..c7083e0ac 100644--- a/src/pipeline/pass_ci.c+++ b/src/pipeline/pass_ci.c@@ -51,7 +51,8 @@ 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 */+ cbm_build_site_fn site_fn; /* a build-site walk's callback */+ cbm_compose_service_fn service_fn; /* a compose walk's callback */void *ud;} ci_t;@@ -937,32 +938,80 @@ static void ci_skaffold_walk(const ci_t *c, TSNode n, const char *dir, int depth}}+/* The Dockerfile a compose build names (a context, or context /+ * dockerfile), as a repository path; NULL: none or unknown. */+static const char *ci_compose_dockerfile(const ci_t *c, TSNode build, const char *dir,+ const ci_env_t *env) {+ if (ts_node_is_null(build)) {+ return NULL;+ }+ 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);+ const char *cdir = ctx && df ? ci_repo_path(c->a, dir, ctx) : NULL;+ return cdir ? ci_repo_path(c->a, cdir, df) : NULL;+}++/* depends_on (a list, or a mapping's keys) and links ("svc" / "svc:alias"). */+static int ci_compose_deps(const ci_t *c, TSNode svc, const char ***out) {+ int nd = 0;+ int nl = 0;+ TSNode dep = ci_get(c, svc, "depends_on");+ TSNode *items = ci_items(c, dep, &nd);+ ci_pair_t *pairs = items ? NULL : ci_pairs(c, dep, &nd);+ TSNode *links = ci_items(c, ci_get(c, svc, "links"), &nl);+ const char **deps = cbm_arena_alloc(c->a, (size_t)(nd + nl + SKIP_ONE) * sizeof(*deps));+ int n = 0;+ for (int i = 0; deps && i < nd; i++) {+ const char *d = items ? ci_scalar(c, items[i], NULL) : pairs[i].key;+ if (d) {+ deps[n++] = d;+ }+ }+ for (int i = 0; deps && i < nl; i++) {+ const char *l = ci_scalar(c, links[i], NULL);+ const char *colon = l ? strchr(l, ':') : NULL;+ if (l) {+ deps[n++] = colon ? cbm_arena_strndup(c->a, l, (size_t)(colon - l)) : l;+ }+ }+ *out = deps;+ return n;+}+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) {+ TSNode svc = ci_content(svcs[i].value);+ if (!ci_type(svc, "block_mapping") && !ci_type(svc, "flow_mapping")) {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;+ int row = 0;+ const char *raw = ci_scalar(c, ci_get(c, svc, "image"), &row);+ const char *image = raw ? ci_interp(c->a, raw, env) : NULL;+ const char *df = ci_compose_dockerfile(c, ci_get(c, svc, "build"), dir, env);+ if (c->site_fn && image && df) {+ cbm_build_site_t site = {+ .via = "compose", .image = image, .dockerfile = df, .line = row + SKIP_ONE};+ c->site_fn(&site, c->ud);}- 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;+ if (c->service_fn) {+ cbm_compose_service_t s = {.name = svcs[i].key,+ .image = image,+ .dockerfile = df,+ .line = (int)ts_node_start_point(svc).row,+ .end_line = (int)ts_node_end_point(svc).row + SKIP_ONE};+ s.ndeps = ci_compose_deps(c, svc, &s.deps);+ c->service_fn(&s, c->ud);}- ci_build_emit(c, "compose", image, ci_repo_path(c->a, dir, ctx), df, row);}}@@ -981,8 +1030,26 @@ bool cbm_build_path(const char *rel) {return strncmp(b, "compose", cl) == 0 && (b[cl] == '.' || b[cl] == '-' || b[cl] == '_');}+static void ci_build_walk(CBMArena *a, const char *rel, const char *src, size_t len,+ const char *dotenv, ci_t proto);+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) {+ ci_build_walk(a, rel, src, len, dotenv, (ci_t){.site_fn = fn, .ud = ud});+}++void cbm_compose_services(CBMArena *a, const char *rel, const char *src, size_t len,+ const char *dotenv, cbm_compose_service_fn fn, void *ud) {+ const char *slash = strrchr(rel, '/');+ const char *base = slash ? slash + SKIP_ONE : rel;+ if (strncmp(base, "skaffold", strlen("skaffold")) == 0) {+ return;+ }+ ci_build_walk(a, rel, src, len, dotenv, (ci_t){.service_fn = fn, .ud = ud});+}++static void ci_build_walk(CBMArena *a, const char *rel, const char *src, size_t len,+ const char *dotenv, ci_t proto) {if (!cbm_build_path(rel)) {return;}@@ -1003,7 +1070,10 @@ void cbm_build_sites(CBMArena *a, const char *rel, const char *src, size_t len,env = ci_dotenv(a, dotenv);}if (tree && dir) {- ci_t c = {.a = a, .src = src, .rel = rel, .site_fn = fn, .ud = ud};+ ci_t c = proto;+ c.a = a;+ c.src = src;+ c.rel = rel;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);@@ -1018,12 +1088,12 @@ void cbm_build_sites(CBMArena *a, const char *rel, const char *src, size_t len,break;}}- if (skaffold) {+ if (skaffold && c.site_fn) {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 {+ } else if (!skaffold) {ci_compose(&c, root, dir, &env);}}diff --git a/src/pipeline/pass_docker.c b/src/pipeline/pass_docker.cindex 896afb9dc..26bdf15d7 100644--- a/src/pipeline/pass_docker.c+++ b/src/pipeline/pass_docker.c@@ -1129,6 +1129,75 @@ static void dk_build_site(const cbm_build_site_t *s, void *ud) {b->edges++;}+typedef struct {+ cbm_pipeline_ctx_t *ctx;+ CBMArena *a;+ const char *file;+ const char *base; /* the compose file's module QN */+ int64_t file_node;+ int pass; /* 0: make the service nodes, 1: their edges */+ int edges;+} dk_compose_t;++static const char *dk_compose_qn(const dk_compose_t *c, const char *name) {+ return cbm_arena_sprintf(c->a, "%s::compose/Service/-/%s", c->base, name);+}++static void dk_compose_edge(dk_compose_t *c, int64_t src, int64_t tgt, const char *type,+ const char *props) {+ if (src > 0 && tgt > 0 && src != tgt) {+ cbm_gbuf_insert_edge(c->ctx->gbuf, src, tgt, type, props);+ c->edges++;+ }+}++/* A compose service: a Resource {ecosystem:compose, kind:Service} the file+ * DEFINES; it DEPENDS_ON the services it depends on / links, USAGE+ * {ref:image} its image and DEPENDS_ON {via:build} the Dockerfile it builds. */+static void dk_compose_service(const cbm_compose_service_t *s, void *ud) {+ dk_compose_t *c = ud;+ const char *qn = dk_compose_qn(c, s->name);+ if (!qn) {+ return;+ }+ if (c->pass == 0) {+ char esc[CBM_SZ_512];+ cbm_json_escape(esc, (int)sizeof(esc), s->name);+ char props[CBM_SZ_1K];+ snprintf(props, sizeof(props),+ "{\"ecosystem\":\"compose\",\"kind\":\"Service\",\"service\":\"%s\"}", esc);+ int64_t id = cbm_gbuf_upsert_node(c->ctx->gbuf, "Resource",+ cbm_arena_sprintf(c->a, "Service/%s", s->name), qn,+ c->file, s->line + SKIP_ONE, s->end_line, props);+ dk_compose_edge(c, c->file_node, id, "DEFINES", "{}");+ return;+ }+ const cbm_gbuf_node_t *self = cbm_gbuf_find_by_qn(c->ctx->gbuf, qn);+ if (!self) {+ return;+ }+ for (int i = 0; i < s->ndeps; i++) {+ const char *dq = dk_compose_qn(c, s->deps[i]);+ const cbm_gbuf_node_t *dep = dq ? cbm_gbuf_find_by_qn(c->ctx->gbuf, dq) : NULL;+ dk_compose_edge(c, self->id, dep ? dep->id : 0, "DEPENDS_ON", "{\"via\":\"depends_on\"}");+ }+ if (s->image) {+ char esc[CBM_SZ_1K];+ cbm_json_escape(esc, (int)sizeof(esc), s->image);+ char props[CBM_SZ_2K];+ snprintf(props, sizeof(props), "{\"ref\":\"image\",\"image\":\"%s\"}", esc);+ dk_compose_edge(c, self->id, cbm_pipeline_image_node(c->ctx, s->image), "USAGE", props);+ }+ if (s->dockerfile) {+ const char *mqn = cbm_fqn_module(c->a, c->ctx->project_name, s->dockerfile);+ const cbm_gbuf_node_t *mod = mqn ? cbm_gbuf_find_by_qn(c->ctx->gbuf, mqn) : NULL;+ if (mod && mod->file_path && strcmp(mod->file_path, s->dockerfile) == 0) {+ dk_compose_edge(c, self->id, mod->id, "DEPENDS_ON",+ "{\"via\":\"build\",\"tool\":\"compose\"}");+ }+ }+}+/* 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) {@@ -1167,6 +1236,15 @@ static int dk_link_builds(cbm_pipeline_ctx_t *ctx, CBMArena *a) {char *dotenv = dk_read_dotenv(ctx, rel);b.file = rel;cbm_build_sites(a, rel, src, len, dotenv, dk_build_site, &b);+ dk_compose_t cs = {.ctx = ctx,+ .a = a,+ .file = rel,+ .base = cbm_fqn_compute(a, ctx->project_name, rel, NULL),+ .file_node = files[i]->id};+ for (cs.pass = 0; cs.base && cs.pass < PAIR_LEN; cs.pass++) {+ cbm_compose_services(a, rel, src, len, dotenv, dk_compose_service, &cs);+ }+ b.edges += cs.edges;if (dotenv) {cbm_free(CBM_MEM_CLASS_OTHER, dotenv);}diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.cindex 1d90c0dd7..f4134be00 100644--- a/src/pipeline/pipeline_incremental.c+++ b/src/pipeline/pipeline_incremental.c@@ -2351,6 +2351,29 @@ static const char *closure_add_variant_partners(cbm_store_t *store, const char *return decline;}+/* Whether the directory of `rel` (whose basename is at `base`) holds a+ * compose file: compose interpolates that directory's .env into it, so the+ * .env changes what its services build and run (pass_ci.c). */+static bool closure_dir_has_compose(const char *repo_path, const char *rel, const char *base) {+ char dir[CBM_SZ_4K];+ int n = snprintf(dir, sizeof(dir), "%s/%.*s", repo_path, (int)(base - rel), rel);+ if (n < 0 || (size_t)n >= sizeof(dir)) {+ return true; /* unknown: decline */+ }+ cbm_dir_t *d = cbm_opendir(dir);+ if (!d) {+ return false;+ }+ bool found = false;+ cbm_dirent_t *e;+ while (!found && (e = cbm_readdir(d)) != NULL) {+ found = !e->is_dir && cbm_build_path(e->name) &&+ strncmp(e->name, "skaffold", strlen("skaffold")) != 0;+ }+ cbm_closedir(d);+ return found;+}+/* Kubernetes inputs reach past their own file (pass_k8s.c): a kustomization* decides which manifests deploy together and in which namespace, a* Chart.yaml links other charts, and a manifest's Services select workloads@@ -2370,14 +2393,22 @@ static const char *closure_k8s_decline(cbm_store_t *store, const char *project,if (strcmp(base, "Chart.yaml") == 0 || strcmp(base, "Chart.yml") == 0) {return "helm_chart_changed";}+ if (strcmp(base, ".env") == 0 && closure_dir_has_compose(repo_path, rel, base)) {+ return "compose_env_changed";+ }cbm_node_t *nodes = NULL;int count = 0;if (cbm_store_find_nodes_by_file(store, project, rel, &nodes, &count) != CBM_STORE_OK) {return "k8s_lookup_failed";}+ /* A compose service (pass_docker.c) is a Resource too, but it links+ * only inside its file and to the Images / Dockerfiles every run+ * recomputes: no bundle reaches past it. */bool held = false;for (int j = 0; j < count && !held; j++) {- held = nodes[j].label && strcmp(nodes[j].label, "Resource") == 0;+ held = nodes[j].label && strcmp(nodes[j].label, "Resource") == 0 &&+ !(nodes[j].properties_json &&+ strstr(nodes[j].properties_json, "\"ecosystem\":\"compose\""));}cbm_store_free_nodes(nodes, count);if (held) {diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.hindex a6bc7a088..34a3a42dc 100644--- a/src/pipeline/pipeline_internal.h+++ b/src/pipeline/pipeline_internal.h@@ -1083,6 +1083,26 @@ typedef struct {} cbm_build_site_t;typedef void (*cbm_build_site_fn)(const cbm_build_site_t *site, void *ud);+/* A compose service (pass_ci.c): its image (a reference, variables+ * expanded; NULL: none or unknown), the Dockerfile its build names+ * (repository path; NULL: no build, or unknown), and the service names its+ * depends_on and links list. */+typedef struct {+ const char *name;+ const char *image;+ const char *dockerfile;+ const char **deps;+ int ndeps;+ int line;+ int end_line;+} cbm_compose_service_t;+typedef void (*cbm_compose_service_fn)(const cbm_compose_service_t *svc, void *ud);++/* Hand fn each service of compose file `rel` (YAML text src; `dotenv` the+ * project's .env text or NULL). Nothing for a file that is no compose file. */+void cbm_compose_services(CBMArena *a, const char *rel, const char *src, size_t len,+ const char *dotenv, cbm_compose_service_fn fn, void *ud);+/* Whether `rel` names a build configuration: skaffold*.yaml|yml,* [docker-]compose[.x].yaml|yml. */bool cbm_build_path(const char *rel);diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.cindex ac37ebaa6..b00d31481 100644--- a/tests/test_grammar_probe_f.c+++ b/tests/test_grammar_probe_f.c@@ -2114,6 +2114,68 @@ TEST(probe_build_configs_build_images_from_dockerfiles) {PASS();}+/* Every edge from a compose service as "<service> -> <target> (TYPE+ * via|ref)", sorted: an Image by repository, a Module by path. */+static char *gpf_compose(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 || ' -> ' || "+ "CASE t.label WHEN 'Image' THEN 'Image:' || t.name WHEN 'Module' THEN 'Module:' || "+ "t.file_path ELSE t.name END || ' (' || e.type || ' ' || "+ "coalesce(json_extract(e.properties,'$.via'), json_extract(e.properties,'$.ref')) || "+ "')' 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 s.label='Resource' AND "+ "json_extract(s.properties,'$.ecosystem')='compose' 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;+}++/* A compose service is a Resource {ecosystem:compose}: it DEPENDS_ON the+ * services its depends_on (a list or a mapping) and links name, USAGE+ * {ref:image} its image and DEPENDS_ON {via:build} the Dockerfile its build+ * names (a context, or context + dockerfile). */+TEST(probe_compose_services_are_resources) {+ static const GpfFile files[] = {+ {"web/Dockerfile", "FROM nginx:1.27\n"},+ {"worker/Dockerfile", "FROM python:3.12\n"},+ {"compose.yaml",+ "services:\n web:\n image: acme/web:1\n build: ./web\n"+ " depends_on:\n - db\n - cache\n links:\n - cache:c\n"+ " db:\n image: postgres:16\n"+ " cache:\n image: redis:7\n depends_on:\n db:\n"+ " condition: service_healthy\n"+ " worker:\n build:\n context: .\n dockerfile: worker/Dockerfile\n"}};+ char *got = gpf_compose(files, 3);+ static const char want[] =+ "Service/cache -> Image:docker.io/library/redis (USAGE image)\n"+ "Service/cache -> Service/db (DEPENDS_ON depends_on)\n"+ "Service/db -> Image:docker.io/library/postgres (USAGE image)\n"+ "Service/web -> Image:docker.io/acme/web (USAGE image)\n"+ "Service/web -> Module:web/Dockerfile (DEPENDS_ON build)\n"+ "Service/web -> Service/cache (DEPENDS_ON depends_on)\n"+ "Service/web -> Service/db (DEPENDS_ON depends_on)\n"+ "Service/worker -> Module:worker/Dockerfile (DEPENDS_ON build)";+ if (!got || strcmp(got, want) != 0) {+ fprintf(stderr, "compose got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);+ free(got);+ FAIL("compose service 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"@@ -4382,6 +4444,7 @@ SUITE(grammar_probe_f) {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_compose_services_are_resources);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.cindex 9b2b4eaf5..1bb965a3d 100644--- a/tests/test_pipeline.c+++ b/tests/test_pipeline.c@@ -3665,6 +3665,39 @@ TEST(incremental_compose_edit_replaces_build_edges) {PASS();}+/* A compose project's .env feeds its compose file (variables in image,+ * build): changing it is no closure repair -- the unchanged compose file's+ * services would keep the old image. It rebuilds; no stale image remains. */+TEST(incremental_compose_dotenv_change_rebuilds) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_composeenv_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ write_temp_file(tmp, "src/web/Dockerfile", "FROM nginx:1.27\n");+ write_temp_file(tmp, ".env", "REG=ghcr.io/old\n");+ write_temp_file(tmp, "compose.yaml",+ "services:\n web:\n image: ${REG}/web\n build: ./src/web\n");+ char db_path[512];+ snprintf(db_path, sizeof(db_path), "%s/composeenv.db", tmp);+ static const char old_img[] =+ "SELECT COUNT(*) FROM nodes WHERE label='Image' AND name='ghcr.io/old/web'";+ static const char new_use[] =+ "SELECT COUNT(*) FROM edges e JOIN nodes t ON t.id=e.target_id WHERE e.type='USAGE' AND "+ "t.name='ghcr.io/new/web'";+ int full_old = k8s_inc_index(tmp, db_path) == 0 ? k8s_inc_count(db_path, old_img) : -1;+ write_temp_file(tmp, ".env", "REG=ghcr.io/new\n");+ int rc = k8s_inc_index(tmp, db_path);+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();+ int after_old = k8s_inc_count(db_path, old_img);+ int after_new = k8s_inc_count(db_path, new_use);+ th_rmtree(tmp);+ ASSERT_EQ(full_old, 1);+ ASSERT_EQ(rc, 0);+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);+ ASSERT_EQ(after_old, 0);+ ASSERT_EQ(after_new, 1);+ PASS();+}+TEST(incremental_deleted_dockerfile_drops_its_image) {cbm_incremental_route_t route = CBM_INCREMENTAL_ROUTE_NONE;int left = incremental_dockerfile_delete(false, &route);@@ -19307,6 +19340,7 @@ SUITE(pipeline_semantic_manifest_repro) {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(incremental_compose_dotenv_change_rebuilds);RUN_TEST(pipeline_sql_lineage_and_relation_isolation);RUN_TEST(pipeline_incremental_sql_table_rename_drops_stale_lineage);RUN_TEST(pipeline_dbt_jinja_lineage);