Skip to content

Commit 3ad4274

Browse files
authored
Merge pull request DeusData#1320 from DeusData/fix/windows-install-contract-and-ipc-determinism
fix(windows): repair install.ps1, honest dry-run status, deterministic daemon_ipc
2 parents c8a58cf + 5f2c146 commit 3ad4274

7 files changed

Lines changed: 161 additions & 55 deletions

File tree

‎install.ps1‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# install.ps1 — One-line installer for codebase-memory-mcp (Windows).
1+
# install.ps1 - One-line installer for codebase-memory-mcp (Windows).
22
#
33
# Usage: see README.md for install instructions.
44
#
@@ -103,7 +103,7 @@ foreach ($arg in $args) {
103103
# PROCESSOR_ARCHITEW6432, which is unset for 64-bit emulated processes. Fall back
104104
# to the env vars only if the .NET API is somehow unavailable.
105105
if ($env:CBM_ARCH) {
106-
# Explicit override wins — used by CI/tests, and an escape hatch under x64
106+
# Explicit override wins - used by CI/tests, and an escape hatch under x64
107107
# emulation on ARM64 where no in-process detection is reliable.
108108
$Arch = $env:CBM_ARCH
109109
} else {
@@ -279,7 +279,7 @@ if (Test-Path -LiteralPath $Dest -PathType Leaf) {
279279
catch { Start-Sleep -Milliseconds 500 }
280280
}
281281
if (-not $renamed) {
282-
Write-Host "error: could not retire the existing $BinName — close all running" -ForegroundColor Red
282+
Write-Host "error: could not retire the existing $BinName - close all running" -ForegroundColor Red
283283
Write-Host " codebase-memory-mcp sessions and coding agents, then re-run." -ForegroundColor Red
284284
Remove-Item -Recurse -Force $TmpDir -ErrorAction SilentlyContinue
285285
exit 1

‎scripts/smoke-test.sh‎

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ smoke_mktemp_dir() {
5757
fi
5858
}
5959

60+
# Byte-identity of two files. `cmp` is NOT present in the Windows MSYS shell, so
61+
# a comparison built on it reports "different" for every pair it is handed —
62+
# including two copies of the same file.
63+
smoke_file_sha256() {
64+
if command -v sha256sum &>/dev/null; then
65+
sha256sum "$1" | awk '{print $1}'
66+
else
67+
shasum -a 256 "$1" | awk '{print $1}'
68+
fi
69+
}
70+
6071
# Every platform ships ONE binary, Windows included: a fixture copy is complete
6172
# with nothing beside it.
6273
copy_smoke_binary() {
@@ -876,36 +887,34 @@ if [[ "$BINARY" == *.exe ]] &&
876887
fi
877888

878889
# 6b: uninstall --dry-run -y
890+
# Windows used to refuse this: a portable extracted bundle was a DIFFERENT
891+
# artifact from the launcher-managed install it would have torn down, so it had
892+
# to decline and point at the managed copy. One binary per platform removes that
893+
# split entirely — the extracted binary IS the installed one — so uninstall now
894+
# plans the same removals it does on Linux and macOS.
879895
echo "--- Phase 6b: uninstall --dry-run ---"
880-
if [[ "$BINARY" == *.exe ]]; then
881-
if UNINSTALL_OUT=$(run_dryrun_env "$BINARY" uninstall --dry-run -y 2>&1); then
882-
echo "FAIL: portable Windows bundle accepted uninstall"
883-
exit 1
884-
fi
885-
if ! echo "$UNINSTALL_OUT" | grep -qi 'managed\|install\|package'; then
886-
echo "FAIL: portable Windows uninstall refusal had no install guidance"
887-
echo "$UNINSTALL_OUT"
888-
exit 1
889-
fi
890-
else
891-
UNINSTALL_OUT=$(run_dryrun_env "$BINARY" uninstall --dry-run -y 2>&1)
892-
if ! echo "$UNINSTALL_OUT" | grep -qi 'uninstall\|remov'; then
893-
echo "FAIL: uninstall --dry-run produced unexpected output"
894-
echo "$UNINSTALL_OUT"
895-
exit 1
896-
fi
896+
UNINSTALL_OUT=$(run_dryrun_env "$BINARY" uninstall --dry-run -y 2>&1)
897+
if ! echo "$UNINSTALL_OUT" | grep -qi 'uninstall\|remov'; then
898+
echo "FAIL: uninstall --dry-run produced unexpected output"
899+
echo "$UNINSTALL_OUT"
900+
exit 1
897901
fi
898902
echo "OK: uninstall --dry-run completed"
899903

900904
# 6c: update --dry-run --standard -y
901905
echo "--- Phase 6c: update --dry-run ---"
902906
if [[ "$BINARY" == *.exe ]]; then
903-
if UPDATE_OUT=$(run_dryrun_env "$BINARY" update --dry-run --standard -y 2>&1); then
904-
echo "FAIL: portable Windows bundle accepted update"
907+
# Same contract Phase 14a asserts against a real update: Windows never
908+
# replaces the running image in-process, so `update` is a handoff — it exits 0
909+
# and prints the exact install.ps1 command. The old refusal here was the
910+
# portable-vs-managed split, which died with the launcher stub.
911+
if ! UPDATE_OUT=$(run_dryrun_env "$BINARY" update --dry-run --standard -y 2>&1); then
912+
echo "FAIL: Windows update handoff exited non-zero"
913+
echo "$UPDATE_OUT"
905914
exit 1
906915
fi
907-
if ! echo "$UPDATE_OUT" | grep -qi 'managed\|install\|package'; then
908-
echo "FAIL: portable Windows update refusal had no install guidance"
916+
if ! echo "$UPDATE_OUT" | grep -q 'install.ps1'; then
917+
echo "FAIL: Windows update did not print the install.ps1 handoff"
909918
echo "$UPDATE_OUT"
910919
exit 1
911920
fi
@@ -1102,10 +1111,10 @@ ROVO_AGENT="$FAKE_HOME/.rovodev/subagents/codebase-memory.md"
11021111
AMAZON_Q_MCP="$FAKE_HOME/.aws/amazonq/default.json"
11031112
mkdir -p "$GITLAB_DIR" "$(dirname "$GITLAB_HOOKS")" "$DEVIN_DIR"
11041113
mkdir -p "$FAKE_HOME/.local/bin"
1105-
# A Windows portable pair is the installer source, not a valid managed target.
1106-
# Leave the canonical destination absent so install can publish the authenticated
1107-
# generation backing + canonical hard-link pair. A one-link copy at that path is
1108-
# deliberately rejected as an unknown/conflicting installation.
1114+
# POSIX seeds the destination so install exercises the replace-an-existing-copy
1115+
# path. Windows leaves it absent and covers the first-install path instead:
1116+
# seeding it would mean running the fixture binary out of the very location the
1117+
# install is about to publish to, which Windows' image lock forbids.
11091118
if [[ "$BINARY" == *.exe ]]; then
11101119
SELF_PATH="$FAKE_HOME/.local/bin/codebase-memory-mcp.exe"
11111120
else
@@ -2934,9 +2943,15 @@ if [ -n "${SMOKE_DOWNLOAD_URL:-}" ]; then
29342943
# Pre-install agent config with positive prior-install identity. POSIX runs
29352944
# update from that exact retired CBM image, so refresh requires only string
29362945
# equality with OS-reported self identity and never probes config paths.
2937-
# Windows retains its fixed-drive missing-path classification coverage.
2946+
#
2947+
# Windows points at the INSTALLED binary, not the retired one. Its update is a
2948+
# handoff to install.ps1 now, so nothing rewrites this entry in-process the way
2949+
# the old launcher-managed update did; leaving it on the retired path would
2950+
# make 14f demand that uninstall delete an entry owned by a DIFFERENT
2951+
# installation, which it correctly refuses to do. install.ps1 re-runs
2952+
# `install`, so this is exactly what a real Windows user is left holding.
29382953
if [[ "$BINARY" == *.exe ]]; then
2939-
STALE_CMD="$UPDATE_HOME/retired-install/codebase-memory-mcp.exe"
2954+
STALE_CMD="$UPDATE_HOME/.local/bin/codebase-memory-mcp.exe"
29402955
else
29412956
STALE_CMD="$UPDATE_DRIVER"
29422957
fi
@@ -2970,7 +2985,8 @@ if [ -n "${SMOKE_DOWNLOAD_URL:-}" ]; then
29702985
echo "FAIL 14a: Windows update did not print the install.ps1 command"
29712986
exit 1
29722987
fi
2973-
if ! cmp -s "$BINARY" "$UPDATE_HOME/.local/bin/codebase-memory-mcp.exe"; then
2988+
if [ "$(smoke_file_sha256 "$BINARY")" != \
2989+
"$(smoke_file_sha256 "$UPDATE_HOME/.local/bin/codebase-memory-mcp.exe")" ]; then
29742990
echo "FAIL 14a: Windows update replaced the binary in-process"
29752991
exit 1
29762992
fi

‎src/cli/cli.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9437,12 +9437,16 @@ int cbm_cmd_install(int argc, char **argv) {
94379437
* so a non-OK here is a plan-side check (agent-config / PATH probe)
94389438
* and must still report that it was a dry-run - on Windows it was
94399439
* silently skipping the summary and reading as a hard failure. Emit
9440-
* the dry-run indicator, and name the underlying status for triage. */
9440+
* the dry-run indicator, and name the underlying status for triage.
9441+
*
9442+
* The status itself still travels: `--dry-run` exists to answer "would
9443+
* this install work?", so a refused plan check has to be visible to a
9444+
* caller that only sees the exit code. Reporting success here made a
9445+
* fail-closed PATH probe indistinguishable from a clean plan. */
94419446
if (dry_run) {
94429447
(void)fprintf(stderr, "note: install --dry-run plan check returned %d\n",
94439448
activation_rc);
94449449
printf("\n(dry-run — no files were modified)\n");
9445-
return CLI_OK;
94469450
}
94479451
return CLI_TRUE;
94489452
}

‎src/daemon/ipc.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ void cbm_daemon_ipc_windows_legacy_guard_release_failures_set_for_test(unsigned
114114
memory_order_release);
115115
}
116116

117+
#ifdef _WIN32
118+
static cbm_daemon_ipc_startup_gate_fn g_startup_gate_for_test;
119+
static void *g_startup_gate_context_for_test;
120+
#endif
121+
122+
void cbm_daemon_ipc_startup_gate_set_for_test(cbm_daemon_ipc_startup_gate_fn gate, void *context) {
123+
#ifdef _WIN32
124+
g_startup_gate_context_for_test = context;
125+
g_startup_gate_for_test = gate;
126+
#else
127+
(void)gate;
128+
(void)context;
129+
#endif
130+
}
131+
132+
#ifdef _WIN32
133+
static void ipc_startup_gate_run(void) {
134+
cbm_daemon_ipc_startup_gate_fn gate = g_startup_gate_for_test;
135+
if (gate) {
136+
gate(g_startup_gate_context_for_test);
137+
}
138+
}
139+
#endif
140+
117141
bool cbm_daemon_ipc_windows_legacy_names(const char *canonical_runtime_parent,
118142
const char *instance_key,
119143
char pipe_out[CBM_DAEMON_IPC_WINDOWS_NAME_CAP],
@@ -5755,6 +5779,9 @@ int cbm_daemon_ipc_startup_lock_try_acquire(const cbm_daemon_ipc_endpoint_t *end
57555779
lock->legacy_guard = legacy_guard;
57565780
lock->legacy_sentinel = INVALID_HANDLE_VALUE;
57575781
*lock_out = lock;
5782+
/* The lock is held and the handoff has not run yet: the one point where a
5783+
* test can pin this interleaving deterministically. No-op in production. */
5784+
ipc_startup_gate_run();
57585785
return 1;
57595786
}
57605787

‎src/daemon/ipc_internal.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,13 @@ void cbm_daemon_ipc_posix_publication_hook_set_for_test(
9696
cbm_daemon_ipc_posix_publication_hook_fn hook, void *context);
9797
void cbm_daemon_ipc_windows_legacy_guard_release_failures_set_for_test(unsigned int count);
9898

99+
/* Deterministic-interleaving seam: fires on the Windows startup path once the
100+
* startup lock is held, before the rendezvous handoff. A test parks here to
101+
* pin the "startup is holding the lock" interleaving by construction. Polling
102+
* for that state instead is a race — the whole acquire → handoff → release
103+
* sequence completes in microseconds whenever the handoff does not block, so
104+
* the observation window has no lower bound. */
105+
typedef void (*cbm_daemon_ipc_startup_gate_fn)(void *context);
106+
void cbm_daemon_ipc_startup_gate_set_for_test(cbm_daemon_ipc_startup_gate_fn gate, void *context);
107+
99108
#endif /* CBM_DAEMON_IPC_INTERNAL_H */

‎tests/test_daemon_ipc.c‎

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,36 @@ typedef struct {
399399
int result;
400400
} ipc_test_win_startup_call_t;
401401

402+
typedef struct {
403+
atomic_bool acquired;
404+
atomic_bool may_proceed;
405+
} ipc_test_startup_gate_t;
406+
407+
/* Runs on the startup thread INSIDE cbm_daemon_ipc_startup_lock_try_acquire,
408+
* with the startup lock held and the rendezvous handoff not yet attempted.
409+
* Announce that state, then park until the test has released its reader. This
410+
* pins the interleaving by construction; the previous version polled for the
411+
* lock being busy, which is unobservable whenever the handoff does not block. */
412+
static void ipc_test_startup_gate(void *context) {
413+
ipc_test_startup_gate_t *gate = context;
414+
atomic_store(&gate->acquired, true);
415+
while (!atomic_load(&gate->may_proceed)) {
416+
cbm_usleep(200);
417+
}
418+
}
419+
420+
static bool ipc_test_startup_gate_wait_acquired(ipc_test_startup_gate_t *gate) {
421+
/* `acquired` never clears, so this wait cannot miss the event; the bound
422+
* only catches a thread that never started at all. */
423+
for (size_t attempt = 0; attempt < 50000U; attempt++) {
424+
if (atomic_load(&gate->acquired)) {
425+
return true;
426+
}
427+
cbm_usleep(200);
428+
}
429+
return false;
430+
}
431+
402432
static void *ipc_test_win_startup_call(void *opaque) {
403433
ipc_test_win_startup_call_t *call = opaque;
404434
cbm_daemon_ipc_startup_lock_t *startup = NULL;
@@ -416,14 +446,6 @@ static void ipc_test_win_lock_release(cbm_private_file_lock_t **lock_io) {
416446
cbm_usleep(1000);
417447
}
418448
}
419-
420-
static bool ipc_test_win_lock_busy(cbm_private_lock_directory_t *directory, const char *base_name) {
421-
cbm_private_file_lock_t *probe = NULL;
422-
cbm_private_file_lock_status_t status =
423-
cbm_private_file_lock_try_acquire(directory, base_name, CBM_PRIVATE_FILE_LOCK_SH, &probe);
424-
ipc_test_win_lock_release(&probe);
425-
return status == CBM_PRIVATE_FILE_LOCK_BUSY;
426-
}
427449
#endif
428450

429451
typedef struct {
@@ -1028,6 +1050,10 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
10281050
bool startup_observed = false;
10291051
int join_status = -1;
10301052
ipc_test_win_startup_call_t call = {.result = -1};
1053+
ipc_test_startup_gate_t gate;
1054+
atomic_init(&gate.acquired, false);
1055+
atomic_init(&gate.may_proceed, false);
1056+
cbm_daemon_ipc_startup_gate_set_for_test(ipc_test_startup_gate, &gate);
10311057

10321058
bool parent_ok = ipc_test_parent_new(parent, "win-record-reader");
10331059
if (parent_ok) {
@@ -1048,21 +1074,19 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
10481074
if (record_status == CBM_PRIVATE_FILE_LOCK_OK) {
10491075
thread_started = cbm_thread_create(&thread, 0, ipc_test_win_startup_call, &call) == 0;
10501076
}
1051-
for (size_t attempt = 0; thread_started && attempt < 200U; attempt++) {
1052-
if (ipc_test_win_lock_busy(directory, "cbm-startup-v2.lock")) {
1053-
startup_observed = true;
1054-
break;
1055-
}
1056-
cbm_usleep(1000);
1077+
if (thread_started) {
1078+
startup_observed = ipc_test_startup_gate_wait_acquired(&gate);
10571079
}
10581080
ipc_test_win_lock_release(&record_reader);
1081+
atomic_store(&gate.may_proceed, true);
10591082
if (thread_started) {
10601083
join_status = cbm_thread_join(&thread);
10611084
}
10621085
if (endpoint) {
10631086
ipc_test_copy_path(address, cbm_daemon_ipc_endpoint_address(endpoint));
10641087
}
10651088

1089+
cbm_daemon_ipc_startup_gate_set_for_test(NULL, NULL);
10661090
ipc_test_win_lock_release(&record_reader);
10671091
cbm_private_lock_directory_close(directory);
10681092
cbm_daemon_ipc_endpoint_free(endpoint);
@@ -1115,6 +1139,11 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
11151139
cbm_daemon_ipc_startup_lock_release(&initial_startup);
11161140
initial_startup = NULL;
11171141

1142+
ipc_test_startup_gate_t gate;
1143+
atomic_init(&gate.acquired, false);
1144+
atomic_init(&gate.may_proceed, false);
1145+
cbm_daemon_ipc_startup_gate_set_for_test(ipc_test_startup_gate, &gate);
1146+
11181147
cbm_private_file_lock_status_t directory_status =
11191148
endpoint ? cbm_daemon_ipc_private_lock_directory_new(endpoint, &directory)
11201149
: CBM_PRIVATE_FILE_LOCK_IO;
@@ -1126,19 +1155,18 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
11261155
if (record_status == CBM_PRIVATE_FILE_LOCK_OK) {
11271156
thread_started = cbm_thread_create(&thread, 0, ipc_test_win_startup_call, &call) == 0;
11281157
}
1129-
for (size_t attempt = 0; thread_started && attempt < 200U; attempt++) {
1130-
if (ipc_test_win_lock_busy(directory, "cbm-startup-v2.lock")) {
1131-
startup_observed = true;
1132-
break;
1133-
}
1134-
cbm_usleep(1000);
1158+
if (thread_started) {
1159+
startup_observed = ipc_test_startup_gate_wait_acquired(&gate);
11351160
}
1161+
/* Claim the lifetime lock while startup is parked in the gate, so the
1162+
* concurrent-owner bridge under test is exercised deterministically. */
11361163
cbm_private_file_lock_status_t lifetime_status =
11371164
startup_observed
11381165
? cbm_private_file_lock_try_acquire(directory, "cbm-lifetime.lock",
11391166
CBM_PRIVATE_FILE_LOCK_EX, &lifetime_owner)
11401167
: CBM_PRIVATE_FILE_LOCK_IO;
11411168
ipc_test_win_lock_release(&record_reader);
1169+
atomic_store(&gate.may_proceed, true);
11421170
if (thread_started) {
11431171
join_status = cbm_thread_join(&thread);
11441172
}
@@ -1147,6 +1175,7 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
11471175
}
11481176
ipc_test_win_lock_release(&lifetime_owner);
11491177

1178+
cbm_daemon_ipc_startup_gate_set_for_test(NULL, NULL);
11501179
ipc_test_win_lock_release(&record_reader);
11511180
ipc_test_win_lock_release(&lifetime_owner);
11521181
cbm_private_lock_directory_close(directory);
@@ -2072,6 +2101,13 @@ typedef struct {
20722101
int receive_result;
20732102
} ipc_forever_wait_server_t;
20742103

2104+
/* The connection is deliberately NOT closed here. The test interrupts this
2105+
* thread's wait through server->connection, and `receive_frame` can also return
2106+
* on its own (a torn connection, an early frame) before that call lands.
2107+
* Closing from this thread therefore freed the connection while the test was
2108+
* still dereferencing the pointer it had already loaded — a use-after-free that
2109+
* crashed roughly one Windows run in five. Ownership stays with the test, which
2110+
* closes it after joining, so `interrupt` can only ever see a live connection. */
20752111
static void *ipc_forever_wait_server(void *opaque) {
20762112
ipc_forever_wait_server_t *server = (ipc_forever_wait_server_t *)opaque;
20772113
cbm_daemon_frame_t frame = {0};
@@ -2084,8 +2120,6 @@ static void *ipc_forever_wait_server(void *opaque) {
20842120
server->connection, CBM_DAEMON_IPC_WAIT_FOREVER, &frame, &payload);
20852121
}
20862122
free(payload);
2087-
cbm_daemon_ipc_connection_close(server->connection);
2088-
server->connection = NULL;
20892123
return NULL;
20902124
}
20912125

@@ -2129,6 +2163,9 @@ TEST(daemon_ipc_wait_forever_is_interruptible) {
21292163
if (thread_started) {
21302164
join_result = cbm_thread_join(&thread);
21312165
}
2166+
/* Safe only after the join: the server thread no longer touches it. */
2167+
cbm_daemon_ipc_connection_close(server.connection);
2168+
server.connection = NULL;
21322169
cbm_daemon_ipc_connection_close(client);
21332170
cbm_daemon_ipc_listener_close(listener);
21342171
cbm_daemon_ipc_endpoint_free(endpoint);

0 commit comments

Comments
 (0)