Skip to content

Commit 96c2fef

Browse files
authored
Merge pull request #2641 from crisacm/fix/2204-opencode-extension-untouched
fix(cli): leave a user-owned OpenCode module untouched instead of appending the block
2 parents 6e0c59f + 8d9a8e3 commit 96c2fef

5 files changed

Lines changed: 273 additions & 13 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9586,6 +9586,35 @@ static void install_devin_durable_context(const cbm_agent_registry_context_t *re
95869586
static void install_generated_client_extension(const char *label, const char *path,
95879587
const char *binary_path,
95889588
char *(*generate)(const char *), bool dry_run) {
9589+
/* #2204: a module that already carries content but none of our markers is
9590+
* the user's. The generated header promises "remove the markers to take
9591+
* ownership", and appending our prelude to a foreign module produces a file
9592+
* the client cannot even parse — the prelude redeclares the user's own
9593+
* import and const names, and both sides declare a default export. Probe
9594+
* ownership before generating so the preview names the refusal instead of
9595+
* promising a write the run will not make. A probe failure (-1) falls
9596+
* through to the write, which reports it through the usual error path. */
9597+
bool foreign =
9598+
cbm_text_managed_block_status(path, CBM_ADAPTER_MARKER_START, CBM_ADAPTER_MARKER_END) == 1;
9599+
if (foreign) {
9600+
/* The --plan receipt (#388) lists only writes the run will make, and
9601+
* its stdout carries the receipt JSON, so plan mode records nothing
9602+
* and prints nothing here. The dry run names the skip instead. */
9603+
if (g_install_plan) {
9604+
return;
9605+
}
9606+
if (dry_run) {
9607+
printf(" extension: %s would be skipped — the file there is not ours (no "
9608+
"codebase-memory-mcp markers), so it is left untouched; delete it or add the "
9609+
"markers to let the installer manage it\n",
9610+
path);
9611+
} else {
9612+
printf(" extension: %s left untouched — the file there is not ours (no "
9613+
"codebase-memory-mcp markers)\n",
9614+
path);
9615+
}
9616+
return;
9617+
}
95899618
if (g_install_plan) {
95909619
plan_record(label, "extension", path);
95919620
return;

‎src/cli/config_text_edit.c‎

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,42 @@ static int text_validate_markers(const char *begin_marker, const char *end_marke
10201020
return TEXT_OK;
10211021
}
10221022

1023+
/* A managed document as read from disk, with our block located in it. */
1024+
typedef struct {
1025+
char *data;
1026+
size_t len;
1027+
size_t bom_len;
1028+
text_file_snapshot_t snapshot;
1029+
text_managed_region_t region;
1030+
} text_managed_document_t;
1031+
1032+
/* The read-validate-scan step shared by the upsert and the read-only ownership
1033+
* probe, so both judge a file by the same rules. An absent file reads as an
1034+
* empty document. On success the caller owns doc->data and releases it; on
1035+
* failure nothing is left allocated. */
1036+
static int text_read_managed_document(const char *file_path, const char *begin_marker,
1037+
size_t begin_len, const char *end_marker, size_t end_len,
1038+
text_managed_document_t *doc) {
1039+
doc->data = NULL;
1040+
doc->len = 0U;
1041+
doc->bom_len = 0U;
1042+
int status = text_read_file(file_path, &doc->data, &doc->len, &doc->snapshot);
1043+
if (status == TEXT_OK) {
1044+
status = text_validate_bytes(doc->data, doc->len, 1);
1045+
}
1046+
if (status == TEXT_OK) {
1047+
doc->bom_len = text_bom_length(doc->data, doc->len);
1048+
status = text_scan_managed_region(doc->data, doc->len, doc->bom_len, begin_marker,
1049+
begin_len, end_marker, end_len, &doc->region);
1050+
}
1051+
if (status != TEXT_OK) {
1052+
free(doc->data);
1053+
doc->data = NULL;
1054+
return TEXT_ERROR;
1055+
}
1056+
return TEXT_OK;
1057+
}
1058+
10231059
static int text_upsert_managed_block(const char *file_path, const char *begin_marker,
10241060
const char *end_marker, const char *owned_content,
10251061
size_t max_document_bytes) {
@@ -1036,21 +1072,16 @@ static int text_upsert_managed_block(const char *file_path, const char *begin_ma
10361072
return TEXT_ERROR;
10371073
}
10381074

1039-
char *old_data = NULL;
1040-
size_t old_len = 0U;
1041-
text_file_snapshot_t snapshot;
1042-
if (text_read_file(file_path, &old_data, &old_len, &snapshot) != TEXT_OK ||
1043-
text_validate_bytes(old_data, old_len, 1) != TEXT_OK) {
1044-
free(old_data);
1045-
return TEXT_ERROR;
1046-
}
1047-
size_t bom_len = text_bom_length(old_data, old_len);
1048-
text_managed_region_t region;
1049-
if (text_scan_managed_region(old_data, old_len, bom_len, begin_marker, begin_len, end_marker,
1050-
end_len, &region) != TEXT_OK) {
1051-
free(old_data);
1075+
text_managed_document_t doc;
1076+
int read_status =
1077+
text_read_managed_document(file_path, begin_marker, begin_len, end_marker, end_len, &doc);
1078+
if (read_status != TEXT_OK) {
10521079
return TEXT_ERROR;
10531080
}
1081+
char *old_data = doc.data;
1082+
size_t old_len = doc.len;
1083+
text_file_snapshot_t snapshot = doc.snapshot;
1084+
text_managed_region_t region = doc.region;
10541085

10551086
text_buffer_t updated = {0};
10561087
int result = TEXT_ERROR;
@@ -1070,6 +1101,7 @@ static int text_upsert_managed_block(const char *file_path, const char *begin_ma
10701101
&snapshot);
10711102
}
10721103
} else {
1104+
size_t bom_len = doc.bom_len;
10731105
const char *eol = NULL;
10741106
size_t eol_len = 0U;
10751107
text_detect_eol(old_data + bom_len, old_len - bom_len, &eol, &eol_len);
@@ -1105,6 +1137,34 @@ int cbm_text_upsert_managed_block_limited(const char *file_path, const char *beg
11051137
max_document_bytes);
11061138
}
11071139

1140+
int cbm_text_managed_block_status(const char *file_path, const char *begin_marker,
1141+
const char *end_marker) {
1142+
size_t begin_len = 0U;
1143+
size_t end_len = 0U;
1144+
if (!text_valid_path(file_path) ||
1145+
text_validate_markers(begin_marker, end_marker, &begin_len, &end_len) != TEXT_OK) {
1146+
return TEXT_ERROR;
1147+
}
1148+
text_managed_document_t doc;
1149+
int read_status =
1150+
text_read_managed_document(file_path, begin_marker, begin_len, end_marker, end_len, &doc);
1151+
if (read_status != TEXT_OK) {
1152+
return TEXT_ERROR;
1153+
}
1154+
int result = TEXT_OK;
1155+
if (!doc.region.present) {
1156+
for (size_t pos = doc.bom_len; pos < doc.len; pos++) {
1157+
char byte = doc.data[pos];
1158+
if (byte != ' ' && byte != '\t' && byte != '\r' && byte != '\n') {
1159+
result = TEXT_UNOWNED;
1160+
break;
1161+
}
1162+
}
1163+
}
1164+
free(doc.data);
1165+
return result;
1166+
}
1167+
11081168
int cbm_text_remove_managed_block(const char *file_path, const char *begin_marker,
11091169
const char *end_marker) {
11101170
size_t begin_len = 0U;

‎src/cli/config_text_edit.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ int cbm_text_upsert_managed_block(const char *file_path, const char *begin_marke
1111
int cbm_text_upsert_managed_block_limited(const char *file_path, const char *begin_marker,
1212
const char *end_marker, const char *owned_content,
1313
size_t max_document_bytes);
14+
/* Read-only ownership probe for a managed block, used before writing a file a
15+
* client auto-loads as a module. Returns 0 when the block may be written (no
16+
* file, a file whose content is only whitespace, or a file already carrying our
17+
* markers), 1 when the file carries content but none of our markers — the
18+
* caller must leave the user's file alone and say so rather than append to it,
19+
* because the generated block redeclares module-level names a hand-ported
20+
* module also declares (#2204) — and -1 for unsafe state or I/O failure. Never
21+
* writes, so a dry run may call it. */
22+
int cbm_text_managed_block_status(const char *file_path, const char *begin_marker,
23+
const char *end_marker);
1424
int cbm_text_remove_managed_block(const char *file_path, const char *begin_marker,
1525
const char *end_marker);
1626

‎tests/test_cli.c‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9574,6 +9574,127 @@ TEST(cli_dry_run_predicts_refused_hook_script_issue1387) {
95749574
FAIL("dry-run must predict a refused hook-script rewrite (#1387)");
95759575
PASS();
95769576
}
9577+
9578+
/* #2204: the OpenCode adapter is an auto-loaded module, and the generated block
9579+
* tells readers to "remove the markers to take ownership". A marker-less file
9580+
* with content is the user's: appending the block to a hand-ported module
9581+
* produces a file the client cannot even parse (the prelude redeclares the
9582+
* user's own import and const names, and both sides declare a default export).
9583+
* The installer must leave that file alone, and the dry run must say so instead
9584+
* of promising a write it will not make. A dry run never writes, so the byte
9585+
* check below follows a REAL install: that is the run that appended. */
9586+
TEST(cli_extension_leaves_user_owned_module_untouched_issue2204) {
9587+
char home[256];
9588+
char bin[320];
9589+
if (!client_detect_fixture(home, sizeof(home), bin, sizeof(bin)))
9590+
FAIL("cbm_mkdtemp failed");
9591+
9592+
/* An `opencode` CLI on the isolated PATH marks OpenCode as installed. */
9593+
char exe[512];
9594+
snprintf(exe, sizeof(exe), "%s/opencode", bin);
9595+
if (write_test_file(exe, "#!/bin/sh\nexit 0\n") != 0 || chmod(exe, 0755) != 0)
9596+
FAIL("opencode fixture failed");
9597+
9598+
char plugins_dir[512];
9599+
snprintf(plugins_dir, sizeof(plugins_dir), "%s/.config/opencode/plugins", home);
9600+
test_mkdirp(plugins_dir);
9601+
char plugin_path[768];
9602+
snprintf(plugin_path, sizeof(plugin_path), "%s/cbm-augment.ts", plugins_dir);
9603+
const char foreign[] = "import { spawn } from 'node:child_process';\n"
9604+
"const BIN = '/opt/other/codebase-memory-mcp';\n"
9605+
"export default { id: 'codebase-memory-augment', setup() {} };\n";
9606+
if (write_test_file(plugin_path, foreign) != 0)
9607+
FAIL("foreign module fixture failed");
9608+
9609+
client_detect_env_t saved = client_detect_env_isolate(bin);
9610+
FILE *capture = tmpfile();
9611+
int saved_stdout = capture ? dup(STDOUT_FILENO) : -1;
9612+
bool redirected = false;
9613+
if (capture && saved_stdout >= 0) {
9614+
fflush(stdout);
9615+
redirected = dup2(fileno(capture), STDOUT_FILENO) >= 0;
9616+
}
9617+
long plan_output_start = -1;
9618+
long plan_output_end = -1;
9619+
char *plan = NULL;
9620+
if (redirected) {
9621+
(void)cbm_install_agent_configs(home, "/opt/other/codebase-memory-mcp", false, true);
9622+
fflush(stdout);
9623+
/* The --plan receipt (#388) replays the same path with writes off. It
9624+
* must not list a module the run leaves alone, and it must print
9625+
* nothing: stdout carries the receipt JSON. */
9626+
plan_output_start = (long)lseek(STDOUT_FILENO, 0, SEEK_CUR);
9627+
plan = cbm_build_install_plan_json(home, "/opt/other/codebase-memory-mcp");
9628+
fflush(stdout);
9629+
/* The real install, the run #2204 is about. */
9630+
plan_output_end = (long)lseek(STDOUT_FILENO, 0, SEEK_CUR);
9631+
(void)cbm_install_agent_configs(home, "/opt/other/codebase-memory-mcp", false, false);
9632+
fflush(stdout);
9633+
(void)dup2(saved_stdout, STDOUT_FILENO);
9634+
}
9635+
if (saved_stdout >= 0)
9636+
close(saved_stdout);
9637+
9638+
char bytes[512] = {0};
9639+
FILE *file = cbm_fopen(plugin_path, "rb");
9640+
size_t got = file ? fread(bytes, 1U, sizeof(bytes) - 1U, file) : 0U;
9641+
if (file)
9642+
fclose(file);
9643+
9644+
/* Control: with the user's module gone the receipt does list the path, so
9645+
* the omission above cannot pass just because OpenCode went undetected. */
9646+
char *control_plan = NULL;
9647+
if (unlink(plugin_path) == 0)
9648+
control_plan = cbm_build_install_plan_json(home, "/opt/other/codebase-memory-mcp");
9649+
client_detect_env_restore(&saved);
9650+
9651+
char output[16384] = {0};
9652+
if (capture) {
9653+
rewind(capture);
9654+
size_t count = fread(output, 1, sizeof(output) - 1U, capture);
9655+
output[count] = '\0';
9656+
fclose(capture);
9657+
}
9658+
9659+
/* Each phase owns its slice of stdout: [0, start) dry run, [start, end)
9660+
* plan mode, [end, len) real install, so no phase passes on another's
9661+
* line. Checked back to front, cutting the buffer at each boundary. */
9662+
bool sliced = plan_output_start >= 0 && plan_output_end >= plan_output_start &&
9663+
(size_t)plan_output_end <= strlen(output);
9664+
bool reported =
9665+
sliced && strstr(output + plan_output_end, "cbm-augment.ts left untouched") != NULL;
9666+
if (sliced)
9667+
output[plan_output_end] = '\0';
9668+
bool plan_silent = sliced && strstr(output + plan_output_start, "cbm-augment.ts") == NULL;
9669+
if (sliced)
9670+
output[plan_output_start] = '\0';
9671+
bool warned =
9672+
sliced && strstr(output, "cbm-augment.ts") != NULL && strstr(output, "not ours") != NULL;
9673+
bool untouched = got == sizeof(foreign) - 1U && memcmp(bytes, foreign, got) == 0;
9674+
bool plan_omits = plan && strstr(plan, "cbm-augment.ts") == NULL;
9675+
bool control_lists = control_plan && strstr(control_plan, "cbm-augment.ts") != NULL;
9676+
free(plan);
9677+
free(control_plan);
9678+
9679+
test_rmdir_r(home);
9680+
if (!redirected)
9681+
FAIL("stdout capture failed");
9682+
if (!sliced)
9683+
FAIL("stdout capture lost a phase boundary");
9684+
if (!warned)
9685+
FAIL("dry-run must predict an untouched user-owned OpenCode module (#2204)");
9686+
if (!untouched)
9687+
FAIL("a real install must leave the user-owned OpenCode module byte-identical (#2204)");
9688+
if (!reported)
9689+
FAIL("a real install must report the user-owned OpenCode module as left untouched (#2204)");
9690+
if (!plan_omits)
9691+
FAIL("the install plan must not list a module the run leaves untouched (#2204)");
9692+
if (!plan_silent)
9693+
FAIL("plan mode must keep stdout for the receipt JSON (#2204)");
9694+
if (!control_lists)
9695+
FAIL("control: the install plan must list the module once it is not user-owned");
9696+
PASS();
9697+
}
95779698
#endif
95789699

95799700
TEST(cli_agent_client_registry_routes_plan_install_and_uninstall) {
@@ -19481,6 +19602,7 @@ SUITE(cli) {
1948119602
#endif
1948219603
#ifndef _WIN32
1948319604
RUN_TEST(cli_dry_run_predicts_refused_hook_script_issue1387);
19605+
RUN_TEST(cli_extension_leaves_user_owned_module_untouched_issue2204);
1948419606
#endif
1948519607
RUN_TEST(cli_agent_client_registry_routes_plan_install_and_uninstall);
1948619608
RUN_TEST(cli_registry_installs_kimi_rovo_amp_durable_context);

‎tests/test_config_text_edit.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,13 +725,52 @@ TEST(config_text_write_owned_document_if_unchanged_rejects_stale_snapshot) {
725725
PASS();
726726
}
727727

728+
/* #2204: the generated OpenCode module must not be appended to a file the user
729+
* owns. The probe separates "may be written" (absent, whitespace-only, or
730+
* already carrying our markers) from "the user's file" (content, no markers) so
731+
* the installer can leave it alone instead of producing an unparseable hybrid. */
732+
TEST(config_text_managed_block_status_reports_foreign_module) {
733+
char dir[CTE_PATH_CAP];
734+
char path[CTE_PATH_CAP];
735+
const char whitespace[] = " \n\t\n";
736+
const char foreign[] = "import { spawn } from 'node:child_process';\n"
737+
"export default { id: 'codebase-memory-augment', setup() {} };\n";
738+
const char unbalanced[] = CTE_BEGIN "\nowned\n";
739+
ASSERT_EQ(cte_fixture(dir, sizeof(dir), path, sizeof(path)), 0);
740+
741+
/* No file yet, an empty file, and a whitespace-only file are all writable. */
742+
ASSERT_EQ(cbm_text_managed_block_status(path, CTE_BEGIN, CTE_END), 0);
743+
ASSERT_EQ(cte_write_bytes(path, "", 0U), 0);
744+
ASSERT_EQ(cbm_text_managed_block_status(path, CTE_BEGIN, CTE_END), 0);
745+
ASSERT_EQ(cte_write_bytes(path, whitespace, sizeof(whitespace) - 1U), 0);
746+
ASSERT_EQ(cbm_text_managed_block_status(path, CTE_BEGIN, CTE_END), 0);
747+
748+
/* Our own block refreshes in place, so it is writable too. */
749+
ASSERT_EQ(cbm_text_upsert_managed_block(path, CTE_BEGIN, CTE_END, "owned"), 0);
750+
ASSERT_EQ(cbm_text_managed_block_status(path, CTE_BEGIN, CTE_END), 0);
751+
752+
/* A hand-ported module with no markers is the user's: refuse, never append. */
753+
ASSERT_EQ(cte_write_bytes(path, foreign, sizeof(foreign) - 1U), 0);
754+
ASSERT_EQ(cbm_text_managed_block_status(path, CTE_BEGIN, CTE_END), 1);
755+
756+
/* Unbalanced markers and malformed marker arguments are unsafe states. */
757+
ASSERT_EQ(cte_write_bytes(path, unbalanced, sizeof(unbalanced) - 1U), 0);
758+
ASSERT_EQ(cbm_text_managed_block_status(path, CTE_BEGIN, CTE_END), -1);
759+
ASSERT_EQ(cbm_text_managed_block_status(path, "same", "same"), -1);
760+
ASSERT_EQ(cbm_text_managed_block_status(NULL, CTE_BEGIN, CTE_END), -1);
761+
762+
th_cleanup(dir);
763+
PASS();
764+
}
765+
728766
SUITE(config_text_edit) {
729767
RUN_TEST(config_text_managed_insert_preserves_bom_comments_and_is_idempotent);
730768
RUN_TEST(config_text_managed_replace_preserves_crlf_surroundings);
731769
RUN_TEST(config_text_managed_no_final_newline_round_trip);
732770
RUN_TEST(config_text_managed_remove_preserves_user_bytes);
733771
RUN_TEST(config_text_managed_malformed_markers_fail_closed);
734772
RUN_TEST(config_text_managed_rejects_unsafe_markers_and_owned_content);
773+
RUN_TEST(config_text_managed_block_status_reports_foreign_module);
735774
RUN_TEST(config_text_owned_document_write_remove_exact_only);
736775
RUN_TEST(config_text_owned_document_migrates_exact_releases_only);
737776
RUN_TEST(config_text_rejects_invalid_utf8_and_controls);

0 commit comments

Comments
 (0)