@cortexkit/magic-context 0.49.24 → 0.49.25

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 (2) hide show
  1. package/dist/index.js +183 -20
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -25260,6 +25260,29 @@ function getOpenCodeV2ActiveGeneration(slot) {
25260
25260
  return;
25261
25261
  }
25262
25262
  }
25263
+ function openCodeV2SlotSpec(spec, platform = process.platform) {
25264
+ if (platform !== "win32")
25265
+ return spec;
25266
+ return Array.from(spec, (char) => '<>:"|?*'.includes(char) || char.charCodeAt(0) < 32 ? "_" : char).join("");
25267
+ }
25268
+ function getOpenCodePluginInstallRoots(hostGeneration, config, npmCacheDir = getOpenCodeV2NpmCacheDir(), platform = process.platform) {
25269
+ const configured = openCodeV2SlotSpec(readConfiguredOpenCodePluginSpec(config), platform);
25270
+ const v2 = listOpenCodeV2PluginCacheSlots(npmCacheDir).sort((a, b) => Number(b.spec === configured) - Number(a.spec === configured)).map((slot) => getOpenCodeV2ActiveGeneration(slot.slot)).filter((generation) => generation !== undefined);
25271
+ const v1 = getOpenCodePluginCacheRoots();
25272
+ return hostGeneration === "v2" ? [...v2, ...v1] : [...v1, ...v2];
25273
+ }
25274
+ function getOpenCodeLoadedPluginInstallRoots(hostGeneration, config, npmCacheDir = getOpenCodeV2NpmCacheDir(), platform = process.platform) {
25275
+ let loaded;
25276
+ if (hostGeneration === "v2") {
25277
+ const configured = openCodeV2SlotSpec(readConfiguredOpenCodePluginSpec(config), platform);
25278
+ const generation = getOpenCodeV2ActiveGeneration(getOpenCodeV2PluginCacheSlot(npmCacheDir, configured));
25279
+ loaded = generation === undefined ? [] : [generation];
25280
+ } else {
25281
+ loaded = getOpenCodePluginCacheRoots();
25282
+ }
25283
+ const others = getOpenCodePluginInstallRoots(hostGeneration, config, npmCacheDir, platform).filter((root) => !loaded.includes(root));
25284
+ return { loaded, others };
25285
+ }
25263
25286
  function readOpenCodeV2CachedPluginVersion(slot) {
25264
25287
  const generation = getOpenCodeV2ActiveGeneration(slot);
25265
25288
  if (!generation)
@@ -33114,6 +33137,51 @@ var init_read_session_formatting = __esm(() => {
33114
33137
  PROMPT_TOKEN_COUNT_BYTES = 8 * 1024 * 1024;
33115
33138
  });
33116
33139
 
33140
+ // ../plugin/src/shared/bounded-session-map.ts
33141
+ class BoundedSessionMap {
33142
+ maxEntries;
33143
+ store = new Map;
33144
+ constructor(maxEntries) {
33145
+ if (!Number.isFinite(maxEntries) || maxEntries < 1) {
33146
+ throw new Error(`BoundedSessionMap: maxEntries must be >= 1, got ${maxEntries}`);
33147
+ }
33148
+ this.maxEntries = maxEntries;
33149
+ }
33150
+ get(sessionId) {
33151
+ const value = this.store.get(sessionId);
33152
+ if (value === undefined)
33153
+ return;
33154
+ this.store.delete(sessionId);
33155
+ this.store.set(sessionId, value);
33156
+ return value;
33157
+ }
33158
+ peek(sessionId) {
33159
+ return this.store.get(sessionId);
33160
+ }
33161
+ has(sessionId) {
33162
+ return this.store.has(sessionId);
33163
+ }
33164
+ set(sessionId, value) {
33165
+ if (this.store.has(sessionId)) {
33166
+ this.store.delete(sessionId);
33167
+ } else if (this.store.size >= this.maxEntries) {
33168
+ const oldest = this.store.keys().next().value;
33169
+ if (oldest !== undefined)
33170
+ this.store.delete(oldest);
33171
+ }
33172
+ this.store.set(sessionId, value);
33173
+ }
33174
+ delete(sessionId) {
33175
+ return this.store.delete(sessionId);
33176
+ }
33177
+ clear() {
33178
+ this.store.clear();
33179
+ }
33180
+ get size() {
33181
+ return this.store.size;
33182
+ }
33183
+ }
33184
+
33117
33185
  // ../plugin/src/shared/stable-json.ts
33118
33186
  function stableStringify(value, seen = new WeakSet) {
33119
33187
  const pieces = [];
@@ -33168,36 +33236,122 @@ function writeStable(value, seen, write) {
33168
33236
  }
33169
33237
 
33170
33238
  // ../plugin/src/features/magic-context/tool-definition-tokens.ts
33239
+ function agentKey(agentName) {
33240
+ return agentName && agentName.length > 0 ? agentName : "default";
33241
+ }
33171
33242
  function keyFor(providerID, modelID, agentName) {
33172
- const agent = agentName && agentName.length > 0 ? agentName : "default";
33173
- return `${providerID}/${modelID}/${agent}`;
33243
+ return `${providerID}/${modelID}/${agentKey(agentName)}`;
33244
+ }
33245
+ function routeFor(providerID, modelID, agentName) {
33246
+ const key = keyFor(providerID, modelID, agentName);
33247
+ let route = routes.get(key);
33248
+ if (!route) {
33249
+ route = {
33250
+ providerID,
33251
+ modelID,
33252
+ agentName: agentKey(agentName),
33253
+ tools: new Map,
33254
+ live: false,
33255
+ clock: 0,
33256
+ lastWallAt: Date.now(),
33257
+ oldestFireAt: Number.POSITIVE_INFINITY,
33258
+ retired: new Map,
33259
+ legacyRows: new Set
33260
+ };
33261
+ routes.set(key, route);
33262
+ }
33263
+ return { key, route };
33264
+ }
33265
+ function advanceClock(route) {
33266
+ const now = Date.now();
33267
+ const gap = Math.min(Math.max(now - route.lastWallAt, 0), MAX_ACTIVITY_GAP_MS);
33268
+ route.clock += gap;
33269
+ route.lastWallAt = Math.max(route.lastWallAt, now);
33270
+ return route.clock;
33271
+ }
33272
+ function readWindowedSince(db) {
33273
+ try {
33274
+ const row = db.prepare("SELECT value FROM schema_migrations_meta WHERE key = ?").get(WINDOWED_SINCE_KEY);
33275
+ if (!row)
33276
+ return;
33277
+ const value = Number(row.value);
33278
+ return Number.isFinite(value) ? value : undefined;
33279
+ } catch {
33280
+ return;
33281
+ }
33282
+ }
33283
+ function markWindowed() {
33284
+ if (windowedSince !== undefined || !persistenceDb)
33285
+ return;
33286
+ try {
33287
+ persistenceDb.prepare("INSERT OR IGNORE INTO schema_migrations_meta (key, value) VALUES (?, ?)").run(WINDOWED_SINCE_KEY, String(boundAt));
33288
+ windowedSince = readWindowedSince(persistenceDb);
33289
+ } catch {}
33174
33290
  }
33175
33291
  function setDatabase(db) {
33292
+ const rebound = persistenceDb !== null && persistenceDb !== db;
33176
33293
  persistenceDb = db;
33294
+ boundAt = Date.now();
33177
33295
  cachedInsertStmt = null;
33296
+ cachedDeleteStmt = null;
33297
+ if (rebound) {
33298
+ for (const route of routes.values()) {
33299
+ for (const slot of route.tools.values()) {
33300
+ slot.persisted = false;
33301
+ slot.writeFailedAt = undefined;
33302
+ }
33303
+ for (const retired of route.retired.values())
33304
+ retired.rowKept = false;
33305
+ route.legacyRows.clear();
33306
+ }
33307
+ }
33308
+ windowedSince = readWindowedSince(db);
33309
+ if (windowedSince !== undefined)
33310
+ return;
33311
+ try {
33312
+ if (!db.prepare("SELECT 1 FROM tool_definition_measurements LIMIT 1").get())
33313
+ markWindowed();
33314
+ } catch {}
33178
33315
  }
33179
33316
  function loadToolDefinitionMeasurements(db) {
33180
33317
  let rows = [];
33181
33318
  try {
33182
- rows = db.prepare("SELECT provider_id, model_id, agent_name, tool_id, token_count FROM tool_definition_measurements").all();
33319
+ rows = db.prepare("SELECT provider_id, model_id, agent_name, tool_id, token_count, recorded_at FROM tool_definition_measurements").all();
33183
33320
  } catch {
33184
33321
  return;
33185
33322
  }
33323
+ const since = readWindowedSince(db);
33324
+ const loadedAt = new Map;
33186
33325
  for (const row of rows) {
33187
- const key = keyFor(row.provider_id, row.model_id, row.agent_name);
33188
- let inner = measurements.get(key);
33189
- if (!inner) {
33190
- inner = new Map;
33191
- measurements.set(key, inner);
33192
- }
33193
- inner.set(row.tool_id, row.token_count);
33326
+ const { route } = routeFor(row.provider_id, row.model_id, row.agent_name);
33327
+ if (route.live)
33328
+ continue;
33329
+ let now = loadedAt.get(route);
33330
+ if (now === undefined) {
33331
+ now = advanceClock(route);
33332
+ loadedAt.set(route, now);
33333
+ }
33334
+ const legacy = since === undefined || !(Number(row.recorded_at) >= since);
33335
+ route.tools.set(row.tool_id, {
33336
+ tokens: row.token_count,
33337
+ fingerprint: undefined,
33338
+ lastFireAt: now,
33339
+ legacy,
33340
+ persisted: !legacy
33341
+ });
33342
+ route.oldestFireAt = Math.min(route.oldestFireAt, now);
33343
+ if (legacy)
33344
+ route.legacyRows.add(row.tool_id);
33345
+ else
33346
+ route.legacyRows.delete(row.tool_id);
33194
33347
  }
33195
33348
  }
33196
- var measurements, fingerprints, persistenceDb = null, cachedInsertStmt = null;
33349
+ var ACTIVITY_WINDOW_MS, MAX_ACTIVITY_GAP_MS = 60000, WINDOWED_SINCE_KEY = "tool_definition_measurements:windowed_since", routes, sessionFlights, persistenceDb = null, windowedSince, boundAt = 0, cachedInsertStmt = null, cachedDeleteStmt = null;
33197
33350
  var init_tool_definition_tokens = __esm(() => {
33198
33351
  init_read_session_formatting();
33199
- measurements = new Map;
33200
- fingerprints = new Map;
33352
+ ACTIVITY_WINDOW_MS = 10 * 60000;
33353
+ routes = new Map;
33354
+ sessionFlights = new BoundedSessionMap(1000);
33201
33355
  });
33202
33356
 
33203
33357
  // ../plugin/src/features/magic-context/tool-owner-backfill.ts
@@ -52538,8 +52692,8 @@ async function runIssueFlow2(options = {}, deps = { collectDiagnostics }) {
52538
52692
  return 1;
52539
52693
  }
52540
52694
  }
52541
- function checkLocalEmbeddingRuntimeForDoctor(runtimePreference = "auto") {
52542
- const runtime = checkLocalEmbeddingRuntime(getOpenCodePluginCacheRoots(), process.platform, process.arch, runtimePreference);
52695
+ function checkLocalEmbeddingRuntimeForDoctor(installRoots, runtimePreference = "auto") {
52696
+ const runtime = checkLocalEmbeddingRuntime(installRoots, process.platform, process.arch, runtimePreference);
52543
52697
  if (runtime.state === "wasm-selected") {
52544
52698
  log3.warn(formatLocalEmbeddingRuntimeWasmSelected(runtime));
52545
52699
  return { issues: 0 };
@@ -52559,15 +52713,15 @@ function checkLocalEmbeddingRuntimeForDoctor(runtimePreference = "auto") {
52559
52713
  log3.success("Embedding provider: local (native runtime selected and OK; Xenova/all-MiniLM-L6-v2 bundled)");
52560
52714
  return { issues: 0 };
52561
52715
  }
52562
- async function checkEmbeddingConfig(magicContextConfigPath) {
52716
+ async function checkEmbeddingConfig(magicContextConfigPath, installRoots) {
52563
52717
  if (!existsSync49(magicContextConfigPath)) {
52564
- return checkLocalEmbeddingRuntimeForDoctor();
52718
+ return checkLocalEmbeddingRuntimeForDoctor(installRoots);
52565
52719
  }
52566
52720
  let rawText;
52567
52721
  try {
52568
52722
  const raw = loadRawConfigFile({ configPath: magicContextConfigPath, tier: "user" });
52569
52723
  if (!raw)
52570
- return checkLocalEmbeddingRuntimeForDoctor();
52724
+ return checkLocalEmbeddingRuntimeForDoctor(installRoots);
52571
52725
  rawText = raw.text;
52572
52726
  } catch {
52573
52727
  log3.warn("Could not read magic-context.jsonc for embedding check");
@@ -52592,7 +52746,7 @@ async function checkEmbeddingConfig(magicContextConfigPath) {
52592
52746
  }
52593
52747
  if (provider === undefined || provider === "local") {
52594
52748
  const runtimePreference = embedding?.local_runtime === "native" || embedding?.local_runtime === "wasm" ? embedding.local_runtime : "auto";
52595
- return checkLocalEmbeddingRuntimeForDoctor(runtimePreference);
52749
+ return checkLocalEmbeddingRuntimeForDoctor(installRoots, runtimePreference);
52596
52750
  }
52597
52751
  if (provider === "synapse") {
52598
52752
  const loaded = loadPluginConfig(process.cwd());
@@ -53271,7 +53425,16 @@ async function runDoctor2(options = {}) {
53271
53425
  }
53272
53426
  } catch {}
53273
53427
  }
53274
- const embeddingCheck = await checkEmbeddingConfig(paths.magicContextConfig);
53428
+ let embeddingHostConfig = null;
53429
+ try {
53430
+ if (paths.opencodeConfigFormat !== "none")
53431
+ embeddingHostConfig = import_comment_json11.parse(readFileSync32(paths.opencodeConfig, "utf-8"));
53432
+ } catch {}
53433
+ const embeddingInstalls = getOpenCodeLoadedPluginInstallRoots(hostGeneration, embeddingHostConfig);
53434
+ const unusedInstalls = embeddingInstalls.others.filter((root) => existsSync49(root));
53435
+ if (unusedInstalls.length > 0)
53436
+ log3.info(`Local embedding runtime: checked only the copy ${hostGeneration === "v2" ? "OpenCode 2" : "OpenCode 1"} loads; other installed copies (not loaded, not checked): ${unusedInstalls.join(", ")}`);
53437
+ const embeddingCheck = await checkEmbeddingConfig(paths.magicContextConfig, embeddingInstalls.loaded);
53275
53438
  issues += embeddingCheck.issues;
53276
53439
  if (embeddingCheck.issues > 0)
53277
53440
  failCount += embeddingCheck.issues;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/magic-context",
3
- "version": "0.49.24",
3
+ "version": "0.49.25",
4
4
  "description": "Unified CLI for Magic Context — setup, doctor, and migration across OpenCode, Pi, and OMP",
5
5
  "keywords": [
6
6
  "opencode",