@cortexkit/opencode-magic-context 0.35.1 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +9 -5
  2. package/dist/agents/permissions.d.ts +17 -0
  3. package/dist/agents/permissions.d.ts.map +1 -1
  4. package/dist/{announcement-gms2nhya.js → announcement-6c1g4es4.js} +2 -2
  5. package/dist/config/schema/magic-context.d.ts +10 -0
  6. package/dist/config/schema/magic-context.d.ts.map +1 -1
  7. package/dist/{conflict-warning-hook-p2pmhvn7.js → conflict-warning-hook-dw9fs934.js} +4 -4
  8. package/dist/{derive-budgets-9crp0cvd.js → derive-budgets-88mxk2w1.js} +3 -3
  9. package/dist/features/magic-context/dreamer/evaluate-smart-notes.d.ts +2 -0
  10. package/dist/features/magic-context/dreamer/evaluate-smart-notes.d.ts.map +1 -1
  11. package/dist/features/magic-context/dreamer/task-executor.d.ts +1 -0
  12. package/dist/features/magic-context/dreamer/task-executor.d.ts.map +1 -1
  13. package/dist/features/magic-context/migrations.d.ts.map +1 -1
  14. package/dist/features/magic-context/smart-notes/condition-compiler.d.ts +37 -0
  15. package/dist/features/magic-context/smart-notes/condition-compiler.d.ts.map +1 -0
  16. package/dist/features/magic-context/smart-notes/runner.d.ts +1 -0
  17. package/dist/features/magic-context/smart-notes/runner.d.ts.map +1 -1
  18. package/dist/features/magic-context/smart-notes/storage.d.ts +3 -3
  19. package/dist/features/magic-context/smart-notes/storage.d.ts.map +1 -1
  20. package/dist/features/magic-context/storage-db.d.ts +1 -1
  21. package/dist/features/magic-context/storage-db.d.ts.map +1 -1
  22. package/dist/features/magic-context/storage-notes.d.ts +13 -0
  23. package/dist/features/magic-context/storage-notes.d.ts.map +1 -1
  24. package/dist/features/magic-context/storage.d.ts +1 -1
  25. package/dist/features/magic-context/storage.d.ts.map +1 -1
  26. package/dist/hooks/magic-context/hook.d.ts +3 -0
  27. package/dist/hooks/magic-context/hook.d.ts.map +1 -1
  28. package/dist/hooks/magic-context/lkg-slot.d.ts +15 -1
  29. package/dist/hooks/magic-context/lkg-slot.d.ts.map +1 -1
  30. package/dist/hooks/magic-context/module-state-sync.d.ts +6 -0
  31. package/dist/hooks/magic-context/module-state-sync.d.ts.map +1 -1
  32. package/dist/hooks/magic-context/module-transport.d.ts +9 -0
  33. package/dist/hooks/magic-context/module-transport.d.ts.map +1 -1
  34. package/dist/hooks/magic-context/raw-fallback-context-limit.d.ts +10 -0
  35. package/dist/hooks/magic-context/raw-fallback-context-limit.d.ts.map +1 -0
  36. package/dist/hooks/magic-context/read-session-formatting.d.ts.map +1 -1
  37. package/dist/hooks/magic-context/rust-mode-transform.d.ts +7 -2
  38. package/dist/hooks/magic-context/rust-mode-transform.d.ts.map +1 -1
  39. package/dist/{index-8aaadh98.js → index-37rt2bka.js} +2 -2
  40. package/dist/{index-fkyamz2w.js → index-3nfgbsp4.js} +99 -56
  41. package/dist/{index-w0nygb4p.js → index-3tnkymva.js} +2 -2
  42. package/dist/{index-k1esqfwk.js → index-f1tnb3vm.js} +1 -1
  43. package/dist/{index-kvrcg31b.js → index-mmrhhk9a.js} +8 -1
  44. package/dist/{index-h5zjfesc.js → index-nsx2n9eg.js} +3 -3
  45. package/dist/{index-dmr999a2.js → index-qf20mv7e.js} +1 -1
  46. package/dist/{index-73xz0247.js → index-tsgs7td8.js} +1 -1
  47. package/dist/{index-ry8rh8cv.js → index-wgvxkqpx.js} +5 -4
  48. package/dist/{index-dpdnjawy.js → index-z8v31d85.js} +5 -5
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +781 -116
  51. package/dist/plugin/dream-timer.d.ts +1 -0
  52. package/dist/plugin/dream-timer.d.ts.map +1 -1
  53. package/dist/plugin/hooks/create-session-hooks.d.ts +3 -0
  54. package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
  55. package/dist/plugin/messages-transform.d.ts +5 -5
  56. package/dist/plugin/messages-transform.d.ts.map +1 -1
  57. package/dist/{recomp-orchestrator-29f4rqax.js → recomp-orchestrator-7wbbvf87.js} +7 -7
  58. package/dist/{safe-notification-target-q9jk5n44.js → safe-notification-target-c11fdda2.js} +3 -3
  59. package/dist/{send-session-notification-sr5g06hz.js → send-session-notification-bgtf4853.js} +4 -4
  60. package/dist/shared/data-path.d.ts +27 -0
  61. package/dist/shared/data-path.d.ts.map +1 -1
  62. package/dist/shared/harness-provider-map.d.ts +23 -20
  63. package/dist/shared/harness-provider-map.d.ts.map +1 -1
  64. package/dist/shared/prompt-surface-runtime.d.ts +15 -2
  65. package/dist/shared/prompt-surface-runtime.d.ts.map +1 -1
  66. package/dist/{storage-meta-session-ev8b55a9.js → storage-meta-session-kh5egr11.js} +4 -4
  67. package/dist/tools/ctx-note/tools.d.ts.map +1 -1
  68. package/package.json +3 -3
  69. package/src/agents/permissions.ts +87 -0
  70. package/src/config/compaction-accessor-guard.test.ts +9 -4
  71. package/src/config/schema/magic-context.test.ts +2 -0
  72. package/src/config/schema/magic-context.ts +15 -0
  73. package/src/shared/data-path.test.ts +65 -9
  74. package/src/shared/data-path.ts +66 -1
  75. package/src/shared/harness-provider-map.test.ts +42 -1
  76. package/src/shared/harness-provider-map.ts +48 -26
  77. package/src/shared/prompt-surface-runtime.ts +13 -5
