@@ -1672,16 +1672,45 @@ static size_t cbm_json_mcp_ownership_fields(cbm_json_mcp_schema_t schema, const
16721672 * populated from config content. */
16731673static CBM_TLS const char *g_previous_managed_mcp_command = NULL;
16741674
1675+ /* Path-shape-insensitive equality for OUR OWN binary path (#1582): clients
1676+ * and installers spell the same Windows file with different separators (the
1677+ * entry stores `C:\...\cbm.exe`, the installer compares `C:/.../cbm.exe`),
1678+ * and Windows filesystems are case-insensitive. Separators always compare
1679+ * equal; case only folds on Windows. POSIX byte-exactness otherwise holds. */
1680+ static bool cbm_json_mcp_paths_equal(const char *a, const char *b) {
1681+ while (*a && *b) {
1682+ char ca = *a;
1683+ char cb = *b;
1684+ if (ca == '\\') {
1685+ ca = '/';
1686+ }
1687+ if (cb == '\\') {
1688+ cb = '/';
1689+ }
1690+ #ifdef _WIN32
1691+ ca = (char)tolower((unsigned char)ca);
1692+ cb = (char)tolower((unsigned char)cb);
1693+ #endif
1694+ if (ca != cb) {
1695+ return false;
1696+ }
1697+ a++;
1698+ b++;
1699+ }
1700+ return *a == *b;
1701+ }
1702+
16751703static bool cbm_json_mcp_owned_command(const char *command, const char *expected_binary,
16761704 const char *previous_managed_binary) {
16771705 if (!command || command[0] == '\0') {
16781706 return false;
16791707 }
1680- if (expected_binary && expected_binary[0] && strcmp(command, expected_binary) == 0) {
1708+ if (expected_binary && expected_binary[0] &&
1709+ cbm_json_mcp_paths_equal(command, expected_binary)) {
16811710 return true;
16821711 }
16831712 if (previous_managed_binary && previous_managed_binary[0] &&
1684- strcmp (command, previous_managed_binary) == 0 ) {
1713+ cbm_json_mcp_paths_equal (command, previous_managed_binary)) {
16851714 return true;
16861715 }
16871716 return strcmp(command, "codebase-memory-mcp") == 0 ||
@@ -1694,7 +1723,12 @@ static bool cbm_json_mcp_owned_command(const char *command, const char *expected
16941723 * missing. Upsert repairs it; remove leaves it alone like any other non-owned
16951724 * entry. POSIX never probes config-supplied paths: only the running executable
16961725 * identity above can authorize a moved-install refresh. */
1697- enum { CBM_JSON_MCP_OWNERSHIP_STALE = 100 };
1726+ enum {
1727+ CBM_JSON_MCP_OWNERSHIP_STALE = 100,
1728+ /* Repairable like STALE, but the entry carries client-added keys: only a
1729+ * FIELD-level repair may touch it — full replacement would drop them. */
1730+ CBM_JSON_MCP_OWNERSHIP_EXTRAS_STALE = 101,
1731+ };
16981732
16991733#ifdef _WIN32
17001734typedef enum {
@@ -1973,7 +2007,8 @@ static int cbm_json_mcp_snapshot_ownership(const char *document, size_t document
19732007 const char *const *object_path, size_t path_len,
19742008 cbm_json_mcp_schema_t schema, const char *entry_name,
19752009 const char *argument, const char *expected_binary,
1976- const char *previous_managed_binary) {
2010+ const char *previous_managed_binary,
2011+ char **command_out) {
19772012 cbm_json_like_object_field_t fields[3];
19782013 size_t field_count = cbm_json_mcp_ownership_fields(schema, argument, fields);
19792014 char *command = NULL;
@@ -1983,17 +2018,47 @@ static int cbm_json_mcp_snapshot_ownership(const char *document, size_t document
19832018 result == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS) &&
19842019 !cbm_json_mcp_owned_command(command, expected_binary, previous_managed_binary)) {
19852020#ifdef _WIN32
1986- result = cbm_json_mcp_command_availability(command) == CBM_JSON_MCP_COMMAND_MISSING
1987- ? CBM_JSON_MCP_OWNERSHIP_STALE
1988- : CBM_JSON_LIKE_OBJECT_MISMATCH;
2021+ bool had_extras = result == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS;
2022+ result =
2023+ cbm_json_mcp_command_availability(command) == CBM_JSON_MCP_COMMAND_MISSING
2024+ ? (had_extras ? CBM_JSON_MCP_OWNERSHIP_EXTRAS_STALE : CBM_JSON_MCP_OWNERSHIP_STALE)
2025+ : CBM_JSON_LIKE_OBJECT_MISMATCH;
19892026#else
19902027 result = CBM_JSON_LIKE_OBJECT_MISMATCH;
19912028#endif
19922029 }
1993- free(command);
2030+ if (command_out) {
2031+ *command_out = command;
2032+ } else {
2033+ free(command);
2034+ }
19942035 return result;
19952036}
19962037
2038+ /* Render just the `command` member's VALUE for a schema — the raw JSON the
2039+ * field-level repair splices in place of a moved install's stale path. */
2040+ static char *cbm_json_mcp_render_command_value(const char *binary_path,
2041+ cbm_json_mcp_schema_t schema) {
2042+ yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
2043+ if (!doc) {
2044+ return NULL;
2045+ }
2046+ yyjson_mut_val *command = cbm_json_mcp_command_is_array(schema)
2047+ ? yyjson_mut_arr(doc)
2048+ : yyjson_mut_strcpy(doc, binary_path);
2049+ bool ok = command != NULL;
2050+ if (ok && cbm_json_mcp_command_is_array(schema)) {
2051+ ok = yyjson_mut_arr_add_strcpy(doc, command, binary_path);
2052+ }
2053+ char *json = NULL;
2054+ if (ok) {
2055+ yyjson_mut_doc_set_root(doc, command);
2056+ json = yyjson_mut_write(doc, YYJSON_WRITE_NOFLAG, NULL);
2057+ }
2058+ yyjson_mut_doc_free(doc);
2059+ return json;
2060+ }
2061+
19972062static int cbm_upsert_json_named_mcp(const char *binary_path, const char *config_path,
19982063 const char *const *object_path, size_t path_len,
19992064 cbm_json_mcp_schema_t schema, const char *entry_name,
@@ -2008,9 +2073,10 @@ static int cbm_upsert_json_named_mcp(const char *binary_path, const char *config
20082073 return CLI_ERR;
20092074 }
20102075 if (read_result == 0) {
2076+ char *command = NULL;
20112077 int ownership = cbm_json_mcp_snapshot_ownership(
20122078 document, document_length, object_path, path_len, schema, entry_name, argument,
2013- binary_path, g_previous_managed_mcp_command);
2079+ binary_path, g_previous_managed_mcp_command, &command );
20142080 /* An entry that already says what we would say, but carries extra keys
20152081 * the client added, is ALREADY SATISFIED. Return success without
20162082 * touching the file.
@@ -2028,11 +2094,55 @@ static int cbm_upsert_json_named_mcp(const char *binary_path, const char *config
20282094 * while preserving the rest is the fuller fix and is tracked there;
20292095 * this makes the common case work without risking anyone's config. */
20302096 if (ownership == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS) {
2097+ /* Owned via the PREVIOUS managed binary during a relocating
2098+ * update: the annotated entry still names the old location, and a
2099+ * wholesale rewrite would drop the client's keys — repair only the
2100+ * command member (#1630's deferred field-merge). An entry already
2101+ * naming the current binary needs nothing. */
2102+ bool via_previous = command && g_previous_managed_mcp_command &&
2103+ strcmp(command, g_previous_managed_mcp_command) == 0 &&
2104+ strcmp(command, binary_path) != 0;
2105+ if (!via_previous) {
2106+ free(command);
2107+ free(document);
2108+ return CLI_OK;
2109+ }
2110+ char *value = cbm_json_mcp_render_command_value(binary_path, schema);
2111+ if (!value) {
2112+ free(command);
2113+ free(document);
2114+ return CLI_ERR;
2115+ }
2116+ int repair = cbm_json_like_replace_field_raw_if_unchanged(
2117+ config_path, object_path, path_len, entry_name, "command", value, document,
2118+ document_length);
2119+ free(value);
2120+ free(command);
20312121 free(document);
2032- return CLI_OK;
2122+ return repair == 0 ? CLI_OK : CLI_ERR;
2123+ }
2124+ /* Windows only: our shape, annotated, and the named binary
2125+ * conclusively missing from the local fixed drives — repair ONLY the
2126+ * command member so the client's keys survive (#1630 field-merge). */
2127+ if (ownership == CBM_JSON_MCP_OWNERSHIP_EXTRAS_STALE) {
2128+ char *value = cbm_json_mcp_render_command_value(binary_path, schema);
2129+ if (!value) {
2130+ free(command);
2131+ free(document);
2132+ return CLI_ERR;
2133+ }
2134+ int repair = cbm_json_like_replace_field_raw_if_unchanged(
2135+ config_path, object_path, path_len, entry_name, "command", value, document,
2136+ document_length);
2137+ free(value);
2138+ free(command);
2139+ free(document);
2140+ return repair == 0 ? CLI_OK : CLI_ERR;
20332141 }
2034- /* STALE (our exact shape, dead binary path) is repairable — that is
2035- * the update contract. Only a genuinely foreign shape refuses. */
2142+ /* STALE (our exact shape, dead binary path on Windows) is repairable
2143+ * — that is the update contract. Only a genuinely foreign shape
2144+ * refuses. */
2145+ free(command);
20362146 if (ownership != CBM_JSON_LIKE_OBJECT_MATCH && ownership != CBM_JSON_LIKE_OBJECT_MISSING &&
20372147 ownership != CBM_JSON_MCP_OWNERSHIP_STALE) {
20382148 free(document);
@@ -2080,7 +2190,7 @@ static int cbm_remove_json_named_mcp(const char *config_path, const char *const
20802190 }
20812191 int ownership =
20822192 cbm_json_mcp_snapshot_ownership(document, document_length, object_path, path_len, schema,
2083- entry_name, argument, expected_binary, NULL);
2193+ entry_name, argument, expected_binary, NULL, NULL );
20842194 if (ownership == CBM_JSON_LIKE_OBJECT_MISSING || ownership == CBM_JSON_LIKE_OBJECT_MISMATCH ||
20852195 ownership == CBM_JSON_MCP_OWNERSHIP_STALE) {
20862196 free(document);
@@ -3342,14 +3452,21 @@ int cbm_upsert_codex_mcp(const char *binary_path, const char *config_path) {
33423452 * handshake closes during initialization, so Codex exposes no cbm tools at
33433453 * all.
33443454 *
3345- * The name is listed unconditionally rather than only when the variable is
3455+ * The names are listed unconditionally rather than only when a variable is
33463456 * set at install time: env_vars names variables to FORWARD IF PRESENT, so
3347- * listing it costs nothing when unset and keeps working for someone who
3348- * sets it after installing — which install-time detection would silently
3349- * fail to cover. */
3457+ * listing them costs nothing when unset and keeps working for someone who
3458+ * sets one after installing — which install-time detection would silently
3459+ * fail to cover.
3460+ *
3461+ * CBM_RUNTIME_DIR joined the list with #1664: since #1645 it relocates
3462+ * the daemon rendezvous, so a Codex subprocess that does not receive it
3463+ * looks for the daemon in the DEFAULT location and never finds it — the
3464+ * same silent client/daemon split CBM_CACHE_DIR caused. Both names decide
3465+ * WHICH daemon a process talks to; behavioural knobs stay unforwarded. */
33503466 int written = snprintf(block, sizeof(block),
33513467 CODEX_CMM_SECTION "\ncommand = \"%s\"\nargs = []\n"
3352- "env_vars = [\"CBM_CACHE_DIR\"]\n",
3468+ "env_vars = [\"CBM_CACHE_DIR\", "
3469+ "\"CBM_RUNTIME_DIR\"]\n",
33533470 escaped);
33543471 if (written < 0 || (size_t)written >= sizeof(block) ||
33553472 cbm_remove_codex_legacy_mcp(config_path) != 0) {
@@ -3716,7 +3833,11 @@ static int cbm_build_yaml_stdio_mcp_block(const char *binary_path, bool goose_sc
37163833 free(encoded);
37173834 return CLI_ERR;
37183835 }
3836+ /* goose's ExtensionConfig::Stdio requires `name` (serde, no default) and
3837+ * its loader silently drops entries that fail to deserialize (#1675) — an
3838+ * entry without it installs cleanly and is then invisible in goose. */
37193839 int written = goose_schema ? snprintf(block, block_size,
3840+ " name: codebase-memory-mcp\n"
37203841 " type: stdio\n"
37213842 " cmd: %s\n"
37223843 " args: []\n"
@@ -3727,6 +3848,19 @@ static int cbm_build_yaml_stdio_mcp_block(const char *binary_path, bool goose_sc
37273848 return written > 0 && (size_t)written < block_size ? CLI_OK : CLI_ERR;
37283849}
37293850
3851+ #ifdef CBM_CLI_ENABLE_TEST_API
3852+ /* Test seam for the agent-config block writers: the exact bytes written into a
3853+ * coding agent's config file are a compatibility contract with THAT agent's
3854+ * parser (goose deserializes with serde and silently drops entries that fail —
3855+ * #1675), so tests must be able to assert the block verbatim. */
3856+ int cbm_cli_build_yaml_stdio_mcp_block_for_test(const char *binary_path, bool goose_schema,
3857+ char *block, size_t block_size) {
3858+ return cbm_build_yaml_stdio_mcp_block(binary_path, goose_schema, block, block_size) == CLI_OK
3859+ ? 0
3860+ : -1;
3861+ }
3862+ #endif
3863+
37303864static int cbm_upsert_yaml_stdio_mcp(const char *binary_path, const char *config_path,
37313865 const char *section_key, bool goose_schema) {
37323866 char block[CLI_BUF_8K];
@@ -3809,7 +3943,7 @@ static int cbm_junie_mcp_preflight(const char *binary_path, const char *config_p
38093943 for (size_t i = 0U; i < sizeof(entries) / sizeof(entries[0]); i++) {
38103944 int ownership = cbm_json_mcp_snapshot_ownership(
38113945 document, document_length, path, 1U, CBM_JSON_MCP_STANDARD, entries[i].name,
3812- entries[i].argument, binary_path, g_previous_managed_mcp_command);
3946+ entries[i].argument, binary_path, g_previous_managed_mcp_command, NULL );
38133947 if (ownership != CBM_JSON_LIKE_OBJECT_MATCH && ownership != CBM_JSON_LIKE_OBJECT_MISSING &&
38143948 ownership != CBM_JSON_MCP_OWNERSHIP_STALE) {
38153949 result = CLI_ERR;
0 commit comments