Skip to content

Commit e148e3e

Browse files
authored
Merge branch 'main' into fix/index-worker-explicit-memory-budget
2 parents b8aca8f + c1a5de3 commit e148e3e

7 files changed

Lines changed: 1235 additions & 50 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 153 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,16 +1672,45 @@ static size_t cbm_json_mcp_ownership_fields(cbm_json_mcp_schema_t schema, const
16721672
* populated from config content. */
16731673
static 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+
16751703
static 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
17001734
typedef 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+
19972062
static 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+
37303864
static 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;

‎src/cli/config_json_like.c‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,86 @@ int cbm_json_like_upsert_entry_if_unchanged(const char *file_path, const char *c
22132213
expected_content, expected_length);
22142214
}
22152215

2216+
int cbm_json_like_replace_field_raw_if_unchanged(const char *file_path,
2217+
const char *const *object_path, size_t path_len,
2218+
const char *entry_key, const char *field_key,
2219+
const char *raw_value,
2220+
const char *expected_content,
2221+
size_t expected_length) {
2222+
if (jl_validate_arguments(file_path, object_path, path_len, entry_key) != 0 || !field_key ||
2223+
field_key[0] == '\0' || !raw_value) {
2224+
return -1;
2225+
}
2226+
size_t value_offset = 0U;
2227+
size_t value_length = 0U;
2228+
if (jl_validate_entry(raw_value, &value_offset, &value_length) != 0) {
2229+
return -1;
2230+
}
2231+
2232+
char *source = NULL;
2233+
size_t source_length = 0;
2234+
bool missing = false;
2235+
jl_file_snapshot_t snapshot;
2236+
if (jl_read_file(file_path, &source, &source_length, &missing, &snapshot) != 0) {
2237+
return -1;
2238+
}
2239+
bool content_matches =
2240+
expected_content
2241+
? !missing && source_length == expected_length &&
2242+
(expected_length == 0U || memcmp(source, expected_content, expected_length) == 0)
2243+
: missing;
2244+
if (!content_matches || missing || source_length == 0U) {
2245+
free(source);
2246+
return -1;
2247+
}
2248+
2249+
size_t object_start = 0;
2250+
if (jl_validate_document(source, source_length, &object_start) != 0) {
2251+
free(source);
2252+
return -1;
2253+
}
2254+
for (size_t i = 0; i < path_len; i++) {
2255+
jl_object_t parent;
2256+
if (jl_scan_object(source, source_length, object_start, object_path[i], &parent) != 0 ||
2257+
parent.match_count != 1U || source[parent.match.value_start] != '{') {
2258+
free(source);
2259+
return -1;
2260+
}
2261+
object_start = parent.match.value_start;
2262+
}
2263+
jl_object_t entry;
2264+
if (jl_scan_object(source, source_length, object_start, entry_key, &entry) != 0 ||
2265+
entry.match_count != 1U || source[entry.match.value_start] != '{') {
2266+
free(source);
2267+
return -1;
2268+
}
2269+
jl_object_t field;
2270+
if (jl_scan_object(source, source_length, entry.match.value_start, field_key, &field) != 0 ||
2271+
field.match_count != 1U) {
2272+
free(source);
2273+
return -1;
2274+
}
2275+
2276+
size_t head = field.match.value_start;
2277+
size_t tail = field.match.value_end;
2278+
size_t updated_length = head + value_length + (source_length - tail);
2279+
char *updated = (char *)malloc(updated_length + 1U);
2280+
if (!updated) {
2281+
free(source);
2282+
return -1;
2283+
}
2284+
memcpy(updated, source, head);
2285+
memcpy(updated + head, raw_value + value_offset, value_length);
2286+
memcpy(updated + head + value_length, source + tail, source_length - tail);
2287+
updated[updated_length] = '\0';
2288+
2289+
int result =
2290+
jl_write_document(file_path, updated, updated_length, source, source_length, &snapshot);
2291+
free(updated);
2292+
free(source);
2293+
return result;
2294+
}
2295+
22162296
static int jl_remove_entry(const char *file_path, const char *const *object_path, size_t path_len,
22172297
const char *entry_key, bool enforce_expected,
22182298
const char *expected_content, size_t expected_length) {

‎src/cli/config_json_like.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ int cbm_json_like_upsert_entry_if_unchanged(const char *file_path, const char *c
8989
const char *entry_json, const char *expected_content,
9090
size_t expected_length);
9191

92+
/* Replace ONE member's value inside the entry object at object_path/entry_key,
93+
* preserving every other byte of the document (comments, ordering, and any
94+
* keys the client added around it). The field-merge primitive for repairing an
95+
* annotated entry (#1630): replacing the whole entry would drop the client's
96+
* keys. raw_value must be a single complete JSON value; the field must exist
97+
* exactly once. Same expected-content contract as the _if_unchanged editors. */
98+
int cbm_json_like_replace_field_raw_if_unchanged(const char *file_path,
99+
const char *const *object_path, size_t path_len,
100+
const char *entry_key, const char *field_key,
101+
const char *raw_value,
102+
const char *expected_content,
103+
size_t expected_length);
104+
92105
/* Remove entry_key from the object at object_path. A missing path or entry is
93106
* a successful no-op. Returns 0 on success and -1 on invalid input or I/O. */
94107
int cbm_json_like_remove_entry(const char *file_path, const char *const *object_path,

0 commit comments

Comments
 (0)