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(infra): Ansible tasks are Functions, a notify CALLS its handlers

Martin Vogel701d95e061ea
diff --git a/README.md b/README.md
index f99cd963a..5b7b0e907 100644
--- a/README.md
+++ b/README.md
@@ -260,7 +260,7 @@ The install script placed beside the binary is **reported, not deleted** — uni
- `USAGE` with `via: terraform` — a Terraform / OpenTofu reference (`var.X`, `local.X`, `module.X`, `data.T.N`, `T.N`) from the top-level block holding it to the block of its module — the `.tf` / `.tofu` files of its directory — that declares it (a `locals` entry: its `locals` block); an entry of the block's `depends_on` list is a `DEPENDS_ON` instead. The edge keeps `ref` (`var`, `local`, `module`, `data`, `resource`) and `line`; a name only mentioned in string text is no reference. A module call with a local source (`module "net" { source = "./modules/net" }`) `IMPORTS` (`local_name`: the module's name) the `Folder` of that directory (the root directory: the `Project`), and `module.net.id` (also `module.net[0].id`, `module.net[*].id`) `USAGE`s (`ref: output`) the `output "id"` there, beside `module.net`
- `OVERRIDE` with `via: tfvars` — a `.tfvars` assignment to a variable the module of its own directory declares, from the file's `Variable` to that `variable` block (the edge keeps `line`); a `.tfvars` file whose own directory declares none of its names sets the nearest ancestor module's that does, the edge marked `inferred` (such a file is applied by a `-var-file` the repository does not state)
- Terragrunt (`via: terragrunt`) — a unit (`terragrunt.hcl`), evaluated in its own context: an `include` `IMPORTS` (`local_name`) the file its `path` names; a `read_terragrunt_config` `IMPORTS` (`read`: the file holding the call) the file it names, from the block holding the call or, for a call in an included file, from the unit's `include` block that brings it in; a `dependency` `DEPENDS_ON` the other unit; a local `terraform` `source` `IMPORTS` its `Folder`. Paths may use `find_in_parent_folders`, `dirname` and `get_terragrunt_dir`; anything else is unknown and links nothing
-- Ansible (`via: ansible`) — a playbook or task file (a YAML file whose top level is a sequence) `IMPORTS` (`local_name`: the directive) what its directives name: `import_playbook` a playbook, `include_tasks` / `import_tasks` / `include` a task file (from its directory, else its role's `tasks/`), `roles:` items and `include_role` / `import_role` a role's `Folder` (`roles/<name>` at or above its directory). A templated target links nothing
+- Ansible (`via: ansible`) — a playbook or task file (a YAML file whose top level is a sequence) `IMPORTS` (`local_name`: the directive) what its directives name: `import_playbook` a playbook, `include_tasks` / `import_tasks` / `include` a task file (from its directory, else its role's `tasks/`), `roles:` items and `include_role` / `import_role` a role's `Folder` (`roles/<name>` at or above its directory). A templated target links nothing. Each task and handler is a `Function` (named by its `name:`, else its module; `source: ansible`, `kind`, `module`) that its file `DEFINES` — in a playbook's plays, a role's `tasks/` and `handlers/` files, and the files those include; a block's tasks, not the block. A `notify` `CALLS` (`via: notify`) each handler of that name or `listen` topic in the task's own role (else its file)
- `EMITS`, `LISTENS_ON` (channels)
- `DATA_FLOWS` with arg-to-param mapping + field access chains
- `SIMILAR_TO` (MinHash + LSH near-clone detection, Jaccard scored)
diff --git a/src/pipeline/pass_ansible.c b/src/pipeline/pass_ansible.c
index a1e854591..349619e38 100644
--- a/src/pipeline/pass_ansible.c
+++ b/src/pipeline/pass_ansible.c
@@ -20,6 +20,20 @@
* (the ansible.builtin. prefix allowed). The edge goes from the file to the
* target file's Module, or to the role's Folder; a target the graph does not
* hold, or one holding a Jinja expression ({{ }}), makes none.
+ *
+ * PHASE 7 P7.2: tasks. A top-level sequence is not Ansible by itself (JSON
+ * patches, image maps and service lists are sequences too): a file is
+ * Ansible when it is a playbook (an item with hosts: / import_playbook:), a
+ * role's tasks/ or handlers/ file, or -- to a fixpoint -- a task file an
+ * Ansible file includes. Each of its tasks (a playbook's pre_tasks / tasks /
+ * post_tasks, its handlers; a block's block / rescue / always, never the
+ * block) is a Function named by its `name:`, else its module, QN
+ * <file>::task/<n> or ::handler/<n> (document order), {source: ansible,
+ * kind, module}, which the file DEFINES. A notify CALLS {via: notify, line}
+ * every handler of that name or listen topic in the task's own role (the
+ * directory before its tasks/), else in its file (a play's handlers). The
+ * incremental repair declines on any change to Ansible content
+ * (closure_ansible_decline): a notify joins two files.
*/
#include "foundation/constants.h"
#include "foundation/compat.h"
@@ -28,6 +42,7 @@
#include "foundation/limits.h"
#include "foundation/log.h"
#include "foundation/mem_core.h"
+#include "foundation/str_util.h" /* cbm_json_escape */
#include "graph_buffer/graph_buffer.h"
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
@@ -40,6 +55,17 @@
#include <stdlib.h>
#include <string.h>
+enum { AN_GROW_MIN = 16 };
+
+/* A notify waiting for every handler: the task, its role and file, the name. */
+typedef struct {
+ int64_t task;
+ char *scope_role; /* "" outside a role */
+ char *scope_file;
+ char *name;
+ int line;
+} an_notify_t;
+
typedef struct {
cbm_pipeline_ctx_t *ctx;
CBMHashTable *modules; /* file path -> its Module node */
@@ -50,13 +76,44 @@ typedef struct {
char role[CBM_SZ_1K]; /* the role holding it (roles/<name>), or "" */
int64_t from; /* its File node */
int edges;
+ char **incs; /* pass 1: the task files includes name, in file order */
+ int inc_n;
+ int inc_cap;
+ char base[CBM_SZ_1K]; /* pass 2: its File QN without `.__file__` */
+ char role_dir[CBM_SZ_1K]; /* the role of its tasks / handlers, or "" */
+ int ordinal[PAIR_LEN]; /* tasks, handlers so far in the file */
+ CBMHashTable *handlers; /* "<role or F:file>\x1f<name>" -> an_ids_t */
+ an_notify_t *notes;
+ int note_n;
+ int note_cap;
+ int tasks;
+ int calls;
} an_t;
static bool an_type(TSNode n, const char *t) {
return !ts_node_is_null(n) && strcmp(ts_node_type(n), t) == 0;
}
-/* A scalar's text (quotes stripped) into out; false for anything else. */
+/* A quoted scalar's escapes decoded in place: `''` in single quotes;
+ * `\"`, `\\`, `\n`, `\t` (any other `\x`: x) in double quotes. */
+static void an_unescape(char *s, bool dq) {
+ char *w = s;
+ for (const char *r = s; *r; r++) {
+ if (dq && r[0] == '\\' && r[SKIP_ONE]) {
+ r++;
+ *w++ = *r == 'n' ? '\n' : *r == 't' ? '\t' : *r;
+ } else if (!dq && r[0] == '\'' && r[SKIP_ONE] == '\'') {
+ r++;
+ *w++ = '\'';
+ } else {
+ *w++ = *r;
+ }
+ }
+ *w = '\0';
+}
+
+/* A scalar's text (quotes stripped, escapes decoded) into out; false for
+ * anything else. */
static bool an_scalar(const an_t *a, TSNode n, char *out, size_t sz) {
while ((an_type(n, "flow_node") || an_type(n, "block_node") || an_type(n, "plain_scalar")) &&
ts_node_named_child_count(n) == SKIP_ONE) {
@@ -67,7 +124,9 @@ static bool an_scalar(const an_t *a, TSNode n, char *out, size_t sz) {
}
uint32_t b = ts_node_start_byte(n);
uint32_t e = ts_node_end_byte(n);
- if (an_type(n, "double_quote_scalar") || an_type(n, "single_quote_scalar")) {
+ bool dq = an_type(n, "double_quote_scalar");
+ bool sq = an_type(n, "single_quote_scalar");
+ if (dq || sq) {
b++;
e = e > b ? e - SKIP_ONE : b;
} else if (!an_type(n, "string_scalar") && !an_type(n, "plain_scalar")) {
@@ -78,6 +137,9 @@ static bool an_scalar(const an_t *a, TSNode n, char *out, size_t sz) {
}
memcpy(out, a->src + b, e - b);
out[e - b] = '\0';
+ if (dq || sq) {
+ an_unescape(out, dq);
+ }
return out[0] != '\0';
}
@@ -123,6 +185,24 @@ static void an_edge(an_t *a, const cbm_gbuf_node_t *tgt, const char *kind, TSNod
a->edges++;
}
+/* Pass 1 keeps the task files an include names: they are Ansible content
+ * even outside a role (an_mark_ansible). */
+static void an_include(an_t *a, const cbm_gbuf_node_t *target) {
+ if (!target || !target->file_path) {
+ return;
+ }
+ if (a->inc_n == a->inc_cap) {
+ int cap = a->inc_cap ? a->inc_cap * PAIR_LEN : AN_GROW_MIN;
+ char **grown = cbm_realloc(CBM_MEM_CLASS_OTHER, a->incs, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ return;
+ }
+ a->incs = grown;
+ a->inc_cap = cap;
+ }
+ a->incs[a->inc_n++] = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, target->file_path);
+}
+
/* A file `v` from `base` (repository-relative), its Module; NULL when none. */
static const cbm_gbuf_node_t *an_file(const an_t *a, const char *base, const char *v) {
char path[CBM_SZ_1K];
@@ -177,7 +257,9 @@ static void an_pair(an_t *a, TSNode pair) {
char tasks[CBM_SZ_1K];
snprintf(tasks, sizeof(tasks), "%s/tasks", a->role);
const cbm_gbuf_node_t *t = an_file(a, a->dir, v);
- an_edge(a, t ? t : a->role[0] ? an_file(a, tasks, v) : NULL, key, pair);
+ const cbm_gbuf_node_t *target = t ? t : a->role[0] ? an_file(a, tasks, v) : NULL;
+ an_edge(a, target, key, pair);
+ an_include(a, target);
}
} else if (strcmp(key, "include_role") == 0 || strcmp(key, "import_role") == 0) {
if (an_scalar(a, an_get(a, value, "name"), v, sizeof(v)) && !strstr(v, "{{")) {
@@ -291,6 +373,416 @@ static char *an_read(const char *path, size_t *len) {
return buf;
}
+/* ── Tasks (P7.2) ─────────────────────────────────────────────────── */
+
+/* Task keywords: every other key of a task is its module. */
+static const char *const AN_KEYWORDS[] = {"name",
+ "when",
+ "tags",
+ "become",
+ "become_user",
+ "become_method",
+ "register",
+ "loop",
+ "with_items",
+ "with_dict",
+ "with_list",
+ "with_fileglob",
+ "with_nested",
+ "with_together",
+ "with_sequence",
+ "with_subelements",
+ "with_first_found",
+ "loop_control",
+ "notify",
+ "listen",
+ "ignore_errors",
+ "changed_when",
+ "failed_when",
+ "delegate_to",
+ "run_once",
+ "vars",
+ "environment",
+ "until",
+ "retries",
+ "delay",
+ "no_log",
+ "check_mode",
+ "diff",
+ "any_errors_fatal",
+ "throttle",
+ "timeout",
+ "args",
+ "action",
+ "local_action",
+ "async",
+ "poll",
+ "debugger",
+ "collections",
+ "module_defaults",
+ "remote_user",
+ "connection",
+ "port",
+ "ignore_unreachable",
+ "block",
+ "rescue",
+ "always",
+ "delegate_facts"};
+
+static bool an_keyword(const char *k) {
+ for (size_t i = 0; i < sizeof(AN_KEYWORDS) / sizeof(AN_KEYWORDS[0]); i++) {
+ if (strcmp(k, AN_KEYWORDS[i]) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/* The first named child of `n` of type `t`, or a null node. */
+static TSNode an_child(TSNode n, const char *t) {
+ for (uint32_t i = 0; !ts_node_is_null(n) && i < ts_node_named_child_count(n); i++) {
+ if (an_type(ts_node_named_child(n, i), t)) {
+ return ts_node_named_child(n, i);
+ }
+ }
+ return (TSNode){0};
+}
+
+/* A value as a block sequence (under its block_node), or a null node. */
+static TSNode an_seq(TSNode v) {
+ while (an_type(v, "block_node") && ts_node_named_child_count(v) == SKIP_ONE) {
+ v = ts_node_named_child(v, 0);
+ }
+ return an_type(v, "block_sequence") ? v : (TSNode){0};
+}
+
+/* The top-level block sequence of a parsed file, or a null node. */
+static TSNode an_top(TSNode root) {
+ return an_seq(an_child(an_child(root, "document"), "block_node"));
+}
+
+/* Item `i` of block sequence `seq` as a block mapping, or a null node. */
+static TSNode an_item(TSNode seq, uint32_t i) {
+ TSNode node = ts_node_named_child(ts_node_named_child(seq, i), 0);
+ return an_type(node, "block_node") ? an_child(node, "block_mapping") : (TSNode){0};
+}
+
+/* Does block mapping `m` hold key `key` (the ansible.builtin. prefix allowed)? */
+static bool an_has(const an_t *a, TSNode m, const char *key) {
+ for (uint32_t i = 0; an_type(m, "block_mapping") && i < ts_node_named_child_count(m); i++) {
+ char k[CBM_SZ_64];
+ if (an_key(a, ts_node_named_child(m, i), k, sizeof(k)) && strcmp(k, key) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/* A playbook: a top-level item is a play (hosts:) or imports one. */
+static bool an_is_playbook(const an_t *a, TSNode top) {
+ for (uint32_t i = 0; !ts_node_is_null(top) && i < ts_node_named_child_count(top); i++) {
+ TSNode m = an_item(top, i);
+ if (an_has(a, m, "hosts") || an_has(a, m, "import_playbook")) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/* The role a task or handler file belongs to: the directory before the first
+ * tasks/ or handlers/ under the last roles/ component that has one
+ * (roles/kubernetes-apps/csi_driver/cinder for .../cinder/tasks/main.yml);
+ * "" outside a role's tasks / handlers. */
+static void an_role_dir(const char *rel, char *out, size_t sz) {
+ out[0] = '\0';
+ for (const char *p = rel; (p = strstr(p, "roles/")) != NULL; p++) {
+ if (p != rel && p[-1] != '/') {
+ continue;
+ }
+ /* the role's name: one character at least, then its tasks/ or handlers/ */
+ const char *from = p + sizeof("roles/");
+ if (from > p + strlen(p)) {
+ break;
+ }
+ const char *t = strstr(from, "/tasks/");
+ const char *h = strstr(from, "/handlers/");
+ const char *end = t && (!h || t < h) ? t : h;
+ if (end) {
+ snprintf(out, sz, "%.*s", (int)(end - rel), rel);
+ }
+ }
+}
+
+/* Ids by key ("<scope>\x1f<name>"). */
+typedef struct {
+ int64_t *items;
+ int count;
+ int cap;
+} an_ids_t;
+
+static void an_ids_add(CBMHashTable *ix, const char *key, int64_t id) {
+ an_ids_t *v = cbm_ht_get(ix, key);
+ if (!v) {
+ v = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*v));
+ char *owned = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, key);
+ if (!v || !owned) {
+ cbm_free(CBM_MEM_CLASS_OTHER, v);
+ cbm_free(CBM_MEM_CLASS_OTHER, owned);
+ return;
+ }
+ cbm_ht_set(ix, owned, v);
+ }
+ if (v->count > 0 && v->items[v->count - SKIP_ONE] == id) {
+ return; /* a handler named and listening alike */
+ }
+ if (v->count == v->cap) {
+ int cap = v->cap ? v->cap * PAIR_LEN : AN_GROW_MIN;
+ int64_t *grown = cbm_realloc(CBM_MEM_CLASS_OTHER, v->items, (size_t)cap * sizeof(int64_t));
+ if (!grown) {
+ return;
+ }
+ v->items = grown;
+ v->cap = cap;
+ }
+ v->items[v->count++] = id;
+}
+
+/* A task list being walked: its sequence, the next item, handlers or not. */
+typedef struct {
+ TSNode seq;
+ uint32_t next;
+ bool handler;
+} an_frame_t;
+
+/* How many names a notify / listen value gives (a scalar: one; a block or
+ * flow list: its items), its list node into `*list` (null for a scalar). */
+static uint32_t an_name_count(const an_t *a, TSNode v, TSNode *list) {
+ char probe[CBM_SZ_256];
+ *list = (TSNode){0};
+ if (an_scalar(a, v, probe, sizeof(probe))) {
+ return SKIP_ONE;
+ }
+ while ((an_type(v, "block_node") || an_type(v, "flow_node")) &&
+ ts_node_named_child_count(v) == SKIP_ONE) {
+ v = ts_node_named_child(v, 0);
+ }
+ if (!an_type(v, "block_sequence") && !an_type(v, "flow_sequence")) {
+ return 0;
+ }
+ *list = v;
+ return ts_node_named_child_count(v);
+}
+
+/* Name `i` of a notify / listen value (see an_name_count). */
+static bool an_name_at(const an_t *a, TSNode v, TSNode list, uint32_t i, char *out, size_t sz) {
+ if (ts_node_is_null(list)) {
+ return i == 0 && an_scalar(a, v, out, sz);
+ }
+ TSNode item = ts_node_named_child(list, i);
+ if (an_type(list, "block_sequence")) {
+ item = ts_node_named_child(item, 0);
+ }
+ return an_scalar(a, item, out, sz);
+}
+
+static void an_index_handler(an_t *a, const char *name, int64_t id) {
+ char key[CBM_SZ_2K];
+ if (a->role_dir[0]) {
+ snprintf(key, sizeof(key), "%s\x1f%s", a->role_dir, name);
+ an_ids_add(a->handlers, key, id);
+ }
+ snprintf(key, sizeof(key), "F:%s\x1f%s", a->file, name);
+ an_ids_add(a->handlers, key, id);
+}
+
+static void an_note(an_t *a, int64_t task, const char *name, int line) {
+ if (a->note_n == a->note_cap) {
+ int cap = a->note_cap ? a->note_cap * PAIR_LEN : AN_GROW_MIN;
+ an_notify_t *grown =
+ cbm_realloc(CBM_MEM_CLASS_OTHER, a->notes, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ return;
+ }
+ a->notes = grown;
+ a->note_cap = cap;
+ }
+ a->notes[a->note_n++] =
+ (an_notify_t){.task = task,
+ .scope_role = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, a->role_dir),
+ .scope_file = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, a->file),
+ .name = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, name),
+ .line = line};
+}
+
+/* Task (or handler) `m`: a Function named by its `name:` (else its module),
+ * QN <file>::task/<n> (handler/<n>), {source: ansible, kind, module}; the
+ * file DEFINES it. A handler is indexed by its name and listen topics; a
+ * notify waits for every handler (an_link_notifies). */
+static void an_task(an_t *a, TSNode m, bool handler) {
+ char module[CBM_SZ_128] = "";
+ for (uint32_t i = 0; !module[0] && i < ts_node_named_child_count(m); i++) {
+ char k[CBM_SZ_128];
+ if (an_key(a, ts_node_named_child(m, i), k, sizeof(k)) && !an_keyword(k)) {
+ snprintf(module, sizeof(module), "%s", k);
+ }
+ }
+ char action[CBM_SZ_256];
+ if (!module[0] && (an_scalar(a, an_get(a, m, "action"), action, sizeof(action)) ||
+ an_scalar(a, an_get(a, m, "local_action"), action, sizeof(action)))) {
+ snprintf(module, sizeof(module), "%.*s", (int)strcspn(action, " \t"), action);
+ }
+ char name[CBM_SZ_512];
+ bool named = an_scalar(a, an_get(a, m, "name"), name, sizeof(name));
+ if (!named) {
+ snprintf(name, sizeof(name), "%s", module[0] ? module : "task");
+ }
+ const char *kind = handler ? "handler" : "task";
+ int ord = ++a->ordinal[handler ? SKIP_ONE : 0];
+ char qn[CBM_SZ_2K + CBM_SZ_64];
+ char em[CBM_SZ_512];
+ char props[CBM_SZ_1K];
+ snprintf(qn, sizeof(qn), "%s::%s/%d", a->base, kind, ord);
+ cbm_json_escape(em, (int)sizeof(em), module);
+ snprintf(props, sizeof(props), "{\"source\":\"ansible\",\"kind\":\"%s\",\"module\":\"%s\"}",
+ kind, em);
+ int64_t id = cbm_gbuf_upsert_node(a->ctx->gbuf, "Function", name, qn, a->file,
+ (int)ts_node_start_point(m).row + SKIP_ONE,
+ (int)ts_node_end_point(m).row + SKIP_ONE, props);
+ if (id <= 0) {
+ return;
+ }
+ cbm_gbuf_insert_edge(a->ctx->gbuf, a->from, id, "DEFINES", "{}");
+ a->tasks++;
+ TSNode list;
+ char n[CBM_SZ_256];
+ if (handler) {
+ if (named) {
+ an_index_handler(a, name, id);
+ }
+ TSNode listen = an_get(a, m, "listen");
+ uint32_t ln = an_name_count(a, listen, &list);
+ for (uint32_t i = 0; i < ln; i++) {
+ if (an_name_at(a, listen, list, i, n, sizeof(n))) {
+ an_index_handler(a, n, id);
+ }
+ }
+ }
+ for (uint32_t i = 0; i < ts_node_named_child_count(m); i++) {
+ TSNode pair = ts_node_named_child(m, i);
+ char k[CBM_SZ_64];
+ if (!an_key(a, pair, k, sizeof(k)) || strcmp(k, "notify") != 0) {
+ continue;
+ }
+ TSNode v = ts_node_child_by_field_name(pair, "value", sizeof("value") - SKIP_ONE);
+ uint32_t nn = an_name_count(a, v, &list);
+ for (uint32_t j = 0; j < nn; j++) {
+ if (an_name_at(a, v, list, j, n, sizeof(n)) && !strstr(n, "{{")) {
+ an_note(a, id, n, (int)ts_node_start_point(pair).row + SKIP_ONE);
+ }
+ }
+ }
+}
+
+/* The tasks of a task list, depth first: a `block:` item walks its block,
+ * rescue and always lists before the next item (Ansible's order). */
+static void an_task_list(an_t *a, TSNode seq, bool handler) {
+ static const char *const PARTS[] = {"always", "rescue", "block"}; /* pushed last-first */
+ an_frame_t *stack = NULL;
+ int depth = 0;
+ int cap = 0;
+ TSNode push[sizeof(PARTS) / sizeof(PARTS[0]) + SKIP_ONE];
+ int pn = 0;
+ push[pn++] = seq;
+ for (;;) {
+ for (int p = 0; p < pn; p++) {
+ if (ts_node_is_null(push[p])) {
+ continue;
+ }
+ if (depth == cap) {
+ int ncap = cap ? cap * PAIR_LEN : AN_GROW_MIN;
+ an_frame_t *grown =
+ cbm_realloc(CBM_MEM_CLASS_OTHER, stack, (size_t)ncap * sizeof(*grown));
+ if (!grown) {
+ cbm_free(CBM_MEM_CLASS_OTHER, stack);
+ return;
+ }
+ stack = grown;
+ cap = ncap;
+ }
+ stack[depth++] = (an_frame_t){.seq = push[p], .next = 0, .handler = handler};
+ }
+ pn = 0;
+ if (depth == 0) {
+ break;
+ }
+ an_frame_t *f = &stack[depth - SKIP_ONE];
+ if (f->next >= ts_node_named_child_count(f->seq)) {
+ depth--;
+ continue;
+ }
+ TSNode m = an_item(f->seq, f->next++);
+ TSNode block = an_seq(an_get(a, m, "block"));
+ if (ts_node_is_null(m)) {
+ continue;
+ }
+ if (ts_node_is_null(block)) {
+ an_task(a, m, f->handler);
+ continue;
+ }
+ for (size_t p = 0; p < sizeof(PARTS) / sizeof(PARTS[0]); p++) {
+ push[pn++] = an_seq(an_get(a, m, PARTS[p]));
+ }
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, stack);
+}
+
+/* The tasks of an Ansible file: a playbook's plays (pre_tasks, tasks,
+ * post_tasks, handlers), else its own items -- handlers under a handlers/
+ * directory. */
+static void an_file_tasks(an_t *a, TSNode top, bool playbook) {
+ static const char *const SECTIONS[] = {"pre_tasks", "tasks", "post_tasks", "handlers"};
+ if (!playbook) {
+ bool handlers = strncmp(a->file, "handlers/", sizeof("handlers/") - SKIP_ONE) == 0 ||
+ strstr(a->file, "/handlers/") != NULL;
+ an_task_list(a, top, handlers);
+ return;
+ }
+ for (uint32_t i = 0; i < ts_node_named_child_count(top); i++) {
+ TSNode play = an_item(top, i);
+ for (size_t s = 0; !ts_node_is_null(play) && s < sizeof(SECTIONS) / sizeof(SECTIONS[0]);
+ s++) {
+ an_task_list(a, an_seq(an_get(a, play, SECTIONS[s])),
+ strcmp(SECTIONS[s], "handlers") == 0);
+ }
+ }
+}
+
+/* notify -> CALLS {via: notify, line} each handler of that name or listen
+ * topic in the task's role, else in its file (a play's handlers). */
+static void an_link_notifies(an_t *a) {
+ for (int i = 0; i < a->note_n; i++) {
+ const an_notify_t *n = &a->notes[i];
+ char key[CBM_SZ_2K];
+ const an_ids_t *ids = NULL;
+ if (n->scope_role && n->scope_role[0]) {
+ snprintf(key, sizeof(key), "%s\x1f%s", n->scope_role, n->name);
+ ids = cbm_ht_get(a->handlers, key);
+ }
+ if (!ids) {
+ snprintf(key, sizeof(key), "F:%s\x1f%s", n->scope_file, n->name);
+ ids = cbm_ht_get(a->handlers, key);
+ }
+ char props[CBM_SZ_64];
+ snprintf(props, sizeof(props), "{\"via\":\"notify\",\"line\":%d}", n->line);
+ for (int j = 0; ids && j < ids->count; j++) {
+ if (ids->items[j] != n->task) {
+ cbm_gbuf_insert_edge(a->ctx->gbuf, n->task, ids->items[j], "CALLS", props);
+ a->calls++;
+ }
+ }
+ }
+}
+
/* path -> node index of one label's nodes. */
static CBMHashTable *an_index(cbm_pipeline_ctx_t *ctx, const char *label) {
CBMHashTable *ix = cbm_ht_create(0);
@@ -311,62 +803,209 @@ static bool an_is_yaml(const char *rel) {
return dot && (strcmp(dot, ".yml") == 0 || strcmp(dot, ".yaml") == 0);
}
+bool cbm_ansible_is_sequence(const char *src) {
+ return an_is_sequence(src);
+}
+
+/* A YAML file whose top level is a sequence: Ansible when it is a playbook,
+ * a role's tasks / handlers file, or included by an Ansible file. */
+typedef struct {
+ int file; /* index into files[] */
+ bool playbook;
+ bool ansible;
+ int inc_lo; /* the task files it includes: incs[inc_lo, inc_hi) */
+ int inc_hi;
+} an_cand_t;
+
+typedef struct {
+ an_cand_t *items;
+ int count;
+ int cap;
+} an_cands_t;
+
+static an_cand_t *an_cand_push(an_cands_t *c, int file, int inc_lo) {
+ if (c->count == c->cap) {
+ int cap = c->cap ? c->cap * PAIR_LEN : AN_GROW_MIN;
+ an_cand_t *grown = cbm_realloc(CBM_MEM_CLASS_OTHER, c->items, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ return NULL;
+ }
+ c->items = grown;
+ c->cap = cap;
+ }
+ c->items[c->count] = (an_cand_t){.file = file, .inc_lo = inc_lo, .inc_hi = inc_lo};
+ return &c->items[c->count++];
+}
+
+/* The per-file state both passes start from. */
+static void an_begin_file(an_t *a, const char *rel, const cbm_gbuf_node_t *fnode, const char *src) {
+ static const char FILE_SUFFIX[] = ".__file__";
+ const char *slash = strrchr(rel, '/');
+ a->src = src;
+ a->file = rel;
+ a->from = fnode->id;
+ snprintf(a->dir, sizeof(a->dir), "%.*s", slash ? (int)(slash - rel) : 0, rel);
+ an_role_of(rel, a->role, sizeof(a->role));
+ an_role_dir(rel, a->role_dir, sizeof(a->role_dir));
+ const char *qn = fnode->qualified_name ? fnode->qualified_name : "";
+ size_t ql = strlen(qn);
+ size_t sl = sizeof(FILE_SUFFIX) - SKIP_ONE;
+ bool suffixed = ql > sl && strcmp(qn + ql - sl, FILE_SUFFIX) == 0;
+ snprintf(a->base, sizeof(a->base), "%.*s", (int)(suffixed ? ql - sl : ql), qn);
+ a->ordinal[0] = 0;
+ a->ordinal[SKIP_ONE] = 0;
+}
+
+/* Which candidates are Ansible: playbooks and role tasks / handlers files,
+ * then -- to a fixpoint, by a worklist -- what an Ansible file includes. */
+static void an_mark_ansible(const an_t *a, an_cands_t *c, const cbm_file_info_t *files) {
+ CBMHashTable *by_path = cbm_ht_create(0);
+ int *work = cbm_calloc(CBM_MEM_CLASS_OTHER, ((size_t)c->count + SKIP_ONE) * sizeof(int));
+ if (!by_path || !work) {
+ cbm_free(CBM_MEM_CLASS_OTHER, work);
+ if (by_path) {
+ cbm_ht_free(by_path);
+ }
+ return;
+ }
+ int top = 0;
+ for (int i = 0; i < c->count; i++) {
+ const char *rel = files[c->items[i].file].rel_path;
+ char role[CBM_SZ_1K];
+ an_role_dir(rel, role, sizeof(role));
+ cbm_ht_set(by_path, rel, (void *)(intptr_t)(i + SKIP_ONE));
+ if (c->items[i].playbook || role[0]) {
+ c->items[i].ansible = true;
+ work[top++] = i;
+ }
+ }
+ while (top > 0) {
+ const an_cand_t *from = &c->items[work[--top]];
+ for (int k = from->inc_lo; k < from->inc_hi; k++) {
+ int j =
+ a->incs[k] ? (int)(intptr_t)cbm_ht_get(by_path, a->incs[k]) - SKIP_ONE : -SKIP_ONE;
+ if (j >= 0 && !c->items[j].ansible) {
+ c->items[j].ansible = true;
+ work[top++] = j;
+ }
+ }
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, work);
+ cbm_ht_free(by_path);
+}
+
+static void an_free_ids_entry(const char *key, void *value, void *ud) {
+ (void)ud;
+ an_ids_t *v = value;
+ if (v) {
+ cbm_free(CBM_MEM_CLASS_OTHER, v->items);
+ cbm_free(CBM_MEM_CLASS_OTHER, v);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)key);
+}
+
+static void an_free(an_t *a) {
+ if (a->modules) {
+ cbm_ht_free(a->modules);
+ }
+ if (a->folders) {
+ cbm_ht_free(a->folders);
+ }
+ for (int i = 0; i < a->inc_n; i++) {
+ cbm_free(CBM_MEM_CLASS_OTHER, a->incs[i]);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, a->incs);
+ for (int i = 0; i < a->note_n; i++) {
+ cbm_free(CBM_MEM_CLASS_OTHER, a->notes[i].scope_role);
+ cbm_free(CBM_MEM_CLASS_OTHER, a->notes[i].scope_file);
+ cbm_free(CBM_MEM_CLASS_OTHER, a->notes[i].name);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, a->notes);
+ if (a->handlers) {
+ cbm_ht_foreach(a->handlers, an_free_ids_entry, NULL);
+ cbm_ht_free(a->handlers);
+ }
+}
+
+/* Parse `files[i]` when it is a YAML file whose top level is a sequence;
+ * NULL otherwise. The text stays in `*src` (the caller frees it). */
+static TSTree *an_parse(TSParser *parser, const cbm_file_info_t *f, char **src) {
+ size_t len = 0;
+ *src = an_is_yaml(f->rel_path) ? an_read(f->path, &len) : NULL;
+ if (!*src || !an_is_sequence(*src)) {
+ return NULL;
+ }
+ return ts_parser_parse_string(parser, NULL, *src, (uint32_t)len);
+}
+
void cbm_pipeline_pass_ansible(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count) {
an_t a = {.ctx = ctx};
- TSParser *parser = NULL;
+ an_cands_t cands = {0};
+ TSParser *parser = ts_parser_new();
CBMHashTable *file_nodes = NULL;
- for (int i = 0; i < file_count; i++) {
- if (!an_is_yaml(files[i].rel_path)) {
- continue;
- }
- size_t len = 0;
- char *src = an_read(files[i].path, &len);
- if (!src || !an_is_sequence(src)) {
- cbm_free(CBM_MEM_CLASS_OTHER, src);
- continue;
+ if (!parser || !ts_parser_set_language(parser, cbm_ts_language(CBM_LANG_YAML))) {
+ if (parser) {
+ ts_parser_delete(parser);
}
- if (!parser) {
- parser = ts_parser_new();
- if (!parser || !ts_parser_set_language(parser, cbm_ts_language(CBM_LANG_YAML))) {
- cbm_free(CBM_MEM_CLASS_OTHER, src);
- break;
- }
+ return;
+ }
+ /* pass 1: composition (IMPORTS); which files are playbooks, what each includes */
+ for (int i = 0; i < file_count; i++) {
+ char *src = NULL;
+ TSTree *tree = an_parse(parser, &files[i], &src);
+ if (tree && !file_nodes) {
a.modules = an_index(ctx, "Module");
a.folders = an_index(ctx, "Folder");
file_nodes = an_index(ctx, "File");
}
const cbm_gbuf_node_t *fnode =
- file_nodes ? cbm_ht_get(file_nodes, files[i].rel_path) : NULL;
- TSTree *tree = fnode ? ts_parser_parse_string(parser, NULL, src, (uint32_t)len) : NULL;
- if (tree) {
- const char *rel = files[i].rel_path;
- const char *slash = strrchr(rel, '/');
- a.src = src;
- a.file = rel;
- a.from = fnode->id;
- snprintf(a.dir, sizeof(a.dir), "%.*s", slash ? (int)(slash - rel) : 0, rel);
- an_role_of(rel, a.role, sizeof(a.role));
+ tree && file_nodes ? cbm_ht_get(file_nodes, files[i].rel_path) : NULL;
+ an_cand_t *c = fnode ? an_cand_push(&cands, i, a.inc_n) : NULL;
+ if (c) {
+ an_begin_file(&a, files[i].rel_path, fnode, src);
an_walk(&a, ts_tree_root_node(tree));
+ c->playbook = an_is_playbook(&a, an_top(ts_tree_root_node(tree)));
+ c->inc_hi = a.inc_n;
+ }
+ if (tree) {
ts_tree_delete(tree);
}
cbm_free(CBM_MEM_CLASS_OTHER, src);
}
- if (parser) {
- ts_parser_delete(parser);
- }
- if (a.modules) {
- cbm_ht_free(a.modules);
+ /* pass 2: the tasks of the Ansible files, then their notifies */
+ an_mark_ansible(&a, &cands, files);
+ a.handlers = cands.count > 0 ? cbm_ht_create(0) : NULL;
+ for (int c = 0; a.handlers && c < cands.count; c++) {
+ const cbm_file_info_t *f = &files[cands.items[c].file];
+ char *src = NULL;
+ TSTree *tree = cands.items[c].ansible ? an_parse(parser, f, &src) : NULL;
+ const cbm_gbuf_node_t *fnode = tree ? cbm_ht_get(file_nodes, f->rel_path) : NULL;
+ if (fnode) {
+ an_begin_file(&a, f->rel_path, fnode, src);
+ an_file_tasks(&a, an_top(ts_tree_root_node(tree)), cands.items[c].playbook);
+ }
+ if (tree) {
+ ts_tree_delete(tree);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, src);
}
- if (a.folders) {
- cbm_ht_free(a.folders);
+ if (a.handlers) {
+ an_link_notifies(&a);
}
+ ts_parser_delete(parser);
if (file_nodes) {
cbm_ht_free(file_nodes);
}
- if (a.edges > 0) {
- char buf[CBM_SZ_32];
- snprintf(buf, sizeof(buf), "%d", a.edges);
- cbm_log_info("pass.ansible.edges", "imports", buf);
+ cbm_free(CBM_MEM_CLASS_OTHER, cands.items);
+ an_free(&a);
+ if (a.edges > 0 || a.tasks > 0) {
+ char e[CBM_SZ_32];
+ char t[CBM_SZ_32];
+ char n[CBM_SZ_32];
+ snprintf(e, sizeof(e), "%d", a.edges);
+ snprintf(t, sizeof(t), "%d", a.tasks);
+ snprintf(n, sizeof(n), "%d", a.calls);
+ cbm_log_info("pass.ansible", "imports", e, "tasks", t, "notifies", n);
}
}
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index cd786062e..3098210ba 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -32,6 +32,7 @@ enum { INCR_RING_BUF = 4, INCR_RING_MASK = 3, INCR_TS_BUF = 24 };
#include "foundation/compat.h"
#include "foundation/compat_fs.h"
#include "foundation/compat_thread.h"
+#include "foundation/limits.h" /* cbm_max_file_bytes */
#include "foundation/platform.h"
#include "foundation/sha256.h"
@@ -2538,6 +2539,62 @@ static const char *closure_terraform_decline(char **paths, int n) {
return NULL;
}
+/* Ansible (pass_ansible.c): a task's notify CALLS the handlers of its role
+ * in another file, and whether a file is Ansible at all can hang on another
+ * file's include. The repair recomputes the changed files only, so a
+ * changed or deleted file that held Ansible tasks, or a changed YAML file
+ * whose top level is a sequence (it may be Ansible now), declines to the
+ * full rebuild. Returns the reason, or NULL. */
+static const char *closure_ansible_decline(cbm_store_t *store, const char *project,
+ const char *repo_path, char **paths, int n_changed,
+ int n_deleted) {
+ for (int i = 0; i < n_changed + n_deleted; i++) {
+ const char *rel = paths[i];
+ cbm_node_t *nodes = NULL;
+ int count = 0;
+ if (cbm_store_find_nodes_by_file(store, project, rel, &nodes, &count) != CBM_STORE_OK) {
+ return "ansible_lookup_failed";
+ }
+ bool held = false;
+ for (int j = 0; j < count && !held; j++) {
+ held = nodes[j].label && strcmp(nodes[j].label, "Function") == 0 &&
+ nodes[j].properties_json &&
+ strstr(nodes[j].properties_json, "\"source\":\"ansible\"");
+ }
+ cbm_store_free_nodes(nodes, count);
+ if (held) {
+ return "ansible_tasks_changed";
+ }
+ const char *dot = strrchr(rel, '.');
+ if (i >= n_changed || !dot || (strcmp(dot, ".yaml") != 0 && strcmp(dot, ".yml") != 0)) {
+ continue;
+ }
+ char path[CBM_SZ_4K];
+ snprintf(path, sizeof(path), "%s/%s", repo_path, rel);
+ FILE *f = cbm_fopen(path, "rb");
+ if (!f) {
+ continue;
+ }
+ (void)fseek(f, 0, SEEK_END);
+ long size = ftell(f);
+ (void)fseek(f, 0, SEEK_SET);
+ char *text = size > 0 && size <= cbm_max_file_bytes()
+ ? cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)size + SKIP_ONE)
+ : NULL;
+ size_t got = text ? fread(text, SKIP_ONE, (size_t)size, f) : 0;
+ (void)fclose(f);
+ if (text) {
+ text[got] = '\0';
+ }
+ bool sequence = text && cbm_ansible_is_sequence(text);
+ cbm_free(CBM_MEM_CLASS_OTHER, text);
+ if (sequence) {
+ return "ansible_content_changed";
+ }
+ }
+ return NULL;
+}
+
/* The env join (pass_envjoin.c) reads each image's files from its
* Dockerfile's Module, stored with the ignore patterns of its context: an
* ignore file changing, or a deletion of a file an image copies (which
@@ -2763,6 +2820,11 @@ static int closure_try_plan(cbm_pipeline_t *p, cbm_store_t *store, const char *p
if (decline) {
goto done;
}
+ decline = closure_ansible_decline(store, project, cbm_pipeline_repo_path(p), changed_paths,
+ n_changed, n_deleted);
+ if (decline) {
+ goto done;
+ }
decline =
closure_stem_mark_decline(project, files, file_count, changed_paths + n_changed, n_deleted);
if (decline) {
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 207ba9382..d0069a30c 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -1316,11 +1316,16 @@ char *cbm_k8s_resource_props(const char *qn, const char *effective_ns, const cha
const char *labels, const char *ecosystem);
/* Ansible (pass_ansible.c): a playbook or task file IMPORTS the playbooks,
- * task files and roles its directives name. Run at the end of the K8s pass,
- * over the same files. */
+ * task files and roles its directives name; its tasks and handlers are
+ * Functions, a notify CALLS the handlers it names. Run at the end of the
+ * K8s pass, over the same files. */
void cbm_pipeline_pass_ansible(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count);
+/* Does a YAML text's first content line start a sequence item (pass_ansible.c)?
+ * The incremental planner's test for Ansible content. */
+bool cbm_ansible_is_sequence(const char *src);
+
/* `v` (a path; "./" and "../" allowed) from repository directory `dir`, as a
* repository path into out; false past the root or when it does not fit
* (pass_terraform.c). */
@@ -1539,8 +1544,10 @@ int cbm_pipeline_build_fresh_semantic_manifest(cbm_pipeline_t *p, const char *pr
* {ecosystem:terraform} its block DEPLOYS
* (pass_terraform.c, extract_unified.c, extract_defs.c); an Ansible
* playbook / task file IMPORTS {via:ansible} the playbooks, task files
- * and roles its directives name (pass_ansible.c). An index built
- * before holds none of them, so it rebuilds once on upgrade. */
+ * and roles its directives name, its tasks and handlers are Functions
+ * {source:ansible} and a notify CALLS {via:notify} the handlers it names
+ * (pass_ansible.c). An index built before holds none of them, so it
+ * rebuilds once on upgrade. */
enum { CBM_SEMANTIC_INDEX_VERSION = 9 };
typedef struct {
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index 2e9d7e4bc..8ee89ffba 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -3373,6 +3373,117 @@ TEST(probe_kustomize_name_prefix_makes_variants) {
PASS();
}
+/* Every Ansible task / handler as "<file>:<line> <kind> <name> [<module>]",
+ * then every notify as "<task> -> <handler file>:<handler> @<line>", then
+ * "functions <n> defined <m>". */
+static char *gpf_ansible_tasks(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((SELECT group_concat(x, char(10)) FROM (SELECT file_path || ':' || "
+ "start_line || ' ' || json_extract(properties,'$.kind') || ' ' || name || ' [' || "
+ "json_extract(properties,'$.module') || ']' AS x FROM nodes WHERE project=?1 AND "
+ "label='Function' AND json_extract(properties,'$.source')='ansible' ORDER BY "
+ "file_path, start_line)), '') || char(10) || coalesce((SELECT group_concat(y, "
+ "char(10)) FROM (SELECT s.name || ' -> ' || t.file_path || ':' || t.name || ' @' || "
+ "json_extract(e.properties,'$.line') AS y 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='CALLS' AND json_extract(e.properties,'$.via')='notify' ORDER BY y)), '') || "
+ "char(10) || 'functions ' || (SELECT COUNT(*) FROM nodes WHERE project=?1 AND "
+ "label='Function' AND json_extract(properties,'$.source')='ansible') || ' defined ' "
+ "|| (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.project=?1 AND e.type='DEFINES' AND s.label='File' AND "
+ "json_extract(t.properties,'$.source')='ansible')";
+ 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 Ansible task (or handler) is a Function, named by its `name:` (else its
+ * module), {source: ansible, kind, module}, which its file DEFINES:
+ * a playbook's pre_tasks / tasks / post_tasks / handlers, a role's tasks/
+ * and handlers/ files, and files an Ansible file includes; a block's tasks
+ * (block, rescue, always), never the block itself. A notify CALLS {via:
+ * notify, line} each handler of that name or listen topic in the task's own
+ * role (a nested role is the directory before its tasks/), else in its file.
+ * A YAML list that is no Ansible (a JSON patch) holds none. */
+TEST(probe_ansible_tasks_are_functions) {
+ static const GpfFile files[] = {
+ {"site.yml",
+ "- hosts: all\n pre_tasks:\n - name: Prep\n command: /bin/true\n"
+ " roles:\n - web\n handlers:\n - name: reload all\n"
+ " service: name=x state=reloaded\n tasks:\n - name: Ping\n ping:\n"
+ " notify: reload all\n- import_playbook: other.yml\n"},
+ {"other.yml",
+ "- hosts: db\n tasks:\n - debug: msg=hi\n - include_tasks: tasks/common.yml\n"},
+ {"tasks/common.yml",
+ "- name: Common\n shell: echo c\n- name: \"Say \\\"hi\\\"\"\n debug:\n msg: x\n"
+ "- name: 'it''s'\n debug:\n msg: y\n- name: Act\n action: shell echo a\n"},
+ {"roles/web/tasks/main.yml",
+ "- name: Install web\n package:\n name: nginx\n notify:\n - restart web\n"
+ " - web changed\n- block:\n - name: Inside block\n template:\n"
+ " src: a.j2\n dest: /etc/a\n notify: restart web\n rescue:\n"
+ " - name: Rescue it\n debug:\n msg: failed\n always:\n"
+ " - name: Always it\n ansible.builtin.file:\n path: /tmp/x\n"
+ " state: absent\n- include_tasks: extra.yml\n"},
+ {"roles/web/tasks/extra.yml", "- name: Extra\n command: echo hi\n"},
+ {"roles/web/handlers/main.yml",
+ "- name: restart web\n service:\n name: nginx\n state: restarted\n"
+ "- name: log change\n debug:\n msg: changed\n listen: web changed\n"},
+ {"roles/apps/csi/tasks/main.yml", "- name: Csi\n command: x\n notify: restart csi\n"},
+ {"roles/apps/csi/handlers/main.yml", "- name: restart csi\n command: y\n"},
+ {"roles/apps/lb/handlers/main.yml", "- name: restart csi\n command: w\n"},
+ {"roles/other/handlers/main.yml", "- name: restart csi\n command: z\n"},
+ {"patches/ops.yaml", "- op: add\n path: /x\n value: 1\n"}};
+ int n = (int)(sizeof(files) / sizeof(files[0]));
+ char *got = gpf_ansible_tasks(files, n);
+ static const char want[] =
+ "other.yml:3 task debug [debug]\n"
+ "other.yml:4 task include_tasks [include_tasks]\n"
+ "roles/apps/csi/handlers/main.yml:1 handler restart csi [command]\n"
+ "roles/apps/csi/tasks/main.yml:1 task Csi [command]\n"
+ "roles/apps/lb/handlers/main.yml:1 handler restart csi [command]\n"
+ "roles/other/handlers/main.yml:1 handler restart csi [command]\n"
+ "roles/web/handlers/main.yml:1 handler restart web [service]\n"
+ "roles/web/handlers/main.yml:5 handler log change [debug]\n"
+ "roles/web/tasks/extra.yml:1 task Extra [command]\n"
+ "roles/web/tasks/main.yml:1 task Install web [package]\n"
+ "roles/web/tasks/main.yml:8 task Inside block [template]\n"
+ "roles/web/tasks/main.yml:14 task Rescue it [debug]\n"
+ "roles/web/tasks/main.yml:18 task Always it [file]\n"
+ "roles/web/tasks/main.yml:22 task include_tasks [include_tasks]\n"
+ "site.yml:3 task Prep [command]\n"
+ "site.yml:8 handler reload all [service]\n"
+ "site.yml:11 task Ping [ping]\n"
+ "tasks/common.yml:1 task Common [shell]\n"
+ "tasks/common.yml:3 task Say \"hi\" [debug]\n"
+ "tasks/common.yml:6 task it's [debug]\n"
+ "tasks/common.yml:9 task Act [shell]\n"
+ "Csi -> roles/apps/csi/handlers/main.yml:restart csi @3\n"
+ "Inside block -> roles/web/handlers/main.yml:restart web @12\n"
+ "Install web -> roles/web/handlers/main.yml:log change @4\n"
+ "Install web -> roles/web/handlers/main.yml:restart web @4\n"
+ "Ping -> site.yml:reload all @13\n"
+ "functions 21 defined 21";
+ if (!got || strcmp(got, want) != 0) {
+ fprintf(stderr, "ansible tasks got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
+ free(got);
+ FAIL("ansible tasks");
+ }
+ free(got);
+ PASS();
+}
+
/* Every script HTTP_CALLS edge as "<source file> <route QN>", then a line
* "edgeless routes <n>": the Route nodes no edge touches. */
static char *gpf_script_http(const GpfFile *files, int nfiles) {
@@ -5818,6 +5929,7 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_ansible_files_import_what_they_name);
RUN_TEST(probe_terraform_kubernetes_objects_are_resources);
RUN_TEST(probe_kustomize_name_prefix_makes_variants);
+ RUN_TEST(probe_ansible_tasks_are_functions);
RUN_TEST(probe_image_hub_splits_by_component);
RUN_TEST(probe_gitops_objects_deploy_their_units);
RUN_TEST(probe_shell_kubectl_deploys_what_it_applies);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 3c2f48941..a770e34e3 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -1533,6 +1533,65 @@ TEST(pipeline_incremental_keeps_ansible_includes_of_an_edited_file) {
PASS();
}
+/* The CALLS edges an Ansible notify draws (via notify); -1 on a query error. */
+static int ansible_notify_calls(cbm_store_t *s, const char *project) {
+ sqlite3_stmt *st = NULL;
+ int n = -1;
+ if (sqlite3_prepare_v2(cbm_store_get_db(s),
+ "SELECT COUNT(*) FROM edges WHERE project=?1 AND type='CALLS' AND "
+ "json_extract(properties,'$.via')='notify'",
+ -1, &st, NULL) == SQLITE_OK &&
+ sqlite3_bind_text(st, 1, project, -1, SQLITE_TRANSIENT) == SQLITE_OK &&
+ sqlite3_step(st) == SQLITE_ROW) {
+ n = sqlite3_column_int(st, 0);
+ }
+ sqlite3_finalize(st);
+ return n;
+}
+
+/* A task's notify CALLS the handlers of its role in another file. Editing
+ * either file alone must land on the full graph: the handler file (a listen
+ * topic renamed: one handler stops answering) re-creates its handlers, the
+ * unchanged task file is not re-walked; the task file (a notifying task
+ * added) is re-walked without the unchanged handler file. Both decline to
+ * the full rebuild (closure_ansible_decline); the repair route kept a stale
+ * edge in the first and lost every notify of the edited file in the second. */
+TEST(pipeline_incremental_keeps_ansible_notifies_across_files) {
+ if (setup_test_repo() != 0) {
+ FAIL("failed to create temp dir");
+ }
+ write_temp_file(g_tmpdir, "roles/app/tasks/main.yml",
+ "- name: configure\n command: /bin/true\n notify: restart app\n");
+ write_temp_file(g_tmpdir, "roles/app/handlers/main.yml",
+ "- name: h1\n command: one\n listen: restart app\n"
+ "- name: h2\n command: two\n listen: restart app\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/test_incr_notify.db", g_tmpdir);
+ int want[] = {2, 1, 2};
+ for (int step = 0; step < 3; step++) {
+ if (step == 1) {
+ write_temp_file(g_tmpdir, "roles/app/handlers/main.yml",
+ "- name: h1\n command: one\n listen: restart app\n"
+ "- name: h2\n command: two\n listen: something else\n");
+ } else if (step == 2) {
+ write_temp_file(g_tmpdir, "roles/app/tasks/main.yml",
+ "- name: configure\n command: /bin/true\n notify: restart app\n"
+ "- name: reconfigure\n command: /bin/false\n notify: restart app\n");
+ }
+ cbm_pipeline_t *p = cbm_pipeline_new(g_tmpdir, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+ int calls = ansible_notify_calls(s, cbm_pipeline_project_name(p));
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ ASSERT_EQ(calls, want[step]);
+ }
+ teardown_test_repo();
+ PASS();
+}
+
/* Regression: a Terraform module's output read lives in one file
* (infra/outputs.tf: module.net.vpc_id) and the module's source in another
* (infra/main.tf). Pointing the source elsewhere edits main.tf alone; the
@@ -23235,6 +23294,7 @@ SUITE(pipeline) {
RUN_TEST(pipeline_incremental_preserves_cross_file_calls);
RUN_TEST(pipeline_incremental_drops_terraform_reads_of_a_moved_module);
RUN_TEST(pipeline_incremental_keeps_ansible_includes_of_an_edited_file);
+ RUN_TEST(pipeline_incremental_keeps_ansible_notifies_across_files);
RUN_TEST(pipeline_field_named_like_method_keeps_its_reads_and_writes);
RUN_TEST(pipeline_objectscript_export_preserves_calls_sequential_parallel);
RUN_TEST(pipeline_objectscript_export_incremental_matches_full_relationships);