@@ -88,6 +88,24 @@ ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
8888[ -f " $CONFIG " ] && . " $CONFIG "
8989HOST=" ${CBM_VM_HOST:? set CBM_VM_HOST in ~/ .claude/ cbm-vm/ config} "
9090USER_=" ${CBM_VM_USER:- test} "
91+
92+ # ── Per-run checkout (the VM's last shared-state hazard) ─────────────────────
93+ # The VM holds ONE working tree at /c/cbm, and `update`/`sync` REPLACE it
94+ # (`git reset --hard` + `clean -fdx`). A second session syncing its own branch
95+ # therefore swaps the code under a running leg — observed 2026-08-06, where a
96+ # gate run on main compiled another branch's files and died on a header that
97+ # does not exist on Windows. That is worse than a red: it is a confident wrong
98+ # answer about the wrong tree.
99+ #
100+ # Setting CBM_CI_RUN_ID gives this run its own checkout, cloned from /c/cbm.
101+ # A local clone hardlinks .git/objects, so it costs seconds and little disk.
102+ # Unset (the default) keeps the historical single-tree behaviour.
103+ VM_BASE_REPO=" /c/cbm"
104+ if [ -n " ${CBM_CI_RUN_ID:- } " ]; then
105+ VM_REPO=" /c/cbm-run-${CBM_CI_RUN_ID} "
106+ else
107+ VM_REPO=" $VM_BASE_REPO "
108+ fi
91109HOST_KEY=" ${CBM_VM_HOST_KEY_SHA256:? set CBM_VM_HOST_KEY_SHA256 in ~/ .claude/ cbm-vm/ config} "
92110LOCAL_BRANCH=" $( git -C " $ROOT " branch --show-current) "
93111BRANCH=" ${CBM_VM_BRANCH:- ${LOCAL_BRANCH:- main} } "
@@ -117,6 +135,29 @@ SCP=(scp "${SSH_OPTIONS[@]}")
117135
118136vm () { local env=" $1 " ; shift
119137 " ${SSH[@]} " " C:\\ msys64\\ msys2_shell.cmd -defterm -no-start -${env} -c \" set -e -o pipefail; $* \" " ; }
138+
139+ # Create this run's own checkout if it does not exist yet. A LOCAL clone
140+ # hardlinks .git/objects, so it is seconds and near-zero disk rather than a
141+ # fresh network clone. No-op when running in the shared (default) tree.
142+ vm_ensure_run_checkout () {
143+ [ " $VM_REPO " = " $VM_BASE_REPO " ] && return 0
144+ vm clangarm64 " [ -d '$VM_REPO /.git' ] || git clone --local '$VM_BASE_REPO ' '$VM_REPO '"
145+ }
146+
147+ # The VM tree must be the one the caller means. A concurrent session running
148+ # `update`/`sync` in the SHARED tree swaps it mid-flight, and the leg then
149+ # reports a confident verdict about someone else's branch. Callers assert the
150+ # expected HEAD; a mismatch fails loudly instead of producing that verdict.
151+ vm_assert_head () {
152+ local want=" $1 " got
153+ got=" $( vm clangarm64 " cd $VM_REPO && git rev-parse --verify HEAD" | tr -d ' \r' ) "
154+ if [ " $got " != " $want " ]; then
155+ echo " FATAL: VM tree $VM_REPO is at $got , expected $want ." >&2
156+ echo " Another session likely re-synced the shared checkout." >&2
157+ echo " Re-run with CBM_CI_RUN_ID set to get an isolated tree." >&2
158+ return 1
159+ fi
160+ }
120161vm_cmd () { " ${SSH[@]} " " $@ " ; } # plain cmd.exe (CI-shaped environment)
121162
122163# A GitHub runner is ephemeral: every job starts on a fresh image with a
@@ -157,13 +198,25 @@ esac
157198case " $cmd " in
158199status)
159200 " ${SSH[@]} " " echo VM_REACHABLE & ver"
160- vm clangarm64 " cd /c/cbm 2>/dev/null && git log --oneline -1 && ls -la build/c/codebase-memory-mcp.exe build/c/test-runner.exe 2>/dev/null || echo 'repo/build missing — run provision-windows.sh'"
201+ vm clangarm64 " cd $VM_REPO 2>/dev/null && git log --oneline -1 && ls -la build/c/codebase-memory-mcp.exe build/c/test-runner.exe 2>/dev/null || echo 'repo/build missing — run provision-windows.sh'"
161202 ;;
162203update)
163- vm clangarm64 " cd /c/cbm && git fetch origin ${BRANCH} && git reset --hard FETCH_HEAD && git clean -fdx && git log --oneline -1"
204+ vm_ensure_run_checkout
205+ vm clangarm64 " cd $VM_REPO && git fetch origin ${BRANCH} && git reset --hard FETCH_HEAD && git clean -fdx && git log --oneline -1"
164206 exec " $0 " build
165207 ;;
208+ drop-run-checkout)
209+ # Explicit tidy-up of THIS run's checkout (no-op in shared mode). The run
210+ # that created it removes it; nothing else may, since another session's
211+ # tree must never be deleted out from under it.
212+ if [ " $VM_REPO " = " $VM_BASE_REPO " ]; then
213+ echo " win.sh: shared checkout ($VM_BASE_REPO ) — nothing to drop" >&2
214+ else
215+ vm clangarm64 " rm -rf '$VM_REPO ' && echo dropped $VM_REPO "
216+ fi
217+ ;;
166218sync)
219+ vm_ensure_run_checkout
167220 local_head=" $( git -C " $ROOT " rev-parse --verify HEAD) "
168221 WIN_MANIFEST=" $( mktemp " ${TMPDIR:-/ tmp} /cbm-vm-manifest.XXXXXX" ) "
169222 WIN_ARCHIVE=" $( mktemp " ${TMPDIR:-/ tmp} /cbm-vm-worktree.XXXXXX.tar" ) "
@@ -172,25 +225,25 @@ sync)
172225 cbm_vm_write_untracked_manifest " $ROOT " " $WIN_MANIFEST "
173226 COPYFILE_DISABLE=1 tar --no-xattrs --no-mac-metadata \
174227 -C " $ROOT " --null -T " $WIN_MANIFEST " -cf " $WIN_ARCHIVE "
175- remote_head=" $( vm clangarm64 " cd /c/cbm && git rev-parse --verify HEAD" ) "
228+ remote_head=" $( vm clangarm64 " cd $VM_REPO && git rev-parse --verify HEAD" ) "
176229 remote_head=" ${remote_head// $' \r ' / } "
177230 if [ " $remote_head " != " $local_head " ]; then
178231 echo " FATAL: Windows VM is at $remote_head , expected local HEAD $local_head ; run win.sh update first." >&2
179232 exit 1
180233 fi
181234 vm clangarm64 \
182- " cd /c/cbm && git reset --hard '$local_head ' && git clean -fdx"
235+ " cd $VM_REPO && git reset --hard '$local_head ' && git clean -fdx"
183236 if [ -s " $WIN_PATCH " ]; then
184237 " ${SSH[@]} " \
185- ' C:\msys64\msys2_shell.cmd -defterm -no-start -clangarm64 -c "cd /c/cbm && git apply --binary --whitespace=nowarn -"' \
238+ ' C:\msys64\msys2_shell.cmd -defterm -no-start -clangarm64 -c "cd $VM_REPO && git apply --binary --whitespace=nowarn -"' \
186239 < " $WIN_PATCH "
187240 fi
188241 if [ -s " $WIN_MANIFEST " ]; then
189242 " ${SSH[@]} " \
190- ' C:\msys64\msys2_shell.cmd -defterm -no-start -clangarm64 -c "cd /c/cbm && tar -xf -"' \
243+ ' C:\msys64\msys2_shell.cmd -defterm -no-start -clangarm64 -c "cd $VM_REPO && tar -xf -"' \
191244 < " $WIN_ARCHIVE "
192245 fi
193- vm clangarm64 " cd /c/cbm && git status --short --branch"
246+ vm clangarm64 " cd $VM_REPO && git status --short --branch"
194247 win_cleanup
195248 exec " $0 " build
196249 ;;
@@ -201,7 +254,7 @@ build)
201254 # NOT via CC='ccache clang', which bypassed the verified-cache env layer.
202255 # The test-runner is no longer built here: the test leg (scripts/test.sh)
203256 # builds its own runner, same as CI's test jobs.
204- vm clangarm64 " cd /c/cbm && scripts/build.sh CC=clang CXX=clang++ > /tmp/win-build.log 2>&1 && echo BUILD_OK || (echo BUILD_FAIL; tail -20 /tmp/win-build.log; exit 1)"
257+ vm clangarm64 " cd $VM_REPO && scripts/build.sh CC=clang CXX=clang++ > /tmp/win-build.log 2>&1 && echo BUILD_OK || (echo BUILD_FAIL; tail -20 /tmp/win-build.log; exit 1)"
205258 ;;
206259test)
207260 [ $# -ge 1 ] || { echo " usage: win.sh test <suite...>" >&2 ; exit 2; }
@@ -210,7 +263,12 @@ test)
210263 # protected per-user temp root via vm-run-tests.sh, which streams FULL
211264 # output and refuses to report success without the runner's completion
212265 # summary. A `| tail -40` here once hid 40 real Windows failures.
213- vm clangarm64 " cd /c/cbm && bash test-infrastructure/vm/vm-run-tests.sh $* "
266+ # CBM_VM_EXPECT_HEAD=<sha> asserts the tree is the one the caller means
267+ # before spending a leg on it — cheap insurance against a concurrent
268+ # session having re-synced the shared checkout.
269+ [ -z " ${CBM_VM_EXPECT_HEAD:- } " ] || vm_assert_head " $CBM_VM_EXPECT_HEAD "
270+ vm clangarm64 " cd $VM_REPO && bash test-infrastructure/vm/vm-run-tests.sh $* "
271+ [ -z " ${CBM_VM_EXPECT_HEAD:- } " ] || vm_assert_head " $CBM_VM_EXPECT_HEAD "
214272 ;;
215273guards)
216274 # Match the Windows CI product build: a clean, embedded-UI product binary.
@@ -222,7 +280,7 @@ guards)
222280 # needs the official MSVC Node.js (/c/node, from provision-windows.sh):
223281 # MSVC-built npm native modules cannot resolve Node-API symbols against
224282 # MSYS2's mingw node.
225- vm clangarm64 " export PATH=/c/node:\$ PATH && cd /c/cbm && scripts/build.sh --with-ui CC=clang CXX=clang++ SANITIZE= BUILD_DIR=build/guards"
283+ vm clangarm64 " export PATH=/c/node:\$ PATH && cd $VM_REPO && scripts/build.sh --with-ui CC=clang CXX=clang++ SANITIZE= BUILD_DIR=build/guards"
226284 # The guards themselves run through plain cmd/PowerShell, exactly like the
227285 # CI job: under the MSYS2 shell TMP is C:\msys64\tmp, whose ancestry
228286 # grants mutation rights to Authenticated Users — the daemon's
@@ -238,14 +296,14 @@ smoke-install)
238296 # build/c (including any test-runner) just as CI's separate jobs imply;
239297 # ccache keeps the rebuild to minutes — parity was the explicit user call
240298 # over incremental speed here.
241- vm clangarm64 " cd /c/cbm && scripts/build.sh CC=clang CXX=clang++ && bash test-infrastructure/vm/vm-smoke.sh"
299+ vm clangarm64 " cd $VM_REPO && scripts/build.sh CC=clang CXX=clang++ && bash test-infrastructure/vm/vm-smoke.sh"
242300 ;;
243301smoke-artifact)
244302 # The release-archive flow with local bytes: canonical build → the ONE
245303 # package-release.sh → extract → vm-smoke.sh in artifact mode — the same
246304 # sequence _build.yml + _smoke.yml run remotely, so archive-layout bugs
247305 # surface here instead of in a release dry run.
248- vm clangarm64 " cd /c/cbm && bash scripts/ci/smoke-artifact.sh windows arm64 CC=clang CXX=clang++"
306+ vm clangarm64 " cd $VM_REPO && bash scripts/ci/smoke-artifact.sh windows arm64 CC=clang CXX=clang++"
249307 ;;
250308soak)
251309 duration=" ${1:- 10} "
@@ -256,7 +314,7 @@ soak)
256314 echo " usage: win.sh soak [positive-minutes]" >&2
257315 exit 2
258316 fi
259- vm clangarm64 " cd /c/cbm && CBM_VM_TEST_LOG=/tmp/win-soak.log bash \
317+ vm clangarm64 " cd $VM_REPO && CBM_VM_TEST_LOG=/tmp/win-soak.log bash \
260318 test-infrastructure/vm/vm-run-tests.sh --soak '$duration '"
261319 ;;
262320sh)
@@ -273,11 +331,11 @@ ubsan-build)
273331 # Validated: UBSan needs no interceptors, so it builds, runs, AND reports
274332 # correctly under emulation. (ASan does NOT: no aarch64 runtime exists and
275333 # the x86_64 runtime faults in emulated process-init — ASan stays CI-only.)
276- vm clang64 " cd /c/cbm && make -j${JOBS} -f Makefile.cbm CC=clang CXX=clang++ SANITIZE='-fsanitize=undefined -fno-omit-frame-pointer' build/c/test-runner > /tmp/win-ubsan-build.log 2>&1 && echo UBSAN_BUILD_OK || (echo UBSAN_BUILD_FAIL; tail -20 /tmp/win-ubsan-build.log; exit 1)"
334+ vm clang64 " cd $VM_REPO && make -j${JOBS} -f Makefile.cbm CC=clang CXX=clang++ SANITIZE='-fsanitize=undefined -fno-omit-frame-pointer' build/c/test-runner > /tmp/win-ubsan-build.log 2>&1 && echo UBSAN_BUILD_OK || (echo UBSAN_BUILD_FAIL; tail -20 /tmp/win-ubsan-build.log; exit 1)"
277335 ;;
278336ubsan-test)
279337 [ $# -ge 1 ] || { echo " usage: win.sh ubsan-test <suite...>" >&2 ; exit 2; }
280- vm clang64 " cd /c/cbm && CBM_VM_TEST_LOG=/tmp/win-ubsan-test.log bash test-infrastructure/vm/vm-run-tests.sh $* "
338+ vm clang64 " cd $VM_REPO && CBM_VM_TEST_LOG=/tmp/win-ubsan-test.log bash test-infrastructure/vm/vm-run-tests.sh $* "
281339 ;;
282340trap-ubsan-build)
283341 # NATIVE ARM64 UBSan via trap mode. -fsanitize-trap=undefined needs NO
@@ -289,13 +347,13 @@ trap-ubsan-build)
289347 # fired, reproduce under the emulated `win.sh ubsan-build`/`ubsan-test`,
290348 # which carries the full runtime + message. BUILD_DIR isolated so it never
291349 # clobbers the plain test-runner.
292- vm clangarm64 " cd /c/cbm && make -j${JOBS} -f Makefile.cbm CC='ccache clang' CXX='ccache clang++' SANITIZE='-fsanitize=undefined -fsanitize-trap=undefined -fstack-protector-strong -fno-omit-frame-pointer' BUILD_DIR=build/trap-ubsan build/trap-ubsan/test-runner > /tmp/win-trap-ubsan-build.log 2>&1 && echo TRAP_UBSAN_BUILD_OK || (echo TRAP_UBSAN_BUILD_FAIL; tail -20 /tmp/win-trap-ubsan-build.log; exit 1)"
350+ vm clangarm64 " cd $VM_REPO && make -j${JOBS} -f Makefile.cbm CC='ccache clang' CXX='ccache clang++' SANITIZE='-fsanitize=undefined -fsanitize-trap=undefined -fstack-protector-strong -fno-omit-frame-pointer' BUILD_DIR=build/trap-ubsan build/trap-ubsan/test-runner > /tmp/win-trap-ubsan-build.log 2>&1 && echo TRAP_UBSAN_BUILD_OK || (echo TRAP_UBSAN_BUILD_FAIL; tail -20 /tmp/win-trap-ubsan-build.log; exit 1)"
293351 ;;
294352trap-ubsan-test)
295353 [ $# -ge 1 ] || { echo " usage: win.sh trap-ubsan-test <suite...>" >&2 ; exit 2; }
296354 # A UB trap crashes the runner with SIGILL (exit 132); the harness reports
297355 # the failing suite so the emulated diagnosis loop can name the check.
298- vm clangarm64 " cd /c/cbm && CBM_VM_RUNNER=build/trap-ubsan/test-runner CBM_VM_TEST_LOG=/tmp/win-trap-ubsan-test.log bash test-infrastructure/vm/vm-run-tests.sh $* "
356+ vm clangarm64 " cd $VM_REPO && CBM_VM_RUNNER=build/trap-ubsan/test-runner CBM_VM_TEST_LOG=/tmp/win-trap-ubsan-test.log bash test-infrastructure/vm/vm-run-tests.sh $* "
299357 ;;
300358pageheap)
301359 # OS-level heap verification (page-granular overflow/UAF detection) for the
@@ -318,7 +376,7 @@ test-par)
318376 # under the CI-shaped protected temp root with FULL output (this leg once
319377 # ran under the MSYS-shared /tmp and piped through `tail -25` — the same
320378 # truncated-blindness class that hid 40 Windows failures from `test`).
321- vm clangarm64 " cd /c/cbm && CBM_VM_TEST_LOG=/tmp/win-test-par.log bash test-infrastructure/vm/vm-run-tests.sh --par"
379+ vm clangarm64 " cd $VM_REPO && CBM_VM_TEST_LOG=/tmp/win-test-par.log bash test-infrastructure/vm/vm-run-tests.sh --par"
322380 ;;
323381help | -h | --help)
324382 print_help
0 commit comments