@cortexkit/pi-magic-context 0.45.0 → 0.46.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.
@@ -8,14 +8,17 @@ import {
8
8
  log
9
9
  } from "./index-7n8gb6j3.js";
10
10
  import {
11
+ closeQuietly,
12
+ ensureColumn,
13
+ healAllNullColumns,
14
+ installV95PerfSchema,
11
15
  resolveOpenCodeDbPath,
12
16
  hasV1MessageTables,
13
17
  openCodeDbPathExists,
14
18
  getOpenCodeDbProbeDescriptions,
15
- closeQuietly,
16
19
  setDatabase,
17
20
  loadToolDefinitionMeasurements
18
- } from "./index-e16q5sd7.js";
21
+ } from "./index-td885aah.js";
19
22
  import {
20
23
  registerSlowWriteReporter,
21
24
  registerSqliteDiagnosticSink,
@@ -25,13 +28,19 @@ import {
25
28
  withoutSqliteTransformPass,
26
29
  isTransientSqliteError,
27
30
  withPrivilegedWriter
28
- } from "./index-2pbbk3en.js";
31
+ } from "./index-wv28nczk.js";
29
32
  import {
30
- shouldEnforcePrivateStoragePermissions
31
- } from "./index-4d3cba70.js";
33
+ shouldEnforcePrivateStoragePermissions,
34
+ ensureStorageDirectorySync,
35
+ writeStorageFileSync,
36
+ writeStorageFileAtomicSync,
37
+ copyStorageFileSync,
38
+ copyStorageTreeSync,
39
+ tightenStorageTreeSync
40
+ } from "./index-vf38qhee.js";
32
41
  import {
33
42
  logSlowWriteTransaction
34
- } from "./index-7s5yk3za.js";
43
+ } from "./index-2p1e8706.js";
35
44
 
36
45
  // ../plugin/src/features/magic-context/fail-closed-block.ts
37
46
  var FAIL_CLOSED_DOCTOR_COMMAND = "npx @cortexkit/magic-context@latest doctor";
@@ -265,8 +274,8 @@ import { homedir } from "node:os";
265
274
  import path3 from "node:path";
266
275
 
267
276
  // ../plugin/src/features/magic-context/memory/project-identity-cache.ts
268
- import { createHash, randomUUID } from "node:crypto";
269
- import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
277
+ import { createHash } from "node:crypto";
278
+ import { readFileSync } from "node:fs";
270
279
  import path2 from "node:path";
271
280
 
272
281
  // ../plugin/src/shared/project-directory-key.ts
@@ -287,10 +296,7 @@ function cachePath(directory, storageDir = getMagicContextStorageDir()) {
287
296
  function rememberGitIdentity(directory, identity) {
288
297
  try {
289
298
  const destination = cachePath(directory);
290
- mkdirSync(path2.dirname(destination), { recursive: true });
291
- const temporary = `${destination}.${randomUUID()}.tmp`;
292
- writeFileSync(temporary, JSON.stringify({ directory: projectDirectoryKey(directory), identity }), { mode: 384 });
293
- renameSync(temporary, destination);
299
+ writeStorageFileAtomicSync(destination, JSON.stringify({ directory: projectDirectoryKey(directory), identity }));
294
300
  } catch {}
295
301
  }
296
302
  function readRememberedGitIdentity(directory, storageDir) {
@@ -1591,100 +1597,6 @@ function installCompartmentHistoryVersions(db) {
1591
1597
  }).immediate();
1592
1598
  }
1593
1599
 
