Skip to content

Commit 713764b

Browse files
authored
Merge pull request DeusData#2116 from DeusData/fix/713-1826-nongit-guard-openhands
fix: bound non-git auto_index (DeusData#713) + complete OpenHands settings/profile registration (DeusData#1826)
2 parents c725453 + f6a2927 commit 713764b

7 files changed

Lines changed: 835 additions & 26 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,140 @@ static int cbm_remove_openclaw_compaction(const char *config_path) {
22862286
: CLI_ERR;
22872287
}
22882288

2289+
/* ── OpenHands settings.json mcp_config (#1826) ───────────────
2290+
* The mcpServers-style config installed above (cbm_install_editor_mcp into
2291+
* ~/.openhands/mcp.json) is not enough: OpenHands only loads a global MCP
2292+
* server it finds registered under settings.json -> mcp_config, in its own
2293+
* shape ({transport, command, enabled} — no args array). Agent profiles then
2294+
* opt in individually via mcp_server_refs (below). */
2295+
2296+
static size_t cbm_openhands_ownership_fields(cbm_json_like_object_field_t fields[3]) {
2297+
fields[0] = (cbm_json_like_object_field_t){
2298+
.key = "transport",
2299+
.shape = CBM_JSON_LIKE_VALUE_STRING,
2300+
.expected_string = "stdio",
2301+
.flags = CBM_JSON_LIKE_FIELD_REQUIRED,
2302+
};
2303+
fields[1] = (cbm_json_like_object_field_t){
2304+
.key = "command",
2305+
.shape = CBM_JSON_LIKE_VALUE_STRING,
2306+
.expected_string = NULL,
2307+
.flags = CBM_JSON_LIKE_FIELD_REQUIRED | CBM_JSON_LIKE_FIELD_CAPTURE_STRING,
2308+
};
2309+
fields[2] = (cbm_json_like_object_field_t){
2310+
.key = "enabled",
2311+
.shape = CBM_JSON_LIKE_VALUE_LITERAL,
2312+
.expected_string = "true",
2313+
.flags = CBM_JSON_LIKE_FIELD_REQUIRED,
2314+
};
2315+
return 3U;
2316+
}
2317+
2318+
static char *cbm_build_openhands_mcp_entry(const char *binary_path) {
2319+
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
2320+
if (!doc) {
2321+
return NULL;
2322+
}
2323+
yyjson_mut_val *root = yyjson_mut_obj(doc);
2324+
bool ok = root && yyjson_mut_obj_add_strcpy(doc, root, "transport", "stdio") &&
2325+
yyjson_mut_obj_add_strcpy(doc, root, "command", binary_path) &&
2326+
yyjson_mut_obj_add_bool(doc, root, "enabled", true);
2327+
char *json = NULL;
2328+
if (ok) {
2329+
yyjson_mut_doc_set_root(doc, root);
2330+
json = yyjson_mut_write(doc, YYJSON_WRITE_NOFLAG, NULL);
2331+
}
2332+
yyjson_mut_doc_free(doc);
2333+
return json;
2334+
}
2335+
2336+
/* Insert or leave alone: an already-owned entry (exact match, or annotated
2337+
* with extra keys the client added) needs no write. A same-named entry that
2338+
* does not match our shape is left untouched and reported as an error rather
2339+
* than overwritten, matching cbm_upsert_json_named_mcp's fail-closed rule for
2340+
* every other editor client. */
2341+
static int cbm_upsert_openhands_settings_mcp(const char *binary_path, const char *settings_path) {
2342+
if (!binary_path || !settings_path) {
2343+
return CLI_ERR;
2344+
}
2345+
static const char *const path[] = {"mcp_config"};
2346+
char *document = NULL;
2347+
size_t document_length = 0U;
2348+
int read_result = cbm_json_like_read_document(settings_path, &document, &document_length);
2349+
if (read_result < 0) {
2350+
return CLI_ERR;
2351+
}
2352+
if (read_result == 0) {
2353+
cbm_json_like_object_field_t fields[3];
2354+
size_t field_count = cbm_openhands_ownership_fields(fields);
2355+
char *command = NULL;
2356+
int ownership = cbm_json_like_match_object_entry(document, document_length, path, 1U,
2357+
CBM_DEFAULT_MCP_SERVER_NAME, fields,
2358+
field_count, &command);
2359+
free(command);
2360+
if (ownership == CBM_JSON_LIKE_OBJECT_MATCH ||
2361+
ownership == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS) {
2362+
free(document);
2363+
return CLI_OK;
2364+
}
2365+
if (ownership != CBM_JSON_LIKE_OBJECT_MISSING) {
2366+
free(document);
2367+
return CLI_ERR;
2368+
}
2369+
}
2370+
char *entry = cbm_build_openhands_mcp_entry(binary_path);
2371+
if (!entry) {
2372+
free(document);
2373+
return CLI_ERR;
2374+
}
2375+
int edit_result = cbm_json_like_upsert_entry_if_unchanged(
2376+
settings_path, path, 1U, CBM_DEFAULT_MCP_SERVER_NAME, entry,
2377+
read_result == 1 ? NULL : document, document_length);
2378+
free(entry);
2379+
free(document);
2380+
return edit_result == 0 ? CLI_OK : CLI_ERR;
2381+
}
2382+
2383+
/* Remove only an entry that is still recognisably ours (an annotated entry is
2384+
* left in place and reported, same rule as insertion above); a missing file,
2385+
* path, or entry is a successful no-op. */
2386+
static int cbm_remove_openhands_settings_mcp(const char *settings_path) {
2387+
if (!settings_path) {
2388+
return CLI_ERR;
2389+
}
2390+
static const char *const path[] = {"mcp_config"};
2391+
char *document = NULL;
2392+
size_t document_length = 0U;
2393+
int read_result = cbm_json_like_read_document(settings_path, &document, &document_length);
2394+
if (read_result == 1) {
2395+
free(document);
2396+
return CLI_OK;
2397+
}
2398+
if (read_result < 0) {
2399+
free(document);
2400+
return CLI_ERR;
2401+
}
2402+
cbm_json_like_object_field_t fields[3];
2403+
size_t field_count = cbm_openhands_ownership_fields(fields);
2404+
char *command = NULL;
2405+
int ownership = cbm_json_like_match_object_entry(document, document_length, path, 1U,
2406+
CBM_DEFAULT_MCP_SERVER_NAME, fields,
2407+
field_count, &command);
2408+
free(command);
2409+
if (ownership == CBM_JSON_LIKE_OBJECT_MISSING || ownership == CBM_JSON_LIKE_OBJECT_MISMATCH) {
2410+
free(document);
2411+
return CLI_OK;
2412+
}
2413+
if (ownership != CBM_JSON_LIKE_OBJECT_MATCH) {
2414+
free(document);
2415+
return CLI_ERR;
2416+
}
2417+
int edit_result = cbm_json_like_remove_entry_if_unchanged(
2418+
settings_path, path, 1U, CBM_DEFAULT_MCP_SERVER_NAME, document, document_length);
2419+
free(document);
2420+
return edit_result == 0 ? CLI_OK : CLI_ERR;
2421+
}
2422+
22892423
/* ── VS Code MCP (servers key with type:stdio) ────────────────── */
22902424

22912425
int cbm_install_vscode_mcp(const char *binary_path, const char *config_path) {
@@ -9355,6 +9489,47 @@ static void uninstall_vscode_profile_configs(const char *code_user, const char *
93559489
cbm_closedir(directory);
93569490
}
93579491

9492+
static bool cbm_filename_has_suffix(const char *name, const char *suffix) {
9493+
size_t name_len = strlen(name);
9494+
size_t suffix_len = strlen(suffix);
9495+
return name_len >= suffix_len && strcmp(name + (name_len - suffix_len), suffix) == 0;
9496+
}
9497+
9498+
/* Register or unregister our server against every existing OpenHands agent
9499+
* profile's mcp_server_refs array (#1826). A missing agent-profiles/
9500+
* directory is a silent no-op in both directions — install must never invent
9501+
* it, and uninstall has nothing to undo there. Only *.json entries are
9502+
* touched; a profile directory may hold arbitrary notes alongside profiles. */
9503+
static void openhands_update_profile_refs(const char *profiles_dir, bool installing, bool dry_run) {
9504+
cbm_dir_t *d = cbm_opendir(profiles_dir);
9505+
if (!d) {
9506+
return;
9507+
}
9508+
cbm_dirent_t *ent;
9509+
while ((ent = cbm_readdir(d)) != NULL) {
9510+
if (strcmp(ent->name, ".") == 0 || strcmp(ent->name, "..") == 0 ||
9511+
!cbm_filename_has_suffix(ent->name, ".json")) {
9512+
continue;
9513+
}
9514+
char profile_path[CLI_BUF_1K];
9515+
snprintf(profile_path, sizeof(profile_path), "%s/%s", profiles_dir, ent->name);
9516+
struct stat state;
9517+
if (stat(profile_path, &state) != 0 || !S_ISREG(state.st_mode) || dry_run) {
9518+
continue;
9519+
}
9520+
int result = installing ? cbm_json_like_add_unique_string(profile_path, "mcp_server_refs",
9521+
CBM_DEFAULT_MCP_SERVER_NAME)
9522+
: cbm_json_like_remove_string(profile_path, "mcp_server_refs",
9523+
CBM_DEFAULT_MCP_SERVER_NAME);
9524+
if (result != CLI_OK) {
9525+
record_agent_config_error(
9526+
!installing, "OpenHands",
9527+
installing ? "profile_refs_install" : "profile_refs_uninstall", profile_path);
9528+
}
9529+
}
9530+
cbm_closedir(d);
9531+
}
9532+
93589533
/* Install MCP configs for editor-based agents (Zed, KiloCode, VS Code, OpenClaw). */
93599534
static void install_editor_agent_configs(const cbm_detected_agents_t *agents, const char *home,
93609535
const char *binary_path, bool force, bool dry_run) {
@@ -9617,11 +9792,32 @@ static void install_additional_agent_configs(const cbm_detected_agents_t *agents
96179792
if (agents->openhands) {
96189793
char cp[CLI_BUF_1K];
96199794
char skills_dir[CLI_BUF_1K];
9795+
char settings_path[CLI_BUF_1K];
9796+
char profiles_dir[CLI_BUF_1K];
96209797
snprintf(cp, sizeof(cp), "%s/.openhands/mcp.json", home);
96219798
snprintf(skills_dir, sizeof(skills_dir), "%s/.agents/skills", home);
9799+
snprintf(settings_path, sizeof(settings_path), "%s/.openhands/settings.json", home);
9800+
snprintf(profiles_dir, sizeof(profiles_dir), "%s/.openhands/agent-profiles", home);
96229801
install_generic_agent_config("OpenHands", binary_path, cp, NULL, dry_run,
96239802
cbm_install_editor_mcp);
96249803
install_agent_skill("OpenHands", skills_dir, force, dry_run);
9804+
/* #1826: the mcpServers-shaped mcp.json above is not enough — OpenHands
9805+
* only loads a server registered under settings.json -> mcp_config, and
9806+
* only for agent profiles that reference it. agent-profiles/ is never
9807+
* invented; a missing directory means nothing to register into yet. */
9808+
if (g_install_plan) {
9809+
plan_record("OpenHands", "mcp_config", settings_path);
9810+
} else {
9811+
if (!dry_run) {
9812+
if (!prepare_config_parent(settings_path) ||
9813+
cbm_upsert_openhands_settings_mcp(binary_path, settings_path) != CLI_OK) {
9814+
record_agent_config_error(false, "OpenHands", "settings_mcp_install",
9815+
settings_path);
9816+
}
9817+
}
9818+
printf(" settings mcp_config: %s\n", settings_path);
9819+
openhands_update_profile_refs(profiles_dir, true, dry_run);
9820+
}
96259821
}
96269822
if (agents->augment) {
96279823
char cp[CLI_BUF_1K];
@@ -11877,11 +12073,21 @@ static void uninstall_additional_agents(const cbm_detected_agents_t *agents, con
1187712073
if (agents->openhands) {
1187812074
char cp[CLI_BUF_1K];
1187912075
char skills_dir[CLI_BUF_1K];
12076+
char settings_path[CLI_BUF_1K];
12077+
char profiles_dir[CLI_BUF_1K];
1188012078
snprintf(cp, sizeof(cp), "%s/.openhands/mcp.json", home);
1188112079
snprintf(skills_dir, sizeof(skills_dir), "%s/.agents/skills", home);
12080+
snprintf(settings_path, sizeof(settings_path), "%s/.openhands/settings.json", home);
12081+
snprintf(profiles_dir, sizeof(profiles_dir), "%s/.openhands/agent-profiles", home);
1188212082
uninstall_agent_mcp_instr((mcp_uninstall_args_t){"OpenHands", cp, NULL}, dry_run,
1188312083
cbm_remove_editor_mcp_owned);
1188412084
printf(" removed %d skill(s)\n", cbm_remove_skills(skills_dir, dry_run));
12085+
/* #1826 counterpart: undo the settings.json registration and every
12086+
* agent profile's mcp_server_refs entry the install above added. */
12087+
if (!dry_run && cbm_remove_openhands_settings_mcp(settings_path) != CLI_OK) {
12088+
record_agent_config_error(true, "OpenHands", "settings_mcp_uninstall", settings_path);
12089+
}
12090+
openhands_update_profile_refs(profiles_dir, false, dry_run);
1188512091
}
1188612092
if (agents->augment) {
1188712093
char cp[CLI_BUF_1K];

‎src/cli/config_json_like.c‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,8 +1223,19 @@ static int jl_make_insertion(const char *text, size_t length, size_t object_star
12231223
return 0;
12241224
}
12251225

1226-
if (object->close_pos == gap_start ||
1227-
!jl_is_space((unsigned char)text[object->close_pos - 1U])) {
1226+
/* Zero-width insertion right at close_pos: whatever byte already sits at
1227+
* close_pos - 1 is untouched and stays in the output. When the original
1228+
* had a real gap there (close_pos != gap_start, e.g. "[ \"a\" ]"), that
1229+
* preserved byte already supplies a separator on the leading side, so
1230+
* mirroring it on the trailing side (space before the bracket) matches
1231+
* the array's own loose style. The fully tight case ("[\"a\"]", nothing
1232+
* at all between the last value and the bracket) has no such byte to
1233+
* lean on: manufacture ONE space so `,new` reads `, new` — the
1234+
* comma-spacing convention every other insertion path uses — and add
1235+
* nothing after, so `new]` stays `new]` rather than gaining a trailing
1236+
* space the original never had (#1826 byte-for-byte round-trip). */
1237+
bool tight = object->close_pos == gap_start;
1238+
if (tight || !jl_is_space((unsigned char)text[object->close_pos - 1U])) {
12281239
if (jl_buffer_char(insertion, ' ') != 0) {
12291240
return -1;
12301241
}
@@ -1235,7 +1246,7 @@ static int jl_make_insertion(const char *text, size_t length, size_t object_star
12351246
if (object->trailing_comma && jl_buffer_char(insertion, ',') != 0) {
12361247
return -1;
12371248
}
1238-
return jl_buffer_char(insertion, ' ');
1249+
return tight ? 0 : jl_buffer_char(insertion, ' ');
12391250
}
12401251

12411252
static int jl_apply_edits(const char *source, size_t source_length, jl_edit_t *edits,
@@ -2411,6 +2422,13 @@ int cbm_json_like_remove_entry_if_unchanged(const char *file_path, const char *c
24112422
expected_length);
24122423
}
24132424

2425+
/* True when [start, end) is the bare 4-byte literal token null (never a
2426+
* quoted "null" string, which is 6 bytes with the quotes) — the documented
2427+
* OpenHands "no list yet" shape for mcp_server_refs (#1826). */
2428+
static bool jl_is_null_literal(const char *text, size_t start, size_t end) {
2429+
return end - start == 4U && memcmp(text + start, "null", 4U) == 0;
2430+
}
2431+
24142432
int cbm_json_like_add_unique_string_at_path(const char *file_path, const char *const *object_path,
24152433
size_t path_len, const char *array_key,
24162434
const char *string_value) {
@@ -2502,6 +2520,24 @@ int cbm_json_like_add_unique_string_at_path(const char *file_path, const char *c
25022520
result = jl_insert_member(source, source_length, object_start, &object, object_path,
25032521
path_len, SIZE_MAX, array_key, array_json.data, array_json.length,
25042522
&updated, &updated_length);
2523+
} else if (jl_is_null_literal(source, object.match.value_start, object.match.value_end)) {
2524+
/* OpenHands profiles ship `"mcp_server_refs": null` as the documented
2525+
* "no list yet" shape (#1826). Splice the built one-element array over
2526+
* the bare null token in place, preserving every other byte (trailing
2527+
* comments included) the way the object-member and array-element
2528+
* edits below already do for their own value spans. */
2529+
size_t head = object.match.value_start;
2530+
size_t tail = object.match.value_end;
2531+
updated_length = head + array_json.length + (source_length - tail);
2532+
updated = (char *)malloc(updated_length + 1U);
2533+
if (!updated) {
2534+
result = -1;
2535+
} else {
2536+
memcpy(updated, source, head);
2537+
memcpy(updated + head, array_json.data, array_json.length);
2538+
memcpy(updated + head + array_json.length, source + tail, source_length - tail);
2539+
updated[updated_length] = '\0';
2540+
}
25052541
} else if (source[object.match.value_start] != '[') {
25062542
result = -1;
25072543
} else {
@@ -2764,6 +2800,24 @@ static int jl_decode_field_string(const char *text, size_t start, size_t end,
27642800
}
27652801
return jl_decode_string_value(text, start, end, value_out) == 0 ? 0 : 1;
27662802
}
2803+
if (shape == CBM_JSON_LIKE_VALUE_LITERAL) {
2804+
/* The value's token boundaries already exclude surrounding trivia
2805+
* (jl_parse_value advances pos past exactly the token). Copy the raw
2806+
* bytes verbatim so a quoted "true" never equals the bare literal
2807+
* true, and the caller's expected_string comparison decides match. */
2808+
if (start >= end) {
2809+
return 1;
2810+
}
2811+
size_t length = end - start;
2812+
char *copy = (char *)malloc(length + 1U);
2813+
if (!copy) {
2814+
return 1;
2815+
}
2816+
memcpy(copy, text + start, length);
2817+
copy[length] = '\0';
2818+
*value_out = copy;
2819+
return 0;
2820+
}
27672821
if (shape != CBM_JSON_LIKE_VALUE_SINGLE_STRING_ARRAY || start >= end || text[start] != '[') {
27682822
return 1;
27692823
}
@@ -2832,11 +2886,13 @@ int cbm_json_like_match_object_entry(const char *document, size_t document_lengt
28322886
size_t capture_count = 0U;
28332887
for (size_t i = 0U; i < field_count; ++i) {
28342888
if (!fields[i].key || fields[i].key[0] == '\0' ||
2835-
fields[i].shape > CBM_JSON_LIKE_VALUE_SINGLE_STRING_ARRAY ||
2889+
fields[i].shape > CBM_JSON_LIKE_VALUE_LITERAL ||
28362890
(fields[i].flags &
28372891
~(CBM_JSON_LIKE_FIELD_REQUIRED | CBM_JSON_LIKE_FIELD_CAPTURE_STRING)) != 0U ||
28382892
((fields[i].flags & CBM_JSON_LIKE_FIELD_CAPTURE_STRING) != 0U &&
2839-
fields[i].shape == CBM_JSON_LIKE_VALUE_EMPTY_ARRAY)) {
2893+
(fields[i].shape == CBM_JSON_LIKE_VALUE_EMPTY_ARRAY ||
2894+
fields[i].shape == CBM_JSON_LIKE_VALUE_LITERAL)) ||
2895+
(fields[i].shape == CBM_JSON_LIKE_VALUE_LITERAL && !fields[i].expected_string)) {
28402896
return -1;
28412897
}
28422898
capture_count += (fields[i].flags & CBM_JSON_LIKE_FIELD_CAPTURE_STRING) != 0U ? 1U : 0U;

‎src/cli/config_json_like.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ typedef enum {
4747
CBM_JSON_LIKE_VALUE_STRING,
4848
CBM_JSON_LIKE_VALUE_EMPTY_ARRAY,
4949
CBM_JSON_LIKE_VALUE_SINGLE_STRING_ARRAY,
50+
/* An exact bare token (true/false/null/a number) matched byte-for-byte
51+
* against expected_string. Requires expected_string and may not be
52+
* combined with CBM_JSON_LIKE_FIELD_CAPTURE_STRING — a fixed token has
53+
* nothing meaningful to capture for the caller. */
54+
CBM_JSON_LIKE_VALUE_LITERAL,
5055
} cbm_json_like_value_shape_t;
5156

5257
enum {

0 commit comments

Comments
 (0)