Insert or leave alone: an already-owned entry (exact match, or annotated * with extra keys the client added) needs no write. A same-named entry that * does not match our shape is left untouched and reported as an error rather * than overwritten, matching cbm_upsert_json_named_mcp's fail-closed rule for * every other editor client. */
| 2538 | * than overwritten, matching cbm_upsert_json_named_mcp's fail-closed rule for |
| 2539 | * every other editor client. */ |
| 2540 | static int cbm_upsert_openhands_settings_mcp(const char *binary_path, const char *settings_path) { |
| 2541 | if (!binary_path || !settings_path) { |
| 2542 | return CLI_ERR; |
| 2543 | } |
| 2544 | static const char *const path[] = {"mcp_config"}; |
| 2545 | char *document = NULL; |
| 2546 | size_t document_length = 0U; |
| 2547 | int read_result = cbm_json_like_read_document(settings_path, &document, &document_length); |
| 2548 | if (read_result < 0) { |
| 2549 | return CLI_ERR; |
| 2550 | } |
| 2551 | if (read_result == 0) { |
| 2552 | cbm_json_like_object_field_t fields[3]; |
| 2553 | size_t field_count = cbm_openhands_ownership_fields(fields); |
| 2554 | char *command = NULL; |
| 2555 | int ownership = cbm_json_like_match_object_entry(document, document_length, path, 1U, |
| 2556 | CBM_DEFAULT_MCP_SERVER_NAME, fields, |
| 2557 | field_count, &command); |
| 2558 | free(command); |
| 2559 | if (ownership == CBM_JSON_LIKE_OBJECT_MATCH || |
| 2560 | ownership == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS) { |
| 2561 | free(document); |
| 2562 | return CLI_OK; |
| 2563 | } |
| 2564 | if (ownership != CBM_JSON_LIKE_OBJECT_MISSING) { |
| 2565 | free(document); |
| 2566 | return CLI_ERR; |
| 2567 | } |
| 2568 | } |
| 2569 | char *entry = cbm_build_openhands_mcp_entry(binary_path); |
| 2570 | if (!entry) { |
| 2571 | free(document); |
| 2572 | return CLI_ERR; |
| 2573 | } |
| 2574 | int edit_result = cbm_json_like_upsert_entry_if_unchanged( |
| 2575 | settings_path, path, 1U, CBM_DEFAULT_MCP_SERVER_NAME, entry, |
| 2576 | read_result == 1 ? NULL : document, document_length); |
| 2577 | free(entry); |
| 2578 | free(document); |
| 2579 | return edit_result == 0 ? CLI_OK : CLI_ERR; |
| 2580 | } |
| 2581 | |
| 2582 | /* Remove only an entry that is still recognisably ours (an annotated entry is |
| 2583 | * left in place and reported, same rule as insertion above); a missing file, |
no test coverage detected