@cortexkit/pi-magic-context 0.40.1 → 0.41.1

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.
@@ -4,8 +4,9 @@ import {
4
4
  getLegacyOpenCodeMagicContextStorageDir,
5
5
  getMagicContextStorageDir,
6
6
  log,
7
+ sanitizeDiagnosticText,
7
8
  sessionLog
8
- } from "./index-hk6sj7dn.js";
9
+ } from "./index-q2vgz0j1.js";
9
10
  import {
10
11
  __commonJS,
11
12
  __export,
@@ -8283,153 +8284,6 @@ function migrateMagicContextConfigLocations(directory, logger) {
8283
8284
  return warnings;
8284
8285
  }
8285
8286
 
8286
- // ../plugin/src/shared/redaction.ts
8287
- import { homedir as homedir2, userInfo } from "node:os";
8288
- function escapeRegex(value) {
8289
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
8290
- }
8291
- var SECRET_WORDS = [
8292
- "key",
8293
- "token",
8294
- "secret",
8295
- "password",
8296
- "auth",
8297
- "authorization",
8298
- "bearer",
8299
- "credential"
8300
- ];
8301
- var SECRET_SEGMENT_PATTERN = new RegExp(`^(?:${SECRET_WORDS.map((w) => `${w}s?`).join("|")})$`, "i");
8302
- var TRAILING_DESCRIPTORS = new Set(["id", "ids", "value", "values", "header", "headers"]);
8303
- function redactionTypeForKey(key) {
8304
- const normalized = key.trim().toLowerCase().replace(/[^a-z0-9_.-]+/g, "_");
8305
- const suffix = normalized.split(".").filter(Boolean).at(-1) ?? normalized;
8306
- return suffix || "secret";
8307
- }
8308
- function isNonSecretScalarValue(value) {
8309
- const v = value.trim();
8310
- if (v === "true" || v === "false" || v === "null" || v === "undefined")
8311
- return true;
8312
- return /^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(v);
8313
- }
8314
- var SECRET_QUALIFIERS = new Set([
8315
- "api",
8316
- "access",
8317
- "private",
8318
- "client",
8319
- "auth",
8320
- "authorization",
8321
- "secret",
8322
- "bearer",
8323
- "session",
8324
- "refresh",
8325
- "service",
8326
- "x",
8327
- "openai",
8328
- "anthropic",
8329
- "google",
8330
- "github",
8331
- "huggingface",
8332
- "aws",
8333
- "azure"
8334
- ]);
8335
- function sanitizePathString(value) {
8336
- const home = homedir2();
8337
- const username = userInfo().username;
8338
- let sanitized = value;
8339
- if (home) {
8340
- sanitized = sanitized.replace(new RegExp(escapeRegex(home), "g"), "~");
8341
- }
8342
- sanitized = sanitized.replace(/\/Users\/[^/]+\//g, "/Users/<USER>/");
8343
- sanitized = sanitized.replace(/\/home\/[^/]+\//g, "/home/<USER>/");
8344
- sanitized = sanitized.replace(/C:\\Users\\[^\\]+\\/g, "C:\\Users\\<USER>\\");
8345
- if (username) {
8346
- sanitized = sanitized.replace(new RegExp(escapeRegex(username), "g"), "<USER>");
8347
- }
8348
- return sanitized;
8349
- }
8350
- var SECRET_TEXT_PATTERNS = [
8351
- {
8352
- pattern: /\bsk-ant-(?:api03-)?[A-Za-z0-9_-]{32,}/g,
8353
- replacement: "<ANTHROPIC_API_KEY_REDACTED>"
8354
- },
8355
- {
8356
- pattern: /\bsk-(?:proj-)?[A-Za-z0-9_-]{32,}/g,
8357
- replacement: "<OPENAI_API_KEY_REDACTED>"
8358
- },
8359
- {
8360
- pattern: /\bgithub_pat_[A-Za-z0-9_]{20,}/g,
8361
- replacement: "<GITHUB_PAT_REDACTED>"
8362
- },
8363
- {
8364
- pattern: /\b(?:gh[opsu]|ghr)_[A-Za-z0-9]{30,}/g,
8365
- replacement: "<GITHUB_TOKEN_REDACTED>"
8366
- },
8367
- {
8368
- pattern: /\bhf_[A-Za-z0-9]{30,}/g,
8369
- replacement: "<HUGGINGFACE_TOKEN_REDACTED>"
8370
- },
8371
- {
8372
- pattern: /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g,
8373
- replacement: "<AWS_ACCESS_KEY_ID_REDACTED>"
8374
- },
8375
- {
8376
- pattern: /\bxox[abprsuvc]-[A-Za-z0-9-]{10,}/g,
8377
- replacement: "<SLACK_TOKEN_REDACTED>"
8378
- },
8379
- {
8380
- pattern: /\bAIza[A-Za-z0-9_-]{35}\b/g,
8381
- replacement: "<GOOGLE_API_KEY_REDACTED>"
8382
- },
8383
- {
8384
- pattern: /\b(Authorization\s*:\s*Bearer\s+)([A-Za-z0-9._~+/=-]{8,})/gi,
8385
- replacement: (_full, prefix) => `${prefix}<REDACTED:bearer>`
8386
- },
8387
- {
8388
- pattern: /\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g,
8389
- replacement: "<JWT_REDACTED>"
8390
- },
8391
- {
8392
- pattern: /(["'])([^"']*(?:key|token|secret|password|auth|bearer|credential)[^"']*)\1(\s*:\s*)(["'])([^"']*)\4/gi,
8393
- replacement: (full, quote, key, separator, valueQuote, value) => isNonSecretScalarValue(value) ? full : `${quote}${key}${quote}${separator}${valueQuote}<REDACTED:${redactionTypeForKey(key)}>${valueQuote}`
8394
- },
8395
- {
8396
- pattern: /\b([A-Za-z0-9_.-]*(?:key|token|secret|password|auth|bearer|credential)[A-Za-z0-9_.-]*)\s*=\s*([^\s'"`]+)/gi,
8397
- replacement: (full, key, value) => isNonSecretScalarValue(value) ? full : `${key}=<REDACTED:${redactionTypeForKey(key)}>`
8398
- }
8399
- ];
8400
- function redactSecretText(value) {
8401
- let redacted = value;
8402
- for (const { pattern, replacement } of SECRET_TEXT_PATTERNS) {
8403
- if (typeof replacement === "string") {
8404
- redacted = redacted.replace(pattern, replacement);
8405
- } else {
8406
- redacted = redacted.replace(pattern, replacement);
8407
- }
8408
- }
8409
- return redacted;
8410
- }
8411
- function sanitizeDiagnosticText(value) {
8412
- return redactSecretText(sanitizePathString(value));
8413
- }
8414
- var SHAREABILITY_SENSITIVE_PATTERNS = [
8415
- /\bC:\/Users\/[^/\s]+/i,
8416
- /(?:^|\s)~\/[^\s]+/,
8417
- /\b(?:api[_-]?key|secret|token|password|passwd|pwd|client[_-]?secret|access[_-]?key)\b\s*[:=]\s*\S+/i,
8418
- /\b(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])(?::\d+)?\b/i,
8419
- /\b(?:10|127)\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/,
8420
- /\b192\.168\.\d{1,3}\.\d{1,3}\b/,
8421
- /\b172\.(?:1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}\b/
8422
- ];
8423
- function hasShareabilitySensitiveText(text) {
8424
- try {
8425
- if (sanitizeDiagnosticText(text) !== text)
8426
- return true;
8427
- return SHAREABILITY_SENSITIVE_PATTERNS.some((pattern) => pattern.test(text));
8428
- } catch {
8429
- return true;
8430
- }
8431
- }
8432
-
8433
8287
  // ../plugin/src/features/magic-context/fail-closed-block.ts
8434
8288
  var FAIL_CLOSED_DOCTOR_COMMAND = "npx @cortexkit/magic-context@latest doctor";
8435
8289
  var FAIL_CLOSED_REPROBE_EVERY_N = 5;
@@ -8656,7 +8510,7 @@ function createFailClosedController(options) {
8656
8510
  import { execFileSync } from "node:child_process";
8657
8511
  import { createHash } from "node:crypto";
8658
8512
  import { existsSync as existsSync2, realpathSync, statSync as statSync2 } from "node:fs";
8659
- import { homedir as homedir3 } from "node:os";
8513
+ import { homedir as homedir2 } from "node:os";
8660
8514
  import path from "node:path";
8661
8515
  var GIT_TIMEOUT_MS = 5000;
8662
8516
  var TRANSIENT_FAILURE_COOLDOWN_MS = 5 * 60 * 1000;
@@ -8670,7 +8524,7 @@ var dubiousOwnershipLoggedDirectories = new Set;
8670
8524
  var dubiousOwnershipWarnedDirectories = new Set;
8671
8525
  var transientGitIdentityReuseLoggedDirectories = new Set;
8672
8526
  var execFileSyncForIdentity = execFileSync;
8673
- var userHomeDirectoryForIdentity = () => homedir3();
8527
+ var userHomeDirectoryForIdentity = () => homedir2();
8674
8528
  var nowMs = () => Date.now();
8675
8529
 
8676
8530
  class ProjectIdentityError extends Error {
@@ -8976,6 +8830,9 @@ function gitRootDirectory(canonical) {
8976
8830
  return null;
8977
8831
  }
8978
8832
  }
8833
+ function directoryHasGitMetadata(directory) {
8834
+ return gitRootDirectory(path.resolve(directory)) !== null;
8835
+ }
8979
8836
  function resolveProjectIdentityForSession(directory, allowHomeProject = false) {
8980
8837
  const canonicalHome = canonicalUserHomeDirectory();
8981
8838
  const canonicalDirectory = (() => {
@@ -9026,7 +8883,7 @@ function scheduleAfterBootQuiet(task, additionalDelayMs = 0) {
9026
8883
  // ../plugin/src/hooks/magic-context/read-session-formatting.ts
9027
8884
  import { existsSync as existsSync3, readFileSync as readFileSync2, realpathSync as realpathSync2 } from "node:fs";
9028
8885
  import { createRequire } from "node:module";
9029
- import { homedir as homedir4 } from "node:os";
8886
+ import { homedir as homedir3 } from "node:os";
9030
8887
  import { dirname as dirname2, join as join2, resolve } from "node:path";
9031
8888
  import { pathToFileURL } from "node:url";
9032
8889
 
@@ -9143,7 +9000,7 @@ var tokenizerLoadPromise;
9143
9000
  var tokenizerWarningSent = false;
9144
9001
  function tokenizerPackageRoots() {
9145
9002
  const cwd = process.cwd();
9146
- const openCodeCache = join2(process.env.XDG_CACHE_HOME ?? join2(homedir4(), ".cache"), "opencode");
9003
+ const openCodeCache = join2(process.env.XDG_CACHE_HOME ?? join2(homedir3(), ".cache"), "opencode");
9147
9004
  const roots = [cwd, openCodeCache];
9148
9005
  const candidates = [];
9149
9006
  for (const root of roots) {
@@ -9961,6 +9818,7 @@ function closeQuietly(db) {
9961
9818
 
9962
9819
  // ../plugin/src/features/magic-context/context-authority.ts
9963
9820
  import { createHash as createHash2, randomUUID } from "node:crypto";
9821
+ var observedAuthorityRoutingByProject = new Map;
9964
9822
  var moduleNoteEvaluationBridges = new Map;
9965
9823
  function getModuleNoteEvaluationBridge(projectPath) {
9966
9824
  return moduleNoteEvaluationBridges.get(projectPath);
@@ -10162,6 +10020,17 @@ function unescapeXml(s) {
10162
10020
  return s.replace(/&amp;/g, "&").replace(/&apos;/g, "'").replace(/&quot;/g, '"').replace(/&lt;/g, "<").replace(/&gt;/g, ">");
10163
10021
  }
10164
10022
 
10023
+ // ../plugin/src/shared/write-transaction-timing.ts
10024
+ var SLOW_WRITE_TRANSACTION_THRESHOLD_MS = 1000;
10025
+ function logSlowWriteTransaction(site, startedAt, thresholdMs = SLOW_WRITE_TRANSACTION_THRESHOLD_MS) {
10026
+ try {
10027
+ const durationMs = performance.now() - startedAt;
10028
+ if (durationMs < thresholdMs)
10029
+ return;
10030
+ log(`[magic-context] slow write transaction: site=${site} held=${durationMs.toFixed(1)}ms`);
10031
+ } catch {}
10032
+ }
10033
+
10165
10034
  // ../plugin/src/features/magic-context/storage-schema-helpers.ts
10166
10035
  function ensureColumn(db, table, column, definition) {
10167
10036
  if (!/^[a-z][a-z0-9_]*$/.test(table) || !/^[a-z][a-z0-9_]*$/.test(column) || !/^[A-Z0-9_"'(),[\]\s]+$/i.test(definition)) {
@@ -12709,6 +12578,15 @@ var MIGRATIONS = [
12709
12578
  );
12710
12579
  `);
12711
12580
  }
12581
+ },
12582
+ {
12583
+ version: 82,
12584
+ description: "record the origin of memory file-independent mappings",
12585
+ up(db) {
12586
+ if (!tableExists2(db, "memory_verifications"))
12587
+ return;
12588
+ ensureColumn(db, "memory_verifications", "mapping_origin", "TEXT NOT NULL DEFAULT 'mapper'");
12589
+ }
12712
12590
  }
12713
12591
  ];
12714
12592
  var LATEST_MIGRATION_VERSION = MIGRATIONS.reduce((max, m) => Math.max(max, m.version), 0);
@@ -12754,6 +12632,7 @@ function runMigrations(db) {
12754
12632
  let migration;
12755
12633
  let currentVersion = 0;
12756
12634
  try {
12635
+ const transactionStartedAt = performance.now();
12757
12636
  const applied = db.transaction(() => {
12758
12637
  currentVersion = getCurrentVersion(db);
12759
12638
  migration = MIGRATIONS.find((candidate) => candidate.version > currentVersion && !isMigrationApplied(db, candidate.version));
@@ -12768,6 +12647,7 @@ function runMigrations(db) {
12768
12647
  db.prepare("INSERT INTO schema_migrations (version, description, applied_at) VALUES (?, ?, ?)").run(migration.version, migration.description, Date.now());
12769
12648
  return true;
12770
12649
  }).immediate();
12650
+ logSlowWriteTransaction("migration-runner", transactionStartedAt);
12771
12651
  if (!applied || !migration)
12772
12652
  break;
12773
12653
  if (migration.version <= 61)
@@ -12792,7 +12672,9 @@ function runMigrations(db) {
12792
12672
  }
12793
12673
  if (touchedLegacyAuthorityBatch) {
12794
12674
  try {
12675
+ const transactionStartedAt = performance.now();
12795
12676
  db.transaction(() => installLatestAuthorityTriggers(db)).immediate();
12677
+ logSlowWriteTransaction("migration-runner", transactionStartedAt);
12796
12678
  } catch (error) {
12797
12679
  throw new Error(`Migration authority-trigger postcondition failed: ${error instanceof Error ? error.message : String(error)}. Database may need manual repair.`);
12798
12680
  }
@@ -13143,7 +13025,7 @@ function getSchemaFenceRejection() {
13143
13025
  function getMigrationOnOpenRefusal() {
13144
13026
  return lastMigrationOnOpenRefusal;
13145
13027
  }
13146
- var LATEST_SUPPORTED_VERSION = 81;
13028
+ var LATEST_SUPPORTED_VERSION = 82;
13147
13029
  var PERMISSIONS_ENFORCEABLE = process.platform !== "win32";
13148
13030
  var defaultStoragePermissionFs = { chmodSync, mkdirSync: mkdirSync2 };
13149
13031
  var storagePermissionFs = defaultStoragePermissionFs;
@@ -13892,8 +13774,10 @@ function initializeDatabase(db) {
13892
13774
  -- verified_at=0 means "mapped (files known) but not yet content-verified".
13893
13775
  -- map-memories sets mapped_at + verified_at=0; verify sets verified_at=now.
13894
13776
  verified_at INTEGER NOT NULL,
13895
- mapped_at INTEGER NOT NULL DEFAULT 0,
13896
- PRIMARY KEY (memory_id, file_path)
13777
+ mapped_at INTEGER NOT NULL DEFAULT 0,
13778
+ -- Distinguishes mapper-authored independence from a host rejection fallback.
13779
+ mapping_origin TEXT NOT NULL DEFAULT 'mapper',
13780
+ PRIMARY KEY (memory_id, file_path)
13897
13781
  );
13898
13782
  CREATE INDEX IF NOT EXISTS idx_memory_verifications_memory ON memory_verifications(memory_id);
13899
13783
 
@@ -15154,6 +15038,13 @@ function clearCachedM0M1(db, sessionId) {
15154
15038
  var emergencyRecoveryArmedSessions = new Set;
15155
15039
  var emergencyRecoveryArmedAtBySession = new Map;
15156
15040
  var providerOverflowReconfirmedSessions = new Set;
15041
+ function describeProtectedTailDrainBudgetSkip(result) {
15042
+ const state = result.budgetState;
15043
+ if (!state)
15044
+ return "historian skip: internal drain budget spent";
15045
+ const resetMinutes = Math.max(1, Math.ceil(state.resetInMs / 60000));
15046
+ return `historian skip: internal drain budget spent (${state.spentTokens}/${state.limitTokens} tokens; resets in ${resetMinutes}m)`;
15047
+ }
15157
15048
  var CAS_RETRY_LIMIT = 5;
15158
15049
  var AUTO_SEARCH_NO_HINT_REASONS = new Set([
15159
15050
  "below-threshold",
@@ -15500,19 +15391,28 @@ function reserveProtectedTailDrainTokens(args) {
15500
15391
  const now = args.now ?? Date.now();
15501
15392
  const requested = Math.max(0, Math.floor(args.trueRawTokens));
15502
15393
  if (requested === 0) {
15503
- return { ok: true, reservedTokens: 0, overQuotaBypass: false, reservation: null };
15394
+ return {
15395
+ ok: true,
15396
+ reservedTokens: 0,
15397
+ overQuotaBypass: false,
15398
+ reservation: null,
15399
+ budgetState: null
15400
+ };
15504
15401
  }
15505
15402
  let result = {
15506
15403
  ok: false,
15507
15404
  reservedTokens: 0,
15508
15405
  overQuotaBypass: false,
15509
15406
  reservation: null,
15510
- skippedReason: "quota exhausted"
15407
+ budgetState: null,
15408
+ skippedReason: "internal drain budget spent"
15511
15409
  };
15512
15410
  args.db.transaction(() => {
15513
15411
  ensureSessionMetaRow(args.db, args.sessionId);
15514
15412
  let meta = loadProtectedTailMeta(args.db, args.sessionId);
15515
- if (now - meta.protectedTailDrainWindowStartedAt > DRAIN_WINDOW_MS) {
15413
+ const windowStartedAt = meta.protectedTailDrainWindowStartedAt;
15414
+ const windowExpired = windowStartedAt <= 0 || windowStartedAt > now || now - windowStartedAt >= DRAIN_WINDOW_MS;
15415
+ if (windowExpired) {
15516
15416
  args.db.prepare(`UPDATE session_meta
15517
15417
  SET protected_tail_drain_window_started_at = ?, protected_tail_drain_tokens = 0
15518
15418
  WHERE session_id = ?`).run(now, args.sessionId);
@@ -15537,22 +15437,35 @@ function reserveProtectedTailDrainTokens(args) {
15537
15437
  const remaining = Math.max(0, budget - meta.protectedTailDrainTokens);
15538
15438
  let reserved = Math.min(requested, args.perRunCap, remaining);
15539
15439
  let bypass = false;
15540
- const inFailureBackoff = meta.historianDrainFailureAt > 0 && now - meta.historianDrainFailureAt < EMERGENCY_DRAIN_FAILURE_BACKOFF_MS;
15440
+ const inFailureBackoff = meta.historianDrainFailureAt > 0 && meta.historianDrainFailureAt <= now && now - meta.historianDrainFailureAt < EMERGENCY_DRAIN_FAILURE_BACKOFF_MS;
15541
15441
  if (reserved <= 0 && latchActive && !inFailureBackoff) {
15542
15442
  reserved = Math.min(requested, args.perRunCap);
15543
15443
  bypass = true;
15544
15444
  }
15545
- if (reserved <= 0)
15445
+ const activeWindowStartedAt = meta.protectedTailDrainWindowStartedAt;
15446
+ const budgetState = (spentTokens) => ({
15447
+ windowStartedAt: activeWindowStartedAt,
15448
+ resetsAt: activeWindowStartedAt + DRAIN_WINDOW_MS,
15449
+ resetInMs: Math.max(0, activeWindowStartedAt + DRAIN_WINDOW_MS - now),
15450
+ spentTokens,
15451
+ limitTokens: budget
15452
+ });
15453
+ if (reserved <= 0) {
15454
+ result = {
15455
+ ...result,
15456
+ budgetState: budgetState(meta.protectedTailDrainTokens)
15457
+ };
15546
15458
  return;
15459
+ }
15547
15460
  args.db.prepare(`UPDATE session_meta
15548
- SET protected_tail_drain_window_started_at = CASE WHEN protected_tail_drain_window_started_at = 0 THEN ? ELSE protected_tail_drain_window_started_at END,
15549
- protected_tail_drain_tokens = COALESCE(protected_tail_drain_tokens, 0) + ?
15550
- WHERE session_id = ?`).run(now, reserved, args.sessionId);
15461
+ SET protected_tail_drain_tokens = COALESCE(protected_tail_drain_tokens, 0) + ?
15462
+ WHERE session_id = ?`).run(reserved, args.sessionId);
15551
15463
  result = {
15552
15464
  ok: true,
15553
15465
  reservedTokens: reserved,
15554
15466
  overQuotaBypass: bypass,
15555
- reservation: { sessionId: args.sessionId, runId: args.runId, tokens: reserved }
15467
+ reservation: { sessionId: args.sessionId, runId: args.runId, tokens: reserved },
15468
+ budgetState: budgetState(meta.protectedTailDrainTokens + reserved)
15556
15469
  };
15557
15470
  })();
15558
15471
  return result;
@@ -15626,19 +15539,38 @@ function parseChannel1NudgeState(raw) {
15626
15539
  try {
15627
15540
  const parsed = JSON.parse(raw);
15628
15541
  if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
15629
- return {
15542
+ const state = {
15630
15543
  level: normalizeLastNudgeLevel(parsed.level),
15631
15544
  ordinal: typeof parsed.ordinal === "number" ? Math.max(0, Math.round(parsed.ordinal)) : 0
15632
15545
  };
15546
+ if (parsed.postReduceGracePending === true)
15547
+ state.postReduceGracePending = true;
15548
+ if (typeof parsed.postReduceGraceBaselineU === "number" && Number.isFinite(parsed.postReduceGraceBaselineU)) {
15549
+ state.postReduceGraceBaselineU = Math.max(0, Math.round(parsed.postReduceGraceBaselineU));
15550
+ }
15551
+ const preLevel = normalizeLastNudgeLevel(parsed.postReduceGracePreLevel);
15552
+ if (preLevel !== "" || parsed.postReduceGracePreLevel === "") {
15553
+ state.postReduceGracePreLevel = preLevel;
15554
+ }
15555
+ return state;
15633
15556
  }
15634
15557
  } catch {}
15635
15558
  return { level: normalizeLastNudgeLevel(raw), ordinal: 0 };
15636
15559
  }
15637
15560
  function serializeChannel1NudgeState(value) {
15638
- return JSON.stringify({
15561
+ const serialized = {
15639
15562
  level: normalizeLastNudgeLevel(value.level),
15640
15563
  ordinal: Math.max(0, Math.round(value.ordinal))
15641
- });
15564
+ };
15565
+ if (value.postReduceGracePending === true)
15566
+ serialized.postReduceGracePending = true;
15567
+ if (value.postReduceGraceBaselineU !== undefined) {
15568
+ serialized.postReduceGraceBaselineU = Math.max(0, Math.round(value.postReduceGraceBaselineU));
15569
+ }
15570
+ if (value.postReduceGracePreLevel !== undefined) {
15571
+ serialized.postReduceGracePreLevel = normalizeLastNudgeLevel(value.postReduceGracePreLevel);
15572
+ }
15573
+ return JSON.stringify(serialized);
15642
15574
  }
15643
15575
  function getLastNudgeUndropped(db, sessionId) {
15644
15576
  const result = db.prepare("SELECT last_nudge_undropped FROM session_meta WHERE session_id = ?").get(sessionId);
@@ -15660,20 +15592,36 @@ function setChannel1NudgeState(db, sessionId, value) {
15660
15592
  db.prepare("UPDATE session_meta SET last_nudge_level = ? WHERE session_id = ?").run(serializeChannel1NudgeState(value), sessionId);
15661
15593
  })();
15662
15594
  }
15663
- function resetLastNudgeCycle(db, sessionId) {
15664
- db.transaction(() => {
15595
+ function markChannel1PostReduceGracePending(db, sessionId) {
15596
+ return db.transaction(() => {
15665
15597
  ensureSessionMetaRow(db, sessionId);
15666
- db.prepare("UPDATE session_meta SET last_nudge_undropped = 0, last_nudge_level = ? WHERE session_id = ?").run(serializeChannel1NudgeState(EMPTY_CHANNEL1_NUDGE_STATE), sessionId);
15598
+ const current = getChannel1NudgeState(db, sessionId);
15599
+ const next = {
15600
+ level: current.level,
15601
+ ordinal: current.ordinal,
15602
+ postReduceGracePending: true,
15603
+ postReduceGracePreLevel: current.level
15604
+ };
15605
+ db.prepare("UPDATE session_meta SET last_nudge_level = ? WHERE session_id = ?").run(serializeChannel1NudgeState(next), sessionId);
15606
+ return next;
15667
15607
  })();
15668
15608
  }
15669
- function resetLastNudgeCycleIfTailShrank(db, sessionId, measuredUndropped) {
15670
- let changed = false;
15671
- db.transaction(() => {
15609
+ function captureChannel1PostReduceGraceBaseline(db, sessionId, measuredUndropped) {
15610
+ return db.transaction(() => {
15672
15611
  ensureSessionMetaRow(db, sessionId);
15673
- const result = db.prepare("UPDATE session_meta SET last_nudge_undropped = 0, last_nudge_level = ? WHERE session_id = ? AND last_nudge_undropped > ?").run(serializeChannel1NudgeState(EMPTY_CHANNEL1_NUDGE_STATE), sessionId, Math.max(0, Math.round(measuredUndropped)));
15674
- changed = (result.changes ?? 0) > 0;
15612
+ const current = getChannel1NudgeState(db, sessionId);
15613
+ if (current.postReduceGracePending !== true)
15614
+ return current;
15615
+ const baselineU = Math.max(0, Math.round(measuredUndropped));
15616
+ const next = {
15617
+ level: current.level,
15618
+ ordinal: current.ordinal,
15619
+ postReduceGraceBaselineU: baselineU,
15620
+ postReduceGracePreLevel: current.postReduceGracePreLevel ?? current.level
15621
+ };
15622
+ db.prepare("UPDATE session_meta SET last_nudge_level = ? WHERE session_id = ?").run(serializeChannel1NudgeState(next), sessionId);
15623
+ return next;
15675
15624
  })();
15676
- return changed;
15677
15625
  }
15678
15626
  function isChannel2StateRow(row) {
15679
15627
  return typeof row === "object" && row !== null && typeof row.channel2_nudge_state === "string";
@@ -16473,9 +16421,11 @@ function retryPendingSessionCleanups(db, limit = 200) {
16473
16421
  return { attempted: rows.length, cleared, failedSessionIds };
16474
16422
  }
16475
16423
  function clearSession(db, sessionId) {
16424
+ const transactionStartedAt = performance.now();
16476
16425
  db.transaction(() => {
16477
16426
  deleteSessionScopedRows(db, [sessionId]);
16478
16427
  })();
16428
+ logSlowWriteTransaction("clear-session", transactionStartedAt);
16479
16429
  }
16480
16430
  // ../plugin/src/hooks/magic-context/todo-view.ts
16481
16431
  import { createHash as createHash4 } from "node:crypto";
@@ -17356,15 +17306,32 @@ function buildToolArcs(messages) {
17356
17306
  }
17357
17307
  return arcs.sort((a, b) => a.invOrdinal - b.invOrdinal || (a.resOrdinal ?? Number.MAX_SAFE_INTEGER) - (b.resOrdinal ?? Number.MAX_SAFE_INTEGER));
17358
17308
  }
17309
+ function fenceBoundaryForCompletedToolArcs(candidate, arcs, publicationFloorOrdinal) {
17310
+ const completed = arcs.filter((arc) => arc.resOrdinal !== null).map((arc) => ({ invocation: arc.invOrdinal, result: arc.resOrdinal }));
17311
+ const component = completed.filter((arc) => completedToolArcCrossesBoundary(arc.invocation, arc.result, candidate));
17312
+ if (component.length === 0)
17313
+ return candidate;
17314
+ for (let pass = 0;pass <= completed.length; pass += 1) {
17315
+ const minInvocation2 = Math.min(...component.map((arc) => arc.invocation));
17316
+ const maxResult2 = Math.max(...component.map((arc) => arc.result));
17317
+ const before = component.length;
17318
+ for (const arc of completed) {
17319
+ if (arc.invocation <= maxResult2 && arc.result >= minInvocation2 && !component.includes(arc)) {
17320
+ component.push(arc);
17321
+ }
17322
+ }
17323
+ if (component.length === before)
17324
+ break;
17325
+ }
17326
+ const minInvocation = Math.min(...component.map((arc) => arc.invocation));
17327
+ const maxResult = Math.max(...component.map((arc) => arc.result));
17328
+ return minInvocation < publicationFloorOrdinal ? maxResult + 1 : minInvocation;
17329
+ }
17359
17330
  function fenceBoundaryForToolArcs(candidate, arcs, lastCompartmentEndOrdinal, recentOpenArcCutoff) {
17360
- let boundary = candidate;
17331
+ const boundary = fenceBoundaryForCompletedToolArcs(candidate, arcs, lastCompartmentEndOrdinal + 1);
17361
17332
  for (const arc of arcs) {
17362
- if (arc.resOrdinal !== null) {
17363
- if (completedToolArcCrossesBoundary(arc.invOrdinal, arc.resOrdinal, boundary)) {
17364
- boundary = arc.resOrdinal + 1;
17365
- }
17333
+ if (arc.resOrdinal !== null)
17366
17334
  continue;
17367
- }
17368
17335
  if (arc.invOrdinal < recentOpenArcCutoff)
17369
17336
  continue;
17370
17337
  if (arc.invOrdinal >= lastCompartmentEndOrdinal + 1 && arc.invOrdinal < boundary) {
@@ -18297,6 +18264,16 @@ function persistMessageHistoryOrphanSweepState(db, cursor, lastSweptAt) {
18297
18264
  cursor_session_id = excluded.cursor_session_id,
18298
18265
  last_swept_at = excluded.last_swept_at`).run(cursor, lastSweptAt);
18299
18266
  }
18267
+ function getOpenCodeSessionScopedCandidateSourceSql() {
18268
+ return SESSION_SCOPED_TABLES.filter((definition) => definition.harnessScoped === true).map((definition) => {
18269
+ const predicates = ["session_id IS NOT NULL", "harness = 'opencode'"];
18270
+ if (definition.extraPredicate)
18271
+ predicates.push(definition.extraPredicate);
18272
+ return `SELECT session_id FROM ${definition.table} WHERE ${predicates.join(" AND ")}`;
18273
+ }).join(`
18274
+ UNION
18275
+ `);
18276
+ }
18300
18277
  function sweepOrphanedOpenCodeMessageIndexes(db, openReadableOpenCodeDb, options = {}) {
18301
18278
  const now = options.now ?? Date.now();
18302
18279
  const batchSize = Math.max(1, Math.floor(options.batchSize ?? MESSAGE_HISTORY_ORPHAN_SWEEP_BATCH_SIZE));
@@ -18320,13 +18297,19 @@ function sweepOrphanedOpenCodeMessageIndexes(db, openReadableOpenCodeDb, options
18320
18297
  }
18321
18298
  try {
18322
18299
  const cutoff = now - safetyAgeMs;
18300
+ const candidateSourceSql = getOpenCodeSessionScopedCandidateSourceSql();
18323
18301
  const candidates = db.prepare(`SELECT session_id
18324
- FROM message_history_index
18325
- WHERE harness = 'opencode'
18326
- AND updated_at <= ?
18327
- AND session_id > ?
18302
+ FROM (${candidateSourceSql}) AS session_candidates
18303
+ WHERE session_id > ?
18304
+ AND NOT EXISTS (
18305
+ SELECT 1
18306
+ FROM message_history_index
18307
+ WHERE message_history_index.session_id = session_candidates.session_id
18308
+ AND message_history_index.harness = 'opencode'
18309
+ AND message_history_index.updated_at > ?
18310
+ )
18328
18311
  ORDER BY session_id ASC
18329
- LIMIT ?`).all(cutoff, cursor, batchSize);
18312
+ LIMIT ?`).all(cursor, cutoff, batchSize);
18330
18313
  const sessionExists = openCodeDb.prepare("SELECT 1 FROM session WHERE id = ? LIMIT 1");
18331
18314
  const missingSessionIds = candidates.filter((candidate) => !sessionExists.get(candidate.session_id)).map((candidate) => candidate.session_id);
18332
18315
  const nextCursor = candidates.length < batchSize ? "" : candidates[candidates.length - 1]?.session_id ?? cursor;
@@ -18335,7 +18318,17 @@ function sweepOrphanedOpenCodeMessageIndexes(db, openReadableOpenCodeDb, options
18335
18318
  let committed = false;
18336
18319
  let deleted = 0;
18337
18320
  try {
18338
- const stillEligible = db.prepare("SELECT 1 FROM message_history_index WHERE session_id = ? AND harness = 'opencode' AND updated_at <= ?");
18321
+ const stillEligible = db.prepare(`SELECT 1
18322
+ FROM (${candidateSourceSql}) AS session_candidates
18323
+ WHERE session_id = ?
18324
+ AND NOT EXISTS (
18325
+ SELECT 1
18326
+ FROM message_history_index
18327
+ WHERE message_history_index.session_id = session_candidates.session_id
18328
+ AND message_history_index.harness = 'opencode'
18329
+ AND message_history_index.updated_at > ?
18330
+ )
18331
+ LIMIT 1`);
18339
18332
  const eligibleSessionIds = missingSessionIds.filter((sessionId) => stillEligible.get(sessionId, cutoff));
18340
18333
  deleteSessionScopedRows(db, eligibleSessionIds, "opencode");
18341
18334
  deleted = eligibleSessionIds.length;
@@ -19201,6 +19194,7 @@ function markNoteChecked(db, noteId) {
19201
19194
  // ../plugin/src/features/magic-context/storage-ops.ts
19202
19195
  var queuePendingOpStatements = new WeakMap;
19203
19196
  var getPendingOpsStatements = new WeakMap;
19197
+ var getPendingOpsCountStatements = new WeakMap;
19204
19198
  var clearPendingOpsStatements = new WeakMap;
19205
19199
  var removePendingOpStatements = new WeakMap;
19206
19200
  function getQueuePendingOpStatement(db) {
@@ -19261,6 +19255,19 @@ function getPendingOps(db, sessionId) {
19261
19255
  const rows = getPendingOpsStatement(db).all(sessionId).filter(isPendingOpRow);
19262
19256
  return rows.map(toPendingOp).filter((op) => op !== null);
19263
19257
  }
19258
+ function getPendingOpsCount(db, sessionId) {
19259
+ try {
19260
+ let statement = getPendingOpsCountStatements.get(db);
19261
+ if (!statement) {
19262
+ statement = db.prepare("SELECT COUNT(*) AS count FROM pending_ops WHERE session_id = ?");
19263
+ getPendingOpsCountStatements.set(db, statement);
19264
+ }
19265
+ const row = statement.get(sessionId);
19266
+ return typeof row?.count === "number" && Number.isFinite(row.count) ? row.count : null;
19267
+ } catch {
19268
+ return null;
19269
+ }
19270
+ }
19264
19271
  function clearPendingOps(db, sessionId) {
19265
19272
  getClearPendingOpsStatement(db).run(sessionId);
19266
19273
  }
@@ -19806,47 +19813,74 @@ function decideChannel1(input) {
19806
19813
  const tailTokens = Math.max(0, input.baselineT + input.turnDeltaT);
19807
19814
  const undroppedTokens = Math.min(tailTokens, Math.max(0, input.baselineU + input.turnDeltaU));
19808
19815
  const severity = Math.min(1, Math.max(0, undroppedTokens / Math.max(tailTokens, 1)));
19809
- const resetCycle = input.hasRecentReduce || undroppedTokens < input.lastNudgeUndropped;
19810
- const lastNudge = resetCycle ? 0 : input.lastNudgeUndropped;
19811
- const lastLevel = resetCycle ? "" : input.lastNudgeLevel;
19812
- const quiet = () => ({
19816
+ const previousLevel = input.lastNudgeLevel;
19817
+ let lastNudge = Math.max(0, input.lastNudgeUndropped);
19818
+ let nextLevel = previousLevel;
19819
+ let clearPostReduceGrace = false;
19820
+ const quiet = (level2 = "gentle") => ({
19813
19821
  fire: false,
19814
- level: "gentle",
19822
+ sticky: false,
19823
+ level: level2,
19815
19824
  undroppedTokens,
19816
19825
  tailTokens,
19817
19826
  severity,
19818
19827
  nextLastNudge: lastNudge,
19819
- nextLastNudgeLevel: lastLevel
19828
+ nextLastNudgeLevel: nextLevel,
19829
+ clearPostReduceGrace
19820
19830
  });
19821
19831
  if (input.evaluable === false || input.generationInvalidated === true)
19822
19832
  return quiet();
19823
- if (input.hasRecentReduce)
19824
- return quiet();
19825
- if (tailTokens < CHANNEL1_MIN_TOKENS)
19826
- return quiet();
19827
- if (undroppedTokens < CHANNEL1_FLOOR_TOKENS || undroppedTokens === 0)
19828
- return quiet();
19829
- if (severity < S_GENTLE)
19833
+ if (input.hasRecentReduce || input.postReduceGracePending)
19830
19834
  return quiet();
19831
- let level;
19832
- if (severity >= S_URGENT)
19833
- level = "urgent";
19834
- else if (severity >= S_FIRM)
19835
- level = "firm";
19836
- else
19837
- level = "gentle";
19838
- const escalated = lastLevel === "" || LEVEL_RANK[level] > LEVEL_RANK[lastLevel];
19839
- const cadenceReached = lastLevel !== "" && undroppedTokens - lastNudge >= channel1RefireTokens(tailTokens);
19840
- if (!escalated && !cadenceReached)
19835
+ let level = "";
19836
+ if (tailTokens >= CHANNEL1_MIN_TOKENS && undroppedTokens >= CHANNEL1_FLOOR_TOKENS && severity >= S_GENTLE) {
19837
+ if (severity >= S_URGENT)
19838
+ level = "urgent";
19839
+ else if (severity >= S_FIRM)
19840
+ level = "firm";
19841
+ else
19842
+ level = "gentle";
19843
+ }
19844
+ const previousRank = previousLevel === "" ? 0 : LEVEL_RANK[previousLevel];
19845
+ const currentRank = level === "" ? 0 : LEVEL_RANK[level];
19846
+ const graceBaseline = input.postReduceGraceBaselineU;
19847
+ if (graceBaseline !== undefined) {
19848
+ const preReduceLevel = input.postReduceGracePreLevel ?? previousLevel;
19849
+ const preReduceRank = preReduceLevel === "" ? 0 : LEVEL_RANK[preReduceLevel];
19850
+ const regrowthReached = undroppedTokens - Math.max(0, graceBaseline) >= channel1RefireTokens(tailTokens);
19851
+ const escalatedAbovePreReduceBand = currentRank > preReduceRank;
19852
+ if (!regrowthReached && !escalatedAbovePreReduceBand) {
19853
+ return quiet(level === "" ? "gentle" : level);
19854
+ }
19855
+ clearPostReduceGrace = true;
19856
+ }
19857
+ if (level === "") {
19858
+ nextLevel = "";
19859
+ lastNudge = 0;
19841
19860
  return quiet();
19861
+ }
19862
+ if (currentRank < previousRank) {
19863
+ nextLevel = level;
19864
+ lastNudge = undroppedTokens;
19865
+ return quiet(level);
19866
+ }
19867
+ const crossedFromBelow = currentRank > previousRank;
19868
+ const cadenceReached = currentRank === previousRank && undroppedTokens - lastNudge >= channel1RefireTokens(tailTokens);
19869
+ const currentTurn = input.currentRealUserTurnCount;
19870
+ const lastFireTurn = input.lastFireOrdinal;
19871
+ const stickyTurnGapReached = currentTurn === undefined || lastFireTurn === undefined || lastFireTurn > currentTurn || currentTurn - lastFireTurn >= CHANNEL1_STICKY_REAL_USER_TURN_GAP;
19872
+ if (!crossedFromBelow && (!cadenceReached || !stickyTurnGapReached))
19873
+ return quiet(level);
19842
19874
  return {
19843
19875
  fire: true,
19876
+ sticky: !crossedFromBelow,
19844
19877
  level,
19845
19878
  undroppedTokens,
19846
19879
  tailTokens,
19847
19880
  severity,
19848
19881
  nextLastNudge: undroppedTokens,
19849
- nextLastNudgeLevel: level
19882
+ nextLastNudgeLevel: level,
19883
+ clearPostReduceGrace
19850
19884
  };
19851
19885
  }
19852
19886
  var CHANNEL2_SEVERITY_THRESHOLD = 0.75;
@@ -19883,44 +19917,42 @@ function formatOldestReclaimableHint(hint) {
19883
19917
  return rendered.length > 0 ? `
19884
19918
  oldest reclaimable: ${rendered}.` : "";
19885
19919
  }
19886
- function buildChannel2Reminder(undroppedTokens, usableWindowTokens, hint) {
19887
- const amount = approxThousands(undroppedTokens);
19888
- const usableWindow = approxThousands(usableWindowTokens);
19920
+ function reclaimableToolOutputCount(parts) {
19921
+ return parts.filter((part) => part.kind === "toolOutput" && part.uTokens > 0).length;
19922
+ }
19923
+ function formatReclaimableOutputSummary(count, tokens) {
19924
+ const outputCount = Math.max(0, Math.floor(count));
19925
+ const outputs = outputCount === 0 ? "spent tool outputs" : `${outputCount} spent tool output${outputCount === 1 ? "" : "s"}`;
19926
+ return `${outputs} (~${approxThousands(tokens)} tokens)`;
19927
+ }
19928
+ function buildChannel2Reminder(undroppedTokens, reclaimableToolOutputs, hint) {
19929
+ const summary = formatReclaimableOutputSummary(reclaimableToolOutputs, undroppedTokens);
19889
19930
  const hintText = formatOldestReclaimableHint(hint);
19890
19931
  return `<system-reminder>
19891
- ` + `Routine housekeeping: an older span of this session folds into compact history automatically — nothing is lost and nothing pauses. ` + `Drop spent tool outputs with ctx_reduce first so the archive keeps only what matters (~${amount} of ~${usableWindow} reclaimable).${hintText}
19932
+ ` + `Routine housekeeping: ${summary} are reclaimable — make a ctx_reduce pass at a natural stopping point.${hintText}
19892
19933
  ` + `</system-reminder>`;
19893
19934
  }
19894
- var CHANNEL1_STICKY_REAL_USER_TURN_GAP = 3;
19895
- function shouldUseStickyChannel1Reminder(input) {
19896
- if (input.lastLevel !== input.level)
19897
- return false;
19898
- if (input.lastOrdinal > input.currentRealUserTurnCount)
19899
- return false;
19900
- const gap = input.currentRealUserTurnCount - input.lastOrdinal;
19901
- return gap >= 0 && gap < CHANNEL1_STICKY_REAL_USER_TURN_GAP;
19902
- }
19903
- function buildChannel1Reminder(level, undroppedTokens, usableWindowTokens, hint, sticky = false) {
19935
+ var CHANNEL1_STICKY_REAL_USER_TURN_GAP = 5;
19936
+ function buildChannel1Reminder(level, undroppedTokens, reclaimableToolOutputs, hint, sticky = false) {
19937
+ const summary = formatReclaimableOutputSummary(reclaimableToolOutputs, undroppedTokens);
19904
19938
  const hintText = formatOldestReclaimableHint(hint);
19905
19939
  if (sticky) {
19906
19940
  return `
19907
19941
 
19908
19942
  <system-reminder>
19909
- Reminder: ctx_reduce housekeeping still pending —${hintText}
19943
+ Reminder: ${summary} are still reclaimable — ctx_reduce them at a natural stopping point.${hintText}
19910
19944
  </system-reminder>`;
19911
19945
  }
19912
- const amount = approxThousands(undroppedTokens);
19913
- const usableWindow = approxThousands(usableWindowTokens);
19914
19946
  let body;
19915
19947
  switch (level) {
19916
19948
  case "gentle":
19917
- body = `Housekeeping: some earlier tool outputs are spent and can be dropped with ctx_reduce when you are done with them. ` + `Context is managed automatically — this is tidiness, never a reason to rush or narrow scope.`;
19949
+ body = `Housekeeping: ${summary} are reclaimable — drop the ones you have already processed with ctx_reduce at a natural stopping point.`;
19918
19950
  break;
19919
19951
  case "firm":
19920
- body = `Housekeeping: ~${amount} of this session's ~${usableWindow} window is spent tool output. ` + `Drop what you have already processed with ctx_reduce at a natural stopping point. ` + `Not a limit — nothing is lost either way.`;
19952
+ body = `Housekeeping: ${summary} are reclaimable — make a ctx_reduce pass at a natural stopping point.`;
19921
19953
  break;
19922
19954
  case "urgent":
19923
- body = `Housekeeping backlog: ~${amount} of this session's ~${usableWindow} window is spent tool output — worth a ctx_reduce pass now. ` + `This is routine and lossless; it is never a reason to change scope.`;
19955
+ body = `Housekeeping backlog: ${summary} are reclaimable — a ctx_reduce pass is due.`;
19924
19956
  break;
19925
19957
  }
19926
19958
  return `
@@ -19981,8 +20013,8 @@ function planEmergencyDrop(input) {
19981
20013
  if (!Number.isFinite(currentTotalInputTokens) || currentTotalInputTokens <= 0) {
19982
20014
  return noop("unknown-usage");
19983
20015
  }
19984
- if (hasPriorDrop && currentTotalInputTokens === priorInputSample) {
19985
- return noop("same-input-sample (awaiting fresh usage after prior drop)");
20016
+ if (hasPriorDrop) {
20017
+ return noop(`pressure-episode-latched (prior sample ${priorInputSample}; awaiting exit or independent bust)`);
19986
20018
  }
19987
20019
  let tailTokens = 0;
19988
20020
  for (const tag of floorTags) {
@@ -20111,6 +20143,7 @@ function updateTagByteSize(db, sessionId, tagNumber, newByteSize) {
20111
20143
  getUpdateTagByteSizeStatement(db).run(newByteSize, sessionId, tagNumber);
20112
20144
  }
20113
20145
  var CONTENT_ID_SUFFIX = /:(?:p|file)\d+$/;
20146
+ var recentTagOwnerStatements = new WeakMap;
20114
20147
  function ownerMessageIdForTagRow(row) {
20115
20148
  if (row.type === "tool") {
20116
20149
  return row.tool_owner_message_id ?? row.message_id;
@@ -21220,22 +21253,28 @@ function getToolCallIds(content) {
21220
21253
  }
21221
21254
  return ids;
21222
21255
  }
21223
- function readPiSessionMessages(ctx) {
21256
+ function readPiSessionSnapshot(ctx) {
21224
21257
  const sm = ctx.sessionManager;
21225
21258
  if (sm === undefined)
21226
- return [];
21259
+ return { branchEntries: [], rawMessages: [] };
21227
21260
  const getBranch = sm.getBranch;
21228
21261
  if (typeof getBranch !== "function")
21229
- return [];
21262
+ return { branchEntries: [], rawMessages: [] };
21230
21263
  let entries;
21231
21264
  try {
21232
21265
  entries = getBranch.call(sm);
21233
21266
  } catch {
21234
- return [];
21267
+ return { branchEntries: [], rawMessages: [] };
21235
21268
  }
21236
21269
  if (!Array.isArray(entries))
21237
- return [];
21238
- return convertEntriesToRawMessages(entries);
21270
+ return { branchEntries: [], rawMessages: [] };
21271
+ return {
21272
+ branchEntries: entries,
21273
+ rawMessages: convertEntriesToRawMessages(entries)
21274
+ };
21275
+ }
21276
+ function readPiSessionMessages(ctx) {
21277
+ return readPiSessionSnapshot(ctx).rawMessages;
21239
21278
  }
21240
21279
  function findLastModelKeyFromBranch(entries) {
21241
21280
  if (!Array.isArray(entries))
@@ -21785,7 +21824,7 @@ function migrateLegacyExperimental(rawConfig, warnings) {
21785
21824
  }
21786
21825
 
21787
21826
  // ../plugin/src/config/schema/magic-context.ts
21788
- import { homedir as homedir5 } from "node:os";
21827
+ import { homedir as homedir4 } from "node:os";
21789
21828
 
21790
21829
  // ../../node_modules/.bun/zod@4.4.3/node_modules/zod/v4/classic/external.js
21791
21830
  var exports_external = {};
@@ -22412,7 +22451,7 @@ __export(exports_util, {
22412
22451
  createTransparentProxy: () => createTransparentProxy,
22413
22452
  defineLazy: () => defineLazy,
22414
22453
  esc: () => esc,
22415
- escapeRegex: () => escapeRegex2,
22454
+ escapeRegex: () => escapeRegex,
22416
22455
  explicitlyAborted: () => explicitlyAborted,
22417
22456
  extend: () => extend,
22418
22457
  finalizeIssue: () => finalizeIssue,
@@ -22689,7 +22728,7 @@ var primitiveTypes = /* @__PURE__ */ new Set([
22689
22728
  "symbol",
22690
22729
  "undefined"
22691
22730
  ]);
22692
- function escapeRegex2(str) {
22731
+ function escapeRegex(str) {
22693
22732
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
22694
22733
  }
22695
22734
  function clone(inst, def, params) {
@@ -23399,7 +23438,7 @@ function emoji() {
23399
23438
  var ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
23400
23439
  var ipv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/;
23401
23440
  var mac = (delimiter) => {
23402
- const escapedDelim = escapeRegex2(delimiter ?? ":");
23441
+ const escapedDelim = escapeRegex(delimiter ?? ":");
23403
23442
  return new RegExp(`^(?:[0-9A-F]{2}${escapedDelim}){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}${escapedDelim}){5}[0-9a-f]{2}$`);
23404
23443
  };
23405
23444
  var cidrv4 = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/;
@@ -23900,7 +23939,7 @@ var $ZodCheckUpperCase = /* @__PURE__ */ $constructor("$ZodCheckUpperCase", (ins
23900
23939
  });
23901
23940
  var $ZodCheckIncludes = /* @__PURE__ */ $constructor("$ZodCheckIncludes", (inst, def) => {
23902
23941
  $ZodCheck.init(inst, def);
23903
- const escapedRegex = escapeRegex2(def.includes);
23942
+ const escapedRegex = escapeRegex(def.includes);
23904
23943
  const pattern = new RegExp(typeof def.position === "number" ? `^.{${def.position}}${escapedRegex}` : escapedRegex);
23905
23944
  def.pattern = pattern;
23906
23945
  inst._zod.onattach.push((inst2) => {
@@ -23924,7 +23963,7 @@ var $ZodCheckIncludes = /* @__PURE__ */ $constructor("$ZodCheckIncludes", (inst,
23924
23963
  });
23925
23964
  var $ZodCheckStartsWith = /* @__PURE__ */ $constructor("$ZodCheckStartsWith", (inst, def) => {
23926
23965
  $ZodCheck.init(inst, def);
23927
- const pattern = new RegExp(`^${escapeRegex2(def.prefix)}.*`);
23966
+ const pattern = new RegExp(`^${escapeRegex(def.prefix)}.*`);
23928
23967
  def.pattern ?? (def.pattern = pattern);
23929
23968
  inst._zod.onattach.push((inst2) => {
23930
23969
  const bag = inst2._zod.bag;
@@ -23947,7 +23986,7 @@ var $ZodCheckStartsWith = /* @__PURE__ */ $constructor("$ZodCheckStartsWith", (i
23947
23986
  });
23948
23987
  var $ZodCheckEndsWith = /* @__PURE__ */ $constructor("$ZodCheckEndsWith", (inst, def) => {
23949
23988
  $ZodCheck.init(inst, def);
23950
- const pattern = new RegExp(`.*${escapeRegex2(def.suffix)}$`);
23989
+ const pattern = new RegExp(`.*${escapeRegex(def.suffix)}$`);
23951
23990
  def.pattern ?? (def.pattern = pattern);
23952
23991
  inst._zod.onattach.push((inst2) => {
23953
23992
  const bag = inst2._zod.bag;
@@ -25594,7 +25633,7 @@ var $ZodEnum = /* @__PURE__ */ $constructor("$ZodEnum", (inst, def) => {
25594
25633
  const values = getEnumValues(def.entries);
25595
25634
  const valuesSet = new Set(values);
25596
25635
  inst._zod.values = valuesSet;
25597
- inst._zod.pattern = new RegExp(`^(${values.filter((k) => propertyKeyTypes.has(typeof k)).map((o) => typeof o === "string" ? escapeRegex2(o) : o.toString()).join("|")})$`);
25636
+ inst._zod.pattern = new RegExp(`^(${values.filter((k) => propertyKeyTypes.has(typeof k)).map((o) => typeof o === "string" ? escapeRegex(o) : o.toString()).join("|")})$`);
25598
25637
  inst._zod.parse = (payload, _ctx) => {
25599
25638
  const input = payload.value;
25600
25639
  if (valuesSet.has(input)) {
@@ -25616,7 +25655,7 @@ var $ZodLiteral = /* @__PURE__ */ $constructor("$ZodLiteral", (inst, def) => {
25616
25655
  }
25617
25656
  const values = new Set(def.values);
25618
25657
  inst._zod.values = values;
25619
- inst._zod.pattern = new RegExp(`^(${def.values.map((o) => typeof o === "string" ? escapeRegex2(o) : o ? escapeRegex2(o.toString()) : String(o)).join("|")})$`);
25658
+ inst._zod.pattern = new RegExp(`^(${def.values.map((o) => typeof o === "string" ? escapeRegex(o) : o ? escapeRegex(o.toString()) : String(o)).join("|")})$`);
25620
25659
  inst._zod.parse = (payload, _ctx) => {
25621
25660
  const input = payload.value;
25622
25661
  if (values.has(input)) {
@@ -25981,7 +26020,7 @@ var $ZodTemplateLiteral = /* @__PURE__ */ $constructor("$ZodTemplateLiteral", (i
25981
26020
  const end = source.endsWith("$") ? source.length - 1 : source.length;
25982
26021
  regexParts.push(source.slice(start, end));
25983
26022
  } else if (part === null || primitiveTypes.has(typeof part)) {
25984
- regexParts.push(escapeRegex2(`${part}`));
26023
+ regexParts.push(escapeRegex(`${part}`));
25985
26024
  } else {
25986
26025
  throw new Error(`Invalid template literal part: ${part}`);
25987
26026
  }
@@ -36557,9 +36596,9 @@ var EmbeddingFallbackProviderSchema = exports_external.enum(["local", "openai-co
36557
36596
  function expandConfigPath(value) {
36558
36597
  const trimmed = value.trim();
36559
36598
  if (trimmed === "~")
36560
- return homedir5();
36599
+ return homedir4();
36561
36600
  if (trimmed.startsWith("~/"))
36562
- return `${homedir5()}/${trimmed.slice(2)}`;
36601
+ return `${homedir4()}/${trimmed.slice(2)}`;
36563
36602
  return trimmed;
36564
36603
  }
36565
36604
  var BaseEmbeddingConfigSchema = exports_external.object({
@@ -36572,6 +36611,7 @@ var BaseEmbeddingConfigSchema = exports_external.object({
36572
36611
  query_input_type: exports_external.string().optional().describe("Optional input_type for query (search) embeddings on asymmetric models (e.g. NVIDIA NIM 'query'). When unset, query embeddings use embedding.input_type. Passage/stored content always uses embedding.input_type."),
36573
36612
  truncate: exports_external.string().optional().describe("Optional truncate mode sent in the embedding request body (e.g. NVIDIA NIM accepts 'NONE' | 'START' | 'END'). Omitted from the request when unset."),
36574
36613
  max_input_tokens: exports_external.number().int().positive().optional().describe("Optional maximum input tokens for chunk embeddings. Defaults conservatively to 512 when omitted."),
36614
+ local_runtime: exports_external.enum(["auto", "native", "wasm"]).default("auto").describe("Local provider only: ONNX runtime selection. 'auto' uses native under Node and uses WASM under Bun versions before 1.4.0, where Bun's NAPI teardown race can panic on quit; native is restored automatically on Bun 1.4.0+. Set 'native' only to prefer speed while accepting that pre-1.4.0 Bun crash risk, or 'wasm' to avoid loading the native addon."),
36575
36615
  local_dtype: exports_external.enum([
36576
36616
  "auto",
36577
36617
  "fp32",
@@ -36628,6 +36668,7 @@ var EmbeddingConfigSchema = BaseEmbeddingConfigSchema.transform((data) => {
36628
36668
  return {
36629
36669
  provider: "local",
36630
36670
  model: data.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
36671
+ local_runtime: data.local_runtime,
36631
36672
  ...data.max_input_tokens ? { max_input_tokens: data.max_input_tokens } : {},
36632
36673
  ...data.local_dtype ? { local_dtype: data.local_dtype } : {}
36633
36674
  };
@@ -36708,7 +36749,8 @@ var MagicContextConfigSchema = exports_external.object({
36708
36749
  }).default({ enforce_private_permissions: true }).describe("Storage permission policy. The default keeps session content and memories owner-private. Disabling enforcement is for trusted shared-group storage managed externally; every group member able to read the storage can read all stored session content and memories."),
36709
36750
  embedding: EmbeddingConfigSchema.default({
36710
36751
  provider: "local",
36711
- model: DEFAULT_LOCAL_EMBEDDING_MODEL
36752
+ model: DEFAULT_LOCAL_EMBEDDING_MODEL,
36753
+ local_runtime: "auto"
36712
36754
  }).describe("Embedding provider configuration"),
36713
36755
  subc: exports_external.object({
36714
36756
  connection_file: exports_external.string().trim().min(1).transform(expandConfigPath).describe("Path to the owner-only subc connection file.")
@@ -39000,12 +39042,12 @@ function loadRawConfigFile(options) {
39000
39042
 
39001
39043
  // ../plugin/src/config/variable.ts
39002
39044
  import { existsSync as existsSync10, readFileSync as readFileSync9 } from "node:fs";
39003
- import { homedir as homedir6 } from "node:os";
39045
+ import { homedir as homedir5 } from "node:os";
39004
39046
  import { dirname as dirname6, isAbsolute as isAbsolute3, resolve as resolve4 } from "node:path";
39005
39047
  var ENV_PATTERN = /\{env:([^}]+)\}/g;
39006
39048
  var FILE_PATTERN = /\{file:([^}]+)\}/g;
39007
39049
  function sensitiveFilePathReason(resolvedPath) {
39008
- const home = homedir6();
39050
+ const home = homedir5();
39009
39051
  const sensitiveDirs = [
39010
39052
  { dir: resolve4(home, ".ssh"), label: "SSH keys" },
39011
39053
  { dir: resolve4(home, ".aws"), label: "AWS credentials" },
@@ -39068,7 +39110,7 @@ function substituteConfigVariables(input) {
39068
39110
  }
39069
39111
  let filePath = rawPath.trim();
39070
39112
  if (filePath.startsWith("~/")) {
39071
- filePath = resolve4(homedir6(), filePath.slice(2));
39113
+ filePath = resolve4(homedir5(), filePath.slice(2));
39072
39114
  } else if (!isAbsolute3(filePath)) {
39073
39115
  filePath = resolve4(configDir, filePath);
39074
39116
  }
@@ -40634,39 +40676,76 @@ function mapBackfillCandidateRows(rows) {
40634
40676
  title: row.title
40635
40677
  }));
40636
40678
  }
40637
- var sessionBackfillCandidateStatements = new WeakMap;
40638
- function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, limit, excludeIds) {
40639
- if (excludeIds && excludeIds.length > 0) {
40640
- const placeholders3 = excludeIds.map(() => "?").join(", ");
40641
- const stmt2 = db.prepare(`SELECT c.id AS id,
40642
- c.session_id AS sessionId,
40643
- c.start_message AS startMessage,
40644
- c.end_message AS endMessage,
40645
- c.title AS title
40646
- FROM compartments c
40647
- JOIN session_projects sp
40648
- ON sp.session_id = c.session_id
40649
- AND sp.harness = c.harness
40650
- AND sp.project_path = ?
40651
- WHERE c.session_id = ?
40652
- AND c.start_message IS NOT NULL
40653
- AND c.end_message IS NOT NULL
40654
- AND c.id NOT IN (${placeholders3})
40655
- AND NOT EXISTS (
40656
- SELECT 1
40657
- FROM compartment_chunk_embeddings current
40658
- WHERE current.compartment_id = c.id
40659
- AND current.project_path = ?
40660
- AND current.model_id = ?
40661
- )
40662
- ORDER BY c.start_message ASC, c.id ASC
40663
- LIMIT ?`);
40664
- const rows2 = stmt2.all(projectPath, sessionId, ...excludeIds, projectPath, modelId, Math.max(1, limit));
40665
- return mapBackfillCandidateRows(rows2);
40679
+ function classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens) {
40680
+ const canonicalText = buildCanonicalChunkTextFromFts(db, candidate.sessionId, candidate.startMessage, candidate.endMessage) || buildCompartmentSummaryFallbackText(db, candidate.id);
40681
+ const windows = chunkCanonicalText(canonicalText, candidate.startMessage, candidate.endMessage, maxInputTokens);
40682
+ const existing = getExistingChunkHashes(db, candidate.id, modelId, projectPath);
40683
+ if (windows.some((window) => !existing.has(window.windowIndex)))
40684
+ return "missing";
40685
+ if (existing.size !== windows.length || windows.some((window) => existing.get(window.windowIndex) !== window.chunkHash)) {
40686
+ return "stale";
40666
40687
  }
40667
- let stmt = sessionBackfillCandidateStatements.get(db);
40668
- if (!stmt) {
40669
- stmt = db.prepare(`SELECT c.id AS id,
40688
+ return null;
40689
+ }
40690
+ function selectHashIncompleteChunkCandidates(db, projectPath, modelId, candidates, limit, maxInputTokens) {
40691
+ const missing = [];
40692
+ const stale = [];
40693
+ for (const candidate of candidates) {
40694
+ const defect = classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens);
40695
+ if (defect === "missing")
40696
+ missing.push(candidate);
40697
+ else if (defect === "stale")
40698
+ stale.push(candidate);
40699
+ }
40700
+ return [...missing, ...stale].slice(0, limit);
40701
+ }
40702
+ var sessionBackfillCandidateStatements = new WeakMap;
40703
+ function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, limit, excludeIds, maxInputTokens = DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS) {
40704
+ const exclusions = excludeIds && excludeIds.length > 0 ? excludeIds : [];
40705
+ const exclusionSql = exclusions.length > 0 ? `AND c.id NOT IN (${exclusions.map(() => "?").join(", ")})` : "";
40706
+ const stmt = exclusions.length > 0 ? db.prepare(`SELECT c.id AS id,
40707
+ c.session_id AS sessionId,
40708
+ c.start_message AS startMessage,
40709
+ c.end_message AS endMessage,
40710
+ c.title AS title
40711
+ FROM compartments c
40712
+ JOIN session_projects sp
40713
+ ON sp.session_id = c.session_id
40714
+ AND sp.harness = c.harness
40715
+ AND sp.project_path = ?
40716
+ WHERE c.session_id = ?
40717
+ AND c.start_message IS NOT NULL
40718
+ AND c.end_message IS NOT NULL
40719
+ ${exclusionSql}
40720
+ ORDER BY c.start_message ASC, c.id ASC`) : (() => {
40721
+ let cached2 = sessionBackfillCandidateStatements.get(db);
40722
+ if (!cached2) {
40723
+ cached2 = db.prepare(`SELECT c.id AS id,
40724
+ c.session_id AS sessionId,
40725
+ c.start_message AS startMessage,
40726
+ c.end_message AS endMessage,
40727
+ c.title AS title
40728
+ FROM compartments c
40729
+ JOIN session_projects sp
40730
+ ON sp.session_id = c.session_id
40731
+ AND sp.harness = c.harness
40732
+ AND sp.project_path = ?
40733
+ WHERE c.session_id = ?
40734
+ AND c.start_message IS NOT NULL
40735
+ AND c.end_message IS NOT NULL
40736
+ ORDER BY c.start_message ASC, c.id ASC`);
40737
+ sessionBackfillCandidateStatements.set(db, cached2);
40738
+ }
40739
+ return cached2;
40740
+ })();
40741
+ const rows = stmt.all(projectPath, sessionId, ...exclusions);
40742
+ return selectHashIncompleteChunkCandidates(db, projectPath, modelId, mapBackfillCandidateRows(rows), Math.max(1, limit), maxInputTokens);
40743
+ }
40744
+ function countUnembeddedSessionCompartments(db, projectPath, sessionId, modelId, maxInputTokens = DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS) {
40745
+ return loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, Number.MAX_SAFE_INTEGER, undefined, maxInputTokens).length;
40746
+ }
40747
+ function countSessionCompartmentEmbedCoverage(db, projectPath, sessionId, modelId, maxInputTokens = DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS) {
40748
+ const rows = db.prepare(`SELECT c.id AS id,
40670
40749
  c.session_id AS sessionId,
40671
40750
  c.start_message AS startMessage,
40672
40751
  c.end_message AS endMessage,
@@ -40679,60 +40758,15 @@ function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelI
40679
40758
  WHERE c.session_id = ?
40680
40759
  AND c.start_message IS NOT NULL
40681
40760
  AND c.end_message IS NOT NULL
40682
- AND NOT EXISTS (
40683
- SELECT 1
40684
- FROM compartment_chunk_embeddings current
40685
- WHERE current.compartment_id = c.id
40686
- AND current.project_path = ?
40687
- AND current.model_id = ?
40688
- )
40689
- ORDER BY c.start_message ASC, c.id ASC
40690
- LIMIT ?`);
40691
- sessionBackfillCandidateStatements.set(db, stmt);
40761
+ ORDER BY c.start_message ASC, c.id ASC`).all(projectPath, sessionId);
40762
+ const candidates = mapBackfillCandidateRows(rows);
40763
+ let embedded = 0;
40764
+ for (const candidate of candidates) {
40765
+ if (classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens) === null) {
40766
+ embedded += 1;
40767
+ }
40692
40768
  }
40693
- const rows = stmt.all(projectPath, sessionId, projectPath, modelId, Math.max(1, limit));
40694
- return mapBackfillCandidateRows(rows);
40695
- }
40696
- function countUnembeddedSessionCompartments(db, projectPath, sessionId, modelId) {
40697
- const row = db.prepare(`SELECT COUNT(*) AS n
40698
- FROM compartments c
40699
- JOIN session_projects sp
40700
- ON sp.session_id = c.session_id
40701
- AND sp.harness = c.harness
40702
- AND sp.project_path = ?
40703
- WHERE c.session_id = ?
40704
- AND c.start_message IS NOT NULL
40705
- AND c.end_message IS NOT NULL
40706
- AND NOT EXISTS (
40707
- SELECT 1
40708
- FROM compartment_chunk_embeddings current
40709
- WHERE current.compartment_id = c.id
40710
- AND current.project_path = ?
40711
- AND current.model_id = ?
40712
- )`).get(projectPath, sessionId, projectPath, modelId);
40713
- return typeof row?.n === "number" ? row.n : 0;
40714
- }
40715
- function countSessionCompartmentEmbedCoverage(db, projectPath, sessionId, modelId) {
40716
- const row = db.prepare(`SELECT
40717
- COUNT(*) AS total,
40718
- SUM(CASE WHEN EXISTS (
40719
- SELECT 1 FROM compartment_chunk_embeddings e
40720
- WHERE e.compartment_id = c.id
40721
- AND e.project_path = ?
40722
- AND e.model_id = ?
40723
- ) THEN 1 ELSE 0 END) AS embedded
40724
- FROM compartments c
40725
- JOIN session_projects sp
40726
- ON sp.session_id = c.session_id
40727
- AND sp.harness = c.harness
40728
- AND sp.project_path = ?
40729
- WHERE c.session_id = ?
40730
- AND c.start_message IS NOT NULL
40731
- AND c.end_message IS NOT NULL`).get(projectPath, modelId, projectPath, sessionId);
40732
- return {
40733
- total: typeof row?.total === "number" ? row.total : 0,
40734
- embedded: typeof row?.embedded === "number" ? row.embedded : 0
40735
- };
40769
+ return { embedded, total: candidates.length };
40736
40770
  }
40737
40771
 
40738
40772
  // ../plugin/src/features/magic-context/memory/cosine-similarity.ts
@@ -42105,21 +42139,28 @@ var SYNAPSE_DEFAULT_MODEL = "gte-modernbert-base-f16";
42105
42139
  var SYNAPSE_MAX_INPUT_TOKENS = 8192;
42106
42140
  var SYNAPSE_DEFAULT_QUERY_TIMEOUT_MS = 3000;
42107
42141
  var SYNAPSE_DEFAULT_BATCH_TIMEOUT_MS = 120000;
42142
+ var SYNAPSE_CERTIFICATION_REFUSAL_REASONS = new Set([
42143
+ "not_certified",
42144
+ "probe_required",
42145
+ "migration_required"
42146
+ ]);
42108
42147
 
42109
42148
  class SynapseEmbeddingError extends Error {
42110
42149
  code;
42111
42150
  retryAfterMs;
42112
42151
  permanent;
42152
+ refusalReason;
42113
42153
  constructor(code, message, options) {
42114
42154
  super(message, options?.cause === undefined ? undefined : { cause: options.cause });
42115
42155
  this.name = "SynapseEmbeddingError";
42116
42156
  this.code = code;
42117
42157
  this.permanent = options?.permanent ?? isPermanentSynapseCode(code);
42118
42158
  this.retryAfterMs = options?.retryAfterMs ?? (this.permanent ? undefined : 100);
42159
+ this.refusalReason = options?.refusalReason;
42119
42160
  }
42120
42161
  }
42121
42162
  function isPermanentSynapseCode(code) {
42122
- return code === "artifact_invalid" || code === "substitution_rejected" || code === "not_certified" || code === "probe_required" || code === "idempotency_conflict" || code === "schema_violation";
42163
+ return code === "artifact_invalid" || code === "substitution_rejected" || code === "not_certified" || code === "probe_required" || code === "idempotency_conflict" || code === "schema_violation" || code === "certification_refused" || code === "needs_reauth";
42123
42164
  }
42124
42165
  function asRecord(value) {
42125
42166
  return value !== null && typeof value === "object" ? value : null;
@@ -42145,11 +42186,21 @@ function readErrorCode(value) {
42145
42186
  }
42146
42187
  return;
42147
42188
  }
42189
+ function readCertificationRefusalReason(value) {
42190
+ if (!(value instanceof SubcCallError))
42191
+ return;
42192
+ const detail = value.detail;
42193
+ if (typeof detail === "string")
42194
+ return detail;
42195
+ const record2 = asRecord(detail);
42196
+ return typeof record2?.reason === "string" ? record2.reason : undefined;
42197
+ }
42148
42198
  function classifyError(value) {
42149
42199
  if (value instanceof SynapseEmbeddingError)
42150
42200
  return value;
42151
42201
  const code = readErrorCode(value) ?? (value instanceof Error ? value.name : "transport");
42152
42202
  const normalized = code.toLowerCase();
42203
+ const refusalReason = readCertificationRefusalReason(value);
42153
42204
  let mapped = "transport";
42154
42205
  if (normalized.includes("queue_full"))
42155
42206
  mapped = "queue_full";
@@ -42161,12 +42212,16 @@ function classifyError(value) {
42161
42212
  mapped = "artifact_invalid";
42162
42213
  else if (normalized.includes("substitution"))
42163
42214
  mapped = "substitution_rejected";
42215
+ else if (normalized === "certification_refused")
42216
+ mapped = "certification_refused";
42164
42217
  else if (normalized.includes("not_certified"))
42165
42218
  mapped = "not_certified";
42166
42219
  else if (normalized.includes("probe_required"))
42167
42220
  mapped = "probe_required";
42168
42221
  else if (normalized.includes("idempotency_conflict"))
42169
42222
  mapped = "idempotency_conflict";
42223
+ else if (normalized.includes("needs_reauth"))
42224
+ mapped = "needs_reauth";
42170
42225
  else if (normalized.includes("schema"))
42171
42226
  mapped = "schema_violation";
42172
42227
  else if (normalized.includes("module_restarted") || normalized.includes("module restarted"))
@@ -42174,9 +42229,41 @@ function classifyError(value) {
42174
42229
  const message = value instanceof Error ? value.message : String(value);
42175
42230
  return new SynapseEmbeddingError(mapped, message, {
42176
42231
  retryAfterMs: readRetryAfter(value) ?? (isPermanentSynapseCode(mapped) ? undefined : 100),
42177
- cause: value
42232
+ cause: value,
42233
+ refusalReason
42178
42234
  });
42179
42235
  }
42236
+ function embeddingFailureFor(error51) {
42237
+ const typedReason = error51.refusalReason;
42238
+ if (typedReason !== undefined && SYNAPSE_CERTIFICATION_REFUSAL_REASONS.has(typedReason) || error51.code === "not_certified" || error51.code === "probe_required") {
42239
+ return {
42240
+ class: "certification_refusal",
42241
+ reason: `SYNAPSE certification refused embedding: ${typedReason ?? error51.code}`,
42242
+ retryable: false
42243
+ };
42244
+ }
42245
+ if (error51.code === "certification_refused") {
42246
+ return {
42247
+ class: "certification_refusal",
42248
+ reason: `SYNAPSE certification refused embedding: ${typedReason ?? "unknown reason"}`,
42249
+ retryable: false
42250
+ };
42251
+ }
42252
+ if (typedReason === "needs_reauth" || typedReason === "needs_reauth_expired") {
42253
+ return {
42254
+ class: "credential_required",
42255
+ reason: "SYNAPSE requires reauthentication",
42256
+ retryable: false
42257
+ };
42258
+ }
42259
+ if (error51.code === "substitution_rejected") {
42260
+ return { class: "substitution_rejected", reason: error51.message, retryable: false };
42261
+ }
42262
+ if (error51.code === "schema_violation" || error51.code === "artifact_invalid") {
42263
+ return { class: "invalid_envelope", reason: error51.message, retryable: false };
42264
+ }
42265
+ return { class: "transport_error", reason: error51.message, retryable: !error51.permanent };
42266
+ }
42180
42267
  function wait(ms) {
42181
42268
  return new Promise((resolve5) => setTimeout(resolve5, ms));
42182
42269
  }
@@ -42296,6 +42383,7 @@ class SynapseEmbeddingProvider {
42296
42383
  initialized = false;
42297
42384
  initializing = null;
42298
42385
  permanentFailure = false;
42386
+ lastFailureReason = null;
42299
42387
  batchLimit = 16;
42300
42388
  tokenBudget = null;
42301
42389
  constructor(options) {
@@ -42370,9 +42458,11 @@ class SynapseEmbeddingProvider {
42370
42458
  this.tokenBudget = metadata.recommended_token_budget ?? this.tokenBudget;
42371
42459
  }
42372
42460
  this.initialized = true;
42461
+ this.recordSuccess();
42373
42462
  return true;
42374
42463
  } catch (error51) {
42375
42464
  const classified = classifyError(error51);
42465
+ this.recordFailure(classified);
42376
42466
  if (classified.permanent) {
42377
42467
  this.permanentFailure = true;
42378
42468
  log(`[magic-context] Synapse lane disabled: ${classified.code}: ${classified.message}`);
@@ -42399,6 +42489,7 @@ class SynapseEmbeddingProvider {
42399
42489
  if (!extracted)
42400
42490
  throw new SynapseEmbeddingError("schema_violation", "Synapse query returned no vector");
42401
42491
  this.validateResponse(extracted.metadata, extracted.vector.length);
42492
+ this.recordSuccess();
42402
42493
  return extracted.vector;
42403
42494
  } catch (error51) {
42404
42495
  this.logCallFailure(error51, "embed.query");
@@ -42462,6 +42553,7 @@ class SynapseEmbeddingProvider {
42462
42553
  const vectorArray = Float32Array.from(vector);
42463
42554
  this.validateResponse({ ...batchEnvelope, ...item }, vectorArray.length);
42464
42555
  output.set(id, vectorArray);
42556
+ this.recordSuccess();
42465
42557
  }
42466
42558
  } catch (error51) {
42467
42559
  const classified = classifyError(error51);
@@ -42484,6 +42576,9 @@ class SynapseEmbeddingProvider {
42484
42576
  isLoaded() {
42485
42577
  return this.initialized;
42486
42578
  }
42579
+ getLastFailureReason() {
42580
+ return this.lastFailureReason;
42581
+ }
42487
42582
  requestConstraints(extra) {
42488
42583
  const metadata = this.metadata;
42489
42584
  if (!metadata)
@@ -42601,11 +42696,18 @@ class SynapseEmbeddingProvider {
42601
42696
  }
42602
42697
  logCallFailure(error51, operation) {
42603
42698
  const classified = classifyError(error51);
42699
+ this.recordFailure(classified);
42604
42700
  if (classified.permanent)
42605
42701
  this.permanentFailure = true;
42606
42702
  const suffix = classified.retryAfterMs === undefined ? "" : ` retry_after_ms=${classified.retryAfterMs}`;
42607
42703
  log(`[magic-context] Synapse ${operation} failed: ${classified.code}${suffix}: ${classified.message}`);
42608
42704
  }
42705
+ recordFailure(error51) {
42706
+ this.lastFailureReason = embeddingFailureFor(error51);
42707
+ }
42708
+ recordSuccess() {
42709
+ this.lastFailureReason = null;
42710
+ }
42609
42711
  }
42610
42712
 
42611
42713
  // ../plugin/src/features/magic-context/memory/normalize-hash.ts
@@ -42659,6 +42761,32 @@ import { chmodSync as chmodSync2, mkdirSync as mkdirSync4 } from "node:fs";
42659
42761
  import { open, stat, unlink, writeFile } from "node:fs/promises";
42660
42762
  import { dirname as dirname7, join as join9 } from "node:path";
42661
42763
  import { pathToFileURL as pathToFileURL2 } from "node:url";
42764
+ var BUN_NAPI_TEARDOWN_FIX_VERSION = [1, 4, 0];
42765
+ function currentLocalEmbeddingHost() {
42766
+ const bun = globalThis.Bun;
42767
+ const bunVersion = typeof process !== "undefined" && typeof process.versions?.bun === "string" ? process.versions.bun : undefined;
42768
+ return {
42769
+ isElectron: typeof process !== "undefined" && Boolean(process.versions?.electron),
42770
+ isBun: Boolean(bun) || Boolean(bunVersion),
42771
+ bunVersion
42772
+ };
42773
+ }
42774
+ function bunHasNapiTeardownFix(version2) {
42775
+ const match = version2?.match(/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/);
42776
+ if (!match || match[4])
42777
+ return false;
42778
+ const parsed = [Number(match[1]), Number(match[2]), Number(match[3])];
42779
+ return parsed.some((part) => !Number.isSafeInteger(part)) ? false : parsed[0] > BUN_NAPI_TEARDOWN_FIX_VERSION[0] || parsed[0] === BUN_NAPI_TEARDOWN_FIX_VERSION[0] && (parsed[1] > BUN_NAPI_TEARDOWN_FIX_VERSION[1] || parsed[1] === BUN_NAPI_TEARDOWN_FIX_VERSION[1] && parsed[2] >= BUN_NAPI_TEARDOWN_FIX_VERSION[2]);
42780
+ }
42781
+ function resolveLocalEmbeddingRuntime(preference = "auto", host = currentLocalEmbeddingHost()) {
42782
+ if (preference === "native" || preference === "wasm")
42783
+ return preference;
42784
+ if (host.isElectron)
42785
+ return "electron";
42786
+ if (host.isBun && !bunHasNapiTeardownFix(host.bunVersion))
42787
+ return "wasm";
42788
+ return "native";
42789
+ }
42662
42790
  var LOCK_POLL_MS = 150;
42663
42791
  var STALE_LOCK_MS = 3 * 60000;
42664
42792
  var MAX_LOCK_WAIT_MS = 5 * 60000;
@@ -42712,20 +42840,21 @@ function startLockHeartbeat(lockPath) {
42712
42840
  }
42713
42841
  var localEmbeddingRuntimeMode = "native";
42714
42842
  var wasmRuntimeInjected = false;
42715
- var isElectronForRuntime = () => typeof process !== "undefined" && Boolean(process.versions?.electron);
42843
+ var localEmbeddingHostForRuntime = currentLocalEmbeddingHost;
42716
42844
  var importWasmOrtForRuntime = async () => {
42717
- const ortWebSpec = `onnxruntime-${"web"}`;
42718
- return await import(ortWebSpec);
42845
+ const { createRequire: createRequireFn } = await import("node:module");
42846
+ const requireFn = createRequireFn(import.meta.url);
42847
+ const ortEntry = requireFn.resolve("onnxruntime-web");
42848
+ return {
42849
+ module: await import("onnxruntime-web"),
42850
+ entryPath: ortEntry
42851
+ };
42719
42852
  };
42720
42853
  var importTransformersForRuntime = async () => {
42721
- const transformersSpec = "@huggingface/transformers";
42722
- return await import(transformersSpec);
42854
+ return await import("./transformers.node-b1wa0hgx.js");
42723
42855
  };
42724
42856
  var importTransformersWasmFallbackForRuntime = async () => {
42725
- const { createRequire: createRequireFn } = await import("node:module");
42726
- const requireFn = createRequireFn(import.meta.url);
42727
- const nodeEntry = requireFn.resolve("@huggingface/transformers");
42728
- const webEntry = pathToFileURL2(join9(dirname7(nodeEntry), "transformers.web.js")).href;
42857
+ const webEntry = new URL("./transformers-web.js", import.meta.url).href;
42729
42858
  return await import(webEntry);
42730
42859
  };
42731
42860
  var modelCacheDirForRuntime = () => join9(getMagicContextStorageDir(), "models");
@@ -42743,17 +42872,9 @@ async function injectWasmOrt() {
42743
42872
  if (wasmRuntimeInjected)
42744
42873
  return true;
42745
42874
  try {
42746
- const ortWeb = await importWasmOrtForRuntime();
42747
- try {
42748
- const { createRequire: createRequireFn } = await import("node:module");
42749
- const requireFn = createRequireFn(import.meta.url);
42750
- const mainEntry = requireFn.resolve("onnxruntime-web");
42751
- const distDir = dirname7(mainEntry);
42752
- if (ortWeb.env?.wasm) {
42753
- ortWeb.env.wasm.wasmPaths = `${pathToFileURL2(distDir).href}/`;
42754
- }
42755
- } catch (pathError) {
42756
- log("[magic-context] could not resolve local onnxruntime-web/dist, falling back to default WASM paths:", pathError instanceof Error ? pathError.message : String(pathError));
42875
+ const { module: ortWeb, entryPath } = await importWasmOrtForRuntime();
42876
+ if (ortWeb.env?.wasm) {
42877
+ ortWeb.env.wasm.wasmPaths = `${pathToFileURL2(dirname7(entryPath)).href}/`;
42757
42878
  }
42758
42879
  globalThis[Symbol.for("onnxruntime")] = ortWeb;
42759
42880
  wasmRuntimeInjected = true;
@@ -42780,10 +42901,18 @@ function disableLocalEmbeddingsAfterRuntimeFailure(detail) {
42780
42901
  localEmbeddingRuntimeMode = "disabled";
42781
42902
  logForRuntime(`[magic-context] local embeddings are disabled because both the onnxruntime-node native binding and the onnxruntime-web (WASM) fallback failed to load. Native failure: ${detail}. Run \`npx @cortexkit/magic-context@latest doctor\` for repair guidance (use \`doctor --force\` to reinstall cached plugin packages), or configure an \`openai-compatible\` embedding HTTP endpoint. Existing memories are unaffected.`);
42782
42903
  }
42783
- async function loadTransformersForLocalEmbedding() {
42904
+ async function loadTransformersForLocalEmbedding(runtimePreference) {
42784
42905
  if (localEmbeddingRuntimeMode === "disabled") {
42785
42906
  throw new Error("local embedding runtime is disabled");
42786
42907
  }
42908
+ const resolvedRuntime = resolveLocalEmbeddingRuntime(runtimePreference, localEmbeddingHostForRuntime());
42909
+ if (resolvedRuntime === "wasm") {
42910
+ if (!await ensureWasmOrtInjected()) {
42911
+ disableLocalEmbeddingsAfterRuntimeFailure("the selected WASM runtime is unavailable");
42912
+ throw new Error("onnxruntime-web failed to load");
42913
+ }
42914
+ return { module: await importTransformersForRuntime(), usesWasm: true };
42915
+ }
42787
42916
  if (localEmbeddingRuntimeMode === "wasm") {
42788
42917
  if (!await ensureWasmOrtInjected()) {
42789
42918
  disableLocalEmbeddingsAfterRuntimeFailure("the previously selected WASM runtime is unavailable");
@@ -42796,7 +42925,7 @@ async function loadTransformersForLocalEmbedding() {
42796
42925
  throw wasmError;
42797
42926
  }
42798
42927
  }
42799
- const electron = isElectronForRuntime();
42928
+ const electron = resolvedRuntime === "electron";
42800
42929
  if (electron) {
42801
42930
  const wasInjected = wasmRuntimeInjected;
42802
42931
  if (await ensureWasmOrtInjected()) {
@@ -42852,6 +42981,8 @@ function isNativeRuntimeMissingError(error51) {
42852
42981
  if (code === "ERR_DLOPEN_FAILED" && lower.includes("onnxruntime")) {
42853
42982
  return true;
42854
42983
  }
42984
+ if (lower.includes('could not load the "sharp" module'))
42985
+ return true;
42855
42986
  const mentionsNativeRuntime = lower.includes("onnxruntime-node") || lower.includes("onnxruntime_binding");
42856
42987
  if (!mentionsNativeRuntime)
42857
42988
  return false;
@@ -42904,19 +43035,22 @@ class LocalEmbeddingProvider {
42904
43035
  maxInputTokens;
42905
43036
  model;
42906
43037
  dtype;
43038
+ runtimePreference;
42907
43039
  pipeline = null;
42908
43040
  initPromise = null;
42909
43041
  inFlight = 0;
42910
43042
  disposing = false;
42911
43043
  disposePromise = null;
42912
43044
  inFlightWaiters = [];
42913
- constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL, maxInputTokens = 512, dtype = DEFAULT_LOCAL_DTYPE) {
43045
+ constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL, maxInputTokens = 512, dtype = DEFAULT_LOCAL_DTYPE, runtimePreference = "auto") {
42914
43046
  this.model = model;
42915
43047
  this.maxInputTokens = maxInputTokens;
42916
43048
  this.dtype = dtype || DEFAULT_LOCAL_DTYPE;
43049
+ this.runtimePreference = runtimePreference;
42917
43050
  this.modelId = getEmbeddingProviderIdentity({
42918
43051
  provider: "local",
42919
43052
  model,
43053
+ local_runtime: runtimePreference,
42920
43054
  ...dtype && dtype !== DEFAULT_LOCAL_DTYPE ? { local_dtype: dtype } : {}
42921
43055
  });
42922
43056
  }
@@ -42939,7 +43073,7 @@ class LocalEmbeddingProvider {
42939
43073
  if (this.disposing) {
42940
43074
  return;
42941
43075
  }
42942
- const { module: transformersModule, usesWasm } = await loadTransformersForLocalEmbedding();
43076
+ const { module: transformersModule, usesWasm } = await loadTransformersForLocalEmbedding(this.runtimePreference);
42943
43077
  const env = transformersModule.env;
42944
43078
  const LogLevel = transformersModule.LogLevel;
42945
43079
  if (LogLevel && "ERROR" in LogLevel) {
@@ -43007,7 +43141,7 @@ class LocalEmbeddingProvider {
43007
43141
  if (!localEmbeddingRuntimeIsDisabled() && isNativeRuntimeMissingError(error51)) {
43008
43142
  disableLocalEmbeddingsAfterRuntimeFailure(error51 instanceof Error ? error51.message : String(error51));
43009
43143
  } else if (!localEmbeddingRuntimeIsDisabled()) {
43010
- log("[magic-context] embedding model failed to load:", error51);
43144
+ logForRuntime("[magic-context] embedding model failed to load:", error51);
43011
43145
  }
43012
43146
  this.pipeline = null;
43013
43147
  } finally {
@@ -44071,6 +44205,13 @@ function markProjectLoadUntrusted(projectIdentity) {
44071
44205
  untrustedLoadProjects.add(projectIdentity);
44072
44206
  }
44073
44207
  var testProviderFactory = null;
44208
+
44209
+ class TestProviderFactoryRequiredError extends Error {
44210
+ constructor() {
44211
+ super("test constructed a network-capable embedding provider without a test factory — install _setTestProviderFactoryForProject or set embedding.provider off in the fixture");
44212
+ this.name = "TestProviderFactoryRequiredError";
44213
+ }
44214
+ }
44074
44215
  function synapseConfigFields(config2) {
44075
44216
  const raw = config2;
44076
44217
  return {
@@ -44105,6 +44246,7 @@ function resolveEmbeddingConfig(config2) {
44105
44246
  return {
44106
44247
  provider: "local",
44107
44248
  model: config2?.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
44249
+ local_runtime: config2?.local_runtime ?? "auto",
44108
44250
  ...config2?.max_input_tokens ? {
44109
44251
  max_input_tokens: normalizeCompartmentChunkMaxInputTokens(config2.max_input_tokens)
44110
44252
  } : {},
@@ -44149,11 +44291,14 @@ function resolveEmbeddingConfig(config2) {
44149
44291
  throw new Error("Unknown embedding provider");
44150
44292
  }
44151
44293
  function createProvider(config2, context) {
44294
+ if (config2.provider === "off") {
44295
+ return null;
44296
+ }
44152
44297
  if (testProviderFactory) {
44153
44298
  return testProviderFactory(config2);
44154
44299
  }
44155
- if (config2.provider === "off") {
44156
- return null;
44300
+ if (process.env.MAGIC_CONTEXT_TEST_DATA_DIR?.trim()) {
44301
+ throw new TestProviderFactoryRequiredError;
44157
44302
  }
44158
44303
  if (config2.provider === "openai-compatible") {
44159
44304
  return new OpenAICompatibleEmbeddingProvider({
@@ -44167,7 +44312,7 @@ function createProvider(config2, context) {
44167
44312
  });
44168
44313
  }
44169
44314
  if (config2.provider === "local") {
44170
- return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens, config2.local_dtype);
44315
+ return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens, config2.local_dtype, config2.local_runtime);
44171
44316
  }
44172
44317
  if (config2.provider === "synapse") {
44173
44318
  const synapse = config2;
@@ -44578,15 +44723,25 @@ function shadowBackfillMissingBase(scope, primaryModelId, shadowModelId, project
44578
44723
  };
44579
44724
  }
44580
44725
  return {
44581
- sql: `SELECT DISTINCT cp.compartment_id AS id
44726
+ sql: `SELECT cp.compartment_id AS id,
44727
+ MIN(CASE WHEN NOT EXISTS (
44728
+ SELECT 1 FROM compartment_chunk_embeddings same_key
44729
+ WHERE same_key.compartment_id = cp.compartment_id
44730
+ AND same_key.window_index = cp.window_index
44731
+ AND same_key.model_id = ?
44732
+ ) THEN 0 ELSE 1 END) AS defect_order
44582
44733
  FROM compartment_chunk_embeddings cp
44583
44734
  WHERE cp.project_path = ? AND cp.model_id = ?
44584
44735
  AND NOT EXISTS (
44585
- SELECT 1 FROM compartment_chunk_embeddings cs
44586
- WHERE cs.compartment_id = cp.compartment_id AND cs.model_id = ?
44587
- )`,
44588
- params: [projectIdentity, primaryModelId, shadowModelId],
44589
- orderBy: " ORDER BY cp.compartment_id"
44736
+ SELECT 1 FROM compartment_chunk_embeddings current
44737
+ WHERE current.compartment_id = cp.compartment_id
44738
+ AND current.window_index = cp.window_index
44739
+ AND current.chunk_hash = cp.chunk_hash
44740
+ AND current.model_id = ?
44741
+ )
44742
+ GROUP BY cp.compartment_id`,
44743
+ params: [shadowModelId, projectIdentity, primaryModelId, shadowModelId],
44744
+ orderBy: " ORDER BY defect_order ASC, cp.compartment_id ASC"
44590
44745
  };
44591
44746
  }
44592
44747
  function shadowBackfillMissingIds(db, projectIdentity, scope, primaryModelId, shadowModelId, limit) {
@@ -45180,7 +45335,8 @@ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, opt
45180
45335
  return { status: "disabled", embedded: 0, total: 0 };
45181
45336
  }
45182
45337
  recordSessionProjectIdentity(db, sessionId, projectIdentity);
45183
- const total = countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot.chunkModelId);
45338
+ const maxInputTokens = getProjectEmbeddingMaxInputTokens(projectIdentity);
45339
+ const total = countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot.chunkModelId, maxInputTokens);
45184
45340
  if (total === 0)
45185
45341
  return { status: "nothing", embedded: 0, total: 0 };
45186
45342
  const holderId = `session-embed-${randomUUID2()}`;
@@ -45218,7 +45374,7 @@ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, opt
45218
45374
  aborted2 = true;
45219
45375
  break;
45220
45376
  }
45221
- const candidates = loadUnembeddedSessionChunkCandidates(db, projectIdentity, sessionId, snapshot.chunkModelId, batchSize, [...skipIds, ...failedIds]);
45377
+ const candidates = loadUnembeddedSessionChunkCandidates(db, projectIdentity, sessionId, snapshot.chunkModelId, batchSize, [...skipIds, ...failedIds], maxInputTokens);
45222
45378
  if (candidates.length === 0)
45223
45379
  break;
45224
45380
  const {
@@ -45263,7 +45419,7 @@ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, opt
45263
45419
  if (aborted2)
45264
45420
  return { status: "aborted", embedded, total, failed: failedIds.length };
45265
45421
  if (providerDown || failedIds.length > 0) {
45266
- const remaining = Math.max(0, countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot.chunkModelId) - skipIds.length);
45422
+ const remaining = Math.max(0, countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot.chunkModelId, maxInputTokens) - skipIds.length);
45267
45423
  if (remaining > 0) {
45268
45424
  return {
45269
45425
  status: "stalled",
@@ -45289,7 +45445,7 @@ function getEmbeddingCoverageStatus(db, projectIdentity, sessionId) {
45289
45445
  commits: { embedded: 0, total: 0, gitEnabled: false }
45290
45446
  };
45291
45447
  }
45292
- const session = countSessionCompartmentEmbedCoverage(db, projectIdentity, sessionId, snapshot.chunkModelId);
45448
+ const session = countSessionCompartmentEmbedCoverage(db, projectIdentity, sessionId, snapshot.chunkModelId, getProjectEmbeddingMaxInputTokens(projectIdentity));
45293
45449
  const memories = getMemoryEmbedCoverage(db, projectIdentity, snapshot.modelId);
45294
45450
  const gitEnabled = snapshot.gitCommitEnabled;
45295
45451
  const commits = gitEnabled ? {
@@ -45310,7 +45466,8 @@ function getEmbeddingCoverageStatus(db, projectIdentity, sessionId) {
45310
45466
  // ../plugin/src/features/magic-context/memory/embedding.ts
45311
45467
  var DEFAULT_EMBEDDING_CONFIG = {
45312
45468
  provider: "local",
45313
- model: DEFAULT_LOCAL_EMBEDDING_MODEL
45469
+ model: DEFAULT_LOCAL_EMBEDDING_MODEL,
45470
+ local_runtime: "auto"
45314
45471
  };
45315
45472
  var embeddingConfig = DEFAULT_EMBEDDING_CONFIG;
45316
45473
  var provider = null;
@@ -45330,7 +45487,7 @@ function createProvider2(config2) {
45330
45487
  });
45331
45488
  }
45332
45489
  if (config2.provider === "local") {
45333
- return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens, config2.local_dtype);
45490
+ return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens, config2.local_dtype, config2.local_runtime);
45334
45491
  }
45335
45492
  if (config2.provider === "synapse") {
45336
45493
  const synapse = config2;
@@ -50752,6 +50909,7 @@ var memoryImportanceColumnCache = new WeakMap;
50752
50909
  var memoryScopeColumnCache = new WeakMap;
50753
50910
  var memoryShareableColumnCache = new WeakMap;
50754
50911
  var memoryClassifiedAtColumnCache = new WeakMap;
50912
+ var memoryVerificationsTableCache = new WeakMap;
50755
50913
  function hasMemoryImportanceColumn(db) {
50756
50914
  const cached2 = memoryImportanceColumnCache.get(db);
50757
50915
  if (cached2 !== undefined)
@@ -50788,6 +50946,14 @@ function hasMemoryClassifiedAtColumn(db) {
50788
50946
  memoryClassifiedAtColumnCache.set(db, hasColumn);
50789
50947
  return hasColumn;
50790
50948
  }
50949
+ function hasMemoryVerificationsTable(db) {
50950
+ const cached2 = memoryVerificationsTableCache.get(db);
50951
+ if (cached2 !== undefined)
50952
+ return cached2;
50953
+ const hasTable = Boolean(db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'memory_verifications'").get());
50954
+ memoryVerificationsTableCache.set(db, hasTable);
50955
+ return hasTable;
50956
+ }
50791
50957
  function getUnclassifiedMemoryIds(db, memoryIds) {
50792
50958
  if (!hasMemoryClassifiedAtColumn(db))
50793
50959
  return [...memoryIds];
@@ -51280,6 +51446,9 @@ function updateMemoryContent(db, id, content, normalizedHash) {
51280
51446
  const rejectionReset = hasMuralCueRejectionCountColumn(db) ? ", mural_cue_rejection_count = 0" : "";
51281
51447
  db.prepare(`UPDATE memories SET mural_cue = NULL, mural_cue_hash = NULL, mural_cue_at = NULL${rejectionReset} WHERE id = ?`).run(id);
51282
51448
  }
51449
+ if (hasMemoryVerificationsTable(db)) {
51450
+ db.prepare("DELETE FROM memory_verifications WHERE memory_id = ?").run(id);
51451
+ }
51283
51452
  let stmt = deleteEmbeddingOnContentUpdateStatements.get(db);
51284
51453
  if (!stmt) {
51285
51454
  stmt = db.prepare("DELETE FROM memory_embeddings WHERE memory_id = ?");
@@ -51550,13 +51719,13 @@ function placeholders3(values) {
51550
51719
  function uniqueSortedFiles(files) {
51551
51720
  return Array.from(new Set(files.filter((file2) => file2 !== MEMORY_VERIFICATION_SENTINEL))).sort();
51552
51721
  }
51553
- function recordMemoryMapping(db, memoryId, normalizedFiles, now) {
51722
+ function recordMemoryMapping(db, memoryId, normalizedFiles, now, mappingOrigin = "mapper") {
51554
51723
  const realFiles = uniqueSortedFiles(normalizedFiles);
51555
51724
  const filesToWrite = realFiles.length > 0 ? realFiles : [MEMORY_VERIFICATION_SENTINEL];
51556
51725
  db.prepare("DELETE FROM memory_verifications WHERE memory_id = ?").run(memoryId);
51557
- const insert = db.prepare("INSERT INTO memory_verifications (memory_id, file_path, verified_at, mapped_at) VALUES (?, ?, 0, ?)");
51726
+ const insert = db.prepare("INSERT INTO memory_verifications (memory_id, file_path, verified_at, mapped_at, mapping_origin) VALUES (?, ?, 0, ?, ?)");
51558
51727
  for (const file2 of filesToWrite) {
51559
- insert.run(memoryId, file2, now);
51728
+ insert.run(memoryId, file2, now, mappingOrigin);
51560
51729
  }
51561
51730
  return filesToWrite.length;
51562
51731
  }
@@ -51564,7 +51733,7 @@ function recordMemoryVerifications(db, memoryId, normalizedFiles, now) {
51564
51733
  const realFiles = uniqueSortedFiles(normalizedFiles);
51565
51734
  const filesToWrite = realFiles.length > 0 ? realFiles : [MEMORY_VERIFICATION_SENTINEL];
51566
51735
  db.prepare("DELETE FROM memory_verifications WHERE memory_id = ?").run(memoryId);
51567
- const insert = db.prepare("INSERT INTO memory_verifications (memory_id, file_path, verified_at, mapped_at) VALUES (?, ?, ?, ?)");
51736
+ const insert = db.prepare("INSERT INTO memory_verifications (memory_id, file_path, verified_at, mapped_at, mapping_origin) VALUES (?, ?, ?, ?, 'mapper')");
51568
51737
  for (const file2 of filesToWrite) {
51569
51738
  insert.run(memoryId, file2, now, now);
51570
51739
  }
@@ -51586,7 +51755,7 @@ function getMemoryVerifications(db, memoryIds) {
51586
51755
  const result = new Map;
51587
51756
  if (ids.length === 0)
51588
51757
  return result;
51589
- const rows = db.prepare(`SELECT memory_id, file_path, verified_at, mapped_at
51758
+ const rows = db.prepare(`SELECT memory_id, file_path, verified_at, mapped_at, mapping_origin
51590
51759
  FROM memory_verifications
51591
51760
  WHERE memory_id IN (${placeholders3(ids)})
51592
51761
  ORDER BY memory_id, file_path`).all(...ids);
@@ -51594,6 +51763,7 @@ function getMemoryVerifications(db, memoryIds) {
51594
51763
  const existing = result.get(row.memory_id) ?? {
51595
51764
  files: [],
51596
51765
  hasSentinel: false,
51766
+ mappingOrigin: "mapper",
51597
51767
  verifiedAt: 0,
51598
51768
  mappedAt: 0
51599
51769
  };
@@ -51602,6 +51772,9 @@ function getMemoryVerifications(db, memoryIds) {
51602
51772
  } else if (!existing.files.includes(row.file_path)) {
51603
51773
  existing.files.push(row.file_path);
51604
51774
  }
51775
+ if (row.mapping_origin === "host_rejected_fallback") {
51776
+ existing.mappingOrigin = "host_rejected_fallback";
51777
+ }
51605
51778
  existing.verifiedAt = Math.max(existing.verifiedAt, row.verified_at);
51606
51779
  existing.mappedAt = Math.max(existing.mappedAt, row.mapped_at ?? 0);
51607
51780
  result.set(row.memory_id, existing);
@@ -52279,7 +52452,7 @@ import { isAbsolute as isAbsolute5 } from "node:path";
52279
52452
 
52280
52453
  // ../retina-local-fs/src/path-fence.ts
52281
52454
  import { lstat, readlink, realpath } from "node:fs/promises";
52282
- import { homedir as homedir7 } from "node:os";
52455
+ import { homedir as homedir6 } from "node:os";
52283
52456
  import { basename as basename4, dirname as dirname8, isAbsolute as isAbsolute4, join as join10, relative, resolve as resolve5, sep } from "node:path";
52284
52457
 
52285
52458
  // ../retina-local-fs/src/errors.ts
@@ -52304,7 +52477,7 @@ async function resolveAndFenceProviderPath(configuredPath, options) {
52304
52477
  return canonical;
52305
52478
  }
52306
52479
  async function resolveFenceRoots(options) {
52307
- const configuredHomePath = resolve5(options.homeDirectory ?? process.env.HOME ?? homedir7());
52480
+ const configuredHomePath = resolve5(options.homeDirectory ?? process.env.HOME ?? homedir6());
52308
52481
  let home;
52309
52482
  try {
52310
52483
  home = await realpath(configuredHomePath);
@@ -53795,8 +53968,16 @@ var GIT_COMMIT_SOURCE_BOOST = 1.2;
53795
53968
  var PRIMER_SOURCE_BOOST = 1.25;
53796
53969
  var messageSearchStatements = new WeakMap;
53797
53970
  var messageSearchStatementsWithCutoff = new WeakMap;
53971
+ var messageSearchDiagnosticStatements = new WeakMap;
53798
53972
  var batchedMessageSearchStatements = new WeakMap;
53799
53973
  var batchedFtsCountStatements = new WeakMap;
53974
+ function createUnifiedSearchDiagnostics() {
53975
+ return {
53976
+ suppressedVisibleMemoryIds: [],
53977
+ suppressedLiveMessageMatches: 0,
53978
+ gitCommitUnavailable: null
53979
+ };
53980
+ }
53800
53981
  function normalizeLimit2(limit) {
53801
53982
  if (typeof limit !== "number" || !Number.isFinite(limit)) {
53802
53983
  return DEFAULT_UNIFIED_SEARCH_LIMIT;
@@ -53887,6 +54068,51 @@ function getMessageSearchStatementWithCutoff(db) {
53887
54068
  }
53888
54069
  return stmt;
53889
54070
  }
54071
+ function getMessageSearchDiagnosticStatement(db) {
54072
+ let stmt = messageSearchDiagnosticStatements.get(db);
54073
+ if (!stmt) {
54074
+ stmt = db.prepare(`
54075
+ WITH matches AS MATERIALIZED (
54076
+ SELECT
54077
+ message_ordinal AS messageOrdinal,
54078
+ message_id AS messageId,
54079
+ role,
54080
+ content,
54081
+ CAST(message_ordinal AS INTEGER) AS ordinalValue,
54082
+ bm25(message_history_fts) AS ftsRank
54083
+ FROM message_history_fts
54084
+ WHERE session_id = ? AND message_history_fts MATCH ?
54085
+ ),
54086
+ eligible AS (
54087
+ SELECT * FROM matches
54088
+ WHERE ordinalValue <= ?
54089
+ ORDER BY ftsRank, ordinalValue ASC
54090
+ LIMIT ?
54091
+ ),
54092
+ summary AS (
54093
+ SELECT COUNT(*) AS suppressedCount
54094
+ FROM matches
54095
+ WHERE ordinalValue > ?
54096
+ )
54097
+ SELECT
54098
+ eligible.messageOrdinal,
54099
+ eligible.messageId,
54100
+ eligible.role,
54101
+ eligible.content,
54102
+ eligible.ftsRank,
54103
+ summary.suppressedCount,
54104
+ 0 AS summaryOnly
54105
+ FROM eligible CROSS JOIN summary
54106
+ UNION ALL
54107
+ SELECT NULL, NULL, NULL, NULL, NULL, summary.suppressedCount, 1
54108
+ FROM summary
54109
+ WHERE NOT EXISTS (SELECT 1 FROM eligible)
54110
+ ORDER BY summaryOnly ASC, ftsRank ASC, messageOrdinal ASC
54111
+ `);
54112
+ messageSearchDiagnosticStatements.set(db, stmt);
54113
+ }
54114
+ return stmt;
54115
+ }
53890
54116
  function getBatchedFtsCountStatement(db, queryCount, cutoff) {
53891
54117
  let statements = batchedFtsCountStatements.get(db);
53892
54118
  if (!statements) {
@@ -54027,10 +54253,8 @@ function mergeMemoryResults(args) {
54027
54253
  const memoryById = new Map(args.memories.map((memory2) => [memory2.id, memory2]));
54028
54254
  const candidateIds = new Set([...args.semanticScores.keys(), ...args.ftsScores.keys()]);
54029
54255
  const results = [];
54256
+ const suppressedVisibleIds = [];
54030
54257
  for (const id of candidateIds) {
54031
- if (args.visibleMemoryIds?.has(id)) {
54032
- continue;
54033
- }
54034
54258
  const memory2 = memoryById.get(id);
54035
54259
  if (!memory2) {
54036
54260
  continue;
@@ -54052,6 +54276,10 @@ function mergeMemoryResults(args) {
54052
54276
  if (score <= 0) {
54053
54277
  continue;
54054
54278
  }
54279
+ if (args.visibleMemoryIds?.has(id)) {
54280
+ suppressedVisibleIds.push(id);
54281
+ continue;
54282
+ }
54055
54283
  results.push({
54056
54284
  source: "memory",
54057
54285
  content: previewText(memory2.content),
@@ -54062,20 +54290,23 @@ function mergeMemoryResults(args) {
54062
54290
  sourceName: args.sourceNameByMemoryId?.get(memory2.id)
54063
54291
  });
54064
54292
  }
54065
- return results.sort((left, right) => {
54066
- if (right.score !== left.score) {
54067
- return right.score - left.score;
54068
- }
54069
- return left.memoryId - right.memoryId;
54070
- }).slice(0, args.limit);
54293
+ return {
54294
+ results: results.sort((left, right) => {
54295
+ if (right.score !== left.score) {
54296
+ return right.score - left.score;
54297
+ }
54298
+ return left.memoryId - right.memoryId;
54299
+ }).slice(0, args.limit),
54300
+ suppressedVisibleIds: suppressedVisibleIds.sort((left, right) => left - right)
54301
+ };
54071
54302
  }
54072
54303
  async function searchMemories(args) {
54073
54304
  if (!args.memoryEnabled) {
54074
- return [];
54305
+ return { results: [], suppressedVisibleIds: [] };
54075
54306
  }
54076
54307
  const memories = args.workspace?.isWorkspaced ? getMemoriesByProjects(args.db, args.workspace.expandedIdentities, ["active", "permanent"], Date.now(), args.workspace.ownIdentities, args.workspace.shareCategories) : getMemoriesByProject(args.db, args.projectPath);
54077
54308
  if (memories.length === 0) {
54078
- return [];
54309
+ return { results: [], suppressedVisibleIds: [] };
54079
54310
  }
54080
54311
  const ftsMatches = getFtsMatches({
54081
54312
  db: args.db,
@@ -54152,6 +54383,21 @@ function runMessageFtsQuery(db, sessionId, ftsQuery, fetchLimit, cutoff) {
54152
54383
  }
54153
54384
  return result;
54154
54385
  }
54386
+ function runMessageFtsQueryWithDiagnostics(args) {
54387
+ if (args.ftsQuery.length === 0)
54388
+ return { rows: [], suppressedCount: 0 };
54389
+ const rawRows = getMessageSearchDiagnosticStatement(args.db).all(args.sessionId, args.ftsQuery, args.cutoff, args.fetchLimit, args.cutoff).map((row) => row);
54390
+ const suppressedCount = rawRows[0]?.suppressedCount ?? 0;
54391
+ const rows = [];
54392
+ for (const row of rawRows) {
54393
+ if (row.summaryOnly === 1)
54394
+ continue;
54395
+ const normalized = normalizeMessageSearchRow(row, args.cutoff);
54396
+ if (normalized)
54397
+ rows.push(normalized);
54398
+ }
54399
+ return { rows, suppressedCount };
54400
+ }
54155
54401
  function getBatchedMessageSearchStatement(db, queryCount, cutoff) {
54156
54402
  let statements = batchedMessageSearchStatements.get(db);
54157
54403
  if (!statements) {
@@ -54218,7 +54464,20 @@ function searchMessages(args) {
54218
54464
  const baseQuery = sanitizeFtsQuery(args.query.trim());
54219
54465
  const probes = args.probes ?? [];
54220
54466
  if (probes.length === 0) {
54221
- const filtered = runMessageFtsQuery(args.db, args.sessionId, baseQuery, fetchLimit, cutoff).slice(0, args.limit);
54467
+ const outcome = args.diagnostics && cutoff !== null ? runMessageFtsQueryWithDiagnostics({
54468
+ db: args.db,
54469
+ sessionId: args.sessionId,
54470
+ ftsQuery: baseQuery,
54471
+ fetchLimit,
54472
+ cutoff
54473
+ }) : {
54474
+ rows: runMessageFtsQuery(args.db, args.sessionId, baseQuery, fetchLimit, cutoff),
54475
+ suppressedCount: 0
54476
+ };
54477
+ if (args.diagnostics) {
54478
+ args.diagnostics.suppressedLiveMessageMatches = outcome.suppressedCount;
54479
+ }
54480
+ const filtered = outcome.rows.slice(0, args.limit);
54222
54481
  return filtered.map((row, rank) => ({
54223
54482
  source: "message",
54224
54483
  content: previewText(row.content),
@@ -54231,8 +54490,19 @@ function searchMessages(args) {
54231
54490
  const sanitizedProbes = probes.map((probe) => ({ probe, query: sanitizeFtsQuery(probe) })).filter((entry) => entry.query.length > 0);
54232
54491
  const corpusSize = getIndexedMessageCorpusSize(args.db, args.sessionId, cutoff);
54233
54492
  const probeCounts = countSessionFtsMatchesBatch(args.db, args.sessionId, sanitizedProbes.map((entry) => entry.query), cutoff);
54493
+ const collectBaseDiagnostics = args.diagnostics !== undefined && cutoff !== null;
54494
+ const baseOutcome = collectBaseDiagnostics && baseQuery.length > 0 ? runMessageFtsQueryWithDiagnostics({
54495
+ db: args.db,
54496
+ sessionId: args.sessionId,
54497
+ ftsQuery: baseQuery,
54498
+ fetchLimit,
54499
+ cutoff
54500
+ }) : null;
54501
+ if (args.diagnostics) {
54502
+ args.diagnostics.suppressedLiveMessageMatches = baseOutcome?.suppressedCount ?? 0;
54503
+ }
54234
54504
  const searchQueries = [
54235
- ...baseQuery.length > 0 ? [baseQuery] : [],
54505
+ ...!collectBaseDiagnostics && baseQuery.length > 0 ? [baseQuery] : [],
54236
54506
  ...sanitizedProbes.map((entry) => entry.query)
54237
54507
  ];
54238
54508
  const rowsByQuery = runMessageFtsQueriesBatch(args.db, args.sessionId, searchQueries, fetchLimit, cutoff);
@@ -54240,10 +54510,11 @@ function searchMessages(args) {
54240
54510
  let queryIndex = 0;
54241
54511
  if (baseQuery.length > 0) {
54242
54512
  queryLists.push({
54243
- rows: rowsByQuery[queryIndex] ?? [],
54513
+ rows: baseOutcome?.rows ?? rowsByQuery[queryIndex] ?? [],
54244
54514
  weight: 1
54245
54515
  });
54246
- queryIndex += 1;
54516
+ if (!collectBaseDiagnostics)
54517
+ queryIndex += 1;
54247
54518
  }
54248
54519
  const probeWeights = new Map;
54249
54520
  sanitizedProbes.forEach((entry, probeIndex) => {
@@ -54630,6 +54901,9 @@ function memoriesToIdLookupResults(args) {
54630
54901
  }));
54631
54902
  }
54632
54903
  function resolveMemoriesByIdsForSearch(args) {
54904
+ if (args.diagnostics) {
54905
+ args.diagnostics.suppressedVisibleMemoryIds = [];
54906
+ }
54633
54907
  if (args.ids.length === 0) {
54634
54908
  return null;
54635
54909
  }
@@ -54640,16 +54914,22 @@ function resolveMemoriesByIdsForSearch(args) {
54640
54914
  }
54641
54915
  const memoriesById = new Map(fetched.map((memory2) => [memory2.id, memory2]));
54642
54916
  const ordered = [];
54917
+ const suppressedVisibleIds = new Set;
54643
54918
  for (const id of args.ids) {
54644
54919
  const memory2 = memoriesById.get(id);
54645
54920
  if (!memory2)
54646
54921
  continue;
54647
- if (args.visibleMemoryIds?.has(id))
54922
+ if (args.visibleMemoryIds?.has(id)) {
54923
+ suppressedVisibleIds.add(id);
54648
54924
  continue;
54925
+ }
54649
54926
  ordered.push(memory2);
54650
54927
  if (ordered.length >= args.limit)
54651
54928
  break;
54652
54929
  }
54930
+ if (args.diagnostics) {
54931
+ args.diagnostics.suppressedVisibleMemoryIds = [...suppressedVisibleIds].sort((left, right) => left - right);
54932
+ }
54653
54933
  if (ordered.length === 0) {
54654
54934
  return null;
54655
54935
  }
@@ -54671,6 +54951,11 @@ async function unifiedSearch(db, sessionId, projectPath, query, options = {}) {
54671
54951
  }
54672
54952
  const limit = normalizeLimit2(options.limit);
54673
54953
  const tierLimit = Math.max(limit * 3, DEFAULT_UNIFIED_SEARCH_LIMIT);
54954
+ if (options.diagnostics) {
54955
+ options.diagnostics.suppressedVisibleMemoryIds = [];
54956
+ options.diagnostics.suppressedLiveMessageMatches = 0;
54957
+ options.diagnostics.gitCommitUnavailable = null;
54958
+ }
54674
54959
  const embeddingEnabled = options.embeddingEnabled ?? true;
54675
54960
  const embedQuery = options.embedQuery ?? embedText;
54676
54961
  const isEmbeddingRuntimeEnabled = options.isEmbeddingRuntimeEnabled ?? isEmbeddingEnabled;
@@ -54680,6 +54965,9 @@ async function unifiedSearch(db, sessionId, projectPath, query, options = {}) {
54680
54965
  const runMemory = activeSources.has("memory") && memoryFeatureEnabled;
54681
54966
  const runMessages = activeSources.has("message");
54682
54967
  const runGitCommits = activeSources.has("git_commit") && gitCommitsEnabled;
54968
+ if (options.diagnostics && activeSources.has("git_commit") && options.gitRepositoryAvailable === false) {
54969
+ options.diagnostics.gitCommitUnavailable = "no_git_repository";
54970
+ }
54683
54971
  const runPrimers = activeSources.has("primer") && memoryFeatureEnabled;
54684
54972
  const runNotes = activeSources.has("note");
54685
54973
  const runCompartmentChunks = runMessages && memoryFeatureEnabled && embeddingEnabled;
@@ -54696,7 +54984,8 @@ async function unifiedSearch(db, sessionId, projectPath, query, options = {}) {
54696
54984
  query: trimmedQuery,
54697
54985
  limit: tierLimit,
54698
54986
  maxOrdinal: options.maxMessageOrdinal,
54699
- probes: messageProbes
54987
+ probes: messageProbes,
54988
+ diagnostics: options.diagnostics
54700
54989
  }) : [];
54701
54990
  const capturedQuery = await queryEmbeddingPromise;
54702
54991
  const embeddingSnapshot = getProjectEmbeddingSnapshot(projectPath);
@@ -54720,7 +55009,7 @@ async function unifiedSearch(db, sessionId, projectPath, query, options = {}) {
54720
55009
  compartments: compartmentResults,
54721
55010
  limit: tierLimit
54722
55011
  });
54723
- const [memoryResults, gitCommitResults, primerResults, noteResults] = await Promise.all([
55012
+ const [memoryOutcome, gitCommitResults, primerResults, noteResults] = await Promise.all([
54724
55013
  runMemory ? searchMemories({
54725
55014
  db,
54726
55015
  projectPath,
@@ -54731,7 +55020,10 @@ async function unifiedSearch(db, sessionId, projectPath, query, options = {}) {
54731
55020
  queryModelId: embeddingModelId && embeddingModelId !== "off" ? embeddingModelId : null,
54732
55021
  workspace,
54733
55022
  visibleMemoryIds: options.visibleMemoryIds
54734
- }) : Promise.resolve([]),
55023
+ }) : Promise.resolve({
55024
+ results: [],
55025
+ suppressedVisibleIds: []
55026
+ }),
54735
55027
  runGitCommits ? Promise.resolve(searchGitCommits({
54736
55028
  db,
54737
55029
  projectPath,
@@ -54757,8 +55049,11 @@ async function unifiedSearch(db, sessionId, projectPath, query, options = {}) {
54757
55049
  probes: messageProbes
54758
55050
  })) : Promise.resolve([])
54759
55051
  ]);
55052
+ if (options.diagnostics) {
55053
+ options.diagnostics.suppressedVisibleMemoryIds = memoryOutcome.suppressedVisibleIds;
55054
+ }
54760
55055
  const results = [
54761
- ...memoryResults,
55056
+ ...memoryOutcome.results,
54762
55057
  ...primerResults,
54763
55058
  ...messageLikeResults,
54764
55059
  ...gitCommitResults,
@@ -54796,6 +55091,123 @@ async function unifiedSearch(db, sessionId, projectPath, query, options = {}) {
54796
55091
  }
54797
55092
  return results;
54798
55093
  }
55094
+ var SEARCH_NOTE_EXPAND_HINT = "Use ctx_expand(start=N-10, end=N) around any note @msg anchor above to read the surrounding conversation context.";
55095
+ function formatSearchAge(timestampMs) {
55096
+ const ageMs = Date.now() - timestampMs;
55097
+ if (ageMs < 0)
55098
+ return "future";
55099
+ const days = Math.floor(ageMs / (24 * 60 * 60 * 1000));
55100
+ if (days <= 0)
55101
+ return "today";
55102
+ if (days === 1)
55103
+ return "1d ago";
55104
+ if (days < 30)
55105
+ return `${days}d ago`;
55106
+ const months = Math.floor(days / 30);
55107
+ if (months === 1)
55108
+ return "1mo ago";
55109
+ if (months < 12)
55110
+ return `${months}mo ago`;
55111
+ const years = Math.floor(days / 365);
55112
+ return years === 1 ? "1y ago" : `${years}y ago`;
55113
+ }
55114
+ function formatUnifiedSearchResult(result, index, currentSessionId) {
55115
+ if (result.source === "memory") {
55116
+ const source = result.sourceName ? ` source=${result.sourceName}` : "";
55117
+ return [
55118
+ `[${index}] [memory] score=${result.score.toFixed(2)} id=${result.memoryId} category=${result.category}${source} match=${result.matchType}`,
55119
+ result.content
55120
+ ].join(`
55121
+ `);
55122
+ }
55123
+ if (result.source === "git_commit") {
55124
+ return [
55125
+ `[${index}] [git_commit] score=${result.score.toFixed(2)} sha=${result.shortSha} ${formatSearchAge(result.committedAtMs)} match=${result.matchType}`,
55126
+ result.content
55127
+ ].join(`
55128
+ `);
55129
+ }
55130
+ if (result.source === "primer") {
55131
+ return [
55132
+ `[${index}] [primer] score=${result.score.toFixed(2)} id=${result.primerId} support=${result.support} match=${result.matchType}`,
55133
+ result.content
55134
+ ].join(`
55135
+ `);
55136
+ }
55137
+ if (result.source === "note") {
55138
+ const anchor = result.anchorOrdinal !== null && result.sourceSessionId === currentSessionId ? ` @msg ${result.anchorOrdinal}` : "";
55139
+ return [
55140
+ `[${index}] [note] score=${result.score.toFixed(2)} id=#${result.noteId} status=${result.status} ${formatSearchAge(result.createdAt)}${anchor}`,
55141
+ result.content
55142
+ ].join(`
55143
+ `);
55144
+ }
55145
+ if (result.source === "compartment") {
55146
+ return [
55147
+ `[${index}] [message] score=${result.score.toFixed(2)} compartment_id=${result.compartmentId} range=${result.startOrdinal}-${result.endOrdinal} match=${result.matchType} title=${result.title}`,
55148
+ result.snippet ? `Snippet: ${result.snippet}` : result.content
55149
+ ].join(`
55150
+ `);
55151
+ }
55152
+ const expandStart = Math.max(1, result.messageOrdinal - 3);
55153
+ const expandEnd = result.messageOrdinal + 3;
55154
+ return [
55155
+ `[${index}] [message] score=${result.score.toFixed(2)} ordinal=${result.messageOrdinal} range=${expandStart}-${expandEnd} role=${result.role}`,
55156
+ result.content
55157
+ ].join(`
55158
+ `);
55159
+ }
55160
+ function formatSearchDiagnosticLines(results, diagnostics) {
55161
+ if (!diagnostics)
55162
+ return [];
55163
+ const lines = [];
55164
+ const visibleIds = [...diagnostics.suppressedVisibleMemoryIds].sort((left, right) => left - right);
55165
+ if (visibleIds.length > 0) {
55166
+ const count = visibleIds.length;
55167
+ const noun = count === 1 ? "match" : "matches";
55168
+ const ids = visibleIds.join(", ");
55169
+ if (results.some((result) => result.source === "memory")) {
55170
+ lines.push(`Memories: ${count} additional ${noun} suppressed because ${count === 1 ? "it is" : "they are"} already visible in your project-memory block (ids ${ids}).`);
55171
+ } else {
55172
+ lines.push(`Memories: ${count} ${noun} found, all already visible in your project-memory block (ids ${ids}).`);
55173
+ }
55174
+ }
55175
+ if (diagnostics.suppressedLiveMessageMatches > 0) {
55176
+ const count = diagnostics.suppressedLiveMessageMatches;
55177
+ lines.push(`Message history: ${count} raw-message ${count === 1 ? "match is" : "matches are"} newer than the last compartment boundary (already in your context).`);
55178
+ }
55179
+ if (diagnostics.gitCommitUnavailable === "no_git_repository") {
55180
+ lines.push("Git commits: no git repository — commit search unavailable for this project.");
55181
+ }
55182
+ return lines;
55183
+ }
55184
+ function formatSearchResults(query, results, currentSessionId, diagnostics) {
55185
+ const diagnosticLines = formatSearchDiagnosticLines(results, diagnostics);
55186
+ if (results.length === 0) {
55187
+ if (diagnosticLines.length > 0) {
55188
+ return `No hidden results found for "${query}".
55189
+
55190
+ ${diagnosticLines.join(`
55191
+ `)}`;
55192
+ }
55193
+ return `No results found for "${query}" across notes, memories, primers, git commits, or message history.`;
55194
+ }
55195
+ const bodyParts = results.map((result, index) => formatUnifiedSearchResult(result, index + 1, currentSessionId));
55196
+ if (diagnosticLines.length > 0)
55197
+ bodyParts.push(diagnosticLines.join(`
55198
+ `));
55199
+ if (results.some((result) => result.source === "message" || result.source === "compartment")) {
55200
+ bodyParts.push("Use ctx_expand(start, end) with the range from any message result above to read the full conversation context.");
55201
+ }
55202
+ if (results.some((result) => result.source === "note" && result.anchorOrdinal !== null && result.sourceSessionId === currentSessionId)) {
55203
+ bodyParts.push(SEARCH_NOTE_EXPAND_HINT);
55204
+ }
55205
+ return `Found ${results.length} result${results.length === 1 ? "" : "s"} for "${query}":
55206
+
55207
+ ${bodyParts.join(`
55208
+
55209
+ `)}`;
55210
+ }
54799
55211
 
54800
55212
  // ../plugin/src/features/magic-context/mural/render-trigger.ts
54801
55213
  import { createHash as createHash15 } from "node:crypto";
@@ -55913,12 +56325,14 @@ function dropMarkerSummaryTag(db, sessionId, summaryMessageId) {
55913
56325
  updateTagStatus(db, sessionId, tagNumber, "dropped");
55914
56326
  }
55915
56327
  function persistMarkerStateAndDropReplacedTag(db, sessionId, state, replacedSummaryMessageId) {
56328
+ const transactionStartedAt = performance.now();
55916
56329
  db.transaction(() => {
55917
56330
  setPersistedCompactionMarkerState(db, sessionId, state);
55918
56331
  if (replacedSummaryMessageId !== null) {
55919
56332
  dropMarkerSummaryTag(db, sessionId, replacedSummaryMessageId);
55920
56333
  }
55921
56334
  })();
56335
+ logSlowWriteTransaction("marker-drain", transactionStartedAt);
55922
56336
  }
55923
56337
  function getCompartmentEndMessageIdForOrdinal(db, sessionId, endOrdinal) {
55924
56338
  const row = db.prepare(`SELECT end_message_id
@@ -56558,7 +56972,6 @@ Picking sources:
56558
56972
 
56559
56973
  // src/tools/ctx-search.ts
56560
56974
  var DEFAULT_LIMIT = 10;
56561
- var NOTE_EXPAND_HINT = "Use ctx_expand(start=N-10, end=N) around any note @msg anchor above to read the surrounding conversation context.";
56562
56975
  var ParamsSchema5 = exports_typebox.Object({
56563
56976
  query: exports_typebox.Optional(exports_typebox.String({
56564
56977
  description: "Search query. Matches against memory content, Primers, git commit messages, and raw user/assistant message text."
@@ -56581,89 +56994,6 @@ function normalizeLimit3(limit) {
56581
56994
  return DEFAULT_LIMIT;
56582
56995
  return Math.max(1, Math.floor(limit));
56583
56996
  }
56584
- function formatAge(committedAtMs) {
56585
- const ageMs = Date.now() - committedAtMs;
56586
- if (ageMs < 0)
56587
- return "future";
56588
- const days = Math.floor(ageMs / (24 * 60 * 60 * 1000));
56589
- if (days <= 0)
56590
- return "today";
56591
- if (days === 1)
56592
- return "1d ago";
56593
- if (days < 30)
56594
- return `${days}d ago`;
56595
- const months = Math.floor(days / 30);
56596
- if (months === 1)
56597
- return "1mo ago";
56598
- if (months < 12)
56599
- return `${months}mo ago`;
56600
- const years = Math.floor(days / 365);
56601
- return years === 1 ? "1y ago" : `${years}y ago`;
56602
- }
56603
- function formatResult(result, index, currentSessionId) {
56604
- if (result.source === "memory") {
56605
- const source = result.sourceName ? ` source=${result.sourceName}` : "";
56606
- return [
56607
- `[${index}] [memory] score=${result.score.toFixed(2)} id=${result.memoryId} category=${result.category}${source} match=${result.matchType}`,
56608
- result.content
56609
- ].join(`
56610
- `);
56611
- }
56612
- if (result.source === "git_commit") {
56613
- return [
56614
- `[${index}] [git_commit] score=${result.score.toFixed(2)} sha=${result.shortSha} ${formatAge(result.committedAtMs)} match=${result.matchType}`,
56615
- result.content
56616
- ].join(`
56617
- `);
56618
- }
56619
- if (result.source === "primer") {
56620
- return [
56621
- `[${index}] [primer] score=${result.score.toFixed(2)} id=${result.primerId} support=${result.support} match=${result.matchType}`,
56622
- result.content
56623
- ].join(`
56624
- `);
56625
- }
56626
- if (result.source === "note") {
56627
- const anchor = result.anchorOrdinal !== null && result.sourceSessionId === currentSessionId ? ` @msg ${result.anchorOrdinal}` : "";
56628
- return [
56629
- `[${index}] [note] score=${result.score.toFixed(2)} id=#${result.noteId} status=${result.status} ${formatAge(result.createdAt)}${anchor}`,
56630
- result.content
56631
- ].join(`
56632
- `);
56633
- }
56634
- if (result.source === "compartment") {
56635
- return [
56636
- `[${index}] [message] score=${result.score.toFixed(2)} compartment_id=${result.compartmentId} range=${result.startOrdinal}-${result.endOrdinal} match=${result.matchType} title=${result.title}`,
56637
- result.snippet ? `Snippet: ${result.snippet}` : result.content
56638
- ].join(`
56639
- `);
56640
- }
56641
- const expandStart = Math.max(1, result.messageOrdinal - 3);
56642
- const expandEnd = result.messageOrdinal + 3;
56643
- return [
56644
- `[${index}] [message] score=${result.score.toFixed(2)} ordinal=${result.messageOrdinal} range=${expandStart}-${expandEnd} role=${result.role}`,
56645
- result.content
56646
- ].join(`
56647
- `);
56648
- }
56649
- function formatSearchResults(query, results, currentSessionId) {
56650
- if (results.length === 0) {
56651
- return `No results found for "${query}" across notes, memories, primers, git commits, or message history.`;
56652
- }
56653
- const bodyParts = results.map((result, index) => formatResult(result, index + 1, currentSessionId));
56654
- if (results.some((result) => result.source === "message" || result.source === "compartment")) {
56655
- bodyParts.push("Use ctx_expand(start, end) with the range from any message result above to read the full conversation context.");
56656
- }
56657
- if (results.some((result) => result.source === "note" && result.anchorOrdinal !== null && result.sourceSessionId === currentSessionId)) {
56658
- bodyParts.push(NOTE_EXPAND_HINT);
56659
- }
56660
- const body = bodyParts.join(`
56661
-
56662
- `);
56663
- return `Found ${results.length} result${results.length === 1 ? "" : "s"} for "${query}":
56664
-
56665
- ${body}`;
56666
- }
56667
56997
  function createCtxSearchTool(deps) {
56668
56998
  const resolveProject = deps.resolveProjectIdentity ?? resolveProjectIdentityForSession;
56669
56999
  return {
@@ -56712,6 +57042,7 @@ function createCtxSearchTool(deps) {
56712
57042
  const lastCompartmentEnd = getLastCompartmentEndMessage(deps.db, sessionId);
56713
57043
  const messageOrdinalCutoff = lastCompartmentEnd >= 0 ? lastCompartmentEnd : 0;
56714
57044
  const visibleMemoryIds = getVisibleMemoryIds(deps.db, sessionId);
57045
+ const diagnostics = createUnifiedSearchDiagnostics();
56715
57046
  const idShape = parseIdShapedQuery(query);
56716
57047
  if (idShape && memoryEnabled) {
56717
57048
  const idResults = resolveMemoriesByIdsForSearch({
@@ -56719,14 +57050,15 @@ function createCtxSearchTool(deps) {
56719
57050
  projectPath: projectIdentity,
56720
57051
  ids: idShape,
56721
57052
  limit: Math.max(normalizeLimit3(params.limit), idShape.length),
56722
- visibleMemoryIds
57053
+ visibleMemoryIds,
57054
+ diagnostics
56723
57055
  });
56724
- if (idResults !== null) {
57056
+ if (idResults !== null || diagnostics.suppressedVisibleMemoryIds.length > 0) {
56725
57057
  return {
56726
57058
  content: [
56727
57059
  {
56728
57060
  type: "text",
56729
- text: formatSearchResults(query, idResults, sessionId)
57061
+ text: formatSearchResults(query, idResults ?? [], sessionId, diagnostics)
56730
57062
  }
56731
57063
  ],
56732
57064
  details: undefined
@@ -56746,13 +57078,15 @@ function createCtxSearchTool(deps) {
56746
57078
  gitCommitsEnabled,
56747
57079
  sources: params.sources,
56748
57080
  visibleMemoryIds,
57081
+ diagnostics,
57082
+ gitRepositoryAvailable: directoryHasGitMetadata(ctx.cwd),
56749
57083
  explicitSearch: true
56750
57084
  });
56751
57085
  return {
56752
57086
  content: [
56753
57087
  {
56754
57088
  type: "text",
56755
- text: formatSearchResults(query, results, sessionId)
57089
+ text: formatSearchResults(query, results, sessionId, diagnostics)
56756
57090
  }
56757
57091
  ],
56758
57092
  details: undefined
@@ -56868,4 +57202,4 @@ function registerMagicContextTools(pi, opts) {
56868
57202
  }
56869
57203
  }
56870
57204
 
56871
- export { COMPACTION_ENABLED_PATH, isDreamerRunnable, isCompactionEnabled, migrateMagicContextConfigLocations, hasShareabilitySensitiveText, FAIL_CLOSED_DOCTOR_COMMAND, formatFailClosedBlockingMessage, createFailClosedBlockingError, isFailClosedBlockingError, shouldBypassFailClosedBlock, createFailClosedController, ProjectIdentityError, resolveProjectIdentityStrict, resolveProjectIdentity, resolveProjectIdentityForSession, beginBootQuietPeriod, scheduleAfterBootQuiet, Database, closeQuietly, isMidTurn, textMentionsRecentCommit, hasMeaningfulUserText, extractTexts, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, isRecord, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isEditTool, applyEditMarkerToInput, setRawMessageProvider, withRawMessageProvider, cleanUserText, withRawSessionMessageCache, readRawSessionMessages, primeTailRawMessageCache, getCachedAbsoluteMessageCount, primeInMemoryTailRawMessageCache, getRawSessionMessageCount, getRawSessionTagKeysThrough, getLegacyProtectedTailStartOrdinal, readSessionChunk, clearCompressionDepth, clearCompressionDepthRange, getMessageIndexSourceIdentity, isMessageIndexSourceCurrent, getLastIndexedOrdinal, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, indexSingleMessage, indexMessagesAfterOrdinal, sweepOrphanedOpenCodeMessageIndexes, recordSessionProjectIdentity, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLease, isCompartmentLeaseHeld, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, ompModelRefToCanonical, resolveModelRefForOmp, persistCachedM0, clearCachedM0M1, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, getCompartmentsByEndMessageId, appendCompartments, saveRecompStagingPass, getRecompStaging, clearRecompStaging, getRecompPartialRange, setRecompPartialRange, escapeXmlAttr, escapeXmlContent, getModuleNoteEvaluationBridge, getContextStoreUuid, parseCompartmentOutput, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, sourceNameForMemory, computeWorkspaceEpochFingerprint, bumpEpochsForWorkspaceMembers, readProjectDocsCanonical, copySessionStateForClone, getErrorMessage, describeError, setStoragePrivatePermissionEnforcement, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, applySqliteTuningPragmas, runSqliteOptimize, openDatabase, openDatabaseAsync, queueM0Mutation, getMaxM0MutationId, queueMemoryMutation, getMemoryMutationsForRender, getMemoryMutationsForRenderByProjects, getMaxMemoryMutationId, getMaxMemoryMutationIdForProjects, MAX_EXECUTE_THRESHOLD, escalationBands, loadProtectedTailMeta, markProtectedTailPolicyV3Seeded, recordProtectedTailPublicationFloor, recordProtectedTailNoEligibleHead, getWrapupInProgressState, isWrapupInProgress, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, reserveProtectedTailDrainTokens, clearEmergencyDrainLatch, recordHistorianDrainFailure, clearHistorianDrainFailure, rollbackProtectedTailDrainReservation, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, resetLastNudgeCycle, resetLastNudgeCycleIfTailShrank, getChannel2NudgeState, getChannel2NudgeClaim, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getPersistedNoteNudge, setPersistedNoteNudgeTrigger, setPersistedNoteNudgeTriggerMessageId, getNoteNudgeAnchors, getAutoSearchHintDecisions, deliverNoteNudgeAtomic, appendAutoSearchHintDecision, pruneNoteNudgeAnchors, pruneAutoSearchHintDecisions, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, getNoteLastReadAt, getHistorianFailureState, incrementHistorianFailure, clearHistorianFailureState, getOverflowState, recordOverflowDetected, clearEmergencyRecovery, clearDetectedContextLimit, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, getProcessedImageStrippedIds, addProcessedImageStrippedIds, getPendingCompactionMarkerState, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, setPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, getSessionsWithPendingPiMarker, peekDeferredExecutePending, setDeferredExecutePendingIfAbsent, clearDeferredExecutePendingIfMatches, setSessionWorkMetrics, getSessionWorkMetrics, getOrCreateSessionMeta, updateSessionMeta, advanceToolReclaimWatermark, retryPendingSessionCleanups, getNotes, getSessionNotes, getPendingSmartNotes, getReadySmartNotes, markNoteReady, markNoteChecked, queuePendingOp, getPendingOps, clearPendingOps, removePendingOp, PRIMER_CANDIDATE_TTL_MS, PRIMER_CANDIDATE_MAX_AGE_MS, primerOccurrenceKey, primerOccurrenceUtcDay, insertPrimerCandidates, updatePrimerCandidateEmbedding, getPrimerCandidatesByIds, getPrimerCandidatesForPromotion, countPrimerCandidatesForProject, getActivePrimers, createPrimer, updatePrimerSupport, updatePrimerAnswer, GLOBAL_USER_PROFILE_PROJECT_PATH, getProjectState, bumpProjectUserProfileVersion, saveSourceContent, replaceSourceContent, getSourceContents, recordSubagentInvocation, getLatestHistorianInvocationId, CTX_REDUCE_KEEP, estimateImageTokensFromDataUrl, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, buildChannel2Reminder, shouldUseStickyChannel1Reminder, buildChannel1Reminder, planEmergencyDrop, updateTagByteSize, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, deriveTagLoadFloor, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, BoundedSessionMap, getWindowOverlay, resolveWindowOverlayFacts, deriveWindowGeometry, formatWindowDerivationLine, isSaneLimit, resolveOutputReserve, getSdkContextLimit, resolveModelConfigOrDefault, promptSurfaceHashMaterial, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, SYNTH_USER_ID_PREFIX, resolvePiStableId, isMidTurnPi, readPiSessionMessages, findLastModelKeyFromBranch, convertEntriesToRawMessages, withContentLanguageDirective, withMigrationLanguageDirective, buildPrimaryLanguageDirective, computeCueContentHash, hasMuralCueColumns, getMuralCueState, memoryNeedsCue, setMuralCue, recordMuralCueRejection, invalidateMemory, computeNormalizedHash, hasMemoryShareableColumn, hasMemoryClassifiedAtColumn, getUnclassifiedMemoryIds, insertMemory, getMemoryByHash, getMemoriesByProject, getMemoriesByProjects, getMaxMemoryIdForProjects, getAllActiveMemoriesForMigration, getMemoryById, setMemoryClassification, archiveMemory, deleteMemory, getMemoryCount, getMemoryCountsByStatus, USER_MEMORY_CANDIDATE_TTL_MS, insertUserMemoryCandidates, getUserMemoryCandidates, deleteUserMemoryCandidates, pruneExpiredUserMemoryCandidates, insertUserMemory, getActiveUserMemories, updateUserMemoryContent, dismissUserMemory, getOpenCodeDbPath2 as getOpenCodeDbPath, parseCron, nextOccurrence, nextDueAtMs, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, DEFAULT_HISTORIAN_TIMEOUT_MS, buildCanonicalChunkTextFromFts, buildCompartmentSummaryFallbackText, canonicalizeInMemoryChunkTextForEmbedding, chunkCanonicalText, chunkEmbeddingWindowsAreCurrent, replaceCompartmentChunkEmbeddings, cosineSimilarity, GIT_SWEEP_LEASE_RENEWAL_MS, acquireGitSweepLease, renewGitSweepLease, markGitSweepSuccessAndRelease, parkGitSweepNonIndexable, releaseGitSweepLease, contentSha256, sweepStaleEmbeddingIdentitiesForProject, enqueueShadowEmbeddingItems, getProjectEmbeddingSnapshot, getProjectChunkEmbeddingModelId, getProjectEmbeddingMaxInputTokens, embedTextForProject, embedBatchForProject, embedItemsForProject, embedUnembeddedMemoriesForProject, drainCommitBacklogForProject, embedSessionCompartmentChunks, getEmbeddingCoverageStatus, promoteSessionFactsDurable, embedPromotedFacts, recordMemoryMapping, recordMemoryVerifications, getUnmappedMemoryIds, clearMemoryVerifications, getMemoryVerifications, resolveGitTopLevel, readGitHead, readGitChangedFilesSince, readGitFileChangeTimesSince, verificationFileExists, normalizeVerificationFiles, wakePlaneStatus, indexCommitsForProject, embedUnembeddedCommits, loadPiConfig, ensureProjectRegisteredFromPiDirectory, resolveMuralWire, updateCompactionMarkerAfterPublication, COMPARTMENT_RENDER_EPOCH, encodeCachedM0UpgradeIdentity, decodeCachedM0UpgradeIdentity, DEFAULT_HISTORY_BUDGET_TOKENS, renderCompartmentAtTier, renderDecayedCompartments, extractM0Block, TEMPORAL_MARKER_PATTERN, temporalMarkerPrefix, clearInjectionCache, getVisibleMemoryIds, renderMemoryBlock, DEFAULT_MEMORY_BUDGET_TOKENS, DEFAULT_USER_PROFILE_BUDGET_TOKENS, trimMemoriesToBudgetV2, trimWorkspaceMemoriesToBudgetV2, trimUserMemoriesToBudget, renderMemoryBlockV2, stripMemoryMuralBlock, unifiedSearch, rememberTodowriteToolCallTodos, parseTodos, setTodoSnapshot, registerTodoOverlay, registerTodoStateLifecycle, registerMagicContextTools };
57205
+ export { COMPACTION_ENABLED_PATH, isDreamerRunnable, isCompactionEnabled, migrateMagicContextConfigLocations, FAIL_CLOSED_DOCTOR_COMMAND, formatFailClosedBlockingMessage, createFailClosedBlockingError, isFailClosedBlockingError, shouldBypassFailClosedBlock, createFailClosedController, ProjectIdentityError, resolveProjectIdentityStrict, resolveProjectIdentity, resolveProjectIdentityForSession, beginBootQuietPeriod, scheduleAfterBootQuiet, Database, closeQuietly, isMidTurn, textMentionsRecentCommit, hasMeaningfulUserText, extractTexts, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, isRecord, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isEditTool, applyEditMarkerToInput, setRawMessageProvider, withRawMessageProvider, cleanUserText, withRawSessionMessageCache, readRawSessionMessages, primeTailRawMessageCache, getCachedAbsoluteMessageCount, primeInMemoryTailRawMessageCache, getRawSessionMessageCount, getRawSessionTagKeysThrough, getLegacyProtectedTailStartOrdinal, readSessionChunk, clearCompressionDepth, clearCompressionDepthRange, getMessageIndexSourceIdentity, isMessageIndexSourceCurrent, getLastIndexedOrdinal, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, indexSingleMessage, indexMessagesAfterOrdinal, sweepOrphanedOpenCodeMessageIndexes, recordSessionProjectIdentity, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLease, isCompartmentLeaseHeld, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, ompModelRefToCanonical, resolveModelRefForOmp, persistCachedM0, clearCachedM0M1, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, getCompartmentsByEndMessageId, appendCompartments, saveRecompStagingPass, getRecompStaging, clearRecompStaging, getRecompPartialRange, setRecompPartialRange, escapeXmlAttr, escapeXmlContent, getModuleNoteEvaluationBridge, getContextStoreUuid, parseCompartmentOutput, logSlowWriteTransaction, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, sourceNameForMemory, computeWorkspaceEpochFingerprint, bumpEpochsForWorkspaceMembers, readProjectDocsCanonical, copySessionStateForClone, getErrorMessage, describeError, setStoragePrivatePermissionEnforcement, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, applySqliteTuningPragmas, runSqliteOptimize, openDatabase, openDatabaseAsync, queueM0Mutation, getMaxM0MutationId, queueMemoryMutation, getMemoryMutationsForRender, getMemoryMutationsForRenderByProjects, getMaxMemoryMutationId, getMaxMemoryMutationIdForProjects, MAX_EXECUTE_THRESHOLD, escalationBands, describeProtectedTailDrainBudgetSkip, loadProtectedTailMeta, markProtectedTailPolicyV3Seeded, recordProtectedTailPublicationFloor, recordProtectedTailNoEligibleHead, getWrapupInProgressState, isWrapupInProgress, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, reserveProtectedTailDrainTokens, clearEmergencyDrainLatch, recordHistorianDrainFailure, clearHistorianDrainFailure, rollbackProtectedTailDrainReservation, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, markChannel1PostReduceGracePending, captureChannel1PostReduceGraceBaseline, getChannel2NudgeState, getChannel2NudgeClaim, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getPersistedNoteNudge, setPersistedNoteNudgeTrigger, setPersistedNoteNudgeTriggerMessageId, getNoteNudgeAnchors, getAutoSearchHintDecisions, deliverNoteNudgeAtomic, appendAutoSearchHintDecision, pruneNoteNudgeAnchors, pruneAutoSearchHintDecisions, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, getNoteLastReadAt, getHistorianFailureState, incrementHistorianFailure, clearHistorianFailureState, getOverflowState, recordOverflowDetected, clearEmergencyRecovery, clearDetectedContextLimit, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, getProcessedImageStrippedIds, addProcessedImageStrippedIds, getPendingCompactionMarkerState, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, setPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, getSessionsWithPendingPiMarker, peekDeferredExecutePending, setDeferredExecutePendingIfAbsent, clearDeferredExecutePendingIfMatches, setSessionWorkMetrics, getSessionWorkMetrics, getOrCreateSessionMeta, updateSessionMeta, advanceToolReclaimWatermark, retryPendingSessionCleanups, getNotes, getSessionNotes, getPendingSmartNotes, getReadySmartNotes, markNoteReady, markNoteChecked, queuePendingOp, getPendingOps, getPendingOpsCount, clearPendingOps, removePendingOp, PRIMER_CANDIDATE_TTL_MS, PRIMER_CANDIDATE_MAX_AGE_MS, primerOccurrenceKey, primerOccurrenceUtcDay, insertPrimerCandidates, updatePrimerCandidateEmbedding, getPrimerCandidatesByIds, getPrimerCandidatesForPromotion, countPrimerCandidatesForProject, getActivePrimers, createPrimer, updatePrimerSupport, updatePrimerAnswer, GLOBAL_USER_PROFILE_PROJECT_PATH, getProjectState, bumpProjectUserProfileVersion, saveSourceContent, replaceSourceContent, getSourceContents, recordSubagentInvocation, getLatestHistorianInvocationId, CTX_REDUCE_KEEP, estimateImageTokensFromDataUrl, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, planEmergencyDrop, updateTagByteSize, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, deriveTagLoadFloor, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, BoundedSessionMap, getWindowOverlay, resolveWindowOverlayFacts, deriveWindowGeometry, formatWindowDerivationLine, isSaneLimit, resolveOutputReserve, getSdkContextLimit, resolveModelConfigOrDefault, promptSurfaceHashMaterial, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, SYNTH_USER_ID_PREFIX, resolvePiStableId, isMidTurnPi, readPiSessionSnapshot, readPiSessionMessages, findLastModelKeyFromBranch, convertEntriesToRawMessages, withContentLanguageDirective, withMigrationLanguageDirective, buildPrimaryLanguageDirective, computeCueContentHash, hasMuralCueColumns, getMuralCueState, memoryNeedsCue, setMuralCue, recordMuralCueRejection, invalidateMemory, computeNormalizedHash, hasMemoryShareableColumn, hasMemoryClassifiedAtColumn, getUnclassifiedMemoryIds, insertMemory, getMemoryByHash, getMemoriesByProject, getMemoriesByProjects, getMaxMemoryIdForProjects, getAllActiveMemoriesForMigration, getMemoryById, setMemoryClassification, archiveMemory, deleteMemory, getMemoryCount, getMemoryCountsByStatus, USER_MEMORY_CANDIDATE_TTL_MS, insertUserMemoryCandidates, getUserMemoryCandidates, deleteUserMemoryCandidates, pruneExpiredUserMemoryCandidates, insertUserMemory, getActiveUserMemories, updateUserMemoryContent, dismissUserMemory, getOpenCodeDbPath2 as getOpenCodeDbPath, parseCron, nextOccurrence, nextDueAtMs, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, DEFAULT_HISTORIAN_TIMEOUT_MS, buildCanonicalChunkTextFromFts, buildCompartmentSummaryFallbackText, canonicalizeInMemoryChunkTextForEmbedding, chunkCanonicalText, chunkEmbeddingWindowsAreCurrent, replaceCompartmentChunkEmbeddings, cosineSimilarity, GIT_SWEEP_LEASE_RENEWAL_MS, acquireGitSweepLease, renewGitSweepLease, markGitSweepSuccessAndRelease, parkGitSweepNonIndexable, releaseGitSweepLease, contentSha256, sweepStaleEmbeddingIdentitiesForProject, enqueueShadowEmbeddingItems, getProjectEmbeddingSnapshot, getProjectChunkEmbeddingModelId, getProjectEmbeddingMaxInputTokens, embedTextForProject, embedBatchForProject, embedItemsForProject, embedUnembeddedMemoriesForProject, drainCommitBacklogForProject, embedSessionCompartmentChunks, getEmbeddingCoverageStatus, promoteSessionFactsDurable, embedPromotedFacts, recordMemoryMapping, recordMemoryVerifications, getUnmappedMemoryIds, clearMemoryVerifications, getMemoryVerifications, resolveGitTopLevel, readGitHead, readGitChangedFilesSince, readGitFileChangeTimesSince, verificationFileExists, normalizeVerificationFiles, wakePlaneStatus, indexCommitsForProject, embedUnembeddedCommits, loadPiConfig, ensureProjectRegisteredFromPiDirectory, resolveMuralWire, updateCompactionMarkerAfterPublication, COMPARTMENT_RENDER_EPOCH, encodeCachedM0UpgradeIdentity, decodeCachedM0UpgradeIdentity, DEFAULT_HISTORY_BUDGET_TOKENS, renderCompartmentAtTier, renderDecayedCompartments, extractM0Block, TEMPORAL_MARKER_PATTERN, temporalMarkerPrefix, clearInjectionCache, getVisibleMemoryIds, renderMemoryBlock, DEFAULT_MEMORY_BUDGET_TOKENS, DEFAULT_USER_PROFILE_BUDGET_TOKENS, trimMemoriesToBudgetV2, trimWorkspaceMemoriesToBudgetV2, trimUserMemoriesToBudget, renderMemoryBlockV2, stripMemoryMuralBlock, unifiedSearch, rememberTodowriteToolCallTodos, parseTodos, setTodoSnapshot, registerTodoOverlay, registerTodoStateLifecycle, registerMagicContextTools };