@cortexkit/pi-magic-context 0.23.1 → 0.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8164,9 +8164,470 @@ function isRelativeProjectFile(projectPath, relativePath) {
8164
8164
 
8165
8165
  // ../plugin/src/features/magic-context/migrations.ts
8166
8166
  init_logger();
8167
- function tableExists(db, name) {
8167
+
8168
+ // ../plugin/src/features/magic-context/workspaces.ts
8169
+ import { createHash as createHash3 } from "node:crypto";
8170
+
8171
+ // ../plugin/src/features/magic-context/memory/constants.ts
8172
+ var V2_MEMORY_CATEGORIES = [
8173
+ "PROJECT_RULES",
8174
+ "ARCHITECTURE",
8175
+ "CONSTRAINTS",
8176
+ "CONFIG_VALUES",
8177
+ "NAMING"
8178
+ ];
8179
+ var PROMOTABLE_CATEGORIES = [
8180
+ "PROJECT_RULES",
8181
+ "ARCHITECTURE",
8182
+ "CONSTRAINTS",
8183
+ "CONFIG_VALUES",
8184
+ "NAMING",
8185
+ "ARCHITECTURE_DECISIONS",
8186
+ "CONFIG_DEFAULTS",
8187
+ "USER_PREFERENCES",
8188
+ "USER_DIRECTIVES",
8189
+ "ENVIRONMENT",
8190
+ "WORKFLOW_RULES",
8191
+ "KNOWN_ISSUES"
8192
+ ];
8193
+ var CATEGORY_PRIORITY = [
8194
+ "PROJECT_RULES",
8195
+ "ARCHITECTURE",
8196
+ "CONSTRAINTS",
8197
+ "CONFIG_VALUES",
8198
+ "NAMING",
8199
+ "USER_DIRECTIVES",
8200
+ "USER_PREFERENCES",
8201
+ "CONFIG_DEFAULTS",
8202
+ "ARCHITECTURE_DECISIONS",
8203
+ "ENVIRONMENT",
8204
+ "WORKFLOW_RULES",
8205
+ "KNOWN_ISSUES"
8206
+ ];
8207
+ var MEMORY_CATEGORY_ORDER_UNKNOWN = 99;
8208
+ var MEMORY_CATEGORY_ORDER_PRIORITY = CATEGORY_PRIORITY.reduce((acc, category, index) => {
8209
+ acc[category] = index;
8210
+ return acc;
8211
+ }, {});
8212
+ var MEMORY_CATEGORY_ORDER_SQL = `CASE category ${CATEGORY_PRIORITY.map((category, index) => `WHEN '${category}' THEN ${index}`).join(" ")} ELSE ${MEMORY_CATEGORY_ORDER_UNKNOWN} END`;
8213
+ var CATEGORY_DEFAULT_TTL = {
8214
+ WORKFLOW_RULES: 90 * 24 * 60 * 60 * 1000,
8215
+ KNOWN_ISSUES: 30 * 24 * 60 * 60 * 1000
8216
+ };
8217
+
8218
+ // ../plugin/src/features/magic-context/memory/project-identity.ts
8219
+ import { execFileSync } from "node:child_process";
8220
+ import { createHash as createHash2 } from "node:crypto";
8221
+ import { statSync } from "node:fs";
8222
+ import path3 from "node:path";
8223
+ var GIT_TIMEOUT_MS = 5000;
8224
+ var identityCache = new Map;
8225
+ var directoryFallbackCache = new Map;
8226
+
8227
+ class ProjectIdentityError extends Error {
8228
+ errorClass;
8229
+ rawDirectory;
8230
+ constructor(errorClass, rawDirectory, message, cause) {
8231
+ super(message);
8232
+ this.name = "ProjectIdentityError";
8233
+ this.errorClass = errorClass;
8234
+ this.rawDirectory = rawDirectory;
8235
+ if (cause) {
8236
+ this.cause = cause;
8237
+ }
8238
+ }
8239
+ }
8240
+ function asError(error) {
8241
+ return error instanceof Error ? error : undefined;
8242
+ }
8243
+ function getErrorCode(error) {
8244
+ if (error === null || typeof error !== "object" || !("code" in error)) {
8245
+ return;
8246
+ }
8247
+ const code = error.code;
8248
+ return typeof code === "string" ? code : undefined;
8249
+ }
8250
+ function getErrorSignal(error) {
8251
+ if (error === null || typeof error !== "object" || !("signal" in error)) {
8252
+ return;
8253
+ }
8254
+ const signal = error.signal;
8255
+ return typeof signal === "string" ? signal : undefined;
8256
+ }
8257
+ function getErrorKilled(error) {
8258
+ if (error === null || typeof error !== "object" || !("killed" in error)) {
8259
+ return false;
8260
+ }
8261
+ return error.killed === true;
8262
+ }
8263
+ function getErrorStderr(error) {
8264
+ if (error === null || typeof error !== "object" || !("stderr" in error)) {
8265
+ return "";
8266
+ }
8267
+ const stderr = error.stderr;
8268
+ if (typeof stderr === "string") {
8269
+ return stderr;
8270
+ }
8271
+ if (Buffer.isBuffer(stderr)) {
8272
+ return stderr.toString("utf8");
8273
+ }
8274
+ return "";
8275
+ }
8276
+ function directoryFallback(directory) {
8277
+ const canonical = path3.resolve(directory);
8278
+ const hash = createHash2("md5").update(canonical, "utf8").digest("hex").slice(0, 12);
8279
+ return `dir:${hash}`;
8280
+ }
8281
+ function assertDirectoryUsable(canonicalDirectory, rawDirectory) {
8282
+ try {
8283
+ const stat = statSync(canonicalDirectory);
8284
+ if (!stat.isDirectory()) {
8285
+ throw new ProjectIdentityError("unknown", rawDirectory, `Project path is not a directory: ${canonicalDirectory}`);
8286
+ }
8287
+ } catch (error) {
8288
+ if (error instanceof ProjectIdentityError) {
8289
+ throw error;
8290
+ }
8291
+ const code = getErrorCode(error);
8292
+ if (code === "EACCES" || code === "EPERM") {
8293
+ throw new ProjectIdentityError("permission_denied", rawDirectory, `Permission denied while accessing project directory: ${canonicalDirectory}`, asError(error));
8294
+ }
8295
+ throw new ProjectIdentityError("unknown", rawDirectory, `Unable to access project directory: ${canonicalDirectory}`, asError(error));
8296
+ }
8297
+ }
8298
+ function isGitTimeoutError(error) {
8299
+ const code = getErrorCode(error);
8300
+ const signal = getErrorSignal(error);
8301
+ return code === "ETIMEDOUT" || signal === "SIGTERM" || signal === "SIGKILL" || getErrorKilled(error);
8302
+ }
8303
+ function classifyGitError(error, rawDirectory) {
8304
+ if (isGitTimeoutError(error)) {
8305
+ return new ProjectIdentityError("git_timeout", rawDirectory, `git rev-list timed out after ${GIT_TIMEOUT_MS}ms`, asError(error));
8306
+ }
8307
+ const code = getErrorCode(error);
8308
+ if (code === "ENOENT") {
8309
+ return new ProjectIdentityError("git_missing", rawDirectory, "git binary is not available in PATH", asError(error));
8310
+ }
8311
+ if (code === "EACCES" || code === "EPERM") {
8312
+ return new ProjectIdentityError("permission_denied", rawDirectory, "Permission denied while spawning git", asError(error));
8313
+ }
8314
+ const stderr = getErrorStderr(error).toLowerCase();
8315
+ if (stderr.includes("not a git repository") || stderr.includes("does not have any commits yet") || stderr.includes("ambiguous argument 'head'") || stderr.includes("unknown revision or path")) {
8316
+ return new ProjectIdentityError("not_git_repo", rawDirectory, "Directory has no git root commit; caller may use directory fallback", asError(error));
8317
+ }
8318
+ return new ProjectIdentityError("unknown", rawDirectory, "git rev-list failed while resolving project identity", asError(error));
8319
+ }
8320
+ function resolveProjectIdentityStrict(directory) {
8321
+ const canonical = path3.resolve(directory);
8322
+ const cached = identityCache.get(canonical);
8323
+ if (cached !== undefined) {
8324
+ return cached;
8325
+ }
8326
+ assertDirectoryUsable(canonical, directory);
8327
+ let output;
8328
+ try {
8329
+ output = execFileSync("git", ["rev-list", "--max-parents=0", "HEAD"], {
8330
+ cwd: canonical,
8331
+ encoding: "utf8",
8332
+ env: { ...process.env, LC_ALL: "C", LANG: "C" },
8333
+ stdio: ["ignore", "pipe", "pipe"],
8334
+ timeout: GIT_TIMEOUT_MS
8335
+ });
8336
+ } catch (error) {
8337
+ throw classifyGitError(error, directory);
8338
+ }
8339
+ const firstLine = output.split(`
8340
+ `)[0]?.trim() ?? "";
8341
+ const rootCommit = firstLine.slice(0, 64);
8342
+ if (rootCommit.length < 7) {
8343
+ throw new ProjectIdentityError("unknown", directory, "git rev-list returned no valid root commit hash");
8344
+ }
8345
+ const identity = `git:${rootCommit}`;
8346
+ identityCache.set(canonical, identity);
8347
+ return identity;
8348
+ }
8349
+ function shouldUseDirectoryFallback(error) {
8350
+ return error.errorClass === "not_git_repo" || error.errorClass === "unknown" && error.message.startsWith("Unable to access project directory:");
8351
+ }
8352
+ function resolveProjectIdentity(directory) {
8353
+ const canonical = path3.resolve(directory);
8354
+ const cachedFallback = directoryFallbackCache.get(canonical);
8355
+ if (cachedFallback !== undefined) {
8356
+ if (!hasGitDir(canonical)) {
8357
+ return cachedFallback;
8358
+ }
8359
+ directoryFallbackCache.delete(canonical);
8360
+ }
8361
+ try {
8362
+ return resolveProjectIdentityStrict(directory);
8363
+ } catch (error) {
8364
+ if (error instanceof ProjectIdentityError && shouldUseDirectoryFallback(error)) {
8365
+ const fallback = directoryFallback(canonical);
8366
+ if (!hasGitDir(canonical)) {
8367
+ directoryFallbackCache.set(canonical, fallback);
8368
+ }
8369
+ return fallback;
8370
+ }
8371
+ throw error;
8372
+ }
8373
+ }
8374
+ function hasGitDir(canonical) {
8375
+ try {
8376
+ statSync(path3.join(canonical, ".git"));
8377
+ return true;
8378
+ } catch {
8379
+ return false;
8380
+ }
8381
+ }
8382
+ function normalizeStoredProjectPath(rawOrStored) {
8383
+ if (rawOrStored.startsWith("git:") || rawOrStored.startsWith("dir:")) {
8384
+ return rawOrStored;
8385
+ }
8386
+ try {
8387
+ return resolveProjectIdentity(rawOrStored);
8388
+ } catch {
8389
+ return directoryFallback(rawOrStored);
8390
+ }
8391
+ }
8392
+ function storedPathBelongsToIdentity(storedProjectPath, projectIdentity) {
8393
+ return storedProjectPath === projectIdentity || normalizeStoredProjectPath(storedProjectPath) === projectIdentity;
8394
+ }
8395
+ // ../plugin/src/features/magic-context/workspaces.ts
8396
+ var VALID_SHARE_CATEGORIES = new Set(V2_MEMORY_CATEGORIES);
8397
+ function tableExists(db, tableName) {
8398
+ const row = db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name = ? LIMIT 1").get(tableName);
8399
+ return Boolean(row);
8400
+ }
8401
+ function columnExists(db, tableName, columnName) {
8402
+ const rows = db.prepare(`PRAGMA table_info(${tableName})`).all();
8403
+ return rows.some((row) => row.name === columnName);
8404
+ }
8405
+ function uniqueSorted(values) {
8406
+ return [...new Set(values)].sort((left, right) => left.localeCompare(right));
8407
+ }
8408
+ function placeholders(values) {
8409
+ return values.map(() => "?").join(", ");
8410
+ }
8411
+ function normalizeShareCategories(raw) {
8412
+ if (raw === null || raw === undefined)
8413
+ return null;
8414
+ if (typeof raw !== "string")
8415
+ return null;
8416
+ let parsed;
8417
+ try {
8418
+ parsed = JSON.parse(raw);
8419
+ } catch {
8420
+ return null;
8421
+ }
8422
+ if (!Array.isArray(parsed))
8423
+ return null;
8424
+ const categories = [];
8425
+ for (const value of parsed) {
8426
+ if (typeof value !== "string" || !VALID_SHARE_CATEGORIES.has(value)) {
8427
+ return null;
8428
+ }
8429
+ if (!categories.includes(value))
8430
+ categories.push(value);
8431
+ }
8432
+ return categories.sort((left, right) => left.localeCompare(right));
8433
+ }
8434
+ function selectWorkspaceShareCategories(db, identities) {
8435
+ const candidates = uniqueSorted(identities.filter((identity) => identity.length > 0));
8436
+ if (candidates.length === 0 || !tableExists(db, "workspace_members") || !tableExists(db, "workspaces") || !columnExists(db, "workspaces", "share_categories")) {
8437
+ return null;
8438
+ }
8439
+ const row = db.prepare(`SELECT workspace.share_categories AS shareCategories
8440
+ FROM workspace_members AS member
8441
+ JOIN workspaces AS workspace ON workspace.id = member.workspace_id
8442
+ WHERE member.project_path IN (${placeholders(candidates)})
8443
+ ORDER BY workspace.id ASC
8444
+ LIMIT 1`).get(...candidates);
8445
+ return normalizeShareCategories(row?.shareCategories ?? null);
8446
+ }
8447
+ function resolveWorkspaceShareCategories(db, projectIdentity) {
8448
+ return selectWorkspaceShareCategories(db, [projectIdentity]);
8449
+ }
8450
+ function resolveWorkspaceIdentitySet(db, projectIdentity) {
8451
+ if (!tableExists(db, "workspace_members")) {
8452
+ return { identities: [projectIdentity], namesByIdentity: new Map };
8453
+ }
8454
+ const rows = db.prepare(`SELECT member.project_path AS identity, member.display_name AS displayName
8455
+ FROM workspace_members AS anchor
8456
+ JOIN workspace_members AS member ON member.workspace_id = anchor.workspace_id
8457
+ WHERE anchor.project_path = ?
8458
+ ORDER BY member.display_name ASC, member.project_path ASC`).all(projectIdentity);
8459
+ if (rows.length === 0) {
8460
+ return { identities: [projectIdentity], namesByIdentity: new Map };
8461
+ }
8462
+ const namesByIdentity = new Map;
8463
+ const identities = [];
8464
+ for (const row of rows) {
8465
+ if (typeof row.identity !== "string" || row.identity.length === 0)
8466
+ continue;
8467
+ if (identities.includes(row.identity))
8468
+ continue;
8469
+ identities.push(row.identity);
8470
+ if (typeof row.displayName === "string" && row.displayName.length > 0) {
8471
+ namesByIdentity.set(row.identity, row.displayName);
8472
+ }
8473
+ }
8474
+ return identities.length > 0 ? { identities, namesByIdentity } : { identities: [projectIdentity], namesByIdentity: new Map };
8475
+ }
8476
+ function expandWorkspaceIdentitySetWithAliases(db, identities) {
8477
+ const canonical = uniqueSorted(identities.filter((identity) => identity.length > 0));
8478
+ const expanded = new Set(canonical);
8479
+ const canonicalIdentityByStoredPath = new Map;
8480
+ for (const identity of canonical) {
8481
+ canonicalIdentityByStoredPath.set(identity, identity);
8482
+ }
8483
+ if (canonical.length === 0 || !tableExists(db, "v22_identity_rekey_map")) {
8484
+ return { expandedIdentities: [...expanded], canonicalIdentityByStoredPath };
8485
+ }
8486
+ const rows = db.prepare(`SELECT old_project_path AS oldProjectPath, new_project_path AS newProjectPath
8487
+ FROM v22_identity_rekey_map
8488
+ WHERE new_project_path IN (${placeholders(canonical)})
8489
+ ORDER BY old_project_path ASC`).all(...canonical);
8490
+ for (const row of rows) {
8491
+ if (typeof row.oldProjectPath !== "string" || typeof row.newProjectPath !== "string") {
8492
+ continue;
8493
+ }
8494
+ if (!canonicalIdentityByStoredPath.has(row.newProjectPath))
8495
+ continue;
8496
+ expanded.add(row.oldProjectPath);
8497
+ canonicalIdentityByStoredPath.set(row.oldProjectPath, row.newProjectPath);
8498
+ }
8499
+ return { expandedIdentities: [...expanded], canonicalIdentityByStoredPath };
8500
+ }
8501
+ function resolveStoredPathWorkspaceIdentity(storedProjectPath, memberIdentities, canonicalIdentityByStoredPath) {
8502
+ const direct = canonicalIdentityByStoredPath.get(storedProjectPath);
8503
+ if (direct)
8504
+ return direct;
8505
+ const normalized = normalizeStoredProjectPath(storedProjectPath);
8506
+ const normalizedDirect = canonicalIdentityByStoredPath.get(normalized);
8507
+ if (normalizedDirect)
8508
+ return normalizedDirect;
8509
+ if (memberIdentities.includes(normalized))
8510
+ return normalized;
8511
+ for (const identity of memberIdentities) {
8512
+ if (storedPathBelongsToIdentity(storedProjectPath, identity)) {
8513
+ return identity;
8514
+ }
8515
+ }
8516
+ return null;
8517
+ }
8518
+ function storedPathBelongsToWorkspace(storedProjectPath, memberIdentities, expandedIdentities, canonicalIdentityByStoredPath) {
8519
+ if (expandedIdentities.includes(storedProjectPath))
8520
+ return true;
8521
+ return resolveStoredPathWorkspaceIdentity(storedProjectPath, memberIdentities, canonicalIdentityByStoredPath) !== null;
8522
+ }
8523
+ function sourceNameForMemory(storedProjectPath, ownIdentity, memberIdentities, namesByIdentity, canonicalIdentityByStoredPath) {
8524
+ const canonicalIdentity = resolveStoredPathWorkspaceIdentity(storedProjectPath, memberIdentities, canonicalIdentityByStoredPath);
8525
+ if (!canonicalIdentity || canonicalIdentity === ownIdentity)
8526
+ return;
8527
+ return namesByIdentity.get(canonicalIdentity);
8528
+ }
8529
+ function getEpochMap(db, identities) {
8530
+ if (identities.length === 0)
8531
+ return new Map;
8532
+ const rows = db.prepare(`SELECT project_path AS projectPath, project_memory_epoch AS epoch
8533
+ FROM project_state
8534
+ WHERE project_path IN (${placeholders(identities)})`).all(...identities);
8535
+ const epochs = new Map;
8536
+ for (const row of rows) {
8537
+ if (typeof row.projectPath !== "string" || typeof row.epoch !== "number")
8538
+ continue;
8539
+ epochs.set(row.projectPath, row.epoch);
8540
+ }
8541
+ return epochs;
8542
+ }
8543
+ function computeWorkspaceEpochFingerprint(db, identities) {
8544
+ const canonical = uniqueSorted(identities.filter((identity) => identity.length > 0));
8545
+ const epochs = getEpochMap(db, canonical);
8546
+ const shareCategories = selectWorkspaceShareCategories(db, canonical);
8547
+ const hash = createHash3("sha256");
8548
+ hash.update("share_categories", "utf8");
8549
+ hash.update("\x00");
8550
+ hash.update(shareCategories === null ? "ALL" : JSON.stringify(shareCategories), "utf8");
8551
+ hash.update(`
8552
+ `);
8553
+ for (const identity of canonical) {
8554
+ hash.update(identity, "utf8");
8555
+ hash.update("\x00");
8556
+ hash.update(String(epochs.get(identity) ?? 0), "utf8");
8557
+ hash.update(`
8558
+ `);
8559
+ }
8560
+ return hash.digest("hex");
8561
+ }
8562
+ function isInTransaction(db) {
8563
+ const candidate = db;
8564
+ return candidate.inTransaction === true || candidate.isTransaction === true;
8565
+ }
8566
+ function workspaceMembersForIdentity(db, identity) {
8567
+ if (!tableExists(db, "workspace_members"))
8568
+ return [identity];
8569
+ const rows = db.prepare(`SELECT member.project_path AS identity
8570
+ FROM workspace_members AS anchor
8571
+ JOIN workspace_members AS member ON member.workspace_id = anchor.workspace_id
8572
+ WHERE anchor.project_path = ?
8573
+ ORDER BY member.project_path ASC`).all(identity);
8574
+ const identities = rows.map((row) => typeof row.identity === "string" ? row.identity : "").filter((value) => value.length > 0);
8575
+ return identities.length > 0 ? uniqueSorted(identities) : [identity];
8576
+ }
8577
+ function bumpEpochRows(db, identities, now) {
8578
+ const stmt = db.prepare(`INSERT INTO project_state
8579
+ (project_path, project_memory_epoch, project_user_profile_version, updated_at)
8580
+ VALUES (?, 1, 0, ?)
8581
+ ON CONFLICT(project_path) DO UPDATE SET
8582
+ project_memory_epoch = project_memory_epoch + 1,
8583
+ updated_at = excluded.updated_at`);
8584
+ for (const identity of uniqueSorted(identities)) {
8585
+ stmt.run(identity, now);
8586
+ }
8587
+ }
8588
+ function bumpEpochsForWorkspaceMembers(db, identity, now = Date.now()) {
8589
+ const run = () => bumpEpochRows(db, workspaceMembersForIdentity(db, identity), now);
8590
+ if (isInTransaction(db)) {
8591
+ run();
8592
+ return;
8593
+ }
8594
+ db.exec("BEGIN IMMEDIATE");
8595
+ try {
8596
+ run();
8597
+ db.exec("COMMIT");
8598
+ } catch (error) {
8599
+ try {
8600
+ db.exec("ROLLBACK");
8601
+ } catch {}
8602
+ throw error;
8603
+ }
8604
+ }
8605
+ function bumpEpochsForWorkspaceMemberSet(db, identities, now = Date.now()) {
8606
+ const run = () => bumpEpochRows(db, identities, now);
8607
+ if (isInTransaction(db)) {
8608
+ run();
8609
+ return;
8610
+ }
8611
+ db.exec("BEGIN IMMEDIATE");
8612
+ try {
8613
+ run();
8614
+ db.exec("COMMIT");
8615
+ } catch (error) {
8616
+ try {
8617
+ db.exec("ROLLBACK");
8618
+ } catch {}
8619
+ throw error;
8620
+ }
8621
+ }
8622
+
8623
+ // ../plugin/src/features/magic-context/migrations.ts
8624
+ function tableExists2(db, name) {
8168
8625
  return Boolean(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = ?").get(name));
8169
8626
  }
8627
+ function columnExists2(db, table, column) {
8628
+ const rows = db.prepare(`PRAGMA table_info(${table})`).all();
8629
+ return rows.some((row) => row.name === column);
8630
+ }
8170
8631
  var MIGRATIONS = [
8171
8632
  {
8172
8633
  version: 1,
@@ -8625,9 +9086,9 @@ var MIGRATIONS = [
8625
9086
  version: 22,
8626
9087
  description: "v2.0 cache architecture schema foundation",
8627
9088
  up: (db) => {
8628
- const hasSessionMetaTable = tableExists(db, "session_meta");
8629
- const hasCompartmentsTable = tableExists(db, "compartments");
8630
- const hasMemoriesTable = tableExists(db, "memories");
9089
+ const hasSessionMetaTable = tableExists2(db, "session_meta");
9090
+ const hasCompartmentsTable = tableExists2(db, "compartments");
9091
+ const hasMemoriesTable = tableExists2(db, "memories");
8631
9092
  if (hasSessionMetaTable) {
8632
9093
  ensureColumn(db, "session_meta", "cached_m0_bytes", "BLOB");
8633
9094
  ensureColumn(db, "session_meta", "cached_m0_project_memory_epoch", "INTEGER");
@@ -8652,7 +9113,7 @@ var MIGRATIONS = [
8652
9113
  ensureColumn(db, "compartments", "p1_embedding_model_id", "TEXT");
8653
9114
  ensureColumn(db, "compartments", "legacy", "INTEGER NOT NULL DEFAULT 0");
8654
9115
  }
8655
- const hasRecompCompartmentsTable = tableExists(db, "recomp_compartments");
9116
+ const hasRecompCompartmentsTable = tableExists2(db, "recomp_compartments");
8656
9117
  if (hasRecompCompartmentsTable) {
8657
9118
  ensureColumn(db, "recomp_compartments", "p1", "TEXT");
8658
9119
  ensureColumn(db, "recomp_compartments", "p2", "TEXT");
@@ -8963,6 +9424,156 @@ var MIGRATIONS = [
8963
9424
  db.prepare("UPDATE session_meta SET force_emergency_bypass_used = 0 WHERE force_emergency_bypass_used IS NULL").run();
8964
9425
  db.prepare("UPDATE session_meta SET last_usage_context_limit = 0 WHERE last_usage_context_limit IS NULL").run();
8965
9426
  }
9427
+ },
9428
+ {
9429
+ version: 33,
9430
+ description: "Compartment chunk embeddings for semantic message-history search",
9431
+ up: (db) => {
9432
+ db.exec(`
9433
+ CREATE TABLE IF NOT EXISTS compartment_chunk_embeddings (
9434
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
9435
+ compartment_id INTEGER NOT NULL REFERENCES compartments(id) ON DELETE CASCADE,
9436
+ session_id TEXT NOT NULL,
9437
+ project_path TEXT NOT NULL,
9438
+ harness TEXT NOT NULL DEFAULT 'opencode',
9439
+ window_index INTEGER NOT NULL DEFAULT 0,
9440
+ start_ordinal INTEGER NOT NULL,
9441
+ end_ordinal INTEGER NOT NULL,
9442
+ chunk_hash TEXT NOT NULL,
9443
+ model_id TEXT NOT NULL,
9444
+ dims INTEGER NOT NULL,
9445
+ vector BLOB NOT NULL,
9446
+ created_at INTEGER NOT NULL,
9447
+ UNIQUE(compartment_id, window_index)
9448
+ );
9449
+ CREATE INDEX IF NOT EXISTS idx_cce_session
9450
+ ON compartment_chunk_embeddings(session_id);
9451
+ CREATE INDEX IF NOT EXISTS idx_cce_project_model
9452
+ ON compartment_chunk_embeddings(project_path, model_id);
9453
+ `);
9454
+ }
9455
+ },
9456
+ {
9457
+ version: 34,
9458
+ description: "workspace tables and m[0] workspace fingerprint cache reset",
9459
+ up: (db) => {
9460
+ db.exec(`
9461
+ CREATE TABLE IF NOT EXISTS workspaces (
9462
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
9463
+ name TEXT NOT NULL UNIQUE,
9464
+ created_at INTEGER NOT NULL,
9465
+ updated_at INTEGER NOT NULL
9466
+ );
9467
+ CREATE TABLE IF NOT EXISTS workspace_members (
9468
+ workspace_id INTEGER NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
9469
+ project_path TEXT NOT NULL,
9470
+ display_name TEXT NOT NULL,
9471
+ display_path TEXT NOT NULL,
9472
+ added_at INTEGER NOT NULL,
9473
+ PRIMARY KEY (workspace_id, project_path)
9474
+ );
9475
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_workspace_member_unique
9476
+ ON workspace_members(project_path);
9477
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_workspace_member_name
9478
+ ON workspace_members(workspace_id, display_name);
9479
+ `);
9480
+ const hasSessionMeta = db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='session_meta' LIMIT 1").get();
9481
+ if (!hasSessionMeta)
9482
+ return;
9483
+ ensureColumn(db, "session_meta", "cached_m0_workspace_fingerprint", "TEXT");
9484
+ const columns = new Set(db.prepare("PRAGMA table_info(session_meta)").all().map((column) => column.name));
9485
+ const clears = [
9486
+ ["cached_m0_bytes", null],
9487
+ ["cached_m1_bytes", null],
9488
+ ["cached_m0_project_memory_epoch", null],
9489
+ ["cached_m0_workspace_fingerprint", null],
9490
+ ["cached_m0_project_user_profile_version", null],
9491
+ ["cached_m0_max_compartment_seq", null],
9492
+ ["cached_m0_max_memory_id", null],
9493
+ ["cached_m0_max_mutation_id", null],
9494
+ ["cached_m0_max_memory_mutation_id", null],
9495
+ ["cached_m0_project_docs_hash", null],
9496
+ ["cached_m0_materialized_at", null],
9497
+ ["cached_m0_session_facts_version", null],
9498
+ ["cached_m0_upgrade_state", null],
9499
+ ["cached_m0_system_hash", null],
9500
+ ["cached_m0_tool_set_hash", null],
9501
+ ["cached_m0_model_key", null],
9502
+ ["cached_m0_last_baseline_end_message_id", null],
9503
+ ["memory_block_cache", ""],
9504
+ ["memory_block_ids", ""],
9505
+ ["memory_block_count", 0]
9506
+ ];
9507
+ const setClauses = [];
9508
+ const values = [];
9509
+ for (const [column, value] of clears) {
9510
+ if (!columns.has(column))
9511
+ continue;
9512
+ setClauses.push(`${column} = ?`);
9513
+ values.push(value);
9514
+ }
9515
+ if (setClauses.length > 0) {
9516
+ db.prepare(`UPDATE session_meta SET ${setClauses.join(", ")}`).run(...values);
9517
+ }
9518
+ }
9519
+ },
9520
+ {
9521
+ version: 35,
9522
+ description: "workspace per-category share defaults and epoch refresh",
9523
+ up: (db) => {
9524
+ db.exec(`
9525
+ CREATE TABLE IF NOT EXISTS workspaces (
9526
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
9527
+ name TEXT NOT NULL UNIQUE,
9528
+ created_at INTEGER NOT NULL,
9529
+ updated_at INTEGER NOT NULL,
9530
+ share_categories TEXT NOT NULL DEFAULT '["CONSTRAINTS"]'
9531
+ );
9532
+ `);
9533
+ if (!columnExists2(db, "workspaces", "share_categories")) {
9534
+ db.exec(`ALTER TABLE workspaces ADD COLUMN share_categories TEXT NOT NULL DEFAULT '["CONSTRAINTS"]'`);
9535
+ }
9536
+ db.prepare(`UPDATE workspaces
9537
+ SET share_categories = '["CONSTRAINTS"]'
9538
+ WHERE share_categories IS NULL OR share_categories = ''`).run();
9539
+ if (!tableExists2(db, "workspace_members"))
9540
+ return;
9541
+ const rows = db.prepare(`SELECT DISTINCT project_path AS identity
9542
+ FROM workspace_members
9543
+ WHERE project_path IS NOT NULL AND project_path <> ''
9544
+ ORDER BY project_path ASC`).all();
9545
+ const identities = rows.map((row) => typeof row.identity === "string" ? row.identity : "").filter((identity) => identity.length > 0);
9546
+ if (identities.length > 0) {
9547
+ bumpEpochsForWorkspaceMemberSet(db, identities, Date.now());
9548
+ }
9549
+ }
9550
+ },
9551
+ {
9552
+ version: 36,
9553
+ description: "session project ownership map for compartment chunk backfill scoping",
9554
+ up: (db) => {
9555
+ db.exec(`
9556
+ CREATE TABLE IF NOT EXISTS session_projects (
9557
+ session_id TEXT NOT NULL,
9558
+ harness TEXT NOT NULL DEFAULT 'opencode',
9559
+ project_path TEXT NOT NULL,
9560
+ updated_at INTEGER NOT NULL,
9561
+ PRIMARY KEY(session_id, harness)
9562
+ );
9563
+ CREATE INDEX IF NOT EXISTS idx_session_projects_project
9564
+ ON session_projects(project_path);
9565
+ `);
9566
+ const hasChunkTable = db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='compartment_chunk_embeddings'").get();
9567
+ if (hasChunkTable) {
9568
+ db.exec(`
9569
+ INSERT OR IGNORE INTO session_projects (session_id, harness, project_path, updated_at)
9570
+ SELECT session_id, harness, MIN(project_path), 0
9571
+ FROM compartment_chunk_embeddings
9572
+ GROUP BY session_id, harness
9573
+ HAVING COUNT(DISTINCT project_path) = 1;
9574
+ `);
9575
+ }
9576
+ }
8966
9577
  }
8967
9578
  ];
8968
9579
  var LATEST_MIGRATION_VERSION = MIGRATIONS.reduce((max, m) => Math.max(max, m.version), 0);
@@ -141940,7 +142551,7 @@ var databases = new Map;
141940
142551
  var persistenceByDatabase = new WeakMap;
141941
142552
  var persistenceErrorByDatabase = new WeakMap;
141942
142553
  var lastSchemaFenceRejection = null;
141943
- var LATEST_SUPPORTED_VERSION = 32;
142554
+ var LATEST_SUPPORTED_VERSION = 36;
141944
142555
  function resolveDatabasePath(dbPathOverride) {
141945
142556
  if (dbPathOverride) {
141946
142557
  return { dbDir: dirname2(dbPathOverride), dbPath: dbPathOverride };
@@ -142106,6 +142717,35 @@ function initializeDatabase(db) {
142106
142717
  );
142107
142718
  CREATE INDEX IF NOT EXISTS idx_compartments_session ON compartments(session_id);
142108
142719
 
142720
+ CREATE TABLE IF NOT EXISTS compartment_chunk_embeddings (
142721
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
142722
+ compartment_id INTEGER NOT NULL REFERENCES compartments(id) ON DELETE CASCADE,
142723
+ session_id TEXT NOT NULL,
142724
+ project_path TEXT NOT NULL,
142725
+ harness TEXT NOT NULL DEFAULT 'opencode',
142726
+ window_index INTEGER NOT NULL DEFAULT 0,
142727
+ start_ordinal INTEGER NOT NULL,
142728
+ end_ordinal INTEGER NOT NULL,
142729
+ chunk_hash TEXT NOT NULL,
142730
+ model_id TEXT NOT NULL,
142731
+ dims INTEGER NOT NULL,
142732
+ vector BLOB NOT NULL,
142733
+ created_at INTEGER NOT NULL,
142734
+ UNIQUE(compartment_id, window_index)
142735
+ );
142736
+ CREATE INDEX IF NOT EXISTS idx_cce_session ON compartment_chunk_embeddings(session_id);
142737
+ CREATE INDEX IF NOT EXISTS idx_cce_project_model ON compartment_chunk_embeddings(project_path, model_id);
142738
+
142739
+ CREATE TABLE IF NOT EXISTS session_projects (
142740
+ session_id TEXT NOT NULL,
142741
+ harness TEXT NOT NULL DEFAULT 'opencode',
142742
+ project_path TEXT NOT NULL,
142743
+ updated_at INTEGER NOT NULL,
142744
+ PRIMARY KEY(session_id, harness)
142745
+ );
142746
+ CREATE INDEX IF NOT EXISTS idx_session_projects_project
142747
+ ON session_projects(project_path);
142748
+
142109
142749
  CREATE TABLE IF NOT EXISTS compartment_events (
142110
142750
  id INTEGER PRIMARY KEY AUTOINCREMENT,
142111
142751
  session_id TEXT NOT NULL,
@@ -142291,6 +142931,25 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
142291
142931
  rekeyed_at INTEGER NOT NULL
142292
142932
  );
142293
142933
 
142934
+ CREATE TABLE IF NOT EXISTS workspaces (
142935
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
142936
+ name TEXT NOT NULL UNIQUE,
142937
+ created_at INTEGER NOT NULL,
142938
+ updated_at INTEGER NOT NULL,
142939
+ share_categories TEXT NOT NULL DEFAULT '["CONSTRAINTS"]'
142940
+ );
142941
+
142942
+ CREATE TABLE IF NOT EXISTS workspace_members (
142943
+ workspace_id INTEGER NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
142944
+ project_path TEXT NOT NULL,
142945
+ display_name TEXT NOT NULL,
142946
+ display_path TEXT NOT NULL,
142947
+ added_at INTEGER NOT NULL,
142948
+ PRIMARY KEY (workspace_id, project_path)
142949
+ );
142950
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_workspace_member_unique ON workspace_members(project_path);
142951
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_workspace_member_name ON workspace_members(workspace_id, display_name);
142952
+
142294
142953
  CREATE TABLE IF NOT EXISTS v22_backfill_failures (
142295
142954
  id INTEGER PRIMARY KEY AUTOINCREMENT,
142296
142955
  table_name TEXT NOT NULL,
@@ -142402,6 +143061,7 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
142402
143061
  deferred_execute_state TEXT,
142403
143062
  cached_m0_bytes BLOB,
142404
143063
  cached_m0_project_memory_epoch INTEGER,
143064
+ cached_m0_workspace_fingerprint TEXT,
142405
143065
  cached_m0_project_user_profile_version INTEGER,
142406
143066
  cached_m0_max_compartment_seq INTEGER,
142407
143067
  cached_m0_max_memory_id INTEGER,
@@ -142560,6 +143220,7 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
142560
143220
  ensureColumn(db, "session_meta", "cleared_reasoning_through_tag", "INTEGER DEFAULT 0");
142561
143221
  ensureColumn(db, "session_meta", "stripped_placeholder_ids", "TEXT DEFAULT ''");
142562
143222
  ensureColumn(db, "session_meta", "stale_reduce_stripped_ids", "TEXT DEFAULT ''");
143223
+ ensureColumn(db, "session_meta", "processed_image_stripped_ids", "TEXT DEFAULT ''");
142563
143224
  ensureColumn(db, "compartments", "start_message_id", "TEXT DEFAULT ''");
142564
143225
  ensureColumn(db, "compartments", "end_message_id", "TEXT DEFAULT ''");
142565
143226
  ensureColumn(db, "memory_embeddings", "model_id", "TEXT");
@@ -142613,6 +143274,7 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
142613
143274
  ensureColumn(db, "memories", "importance", "INTEGER");
142614
143275
  ensureColumn(db, "session_meta", "cached_m0_bytes", "BLOB");
142615
143276
  ensureColumn(db, "session_meta", "cached_m0_project_memory_epoch", "INTEGER");
143277
+ ensureColumn(db, "session_meta", "cached_m0_workspace_fingerprint", "TEXT");
142616
143278
  ensureColumn(db, "session_meta", "cached_m0_project_user_profile_version", "INTEGER");
142617
143279
  ensureColumn(db, "session_meta", "cached_m0_max_compartment_seq", "INTEGER");
142618
143280
  ensureColumn(db, "session_meta", "cached_m0_max_memory_id", "INTEGER");
@@ -142644,6 +143306,15 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
142644
143306
  project_user_profile_version INTEGER NOT NULL DEFAULT 0,
142645
143307
  updated_at INTEGER NOT NULL DEFAULT 0
142646
143308
  );
143309
+ CREATE TABLE IF NOT EXISTS session_projects (
143310
+ session_id TEXT NOT NULL,
143311
+ harness TEXT NOT NULL DEFAULT 'opencode',
143312
+ project_path TEXT NOT NULL,
143313
+ updated_at INTEGER NOT NULL,
143314
+ PRIMARY KEY(session_id, harness)
143315
+ );
143316
+ CREATE INDEX IF NOT EXISTS idx_session_projects_project
143317
+ ON session_projects(project_path);
142647
143318
  CREATE TABLE IF NOT EXISTS m0_mutation_log (
142648
143319
  id INTEGER PRIMARY KEY AUTOINCREMENT,
142649
143320
  session_id TEXT NOT NULL,
@@ -142671,6 +143342,23 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
142671
143342
  new_project_path TEXT NOT NULL,
142672
143343
  rekeyed_at INTEGER NOT NULL
142673
143344
  );
143345
+ CREATE TABLE IF NOT EXISTS workspaces (
143346
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
143347
+ name TEXT NOT NULL UNIQUE,
143348
+ created_at INTEGER NOT NULL,
143349
+ updated_at INTEGER NOT NULL,
143350
+ share_categories TEXT NOT NULL DEFAULT '["CONSTRAINTS"]'
143351
+ );
143352
+ CREATE TABLE IF NOT EXISTS workspace_members (
143353
+ workspace_id INTEGER NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
143354
+ project_path TEXT NOT NULL,
143355
+ display_name TEXT NOT NULL,
143356
+ display_path TEXT NOT NULL,
143357
+ added_at INTEGER NOT NULL,
143358
+ PRIMARY KEY (workspace_id, project_path)
143359
+ );
143360
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_workspace_member_unique ON workspace_members(project_path);
143361
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_workspace_member_name ON workspace_members(workspace_id, display_name);
142674
143362
  CREATE TABLE IF NOT EXISTS v22_backfill_failures (
142675
143363
  id INTEGER PRIMARY KEY AUTOINCREMENT,
142676
143364
  table_name TEXT NOT NULL,
@@ -142692,6 +143380,7 @@ CREATE INDEX IF NOT EXISTS idx_dream_queue_pending ON dream_queue(started_at, en
142692
143380
  ensureColumn(db, "recomp_compartments", "harness", "TEXT NOT NULL DEFAULT 'opencode'");
142693
143381
  ensureColumn(db, "recomp_facts", "harness", "TEXT NOT NULL DEFAULT 'opencode'");
142694
143382
  ensureColumn(db, "message_history_index", "harness", "TEXT NOT NULL DEFAULT 'opencode'");
143383
+ ensureColumn(db, "workspaces", "share_categories", `TEXT NOT NULL DEFAULT '["CONSTRAINTS"]'`);
142695
143384
  }
142696
143385
  function healAllNullColumns(db) {
142697
143386
  healNullTextColumns(db);
@@ -142730,6 +143419,7 @@ function healNullTextColumns(db) {
142730
143419
  ["system_prompt_hash", ""],
142731
143420
  ["stripped_placeholder_ids", ""],
142732
143421
  ["stale_reduce_stripped_ids", ""],
143422
+ ["processed_image_stripped_ids", ""],
142733
143423
  ["memory_block_cache", ""],
142734
143424
  ["memory_block_ids", ""],
142735
143425
  ["compaction_marker_state", ""],
@@ -142774,7 +143464,7 @@ function healNullIntegerColumns(db) {
142774
143464
  }
142775
143465
  }
142776
143466
  function ensureColumn(db, table, column, definition) {
142777
- if (!/^[a-z][a-z0-9_]*$/.test(table) || !/^[a-z][a-z0-9_]*$/.test(column) || !/^[A-Z0-9_'(),[\]\s]+$/i.test(definition)) {
143467
+ if (!/^[a-z][a-z0-9_]*$/.test(table) || !/^[a-z][a-z0-9_]*$/.test(column) || !/^[A-Z0-9_"'(),[\]\s]+$/i.test(definition)) {
142778
143468
  throw new Error(`Unsafe schema identifier: ${table}.${column} ${definition}`);
142779
143469
  }
142780
143470
  const rows = db.prepare(`PRAGMA table_info(${table})`).all();
@@ -143852,10 +144542,10 @@ function mergeDefs(...defs) {
143852
144542
  function cloneDef(schema) {
143853
144543
  return mergeDefs(schema._zod.def);
143854
144544
  }
143855
- function getElementAtPath(obj, path3) {
143856
- if (!path3)
144545
+ function getElementAtPath(obj, path4) {
144546
+ if (!path4)
143857
144547
  return obj;
143858
- return path3.reduce((acc, key) => acc?.[key], obj);
144548
+ return path4.reduce((acc, key) => acc?.[key], obj);
143859
144549
  }
143860
144550
  function promiseAllObject(promisesObj) {
143861
144551
  const keys = Object.keys(promisesObj);
@@ -144263,11 +144953,11 @@ function explicitlyAborted(x, startIndex = 0) {
144263
144953
  }
144264
144954
  return false;
144265
144955
  }
144266
- function prefixIssues(path3, issues) {
144956
+ function prefixIssues(path4, issues) {
144267
144957
  return issues.map((iss) => {
144268
144958
  var _a2;
144269
144959
  (_a2 = iss).path ?? (_a2.path = []);
144270
- iss.path.unshift(path3);
144960
+ iss.path.unshift(path4);
144271
144961
  return iss;
144272
144962
  });
144273
144963
  }
@@ -144414,16 +145104,16 @@ function flattenError(error, mapper = (issue2) => issue2.message) {
144414
145104
  }
144415
145105
  function formatError(error, mapper = (issue2) => issue2.message) {
144416
145106
  const fieldErrors = { _errors: [] };
144417
- const processError = (error2, path3 = []) => {
145107
+ const processError = (error2, path4 = []) => {
144418
145108
  for (const issue2 of error2.issues) {
144419
145109
  if (issue2.code === "invalid_union" && issue2.errors.length) {
144420
- issue2.errors.map((issues) => processError({ issues }, [...path3, ...issue2.path]));
145110
+ issue2.errors.map((issues) => processError({ issues }, [...path4, ...issue2.path]));
144421
145111
  } else if (issue2.code === "invalid_key") {
144422
- processError({ issues: issue2.issues }, [...path3, ...issue2.path]);
145112
+ processError({ issues: issue2.issues }, [...path4, ...issue2.path]);
144423
145113
  } else if (issue2.code === "invalid_element") {
144424
- processError({ issues: issue2.issues }, [...path3, ...issue2.path]);
145114
+ processError({ issues: issue2.issues }, [...path4, ...issue2.path]);
144425
145115
  } else {
144426
- const fullpath = [...path3, ...issue2.path];
145116
+ const fullpath = [...path4, ...issue2.path];
144427
145117
  if (fullpath.length === 0) {
144428
145118
  fieldErrors._errors.push(mapper(issue2));
144429
145119
  } else {
@@ -144450,17 +145140,17 @@ function formatError(error, mapper = (issue2) => issue2.message) {
144450
145140
  }
144451
145141
  function treeifyError(error, mapper = (issue2) => issue2.message) {
144452
145142
  const result = { errors: [] };
144453
- const processError = (error2, path3 = []) => {
145143
+ const processError = (error2, path4 = []) => {
144454
145144
  var _a2, _b;
144455
145145
  for (const issue2 of error2.issues) {
144456
145146
  if (issue2.code === "invalid_union" && issue2.errors.length) {
144457
- issue2.errors.map((issues) => processError({ issues }, [...path3, ...issue2.path]));
145147
+ issue2.errors.map((issues) => processError({ issues }, [...path4, ...issue2.path]));
144458
145148
  } else if (issue2.code === "invalid_key") {
144459
- processError({ issues: issue2.issues }, [...path3, ...issue2.path]);
145149
+ processError({ issues: issue2.issues }, [...path4, ...issue2.path]);
144460
145150
  } else if (issue2.code === "invalid_element") {
144461
- processError({ issues: issue2.issues }, [...path3, ...issue2.path]);
145151
+ processError({ issues: issue2.issues }, [...path4, ...issue2.path]);
144462
145152
  } else {
144463
- const fullpath = [...path3, ...issue2.path];
145153
+ const fullpath = [...path4, ...issue2.path];
144464
145154
  if (fullpath.length === 0) {
144465
145155
  result.errors.push(mapper(issue2));
144466
145156
  continue;
@@ -144492,8 +145182,8 @@ function treeifyError(error, mapper = (issue2) => issue2.message) {
144492
145182
  }
144493
145183
  function toDotPath(_path) {
144494
145184
  const segs = [];
144495
- const path3 = _path.map((seg) => typeof seg === "object" ? seg.key : seg);
144496
- for (const seg of path3) {
145185
+ const path4 = _path.map((seg) => typeof seg === "object" ? seg.key : seg);
145186
+ for (const seg of path4) {
144497
145187
  if (typeof seg === "number")
144498
145188
  segs.push(`[${seg}]`);
144499
145189
  else if (typeof seg === "symbol")
@@ -156952,13 +157642,13 @@ function resolveRef(ref, ctx) {
156952
157642
  if (!ref.startsWith("#")) {
156953
157643
  throw new Error("External $ref is not supported, only local refs (#/...) are allowed");
156954
157644
  }
156955
- const path3 = ref.slice(1).split("/").filter(Boolean);
156956
- if (path3.length === 0) {
157645
+ const path4 = ref.slice(1).split("/").filter(Boolean);
157646
+ if (path4.length === 0) {
156957
157647
  return ctx.rootSchema;
156958
157648
  }
156959
157649
  const defsKey = ctx.version === "draft-2020-12" ? "$defs" : "definitions";
156960
- if (path3[0] === defsKey) {
156961
- const key = path3[1];
157650
+ if (path4[0] === defsKey) {
157651
+ const key = path4[1];
156962
157652
  if (!key || !ctx.defs[key]) {
156963
157653
  throw new Error(`Reference not found: ${ref}`);
156964
157654
  }
@@ -157446,7 +158136,8 @@ var BaseEmbeddingConfigSchema = exports_external.object({
157446
158136
  endpoint: exports_external.string().optional().describe("API endpoint URL. Required when provider is openai-compatible."),
157447
158137
  api_key: exports_external.string().optional().describe("API key for remote embedding provider (optional)"),
157448
158138
  input_type: exports_external.string().optional().describe("Optional input_type sent in the embedding request body. Required by some openai-compatible providers (e.g. NVIDIA NIM expects 'query' or 'passage'). Omitted from the request when unset."),
157449
- truncate: exports_external.string().optional().describe("Optional truncate mode sent in the embedding request body (e.g. NVIDIA NIM accepts 'NONE' | 'START' | 'END'). Omitted from the request when unset.")
158139
+ truncate: exports_external.string().optional().describe("Optional truncate mode sent in the embedding request body (e.g. NVIDIA NIM accepts 'NONE' | 'START' | 'END'). Omitted from the request when unset."),
158140
+ max_input_tokens: exports_external.number().int().positive().optional().describe("Optional maximum input tokens for chunk embeddings. Defaults conservatively to 512 when omitted.")
157450
158141
  }).superRefine((data, ctx) => {
157451
158142
  if (data.provider === "openai-compatible" && !data.endpoint?.trim()) {
157452
158143
  ctx.addIssue({
@@ -157467,7 +158158,8 @@ var EmbeddingConfigSchema = BaseEmbeddingConfigSchema.transform((data) => {
157467
158158
  if (data.provider === "local") {
157468
158159
  return {
157469
158160
  provider: "local",
157470
- model: data.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL
158161
+ model: data.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
158162
+ ...data.max_input_tokens ? { max_input_tokens: data.max_input_tokens } : {}
157471
158163
  };
157472
158164
  }
157473
158165
  if (data.provider === "openai-compatible") {
@@ -157480,7 +158172,8 @@ var EmbeddingConfigSchema = BaseEmbeddingConfigSchema.transform((data) => {
157480
158172
  endpoint: data.endpoint?.trim() ?? "",
157481
158173
  ...apiKey ? { api_key: apiKey } : {},
157482
158174
  ...inputType ? { input_type: inputType } : {},
157483
- ...truncate ? { truncate } : {}
158175
+ ...truncate ? { truncate } : {},
158176
+ ...data.max_input_tokens ? { max_input_tokens: data.max_input_tokens } : {}
157484
158177
  };
157485
158178
  }
157486
158179
  return { provider: "off" };
@@ -157726,31 +158419,31 @@ function getUserConfigPaths() {
157726
158419
  return [`${basePath}.jsonc`, `${basePath}.json`];
157727
158420
  }
157728
158421
  function resolveFirstExisting(paths) {
157729
- return paths.find((path3) => existsSync5(path3));
158422
+ return paths.find((path4) => existsSync5(path4));
157730
158423
  }
157731
- function loadConfigFile(path3, scope) {
158424
+ function loadConfigFile(path4, scope) {
157732
158425
  try {
157733
- const rawText = readFileSync3(path3, "utf-8");
158426
+ const rawText = readFileSync3(path4, "utf-8");
157734
158427
  const substituted = substituteConfigVariables({
157735
158428
  text: rawText,
157736
- configPath: path3,
158429
+ configPath: path4,
157737
158430
  isProjectConfig: scope === "project"
157738
158431
  });
157739
158432
  return {
157740
- path: path3,
158433
+ path: path4,
157741
158434
  scope,
157742
158435
  config: import_comment_json.parse(substituted.text),
157743
- warnings: substituted.warnings.map((warning) => `${path3}: ${warning}`),
158436
+ warnings: substituted.warnings.map((warning) => `${path4}: ${warning}`),
157744
158437
  loadOutcome: substituted.warnings.length > 0 ? "substitution-failure" : "ok"
157745
158438
  };
157746
158439
  } catch (error51) {
157747
158440
  const message = error51 instanceof Error ? error51.message : String(error51);
157748
158441
  return {
157749
- path: path3,
158442
+ path: path4,
157750
158443
  scope,
157751
158444
  config: {},
157752
158445
  warnings: [
157753
- `${path3}: failed to load config: ${message}; using defaults for this file.`
158446
+ `${path4}: failed to load config: ${message}; using defaults for this file.`
157754
158447
  ],
157755
158448
  loadOutcome: typeof error51.code === "string" ? "project-file-io-error" : "project-file-parse-error"
157756
158449
  };
@@ -157916,9 +158609,9 @@ function bindSubstitutionFailures(loaded) {
157916
158609
  }
157917
158610
  const emptyPaths = collectEmptyStringPaths(loaded.config);
157918
158611
  return loaded.warnings.map((message) => {
157919
- const matchedPath = emptyPaths.find((path3) => {
157920
- const tail = path3.split(".").at(-1) ?? path3;
157921
- return message.includes(path3) || message.toLowerCase().includes(tail.toLowerCase());
158612
+ const matchedPath = emptyPaths.find((path4) => {
158613
+ const tail = path4.split(".").at(-1) ?? path4;
158614
+ return message.includes(path4) || message.toLowerCase().includes(tail.toLowerCase());
157922
158615
  });
157923
158616
  return {
157924
158617
  keyPath: matchedPath ?? "<unknown>",
@@ -158006,6 +158699,443 @@ function loadPiConfigDetailed(opts = {}) {
158006
158699
  // ../plugin/src/features/magic-context/memory/embedding.ts
158007
158700
  init_logger();
158008
158701
 
158702
+ // ../plugin/src/features/magic-context/compartment-chunk-embedding.ts
158703
+ import { createHash as createHash4 } from "node:crypto";
158704
+ var DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS = 512;
158705
+ var CHUNK_WINDOW_SAFETY_RATIO = 0.9;
158706
+ var loadFtsRowsStatements = new WeakMap;
158707
+ var existingHashStatements = new WeakMap;
158708
+ var existingHashByProjectStatements = new WeakMap;
158709
+ var deleteByCompartmentStatements = new WeakMap;
158710
+ var insertEmbeddingStatements = new WeakMap;
158711
+ var distinctModelStatements = new WeakMap;
158712
+ var clearProjectStatements = new WeakMap;
158713
+ var clearProjectModelStatements = new WeakMap;
158714
+ var searchRowsStatements = new WeakMap;
158715
+ var searchRowsByModelStatements = new WeakMap;
158716
+ var backfillCandidateStatements = new WeakMap;
158717
+ function getLoadFtsRowsStatement(db) {
158718
+ let stmt = loadFtsRowsStatements.get(db);
158719
+ if (!stmt) {
158720
+ stmt = db.prepare(`SELECT message_ordinal AS messageOrdinal, role, content
158721
+ FROM message_history_fts
158722
+ WHERE session_id = ?
158723
+ AND message_ordinal >= ?
158724
+ AND message_ordinal <= ?
158725
+ AND role IN ('user', 'assistant')
158726
+ ORDER BY message_ordinal ASC`);
158727
+ loadFtsRowsStatements.set(db, stmt);
158728
+ }
158729
+ return stmt;
158730
+ }
158731
+ function getExistingHashStatement(db, scopedToProject) {
158732
+ const map2 = scopedToProject ? existingHashByProjectStatements : existingHashStatements;
158733
+ let stmt = map2.get(db);
158734
+ if (!stmt) {
158735
+ stmt = db.prepare(`SELECT window_index AS windowIndex, chunk_hash AS chunkHash
158736
+ FROM compartment_chunk_embeddings
158737
+ WHERE compartment_id = ?
158738
+ AND model_id = ?
158739
+ ${scopedToProject ? "AND project_path = ?" : ""}
158740
+ ORDER BY window_index ASC`);
158741
+ map2.set(db, stmt);
158742
+ }
158743
+ return stmt;
158744
+ }
158745
+ function getDeleteByCompartmentStatement(db) {
158746
+ let stmt = deleteByCompartmentStatements.get(db);
158747
+ if (!stmt) {
158748
+ stmt = db.prepare("DELETE FROM compartment_chunk_embeddings WHERE compartment_id = ?");
158749
+ deleteByCompartmentStatements.set(db, stmt);
158750
+ }
158751
+ return stmt;
158752
+ }
158753
+ function getInsertEmbeddingStatement(db) {
158754
+ let stmt = insertEmbeddingStatements.get(db);
158755
+ if (!stmt) {
158756
+ stmt = db.prepare(`INSERT INTO compartment_chunk_embeddings (
158757
+ compartment_id, session_id, project_path, harness, window_index,
158758
+ start_ordinal, end_ordinal, chunk_hash, model_id, dims, vector, created_at
158759
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
158760
+ insertEmbeddingStatements.set(db, stmt);
158761
+ }
158762
+ return stmt;
158763
+ }
158764
+ function getDistinctModelStatement(db) {
158765
+ let stmt = distinctModelStatements.get(db);
158766
+ if (!stmt) {
158767
+ stmt = db.prepare(`SELECT DISTINCT model_id AS modelId
158768
+ FROM compartment_chunk_embeddings
158769
+ WHERE project_path = ?`);
158770
+ distinctModelStatements.set(db, stmt);
158771
+ }
158772
+ return stmt;
158773
+ }
158774
+ function getClearProjectStatement(db) {
158775
+ let stmt = clearProjectStatements.get(db);
158776
+ if (!stmt) {
158777
+ stmt = db.prepare("DELETE FROM compartment_chunk_embeddings WHERE project_path = ?");
158778
+ clearProjectStatements.set(db, stmt);
158779
+ }
158780
+ return stmt;
158781
+ }
158782
+ function getClearProjectModelStatement(db) {
158783
+ let stmt = clearProjectModelStatements.get(db);
158784
+ if (!stmt) {
158785
+ stmt = db.prepare("DELETE FROM compartment_chunk_embeddings WHERE project_path = ? AND model_id = ?");
158786
+ clearProjectModelStatements.set(db, stmt);
158787
+ }
158788
+ return stmt;
158789
+ }
158790
+ function getSearchRowsStatement(db, withModel) {
158791
+ const map2 = withModel ? searchRowsByModelStatements : searchRowsStatements;
158792
+ let stmt = map2.get(db);
158793
+ if (!stmt) {
158794
+ stmt = db.prepare(`SELECT e.compartment_id AS compartmentId,
158795
+ e.session_id AS sessionId,
158796
+ c.title AS title,
158797
+ c.start_message AS compartmentStart,
158798
+ c.end_message AS compartmentEnd,
158799
+ e.window_index AS windowIndex,
158800
+ e.start_ordinal AS windowStart,
158801
+ e.end_ordinal AS windowEnd,
158802
+ e.chunk_hash AS chunkHash,
158803
+ e.model_id AS modelId,
158804
+ e.dims AS dims,
158805
+ e.vector AS vector
158806
+ FROM compartment_chunk_embeddings e
158807
+ JOIN compartments c ON c.id = e.compartment_id
158808
+ WHERE e.session_id = ?
158809
+ AND e.project_path = ?
158810
+ ${withModel ? "AND e.model_id = ?" : ""}
158811
+ ORDER BY e.compartment_id ASC, e.window_index ASC`);
158812
+ map2.set(db, stmt);
158813
+ }
158814
+ return stmt;
158815
+ }
158816
+ function isFinitePositiveInteger(value) {
158817
+ return typeof value === "number" && Number.isFinite(value) && value > 0;
158818
+ }
158819
+ function normalizeCompartmentChunkMaxInputTokens(value) {
158820
+ if (!isFinitePositiveInteger(value)) {
158821
+ return DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS;
158822
+ }
158823
+ return Math.max(1, Math.floor(value));
158824
+ }
158825
+ function normalizeContent(text) {
158826
+ return text.replace(/\s+/g, " ").trim();
158827
+ }
158828
+ function formatOrdinalRange(start, end) {
158829
+ return start === end ? `[${start}]` : `[${start}-${end}]`;
158830
+ }
158831
+ function rolePrefix(role) {
158832
+ if (role === "user")
158833
+ return "U";
158834
+ if (role === "assistant")
158835
+ return "A";
158836
+ return null;
158837
+ }
158838
+ function parseOrdinal(value) {
158839
+ const parsed = typeof value === "number" ? value : Number.parseInt(String(value ?? ""), 10);
158840
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
158841
+ }
158842
+ function parseCanonicalLineRange(line) {
158843
+ const match = /^\[(\d+)(?:-(\d+))?\]\s+[UA]:/.exec(line.trim());
158844
+ if (!match)
158845
+ return null;
158846
+ const start = Number.parseInt(match[1], 10);
158847
+ const end = match[2] ? Number.parseInt(match[2], 10) : start;
158848
+ if (!Number.isFinite(start) || !Number.isFinite(end))
158849
+ return null;
158850
+ return { start, end };
158851
+ }
158852
+ function hashChunkText(text) {
158853
+ return createHash4("sha256").update(text).digest("hex");
158854
+ }
158855
+ function vectorBlob(vector) {
158856
+ return new Uint8Array(vector.buffer, vector.byteOffset, vector.byteLength);
158857
+ }
158858
+ function toFloat32Array(blob) {
158859
+ if (blob instanceof Uint8Array) {
158860
+ const buffer2 = blob.buffer.slice(blob.byteOffset, blob.byteOffset + blob.byteLength);
158861
+ return new Float32Array(buffer2);
158862
+ }
158863
+ return new Float32Array(blob.slice(0));
158864
+ }
158865
+ function buildCanonicalChunkTextFromFts(db, sessionId, startOrdinal, endOrdinal) {
158866
+ if (endOrdinal < startOrdinal)
158867
+ return "";
158868
+ const rows = getLoadFtsRowsStatement(db).all(sessionId, startOrdinal, endOrdinal).map((row) => row);
158869
+ const lines = [];
158870
+ let current = null;
158871
+ const flush2 = () => {
158872
+ if (!current || current.parts.length === 0)
158873
+ return;
158874
+ lines.push(`${formatOrdinalRange(current.start, current.end)} ${current.role}: ${current.parts.join(" / ")}`);
158875
+ current = null;
158876
+ };
158877
+ for (const row of rows) {
158878
+ const ordinal = parseOrdinal(row.messageOrdinal);
158879
+ const prefix = rolePrefix(row.role);
158880
+ const content = typeof row.content === "string" ? normalizeContent(row.content) : "";
158881
+ if (ordinal === null || prefix === null || content.length === 0)
158882
+ continue;
158883
+ if (current && current.role === prefix) {
158884
+ current.end = ordinal;
158885
+ current.parts.push(content);
158886
+ continue;
158887
+ }
158888
+ flush2();
158889
+ current = { role: prefix, start: ordinal, end: ordinal, parts: [content] };
158890
+ }
158891
+ flush2();
158892
+ return lines.join(`
158893
+ `);
158894
+ }
158895
+ function canonicalizeInMemoryChunkTextForEmbedding(chunkText, startOrdinal, endOrdinal) {
158896
+ const lines = [];
158897
+ for (const rawLine of chunkText.split(/\r?\n/)) {
158898
+ const line = rawLine.trim();
158899
+ const match = /^(\[(\d+)(?:-(\d+))?\]\s+[UA]:)\s*(.*)$/.exec(line);
158900
+ if (!match)
158901
+ continue;
158902
+ const lineStart = Number.parseInt(match[2], 10);
158903
+ const lineEnd = match[3] ? Number.parseInt(match[3], 10) : lineStart;
158904
+ if (startOrdinal != null && lineEnd < startOrdinal)
158905
+ continue;
158906
+ if (endOrdinal != null && lineStart > endOrdinal)
158907
+ continue;
158908
+ const rawParts = match[4].split(" / ").map((part) => normalizeContent(part)).filter((part) => part.length > 0);
158909
+ const ordinalSpan = lineEnd - lineStart + 1;
158910
+ const roleLabel = match[1].slice(match[1].indexOf("]") + 2);
158911
+ if (ordinalSpan === rawParts.length) {
158912
+ const retained = rawParts.map((part, index) => ({ ordinal: lineStart + index, part })).filter(({ ordinal, part }) => {
158913
+ if (part.startsWith("TC:"))
158914
+ return false;
158915
+ if (startOrdinal != null && ordinal < startOrdinal)
158916
+ return false;
158917
+ if (endOrdinal != null && ordinal > endOrdinal)
158918
+ return false;
158919
+ return true;
158920
+ });
158921
+ if (retained.length === 0)
158922
+ continue;
158923
+ const retainedStart = retained[0].ordinal;
158924
+ const retainedEnd = retained[retained.length - 1].ordinal;
158925
+ lines.push(`${formatOrdinalRange(retainedStart, retainedEnd)} ${roleLabel} ${retained.map(({ part }) => part).join(" / ")}`);
158926
+ continue;
158927
+ }
158928
+ const parts = rawParts.filter((part) => !part.startsWith("TC:"));
158929
+ if (parts.length === 0)
158930
+ continue;
158931
+ lines.push(`${match[1]} ${parts.join(" / ")}`);
158932
+ }
158933
+ return lines.join(`
158934
+ `);
158935
+ }
158936
+ function chunkCanonicalText(canonicalText, startOrdinal, endOrdinal, maxInputTokens) {
158937
+ const lines = canonicalText.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
158938
+ if (lines.length === 0 || endOrdinal < startOrdinal)
158939
+ return [];
158940
+ const normalizedMax = normalizeCompartmentChunkMaxInputTokens(maxInputTokens);
158941
+ const effectiveMax = Math.max(1, Math.floor(normalizedMax * CHUNK_WINDOW_SAFETY_RATIO));
158942
+ const fullText = lines.join(`
158943
+ `);
158944
+ if (estimateTokens(fullText) <= effectiveMax) {
158945
+ return [
158946
+ {
158947
+ windowIndex: 0,
158948
+ startOrdinal,
158949
+ endOrdinal,
158950
+ text: fullText,
158951
+ chunkHash: hashChunkText(fullText)
158952
+ }
158953
+ ];
158954
+ }
158955
+ const windows = [];
158956
+ let currentLines = [];
158957
+ let currentStart = null;
158958
+ let currentEnd = null;
158959
+ let currentTokens = 0;
158960
+ const flush2 = () => {
158961
+ if (currentLines.length === 0 || currentStart === null || currentEnd === null)
158962
+ return;
158963
+ const text = currentLines.join(`
158964
+ `);
158965
+ windows.push({
158966
+ windowIndex: windows.length + 1,
158967
+ startOrdinal: currentStart,
158968
+ endOrdinal: currentEnd,
158969
+ text,
158970
+ chunkHash: hashChunkText(text)
158971
+ });
158972
+ currentLines = [];
158973
+ currentStart = null;
158974
+ currentEnd = null;
158975
+ currentTokens = 0;
158976
+ };
158977
+ for (const line of lines) {
158978
+ const range = parseCanonicalLineRange(line);
158979
+ const lineStart = range?.start ?? startOrdinal;
158980
+ const lineEnd = range?.end ?? lineStart;
158981
+ const lineTokens = estimateTokens(line);
158982
+ if (currentLines.length > 0 && currentTokens + lineTokens > effectiveMax) {
158983
+ flush2();
158984
+ }
158985
+ if (currentLines.length === 0) {
158986
+ currentStart = lineStart;
158987
+ }
158988
+ currentLines.push(line);
158989
+ currentEnd = lineEnd;
158990
+ currentTokens += lineTokens;
158991
+ }
158992
+ flush2();
158993
+ return windows;
158994
+ }
158995
+ function getExistingChunkHashes(db, compartmentId, modelId, projectPath) {
158996
+ const scoped = typeof projectPath === "string" && projectPath.length > 0;
158997
+ const rows = scoped ? getExistingHashStatement(db, true).all(compartmentId, modelId, projectPath) : getExistingHashStatement(db, false).all(compartmentId, modelId);
158998
+ return new Map(rows.filter((row) => typeof row.windowIndex === "number" && typeof row.chunkHash === "string").map((row) => [row.windowIndex, row.chunkHash]));
158999
+ }
159000
+ function chunkEmbeddingWindowsAreCurrent(db, compartmentId, modelId, windows, projectPath) {
159001
+ const existing = getExistingChunkHashes(db, compartmentId, modelId, projectPath);
159002
+ if (existing.size !== windows.length)
159003
+ return false;
159004
+ return windows.every((window) => existing.get(window.windowIndex) === window.chunkHash);
159005
+ }
159006
+ function replaceCompartmentChunkEmbeddings(db, rows) {
159007
+ if (rows.length === 0)
159008
+ return;
159009
+ const compartmentId = rows[0].compartmentId;
159010
+ const now = Date.now();
159011
+ db.transaction(() => {
159012
+ getDeleteByCompartmentStatement(db).run(compartmentId);
159013
+ const insert = getInsertEmbeddingStatement(db);
159014
+ for (const row of rows) {
159015
+ insert.run(row.compartmentId, row.sessionId, row.projectPath, getHarness(), row.window.windowIndex, row.window.startOrdinal, row.window.endOrdinal, row.window.chunkHash, row.modelId, row.vector.length, vectorBlob(row.vector), row.createdAt ?? now);
159016
+ }
159017
+ })();
159018
+ }
159019
+ function getDistinctChunkEmbeddingModelIds(db, projectPath) {
159020
+ const rows = getDistinctModelStatement(db).all(projectPath);
159021
+ return new Set(rows.map((row) => typeof row.modelId === "string" ? row.modelId : null));
159022
+ }
159023
+ function clearChunkEmbeddingsForProject(db, projectPath, modelId) {
159024
+ if (modelId) {
159025
+ return getClearProjectModelStatement(db).run(projectPath, modelId).changes;
159026
+ }
159027
+ return getClearProjectStatement(db).run(projectPath).changes;
159028
+ }
159029
+ function loadCompartmentChunkEmbeddingsForSearch(db, sessionId, projectPath, modelId) {
159030
+ const rows = modelId ? getSearchRowsStatement(db, true).all(sessionId, projectPath, modelId) : getSearchRowsStatement(db, false).all(sessionId, projectPath);
159031
+ return rows.filter((row) => typeof row.compartmentId === "number" && typeof row.sessionId === "string" && typeof row.title === "string" && typeof row.compartmentStart === "number" && typeof row.compartmentEnd === "number" && typeof row.windowIndex === "number" && typeof row.windowStart === "number" && typeof row.windowEnd === "number" && typeof row.chunkHash === "string" && typeof row.modelId === "string" && typeof row.dims === "number" && (row.vector instanceof Uint8Array || row.vector instanceof ArrayBuffer)).map((row) => ({
159032
+ compartmentId: row.compartmentId,
159033
+ sessionId: row.sessionId,
159034
+ title: row.title,
159035
+ startOrdinal: row.compartmentStart,
159036
+ endOrdinal: row.compartmentEnd,
159037
+ windowIndex: row.windowIndex,
159038
+ windowStartOrdinal: row.windowStart,
159039
+ windowEndOrdinal: row.windowEnd,
159040
+ chunkHash: row.chunkHash,
159041
+ modelId: row.modelId,
159042
+ dims: row.dims,
159043
+ vector: toFloat32Array(row.vector)
159044
+ }));
159045
+ }
159046
+ function mapBackfillCandidateRows(rows) {
159047
+ return rows.filter((row) => {
159048
+ if (row === null || typeof row !== "object")
159049
+ return false;
159050
+ const candidate = row;
159051
+ return typeof candidate.id === "number" && typeof candidate.sessionId === "string" && typeof candidate.startMessage === "number" && typeof candidate.endMessage === "number" && typeof candidate.title === "string";
159052
+ }).map((row) => ({
159053
+ id: row.id,
159054
+ sessionId: row.sessionId,
159055
+ startMessage: row.startMessage,
159056
+ endMessage: row.endMessage,
159057
+ title: row.title
159058
+ }));
159059
+ }
159060
+ var sessionBackfillCandidateStatements = new WeakMap;
159061
+ function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, limit, excludeIds) {
159062
+ if (excludeIds && excludeIds.length > 0) {
159063
+ const placeholders2 = excludeIds.map(() => "?").join(", ");
159064
+ const stmt2 = db.prepare(`SELECT c.id AS id,
159065
+ c.session_id AS sessionId,
159066
+ c.start_message AS startMessage,
159067
+ c.end_message AS endMessage,
159068
+ c.title AS title
159069
+ FROM compartments c
159070
+ JOIN session_projects sp
159071
+ ON sp.session_id = c.session_id
159072
+ AND sp.harness = c.harness
159073
+ AND sp.project_path = ?
159074
+ WHERE c.session_id = ?
159075
+ AND c.start_message IS NOT NULL
159076
+ AND c.end_message IS NOT NULL
159077
+ AND c.id NOT IN (${placeholders2})
159078
+ AND NOT EXISTS (
159079
+ SELECT 1
159080
+ FROM compartment_chunk_embeddings current
159081
+ WHERE current.compartment_id = c.id
159082
+ AND current.project_path = ?
159083
+ AND current.model_id = ?
159084
+ )
159085
+ ORDER BY c.start_message ASC, c.id ASC
159086
+ LIMIT ?`);
159087
+ const rows2 = stmt2.all(projectPath, sessionId, ...excludeIds, projectPath, modelId, Math.max(1, limit));
159088
+ return mapBackfillCandidateRows(rows2);
159089
+ }
159090
+ let stmt = sessionBackfillCandidateStatements.get(db);
159091
+ if (!stmt) {
159092
+ stmt = db.prepare(`SELECT c.id AS id,
159093
+ c.session_id AS sessionId,
159094
+ c.start_message AS startMessage,
159095
+ c.end_message AS endMessage,
159096
+ c.title AS title
159097
+ FROM compartments c
159098
+ JOIN session_projects sp
159099
+ ON sp.session_id = c.session_id
159100
+ AND sp.harness = c.harness
159101
+ AND sp.project_path = ?
159102
+ WHERE c.session_id = ?
159103
+ AND c.start_message IS NOT NULL
159104
+ AND c.end_message IS NOT NULL
159105
+ AND NOT EXISTS (
159106
+ SELECT 1
159107
+ FROM compartment_chunk_embeddings current
159108
+ WHERE current.compartment_id = c.id
159109
+ AND current.project_path = ?
159110
+ AND current.model_id = ?
159111
+ )
159112
+ ORDER BY c.start_message ASC, c.id ASC
159113
+ LIMIT ?`);
159114
+ sessionBackfillCandidateStatements.set(db, stmt);
159115
+ }
159116
+ const rows = stmt.all(projectPath, sessionId, projectPath, modelId, Math.max(1, limit));
159117
+ return mapBackfillCandidateRows(rows);
159118
+ }
159119
+ function countUnembeddedSessionCompartments(db, projectPath, sessionId, modelId) {
159120
+ const row = db.prepare(`SELECT COUNT(*) AS n
159121
+ FROM compartments c
159122
+ JOIN session_projects sp
159123
+ ON sp.session_id = c.session_id
159124
+ AND sp.harness = c.harness
159125
+ AND sp.project_path = ?
159126
+ WHERE c.session_id = ?
159127
+ AND c.start_message IS NOT NULL
159128
+ AND c.end_message IS NOT NULL
159129
+ AND NOT EXISTS (
159130
+ SELECT 1
159131
+ FROM compartment_chunk_embeddings current
159132
+ WHERE current.compartment_id = c.id
159133
+ AND current.project_path = ?
159134
+ AND current.model_id = ?
159135
+ )`).get(projectPath, sessionId, projectPath, modelId);
159136
+ return typeof row?.n === "number" ? row.n : 0;
159137
+ }
159138
+
158009
159139
  // ../plugin/src/features/magic-context/memory/cosine-similarity.ts
158010
159140
  function cosineSimilarity(a, b) {
158011
159141
  if (a.length !== b.length) {
@@ -158024,13 +159154,13 @@ function cosineSimilarity(a, b) {
158024
159154
  }
158025
159155
 
158026
159156
  // ../plugin/src/features/magic-context/memory/normalize-hash.ts
158027
- import { createHash as createHash2 } from "node:crypto";
159157
+ import { createHash as createHash5 } from "node:crypto";
158028
159158
  function normalizeMemoryContent(content) {
158029
159159
  return content.toLowerCase().replace(/\s+/g, " ").trim();
158030
159160
  }
158031
159161
  function computeNormalizedHash(content) {
158032
159162
  const normalized = normalizeMemoryContent(content);
158033
- return createHash2("md5").update(normalized).digest("hex");
159163
+ return createHash5("md5").update(normalized).digest("hex");
158034
159164
  }
158035
159165
 
158036
159166
  // ../plugin/src/features/magic-context/memory/embedding-identity.ts
@@ -158174,19 +159304,19 @@ function isArrayLikeNumber(value) {
158174
159304
  }
158175
159305
  return arr.length === 0 || typeof arr[0] === "number";
158176
159306
  }
158177
- function toFloat32Array(values) {
159307
+ function toFloat32Array2(values) {
158178
159308
  return values instanceof Float32Array ? new Float32Array(values) : Float32Array.from(Array.from(values));
158179
159309
  }
158180
159310
  function extractBatchEmbeddings(result, expectedCount) {
158181
159311
  const { data } = result;
158182
159312
  if (Array.isArray(data) && data.length === expectedCount && data.every((entry) => typeof entry !== "number" && isArrayLikeNumber(entry))) {
158183
- return data.map((entry) => toFloat32Array(entry));
159313
+ return data.map((entry) => toFloat32Array2(entry));
158184
159314
  }
158185
159315
  if (!isArrayLikeNumber(data)) {
158186
159316
  log("[magic-context] embedding batch returned unexpected data shape");
158187
159317
  return Array.from({ length: expectedCount }, () => null);
158188
159318
  }
158189
- const flatData = toFloat32Array(data);
159319
+ const flatData = toFloat32Array2(data);
158190
159320
  const dimension = result.dims?.at(-1) ?? flatData.length / expectedCount;
158191
159321
  if (!Number.isInteger(dimension) || dimension <= 0 || flatData.length !== expectedCount * dimension) {
158192
159322
  log("[magic-context] embedding batch returned invalid dimensions");
@@ -158201,6 +159331,7 @@ function extractBatchEmbeddings(result, expectedCount) {
158201
159331
 
158202
159332
  class LocalEmbeddingProvider {
158203
159333
  modelId;
159334
+ maxInputTokens;
158204
159335
  model;
158205
159336
  pipeline = null;
158206
159337
  initPromise = null;
@@ -158208,8 +159339,9 @@ class LocalEmbeddingProvider {
158208
159339
  disposing = false;
158209
159340
  disposePromise = null;
158210
159341
  inFlightWaiters = [];
158211
- constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL) {
159342
+ constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL, maxInputTokens = 512) {
158212
159343
  this.model = model;
159344
+ this.maxInputTokens = maxInputTokens;
158213
159345
  this.modelId = getEmbeddingProviderIdentity({ provider: "local", model });
158214
159346
  }
158215
159347
  async initialize() {
@@ -158457,6 +159589,13 @@ function blockedEmbeddingEndpointReason(endpoint) {
158457
159589
  function normalizeEndpoint3(endpoint) {
158458
159590
  return endpoint?.trim().replace(/\/+$/, "") ?? "";
158459
159591
  }
159592
+ function embeddingModelsMatch(served, requested) {
159593
+ const a = served.trim().toLowerCase();
159594
+ const b = requested.trim().toLowerCase();
159595
+ if (a.length === 0 || b.length === 0)
159596
+ return true;
159597
+ return a === b || a.includes(b) || b.includes(a);
159598
+ }
158460
159599
  var FAILURE_THRESHOLD = 3;
158461
159600
  var FAILURE_WINDOW_MS = 60000;
158462
159601
  var OPEN_DURATION_MS = 5 * 60000;
@@ -158464,6 +159603,7 @@ var FETCH_TIMEOUT_MS = 30000;
158464
159603
 
158465
159604
  class OpenAICompatibleEmbeddingProvider {
158466
159605
  modelId;
159606
+ maxInputTokens;
158467
159607
  endpoint;
158468
159608
  model;
158469
159609
  apiKey;
@@ -158473,6 +159613,7 @@ class OpenAICompatibleEmbeddingProvider {
158473
159613
  failureTimes = [];
158474
159614
  circuitOpenUntil = 0;
158475
159615
  openLogged = false;
159616
+ modelMismatchLogged = false;
158476
159617
  halfOpenProbeInFlight = false;
158477
159618
  constructor(options) {
158478
159619
  this.endpoint = normalizeEndpoint3(options.endpoint);
@@ -158480,11 +159621,13 @@ class OpenAICompatibleEmbeddingProvider {
158480
159621
  this.apiKey = options.apiKey?.trim() ?? "";
158481
159622
  this.inputType = options.inputType?.trim() ?? "";
158482
159623
  this.truncate = options.truncate?.trim() ?? "";
159624
+ this.maxInputTokens = typeof options.maxInputTokens === "number" && Number.isFinite(options.maxInputTokens) ? Math.max(1, Math.floor(options.maxInputTokens)) : 512;
158483
159625
  this.modelId = getEmbeddingProviderIdentity({
158484
159626
  provider: "openai-compatible",
158485
159627
  endpoint: this.endpoint,
158486
159628
  model: this.model,
158487
- ...this.apiKey ? { api_key: this.apiKey } : {}
159629
+ ...this.apiKey ? { api_key: this.apiKey } : {},
159630
+ ...this.inputType ? { input_type: this.inputType } : {}
158488
159631
  });
158489
159632
  }
158490
159633
  async initialize() {
@@ -158569,6 +159712,15 @@ class OpenAICompatibleEmbeddingProvider {
158569
159712
  this.recordFailure(isProbe);
158570
159713
  return Array.from({ length: texts.length }, () => null);
158571
159714
  }
159715
+ const servedModel = typeof body.model === "string" ? body.model : "";
159716
+ if (this.model && servedModel && !embeddingModelsMatch(servedModel, this.model)) {
159717
+ if (!this.modelMismatchLogged) {
159718
+ log(`[magic-context] embedding endpoint served a DIFFERENT model than requested — refusing the substituted vectors (they have the wrong dimensions/space). requested="${this.model}" served="${servedModel}". The endpoint likely substituted a loaded model; load/select "${this.model}" on the endpoint, or set embedding.model to the served model.`);
159719
+ this.modelMismatchLogged = true;
159720
+ }
159721
+ this.recordFailure(isProbe);
159722
+ return Array.from({ length: texts.length }, () => null);
159723
+ }
158572
159724
  const items = Array.isArray(body.data) ? body.data : [];
158573
159725
  const results = Array.from({ length: texts.length }, (_, index) => {
158574
159726
  const embedding = items[index]?.embedding;
@@ -158688,9 +159840,9 @@ function isEmbeddingRow(row) {
158688
159840
  if (row === null || typeof row !== "object")
158689
159841
  return false;
158690
159842
  const candidate = row;
158691
- return typeof candidate.memoryId === "number" && isEmbeddingBlob(candidate.embedding);
159843
+ return typeof candidate.memoryId === "number" && isEmbeddingBlob(candidate.embedding) && (candidate.modelId === null || typeof candidate.modelId === "string");
158692
159844
  }
158693
- function toFloat32Array2(blob) {
159845
+ function toFloat32Array3(blob) {
158694
159846
  if (blob instanceof Uint8Array) {
158695
159847
  const buffer2 = blob.buffer.slice(blob.byteOffset, blob.byteOffset + blob.byteLength);
158696
159848
  return new Float32Array(buffer2);
@@ -158708,7 +159860,7 @@ function getSaveEmbeddingStatement(db) {
158708
159860
  function getLoadAllEmbeddingsStatement(db) {
158709
159861
  let stmt = loadAllEmbeddingsStatements.get(db);
158710
159862
  if (!stmt) {
158711
- stmt = db.prepare("SELECT memory_embeddings.memory_id AS memoryId, memory_embeddings.embedding AS embedding FROM memory_embeddings INNER JOIN memories ON memories.id = memory_embeddings.memory_id WHERE memories.project_path = ? ORDER BY memory_embeddings.memory_id ASC");
159863
+ stmt = db.prepare("SELECT memory_embeddings.memory_id AS memoryId, memory_embeddings.embedding AS embedding, memory_embeddings.model_id AS modelId FROM memory_embeddings INNER JOIN memories ON memories.id = memory_embeddings.memory_id WHERE memories.project_path = ? ORDER BY memory_embeddings.memory_id ASC");
158712
159864
  loadAllEmbeddingsStatements.set(db, stmt);
158713
159865
  }
158714
159866
  return stmt;
@@ -158737,7 +159889,10 @@ function loadAllEmbeddings(db, projectPath) {
158737
159889
  const rows = getLoadAllEmbeddingsStatement(db).all(projectPath).filter(isEmbeddingRow);
158738
159890
  const embeddings = new Map;
158739
159891
  for (const row of rows) {
158740
- embeddings.set(row.memoryId, toFloat32Array2(row.embedding));
159892
+ embeddings.set(row.memoryId, {
159893
+ embedding: toFloat32Array3(row.embedding),
159894
+ modelId: row.modelId
159895
+ });
158741
159896
  }
158742
159897
  return embeddings;
158743
159898
  }
@@ -158750,7 +159905,7 @@ function getDistinctStoredModelIds(db, projectPath) {
158750
159905
  }
158751
159906
 
158752
159907
  // ../plugin/src/features/magic-context/project-embedding-registry.ts
158753
- import { createHash as createHash3, randomUUID } from "node:crypto";
159908
+ import { createHash as createHash6, randomUUID } from "node:crypto";
158754
159909
  init_logger();
158755
159910
 
158756
159911
  // ../plugin/src/features/magic-context/git-commits/storage-git-commit-embeddings.ts
@@ -158758,7 +159913,7 @@ var saveStatements = new WeakMap;
158758
159913
  var loadProjectStatements = new WeakMap;
158759
159914
  var loadUnembeddedStatements = new WeakMap;
158760
159915
  var countEmbeddedStatements = new WeakMap;
158761
- var clearProjectStatements = new WeakMap;
159916
+ var clearProjectStatements2 = new WeakMap;
158762
159917
  var distinctModelIdStatements = new WeakMap;
158763
159918
  function getSaveStatement(db) {
158764
159919
  let stmt = saveStatements.get(db);
@@ -158806,12 +159961,12 @@ function getCountEmbeddedStatement(db) {
158806
159961
  }
158807
159962
  return stmt;
158808
159963
  }
158809
- function getClearProjectStatement(db) {
158810
- let stmt = clearProjectStatements.get(db);
159964
+ function getClearProjectStatement2(db) {
159965
+ let stmt = clearProjectStatements2.get(db);
158811
159966
  if (!stmt) {
158812
159967
  stmt = db.prepare(`DELETE FROM git_commit_embeddings
158813
159968
  WHERE sha IN (SELECT sha FROM git_commits WHERE project_path = ?)`);
158814
- clearProjectStatements.set(db, stmt);
159969
+ clearProjectStatements2.set(db, stmt);
158815
159970
  }
158816
159971
  return stmt;
158817
159972
  }
@@ -158847,7 +160002,7 @@ function countEmbeddedCommits(db, projectPath) {
158847
160002
  return row?.count ?? 0;
158848
160003
  }
158849
160004
  function clearProjectCommitEmbeddings(db, projectPath) {
158850
- return getClearProjectStatement(db).run(projectPath).changes;
160005
+ return getClearProjectStatement2(db).run(projectPath).changes;
158851
160006
  }
158852
160007
  function getDistinctCommitEmbeddingModelIds(db, projectPath) {
158853
160008
  const rows = getDistinctModelIdStatement(db).all(projectPath);
@@ -159011,9 +160166,83 @@ function invalidateMemory(projectPath, memoryId) {
159011
160166
  cached2?.embeddings.delete(memoryId);
159012
160167
  }
159013
160168
 
160169
+ // ../plugin/src/features/magic-context/session-project-storage.ts
160170
+ var upsertSessionProjectStatements = new WeakMap;
160171
+ var repairSessionChunkProjectStatements = new WeakMap;
160172
+ var repairProjectChunkProjectStatements = new WeakMap;
160173
+ function getUpsertSessionProjectStatement(db) {
160174
+ let stmt = upsertSessionProjectStatements.get(db);
160175
+ if (!stmt) {
160176
+ stmt = db.prepare(`INSERT INTO session_projects (session_id, harness, project_path, updated_at)
160177
+ VALUES (?, ?, ?, ?)
160178
+ ON CONFLICT(session_id, harness) DO UPDATE SET
160179
+ project_path = excluded.project_path,
160180
+ updated_at = excluded.updated_at
160181
+ WHERE session_projects.project_path <> excluded.project_path`);
160182
+ upsertSessionProjectStatements.set(db, stmt);
160183
+ }
160184
+ return stmt;
160185
+ }
160186
+ function getRepairSessionChunkProjectStatement(db) {
160187
+ let stmt = repairSessionChunkProjectStatements.get(db);
160188
+ if (!stmt) {
160189
+ stmt = db.prepare(`UPDATE compartment_chunk_embeddings
160190
+ SET project_path = ?
160191
+ WHERE session_id = ?
160192
+ AND harness = ?
160193
+ AND project_path <> ?`);
160194
+ repairSessionChunkProjectStatements.set(db, stmt);
160195
+ }
160196
+ return stmt;
160197
+ }
160198
+ function getRepairProjectChunkProjectStatement(db) {
160199
+ let stmt = repairProjectChunkProjectStatements.get(db);
160200
+ if (!stmt) {
160201
+ stmt = db.prepare(`UPDATE compartment_chunk_embeddings
160202
+ SET project_path = (
160203
+ SELECT sp.project_path
160204
+ FROM session_projects sp
160205
+ WHERE sp.session_id = compartment_chunk_embeddings.session_id
160206
+ AND sp.harness = compartment_chunk_embeddings.harness
160207
+ LIMIT 1
160208
+ )
160209
+ WHERE EXISTS (
160210
+ SELECT 1
160211
+ FROM session_projects sp
160212
+ WHERE sp.session_id = compartment_chunk_embeddings.session_id
160213
+ AND sp.harness = compartment_chunk_embeddings.harness
160214
+ AND sp.project_path <> compartment_chunk_embeddings.project_path
160215
+ AND (
160216
+ sp.project_path = ?
160217
+ OR compartment_chunk_embeddings.project_path = ?
160218
+ )
160219
+ )`);
160220
+ repairProjectChunkProjectStatements.set(db, stmt);
160221
+ }
160222
+ return stmt;
160223
+ }
160224
+ function recordSessionProjectIdentity(db, sessionId, projectPath) {
160225
+ if (!sessionId || !projectPath)
160226
+ return;
160227
+ const harness = getHarness();
160228
+ const now = Date.now();
160229
+ db.transaction(() => {
160230
+ getUpsertSessionProjectStatement(db).run(sessionId, harness, projectPath, now);
160231
+ getRepairSessionChunkProjectStatement(db).run(projectPath, sessionId, harness, projectPath);
160232
+ })();
160233
+ }
160234
+ function repairMisScopedCompartmentChunkEmbeddingsForProject(db, projectPath) {
160235
+ if (!projectPath)
160236
+ return 0;
160237
+ return getRepairProjectChunkProjectStatement(db).run(projectPath, projectPath).changes;
160238
+ }
160239
+
159014
160240
  // ../plugin/src/features/magic-context/project-embedding-registry.ts
159015
160241
  var OFF_PROVIDER_IDENTITY = "embedding-provider:off";
159016
160242
  var SWEEP_MAX_WALL_CLOCK_MS = 10 * 60 * 1000;
160243
+ var CHUNK_DRAIN_BATCH_SIZE = 8;
160244
+ var MAX_WINDOWS_PER_EMBED_CALL = 16;
160245
+ var SESSION_EMBED_LEASE_RENEWAL_MS = 60 * 1000;
159017
160246
  var projectRegistrations = new Map;
159018
160247
  var loadUnembeddedMemoriesStatements = new WeakMap;
159019
160248
  var globalRegistrationGeneration = 0;
@@ -159022,7 +160251,10 @@ function resolveEmbeddingConfig(config2) {
159022
160251
  if (!config2 || config2.provider === "local") {
159023
160252
  return {
159024
160253
  provider: "local",
159025
- model: config2?.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL
160254
+ model: config2?.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
160255
+ ...config2?.max_input_tokens ? {
160256
+ max_input_tokens: normalizeCompartmentChunkMaxInputTokens(config2.max_input_tokens)
160257
+ } : {}
159026
160258
  };
159027
160259
  }
159028
160260
  if (config2.provider === "openai-compatible") {
@@ -159035,7 +160267,10 @@ function resolveEmbeddingConfig(config2) {
159035
160267
  endpoint: config2.endpoint.trim(),
159036
160268
  ...apiKey ? { api_key: apiKey } : {},
159037
160269
  ...inputType ? { input_type: inputType } : {},
159038
- ...truncate ? { truncate } : {}
160270
+ ...truncate ? { truncate } : {},
160271
+ ...config2.max_input_tokens ? {
160272
+ max_input_tokens: normalizeCompartmentChunkMaxInputTokens(config2.max_input_tokens)
160273
+ } : {}
159039
160274
  };
159040
160275
  }
159041
160276
  return { provider: "off" };
@@ -159053,10 +160288,11 @@ function createProvider(config2) {
159053
160288
  model: config2.model,
159054
160289
  apiKey: config2.api_key,
159055
160290
  inputType: config2.input_type,
159056
- truncate: config2.truncate
160291
+ truncate: config2.truncate,
160292
+ maxInputTokens: config2.max_input_tokens
159057
160293
  });
159058
160294
  }
159059
- return new LocalEmbeddingProvider(config2.model);
160295
+ return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens);
159060
160296
  }
159061
160297
  function stableStringify2(value) {
159062
160298
  if (Array.isArray(value)) {
@@ -159069,7 +160305,7 @@ function stableStringify2(value) {
159069
160305
  return JSON.stringify(value);
159070
160306
  }
159071
160307
  function sha256Prefix(value, length = 16) {
159072
- return createHash3("sha256").update(value).digest("hex").slice(0, length);
160308
+ return createHash6("sha256").update(value).digest("hex").slice(0, length);
159073
160309
  }
159074
160310
  function getRuntimeFingerprint(config2) {
159075
160311
  if (config2.provider === "off") {
@@ -159077,6 +160313,18 @@ function getRuntimeFingerprint(config2) {
159077
160313
  }
159078
160314
  return `${getEmbeddingProviderIdentity(config2)}:${sha256Prefix(stableStringify2(config2))}`;
159079
160315
  }
160316
+ function getChunkEmbeddingModelId(config2, providerIdentity) {
160317
+ if (config2.provider === "off") {
160318
+ return OFF_PROVIDER_IDENTITY;
160319
+ }
160320
+ const chunkIdentity = {
160321
+ providerIdentity,
160322
+ chunkerVersion: 2,
160323
+ maxInputTokens: normalizeCompartmentChunkMaxInputTokens("max_input_tokens" in config2 ? config2.max_input_tokens : undefined),
160324
+ truncate: config2.provider === "openai-compatible" ? config2.truncate ?? "" : ""
160325
+ };
160326
+ return `${providerIdentity}:chunk:${sha256Prefix(stableStringify2(chunkIdentity))}`;
160327
+ }
159080
160328
  function sameFeatures(a, b) {
159081
160329
  return a.memoryEnabled === b.memoryEnabled && a.gitCommitEnabled === b.gitCommitEnabled;
159082
160330
  }
@@ -159093,7 +160341,8 @@ function snapshotFor(registration) {
159093
160341
  features: { ...registration.features },
159094
160342
  enabled,
159095
160343
  gitCommitEnabled,
159096
- modelId: registration.observationMode || !providerIsOn ? "off" : registration.modelId
160344
+ modelId: registration.observationMode || !providerIsOn ? "off" : registration.modelId,
160345
+ chunkModelId: registration.observationMode || !providerIsOn ? "off" : registration.chunkModelId
159097
160346
  };
159098
160347
  }
159099
160348
  function disposeProvider(provider) {
@@ -159113,7 +160362,7 @@ function anyStoredModelIdIsStale(storedIds, currentId) {
159113
160362
  }
159114
160363
  return false;
159115
160364
  }
159116
- function maybeWipeStaleEmbeddings(db, projectIdentity, currentProviderIdentity, features) {
160365
+ function maybeWipeStaleEmbeddings(db, projectIdentity, currentProviderIdentity, currentChunkIdentity, features) {
159117
160366
  if (currentProviderIdentity === OFF_PROVIDER_IDENTITY) {
159118
160367
  return false;
159119
160368
  }
@@ -159134,6 +160383,14 @@ function maybeWipeStaleEmbeddings(db, projectIdentity, currentProviderIdentity,
159134
160383
  wiped = true;
159135
160384
  }
159136
160385
  }
160386
+ if (features.memoryEnabled) {
160387
+ repairMisScopedCompartmentChunkEmbeddingsForProject(db, projectIdentity);
160388
+ const chunkIds = getDistinctChunkEmbeddingModelIds(db, projectIdentity);
160389
+ if (anyStoredModelIdIsStale(chunkIds, currentChunkIdentity)) {
160390
+ clearChunkEmbeddingsForProject(db, projectIdentity);
160391
+ wiped = true;
160392
+ }
160393
+ }
159137
160394
  })();
159138
160395
  return wiped;
159139
160396
  }
@@ -159141,10 +160398,11 @@ function registerProjectEmbeddingAndMaybeWipe(db, projectIdentity, config2, feat
159141
160398
  const resolvedConfig = resolveEmbeddingConfig(config2);
159142
160399
  const providerIdentity = getEmbeddingProviderIdentity(resolvedConfig);
159143
160400
  const runtimeFingerprint = getRuntimeFingerprint(resolvedConfig);
160401
+ const chunkModelId = getChunkEmbeddingModelId(resolvedConfig, providerIdentity);
159144
160402
  const prior = projectRegistrations.get(projectIdentity);
159145
160403
  const canReuseProvider = prior !== undefined && !prior.observationMode && prior.runtimeFingerprint === runtimeFingerprint && prior.providerIdentity === providerIdentity;
159146
- const wiped = maybeWipeStaleEmbeddings(db, projectIdentity, providerIdentity, features);
159147
- const generationChanged = prior === undefined || prior.observationMode || prior.runtimeFingerprint !== runtimeFingerprint || !sameFeatures(prior.features, features) || wiped;
160404
+ const wiped = maybeWipeStaleEmbeddings(db, projectIdentity, providerIdentity, chunkModelId, features);
160405
+ const generationChanged = prior === undefined || prior.observationMode || prior.runtimeFingerprint !== runtimeFingerprint || prior.chunkModelId !== chunkModelId || !sameFeatures(prior.features, features) || wiped;
159148
160406
  const generation = generationChanged ? ++globalRegistrationGeneration : prior.generation;
159149
160407
  const registration = {
159150
160408
  projectIdentity,
@@ -159156,6 +160414,7 @@ function registerProjectEmbeddingAndMaybeWipe(db, projectIdentity, config2, feat
159156
160414
  generation,
159157
160415
  features: { ...features },
159158
160416
  modelId: providerIdentity === OFF_PROVIDER_IDENTITY ? "off" : providerIdentity,
160417
+ chunkModelId: providerIdentity === OFF_PROVIDER_IDENTITY ? "off" : chunkModelId,
159159
160418
  observationMode: false
159160
160419
  };
159161
160420
  projectRegistrations.set(projectIdentity, registration);
@@ -159178,6 +160437,7 @@ function registerProjectInObservationMode(db, projectIdentity, sourceDirectory,
159178
160437
  generation,
159179
160438
  features: { memoryEnabled: false, gitCommitEnabled: false },
159180
160439
  modelId: "off",
160440
+ chunkModelId: "off",
159181
160441
  observationMode: true
159182
160442
  };
159183
160443
  projectRegistrations.set(projectIdentity, registration);
@@ -159188,6 +160448,15 @@ function getProjectEmbeddingSnapshot(projectIdentity) {
159188
160448
  const registration = projectRegistrations.get(projectIdentity);
159189
160449
  return registration ? snapshotFor(registration) : null;
159190
160450
  }
160451
+ function getProjectChunkEmbeddingModelId(projectIdentity) {
160452
+ const registration = projectRegistrations.get(projectIdentity);
160453
+ return registration && !registration.observationMode ? registration.chunkModelId : "off";
160454
+ }
160455
+ function getProjectEmbeddingMaxInputTokens(projectIdentity) {
160456
+ const registration = projectRegistrations.get(projectIdentity);
160457
+ const configMax = registration?.config && "max_input_tokens" in registration.config ? registration.config.max_input_tokens : undefined;
160458
+ return normalizeCompartmentChunkMaxInputTokens(registration?.provider?.maxInputTokens ?? configMax);
160459
+ }
159191
160460
  function getOrCreateProjectProvider(registration) {
159192
160461
  if (registration.providerIdentity === OFF_PROVIDER_IDENTITY || registration.observationMode) {
159193
160462
  return null;
@@ -159282,6 +160551,131 @@ async function embedUnembeddedMemoriesForProject(db, projectIdentity, batchSize
159282
160551
  return 0;
159283
160552
  }
159284
160553
  }
160554
+ async function embedCandidateChunkBatch(db, projectIdentity, modelId, candidates, signal) {
160555
+ const noWork = [];
160556
+ if (candidates.length === 0)
160557
+ return { embedded: 0, noWork };
160558
+ const maxInputTokens = getProjectEmbeddingMaxInputTokens(projectIdentity);
160559
+ const prepared = [];
160560
+ for (const candidate of candidates) {
160561
+ const canonicalText = buildCanonicalChunkTextFromFts(db, candidate.sessionId, candidate.startMessage, candidate.endMessage);
160562
+ if (canonicalText.length === 0) {
160563
+ noWork.push(candidate.id);
160564
+ continue;
160565
+ }
160566
+ const windows = chunkCanonicalText(canonicalText, candidate.startMessage, candidate.endMessage, maxInputTokens);
160567
+ if (windows.length === 0 || chunkEmbeddingWindowsAreCurrent(db, candidate.id, modelId, windows, projectIdentity)) {
160568
+ noWork.push(candidate.id);
160569
+ continue;
160570
+ }
160571
+ prepared.push({ candidate, windows });
160572
+ }
160573
+ if (prepared.length === 0)
160574
+ return { embedded: 0, noWork };
160575
+ let embedded = 0;
160576
+ let i = 0;
160577
+ while (i < prepared.length) {
160578
+ if (signal?.aborted)
160579
+ break;
160580
+ const slice = [];
160581
+ let windowCount = 0;
160582
+ do {
160583
+ const item = prepared[i];
160584
+ slice.push(item);
160585
+ windowCount += item.windows.length;
160586
+ i += 1;
160587
+ } while (i < prepared.length && windowCount + prepared[i].windows.length <= MAX_WINDOWS_PER_EMBED_CALL);
160588
+ const texts = [];
160589
+ for (const item of slice)
160590
+ texts.push(...item.windows.map((w) => w.text));
160591
+ try {
160592
+ const result = await embedBatchForProject(projectIdentity, texts, signal);
160593
+ if (!result)
160594
+ continue;
160595
+ if (signal?.aborted)
160596
+ break;
160597
+ let offset = 0;
160598
+ for (const item of slice) {
160599
+ const vectors = result.vectors.slice(offset, offset + item.windows.length);
160600
+ offset += item.windows.length;
160601
+ if (vectors.length !== item.windows.length || vectors.some((v) => !v)) {
160602
+ continue;
160603
+ }
160604
+ const rows = item.windows.map((window, index) => ({
160605
+ compartmentId: item.candidate.id,
160606
+ sessionId: item.candidate.sessionId,
160607
+ projectPath: projectIdentity,
160608
+ window,
160609
+ modelId,
160610
+ vector: vectors[index]
160611
+ }));
160612
+ replaceCompartmentChunkEmbeddings(db, rows);
160613
+ embedded += 1;
160614
+ }
160615
+ } catch (error51) {
160616
+ log("[magic-context] failed to proactively embed compartment chunks:", error51);
160617
+ }
160618
+ }
160619
+ return { embedded, noWork };
160620
+ }
160621
+ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, options) {
160622
+ const snapshot = getProjectEmbeddingSnapshot(projectIdentity);
160623
+ if (!snapshot?.enabled || snapshot.chunkModelId === "off") {
160624
+ return { status: "disabled", embedded: 0, total: 0 };
160625
+ }
160626
+ recordSessionProjectIdentity(db, sessionId, projectIdentity);
160627
+ const total = countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot.chunkModelId);
160628
+ if (total === 0)
160629
+ return { status: "nothing", embedded: 0, total: 0 };
160630
+ const holderId = `session-embed-${randomUUID()}`;
160631
+ const lease = acquireGitSweepLease(db, projectIdentity, holderId, { ignoreCooldown: true });
160632
+ if (!lease.acquired)
160633
+ return { status: "busy", embedded: 0, total };
160634
+ const renewal = setInterval(() => {
160635
+ try {
160636
+ renewGitSweepLease(db, projectIdentity, holderId);
160637
+ } catch {}
160638
+ }, SESSION_EMBED_LEASE_RENEWAL_MS);
160639
+ renewal.unref?.();
160640
+ const batchSize = Math.max(1, options?.batchSize ?? CHUNK_DRAIN_BATCH_SIZE);
160641
+ const skipIds = [];
160642
+ let embedded = 0;
160643
+ let aborted2 = false;
160644
+ let providerStalled = false;
160645
+ try {
160646
+ options?.onProgress?.({ embedded, total });
160647
+ for (;; ) {
160648
+ if (options?.signal?.aborted) {
160649
+ aborted2 = true;
160650
+ break;
160651
+ }
160652
+ const candidates = loadUnembeddedSessionChunkCandidates(db, projectIdentity, sessionId, snapshot.chunkModelId, batchSize, skipIds);
160653
+ if (candidates.length === 0)
160654
+ break;
160655
+ const { embedded: n, noWork } = await embedCandidateChunkBatch(db, projectIdentity, snapshot.chunkModelId, candidates, options?.signal);
160656
+ for (const id of noWork)
160657
+ skipIds.push(id);
160658
+ if (n === 0 && noWork.length === 0) {
160659
+ providerStalled = true;
160660
+ break;
160661
+ }
160662
+ embedded += n;
160663
+ options?.onProgress?.({ embedded: Math.min(embedded, total), total });
160664
+ await new Promise((resolve3) => setTimeout(resolve3, 0));
160665
+ }
160666
+ } finally {
160667
+ clearInterval(renewal);
160668
+ releaseGitSweepLease(db, projectIdentity, holderId);
160669
+ }
160670
+ if (aborted2)
160671
+ return { status: "aborted", embedded, total };
160672
+ if (providerStalled) {
160673
+ const remaining = Math.max(0, countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot.chunkModelId) - skipIds.length);
160674
+ if (remaining > 0)
160675
+ return { status: "stalled", embedded, total, remaining };
160676
+ }
160677
+ return { status: "done", embedded, total };
160678
+ }
159285
160679
 
159286
160680
  // ../plugin/src/features/magic-context/memory/embedding.ts
159287
160681
  var DEFAULT_EMBEDDING_CONFIG = {
@@ -159301,10 +160695,11 @@ function createProvider2(config2) {
159301
160695
  model: config2.model,
159302
160696
  apiKey: config2.api_key,
159303
160697
  inputType: config2.input_type,
159304
- truncate: config2.truncate
160698
+ truncate: config2.truncate,
160699
+ maxInputTokens: config2.max_input_tokens
159305
160700
  });
159306
160701
  }
159307
- return new LocalEmbeddingProvider(config2.model);
160702
+ return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens);
159308
160703
  }
159309
160704
  function getOrCreateProvider() {
159310
160705
  if (provider) {
@@ -159328,186 +160723,8 @@ async function embedText(text, signal) {
159328
160723
  }
159329
160724
  var SWEEP_MAX_WALL_CLOCK_MS2 = 10 * 60 * 1000;
159330
160725
 
159331
- // ../plugin/src/features/magic-context/memory/project-identity.ts
159332
- import { execFileSync } from "node:child_process";
159333
- import { createHash as createHash4 } from "node:crypto";
159334
- import { statSync } from "node:fs";
159335
- import path3 from "node:path";
159336
- var GIT_TIMEOUT_MS = 5000;
159337
- var identityCache = new Map;
159338
- var directoryFallbackCache = new Map;
159339
-
159340
- class ProjectIdentityError extends Error {
159341
- errorClass;
159342
- rawDirectory;
159343
- constructor(errorClass, rawDirectory, message, cause) {
159344
- super(message);
159345
- this.name = "ProjectIdentityError";
159346
- this.errorClass = errorClass;
159347
- this.rawDirectory = rawDirectory;
159348
- if (cause) {
159349
- this.cause = cause;
159350
- }
159351
- }
159352
- }
159353
- function asError(error51) {
159354
- return error51 instanceof Error ? error51 : undefined;
159355
- }
159356
- function getErrorCode(error51) {
159357
- if (error51 === null || typeof error51 !== "object" || !("code" in error51)) {
159358
- return;
159359
- }
159360
- const code = error51.code;
159361
- return typeof code === "string" ? code : undefined;
159362
- }
159363
- function getErrorSignal(error51) {
159364
- if (error51 === null || typeof error51 !== "object" || !("signal" in error51)) {
159365
- return;
159366
- }
159367
- const signal = error51.signal;
159368
- return typeof signal === "string" ? signal : undefined;
159369
- }
159370
- function getErrorKilled(error51) {
159371
- if (error51 === null || typeof error51 !== "object" || !("killed" in error51)) {
159372
- return false;
159373
- }
159374
- return error51.killed === true;
159375
- }
159376
- function getErrorStderr(error51) {
159377
- if (error51 === null || typeof error51 !== "object" || !("stderr" in error51)) {
159378
- return "";
159379
- }
159380
- const stderr = error51.stderr;
159381
- if (typeof stderr === "string") {
159382
- return stderr;
159383
- }
159384
- if (Buffer.isBuffer(stderr)) {
159385
- return stderr.toString("utf8");
159386
- }
159387
- return "";
159388
- }
159389
- function directoryFallback(directory) {
159390
- const canonical = path3.resolve(directory);
159391
- const hash2 = createHash4("md5").update(canonical, "utf8").digest("hex").slice(0, 12);
159392
- return `dir:${hash2}`;
159393
- }
159394
- function assertDirectoryUsable(canonicalDirectory, rawDirectory) {
159395
- try {
159396
- const stat2 = statSync(canonicalDirectory);
159397
- if (!stat2.isDirectory()) {
159398
- throw new ProjectIdentityError("unknown", rawDirectory, `Project path is not a directory: ${canonicalDirectory}`);
159399
- }
159400
- } catch (error51) {
159401
- if (error51 instanceof ProjectIdentityError) {
159402
- throw error51;
159403
- }
159404
- const code = getErrorCode(error51);
159405
- if (code === "EACCES" || code === "EPERM") {
159406
- throw new ProjectIdentityError("permission_denied", rawDirectory, `Permission denied while accessing project directory: ${canonicalDirectory}`, asError(error51));
159407
- }
159408
- throw new ProjectIdentityError("unknown", rawDirectory, `Unable to access project directory: ${canonicalDirectory}`, asError(error51));
159409
- }
159410
- }
159411
- function isGitTimeoutError(error51) {
159412
- const code = getErrorCode(error51);
159413
- const signal = getErrorSignal(error51);
159414
- return code === "ETIMEDOUT" || signal === "SIGTERM" || signal === "SIGKILL" || getErrorKilled(error51);
159415
- }
159416
- function classifyGitError(error51, rawDirectory) {
159417
- if (isGitTimeoutError(error51)) {
159418
- return new ProjectIdentityError("git_timeout", rawDirectory, `git rev-list timed out after ${GIT_TIMEOUT_MS}ms`, asError(error51));
159419
- }
159420
- const code = getErrorCode(error51);
159421
- if (code === "ENOENT") {
159422
- return new ProjectIdentityError("git_missing", rawDirectory, "git binary is not available in PATH", asError(error51));
159423
- }
159424
- if (code === "EACCES" || code === "EPERM") {
159425
- return new ProjectIdentityError("permission_denied", rawDirectory, "Permission denied while spawning git", asError(error51));
159426
- }
159427
- const stderr = getErrorStderr(error51).toLowerCase();
159428
- if (stderr.includes("not a git repository") || stderr.includes("does not have any commits yet") || stderr.includes("ambiguous argument 'head'") || stderr.includes("unknown revision or path")) {
159429
- return new ProjectIdentityError("not_git_repo", rawDirectory, "Directory has no git root commit; caller may use directory fallback", asError(error51));
159430
- }
159431
- return new ProjectIdentityError("unknown", rawDirectory, "git rev-list failed while resolving project identity", asError(error51));
159432
- }
159433
- function resolveProjectIdentityStrict(directory) {
159434
- const canonical = path3.resolve(directory);
159435
- const cached2 = identityCache.get(canonical);
159436
- if (cached2 !== undefined) {
159437
- return cached2;
159438
- }
159439
- assertDirectoryUsable(canonical, directory);
159440
- let output;
159441
- try {
159442
- output = execFileSync("git", ["rev-list", "--max-parents=0", "HEAD"], {
159443
- cwd: canonical,
159444
- encoding: "utf8",
159445
- env: { ...process.env, LC_ALL: "C", LANG: "C" },
159446
- stdio: ["ignore", "pipe", "pipe"],
159447
- timeout: GIT_TIMEOUT_MS
159448
- });
159449
- } catch (error51) {
159450
- throw classifyGitError(error51, directory);
159451
- }
159452
- const firstLine = output.split(`
159453
- `)[0]?.trim() ?? "";
159454
- const rootCommit = firstLine.slice(0, 64);
159455
- if (rootCommit.length < 7) {
159456
- throw new ProjectIdentityError("unknown", directory, "git rev-list returned no valid root commit hash");
159457
- }
159458
- const identity = `git:${rootCommit}`;
159459
- identityCache.set(canonical, identity);
159460
- return identity;
159461
- }
159462
- function shouldUseDirectoryFallback(error51) {
159463
- return error51.errorClass === "not_git_repo" || error51.errorClass === "unknown" && error51.message.startsWith("Unable to access project directory:");
159464
- }
159465
- function resolveProjectIdentity(directory) {
159466
- const canonical = path3.resolve(directory);
159467
- const cachedFallback = directoryFallbackCache.get(canonical);
159468
- if (cachedFallback !== undefined) {
159469
- if (!hasGitDir(canonical)) {
159470
- return cachedFallback;
159471
- }
159472
- directoryFallbackCache.delete(canonical);
159473
- }
159474
- try {
159475
- return resolveProjectIdentityStrict(directory);
159476
- } catch (error51) {
159477
- if (error51 instanceof ProjectIdentityError && shouldUseDirectoryFallback(error51)) {
159478
- const fallback = directoryFallback(canonical);
159479
- if (!hasGitDir(canonical)) {
159480
- directoryFallbackCache.set(canonical, fallback);
159481
- }
159482
- return fallback;
159483
- }
159484
- throw error51;
159485
- }
159486
- }
159487
- function hasGitDir(canonical) {
159488
- try {
159489
- statSync(path3.join(canonical, ".git"));
159490
- return true;
159491
- } catch {
159492
- return false;
159493
- }
159494
- }
159495
- function normalizeStoredProjectPath(rawOrStored) {
159496
- if (rawOrStored.startsWith("git:") || rawOrStored.startsWith("dir:")) {
159497
- return rawOrStored;
159498
- }
159499
- try {
159500
- return resolveProjectIdentity(rawOrStored);
159501
- } catch {
159502
- return directoryFallback(rawOrStored);
159503
- }
159504
- }
159505
- function storedPathBelongsToIdentity(storedProjectPath, projectIdentity) {
159506
- return storedProjectPath === projectIdentity || normalizeStoredProjectPath(storedProjectPath) === projectIdentity;
159507
- }
159508
-
159509
160726
  // ../plugin/src/plugin/embedding-bootstrap-helpers.ts
159510
- import { createHash as createHash5 } from "node:crypto";
160727
+ import { createHash as createHash7 } from "node:crypto";
159511
160728
  init_logger();
159512
160729
  var EMBEDDING_AFFECTING_KEYS = new Set([
159513
160730
  "embedding.api_key",
@@ -159529,7 +160746,7 @@ var EMBEDDING_WARNING_TERMS = [
159529
160746
  ];
159530
160747
  var loggedFailureSignatures = new Map;
159531
160748
  function sha256Prefix2(value, length = 16) {
159532
- return createHash5("sha256").update(value).digest("hex").slice(0, length);
160749
+ return createHash7("sha256").update(value).digest("hex").slice(0, length);
159533
160750
  }
159534
160751
  function warningLooksEmbeddingRelated(message) {
159535
160752
  const lower = message.toLowerCase();
@@ -159690,6 +160907,7 @@ var SESSION_META_SELECT_COLUMNS = [
159690
160907
  "cached_m0_bytes",
159691
160908
  "cached_m1_bytes",
159692
160909
  "cached_m0_project_memory_epoch",
160910
+ "cached_m0_workspace_fingerprint",
159693
160911
  "cached_m0_project_user_profile_version",
159694
160912
  "cached_m0_max_compartment_seq",
159695
160913
  "cached_m0_max_memory_id",
@@ -159737,6 +160955,7 @@ var META_COLUMNS = {
159737
160955
  cachedM0Bytes: "cached_m0_bytes",
159738
160956
  cachedM1Bytes: "cached_m1_bytes",
159739
160957
  cachedM0ProjectMemoryEpoch: "cached_m0_project_memory_epoch",
160958
+ cachedM0WorkspaceFingerprint: "cached_m0_workspace_fingerprint",
159740
160959
  cachedM0ProjectUserProfileVersion: "cached_m0_project_user_profile_version",
159741
160960
  cachedM0MaxCompartmentSeq: "cached_m0_max_compartment_seq",
159742
160961
  cachedM0MaxMemoryId: "cached_m0_max_memory_id",
@@ -159766,6 +160985,7 @@ var NULL_BIND_META_KEYS = new Set([
159766
160985
  "cachedM0Bytes",
159767
160986
  "cachedM1Bytes",
159768
160987
  "cachedM0ProjectMemoryEpoch",
160988
+ "cachedM0WorkspaceFingerprint",
159769
160989
  "cachedM0ProjectUserProfileVersion",
159770
160990
  "cachedM0MaxCompartmentSeq",
159771
160991
  "cachedM0MaxMemoryId",
@@ -159799,7 +161019,7 @@ function isSessionMetaRow(row) {
159799
161019
  if (row === null || typeof row !== "object")
159800
161020
  return false;
159801
161021
  const r = row;
159802
- return typeof r.session_id === "string" && typeof r.last_response_time === "number" && isStringOrNull(r.cache_ttl) && typeof r.counter === "number" && typeof r.last_nudge_tokens === "number" && isStringOrNull(r.last_nudge_band) && isStringOrNull(r.last_transform_error) && typeof r.is_subagent === "number" && typeof r.last_context_percentage === "number" && typeof r.last_input_tokens === "number" && isNumberOrNull(r.observed_safe_input_tokens) && isNumberOrNull(r.cache_alert_sent) && isNumberOrNull(r.times_execute_threshold_reached) && isNumberOrNull(r.compartment_in_progress) && (r.system_prompt_hash === null || typeof r.system_prompt_hash === "string" || typeof r.system_prompt_hash === "number") && isNumberOrNull(r.system_prompt_tokens) && isNumberOrNull(r.conversation_tokens) && isNumberOrNull(r.tool_call_tokens) && isNumberOrNull(r.cleared_reasoning_through_tag) && isStringOrNull(r.last_todo_state) && isBlobOrNull(r.cached_m0_bytes) && isBlobOrNull(r.cached_m1_bytes) && isNumberOrNull(r.cached_m0_project_memory_epoch) && isNumberOrNull(r.cached_m0_project_user_profile_version) && isNumberOrNull(r.cached_m0_max_compartment_seq) && isNumberOrNull(r.cached_m0_max_memory_id) && isNumberOrNull(r.cached_m0_max_mutation_id) && isNumberOrNull(r.cached_m0_max_memory_mutation_id) && isStringOrNull(r.cached_m0_project_docs_hash) && isNumberOrNull(r.cached_m0_materialized_at) && isNumberOrNull(r.cached_m0_session_facts_version) && isStringOrNull(r.cached_m0_upgrade_state) && isStringOrNull(r.cached_m0_system_hash) && isStringOrNull(r.cached_m0_tool_set_hash) && isStringOrNull(r.cached_m0_model_key) && isStringOrNull(r.last_observed_model_key) && isNumberOrNull(r.last_usage_context_limit) && isNumberOrNull(r.prior_boundary_ordinal) && isNumberOrNull(r.protected_tail_policy_version) && isNumberOrNull(r.protected_tail_drain_window_started_at) && isNumberOrNull(r.protected_tail_drain_tokens) && isNumberOrNull(r.recovery_no_eligible_head_count) && isNumberOrNull(r.force_emergency_bypass_window_start) && isNumberOrNull(r.force_emergency_bypass_used) && isNumberOrNull(r.upgrade_reminded_at) && isNumberOrNull(r.pi_stable_id_scheme);
161022
+ return typeof r.session_id === "string" && typeof r.last_response_time === "number" && isStringOrNull(r.cache_ttl) && typeof r.counter === "number" && typeof r.last_nudge_tokens === "number" && isStringOrNull(r.last_nudge_band) && isStringOrNull(r.last_transform_error) && typeof r.is_subagent === "number" && typeof r.last_context_percentage === "number" && typeof r.last_input_tokens === "number" && isNumberOrNull(r.observed_safe_input_tokens) && isNumberOrNull(r.cache_alert_sent) && isNumberOrNull(r.times_execute_threshold_reached) && isNumberOrNull(r.compartment_in_progress) && (r.system_prompt_hash === null || typeof r.system_prompt_hash === "string" || typeof r.system_prompt_hash === "number") && isNumberOrNull(r.system_prompt_tokens) && isNumberOrNull(r.conversation_tokens) && isNumberOrNull(r.tool_call_tokens) && isNumberOrNull(r.cleared_reasoning_through_tag) && isStringOrNull(r.last_todo_state) && isBlobOrNull(r.cached_m0_bytes) && isBlobOrNull(r.cached_m1_bytes) && isNumberOrNull(r.cached_m0_project_memory_epoch) && isStringOrNull(r.cached_m0_workspace_fingerprint) && isNumberOrNull(r.cached_m0_project_user_profile_version) && isNumberOrNull(r.cached_m0_max_compartment_seq) && isNumberOrNull(r.cached_m0_max_memory_id) && isNumberOrNull(r.cached_m0_max_mutation_id) && isNumberOrNull(r.cached_m0_max_memory_mutation_id) && isStringOrNull(r.cached_m0_project_docs_hash) && isNumberOrNull(r.cached_m0_materialized_at) && isNumberOrNull(r.cached_m0_session_facts_version) && isStringOrNull(r.cached_m0_upgrade_state) && isStringOrNull(r.cached_m0_system_hash) && isStringOrNull(r.cached_m0_tool_set_hash) && isStringOrNull(r.cached_m0_model_key) && isStringOrNull(r.last_observed_model_key) && isNumberOrNull(r.last_usage_context_limit) && isNumberOrNull(r.prior_boundary_ordinal) && isNumberOrNull(r.protected_tail_policy_version) && isNumberOrNull(r.protected_tail_drain_window_started_at) && isNumberOrNull(r.protected_tail_drain_tokens) && isNumberOrNull(r.recovery_no_eligible_head_count) && isNumberOrNull(r.force_emergency_bypass_window_start) && isNumberOrNull(r.force_emergency_bypass_used) && isNumberOrNull(r.upgrade_reminded_at) && isNumberOrNull(r.pi_stable_id_scheme);
159803
161023
  }
159804
161024
  function getDefaultSessionMeta(sessionId) {
159805
161025
  return {
@@ -159826,6 +161046,7 @@ function getDefaultSessionMeta(sessionId) {
159826
161046
  cachedM0Bytes: null,
159827
161047
  cachedM1Bytes: null,
159828
161048
  cachedM0ProjectMemoryEpoch: null,
161049
+ cachedM0WorkspaceFingerprint: null,
159829
161050
  cachedM0ProjectUserProfileVersion: null,
159830
161051
  cachedM0MaxCompartmentSeq: null,
159831
161052
  cachedM0MaxMemoryId: null,
@@ -159888,6 +161109,7 @@ function toSessionMeta(row) {
159888
161109
  cachedM0Bytes: toBufferOrNull(row.cached_m0_bytes),
159889
161110
  cachedM1Bytes: toBufferOrNull(row.cached_m1_bytes),
159890
161111
  cachedM0ProjectMemoryEpoch: numOrNull(row.cached_m0_project_memory_epoch),
161112
+ cachedM0WorkspaceFingerprint: stringOrNull(row.cached_m0_workspace_fingerprint),
159891
161113
  cachedM0ProjectUserProfileVersion: numOrNull(row.cached_m0_project_user_profile_version),
159892
161114
  cachedM0MaxCompartmentSeq: numOrNull(row.cached_m0_max_compartment_seq),
159893
161115
  cachedM0MaxMemoryId: numOrNull(row.cached_m0_max_memory_id),
@@ -159918,6 +161140,7 @@ function persistCachedM0(db, sessionId, payload) {
159918
161140
  db.prepare(`UPDATE session_meta SET
159919
161141
  cached_m0_bytes = ?,
159920
161142
  cached_m0_project_memory_epoch = ?,
161143
+ cached_m0_workspace_fingerprint = ?,
159921
161144
  cached_m0_project_user_profile_version = ?,
159922
161145
  cached_m0_max_compartment_seq = ?,
159923
161146
  cached_m0_max_memory_id = ?,
@@ -159930,7 +161153,7 @@ function persistCachedM0(db, sessionId, payload) {
159930
161153
  cached_m0_upgrade_state = ?,
159931
161154
  cached_m0_system_hash = ?,
159932
161155
  cached_m0_model_key = ?
159933
- WHERE session_id = ?`).run(Buffer2.from(payload.m0Bytes), payload.projectMemoryEpoch, payload.projectUserProfileVersion, payload.maxCompartmentSeq, payload.maxMemoryId, payload.maxMutationId, payload.maxMemoryMutationId ?? null, payload.m1Bytes ? Buffer2.from(payload.m1Bytes) : null, payload.projectDocsHash, payload.materializedAt, payload.sessionFactsVersion, payload.upgradeState, payload.systemHash ?? "", payload.modelKey ?? "", sessionId);
161156
+ WHERE session_id = ?`).run(Buffer2.from(payload.m0Bytes), payload.projectMemoryEpoch, payload.workspaceFingerprint ?? null, payload.projectUserProfileVersion, payload.maxCompartmentSeq, payload.maxMemoryId, payload.maxMutationId, payload.maxMemoryMutationId ?? null, payload.m1Bytes ? Buffer2.from(payload.m1Bytes) : null, payload.projectDocsHash, payload.materializedAt, payload.sessionFactsVersion, payload.upgradeState, payload.systemHash ?? "", payload.modelKey ?? "", sessionId);
159934
161157
  }
159935
161158
  function clearCachedM0M1(db, sessionId) {
159936
161159
  ensureSessionMetaRow(db, sessionId);
@@ -159939,6 +161162,7 @@ function clearCachedM0M1(db, sessionId) {
159939
161162
  ["cached_m0_bytes", null],
159940
161163
  ["cached_m1_bytes", null],
159941
161164
  ["cached_m0_project_memory_epoch", null],
161165
+ ["cached_m0_workspace_fingerprint", null],
159942
161166
  ["cached_m0_project_user_profile_version", null],
159943
161167
  ["cached_m0_max_compartment_seq", null],
159944
161168
  ["cached_m0_max_memory_id", null],
@@ -160274,8 +161498,8 @@ function readRawSessionTailFromDb(db, sessionId, baseOrdinal, anchorMessageId) {
160274
161498
  const CHUNK = 800;
160275
161499
  for (let i = 0;i < ids.length; i += CHUNK) {
160276
161500
  const slice = ids.slice(i, i + CHUNK);
160277
- const placeholders = slice.map(() => "?").join(",");
160278
- const partRows = db.prepare(`SELECT message_id, data, time_updated FROM part WHERE session_id = ? AND message_id IN (${placeholders}) ORDER BY time_created ASC, id ASC`).all(sessionId, ...slice).filter(isRawPartRow);
161501
+ const placeholders2 = slice.map(() => "?").join(",");
161502
+ const partRows = db.prepare(`SELECT message_id, data, time_updated FROM part WHERE session_id = ? AND message_id IN (${placeholders2}) ORDER BY time_created ASC, id ASC`).all(sessionId, ...slice).filter(isRawPartRow);
160279
161503
  for (const part of partRows) {
160280
161504
  const list = partsByMessageId.get(part.message_id) ?? [];
160281
161505
  list.push(attachRawPartVersion(parseJsonUnknown(part.data), part.time_updated));
@@ -165172,53 +166396,6 @@ function createCtxExpandTool(deps) {
165172
166396
  }
165173
166397
  };
165174
166398
  }
165175
-
165176
- // ../plugin/src/features/magic-context/memory/constants.ts
165177
- var V2_MEMORY_CATEGORIES = [
165178
- "PROJECT_RULES",
165179
- "ARCHITECTURE",
165180
- "CONSTRAINTS",
165181
- "CONFIG_VALUES",
165182
- "NAMING"
165183
- ];
165184
- var PROMOTABLE_CATEGORIES = [
165185
- "PROJECT_RULES",
165186
- "ARCHITECTURE",
165187
- "CONSTRAINTS",
165188
- "CONFIG_VALUES",
165189
- "NAMING",
165190
- "ARCHITECTURE_DECISIONS",
165191
- "CONFIG_DEFAULTS",
165192
- "USER_PREFERENCES",
165193
- "USER_DIRECTIVES",
165194
- "ENVIRONMENT",
165195
- "WORKFLOW_RULES",
165196
- "KNOWN_ISSUES"
165197
- ];
165198
- var CATEGORY_PRIORITY = [
165199
- "PROJECT_RULES",
165200
- "ARCHITECTURE",
165201
- "CONSTRAINTS",
165202
- "CONFIG_VALUES",
165203
- "NAMING",
165204
- "USER_DIRECTIVES",
165205
- "USER_PREFERENCES",
165206
- "CONFIG_DEFAULTS",
165207
- "ARCHITECTURE_DECISIONS",
165208
- "ENVIRONMENT",
165209
- "WORKFLOW_RULES",
165210
- "KNOWN_ISSUES"
165211
- ];
165212
- var MEMORY_CATEGORY_ORDER_UNKNOWN = 99;
165213
- var MEMORY_CATEGORY_ORDER_PRIORITY = CATEGORY_PRIORITY.reduce((acc, category, index) => {
165214
- acc[category] = index;
165215
- return acc;
165216
- }, {});
165217
- var MEMORY_CATEGORY_ORDER_SQL = `CASE category ${CATEGORY_PRIORITY.map((category, index) => `WHEN '${category}' THEN ${index}`).join(" ")} ELSE ${MEMORY_CATEGORY_ORDER_UNKNOWN} END`;
165218
- var CATEGORY_DEFAULT_TTL = {
165219
- WORKFLOW_RULES: 90 * 24 * 60 * 60 * 1000,
165220
- KNOWN_ISSUES: 30 * 24 * 60 * 60 * 1000
165221
- };
165222
166399
  // ../plugin/src/features/magic-context/memory/embedding-backfill.ts
165223
166400
  init_logger();
165224
166401
  async function ensureMemoryEmbeddings(args) {
@@ -165243,7 +166420,7 @@ async function ensureMemoryEmbeddings(args) {
165243
166420
  continue;
165244
166421
  }
165245
166422
  saveEmbedding(args.db, memory2.id, embedding, result.modelId);
165246
- staged.set(memory2.id, embedding);
166423
+ staged.set(memory2.id, { embedding, modelId: result.modelId });
165247
166424
  }
165248
166425
  })();
165249
166426
  const currentSnapshot = getProjectEmbeddingSnapshot(args.projectIdentity);
@@ -165446,8 +166623,8 @@ function getMemoriesByProjectStatement(db, statuses) {
165446
166623
  }
165447
166624
  let stmt = statements.get(db);
165448
166625
  if (!stmt) {
165449
- const placeholders = statuses.map(() => "?").join(", ");
165450
- stmt = db.prepare(`SELECT ${getMemorySelectColumns(db)} FROM memories WHERE project_path = ? AND status IN (${placeholders}) AND (expires_at IS NULL OR expires_at > ?) ORDER BY category ASC, updated_at DESC, id ASC`);
166626
+ const placeholders2 = statuses.map(() => "?").join(", ");
166627
+ stmt = db.prepare(`SELECT ${getMemorySelectColumns(db)} FROM memories WHERE project_path = ? AND status IN (${placeholders2}) AND (expires_at IS NULL OR expires_at > ?) ORDER BY category ASC, updated_at DESC, id ASC`);
165451
166628
  statements.set(db, stmt);
165452
166629
  }
165453
166630
  return stmt;
@@ -165592,6 +166769,97 @@ function getMemoriesByProject(db, projectPath, statuses = ["active", "permanent"
165592
166769
  const rows = getMemoriesByProjectStatement(db, statuses).all(projectPath, ...statuses, expiryCutoff).filter(isMemoryRow);
165593
166770
  return rows.map(toMemory);
165594
166771
  }
166772
+ function sqlPlaceholders(values) {
166773
+ return values.map(() => "?").join(", ");
166774
+ }
166775
+ function uniqueValues(values) {
166776
+ return [...new Set(values.filter((value) => value.length > 0))];
166777
+ }
166778
+ function buildWorkspaceMemorySqlFilter(args) {
166779
+ if (args.shareCategories === null || args.shareCategories === undefined) {
166780
+ return { clause: "", params: [], active: false };
166781
+ }
166782
+ const identities = uniqueValues(args.identities);
166783
+ const identitySet = new Set(identities);
166784
+ const ownSet = new Set(uniqueValues(args.ownIdentities ?? []).filter((identity) => identitySet.has(identity)));
166785
+ const foreignIdentities = identities.filter((identity) => !ownSet.has(identity));
166786
+ if (foreignIdentities.length === 0) {
166787
+ return { clause: "", params: [], active: false };
166788
+ }
166789
+ const ownIdentities = identities.filter((identity) => ownSet.has(identity));
166790
+ const shareCategories = uniqueValues([...args.shareCategories]);
166791
+ const qualifier = args.tableName ? `${args.tableName}.` : "";
166792
+ const predicates = [];
166793
+ const params = [];
166794
+ if (ownIdentities.length > 0) {
166795
+ predicates.push(`${qualifier}project_path IN (${sqlPlaceholders(ownIdentities)})`);
166796
+ params.push(...ownIdentities);
166797
+ }
166798
+ if (foreignIdentities.length > 0 && shareCategories.length > 0) {
166799
+ predicates.push(`(${qualifier}project_path IN (${sqlPlaceholders(foreignIdentities)}) AND ${qualifier}category IN (${sqlPlaceholders(shareCategories)}))`);
166800
+ params.push(...foreignIdentities, ...shareCategories);
166801
+ }
166802
+ if (predicates.length === 0) {
166803
+ return { clause: " AND 0 = 1", params: [], active: true };
166804
+ }
166805
+ return { clause: ` AND (${predicates.join(" OR ")})`, params, active: true };
166806
+ }
166807
+ function getMemoriesByProjects(db, projectPaths, statuses = ["active", "permanent"], expiryCutoff = Date.now(), ownIdentities, shareCategories) {
166808
+ const identities = uniqueValues(projectPaths);
166809
+ if (identities.length === 0 || statuses.length === 0)
166810
+ return [];
166811
+ const sharingFilter = buildWorkspaceMemorySqlFilter({
166812
+ identities,
166813
+ ownIdentities,
166814
+ shareCategories
166815
+ });
166816
+ if (identities.length === 1 && !sharingFilter.active) {
166817
+ return getMemoriesByProject(db, identities[0], statuses, expiryCutoff);
166818
+ }
166819
+ const rows = db.prepare(`SELECT ${getMemorySelectColumns(db)}
166820
+ FROM memories
166821
+ WHERE project_path IN (${sqlPlaceholders(identities)})
166822
+ AND status IN (${sqlPlaceholders(statuses)})
166823
+ AND (expires_at IS NULL OR expires_at > ?)${sharingFilter.clause}
166824
+ ORDER BY category ASC, updated_at DESC, id ASC`).all(...identities, ...statuses, expiryCutoff, ...sharingFilter.params).filter(isMemoryRow);
166825
+ return rows.map(toMemory);
166826
+ }
166827
+ function getMaxMemoryIdForProjects(db, projectPaths, ownIdentities, shareCategories) {
166828
+ const identities = uniqueValues(projectPaths);
166829
+ if (identities.length === 0)
166830
+ return 0;
166831
+ const sharingFilter = buildWorkspaceMemorySqlFilter({
166832
+ identities,
166833
+ ownIdentities,
166834
+ shareCategories
166835
+ });
166836
+ if (identities.length === 1 && !sharingFilter.active) {
166837
+ const row2 = db.prepare("SELECT COALESCE(MAX(id), 0) AS max_id FROM memories WHERE project_path = ?").get(identities[0]);
166838
+ return typeof row2?.max_id === "number" ? row2.max_id : 0;
166839
+ }
166840
+ const row = db.prepare(`SELECT COALESCE(MAX(id), 0) AS max_id
166841
+ FROM memories
166842
+ WHERE project_path IN (${sqlPlaceholders(identities)})${sharingFilter.clause}`).get(...identities, ...sharingFilter.params);
166843
+ return typeof row?.max_id === "number" ? row.max_id : 0;
166844
+ }
166845
+ function readNewMemoriesForM1Union(db, projectPaths, afterId, expiryCutoff, ownIdentities, shareCategories) {
166846
+ const identities = uniqueValues(projectPaths);
166847
+ if (identities.length === 0)
166848
+ return [];
166849
+ const sharingFilter = buildWorkspaceMemorySqlFilter({
166850
+ identities,
166851
+ ownIdentities,
166852
+ shareCategories
166853
+ });
166854
+ const rows = db.prepare(`SELECT ${getMemorySelectColumns(db)}
166855
+ FROM memories
166856
+ WHERE project_path IN (${sqlPlaceholders(identities)})
166857
+ AND id > ?
166858
+ AND status IN ('active', 'permanent')
166859
+ AND (expires_at IS NULL OR expires_at > ?)${sharingFilter.clause}
166860
+ ORDER BY ${MEMORY_CATEGORY_ORDER_SQL}, id ASC`).all(...identities, afterId, expiryCutoff, ...sharingFilter.params).filter(isMemoryRow);
166861
+ return rows.map(toMemory);
166862
+ }
165595
166863
  function getAllActiveMemoriesForMigration(db, projectPath) {
165596
166864
  const rows = getActiveMemoriesNoExpiryStatement(db).all(projectPath).filter(isMemoryRow);
165597
166865
  return rows.map(toMemory);
@@ -165759,6 +167027,7 @@ async function embedAndStoreMemory(db, sessionId, projectPath, memoryId, content
165759
167027
  // ../plugin/src/features/magic-context/memory/storage-memory-fts.ts
165760
167028
  var DEFAULT_SEARCH_LIMIT = 10;
165761
167029
  var searchStatements = new WeakMap;
167030
+ var unionSearchStatements = new Map;
165762
167031
  function getSearchStatement(db) {
165763
167032
  let stmt = searchStatements.get(db);
165764
167033
  if (!stmt) {
@@ -165767,6 +167036,23 @@ function getSearchStatement(db) {
165767
167036
  }
165768
167037
  return stmt;
165769
167038
  }
167039
+ function getUnionSearchStatement(db, arity) {
167040
+ let statements = unionSearchStatements.get(arity);
167041
+ if (!statements) {
167042
+ statements = new WeakMap;
167043
+ unionSearchStatements.set(arity, statements);
167044
+ }
167045
+ let stmt = statements.get(db);
167046
+ if (!stmt) {
167047
+ const placeholders2 = Array.from({ length: arity }, () => "?").join(", ");
167048
+ stmt = db.prepare(`SELECT ${getMemorySelectColumns(db)} FROM memories_fts INNER JOIN memories ON memories.id = memories_fts.rowid WHERE memories.project_path IN (${placeholders2}) AND memories.status IN ('active', 'permanent') AND (memories.expires_at IS NULL OR memories.expires_at > ?) AND memories_fts MATCH ? ORDER BY bm25(memories_fts), memories.updated_at DESC, memories.id ASC LIMIT ?`);
167049
+ statements.set(db, stmt);
167050
+ }
167051
+ return stmt;
167052
+ }
167053
+ function uniqueProjectPaths(projectPaths) {
167054
+ return [...new Set(projectPaths.filter((path4) => path4.length > 0))];
167055
+ }
165770
167056
  function sanitizeFtsQuery(query) {
165771
167057
  const tokens = query.split(/\s+/).filter((token) => token.length > 0);
165772
167058
  if (tokens.length === 0)
@@ -165785,6 +167071,28 @@ function searchMemoriesFTS(db, projectPath, query, limit = DEFAULT_SEARCH_LIMIT)
165785
167071
  const rows = getSearchStatement(db).all(projectPath, Date.now(), sanitized, limit).filter(isMemoryRow);
165786
167072
  return rows.map(toMemory);
165787
167073
  }
167074
+ function searchMemoriesFTSUnion(db, projectPaths, query, limit = DEFAULT_SEARCH_LIMIT, ownIdentities, shareCategories) {
167075
+ const identities = uniqueProjectPaths(projectPaths);
167076
+ if (identities.length === 0)
167077
+ return [];
167078
+ const sharingFilter = buildWorkspaceMemorySqlFilter({
167079
+ identities,
167080
+ ownIdentities,
167081
+ shareCategories,
167082
+ tableName: "memories"
167083
+ });
167084
+ if (identities.length === 1 && !sharingFilter.active) {
167085
+ return searchMemoriesFTS(db, identities[0], query, limit);
167086
+ }
167087
+ const trimmedQuery = query.trim();
167088
+ if (trimmedQuery.length === 0 || limit <= 0)
167089
+ return [];
167090
+ const sanitized = sanitizeFtsQuery(trimmedQuery);
167091
+ if (sanitized.length === 0)
167092
+ return [];
167093
+ const rows = sharingFilter.active ? db.prepare(`SELECT ${getMemorySelectColumns(db)} FROM memories_fts INNER JOIN memories ON memories.id = memories_fts.rowid WHERE memories.project_path IN (${identities.map(() => "?").join(", ")}) AND memories.status IN ('active', 'permanent') AND (memories.expires_at IS NULL OR memories.expires_at > ?) AND memories_fts MATCH ?${sharingFilter.clause} ORDER BY bm25(memories_fts), memories.updated_at DESC, memories.id ASC LIMIT ?`).all(...identities, Date.now(), sanitized, ...sharingFilter.params, limit).filter(isMemoryRow) : getUnionSearchStatement(db, identities.length).all(...identities, Date.now(), sanitized, limit).filter(isMemoryRow);
167094
+ return rows.map(toMemory);
167095
+ }
165788
167096
  // ../plugin/src/features/magic-context/message-index.ts
165789
167097
  var lastIndexedStatements = new WeakMap;
165790
167098
  var insertMessageStatements = new WeakMap;
@@ -165931,7 +167239,7 @@ function indexMessagesAfterOrdinal(db, sessionId, messages, lastIndexedOrdinal,
165931
167239
  return inserted;
165932
167240
  }
165933
167241
  // ../plugin/src/features/magic-context/project-docs-hash.ts
165934
- import { createHash as createHash6 } from "node:crypto";
167242
+ import { createHash as createHash8 } from "node:crypto";
165935
167243
  import { lstatSync, readFileSync as readFileSync4, statSync as statSync2 } from "node:fs";
165936
167244
  import path4 from "node:path";
165937
167245
  var PROJECT_DOC_FILES = ["ARCHITECTURE.md", "STRUCTURE.md"];
@@ -166029,7 +167337,7 @@ function hashCanonicalPieces(hashPieces) {
166029
167337
  if (hashPieces.length === 0) {
166030
167338
  return "";
166031
167339
  }
166032
- return createHash6("sha256").update(hashPieces.join(PROJECT_DOCS_DELIMITER), "utf8").digest("hex");
167340
+ return createHash8("sha256").update(hashPieces.join(PROJECT_DOCS_DELIMITER), "utf8").digest("hex");
166033
167341
  }
166034
167342
  function readProjectDocsCanonical(projectDirectory) {
166035
167343
  const canonicalDirectory = path4.resolve(projectDirectory);
@@ -166135,18 +167443,13 @@ function getMemoryMutation(db, id) {
166135
167443
  WHERE id = ?`).get(id);
166136
167444
  return row ? toMemoryMutation(row) : null;
166137
167445
  }
166138
- function getMemoryMutationsForRender(db, projectPath, afterId, renderedMemoryIds) {
166139
- if (renderedMemoryIds.length === 0)
166140
- return [];
166141
- const uniqueIds = [...new Set(renderedMemoryIds)].sort((left, right) => left - right);
166142
- const placeholders = uniqueIds.map(() => "?").join(", ");
166143
- const rows = db.prepare(`SELECT id, project_path, mutation_type, target_memory_id,
166144
- superseded_by_id, category, new_content, queued_at
166145
- FROM memory_mutation_log
166146
- WHERE project_path = ?
166147
- AND id > ?
166148
- AND target_memory_id IN (${placeholders})
166149
- ORDER BY id ASC`).all(projectPath, afterId ?? 0, ...uniqueIds);
167446
+ function uniqueProjectPaths2(projectPaths) {
167447
+ return [...new Set(projectPaths.filter((path5) => path5.length > 0))];
167448
+ }
167449
+ function placeholders2(values) {
167450
+ return values.map(() => "?").join(", ");
167451
+ }
167452
+ function coalesceMutations(rows) {
166150
167453
  const chosenByTarget = new Map;
166151
167454
  for (const dbRow of rows) {
166152
167455
  const candidate = toMemoryMutation(dbRow);
@@ -166167,10 +167470,54 @@ function getMemoryMutationsForRender(db, projectPath, afterId, renderedMemoryIds
166167
167470
  }
166168
167471
  return [...chosenByTarget.values()].sort((left, right) => left.id - right.id);
166169
167472
  }
167473
+ function getMemoryMutationsForRender(db, projectPath, afterId, renderedMemoryIds) {
167474
+ if (renderedMemoryIds.length === 0)
167475
+ return [];
167476
+ const uniqueIds = [...new Set(renderedMemoryIds)].sort((left, right) => left - right);
167477
+ const placeholders3 = uniqueIds.map(() => "?").join(", ");
167478
+ const rows = db.prepare(`SELECT id, project_path, mutation_type, target_memory_id,
167479
+ superseded_by_id, category, new_content, queued_at
167480
+ FROM memory_mutation_log
167481
+ WHERE project_path = ?
167482
+ AND id > ?
167483
+ AND target_memory_id IN (${placeholders3})
167484
+ ORDER BY id ASC`).all(projectPath, afterId ?? 0, ...uniqueIds);
167485
+ return coalesceMutations(rows);
167486
+ }
167487
+ function getMemoryMutationsForRenderByProjects(db, projectPaths, afterId, renderedMemoryIds) {
167488
+ if (renderedMemoryIds.length === 0)
167489
+ return [];
167490
+ const identities = uniqueProjectPaths2(projectPaths);
167491
+ if (identities.length === 0)
167492
+ return [];
167493
+ if (identities.length === 1) {
167494
+ return getMemoryMutationsForRender(db, identities[0], afterId, renderedMemoryIds);
167495
+ }
167496
+ const uniqueIds = [...new Set(renderedMemoryIds)].sort((left, right) => left - right);
167497
+ const rows = db.prepare(`SELECT id, project_path, mutation_type, target_memory_id,
167498
+ superseded_by_id, category, new_content, queued_at
167499
+ FROM memory_mutation_log
167500
+ WHERE project_path IN (${placeholders2(identities)})
167501
+ AND id > ?
167502
+ AND target_memory_id IN (${placeholders2(uniqueIds)})
167503
+ ORDER BY id ASC`).all(...identities, afterId ?? 0, ...uniqueIds);
167504
+ return coalesceMutations(rows);
167505
+ }
166170
167506
  function getMaxMemoryMutationId(db, projectPath) {
166171
167507
  const row = db.prepare("SELECT MAX(id) AS max_id FROM memory_mutation_log WHERE project_path = ?").get(projectPath);
166172
167508
  return row?.max_id ?? null;
166173
167509
  }
167510
+ function getMaxMemoryMutationIdForProjects(db, projectPaths) {
167511
+ const identities = uniqueProjectPaths2(projectPaths);
167512
+ if (identities.length === 0)
167513
+ return null;
167514
+ if (identities.length === 1)
167515
+ return getMaxMemoryMutationId(db, identities[0]);
167516
+ const row = db.prepare(`SELECT MAX(id) AS max_id
167517
+ FROM memory_mutation_log
167518
+ WHERE project_path IN (${placeholders2(identities)})`).get(...identities);
167519
+ return row?.max_id ?? null;
167520
+ }
166174
167521
  // ../plugin/src/features/magic-context/storage-meta-persisted.ts
166175
167522
  init_logger();
166176
167523
  var CAS_RETRY_LIMIT = 5;
@@ -167016,9 +168363,9 @@ function buildStatusClause(status) {
167016
168363
  if (statuses.length === 0) {
167017
168364
  return null;
167018
168365
  }
167019
- const placeholders = statuses.map(() => "?").join(", ");
168366
+ const placeholders3 = statuses.map(() => "?").join(", ");
167020
168367
  return {
167021
- sql: `status IN (${placeholders})`,
168368
+ sql: `status IN (${placeholders3})`,
167022
168369
  params: statuses
167023
168370
  };
167024
168371
  }
@@ -167214,19 +168561,6 @@ function getProjectState(db, projectPath) {
167214
168561
  WHERE project_path = ?`).get(projectPath);
167215
168562
  return row ? toProjectState(row) : null;
167216
168563
  }
167217
- function bumpProjectMemoryEpoch(db, projectPath, now = Date.now()) {
167218
- db.prepare(`INSERT INTO project_state
167219
- (project_path, project_memory_epoch, project_user_profile_version, updated_at)
167220
- VALUES (?, 1, 0, ?)
167221
- ON CONFLICT(project_path) DO UPDATE SET
167222
- project_memory_epoch = project_memory_epoch + 1,
167223
- updated_at = excluded.updated_at`).run(projectPath, now);
167224
- const state = getProjectState(db, projectPath);
167225
- if (!state) {
167226
- throw new Error(`Failed to bump project memory epoch for ${projectPath}`);
167227
- }
167228
- return state;
167229
- }
167230
168564
  function bumpProjectUserProfileVersion(db, projectPath = GLOBAL_USER_PROFILE_PROJECT_PATH, now = Date.now()) {
167231
168565
  db.prepare(`INSERT INTO project_state
167232
168566
  (project_path, project_memory_epoch, project_user_profile_version, updated_at)
@@ -167260,8 +168594,8 @@ function getSourceContents(db, sessionId, tagIds) {
167260
168594
  if (tagIds.length === 0) {
167261
168595
  return new Map;
167262
168596
  }
167263
- const placeholders = tagIds.map(() => "?").join(", ");
167264
- const rows = db.prepare(`SELECT tag_id, content FROM source_contents WHERE session_id = ? AND tag_id IN (${placeholders})`).all(sessionId, ...tagIds).filter(isSourceContentRow);
168597
+ const placeholders3 = tagIds.map(() => "?").join(", ");
168598
+ const rows = db.prepare(`SELECT tag_id, content FROM source_contents WHERE session_id = ? AND tag_id IN (${placeholders3})`).all(sessionId, ...tagIds).filter(isSourceContentRow);
167265
168599
  const sources = new Map;
167266
168600
  for (const row of rows) {
167267
168601
  sources.set(row.tag_id, row.content);
@@ -167352,15 +168686,22 @@ function ownerMessageIdForTagRow(row) {
167352
168686
  }
167353
168687
  return row.message_id.replace(CONTENT_ID_SUFFIX, "");
167354
168688
  }
167355
- function getActiveTagTokenAggregate(db, sessionId) {
167356
- const row = db.prepare(`SELECT
168689
+ function getActiveTagTokenAggregate(db, sessionId, protectedTags = 0) {
168690
+ const toolOutputExpr = protectedTags > 0 ? `COALESCE(SUM(CASE WHEN type = 'tool' AND tag_number < (
168691
+ SELECT tag_number FROM tags
168692
+ WHERE session_id = ? AND status = 'active'
168693
+ ORDER BY tag_number DESC LIMIT 1 OFFSET ?
168694
+ ) THEN COALESCE(token_count, 0) ELSE 0 END), 0)` : `COALESCE(SUM(CASE WHEN type = 'tool' THEN COALESCE(token_count, 0) ELSE 0 END), 0)`;
168695
+ const sql = `SELECT
167357
168696
  COALESCE(SUM(CASE WHEN type != 'tool' THEN COALESCE(token_count, 0) ELSE 0 END), 0)
167358
168697
  + COALESCE(SUM(COALESCE(reasoning_token_count, 0)), 0) AS conversation,
167359
168698
  COALESCE(SUM(CASE WHEN type = 'tool' THEN COALESCE(token_count, 0) + COALESCE(input_token_count, 0) ELSE 0 END), 0) AS tool_call,
167360
- COALESCE(SUM(CASE WHEN type = 'tool' THEN COALESCE(token_count, 0) ELSE 0 END), 0) AS tool_output,
168699
+ ${toolOutputExpr} AS tool_output,
167361
168700
  COALESCE(SUM(CASE WHEN token_count IS NULL THEN 1 ELSE 0 END), 0) AS null_count
167362
168701
  FROM tags
167363
- WHERE session_id = ? AND status = 'active'`).get(sessionId);
168702
+ WHERE session_id = ? AND status = 'active'`;
168703
+ const params = protectedTags > 0 ? [sessionId, protectedTags - 1, sessionId] : [sessionId];
168704
+ const row = db.prepare(sql).get(...params);
167364
168705
  return {
167365
168706
  conversation: row?.conversation ?? 0,
167366
168707
  toolCall: row?.tool_call ?? 0,
@@ -167551,8 +168892,8 @@ function getTagsByNumbers(db, sessionId, tagNumbers) {
167551
168892
  }
167552
168893
  return all;
167553
168894
  }
167554
- const placeholders = tagNumbers.map(() => "?").join(",");
167555
- const rows = db.prepare(`SELECT ${TAG_SELECT_COLUMNS} FROM tags WHERE session_id = ? AND tag_number IN (${placeholders}) ORDER BY tag_number ASC, id ASC`).all(sessionId, ...tagNumbers).filter(isTagRow);
168895
+ const placeholders3 = tagNumbers.map(() => "?").join(",");
168896
+ const rows = db.prepare(`SELECT ${TAG_SELECT_COLUMNS} FROM tags WHERE session_id = ? AND tag_number IN (${placeholders3}) ORDER BY tag_number ASC, id ASC`).all(sessionId, ...tagNumbers).filter(isTagRow);
167556
168897
  return rows.map(toTagEntry);
167557
168898
  }
167558
168899
  var getToolTagNumberByOwnerStatements = new WeakMap;
@@ -167742,6 +169083,23 @@ function createCtxMemoryTool(deps) {
167742
169083
  }
167743
169084
  const projectIdentity = resolveProjectIdentity(ctx.cwd);
167744
169085
  await deps.ensureProjectRegistered?.(ctx.cwd, deps.db);
169086
+ const workspaceIdentitySet = resolveWorkspaceIdentitySet(deps.db, projectIdentity);
169087
+ const expandedWorkspace = expandWorkspaceIdentitySetWithAliases(deps.db, workspaceIdentitySet.identities);
169088
+ const workspaceVisibleIdentities = workspaceIdentitySet.identities.length > 1 ? expandedWorkspace.expandedIdentities : workspaceIdentitySet.identities;
169089
+ const targetIdentityForStoredPath = (rawProjectPath) => workspaceIdentitySet.identities.length > 1 ? resolveStoredPathWorkspaceIdentity(rawProjectPath, workspaceIdentitySet.identities, expandedWorkspace.canonicalIdentityByStoredPath) ?? normalizeStoredProjectPath(rawProjectPath) : normalizeStoredProjectPath(rawProjectPath);
169090
+ const toolShareCategories = workspaceIdentitySet.identities.length > 1 ? resolveWorkspaceShareCategories(deps.db, projectIdentity) : null;
169091
+ const memoryVisibleToTool = (memory2) => {
169092
+ if (workspaceIdentitySet.identities.length <= 1) {
169093
+ return storedPathBelongsToIdentity(memory2.projectPath, projectIdentity);
169094
+ }
169095
+ if (!storedPathBelongsToWorkspace(memory2.projectPath, workspaceIdentitySet.identities, workspaceVisibleIdentities, expandedWorkspace.canonicalIdentityByStoredPath)) {
169096
+ return false;
169097
+ }
169098
+ const isOwn = targetIdentityForStoredPath(memory2.projectPath) === projectIdentity;
169099
+ if (isOwn)
169100
+ return true;
169101
+ return toolShareCategories === null || toolShareCategories.includes(memory2.category);
169102
+ };
167745
169103
  const snapshot = getProjectEmbeddingSnapshot(projectIdentity);
167746
169104
  if (snapshot ? !snapshot.features.memoryEnabled : deps.memoryEnabled === false) {
167747
169105
  return err2("Cross-session memory is disabled for this project.");
@@ -167788,28 +169146,34 @@ function createCtxMemoryTool(deps) {
167788
169146
  return err2("Error: 'content' is required when action is 'update'.");
167789
169147
  }
167790
169148
  const memory2 = getMemoryById(deps.db, updateId);
167791
- if (!memory2 || !storedPathBelongsToIdentity(memory2.projectPath, projectIdentity)) {
169149
+ if (!memory2 || !memoryVisibleToTool(memory2)) {
167792
169150
  return err2(`Error: Memory with ID ${updateId} was not found.`);
167793
169151
  }
167794
169152
  if (!dreamerAllowed && !isPrimaryMutableMemory(memory2)) {
167795
169153
  return err2(inactiveMemoryError(updateId, "updating"));
167796
169154
  }
167797
169155
  const normalizedHash = computeNormalizedHash(content);
167798
- const duplicate = getMemoryByHash(deps.db, projectIdentity, memory2.category, normalizedHash);
169156
+ const targetIdentity = targetIdentityForStoredPath(memory2.projectPath);
169157
+ const duplicate = getMemoryByHash(deps.db, targetIdentity, memory2.category, normalizedHash);
167799
169158
  if (duplicate && duplicate.id !== memory2.id) {
167800
169159
  return err2(`Error: Memory content already exists as ID ${duplicate.id}; merge or archive duplicates instead.`);
167801
169160
  }
167802
169161
  deps.db.transaction(() => {
167803
169162
  updateMemoryContent(deps.db, memory2.id, content, normalizedHash);
167804
169163
  queueMemoryMutation(deps.db, {
167805
- projectPath: projectIdentity,
169164
+ projectPath: targetIdentity,
167806
169165
  mutationType: "update",
167807
169166
  targetMemoryId: memory2.id,
167808
169167
  category: memory2.category,
167809
169168
  newContent: content
167810
169169
  });
167811
169170
  })();
167812
- queueEmbedding({ deps, projectIdentity, memoryId: memory2.id, content });
169171
+ queueEmbedding({
169172
+ deps,
169173
+ projectIdentity: targetIdentity,
169174
+ memoryId: memory2.id,
169175
+ content
169176
+ });
167813
169177
  return ok2(`Updated memory [ID: ${memory2.id}] in ${memory2.category}.`);
167814
169178
  }
167815
169179
  if (params.action === "merge") {
@@ -167829,7 +169193,7 @@ function createCtxMemoryTool(deps) {
167829
169193
  return err2("Error: One or more source memories were not found.");
167830
169194
  }
167831
169195
  if (!dreamerAllowed) {
167832
- const foreign = sourceMemories.find((memory2) => !storedPathBelongsToIdentity(memory2.projectPath, projectIdentity));
169196
+ const foreign = sourceMemories.find((memory2) => !memoryVisibleToTool(memory2));
167833
169197
  if (foreign) {
167834
169198
  return err2(`Error: Memory with ID ${foreign.id} was not found.`);
167835
169199
  }
@@ -167918,26 +169282,36 @@ function createCtxMemoryTool(deps) {
167918
169282
  return ok2(`Merged memories [${ids.join(", ")}] into canonical memory [ID: ${canonicalMemory.id}] in ${category}; superseded [${supersededIds.join(", ")}].`);
167919
169283
  }
167920
169284
  if (params.action === "archive") {
167921
- const archiveIds = params.ids;
167922
- if (!archiveIds || archiveIds.length === 0 || !archiveIds.every(Number.isInteger)) {
169285
+ const rawArchiveIds = params.ids;
169286
+ if (!rawArchiveIds || rawArchiveIds.length === 0 || !rawArchiveIds.every(Number.isInteger)) {
167923
169287
  return err2("Error: 'ids' must contain at least one integer memory ID when action is 'archive'.");
167924
169288
  }
169289
+ const archiveIds = [...new Set(rawArchiveIds)];
167925
169290
  for (const memoryId of archiveIds) {
167926
169291
  const memory2 = getMemoryById(deps.db, memoryId);
167927
- if (!memory2 || !storedPathBelongsToIdentity(memory2.projectPath, projectIdentity)) {
169292
+ if (!memory2 || !memoryVisibleToTool(memory2)) {
167928
169293
  return err2(`Error: Memory with ID ${memoryId} was not found.`);
167929
169294
  }
167930
169295
  if (!dreamerAllowed && !isPrimaryMutableMemory(memory2)) {
167931
169296
  return err2(inactiveMemoryError(memoryId, "archiving"));
167932
169297
  }
167933
169298
  }
169299
+ const targets = archiveIds.map((memoryId) => {
169300
+ const memory2 = getMemoryById(deps.db, memoryId);
169301
+ if (!memory2)
169302
+ throw new Error(`validated memory ${memoryId} disappeared`);
169303
+ return {
169304
+ memoryId,
169305
+ projectIdentity: targetIdentityForStoredPath(memory2.projectPath)
169306
+ };
169307
+ });
167934
169308
  deps.db.transaction(() => {
167935
- for (const memoryId of archiveIds) {
167936
- archiveMemory(deps.db, memoryId, params.reason);
169309
+ for (const target2 of targets) {
169310
+ archiveMemory(deps.db, target2.memoryId, params.reason);
167937
169311
  queueMemoryMutation(deps.db, {
167938
- projectPath: projectIdentity,
169312
+ projectPath: target2.projectIdentity,
167939
169313
  mutationType: "archive",
167940
- targetMemoryId: memoryId
169314
+ targetMemoryId: target2.memoryId
167941
169315
  });
167942
169316
  }
167943
169317
  })();
@@ -168909,6 +170283,37 @@ function previewText(text) {
168909
170283
  }
168910
170284
  return `${normalized.slice(0, RESULT_PREVIEW_LIMIT - 1).trimEnd()}…`;
168911
170285
  }
170286
+ function resolveSearchWorkspaceContext(db, projectPath, identitySet) {
170287
+ const resolved = identitySet ?? resolveWorkspaceIdentitySet(db, projectPath);
170288
+ const isWorkspaced = resolved.identities.length > 1;
170289
+ const expanded = expandWorkspaceIdentitySetWithAliases(db, resolved.identities);
170290
+ const expandedIdentities = isWorkspaced ? expanded.expandedIdentities : resolved.identities;
170291
+ const canonicalIdentityByStoredPath = isWorkspaced ? expanded.canonicalIdentityByStoredPath : new Map(resolved.identities.map((identity) => [identity, identity]));
170292
+ const ownIdentities = expandedIdentities.filter((identity) => canonicalIdentityByStoredPath.get(identity) === projectPath);
170293
+ return {
170294
+ identities: resolved.identities,
170295
+ expandedIdentities,
170296
+ ownIdentities,
170297
+ shareCategories: isWorkspaced ? resolveWorkspaceShareCategories(db, projectPath) : null,
170298
+ namesByIdentity: resolved.namesByIdentity,
170299
+ canonicalIdentityByStoredPath,
170300
+ isWorkspaced
170301
+ };
170302
+ }
170303
+ function memoryWorkspaceIdentity(memory2, workspace) {
170304
+ return resolveStoredPathWorkspaceIdentity(memory2.projectPath, workspace.identities, workspace.canonicalIdentityByStoredPath);
170305
+ }
170306
+ function sourceNamesForSearchMemories(args) {
170307
+ if (!args.workspace.isWorkspaced)
170308
+ return;
170309
+ const sourceNames = new Map;
170310
+ for (const memory2 of args.memories) {
170311
+ const source = sourceNameForMemory(memory2.projectPath, args.projectPath, args.workspace.identities, args.workspace.namesByIdentity, args.workspace.canonicalIdentityByStoredPath);
170312
+ if (source)
170313
+ sourceNames.set(memory2.id, source);
170314
+ }
170315
+ return sourceNames.size > 0 ? sourceNames : undefined;
170316
+ }
168912
170317
  function getMessageSearchStatement(db) {
168913
170318
  let stmt = messageSearchStatements.get(db);
168914
170319
  if (!stmt) {
@@ -168917,6 +170322,30 @@ function getMessageSearchStatement(db) {
168917
170322
  }
168918
170323
  return stmt;
168919
170324
  }
170325
+ var ftsRowCountStatements = new WeakMap;
170326
+ var ftsMatchCountStatements = new WeakMap;
170327
+ function getSessionFtsRowCount(db, sessionId) {
170328
+ let stmt = ftsRowCountStatements.get(db);
170329
+ if (!stmt) {
170330
+ stmt = db.prepare("SELECT COUNT(*) AS n FROM message_history_fts WHERE session_id = ?");
170331
+ ftsRowCountStatements.set(db, stmt);
170332
+ }
170333
+ const row = stmt.get(sessionId);
170334
+ return typeof row?.n === "number" ? row.n : 0;
170335
+ }
170336
+ function countSessionFtsMatches(db, sessionId, ftsQuery) {
170337
+ let stmt = ftsMatchCountStatements.get(db);
170338
+ if (!stmt) {
170339
+ stmt = db.prepare("SELECT COUNT(*) AS n FROM message_history_fts WHERE session_id = ? AND message_history_fts MATCH ?");
170340
+ ftsMatchCountStatements.set(db, stmt);
170341
+ }
170342
+ try {
170343
+ const row = stmt.get(sessionId, ftsQuery);
170344
+ return typeof row?.n === "number" ? row.n : 0;
170345
+ } catch {
170346
+ return 0;
170347
+ }
170348
+ }
168920
170349
  function getMessageOrdinal(value) {
168921
170350
  if (typeof value === "number" && Number.isFinite(value)) {
168922
170351
  return value;
@@ -168932,25 +170361,63 @@ async function getSemanticScores(args) {
168932
170361
  if (!args.queryEmbedding || args.memories.length === 0) {
168933
170362
  return semanticScores;
168934
170363
  }
168935
- const cachedEmbeddings = getProjectEmbeddings(args.db, args.projectPath);
168936
- const embeddings = await ensureMemoryEmbeddings({
168937
- db: args.db,
168938
- projectIdentity: args.projectPath,
168939
- memories: args.memories,
168940
- existingEmbeddings: cachedEmbeddings
168941
- });
170364
+ if (!args.workspace?.isWorkspaced) {
170365
+ const cachedEmbeddings = getProjectEmbeddings(args.db, args.projectPath);
170366
+ const embeddings = await ensureMemoryEmbeddings({
170367
+ db: args.db,
170368
+ projectIdentity: args.projectPath,
170369
+ memories: args.memories,
170370
+ existingEmbeddings: cachedEmbeddings
170371
+ });
170372
+ for (const memory2 of args.memories) {
170373
+ const memoryEmbedding = embeddings.get(memory2.id);
170374
+ if (!memoryEmbedding) {
170375
+ continue;
170376
+ }
170377
+ semanticScores.set(memory2.id, normalizeCosineScore(cosineSimilarity(args.queryEmbedding, memoryEmbedding.embedding)));
170378
+ }
170379
+ return semanticScores;
170380
+ }
170381
+ if (!args.queryModelId || args.queryModelId === "off") {
170382
+ return semanticScores;
170383
+ }
170384
+ const workspace = args.workspace;
170385
+ const memoriesByIdentity = new Map;
168942
170386
  for (const memory2 of args.memories) {
168943
- const memoryEmbedding = embeddings.get(memory2.id);
168944
- if (!memoryEmbedding) {
170387
+ const identity = memoryWorkspaceIdentity(memory2, workspace);
170388
+ if (!identity)
168945
170389
  continue;
170390
+ const list = memoriesByIdentity.get(identity) ?? [];
170391
+ list.push(memory2);
170392
+ memoriesByIdentity.set(identity, list);
170393
+ }
170394
+ const ownMemories = memoriesByIdentity.get(args.projectPath) ?? [];
170395
+ if (ownMemories.length > 0) {
170396
+ const ownEmbeddings = getProjectEmbeddings(args.db, args.projectPath);
170397
+ await ensureMemoryEmbeddings({
170398
+ db: args.db,
170399
+ projectIdentity: args.projectPath,
170400
+ memories: ownMemories,
170401
+ existingEmbeddings: ownEmbeddings
170402
+ });
170403
+ }
170404
+ for (const identity of workspace.identities) {
170405
+ const memberMemories = memoriesByIdentity.get(identity) ?? [];
170406
+ if (memberMemories.length === 0)
170407
+ continue;
170408
+ const cachedEmbeddings = getProjectEmbeddings(args.db, identity);
170409
+ for (const memory2 of memberMemories) {
170410
+ const memoryEmbedding = cachedEmbeddings.get(memory2.id);
170411
+ if (!memoryEmbedding || memoryEmbedding.modelId !== args.queryModelId)
170412
+ continue;
170413
+ semanticScores.set(memory2.id, normalizeCosineScore(cosineSimilarity(args.queryEmbedding, memoryEmbedding.embedding)));
168946
170414
  }
168947
- semanticScores.set(memory2.id, normalizeCosineScore(cosineSimilarity(args.queryEmbedding, memoryEmbedding)));
168948
170415
  }
168949
170416
  return semanticScores;
168950
170417
  }
168951
170418
  function getFtsMatches(args) {
168952
170419
  try {
168953
- return searchMemoriesFTS(args.db, args.projectPath, args.query, args.limit);
170420
+ return args.workspace?.isWorkspaced ? searchMemoriesFTSUnion(args.db, args.workspace.expandedIdentities, args.query, args.limit, args.workspace.ownIdentities, args.workspace.shareCategories) : searchMemoriesFTS(args.db, args.projectPath, args.query, args.limit);
168954
170421
  } catch (error51) {
168955
170422
  log(`[search] FTS query failed for "${args.query}": ${error51 instanceof Error ? error51.message : String(error51)}`);
168956
170423
  return [];
@@ -168964,8 +170431,11 @@ function selectSemanticCandidates(args) {
168964
170431
  return args.memories;
168965
170432
  }
168966
170433
  const candidateIds = new Set(args.ftsMatches.map((memory2) => memory2.id));
168967
- const cachedEmbeddings = peekProjectEmbeddings(args.projectPath);
168968
- if (cachedEmbeddings) {
170434
+ const embeddingProjects = args.workspace?.isWorkspaced ? args.workspace.identities : [args.projectPath];
170435
+ for (const projectPath of embeddingProjects) {
170436
+ const cachedEmbeddings = peekProjectEmbeddings(projectPath);
170437
+ if (!cachedEmbeddings)
170438
+ continue;
168969
170439
  for (const memoryId of cachedEmbeddings.keys()) {
168970
170440
  candidateIds.add(memoryId);
168971
170441
  }
@@ -169007,7 +170477,8 @@ function mergeMemoryResults(args) {
169007
170477
  score,
169008
170478
  memoryId: memory2.id,
169009
170479
  category: memory2.category,
169010
- matchType
170480
+ matchType,
170481
+ sourceName: args.sourceNameByMemoryId?.get(memory2.id)
169011
170482
  });
169012
170483
  }
169013
170484
  return results.sort((left, right) => {
@@ -169021,7 +170492,7 @@ async function searchMemories(args) {
169021
170492
  if (!args.memoryEnabled) {
169022
170493
  return [];
169023
170494
  }
169024
- const memories = getMemoriesByProject(args.db, args.projectPath);
170495
+ const memories = args.workspace?.isWorkspaced ? getMemoriesByProjects(args.db, args.workspace.expandedIdentities, ["active", "permanent"], Date.now(), args.workspace.ownIdentities, args.workspace.shareCategories) : getMemoriesByProject(args.db, args.projectPath);
169025
170496
  if (memories.length === 0) {
169026
170497
  return [];
169027
170498
  }
@@ -169029,26 +170500,43 @@ async function searchMemories(args) {
169029
170500
  db: args.db,
169030
170501
  projectPath: args.projectPath,
169031
170502
  query: args.query,
169032
- limit: FTS_SEMANTIC_CANDIDATE_LIMIT
170503
+ limit: FTS_SEMANTIC_CANDIDATE_LIMIT,
170504
+ workspace: args.workspace
169033
170505
  });
169034
170506
  const ftsScores = getFtsScores(ftsMatches);
169035
170507
  const semanticCandidates = selectSemanticCandidates({
169036
170508
  memories,
169037
170509
  projectPath: args.projectPath,
169038
- ftsMatches
170510
+ ftsMatches,
170511
+ workspace: args.workspace
169039
170512
  });
169040
170513
  const semanticScores = await getSemanticScores({
169041
170514
  db: args.db,
169042
170515
  projectPath: args.projectPath,
169043
170516
  memories: semanticCandidates,
169044
- queryEmbedding: args.queryEmbedding
170517
+ queryEmbedding: args.queryEmbedding,
170518
+ queryModelId: args.queryModelId,
170519
+ workspace: args.workspace
169045
170520
  });
169046
170521
  return mergeMemoryResults({
169047
170522
  memories,
169048
170523
  semanticScores,
169049
170524
  ftsScores,
169050
170525
  limit: args.limit,
169051
- visibleMemoryIds: args.visibleMemoryIds
170526
+ visibleMemoryIds: args.visibleMemoryIds,
170527
+ sourceNameByMemoryId: sourceNamesForSearchMemories({
170528
+ memories,
170529
+ projectPath: args.projectPath,
170530
+ workspace: args.workspace ?? {
170531
+ identities: [args.projectPath],
170532
+ expandedIdentities: [args.projectPath],
170533
+ namesByIdentity: new Map,
170534
+ canonicalIdentityByStoredPath: new Map([[args.projectPath, args.projectPath]]),
170535
+ ownIdentities: [args.projectPath],
170536
+ shareCategories: null,
170537
+ isWorkspaced: false
170538
+ }
170539
+ })
169052
170540
  });
169053
170541
  }
169054
170542
  function linearDecayScore(rank, total) {
@@ -169079,7 +170567,13 @@ function runMessageFtsQuery(db, sessionId, ftsQuery, fetchLimit, cutoff) {
169079
170567
  return result;
169080
170568
  }
169081
170569
  var RRF_K = 60;
169082
- var VERBATIM_PROBE_BONUS = 0.5;
170570
+ var VERBATIM_RANK_BONUS = 1 / RRF_K;
170571
+ var IDF_FALLOFF = 100;
170572
+ function probeDiscriminationWeight(df, corpusSize) {
170573
+ if (corpusSize <= 0 || df <= 0)
170574
+ return 1;
170575
+ return 1 / (1 + IDF_FALLOFF * df / corpusSize);
170576
+ }
169083
170577
  function searchMessages(args) {
169084
170578
  const cutoff = args.maxOrdinal != null && args.maxOrdinal >= 0 ? args.maxOrdinal : null;
169085
170579
  const fetchLimit = args.maxOrdinal != null && args.maxOrdinal >= 0 ? args.limit * 3 : args.limit;
@@ -169096,20 +170590,31 @@ function searchMessages(args) {
169096
170590
  role: row.role
169097
170591
  }));
169098
170592
  }
170593
+ const corpusSize = getSessionFtsRowCount(args.db, args.sessionId);
169099
170594
  const queryLists = [];
169100
170595
  if (baseQuery.length > 0) {
169101
- queryLists.push(runMessageFtsQuery(args.db, args.sessionId, baseQuery, fetchLimit, cutoff));
170596
+ queryLists.push({
170597
+ rows: runMessageFtsQuery(args.db, args.sessionId, baseQuery, fetchLimit, cutoff),
170598
+ weight: 1
170599
+ });
169102
170600
  }
170601
+ const probeWeights = new Map;
169103
170602
  for (const probe of probes) {
169104
170603
  const probeQuery = sanitizeFtsQuery(probe);
169105
170604
  if (probeQuery.length === 0)
169106
170605
  continue;
169107
- queryLists.push(runMessageFtsQuery(args.db, args.sessionId, probeQuery, fetchLimit, cutoff));
170606
+ const df = countSessionFtsMatches(args.db, args.sessionId, probeQuery);
170607
+ const weight = probeDiscriminationWeight(df, corpusSize);
170608
+ probeWeights.set(probe, weight);
170609
+ queryLists.push({
170610
+ rows: runMessageFtsQuery(args.db, args.sessionId, probeQuery, fetchLimit, cutoff),
170611
+ weight
170612
+ });
169108
170613
  }
169109
170614
  const fused = new Map;
169110
170615
  for (const list of queryLists) {
169111
- list.forEach((row, rank) => {
169112
- const rrf = 1 / (RRF_K + rank);
170616
+ list.rows.forEach((row, rank) => {
170617
+ const rrf = list.weight / (RRF_K + rank);
169113
170618
  const existing = fused.get(row.messageId);
169114
170619
  if (existing) {
169115
170620
  existing.score += rrf;
@@ -169119,26 +170624,107 @@ function searchMessages(args) {
169119
170624
  });
169120
170625
  }
169121
170626
  for (const entry of fused.values()) {
169122
- if (containsProbeVerbatim(entry.row.content, probes)) {
169123
- entry.score += VERBATIM_PROBE_BONUS;
170627
+ let best = 0;
170628
+ for (const probe of probes) {
170629
+ const weight = probeWeights.get(probe) ?? 0;
170630
+ if (weight > best && containsProbeVerbatim(entry.row.content, [probe])) {
170631
+ best = weight;
170632
+ }
170633
+ }
170634
+ if (best > 0) {
170635
+ entry.score += best * VERBATIM_RANK_BONUS;
169124
170636
  }
169125
170637
  }
169126
170638
  const ranked = [...fused.values()].sort((a, b) => b.score !== a.score ? b.score - a.score : a.row.messageOrdinal - b.row.messageOrdinal).slice(0, args.limit);
169127
- const maxScore = ranked.length > 0 ? ranked[0].score : 1;
169128
- return ranked.map((entry) => ({
170639
+ return ranked.map((entry, rank) => ({
169129
170640
  source: "message",
169130
170641
  content: previewText(entry.row.content),
169131
- score: maxScore > 0 ? entry.score / maxScore : 0,
170642
+ score: linearDecayScore(rank, ranked.length),
169132
170643
  messageOrdinal: entry.row.messageOrdinal,
169133
170644
  messageId: entry.row.messageId,
169134
170645
  role: entry.row.role
169135
170646
  }));
169136
170647
  }
170648
+ function searchCompartmentChunks(args) {
170649
+ if (!args.queryEmbedding || args.limit <= 0)
170650
+ return [];
170651
+ const cutoff = args.maxOrdinal != null && args.maxOrdinal >= 0 ? args.maxOrdinal : null;
170652
+ const rows = loadCompartmentChunkEmbeddingsForSearch(args.db, args.sessionId, args.projectPath, args.modelId);
170653
+ if (rows.length === 0)
170654
+ return [];
170655
+ const byCompartment = new Map;
170656
+ for (const row of rows) {
170657
+ if (cutoff !== null && row.endOrdinal > cutoff) {
170658
+ continue;
170659
+ }
170660
+ const score = normalizeCosineScore(cosineSimilarity(args.queryEmbedding, row.vector));
170661
+ if (score <= 0)
170662
+ continue;
170663
+ const existing = byCompartment.get(row.compartmentId);
170664
+ if (!existing || score > existing.score) {
170665
+ byCompartment.set(row.compartmentId, { row, score });
170666
+ }
170667
+ }
170668
+ return [...byCompartment.values()].sort((left, right) => right.score !== left.score ? right.score - left.score : left.row.startOrdinal - right.row.startOrdinal).slice(0, args.limit).map(({ row, score }) => ({
170669
+ source: "compartment",
170670
+ content: previewText(row.title),
170671
+ score: score * SINGLE_SOURCE_PENALTY,
170672
+ compartmentId: row.compartmentId,
170673
+ sessionId: row.sessionId,
170674
+ title: row.title,
170675
+ startOrdinal: row.startOrdinal,
170676
+ endOrdinal: row.endOrdinal,
170677
+ matchType: "semantic"
170678
+ }));
170679
+ }
170680
+ function mergeMessageAndCompartmentResults(args) {
170681
+ if (args.compartments.length === 0)
170682
+ return args.messages;
170683
+ if (args.messages.length === 0)
170684
+ return args.compartments;
170685
+ const fused = new Map;
170686
+ const add = (key, result, score, tieOrdinal) => {
170687
+ const existing = fused.get(key);
170688
+ if (existing) {
170689
+ existing.score += score;
170690
+ return existing;
170691
+ }
170692
+ const entry = { result, score, tieOrdinal, snippetScore: -1 };
170693
+ fused.set(key, entry);
170694
+ return entry;
170695
+ };
170696
+ args.compartments.forEach((compartment, rank) => {
170697
+ add(`compartment:${compartment.compartmentId}`, compartment, 1 / (RRF_K + rank), compartment.startOrdinal);
170698
+ });
170699
+ for (const [rank, message] of args.messages.entries()) {
170700
+ const containing = args.compartments.find((compartment) => message.messageOrdinal >= compartment.startOrdinal && message.messageOrdinal <= compartment.endOrdinal);
170701
+ const contribution = 1 / (RRF_K + rank);
170702
+ if (!containing) {
170703
+ add(`message:${message.messageId}`, message, contribution, message.messageOrdinal);
170704
+ continue;
170705
+ }
170706
+ const entry = add(`compartment:${containing.compartmentId}`, containing, contribution, containing.startOrdinal);
170707
+ if (message.score > entry.snippetScore && entry.result.source === "compartment") {
170708
+ entry.snippetScore = message.score;
170709
+ entry.result = {
170710
+ ...entry.result,
170711
+ matchType: "hybrid",
170712
+ snippet: message.content
170713
+ };
170714
+ }
170715
+ }
170716
+ const ranked = [...fused.values()].sort((left, right) => right.score !== left.score ? right.score - left.score : left.tieOrdinal - right.tieOrdinal).slice(0, args.limit);
170717
+ return ranked.map((entry, rank) => ({
170718
+ ...entry.result,
170719
+ score: linearDecayScore(rank, ranked.length)
170720
+ }));
170721
+ }
169137
170722
  function getSourceBoost(result) {
169138
170723
  switch (result.source) {
169139
170724
  case "memory":
169140
170725
  return MEMORY_SOURCE_BOOST;
169141
170726
  case "message":
170727
+ case "compartment":
169142
170728
  return MESSAGE_SOURCE_BOOST;
169143
170729
  case "git_commit":
169144
170730
  return GIT_COMMIT_SOURCE_BOOST;
@@ -169156,6 +170742,9 @@ function compareUnifiedResults(left, right) {
169156
170742
  if (left.source === "message" && right.source === "message") {
169157
170743
  return left.messageOrdinal - right.messageOrdinal;
169158
170744
  }
170745
+ if (left.source === "compartment" && right.source === "compartment") {
170746
+ return left.startOrdinal - right.startOrdinal;
170747
+ }
169159
170748
  if (left.source === "git_commit" && right.source === "git_commit") {
169160
170749
  return right.committedAtMs - left.committedAtMs;
169161
170750
  }
@@ -169206,10 +170795,12 @@ async function unifiedSearch(db, sessionId, projectPath, query, options3 = {}) {
169206
170795
  const isEmbeddingRuntimeEnabled = options3.isEmbeddingRuntimeEnabled ?? isEmbeddingEnabled;
169207
170796
  const gitCommitsEnabled = options3.gitCommitsEnabled ?? false;
169208
170797
  const activeSources = resolveSources(options3.sources);
169209
- const runMemory = activeSources.has("memory") && (options3.memoryEnabled ?? true);
170798
+ const memoryFeatureEnabled = options3.memoryEnabled ?? true;
170799
+ const runMemory = activeSources.has("memory") && memoryFeatureEnabled;
169210
170800
  const runMessages = activeSources.has("message");
169211
170801
  const runGitCommits = activeSources.has("git_commit") && gitCommitsEnabled;
169212
- const needsEmbedding = (runMemory || runGitCommits) && embeddingEnabled && isEmbeddingRuntimeEnabled();
170802
+ const runCompartmentChunks = runMessages && memoryFeatureEnabled && embeddingEnabled;
170803
+ const needsEmbedding = (runMemory || runGitCommits || runCompartmentChunks) && embeddingEnabled && isEmbeddingRuntimeEnabled();
169213
170804
  const queryEmbeddingPromise = needsEmbedding ? embedQuery(trimmedQuery, options3.signal).catch((error51) => {
169214
170805
  log(`[search] query embedding failed: ${error51 instanceof Error ? error51.message : String(error51)}`);
169215
170806
  return null;
@@ -169225,6 +170816,24 @@ async function unifiedSearch(db, sessionId, projectPath, query, options3 = {}) {
169225
170816
  probes: messageProbes
169226
170817
  }) : [];
169227
170818
  const queryEmbedding = await queryEmbeddingPromise;
170819
+ const workspace = resolveSearchWorkspaceContext(db, projectPath);
170820
+ const embeddingSnapshot = getProjectEmbeddingSnapshot(projectPath);
170821
+ const embeddingModelId = embeddingSnapshot?.modelId;
170822
+ const chunkModelId = embeddingSnapshot?.chunkModelId;
170823
+ const compartmentResults = runCompartmentChunks ? searchCompartmentChunks({
170824
+ db,
170825
+ sessionId,
170826
+ projectPath,
170827
+ queryEmbedding,
170828
+ limit: tierLimit,
170829
+ maxOrdinal: options3.maxMessageOrdinal,
170830
+ modelId: chunkModelId && chunkModelId !== "off" ? chunkModelId : null
170831
+ }) : [];
170832
+ const messageLikeResults = mergeMessageAndCompartmentResults({
170833
+ messages: messageResults,
170834
+ compartments: compartmentResults,
170835
+ limit: tierLimit
170836
+ });
169228
170837
  const [memoryResults, gitCommitResults] = await Promise.all([
169229
170838
  runMemory ? searchMemories({
169230
170839
  db,
@@ -169233,6 +170842,8 @@ async function unifiedSearch(db, sessionId, projectPath, query, options3 = {}) {
169233
170842
  limit: tierLimit,
169234
170843
  memoryEnabled: true,
169235
170844
  queryEmbedding,
170845
+ queryModelId: embeddingModelId && embeddingModelId !== "off" ? embeddingModelId : null,
170846
+ workspace,
169236
170847
  visibleMemoryIds: options3.visibleMemoryIds
169237
170848
  }) : Promise.resolve([]),
169238
170849
  runGitCommits ? Promise.resolve(searchGitCommits({
@@ -169243,7 +170854,7 @@ async function unifiedSearch(db, sessionId, projectPath, query, options3 = {}) {
169243
170854
  queryEmbedding
169244
170855
  })) : Promise.resolve([])
169245
170856
  ]);
169246
- const results = [...memoryResults, ...messageResults, ...gitCommitResults].sort(compareUnifiedResults).slice(0, limit);
170857
+ const results = [...memoryResults, ...messageLikeResults, ...gitCommitResults].sort(compareUnifiedResults).slice(0, limit);
169247
170858
  const countRetrievals = options3.countRetrievals ?? true;
169248
170859
  if (countRetrievals) {
169249
170860
  const memoryIds = results.filter((result) => result.source === "memory").map((result) => result.memoryId);
@@ -169284,8 +170895,8 @@ function getUserMemoryCandidates(db) {
169284
170895
  function deleteUserMemoryCandidates(db, ids) {
169285
170896
  if (ids.length === 0)
169286
170897
  return;
169287
- const placeholders = ids.map(() => "?").join(",");
169288
- db.prepare(`DELETE FROM user_memory_candidates WHERE id IN (${placeholders})`).run(...ids);
170898
+ const placeholders3 = ids.map(() => "?").join(",");
170899
+ db.prepare(`DELETE FROM user_memory_candidates WHERE id IN (${placeholders3})`).run(...ids);
169289
170900
  }
169290
170901
  function insertUserMemory(db, content, sourceCandidateIds) {
169291
170902
  const now = Date.now();
@@ -169837,26 +171448,40 @@ ${sections.join(`
169837
171448
  var DEFAULT_MEMORY_BUDGET_TOKENS = 8000;
169838
171449
  var MEMORY_BLOCK_WRAPPER_TOKENS = 6;
169839
171450
  var DEFAULT_USER_PROFILE_BUDGET_TOKENS = 4000;
171451
+ function memoryCanonicalIdentity(memory2, workspace) {
171452
+ return resolveStoredPathWorkspaceIdentity(memory2.projectPath, workspace.identities, workspace.canonicalIdentityByStoredPath);
171453
+ }
171454
+ function memorySelectionOrder(left, right) {
171455
+ if (left.status === "permanent" && right.status !== "permanent")
171456
+ return -1;
171457
+ if (right.status === "permanent" && left.status !== "permanent")
171458
+ return 1;
171459
+ const leftImportance = left.importance ?? Number.NEGATIVE_INFINITY;
171460
+ const rightImportance = right.importance ?? Number.NEGATIVE_INFINITY;
171461
+ const importanceDiff = rightImportance - leftImportance;
171462
+ if (importanceDiff !== 0)
171463
+ return importanceDiff;
171464
+ return left.id - right.id;
171465
+ }
171466
+ function memoryRenderOrder(left, right) {
171467
+ const aPriority = MEMORY_CATEGORY_ORDER_PRIORITY[left.category] ?? MEMORY_CATEGORY_ORDER_UNKNOWN;
171468
+ const bPriority = MEMORY_CATEGORY_ORDER_PRIORITY[right.category] ?? MEMORY_CATEGORY_ORDER_UNKNOWN;
171469
+ const categoryDiff = aPriority - bPriority;
171470
+ if (categoryDiff !== 0)
171471
+ return categoryDiff;
171472
+ return left.id - right.id;
171473
+ }
169840
171474
  var maxCompartmentSeqStatements = new WeakMap;
169841
171475
  var maxMemoryIdStatements = new WeakMap;
169842
171476
  var legacyCompartmentCountStatements = new WeakMap;
169843
171477
  var m0CompartmentStatements = new WeakMap;
169844
171478
  var newCompartmentStatements = new WeakMap;
169845
- function trimMemoriesToBudgetV2(sessionId, memories, budgetTokens) {
169846
- const selectionOrder = [...memories].sort((a, b) => {
169847
- if (a.status === "permanent" && b.status !== "permanent")
169848
- return -1;
169849
- if (b.status === "permanent" && a.status !== "permanent")
169850
- return 1;
169851
- const importanceDiff = (b.importance ?? 50) - (a.importance ?? 50);
169852
- if (importanceDiff !== 0)
169853
- return importanceDiff;
169854
- return a.id - b.id;
169855
- });
171479
+ function trimMemoriesToBudgetV2(sessionId, memories, budgetTokens, renderOptions = {}) {
171480
+ const selectionOrder = [...memories].sort(memorySelectionOrder);
169856
171481
  const selected = [];
169857
171482
  let usedTokens = MEMORY_BLOCK_WRAPPER_TOKENS;
169858
171483
  for (const memory2 of selectionOrder) {
169859
- const memoryTokens = estimateTokens(renderMemoryLineV2(memory2));
171484
+ const memoryTokens = estimateTokens(renderMemoryLineV2(memory2, renderOptions.sourceNameByMemoryId?.get(memory2.id)));
169860
171485
  if (usedTokens + memoryTokens > budgetTokens)
169861
171486
  continue;
169862
171487
  selected.push(memory2);
@@ -169865,16 +171490,70 @@ function trimMemoriesToBudgetV2(sessionId, memories, budgetTokens) {
169865
171490
  if (selected.length < memories.length) {
169866
171491
  sessionLog(sessionId, `v2 trimmed memories from ${memories.length} to ${selected.length} to fit injection budget of ${budgetTokens} tokens`);
169867
171492
  }
169868
- const renderOrder = [...selected].sort((a, b) => {
169869
- const aPriority = MEMORY_CATEGORY_ORDER_PRIORITY[a.category] ?? MEMORY_CATEGORY_ORDER_UNKNOWN;
169870
- const bPriority = MEMORY_CATEGORY_ORDER_PRIORITY[b.category] ?? MEMORY_CATEGORY_ORDER_UNKNOWN;
169871
- const categoryDiff = aPriority - bPriority;
169872
- if (categoryDiff !== 0)
169873
- return categoryDiff;
169874
- return a.id - b.id;
169875
- });
171493
+ const renderOrder = [...selected].sort(memoryRenderOrder);
169876
171494
  return { selected, renderOrder };
169877
171495
  }
171496
+ function trimWorkspaceMemoriesToBudgetV2(sessionId, memories, budgetTokens, workspace, renderOptions = {}) {
171497
+ if (!workspace.isWorkspaced) {
171498
+ return trimMemoriesToBudgetV2(sessionId, memories, budgetTokens, renderOptions);
171499
+ }
171500
+ const selected = [];
171501
+ const selectedIds = new Set;
171502
+ let usedTokens = MEMORY_BLOCK_WRAPPER_TOKENS;
171503
+ const tokenCost = (memory2) => estimateTokens(renderMemoryLineV2(memory2, renderOptions.sourceNameByMemoryId?.get(memory2.id)));
171504
+ const trySelect = (memory2) => {
171505
+ if (selectedIds.has(memory2.id))
171506
+ return false;
171507
+ const tokens = tokenCost(memory2);
171508
+ if (usedTokens + tokens > budgetTokens)
171509
+ return false;
171510
+ selected.push(memory2);
171511
+ selectedIds.add(memory2.id);
171512
+ usedTokens += tokens;
171513
+ return true;
171514
+ };
171515
+ for (const memory2 of memories.filter((candidate) => candidate.status === "permanent").sort(memorySelectionOrder)) {
171516
+ trySelect(memory2);
171517
+ }
171518
+ const remainingAfterPermanent = Math.max(0, budgetTokens - usedTokens);
171519
+ const floorTokens = remainingAfterPermanent / Math.max(1, workspace.identities.length);
171520
+ const byIdentity = new Map;
171521
+ for (const memory2 of memories) {
171522
+ if (memory2.status === "permanent")
171523
+ continue;
171524
+ const identity = memoryCanonicalIdentity(memory2, workspace);
171525
+ if (!identity)
171526
+ continue;
171527
+ const list = byIdentity.get(identity) ?? [];
171528
+ list.push(memory2);
171529
+ byIdentity.set(identity, list);
171530
+ }
171531
+ for (const identity of workspace.identities) {
171532
+ let memberTokens = 0;
171533
+ const candidates2 = (byIdentity.get(identity) ?? []).sort(memorySelectionOrder);
171534
+ for (const memory2 of candidates2) {
171535
+ if (selectedIds.has(memory2.id))
171536
+ continue;
171537
+ const tokens = tokenCost(memory2);
171538
+ if (memberTokens + tokens > floorTokens)
171539
+ continue;
171540
+ if (usedTokens + tokens > budgetTokens)
171541
+ continue;
171542
+ selected.push(memory2);
171543
+ selectedIds.add(memory2.id);
171544
+ usedTokens += tokens;
171545
+ memberTokens += tokens;
171546
+ }
171547
+ }
171548
+ const remaining = memories.filter((memory2) => !selectedIds.has(memory2.id)).sort(memorySelectionOrder);
171549
+ for (const memory2 of remaining) {
171550
+ trySelect(memory2);
171551
+ }
171552
+ if (selected.length < memories.length) {
171553
+ sessionLog(sessionId, `v2 trimmed memories from ${memories.length} to ${selected.length} to fit injection budget of ${budgetTokens} tokens`);
171554
+ }
171555
+ return { selected, renderOrder: [...selected].sort(memoryRenderOrder) };
171556
+ }
169878
171557
  function trimUserMemoriesToBudget(memories, budgetTokens) {
169879
171558
  const selected = [];
169880
171559
  let usedTokens = 0;
@@ -169887,15 +171566,16 @@ function trimUserMemoriesToBudget(memories, budgetTokens) {
169887
171566
  }
169888
171567
  return selected;
169889
171568
  }
169890
- function renderMemoryLineV2(memory2) {
169891
- return ` <memory id="${memory2.id}" category="${escapeXmlAttr(memory2.category)}" importance="${memory2.importance ?? 50}">${escapeXmlContent(memory2.content)}</memory>`;
171569
+ function renderMemoryLineV2(memory2, sourceName) {
171570
+ const sourceAttr = sourceName ? ` source="${escapeXmlAttr(sourceName)}"` : "";
171571
+ return ` <memory id="${memory2.id}" category="${escapeXmlAttr(memory2.category)}"${sourceAttr} importance="${memory2.importance ?? 50}">${escapeXmlContent(memory2.content)}</memory>`;
169892
171572
  }
169893
- function renderMemoryBlockV2(memories, wrapper = "project-memory") {
171573
+ function renderMemoryBlockV2(memories, wrapper = "project-memory", renderOptions = {}) {
169894
171574
  if (memories.length === 0)
169895
171575
  return "";
169896
171576
  const lines = [`<${wrapper}>`];
169897
171577
  for (const memory2 of memories) {
169898
- lines.push(renderMemoryLineV2(memory2));
171578
+ lines.push(renderMemoryLineV2(memory2, renderOptions.sourceNameByMemoryId?.get(memory2.id)));
169899
171579
  }
169900
171580
  lines.push(`</${wrapper}>`);
169901
171581
  return lines.join(`
@@ -169960,8 +171640,9 @@ function formatAge(committedAtMs) {
169960
171640
  }
169961
171641
  function formatResult(result, index) {
169962
171642
  if (result.source === "memory") {
171643
+ const source = result.sourceName ? ` source=${result.sourceName}` : "";
169963
171644
  return [
169964
- `[${index}] [memory] score=${result.score.toFixed(2)} id=${result.memoryId} category=${result.category} match=${result.matchType}`,
171645
+ `[${index}] [memory] score=${result.score.toFixed(2)} id=${result.memoryId} category=${result.category}${source} match=${result.matchType}`,
169965
171646
  result.content
169966
171647
  ].join(`
169967
171648
  `);
@@ -169971,6 +171652,13 @@ function formatResult(result, index) {
169971
171652
  `[${index}] [git_commit] score=${result.score.toFixed(2)} sha=${result.shortSha} ${formatAge(result.committedAtMs)} match=${result.matchType}`,
169972
171653
  result.content
169973
171654
  ].join(`
171655
+ `);
171656
+ }
171657
+ if (result.source === "compartment") {
171658
+ return [
171659
+ `[${index}] [message] score=${result.score.toFixed(2)} compartment_id=${result.compartmentId} range=${result.startOrdinal}-${result.endOrdinal} match=${result.matchType} title=${result.title}`,
171660
+ result.snippet ? `Snippet: ${result.snippet}` : result.content
171661
+ ].join(`
169974
171662
  `);
169975
171663
  }
169976
171664
  const expandStart = Math.max(1, result.messageOrdinal - 3);
@@ -169986,7 +171674,7 @@ function formatSearchResults(query, results) {
169986
171674
  return `No results found for "${query}" across memories, git commits, or message history.`;
169987
171675
  }
169988
171676
  const bodyParts = results.map((result, index) => formatResult(result, index + 1));
169989
- if (results.some((result) => result.source === "message")) {
171677
+ if (results.some((result) => result.source === "message" || result.source === "compartment")) {
169990
171678
  bodyParts.push("Use ctx_expand(start, end) with the range from any message result above to read the full conversation context.");
169991
171679
  }
169992
171680
  const body = bodyParts.join(`