Skip to content

Commit 706d9cc

Browse files
committed
Override the default log file path by environment variable MAGIC_CONTEXT_LOG_PATH
1 parent f3e681b commit 706d9cc

5 files changed

Lines changed: 30 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ Magic Context also writes to a few other locations:
260260
|---|---|---|
261261
| `~/.local/share/cortexkit/magic-context/context.db` | SQLite database — tags, compartments, memories, all durable state (XDG-equivalent on Windows) | **Must persist.** Losing it loses your memory/history. |
262262
| `~/.local/share/cortexkit/magic-context/models/` | Local embedding model cache (~90 MB `Xenova/all-MiniLM-L6-v2` ONNX), downloaded on first use when local embeddings are enabled | Should persist, else re-downloaded each run. Not used when `memory.enabled: false` or an `openai_compatible`/`ollama` embedding backend is configured. |
263-
| `${TMPDIR}/opencode/magic-context/magic-context.log` (`pi/` for Pi) | Diagnostic log | Disposable. |
263+
| `MAGIC_CONTEXT_LOG_PATH` (fallback: `${TMPDIR}/opencode/magic-context/magic-context.log`, `pi/` for Pi) | Diagnostic log | Disposable. |
264264

265265
**Sandboxed / ephemeral environments (Docker, CI, disposable containers):** mount the `~/.local/share/cortexkit/magic-context/` directory on a persistent volume so the database and model cache survive between runs. If only the model cache is ephemeral, the model is simply re-downloaded; if the database is ephemeral, memory and history don't accumulate. To avoid the ~90 MB model download entirely, set `memory.enabled: false` or point `embedding` at a remote `openai_compatible`/`ollama` backend.
266266

packages/dashboard/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ packages/dashboard/
7171
The dashboard reads from the same SQLite database the plugin writes to:
7272
- **Database**: `~/.local/share/opencode/storage/plugin/magic-context/context.db`
7373
- **Config**: `~/.config/opencode/magic-context.jsonc`
74-
- **Logs**: `/tmp/magic-context.log`
74+
- **Logs**: `MAGIC_CONTEXT_LOG_PATH` (fallback: `${TMPDIR}/opencode/magic-context/magic-context.log`, `pi/` for Pi)
7575

7676
Database access uses WAL mode for safe concurrent reads while the plugin writes. Write operations (memory edits, dream queue entries) use `busy_timeout` to handle contention.
7777

packages/docs/src/content/docs/reference/dashboard.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ Use [Configuration](/reference/configuration/) for the full generated key refere
101101

102102
## Logs
103103

104-
Optional **log tail** for `magic-context.log` with filtering — useful alongside Cache when correlating busts with plugin log lines. Open from the sidebar **Logs** item.
104+
Optional **log tail** for `MAGIC_CONTEXT_LOG_PATH` (fallback: `${TMPDIR}/opencode/magic-context/magic-context.log`) with filtering — useful alongside Cache when correlating busts with plugin log lines. Open from the sidebar **Logs** item.

packages/plugin/src/shared/data-path.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getCacheDir,
66
getDataDir,
77
getLegacyOpenCodeMagicContextStorageDir,
8+
getMagicContextLogPath,
89
getMagicContextStorageDir,
910
getOpenCodeCacheDir,
1011
getOpenCodeStorageDir,
@@ -16,17 +17,20 @@ const savedEnv = {
1617
XDG_CACHE_HOME: process.env.XDG_CACHE_HOME,
1718
XDG_DATA_HOME: process.env.XDG_DATA_HOME,
1819
LOCALAPPDATA: process.env.LOCALAPPDATA,
20+
MAGIC_CONTEXT_LOG_PATH: process.env.MAGIC_CONTEXT_LOG_PATH,
1921
};
2022

2123
describe("data-path", () => {
2224
beforeEach(() => {
2325
process.env.XDG_CACHE_HOME = undefined;
2426
process.env.XDG_DATA_HOME = undefined;
2527
process.env.LOCALAPPDATA = undefined;
28+
process.env.MAGIC_CONTEXT_LOG_PATH = undefined;
2629
// Bun's env handling: explicit delete for unset
2730
delete process.env.XDG_CACHE_HOME;
2831
delete process.env.XDG_DATA_HOME;
2932
delete process.env.LOCALAPPDATA;
33+
delete process.env.MAGIC_CONTEXT_LOG_PATH;
3034
});
3135

3236
afterEach(() => {
@@ -35,6 +39,9 @@ describe("data-path", () => {
3539
if (savedEnv.XDG_DATA_HOME !== undefined)
3640
process.env.XDG_DATA_HOME = savedEnv.XDG_DATA_HOME;
3741
if (savedEnv.LOCALAPPDATA !== undefined) process.env.LOCALAPPDATA = savedEnv.LOCALAPPDATA;
42+
if (savedEnv.MAGIC_CONTEXT_LOG_PATH !== undefined)
43+
process.env.MAGIC_CONTEXT_LOG_PATH = savedEnv.MAGIC_CONTEXT_LOG_PATH;
44+
else delete process.env.MAGIC_CONTEXT_LOG_PATH;
3845
});
3946

4047
test("getCacheDir falls back to <homedir>/.cache when XDG_CACHE_HOME is unset (all platforms)", () => {
@@ -156,4 +163,22 @@ describe("data-path", () => {
156163
path.join("/some/project/", ".opencode", "magic-context"),
157164
);
158165
});
166+
167+
test("getMagicContextLogPath falls back to harness temp dir when env unset", () => {
168+
expect(getMagicContextLogPath("opencode")).toBe(
169+
path.join(os.tmpdir(), "opencode", "magic-context", "magic-context.log"),
170+
);
171+
});
172+
173+
test("getMagicContextLogPath honors MAGIC_CONTEXT_LOG_PATH", () => {
174+
process.env.MAGIC_CONTEXT_LOG_PATH = "/tmp/custom/magic-context.log";
175+
expect(getMagicContextLogPath("pi")).toBe("/tmp/custom/magic-context.log");
176+
});
177+
178+
test("getMagicContextLogPath ignores blank MAGIC_CONTEXT_LOG_PATH", () => {
179+
process.env.MAGIC_CONTEXT_LOG_PATH = " ";
180+
expect(getMagicContextLogPath("pi")).toBe(
181+
path.join(os.tmpdir(), "pi", "magic-context", "magic-context.log"),
182+
);
183+
});
159184
});

packages/plugin/src/shared/data-path.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export function getMagicContextTempDir(harness: HarnessId = getHarness()): strin
4545
* reflected in the next flush.
4646
*/
4747
export function getMagicContextLogPath(harness: HarnessId = getHarness()): string {
48+
const envPath = process.env.MAGIC_CONTEXT_LOG_PATH?.trim();
49+
if (envPath) return envPath;
4850
return path.join(getMagicContextTempDir(harness), "magic-context.log");
4951
}
5052

0 commit comments

Comments
 (0)