1594
- // ../plugin/src/features/magic-context/storage-schema-helpers.ts
1595
- function ensureColumn(db, table, column, definition) {
1596
- if (!/^[a-z][a-z0-9_]*$/.test(table) || !/^[a-z][a-z0-9_]*$/.test(column) || !/^[A-Z0-9_"'(),[\]\s]+$/i.test(definition)) {
1597
- throw new Error(`Unsafe schema identifier: ${table}.${column} ${definition}`);
1598
- }
1599
- const rows = db.prepare(`PRAGMA table_info(${table})`).all();
1600
- if (rows.some((row) => row.name === column)) {
1601
- return;
1602
- }
1603
- try {
1604
- db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
1605
- } catch (err) {
1606
- const recheck = db.prepare(`PRAGMA table_info(${table})`).all();
1607
- if (recheck.some((row) => row.name === column)) {
1608
- return;
1609
- }
1610
- throw err;
1611
- }
1612
- }
1613
- function healAllNullColumns(db) {
1614
- const existingColumns = getSessionMetaColumns(db);
1615
- const fallbacks = [
1616
- ["cache_ttl", ""],
1617
- ["last_nudge_band", ""],
1618
- ["last_nudge_level", ""],
1619
- ["channel2_nudge_claim_token", ""],
1620
- ["last_transform_error", ""],
1621
- ["nudge_anchor_message_id", ""],
1622
- ["nudge_anchor_text", ""],
1623
- ["sticky_turn_reminder_text", ""],
1624
- ["sticky_turn_reminder_message_id", ""],
1625
- ["note_nudge_trigger_message_id", ""],
1626
- ["note_nudge_sticky_text", ""],
1627
- ["note_nudge_sticky_message_id", ""],
1628
- ["last_todo_state", ""],
1629
- ["todo_synthetic_call_id", ""],
1630
- ["todo_synthetic_anchor_message_id", ""],
1631
- ["todo_synthetic_state_json", ""],
1632
- ["system_prompt_hash", ""],
1633
- ["stripped_placeholder_ids", ""],
1634
- ["stale_reduce_stripped_ids", ""],
1635
- ["processed_image_stripped_ids", ""],
1636
- ["merged_reasoning_stripped_ids", ""],
1637
- ["thinking_binding_recovery_target", ""],
1638
- ["trailing_blank_decisions", ""],
1639
- ["memory_block_cache", ""],
1640
- ["memory_block_ids", ""],
1641
- ["compaction_marker_state", ""],
1642
- ["key_files", ""],
1643
- ["times_execute_threshold_reached", 0],
1644
- ["compartment_in_progress", 0],
1645
- ["historian_failure_count", 0],
1646
- ["cleared_reasoning_through_tag", 0],
1647
- ["memory_block_count", 0],
1648
- ["system_prompt_tokens", 0],
1649
- ["conversation_tokens", 0],
1650
- ["tool_call_tokens", 0],
1651
- ["note_nudge_trigger_pending", 0],
1652
- ["observed_safe_input_tokens", 0],
1653
- ["cache_alert_sent", 0],
1654
- ["new_work_tokens", 0],
1655
- ["total_input_tokens", 0],
1656
- ["last_emergency_input_sample", 0],
1657
- ["channel2_nudge_claimed_at", 0],
1658
- ["last_usage_context_limit", 0],
1659
- ["prior_boundary_ordinal", 1],
1660
- ["protected_tail_policy_version", 0],
1661
- ["protected_tail_drain_window_started_at", 0],
1662
- ["protected_tail_drain_tokens", 0],
1663
- ["recovery_no_eligible_head_count", 0],
1664
- ["force_emergency_bypass_window_start", 0],
1665
- ["force_emergency_bypass_used", 0],
1666
- ["emergency_drain_active", 0],
1667
- ["historian_drain_failure_at", 0]
1668
- ];
1669
- const presentFallbacks = fallbacks.filter(([column]) => existingColumns.has(column));
1670
- if (presentFallbacks.length > 0) {
1671
- const assignments = presentFallbacks.map(([column]) => `${column} = COALESCE(${column}, ?)`).join(", ");
1672
- const nullPredicate = presentFallbacks.map(([column]) => `${column} IS NULL`).join(" OR ");
1673
- db.prepare(`UPDATE session_meta SET ${assignments} WHERE ${nullPredicate}`).run(...presentFallbacks.map(([, fallback]) => fallback));
1674
- }
1675
- healMissingMemoryBlockIds(db, existingColumns);
1676
- }
1677
- function getSessionMetaColumns(db) {
1678
- const rows = db.prepare("PRAGMA table_info(session_meta)").all();
1679
- return new Set(rows.flatMap((row) => typeof row.name === "string" ? [row.name] : []));
1680
- }
1681
- function healMissingMemoryBlockIds(db, columns) {
1682
- if (!columns.has("memory_block_cache") || !columns.has("memory_block_ids") || !columns.has("memory_block_count")) {
1683
- return;
1684
- }
1685
- db.prepare("UPDATE session_meta SET memory_block_cache = '' WHERE memory_block_cache != '' AND (memory_block_ids IS NULL OR memory_block_ids = '') AND memory_block_count > 0").run();
1686
- }
1687
-
1688
1600
  // ../plugin/src/features/magic-context/workspaces.ts
1689
1601
  import { createHash as createHash4 } from "node:crypto";
1690
1602
  var VALID_SHARE_CATEGORIES = new Set(V2_MEMORY_CATEGORIES);
@@ -4296,6 +4208,13 @@ var MIGRATIONS = [
4296
4208
  splitLkgSlotPrefixes(db);
4297
4209
  splitReplayDecisions(db);
4298
4210
  }
4211
+ },
4212
+ {
4213
+ version: 95,
4214
+ description: "cover cleanup and retention reads, index git FTS rowids and store temporal replay decisions",
4215
+ up(db) {
4216
+ installV95PerfSchema(db, true);
4217
+ }
4299
4218
  }
4300
4219
  ];
4301
4220
  var LATEST_MIGRATION_VERSION = MIGRATIONS.reduce((max, m) => Math.max(max, m.version), 0);
@@ -4323,6 +4242,9 @@ function hasPendingMigrations(db) {
4323
4242
  return MIGRATIONS.some((candidate) => candidate.version > currentVersion && !isMigrationApplied(db, candidate.version));
4324
4243
  }
