@cortexkit/magic-context 0.49.19 → 0.49.20

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 +120 -25
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2285,6 +2285,15 @@ function scheduleFlush() {
2285
2285
  flush();
2286
2286
  }, FLUSH_INTERVAL_MS);
2287
2287
  }
2288
+ function appendLine(line) {
2289
+ buffer.push(line);
2290
+ bufferedBytes += Buffer.byteLength(line);
2291
+ boundBuffer();
2292
+ if (syncLogWrites || buffer.length >= BUFFER_SIZE_LIMIT)
2293
+ flush();
2294
+ else
2295
+ scheduleFlush();
2296
+ }
2288
2297
  function log(message, data) {
2289
2298
  try {
2290
2299
  let diagnostic = stalePluginBuildDiagnostic(data);
@@ -2307,24 +2316,18 @@ ${data.stack}` : ""}`)}` : ` ${JSON.stringify(sanitizeConfigValue(data))}`;
2307
2316
  lineForwarder(line);
2308
2317
  return;
2309
2318
  }
2310
- buffer.push(line);
2311
- bufferedBytes += Buffer.byteLength(line);
2312
- boundBuffer();
2313
- if (buffer.length >= BUFFER_SIZE_LIMIT) {
2314
- flush();
2315
- } else {
2316
- scheduleFlush();
2317
- }
2319
+ appendLine(line);
2318
2320
  } catch {}
2319
2321
  }
2320
2322
  function sessionLog(sessionId, message, data) {
2321
2323
  log(`[magic-context][${sessionId}] ${message}`, data);
2322
2324
  }
2323
- var isTestEnv = false, buffer, flushTimer = null, FLUSH_INTERVAL_MS = 500, BUFFER_SIZE_LIMIT = 50, MAX_BUFFERED_BYTES, bufferedBytes = 0, droppedLines = 0, MAX_LOG_FILE_BYTES, SIZE_CHECK_INTERVAL_FLUSHES = 64, activeLogFile = null, activeLogSize = null, flushesSinceSizeCheck = 0, swallowedWriteCount = 0, lastErrorMessage = null, lastErrorTime = null, lineForwarder = null;
2325
+ var isTestEnv = false, syncLogWrites, buffer, flushTimer = null, FLUSH_INTERVAL_MS = 500, BUFFER_SIZE_LIMIT = 50, MAX_BUFFERED_BYTES, bufferedBytes = 0, droppedLines = 0, MAX_LOG_FILE_BYTES, SIZE_CHECK_INTERVAL_FLUSHES = 64, activeLogFile = null, activeLogSize = null, flushesSinceSizeCheck = 0, swallowedWriteCount = 0, lastErrorMessage = null, lastErrorTime = null, lineForwarder = null;
2324
2326
  var init_logger = __esm(() => {
2325
2327
  init_data_path();
2326
2328
  init_redaction();
2327
2329
  init_stale_plugin_build();
2330
+ syncLogWrites = process.env.MAGIC_CONTEXT_LOG_SYNC === "1";
2328
2331
  buffer = [];
2329
2332
  MAX_BUFFERED_BYTES = 1024 * 1024;
2330
2333
  MAX_LOG_FILE_BYTES = 32 * 1024 * 1024;
@@ -13591,7 +13594,7 @@ async function withAsyncPrivilegedWriter(db, operation, options = {}) {
13591
13594
  } catch (error) {
13592
13595
  if (entered || !(error instanceof SqliteAcquisitionBusyError))
13593
13596
  throw error;
13594
- options.beforeRetry?.(error);
13597
+ await options.beforeRetry?.(error);
13595
13598
  const elapsed = performance.now() - started;
13596
13599
  if (elapsed >= budgetMs) {
13597
13600
  reportSqliteDiagnostic(`sqlite writer site=privileged_writer lane=foreground acquire_ms=${Math.round(elapsed)} attempts=${attempts} outcome=busy`);
@@ -25440,6 +25443,61 @@ var init_exit_abort_registry = __esm(() => {
25440
25443
  controllers = new Set;
25441
25444
  });
25442
25445
 
25446
+ // ../plugin/src/shared/worker-shutdown.ts
25447
+ function createWorkerShutdown(worker, site, deadlineMs) {
25448
+ let exited = false;
25449
+ let forced = false;
25450
+ let closing;
25451
+ const exit = new Promise((resolve) => {
25452
+ worker.once("exit", () => {
25453
+ exited = true;
25454
+ resolve();
25455
+ });
25456
+ });
25457
+ const record = () => {
25458
+ const counts = outcomes.get(site) ?? { graceful: 0, forced: 0 };
25459
+ if (forced)
25460
+ counts.forced += 1;
25461
+ else
25462
+ counts.graceful += 1;
25463
+ outcomes.set(site, counts);
25464
+ };
25465
+ const terminate = () => {
25466
+ forced = true;
25467
+ worker.terminate().catch(() => {});
25468
+ };
25469
+ return {
25470
+ close() {
25471
+ if (closing)
25472
+ return closing;
25473
+ if (exited) {
25474
+ closing = Promise.resolve();
25475
+ return closing;
25476
+ }
25477
+ try {
25478
+ worker.postMessage(WORKER_CLOSE_MESSAGE);
25479
+ } catch {
25480
+ terminate();
25481
+ }
25482
+ const fallback = setTimeout(() => {
25483
+ if (!exited)
25484
+ terminate();
25485
+ }, deadlineMs);
25486
+ fallback.unref?.();
25487
+ closing = exit.then(() => {
25488
+ clearTimeout(fallback);
25489
+ record();
25490
+ });
25491
+ return closing;
25492
+ }
25493
+ };
25494
+ }
25495
+ var WORKER_CLOSE_MESSAGE, outcomes;
25496
+ var init_worker_shutdown = __esm(() => {
25497
+ WORKER_CLOSE_MESSAGE = { kind: "close" };
25498
+ outcomes = new Map;
25499
+ });
25500
+
25443
25501
  // ../plugin/src/hooks/magic-context/read-session-raw.ts
25444
25502
  import { isMainThread } from "node:worker_threads";
25445
25503
  function isRawMessageRow(row) {
@@ -26463,6 +26521,7 @@ function runSessionWarmupWorker(input, options) {
26463
26521
  return new Promise((resolve) => {
26464
26522
  const openCancellation = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));
26465
26523
  let worker;
26524
+ let shutdown;
26466
26525
  let deadline;
26467
26526
  let settled = false;
26468
26527
  const finish = (result) => {
@@ -26474,7 +26533,7 @@ function runSessionWarmupWorker(input, options) {
26474
26533
  options.signal?.removeEventListener("abort", abort);
26475
26534
  if (worker) {
26476
26535
  activeWorkers.delete(worker);
26477
- const stopped = worker.terminate();
26536
+ const stopped = shutdown?.close() ?? worker.terminate();
26478
26537
  worker.unref();
26479
26538
  stopped.finally(() => {
26480
26539
  releaseIndexOwner(input);
@@ -26500,6 +26559,7 @@ function runSessionWarmupWorker(input, options) {
26500
26559
  worker = inRootAsyncContext(() => new Worker(options.entry ?? defaultEntry(), {
26501
26560
  workerData: { ...input, openCancellation: openCancellation.buffer }
26502
26561
  }));
26562
+ shutdown = createWorkerShutdown(worker, "raw-ordinal-warmup", WARMUP_CLOSE_DEADLINE_MS);
26503
26563
  } catch (error) {
26504
26564
  finish({ failure: "start-failed", detail: String(error) });
26505
26565
  return;
@@ -26594,10 +26654,11 @@ function recordFailure(key, sessionId, failure, detail) {
26594
26654
  failedAt.set(key, Date.now());
26595
26655
  log(`[magic-context] WARN canonical ordinal warm-up failed for ${sessionId} (${failure}: ${detail}); counts remain bounded and callers must defer or retry`);
26596
26656
  }
26597
- var ACCEPT_TIMEOUT_MS = 2000, RESULT_TIMEOUT_MS, FAILURE_BACKOFF_MS, inFlight, connectionIds, nextConnectionId = 0, failedAt, activeWorkers, failures, exitHookRegistered = false;
26657
+ var ACCEPT_TIMEOUT_MS = 2000, RESULT_TIMEOUT_MS, WARMUP_CLOSE_DEADLINE_MS = 2000, FAILURE_BACKOFF_MS, inFlight, connectionIds, nextConnectionId = 0, failedAt, activeWorkers, failures, exitHookRegistered = false;
26598
26658
  var init_raw_ordinal_warmup = __esm(async () => {
26599
26659
  init_exit_abort_registry();
26600
26660
  init_logger();
26661
+ init_worker_shutdown();
26601
26662
  await __promiseAll([
26602
26663
  init_sqlite(),
26603
26664
  init_read_session_raw(),
@@ -49217,6 +49278,18 @@ var init_diagnostics_opencode = __esm(async () => {
49217
49278
  // ../plugin/src/features/magic-context/memory/embedding-model-match.ts
49218
49279
  var init_embedding_model_match = () => {};
49219
49280
 
49281
+ // ../plugin/src/features/magic-context/memory/local-embedding-pooling.ts
49282
+ var decisionLogged, unavailableLogged, metadataByCache, testMetadataByModel;
49283
+ var init_local_embedding_pooling = __esm(() => {
49284
+ init_data_path();
49285
+ init_logger();
49286
+ init_storage_permissions();
49287
+ decisionLogged = new Set;
49288
+ unavailableLogged = new Set;
49289
+ metadataByCache = new Map;
49290
+ testMetadataByModel = new Map;
49291
+ });
49292
+
49220
49293
  // ../plugin/src/features/magic-context/memory/normalize-hash.ts
49221
49294
  var init_normalize_hash = () => {};
49222
49295
 
@@ -49224,6 +49297,7 @@ var init_normalize_hash = () => {};
49224
49297
  var init_embedding_identity = __esm(async () => {
49225
49298
  init_magic_context();
49226
49299
  init_embedding_model_match();
49300
+ init_local_embedding_pooling();
49227
49301
  init_normalize_hash();
49228
49302
  await init_embedding_synapse();
49229
49303
  });
@@ -49277,6 +49351,7 @@ var init_embedding_local = __esm(async () => {
49277
49351
  init_logger();
49278
49352
  init_stale_plugin_build();
49279
49353
  init_storage_permissions();
49354
+ init_local_embedding_pooling();
49280
49355
  await __promiseAll([
49281
49356
  init_embedding_identity(),
49282
49357
  init_embedding_worker_client(),
@@ -51981,6 +52056,9 @@ function resolveHarnessBlock(container, harness) {
51981
52056
  const selected = harness === "omp" ? container?.omp ?? container?.pi : container?.[harness];
51982
52057
  return asRecord2(selected);
51983
52058
  }
52059
+ function sourceHarness(container, harness) {
52060
+ return harness === "omp" && container?.omp == null ? "pi" : harness;
52061
+ }
51984
52062
  function normalizeModelEntry(entry, harness) {
51985
52063
  if (typeof entry === "string") {
51986
52064
  const model = readString(entry);
@@ -52027,13 +52105,19 @@ function resolveHistorianModel(config, harness) {
52027
52105
  const block = resolveHarnessBlock(historian, harness);
52028
52106
  if (!block)
52029
52107
  return { fallbacks: [] };
52108
+ const source = `historian.${sourceHarness(historian, harness)}`;
52109
+ const primary = resolvePrimaryEntry({
52110
+ entry: block.model,
52111
+ defaultQualifier: block[harness === "opencode" ? "variant" : "thinking_level"],
52112
+ harness
52113
+ });
52114
+ const fallbacks = resolveFallbackEntries(block.fallback_models, harness);
52030
52115
  return {
52031
- primary: resolvePrimaryEntry({
52032
- entry: block.model,
52033
- defaultQualifier: block[harness === "opencode" ? "variant" : "thinking_level"],
52034
- harness
52035
- }),
52036
- fallbacks: resolveFallbackEntries(block.fallback_models, harness)
52116
+ primary: primary ? { ...primary, sourceConfigKey: `${source}.model` } : undefined,
52117
+ fallbacks: fallbacks.map((entry) => ({
52118
+ ...entry,
52119
+ sourceConfigKey: `${source}.fallback_models`
52120
+ }))
52037
52121
  };
52038
52122
  }
52039
52123
  function resolveDreamerTaskModel(args) {
@@ -52052,15 +52136,25 @@ function resolveDreamerTaskModel(args) {
52052
52136
  const taskQualifier = taskBlock?.[args.harness === "opencode" ? "variant" : "thinking_level"];
52053
52137
  const harnessQualifier = harnessBlock[args.harness === "opencode" ? "variant" : "thinking_level"] ?? normalizeModelEntry(harnessBlock.model, args.harness)?.qualifier;
52054
52138
  const taskHasModel = taskBlock !== undefined && hasOwn2(taskBlock, "model");
52139
+ const taskHasFallbacks = taskBlock !== undefined && hasOwn2(taskBlock, "fallback_models");
52055
52140
  const primarySource = taskHasModel ? taskBlock?.model : harnessBlock.model;
52056
- const fallbackSource = taskBlock !== undefined && hasOwn2(taskBlock, "fallback_models") ? taskBlock.fallback_models : harnessBlock.fallback_models;
52141
+ const fallbackSource = taskHasFallbacks ? taskBlock?.fallback_models : harnessBlock.fallback_models;
52142
+ const source = `dreamer.${sourceHarness(dreamer, args.harness)}`;
52143
+ const primary = resolvePrimaryEntry({
52144
+ entry: primarySource,
52145
+ defaultQualifier: taskQualifier ?? harnessQualifier,
52146
+ harness: args.harness
52147
+ });
52148
+ const fallbacks = resolveFallbackEntries(fallbackSource, args.harness);
52057
52149
  return {
52058
- primary: resolvePrimaryEntry({
52059
- entry: primarySource,
52060
- defaultQualifier: taskQualifier ?? harnessQualifier,
52061
- harness: args.harness
52062
- }),
52063
- fallbacks: resolveFallbackEntries(fallbackSource, args.harness),
52150
+ primary: primary ? {
52151
+ ...primary,
52152
+ sourceConfigKey: taskHasModel ? `${source}.tasks.${args.task}.model` : `${source}.model`
52153
+ } : undefined,
52154
+ fallbacks: fallbacks.map((entry) => ({
52155
+ ...entry,
52156
+ sourceConfigKey: taskHasFallbacks ? `${source}.tasks.${args.task}.fallback_models` : `${source}.fallback_models`
52157
+ })),
52064
52158
  schedule: readString(schedulingTask?.schedule),
52065
52159
  timeoutMinutes: typeof taskBlock?.timeout_minutes === "number" ? taskBlock.timeout_minutes : undefined,
52066
52160
  promotionThreshold: typeof schedulingTask?.promotion_threshold === "number" ? schedulingTask.promotion_threshold : undefined
@@ -52080,6 +52174,7 @@ function buildDreamTaskRuntimeConfigs(dreamer, harness, language, smartNotesEnab
52080
52174
  schedule: resolved.schedule ?? "",
52081
52175
  model: resolved.primary,
52082
52176
  fallbackModels: resolved.fallbacks,
52177
+ modelConfigKey: resolved.primary?.sourceConfigKey ?? resolved.fallbacks[0]?.sourceConfigKey,
52083
52178
  thinkingLevel: harness === "pi" ? resolved.primary?.qualifier : undefined,
52084
52179
  language,
52085
52180
  timeoutMinutes: resolved.timeoutMinutes ?? 20,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/magic-context",
3
- "version": "0.49.19",
3
+ "version": "0.49.20",
4
4
  "description": "Unified CLI for Magic Context — setup, doctor, and migration across OpenCode, Pi, and OMP",
5
5
  "keywords": [
6
6
  "opencode",