@@ -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
95799700TEST(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);
0 commit comments