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(mcp): add manage_adr mode='set_sections'
Martin Vogel7bef7f4d663e
diff --git a/README.md b/README.mdindex df5c33441..3c99104e5 100644--- a/README.md+++ b/README.md@@ -666,7 +666,7 @@ JSON arguments can also be piped on stdin, for tools that take arguments. A tool| `get_code_snippet` | Read source code for a function by qualified name. || `get_architecture` | Codebase overview: languages, packages, routes, hotspots, clusters, ADR. || `search_code` | Grep-like text search within indexed project files. |-| `manage_adr` | CRUD for Architecture Decision Records. Query modes do not wait behind a same-project reindex; writes remain serialized. |+| `manage_adr` | CRUD for Architecture Decision Records (`get` reads, `update` replaces the whole document, `set_sections` rewrites only the named sections, `sections` lists headings). Query modes do not wait behind a same-project reindex; writes remain serialized. || `ingest_traces` | Ingest runtime traces to validate HTTP_CALLS edges. |`manage_adr` query modes (`get` and `sections`) use the server's cached query store so they can proceed while a same-project reindex is running. If another process publishes a replacement store during reindexing, they can return the pre-publication ADR until idle eviction refreshes that cache. Updates remain serialized through the project mutation guard.diff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex 5e64fabdf..f07ab1f33 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -706,10 +706,19 @@ static const tool_def_t TOOLS[] = {{"manage_adr", "Manage ADR", "Create or update Architecture Decision Records","{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},\"mode\":{\"type\":"- "\"string\",\"enum\":[\"get\",\"update\",\"sections\"],\"description\":\"update replaces "- "the entire ADR document; sections only lists existing "- "headings\"},\"content\":{\"type\":\"string\",\"description\":\"Complete replacement document "- "required by update\"}},\"additionalProperties\":false,"+ "\"string\",\"enum\":[\"get\",\"update\",\"set_sections\",\"sections\"],\"description\":"+ "\"update REPLACES the entire ADR document; set_sections rewrites only the named sections "+ "and leaves every other byte of the stored document untouched, so adding one entry does not "+ "mean re-sending the whole ADR (setting the same section to the same body twice leaves the "+ "document byte-identical, so retrying after a lost response is safe); sections only lists "+ "existing headings\"},\"content\":{\"type\":\"string\",\"description\":\"Complete replacement "+ "document required by update\"},\"section_updates\":{\"type\":\"object\",\"description\":"+ "\"Required by set_sections: section name -> new body for that section. Only the six "+ "canonical sections exist; any other name is rejected.\",\"properties\":{"+ "\"PURPOSE\":{\"type\":\"string\"},\"STACK\":{\"type\":\"string\"},"+ "\"ARCHITECTURE\":{\"type\":\"string\"},\"PATTERNS\":{\"type\":\"string\"},"+ "\"TRADEOFFS\":{\"type\":\"string\"},\"PHILOSOPHY\":{\"type\":\"string\"}},"+ "\"additionalProperties\":false}},\"additionalProperties\":false,""\"required\":[\"project\"]}"},{"ingest_traces", "Ingest traces", "Ingest runtime traces to enhance the knowledge graph",@@ -11848,6 +11857,123 @@ static cbm_store_t *open_adr_store_for_write(cbm_mcp_server_t *srv, cbm_store_treturn *owned_rw;}+/* Parsed `section_updates` for mode='set_sections'.+ *+ * mode='update' replaces the whole document, so adding one entry costs a full+ * re-send and the stored ADR is only ever as good as that round-trip. Writing+ * named sections instead leaves the rest of the document as the authority for+ * itself — and, unlike a whole-document append, applying the same request+ * twice yields the same document, so a client that retries after a lost+ * response cannot silently duplicate content. */+typedef struct {+ char *keys[PROPS_MAX];+ char *values[PROPS_MAX];+ int count;+ /* Rejection reason, or NULL when the request parsed cleanly. Set means no+ * store was opened and nothing was written. */+ const char *status;+ const char *error;+} adr_section_updates_t;++static void adr_section_updates_free(adr_section_updates_t *u) {+ for (int i = 0; i < u->count; i++) {+ free(u->keys[i]);+ free(u->values[i]);+ }+ u->count = 0;+}++static bool adr_collect_section_update(adr_section_updates_t *u, yyjson_val *key, yyjson_val *val) {+ const char *name = yyjson_get_str(key);+ if (!name || !name[0]) {+ u->status = "invalid_section_updates";+ u->error = "'section_updates' keys must be non-empty section names. "+ "No ADR write was performed.";+ return false;+ }+ if (!yyjson_is_str(val)) {+ u->status = "invalid_section_updates";+ u->error = "'section_updates' values must be strings (the new body for that section). "+ "No ADR write was performed.";+ return false;+ }+ const char *body = yyjson_get_str(val);+ /* An empty body would render a heading with nothing under it — a silent+ * content deletion wearing the response shape of an update. Clearing a+ * section is whole-document surgery; that is what mode='update' is for. */+ if (!body || !body[0]) {+ u->status = "empty_section_content";+ u->error = "'section_updates' values must be non-empty; use mode='update' to remove a "+ "section. No ADR write was performed.";+ return false;+ }+ if (u->count >= PROPS_MAX) {+ u->status = "too_many_sections";+ u->error = "'section_updates' carries more entries than an ADR can hold. "+ "No ADR write was performed.";+ return false;+ }+ u->keys[u->count] = heap_strdup(name);+ u->values[u->count] = heap_strdup(body);+ if (!u->keys[u->count] || !u->values[u->count]) {+ free(u->keys[u->count]);+ free(u->values[u->count]);+ u->status = "write_error";+ u->error = "out of memory parsing 'section_updates'. No ADR write was performed.";+ return false;+ }+ u->count++;+ return true;+}++static adr_section_updates_t adr_parse_section_updates(const char *args) {+ adr_section_updates_t u;+ memset(&u, 0, sizeof(u));++ yyjson_doc *doc = yyjson_read(args, strlen(args), 0);+ yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;+ yyjson_val *updates =+ (root && yyjson_is_obj(root)) ? yyjson_obj_get(root, "section_updates") : NULL;++ if (!updates) {+ /* Never fall through to 'get': a caller that meant to write must not+ * receive a success-shaped read. */+ u.status = "missing_section_updates";+ u.error = "mode='set_sections' requires 'section_updates', an object mapping section "+ "name to its new body. No ADR write was performed.";+ } else if (!yyjson_is_obj(updates) || yyjson_obj_size(updates) == 0) {+ u.status = "invalid_section_updates";+ u.error = "'section_updates' must be a non-empty object mapping section name to its new "+ "body. No ADR write was performed.";+ } else {+ size_t idx = 0;+ size_t max = 0;+ yyjson_val *key = NULL;+ yyjson_val *val = NULL;+ yyjson_obj_foreach(updates, idx, max, key, val) {+ if (!adr_collect_section_update(&u, key, val)) {+ adr_section_updates_free(&u);+ break;+ }+ }+ }+ yyjson_doc_free(doc);+ return u;+}++/* Build the rejection payload for a set_sections request that never reached a+ * store. Caller frees. */+static char *adr_section_updates_error(const adr_section_updates_t *u) {+ yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *root_obj = yyjson_mut_obj(doc);+ yyjson_mut_doc_set_root(doc, root_obj);+ yyjson_mut_obj_add_str(doc, root_obj, "status", u->status);+ yyjson_mut_obj_add_strcpy(doc, root_obj, "error", u->error);+ char *json = yy_doc_to_str(doc);+ yyjson_mut_doc_free(doc);+ return json;+}+static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {char *project = get_project_arg(args);char *mode_str = cbm_mcp_get_string_arg(args, "mode");@@ -11879,12 +12005,51 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {true);}+ bool set_sections_mode = (strcmp(mode_str, "set_sections") == 0);+ adr_section_updates_t updates;+ memset(&updates, 0, sizeof(updates));+ char section_key_err[CBM_SZ_256] = "";+ if (set_sections_mode) {+ updates = adr_parse_section_updates(args);+ /* Only the six canonical names are writable, and that is a correctness+ * constraint rather than a house style: adr_try_section_header() parses+ * ONLY canonical headers, so a '## FOO' written here would be read back+ * as body text of the section above it and a second identical write+ * would append a duplicate — destroying exactly the idempotence this+ * mode exists to provide. */+ if (!updates.status && cbm_adr_validate_section_keys(+ (const char **)updates.keys, updates.count, section_key_err,+ (int)sizeof(section_key_err)) != CBM_STORE_OK) {+ adr_section_updates_free(&updates);+ updates.status = "invalid_section_name";+ updates.error = section_key_err;+ }+ if (updates.status) {+ /* Reject before taking the project lease or opening a store: a+ * malformed write must not block an index, and must not read. */+ char *err = adr_section_updates_error(&updates);+ adr_section_updates_free(&updates);+ free(project);+ free(mode_str);+ free(content);+ char *res = cbm_mcp_text_result(err, true);+ free(err);+ return res;+ }+ }++ /* This classification is load-bearing. A mode missing from it takes no+ * per-project mutation lease, resolves the store query-only, and never+ * reaches open_adr_store_for_write — so its write would be attempted+ * through a read-only handle, concurrently with an active index. */bool write_request =- content && (strcmp(mode_str, "update") == 0 || strcmp(mode_str, "store") == 0);+ (content && (strcmp(mode_str, "update") == 0 || strcmp(mode_str, "store") == 0)) ||+ set_sections_mode;bool mutation_held = false;if (write_request && project) {mutation_held = mcp_project_mutation_begin(srv, project);if (!mutation_held) {+ adr_section_updates_free(&updates);free(project);free(mode_str);free(content);@@ -11893,6 +12058,7 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {}if (mcp_request_cancelled(srv)) {mcp_project_mutation_end(srv, project);+ adr_section_updates_free(&updates);free(project);free(mode_str);free(content);@@ -11921,6 +12087,7 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {if (mutation_held) {mcp_project_mutation_end(srv, project);}+ adr_section_updates_free(&updates);free(project);free(mode_str);free(content);@@ -11935,6 +12102,7 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {if (mutation_held) {mcp_project_mutation_end(srv, project);}+ adr_section_updates_free(&updates);free(project);free(mode_str);free(content);@@ -11981,13 +12149,69 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {}}+ /* A set_sections write must see a legacy file-backed ADR too. The+ * migration above deliberately runs on the read path only — it must never+ * block on the lease — so the write path reads the file here, where the+ * exclusive project lease and a writable store are already held. Merging+ * onto an empty document instead would silently discard an ADR the user+ * still has on disk. */+ char *legacy_seed = NULL;+ if (set_sections_mode && !have_adr) {+ char *root_path = project_root_from_store(store, project);+ legacy_seed = adr_read_legacy_file(root_path);+ free(root_path);+ }+yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);yyjson_mut_val *root_obj = yyjson_mut_obj(doc);yyjson_mut_doc_set_root(doc, root_obj);bool is_error = false;const char *adr_content = have_adr ? adr.content : legacy;- if (write_request) {+ if (set_sections_mode) {+ /* cbm_store_adr_update_sections requires an existing row — its+ * contract, pinned by TEST(adr_update_no_existing). Seed one when the+ * project has none, so the mode degrades to a plain create: the legacy+ * document when there is one, an empty document otherwise. */+ bool base_present = have_adr;+ bool seeded_empty = false;+ if (!base_present) {+ const char *seed = legacy_seed ? legacy_seed : "";+ if (cbm_store_adr_store(store, project, seed) == CBM_STORE_OK) {+ base_present = true;+ seeded_empty = (legacy_seed == NULL);+ }+ }+ cbm_adr_t updated;+ memset(&updated, 0, sizeof(updated));+ int section_rc = base_present ? cbm_store_adr_update_sections(+ store, project, (const char **)updates.keys,+ (const char **)updates.values, updates.count, &updated)+ : CBM_STORE_ERR;+ if (section_rc == CBM_STORE_OK) {+ yyjson_mut_obj_add_str(doc, root_obj, "status", "sections_updated");+ yyjson_mut_obj_add_str(doc, root_obj, "semantics",+ "named_sections_replaced_rest_preserved");+ yyjson_mut_obj_add_uint(doc, root_obj, "sections_written", (uint64_t)updates.count);+ /* Callers confirm the write landed without re-fetching the ADR. */+ yyjson_mut_obj_add_uint(doc, root_obj, "content_length",+ (uint64_t)strlen(updated.content));+ cbm_store_adr_free(&updated);+ } else {+ /* Undo an empty seed. A rejected write must not leave the project+ * holding a blank ADR where `get` used to answer no_adr. A legacy+ * seed is a real migration and is kept. */+ if (seeded_empty) {+ (void)cbm_store_adr_delete(store, project);+ }+ yyjson_mut_obj_add_str(doc, root_obj, "status", "write_error");+ const char *store_err = cbm_store_error(store);+ if (store_err && store_err[0]) {+ yyjson_mut_obj_add_strcpy(doc, root_obj, "error", store_err);+ }+ is_error = true;+ }+ } else if (write_request) {if (cbm_store_adr_store(store, project, content) == CBM_STORE_OK) {yyjson_mut_obj_add_str(doc, root_obj, "status", "updated");yyjson_mut_obj_add_str(doc, root_obj, "semantics", "whole_document_replaced");@@ -12018,6 +12242,8 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {if (mutation_held) {mcp_project_mutation_end(srv, project);}+ adr_section_updates_free(&updates);+ free(legacy_seed);free(legacy);free(project);free(mode_str);diff --git a/src/store/store.c b/src/store/store.cindex c8a3528d7..e8bc8e5f4 100644--- a/src/store/store.c+++ b/src/store/store.c@@ -8883,10 +8883,25 @@ int cbm_store_adr_delete(cbm_store_t *s, const char *project) {int cbm_store_adr_update_sections(cbm_store_t *s, const char *project, const char **keys,const char **values, int count, cbm_adr_t *out) {+ if (!s || !s->db || !project || !keys || !values || !out) {+ return CBM_STORE_ERR;+ }++ /* The read-modify-write below must be ONE transaction. Three writers+ * replace this row wholesale — the indexing pipeline, the UI POST+ * /api/adr handler, and manage_adr mode='update' — so an unguarded+ * get/merge/store silently loses whichever of them commits between the+ * read and the UPSERT. BEGIN IMMEDIATE takes the write lock up front, so a+ * competing writer waits rather than being overwritten. */+ if (cbm_store_begin(s) != CBM_STORE_OK) {+ return CBM_STORE_ERR;+ }+/* Get existing ADR */cbm_adr_t existing;int rc = cbm_store_adr_get(s, project, &existing);if (rc != CBM_STORE_OK) {+ (void)cbm_store_rollback(s);store_set_error(s, "no existing ADR to update");return rc;}@@ -8917,13 +8932,20 @@ int cbm_store_adr_update_sections(cbm_store_t *s, const char *project, const chachar *merged = cbm_adr_render(§ions);cbm_adr_sections_free(§ions);+ if (!merged) {+ (void)cbm_store_rollback(s);+ store_set_error(s, "failed to render merged ADR");+ return CBM_STORE_ERR;+ }+/* Check length */if ((int)strlen(merged) > CBM_ADR_MAX_LENGTH) {char msg[CBM_SZ_128];snprintf(msg, sizeof(msg), "merged ADR exceeds %d chars (%d chars)", CBM_ADR_MAX_LENGTH,(int)strlen(merged));- store_set_error(s, msg);free(merged);+ (void)cbm_store_rollback(s);+ store_set_error(s, msg);return CBM_STORE_ERR;}@@ -8931,10 +8953,22 @@ int cbm_store_adr_update_sections(cbm_store_t *s, const char *project, const charc = cbm_store_adr_store(s, project, merged);free(merged);if (rc != CBM_STORE_OK) {+ (void)cbm_store_rollback(s);return rc;}- return cbm_store_adr_get(s, project, out);+ /* Read back INSIDE the transaction so `out` is exactly what commits. */+ rc = cbm_store_adr_get(s, project, out);+ if (rc != CBM_STORE_OK) {+ (void)cbm_store_rollback(s);+ return rc;+ }+ if (cbm_store_commit(s) != CBM_STORE_OK) {+ cbm_store_adr_free(out);+ (void)cbm_store_rollback(s);+ return CBM_STORE_ERR;+ }+ return CBM_STORE_OK;}void cbm_store_adr_free(cbm_adr_t *adr) {diff --git a/tests/test_mcp.c b/tests/test_mcp.cindex de36cb6bd..21b5826b3 100644--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@ -5847,6 +5847,285 @@ TEST(tool_manage_adr_rejects_removed_sections_argument) {PASS();}+/* mode='set_sections' rewrites only the named sections. mode='update' replaces+ * the whole document, so adding one entry costs a full re-send and every byte+ * the caller did not mean to touch survives only as well as that round-trip. */+TEST(tool_manage_adr_set_sections_replaces_only_named) {+ const char *project = "adr-sec-named";+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ ASSERT_EQ(cbm_store_upsert_project(st, project, "/tmp/adr-sec-named"), CBM_STORE_OK);+ cbm_mcp_server_set_project(srv, project);+ ASSERT_EQ(cbm_store_adr_store(st, project, "## PURPOSE\nOriginal purpose.\n\n## STACK\nC."),+ CBM_STORE_OK);++ /* A section write is a mutation: it must take the per-project lease, or it+ * runs concurrently with an index through a query-only store handle. */+ mcp_mutation_guard_probe_t probe = {0};+ cbm_mcp_server_set_project_mutation_guard(srv, mcp_mutation_guard_probe_begin,+ mcp_mutation_guard_probe_end, &probe);++ char *resp = cbm_mcp_handle_tool(srv, "manage_adr",+ "{\"project\":\"adr-sec-named\",\"mode\":\"set_sections\","+ "\"section_updates\":{\"PATTERNS\":\"- Pipeline stages.\"}}");+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "sections_updated"));+ ASSERT_NULL(strstr(resp, "\"isError\":true"));+ free(resp);+ ASSERT_EQ(probe.begin_count, 1);+ ASSERT_EQ(probe.end_count, 1);+ ASSERT_STR_EQ(probe.begin_projects[0], project);++ /* The sections nobody named survive verbatim, and the named one landed. */+ cbm_adr_t adr;+ memset(&adr, 0, sizeof(adr));+ ASSERT_EQ(cbm_store_adr_get(st, project, &adr), CBM_STORE_OK);+ ASSERT_NOT_NULL(adr.content);+ ASSERT_NOT_NULL(strstr(adr.content, "## PURPOSE\nOriginal purpose."));+ ASSERT_NOT_NULL(strstr(adr.content, "## STACK\nC."));+ ASSERT_NOT_NULL(strstr(adr.content, "## PATTERNS\n- Pipeline stages."));+ cbm_store_adr_free(&adr);++ cbm_mcp_server_free(srv);+ PASS();+}++/* THE reason this shape was chosen over a whole-document append: applying the+ * same request twice must leave the document byte-identical. An MCP client that+ * loses a response and retries would silently duplicate an appended chunk. */+TEST(tool_manage_adr_set_sections_is_idempotent) {+ const char *project = "adr-sec-idem";+ const char *request = "{\"project\":\"adr-sec-idem\",\"mode\":\"set_sections\","+ "\"section_updates\":{\"PATTERNS\":\"- Pipeline stages.\"}}";+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ ASSERT_EQ(cbm_store_upsert_project(st, project, "/tmp/adr-sec-idem"), CBM_STORE_OK);+ cbm_mcp_server_set_project(srv, project);+ ASSERT_EQ(cbm_store_adr_store(st, project, "## PURPOSE\nOriginal purpose.\n\n## STACK\nC."),+ CBM_STORE_OK);++ char *resp = cbm_mcp_handle_tool(srv, "manage_adr", request);+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "sections_updated"));+ free(resp);++ cbm_adr_t first;+ memset(&first, 0, sizeof(first));+ ASSERT_EQ(cbm_store_adr_get(st, project, &first), CBM_STORE_OK);+ ASSERT_NOT_NULL(first.content);+ char *after_first = strdup(first.content);+ ASSERT_NOT_NULL(after_first);+ cbm_store_adr_free(&first);++ /* Replay the identical request — the lost-response retry. */+ resp = cbm_mcp_handle_tool(srv, "manage_adr", request);+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "sections_updated"));+ ASSERT_NULL(strstr(resp, "\"isError\":true"));+ free(resp);++ cbm_adr_t second;+ memset(&second, 0, sizeof(second));+ ASSERT_EQ(cbm_store_adr_get(st, project, &second), CBM_STORE_OK);+ ASSERT_NOT_NULL(second.content);+ ASSERT_STR_EQ(second.content, after_first);+ /* And the body is present exactly once, not appended twice. */+ const char *hit = strstr(second.content, "- Pipeline stages.");+ ASSERT_NOT_NULL(hit);+ ASSERT_NULL(strstr(hit + 1, "- Pipeline stages."));+ cbm_store_adr_free(&second);+ free(after_first);++ cbm_mcp_server_free(srv);+ PASS();+}++/* A project with no ADR yet degrades to a plain create rather than erroring:+ * the store primitive requires an existing row, so the handler seeds one. */+TEST(tool_manage_adr_set_sections_creates_when_absent) {+ const char *project = "adr-sec-new";+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ ASSERT_EQ(cbm_store_upsert_project(st, project, "/tmp/adr-sec-new"), CBM_STORE_OK);+ cbm_mcp_server_set_project(srv, project);++ char *resp = cbm_mcp_handle_tool(srv, "manage_adr",+ "{\"project\":\"adr-sec-new\",\"mode\":\"set_sections\","+ "\"section_updates\":{\"PURPOSE\":\"Only entry.\"}}");+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "sections_updated"));+ ASSERT_NULL(strstr(resp, "\"isError\":true"));+ free(resp);++ cbm_adr_t adr;+ memset(&adr, 0, sizeof(adr));+ ASSERT_EQ(cbm_store_adr_get(st, project, &adr), CBM_STORE_OK);+ /* Exact match: a create must not leave a leading separator behind. */+ ASSERT_STR_EQ(adr.content, "## PURPOSE\nOnly entry.");+ cbm_store_adr_free(&adr);++ cbm_mcp_server_free(srv);+ PASS();+}++/* set_sections without section_updates must fail loudly. Falling through to+ * 'get' would hand a caller that meant to write a success-shaped read — and it+ * must not take the mutation lease on the way to being rejected. */+TEST(tool_manage_adr_set_sections_without_updates_errors) {+ const char *project = "adr-sec-missing";+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ ASSERT_EQ(cbm_store_upsert_project(st, project, "/tmp/adr-sec-missing"), CBM_STORE_OK);+ cbm_mcp_server_set_project(srv, project);+ ASSERT_EQ(cbm_store_adr_store(st, project, "## PURPOSE\nUntouched.\n"), CBM_STORE_OK);++ mcp_mutation_guard_probe_t probe = {0};+ cbm_mcp_server_set_project_mutation_guard(srv, mcp_mutation_guard_probe_begin,+ mcp_mutation_guard_probe_end, &probe);++ char *resp = cbm_mcp_handle_tool(srv, "manage_adr",+ "{\"project\":\"adr-sec-missing\",\"mode\":\"set_sections\"}");+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "missing_section_updates"));+ ASSERT_NOT_NULL(strstr(resp, "No ADR write was performed"));+ ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));+ ASSERT_NULL(strstr(resp, "adr_hint"));+ free(resp);+ ASSERT_EQ(probe.begin_count, 0);+ ASSERT_EQ(probe.end_count, 0);++ cbm_adr_t adr;+ memset(&adr, 0, sizeof(adr));+ ASSERT_EQ(cbm_store_adr_get(st, project, &adr), CBM_STORE_OK);+ ASSERT_STR_EQ(adr.content, "## PURPOSE\nUntouched.\n");+ cbm_store_adr_free(&adr);++ cbm_mcp_server_free(srv);+ PASS();+}++/* An empty body would render a heading with nothing under it — a content+ * deletion wearing the response shape of an update. A non-canonical name would+ * be parsed back as body text of the section above it, so writing it twice+ * would duplicate it: both are rejected before any store is opened. */+TEST(tool_manage_adr_set_sections_rejects_empty_and_unknown_sections) {+ const char *project = "adr-sec-guards";+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ ASSERT_EQ(cbm_store_upsert_project(st, project, "/tmp/adr-sec-guards"), CBM_STORE_OK);+ cbm_mcp_server_set_project(srv, project);+ ASSERT_EQ(cbm_store_adr_store(st, project, "## PURPOSE\nUntouched.\n"), CBM_STORE_OK);++ char *resp = cbm_mcp_handle_tool(srv, "manage_adr",+ "{\"project\":\"adr-sec-guards\",\"mode\":\"set_sections\","+ "\"section_updates\":{\"PURPOSE\":\"\"}}");+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "empty_section_content"));+ ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));+ free(resp);++ resp = cbm_mcp_handle_tool(srv, "manage_adr",+ "{\"project\":\"adr-sec-guards\",\"mode\":\"set_sections\","+ "\"section_updates\":{\"DECISIONS\":\"- Chose SQLite.\"}}");+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "invalid_section_name"));+ ASSERT_NOT_NULL(strstr(resp, "DECISIONS"));+ ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));+ free(resp);++ resp = cbm_mcp_handle_tool(srv, "manage_adr",+ "{\"project\":\"adr-sec-guards\",\"mode\":\"set_sections\","+ "\"section_updates\":{}}");+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "invalid_section_updates"));+ ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));+ free(resp);++ /* Every rejection left the stored ADR byte-identical. */+ cbm_adr_t adr;+ memset(&adr, 0, sizeof(adr));+ ASSERT_EQ(cbm_store_adr_get(st, project, &adr), CBM_STORE_OK);+ ASSERT_STR_EQ(adr.content, "## PURPOSE\nUntouched.\n");+ cbm_store_adr_free(&adr);++ cbm_mcp_server_free(srv);+ PASS();+}++/* CBM_ADR_MAX_LENGTH is enforced on this path — mode='update' bypasses it, so+ * exposing an incremental writer without the cap would make unbounded growth+ * cheap. The rejected merge must also roll back to the byte-identical prior+ * document rather than leaving a half-applied write. */+TEST(tool_manage_adr_set_sections_rejects_oversize) {+ const char *project = "adr-sec-cap";+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ ASSERT_EQ(cbm_store_upsert_project(st, project, "/tmp/adr-sec-cap"), CBM_STORE_OK);+ cbm_mcp_server_set_project(srv, project);+ ASSERT_EQ(cbm_store_adr_store(st, project, "## PURPOSE\nSmall.\n"), CBM_STORE_OK);++ size_t huge_len = (size_t)CBM_ADR_MAX_LENGTH + 100;+ char *huge = malloc(huge_len + 1);+ ASSERT_NOT_NULL(huge);+ memset(huge, 'x', huge_len);+ huge[huge_len] = '\0';++ size_t args_len = huge_len + 256;+ char *args = malloc(args_len);+ ASSERT_NOT_NULL(args);+ snprintf(args, args_len,+ "{\"project\":\"adr-sec-cap\",\"mode\":\"set_sections\","+ "\"section_updates\":{\"STACK\":\"%s\"}}",+ huge);++ char *resp = cbm_mcp_handle_tool(srv, "manage_adr", args);+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "write_error"));+ ASSERT_NOT_NULL(strstr(resp, "exceeds"));+ ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));+ free(resp);+ free(args);+ free(huge);++ cbm_adr_t adr;+ memset(&adr, 0, sizeof(adr));+ ASSERT_EQ(cbm_store_adr_get(st, project, &adr), CBM_STORE_OK);+ ASSERT_STR_EQ(adr.content, "## PURPOSE\nSmall.\n");+ cbm_store_adr_free(&adr);++ cbm_mcp_server_free(srv);+ PASS();+}++/* The mode must be advertised, or callers never learn it exists and keep+ * paying for whole-document rewrites. */+TEST(tool_manage_adr_set_sections_is_advertised) {+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ char *resp = cbm_mcp_server_handle(+ srv, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\",\"params\":{}}");+ ASSERT_NOT_NULL(resp);+ const char *adr_tool = strstr(resp, "manage_adr");+ ASSERT_NOT_NULL(adr_tool);+ ASSERT_NOT_NULL(strstr(adr_tool, "set_sections"));+ ASSERT_NOT_NULL(strstr(adr_tool, "section_updates"));+ free(resp);+ cbm_mcp_server_free(srv);+ PASS();+}+TEST(tool_manage_adr_mutation_guard_balances_success) {const char *project = "guard-adr-success";cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);@@ -13163,6 +13442,13 @@ SUITE(mcp) {RUN_TEST(tool_manage_adr_get_with_existing_adr);RUN_TEST(tool_manage_adr_unified_backend_issue256);RUN_TEST(tool_manage_adr_rejects_removed_sections_argument);+ RUN_TEST(tool_manage_adr_set_sections_replaces_only_named);+ RUN_TEST(tool_manage_adr_set_sections_is_idempotent);+ RUN_TEST(tool_manage_adr_set_sections_creates_when_absent);+ RUN_TEST(tool_manage_adr_set_sections_without_updates_errors);+ RUN_TEST(tool_manage_adr_set_sections_rejects_empty_and_unknown_sections);+ RUN_TEST(tool_manage_adr_set_sections_rejects_oversize);+ RUN_TEST(tool_manage_adr_set_sections_is_advertised);RUN_TEST(tool_index_repository_reports_store_backed_adr);RUN_TEST(tool_index_repository_resolves_root_path_from_project_name_issue1211);RUN_TEST(tool_index_repository_unknown_project_name_still_requires_repo_path);