-
Notifications
You must be signed in to change notification settings - Fork 81
fix(storage): honour MAGIC_CONTEXT_TEST_DATA_DIR in the shared storage resolver #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ualtinok
merged 3 commits into
cortexkit:master
from
randomvariable:fix/storage-test-isolation
Aug 10, 2026
+135
−81
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
6c9a05f
fix(storage): honour MAGIC_CONTEXT_TEST_DATA_DIR in the shared storag…
randomvariable 9cfcad5
fix(storage): hoist the NODE_ENV test backstop into the storage resolver
randomvariable 86a7d56
test(data-path): restore-or-delete every guard var the suite lifts
randomvariable File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import * as os from "node:os"; | ||
| import * as path from "node:path"; | ||
| import { getHarness, type HarnessId } from "./harness"; | ||
|
|
@@ -167,11 +167,76 @@ export function getOpenCodeStorageDir(): string { | |
| * - Future cross-harness session migration | ||
| * | ||
| * Layout: <XDG_DATA_HOME>/cortexkit/magic-context/ | ||
| * | ||
| * TEST-ISOLATION GUARD. `openDatabase()` has been guarded in | ||
| * `resolveDatabasePath()` since the 2026-06-01 (v26) and 2026-06-19 (v41) | ||
| * incidents, in which unisolated tests migrated the user's REAL shared DB. | ||
| * Every OTHER caller bypassed that guard — notably the CLI doctors, which | ||
| * build `join(getMagicContextStorageDir(), "context.db")` themselves and run | ||
| * `PRAGMA integrity_check` against it. A test that deletes XDG_DATA_HOME to | ||
| * exercise path fallbacks cannot restore isolation by setting | ||
| * `process.env.HOME`: bun caches `os.homedir()` at startup, so `getDataDir()` | ||
| * still resolves to the real home. MAGIC_CONTEXT_TEST_DATA_DIR — set by | ||
| * `packages/plugin/test-preload.ts`, and restored by any test that touches it | ||
| * — is honored here, so the preload's "cannot be defeated" guarantee holds for | ||
| * every caller, not just `openDatabase()`. It is never set in production. | ||
| * | ||
| * CWD-INDEPENDENT BACKSTOP. The preload only runs when `bun test`'s CWD has a | ||
| * bunfig wiring `[test] preload`. A run from a dir WITHOUT that wiring (a | ||
| * package missing its bunfig, or a brand-new one) executes every *.test.ts with | ||
| * NO preload, and this resolver would hand back the REAL shared path — which is | ||
| * how the live DB reached v41. Bun sets NODE_ENV=test for EVERY `bun test` | ||
| * regardless of CWD, and production never sets it, so that window redirects to | ||
| * a memoized throwaway dir instead. It lives here rather than in | ||
| * `resolveDatabasePath()` so direct callers (the CLI doctors' own | ||
| * `PRAGMA integrity_check`, announcements, the models.dev cache) are covered | ||
| * too — they never go through the DB resolver. | ||
| * | ||
| * XDG_DATA_HOME still wins over both: a test that manages its own data home is | ||
| * already controlled, and production has no test dir set at all. | ||
| */ | ||
| export function getMagicContextStorageDir(): string { | ||
| if (!process.env.XDG_DATA_HOME) { | ||
| const testDataDir = process.env.MAGIC_CONTEXT_TEST_DATA_DIR; | ||
| if (testDataDir) { | ||
| return path.join(testDataDir, "cortexkit", "magic-context"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Tests that unset Prompt for AI agents |
||
| } | ||
| if (process.env.NODE_ENV === "test") { | ||
| return getTestBackstopStorageDir(); | ||
| } | ||
| } | ||
| return path.join(getDataDir(), "cortexkit", "magic-context"); | ||
| } | ||
|
|
||
| let testBackstopStorageDir: string | null = null; | ||
| let testBackstopWarned = false; | ||
|
|
||
| /** | ||
| * Memoized per process so repeated calls in the same unisolated test resolve to | ||
| * the SAME path — `openDatabase()` caches by path, and a fresh temp dir per call | ||
| * would defeat that cache and hand back different DB handles. | ||
| */ | ||
| function getTestBackstopStorageDir(): string { | ||
| if (!testBackstopStorageDir) { | ||
| testBackstopStorageDir = path.join( | ||
| mkdtempSync(path.join(os.tmpdir(), "mc-test-db-backstop-")), | ||
| "cortexkit", | ||
| "magic-context", | ||
| ); | ||
| } | ||
| if (!testBackstopWarned) { | ||
| testBackstopWarned = true; | ||
| // Deliberately console, not the logger: logger.ts imports this module. | ||
| console.warn( | ||
| "[magic-context] TEST BACKSTOP: NODE_ENV=test with no MAGIC_CONTEXT_TEST_DATA_DIR " + | ||
| `— redirecting storage to a throwaway temp dir (${testBackstopStorageDir}) so no ` + | ||
| "test can touch the user's real shared database. Wire `[test] preload` in this " + | ||
| "package's bunfig.toml.", | ||
| ); | ||
| } | ||
| return testBackstopStorageDir; | ||
| } | ||
|
|
||
| /** | ||
| * Legacy magic-context storage directory used by the OpenCode plugin before the | ||
| * shared cortexkit path. Used only for one-time migration of existing data into | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.