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
fix(cli): install-entry compatibility repairs for goose and annotated MCP entries
Martin Vogel33d1ecea1bbf
diff --git a/src/cli/cli.c b/src/cli/cli.cindex 63e73c686..7d1c89f7d 100644--- a/src/cli/cli.c+++ b/src/cli/cli.c@@ -1694,7 +1694,12 @@ static bool cbm_json_mcp_owned_command(const char *command, const char *expected* missing. Upsert repairs it; remove leaves it alone like any other non-owned* entry. POSIX never probes config-supplied paths: only the running executable* identity above can authorize a moved-install refresh. */-enum { CBM_JSON_MCP_OWNERSHIP_STALE = 100 };+enum {+ CBM_JSON_MCP_OWNERSHIP_STALE = 100,+ /* Repairable like STALE, but the entry carries client-added keys: only a+ * FIELD-level repair may touch it — full replacement would drop them. */+ CBM_JSON_MCP_OWNERSHIP_EXTRAS_STALE = 101,+};#ifdef _WIN32typedef enum {@@ -1973,7 +1978,8 @@ static int cbm_json_mcp_snapshot_ownership(const char *document, size_t documentconst char *const *object_path, size_t path_len,cbm_json_mcp_schema_t schema, const char *entry_name,const char *argument, const char *expected_binary,- const char *previous_managed_binary) {+ const char *previous_managed_binary,+ char **command_out) {cbm_json_like_object_field_t fields[3];size_t field_count = cbm_json_mcp_ownership_fields(schema, argument, fields);char *command = NULL;@@ -1983,17 +1989,47 @@ static int cbm_json_mcp_snapshot_ownership(const char *document, size_t documentresult == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS) &&!cbm_json_mcp_owned_command(command, expected_binary, previous_managed_binary)) {#ifdef _WIN32- result = cbm_json_mcp_command_availability(command) == CBM_JSON_MCP_COMMAND_MISSING- ? CBM_JSON_MCP_OWNERSHIP_STALE- : CBM_JSON_LIKE_OBJECT_MISMATCH;+ bool had_extras = result == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS;+ result =+ cbm_json_mcp_command_availability(command) == CBM_JSON_MCP_COMMAND_MISSING+ ? (had_extras ? CBM_JSON_MCP_OWNERSHIP_EXTRAS_STALE : CBM_JSON_MCP_OWNERSHIP_STALE)+ : CBM_JSON_LIKE_OBJECT_MISMATCH;#elseresult = CBM_JSON_LIKE_OBJECT_MISMATCH;#endif}- free(command);+ if (command_out) {+ *command_out = command;+ } else {+ free(command);+ }return result;}+/* Render just the `command` member's VALUE for a schema — the raw JSON the+ * field-level repair splices in place of a moved install's stale path. */+static char *cbm_json_mcp_render_command_value(const char *binary_path,+ cbm_json_mcp_schema_t schema) {+ yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);+ if (!doc) {+ return NULL;+ }+ yyjson_mut_val *command = cbm_json_mcp_command_is_array(schema)+ ? yyjson_mut_arr(doc)+ : yyjson_mut_strcpy(doc, binary_path);+ bool ok = command != NULL;+ if (ok && cbm_json_mcp_command_is_array(schema)) {+ ok = yyjson_mut_arr_add_strcpy(doc, command, binary_path);+ }+ char *json = NULL;+ if (ok) {+ yyjson_mut_doc_set_root(doc, command);+ json = yyjson_mut_write(doc, YYJSON_WRITE_NOFLAG, NULL);+ }+ yyjson_mut_doc_free(doc);+ return json;+}+static int cbm_upsert_json_named_mcp(const char *binary_path, const char *config_path,const char *const *object_path, size_t path_len,cbm_json_mcp_schema_t schema, const char *entry_name,@@ -2008,9 +2044,10 @@ static int cbm_upsert_json_named_mcp(const char *binary_path, const char *configreturn CLI_ERR;}if (read_result == 0) {+ char *command = NULL;int ownership = cbm_json_mcp_snapshot_ownership(document, document_length, object_path, path_len, schema, entry_name, argument,- binary_path, g_previous_managed_mcp_command);+ binary_path, g_previous_managed_mcp_command, &command);/* An entry that already says what we would say, but carries extra keys* the client added, is ALREADY SATISFIED. Return success without* touching the file.@@ -2028,11 +2065,55 @@ static int cbm_upsert_json_named_mcp(const char *binary_path, const char *config* while preserving the rest is the fuller fix and is tracked there;* this makes the common case work without risking anyone's config. */if (ownership == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS) {+ /* Owned via the PREVIOUS managed binary during a relocating+ * update: the annotated entry still names the old location, and a+ * wholesale rewrite would drop the client's keys — repair only the+ * command member (#1630's deferred field-merge). An entry already+ * naming the current binary needs nothing. */+ bool via_previous = command && g_previous_managed_mcp_command &&+ strcmp(command, g_previous_managed_mcp_command) == 0 &&+ strcmp(command, binary_path) != 0;+ if (!via_previous) {+ free(command);+ free(document);+ return CLI_OK;+ }+ char *value = cbm_json_mcp_render_command_value(binary_path, schema);+ if (!value) {+ free(command);+ free(document);+ return CLI_ERR;+ }+ int repair = cbm_json_like_replace_field_raw_if_unchanged(+ config_path, object_path, path_len, entry_name, "command", value, document,+ document_length);+ free(value);+ free(command);free(document);- return CLI_OK;+ return repair == 0 ? CLI_OK : CLI_ERR;+ }+ /* Windows only: our shape, annotated, and the named binary+ * conclusively missing from the local fixed drives — repair ONLY the+ * command member so the client's keys survive (#1630 field-merge). */+ if (ownership == CBM_JSON_MCP_OWNERSHIP_EXTRAS_STALE) {+ char *value = cbm_json_mcp_render_command_value(binary_path, schema);+ if (!value) {+ free(command);+ free(document);+ return CLI_ERR;+ }+ int repair = cbm_json_like_replace_field_raw_if_unchanged(+ config_path, object_path, path_len, entry_name, "command", value, document,+ document_length);+ free(value);+ free(command);+ free(document);+ return repair == 0 ? CLI_OK : CLI_ERR;}- /* STALE (our exact shape, dead binary path) is repairable — that is- * the update contract. Only a genuinely foreign shape refuses. */+ /* STALE (our exact shape, dead binary path on Windows) is repairable+ * — that is the update contract. Only a genuinely foreign shape+ * refuses. */+ free(command);if (ownership != CBM_JSON_LIKE_OBJECT_MATCH && ownership != CBM_JSON_LIKE_OBJECT_MISSING &&ownership != CBM_JSON_MCP_OWNERSHIP_STALE) {free(document);@@ -2080,7 +2161,7 @@ static int cbm_remove_json_named_mcp(const char *config_path, const char *const}int ownership =cbm_json_mcp_snapshot_ownership(document, document_length, object_path, path_len, schema,- entry_name, argument, expected_binary, NULL);+ entry_name, argument, expected_binary, NULL, NULL);if (ownership == CBM_JSON_LIKE_OBJECT_MISSING || ownership == CBM_JSON_LIKE_OBJECT_MISMATCH ||ownership == CBM_JSON_MCP_OWNERSHIP_STALE) {free(document);@@ -3716,7 +3797,11 @@ static int cbm_build_yaml_stdio_mcp_block(const char *binary_path, bool goose_scfree(encoded);return CLI_ERR;}+ /* goose's ExtensionConfig::Stdio requires `name` (serde, no default) and+ * its loader silently drops entries that fail to deserialize (#1675) — an+ * entry without it installs cleanly and is then invisible in goose. */int written = goose_schema ? snprintf(block, block_size,+ " name: codebase-memory-mcp\n"" type: stdio\n"" cmd: %s\n"" args: []\n"@@ -3727,6 +3812,19 @@ static int cbm_build_yaml_stdio_mcp_block(const char *binary_path, bool goose_screturn written > 0 && (size_t)written < block_size ? CLI_OK : CLI_ERR;}+#ifdef CBM_CLI_ENABLE_TEST_API+/* Test seam for the agent-config block writers: the exact bytes written into a+ * coding agent's config file are a compatibility contract with THAT agent's+ * parser (goose deserializes with serde and silently drops entries that fail —+ * #1675), so tests must be able to assert the block verbatim. */+int cbm_cli_build_yaml_stdio_mcp_block_for_test(const char *binary_path, bool goose_schema,+ char *block, size_t block_size) {+ return cbm_build_yaml_stdio_mcp_block(binary_path, goose_schema, block, block_size) == CLI_OK+ ? 0+ : -1;+}+#endif+static int cbm_upsert_yaml_stdio_mcp(const char *binary_path, const char *config_path,const char *section_key, bool goose_schema) {char block[CLI_BUF_8K];@@ -3809,7 +3907,7 @@ static int cbm_junie_mcp_preflight(const char *binary_path, const char *config_pfor (size_t i = 0U; i < sizeof(entries) / sizeof(entries[0]); i++) {int ownership = cbm_json_mcp_snapshot_ownership(document, document_length, path, 1U, CBM_JSON_MCP_STANDARD, entries[i].name,- entries[i].argument, binary_path, g_previous_managed_mcp_command);+ entries[i].argument, binary_path, g_previous_managed_mcp_command, NULL);if (ownership != CBM_JSON_LIKE_OBJECT_MATCH && ownership != CBM_JSON_LIKE_OBJECT_MISSING &&ownership != CBM_JSON_MCP_OWNERSHIP_STALE) {result = CLI_ERR;diff --git a/src/cli/config_json_like.c b/src/cli/config_json_like.cindex 4d0ecc8d8..dc16c3c5c 100644--- a/src/cli/config_json_like.c+++ b/src/cli/config_json_like.c@@ -2213,6 +2213,86 @@ int cbm_json_like_upsert_entry_if_unchanged(const char *file_path, const char *cexpected_content, expected_length);}+int cbm_json_like_replace_field_raw_if_unchanged(const char *file_path,+ const char *const *object_path, size_t path_len,+ const char *entry_key, const char *field_key,+ const char *raw_value,+ const char *expected_content,+ size_t expected_length) {+ if (jl_validate_arguments(file_path, object_path, path_len, entry_key) != 0 || !field_key ||+ field_key[0] == '\0' || !raw_value) {+ return -1;+ }+ size_t value_offset = 0U;+ size_t value_length = 0U;+ if (jl_validate_entry(raw_value, &value_offset, &value_length) != 0) {+ return -1;+ }++ char *source = NULL;+ size_t source_length = 0;+ bool missing = false;+ jl_file_snapshot_t snapshot;+ if (jl_read_file(file_path, &source, &source_length, &missing, &snapshot) != 0) {+ return -1;+ }+ bool content_matches =+ expected_content+ ? !missing && source_length == expected_length &&+ (expected_length == 0U || memcmp(source, expected_content, expected_length) == 0)+ : missing;+ if (!content_matches || missing || source_length == 0U) {+ free(source);+ return -1;+ }++ size_t object_start = 0;+ if (jl_validate_document(source, source_length, &object_start) != 0) {+ free(source);+ return -1;+ }+ for (size_t i = 0; i < path_len; i++) {+ jl_object_t parent;+ if (jl_scan_object(source, source_length, object_start, object_path[i], &parent) != 0 ||+ parent.match_count != 1U || source[parent.match.value_start] != '{') {+ free(source);+ return -1;+ }+ object_start = parent.match.value_start;+ }+ jl_object_t entry;+ if (jl_scan_object(source, source_length, object_start, entry_key, &entry) != 0 ||+ entry.match_count != 1U || source[entry.match.value_start] != '{') {+ free(source);+ return -1;+ }+ jl_object_t field;+ if (jl_scan_object(source, source_length, entry.match.value_start, field_key, &field) != 0 ||+ field.match_count != 1U) {+ free(source);+ return -1;+ }++ size_t head = field.match.value_start;+ size_t tail = field.match.value_end;+ size_t updated_length = head + value_length + (source_length - tail);+ char *updated = (char *)malloc(updated_length + 1U);+ if (!updated) {+ free(source);+ return -1;+ }+ memcpy(updated, source, head);+ memcpy(updated + head, raw_value + value_offset, value_length);+ memcpy(updated + head + value_length, source + tail, source_length - tail);+ updated[updated_length] = '\0';++ int result =+ jl_write_document(file_path, updated, updated_length, source, source_length, &snapshot);+ free(updated);+ free(source);+ return result;+}+static int jl_remove_entry(const char *file_path, const char *const *object_path, size_t path_len,const char *entry_key, bool enforce_expected,const char *expected_content, size_t expected_length) {diff --git a/src/cli/config_json_like.h b/src/cli/config_json_like.hindex 905721d67..23a8687e5 100644--- a/src/cli/config_json_like.h+++ b/src/cli/config_json_like.h@@ -89,6 +89,19 @@ int cbm_json_like_upsert_entry_if_unchanged(const char *file_path, const char *cconst char *entry_json, const char *expected_content,size_t expected_length);+/* Replace ONE member's value inside the entry object at object_path/entry_key,+ * preserving every other byte of the document (comments, ordering, and any+ * keys the client added around it). The field-merge primitive for repairing an+ * annotated entry (#1630): replacing the whole entry would drop the client's+ * keys. raw_value must be a single complete JSON value; the field must exist+ * exactly once. Same expected-content contract as the _if_unchanged editors. */+int cbm_json_like_replace_field_raw_if_unchanged(const char *file_path,+ const char *const *object_path, size_t path_len,+ const char *entry_key, const char *field_key,+ const char *raw_value,+ const char *expected_content,+ size_t expected_length);+/* Remove entry_key from the object at object_path. A missing path or entry is* a successful no-op. Returns 0 on success and -1 on invalid input or I/O. */int cbm_json_like_remove_entry(const char *file_path, const char *const *object_path,diff --git a/tests/test_cli.c b/tests/test_cli.cindex d1524fe47..634cee89e 100644--- a/tests/test_cli.c+++ b/tests/test_cli.c@@ -52,6 +52,8 @@ int cbm_cli_checksum_manifest_digest(const char *manifest_path, const char *archvoid cbm_cli_set_activation_cleanup_failure_for_test(bool enabled);int cbm_cli_activation_abort_cleanup_probe_for_test(void);bool cbm_cli_activation_test_ops_installed(void);+int cbm_cli_build_yaml_stdio_mcp_block_for_test(const char *binary_path, bool goose_schema,+ char *block, size_t block_size);TEST(cli_progress_visibility_policy) {ASSERT_TRUE(cbm_cli_progress_enabled(true, false));@@ -2938,6 +2940,94 @@ TEST(cli_junie_mcp_repairs_all_known_previous_aliases_atomically) {PASS();}+TEST(cli_goose_block_carries_required_name_issue1675) {+ /* goose's ExtensionConfig::Stdio declares `name` as a required serde field+ * with no default, and its loader silently drops entries that fail to+ * deserialize — an entry without `name:` installs "successfully" and is+ * then invisible in goose. The block is the compatibility contract. */+ char block[512];+ ASSERT_EQ(cbm_cli_build_yaml_stdio_mcp_block_for_test("/opt/codebase-memory-mcp", true, block,+ sizeof(block)),+ 0);+ ASSERT(strstr(block, "name: codebase-memory-mcp\n") != NULL);+ ASSERT(strstr(block, "type: stdio\n") != NULL);+ ASSERT(strstr(block, "enabled: true\n") != NULL);++ /* The non-goose YAML schema (command-only) must stay name-free. */+ ASSERT_EQ(cbm_cli_build_yaml_stdio_mcp_block_for_test("/opt/codebase-memory-mcp", false, block,+ sizeof(block)),+ 0);+ ASSERT(strstr(block, "name:") == NULL);+ PASS();+}++TEST(cli_editor_mcp_field_repairs_annotated_entry_via_previous_issue1630) {+ /* The relocating-update flow is the AUTHORIZED repair channel: the entry+ * still names the previous managed binary and the client annotated it, so+ * a wholesale rewrite would drop those keys. Only the command member may+ * change; comments and client keys survive byte-for-byte. */+ char tmpdir[256];+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-oc-XXXXXX");+ if (!cbm_mkdtemp(tmpdir))+ FAIL("cbm_mkdtemp failed");+ char configpath[512];+ snprintf(configpath, sizeof(configpath), "%s/.claude.json", tmpdir);+ write_test_file(configpath, "{\n"+ " // user config\n"+ " \"mcpServers\": {\n"+ " \"codebase-memory-mcp\": {\n"+ " \"command\": \"/old/place/codebase-memory-mcp\",\n"+ " \"enabled\": true,\n"+ " \"timeout\": 5\n"+ " },\n"+ " },\n"+ "}\n");+ ASSERT_EQ(cbm_install_editor_mcp_with_previous_for_testing(+ "/opt/codebase-memory-mcp", "/old/place/codebase-memory-mcp", configpath),+ 0);+ const char *data = read_test_file(configpath);+ ASSERT_NOT_NULL(data);+ ASSERT(strstr(data, "\"command\": \"/opt/codebase-memory-mcp\"") != NULL);+ ASSERT(strstr(data, "/old/place/") == NULL);+ ASSERT(strstr(data, "\"enabled\": true") != NULL);+ ASSERT(strstr(data, "\"timeout\": 5") != NULL);+ ASSERT(strstr(data, "// user config") != NULL);+ test_rmdir_r(tmpdir);+ PASS();+}++TEST(cli_opencode_moved_entry_without_authority_refuses_issue1630) {+ /* POSIX never trusts a config-supplied path — with no previous-managed+ * identity and no dead-path proof, a moved-looking entry is preserved+ * byte-for-byte and install fails loudly for the user to inspect. */+ char tmpdir[256];+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-oc-XXXXXX");+ if (!cbm_mkdtemp(tmpdir))+ FAIL("cbm_mkdtemp failed");+ char configpath[512];+ snprintf(configpath, sizeof(configpath), "%s/opencode.jsonc", tmpdir);+ const char *initial = "{\n"+ " \"mcp\": {\n"+ " \"codebase-memory-mcp\": {\n"+ " \"command\": [\"/old/place/codebase-memory-mcp\"],\n"+ " \"type\": \"local\"\n"+ " }\n"+ " }\n"+ "}\n";+ write_test_file(configpath, initial);+#ifdef _WIN32+ /* On Windows the conclusive dead-path probe authorizes the repair. */+ ASSERT_EQ(cbm_upsert_opencode_mcp("/opt/codebase-memory-mcp", configpath), 0);+#else+ ASSERT(cbm_upsert_opencode_mcp("/opt/codebase-memory-mcp", configpath) != 0);+ const char *data = read_test_file(configpath);+ ASSERT_NOT_NULL(data);+ ASSERT(strstr(data, "/old/place/") != NULL);+#endif+ test_rmdir_r(tmpdir);+ PASS();+}+TEST(cli_gemini_mcp_install) {/* Port of TestGeminiMCPInstall */char tmpdir[256];@@ -12868,6 +12958,9 @@ SUITE(cli) {RUN_TEST(cli_editor_mcp_uninstall);RUN_TEST(cli_junie_mcp_install_issue651);RUN_TEST(cli_junie_mcp_repairs_all_known_previous_aliases_atomically);+ RUN_TEST(cli_goose_block_carries_required_name_issue1675);+ RUN_TEST(cli_editor_mcp_field_repairs_annotated_entry_via_previous_issue1630);+ RUN_TEST(cli_opencode_moved_entry_without_authority_refuses_issue1630);RUN_TEST(cli_gemini_mcp_install);RUN_TEST(cli_openclaw_mcp_install_uses_nested_servers);RUN_TEST(cli_openclaw_mcp_preserves_existing_config);