Commit 11a5b70
committed
fix: CWD-independent test DB-isolation backstop (prevents live-DB migration)
ROOT CAUSE of the live-DB v41 fail-close incident (a re-run of the 2026-06-01
v26 incident): the MAGIC_CONTEXT_TEST_DATA_DIR isolation that keeps `bun test`
off the user's REAL shared DB (~/.local/share/cortexkit/magic-context/context.db)
is set by test-preload.ts, wired ONLY through each package's bunfig.toml
`[test] preload`. Two CWD holes:
- the monorepo ROOT bunfig.toml had no `[test] preload`, so a bare `bun test`
from the repo/worktree root recursively ran every package's *.test.ts with
NO isolation;
- packages/cli had no bunfig.toml at all.
A D19 mason worktree at LATEST_SUPPORTED_VERSION=41 ran `bun test`; an unisolated
run called bare openDatabase() → resolved to the real DB → ran migration v41 on
it → the user's v40 binaries fail-closed on restart. (v41 only ensureColumn'd one
nullable TEXT column, so the data was safe; user rolled the migrations row back.)
CLASS-ELIMINATION FIX (not a per-CWD patch): Bun sets NODE_ENV=test for EVERY
`bun test` regardless of CWD/bunfig (verified), and it is never "test" in the
plugin runtime. resolveDatabasePath() now has a backstop: when NODE_ENV=test and
neither MAGIC_CONTEXT_TEST_DATA_DIR nor XDG_DATA_HOME is set (the dangerous
window), it redirects the DB into a memoized throwaway temp dir so the real
shared DB is physically unreachable from ANY CWD — even a brand-new package that
forgets its bunfig. Empirically proven: with the preload env deleted mid-test,
resolveDatabasePath returns a mc-test-db-backstop-* path, never ~/.local/share.
Defense in depth: added `[test] preload` to the root bunfig.toml and a new
packages/cli/bunfig.toml (both pointing at the canonical plugin test-preload.ts).
Added a structural guard test asserting (a) the backstop redirects when
unisolated, (b) a test's own XDG_DATA_HOME is still honored, and (c) all four
test packages wire the preload — so a future package that forgets it fails CI.
Gate: plugin 2227/0 (+14), CLI 185/0, Pi 487/0, tsc + biome clean.1 parent 65fe58f commit 11a5b70
4 files changed
Lines changed: 140 additions & 5 deletions
File tree
- packages
- cli
- plugin/src/features/magic-context
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
Lines changed: 63 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
| 4 | + | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
10 | 15 | | |
11 | 16 | | |
12 | 17 | | |
| |||
302 | 307 | | |
303 | 308 | | |
304 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
305 | 365 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
48 | 51 | | |
49 | 52 | | |
50 | 53 | | |
| |||
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
70 | 110 | | |
71 | 111 | | |
72 | 112 | | |
73 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
74 | 127 | | |
75 | 128 | | |
76 | 129 | | |
| |||
0 commit comments