Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(win): reconcile the C suite with the managed-launcher contract

Martin Vogelf51745533cf5
diff --git a/src/cli/activation_transaction.c b/src/cli/activation_transaction.c
index 269acacaa..2445703f6 100644
--- a/src/cli/activation_transaction.c
+++ b/src/cli/activation_transaction.c
@@ -17,6 +17,7 @@
#define WIN32_LEAN_AND_MEAN
#endif
#include <aclapi.h>
+#include <sddl.h>
#include <windows.h>
#else
#include <fcntl.h>
@@ -78,6 +79,37 @@ struct cbm_activation_transaction {
#endif
};
+/* The transaction's security predicates refuse by returning false without a
+ * usable OS last-error, so every caller used to report the same blind
+ * "status -3, os 0" for five distinct refusal classes (ancestor open,
+ * owner, DACL, target snapshot, staged create). The first predicate that
+ * refuses records what refused here; callers append the note to their error
+ * line. First-wins so the deepest (root) refusal survives the unwind.
+ * Single-threaded by contract: CLI activation runs on one thread. */
+static char g_activation_refusal_note[512];
+
+/* The validated object's path, set by each validator for the duration of its
+ * predicate calls so the note can say WHAT was refused, not just why. Points
+ * into the caller's storage; never dereferenced after the validator returns. */
+static const char *g_activation_refusal_object = NULL;
+
+static void activation_refusal_clear(void) {
+ g_activation_refusal_note[0] = '\0';
+}
+
+static void activation_note_refusal(const char *predicate, unsigned long os_error) {
+ if (g_activation_refusal_note[0] != '\0') {
+ return;
+ }
+ (void)snprintf(g_activation_refusal_note, sizeof(g_activation_refusal_note), "%s (os %lu)%s%s",
+ predicate, os_error, g_activation_refusal_object ? " at " : "",
+ g_activation_refusal_object ? g_activation_refusal_object : "");
+}
+
+const char *cbm_activation_transaction_refusal_note(void) {
+ return g_activation_refusal_note;
+}
+
#ifdef _WIN32
typedef HANDLE activation_native_file_t;
#define ACTIVATION_INVALID_FILE INVALID_HANDLE_VALUE
@@ -382,6 +414,9 @@ static bool activation_windows_owner_is_trusted(HANDLE handle) {
bool trusted = result == ERROR_SUCCESS && owner && IsValidSid(owner) &&
(EqualSid(owner, user_sid) || IsWellKnownSid(owner, WinLocalSystemSid) ||
IsWellKnownSid(owner, WinBuiltinAdministratorsSid));
+ if (!trusted) {
+ activation_note_refusal("owner-not-trusted", result);
+ }
if (descriptor) {
(void)LocalFree(descriptor);
}
@@ -400,6 +435,9 @@ static bool activation_windows_owner_is_current(HANDLE handle) {
DWORD result = GetSecurityInfo(handle, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &owner, NULL,
NULL, NULL, &descriptor);
bool same = result == ERROR_SUCCESS && owner && IsValidSid(owner) && EqualSid(owner, user_sid);
+ if (!same) {
+ activation_note_refusal("owner-not-current-user", result);
+ }
if (descriptor) {
(void)LocalFree(descriptor);
}
@@ -482,9 +520,21 @@ static bool activation_windows_acl_check(HANDLE handle, DWORD tolerated_untruste
* launcher ancestry validators). */
IsWellKnownSid(sid, WinCreatorOwnerRightsSid));
if (!trusted) {
+ char label[128];
+ LPSTR sid_text = NULL;
+ (void)snprintf(label, sizeof(label), "acl-grants-cross-account-mutation to %s",
+ ConvertSidToStringSidA(sid, &sid_text) && sid_text ? sid_text
+ : "unparsable-sid");
+ if (sid_text) {
+ (void)LocalFree(sid_text);
+ }
+ activation_note_refusal(label, 0UL);
secure = false;
}
}
+ if (!secure) {
+ activation_note_refusal("acl-unreadable-or-malformed", result);
+ }
if (descriptor) {
(void)LocalFree(descriptor);
}
@@ -560,6 +610,7 @@ static HANDLE activation_windows_directory_open_no_reparse(const char *directory
CreateFileW(prefixed, FILE_READ_ATTRIBUTES | READ_CONTROL,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
+ DWORD open_error = handle == INVALID_HANDLE_VALUE ? GetLastError() : ERROR_SUCCESS;
prefixed[boundary + 4U] = saved;
BY_HANDLE_FILE_INFORMATION information;
bool valid = handle != INVALID_HANDLE_VALUE && GetFileType(handle) == FILE_TYPE_DISK &&
@@ -567,6 +618,10 @@ static HANDLE activation_windows_directory_open_no_reparse(const char *directory
(information.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 &&
(information.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0;
if (!valid) {
+ activation_note_refusal(handle == INVALID_HANDLE_VALUE
+ ? "ancestor-component-open"
+ : "ancestor-component-not-plain-directory",
+ open_error);
if (handle != INVALID_HANDLE_VALUE) {
(void)CloseHandle(handle);
}
@@ -592,6 +647,7 @@ static HANDLE activation_windows_directory_open_no_reparse(const char *directory
* copy's build-fingerprint validation. */
static bool activation_source_directory_secure(const char *directory,
activation_file_identity_t *identity_out) {
+ g_activation_refusal_object = directory;
HANDLE handle = activation_windows_directory_open_no_reparse(directory);
BY_HANDLE_FILE_INFORMATION information;
bool open_ok = handle != INVALID_HANDLE_VALUE &&
@@ -605,12 +661,14 @@ static bool activation_source_directory_secure(const char *directory,
if (handle != INVALID_HANDLE_VALUE) {
(void)CloseHandle(handle);
}
+ g_activation_refusal_object = NULL;
return acl_ok;
}
static bool activation_directory_secure(const char *directory, int *unused,
activation_file_identity_t *identity_out) {
(void)unused;
+ g_activation_refusal_object = directory;
HANDLE handle = activation_windows_directory_open_no_reparse(directory);
BY_HANDLE_FILE_INFORMATION information;
bool open_ok = handle != INVALID_HANDLE_VALUE &&
@@ -623,6 +681,7 @@ static bool activation_directory_secure(const char *directory, int *unused,
if (handle != INVALID_HANDLE_VALUE) {
(void)CloseHandle(handle);
}
+ g_activation_refusal_object = NULL;
return acl_ok;
}
@@ -853,6 +912,7 @@ static activation_create_status_t activation_private_file_create(
free(wide);
return ACTIVATION_CREATE_ERROR;
}
+ g_activation_refusal_object = path;
SetLastError(ERROR_SUCCESS);
HANDLE file = CreateFileW(wide, GENERIC_READ | GENERIC_WRITE | READ_CONTROL | DELETE,
FILE_SHARE_READ, &security.attributes, CREATE_NEW,
@@ -865,6 +925,11 @@ static activation_create_status_t activation_private_file_create(
activation_windows_owner_is_current(file) && activation_windows_acl_secure(file);
activation_windows_security_destroy(&security);
if (!valid) {
+ if (file == INVALID_HANDLE_VALUE && error != ERROR_FILE_EXISTS &&
+ error != ERROR_ALREADY_EXISTS) {
+ activation_note_refusal("staged-file-create", error);
+ }
+ g_activation_refusal_object = NULL;
if (file != INVALID_HANDLE_VALUE) {
(void)CloseHandle(file);
}
@@ -872,6 +937,7 @@ static activation_create_status_t activation_private_file_create(
? ACTIVATION_CREATE_EXISTS
: ACTIVATION_CREATE_ERROR;
}
+ g_activation_refusal_object = NULL;
*file_out = file;
return ACTIVATION_CREATE_OK;
#else
@@ -1171,6 +1237,7 @@ static cbm_activation_transaction_status_t activation_transaction_prepare(
const char *target_path, activation_action_t action,
cbm_activation_transaction_t **transaction_out) {
*transaction_out = NULL;
+ activation_refusal_clear();
cbm_activation_transaction_t *transaction = calloc(1, sizeof(*transaction));
if (!transaction) {
return CBM_ACTIVATION_TRANSACTION_NO_MEMORY;
@@ -1203,8 +1270,7 @@ static cbm_activation_transaction_status_t activation_transaction_prepare(
}
if (!activation_entry_snapshot(transaction, transaction->target_path, transaction->target_name,
&transaction->target_existed, &transaction->target_identity)) {
-#ifdef _WIN32
-#endif
+ activation_note_refusal("target-entry-snapshot", 0UL);
activation_transaction_destroy(transaction);
return CBM_ACTIVATION_TRANSACTION_IO;
}
diff --git a/src/cli/activation_transaction.h b/src/cli/activation_transaction.h
index 028b80a66..ac67a34be 100644
--- a/src/cli/activation_transaction.h
+++ b/src/cli/activation_transaction.h
@@ -85,4 +85,11 @@ const char *cbm_activation_transaction_deferred_path(
const char *cbm_activation_transaction_status_message(cbm_activation_transaction_status_t status);
+/* Which security predicate refused the most recent transaction, as
+ * "predicate (os N)", or "" when nothing refused since the last prepare.
+ * The predicates refuse without a usable OS last-error, so this is the only
+ * way a caller can say WHY staging failed. Reset by every prepare/stage
+ * entry; single-threaded like the rest of the transaction API. */
+const char *cbm_activation_transaction_refusal_note(void);
+
#endif /* CBM_ACTIVATION_TRANSACTION_H */
diff --git a/src/cli/cli.c b/src/cli/cli.c
index a9df19fd5..04013a669 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -410,6 +410,10 @@ void cbm_cli_set_activation_ops_for_test(const cbm_cli_activation_ops_t *ops) {
g_cli_activation_test_ops_set = true;
}
+bool cbm_cli_activation_test_ops_installed(void) {
+ return g_cli_activation_test_ops_set;
+}
+
void cbm_cli_set_activation_runtime_parent_for_test(const char *runtime_parent) {
g_cli_activation_runtime_parent_for_test = runtime_parent;
}
@@ -1276,7 +1280,7 @@ static int cli_activation_transaction_finalize_close(
return cli_activation_transaction_abort(transaction_io);
}
-#ifndef _WIN32
+#if !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API)
static void cli_activation_transaction_finalize_committed_or_fail_stop(
cbm_activation_transaction_t **transaction_io, const char *component) {
if (!transaction_io || !*transaction_io) {
@@ -9346,7 +9350,12 @@ int cbm_install_handle_existing_indexes(const char *home, bool reset, bool dry_r
/* ── Subcommand: install ──────────────────────────────────────── */
-#ifndef _WIN32
+/* The portable activation flow (self-path detection, simple-copy activate)
+ * serves every POSIX build, and Windows TEST builds only: there the C suite
+ * reaches it through the activation test seam to unit-cover the shared
+ * activation/agent-config semantics, while release Windows binaries compile
+ * exclusively the managed launcher transaction. */
+#if !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API)
/* Detect the running binary's path at runtime. Falls back to ~/.local/bin/. */
static void cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
buf[0] = '\0';
@@ -9372,7 +9381,7 @@ static void cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
#endif
}
}
-#endif
+#endif /* !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API) */
/* Build the agent.install.plan.v1 receipt (#388): a machine-readable list of
* the config / instruction / skill / agent / hook files `install` WOULD write, produced by
@@ -9494,7 +9503,7 @@ char *cbm_build_install_plan_json(const char *home, const char *binary_path) {
return cbm_build_install_plan_json_options(home, binary_path, false);
}
-#ifndef _WIN32
+#if !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API)
typedef struct {
const char *bin_target;
const char *bin_dir;
@@ -9615,7 +9624,7 @@ static int cli_install_activate(void *opaque) {
"install_transaction_finalize");
return CLI_OK;
}
-#endif
+#endif /* !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API) */
#ifdef _WIN32
typedef struct {
@@ -9759,8 +9768,9 @@ static bool cli_windows_stage_private_file(
* them into one blind "staging failed" with no OS error code. */
bool ready = status == CBM_ACTIVATION_TRANSACTION_OK && transaction;
if (!ready) {
- (void)fprintf(stderr, "error: staging transaction open failed (status %d, os %lu)\n",
- (int)status, (unsigned long)GetLastError());
+ const char *refusal = cbm_activation_transaction_refusal_note();
+ (void)fprintf(stderr, "error: staging transaction open failed (status %d, os %lu%s%s)\n",
+ (int)status, (unsigned long)GetLastError(), refusal[0] ? ": " : "", refusal);
}
if (ready && !cli_activation_transaction_expected_build(transaction, &validator)) {
(void)fprintf(stderr, "error: staged copy build-identity validation failed (os %lu)\n",
@@ -10507,11 +10517,27 @@ int cbm_cmd_install(int argc, char **argv) {
printf("codebase-memory-mcp install %s\n\n", CBM_VERSION);
#ifdef _WIN32
- return cli_windows_managed_install(home, requested_bin_dir, dry_run, force, reset_indexes,
- skip_config);
+ /* Fail-closed dispatch polarity: the managed launcher transaction is the
+ * ONLY Windows install path a release binary contains. Test builds
+ * additionally compile the portable flow below, reachable solely through
+ * the activation test seam and loudly announced, so the shared
+ * activation/agent-config semantics stay unit-covered while a misroute
+ * can never pass silently. The real managed transaction has its own
+ * end-to-end gates (launcher guard, agent-config smoke, smoke-install). */
+#ifdef CBM_CLI_ENABLE_TEST_API
+ if (g_cli_activation_test_ops_set) {
+ (void)fprintf(stderr,
+ "*** cbm test seam: portable install flow engaged; the managed Windows "
+ "install transaction is bypassed (test builds only) ***\n");
+ } else
+#endif
+ {
+ return cli_windows_managed_install(home, requested_bin_dir, dry_run, force, reset_indexes,
+ skip_config);
+ }
#endif
-#ifndef _WIN32
+#if !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API)
char self_path[CLI_BUF_1K] = {0};
cbm_detect_self_path(self_path, sizeof(self_path), home);
@@ -10733,7 +10759,7 @@ int cbm_cmd_install(int argc, char **argv) {
printf("\n(dry-run — no files were modified)\n");
}
return 0;
-#endif
+#endif /* !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API) */
}
/* ── Subcommand: uninstall ────────────────────────────────────── */
@@ -11834,7 +11860,7 @@ static void uninstall_additional_agents(const cbm_detected_agents_t *agents, con
}
}
-#ifndef _WIN32
+#if !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API)
typedef struct {
const char *home;
const char *bin_path;
@@ -11901,7 +11927,7 @@ static int cli_uninstall_activate(void *opaque) {
}
return CLI_OK;
}
-#endif
+#endif /* !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API) */
#ifdef _WIN32
typedef struct {
@@ -12093,9 +12119,17 @@ int cbm_cmd_uninstall(int argc, char **argv) {
#ifdef _WIN32
/* A direct/package-manager payload is a one-shot portable instance. Fail
* before HOME/cache discovery, prompts, daemon IPC, or filesystem writes:
- * only the permanent launcher may authorize managed removal. */
- if (!cli_windows_require_managed_mutation(CBM_WINDOWS_LAUNCHER_ACTION_UNINSTALL)) {
- return CLI_TRUE;
+ * only the permanent launcher may authorize managed removal. Test builds
+ * additionally compile the portable flow below, reachable solely through
+ * the activation test seam (loud), so the shared removal semantics stay
+ * unit-covered; release binaries contain only the managed path. */
+#ifdef CBM_CLI_ENABLE_TEST_API
+ if (!g_cli_activation_test_ops_set)
+#endif
+ {
+ if (!cli_windows_require_managed_mutation(CBM_WINDOWS_LAUNCHER_ACTION_UNINSTALL)) {
+ return CLI_TRUE;
+ }
}
#endif
@@ -12108,15 +12142,24 @@ int cbm_cmd_uninstall(int argc, char **argv) {
printf("codebase-memory-mcp uninstall\n\n");
#ifdef _WIN32
- int windows_result = cli_windows_managed_uninstall(home, dry_run);
- if (windows_result == CLI_OK) {
- printf("\nUninstall complete. Please restart your coding-agent "
- "sessions to properly take this into account.\n");
+#ifdef CBM_CLI_ENABLE_TEST_API
+ if (g_cli_activation_test_ops_set) {
+ (void)fprintf(stderr,
+ "*** cbm test seam: portable uninstall flow engaged; the managed Windows "
+ "uninstall transaction is bypassed (test builds only) ***\n");
+ } else
+#endif
+ {
+ int windows_result = cli_windows_managed_uninstall(home, dry_run);
+ if (windows_result == CLI_OK) {
+ printf("\nUninstall complete. Please restart your coding-agent "
+ "sessions to properly take this into account.\n");
+ }
+ return windows_result == CLI_OK ? CLI_OK : CLI_TRUE;
}
- return windows_result == CLI_OK ? CLI_OK : CLI_TRUE;
#endif
-#ifndef _WIN32
+#if !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API)
g_agent_uninstall_errors = 0;
cbm_detected_agents_t agents = cbm_detect_agents(home);
@@ -12140,7 +12183,12 @@ int cbm_cmd_uninstall(int argc, char **argv) {
char bin_path_storage[CLI_BUF_1K];
const char *bin_path = bin_path_storage;
+#ifdef _WIN32
+ snprintf(bin_path_storage, sizeof(bin_path_storage), "%s/.local/bin/codebase-memory-mcp.exe",
+ home);
+#else
snprintf(bin_path_storage, sizeof(bin_path_storage), "%s/.local/bin/codebase-memory-mcp", home);
+#endif
struct stat binary_status;
bool binary_exists = stat(bin_path, &binary_status) == 0;
cbm_activation_transaction_t *binary_transaction = NULL;
@@ -12183,7 +12231,7 @@ int cbm_cmd_uninstall(int argc, char **argv) {
printf("(dry-run — no files were modified)\n");
}
return g_agent_uninstall_errors == 0 ? 0 : CLI_TRUE;
-#endif
+#endif /* !defined(_WIN32) || defined(CBM_CLI_ENABLE_TEST_API) */
}
/* ── Subcommand: update ───────────────────────────────────────── */
@@ -12923,8 +12971,20 @@ int cbm_cmd_update(int argc, char **argv) {
#ifdef _WIN32
/* Refuse portable self-update before HOME/cache discovery, the release
- * version check, network I/O, prompts, or cohort construction. */
- if (!cli_windows_require_managed_mutation(CBM_WINDOWS_LAUNCHER_ACTION_UPDATE)) {
+ * version check, network I/O, prompts, or cohort construction. In test
+ * builds the activation test seam routes past both managed gates (loud)
+ * so the shared update semantics stay unit-covered; release binaries
+ * always enforce them. */
+ bool update_seam_portable = false;
+#ifdef CBM_CLI_ENABLE_TEST_API
+ update_seam_portable = g_cli_activation_test_ops_set;
+ if (update_seam_portable) {
+ (void)fprintf(stderr, "*** cbm test seam: portable update flow engaged; the managed "
+ "Windows update gates are bypassed (test builds only) ***\n");
+ }
+#endif
+ if (!update_seam_portable &&
+ !cli_windows_require_managed_mutation(CBM_WINDOWS_LAUNCHER_ACTION_UPDATE)) {
return CLI_TRUE;
}
#endif
@@ -12936,7 +12996,7 @@ int cbm_cmd_update(int argc, char **argv) {
}
#ifdef _WIN32
- if (!cli_windows_managed_update_preflight(!dry_run)) {
+ if (!update_seam_portable && !cli_windows_managed_update_preflight(!dry_run)) {
return CLI_TRUE;
}
#endif
diff --git a/src/foundation/subprocess.c b/src/foundation/subprocess.c
index 5532c33af..5f3530f1f 100644
--- a/src/foundation/subprocess.c
+++ b/src/foundation/subprocess.c
@@ -680,8 +680,13 @@ static int cbm_subprocess_spawn_win(cbm_subprocess_t *process) {
if (process->log_file) {
wchar_t *wlog = cbm_path_to_wide(process->log_file);
if (wlog) {
- log = CreateFileW(wlog, GENERIC_WRITE, FILE_SHARE_READ, &security, CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL, NULL);
+ /* FILE_SHARE_WRITE keeps POSIX parity: on unix nothing stops a
+ * second producer from appending to the redirected log while the
+ * child holds it (supervision wrappers rely on that), and the
+ * log's owner-only directory is what gates who can. Without it
+ * the child's handle mandatory-locks every other writer out. */
+ log = CreateFileW(wlog, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &security,
+ CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
free(wlog);
}
if (log == INVALID_HANDLE_VALUE) {
diff --git a/src/ui/config.c b/src/ui/config.c
index bb8eb3284..0694f7e77 100644
--- a/src/ui/config.c
+++ b/src/ui/config.c
@@ -187,6 +187,51 @@ static bool config_parent_directory(const char *path, char *directory, size_t di
#ifdef _WIN32
static volatile LONG g_config_temp_sequence = 0;
+typedef struct {
+ DWORD Flags;
+ HANDLE RootDirectory;
+ DWORD FileNameLength;
+ WCHAR FileName[1];
+} config_file_rename_info_ex_t;
+
+#define CONFIG_FILE_RENAME_INFO_EX_CLASS ((FILE_INFO_BY_HANDLE_CLASS)22)
+#define CONFIG_FILE_RENAME_REPLACE 0x00000001U
+#define CONFIG_FILE_RENAME_POSIX 0x00000002U
+
+/* Publish the still-open temp file over the destination with POSIX rename
+ * semantics: MoveFileExW's legacy replace cannot supersede a destination that
+ * a reader holds open (its name lingers until the last handle closes, even
+ * with FILE_SHARE_DELETE), so a config save racing an open reader failed.
+ * The POSIX form frees the name immediately. The rename target must be the
+ * bare drive path — the NT layer rejects the \\?\ prefix here — and the name
+ * buffer is NUL-terminated in an over-allocated buffer because filter
+ * drivers read FileName as NUL-terminated despite FileNameLength. */
+static bool config_posix_rename_handle(HANDLE file, const wchar_t *target_path) {
+ size_t chars = wcslen(target_path);
+ if (chars >= 4U && wcsncmp(target_path, L"\\\\?\\", 4) == 0) {
+ target_path += 4;
+ chars -= 4U;
+ }
+ if (chars == 0U || chars > (size_t)UINT32_MAX / sizeof(wchar_t)) {
+ return false;
+ }
+ size_t bytes = chars * sizeof(wchar_t);
+ size_t allocation = offsetof(config_file_rename_info_ex_t, FileName) + bytes + sizeof(wchar_t);
+ config_file_rename_info_ex_t *rename = calloc(1U, allocation);
+ if (!rename) {
+ return false;
+ }
+ rename->Flags = CONFIG_FILE_RENAME_POSIX | CONFIG_FILE_RENAME_REPLACE;
+ rename->RootDirectory = NULL;
+ rename->FileNameLength = (DWORD)bytes;
+ memcpy(rename->FileName, target_path, bytes);
+ rename->FileName[chars] = L'\0';
+ bool renamed = SetFileInformationByHandle(file, CONFIG_FILE_RENAME_INFO_EX_CLASS, rename,
+ (DWORD)allocation) != 0;
+ free(rename);
+ return renamed;
+}
+
static bool config_write_atomic(const char *path, const char *json, size_t json_length) {
wchar_t *wide_path = cbm_path_to_wide(path);
if (!wide_path) {
@@ -203,7 +248,9 @@ static bool config_write_atomic(const char *path, const char *json, size_t json_
if (written <= 0 || (size_t)written >= temporary_capacity) {
break;
}
- file = CreateFileW(temporary, GENERIC_WRITE, 0, NULL, CREATE_NEW,
+ /* DELETE access on the temp handle is what permits the
+ * rename-by-handle publish below. */
+ file = CreateFileW(temporary, GENERIC_WRITE | DELETE, 0, NULL, CREATE_NEW,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
if (file != INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS) {
break;
@@ -222,13 +269,19 @@ static bool config_write_atomic(const char *path, const char *json, size_t json_
if (ok) {
ok = FlushFileBuffers(file) != 0;
}
+ bool published = ok && config_posix_rename_handle(file, wide_path);
if (file != INVALID_HANDLE_VALUE && !CloseHandle(file)) {
ok = false;
}
- if (ok) {
- ok = MoveFileExW(temporary, wide_path,
- MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) != 0;
+ if (ok && !published) {
+ /* Error-driven fallback, not a version probe: POSIX rename needs
+ * NTFS-class filesystem support, so exFAT/SMB-homed configs land
+ * here, keeping the pre-POSIX behavior (replace can fail while a
+ * reader holds the destination open). */
+ published = MoveFileExW(temporary, wide_path,
+ MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) != 0;
}
+ ok = ok && published;
if (!ok && temporary) {
(void)DeleteFileW(temporary);
}
diff --git a/tests/test_activation_transaction.c b/tests/test_activation_transaction.c
index 290a07135..39e68fe0b 100644
--- a/tests/test_activation_transaction.c
+++ b/tests/test_activation_transaction.c
@@ -322,7 +322,10 @@ TEST(activation_transaction_stages_same_directory_private_executable_and_aborts)
const char *staged = cbm_activation_transaction_staged_path(transaction);
ASSERT_NOT_NULL(staged);
ASSERT_TRUE(strncmp(staged, directory, strlen(directory)) == 0);
- ASSERT_EQ(staged[strlen(directory)], '/');
+ /* The invariant is containment in the target's directory, not a specific
+ * separator: Windows composes the staged path with '\' because the \\?\
+ * namespace performs no forward-slash translation. */
+ ASSERT_TRUE(staged[strlen(directory)] == '/' || staged[strlen(directory)] == '\\');
ASSERT_FALSE(activation_test_exists(target));
char contents[ACTIVATION_TEST_CONTENT_CAP];
diff --git a/tests/test_cli.c b/tests/test_cli.c
index bcde3343f..a0def04e1 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -49,6 +49,7 @@ int cbm_cli_checksum_manifest_digest(const char *manifest_path, const char *arch
size_t out_size);
void cbm_cli_set_activation_cleanup_failure_for_test(bool enabled);
int cbm_cli_activation_abort_cleanup_probe_for_test(void);
+bool cbm_cli_activation_test_ops_installed(void);
TEST(cli_progress_visibility_policy) {
ASSERT_TRUE(cbm_cli_progress_enabled(true, false));
@@ -462,6 +463,46 @@ static cbm_cli_activation_ops_t cli_activation_fake_ops(cli_activation_fake_t *f
return ops;
}
+/* Every install/update/uninstall in this suite dispatches through here. On
+ * Windows a test that has not installed its own activation ops gets a default
+ * fake for the duration of the command: without the seam, the portable-payload
+ * gate (correctly) refuses managed mutations before the shared agent-config
+ * logic these tests verify ever runs. POSIX behavior is untouched — tests
+ * without ops keep exercising the real activation machinery. */
+static cli_activation_fake_t g_cli_test_seam_fake;
+static cbm_cli_activation_ops_t g_cli_test_seam_ops;
+
+static int cli_test_cmd_dispatch(int (*command)(int, char **), int argc, char **argv) {
+#ifdef _WIN32
+ bool engage = !cbm_cli_activation_test_ops_installed();
+#else
+ bool engage = false;
+#endif
+ if (engage) {
+ memset(&g_cli_test_seam_fake, 0, sizeof(g_cli_test_seam_fake));
+ g_cli_test_seam_fake.mutation_reserve_result = 1;
+ g_cli_test_seam_ops = cli_activation_fake_ops(&g_cli_test_seam_fake);
+ cbm_cli_set_activation_ops_for_test(&g_cli_test_seam_ops);
+ }
+ int rc = command(argc, argv);
+ if (engage) {
+ cbm_cli_set_activation_ops_for_test(NULL);
+ }
+ return rc;
+}
+
+static int cli_test_cmd_install(int argc, char **argv) {
+ return cli_test_cmd_dispatch(cbm_cmd_install, argc, argv);
+}
+
+static int cli_test_cmd_uninstall(int argc, char **argv) {
+ return cli_test_cmd_dispatch(cbm_cmd_uninstall, argc, argv);
+}
+
+static int cli_test_cmd_update(int argc, char **argv) {
+ return cli_test_cmd_dispatch(cbm_cmd_update, argc, argv);
+}
+
static void cli_activation_save_env(char **home_out, char **cache_out) {
const char *home = getenv("HOME");
const char *cache = getenv("CBM_CACHE_DIR");
@@ -832,7 +873,7 @@ TEST(cli_activation_quiesce_does_not_wait_on_bootstrap_startup) {
char dir_arg[640];
snprintf(dir_arg, sizeof(dir_arg), "--dir=%s", install_dir);
char *install_argv[] = {"--force", "--skip-config", "--yes", dir_arg};
- int install_rc = child_ready ? cbm_cmd_install(4, install_argv) : -1;
+ int install_rc = child_ready ? cli_test_cmd_install(4, install_argv) : -1;
cbm_cli_set_activation_runtime_parent_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -901,7 +942,7 @@ TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force"};
- int rc = cbm_cmd_install(1, argv);
+ int rc = cli_test_cmd_install(1, argv);
cbm_cli_set_activation_ops_for_test(NULL);
const char *installed = read_test_file(bin_target);
@@ -953,11 +994,11 @@ TEST(cli_install_dir_and_skip_config_stage_first_install_safely) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force", "--skip-config", "--yes", "--dir", install_dir};
- int rc = cbm_cmd_install(5, argv);
+ int rc = cli_test_cmd_install(5, argv);
char equals_arg[640];
snprintf(equals_arg, sizeof(equals_arg), "--dir=%s", install_dir);
char *dry_argv[] = {"--force", "--skip-config", "--dry-run", equals_arg};
- int dry_rc = cbm_cmd_install(4, dry_argv);
+ int dry_rc = cli_test_cmd_install(4, dry_argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -994,11 +1035,11 @@ TEST(cli_activation_commands_reject_malformed_and_unknown_flags) {
char *bad_install[] = {"--skip-config=value"};
char *bad_update[] = {"--not-an-update-option"};
char *bad_uninstall[] = {"--not-an-uninstall-option"};
- int missing_rc = cbm_cmd_install(1, missing_dir);
- int empty_rc = cbm_cmd_install(1, empty_dir);
- int install_rc = cbm_cmd_install(1, bad_install);
- int update_rc = cbm_cmd_update(1, bad_update);
- int uninstall_rc = cbm_cmd_uninstall(1, bad_uninstall);
+ int missing_rc = cli_test_cmd_install(1, missing_dir);
+ int empty_rc = cli_test_cmd_install(1, empty_dir);
+ int install_rc = cli_test_cmd_install(1, bad_install);
+ int update_rc = cli_test_cmd_update(1, bad_update);
+ int uninstall_rc = cli_test_cmd_uninstall(1, bad_uninstall);
cbm_cli_set_activation_ops_for_test(NULL);
ASSERT_EQ(missing_rc, 1);
@@ -1049,7 +1090,7 @@ TEST(cli_install_reset_deletion_waits_for_final_activation_guard) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force", "--reset-indexes", "--yes"};
- int rc = cbm_cmd_install(3, argv);
+ int rc = cli_test_cmd_install(3, argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -1114,7 +1155,7 @@ TEST(cli_install_config_only_waits_for_cohort_drain) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
cbm_set_auto_answer_for_test(-1);
- int rc = cbm_cmd_install(0, NULL);
+ int rc = cli_test_cmd_install(0, NULL);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -1180,13 +1221,17 @@ TEST(cli_install_config_and_path_finish_before_guard_release) {
.mutation_reserve_result = 1,
.guarded_path_a = codex_config,
.guarded_text_a = "codebase-memory-mcp",
+#ifndef _WIN32
+ /* Windows configures PATH through the user registry, not a shell rc,
+ * so the rc-file visibility guard is a POSIX-only expectation. */
.guarded_path_b = shell_rc,
.guarded_text_b = "export PATH",
+#endif
};
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
cbm_set_auto_answer_for_test(-1);
- int rc = cbm_cmd_install(0, NULL);
+ int rc = cli_test_cmd_install(0, NULL);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -1251,7 +1296,7 @@ TEST(cli_install_config_failure_keeps_published_binary) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force", "--yes"};
- int rc = cbm_cmd_install(2, argv);
+ int rc = cli_test_cmd_install(2, argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -1302,7 +1347,7 @@ TEST(cli_update_download_failure_does_not_quiesce_sessions) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force", "--standard", "--yes"};
- int rc = cbm_cmd_update(3, argv);
+ int rc = cli_test_cmd_update(3, argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -1363,7 +1408,7 @@ TEST(cli_update_already_current_does_not_quiesce_sessions) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--standard"};
- int rc = fixture_ready ? cbm_cmd_update(1, argv) : -1;
+ int rc = fixture_ready ? cli_test_cmd_update(1, argv) : -1;
cbm_cli_set_activation_ops_for_test(NULL);
if (old_path) {
@@ -1495,7 +1540,7 @@ TEST(cli_update_agent_configs_finish_before_guard_release) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force", "--standard"};
- int rc = cbm_cmd_update(2, argv);
+ int rc = cli_test_cmd_update(2, argv);
cbm_cli_set_activation_ops_for_test(NULL);
/* Re-run against a known old target while one independently detected agent
@@ -1517,7 +1562,7 @@ TEST(cli_update_agent_configs_finish_before_guard_release) {
};
cbm_cli_activation_ops_t failure_ops = cli_activation_fake_ops(&config_failure);
cbm_cli_set_activation_ops_for_test(&failure_ops);
- int config_failure_rc = cbm_cmd_update(2, argv);
+ int config_failure_rc = cli_test_cmd_update(2, argv);
cbm_cli_set_activation_ops_for_test(NULL);
struct stat updated_status;
bool replacement_kept = stat(bin_target, &updated_status) == 0 &&
@@ -1582,7 +1627,7 @@ TEST(cli_uninstall_quiesces_active_cohort_before_removing_binary_and_index) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--yes"};
- int rc = cbm_cmd_uninstall(1, argv);
+ int rc = cli_test_cmd_uninstall(1, argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -1642,7 +1687,7 @@ TEST(cli_uninstall_preserves_binary_and_index_when_cohort_does_not_drain) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--yes"};
- int rc = cbm_cmd_uninstall(1, argv);
+ int rc = cli_test_cmd_uninstall(1, argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -1687,10 +1732,10 @@ TEST(cli_activation_guard_is_bypassed_for_dry_run_and_plan) {
char *install_plan[] = {"--force", "--plan"};
char *update_dry[] = {"--force", "--dry-run", "--standard"};
char *uninstall_dry[] = {"--dry-run", "--yes"};
- int install_dry_rc = cbm_cmd_install(2, install_dry);
- int install_plan_rc = cbm_cmd_install(2, install_plan);
- int update_dry_rc = cbm_cmd_update(3, update_dry);
- int uninstall_dry_rc = cbm_cmd_uninstall(2, uninstall_dry);
+ int install_dry_rc = cli_test_cmd_install(2, install_dry);
+ int install_plan_rc = cli_test_cmd_install(2, install_plan);
+ int update_dry_rc = cli_test_cmd_update(3, update_dry);
+ int uninstall_dry_rc = cli_test_cmd_uninstall(2, uninstall_dry);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -2622,7 +2667,7 @@ TEST(cli_openclaw_compaction_preserves_user_owned_section) {
free(installed);
char *argv[] = {"uninstall", "--yes"};
- int rc = cbm_cmd_uninstall(2, argv);
+ int rc = cli_test_cmd_uninstall(2, argv);
char *uninstalled = read_test_file_alloc(config_path);
bool preserved_user =
uninstalled && strstr(uninstalled, "Codebase Memory") && strstr(uninstalled, "User Notes");
@@ -2711,7 +2756,7 @@ TEST(cli_openclaw_uninstall_removes_compaction_when_workspace_is_ambiguous) {
cbm_setenv("HOME", tmpdir, 1);
cbm_setenv("PATH", tmpdir, 1);
char *argv[] = {"uninstall", "--yes"};
- int rc = cbm_cmd_uninstall(2, argv);
+ int rc = cli_test_cmd_uninstall(2, argv);
char *after = read_test_file_alloc(config_path);
bool removed = after && !strstr(after, "Codebase Knowledge Graph (codebase-memory-mcp)");
@@ -2822,7 +2867,7 @@ TEST(cli_vscode_profile_mcp_uninstall) {
cbm_setenv("APPDATA", appdata, 1);
#endif
char *argv[] = {"uninstall", "--yes"};
- int rc = cbm_cmd_uninstall(2, argv);
+ int rc = cli_test_cmd_uninstall(2, argv);
char *base = read_test_file_alloc(base_config);
char *profile = read_test_file_alloc(profile_config);
bool removed = base && profile && !strstr(base, "codebase-memory-mcp") &&
@@ -3800,7 +3845,7 @@ TEST(cli_agent_uninstall_reports_safe_editor_refusal) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--yes"};
- int rc = cbm_cmd_uninstall(1, argv);
+ int rc = cli_test_cmd_uninstall(1, argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
char *after = read_test_file_alloc(config_path);
@@ -3836,7 +3881,7 @@ TEST(cli_special_hook_failures_propagate_from_install_and_uninstall) {
cbm_setenv("PATH", tmpdir, 1);
int install_rc = cbm_install_agent_configs(tmpdir, "/opt/codebase-memory-mcp", false, false);
char *args[] = {"-n"};
- int uninstall_rc = cbm_cmd_uninstall(1, args);
+ int uninstall_rc = cli_test_cmd_uninstall(1, args);
char *after = read_test_file_alloc(hooks_path);
bool unchanged = after && strcmp(after, "[]\n") == 0;
@@ -5022,7 +5067,7 @@ TEST(cli_cline_data_dir_only_redirects_data_state) {
free(preserved_hook);
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
preserved_hook = read_test_file_alloc(hook_paths[3]);
bool removed =
stat(skill, &state) != 0 && preserved_hook && strcmp(preserved_hook, modified_hook) == 0;
@@ -5095,7 +5140,7 @@ TEST(cli_warp_installs_shared_skill_without_mcp_or_permissions) {
const char *modified = "---\nname: codebase-memory\n---\nUser-owned Warp skill.\n";
write_test_file(skill_path, modified);
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *preserved = read_test_file_alloc(skill_path);
bool ownership_safe = preserved && strcmp(preserved, modified) == 0;
free(preserved);
@@ -5164,7 +5209,7 @@ TEST(cli_owned_durable_profiles_preserve_user_files) {
write_test_file(copilot_skill, modified_skill);
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *codex_after = read_test_file_alloc(codex_agent);
char *skill_after = read_test_file_alloc(copilot_skill);
cursor_after = read_test_file_alloc(cursor_agent);
@@ -5253,7 +5298,7 @@ TEST(cli_tiered_codex_profiles_migrate_preserve_and_uninstall) {
const char *modified_verify = "name = \"codebase-memory\"\nuser_note = \"preserve verify\"\n";
write_test_file(verify_path, modified_verify);
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
scout = read_test_file_alloc(scout_path);
verify = read_test_file_alloc(verify_path);
struct stat state;
@@ -5323,7 +5368,7 @@ TEST(cli_tiered_vibe_installs_matching_agent_prompt_sets) {
}
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
struct stat state;
bool removed = uninstall_rc == 0;
for (size_t i = 0U; removed && i < 3U; i++) {
@@ -5402,7 +5447,7 @@ TEST(cli_junie_current_durable_context_contract) {
free(skill_twice);
char *argv[] = {"uninstall", "--yes"};
- int exact_uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int exact_uninstall_rc = cli_test_cmd_uninstall(2, argv);
bool exact_removed = stat(skill_path, &state) != 0 && stat(agent_path, &state) != 0;
const char *modified_skill = "---\nname: codebase-memory\n---\nUser-owned Junie skill.\n";
@@ -5412,7 +5457,7 @@ TEST(cli_junie_current_durable_context_contract) {
cbm_install_agent_configs(tmpdir, "/opt/codebase-memory-mcp", false, false);
write_test_file(skill_path, modified_skill);
write_test_file(agent_path, modified_agent);
- int modified_uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int modified_uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *skill_after = read_test_file_alloc(skill_path);
char *agent_after = read_test_file_alloc(agent_path);
bool modified_preserved = skill_after && agent_after &&
@@ -5473,7 +5518,7 @@ TEST(cli_rovo_installs_documented_global_memory) {
free(second);
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *after = read_test_file_alloc(memory_path);
bool cleaned = uninstall_rc == 0 && after && strcmp(after, personal) == 0;
free(after);
@@ -5537,7 +5582,7 @@ TEST(cli_hermes_stable_shell_context_contract) {
free(installed);
char *argv[] = {"uninstall", "--yes"};
- int exact_uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int exact_uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *after_exact = read_test_file_alloc(config_path);
bool exact_removed = after_exact && strstr(after_exact, "theme: solarized") &&
strstr(after_exact, "/usr/bin/user-hermes-hook") &&
@@ -5554,7 +5599,7 @@ TEST(cli_hermes_stable_shell_context_contract) {
write_test_file(config_path, modified);
}
free(modified);
- int modified_uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int modified_uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *after_modified = read_test_file_alloc(config_path);
bool modified_preserved = hook_was_modified && after_modified &&
strstr(after_modified, "--dialect xermes") &&
@@ -5826,7 +5871,7 @@ TEST(cli_agent_client_registry_routes_plan_install_and_uninstall) {
free(qoder_data);
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
qoder_data = read_test_file_alloc(qoder_settings);
qoder_doc = qoder_data ? yyjson_read(qoder_data, strlen(qoder_data), 0) : NULL;
qoder_root = qoder_doc ? yyjson_doc_get_root(qoder_doc) : NULL;
@@ -6045,7 +6090,7 @@ TEST(cli_registry_installs_kimi_rovo_amp_durable_context) {
free(amp_skill_twice);
char *argv[] = {"uninstall", "--yes"};
- int exact_uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int exact_uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *kimi_after_uninstall = read_test_file_alloc(kimi_instructions);
char *kimi_config_after_uninstall = read_test_file_alloc(kimi_config);
char *amp_after_uninstall = read_test_file_alloc(amp_instructions);
@@ -6072,7 +6117,7 @@ TEST(cli_registry_installs_kimi_rovo_amp_durable_context) {
write_test_file(kimi_skill, modified_kimi_skill);
write_test_file(rovo_agent, modified_rovo_agent);
write_test_file(amp_skill, modified_amp_skill);
- int modified_uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int modified_uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *kimi_skill_after = read_test_file_alloc(kimi_skill);
char *rovo_agent_after = read_test_file_alloc(rovo_agent);
char *amp_skill_after = read_test_file_alloc(amp_skill);
@@ -6243,7 +6288,7 @@ TEST(cli_registry_installs_gitlab_and_devin_lifecycle_context) {
free(devin_agents_data);
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
gitlab_data = read_test_file_alloc(gitlab_hooks);
devin_data = read_test_file_alloc(devin_config);
devin_agents_data = read_test_file_alloc(devin_agents);
@@ -6330,7 +6375,7 @@ TEST(cli_registry_hook_cleanup_is_independent_from_mcp_ownership) {
}
char *argv[] = {"uninstall", "--yes"};
- int foreign_mcp_uninstall = cbm_cmd_uninstall(2, argv);
+ int foreign_mcp_uninstall = cli_test_cmd_uninstall(2, argv);
bool independent_cleanup = foreign_mcp_uninstall == 0;
for (size_t i = 0U; i < sizeof(paths) / sizeof(paths[0]); i++) {
char *data = read_test_file_alloc(paths[i]);
@@ -6370,7 +6415,7 @@ TEST(cli_registry_hook_cleanup_is_independent_from_mcp_ownership) {
fflush(stdout);
redirected = dup2(fileno(capture), STDOUT_FILENO) >= 0;
}
- int modified_hook_uninstall = redirected ? cbm_cmd_uninstall(2, argv) : -1;
+ int modified_hook_uninstall = redirected ? cli_test_cmd_uninstall(2, argv) : -1;
if (redirected) {
fflush(stdout);
(void)dup2(saved_stdout, STDOUT_FILENO);
@@ -6615,7 +6660,7 @@ TEST(cli_registry_installs_codebuddy_bob_and_pochi_durable_context) {
first_rc == 0 && second_rc == 0 && codebuddy_installed && bob_installed && pochi_installed;
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *codebuddy_after = read_test_file_alloc(codebuddy_memory);
char *bob_after = read_test_file_alloc(bob_rule);
char *pochi_after = read_test_file_alloc(pochi_rules);
@@ -6820,7 +6865,7 @@ TEST(cli_gemini_installs_dedicated_graph_subagent) {
free(plan);
char *args[] = {"-n"};
- int uninstall_rc = cbm_cmd_uninstall(1, args);
+ int uninstall_rc = cli_test_cmd_uninstall(1, args);
struct stat state;
bool removed = stat(scout_path, &state) != 0 && stat(agent_path, &state) != 0 &&
stat(auditor_path, &state) != 0;
@@ -7254,7 +7299,7 @@ TEST(cli_augment_installs_session_context_and_subagent) {
free(plan);
char *args[] = {"-n"};
- int uninstall_rc = cbm_cmd_uninstall(1, args);
+ int uninstall_rc = cli_test_cmd_uninstall(1, args);
char *settings_after = read_test_file_alloc(settings_path);
struct stat removed_state;
bool removed = (!settings_after || (!strstr(settings_after, "codebase-memory-mcp") &&
@@ -7588,7 +7633,7 @@ TEST(cli_uninstall_preserves_hook_script_with_modified_binary) {
free(installed);
char *args[] = {"-n"};
- int uninstall_rc = cbm_cmd_uninstall(1, args);
+ int uninstall_rc = cli_test_cmd_uninstall(1, args);
char *after = read_test_file_alloc(script_path);
bool preserved = modified && after && strcmp(after, modified) == 0;
free(after);
@@ -7981,7 +8026,7 @@ TEST(cli_copilot_uninstall_preserves_foreign_named_manifest) {
cbm_setenv("PATH", tmpdir, 1);
cbm_unsetenv("COPILOT_HOME");
char *argv[] = {"uninstall", "--yes"};
- int rc = cbm_cmd_uninstall(2, argv);
+ int rc = cli_test_cmd_uninstall(2, argv);
char *after = read_test_file_alloc(manifest_path);
bool preserved = after && strcmp(after, foreign) == 0;
@@ -8025,7 +8070,7 @@ TEST(cli_copilot_uninstall_preserves_canonical_shaped_foreign_manifest) {
cbm_setenv("PATH", tmpdir, 1);
cbm_unsetenv("COPILOT_HOME");
char *argv[] = {"uninstall", "--yes"};
- int rc = cbm_cmd_uninstall(2, argv);
+ int rc = cli_test_cmd_uninstall(2, argv);
char *after = read_test_file_alloc(manifest_path);
bool preserved = after && strcmp(after, foreign) == 0;
@@ -8112,7 +8157,7 @@ TEST(cli_vscode_only_installs_copilot_durable_context) {
const char *modified = "user-modified-vscode-agent\n";
write_test_file(agent_path, modified);
char *argv[] = {"uninstall", "--yes"};
- int rc = cbm_cmd_uninstall(2, argv);
+ int rc = cli_test_cmd_uninstall(2, argv);
struct stat removed_hook;
struct stat removed_skill;
char *preserved = read_test_file_alloc(agent_path);
@@ -8200,7 +8245,7 @@ TEST(cli_lifecycle_hooks_preserve_foreign_substring_commands) {
free(factory_after_install);
char *argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
char *qwen_after_uninstall = read_test_file_alloc(qwen_settings);
char *factory_after_uninstall = read_test_file_alloc(factory_hooks);
bool qwen_uninstall_preserved = qwen_after_uninstall &&
@@ -9361,7 +9406,7 @@ TEST(cli_uninstall_removes_claude_hook_scripts) {
cbm_install_agent_configs(tmpdir, binary, false, false);
char *args[] = {"-n"};
- int rc = cbm_cmd_uninstall(1, args);
+ int rc = cli_test_cmd_uninstall(1, args);
#ifdef _WIN32
const char *const names[] = {
"cbm-code-discovery-gate.cmd",
@@ -9422,7 +9467,7 @@ TEST(cli_uninstall_preserves_modified_claude_hook_script) {
const char *sentinel = "#!/bin/sh\necho user-modified-session-hook\n";
write_test_file(modified_path, sentinel);
char *args[] = {"-n"};
- (void)cbm_cmd_uninstall(1, args);
+ (void)cli_test_cmd_uninstall(1, args);
char *after = read_test_file_alloc(modified_path);
bool preserved = after && strcmp(after, sentinel) == 0;
free(after);
@@ -10445,7 +10490,7 @@ TEST(cli_windows_claude_lifecycle_migrates_only_exact_owned_legacy_state) {
}
char *uninstall_argv[] = {"uninstall", "--yes"};
- int uninstall_rc = cbm_cmd_uninstall(2, uninstall_argv);
+ int uninstall_rc = cli_test_cmd_uninstall(2, uninstall_argv);
char *uninstalled_settings = read_test_file_alloc(settings_path);
yyjson_doc *uninstalled_doc =
uninstalled_settings ? yyjson_read(uninstalled_settings, strlen(uninstalled_settings), 0)
@@ -10574,7 +10619,7 @@ TEST(cli_windows_claude_hook_scripts_migrate_and_uninstall_all_owned_shapes) {
write_test_file(legacy_path, current_scripts[i]) == 0;
}
char *uninstall_argv[] = {"uninstall", "--yes"};
- int uninstall_rc = uninstall_seeded ? cbm_cmd_uninstall(2, uninstall_argv) : -1;
+ int uninstall_rc = uninstall_seeded ? cli_test_cmd_uninstall(2, uninstall_argv) : -1;
bool all_owned_shapes_removed = uninstall_rc == 0;
for (size_t i = 0U; i < sizeof(legacy_names) / sizeof(legacy_names[0]); i++) {
char current_path[768];
@@ -11554,6 +11599,89 @@ TEST(cli_sha256_file_matches_known_vector) {
PASS();
}
+#ifdef _WIN32
+/* The fail-closed release contract, asserted with the activation seam OFF:
+ * these calls take the exact dispatch a release binary ships (the portable
+ * body is unreachable), so the gates themselves keep direct unit coverage. */
+TEST(cli_windows_release_gates_refuse_portable_mutations) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-portable-refusal-XXXXXX");
+ if (!cbm_mkdtemp(tmpdir)) {
+ FAIL("cbm_mkdtemp failed");
+ }
+ char *old_home = NULL;
+ char *old_cache = NULL;
+ cli_activation_save_env(&old_home, &old_cache);
+ cbm_setenv("HOME", tmpdir, 1);
+ char cache_dir[512];
+ snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir);
+ cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
+
+ char bin_dir[512];
+ char bin_target[640];
+ snprintf(bin_dir, sizeof(bin_dir), "%s/.local/bin", tmpdir);
+ test_mkdirp(bin_dir);
+ snprintf(bin_target, sizeof(bin_target), "%s/codebase-memory-mcp.exe", bin_dir);
+ write_test_file(bin_target, "portable refusal must not touch this");
+
+ /* A one-shot portable payload (this test runner has no launcher context)
+ * may never self-mutate a managed surface. */
+ char *uninstall_argv[] = {"--yes"};
+ int uninstall_rc = cbm_cmd_uninstall(1, uninstall_argv);
+ char *update_argv[] = {"--yes"};
+ int update_rc = cbm_cmd_update(1, update_argv);
+
+ const char *installed = read_test_file(bin_target);
+ bool preserved = installed && strcmp(installed, "portable refusal must not touch this") == 0;
+ cli_activation_restore_env(old_home, old_cache);
+ test_rmdir_r(tmpdir);
+
+ ASSERT_EQ(uninstall_rc, 1);
+ ASSERT_EQ(update_rc, 1);
+ ASSERT_TRUE(preserved);
+ PASS();
+}
+
+TEST(cli_windows_release_gate_refuses_foreign_install_target) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-foreign-target-XXXXXX");
+ if (!cbm_mkdtemp(tmpdir)) {
+ FAIL("cbm_mkdtemp failed");
+ }
+ char *old_home = NULL;
+ char *old_cache = NULL;
+ cli_activation_save_env(&old_home, &old_cache);
+ cbm_setenv("HOME", tmpdir, 1);
+ char cache_dir[512];
+ snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir);
+ cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
+
+ char bin_dir[512];
+ char bin_target[640];
+ snprintf(bin_dir, sizeof(bin_dir), "%s/.local/bin", tmpdir);
+ test_mkdirp(bin_dir);
+ snprintf(bin_target, sizeof(bin_target), "%s/codebase-memory-mcp.exe", bin_dir);
+ write_test_file(bin_target, "foreign binary must be preserved");
+
+ /* An unmanaged file at the canonical launcher path is a conflict the
+ * managed transaction refuses to adopt or replace — even under --force
+ * with prompts auto-answered. */
+ cbm_set_auto_answer_for_test(-1);
+ char *argv[] = {"--force", "-y"};
+ int rc = cbm_cmd_install(2, argv);
+ cbm_set_auto_answer_for_test(0);
+
+ const char *installed = read_test_file(bin_target);
+ bool preserved = installed && strcmp(installed, "foreign binary must be preserved") == 0;
+ cli_activation_restore_env(old_home, old_cache);
+ test_rmdir_r(tmpdir);
+
+ ASSERT_EQ(rc, 1);
+ ASSERT_TRUE(preserved);
+ PASS();
+}
+#endif /* _WIN32 */
+
/* ═══════════════════════════════════════════════════════════════════
* Suite definition
* ═══════════════════════════════════════════════════════════════════ */
@@ -11591,6 +11719,10 @@ SUITE(cli) {
RUN_TEST(cli_uninstall_quiesces_active_cohort_before_removing_binary_and_index);
RUN_TEST(cli_uninstall_preserves_binary_and_index_when_cohort_does_not_drain);
RUN_TEST(cli_activation_guard_is_bypassed_for_dry_run_and_plan);
+#ifdef _WIN32
+ RUN_TEST(cli_windows_release_gates_refuse_portable_mutations);
+ RUN_TEST(cli_windows_release_gate_refuses_foreign_install_target);
+#endif
/* Version (2 tests — selfupdate_test.go) */
RUN_TEST(cli_compare_versions);
diff --git a/tests/test_ui.c b/tests/test_ui.c
index 30e069a12..a403dbbbe 100644
--- a/tests/test_ui.c
+++ b/tests/test_ui.c
@@ -116,19 +116,22 @@ TEST(config_save_atomically_replaces_a_complete_generation) {
.ui_enabled = true,
.ui_port = 22222,
};
- ASSERT_TRUE(cbm_ui_config_save(&new_generation));
+ /* Capture everything, clean up, and only then assert: an assert firing
+ * before the env restore leaks CBM_CACHE_DIR into every later test in the
+ * process, turning one regression into a cascade. */
+ bool saved = cbm_ui_config_save(&new_generation);
char old_bytes[512] = {0};
#ifdef _WIN32
DWORD old_length = 0;
- ASSERT_TRUE(ReadFile(old_handle, old_bytes, (DWORD)sizeof(old_bytes) - 1U, &old_length, NULL) !=
- 0);
- ASSERT_GT(old_length, 0);
- ASSERT_TRUE(CloseHandle(old_handle) != 0);
+ bool old_read = ReadFile(old_handle, old_bytes, (DWORD)sizeof(old_bytes) - 1U, &old_length,
+ NULL) != 0 &&
+ old_length > 0;
+ bool old_closed = CloseHandle(old_handle) != 0;
#else
size_t old_length = fread(old_bytes, 1, sizeof(old_bytes) - 1, old_handle);
- ASSERT_GT(old_length, 0);
- ASSERT_EQ(fclose(old_handle), 0);
+ bool old_read = old_length > 0;
+ bool old_closed = fclose(old_handle) == 0;
#endif
cbm_ui_config_t loaded = {0};
@@ -142,6 +145,9 @@ TEST(config_save_atomically_replaces_a_complete_generation) {
free(old_cache);
(void)th_rmtree(td);
+ ASSERT_TRUE(saved);
+ ASSERT_TRUE(old_read);
+ ASSERT_TRUE(old_closed);
/* An in-place truncate/rewrite mutates the already-open handle. Atomic
* replacement leaves it attached to the complete prior generation. */
ASSERT_NOT_NULL(strstr(old_bytes, "11111"));
diff --git a/tests/test_version_cohort.c b/tests/test_version_cohort.c
index a1e791b4b..428eee66c 100644
--- a/tests/test_version_cohort.c
+++ b/tests/test_version_cohort.c
@@ -796,9 +796,13 @@ TEST(version_cohort_crash_releases_process_lifetime_lease) {
}
cbm_usleep(1000);
}
+ /* The turnover acquire gets its own conflict record: acquire zeroes its
+ * conflict_out on entry, so reusing `conflict` here would erase the
+ * probe's VERSION_CONFLICT detail before the assertions read it. */
+ cbm_daemon_conflict_t turnover_conflict;
cbm_version_cohort_status_t turnover_status =
manager && terminal
- ? cbm_version_cohort_acquire(manager, &build_b, UINT64_MAX, &lease, &conflict)
+ ? cbm_version_cohort_acquire(manager, &build_b, UINT64_MAX, &lease, &turnover_conflict)
: CBM_VERSION_COHORT_IO;
version_cohort_release(&lease);