4325
4244
  var mainThreadMigrationBodies = 0;
4245
+ function getMainThreadMigrationBodyCount() {
4246
+ return mainThreadMigrationBodies;
4247
+ }
4326
4248
  function isSiblingMigrationConflict(db, error, version) {
4327
4249
  if (!(error instanceof Error))
4328
4250
  return false;
@@ -4453,11 +4375,7 @@ function scheduleAfterBootQuiet(task, additionalDelayMs = 0) {
4453
4375
 
4454
4376
  // ../plugin/src/features/magic-context/storage-db.ts
4455
4377
  import {
4456
- chmodSync,
4457
- copyFileSync,
4458
- cpSync,
4459
4378
  existsSync as existsSync3,
4460
- mkdirSync as mkdirSync2,
4461
4379
  readdirSync,
4462
4380
  readFileSync as readFileSync3,
4463
4381
  rmdirSync,
@@ -5249,8 +5167,20 @@ function inspectWindowsProcessesSync() {
5249
5167
  };
5250
5168
  }
5251
5169
 
5170
+ // ../plugin/src/shared/sqlite-context-pragmas.ts
5171
+ function configureContextDatabasePragmas(db, readonly = false) {
5172
+ const [row] = db.prepare(readonly ? "PRAGMA journal_mode" : "PRAGMA journal_mode=WAL").all();
5173
+ const mode = row?.journal_mode?.toLowerCase();
5174
+ if (mode === "memory" || readonly && mode !== "wal")
5175
+ return;
5176
+ if (mode !== "wal") {
5177
+ throw new Error(`context.db requires WAL before synchronous=NORMAL (got ${mode ?? "unknown"})`);
5178
+ }
5179
+ db.exec("PRAGMA synchronous=NORMAL");
5180
+ }
5181
+
5252
5182
  // ../plugin/src/features/magic-context/context-store-uuid.ts
5253
- import { randomUUID as randomUUID2 } from "node:crypto";
5183
+ import { randomUUID } from "node:crypto";
5254
5184
  function getContextStoreUuid(db) {
5255
5185
  const row = db.prepare("SELECT value FROM context_store_meta WHERE key = 'store_uuid'").get();
5256
5186
  return typeof row?.value === "string" && row.value.length > 0 ? row.value : null;
@@ -5259,7 +5189,7 @@ function ensureContextStoreUuid(db) {
5259
5189
  const existing = getContextStoreUuid(db);
5260
5190
  if (existing)
5261
5191
  return existing;
5262
- const minted = randomUUID2();
5192
+ const minted = randomUUID();
5263
5193
  withPrivilegedWriter(db, () => {
5264
5194
  db.transaction(() => {
5265
5195
  db.prepare("INSERT INTO context_store_meta(key, value) VALUES ('store_uuid', ?) ON CONFLICT(key) DO NOTHING").run(minted);
@@ -5516,6 +5446,9 @@ function startBootDeadline(timeoutMs, onDeferred) {
5516
5446
  }
5517
5447
 
5518
5448
  // ../plugin/src/features/magic-context/migration-worker-client.ts
5449
+ function workerStartError(detail) {
5450
+ return new Error(`the migration worker could not start (${detail}); reinstall or rebuild the plugin`);
5451
+ }
5519
5452
  function defaultWorkerEntry() {
5520
5453
  return new URL(new URL(import.meta.url).pathname.endsWith(".ts") ? "./migration-worker.ts" : "./migration-worker.js", import.meta.url);
5521
5454
  }
@@ -5526,8 +5459,7 @@ function runMigrationsOffThread(data) {
5526
5459
  try {
5527
5460
  worker = new Worker(workerEntryOverride ?? defaultWorkerEntry(), { workerData: data });
5528
5461
  } catch (error) {
5529
- log(`[migrations] could not start the migration worker (${error instanceof Error ? error.message : String(error)}); applying migrations on the main thread`);
5530
- resolve("worker_unavailable");
5462
+ reject(workerStartError(error instanceof Error ? error.message : String(error)));
5531
5463
  return;
5532
5464
  }
5533
5465
  log(`[migrations] applying pending migrations on a worker thread: ${data.dbPath}`);
@@ -5551,14 +5483,14 @@ function runMigrationsOffThread(data) {
5551
5483
  };
5552
5484
  const unavailable = (detail) => {
5553
5485
  settle(() => {
5554
- log(`[migrations] the migration worker did not start (${detail}); applying migrations on the main thread`);
5555
- resolve("worker_unavailable");
5486
+ reject(workerStartError(detail));
5556
5487
  });
5557
5488
  };
5558
5489
  worker.on("message", (message) => {
5559
5490
  switch (message.type) {
5560
5491
  case "ready":
5561
5492
  ready = true;
5493
+ log(`[migrations] migration worker ready: ${data.dbPath}`);
5562
5494
  return;
5563
5495
  case "log":
5564
5496
  writeForwardedLogLine(message.line);
@@ -5568,9 +5500,17 @@ function runMigrationsOffThread(data) {
5568
5500
  setBusy(!message.waiting);
5569
5501
  return;
5570
5502
  case "done":
5503
+ if (!ready) {
5504
+ unavailable("reported completion before ready");
5505
+ return;
5506
+ }
5571
5507
  settle(() => resolve("migrated"));
5572
5508
  return;
5573
5509
  case "failed":
5510
+ if (!ready) {
5511
+ unavailable(message.message);
5512
+ return;
5513
+ }
5574
5514
  settle(() => reject(new Error(message.message)));
5575
5515
  return;
5576
5516
  }
@@ -5869,38 +5809,14 @@ function getSchemaFenceRejection() {
5869
5809
  function getMigrationOnOpenRefusal() {
5870
5810
  return lastMigrationOnOpenRefusal;
5871
5811
  }
5872
- var LATEST_SUPPORTED_VERSION = 94;
5812
+ var LATEST_SUPPORTED_VERSION = 95;
5873
5813
  var BOOT_SQLITE_BUSY_TIMEOUT_MS = 5000;
5874
- var PERMISSIONS_ENFORCEABLE = process.platform !== "win32";
5875
- var defaultStoragePermissionFs = { chmodSync, mkdirSync: mkdirSync2 };
5876
- var storagePermissionFs = defaultStoragePermissionFs;
5877
5814
  function ensureSecureStorageDir(dir) {
5878
- if (!shouldEnforcePrivateStoragePermissions()) {
5879
- storagePermissionFs.mkdirSync(dir, { recursive: true });
5880
- return;
5881
- }
5882
- storagePermissionFs.mkdirSync(dir, { recursive: true, mode: 448 });
5883
- if (!PERMISSIONS_ENFORCEABLE)
5884
- return;
5885
- try {
5886
- storagePermissionFs.chmodSync(dir, 448);
5887
- } catch (error) {
5888
- log(`[magic-context] could not restrict storage dir permissions on ${dir}: ${getErrorMessage(error)}`);
5889
- }
5815
+ ensureStorageDirectorySync(dir);
5890
5816
  }
5891
- function restrictDatabaseFilePermissions(dbPath) {
5892
- if (!PERMISSIONS_ENFORCEABLE || !shouldEnforcePrivateStoragePermissions())
5893
- return;
5894
- for (const suffix of ["", "-wal", "-shm"]) {
5895
- const file = `${dbPath}${suffix}`;
5896
- if (!existsSync3(file))
5897
- continue;
5898
- try {
5899
- storagePermissionFs.chmodSync(file, 384);
5900
- } catch (error) {
5901
- log(`[magic-context] could not restrict DB file permissions on ${file}: ${getErrorMessage(error)}`);
5902
- }
5903
- }
5817
+ function tightenStorageTree(dir) {
5818
+ const { failures } = tightenStorageTreeSync(dir);
5819
+ log(`[magic-context] storage permission tightening failures=${failures}`);
5904
5820
  }
5905
5821
  function resolveBootBusyTimeoutMs(value) {
5906
5822
  if (value === undefined)
@@ -5939,6 +5855,7 @@ function migrateLegacyStorageIfNeeded(targetDbPath, targetDbDir) {
5939
5855
  try {
5940
5856
  const legacyDb = new Database(legacyDbPath);
5941
5857
  try {
5858
+ configureContextDatabasePragmas(legacyDb, true);
5942
5859
  legacyDb.exec("PRAGMA wal_checkpoint(TRUNCATE)");
5943
5860
  } finally {
5944
5861
  closeQuietly(legacyDb);
@@ -5951,7 +5868,7 @@ function migrateLegacyStorageIfNeeded(targetDbPath, targetDbDir) {
5951
5868
  const dst = join(targetDbDir, `context.db${suffix}`);
5952
5869
  if (existsSync3(src)) {
5953
5870
  try {
5954
- copyFileSync(src, dst);
5871
+ copyStorageFileSync(src, dst);
5955
5872
  } catch (error) {
5956
5873
  log(`[magic-context] failed to copy ${src}:`, getErrorMessage(error));
5957
5874
  }
@@ -5961,7 +5878,7 @@ function migrateLegacyStorageIfNeeded(targetDbPath, targetDbDir) {
5961
5878
  const targetModelsDir = join(targetDbDir, "models");
5962
5879
  if (existsSync3(legacyModelsDir) && !existsSync3(targetModelsDir)) {
5963
5880
  try {
5964
- cpSync(legacyModelsDir, targetModelsDir, { recursive: true });
5881
+ copyStorageTreeSync(legacyModelsDir, targetModelsDir);
5965
5882
  } catch (error) {
5966
5883
  log("[magic-context] failed to copy embedding model cache:", getErrorMessage(error));
5967
5884
  }
@@ -6326,8 +6243,8 @@ function finishDatabaseOpen(db, dbPath, explicitDbPath, latestSupportedVersion)
6326
6243
  startMessageFtsRowidMapBackfill(db).then(async () => {
6327
6244
  if (!readsOpenCodeStore)
6328
6245
  return;
6329
- const { readRawSessionMessagePage, readRawSessionMessages } = await importPluginModule(() => import("./read-session-chunk-kmt7a6zj.js"));
6330
- const { startMessageTimeBackfill } = await importPluginModule(() => import("./message-time-backfill-g83dkh8m.js"));
6246
+ const { readRawSessionMessagePage, readRawSessionMessages } = await importPluginModule(() => import("./read-session-chunk-tc6yn2pe.js"));
6247
+ const { startMessageTimeBackfill } = await importPluginModule(() => import("./message-time-backfill-4688z1k2.js"));
6331
6248
  await startMessageTimeBackfill(db, Object.assign(readRawSessionMessages, {
6332
6249
  readPage: readRawSessionMessagePage
6333
6250
  }));
@@ -6350,7 +6267,7 @@ function finishDatabaseOpen(db, dbPath, explicitDbPath, latestSupportedVersion)
6350
6267
  }
6351
6268
  setDatabase(db);
6352
6269
  loadToolDefinitionMeasurements(db);
6353
- restrictDatabaseFilePermissions(dbPath);
6270
+ tightenStorageTree(dirname(dbPath));
6354
6271
  databases.set(dbPath, db);
6355
6272
  pathByDatabase.set(db, dbPath);
6356
6273
  persistenceByDatabase.set(db, true);
@@ -6363,7 +6280,7 @@ function finishDatabaseOpen(db, dbPath, explicitDbPath, latestSupportedVersion)
6363
6280
  function initializeDatabase(db, busyTimeoutMs = BOOT_SQLITE_BUSY_TIMEOUT_MS) {
6364
6281
  db.exec(`PRAGMA busy_timeout=${resolveBootBusyTimeoutMs(busyTimeoutMs)}`);
6365
6282
  db.exec("PRAGMA foreign_keys=ON");
6366
- db.exec("PRAGMA journal_mode=WAL");
6283
+ configureContextDatabasePragmas(db);
6367
6284
  applySqliteTuningPragmas(db);
6368
6285
  db.exec(`
6369
6286
  CREATE TABLE IF NOT EXISTS tags (
@@ -6425,7 +6342,6 @@ function initializeDatabase(db, busyTimeoutMs = BOOT_SQLITE_BUSY_TIMEOUT_MS) {
6425
6342
  harness TEXT NOT NULL DEFAULT 'opencode',
6426
6343
  UNIQUE(session_id, sequence)
6427
6344
  );
6428
- CREATE INDEX IF NOT EXISTS idx_compartments_session ON compartments(session_id);
6429
6345
 
6430
6346
  CREATE TABLE IF NOT EXISTS compartment_chunk_embeddings (
6431
6347
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -6486,7 +6402,6 @@ function initializeDatabase(db, busyTimeoutMs = BOOT_SQLITE_BUSY_TIMEOUT_MS) {
6486
6402
  harness TEXT NOT NULL DEFAULT 'opencode',
6487
6403
  PRIMARY KEY(session_id, message_ordinal)
6488
6404
  );
6489
- CREATE INDEX IF NOT EXISTS idx_compression_depth_session ON compression_depth(session_id);
6490
6405
 
6491
6406
  CREATE TABLE IF NOT EXISTS session_facts (
6492
6407
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -6792,7 +6707,6 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
6792
6707
  stale_reason TEXT,
6793
6708
  PRIMARY KEY (project_path, path)
6794
6709
  );
6795
- CREATE INDEX IF NOT EXISTS idx_project_key_files_project ON project_key_files(project_path);
6796
6710
  CREATE INDEX IF NOT EXISTS idx_project_key_files_generated_at ON project_key_files(project_path, generated_at);
6797
6711
 
6798
6712
  CREATE TABLE IF NOT EXISTS project_key_files_version (
@@ -7177,10 +7091,7 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
7177
7091
  input_tokens INTEGER NOT NULL DEFAULT 0,
7178
7092
  PRIMARY KEY (session_id, harness, message_id)
7179
7093
  );
7180
- CREATE INDEX IF NOT EXISTS idx_transform_decisions_session_harness
7181
- ON transform_decisions(session_id, harness);
7182
7094
 
7183
- CREATE INDEX IF NOT EXISTS idx_tags_session_tag_number ON tags(session_id, tag_number);
7184
7095
  CREATE INDEX IF NOT EXISTS idx_tags_session_message_id ON tags(session_id, message_id);
7185
7096
 
7186
7097
  -- Clone/import paths can write tags before session bootstrap. Keep trigger-created
@@ -7226,9 +7137,7 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
7226
7137
  WHERE NEW.session_id != OLD.session_id
7227
7138
  ON CONFLICT(session_id) DO UPDATE SET tags_version = tags_version + 1;
7228
7139
  END;
7229
- CREATE INDEX IF NOT EXISTS idx_pending_ops_session ON pending_ops(session_id);
7230
7140
  CREATE INDEX IF NOT EXISTS idx_pending_ops_session_tag_id ON pending_ops(session_id, tag_id);
7231
- CREATE INDEX IF NOT EXISTS idx_source_contents_session ON source_contents(session_id);
7232
7141
 
7233
7142
  CREATE TABLE IF NOT EXISTS recomp_compartments (
7234
7143
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -7581,8 +7490,6 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
7581
7490
  input_tokens INTEGER NOT NULL DEFAULT 0,
7582
7491
  PRIMARY KEY (session_id, harness, message_id)
7583
7492
  );
7584
- CREATE INDEX IF NOT EXISTS idx_transform_decisions_session_harness
7585
- ON transform_decisions(session_id, harness);
7586
7493
  `);
7587
7494
  ensureColumn(db, "transform_decisions", "system_hash_prev", "TEXT");
7588
7495
  ensureColumn(db, "transform_decisions", "system_hash_new", "TEXT");
@@ -7604,12 +7511,26 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
7604
7511
  ON message_history_index(harness, session_id, updated_at);
7605
7512
  `);
7606
7513
  ensureColumn(db, "workspaces", "share_categories", `TEXT NOT NULL DEFAULT '["CONSTRAINTS"]'`);
7514
+ const version = getPersistedSchemaVersion(db);
7515
+ if (version === 0 || version >= 95)
7516
+ installV95PerfSchema(db, false, version === 0);
7607
7517
  }
7608
7518
  var CHANNEL2_CLAIM_TTL_MS = 10 * 60000;
7519
+ var channel2HealStatements = new WeakMap;
7609
7520
  function healWedgedChannel2Claims(db) {
7610
7521
  try {
7611
7522
  const staleBefore = Date.now() - CHANNEL2_CLAIM_TTL_MS;
7612
- db.prepare("UPDATE session_meta SET channel2_nudge_state = '', channel2_nudge_claimed_at = 0, channel2_nudge_claim_token = '' WHERE channel2_nudge_state = 'claimed' AND (channel2_nudge_claimed_at IS NULL OR channel2_nudge_claimed_at = 0 OR channel2_nudge_claimed_at <= ?)").run(staleBefore);
7523
+ let statements = channel2HealStatements.get(db);
7524
+ if (!statements) {
7525
+ const predicate = "channel2_nudge_state = 'claimed' AND (channel2_nudge_claimed_at IS NULL OR channel2_nudge_claimed_at = 0 OR channel2_nudge_claimed_at <= ?)";
7526
+ statements = {
7527
+ stale: db.prepare(`SELECT 1 FROM session_meta WHERE ${predicate} LIMIT 1`),
7528
+ heal: db.prepare(`UPDATE session_meta SET channel2_nudge_state = '', channel2_nudge_claimed_at = 0, channel2_nudge_claim_token = '' WHERE ${predicate}`)
7529
+ };
7530
+ channel2HealStatements.set(db, statements);
7531
+ }
7532
+ if (statements.stale.get(staleBefore))
7533
+ statements.heal.run(staleBefore);
7613
7534
  } catch {}
7614
7535
  }
7615
7536
  function openDatabase(dbPathOrOptions) {
@@ -7641,18 +7562,38 @@ function openDatabase(dbPathOrOptions) {
7641
7562
  migrateLegacyStorageIfNeeded(dbPath, dbDir);
7642
7563
  }
7643
7564
  ensureSecureStorageDir(dbDir);
7565
+ tightenStorageTree(dbDir);
7566
+ if (!existsSync3(dbPath) && shouldEnforcePrivateStoragePermissions()) {
7567
+ try {
7568
+ writeStorageFileSync(dbPath, new Uint8Array, { flag: "wx" });
7569
+ } catch (error) {
7570
+ if (error.code !== "EEXIST")
7571
+ throw error;
7572
+ }
7573
+ }
7644
7574
  db = new Database(dbPath);
7645
7575
  installBootBusyTimeout(db, dbPath, busyTimeoutMs, options?.onBootBusyTimeout);
7646
7576
  if (!enforceSchemaFence(db, dbPath, latestSupportedVersion)) {
7647
7577
  closeQuietly(db);
7648
7578
  return null;
7649
7579
  }
7580
+ if (options?.allowMigrations === false && hasPendingMigrations(db)) {
7581
+ log(`[magic-context] storage not ready: pending migrations at ${dbPath}; host callers must await the async opener`);
7582
+ closeQuietly(db);
7583
+ return null;
7584
+ }
7650
7585
  if (!enforceMigrationOnOpenGuard(db, dbPath, dbDir, latestSupportedVersion)) {
7651
7586
  closeQuietly(db);
7652
7587
  return null;
7653
7588
  }
7654
7589
  initializeDatabase(db, busyTimeoutMs);
7655
- runMigrations(db);
7590
+ if (options?.allowMigrations === false) {
7591
+ if (hasPendingMigrations(db)) {
7592
+ closeQuietly(db);
7593
+ return null;
7594
+ }
7595
+ } else
7596
+ runMigrations(db);
7656
7597
  ensureContextStoreUuid(db);
7657
7598
  return finishDatabaseOpen(db, dbPath, explicitDbPath, latestSupportedVersion);
7658
7599
  } catch (error) {
@@ -7663,6 +7604,9 @@ function openDatabase(dbPathOrOptions) {
7663
7604
  throw new Error(`[magic-context] storage unavailable: ${detail}. Magic Context is disabled for this run; check log for details.`);
7664
7605
  }
7665
7606
  }
7607
+ function openCurrentDatabase(options = {}) {
7608
+ return openDatabase({ ...options, allowMigrations: false });
7609
+ }
7666
7610
  async function openDatabaseAsync(dbPathOrOptions) {
7667
7611
  const options = typeof dbPathOrOptions === "string" ? { dbPath: dbPathOrOptions } : dbPathOrOptions;
7668
7612
  const explicitDbPath = options?.dbPath !== undefined;
@@ -7690,8 +7634,12 @@ async function openDatabaseAsync(dbPathOrOptions) {
7690
7634
  const pending = pendingAsyncOpens.get(dbPath);
7691
7635
  if (pending)
7692
7636
  return pending;
7637
+ if (!isFileBackedPath(dbPath)) {
7638
+ throw new Error("async migration requires a file-backed database; use the explicit synchronous opener for in-memory or URI test databases");
7639
+ }
7693
7640
  const opening = (async () => {
7694
7641
  let db;
7642
+ const mainThreadBodiesBefore = getMainThreadMigrationBodyCount();
7695
7643
  const openStartedAt = performance.now();
7696
7644
  let openMs = 0;
7697
7645
  let guardMs = 0;
@@ -7702,6 +7650,15 @@ async function openDatabaseAsync(dbPathOrOptions) {
7702
7650
  if (!explicitDbPath)
7703
7651
  migrateLegacyStorageIfNeeded(dbPath, dbDir);
7704
7652
  ensureSecureStorageDir(dbDir);
7653
+ tightenStorageTree(dbDir);
7654
+ if (!existsSync3(dbPath) && shouldEnforcePrivateStoragePermissions()) {
7655
+ try {
7656
+ writeStorageFileSync(dbPath, new Uint8Array, { flag: "wx" });
7657
+ } catch (error) {
7658
+ if (error.code !== "EEXIST")
7659
+ throw error;
7660
+ }
7661
+ }
7705
7662
  db = new Database(dbPath);
7706
7663
  installBootBusyTimeout(db, dbPath, busyTimeoutMs, options?.onBootBusyTimeout);
7707
7664
  openMs = performance.now() - openStartedAt;
@@ -7719,15 +7676,20 @@ async function openDatabaseAsync(dbPathOrOptions) {
7719
7676
  }
7720
7677
  guardMs = performance.now() - guardStartedAt;
7721
7678
  migrateStartedAt = performance.now();
7722
- if (isFileBackedPath(dbPath) && hasPendingMigrations(db)) {
7679
+ if (hasPendingMigrations(db)) {
7723
7680
  await runMigrationsOffThread({
7724
7681
  dbPath,
7725
7682
  busyTimeoutMs,
7726
7683
  sqlitePragmaConfig: { ...sqlitePragmaConfig }
7727
7684
  });
7728
7685
  }
7686
+ if (hasPendingMigrations(db)) {
7687
+ throw new Error("the migration worker did not complete the pending migration; reinstall or rebuild the plugin");
7688
+ }
7729
7689
  initializeDatabase(db, busyTimeoutMs);
7730
- await runMigrationsWithRetry(db);
7690
+ if (hasPendingMigrations(db)) {
7691
+ throw new Error("storage initialization left a pending migration; reinstall or rebuild the plugin");
7692
+ }
7731
7693
  ensureContextStoreUuid(db);
7732
7694
  const opened = finishDatabaseOpen(db, dbPath, explicitDbPath, latestSupportedVersion);
7733
7695
  migrateMs = performance.now() - migrateStartedAt;
@@ -7739,6 +7701,7 @@ async function openDatabaseAsync(dbPathOrOptions) {
7739
7701
  log(`[magic-context] storage fatal: failed to open ${dbPath}: ${detail}`);
7740
7702
  throw new Error(`[magic-context] storage unavailable: ${detail}. Magic Context is disabled for this run; check log for details.`);
7741
7703
  } finally {
7704
+ log(`[migrations] async open main-thread migration-body count: ${getMainThreadMigrationBodyCount() - mainThreadBodiesBefore} (total=${getMainThreadMigrationBodyCount()}) path=${dbPath}`);
7742
7705
  if (openMs === 0)
7743
7706
  openMs = performance.now() - openStartedAt;
7744
7707
  if (guardStartedAt !== null && guardMs === 0) {
@@ -7771,4 +7734,4 @@ function closeDatabase() {
7771
7734
  }
7772
7735
  }
7773
7736
 
7774
- export { FAIL_CLOSED_DOCTOR_COMMAND, formatFailClosedBlockingMessage, createFailClosedBlockingError, isFailClosedBlockingError, shouldBypassFailClosedBlock, createFailClosedController, setHomeProjectPermission, ProjectIdentityError, resolveProjectIdentityStrict, isUserHomeDirectory, resolveProjectIdentity, directoryHasGitMetadata, isUsableProjectIdentity, resolveProjectIdentityForSession, normalizeStoredProjectPath, storedPathBelongsToIdentity, recordMessageFtsRowid, recordIndexedMessageTime, messageFtsOrdinalRangeIsMapped, deleteUnmappedMessageFtsRows, piHarnessKindFromExecutable, isPidAlive, V2_MEMORY_CATEGORIES, PROMOTABLE_CATEGORIES, CATEGORY_PRIORITY, getMemoryCategoryOrder, CATEGORY_DEFAULT_TTL, unescapeXml, parseCompartmentOutput, layoutLkgPrefix, assembleLkgPrefix, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, resolveStoredPathWorkspaceIdentity, storedPathBelongsToWorkspace, sourceNameForMemory, computeWorkspaceEpochFingerprint, managedAuthorityNoteRow, runMigrationsWithRetry, beginBootQuietPeriod, scheduleAfterBootQuiet, getErrorMessage, describeError, startBootDeadline, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, applySqliteTuningPragmas, runSqliteOptimize, initializeDatabase, openDatabase, openDatabaseAsync, closeDatabase };
7737
+ export { FAIL_CLOSED_DOCTOR_COMMAND, formatFailClosedBlockingMessage, createFailClosedBlockingError, isFailClosedBlockingError, shouldBypassFailClosedBlock, createFailClosedController, setHomeProjectPermission, ProjectIdentityError, resolveProjectIdentityStrict, isUserHomeDirectory, resolveProjectIdentity, directoryHasGitMetadata, isUsableProjectIdentity, resolveProjectIdentityForSession, normalizeStoredProjectPath, storedPathBelongsToIdentity, recordMessageFtsRowid, recordIndexedMessageTime, messageFtsOrdinalRangeIsMapped, deleteUnmappedMessageFtsRows, piHarnessKindFromExecutable, isPidAlive, V2_MEMORY_CATEGORIES, PROMOTABLE_CATEGORIES, CATEGORY_PRIORITY, getMemoryCategoryOrder, CATEGORY_DEFAULT_TTL, unescapeXml, parseCompartmentOutput, layoutLkgPrefix, assembleLkgPrefix, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, resolveStoredPathWorkspaceIdentity, storedPathBelongsToWorkspace, sourceNameForMemory, computeWorkspaceEpochFingerprint, managedAuthorityNoteRow, runMigrationsWithRetry, beginBootQuietPeriod, scheduleAfterBootQuiet, getErrorMessage, describeError, configureContextDatabasePragmas, startBootDeadline, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, applySqliteTuningPragmas, runSqliteOptimize, initializeDatabase, openDatabase, openCurrentDatabase, openDatabaseAsync, closeDatabase };
@@ -272,4 +272,19 @@ function renderToolTemplate(source, input, output) {
272
272
  return truncate(render(bareOutputNodes, root), 1000);
273
273
  }
274
274
 
275
- export { canonicalModelIdentity, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, ompModelRefToCanonical, resolveModelRefForOmp, toolTemplateError, renderToolTemplate };
275
+ // ../plugin/src/shared/protected-tools-policy.ts
276
+ var DEFAULT_PROTECTED_TOOLS = {
277
+ todowrite: 1,
278
+ ctx_reduce: 3
279
+ };
280
+ function normalizeProtectedToolName(name) {
281
+ return (name ?? "").toLowerCase().replace(/^mcp_/, "");
282
+ }
283
+ function mergeProtectedTools(...maps) {
284
+ return Object.fromEntries([DEFAULT_PROTECTED_TOOLS, ...maps].flatMap((map) => Object.entries(map ?? {}).map(([name, count]) => [
285
+ normalizeProtectedToolName(name),
286
+ count
287
+ ])));
288
+ }
289
+
290
+ export { canonicalModelIdentity, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, ompModelRefToCanonical, resolveModelRefForOmp, toolTemplateError, renderToolTemplate, DEFAULT_PROTECTED_TOOLS, normalizeProtectedToolName, mergeProtectedTools };