@cortexkit/pi-magic-context 0.42.0 → 0.42.2

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.
@@ -4165,6 +4165,28 @@ var $ZodEnum = /* @__PURE__ */ $constructor("$ZodEnum", (inst, def) => {
4165
4165
  return payload;
4166
4166
  };
4167
4167
  });
4168
+ var $ZodLiteral = /* @__PURE__ */ $constructor("$ZodLiteral", (inst, def) => {
4169
+ $ZodType.init(inst, def);
4170
+ if (def.values.length === 0) {
4171
+ throw new Error("Cannot create literal schema with no valid values");
4172
+ }
4173
+ const values = new Set(def.values);
4174
+ inst._zod.values = values;
4175
+ inst._zod.pattern = new RegExp(`^(${def.values.map((o) => typeof o === "string" ? escapeRegex(o) : o ? escapeRegex(o.toString()) : String(o)).join("|")})$`);
4176
+ inst._zod.parse = (payload, _ctx) => {
4177
+ const input = payload.value;
4178
+ if (values.has(input)) {
4179
+ return payload;
4180
+ }
4181
+ payload.issues.push({
4182
+ code: "invalid_value",
4183
+ values: def.values,
4184
+ input,
4185
+ inst
4186
+ });
4187
+ return payload;
4188
+ };
4189
+ });
4168
4190
  var $ZodTransform = /* @__PURE__ */ $constructor("$ZodTransform", (inst, def) => {
4169
4191
  $ZodType.init(inst, def);
4170
4192
  inst._zod.optin = "optional";
@@ -5370,6 +5392,44 @@ var enumProcessor = (schema, _ctx, json, _params) => {
5370
5392
  json.type = "string";
5371
5393
  json.enum = values;
5372
5394
  };
5395
+ var literalProcessor = (schema, ctx, json, _params) => {
5396
+ const def = schema._zod.def;
5397
+ const vals = [];
5398
+ for (const val of def.values) {
5399
+ if (val === undefined) {
5400
+ if (ctx.unrepresentable === "throw") {
5401
+ throw new Error("Literal `undefined` cannot be represented in JSON Schema");
5402
+ }
5403
+ } else if (typeof val === "bigint") {
5404
+ if (ctx.unrepresentable === "throw") {
5405
+ throw new Error("BigInt literals cannot be represented in JSON Schema");
5406
+ } else {
5407
+ vals.push(Number(val));
5408
+ }
5409
+ } else {
5410
+ vals.push(val);
5411
+ }
5412
+ }
5413
+ if (vals.length === 0) {} else if (vals.length === 1) {
5414
+ const val = vals[0];
5415
+ json.type = val === null ? "null" : typeof val;
5416
+ if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
5417
+ json.enum = [val];
5418
+ } else {
5419
+ json.const = val;
5420
+ }
5421
+ } else {
5422
+ if (vals.every((v) => typeof v === "number"))
5423
+ json.type = "number";
5424
+ if (vals.every((v) => typeof v === "string"))
5425
+ json.type = "string";
5426
+ if (vals.every((v) => typeof v === "boolean"))
5427
+ json.type = "boolean";
5428
+ if (vals.every((v) => v === null))
5429
+ json.type = "null";
5430
+ json.enum = vals;
5431
+ }
5432
+ };
5373
5433
  var customProcessor = (_schema, ctx, _json, _params) => {
5374
5434
  if (ctx.unrepresentable === "throw") {
5375
5435
  throw new Error("Custom types cannot be represented in JSON Schema");
@@ -6246,6 +6306,27 @@ function _enum(values, params) {
6246
6306
  ...normalizeParams(params)
6247
6307
  });
6248
6308
  }
6309
+ var ZodLiteral = /* @__PURE__ */ $constructor("ZodLiteral", (inst, def) => {
6310
+ $ZodLiteral.init(inst, def);
6311
+ ZodType.init(inst, def);
6312
+ inst._zod.processJSONSchema = (ctx, json, params) => literalProcessor(inst, ctx, json, params);
6313
+ inst.values = new Set(def.values);
6314
+ Object.defineProperty(inst, "value", {
6315
+ get() {
6316
+ if (def.values.length > 1) {
6317
+ throw new Error("This schema contains multiple valid literal values. Use `.values` instead.");
6318
+ }
6319
+ return def.values[0];
6320
+ }
6321
+ });
6322
+ });
6323
+ function literal(value, params) {
6324
+ return new ZodLiteral({
6325
+ type: "literal",
6326
+ values: Array.isArray(value) ? value : [value],
6327
+ ...normalizeParams(params)
6328
+ });
6329
+ }
6249
6330
  var ZodTransform = /* @__PURE__ */ $constructor("ZodTransform", (inst, def) => {
6250
6331
  $ZodTransform.init(inst, def);
6251
6332
  ZodType.init(inst, def);
@@ -7017,6 +7098,8 @@ var BaseEmbeddingConfigSchema = object({
7017
7098
  api_key: string2().optional().describe("API key for remote embedding provider (optional)"),
7018
7099
  input_type: string2().optional().describe("Default input_type for stored/indexed (passage) embeddings in the request body. Required by some openai-compatible providers (e.g. NVIDIA NIM). Omitted from the request when unset."),
7019
7100
  query_input_type: string2().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."),
7101
+ query_instruction: union([string2(), literal(false)]).optional().describe("OpenAI-compatible query prefix override. A string is prepended verbatim to search queries; false disables the built-in model-family instruction. Qwen3-Embedding, gte-Qwen instruct, e5 instruct, and Nomic families have built-in recipes. Query-only changes do not re-embed stored content. User-level only; project values are ignored."),
7102
+ document_prefix: string2().optional().describe("OpenAI-compatible stored-document prefix override, prepended verbatim. Defaults to the model-family recipe (empty for Qwen3/gte/e5 instruct; 'search_document: ' for Nomic). Changing it changes stored vectors and triggers re-embedding. User-level only; project values are ignored."),
7020
7103
  truncate: string2().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."),
7021
7104
  max_input_tokens: number2().int().positive().optional().describe("Optional maximum input tokens for chunk embeddings. Defaults conservatively to 512 when omitted."),
7022
7105
  local_runtime: _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."),
@@ -7068,6 +7151,8 @@ var EmbeddingConfigSchema = BaseEmbeddingConfigSchema.transform((data) => {
7068
7151
  ...apiKey ? { api_key: apiKey } : {},
7069
7152
  ...inputType ? { input_type: inputType } : {},
7070
7153
  ...queryInputType ? { query_input_type: queryInputType } : {},
7154
+ ...data.query_instruction !== undefined ? { query_instruction: data.query_instruction } : {},
7155
+ ...data.document_prefix !== undefined ? { document_prefix: data.document_prefix } : {},
7071
7156
  ...truncate ? { truncate } : {},
7072
7157
  ...data.max_input_tokens ? { max_input_tokens: data.max_input_tokens } : {}
7073
7158
  };
@@ -7093,6 +7178,8 @@ var EmbeddingConfigSchema = BaseEmbeddingConfigSchema.transform((data) => {
7093
7178
  ...apiKey ? { api_key: apiKey } : {},
7094
7179
  ...inputType ? { input_type: inputType } : {},
7095
7180
  ...queryInputType ? { query_input_type: queryInputType } : {},
7181
+ ...data.query_instruction !== undefined ? { query_instruction: data.query_instruction } : {},
7182
+ ...data.document_prefix !== undefined ? { document_prefix: data.document_prefix } : {},
7096
7183
  ...truncate ? { truncate } : {},
7097
7184
  ...data.max_input_tokens ? { max_input_tokens: data.max_input_tokens } : {}
7098
7185
  };
@@ -7226,7 +7313,13 @@ var HARNESS_KEYS = PER_HARNESS_MODEL_KEYS;
7226
7313
  var HISTORIAN_USER_ONLY_FIELDS = PER_HARNESS_MIGRATION_INVENTORY.historian.migrated_execution;
7227
7314
  var PROMPT_SURFACE_USER_ONLY_FIELDS = ["guidance_override_path", "tool_descriptions"];
7228
7315
  var AGENT_ESCALATION_FIELDS = ["prompt", "permission", "tools"];
7229
- var EMBEDDING_DESTINATION_FIELDS = ["endpoint", "provider", "fallback_provider"];
7316
+ var EMBEDDING_USER_ONLY_FIELDS = [
7317
+ "endpoint",
7318
+ "provider",
7319
+ "fallback_provider",
7320
+ "query_instruction",
7321
+ "document_prefix"
7322
+ ];
7230
7323
  var PERCENTAGE_THRESHOLD_REASON = "security: a repository may only raise compaction thresholds above the user's effective value; it cannot force earlier historian work or cloned-repo cost escalation.";
7231
7324
  var TOKEN_THRESHOLD_REASON = "security: a repository may only raise execute_threshold_tokens above the user's trusted token threshold; it cannot force earlier historian work or cloned-repo cost escalation.";
7232
7325
  var TOKEN_THRESHOLD_INTRODUCTION_REASON = "security: a repository cannot introduce a new execute_threshold_tokens override when the user has no trusted token threshold for that key; that could force earlier historian work or cloned-repo cost escalation.";
@@ -7460,14 +7553,14 @@ function stripUnsafeProjectConfigFields(projectRaw) {
7460
7553
  const embedding = projectRaw.embedding;
7461
7554
  if (isPlainObject2(embedding)) {
7462
7555
  const removed = [];
7463
- for (const field of EMBEDDING_DESTINATION_FIELDS) {
7556
+ for (const field of EMBEDDING_USER_ONLY_FIELDS) {
7464
7557
  if (field in embedding) {
7465
7558
  delete embedding[field];
7466
7559
  removed.push(field);
7467
7560
  }
7468
7561
  }
7469
7562
  if (removed.length > 0) {
7470
- warnings.push(`Ignoring embedding.${removed.join("/")} from project config ` + "(security: a repository cannot choose where private text is embedded).");
7563
+ warnings.push(`Ignoring embedding.${removed.join("/")} from project config ` + "(security: a repository cannot choose where or how private text is embedded).");
7471
7564
  }
7472
7565
  }
7473
7566
  for (const agentKey of HIDDEN_AGENT_KEYS) {
@@ -10076,6 +10169,7 @@ function logSlowWriteTransaction(site, startedAt, thresholdMs = SLOW_WRITE_TRANS
10076
10169
 
10077
10170
  // ../plugin/src/features/magic-context/context-authority.ts
10078
10171
  import { createHash as createHash3, randomUUID } from "node:crypto";
10172
+ var AUTHORITY_DOMAINS = ["memories", "notes"];
10079
10173
  var observedAuthorityRoutingByProject = new Map;
10080
10174
  var moduleNoteEvaluationBridges = new Map;
10081
10175
  function getModuleNoteEvaluationBridge(projectPath) {
@@ -10097,7 +10191,788 @@ function ensureContextStoreUuid(db) {
10097
10191
  });
10098
10192
  return getContextStoreUuid(db) ?? minted;
10099
10193
  }
10194
+ function bumpDomainMutationEpoch(db, projectPath, domain) {
10195
+ db.prepare(`INSERT INTO domain_mutation_epoch(project_path, domain, epoch) VALUES (?, ?, 1)
10196
+ ON CONFLICT(project_path, domain) DO UPDATE SET epoch = epoch + 1`).run(projectPath, domain);
10197
+ }
10100
10198
  var MAX_AUTHORITY_SEED_FRAME_BYTES = 900 * 1024;
10199
+ function getMirrorCursor(db, domain) {
10200
+ const row = db.prepare("SELECT cursor FROM mirror_cursors WHERE domain = ?").get(domain);
10201
+ return typeof row?.cursor === "number" ? row.cursor : 0;
10202
+ }
10203
+ function rowNumber(row, key, fallback = 0) {
10204
+ const value = row[key];
10205
+ return typeof value === "number" && Number.isFinite(value) ? value : fallback;
10206
+ }
10207
+ function rowString(row, key, fallback = "") {
10208
+ const value = row[key];
10209
+ return typeof value === "string" ? value : fallback;
10210
+ }
10211
+ function rowNullableString(row, key) {
10212
+ const value = row[key];
10213
+ return typeof value === "string" ? value : null;
10214
+ }
10215
+ var MEMORY_SNAPSHOT_COLUMNS = [
10216
+ "id",
10217
+ "project_path",
10218
+ "category",
10219
+ "content",
10220
+ "normalized_hash",
10221
+ "importance",
10222
+ "scope",
10223
+ "shareable",
10224
+ "source_session_id",
10225
+ "source_type",
10226
+ "seen_count",
10227
+ "retrieval_count",
10228
+ "first_seen_at",
10229
+ "created_at",
10230
+ "updated_at",
10231
+ "last_seen_at",
10232
+ "last_retrieved_at",
10233
+ "status",
10234
+ "expires_at",
10235
+ "verification_status",
10236
+ "verified_at",
10237
+ "classified_at",
10238
+ "superseded_by_memory_id",
10239
+ "merged_from",
10240
+ "metadata_json",
10241
+ "context_store_uuid",
10242
+ "context_row_id",
10243
+ "mural_cue",
10244
+ "mural_cue_hash",
10245
+ "mural_cue_at",
10246
+ "mural_cue_rejection_count"
10247
+ ];
10248
+ var IMMUTABLE_MEMORY_SNAPSHOT_COLUMNS = ["project_path", "first_seen_at", "created_at"];
10249
+ var CLASSIFICATION_MEMORY_SNAPSHOT_COLUMNS = [
10250
+ "importance",
10251
+ "scope",
10252
+ "shareable",
10253
+ "source_type",
10254
+ "classified_at"
10255
+ ];
10256
+ var VERIFICATION_MEMORY_SNAPSHOT_COLUMNS = [
10257
+ "verification_status",
10258
+ "verified_at",
10259
+ "mapping",
10260
+ "mapping_origin"
10261
+ ];
10262
+ var MURAL_MEMORY_SNAPSHOT_COLUMNS = [
10263
+ "mural_cue",
10264
+ "mural_cue_hash",
10265
+ "mural_cue_at",
10266
+ "mural_cue_rejection_count"
10267
+ ];
10268
+ var UPDATED_MEMORY_SNAPSHOT_COLUMNS = [
10269
+ "category",
10270
+ "content",
10271
+ "normalized_hash",
10272
+ "source_session_id",
10273
+ "seen_count",
10274
+ "retrieval_count",
10275
+ "updated_at",
10276
+ "last_seen_at",
10277
+ "last_retrieved_at",
10278
+ "status",
10279
+ "expires_at",
10280
+ "superseded_by_memory_id",
10281
+ "merged_from",
10282
+ "metadata_json"
10283
+ ];
10284
+ function memorySnapshotTimestamp(row, key) {
10285
+ const value = row[key];
10286
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
10287
+ }
10288
+ function parsedMemorySnapshot(snapshotJson, fallback) {
10289
+ try {
10290
+ const parsed = JSON.parse(snapshotJson);
10291
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : fallback;
10292
+ } catch {
10293
+ return fallback;
10294
+ }
10295
+ }
10296
+ function guardMemorySnapshotByRecency(args) {
10297
+ const effectiveRow = { ...args.row };
10298
+ if (!args.existing) {
10299
+ return { effectiveRow, hostUpdatedNewer: false, hostVerificationNewer: false };
10300
+ }
10301
+ const existing = args.existing;
10302
+ const preserve = (columns) => {
10303
+ for (const column of columns) {
10304
+ if (Object.hasOwn(existing, column)) {
10305
+ effectiveRow[column] = existing[column];
10306
+ }
10307
+ }
10308
+ };
10309
+ preserve(IMMUTABLE_MEMORY_SNAPSHOT_COLUMNS);
10310
+ const hostUpdatedNewer = memorySnapshotTimestamp(existing, "updated_at") > memorySnapshotTimestamp(args.snapshot, "updated_at");
10311
+ const hostClassificationNewer = hostUpdatedNewer || memorySnapshotTimestamp(existing, "classified_at") > memorySnapshotTimestamp(args.snapshot, "classified_at");
10312
+ const snapshotCarriesUpdatedAt = hasSnapshotField(args.snapshot, "updated_at");
10313
+ const hostVerificationNewer = hostUpdatedNewer && snapshotCarriesUpdatedAt;
10314
+ const hostMuralNewer = hostUpdatedNewer && snapshotCarriesUpdatedAt;
10315
+ if (hostUpdatedNewer)
10316
+ preserve(UPDATED_MEMORY_SNAPSHOT_COLUMNS);
10317
+ if (hostClassificationNewer)
10318
+ preserve(CLASSIFICATION_MEMORY_SNAPSHOT_COLUMNS);
10319
+ if (hostVerificationNewer)
10320
+ preserve(VERIFICATION_MEMORY_SNAPSHOT_COLUMNS);
10321
+ if (hostMuralNewer)
10322
+ preserve(MURAL_MEMORY_SNAPSHOT_COLUMNS);
10323
+ return { effectiveRow, hostUpdatedNewer, hostVerificationNewer };
10324
+ }
10325
+ function hasSnapshotField(row, key) {
10326
+ return Object.prototype.hasOwnProperty.call(row, key);
10327
+ }
10328
+ function isCompleteMemorySnapshot(row) {
10329
+ return MEMORY_SNAPSHOT_COLUMNS.every((column) => hasSnapshotField(row, column));
10330
+ }
10331
+ function memoryResnapshotState(db) {
10332
+ const row = db.prepare("SELECT status, generation, updated_at FROM mirror_resnapshot_state WHERE domain = 'memories'").get();
10333
+ return row ?? null;
10334
+ }
10335
+ function casMemoryResnapshotState(db, observed, status, generation) {
10336
+ const result = db.prepare(`UPDATE mirror_resnapshot_state
10337
+ SET status = ?, generation = ?, updated_at = ?
10338
+ WHERE domain = 'memories'
10339
+ AND status = ?
10340
+ AND generation IS ?`).run(status, generation, Date.now(), observed.status, observed.generation);
10341
+ return result.changes === 1;
10342
+ }
10343
+ function upgradeMemoryMirrorNeedsResnapshot(db) {
10344
+ const missingLive = db.prepare(`SELECT 1
10345
+ FROM mirror_identity identity
10346
+ WHERE identity.domain = 'memories'
10347
+ AND NOT EXISTS (
10348
+ SELECT 1
10349
+ FROM mirror_live_memory_rows live
10350
+ WHERE live.module_project = identity.module_project
10351
+ AND live.module_row_id = identity.module_row_id
10352
+ )
10353
+ LIMIT 1`).get();
10354
+ if (missingLive)
10355
+ return true;
10356
+ const orphanedLive = db.prepare(`SELECT 1
10357
+ FROM mirror_live_memory_rows live
10358
+ WHERE NOT EXISTS (
10359
+ SELECT 1
10360
+ FROM mirror_identity identity
10361
+ WHERE identity.domain = 'memories'
10362
+ AND identity.module_project = live.module_project
10363
+ AND identity.module_row_id = live.module_row_id
10364
+ )
10365
+ LIMIT 1`).get();
10366
+ return Boolean(orphanedLive);
10367
+ }
10368
+ function stageLiveMemorySnapshotPage(db, generation, rows) {
10369
+ const owned = db.prepare(`SELECT 1
10370
+ FROM mirror_resnapshot_state
10371
+ WHERE domain = 'memories'
10372
+ AND status = 'resnapshotting'
10373
+ AND generation = ?`).get(generation);
10374
+ if (!owned)
10375
+ return false;
10376
+ const insert = db.prepare(`INSERT OR IGNORE INTO mirror_live_staging(
10377
+ generation, module_project, module_row_id, category, normalized_hash, full_row_snapshot
10378
+ ) VALUES (?, ?, ?, ?, ?, ?)`);
10379
+ for (const feed of rows) {
10380
+ const row = feed.full_row_snapshot;
10381
+ const moduleProject = rowString(row, "project_path");
10382
+ const normalizedHash = rowString(row, "normalized_hash");
10383
+ if (!moduleProject || !normalizedHash) {
10384
+ throw new Error("live memory snapshot omitted project_path or normalized_hash");
10385
+ }
10386
+ insert.run(generation, moduleProject, feed.module_row_id, rowString(row, "category", "CONSTRAINTS"), normalizedHash, JSON.stringify(row));
10387
+ }
10388
+ return true;
10389
+ }
10390
+ function installStagedLiveMemorySnapshot(db, generation) {
10391
+ const owned = db.prepare(`SELECT 1
10392
+ FROM mirror_resnapshot_state
10393
+ WHERE domain = 'memories'
10394
+ AND status = 'resnapshotting'
10395
+ AND generation = ?`).get(generation);
10396
+ if (!owned)
10397
+ return false;
10398
+ db.prepare("DELETE FROM mirror_live_memory_rows").run();
10399
+ db.prepare(`INSERT INTO mirror_live_memory_rows(
10400
+ module_project, module_row_id, category, normalized_hash, full_row_snapshot
10401
+ )
10402
+ SELECT module_project, module_row_id, category, normalized_hash, full_row_snapshot
10403
+ FROM mirror_live_staging
10404
+ WHERE generation = ?`).run(generation);
10405
+ const completed = db.prepare(`UPDATE mirror_resnapshot_state
10406
+ SET status = 'complete', updated_at = ?
10407
+ WHERE domain = 'memories'
10408
+ AND status = 'resnapshotting'
10409
+ AND generation = ?`).run(Date.now(), generation);
10410
+ if (completed.changes !== 1) {
10411
+ throw new Error("live memory resnapshot ownership changed inside its install transaction");
10412
+ }
10413
+ db.prepare("DELETE FROM mirror_live_staging WHERE generation = ?").run(generation);
10414
+ return true;
10415
+ }
10416
+ function ensureMemoryRepairState(db) {
10417
+ db.exec(`
10418
+ CREATE TABLE IF NOT EXISTS mirror_memory_repair_state (
10419
+ id INTEGER PRIMARY KEY CHECK(id = 1),
10420
+ dirty INTEGER NOT NULL DEFAULT 0 CHECK(dirty IN (0, 1)),
10421
+ updated_at INTEGER NOT NULL DEFAULT 0
10422
+ );
10423
+ `);
10424
+ db.prepare("INSERT OR IGNORE INTO mirror_memory_repair_state(id, dirty, updated_at) VALUES (1, 0, 0)").run();
10425
+ }
10426
+ function markMemoryRepairPending(db) {
10427
+ ensureMemoryRepairState(db);
10428
+ db.prepare("UPDATE mirror_memory_repair_state SET dirty = 1, updated_at = ? WHERE id = 1").run(Date.now());
10429
+ }
10430
+ function prepareMirrorPageStatements(db) {
10431
+ return {
10432
+ identityByModule: db.prepare("SELECT context_row_id FROM mirror_identity WHERE domain = ? AND module_project = ? AND module_row_id = ?"),
10433
+ insertIdentity: db.prepare("INSERT OR IGNORE INTO mirror_identity(domain, module_project, module_row_id, context_row_id) VALUES (?, ?, ?, ?)"),
10434
+ deleteIdentityByContext: db.prepare("DELETE FROM mirror_identity WHERE domain = ? AND context_row_id = ?"),
10435
+ deleteLiveMemory: db.prepare("DELETE FROM mirror_live_memory_rows WHERE module_project = ? AND module_row_id = ?"),
10436
+ deletePendingReferencesForMemory: db.prepare("DELETE FROM mirror_pending_references WHERE domain = ? AND module_project = ? AND (module_row_id = ? OR target_module_row_id = ?)"),
10437
+ deleteIdentity: db.prepare("DELETE FROM mirror_identity WHERE domain = ? AND module_project = ? AND module_row_id = ?"),
10438
+ sharedIdentity: db.prepare("SELECT 1 FROM mirror_identity WHERE domain = ? AND context_row_id = ? LIMIT 1"),
10439
+ memoryById: db.prepare(`SELECT project_path, category, content, normalized_hash, importance, scope, shareable,
10440
+ source_session_id, source_type, seen_count, retrieval_count, first_seen_at,
10441
+ created_at, updated_at, last_seen_at, last_retrieved_at, status, expires_at,
10442
+ verification_status, verified_at, classified_at, superseded_by_memory_id,
10443
+ merged_from, metadata_json, mural_cue, mural_cue_hash, mural_cue_at,
10444
+ mural_cue_rejection_count
10445
+ FROM memories WHERE id = ?`),
10446
+ memoryIdByStoreId: db.prepare("SELECT id FROM memories WHERE id = ? AND project_path = ?"),
10447
+ memoryCandidates: db.prepare("SELECT id FROM memories WHERE project_path = ? AND category = ? AND normalized_hash = ? ORDER BY id"),
10448
+ insertMemory: db.prepare("INSERT INTO memories (project_path, category, content, normalized_hash, first_seen_at, created_at, updated_at, last_seen_at) VALUES (?, ?, '', '', 0, 0, 0, 0)"),
10449
+ liveMemoryMatches: db.prepare(`SELECT module_project, module_row_id FROM mirror_live_memory_rows
10450
+ WHERE module_project = ? AND category = ? AND normalized_hash = ?
10451
+ ORDER BY module_row_id LIMIT 2`),
10452
+ deleteMemoryEmbeddings: db.prepare("DELETE FROM memory_embeddings WHERE memory_id = ?"),
10453
+ deleteMemory: db.prepare("DELETE FROM memories WHERE id = ?"),
10454
+ liveMemorySnapshot: db.prepare("SELECT full_row_snapshot FROM mirror_live_memory_rows WHERE module_project = ? AND module_row_id = ?"),
10455
+ upsertLiveMemory: db.prepare(`INSERT INTO mirror_live_memory_rows(
10456
+ module_project, module_row_id, category, normalized_hash, full_row_snapshot
10457
+ ) VALUES (?, ?, ?, ?, ?)
10458
+ ON CONFLICT(module_project, module_row_id) DO UPDATE SET
10459
+ category = excluded.category,
10460
+ normalized_hash = excluded.normalized_hash,
10461
+ full_row_snapshot = excluded.full_row_snapshot`),
10462
+ updateMemory: db.prepare(`UPDATE memories SET project_path = ?, category = ?, content = ?, normalized_hash = ?,
10463
+ importance = ?, scope = ?, shareable = ?, source_session_id = ?, source_type = ?,
10464
+ seen_count = ?, retrieval_count = ?, first_seen_at = ?, created_at = ?, updated_at = ?,
10465
+ last_seen_at = ?, last_retrieved_at = ?, status = ?, expires_at = ?,
10466
+ verification_status = ?, verified_at = ?, classified_at = ?, superseded_by_memory_id = ?,
10467
+ merged_from = ?, metadata_json = ?, mural_cue = ?, mural_cue_hash = ?, mural_cue_at = ?,
10468
+ mural_cue_rejection_count = ? WHERE id = ?`),
10469
+ updateSuperseded: db.prepare("UPDATE memories SET superseded_by_memory_id = ?, updated_at = ? WHERE id = ?"),
10470
+ deletePendingReference: db.prepare("DELETE FROM mirror_pending_references WHERE domain = 'memories' AND module_project = ? AND module_row_id = ?"),
10471
+ upsertPendingReference: db.prepare("INSERT INTO mirror_pending_references(domain, module_project, module_row_id, target_module_row_id) VALUES ('memories', ?, ?, ?) ON CONFLICT(domain, module_project, module_row_id) DO UPDATE SET target_module_row_id = excluded.target_module_row_id"),
10472
+ deleteMemoryVerifications: db.prepare("DELETE FROM memory_verifications WHERE memory_id = ?"),
10473
+ insertMemoryVerification: db.prepare("INSERT INTO memory_verifications(memory_id, file_path, verified_at, mapped_at, mapping_origin) VALUES (?, ?, ?, ?, ?)"),
10474
+ noteById: db.prepare("SELECT * FROM notes WHERE id = ?"),
10475
+ noteIdByStoreId: db.prepare("SELECT id FROM notes WHERE id = ? AND type = 'smart' AND project_path = ?"),
10476
+ insertNote: db.prepare("INSERT INTO notes (type, status, content, project_path, session_id, created_at, updated_at) VALUES ('smart', 'active', '', ?, ?, 0, 0)"),
10477
+ deleteNote: db.prepare("DELETE FROM notes WHERE id = ?"),
10478
+ deleteNoteRevisions: db.prepare("DELETE FROM mirror_note_revisions WHERE module_project = ? AND module_row_id = ?"),
10479
+ updateNote: db.prepare(`UPDATE notes SET type = ?, status = ?, project_path = ?, session_id = ?, content = ?,
10480
+ surface_condition = ?, compiled_provider = ?, compiled_config = ?, compiled_at = ?, compile_status = ?,
10481
+ ready_at = ?, ready_reason = ?, manifest_json = ?, compiled_check = ?,
10482
+ check_hash = ?, check_cron = ?, check_failure_count = ?, check_network_failure_count = ?,
10483
+ check_quarantined_until = ?, check_next_due_at = ?, check_compiled_at = ?, check_false_since_at = ?,
10484
+ check_last_liveness_at = ?, last_checked_at = ?, check_status = ?, check_version = ?,
10485
+ policy_version = ?, anchor_block_id = ?, anchor_ordinal = ?, created_at = ?, updated_at = ? WHERE id = ?`),
10486
+ upsertNoteRevision: db.prepare("INSERT OR REPLACE INTO mirror_note_revisions(module_project, module_row_id, context_row_id, status_version) VALUES (?, ?, ?, ?)"),
10487
+ translateMemoryReferences: db.prepare(`UPDATE memories
10488
+ SET superseded_by_memory_id = (
10489
+ SELECT target.context_row_id
10490
+ FROM mirror_pending_references pending
10491
+ JOIN mirror_identity source
10492
+ ON source.domain = pending.domain
10493
+ AND source.module_project = pending.module_project
10494
+ AND source.module_row_id = pending.module_row_id
10495
+ JOIN mirror_identity target
10496
+ ON target.domain = pending.domain
10497
+ AND target.module_project = pending.module_project
10498
+ AND target.module_row_id = pending.target_module_row_id
10499
+ WHERE pending.domain = 'memories'
10500
+ AND source.context_row_id = memories.id
10501
+ ),
10502
+ updated_at = ?
10503
+ WHERE id IN (
10504
+ SELECT source.context_row_id
10505
+ FROM mirror_pending_references pending
10506
+ JOIN mirror_identity source
10507
+ ON source.domain = pending.domain
10508
+ AND source.module_project = pending.module_project
10509
+ AND source.module_row_id = pending.module_row_id
10510
+ JOIN mirror_identity target
10511
+ ON target.domain = pending.domain
10512
+ AND target.module_project = pending.module_project
10513
+ AND target.module_row_id = pending.target_module_row_id
10514
+ WHERE pending.domain = 'memories'
10515
+ )`),
10516
+ clearTranslatedReferences: db.prepare(`DELETE FROM mirror_pending_references
10517
+ WHERE domain = 'memories'
10518
+ AND EXISTS (
10519
+ SELECT 1
10520
+ FROM mirror_identity source
10521
+ JOIN mirror_identity target
10522
+ ON target.domain = source.domain
10523
+ AND target.module_project = source.module_project
10524
+ AND target.module_row_id = mirror_pending_references.target_module_row_id
10525
+ WHERE source.domain = mirror_pending_references.domain
10526
+ AND source.module_project = mirror_pending_references.module_project
10527
+ AND source.module_row_id = mirror_pending_references.module_row_id
10528
+ )`),
10529
+ repairPending: db.prepare("SELECT dirty FROM mirror_memory_repair_state WHERE id = 1"),
10530
+ markRepairPending: db.prepare("UPDATE mirror_memory_repair_state SET dirty = 1, updated_at = ? WHERE id = 1"),
10531
+ clearRepairPending: db.prepare("UPDATE mirror_memory_repair_state SET dirty = 0, updated_at = ? WHERE id = 1"),
10532
+ repairCandidates: db.prepare(`SELECT memory.id, memory.updated_at, memory.classified_at, live.full_row_snapshot
10533
+ FROM memories memory
10534
+ JOIN mirror_identity identity
10535
+ ON identity.domain = 'memories'
10536
+ AND identity.context_row_id = memory.id
10537
+ JOIN mirror_live_memory_rows live
10538
+ ON live.module_project = identity.module_project
10539
+ AND live.module_row_id = identity.module_row_id
10540
+ JOIN authority_managed managed
10541
+ ON managed.project_path = memory.project_path
10542
+ OR managed.project_path = identity.module_project
10543
+ WHERE (memory.source_type IS NULL OR memory.importance IS NULL)
10544
+ AND live.full_row_snapshot IS NOT NULL`),
10545
+ repairMemory: db.prepare(`UPDATE memories
10546
+ SET source_type = COALESCE(source_type, ?),
10547
+ importance = COALESCE(importance, ?),
10548
+ updated_at = ?
10549
+ WHERE id = ?
10550
+ AND COALESCE(updated_at, 0) <= ?
10551
+ AND COALESCE(classified_at, 0) <= ?
10552
+ AND ((source_type IS NULL AND ? IS NOT NULL)
10553
+ OR (importance IS NULL AND ? IS NOT NULL))`),
10554
+ updateCursor: db.prepare("INSERT INTO mirror_cursors(domain, cursor, updated_at) VALUES (?, ?, ?) ON CONFLICT(domain) DO UPDATE SET cursor = excluded.cursor, updated_at = excluded.updated_at"),
10555
+ contextStoreUuid: getContextStoreUuid(db)
10556
+ };
10557
+ }
10558
+ function mirrorIdentity(db, domain, moduleProject, moduleRowId, statements) {
10559
+ return (statements?.identityByModule ?? db.prepare("SELECT context_row_id FROM mirror_identity WHERE domain = ? AND module_project = ? AND module_row_id = ?")).get(domain, moduleProject, moduleRowId) ?? null;
10560
+ }
10561
+ function rememberIdentity(db, domain, moduleProject, moduleRowId, contextRowId, statements, replaceContextIdentity = domain === "notes") {
10562
+ const existing = mirrorIdentity(db, domain, moduleProject, moduleRowId, statements);
10563
+ if (existing?.context_row_id === contextRowId)
10564
+ return;
10565
+ if (existing) {
10566
+ if (!replaceContextIdentity)
10567
+ return;
10568
+ (statements?.deleteIdentity ?? db.prepare("DELETE FROM mirror_identity WHERE domain = ? AND module_project = ? AND module_row_id = ?")).run(domain, moduleProject, moduleRowId);
10569
+ }
10570
+ if (replaceContextIdentity) {
10571
+ (statements?.deleteIdentityByContext ?? db.prepare("DELETE FROM mirror_identity WHERE domain = ? AND context_row_id = ?")).run(domain, contextRowId);
10572
+ }
10573
+ (statements?.insertIdentity ?? db.prepare("INSERT OR IGNORE INTO mirror_identity(domain, module_project, module_row_id, context_row_id) VALUES (?, ?, ?, ?)")).run(domain, moduleProject, moduleRowId, contextRowId);
10574
+ }
10575
+ function contextMemoryId(db, domain, moduleProject, row, moduleRowId, statements) {
10576
+ const mapped = mirrorIdentity(db, domain, moduleProject, moduleRowId, statements);
10577
+ if (mapped)
10578
+ return { contextId: mapped.context_row_id, inserted: false };
10579
+ const sourceUuid = rowNullableString(row, "context_store_uuid");
10580
+ const sourceId = rowNumber(row, "context_row_id", -1);
10581
+ const localStoreUuid = statements?.contextStoreUuid ?? getContextStoreUuid(db);
10582
+ if (sourceUuid && sourceUuid === localStoreUuid && sourceId >= 0) {
10583
+ const existing = (statements?.memoryIdByStoreId ?? db.prepare("SELECT id FROM memories WHERE id = ? AND project_path = ?")).get(sourceId, moduleProject);
10584
+ if (existing?.id !== undefined) {
10585
+ rememberIdentity(db, domain, moduleProject, moduleRowId, existing.id, statements, true);
10586
+ return { contextId: existing.id, inserted: false };
10587
+ }
10588
+ }
10589
+ const normalizedHash = rowString(row, "normalized_hash");
10590
+ const category = rowString(row, "category", "CONSTRAINTS");
10591
+ if (normalizedHash) {
10592
+ const candidates = (statements?.memoryCandidates ?? db.prepare("SELECT id FROM memories WHERE project_path = ? AND category = ? AND normalized_hash = ? ORDER BY id")).all(moduleProject, category, normalizedHash);
10593
+ if (candidates.length === 1 && candidates[0]?.id !== undefined) {
10594
+ rememberIdentity(db, domain, moduleProject, moduleRowId, candidates[0].id, statements);
10595
+ return { contextId: candidates[0].id, inserted: false };
10596
+ }
10597
+ }
10598
+ const result = (statements?.insertMemory ?? db.prepare("INSERT INTO memories (project_path, category, content, normalized_hash, first_seen_at, created_at, updated_at, last_seen_at) VALUES (?, ?, '', '', 0, 0, 0, 0)")).run(moduleProject, rowString(row, "category", "CONSTRAINTS"));
10599
+ const contextId = Number(result.lastInsertRowid);
10600
+ rememberIdentity(db, domain, moduleProject, moduleRowId, contextId, statements);
10601
+ return { contextId, inserted: true };
10602
+ }
10603
+ function applyMemoryRow(db, feed, statements) {
10604
+ const row = feed.full_row_snapshot;
10605
+ const moduleProject = rowString(row, "project_path");
10606
+ if (!moduleProject)
10607
+ throw new Error("memory feed snapshot has no project_path");
10608
+ if (feed.op === "tombstone") {
10609
+ statements.deleteLiveMemory.run(moduleProject, feed.module_row_id);
10610
+ statements.deletePendingReferencesForMemory.run(feed.domain, moduleProject, feed.module_row_id, feed.module_row_id);
10611
+ const mapped = mirrorIdentity(db, feed.domain, moduleProject, feed.module_row_id, statements);
10612
+ if (!mapped)
10613
+ return;
10614
+ statements.deleteIdentity.run(feed.domain, moduleProject, feed.module_row_id);
10615
+ const shared = statements.sharedIdentity.get(feed.domain, mapped.context_row_id);
10616
+ if (shared)
10617
+ return;
10618
+ const contextRow = statements.memoryById.get(mapped.context_row_id);
10619
+ if (contextRow?.project_path && contextRow.category && contextRow.normalized_hash) {
10620
+ const liveMatches = statements.liveMemoryMatches.all(contextRow.project_path, contextRow.category, contextRow.normalized_hash);
10621
+ if (liveMatches.length === 1 && liveMatches[0]) {
10622
+ rememberIdentity(db, feed.domain, liveMatches[0].module_project, liveMatches[0].module_row_id, mapped.context_row_id, statements);
10623
+ return;
10624
+ }
10625
+ }
10626
+ statements.deleteMemoryEmbeddings.run(mapped.context_row_id);
10627
+ statements.deleteMemory.run(mapped.context_row_id);
10628
+ return;
10629
+ }
10630
+ const storedLive = statements.liveMemorySnapshot.get(moduleProject, feed.module_row_id);
10631
+ const snapshotJson = !isCompleteMemorySnapshot(row) && storedLive?.full_row_snapshot ? storedLive.full_row_snapshot : JSON.stringify(row);
10632
+ statements.upsertLiveMemory.run(moduleProject, feed.module_row_id, rowString(row, "category", "CONSTRAINTS"), rowString(row, "normalized_hash"), snapshotJson);
10633
+ const metadataClobberRisk = hasSnapshotField(row, "source_type") && rowNullableString(row, "source_type") === null || hasSnapshotField(row, "importance") && !(typeof row.importance === "number" && Number.isFinite(row.importance));
10634
+ if (metadataClobberRisk)
10635
+ statements.markRepairPending.run(Date.now());
10636
+ const { contextId, inserted } = contextMemoryId(db, feed.domain, moduleProject, row, feed.module_row_id, statements);
10637
+ const existing = statements.memoryById.get(contextId);
10638
+ if (existing && existing.project_path !== moduleProject) {
10639
+ log(`[magic-context] skipping memory mirror update for module ${moduleProject}/${feed.module_row_id}: context row ${contextId} belongs to ${existing.project_path}`);
10640
+ return;
10641
+ }
10642
+ const retainedRecencySnapshot = parsedMemorySnapshot(snapshotJson, row);
10643
+ const recencySnapshot = hasSnapshotField(row, "updated_at") ? row : hasSnapshotField(row, "classified_at") ? { ...retainedRecencySnapshot, classified_at: row.classified_at } : retainedRecencySnapshot;
10644
+ const recencyGuard = guardMemorySnapshotByRecency({
10645
+ row,
10646
+ snapshot: recencySnapshot,
10647
+ existing: inserted ? undefined : existing
10648
+ });
10649
+ const projectedRow = recencyGuard.effectiveRow;
10650
+ const has = (key) => hasSnapshotField(row, key);
10651
+ const nullableNumber = (key, previous) => has(key) ? typeof projectedRow[key] === "number" && Number.isFinite(projectedRow[key]) ? projectedRow[key] : null : previous ?? null;
10652
+ const nullableString = (key, previous) => has(key) ? rowNullableString(projectedRow, key) : previous ?? null;
10653
+ const hasSuperseded = has("superseded_by_memory_id") && !recencyGuard.hostUpdatedNewer;
10654
+ const previousHash = existing?.normalized_hash;
10655
+ statements.updateMemory.run(has("project_path") ? rowString(projectedRow, "project_path") : existing?.project_path ?? moduleProject, has("category") ? rowString(projectedRow, "category", "CONSTRAINTS") : existing?.category ?? "CONSTRAINTS", has("content") ? rowString(projectedRow, "content") : existing?.content ?? "", has("normalized_hash") ? rowString(projectedRow, "normalized_hash") : existing?.normalized_hash ?? "", has("importance") ? typeof projectedRow.importance === "number" && Number.isFinite(projectedRow.importance) ? projectedRow.importance : null : existing?.importance ?? null, has("scope") ? rowString(projectedRow, "scope", "project") : existing?.scope ?? "project", has("shareable") ? rowNumber(projectedRow, "shareable") : existing?.shareable ?? 0, nullableString("source_session_id", existing?.source_session_id), nullableString("source_type", existing?.source_type), has("seen_count") ? rowNumber(projectedRow, "seen_count", 1) : existing?.seen_count ?? 1, has("retrieval_count") ? rowNumber(projectedRow, "retrieval_count") : existing?.retrieval_count ?? 0, has("first_seen_at") ? rowNumber(projectedRow, "first_seen_at") : existing?.first_seen_at ?? 0, has("created_at") ? rowNumber(projectedRow, "created_at") : existing?.created_at ?? 0, has("updated_at") ? rowNumber(projectedRow, "updated_at") : existing?.updated_at ?? 0, has("last_seen_at") ? rowNumber(projectedRow, "last_seen_at") : existing?.last_seen_at ?? 0, nullableNumber("last_retrieved_at", existing?.last_retrieved_at), has("status") ? rowString(projectedRow, "status", "active") : existing?.status ?? "active", nullableNumber("expires_at", existing?.expires_at), has("verification_status") ? rowString(projectedRow, "verification_status", "unverified") : existing?.verification_status ?? "unverified", nullableNumber("verified_at", existing?.verified_at), nullableNumber("classified_at", existing?.classified_at), hasSuperseded ? null : existing?.superseded_by_memory_id ?? null, nullableString("merged_from", existing?.merged_from), nullableString("metadata_json", existing?.metadata_json), nullableString("mural_cue", existing?.mural_cue), nullableString("mural_cue_hash", existing?.mural_cue_hash), nullableNumber("mural_cue_at", existing?.mural_cue_at), has("mural_cue_rejection_count") ? rowNumber(projectedRow, "mural_cue_rejection_count") : existing?.mural_cue_rejection_count ?? 0, contextId);
10656
+ if (hasSuperseded && typeof projectedRow.superseded_by_memory_id === "number") {
10657
+ const translated = mirrorIdentity(db, "memories", moduleProject, projectedRow.superseded_by_memory_id, statements);
10658
+ if (translated) {
10659
+ statements.updateSuperseded.run(translated.context_row_id, Date.now(), contextId);
10660
+ statements.deletePendingReference.run(moduleProject, feed.module_row_id);
10661
+ } else {
10662
+ statements.upsertPendingReference.run(moduleProject, feed.module_row_id, projectedRow.superseded_by_memory_id);
10663
+ }
10664
+ } else if (hasSuperseded) {
10665
+ statements.deletePendingReference.run(moduleProject, feed.module_row_id);
10666
+ }
10667
+ const appliedHash = has("normalized_hash") ? rowString(projectedRow, "normalized_hash") : previousHash;
10668
+ if (previousHash !== appliedHash && appliedHash !== undefined) {
10669
+ statements.deleteMemoryEmbeddings.run(contextId);
10670
+ }
10671
+ if (has("mapping") && !recencyGuard.hostVerificationNewer) {
10672
+ statements.deleteMemoryVerifications.run(contextId);
10673
+ if (Array.isArray(projectedRow.mapping)) {
10674
+ const files = [
10675
+ ...new Set(projectedRow.mapping.filter((file) => typeof file === "string").sort())
10676
+ ];
10677
+ const verifiedAt = rowNumber(projectedRow, "verified_at");
10678
+ const mappedAt = rowNumber(projectedRow, "updated_at", Date.now());
10679
+ const mappingOrigin = projectedRow.mapping_origin === "host_rejected_fallback" ? "host_rejected_fallback" : "mapper";
10680
+ for (const file of files.length > 0 ? files : [""]) {
10681
+ statements.insertMemoryVerification.run(contextId, file, verifiedAt, mappedAt, mappingOrigin);
10682
+ }
10683
+ }
10684
+ }
10685
+ }
10686
+ function repairNullClobberedMemoryRows(statements) {
10687
+ const pending = statements.repairPending.get();
10688
+ if (pending?.dirty !== 1)
10689
+ return;
10690
+ const candidates = statements.repairCandidates.all();
10691
+ for (const candidate of candidates) {
10692
+ if (!candidate.full_row_snapshot)
10693
+ continue;
10694
+ let snapshot;
10695
+ try {
10696
+ const parsed = JSON.parse(candidate.full_row_snapshot);
10697
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
10698
+ continue;
10699
+ snapshot = parsed;
10700
+ } catch {
10701
+ continue;
10702
+ }
10703
+ const sourceType = hasSnapshotField(snapshot, "source_type") && typeof snapshot.source_type === "string" ? snapshot.source_type : null;
10704
+ const importance = hasSnapshotField(snapshot, "importance") && typeof snapshot.importance === "number" && Number.isFinite(snapshot.importance) ? snapshot.importance : null;
10705
+ if (sourceType === null && importance === null)
10706
+ continue;
10707
+ const snapshotVintage = Math.max(memorySnapshotTimestamp(snapshot, "updated_at"), memorySnapshotTimestamp(snapshot, "classified_at"));
10708
+ const hostVintage = Math.max(candidate.updated_at ?? 0, candidate.classified_at ?? 0);
10709
+ if (hostVintage > snapshotVintage)
10710
+ continue;
10711
+ statements.repairMemory.run(sourceType, importance, Date.now(), candidate.id, snapshotVintage, snapshotVintage, sourceType, importance);
10712
+ }
10713
+ statements.clearRepairPending.run(Date.now());
10714
+ }
10715
+ function contextNoteId(db, feed, moduleProject, statements) {
10716
+ const mapped = mirrorIdentity(db, feed.domain, moduleProject, feed.module_row_id, statements);
10717
+ if (mapped)
10718
+ return mapped.context_row_id;
10719
+ const row = feed.full_row_snapshot;
10720
+ const sourceId = rowNumber(row, "context_row_id", -1);
10721
+ const sourceUuid = rowNullableString(row, "context_store_uuid");
10722
+ const localStoreUuid = statements?.contextStoreUuid ?? getContextStoreUuid(db);
10723
+ if (sourceUuid && sourceUuid === localStoreUuid && sourceId >= 0) {
10724
+ const existing = (statements?.noteIdByStoreId ?? db.prepare("SELECT id FROM notes WHERE id = ? AND type = 'smart' AND project_path = ?")).get(sourceId, moduleProject);
10725
+ if (existing?.id !== undefined) {
10726
+ rememberIdentity(db, feed.domain, moduleProject, feed.module_row_id, existing.id, statements);
10727
+ return existing.id;
10728
+ }
10729
+ }
10730
+ const result = (statements?.insertNote ?? db.prepare("INSERT INTO notes (type, status, content, project_path, session_id, created_at, updated_at) VALUES ('smart', 'active', '', ?, ?, 0, 0)")).run(moduleProject, rowNullableString(row, "session_id"));
10731
+ const contextId = Number(result.lastInsertRowid);
10732
+ rememberIdentity(db, feed.domain, moduleProject, feed.module_row_id, contextId, statements);
10733
+ return contextId;
10734
+ }
10735
+ function translateMemoryReferences(statements) {
10736
+ statements.translateMemoryReferences.run(Date.now());
10737
+ statements.clearTranslatedReferences.run();
10738
+ }
10739
+ function applyNoteRow(db, feed, statements) {
10740
+ const row = feed.full_row_snapshot;
10741
+ const moduleProject = rowString(row, "project_path");
10742
+ if (!moduleProject)
10743
+ throw new Error("note feed snapshot has no project_path");
10744
+ if (feed.op === "tombstone") {
10745
+ const mapped = mirrorIdentity(db, feed.domain, moduleProject, feed.module_row_id, statements);
10746
+ if (!mapped)
10747
+ return;
10748
+ statements.deleteNote.run(mapped.context_row_id);
10749
+ statements.deleteIdentity.run(feed.domain, moduleProject, feed.module_row_id);
10750
+ statements.deleteNoteRevisions.run(moduleProject, feed.module_row_id);
10751
+ return;
10752
+ }
10753
+ const contextId = contextNoteId(db, feed, moduleProject, statements);
10754
+ const existing = statements.noteById.get(contextId);
10755
+ const effectiveRow = { ...existing ?? {}, ...row };
10756
+ if (!hasSnapshotField(row, "created_at_ms") && existing?.created_at !== undefined) {
10757
+ effectiveRow.created_at_ms = existing.created_at;
10758
+ }
10759
+ if (!hasSnapshotField(row, "updated_at_ms") && existing?.updated_at !== undefined) {
10760
+ effectiveRow.updated_at_ms = existing.updated_at;
10761
+ }
10762
+ const moduleStatus = rowString(effectiveRow, "status", "active");
10763
+ const contextStatus = moduleStatus === "surfaced" || moduleStatus === "surfacing" ? "ready" : moduleStatus;
10764
+ statements.updateNote.run(rowString(effectiveRow, "type", "smart"), contextStatus, moduleProject, rowNullableString(effectiveRow, "session_id"), rowString(effectiveRow, "content"), rowNullableString(effectiveRow, "surface_condition"), rowNullableString(effectiveRow, "compiled_provider"), rowNullableString(effectiveRow, "compiled_config"), typeof effectiveRow.compiled_at === "number" ? effectiveRow.compiled_at : null, rowNullableString(effectiveRow, "compile_status"), typeof effectiveRow.ready_at === "number" ? effectiveRow.ready_at : null, rowNullableString(effectiveRow, "ready_reason"), rowNullableString(effectiveRow, "manifest_json"), rowNullableString(effectiveRow, "compiled_check"), rowNullableString(effectiveRow, "check_hash"), rowNullableString(effectiveRow, "check_cron"), rowNumber(effectiveRow, "check_failure_count"), rowNumber(effectiveRow, "check_network_failure_count"), typeof effectiveRow.check_quarantined_until === "number" ? effectiveRow.check_quarantined_until : null, typeof effectiveRow.check_next_due_at === "number" ? effectiveRow.check_next_due_at : null, typeof effectiveRow.check_compiled_at === "number" ? effectiveRow.check_compiled_at : null, typeof effectiveRow.check_false_since_at === "number" ? effectiveRow.check_false_since_at : null, typeof effectiveRow.check_last_liveness_at === "number" ? effectiveRow.check_last_liveness_at : null, typeof effectiveRow.last_checked_at === "number" ? effectiveRow.last_checked_at : null, rowString(effectiveRow, "check_status", "uncompiled"), rowNumber(effectiveRow, "check_version"), rowNumber(effectiveRow, "policy_version", 1), rowNullableString(effectiveRow, "anchor_block_id"), typeof effectiveRow.anchor_ordinal === "number" ? effectiveRow.anchor_ordinal : null, rowNumber(effectiveRow, "created_at_ms"), rowNumber(effectiveRow, "updated_at_ms"), contextId);
10765
+ statements.upsertNoteRevision.run(moduleProject, feed.module_row_id, contextId, rowNumber(effectiveRow, "status_version"));
10766
+ }
10767
+ function applyMirrorPage(args) {
10768
+ const { db, page } = args;
10769
+ if (!AUTHORITY_DOMAINS.includes(page.domain))
10770
+ throw new Error("unknown mirror domain");
10771
+ const durableCursor = getMirrorCursor(db, page.domain);
10772
+ if (page.cursor !== durableCursor) {
10773
+ throw new Error(`mirror cursor mismatch for ${page.domain}: expected ${durableCursor}, got ${page.cursor}`);
10774
+ }
10775
+ if (page.next_cursor < durableCursor) {
10776
+ throw new Error("mirror page moved its cursor backwards");
10777
+ }
10778
+ const resnapshotState = page.domain === "memories" ? memoryResnapshotState(db) : null;
10779
+ const hasNewRows = page.rows.some((feed) => feed.domain === page.domain && feed.feed_seq > durableCursor);
10780
+ const replayMustRun = page.domain === "memories" && resnapshotState !== null && resnapshotState.status !== "complete" && (durableCursor === 0 || page.rows.some((feed) => feed.op === "tombstone"));
10781
+ if (!hasNewRows && !replayMustRun) {
10782
+ if (page.next_cursor <= durableCursor)
10783
+ return durableCursor;
10784
+ withPrivilegedWriter(db, () => {
10785
+ db.prepare("INSERT INTO mirror_cursors(domain, cursor, updated_at) VALUES (?, ?, ?) ON CONFLICT(domain) DO UPDATE SET cursor = excluded.cursor, updated_at = excluded.updated_at").run(page.domain, page.next_cursor, Date.now());
10786
+ });
10787
+ return page.next_cursor;
10788
+ }
10789
+ let nextCursor = durableCursor;
10790
+ withPrivilegedWriter(db, () => {
10791
+ db.transaction(() => {
10792
+ ensureMemoryRepairState(db);
10793
+ const statements = prepareMirrorPageStatements(db);
10794
+ const currentResnapshotState = page.domain === "memories" ? memoryResnapshotState(db) : null;
10795
+ let resnapshotStatus = page.domain === "memories" ? currentResnapshotState?.status ?? null : "complete";
10796
+ if (page.domain === "memories" && durableCursor === 0 && currentResnapshotState && resnapshotStatus !== "complete") {
10797
+ statements.markRepairPending.run(Date.now());
10798
+ if (!casMemoryResnapshotState(db, currentResnapshotState, "complete", currentResnapshotState.generation)) {
10799
+ throw new Error("memory mirror resnapshot ownership changed during replay");
10800
+ }
10801
+ resnapshotStatus = "complete";
10802
+ }
10803
+ if (page.domain === "memories" && resnapshotStatus !== "complete" && page.rows.some((feed) => feed.op === "tombstone")) {
10804
+ throw new Error("memory mirror resnapshot must complete before tombstones");
10805
+ }
10806
+ if (page.domain === "memories") {
10807
+ for (const feed of page.rows) {
10808
+ if (feed.domain !== "memories" || feed.op === "tombstone" || feed.feed_seq <= durableCursor) {
10809
+ continue;
10810
+ }
10811
+ const row = feed.full_row_snapshot;
10812
+ const moduleProject = rowString(row, "project_path");
10813
+ const sourceUuid = rowNullableString(row, "context_store_uuid");
10814
+ const sourceId = rowNumber(row, "context_row_id", -1);
10815
+ if (!moduleProject || !sourceUuid || sourceUuid !== statements.contextStoreUuid || sourceId < 0) {
10816
+ continue;
10817
+ }
10818
+ const existing = statements.memoryIdByStoreId.get(sourceId, moduleProject);
10819
+ if (existing?.id === undefined)
10820
+ continue;
10821
+ rememberIdentity(db, "memories", moduleProject, feed.module_row_id, existing.id, statements, true);
10822
+ }
10823
+ }
10824
+ const touchedProjects = new Set;
10825
+ for (const feed of page.rows) {
10826
+ if (feed.domain !== page.domain || feed.feed_seq <= nextCursor)
10827
+ continue;
10828
+ const projectPath = rowString(feed.full_row_snapshot, "project_path");
10829
+ if (projectPath)
10830
+ touchedProjects.add(projectPath);
10831
+ if (feed.domain === "memories")
10832
+ applyMemoryRow(db, feed, statements);
10833
+ else
10834
+ applyNoteRow(db, feed, statements);
10835
+ nextCursor = feed.feed_seq;
10836
+ }
10837
+ if (page.domain === "memories") {
10838
+ translateMemoryReferences(statements);
10839
+ repairNullClobberedMemoryRows(statements);
10840
+ }
10841
+ for (const projectPath of touchedProjects) {
10842
+ bumpDomainMutationEpoch(db, projectPath, page.domain);
10843
+ }
10844
+ if (page.next_cursor < nextCursor) {
10845
+ throw new Error("mirror page moved its cursor backwards");
10846
+ }
10847
+ nextCursor = Math.max(nextCursor, page.next_cursor);
10848
+ statements.updateCursor.run(page.domain, nextCursor, Date.now());
10849
+ }).immediate();
10850
+ });
10851
+ return nextCursor;
10852
+ }
10853
+ async function ensureLiveMemoryResnapshot(args) {
10854
+ let adoptedWinner = false;
10855
+ let generation;
10856
+ while (true) {
10857
+ const observed = memoryResnapshotState(args.db);
10858
+ if (!observed || observed.status === "complete")
10859
+ return;
10860
+ if (observed.status === "resnapshotting") {
10861
+ withPrivilegedWriter(args.db, () => markMemoryRepairPending(args.db));
10862
+ }
10863
+ if (observed.status === "pending_check" && !upgradeMemoryMirrorNeedsResnapshot(args.db)) {
10864
+ let completed = false;
10865
+ withPrivilegedWriter(args.db, () => {
10866
+ args.db.transaction(() => {
10867
+ markMemoryRepairPending(args.db);
10868
+ completed = casMemoryResnapshotState(args.db, observed, "complete", observed.generation);
10869
+ if (completed) {
10870
+ args.db.prepare("DELETE FROM mirror_live_staging WHERE generation IS NOT ?").run(observed.generation);
10871
+ }
10872
+ }).immediate();
10873
+ });
10874
+ if (completed)
10875
+ return;
10876
+ continue;
10877
+ }
10878
+ if (!args.module.mirrorPull) {
10879
+ throw new Error("memory mirror resnapshot requires the mirror.pull module route");
10880
+ }
10881
+ const now = Date.now();
10882
+ generation = `${now.toString(36)}:${crypto.randomUUID()}`;
10883
+ let claimed = false;
10884
+ withPrivilegedWriter(args.db, () => {
10885
+ args.db.transaction(() => {
10886
+ claimed = casMemoryResnapshotState(args.db, observed, "resnapshotting", generation);
10887
+ if (claimed) {
10888
+ markMemoryRepairPending(args.db);
10889
+ args.db.prepare("DELETE FROM mirror_live_staging WHERE generation != ?").run(generation);
10890
+ }
10891
+ }).immediate();
10892
+ });
10893
+ if (claimed)
10894
+ break;
10895
+ const winner = memoryResnapshotState(args.db);
10896
+ if (!adoptedWinner && winner?.status === "resnapshotting" && typeof winner.generation === "string" && winner.generation.length > 0) {
10897
+ generation = winner.generation;
10898
+ adoptedWinner = true;
10899
+ break;
10900
+ }
10901
+ }
10902
+ let cursor = 0;
10903
+ while (true) {
10904
+ const response = await args.module.mirrorPull({
10905
+ domain: "memories",
10906
+ cursor,
10907
+ limit: args.limit,
10908
+ live_only: true
10909
+ });
10910
+ const page = response.page;
10911
+ if (page.domain !== "memories" || page.cursor !== cursor) {
10912
+ throw new Error("live memory resnapshot returned a mismatched page");
10913
+ }
10914
+ let staged = false;
10915
+ withPrivilegedWriter(args.db, () => {
10916
+ args.db.transaction(() => {
10917
+ staged = stageLiveMemorySnapshotPage(args.db, generation, page.rows);
10918
+ }).immediate();
10919
+ });
10920
+ if (!staged)
10921
+ return;
10922
+ if (!page.has_more)
10923
+ break;
10924
+ if (page.next_cursor <= cursor) {
10925
+ throw new Error("live memory resnapshot did not advance its cursor");
10926
+ }
10927
+ cursor = page.next_cursor;
10928
+ }
10929
+ withPrivilegedWriter(args.db, () => {
10930
+ args.db.transaction(() => {
10931
+ installStagedLiveMemorySnapshot(args.db, generation);
10932
+ }).immediate();
10933
+ });
10934
+ }
10935
+ async function pullAndApplyMirrorPageWithStatus(args) {
10936
+ if (!args.module.mirrorPull) {
10937
+ throw new Error("memory mirror consumer requires the mirror.pull module route");
10938
+ }
10939
+ const limit = Math.max(1, Math.min(args.limit ?? 100, 1000));
10940
+ if (args.domain === "memories") {
10941
+ await ensureLiveMemoryResnapshot({ db: args.db, module: args.module, limit });
10942
+ }
10943
+ const cursor = getMirrorCursor(args.db, args.domain);
10944
+ const response = await args.module.mirrorPull({
10945
+ domain: args.domain,
10946
+ cursor,
10947
+ limit
10948
+ });
10949
+ const nextCursor = applyMirrorPage({ db: args.db, page: response.page });
10950
+ return {
10951
+ cursor: nextCursor,
10952
+ hasMore: response.page.has_more,
10953
+ rowsApplied: response.page.rows.filter((row) => row.domain === args.domain && row.feed_seq > cursor).length
10954
+ };
10955
+ }
10956
+ async function drainMirrorPages(args) {
10957
+ const pageBudget = args.pageBudget === undefined ? Number.MAX_SAFE_INTEGER : Math.max(1, Math.floor(args.pageBudget));
10958
+ let pagesPulled = 0;
10959
+ let rowsApplied = 0;
10960
+ let cursor = getMirrorCursor(args.db, args.domain);
10961
+ while (pagesPulled < pageBudget) {
10962
+ const priorCursor = cursor;
10963
+ const page = await pullAndApplyMirrorPageWithStatus(args);
10964
+ pagesPulled += 1;
10965
+ rowsApplied += page.rowsApplied;
10966
+ cursor = page.cursor;
10967
+ if (!page.hasMore) {
10968
+ return { cursor, pagesPulled, rowsApplied, complete: true, budgetExhausted: false };
10969
+ }
10970
+ if (cursor === priorCursor) {
10971
+ return { cursor, pagesPulled, rowsApplied, complete: false, budgetExhausted: false };
10972
+ }
10973
+ }
10974
+ return { cursor, pagesPulled, rowsApplied, complete: false, budgetExhausted: true };
10975
+ }
10101
10976
  var mirrorFlights = new WeakMap;
10102
10977
 
10103
10978
  // ../plugin/src/features/magic-context/message-fts-rowid-map.ts
@@ -13272,29 +14147,6 @@ function resolveOpenCodeDbPath() {
13272
14147
  function openCodeDbPathExists(resolution = resolveOpenCodeDbPath()) {
13273
14148
  return resolution.path !== ":memory:" && existsSync5(resolution.path);
13274
14149
  }
13275
- function getOpenCodeDbProbeDescriptions(resolution = resolveOpenCodeDbPath()) {
13276
- if (resolution.source === "OPENCODE_DB")
13277
- return [resolution.path];
13278
- if (resolution.source === "channel" || process.env.OPENCODE_DISABLE_CHANNEL_DB === "1" || process.env.OPENCODE_DISABLE_CHANNEL_DB === "true") {
13279
- return [resolution.path];
13280
- }
13281
- const dataDir = openCodeDataDir();
13282
- return [
13283
- join4(dataDir, "opencode.db"),
13284
- join4(dataDir, "opencode-local.db"),
13285
- join4(dataDir, "opencode-dev.db"),
13286
- join4(dataDir, "opencode-<channel>.db")
13287
- ];
13288
- }
13289
- function lookedForText(resolution) {
13290
- return getOpenCodeDbProbeDescriptions(resolution).join(", ");
13291
- }
13292
- function formatOpenCodeDbMissingStatusLine(resolution = resolveOpenCodeDbPath()) {
13293
- return `OpenCode DB: MISSING (looked for ${lookedForText(resolution)}). History compaction (historian) and the mid-turn valve are disabled; set OPENCODE_DB if OpenCode stores it elsewhere.`;
13294
- }
13295
- function formatOpenCodeDbReadFailureStatusLine(failure) {
13296
- return `OpenCode DB: READ FAILED (path=${failure.path}, source=${failure.source}) — ${failure.message}`;
13297
- }
13298
14150
  function recordOpenCodeDbReadFailure(resolution, error) {
13299
14151
  const message = error instanceof Error ? error.message : String(error);
13300
14152
  lastReadFailure = { ...resolution, message };
@@ -13304,9 +14156,6 @@ function clearOpenCodeDbReadFailure(path) {
13304
14156
  if (path === undefined || lastReadFailure?.path === path)
13305
14157
  lastReadFailure = null;
13306
14158
  }
13307
- function getOpenCodeDbReadFailure() {
13308
- return lastReadFailure;
13309
- }
13310
14159
  function claimOpenCodeDbDiagnosticOnce(surface, resolution) {
13311
14160
  const key = `${surface}\x00${resolution.path}\x00${resolution.source}`;
13312
14161
  if (claimedDiagnostics.has(key))
@@ -18406,9 +19255,21 @@ function getPersistedCompactionMarkerState(db, sessionId) {
18406
19255
  return parsePersistedCompactionMarkerState(row?.compaction_marker_state, row?.compaction_marker_target_end_message_id);
18407
19256
  }
18408
19257
  function setPersistedCompactionMarkerState(db, sessionId, state) {
18409
- ensureSessionMetaRow(db, sessionId);
18410
- const json = state ? JSON.stringify(state) : "";
18411
- db.prepare("UPDATE session_meta SET compaction_marker_state = ?, compaction_marker_target_end_message_id = ? WHERE session_id = ?").run(json, state?.targetEndMessageId ?? null, sessionId);
19258
+ db.transaction(() => {
19259
+ ensureSessionMetaRow(db, sessionId);
19260
+ const previous = state === null ? getPersistedCompactionMarkerState(db, sessionId) ?? getDeferredClearedCompactionMarkerState(db, sessionId) : null;
19261
+ const json = state ? JSON.stringify(state) : previous ? JSON.stringify({ deferredClear: previous }) : "";
19262
+ db.prepare("UPDATE session_meta SET compaction_marker_state = ?, compaction_marker_target_end_message_id = ? WHERE session_id = ?").run(json, state?.targetEndMessageId ?? null, sessionId);
19263
+ })();
19264
+ }
19265
+ function getDeferredClearedCompactionMarkerState(db, sessionId) {
19266
+ const row = db.prepare("SELECT compaction_marker_state FROM session_meta WHERE session_id = ?").get(sessionId);
19267
+ try {
19268
+ const parsed = JSON.parse(row?.compaction_marker_state || "null");
19269
+ return parsePersistedCompactionMarkerState(JSON.stringify(parsed?.deferredClear));
19270
+ } catch {
19271
+ return null;
19272
+ }
18412
19273
  }
18413
19274
  var MAX_STRIPPED_PLACEHOLDER_IDS = 4096;
18414
19275
  function parseStrippedPlaceholderState(raw) {
@@ -21164,29 +22025,35 @@ function getPiContentDecisions(db, sessionId) {
21164
22025
  return new Set(readEntries(row?.decisions ?? null).filter((value) => decodePiContentDecision(value) !== null));
21165
22026
  }
21166
22027
  function freezePiContentDecision(db, sessionId, kind, messageId) {
21167
- ensureSessionMetaRow(db, sessionId);
21168
- const entry = encodePiContentDecision(kind, messageId);
21169
- return db.transaction(() => {
21170
- for (let attempt = 0;attempt < 5; attempt++) {
21171
- const row = db.prepare("SELECT merged_reasoning_stripped_ids AS decisions FROM session_meta WHERE session_id = ?").get(sessionId);
21172
- const current = readEntries(row.decisions);
21173
- if (current.includes(entry))
21174
- return true;
21175
- const ownsTag = db.prepare("SELECT 1 FROM tags WHERE session_id = ? AND message_id = ? LIMIT 1");
21176
- const ownsMessage = db.prepare("SELECT 1 FROM tags WHERE session_id = ? AND message_id >= ? AND message_id < ? LIMIT 1");
21177
- const kept = current.filter((value) => {
21178
- const decision = decodePiContentDecision(value);
21179
- return !decision || !!(decision[0] === "seam-temporal-strip" ? ownsMessage.get(sessionId, `${decision[1]}:p`, `${decision[1]}:q`) : ownsTag.get(sessionId, decision[1]));
21180
- });
21181
- if (kept.filter((value) => decodePiContentDecision(value) !== null).length >= PI_CONTENT_DECISION_LIMIT)
21182
- return false;
21183
- kept.push(entry);
21184
- const result = db.prepare("UPDATE session_meta SET merged_reasoning_stripped_ids = ? WHERE session_id = ? AND merged_reasoning_stripped_ids IS ?").run(JSON.stringify(kept), sessionId, row.decisions);
21185
- if (result.changes > 0)
21186
- return true;
21187
- }
21188
- return false;
21189
- })();
22028
+ try {
22029
+ ensureSessionMetaRow(db, sessionId);
22030
+ const entry = encodePiContentDecision(kind, messageId);
22031
+ return db.transaction(() => {
22032
+ for (let attempt = 0;attempt < 5; attempt++) {
22033
+ const row = db.prepare("SELECT merged_reasoning_stripped_ids AS decisions FROM session_meta WHERE session_id = ?").get(sessionId);
22034
+ const current = readEntries(row.decisions);
22035
+ if (current.includes(entry))
22036
+ return true;
22037
+ const ownsTag = db.prepare("SELECT 1 FROM tags WHERE session_id = ? AND message_id = ? LIMIT 1");
22038
+ const ownsMessage = db.prepare("SELECT 1 FROM tags WHERE session_id = ? AND message_id >= ? AND message_id < ? LIMIT 1");
22039
+ const kept = current.filter((value) => {
22040
+ const decision = decodePiContentDecision(value);
22041
+ return !decision || !!(decision[0] === "seam-temporal-strip" ? ownsMessage.get(sessionId, `${decision[1]}:p`, `${decision[1]}:q`) : ownsTag.get(sessionId, decision[1]));
22042
+ });
22043
+ if (kept.filter((value) => decodePiContentDecision(value) !== null).length >= PI_CONTENT_DECISION_LIMIT)
22044
+ return false;
22045
+ kept.push(entry);
22046
+ const result = db.prepare("UPDATE session_meta SET merged_reasoning_stripped_ids = ? WHERE session_id = ? AND merged_reasoning_stripped_ids IS ?").run(JSON.stringify(kept), sessionId, row.decisions);
22047
+ if (result.changes > 0)
22048
+ return true;
22049
+ }
22050
+ return false;
22051
+ }).immediate();
22052
+ } catch (error) {
22053
+ if (/database (?:table )?is locked|sqlite_(busy|locked)/i.test(String(error)))
22054
+ return false;
22055
+ throw error;
22056
+ }
21190
22057
  }
21191
22058
 
21192
22059
  // ../plugin/src/features/magic-context/storage-native-replay.ts
@@ -26846,7 +27713,8 @@ function getSynapseBatchRequestKey(args) {
26846
27713
  allow_equivalent: false,
26847
27714
  accept_declared: false,
26848
27715
  ids: args.items.map((item) => item.id),
26849
- content_sha256: args.items.map((item) => item.contentSha256)
27716
+ content_sha256: args.items.map((item) => item.contentSha256),
27717
+ ...args.purpose === "query" ? { purpose: args.purpose } : {}
26850
27718
  }));
26851
27719
  }
26852
27720
  function hashContent(text) {
@@ -27107,7 +27975,7 @@ class SynapseEmbeddingProvider {
27107
27975
  })();
27108
27976
  return this.initializing;
27109
27977
  }
27110
- async embed(text, signal) {
27978
+ async embed(text, signal, purpose = "passage") {
27111
27979
  if (!await this.initialize() || signal?.aborted || !this.metadata)
27112
27980
  return null;
27113
27981
  if (estimateTokens(text) > this.metadata.max_tokens)
@@ -27117,6 +27985,7 @@ class SynapseEmbeddingProvider {
27117
27985
  const value = await this.callWithRetry("embed.query", this.requestConstraints({
27118
27986
  id,
27119
27987
  text,
27988
+ purpose,
27120
27989
  deadline_ms: this.options.queryTimeoutMs ?? SYNAPSE_DEFAULT_QUERY_TIMEOUT_MS
27121
27990
  }), this.options.queryTimeoutMs ?? SYNAPSE_DEFAULT_QUERY_TIMEOUT_MS, true, signal);
27122
27991
  const extracted = extractVector(value);
@@ -27136,7 +28005,7 @@ class SynapseEmbeddingProvider {
27136
28005
  return null;
27137
28006
  }
27138
28007
  }
27139
- async embedBatch(texts, signal) {
28008
+ async embedBatch(texts, signal, purpose = "passage") {
27140
28009
  if (texts.length === 0)
27141
28010
  return [];
27142
28011
  const items = texts.map((text, index) => ({
@@ -27144,10 +28013,10 @@ class SynapseEmbeddingProvider {
27144
28013
  text,
27145
28014
  contentSha256: hashContent(text)
27146
28015
  }));
27147
- const map = await this.embedItems(items, signal);
28016
+ const map = await this.embedItems(items, signal, purpose);
27148
28017
  return items.map((item) => map.get(item.id) ?? null);
27149
28018
  }
27150
- async embedItems(items, signal) {
28019
+ async embedItems(items, signal, purpose = "passage") {
27151
28020
  const output = new Map;
27152
28021
  if (items.length === 0 || !await this.initialize() || !this.metadata || signal?.aborted) {
27153
28022
  return output;
@@ -27160,12 +28029,12 @@ class SynapseEmbeddingProvider {
27160
28029
  const page = this.nextPage(eligibleItems, start);
27161
28030
  start += page.length;
27162
28031
  try {
27163
- const requestKey = this.requestKey(page);
28032
+ const requestKey = this.requestKey(page, purpose);
27164
28033
  let body = {};
27165
28034
  let restarted = false;
27166
28035
  for (;; ) {
27167
28036
  try {
27168
- body = await this.callWithRetry("embed.batch", this.batchRequest(page, requestKey), this.options.batchTimeoutMs ?? SYNAPSE_DEFAULT_BATCH_TIMEOUT_MS, true, signal);
28037
+ body = await this.callWithRetry("embed.batch", this.batchRequest(page, requestKey, purpose), this.options.batchTimeoutMs ?? SYNAPSE_DEFAULT_BATCH_TIMEOUT_MS, true, signal);
27169
28038
  const first = responseBody(body);
27170
28039
  const jobId = typeof first.job_id === "string" ? first.job_id : null;
27171
28040
  if (jobId)
@@ -27235,24 +28104,26 @@ class SynapseEmbeddingProvider {
27235
28104
  accept_declared: false
27236
28105
  };
27237
28106
  }
27238
- batchRequest(items, requestKey) {
28107
+ batchRequest(items, requestKey, purpose) {
27239
28108
  return this.requestConstraints({
27240
28109
  items: items.map((item) => ({
27241
28110
  id: item.id,
27242
28111
  text: item.text,
27243
28112
  content_sha256: item.contentSha256
27244
28113
  })),
27245
- request_key: requestKey
28114
+ request_key: requestKey,
28115
+ purpose
27246
28116
  });
27247
28117
  }
27248
- requestKey(items) {
28118
+ requestKey(items, purpose) {
27249
28119
  if (!this.metadata)
27250
28120
  throw new SynapseEmbeddingError("transport", "Synapse metadata is unavailable");
27251
28121
  return getSynapseBatchRequestKey({
27252
28122
  model: this.metadata.model,
27253
28123
  fingerprint: this.metadata.fingerprint,
27254
28124
  tableEpoch: this.metadata.table_epoch,
27255
- items
28125
+ items,
28126
+ purpose
27256
28127
  });
27257
28128
  }
27258
28129
  async pollBatch(jobId, requestKey, signal) {
@@ -27462,6 +28333,7 @@ var existingHashStatements = new WeakMap;
27462
28333
  var existingHashByProjectStatements = new WeakMap;
27463
28334
  var deleteByCompartmentStatements = new WeakMap;
27464
28335
  var insertEmbeddingStatements = new WeakMap;
28336
+ var renumberEmbeddingWindowStatements = new WeakMap;
27465
28337
  var searchRowsStatements = new WeakMap;
27466
28338
  var searchRowsByModelStatements = new WeakMap;
27467
28339
  var searchPoolProbeStatements = new WeakMap;
@@ -27512,6 +28384,19 @@ function getInsertEmbeddingStatement(db) {
27512
28384
  }
27513
28385
  return stmt;
27514
28386
  }
28387
+ function getRenumberEmbeddingWindowStatement(db) {
28388
+ let stmt = renumberEmbeddingWindowStatements.get(db);
28389
+ if (!stmt) {
28390
+ stmt = db.prepare(`UPDATE compartment_chunk_embeddings
28391
+ SET window_index = ?
28392
+ WHERE compartment_id = ?
28393
+ AND model_id = ?
28394
+ AND project_path = ?
28395
+ AND window_index = ?`);
28396
+ renumberEmbeddingWindowStatements.set(db, stmt);
28397
+ }
28398
+ return stmt;
28399
+ }
27515
28400
  function getSearchPoolProbeStatement(db) {
27516
28401
  let stmt = searchPoolProbeStatements.get(db);
27517
28402
  if (!stmt) {
@@ -27673,6 +28558,11 @@ function parseCanonicalLineRange(line) {
27673
28558
  return null;
27674
28559
  return { start, end };
27675
28560
  }
28561
+ function assertOrdinalRangeWithinCompartment(rangeStart, rangeEnd, compartmentStart, compartmentEnd) {
28562
+ if (rangeStart < compartmentStart || rangeEnd > compartmentEnd || rangeEnd < rangeStart) {
28563
+ throw new RangeError(`Canonical chunk range ${rangeStart}-${rangeEnd} lies outside compartment ${compartmentStart}-${compartmentEnd}`);
28564
+ }
28565
+ }
27676
28566
  function hashChunkText(text) {
27677
28567
  return createHash10("sha256").update(text).digest("hex");
27678
28568
  }
@@ -27761,6 +28651,10 @@ function canonicalizeInMemoryChunkTextForEmbedding(chunkText, startOrdinal, endO
27761
28651
  lines.push(`${formatOrdinalRange(retainedStart, retainedEnd)} ${roleLabel} ${retained.map(({ part }) => part).join(" / ")}`);
27762
28652
  continue;
27763
28653
  }
28654
+ const clipsRequestedRange = startOrdinal != null && lineStart < startOrdinal || endOrdinal != null && lineEnd > endOrdinal;
28655
+ if (clipsRequestedRange) {
28656
+ return "";
28657
+ }
27764
28658
  const parts = rawParts.filter((part) => !part.startsWith("TC:"));
27765
28659
  if (parts.length === 0)
27766
28660
  continue;
@@ -27773,6 +28667,12 @@ function chunkCanonicalText(canonicalText, startOrdinal, endOrdinal, maxInputTok
27773
28667
  const lines = canonicalText.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
27774
28668
  if (lines.length === 0 || endOrdinal < startOrdinal)
27775
28669
  return [];
28670
+ for (const line of lines) {
28671
+ const range = parseCanonicalLineRange(line);
28672
+ if (range) {
28673
+ assertOrdinalRangeWithinCompartment(range.start, range.end, startOrdinal, endOrdinal);
28674
+ }
28675
+ }
27776
28676
  const normalizedMax = normalizeCompartmentChunkMaxInputTokens(maxInputTokens);
27777
28677
  const effectiveMax = Math.max(1, Math.floor(normalizedMax * CHUNK_WINDOW_SAFETY_RATIO));
27778
28678
  const fullText = lines.join(`
@@ -27798,8 +28698,9 @@ function chunkCanonicalText(canonicalText, startOrdinal, endOrdinal, maxInputTok
27798
28698
  return;
27799
28699
  const text = currentLines.join(`
27800
28700
  `);
28701
+ assertOrdinalRangeWithinCompartment(currentStart, currentEnd, startOrdinal, endOrdinal);
27801
28702
  windows.push({
27802
- windowIndex: windows.length + 1,
28703
+ windowIndex: windows.length,
27803
28704
  startOrdinal: currentStart,
27804
28705
  endOrdinal: currentEnd,
27805
28706
  text,
@@ -27818,8 +28719,9 @@ function chunkCanonicalText(canonicalText, startOrdinal, endOrdinal, maxInputTok
27818
28719
  if (lineTokens > effectiveMax) {
27819
28720
  flush();
27820
28721
  for (const slice of splitOversizedLine(line, effectiveMax)) {
28722
+ assertOrdinalRangeWithinCompartment(lineStart, lineEnd, startOrdinal, endOrdinal);
27821
28723
  windows.push({
27822
- windowIndex: windows.length + 1,
28724
+ windowIndex: windows.length,
27823
28725
  startOrdinal: lineStart,
27824
28726
  endOrdinal: lineEnd,
27825
28727
  text: slice,
@@ -27969,34 +28871,57 @@ function mapBackfillCandidateRows(rows) {
27969
28871
  title: row.title
27970
28872
  }));
27971
28873
  }
28874
+ function renumberOneBasedChunkWindows(db, candidate, projectPath, modelId, windows) {
28875
+ const update = getRenumberEmbeddingWindowStatement(db);
28876
+ db.transaction(() => {
28877
+ for (const window of windows) {
28878
+ const result = update.run(window.windowIndex, candidate.id, modelId, projectPath, window.windowIndex + 1);
28879
+ if (result.changes !== 1) {
28880
+ throw new Error(`Failed to renumber compartment ${candidate.id} window ${window.windowIndex + 1}`);
28881
+ }
28882
+ }
28883
+ })();
28884
+ invalidateDecodedSearchPools(db, ([sessionId, cachedProjectPath, cachedModelId]) => sessionId === candidate.sessionId && cachedProjectPath === projectPath && cachedModelId === modelId);
28885
+ }
27972
28886
  function classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens) {
27973
28887
  const mappedText = buildCanonicalChunkTextFromFts(db, candidate.sessionId, candidate.startMessage, candidate.endMessage);
27974
28888
  if (mappedText === null)
27975
- return "deferred";
28889
+ return { defect: "deferred", windows: [] };
27976
28890
  const canonicalText = mappedText || buildCompartmentSummaryFallbackText(db, candidate.id);
27977
28891
  const windows = chunkCanonicalText(canonicalText, candidate.startMessage, candidate.endMessage, maxInputTokens);
27978
28892
  const existing = getExistingChunkHashes(db, candidate.id, modelId, projectPath);
27979
- if (windows.some((window) => !existing.has(window.windowIndex)))
27980
- return "missing";
28893
+ const isMatchingOneBasedSet = windows.length > 0 && existing.size === windows.length && windows.every((window) => existing.get(window.windowIndex + 1) === window.chunkHash);
28894
+ if (isMatchingOneBasedSet)
28895
+ return { defect: "renumber", windows };
28896
+ const expectedWindowIndexes = new Set(windows.map((window) => window.windowIndex));
28897
+ if ([...existing.keys()].some((windowIndex) => !expectedWindowIndexes.has(windowIndex))) {
28898
+ return { defect: "stale", windows };
28899
+ }
28900
+ if (windows.some((window) => !existing.has(window.windowIndex))) {
28901
+ return { defect: "missing", windows };
28902
+ }
27981
28903
  if (existing.size !== windows.length || windows.some((window) => existing.get(window.windowIndex) !== window.chunkHash)) {
27982
- return "stale";
28904
+ return { defect: "stale", windows };
27983
28905
  }
27984
- return null;
28906
+ return { defect: null, windows };
27985
28907
  }
27986
- function selectHashIncompleteChunkCandidates(db, projectPath, modelId, candidates, limit, maxInputTokens) {
28908
+ function selectHashIncompleteChunkCandidates(db, projectPath, modelId, candidates, limit, maxInputTokens, leaseHeldRenumber = false) {
27987
28909
  const missing = [];
27988
28910
  const stale = [];
27989
28911
  for (const candidate of candidates) {
27990
- const defect = classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens);
28912
+ const { defect, windows } = classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens);
27991
28913
  if (defect === "missing")
27992
28914
  missing.push(candidate);
27993
28915
  else if (defect === "stale")
27994
28916
  stale.push(candidate);
28917
+ else if (defect === "renumber" && leaseHeldRenumber) {
28918
+ renumberOneBasedChunkWindows(db, candidate, projectPath, modelId, windows);
28919
+ }
27995
28920
  }
27996
28921
  return [...missing, ...stale].slice(0, limit);
27997
28922
  }
27998
28923
  var sessionBackfillCandidateStatements = new WeakMap;
27999
- function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, limit, excludeIds, maxInputTokens = DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS) {
28924
+ function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, limit, excludeIds, maxInputTokens = DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS, leaseHeldRenumber = false) {
28000
28925
  const exclusions = excludeIds && excludeIds.length > 0 ? excludeIds : [];
28001
28926
  const exclusionSql = exclusions.length > 0 ? `AND c.id NOT IN (${exclusions.map(() => "?").join(", ")})` : "";
28002
28927
  const stmt = exclusions.length > 0 ? db.prepare(`SELECT c.id AS id,
@@ -28035,7 +28960,7 @@ function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelI
28035
28960
  return cached;
28036
28961
  })();
28037
28962
  const rows = stmt.all(projectPath, sessionId, ...exclusions);
28038
- return selectHashIncompleteChunkCandidates(db, projectPath, modelId, mapBackfillCandidateRows(rows), Math.max(1, limit), maxInputTokens);
28963
+ return selectHashIncompleteChunkCandidates(db, projectPath, modelId, mapBackfillCandidateRows(rows), Math.max(1, limit), maxInputTokens, leaseHeldRenumber);
28039
28964
  }
28040
28965
  function countUnembeddedSessionCompartments(db, projectPath, sessionId, modelId, maxInputTokens = DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS) {
28041
28966
  return loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, Number.MAX_SAFE_INTEGER, undefined, maxInputTokens).length;
@@ -28058,9 +28983,9 @@ function countSessionCompartmentEmbedCoverage(db, projectPath, sessionId, modelI
28058
28983
  const candidates = mapBackfillCandidateRows(rows);
28059
28984
  let embedded = 0;
28060
28985
  for (const candidate of candidates) {
28061
- if (classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens) === null) {
28986
+ const { defect } = classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens);
28987
+ if (defect === null || defect === "renumber")
28062
28988
  embedded += 1;
28063
- }
28064
28989
  }
28065
28990
  return { embedded, total: candidates.length };
28066
28991
  }
@@ -28082,6 +29007,78 @@ function cosineSimilarity(a, b) {
28082
29007
  return denominator === 0 ? 0 : dotProduct / denominator;
28083
29008
  }
28084
29009
 
29010
+ // ../plugin/src/features/magic-context/memory/embedding-model-match.ts
29011
+ function parseEmbeddingModel(model) {
29012
+ const lastColon = model.lastIndexOf(":");
29013
+ if (lastColon > model.lastIndexOf("/")) {
29014
+ return { base: model.slice(0, lastColon), tag: model.slice(lastColon + 1) };
29015
+ }
29016
+ return { base: model };
29017
+ }
29018
+ function normalizeEmbeddingModelId(model) {
29019
+ return parseEmbeddingModel(model.trim().toLowerCase()).base;
29020
+ }
29021
+ function matchNormalizedEmbeddingModels(a, b) {
29022
+ if (a.length === 0 || b.length === 0)
29023
+ return true;
29024
+ if (a === b)
29025
+ return true;
29026
+ const longer = a.length >= b.length ? a : b;
29027
+ const shorter = a.length >= b.length ? b : a;
29028
+ const isBoundary = (ch) => ch === "-" || ch === "/";
29029
+ if (longer.startsWith(shorter) && isBoundary(longer.charAt(shorter.length)))
29030
+ return true;
29031
+ if (longer.endsWith(shorter) && isBoundary(longer.charAt(longer.length - shorter.length - 1)))
29032
+ return true;
29033
+ return false;
29034
+ }
29035
+ function embeddingModelsMatch(served, requested) {
29036
+ const servedNormalized = served.trim().toLowerCase();
29037
+ const requestedNormalized = requested.trim().toLowerCase();
29038
+ const servedModel = parseEmbeddingModel(servedNormalized);
29039
+ const requestedModel = parseEmbeddingModel(requestedNormalized);
29040
+ if (servedModel.tag !== undefined && requestedModel.tag !== undefined && servedModel.tag !== requestedModel.tag) {
29041
+ return false;
29042
+ }
29043
+ return matchNormalizedEmbeddingModels(servedModel.base, requestedModel.base);
29044
+ }
29045
+ var QWEN3_QUERY_INSTRUCTION = `Instruct: Given a web search query, retrieve relevant passages that answer the query
29046
+ Query: `;
29047
+ var INSTRUCT_WEB_SEARCH_QUERY_PREFIX = QWEN3_QUERY_INSTRUCTION;
29048
+ var EMBEDDING_MODEL_PREFIX_FAMILIES = [
29049
+ {
29050
+ family: "qwen3-embedding",
29051
+ basenamePattern: /^qwen3-embedding(?:-|$)/,
29052
+ prefixes: { queryPrefix: QWEN3_QUERY_INSTRUCTION, documentPrefix: "" }
29053
+ },
29054
+ {
29055
+ family: "gte-qwen-instruct",
29056
+ basenamePattern: /^gte-qwen.*-instruct(?:-|$)/,
29057
+ prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
29058
+ },
29059
+ {
29060
+ family: "e5-instruct",
29061
+ basenamePattern: /^(?:multilingual-)?e5-.*-instruct(?:-|$)/,
29062
+ prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
29063
+ },
29064
+ {
29065
+ family: "nomic-embed-text",
29066
+ basenamePattern: /^nomic-embed-text(?:-|$)/,
29067
+ prefixes: { queryPrefix: "search_query: ", documentPrefix: "search_document: " }
29068
+ }
29069
+ ];
29070
+ function modelBasename(model) {
29071
+ const normalized = normalizeEmbeddingModelId(model);
29072
+ return normalized.slice(normalized.lastIndexOf("/") + 1);
29073
+ }
29074
+ function resolveEmbeddingTextPrefixes(model, queryInstruction, documentPrefix) {
29075
+ const family = EMBEDDING_MODEL_PREFIX_FAMILIES.find(({ basenamePattern }) => basenamePattern.test(modelBasename(model)));
29076
+ return {
29077
+ queryPrefix: queryInstruction === false ? "" : queryInstruction !== undefined ? queryInstruction : family?.prefixes.queryPrefix ?? "",
29078
+ documentPrefix: documentPrefix ?? family?.prefixes.documentPrefix ?? ""
29079
+ };
29080
+ }
29081
+
28085
29082
  // ../plugin/src/features/magic-context/memory/normalize-hash.ts
28086
29083
  import { createHash as createHash11 } from "node:crypto";
28087
29084
  function normalizeMemoryContent(content) {
@@ -28110,6 +29107,7 @@ function getEmbeddingProviderIdentity(config) {
28110
29107
  throw new Error("Unknown embedding provider");
28111
29108
  }
28112
29109
  const truncate = config.provider === "openai-compatible" ? config.truncate?.trim() : undefined;
29110
+ const documentPrefix = config.provider === "openai-compatible" ? resolveEmbeddingTextPrefixes(config.model, config.query_instruction, config.document_prefix).documentPrefix : "";
28113
29111
  const localDtype = config.provider === "local" && config.local_dtype && config.local_dtype !== "fp32" ? config.local_dtype : undefined;
28114
29112
  const identityInput = config.provider === "openai-compatible" ? {
28115
29113
  provider: "openai-compatible",
@@ -28117,6 +29115,7 @@ function getEmbeddingProviderIdentity(config) {
28117
29115
  endpoint: normalizeEndpoint2(config.endpoint),
28118
29116
  apiKeyPresent: Boolean(config.api_key?.trim()),
28119
29117
  inputType: config.input_type?.trim() || "",
29118
+ ...documentPrefix ? { documentPrefix } : {},
28120
29119
  ...truncate ? { truncate } : {}
28121
29120
  } : {
28122
29121
  provider: "local",
@@ -28863,37 +29862,6 @@ function blockedEmbeddingEndpointReason(endpoint) {
28863
29862
  function normalizeEndpoint3(endpoint) {
28864
29863
  return endpoint?.trim().replace(/\/+$/, "") ?? "";
28865
29864
  }
28866
- function parseEmbeddingModel(model) {
28867
- const lastColon = model.lastIndexOf(":");
28868
- if (lastColon > model.lastIndexOf("/")) {
28869
- return { base: model.slice(0, lastColon), tag: model.slice(lastColon + 1) };
28870
- }
28871
- return { base: model };
28872
- }
28873
- function matchNormalizedEmbeddingModels(a, b) {
28874
- if (a.length === 0 || b.length === 0)
28875
- return true;
28876
- if (a === b)
28877
- return true;
28878
- const longer = a.length >= b.length ? a : b;
28879
- const shorter = a.length >= b.length ? b : a;
28880
- const isBoundary = (ch) => ch === "-" || ch === "/";
28881
- if (longer.startsWith(shorter) && isBoundary(longer.charAt(shorter.length)))
28882
- return true;
28883
- if (longer.endsWith(shorter) && isBoundary(longer.charAt(longer.length - shorter.length - 1)))
28884
- return true;
28885
- return false;
28886
- }
28887
- function embeddingModelsMatch(served, requested) {
28888
- const a = served.trim().toLowerCase();
28889
- const b = requested.trim().toLowerCase();
28890
- const servedModel = parseEmbeddingModel(a);
28891
- const requestedModel = parseEmbeddingModel(b);
28892
- if (servedModel.tag !== undefined && requestedModel.tag !== undefined && servedModel.tag !== requestedModel.tag) {
28893
- return false;
28894
- }
28895
- return matchNormalizedEmbeddingModels(servedModel.base, requestedModel.base);
28896
- }
28897
29865
  var FAILURE_THRESHOLD = 3;
28898
29866
  var FAILURE_WINDOW_MS = 60000;
28899
29867
  var OPEN_DURATION_MS = 5 * 60000;
@@ -28907,6 +29875,8 @@ class OpenAICompatibleEmbeddingProvider {
28907
29875
  apiKey;
28908
29876
  inputType;
28909
29877
  queryInputType;
29878
+ queryPrefix;
29879
+ documentPrefix;
28910
29880
  truncate;
28911
29881
  initialized = false;
28912
29882
  failureTimes = [];
@@ -28921,6 +29891,9 @@ class OpenAICompatibleEmbeddingProvider {
28921
29891
  this.apiKey = options.apiKey?.trim() ?? "";
28922
29892
  this.inputType = options.inputType?.trim() ?? "";
28923
29893
  this.queryInputType = options.queryInputType?.trim() ?? "";
29894
+ const prefixes = resolveEmbeddingTextPrefixes(this.model, options.queryInstruction, options.documentPrefix);
29895
+ this.queryPrefix = prefixes.queryPrefix;
29896
+ this.documentPrefix = prefixes.documentPrefix;
28924
29897
  this.truncate = options.truncate?.trim() ?? "";
28925
29898
  this.maxInputTokens = typeof options.maxInputTokens === "number" && Number.isFinite(options.maxInputTokens) ? Math.max(1, Math.floor(options.maxInputTokens)) : 512;
28926
29899
  this.modelId = getEmbeddingProviderIdentity({
@@ -28929,6 +29902,7 @@ class OpenAICompatibleEmbeddingProvider {
28929
29902
  model: this.model,
28930
29903
  ...this.apiKey ? { api_key: this.apiKey } : {},
28931
29904
  ...this.inputType ? { input_type: this.inputType } : {},
29905
+ ...options.documentPrefix !== undefined ? { document_prefix: options.documentPrefix } : {},
28932
29906
  ...this.truncate ? { truncate: this.truncate } : {}
28933
29907
  });
28934
29908
  }
@@ -28963,7 +29937,8 @@ class OpenAICompatibleEmbeddingProvider {
28963
29937
  if (texts.length === 0) {
28964
29938
  return [];
28965
29939
  }
28966
- const requestTexts = texts.map((t) => t.trim().length === 0 ? " " : t);
29940
+ const textPrefix = purpose === "query" ? this.queryPrefix : this.documentPrefix;
29941
+ const requestTexts = texts.map((text) => `${textPrefix}${text.trim().length === 0 ? " " : text}`);
28967
29942
  if (!await this.initialize()) {
28968
29943
  return Array.from({ length: texts.length }, () => null);
28969
29944
  }
@@ -29930,6 +30905,8 @@ function resolveEmbeddingConfig(config) {
29930
30905
  ...apiKey ? { api_key: apiKey } : {},
29931
30906
  ...inputType ? { input_type: inputType } : {},
29932
30907
  ...queryInputType ? { query_input_type: queryInputType } : {},
30908
+ ...config.query_instruction !== undefined ? { query_instruction: config.query_instruction } : {},
30909
+ ...config.document_prefix !== undefined ? { document_prefix: config.document_prefix } : {},
29933
30910
  ...truncate ? { truncate } : {},
29934
30911
  ...config.max_input_tokens ? {
29935
30912
  max_input_tokens: normalizeCompartmentChunkMaxInputTokens(config.max_input_tokens)
@@ -29977,6 +30954,8 @@ function createProvider(config, context) {
29977
30954
  apiKey: config.api_key,
29978
30955
  inputType: config.input_type,
29979
30956
  queryInputType: config.query_input_type,
30957
+ queryInstruction: config.query_instruction,
30958
+ documentPrefix: config.document_prefix,
29980
30959
  truncate: config.truncate,
29981
30960
  maxInputTokens: config.max_input_tokens
29982
30961
  });
@@ -31142,8 +32121,6 @@ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, opt
31142
32121
  recordSessionProjectIdentity(db, sessionId, projectIdentity);
31143
32122
  const maxInputTokens = getProjectEmbeddingMaxInputTokens(projectIdentity);
31144
32123
  const total = countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot.chunkModelId, maxInputTokens);
31145
- if (total === 0)
31146
- return { status: "nothing", embedded: 0, total: 0 };
31147
32124
  const holderId = `session-embed-${randomUUID2()}`;
31148
32125
  const lease = acquireGitSweepLease(db, projectIdentity, holderId, { ignoreCooldown: true });
31149
32126
  if (!lease.acquired)
@@ -31179,7 +32156,7 @@ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, opt
31179
32156
  aborted = true;
31180
32157
  break;
31181
32158
  }
31182
- const candidates = loadUnembeddedSessionChunkCandidates(db, projectIdentity, sessionId, snapshot.chunkModelId, batchSize, [...skipIds, ...failedIds], maxInputTokens);
32159
+ const candidates = loadUnembeddedSessionChunkCandidates(db, projectIdentity, sessionId, snapshot.chunkModelId, batchSize, [...skipIds, ...failedIds], maxInputTokens, true);
31183
32160
  if (candidates.length === 0)
31184
32161
  break;
31185
32162
  const {
@@ -31221,6 +32198,8 @@ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, opt
31221
32198
  log("[magic-context] embed drain: lease release failed (will TTL-expire):", error);
31222
32199
  }
31223
32200
  }
32201
+ if (total === 0)
32202
+ return { status: "nothing", embedded: 0, total: 0 };
31224
32203
  if (aborted)
31225
32204
  return { status: "aborted", embedded, total, failed: failedIds.length };
31226
32205
  if (providerDown || failedIds.length > 0) {
@@ -31291,6 +32270,8 @@ function createProvider2(config) {
31291
32270
  apiKey: config.api_key,
31292
32271
  inputType: config.input_type,
31293
32272
  queryInputType: config.query_input_type,
32273
+ queryInstruction: config.query_instruction,
32274
+ documentPrefix: config.document_prefix,
31294
32275
  truncate: config.truncate,
31295
32276
  maxInputTokens: config.max_input_tokens
31296
32277
  });
@@ -31349,6 +32330,8 @@ var EMBEDDING_AFFECTING_KEYS = new Set([
31349
32330
  "embedding.truncate",
31350
32331
  "embedding.max_input_tokens",
31351
32332
  "embedding.query_input_type",
32333
+ "embedding.query_instruction",
32334
+ "embedding.document_prefix",
31352
32335
  "embedding.fallback_provider",
31353
32336
  "subc",
31354
32337
  "subc.connection_file",
@@ -39480,4 +40463,4 @@ function registerMagicContextTools(pi, opts) {
39480
40463
  }
39481
40464
  }
39482
40465
 
39483
- export { COMPACTION_ENABLED_PATH, isDreamerRunnable, isCompactionEnabled, migrateMagicContextConfigLocations, withContentLanguageDirective, withMigrationLanguageDirective, buildPrimaryLanguageDirective, parseCron, nextOccurrence, nextDueAtMs, canonicalModelIdentity, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, ompModelRefToCanonical, resolveModelRefForOmp, resolveModelConfigValue, resolveModelConfigOrDefault, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, DEFAULT_HISTORIAN_TIMEOUT_MS, getProtectedTokensTierOverrides, sanitizeDiagnosticText, hasShareabilitySensitiveText, FAIL_CLOSED_DOCTOR_COMMAND, formatFailClosedBlockingMessage, createFailClosedBlockingError, isFailClosedBlockingError, shouldBypassFailClosedBlock, createFailClosedController, setHarness, getHarness, ensureCortexKitArtifactGitignore, getProjectMagicContextHistorianDir, getMagicContextStorageResolution, getMagicContextStorageDir, log, sessionLog, flushLogger, ProjectIdentityError, resolveProjectIdentityStrict, resolveProjectIdentity, resolveProjectIdentityForSession, beginBootQuietPeriod, scheduleAfterBootQuiet, CTX_REDUCE_KEEP, newestCtxReduceTagNumbers, textMentionsRecentCommit, hasMeaningfulUserText, extractTexts, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isRecord, estimateImageTokensFromDataUrl, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, planEmergencyDrop, updateTagByteSize, getRecentTagOwnerMessageIds, 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, getAssignableTagNumberByMessageId, deriveTagLoadFloor, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, resolveOpenCodeDbPath, openCodeDbPathExists, formatOpenCodeDbMissingStatusLine, formatOpenCodeDbReadFailureStatusLine, recordOpenCodeDbReadFailure, clearOpenCodeDbReadFailure, getOpenCodeDbReadFailure, claimOpenCodeDbDiagnosticOnce, Database, closeQuietly, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, DROPPED_INPUT_MESSAGE, droppedInputMarker, containsDroppedInputPlaceholder, isEditTool, applyEditMarkerToInput, setRawMessageProvider, withRawMessageProvider, cleanUserText, withRawSessionMessageCache, readRawSessionMessages, primeTailRawMessageCache, getCachedAbsoluteMessageCount, primeInMemoryTailRawMessageCache, getRawSessionMessageCount, getRawSessionTagKeysThrough, getLegacyProtectedTailStartOrdinal, readSessionChunk, logSlowWriteTransaction, clearCompressionDepth, clearCompressionDepthRange, getMessageIndexSourceIdentity, isMessageIndexSourceCurrent, getLastIndexedOrdinal, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, indexSingleMessage, indexMessagesAfterOrdinal, sweepOrphanedOpenCodeMessageIndexes, recordSessionProjectIdentity, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLease, releaseCompartmentLeaseBestEffort, isCompartmentLeaseHeld, isNoContentCompartment, HAS_COMPARTMENT_CONTENT_SQL, persistCachedM0, clearCachedM0M1, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, getCompartmentsByEndMessageId, appendCompartments, saveRecompStagingPass, getRecompStaging, clearRecompStaging, getRecompPartialRange, setRecompPartialRange, escapeXmlAttr, escapeXmlContent, getModuleNoteEvaluationBridge, getContextStoreUuid, parseCompartmentOutput, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, sourceNameForMemory, computeWorkspaceEpochFingerprint, bumpEpochsForWorkspaceMembers, readProjectDocsCanonical, encodePiContentDecision, getPiContentDecisions, freezePiContentDecision, getNativeReplayState, saveNativeToolInputs, addNativeReasoningIds, copySessionStateForClone, getErrorMessage, describeError, piHarnessKindFromExecutable, setStoragePrivatePermissionEnforcement, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, applySqliteTuningPragmas, runSqliteOptimize, openDatabase, openDatabaseAsync, queueM0Mutation, getMaxM0MutationId, queueMemoryMutation, getMemoryMutationsForRender, getMemoryMutationsForRenderByProjects, getMaxMemoryMutationId, getMaxMemoryMutationIdForProjects, MAX_EXECUTE_THRESHOLD, escalationBands, computeProtectionWindow, readEpochFloorSnapshot, getProtectionWindowForSession, isProviderOverflowFailClosedProven, 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, incrementHistorianFailure, clearHistorianFailureState, getOverflowState, recordOverflowDetected, clearEmergencyRecovery, clearDetectedContextLimit, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, NEWEST_REASONING_BEARING_ASSISTANT, THINKING_BINDING_RECOVERY_FROZEN_PREFIX, thinkingBindingRecoveryFrozenId, getThinkingBindingRecoveryTarget, armThinkingBindingRecovery, clearThinkingBindingRecoveryIf, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getProcessedImageStrippedIds, addProcessedImageStrippedIds, getPendingCompactionMarkerState, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, setPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, getSessionsWithPendingPiMarker, setSessionWorkMetrics, getSessionWorkMetrics, resolveEpochFloorForPass, getOrCreateSessionMeta, updateSessionMeta, advanceToolReclaimWatermark, retryPendingSessionCleanups, retryPendingRustSessionCleanupsForProject, 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, getSourceContents, recordSubagentInvocation, getLatestHistorianInvocationId, BoundedSessionMap, MIN_PLAUSIBLE_CONTEXT_LIMIT, reloadWindowOverlay, getWindowOverlay, resolveWindowOverlayFacts, deriveWindowGeometry, hasTrustedAbsoluteWall, applyProvenInputFloor, formatWindowDerivationLine, isSaneLimit, resolveOutputReserve, getSdkContextLimit, formatConfigParseStatusLine, formatConfigParseNotice, claimConfigParseFailuresOnce, promptSurfaceHashMaterial, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, SYNTH_USER_ID_PREFIX, resolvePiStableId, readPiSessionSnapshot, readPiSessionMessages, readPiSessionMessagePage, findLastModelKeyFromBranch, convertEntriesToRawMessages, convertEntriesToRawMessagePage, 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, formatSynapseLaneDescriptor, buildCanonicalChunkTextFromFts, buildCompartmentSummaryFallbackText, canonicalizeInMemoryChunkTextForEmbedding, chunkCanonicalText, chunkEmbeddingWindowsAreCurrent, replaceCompartmentChunkEmbeddings, cosineSimilarity, GIT_SWEEP_LEASE_RENEWAL_MS, acquireGitSweepLease, renewGitSweepLease, markGitSweepSuccessAndRelease, parkGitSweepNonIndexable, releaseGitSweepLease, describeShadowBackfillWriteRefusal, 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, isDirectiveShapedProjectRule, takeCurateSafetyRefusalCount, wakePlaneStatus, indexCommitsForProject, embedUnembeddedCommits, loadPiConfig, ensureProjectRegisteredFromPiDirectory, resolvePiHarnessDetection, resolvePiHarnessKind, 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, syncCtxMemoryToolEnabled, registerMagicContextTools };
40466
+ export { COMPACTION_ENABLED_PATH, isDreamerRunnable, isCompactionEnabled, migrateMagicContextConfigLocations, withContentLanguageDirective, withMigrationLanguageDirective, buildPrimaryLanguageDirective, parseCron, nextOccurrence, nextDueAtMs, canonicalModelIdentity, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, ompModelRefToCanonical, resolveModelRefForOmp, resolveModelConfigValue, resolveModelConfigOrDefault, DEFAULT_HISTORIAN_TIMEOUT_MS, getProtectedTokensTierOverrides, sanitizeDiagnosticText, hasShareabilitySensitiveText, FAIL_CLOSED_DOCTOR_COMMAND, formatFailClosedBlockingMessage, createFailClosedBlockingError, isFailClosedBlockingError, shouldBypassFailClosedBlock, createFailClosedController, setHarness, getHarness, ensureCortexKitArtifactGitignore, getProjectMagicContextHistorianDir, getMagicContextStorageResolution, getMagicContextStorageDir, log, sessionLog, flushLogger, ProjectIdentityError, resolveProjectIdentityStrict, resolveProjectIdentity, resolveProjectIdentityForSession, beginBootQuietPeriod, scheduleAfterBootQuiet, CTX_REDUCE_KEEP, newestCtxReduceTagNumbers, textMentionsRecentCommit, hasMeaningfulUserText, extractTexts, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isRecord, estimateImageTokensFromDataUrl, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, planEmergencyDrop, updateTagByteSize, getRecentTagOwnerMessageIds, 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, getAssignableTagNumberByMessageId, deriveTagLoadFloor, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, resolveOpenCodeDbPath, openCodeDbPathExists, recordOpenCodeDbReadFailure, clearOpenCodeDbReadFailure, claimOpenCodeDbDiagnosticOnce, Database, closeQuietly, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, DROPPED_INPUT_MESSAGE, droppedInputMarker, containsDroppedInputPlaceholder, isEditTool, applyEditMarkerToInput, setRawMessageProvider, withRawMessageProvider, cleanUserText, withRawSessionMessageCache, readRawSessionMessages, primeTailRawMessageCache, getCachedAbsoluteMessageCount, primeInMemoryTailRawMessageCache, getRawSessionMessageCount, getRawSessionTagKeysThrough, getLegacyProtectedTailStartOrdinal, readSessionChunk, logSlowWriteTransaction, clearCompressionDepth, clearCompressionDepthRange, getMessageIndexSourceIdentity, isMessageIndexSourceCurrent, getLastIndexedOrdinal, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, indexSingleMessage, indexMessagesAfterOrdinal, sweepOrphanedOpenCodeMessageIndexes, recordSessionProjectIdentity, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLease, releaseCompartmentLeaseBestEffort, isCompartmentLeaseHeld, isNoContentCompartment, HAS_COMPARTMENT_CONTENT_SQL, persistCachedM0, clearCachedM0M1, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, getCompartmentsByEndMessageId, appendCompartments, saveRecompStagingPass, getRecompStaging, clearRecompStaging, getRecompPartialRange, setRecompPartialRange, escapeXmlAttr, escapeXmlContent, getModuleNoteEvaluationBridge, getContextStoreUuid, drainMirrorPages, parseCompartmentOutput, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, sourceNameForMemory, computeWorkspaceEpochFingerprint, bumpEpochsForWorkspaceMembers, readProjectDocsCanonical, encodePiContentDecision, getPiContentDecisions, freezePiContentDecision, getNativeReplayState, saveNativeToolInputs, addNativeReasoningIds, copySessionStateForClone, getErrorMessage, describeError, piHarnessKindFromExecutable, setStoragePrivatePermissionEnforcement, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, applySqliteTuningPragmas, runSqliteOptimize, openDatabase, openDatabaseAsync, queueM0Mutation, getMaxM0MutationId, queueMemoryMutation, getMemoryMutationsForRender, getMemoryMutationsForRenderByProjects, getMaxMemoryMutationId, getMaxMemoryMutationIdForProjects, MAX_EXECUTE_THRESHOLD, escalationBands, computeProtectionWindow, readEpochFloorSnapshot, getProtectionWindowForSession, isProviderOverflowFailClosedProven, 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, incrementHistorianFailure, clearHistorianFailureState, getOverflowState, recordOverflowDetected, clearEmergencyRecovery, clearDetectedContextLimit, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, NEWEST_REASONING_BEARING_ASSISTANT, THINKING_BINDING_RECOVERY_FROZEN_PREFIX, thinkingBindingRecoveryFrozenId, getThinkingBindingRecoveryTarget, armThinkingBindingRecovery, clearThinkingBindingRecoveryIf, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getProcessedImageStrippedIds, addProcessedImageStrippedIds, getPendingCompactionMarkerState, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, setPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, getSessionsWithPendingPiMarker, setSessionWorkMetrics, getSessionWorkMetrics, resolveEpochFloorForPass, getOrCreateSessionMeta, updateSessionMeta, advanceToolReclaimWatermark, retryPendingSessionCleanups, retryPendingRustSessionCleanupsForProject, 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, getSourceContents, recordSubagentInvocation, getLatestHistorianInvocationId, BoundedSessionMap, MIN_PLAUSIBLE_CONTEXT_LIMIT, reloadWindowOverlay, getWindowOverlay, resolveWindowOverlayFacts, deriveWindowGeometry, hasTrustedAbsoluteWall, applyProvenInputFloor, formatWindowDerivationLine, isSaneLimit, resolveOutputReserve, getSdkContextLimit, formatConfigParseStatusLine, formatConfigParseNotice, claimConfigParseFailuresOnce, promptSurfaceHashMaterial, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, SYNTH_USER_ID_PREFIX, resolvePiStableId, readPiSessionSnapshot, readPiSessionMessages, readPiSessionMessagePage, findLastModelKeyFromBranch, convertEntriesToRawMessages, convertEntriesToRawMessagePage, 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, formatSynapseLaneDescriptor, buildCanonicalChunkTextFromFts, buildCompartmentSummaryFallbackText, canonicalizeInMemoryChunkTextForEmbedding, chunkCanonicalText, chunkEmbeddingWindowsAreCurrent, replaceCompartmentChunkEmbeddings, cosineSimilarity, GIT_SWEEP_LEASE_RENEWAL_MS, acquireGitSweepLease, renewGitSweepLease, markGitSweepSuccessAndRelease, parkGitSweepNonIndexable, releaseGitSweepLease, describeShadowBackfillWriteRefusal, 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, isDirectiveShapedProjectRule, takeCurateSafetyRefusalCount, wakePlaneStatus, indexCommitsForProject, embedUnembeddedCommits, loadPiConfig, ensureProjectRegisteredFromPiDirectory, resolvePiHarnessDetection, resolvePiHarnessKind, 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, syncCtxMemoryToolEnabled, registerMagicContextTools };