@@ -1,4 +1,4 @@
1
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
1
+ import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
2
2
  import * as os from "node:os";
3
3
  import * as path from "node:path";
4
4
  import { getHarness, type HarnessId } from "./harness";
@@ -167,11 +167,76 @@ export function getOpenCodeStorageDir(): string {
167
167
  * - Future cross-harness session migration
168
168
  *
169
169
  * Layout: <XDG_DATA_HOME>/cortexkit/magic-context/
170
+ *
171
+ * TEST-ISOLATION GUARD. `openDatabase()` has been guarded in
172
+ * `resolveDatabasePath()` since the 2026-06-01 (v26) and 2026-06-19 (v41)
173
+ * incidents, in which unisolated tests migrated the user's REAL shared DB.
174
+ * Every OTHER caller bypassed that guard — notably the CLI doctors, which
175
+ * build `join(getMagicContextStorageDir(), "context.db")` themselves and run
176
+ * `PRAGMA integrity_check` against it. A test that deletes XDG_DATA_HOME to
177
+ * exercise path fallbacks cannot restore isolation by setting
178
+ * `process.env.HOME`: bun caches `os.homedir()` at startup, so `getDataDir()`
179
+ * still resolves to the real home. MAGIC_CONTEXT_TEST_DATA_DIR — set by
180
+ * `packages/plugin/test-preload.ts`, and restored by any test that touches it
181
+ * — is honored here, so the preload's "cannot be defeated" guarantee holds for
182
+ * every caller, not just `openDatabase()`. It is never set in production.
183
+ *
184
+ * CWD-INDEPENDENT BACKSTOP. The preload only runs when `bun test`'s CWD has a
185
+ * bunfig wiring `[test] preload`. A run from a dir WITHOUT that wiring (a
186
+ * package missing its bunfig, or a brand-new one) executes every *.test.ts with
187
+ * NO preload, and this resolver would hand back the REAL shared path — which is
188
+ * how the live DB reached v41. Bun sets NODE_ENV=test for EVERY `bun test`
189
+ * regardless of CWD, and production never sets it, so that window redirects to
190
+ * a memoized throwaway dir instead. It lives here rather than in
191
+ * `resolveDatabasePath()` so direct callers (the CLI doctors' own
192
+ * `PRAGMA integrity_check`, announcements, the models.dev cache) are covered
193
+ * too — they never go through the DB resolver.
194
+ *
195
+ * XDG_DATA_HOME still wins over both: a test that manages its own data home is
196
+ * already controlled, and production has no test dir set at all.
170
197
  */
171
198
  export function getMagicContextStorageDir(): string {
199
+ if (!process.env.XDG_DATA_HOME) {
200
+ const testDataDir = process.env.MAGIC_CONTEXT_TEST_DATA_DIR;
201
+ if (testDataDir) {
202
+ return path.join(testDataDir, "cortexkit", "magic-context");
203
+ }
204
+ if (process.env.NODE_ENV === "test") {
205
+ return getTestBackstopStorageDir();
206
+ }
207
+ }
172
208
  return path.join(getDataDir(), "cortexkit", "magic-context");
173
209
  }
174
210
 
211
+ let testBackstopStorageDir: string | null = null;
212
+ let testBackstopWarned = false;
213
+
214
+ /**
215
+ * Memoized per process so repeated calls in the same unisolated test resolve to
216
+ * the SAME path — `openDatabase()` caches by path, and a fresh temp dir per call
217
+ * would defeat that cache and hand back different DB handles.
218
+ */
219
+ function getTestBackstopStorageDir(): string {
220
+ if (!testBackstopStorageDir) {
221
+ testBackstopStorageDir = path.join(
222
+ mkdtempSync(path.join(os.tmpdir(), "mc-test-db-backstop-")),
223
+ "cortexkit",
224
+ "magic-context",
225
+ );
226
+ }
227
+ if (!testBackstopWarned) {
228
+ testBackstopWarned = true;
229
+ // Deliberately console, not the logger: logger.ts imports this module.
230
+ console.warn(
231
+ "[magic-context] TEST BACKSTOP: NODE_ENV=test with no MAGIC_CONTEXT_TEST_DATA_DIR " +
232
+ `— redirecting storage to a throwaway temp dir (${testBackstopStorageDir}) so no ` +
233
+ "test can touch the user's real shared database. Wire `[test] preload` in this " +
234
+ "package's bunfig.toml.",
235
+ );
236
+ }
237
+ return testBackstopStorageDir;
238
+ }
239
+
175
240
  /**
176
241
  * Legacy magic-context storage directory used by the OpenCode plugin before the
177
242
  * shared cortexkit path. Used only for one-time migration of existing data into
@@ -1,5 +1,10 @@
1
1
  import { describe, expect, it } from "bun:test";
2
- import { piModelRefToCanonical, resolveModelRefForPi } from "./harness-provider-map";
2
+ import {
3
+ ompModelRefToCanonical,
4
+ piModelRefToCanonical,
5
+ resolveModelRefForOmp,
6
+ resolveModelRefForPi,
7
+ } from "./harness-provider-map";
3
8
 
4
9
  describe("harness-provider-map", () => {
5
10
  describe("resolveModelRefForPi (canonical -> Pi, used when spawning)", () => {
@@ -61,3 +66,39 @@ describe("harness-provider-map", () => {
61
66
  });
62
67
  });
63
68
  });
69
+
70
+ describe("OMP provider boundary", () => {
71
+ const representativeRefs = [
72
+ ["openai/gpt-5.5", "openai-codex/gpt-5.5"],
73
+ ["google/antigravity/gemini-3.5-flash", "google-antigravity/antigravity/gemini-3.5-flash"],
74
+ ["anthropic/claude-opus-4-8", "anthropic/claude-opus-4-8"],
75
+ ["@scope/provider/nested/model", "@scope/provider/nested/model"],
76
+ ] as const;
77
+
78
+ it.each(
79
+ representativeRefs,
80
+ )("round-trips canonical %s through OMP selector %s", (canonical, omp) => {
81
+ expect(resolveModelRefForOmp(canonical)).toBe(omp);
82
+ expect(ompModelRefToCanonical(omp)).toBe(canonical);
83
+ });
84
+
85
+ it("normalizes an already-native OMP selector idempotently", () => {
86
+ const selector = "openai-codex/team/nested/gpt-5.5";
87
+ expect(resolveModelRefForOmp(selector)).toBe(selector);
88
+ expect(resolveModelRefForOmp(ompModelRefToCanonical(selector))).toBe(selector);
89
+ });
90
+
91
+ it("passes through provider ids that collide with Object.prototype members", () => {
92
+ for (const ref of [
93
+ "constructor/model",
94
+ "toString/model",
95
+ "__proto__/model",
96
+ "hasOwnProperty/model",
97
+ ]) {
98
+ expect(resolveModelRefForOmp(ref)).toBe(ref);
99
+ expect(ompModelRefToCanonical(ref)).toBe(ref);
100
+ expect(resolveModelRefForPi(ref)).toBe(ref);
101
+ expect(piModelRefToCanonical(ref)).toBe(ref);
102
+ }
103
+ });
104
+ });
@@ -2,54 +2,66 @@
2
2
  * Provider-id translation between the canonical (OpenCode) form stored in the
3
3
  * shared magic-context config and Pi's harness-native provider ids.
4
4
  *
5
- * OpenCode and Pi now share ONE config, but a few auth-plugin providers were
6
- * named differently on each side. The model id AFTER the slash is identical;
7
- * only the provider prefix differs:
5
+ * Provider IDs are a harness boundary, not a database identity. Shared config
6
+ * always stores the canonical OpenCode form. Each non-OpenCode harness owns an
7
+ * explicit pair of edge transforms:
8
8
  *
9
- * canonical (OpenCode) Pi's preferred auth-plugin form
10
- * -------------------- ------------------------------
9
+ * canonical (OpenCode) Pi / OMP selector
10
+ * -------------------- -----------------
11
11
  * openai/<model> openai-codex/<model>
12
12
  * google/<model> google-antigravity/<model>
13
13
  * anthropic/<model> anthropic/<model> (same; every other provider too)
14
14
  *
15
- * The mapping is intentionally not a one-to-one provider identity. Pi also
16
- * exposes plain `openai` and `google` providers for direct API keys, while the
17
- * canonical prefix does not record whether a subscription or API-key backend
18
- * should win. Pi therefore starts with the preferred auth-plugin form at the
19
- * spawn boundary and subagent-runner may retry once with the untranslated
20
- * canonical form when that form reports missing credentials. The runner caches
21
- * the winning form per provider prefix in memory only; setup writes still use
22
- * the reverse mapping below so configs remain canonical.
15
+ * Pi and OMP currently expose the same two subscription-provider aliases, but
16
+ * their exported functions remain distinct deliberately. A future OMP catalog
17
+ * rename must not silently change plain-Pi behavior (or vice versa).
23
18
  *
24
- * Canonical = OpenCode: the config always stores the OpenCode form. Pi
25
- * translates at its edges:
26
- * - read: canonical -> Pi when spawning a configured model (subagent-runner),
27
- * with the runtime credential fallback described above.
28
- * - write: Pi -> canonical in the Pi setup wizard, so a config written from
29
- * the Pi side stays readable by OpenCode.
19
+ * The mapping is intentionally not a one-to-one provider identity. Both
20
+ * harnesses also expose plain `openai` and `google` providers for direct API
21
+ * keys, while the canonical prefix does not record whether a subscription or
22
+ * API-key backend should win. The runtime starts with the preferred
23
+ * subscription form and subagent-runner may retry once with the untranslated
24
+ * canonical form when that form reports missing credentials.
30
25
  *
31
- * OpenCode needs no translation (canonical IS the OpenCode form).
26
+ * Only the provider prefix before the first slash is translated. Model IDs,
27
+ * including nested IDs containing more slashes, are preserved byte-for-byte.
28
+ * Scoped or otherwise unknown provider prefixes are identities.
29
+ *
30
+ * OpenCode needs no translation because canonical is its native form.
32
31
  */
33
32
 
34
- const CANONICAL_TO_PI_PROVIDER: Record<string, string> = {
33
+ const CANONICAL_TO_PI_PROVIDER: Readonly<Record<string, string>> = {
35
34
  openai: "openai-codex",
36
35
  google: "google-antigravity",
37
36
  };
38
37
 
39
- const PI_TO_CANONICAL_PROVIDER: Record<string, string> = {
38
+ const PI_TO_CANONICAL_PROVIDER: Readonly<Record<string, string>> = {
39
+ "openai-codex": "openai",
40
+ "google-antigravity": "google",
41
+ };
42
+
43
+ const CANONICAL_TO_OMP_PROVIDER: Readonly<Record<string, string>> = {
44
+ openai: "openai-codex",
45
+ google: "google-antigravity",
46
+ };
47
+
48
+ const OMP_TO_CANONICAL_PROVIDER: Readonly<Record<string, string>> = {
40
49
  "openai-codex": "openai",
41
50
  "google-antigravity": "google",
42
51
  };
43
52
 
44
53
  /** Remap only the provider prefix (text before the first "/"), preserving the
45
- * model id verbatim. No "/", empty provider, or unmapped provider -> unchanged. */
46
- function remapProviderPrefix(ref: string, map: Record<string, string>): string {
54
+ * model id verbatim. No "/", empty provider, or unmapped provider -> unchanged.
55
+ * Lookups are own-property only, so provider ids that collide with
56
+ * `Object.prototype` members (`constructor/model`, `toString/model`) stay
57
+ * identities instead of becoming garbage prefixes. */
58
+ function remapProviderPrefix(ref: string, map: Readonly<Record<string, string>>): string {
47
59
  if (typeof ref !== "string") return ref;
48
60
  const slash = ref.indexOf("/");
49
61
  if (slash <= 0) return ref;
50
62
  const provider = ref.slice(0, slash);
51
- const mapped = map[provider];
52
- return mapped ? `${mapped}${ref.slice(slash)}` : ref;
63
+ if (!Object.hasOwn(map, provider)) return ref;
64
+ return `${map[provider]}${ref.slice(slash)}`;
53
65
  }
54
66
 
55
67
  /** Pi-native `provider/model` -> canonical (OpenCode). Identity when unmapped.
@@ -64,3 +76,13 @@ export function piModelRefToCanonical(ref: string): string {
64
76
  export function resolveModelRefForPi(ref: string): string {
65
77
  return remapProviderPrefix(piModelRefToCanonical(ref), CANONICAL_TO_PI_PROVIDER);
66
78
  }
79
+
80
+ /** OMP-native selector -> canonical shared-config model reference. */
81
+ export function ompModelRefToCanonical(ref: string): string {
82
+ return remapProviderPrefix(ref, OMP_TO_CANONICAL_PROVIDER);
83
+ }
84
+
85
+ /** Canonical shared-config model reference -> OMP-native selector. */
86
+ export function resolveModelRefForOmp(ref: string): string {
87
+ return remapProviderPrefix(ompModelRefToCanonical(ref), CANONICAL_TO_OMP_PROVIDER);
88
+ }
@@ -23,7 +23,12 @@ import {
23
23
  const GUIDANCE_MARKER = "## Magic Context";
24
24
  const GUIDANCE_MARKER_LINE = /^## Magic Context[\t ]*\r?$/gm;
25
25
 
26
- export const PROMPT_SURFACE_TOOL_IDS = [
26
+ /**
27
+ * The single source of truth for the ctx_* tools exposed by the prompt surface.
28
+ * Consumers derive their registries and light-description catalogs from this list
29
+ * so adding a tool cannot silently skip one prompt-surface integration point.
30
+ */
31
+ export const ACTIVE_TOOL_IDS = [
27
32
  "ctx_reduce",
28
33
  "ctx_expand",
29
34
  "ctx_note",
@@ -31,15 +36,18 @@ export const PROMPT_SURFACE_TOOL_IDS = [
31
36
  "ctx_search",
32
37
  ] as const;
33
38
 
34
- export type PromptSurfaceToolId = (typeof PROMPT_SURFACE_TOOL_IDS)[number];
39
+ /** @deprecated Use ACTIVE_TOOL_IDS. Kept as an alias for existing consumers. */
40
+ export const PROMPT_SURFACE_TOOL_IDS = ACTIVE_TOOL_IDS;
41
+
42
+ export type PromptSurfaceToolId = (typeof ACTIVE_TOOL_IDS)[number];
35
43
 
36
- export const LIGHT_TOOL_DESCRIPTIONS: Readonly<Record<PromptSurfaceToolId, string>> = {
44
+ export const LIGHT_TOOL_DESCRIPTIONS = {
37
45
  ctx_reduce: CTX_REDUCE_LIGHT_DESCRIPTION,
38
46
  ctx_expand: CTX_EXPAND_LIGHT_DESCRIPTION,
39
47
  ctx_note: CTX_NOTE_LIGHT_DESCRIPTION,
40
48
  ctx_memory: CTX_MEMORY_LIGHT_DESCRIPTION,
41
49
  ctx_search: CTX_SEARCH_LIGHT_DESCRIPTION,
42
- };
50
+ } as const satisfies Readonly<Record<PromptSurfaceToolId, string>>;
43
51
 
44
52
  /**
45
53
  * Preserve the existing full-preset hash exactly while giving other presets a
@@ -54,7 +62,7 @@ export function promptSurfaceHashMaterial(
54
62
  : `${systemContent}\n\0magic-context-prompt-surface:${preset}`;
55
63
  }
56
64
 
57
- const PROMPT_SURFACE_TOOL_ID_SET = new Set<string>(PROMPT_SURFACE_TOOL_IDS);
65
+ const PROMPT_SURFACE_TOOL_ID_SET = new Set<string>(ACTIVE_TOOL_IDS);
58
66
 
59
67
  export interface PromptSurfaceGuidanceSelection {
60
68
  /** The configured built-in preset. */