Skip to content

Commit 28e24c6

Browse files
authored
Merge pull request DeusData#2324 from DeusData/fix/issue-1720
fix(install): adopt the Codex MCP table after Codex rewrites it (DeusData#1720)
2 parents f67243f + e81fd49 commit 28e24c6

4 files changed

Lines changed: 280 additions & 107 deletions

File tree

‎src/cli/config_toml_edit.c‎

Lines changed: 184 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,48 +2516,117 @@ static int toml_legacy_args_are_empty(const char *data, const toml_assignment_t
25162516
return pos == end;
25172517
}
25182518

2519-
static int toml_legacy_schema_is_owned(int command_count, int command_owned, int args_count,
2520-
int args_empty) {
2521-
return command_count == 1 && command_owned && args_count <= 1 &&
2522-
(args_count == 0 || args_empty);
2519+
/* #1720: the only names cbm has ever listed in env_vars (#1562, #1664). */
2520+
static int toml_legacy_env_name_is_owned(const toml_string_t *name) {
2521+
static const char *const owned[] = {"CBM_CACHE_DIR", "CBM_RUNTIME_DIR"};
2522+
for (size_t i = 0U; i < sizeof(owned) / sizeof(owned[0]); ++i) {
2523+
size_t owned_len = strlen(owned[i]);
2524+
if (name->len == owned_len && memcmp(name->data, owned[i], owned_len) == 0) {
2525+
return 1;
2526+
}
2527+
}
2528+
return 0;
25232529
}
25242530

2525-
int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,
2526-
const char *begin_marker, const char *end_marker) {
2527-
if (!toml_valid_path(file_path) || !table_name || !toml_valid_marker(begin_marker) ||
2528-
!toml_valid_marker(end_marker) || strcmp(begin_marker, end_marker) == 0) {
2529-
return TOML_EDIT_ERR;
2530-
}
2531-
toml_key_path_t desired;
2532-
if (toml_parse_key_path(table_name, 0U, strlen(table_name), &desired) != TOML_EDIT_OK) {
2533-
return TOML_EDIT_ERR;
2531+
static void toml_skip_blanks(const char *data, size_t end, size_t *pos) {
2532+
while (*pos < end && (data[*pos] == ' ' || data[*pos] == '\t')) {
2533+
++*pos;
25342534
}
2535+
}
25352536

2536-
char *existing = NULL;
2537-
size_t existing_len = 0U;
2538-
toml_file_snapshot_t snapshot;
2539-
if (toml_read_file(file_path, &existing, &existing_len, &snapshot) != TOML_EDIT_OK ||
2540-
!toml_text_is_safe(existing, existing_len, 1)) {
2541-
toml_key_path_dispose(&desired);
2542-
free(existing);
2543-
return TOML_EDIT_ERR;
2537+
/* A single-line array of strings naming only variables cbm forwards: the
2538+
* value cbm writes, as Codex re-serializes it when it rewrites the table. */
2539+
static int toml_legacy_env_vars_are_owned(const char *data, const toml_assignment_t *assignment) {
2540+
if (!assignment->present || assignment->multiline_value) {
2541+
return 0;
25442542
}
2545-
2546-
toml_line_t begin_line = {0};
2547-
toml_line_t end_line = {0};
2548-
int has_managed_pair = 0;
2549-
if (toml_find_markers(existing, existing_len, begin_marker, end_marker, &begin_line, &end_line,
2550-
&has_managed_pair) != TOML_EDIT_OK) {
2551-
toml_key_path_dispose(&desired);
2552-
free(existing);
2553-
return TOML_EDIT_ERR;
2543+
size_t pos = assignment->value_start;
2544+
size_t end = assignment->value_end;
2545+
if (pos >= end || data[pos++] != '[') {
2546+
return 0;
25542547
}
2555-
if (has_managed_pair) {
2556-
toml_key_path_dispose(&desired);
2557-
free(existing);
2558-
return TOML_EDIT_OK;
2548+
for (;;) {
2549+
toml_skip_blanks(data, end, &pos);
2550+
if (pos < end && data[pos] == ']') {
2551+
pos++;
2552+
break;
2553+
}
2554+
toml_string_t name = {0};
2555+
if (pos >= end || toml_parse_string(data + pos, end - pos, &name) != TOML_EDIT_OK) {
2556+
return 0;
2557+
}
2558+
int owned = toml_legacy_env_name_is_owned(&name);
2559+
pos += name.consumed;
2560+
toml_string_dispose(&name);
2561+
toml_skip_blanks(data, end, &pos);
2562+
if (!owned || pos >= end || (data[pos] != ',' && data[pos] != ']')) {
2563+
return 0;
2564+
}
2565+
if (data[pos] == ',') {
2566+
pos++;
2567+
}
2568+
}
2569+
toml_skip_blanks(data, end, &pos);
2570+
return pos == end;
2571+
}
2572+
2573+
typedef struct {
2574+
int command_count;
2575+
int command_owned;
2576+
int args_count;
2577+
int args_empty;
2578+
int env_vars_count;
2579+
int env_vars_owned;
2580+
} toml_legacy_shape_t;
2581+
2582+
static int toml_legacy_schema_is_owned(const toml_legacy_shape_t *shape) {
2583+
return shape->command_count == 1 && shape->command_owned && shape->args_count <= 1 &&
2584+
(shape->args_count == 0 || shape->args_empty) && shape->env_vars_count <= 1 &&
2585+
(shape->env_vars_count == 0 || shape->env_vars_owned);
2586+
}
2587+
2588+
/* Classify one assignment inside the target table. Returns TOML_EDIT_ERR for
2589+
* a duplicated key or an unparsable command, otherwise TOML_EDIT_OK with
2590+
* *foreign set when the assignment is not part of the owned schema. */
2591+
static int toml_legacy_classify_assignment(const char *data, const toml_assignment_t *assignment,
2592+
toml_legacy_shape_t *shape, int *foreign) {
2593+
if (assignment->key.count != 1U) {
2594+
*foreign = 1;
2595+
} else if (toml_key_path_is_single(&assignment->key, "command")) {
2596+
if (++shape->command_count > 1 ||
2597+
toml_legacy_command_is_owned(data, assignment, &shape->command_owned) != TOML_EDIT_OK) {
2598+
return TOML_EDIT_ERR;
2599+
}
2600+
if (!shape->command_owned) {
2601+
*foreign = 1;
2602+
}
2603+
} else if (toml_key_path_is_single(&assignment->key, "args")) {
2604+
shape->args_empty = toml_legacy_args_are_empty(data, assignment);
2605+
if (++shape->args_count > 1) {
2606+
return TOML_EDIT_ERR;
2607+
}
2608+
if (!shape->args_empty) {
2609+
*foreign = 1;
2610+
}
2611+
} else if (toml_key_path_is_single(&assignment->key, "env_vars")) {
2612+
shape->env_vars_owned = toml_legacy_env_vars_are_owned(data, assignment);
2613+
if (++shape->env_vars_count > 1) {
2614+
return TOML_EDIT_ERR;
2615+
}
2616+
if (!shape->env_vars_owned) {
2617+
*foreign = 1;
2618+
}
2619+
} else {
2620+
*foreign = 1;
25592621
}
2622+
return TOML_EDIT_OK;
2623+
}
25602624

2625+
/* Copy existing into output without the one owned legacy table. Returns
2626+
* TOML_EDIT_OK (output holds the result, unchanged when the table is absent),
2627+
* TOML_EDIT_FOREIGN for a same-name table that is not ours, or TOML_EDIT_ERR. */
2628+
static int toml_strip_legacy_table(const char *existing, size_t existing_len,
2629+
const toml_key_path_t *desired, toml_buffer_t *output) {
25612630
size_t cursor = 0U;
25622631
toml_line_t line;
25632632
int multiline_state = TOML_STRING_NONE;
@@ -2566,10 +2635,7 @@ int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,
25662635
int target_foreign = 0;
25672636
int target_array_seen = 0;
25682637
int target_regular_seen = 0;
2569-
int command_count = 0;
2570-
int command_owned = 0;
2571-
int args_count = 0;
2572-
int args_empty = 0;
2638+
toml_legacy_shape_t shape = {0};
25732639
size_t edit_start = SIZE_MAX;
25742640
size_t edit_end = existing_len;
25752641
while (toml_next_line(existing, existing_len, &cursor, &line)) {
@@ -2578,44 +2644,34 @@ int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,
25782644
if (!line_in_multiline) {
25792645
toml_header_t header;
25802646
if (toml_parse_header(existing, &line, "", &header) != TOML_EDIT_OK) {
2581-
toml_key_path_dispose(&desired);
2582-
free(existing);
25832647
return TOML_EDIT_ERR;
25842648
}
25852649
if (header.present) {
25862650
handled_header = 1;
2587-
int exact = toml_key_path_equal(&header.path, &desired);
2588-
int descendant = header.path.count > desired.count &&
2589-
toml_key_path_has_prefix(&header.path, &desired);
2651+
int exact = toml_key_path_equal(&header.path, desired);
2652+
int descendant = header.path.count > desired->count &&
2653+
toml_key_path_has_prefix(&header.path, desired);
25902654
if (exact) {
25912655
if (header.array) {
25922656
if (target_regular_seen) {
25932657
toml_header_dispose(&header);
2594-
toml_key_path_dispose(&desired);
2595-
free(existing);
25962658
return TOML_EDIT_ERR;
25972659
}
25982660
target_array_seen = 1;
25992661
target_foreign = 1;
26002662
target_count++;
26012663
} else if (target_array_seen || target_regular_seen) {
26022664
toml_header_dispose(&header);
2603-
toml_key_path_dispose(&desired);
2604-
free(existing);
26052665
return TOML_EDIT_ERR;
26062666
} else {
26072667
target_regular_seen = 1;
26082668
target_count = 1;
26092669
}
26102670
target_active = 1;
2611-
command_count = 0;
2612-
command_owned = 0;
2613-
args_count = 0;
2614-
args_empty = 0;
2671+
memset(&shape, 0, sizeof(shape));
26152672
edit_start = header.edit_start;
26162673
} else if (target_active) {
2617-
if (descendant || !toml_legacy_schema_is_owned(command_count, command_owned,
2618-
args_count, args_empty)) {
2674+
if (descendant || !toml_legacy_schema_is_owned(&shape)) {
26192675
target_foreign = 1;
26202676
}
26212677
edit_end = header.edit_start;
@@ -2627,78 +2683,104 @@ int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,
26272683
if (target_active && !handled_header && !line_in_multiline &&
26282684
!toml_line_is_blank_or_comment(existing, &line)) {
26292685
toml_assignment_t assignment;
2630-
if (toml_parse_assignment(existing, &line, &assignment) != TOML_EDIT_OK ||
2631-
!assignment.present) {
2632-
toml_assignment_dispose(&assignment);
2633-
toml_key_path_dispose(&desired);
2634-
free(existing);
2635-
return TOML_EDIT_ERR;
2636-
}
2637-
if (assignment.key.count != 1U) {
2638-
target_foreign = 1;
2639-
} else if (toml_key_path_is_single(&assignment.key, "command")) {
2640-
if (++command_count > 1 ||
2641-
toml_legacy_command_is_owned(existing, &assignment, &command_owned) !=
2642-
TOML_EDIT_OK) {
2643-
toml_assignment_dispose(&assignment);
2644-
toml_key_path_dispose(&desired);
2645-
free(existing);
2646-
return TOML_EDIT_ERR;
2647-
}
2648-
if (!command_owned) {
2649-
target_foreign = 1;
2650-
}
2651-
} else if (toml_key_path_is_single(&assignment.key, "args")) {
2652-
args_count++;
2653-
args_empty = toml_legacy_args_are_empty(existing, &assignment);
2654-
if (args_count > 1) {
2655-
toml_assignment_dispose(&assignment);
2656-
toml_key_path_dispose(&desired);
2657-
free(existing);
2658-
return TOML_EDIT_ERR;
2659-
}
2660-
if (!args_empty) {
2661-
target_foreign = 1;
2662-
}
2663-
} else {
2664-
target_foreign = 1;
2686+
int classified = TOML_EDIT_ERR;
2687+
if (toml_parse_assignment(existing, &line, &assignment) == TOML_EDIT_OK &&
2688+
assignment.present) {
2689+
classified =
2690+
toml_legacy_classify_assignment(existing, &assignment, &shape, &target_foreign);
26652691
}
26662692
toml_assignment_dispose(&assignment);
2693+
if (classified != TOML_EDIT_OK) {
2694+
return TOML_EDIT_ERR;
2695+
}
26672696
}
26682697
if (toml_scan_line_strings(existing, &line, &multiline_state) != TOML_EDIT_OK) {
2669-
toml_key_path_dispose(&desired);
2670-
free(existing);
26712698
return TOML_EDIT_ERR;
26722699
}
26732700
}
2674-
toml_key_path_dispose(&desired);
26752701
if (multiline_state != TOML_STRING_NONE) {
2676-
free(existing);
26772702
return TOML_EDIT_ERR;
26782703
}
2679-
if (target_active &&
2680-
!toml_legacy_schema_is_owned(command_count, command_owned, args_count, args_empty)) {
2704+
if (target_active && !toml_legacy_schema_is_owned(&shape)) {
26812705
target_foreign = 1;
26822706
}
26832707
if (target_foreign) {
2684-
free(existing);
26852708
return TOML_EDIT_FOREIGN;
26862709
}
26872710
if (target_count == 0) {
2711+
return toml_buffer_append(output, existing, existing_len);
2712+
}
2713+
if (toml_buffer_append(output, existing, edit_start) != TOML_EDIT_OK ||
2714+
toml_buffer_append(output, existing + edit_end, existing_len - edit_end) != TOML_EDIT_OK) {
2715+
return TOML_EDIT_ERR;
2716+
}
2717+
return TOML_EDIT_OK;
2718+
}
2719+
2720+
int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,
2721+
const char *begin_marker, const char *end_marker) {
2722+
if (!toml_valid_path(file_path) || !table_name || !toml_valid_marker(begin_marker) ||
2723+
!toml_valid_marker(end_marker) || strcmp(begin_marker, end_marker) == 0) {
2724+
return TOML_EDIT_ERR;
2725+
}
2726+
toml_key_path_t desired;
2727+
if (toml_parse_key_path(table_name, 0U, strlen(table_name), &desired) != TOML_EDIT_OK) {
2728+
return TOML_EDIT_ERR;
2729+
}
2730+
2731+
char *existing = NULL;
2732+
size_t existing_len = 0U;
2733+
toml_file_snapshot_t snapshot;
2734+
if (toml_read_file(file_path, &existing, &existing_len, &snapshot) != TOML_EDIT_OK ||
2735+
!toml_text_is_safe(existing, existing_len, 1)) {
2736+
toml_key_path_dispose(&desired);
2737+
free(existing);
2738+
return TOML_EDIT_ERR;
2739+
}
2740+
2741+
toml_line_t begin_line = {0};
2742+
toml_line_t end_line = {0};
2743+
int has_managed_pair = 0;
2744+
int find_rc = toml_find_markers(existing, existing_len, begin_marker, end_marker, &begin_line,
2745+
&end_line, &has_managed_pair);
2746+
/* #1720: Codex rewrites the whole [mcp_servers] table when it edits MCP
2747+
* servers itself and drops comment decor on the way, so our begin marker
2748+
* vanishes while the end marker survives below whatever table follows.
2749+
* The stray line is ours (#1558) and removing it guesses nothing; the
2750+
* table it used to bracket is then judged by its shape like any other
2751+
* unmarked table, so a foreign one still fails closed untouched. */
2752+
toml_buffer_t healed = {0};
2753+
const char *scan = existing;
2754+
size_t scan_len = existing_len;
2755+
int result = TOML_EDIT_OK;
2756+
if (find_rc == TOML_EDIT_ORPHAN_MARKER) {
2757+
const toml_line_t *stray = begin_line.full_end > 0U ? &begin_line : &end_line;
2758+
if (toml_buffer_append(&healed, existing, stray->start) != TOML_EDIT_OK ||
2759+
toml_buffer_append(&healed, existing + stray->full_end,
2760+
existing_len - stray->full_end) != TOML_EDIT_OK) {
2761+
result = TOML_EDIT_ERR;
2762+
}
2763+
scan = healed.data ? healed.data : "";
2764+
scan_len = healed.len;
2765+
} else if (find_rc != TOML_EDIT_OK) {
2766+
result = TOML_EDIT_ERR;
2767+
} else if (has_managed_pair) {
2768+
toml_key_path_dispose(&desired);
26882769
free(existing);
26892770
return TOML_EDIT_OK;
26902771
}
26912772

26922773
toml_buffer_t output = {0};
2693-
if (toml_buffer_append(&output, existing, edit_start) != TOML_EDIT_OK ||
2694-
toml_buffer_append(&output, existing + edit_end, existing_len - edit_end) != TOML_EDIT_OK) {
2695-
toml_buffer_dispose(&output);
2696-
free(existing);
2697-
return TOML_EDIT_ERR;
2774+
if (result == TOML_EDIT_OK) {
2775+
result = toml_strip_legacy_table(scan, scan_len, &desired, &output);
2776+
}
2777+
if (result == TOML_EDIT_OK) {
2778+
result = toml_write_atomic(file_path, existing, existing_len,
2779+
output.data ? output.data : "", output.len, &snapshot);
26982780
}
2699-
int result =
2700-
toml_write_atomic(file_path, existing, existing_len, output.data, output.len, &snapshot);
27012781
toml_buffer_dispose(&output);
2782+
toml_buffer_dispose(&healed);
2783+
toml_key_path_dispose(&desired);
27022784
free(existing);
27032785
return result;
27042786
}

‎src/cli/config_toml_edit.h‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ int cbm_toml_remove_managed_block(const char *file_path, const char *begin_marke
3131
const char *end_marker);
3232

3333
/* Remove one pre-marker codebase-memory-mcp table only when it has the known
34-
* historical schema: one owned command basename, optional empty args, and no
35-
* unknown assignments or descendant tables. Returns 1 for a syntactically
36-
* valid same-name foreign table and leaves it byte-identical, 0 for removed or
37-
* absent owned state, and -1 for malformed input or I/O failure. A supplied
38-
* managed-marker pair makes this a successful no-op. */
34+
* historical schema: one owned command basename, optional empty args, optional
35+
* env_vars naming only CBM_CACHE_DIR/CBM_RUNTIME_DIR, and no unknown
36+
* assignments or descendant tables. A lone orphaned managed marker (left when
37+
* a client rewrote the table and dropped its comments, #1720) is dropped in
38+
* the same write. Returns 1 for a syntactically valid same-name foreign table
39+
* and leaves the file byte-identical, 0 for removed or absent owned state, and
40+
* -1 for malformed input or I/O failure. A supplied managed-marker pair makes
41+
* this a successful no-op. */
3942
int cbm_toml_remove_legacy_table(const char *file_path, const char *table_name,
4043
const char *begin_marker, const char *end_marker);
4144

0 commit comments

Comments
 (0)