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(install): adopt the Codex MCP table after Codex rewrites it (#1720)
Martin Vogele81fd499f862
diff --git a/src/cli/config_toml_edit.c b/src/cli/config_toml_edit.cindex e14bcaffb..684071cfd 100644--- a/src/cli/config_toml_edit.c+++ b/src/cli/config_toml_edit.c@@ -2516,48 +2516,117 @@ static int toml_legacy_args_are_empty(const char *data, const toml_assignment_treturn pos == end;}-static int toml_legacy_schema_is_owned(int command_count, int command_owned, int args_count,- int args_empty) {- return command_count == 1 && command_owned && args_count <= 1 &&- (args_count == 0 || args_empty);+/* #1720: the only names cbm has ever listed in env_vars (#1562, #1664). */+static int toml_legacy_env_name_is_owned(const toml_string_t *name) {+ static const char *const owned[] = {"CBM_CACHE_DIR", "CBM_RUNTIME_DIR"};+ for (size_t i = 0U; i < sizeof(owned) / sizeof(owned[0]); ++i) {+ size_t owned_len = strlen(owned[i]);+ if (name->len == owned_len && memcmp(name->data, owned[i], owned_len) == 0) {+ return 1;+ }+ }+ return 0;}-int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,- const char *begin_marker, const char *end_marker) {- if (!toml_valid_path(file_path) || !table_name || !toml_valid_marker(begin_marker) ||- !toml_valid_marker(end_marker) || strcmp(begin_marker, end_marker) == 0) {- return TOML_EDIT_ERR;- }- toml_key_path_t desired;- if (toml_parse_key_path(table_name, 0U, strlen(table_name), &desired) != TOML_EDIT_OK) {- return TOML_EDIT_ERR;+static void toml_skip_blanks(const char *data, size_t end, size_t *pos) {+ while (*pos < end && (data[*pos] == ' ' || data[*pos] == '\t')) {+ ++*pos;}+}- char *existing = NULL;- size_t existing_len = 0U;- toml_file_snapshot_t snapshot;- if (toml_read_file(file_path, &existing, &existing_len, &snapshot) != TOML_EDIT_OK ||- !toml_text_is_safe(existing, existing_len, 1)) {- toml_key_path_dispose(&desired);- free(existing);- return TOML_EDIT_ERR;+/* A single-line array of strings naming only variables cbm forwards: the+ * value cbm writes, as Codex re-serializes it when it rewrites the table. */+static int toml_legacy_env_vars_are_owned(const char *data, const toml_assignment_t *assignment) {+ if (!assignment->present || assignment->multiline_value) {+ return 0;}-- toml_line_t begin_line = {0};- toml_line_t end_line = {0};- int has_managed_pair = 0;- if (toml_find_markers(existing, existing_len, begin_marker, end_marker, &begin_line, &end_line,- &has_managed_pair) != TOML_EDIT_OK) {- toml_key_path_dispose(&desired);- free(existing);- return TOML_EDIT_ERR;+ size_t pos = assignment->value_start;+ size_t end = assignment->value_end;+ if (pos >= end || data[pos++] != '[') {+ return 0;}- if (has_managed_pair) {- toml_key_path_dispose(&desired);- free(existing);- return TOML_EDIT_OK;+ for (;;) {+ toml_skip_blanks(data, end, &pos);+ if (pos < end && data[pos] == ']') {+ pos++;+ break;+ }+ toml_string_t name = {0};+ if (pos >= end || toml_parse_string(data + pos, end - pos, &name) != TOML_EDIT_OK) {+ return 0;+ }+ int owned = toml_legacy_env_name_is_owned(&name);+ pos += name.consumed;+ toml_string_dispose(&name);+ toml_skip_blanks(data, end, &pos);+ if (!owned || pos >= end || (data[pos] != ',' && data[pos] != ']')) {+ return 0;+ }+ if (data[pos] == ',') {+ pos++;+ }+ }+ toml_skip_blanks(data, end, &pos);+ return pos == end;+}++typedef struct {+ int command_count;+ int command_owned;+ int args_count;+ int args_empty;+ int env_vars_count;+ int env_vars_owned;+} toml_legacy_shape_t;++static int toml_legacy_schema_is_owned(const toml_legacy_shape_t *shape) {+ return shape->command_count == 1 && shape->command_owned && shape->args_count <= 1 &&+ (shape->args_count == 0 || shape->args_empty) && shape->env_vars_count <= 1 &&+ (shape->env_vars_count == 0 || shape->env_vars_owned);+}++/* Classify one assignment inside the target table. Returns TOML_EDIT_ERR for+ * a duplicated key or an unparsable command, otherwise TOML_EDIT_OK with+ * *foreign set when the assignment is not part of the owned schema. */+static int toml_legacy_classify_assignment(const char *data, const toml_assignment_t *assignment,+ toml_legacy_shape_t *shape, int *foreign) {+ if (assignment->key.count != 1U) {+ *foreign = 1;+ } else if (toml_key_path_is_single(&assignment->key, "command")) {+ if (++shape->command_count > 1 ||+ toml_legacy_command_is_owned(data, assignment, &shape->command_owned) != TOML_EDIT_OK) {+ return TOML_EDIT_ERR;+ }+ if (!shape->command_owned) {+ *foreign = 1;+ }+ } else if (toml_key_path_is_single(&assignment->key, "args")) {+ shape->args_empty = toml_legacy_args_are_empty(data, assignment);+ if (++shape->args_count > 1) {+ return TOML_EDIT_ERR;+ }+ if (!shape->args_empty) {+ *foreign = 1;+ }+ } else if (toml_key_path_is_single(&assignment->key, "env_vars")) {+ shape->env_vars_owned = toml_legacy_env_vars_are_owned(data, assignment);+ if (++shape->env_vars_count > 1) {+ return TOML_EDIT_ERR;+ }+ if (!shape->env_vars_owned) {+ *foreign = 1;+ }+ } else {+ *foreign = 1;}+ return TOML_EDIT_OK;+}+/* Copy existing into output without the one owned legacy table. Returns+ * TOML_EDIT_OK (output holds the result, unchanged when the table is absent),+ * TOML_EDIT_FOREIGN for a same-name table that is not ours, or TOML_EDIT_ERR. */+static int toml_strip_legacy_table(const char *existing, size_t existing_len,+ const toml_key_path_t *desired, toml_buffer_t *output) {size_t cursor = 0U;toml_line_t line;int multiline_state = TOML_STRING_NONE;@@ -2566,10 +2635,7 @@ int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,int target_foreign = 0;int target_array_seen = 0;int target_regular_seen = 0;- int command_count = 0;- int command_owned = 0;- int args_count = 0;- int args_empty = 0;+ toml_legacy_shape_t shape = {0};size_t edit_start = SIZE_MAX;size_t edit_end = existing_len;while (toml_next_line(existing, existing_len, &cursor, &line)) {@@ -2578,21 +2644,17 @@ int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,if (!line_in_multiline) {toml_header_t header;if (toml_parse_header(existing, &line, "", &header) != TOML_EDIT_OK) {- toml_key_path_dispose(&desired);- free(existing);return TOML_EDIT_ERR;}if (header.present) {handled_header = 1;- int exact = toml_key_path_equal(&header.path, &desired);- int descendant = header.path.count > desired.count &&- toml_key_path_has_prefix(&header.path, &desired);+ int exact = toml_key_path_equal(&header.path, desired);+ int descendant = header.path.count > desired->count &&+ toml_key_path_has_prefix(&header.path, desired);if (exact) {if (header.array) {if (target_regular_seen) {toml_header_dispose(&header);- toml_key_path_dispose(&desired);- free(existing);return TOML_EDIT_ERR;}target_array_seen = 1;@@ -2600,22 +2662,16 @@ int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,target_count++;} else if (target_array_seen || target_regular_seen) {toml_header_dispose(&header);- toml_key_path_dispose(&desired);- free(existing);return TOML_EDIT_ERR;} else {target_regular_seen = 1;target_count = 1;}target_active = 1;- command_count = 0;- command_owned = 0;- args_count = 0;- args_empty = 0;+ memset(&shape, 0, sizeof(shape));edit_start = header.edit_start;} else if (target_active) {- if (descendant || !toml_legacy_schema_is_owned(command_count, command_owned,- args_count, args_empty)) {+ if (descendant || !toml_legacy_schema_is_owned(&shape)) {target_foreign = 1;}edit_end = header.edit_start;@@ -2627,78 +2683,104 @@ int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,if (target_active && !handled_header && !line_in_multiline &&!toml_line_is_blank_or_comment(existing, &line)) {toml_assignment_t assignment;- if (toml_parse_assignment(existing, &line, &assignment) != TOML_EDIT_OK ||- !assignment.present) {- toml_assignment_dispose(&assignment);- toml_key_path_dispose(&desired);- free(existing);- return TOML_EDIT_ERR;- }- if (assignment.key.count != 1U) {- target_foreign = 1;- } else if (toml_key_path_is_single(&assignment.key, "command")) {- if (++command_count > 1 ||- toml_legacy_command_is_owned(existing, &assignment, &command_owned) !=- TOML_EDIT_OK) {- toml_assignment_dispose(&assignment);- toml_key_path_dispose(&desired);- free(existing);- return TOML_EDIT_ERR;- }- if (!command_owned) {- target_foreign = 1;- }- } else if (toml_key_path_is_single(&assignment.key, "args")) {- args_count++;- args_empty = toml_legacy_args_are_empty(existing, &assignment);- if (args_count > 1) {- toml_assignment_dispose(&assignment);- toml_key_path_dispose(&desired);- free(existing);- return TOML_EDIT_ERR;- }- if (!args_empty) {- target_foreign = 1;- }- } else {- target_foreign = 1;+ int classified = TOML_EDIT_ERR;+ if (toml_parse_assignment(existing, &line, &assignment) == TOML_EDIT_OK &&+ assignment.present) {+ classified =+ toml_legacy_classify_assignment(existing, &assignment, &shape, &target_foreign);}toml_assignment_dispose(&assignment);+ if (classified != TOML_EDIT_OK) {+ return TOML_EDIT_ERR;+ }}if (toml_scan_line_strings(existing, &line, &multiline_state) != TOML_EDIT_OK) {- toml_key_path_dispose(&desired);- free(existing);return TOML_EDIT_ERR;}}- toml_key_path_dispose(&desired);if (multiline_state != TOML_STRING_NONE) {- free(existing);return TOML_EDIT_ERR;}- if (target_active &&- !toml_legacy_schema_is_owned(command_count, command_owned, args_count, args_empty)) {+ if (target_active && !toml_legacy_schema_is_owned(&shape)) {target_foreign = 1;}if (target_foreign) {- free(existing);return TOML_EDIT_FOREIGN;}if (target_count == 0) {+ return toml_buffer_append(output, existing, existing_len);+ }+ if (toml_buffer_append(output, existing, edit_start) != TOML_EDIT_OK ||+ toml_buffer_append(output, existing + edit_end, existing_len - edit_end) != TOML_EDIT_OK) {+ return TOML_EDIT_ERR;+ }+ return TOML_EDIT_OK;+}++int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,+ const char *begin_marker, const char *end_marker) {+ if (!toml_valid_path(file_path) || !table_name || !toml_valid_marker(begin_marker) ||+ !toml_valid_marker(end_marker) || strcmp(begin_marker, end_marker) == 0) {+ return TOML_EDIT_ERR;+ }+ toml_key_path_t desired;+ if (toml_parse_key_path(table_name, 0U, strlen(table_name), &desired) != TOML_EDIT_OK) {+ return TOML_EDIT_ERR;+ }++ char *existing = NULL;+ size_t existing_len = 0U;+ toml_file_snapshot_t snapshot;+ if (toml_read_file(file_path, &existing, &existing_len, &snapshot) != TOML_EDIT_OK ||+ !toml_text_is_safe(existing, existing_len, 1)) {+ toml_key_path_dispose(&desired);+ free(existing);+ return TOML_EDIT_ERR;+ }++ toml_line_t begin_line = {0};+ toml_line_t end_line = {0};+ int has_managed_pair = 0;+ int find_rc = toml_find_markers(existing, existing_len, begin_marker, end_marker, &begin_line,+ &end_line, &has_managed_pair);+ /* #1720: Codex rewrites the whole [mcp_servers] table when it edits MCP+ * servers itself and drops comment decor on the way, so our begin marker+ * vanishes while the end marker survives below whatever table follows.+ * The stray line is ours (#1558) and removing it guesses nothing; the+ * table it used to bracket is then judged by its shape like any other+ * unmarked table, so a foreign one still fails closed untouched. */+ toml_buffer_t healed = {0};+ const char *scan = existing;+ size_t scan_len = existing_len;+ int result = TOML_EDIT_OK;+ if (find_rc == TOML_EDIT_ORPHAN_MARKER) {+ const toml_line_t *stray = begin_line.full_end > 0U ? &begin_line : &end_line;+ if (toml_buffer_append(&healed, existing, stray->start) != TOML_EDIT_OK ||+ toml_buffer_append(&healed, existing + stray->full_end,+ existing_len - stray->full_end) != TOML_EDIT_OK) {+ result = TOML_EDIT_ERR;+ }+ scan = healed.data ? healed.data : "";+ scan_len = healed.len;+ } else if (find_rc != TOML_EDIT_OK) {+ result = TOML_EDIT_ERR;+ } else if (has_managed_pair) {+ toml_key_path_dispose(&desired);free(existing);return TOML_EDIT_OK;}toml_buffer_t output = {0};- if (toml_buffer_append(&output, existing, edit_start) != TOML_EDIT_OK ||- toml_buffer_append(&output, existing + edit_end, existing_len - edit_end) != TOML_EDIT_OK) {- toml_buffer_dispose(&output);- free(existing);- return TOML_EDIT_ERR;+ if (result == TOML_EDIT_OK) {+ result = toml_strip_legacy_table(scan, scan_len, &desired, &output);+ }+ if (result == TOML_EDIT_OK) {+ result = toml_write_atomic(file_path, existing, existing_len,+ output.data ? output.data : "", output.len, &snapshot);}- int result =- toml_write_atomic(file_path, existing, existing_len, output.data, output.len, &snapshot);toml_buffer_dispose(&output);+ toml_buffer_dispose(&healed);+ toml_key_path_dispose(&desired);free(existing);return result;}diff --git a/src/cli/config_toml_edit.h b/src/cli/config_toml_edit.hindex 4e41a4d9c..1b0ef137f 100644--- a/src/cli/config_toml_edit.h+++ b/src/cli/config_toml_edit.h@@ -31,11 +31,14 @@ int cbm_toml_remove_managed_block(const char *file_path, const char *begin_markeconst char *end_marker);/* Remove one pre-marker codebase-memory-mcp table only when it has the known- * historical schema: one owned command basename, optional empty args, and no- * unknown assignments or descendant tables. Returns 1 for a syntactically- * valid same-name foreign table and leaves it byte-identical, 0 for removed or- * absent owned state, and -1 for malformed input or I/O failure. A supplied- * managed-marker pair makes this a successful no-op. */+ * historical schema: one owned command basename, optional empty args, optional+ * env_vars naming only CBM_CACHE_DIR/CBM_RUNTIME_DIR, and no unknown+ * assignments or descendant tables. A lone orphaned managed marker (left when+ * a client rewrote the table and dropped its comments, #1720) is dropped in+ * the same write. Returns 1 for a syntactically valid same-name foreign table+ * and leaves the file byte-identical, 0 for removed or absent owned state, and+ * -1 for malformed input or I/O failure. A supplied managed-marker pair makes+ * this a successful no-op. */int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,const char *begin_marker, const char *end_marker);diff --git a/tests/test_cli.c b/tests/test_cli.cindex 267b403dd..02b3f8f85 100644--- a/tests/test_cli.c+++ b/tests/test_cli.c@@ -13804,6 +13804,57 @@ TEST(cli_codex_legacy_migration_ignores_header_text_in_multiline_string) {PASS();}+/* #1720: Codex rewrites its own [mcp_servers] tables whenever it edits MCP+ * servers (`codex mcp add`, the app's MCP settings, wrappers that drive it).+ * The rewrite drops comment decor, so our begin marker vanishes, `args = []`+ * is omitted, and the end marker survives as an ORPHAN below the next table.+ * The config below is byte-for-byte what codex-cli 0.153.4 produced from our+ * managed block after `codex mcp add orca -- /bin/echo hi`. Every later+ * install then failed `op=mcp_install` on a table cbm itself had written. */+TEST(cli_upsert_codex_mcp_adopts_codex_rewritten_table) {+ char tmpdir[256];+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-codex-rewrite-XXXXXX");+ if (!cbm_mkdtemp(tmpdir))+ FAIL("cbm_mkdtemp failed");+ char configpath[512];+ snprintf(configpath, sizeof(configpath), "%s/config.toml", tmpdir);+ write_test_file(configpath, "model = \"gpt-5\"\n"+ "\n"+ "[projects.\"/tmp/x\"]\n"+ "trust_level = \"trusted\"\n"+ "\n"+ "[mcp_servers.codebase-memory-mcp]\n"+ "command = \"/opt/cbm/codebase-memory-mcp\"\n"+ "env_vars = [\"CBM_CACHE_DIR\", \"CBM_RUNTIME_DIR\"]\n"+ "\n"+ "[mcp_servers.orca]\n"+ "command = \"/bin/echo\"\n"+ "args = [\"hi\"]\n"+ "# <<< codebase-memory-mcp MCP <<<\n"+ "\n"+ "[tui]\n"+ "theme = \"dark\"\n");++ int rc = cbm_upsert_codex_mcp("/new/cbm/codebase-memory-mcp", configpath);+ char *data = read_test_file_alloc(configpath);+ int tables = 0;+ for (const char *p = data; p && (p = strstr(p, "[mcp_servers.codebase-memory-mcp]")); p++) {+ tables++;+ }+ bool ok = data && tables == 1 && strstr(data, "/opt/cbm/") == NULL &&+ strstr(data, "command = \"/new/cbm/codebase-memory-mcp\"") != NULL &&+ strstr(data, "[mcp_servers.orca]\ncommand = \"/bin/echo\"\nargs = [\"hi\"]\n") &&+ strstr(data, "trust_level = \"trusted\"") && strstr(data, "theme = \"dark\"") &&+ strstr(data, "# >>> codebase-memory-mcp MCP >>>\n[mcp_servers.codebase-memory-mcp]");+ free(data);+ test_rmdir_r(tmpdir);+ if (rc != 0)+ FAIL("install must adopt the owned table Codex rewrote (orphan end marker, no args)");+ if (!ok)+ FAIL("adoption must leave one managed cbm table and every other entry intact");+ PASS();+}+/* ═══════════════════════════════════════════════════════════════════* Group B: MCP Config Upsert — Zed (corrected format)* ═══════════════════════════════════════════════════════════════════ */@@ -16942,6 +16993,7 @@ SUITE(cli) {RUN_TEST(cli_upsert_codex_mcp_existing);RUN_TEST(cli_upsert_codex_mcp_replace);RUN_TEST(cli_codex_legacy_migration_ignores_header_text_in_multiline_string);+ RUN_TEST(cli_upsert_codex_mcp_adopts_codex_rewritten_table);/* Zed MCP format fix (1 test — group B) */RUN_TEST(cli_zed_mcp_uses_args_format);diff --git a/tests/test_config_toml_edit.c b/tests/test_config_toml_edit.cindex 588b11316..465f3d922 100644--- a/tests/test_config_toml_edit.c+++ b/tests/test_config_toml_edit.c@@ -566,6 +566,41 @@ TEST(config_toml_legacy_remove_reports_foreign_table_without_mutation) {PASS();}+/* #1720: `env_vars` naming only the variables cbm itself forwards is part of+ * the owned shape (#1562/#1664), so a table Codex rewrote without markers is+ * adopted; any other forwarded name keeps the table foreign. */+TEST(config_toml_legacy_remove_adopts_owned_env_vars_only) {+ char dir[CTE_PATH_CAP];+ char path[CTE_PATH_CAP];+ char actual[CTE_FILE_CAP];+ ASSERT_EQ(cte_fixture(dir, sizeof(dir), path, sizeof(path)), 0);+ ASSERT_EQ(th_write_file(path, "theme = \"dark\"\n"+ "[mcp_servers.codebase-memory-mcp]\n"+ "command = 'C:\\cbm\\codebase-memory-mcp.exe'\n"+ "env_vars = [\"CBM_CACHE_DIR\"]\n"+ "\n"+ "[tui]\n"+ "keep = true\n"),+ 0);+ ASSERT_EQ(+ cbm_toml_remove_legacy_table(path, "mcp_servers.codebase-memory-mcp", CTE_BEGIN, CTE_END),+ 0);+ ASSERT_EQ(cte_read(path, actual, sizeof(actual)), 0);+ ASSERT_STR_EQ(actual, "theme = \"dark\"\n[tui]\nkeep = true\n");++ const char *foreign = "[mcp_servers.codebase-memory-mcp]\n"+ "command = \"/opt/codebase-memory-mcp\"\n"+ "env_vars = [\"CBM_CACHE_DIR\", \"HOME\"]\n";+ ASSERT_EQ(th_write_file(path, foreign), 0);+ ASSERT_EQ(+ cbm_toml_remove_legacy_table(path, "mcp_servers.codebase-memory-mcp", CTE_BEGIN, CTE_END),+ 1);+ ASSERT_EQ(cte_read(path, actual, sizeof(actual)), 0);+ ASSERT_STR_EQ(actual, foreign);+ th_cleanup(dir);+ PASS();+}+TEST(config_toml_vibe_non_array_and_dotted_conflicts_fail_closed) {char dir[CTE_PATH_CAP];char path[CTE_PATH_CAP];@@ -1558,4 +1593,5 @@ SUITE(config_toml_edit) {RUN_TEST(config_toml_codex_accepts_quoted_binary_path_with_spaces);RUN_TEST(config_toml_codex_preserves_bom_crlf_and_foreign_aot);RUN_TEST(config_toml_legacy_remove_reports_foreign_table_without_mutation);+ RUN_TEST(config_toml_legacy_remove_adopts_owned_env_vars_only);}