@wolfx/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.
- package/dist/index.js +2327 -347
- package/dist/subagent-entry.js +2101 -413
- package/package.json +1 -1
package/dist/subagent-entry.js
CHANGED
|
@@ -8164,9 +8164,470 @@ function isRelativeProjectFile(projectPath, relativePath) {
|
|
|
8164
8164
|
|
|
8165
8165
|
// ../plugin/src/features/magic-context/migrations.ts
|
|
8166
8166
|
init_logger();
|
|
8167
|
-
|
|
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 =
|
|
8629
|
-
const hasCompartmentsTable =
|
|
8630
|
-
const hasMemoriesTable =
|
|
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 =
|
|
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 =
|
|
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();
|
|
@@ -143856,10 +144546,10 @@ function mergeDefs(...defs) {
|
|
|
143856
144546
|
function cloneDef(schema) {
|
|
143857
144547
|
return mergeDefs(schema._zod.def);
|
|
143858
144548
|
}
|
|
143859
|
-
function getElementAtPath(obj,
|
|
143860
|
-
if (!
|
|
144549
|
+
function getElementAtPath(obj, path4) {
|
|
144550
|
+
if (!path4)
|
|
143861
144551
|
return obj;
|
|
143862
|
-
return
|
|
144552
|
+
return path4.reduce((acc, key) => acc?.[key], obj);
|
|
143863
144553
|
}
|
|
143864
144554
|
function promiseAllObject(promisesObj) {
|
|
143865
144555
|
const keys = Object.keys(promisesObj);
|
|
@@ -144267,11 +144957,11 @@ function explicitlyAborted(x, startIndex = 0) {
|
|
|
144267
144957
|
}
|
|
144268
144958
|
return false;
|
|
144269
144959
|
}
|
|
144270
|
-
function prefixIssues(
|
|
144960
|
+
function prefixIssues(path4, issues) {
|
|
144271
144961
|
return issues.map((iss) => {
|
|
144272
144962
|
var _a2;
|
|
144273
144963
|
(_a2 = iss).path ?? (_a2.path = []);
|
|
144274
|
-
iss.path.unshift(
|
|
144964
|
+
iss.path.unshift(path4);
|
|
144275
144965
|
return iss;
|
|
144276
144966
|
});
|
|
144277
144967
|
}
|
|
@@ -144418,16 +145108,16 @@ function flattenError(error, mapper = (issue2) => issue2.message) {
|
|
|
144418
145108
|
}
|
|
144419
145109
|
function formatError(error, mapper = (issue2) => issue2.message) {
|
|
144420
145110
|
const fieldErrors = { _errors: [] };
|
|
144421
|
-
const processError = (error2,
|
|
145111
|
+
const processError = (error2, path4 = []) => {
|
|
144422
145112
|
for (const issue2 of error2.issues) {
|
|
144423
145113
|
if (issue2.code === "invalid_union" && issue2.errors.length) {
|
|
144424
|
-
issue2.errors.map((issues) => processError({ issues }, [...
|
|
145114
|
+
issue2.errors.map((issues) => processError({ issues }, [...path4, ...issue2.path]));
|
|
144425
145115
|
} else if (issue2.code === "invalid_key") {
|
|
144426
|
-
processError({ issues: issue2.issues }, [...
|
|
145116
|
+
processError({ issues: issue2.issues }, [...path4, ...issue2.path]);
|
|
144427
145117
|
} else if (issue2.code === "invalid_element") {
|
|
144428
|
-
processError({ issues: issue2.issues }, [...
|
|
145118
|
+
processError({ issues: issue2.issues }, [...path4, ...issue2.path]);
|
|
144429
145119
|
} else {
|
|
144430
|
-
const fullpath = [...
|
|
145120
|
+
const fullpath = [...path4, ...issue2.path];
|
|
144431
145121
|
if (fullpath.length === 0) {
|
|
144432
145122
|
fieldErrors._errors.push(mapper(issue2));
|
|
144433
145123
|
} else {
|
|
@@ -144454,17 +145144,17 @@ function formatError(error, mapper = (issue2) => issue2.message) {
|
|
|
144454
145144
|
}
|
|
144455
145145
|
function treeifyError(error, mapper = (issue2) => issue2.message) {
|
|
144456
145146
|
const result = { errors: [] };
|
|
144457
|
-
const processError = (error2,
|
|
145147
|
+
const processError = (error2, path4 = []) => {
|
|
144458
145148
|
var _a2, _b;
|
|
144459
145149
|
for (const issue2 of error2.issues) {
|
|
144460
145150
|
if (issue2.code === "invalid_union" && issue2.errors.length) {
|
|
144461
|
-
issue2.errors.map((issues) => processError({ issues }, [...
|
|
145151
|
+
issue2.errors.map((issues) => processError({ issues }, [...path4, ...issue2.path]));
|
|
144462
145152
|
} else if (issue2.code === "invalid_key") {
|
|
144463
|
-
processError({ issues: issue2.issues }, [...
|
|
145153
|
+
processError({ issues: issue2.issues }, [...path4, ...issue2.path]);
|
|
144464
145154
|
} else if (issue2.code === "invalid_element") {
|
|
144465
|
-
processError({ issues: issue2.issues }, [...
|
|
145155
|
+
processError({ issues: issue2.issues }, [...path4, ...issue2.path]);
|
|
144466
145156
|
} else {
|
|
144467
|
-
const fullpath = [...
|
|
145157
|
+
const fullpath = [...path4, ...issue2.path];
|
|
144468
145158
|
if (fullpath.length === 0) {
|
|
144469
145159
|
result.errors.push(mapper(issue2));
|
|
144470
145160
|
continue;
|
|
@@ -144496,8 +145186,8 @@ function treeifyError(error, mapper = (issue2) => issue2.message) {
|
|
|
144496
145186
|
}
|
|
144497
145187
|
function toDotPath(_path) {
|
|
144498
145188
|
const segs = [];
|
|
144499
|
-
const
|
|
144500
|
-
for (const seg of
|
|
145189
|
+
const path4 = _path.map((seg) => typeof seg === "object" ? seg.key : seg);
|
|
145190
|
+
for (const seg of path4) {
|
|
144501
145191
|
if (typeof seg === "number")
|
|
144502
145192
|
segs.push(`[${seg}]`);
|
|
144503
145193
|
else if (typeof seg === "symbol")
|
|
@@ -156956,13 +157646,13 @@ function resolveRef(ref, ctx) {
|
|
|
156956
157646
|
if (!ref.startsWith("#")) {
|
|
156957
157647
|
throw new Error("External $ref is not supported, only local refs (#/...) are allowed");
|
|
156958
157648
|
}
|
|
156959
|
-
const
|
|
156960
|
-
if (
|
|
157649
|
+
const path4 = ref.slice(1).split("/").filter(Boolean);
|
|
157650
|
+
if (path4.length === 0) {
|
|
156961
157651
|
return ctx.rootSchema;
|
|
156962
157652
|
}
|
|
156963
157653
|
const defsKey = ctx.version === "draft-2020-12" ? "$defs" : "definitions";
|
|
156964
|
-
if (
|
|
156965
|
-
const key =
|
|
157654
|
+
if (path4[0] === defsKey) {
|
|
157655
|
+
const key = path4[1];
|
|
156966
157656
|
if (!key || !ctx.defs[key]) {
|
|
156967
157657
|
throw new Error(`Reference not found: ${ref}`);
|
|
156968
157658
|
}
|
|
@@ -157450,7 +158140,8 @@ var BaseEmbeddingConfigSchema = exports_external.object({
|
|
|
157450
158140
|
endpoint: exports_external.string().optional().describe("API endpoint URL. Required when provider is openai-compatible."),
|
|
157451
158141
|
api_key: exports_external.string().optional().describe("API key for remote embedding provider (optional)"),
|
|
157452
158142
|
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."),
|
|
157453
|
-
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.")
|
|
158143
|
+
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."),
|
|
158144
|
+
max_input_tokens: exports_external.number().int().positive().optional().describe("Optional maximum input tokens for chunk embeddings. Defaults conservatively to 512 when omitted.")
|
|
157454
158145
|
}).superRefine((data, ctx) => {
|
|
157455
158146
|
if (data.provider === "openai-compatible" && !data.endpoint?.trim()) {
|
|
157456
158147
|
ctx.addIssue({
|
|
@@ -157471,7 +158162,8 @@ var EmbeddingConfigSchema = BaseEmbeddingConfigSchema.transform((data) => {
|
|
|
157471
158162
|
if (data.provider === "local") {
|
|
157472
158163
|
return {
|
|
157473
158164
|
provider: "local",
|
|
157474
|
-
model: data.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL
|
|
158165
|
+
model: data.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
158166
|
+
...data.max_input_tokens ? { max_input_tokens: data.max_input_tokens } : {}
|
|
157475
158167
|
};
|
|
157476
158168
|
}
|
|
157477
158169
|
if (data.provider === "openai-compatible") {
|
|
@@ -157484,7 +158176,8 @@ var EmbeddingConfigSchema = BaseEmbeddingConfigSchema.transform((data) => {
|
|
|
157484
158176
|
endpoint: data.endpoint?.trim() ?? "",
|
|
157485
158177
|
...apiKey ? { api_key: apiKey } : {},
|
|
157486
158178
|
...inputType ? { input_type: inputType } : {},
|
|
157487
|
-
...truncate ? { truncate } : {}
|
|
158179
|
+
...truncate ? { truncate } : {},
|
|
158180
|
+
...data.max_input_tokens ? { max_input_tokens: data.max_input_tokens } : {}
|
|
157488
158181
|
};
|
|
157489
158182
|
}
|
|
157490
158183
|
return { provider: "off" };
|
|
@@ -157733,31 +158426,31 @@ function getUserConfigPaths() {
|
|
|
157733
158426
|
return [`${basePath}.jsonc`, `${basePath}.json`];
|
|
157734
158427
|
}
|
|
157735
158428
|
function resolveFirstExisting(paths) {
|
|
157736
|
-
return paths.find((
|
|
158429
|
+
return paths.find((path4) => existsSync5(path4));
|
|
157737
158430
|
}
|
|
157738
|
-
function loadConfigFile(
|
|
158431
|
+
function loadConfigFile(path4, scope) {
|
|
157739
158432
|
try {
|
|
157740
|
-
const rawText = readFileSync3(
|
|
158433
|
+
const rawText = readFileSync3(path4, "utf-8");
|
|
157741
158434
|
const substituted = substituteConfigVariables({
|
|
157742
158435
|
text: rawText,
|
|
157743
|
-
configPath:
|
|
158436
|
+
configPath: path4,
|
|
157744
158437
|
isProjectConfig: scope === "project"
|
|
157745
158438
|
});
|
|
157746
158439
|
return {
|
|
157747
|
-
path:
|
|
158440
|
+
path: path4,
|
|
157748
158441
|
scope,
|
|
157749
158442
|
config: import_comment_json.parse(substituted.text),
|
|
157750
|
-
warnings: substituted.warnings.map((warning) => `${
|
|
158443
|
+
warnings: substituted.warnings.map((warning) => `${path4}: ${warning}`),
|
|
157751
158444
|
loadOutcome: substituted.warnings.length > 0 ? "substitution-failure" : "ok"
|
|
157752
158445
|
};
|
|
157753
158446
|
} catch (error51) {
|
|
157754
158447
|
const message = error51 instanceof Error ? error51.message : String(error51);
|
|
157755
158448
|
return {
|
|
157756
|
-
path:
|
|
158449
|
+
path: path4,
|
|
157757
158450
|
scope,
|
|
157758
158451
|
config: {},
|
|
157759
158452
|
warnings: [
|
|
157760
|
-
`${
|
|
158453
|
+
`${path4}: failed to load config: ${message}; using defaults for this file.`
|
|
157761
158454
|
],
|
|
157762
158455
|
loadOutcome: typeof error51.code === "string" ? "project-file-io-error" : "project-file-parse-error"
|
|
157763
158456
|
};
|
|
@@ -157923,9 +158616,9 @@ function bindSubstitutionFailures(loaded) {
|
|
|
157923
158616
|
}
|
|
157924
158617
|
const emptyPaths = collectEmptyStringPaths(loaded.config);
|
|
157925
158618
|
return loaded.warnings.map((message) => {
|
|
157926
|
-
const matchedPath = emptyPaths.find((
|
|
157927
|
-
const tail =
|
|
157928
|
-
return message.includes(
|
|
158619
|
+
const matchedPath = emptyPaths.find((path4) => {
|
|
158620
|
+
const tail = path4.split(".").at(-1) ?? path4;
|
|
158621
|
+
return message.includes(path4) || message.toLowerCase().includes(tail.toLowerCase());
|
|
157929
158622
|
});
|
|
157930
158623
|
return {
|
|
157931
158624
|
keyPath: matchedPath ?? "<unknown>",
|
|
@@ -158013,6 +158706,443 @@ function loadPiConfigDetailed(opts = {}) {
|
|
|
158013
158706
|
// ../plugin/src/features/magic-context/memory/embedding.ts
|
|
158014
158707
|
init_logger();
|
|
158015
158708
|
|
|
158709
|
+
// ../plugin/src/features/magic-context/compartment-chunk-embedding.ts
|
|
158710
|
+
import { createHash as createHash4 } from "node:crypto";
|
|
158711
|
+
var DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS = 512;
|
|
158712
|
+
var CHUNK_WINDOW_SAFETY_RATIO = 0.9;
|
|
158713
|
+
var loadFtsRowsStatements = new WeakMap;
|
|
158714
|
+
var existingHashStatements = new WeakMap;
|
|
158715
|
+
var existingHashByProjectStatements = new WeakMap;
|
|
158716
|
+
var deleteByCompartmentStatements = new WeakMap;
|
|
158717
|
+
var insertEmbeddingStatements = new WeakMap;
|
|
158718
|
+
var distinctModelStatements = new WeakMap;
|
|
158719
|
+
var clearProjectStatements = new WeakMap;
|
|
158720
|
+
var clearProjectModelStatements = new WeakMap;
|
|
158721
|
+
var searchRowsStatements = new WeakMap;
|
|
158722
|
+
var searchRowsByModelStatements = new WeakMap;
|
|
158723
|
+
var backfillCandidateStatements = new WeakMap;
|
|
158724
|
+
function getLoadFtsRowsStatement(db) {
|
|
158725
|
+
let stmt = loadFtsRowsStatements.get(db);
|
|
158726
|
+
if (!stmt) {
|
|
158727
|
+
stmt = db.prepare(`SELECT message_ordinal AS messageOrdinal, role, content
|
|
158728
|
+
FROM message_history_fts
|
|
158729
|
+
WHERE session_id = ?
|
|
158730
|
+
AND message_ordinal >= ?
|
|
158731
|
+
AND message_ordinal <= ?
|
|
158732
|
+
AND role IN ('user', 'assistant')
|
|
158733
|
+
ORDER BY message_ordinal ASC`);
|
|
158734
|
+
loadFtsRowsStatements.set(db, stmt);
|
|
158735
|
+
}
|
|
158736
|
+
return stmt;
|
|
158737
|
+
}
|
|
158738
|
+
function getExistingHashStatement(db, scopedToProject) {
|
|
158739
|
+
const map2 = scopedToProject ? existingHashByProjectStatements : existingHashStatements;
|
|
158740
|
+
let stmt = map2.get(db);
|
|
158741
|
+
if (!stmt) {
|
|
158742
|
+
stmt = db.prepare(`SELECT window_index AS windowIndex, chunk_hash AS chunkHash
|
|
158743
|
+
FROM compartment_chunk_embeddings
|
|
158744
|
+
WHERE compartment_id = ?
|
|
158745
|
+
AND model_id = ?
|
|
158746
|
+
${scopedToProject ? "AND project_path = ?" : ""}
|
|
158747
|
+
ORDER BY window_index ASC`);
|
|
158748
|
+
map2.set(db, stmt);
|
|
158749
|
+
}
|
|
158750
|
+
return stmt;
|
|
158751
|
+
}
|
|
158752
|
+
function getDeleteByCompartmentStatement(db) {
|
|
158753
|
+
let stmt = deleteByCompartmentStatements.get(db);
|
|
158754
|
+
if (!stmt) {
|
|
158755
|
+
stmt = db.prepare("DELETE FROM compartment_chunk_embeddings WHERE compartment_id = ?");
|
|
158756
|
+
deleteByCompartmentStatements.set(db, stmt);
|
|
158757
|
+
}
|
|
158758
|
+
return stmt;
|
|
158759
|
+
}
|
|
158760
|
+
function getInsertEmbeddingStatement(db) {
|
|
158761
|
+
let stmt = insertEmbeddingStatements.get(db);
|
|
158762
|
+
if (!stmt) {
|
|
158763
|
+
stmt = db.prepare(`INSERT INTO compartment_chunk_embeddings (
|
|
158764
|
+
compartment_id, session_id, project_path, harness, window_index,
|
|
158765
|
+
start_ordinal, end_ordinal, chunk_hash, model_id, dims, vector, created_at
|
|
158766
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
158767
|
+
insertEmbeddingStatements.set(db, stmt);
|
|
158768
|
+
}
|
|
158769
|
+
return stmt;
|
|
158770
|
+
}
|
|
158771
|
+
function getDistinctModelStatement(db) {
|
|
158772
|
+
let stmt = distinctModelStatements.get(db);
|
|
158773
|
+
if (!stmt) {
|
|
158774
|
+
stmt = db.prepare(`SELECT DISTINCT model_id AS modelId
|
|
158775
|
+
FROM compartment_chunk_embeddings
|
|
158776
|
+
WHERE project_path = ?`);
|
|
158777
|
+
distinctModelStatements.set(db, stmt);
|
|
158778
|
+
}
|
|
158779
|
+
return stmt;
|
|
158780
|
+
}
|
|
158781
|
+
function getClearProjectStatement(db) {
|
|
158782
|
+
let stmt = clearProjectStatements.get(db);
|
|
158783
|
+
if (!stmt) {
|
|
158784
|
+
stmt = db.prepare("DELETE FROM compartment_chunk_embeddings WHERE project_path = ?");
|
|
158785
|
+
clearProjectStatements.set(db, stmt);
|
|
158786
|
+
}
|
|
158787
|
+
return stmt;
|
|
158788
|
+
}
|
|
158789
|
+
function getClearProjectModelStatement(db) {
|
|
158790
|
+
let stmt = clearProjectModelStatements.get(db);
|
|
158791
|
+
if (!stmt) {
|
|
158792
|
+
stmt = db.prepare("DELETE FROM compartment_chunk_embeddings WHERE project_path = ? AND model_id = ?");
|
|
158793
|
+
clearProjectModelStatements.set(db, stmt);
|
|
158794
|
+
}
|
|
158795
|
+
return stmt;
|
|
158796
|
+
}
|
|
158797
|
+
function getSearchRowsStatement(db, withModel) {
|
|
158798
|
+
const map2 = withModel ? searchRowsByModelStatements : searchRowsStatements;
|
|
158799
|
+
let stmt = map2.get(db);
|
|
158800
|
+
if (!stmt) {
|
|
158801
|
+
stmt = db.prepare(`SELECT e.compartment_id AS compartmentId,
|
|
158802
|
+
e.session_id AS sessionId,
|
|
158803
|
+
c.title AS title,
|
|
158804
|
+
c.start_message AS compartmentStart,
|
|
158805
|
+
c.end_message AS compartmentEnd,
|
|
158806
|
+
e.window_index AS windowIndex,
|
|
158807
|
+
e.start_ordinal AS windowStart,
|
|
158808
|
+
e.end_ordinal AS windowEnd,
|
|
158809
|
+
e.chunk_hash AS chunkHash,
|
|
158810
|
+
e.model_id AS modelId,
|
|
158811
|
+
e.dims AS dims,
|
|
158812
|
+
e.vector AS vector
|
|
158813
|
+
FROM compartment_chunk_embeddings e
|
|
158814
|
+
JOIN compartments c ON c.id = e.compartment_id
|
|
158815
|
+
WHERE e.session_id = ?
|
|
158816
|
+
AND e.project_path = ?
|
|
158817
|
+
${withModel ? "AND e.model_id = ?" : ""}
|
|
158818
|
+
ORDER BY e.compartment_id ASC, e.window_index ASC`);
|
|
158819
|
+
map2.set(db, stmt);
|
|
158820
|
+
}
|
|
158821
|
+
return stmt;
|
|
158822
|
+
}
|
|
158823
|
+
function isFinitePositiveInteger(value) {
|
|
158824
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
158825
|
+
}
|
|
158826
|
+
function normalizeCompartmentChunkMaxInputTokens(value) {
|
|
158827
|
+
if (!isFinitePositiveInteger(value)) {
|
|
158828
|
+
return DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS;
|
|
158829
|
+
}
|
|
158830
|
+
return Math.max(1, Math.floor(value));
|
|
158831
|
+
}
|
|
158832
|
+
function normalizeContent(text) {
|
|
158833
|
+
return text.replace(/\s+/g, " ").trim();
|
|
158834
|
+
}
|
|
158835
|
+
function formatOrdinalRange(start, end) {
|
|
158836
|
+
return start === end ? `[${start}]` : `[${start}-${end}]`;
|
|
158837
|
+
}
|
|
158838
|
+
function rolePrefix(role) {
|
|
158839
|
+
if (role === "user")
|
|
158840
|
+
return "U";
|
|
158841
|
+
if (role === "assistant")
|
|
158842
|
+
return "A";
|
|
158843
|
+
return null;
|
|
158844
|
+
}
|
|
158845
|
+
function parseOrdinal(value) {
|
|
158846
|
+
const parsed = typeof value === "number" ? value : Number.parseInt(String(value ?? ""), 10);
|
|
158847
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
|
|
158848
|
+
}
|
|
158849
|
+
function parseCanonicalLineRange(line) {
|
|
158850
|
+
const match = /^\[(\d+)(?:-(\d+))?\]\s+[UA]:/.exec(line.trim());
|
|
158851
|
+
if (!match)
|
|
158852
|
+
return null;
|
|
158853
|
+
const start = Number.parseInt(match[1], 10);
|
|
158854
|
+
const end = match[2] ? Number.parseInt(match[2], 10) : start;
|
|
158855
|
+
if (!Number.isFinite(start) || !Number.isFinite(end))
|
|
158856
|
+
return null;
|
|
158857
|
+
return { start, end };
|
|
158858
|
+
}
|
|
158859
|
+
function hashChunkText(text) {
|
|
158860
|
+
return createHash4("sha256").update(text).digest("hex");
|
|
158861
|
+
}
|
|
158862
|
+
function vectorBlob(vector) {
|
|
158863
|
+
return new Uint8Array(vector.buffer, vector.byteOffset, vector.byteLength);
|
|
158864
|
+
}
|
|
158865
|
+
function toFloat32Array(blob) {
|
|
158866
|
+
if (blob instanceof Uint8Array) {
|
|
158867
|
+
const buffer2 = blob.buffer.slice(blob.byteOffset, blob.byteOffset + blob.byteLength);
|
|
158868
|
+
return new Float32Array(buffer2);
|
|
158869
|
+
}
|
|
158870
|
+
return new Float32Array(blob.slice(0));
|
|
158871
|
+
}
|
|
158872
|
+
function buildCanonicalChunkTextFromFts(db, sessionId, startOrdinal, endOrdinal) {
|
|
158873
|
+
if (endOrdinal < startOrdinal)
|
|
158874
|
+
return "";
|
|
158875
|
+
const rows = getLoadFtsRowsStatement(db).all(sessionId, startOrdinal, endOrdinal).map((row) => row);
|
|
158876
|
+
const lines = [];
|
|
158877
|
+
let current = null;
|
|
158878
|
+
const flush2 = () => {
|
|
158879
|
+
if (!current || current.parts.length === 0)
|
|
158880
|
+
return;
|
|
158881
|
+
lines.push(`${formatOrdinalRange(current.start, current.end)} ${current.role}: ${current.parts.join(" / ")}`);
|
|
158882
|
+
current = null;
|
|
158883
|
+
};
|
|
158884
|
+
for (const row of rows) {
|
|
158885
|
+
const ordinal = parseOrdinal(row.messageOrdinal);
|
|
158886
|
+
const prefix = rolePrefix(row.role);
|
|
158887
|
+
const content = typeof row.content === "string" ? normalizeContent(row.content) : "";
|
|
158888
|
+
if (ordinal === null || prefix === null || content.length === 0)
|
|
158889
|
+
continue;
|
|
158890
|
+
if (current && current.role === prefix) {
|
|
158891
|
+
current.end = ordinal;
|
|
158892
|
+
current.parts.push(content);
|
|
158893
|
+
continue;
|
|
158894
|
+
}
|
|
158895
|
+
flush2();
|
|
158896
|
+
current = { role: prefix, start: ordinal, end: ordinal, parts: [content] };
|
|
158897
|
+
}
|
|
158898
|
+
flush2();
|
|
158899
|
+
return lines.join(`
|
|
158900
|
+
`);
|
|
158901
|
+
}
|
|
158902
|
+
function canonicalizeInMemoryChunkTextForEmbedding(chunkText, startOrdinal, endOrdinal) {
|
|
158903
|
+
const lines = [];
|
|
158904
|
+
for (const rawLine of chunkText.split(/\r?\n/)) {
|
|
158905
|
+
const line = rawLine.trim();
|
|
158906
|
+
const match = /^(\[(\d+)(?:-(\d+))?\]\s+[UA]:)\s*(.*)$/.exec(line);
|
|
158907
|
+
if (!match)
|
|
158908
|
+
continue;
|
|
158909
|
+
const lineStart = Number.parseInt(match[2], 10);
|
|
158910
|
+
const lineEnd = match[3] ? Number.parseInt(match[3], 10) : lineStart;
|
|
158911
|
+
if (startOrdinal != null && lineEnd < startOrdinal)
|
|
158912
|
+
continue;
|
|
158913
|
+
if (endOrdinal != null && lineStart > endOrdinal)
|
|
158914
|
+
continue;
|
|
158915
|
+
const rawParts = match[4].split(" / ").map((part) => normalizeContent(part)).filter((part) => part.length > 0);
|
|
158916
|
+
const ordinalSpan = lineEnd - lineStart + 1;
|
|
158917
|
+
const roleLabel = match[1].slice(match[1].indexOf("]") + 2);
|
|
158918
|
+
if (ordinalSpan === rawParts.length) {
|
|
158919
|
+
const retained = rawParts.map((part, index) => ({ ordinal: lineStart + index, part })).filter(({ ordinal, part }) => {
|
|
158920
|
+
if (part.startsWith("TC:"))
|
|
158921
|
+
return false;
|
|
158922
|
+
if (startOrdinal != null && ordinal < startOrdinal)
|
|
158923
|
+
return false;
|
|
158924
|
+
if (endOrdinal != null && ordinal > endOrdinal)
|
|
158925
|
+
return false;
|
|
158926
|
+
return true;
|
|
158927
|
+
});
|
|
158928
|
+
if (retained.length === 0)
|
|
158929
|
+
continue;
|
|
158930
|
+
const retainedStart = retained[0].ordinal;
|
|
158931
|
+
const retainedEnd = retained[retained.length - 1].ordinal;
|
|
158932
|
+
lines.push(`${formatOrdinalRange(retainedStart, retainedEnd)} ${roleLabel} ${retained.map(({ part }) => part).join(" / ")}`);
|
|
158933
|
+
continue;
|
|
158934
|
+
}
|
|
158935
|
+
const parts = rawParts.filter((part) => !part.startsWith("TC:"));
|
|
158936
|
+
if (parts.length === 0)
|
|
158937
|
+
continue;
|
|
158938
|
+
lines.push(`${match[1]} ${parts.join(" / ")}`);
|
|
158939
|
+
}
|
|
158940
|
+
return lines.join(`
|
|
158941
|
+
`);
|
|
158942
|
+
}
|
|
158943
|
+
function chunkCanonicalText(canonicalText, startOrdinal, endOrdinal, maxInputTokens) {
|
|
158944
|
+
const lines = canonicalText.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
|
|
158945
|
+
if (lines.length === 0 || endOrdinal < startOrdinal)
|
|
158946
|
+
return [];
|
|
158947
|
+
const normalizedMax = normalizeCompartmentChunkMaxInputTokens(maxInputTokens);
|
|
158948
|
+
const effectiveMax = Math.max(1, Math.floor(normalizedMax * CHUNK_WINDOW_SAFETY_RATIO));
|
|
158949
|
+
const fullText = lines.join(`
|
|
158950
|
+
`);
|
|
158951
|
+
if (estimateTokens(fullText) <= effectiveMax) {
|
|
158952
|
+
return [
|
|
158953
|
+
{
|
|
158954
|
+
windowIndex: 0,
|
|
158955
|
+
startOrdinal,
|
|
158956
|
+
endOrdinal,
|
|
158957
|
+
text: fullText,
|
|
158958
|
+
chunkHash: hashChunkText(fullText)
|
|
158959
|
+
}
|
|
158960
|
+
];
|
|
158961
|
+
}
|
|
158962
|
+
const windows = [];
|
|
158963
|
+
let currentLines = [];
|
|
158964
|
+
let currentStart = null;
|
|
158965
|
+
let currentEnd = null;
|
|
158966
|
+
let currentTokens = 0;
|
|
158967
|
+
const flush2 = () => {
|
|
158968
|
+
if (currentLines.length === 0 || currentStart === null || currentEnd === null)
|
|
158969
|
+
return;
|
|
158970
|
+
const text = currentLines.join(`
|
|
158971
|
+
`);
|
|
158972
|
+
windows.push({
|
|
158973
|
+
windowIndex: windows.length + 1,
|
|
158974
|
+
startOrdinal: currentStart,
|
|
158975
|
+
endOrdinal: currentEnd,
|
|
158976
|
+
text,
|
|
158977
|
+
chunkHash: hashChunkText(text)
|
|
158978
|
+
});
|
|
158979
|
+
currentLines = [];
|
|
158980
|
+
currentStart = null;
|
|
158981
|
+
currentEnd = null;
|
|
158982
|
+
currentTokens = 0;
|
|
158983
|
+
};
|
|
158984
|
+
for (const line of lines) {
|
|
158985
|
+
const range = parseCanonicalLineRange(line);
|
|
158986
|
+
const lineStart = range?.start ?? startOrdinal;
|
|
158987
|
+
const lineEnd = range?.end ?? lineStart;
|
|
158988
|
+
const lineTokens = estimateTokens(line);
|
|
158989
|
+
if (currentLines.length > 0 && currentTokens + lineTokens > effectiveMax) {
|
|
158990
|
+
flush2();
|
|
158991
|
+
}
|
|
158992
|
+
if (currentLines.length === 0) {
|
|
158993
|
+
currentStart = lineStart;
|
|
158994
|
+
}
|
|
158995
|
+
currentLines.push(line);
|
|
158996
|
+
currentEnd = lineEnd;
|
|
158997
|
+
currentTokens += lineTokens;
|
|
158998
|
+
}
|
|
158999
|
+
flush2();
|
|
159000
|
+
return windows;
|
|
159001
|
+
}
|
|
159002
|
+
function getExistingChunkHashes(db, compartmentId, modelId, projectPath) {
|
|
159003
|
+
const scoped = typeof projectPath === "string" && projectPath.length > 0;
|
|
159004
|
+
const rows = scoped ? getExistingHashStatement(db, true).all(compartmentId, modelId, projectPath) : getExistingHashStatement(db, false).all(compartmentId, modelId);
|
|
159005
|
+
return new Map(rows.filter((row) => typeof row.windowIndex === "number" && typeof row.chunkHash === "string").map((row) => [row.windowIndex, row.chunkHash]));
|
|
159006
|
+
}
|
|
159007
|
+
function chunkEmbeddingWindowsAreCurrent(db, compartmentId, modelId, windows, projectPath) {
|
|
159008
|
+
const existing = getExistingChunkHashes(db, compartmentId, modelId, projectPath);
|
|
159009
|
+
if (existing.size !== windows.length)
|
|
159010
|
+
return false;
|
|
159011
|
+
return windows.every((window) => existing.get(window.windowIndex) === window.chunkHash);
|
|
159012
|
+
}
|
|
159013
|
+
function replaceCompartmentChunkEmbeddings(db, rows) {
|
|
159014
|
+
if (rows.length === 0)
|
|
159015
|
+
return;
|
|
159016
|
+
const compartmentId = rows[0].compartmentId;
|
|
159017
|
+
const now = Date.now();
|
|
159018
|
+
db.transaction(() => {
|
|
159019
|
+
getDeleteByCompartmentStatement(db).run(compartmentId);
|
|
159020
|
+
const insert = getInsertEmbeddingStatement(db);
|
|
159021
|
+
for (const row of rows) {
|
|
159022
|
+
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);
|
|
159023
|
+
}
|
|
159024
|
+
})();
|
|
159025
|
+
}
|
|
159026
|
+
function getDistinctChunkEmbeddingModelIds(db, projectPath) {
|
|
159027
|
+
const rows = getDistinctModelStatement(db).all(projectPath);
|
|
159028
|
+
return new Set(rows.map((row) => typeof row.modelId === "string" ? row.modelId : null));
|
|
159029
|
+
}
|
|
159030
|
+
function clearChunkEmbeddingsForProject(db, projectPath, modelId) {
|
|
159031
|
+
if (modelId) {
|
|
159032
|
+
return getClearProjectModelStatement(db).run(projectPath, modelId).changes;
|
|
159033
|
+
}
|
|
159034
|
+
return getClearProjectStatement(db).run(projectPath).changes;
|
|
159035
|
+
}
|
|
159036
|
+
function loadCompartmentChunkEmbeddingsForSearch(db, sessionId, projectPath, modelId) {
|
|
159037
|
+
const rows = modelId ? getSearchRowsStatement(db, true).all(sessionId, projectPath, modelId) : getSearchRowsStatement(db, false).all(sessionId, projectPath);
|
|
159038
|
+
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) => ({
|
|
159039
|
+
compartmentId: row.compartmentId,
|
|
159040
|
+
sessionId: row.sessionId,
|
|
159041
|
+
title: row.title,
|
|
159042
|
+
startOrdinal: row.compartmentStart,
|
|
159043
|
+
endOrdinal: row.compartmentEnd,
|
|
159044
|
+
windowIndex: row.windowIndex,
|
|
159045
|
+
windowStartOrdinal: row.windowStart,
|
|
159046
|
+
windowEndOrdinal: row.windowEnd,
|
|
159047
|
+
chunkHash: row.chunkHash,
|
|
159048
|
+
modelId: row.modelId,
|
|
159049
|
+
dims: row.dims,
|
|
159050
|
+
vector: toFloat32Array(row.vector)
|
|
159051
|
+
}));
|
|
159052
|
+
}
|
|
159053
|
+
function mapBackfillCandidateRows(rows) {
|
|
159054
|
+
return rows.filter((row) => {
|
|
159055
|
+
if (row === null || typeof row !== "object")
|
|
159056
|
+
return false;
|
|
159057
|
+
const candidate = row;
|
|
159058
|
+
return typeof candidate.id === "number" && typeof candidate.sessionId === "string" && typeof candidate.startMessage === "number" && typeof candidate.endMessage === "number" && typeof candidate.title === "string";
|
|
159059
|
+
}).map((row) => ({
|
|
159060
|
+
id: row.id,
|
|
159061
|
+
sessionId: row.sessionId,
|
|
159062
|
+
startMessage: row.startMessage,
|
|
159063
|
+
endMessage: row.endMessage,
|
|
159064
|
+
title: row.title
|
|
159065
|
+
}));
|
|
159066
|
+
}
|
|
159067
|
+
var sessionBackfillCandidateStatements = new WeakMap;
|
|
159068
|
+
function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, limit, excludeIds) {
|
|
159069
|
+
if (excludeIds && excludeIds.length > 0) {
|
|
159070
|
+
const placeholders2 = excludeIds.map(() => "?").join(", ");
|
|
159071
|
+
const stmt2 = db.prepare(`SELECT c.id AS id,
|
|
159072
|
+
c.session_id AS sessionId,
|
|
159073
|
+
c.start_message AS startMessage,
|
|
159074
|
+
c.end_message AS endMessage,
|
|
159075
|
+
c.title AS title
|
|
159076
|
+
FROM compartments c
|
|
159077
|
+
JOIN session_projects sp
|
|
159078
|
+
ON sp.session_id = c.session_id
|
|
159079
|
+
AND sp.harness = c.harness
|
|
159080
|
+
AND sp.project_path = ?
|
|
159081
|
+
WHERE c.session_id = ?
|
|
159082
|
+
AND c.start_message IS NOT NULL
|
|
159083
|
+
AND c.end_message IS NOT NULL
|
|
159084
|
+
AND c.id NOT IN (${placeholders2})
|
|
159085
|
+
AND NOT EXISTS (
|
|
159086
|
+
SELECT 1
|
|
159087
|
+
FROM compartment_chunk_embeddings current
|
|
159088
|
+
WHERE current.compartment_id = c.id
|
|
159089
|
+
AND current.project_path = ?
|
|
159090
|
+
AND current.model_id = ?
|
|
159091
|
+
)
|
|
159092
|
+
ORDER BY c.start_message ASC, c.id ASC
|
|
159093
|
+
LIMIT ?`);
|
|
159094
|
+
const rows2 = stmt2.all(projectPath, sessionId, ...excludeIds, projectPath, modelId, Math.max(1, limit));
|
|
159095
|
+
return mapBackfillCandidateRows(rows2);
|
|
159096
|
+
}
|
|
159097
|
+
let stmt = sessionBackfillCandidateStatements.get(db);
|
|
159098
|
+
if (!stmt) {
|
|
159099
|
+
stmt = db.prepare(`SELECT c.id AS id,
|
|
159100
|
+
c.session_id AS sessionId,
|
|
159101
|
+
c.start_message AS startMessage,
|
|
159102
|
+
c.end_message AS endMessage,
|
|
159103
|
+
c.title AS title
|
|
159104
|
+
FROM compartments c
|
|
159105
|
+
JOIN session_projects sp
|
|
159106
|
+
ON sp.session_id = c.session_id
|
|
159107
|
+
AND sp.harness = c.harness
|
|
159108
|
+
AND sp.project_path = ?
|
|
159109
|
+
WHERE c.session_id = ?
|
|
159110
|
+
AND c.start_message IS NOT NULL
|
|
159111
|
+
AND c.end_message IS NOT NULL
|
|
159112
|
+
AND NOT EXISTS (
|
|
159113
|
+
SELECT 1
|
|
159114
|
+
FROM compartment_chunk_embeddings current
|
|
159115
|
+
WHERE current.compartment_id = c.id
|
|
159116
|
+
AND current.project_path = ?
|
|
159117
|
+
AND current.model_id = ?
|
|
159118
|
+
)
|
|
159119
|
+
ORDER BY c.start_message ASC, c.id ASC
|
|
159120
|
+
LIMIT ?`);
|
|
159121
|
+
sessionBackfillCandidateStatements.set(db, stmt);
|
|
159122
|
+
}
|
|
159123
|
+
const rows = stmt.all(projectPath, sessionId, projectPath, modelId, Math.max(1, limit));
|
|
159124
|
+
return mapBackfillCandidateRows(rows);
|
|
159125
|
+
}
|
|
159126
|
+
function countUnembeddedSessionCompartments(db, projectPath, sessionId, modelId) {
|
|
159127
|
+
const row = db.prepare(`SELECT COUNT(*) AS n
|
|
159128
|
+
FROM compartments c
|
|
159129
|
+
JOIN session_projects sp
|
|
159130
|
+
ON sp.session_id = c.session_id
|
|
159131
|
+
AND sp.harness = c.harness
|
|
159132
|
+
AND sp.project_path = ?
|
|
159133
|
+
WHERE c.session_id = ?
|
|
159134
|
+
AND c.start_message IS NOT NULL
|
|
159135
|
+
AND c.end_message IS NOT NULL
|
|
159136
|
+
AND NOT EXISTS (
|
|
159137
|
+
SELECT 1
|
|
159138
|
+
FROM compartment_chunk_embeddings current
|
|
159139
|
+
WHERE current.compartment_id = c.id
|
|
159140
|
+
AND current.project_path = ?
|
|
159141
|
+
AND current.model_id = ?
|
|
159142
|
+
)`).get(projectPath, sessionId, projectPath, modelId);
|
|
159143
|
+
return typeof row?.n === "number" ? row.n : 0;
|
|
159144
|
+
}
|
|
159145
|
+
|
|
158016
159146
|
// ../plugin/src/features/magic-context/memory/cosine-similarity.ts
|
|
158017
159147
|
function cosineSimilarity(a, b) {
|
|
158018
159148
|
if (a.length !== b.length) {
|
|
@@ -158031,13 +159161,13 @@ function cosineSimilarity(a, b) {
|
|
|
158031
159161
|
}
|
|
158032
159162
|
|
|
158033
159163
|
// ../plugin/src/features/magic-context/memory/normalize-hash.ts
|
|
158034
|
-
import { createHash as
|
|
159164
|
+
import { createHash as createHash5 } from "node:crypto";
|
|
158035
159165
|
function normalizeMemoryContent(content) {
|
|
158036
159166
|
return content.toLowerCase().replace(/\s+/g, " ").trim();
|
|
158037
159167
|
}
|
|
158038
159168
|
function computeNormalizedHash(content) {
|
|
158039
159169
|
const normalized = normalizeMemoryContent(content);
|
|
158040
|
-
return
|
|
159170
|
+
return createHash5("md5").update(normalized).digest("hex");
|
|
158041
159171
|
}
|
|
158042
159172
|
|
|
158043
159173
|
// ../plugin/src/features/magic-context/memory/embedding-identity.ts
|
|
@@ -158181,19 +159311,19 @@ function isArrayLikeNumber(value) {
|
|
|
158181
159311
|
}
|
|
158182
159312
|
return arr.length === 0 || typeof arr[0] === "number";
|
|
158183
159313
|
}
|
|
158184
|
-
function
|
|
159314
|
+
function toFloat32Array2(values) {
|
|
158185
159315
|
return values instanceof Float32Array ? new Float32Array(values) : Float32Array.from(Array.from(values));
|
|
158186
159316
|
}
|
|
158187
159317
|
function extractBatchEmbeddings(result, expectedCount) {
|
|
158188
159318
|
const { data } = result;
|
|
158189
159319
|
if (Array.isArray(data) && data.length === expectedCount && data.every((entry) => typeof entry !== "number" && isArrayLikeNumber(entry))) {
|
|
158190
|
-
return data.map((entry) =>
|
|
159320
|
+
return data.map((entry) => toFloat32Array2(entry));
|
|
158191
159321
|
}
|
|
158192
159322
|
if (!isArrayLikeNumber(data)) {
|
|
158193
159323
|
log("[magic-context] embedding batch returned unexpected data shape");
|
|
158194
159324
|
return Array.from({ length: expectedCount }, () => null);
|
|
158195
159325
|
}
|
|
158196
|
-
const flatData =
|
|
159326
|
+
const flatData = toFloat32Array2(data);
|
|
158197
159327
|
const dimension = result.dims?.at(-1) ?? flatData.length / expectedCount;
|
|
158198
159328
|
if (!Number.isInteger(dimension) || dimension <= 0 || flatData.length !== expectedCount * dimension) {
|
|
158199
159329
|
log("[magic-context] embedding batch returned invalid dimensions");
|
|
@@ -158208,6 +159338,7 @@ function extractBatchEmbeddings(result, expectedCount) {
|
|
|
158208
159338
|
|
|
158209
159339
|
class LocalEmbeddingProvider {
|
|
158210
159340
|
modelId;
|
|
159341
|
+
maxInputTokens;
|
|
158211
159342
|
model;
|
|
158212
159343
|
pipeline = null;
|
|
158213
159344
|
initPromise = null;
|
|
@@ -158215,8 +159346,9 @@ class LocalEmbeddingProvider {
|
|
|
158215
159346
|
disposing = false;
|
|
158216
159347
|
disposePromise = null;
|
|
158217
159348
|
inFlightWaiters = [];
|
|
158218
|
-
constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL) {
|
|
159349
|
+
constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL, maxInputTokens = 512) {
|
|
158219
159350
|
this.model = model;
|
|
159351
|
+
this.maxInputTokens = maxInputTokens;
|
|
158220
159352
|
this.modelId = getEmbeddingProviderIdentity({ provider: "local", model });
|
|
158221
159353
|
}
|
|
158222
159354
|
async initialize() {
|
|
@@ -158464,6 +159596,13 @@ function blockedEmbeddingEndpointReason(endpoint) {
|
|
|
158464
159596
|
function normalizeEndpoint3(endpoint) {
|
|
158465
159597
|
return endpoint?.trim().replace(/\/+$/, "") ?? "";
|
|
158466
159598
|
}
|
|
159599
|
+
function embeddingModelsMatch(served, requested) {
|
|
159600
|
+
const a = served.trim().toLowerCase();
|
|
159601
|
+
const b = requested.trim().toLowerCase();
|
|
159602
|
+
if (a.length === 0 || b.length === 0)
|
|
159603
|
+
return true;
|
|
159604
|
+
return a === b || a.includes(b) || b.includes(a);
|
|
159605
|
+
}
|
|
158467
159606
|
var FAILURE_THRESHOLD = 3;
|
|
158468
159607
|
var FAILURE_WINDOW_MS = 60000;
|
|
158469
159608
|
var OPEN_DURATION_MS = 5 * 60000;
|
|
@@ -158471,6 +159610,7 @@ var FETCH_TIMEOUT_MS = 30000;
|
|
|
158471
159610
|
|
|
158472
159611
|
class OpenAICompatibleEmbeddingProvider {
|
|
158473
159612
|
modelId;
|
|
159613
|
+
maxInputTokens;
|
|
158474
159614
|
endpoint;
|
|
158475
159615
|
model;
|
|
158476
159616
|
apiKey;
|
|
@@ -158480,6 +159620,7 @@ class OpenAICompatibleEmbeddingProvider {
|
|
|
158480
159620
|
failureTimes = [];
|
|
158481
159621
|
circuitOpenUntil = 0;
|
|
158482
159622
|
openLogged = false;
|
|
159623
|
+
modelMismatchLogged = false;
|
|
158483
159624
|
halfOpenProbeInFlight = false;
|
|
158484
159625
|
constructor(options) {
|
|
158485
159626
|
this.endpoint = normalizeEndpoint3(options.endpoint);
|
|
@@ -158487,11 +159628,13 @@ class OpenAICompatibleEmbeddingProvider {
|
|
|
158487
159628
|
this.apiKey = options.apiKey?.trim() ?? "";
|
|
158488
159629
|
this.inputType = options.inputType?.trim() ?? "";
|
|
158489
159630
|
this.truncate = options.truncate?.trim() ?? "";
|
|
159631
|
+
this.maxInputTokens = typeof options.maxInputTokens === "number" && Number.isFinite(options.maxInputTokens) ? Math.max(1, Math.floor(options.maxInputTokens)) : 512;
|
|
158490
159632
|
this.modelId = getEmbeddingProviderIdentity({
|
|
158491
159633
|
provider: "openai-compatible",
|
|
158492
159634
|
endpoint: this.endpoint,
|
|
158493
159635
|
model: this.model,
|
|
158494
|
-
...this.apiKey ? { api_key: this.apiKey } : {}
|
|
159636
|
+
...this.apiKey ? { api_key: this.apiKey } : {},
|
|
159637
|
+
...this.inputType ? { input_type: this.inputType } : {}
|
|
158495
159638
|
});
|
|
158496
159639
|
}
|
|
158497
159640
|
async initialize() {
|
|
@@ -158576,6 +159719,15 @@ class OpenAICompatibleEmbeddingProvider {
|
|
|
158576
159719
|
this.recordFailure(isProbe);
|
|
158577
159720
|
return Array.from({ length: texts.length }, () => null);
|
|
158578
159721
|
}
|
|
159722
|
+
const servedModel = typeof body.model === "string" ? body.model : "";
|
|
159723
|
+
if (this.model && servedModel && !embeddingModelsMatch(servedModel, this.model)) {
|
|
159724
|
+
if (!this.modelMismatchLogged) {
|
|
159725
|
+
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.`);
|
|
159726
|
+
this.modelMismatchLogged = true;
|
|
159727
|
+
}
|
|
159728
|
+
this.recordFailure(isProbe);
|
|
159729
|
+
return Array.from({ length: texts.length }, () => null);
|
|
159730
|
+
}
|
|
158579
159731
|
const items = Array.isArray(body.data) ? body.data : [];
|
|
158580
159732
|
const results = Array.from({ length: texts.length }, (_, index) => {
|
|
158581
159733
|
const embedding = items[index]?.embedding;
|
|
@@ -158695,9 +159847,9 @@ function isEmbeddingRow(row) {
|
|
|
158695
159847
|
if (row === null || typeof row !== "object")
|
|
158696
159848
|
return false;
|
|
158697
159849
|
const candidate = row;
|
|
158698
|
-
return typeof candidate.memoryId === "number" && isEmbeddingBlob(candidate.embedding);
|
|
159850
|
+
return typeof candidate.memoryId === "number" && isEmbeddingBlob(candidate.embedding) && (candidate.modelId === null || typeof candidate.modelId === "string");
|
|
158699
159851
|
}
|
|
158700
|
-
function
|
|
159852
|
+
function toFloat32Array3(blob) {
|
|
158701
159853
|
if (blob instanceof Uint8Array) {
|
|
158702
159854
|
const buffer2 = blob.buffer.slice(blob.byteOffset, blob.byteOffset + blob.byteLength);
|
|
158703
159855
|
return new Float32Array(buffer2);
|
|
@@ -158715,7 +159867,7 @@ function getSaveEmbeddingStatement(db) {
|
|
|
158715
159867
|
function getLoadAllEmbeddingsStatement(db) {
|
|
158716
159868
|
let stmt = loadAllEmbeddingsStatements.get(db);
|
|
158717
159869
|
if (!stmt) {
|
|
158718
|
-
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");
|
|
159870
|
+
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");
|
|
158719
159871
|
loadAllEmbeddingsStatements.set(db, stmt);
|
|
158720
159872
|
}
|
|
158721
159873
|
return stmt;
|
|
@@ -158744,7 +159896,10 @@ function loadAllEmbeddings(db, projectPath) {
|
|
|
158744
159896
|
const rows = getLoadAllEmbeddingsStatement(db).all(projectPath).filter(isEmbeddingRow);
|
|
158745
159897
|
const embeddings = new Map;
|
|
158746
159898
|
for (const row of rows) {
|
|
158747
|
-
embeddings.set(row.memoryId,
|
|
159899
|
+
embeddings.set(row.memoryId, {
|
|
159900
|
+
embedding: toFloat32Array3(row.embedding),
|
|
159901
|
+
modelId: row.modelId
|
|
159902
|
+
});
|
|
158748
159903
|
}
|
|
158749
159904
|
return embeddings;
|
|
158750
159905
|
}
|
|
@@ -158757,7 +159912,7 @@ function getDistinctStoredModelIds(db, projectPath) {
|
|
|
158757
159912
|
}
|
|
158758
159913
|
|
|
158759
159914
|
// ../plugin/src/features/magic-context/project-embedding-registry.ts
|
|
158760
|
-
import { createHash as
|
|
159915
|
+
import { createHash as createHash6, randomUUID } from "node:crypto";
|
|
158761
159916
|
init_logger();
|
|
158762
159917
|
|
|
158763
159918
|
// ../plugin/src/features/magic-context/git-commits/storage-git-commit-embeddings.ts
|
|
@@ -158765,7 +159920,7 @@ var saveStatements = new WeakMap;
|
|
|
158765
159920
|
var loadProjectStatements = new WeakMap;
|
|
158766
159921
|
var loadUnembeddedStatements = new WeakMap;
|
|
158767
159922
|
var countEmbeddedStatements = new WeakMap;
|
|
158768
|
-
var
|
|
159923
|
+
var clearProjectStatements2 = new WeakMap;
|
|
158769
159924
|
var distinctModelIdStatements = new WeakMap;
|
|
158770
159925
|
function getSaveStatement(db) {
|
|
158771
159926
|
let stmt = saveStatements.get(db);
|
|
@@ -158813,12 +159968,12 @@ function getCountEmbeddedStatement(db) {
|
|
|
158813
159968
|
}
|
|
158814
159969
|
return stmt;
|
|
158815
159970
|
}
|
|
158816
|
-
function
|
|
158817
|
-
let stmt =
|
|
159971
|
+
function getClearProjectStatement2(db) {
|
|
159972
|
+
let stmt = clearProjectStatements2.get(db);
|
|
158818
159973
|
if (!stmt) {
|
|
158819
159974
|
stmt = db.prepare(`DELETE FROM git_commit_embeddings
|
|
158820
159975
|
WHERE sha IN (SELECT sha FROM git_commits WHERE project_path = ?)`);
|
|
158821
|
-
|
|
159976
|
+
clearProjectStatements2.set(db, stmt);
|
|
158822
159977
|
}
|
|
158823
159978
|
return stmt;
|
|
158824
159979
|
}
|
|
@@ -158854,7 +160009,7 @@ function countEmbeddedCommits(db, projectPath) {
|
|
|
158854
160009
|
return row?.count ?? 0;
|
|
158855
160010
|
}
|
|
158856
160011
|
function clearProjectCommitEmbeddings(db, projectPath) {
|
|
158857
|
-
return
|
|
160012
|
+
return getClearProjectStatement2(db).run(projectPath).changes;
|
|
158858
160013
|
}
|
|
158859
160014
|
function getDistinctCommitEmbeddingModelIds(db, projectPath) {
|
|
158860
160015
|
const rows = getDistinctModelIdStatement(db).all(projectPath);
|
|
@@ -159018,9 +160173,83 @@ function invalidateMemory(projectPath, memoryId) {
|
|
|
159018
160173
|
cached2?.embeddings.delete(memoryId);
|
|
159019
160174
|
}
|
|
159020
160175
|
|
|
160176
|
+
// ../plugin/src/features/magic-context/session-project-storage.ts
|
|
160177
|
+
var upsertSessionProjectStatements = new WeakMap;
|
|
160178
|
+
var repairSessionChunkProjectStatements = new WeakMap;
|
|
160179
|
+
var repairProjectChunkProjectStatements = new WeakMap;
|
|
160180
|
+
function getUpsertSessionProjectStatement(db) {
|
|
160181
|
+
let stmt = upsertSessionProjectStatements.get(db);
|
|
160182
|
+
if (!stmt) {
|
|
160183
|
+
stmt = db.prepare(`INSERT INTO session_projects (session_id, harness, project_path, updated_at)
|
|
160184
|
+
VALUES (?, ?, ?, ?)
|
|
160185
|
+
ON CONFLICT(session_id, harness) DO UPDATE SET
|
|
160186
|
+
project_path = excluded.project_path,
|
|
160187
|
+
updated_at = excluded.updated_at
|
|
160188
|
+
WHERE session_projects.project_path <> excluded.project_path`);
|
|
160189
|
+
upsertSessionProjectStatements.set(db, stmt);
|
|
160190
|
+
}
|
|
160191
|
+
return stmt;
|
|
160192
|
+
}
|
|
160193
|
+
function getRepairSessionChunkProjectStatement(db) {
|
|
160194
|
+
let stmt = repairSessionChunkProjectStatements.get(db);
|
|
160195
|
+
if (!stmt) {
|
|
160196
|
+
stmt = db.prepare(`UPDATE compartment_chunk_embeddings
|
|
160197
|
+
SET project_path = ?
|
|
160198
|
+
WHERE session_id = ?
|
|
160199
|
+
AND harness = ?
|
|
160200
|
+
AND project_path <> ?`);
|
|
160201
|
+
repairSessionChunkProjectStatements.set(db, stmt);
|
|
160202
|
+
}
|
|
160203
|
+
return stmt;
|
|
160204
|
+
}
|
|
160205
|
+
function getRepairProjectChunkProjectStatement(db) {
|
|
160206
|
+
let stmt = repairProjectChunkProjectStatements.get(db);
|
|
160207
|
+
if (!stmt) {
|
|
160208
|
+
stmt = db.prepare(`UPDATE compartment_chunk_embeddings
|
|
160209
|
+
SET project_path = (
|
|
160210
|
+
SELECT sp.project_path
|
|
160211
|
+
FROM session_projects sp
|
|
160212
|
+
WHERE sp.session_id = compartment_chunk_embeddings.session_id
|
|
160213
|
+
AND sp.harness = compartment_chunk_embeddings.harness
|
|
160214
|
+
LIMIT 1
|
|
160215
|
+
)
|
|
160216
|
+
WHERE EXISTS (
|
|
160217
|
+
SELECT 1
|
|
160218
|
+
FROM session_projects sp
|
|
160219
|
+
WHERE sp.session_id = compartment_chunk_embeddings.session_id
|
|
160220
|
+
AND sp.harness = compartment_chunk_embeddings.harness
|
|
160221
|
+
AND sp.project_path <> compartment_chunk_embeddings.project_path
|
|
160222
|
+
AND (
|
|
160223
|
+
sp.project_path = ?
|
|
160224
|
+
OR compartment_chunk_embeddings.project_path = ?
|
|
160225
|
+
)
|
|
160226
|
+
)`);
|
|
160227
|
+
repairProjectChunkProjectStatements.set(db, stmt);
|
|
160228
|
+
}
|
|
160229
|
+
return stmt;
|
|
160230
|
+
}
|
|
160231
|
+
function recordSessionProjectIdentity(db, sessionId, projectPath) {
|
|
160232
|
+
if (!sessionId || !projectPath)
|
|
160233
|
+
return;
|
|
160234
|
+
const harness = getHarness();
|
|
160235
|
+
const now = Date.now();
|
|
160236
|
+
db.transaction(() => {
|
|
160237
|
+
getUpsertSessionProjectStatement(db).run(sessionId, harness, projectPath, now);
|
|
160238
|
+
getRepairSessionChunkProjectStatement(db).run(projectPath, sessionId, harness, projectPath);
|
|
160239
|
+
})();
|
|
160240
|
+
}
|
|
160241
|
+
function repairMisScopedCompartmentChunkEmbeddingsForProject(db, projectPath) {
|
|
160242
|
+
if (!projectPath)
|
|
160243
|
+
return 0;
|
|
160244
|
+
return getRepairProjectChunkProjectStatement(db).run(projectPath, projectPath).changes;
|
|
160245
|
+
}
|
|
160246
|
+
|
|
159021
160247
|
// ../plugin/src/features/magic-context/project-embedding-registry.ts
|
|
159022
160248
|
var OFF_PROVIDER_IDENTITY = "embedding-provider:off";
|
|
159023
160249
|
var SWEEP_MAX_WALL_CLOCK_MS = 10 * 60 * 1000;
|
|
160250
|
+
var CHUNK_DRAIN_BATCH_SIZE = 8;
|
|
160251
|
+
var MAX_WINDOWS_PER_EMBED_CALL = 16;
|
|
160252
|
+
var SESSION_EMBED_LEASE_RENEWAL_MS = 60 * 1000;
|
|
159024
160253
|
var projectRegistrations = new Map;
|
|
159025
160254
|
var loadUnembeddedMemoriesStatements = new WeakMap;
|
|
159026
160255
|
var globalRegistrationGeneration = 0;
|
|
@@ -159029,7 +160258,10 @@ function resolveEmbeddingConfig(config2) {
|
|
|
159029
160258
|
if (!config2 || config2.provider === "local") {
|
|
159030
160259
|
return {
|
|
159031
160260
|
provider: "local",
|
|
159032
|
-
model: config2?.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL
|
|
160261
|
+
model: config2?.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
160262
|
+
...config2?.max_input_tokens ? {
|
|
160263
|
+
max_input_tokens: normalizeCompartmentChunkMaxInputTokens(config2.max_input_tokens)
|
|
160264
|
+
} : {}
|
|
159033
160265
|
};
|
|
159034
160266
|
}
|
|
159035
160267
|
if (config2.provider === "openai-compatible") {
|
|
@@ -159042,7 +160274,10 @@ function resolveEmbeddingConfig(config2) {
|
|
|
159042
160274
|
endpoint: config2.endpoint.trim(),
|
|
159043
160275
|
...apiKey ? { api_key: apiKey } : {},
|
|
159044
160276
|
...inputType ? { input_type: inputType } : {},
|
|
159045
|
-
...truncate ? { truncate } : {}
|
|
160277
|
+
...truncate ? { truncate } : {},
|
|
160278
|
+
...config2.max_input_tokens ? {
|
|
160279
|
+
max_input_tokens: normalizeCompartmentChunkMaxInputTokens(config2.max_input_tokens)
|
|
160280
|
+
} : {}
|
|
159046
160281
|
};
|
|
159047
160282
|
}
|
|
159048
160283
|
return { provider: "off" };
|
|
@@ -159060,10 +160295,11 @@ function createProvider(config2) {
|
|
|
159060
160295
|
model: config2.model,
|
|
159061
160296
|
apiKey: config2.api_key,
|
|
159062
160297
|
inputType: config2.input_type,
|
|
159063
|
-
truncate: config2.truncate
|
|
160298
|
+
truncate: config2.truncate,
|
|
160299
|
+
maxInputTokens: config2.max_input_tokens
|
|
159064
160300
|
});
|
|
159065
160301
|
}
|
|
159066
|
-
return new LocalEmbeddingProvider(config2.model);
|
|
160302
|
+
return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens);
|
|
159067
160303
|
}
|
|
159068
160304
|
function stableStringify2(value) {
|
|
159069
160305
|
if (Array.isArray(value)) {
|
|
@@ -159076,7 +160312,7 @@ function stableStringify2(value) {
|
|
|
159076
160312
|
return JSON.stringify(value);
|
|
159077
160313
|
}
|
|
159078
160314
|
function sha256Prefix(value, length = 16) {
|
|
159079
|
-
return
|
|
160315
|
+
return createHash6("sha256").update(value).digest("hex").slice(0, length);
|
|
159080
160316
|
}
|
|
159081
160317
|
function getRuntimeFingerprint(config2) {
|
|
159082
160318
|
if (config2.provider === "off") {
|
|
@@ -159084,6 +160320,18 @@ function getRuntimeFingerprint(config2) {
|
|
|
159084
160320
|
}
|
|
159085
160321
|
return `${getEmbeddingProviderIdentity(config2)}:${sha256Prefix(stableStringify2(config2))}`;
|
|
159086
160322
|
}
|
|
160323
|
+
function getChunkEmbeddingModelId(config2, providerIdentity) {
|
|
160324
|
+
if (config2.provider === "off") {
|
|
160325
|
+
return OFF_PROVIDER_IDENTITY;
|
|
160326
|
+
}
|
|
160327
|
+
const chunkIdentity = {
|
|
160328
|
+
providerIdentity,
|
|
160329
|
+
chunkerVersion: 2,
|
|
160330
|
+
maxInputTokens: normalizeCompartmentChunkMaxInputTokens("max_input_tokens" in config2 ? config2.max_input_tokens : undefined),
|
|
160331
|
+
truncate: config2.provider === "openai-compatible" ? config2.truncate ?? "" : ""
|
|
160332
|
+
};
|
|
160333
|
+
return `${providerIdentity}:chunk:${sha256Prefix(stableStringify2(chunkIdentity))}`;
|
|
160334
|
+
}
|
|
159087
160335
|
function sameFeatures(a, b) {
|
|
159088
160336
|
return a.memoryEnabled === b.memoryEnabled && a.gitCommitEnabled === b.gitCommitEnabled;
|
|
159089
160337
|
}
|
|
@@ -159100,7 +160348,8 @@ function snapshotFor(registration) {
|
|
|
159100
160348
|
features: { ...registration.features },
|
|
159101
160349
|
enabled,
|
|
159102
160350
|
gitCommitEnabled,
|
|
159103
|
-
modelId: registration.observationMode || !providerIsOn ? "off" : registration.modelId
|
|
160351
|
+
modelId: registration.observationMode || !providerIsOn ? "off" : registration.modelId,
|
|
160352
|
+
chunkModelId: registration.observationMode || !providerIsOn ? "off" : registration.chunkModelId
|
|
159104
160353
|
};
|
|
159105
160354
|
}
|
|
159106
160355
|
function disposeProvider(provider) {
|
|
@@ -159120,7 +160369,7 @@ function anyStoredModelIdIsStale(storedIds, currentId) {
|
|
|
159120
160369
|
}
|
|
159121
160370
|
return false;
|
|
159122
160371
|
}
|
|
159123
|
-
function maybeWipeStaleEmbeddings(db, projectIdentity, currentProviderIdentity, features) {
|
|
160372
|
+
function maybeWipeStaleEmbeddings(db, projectIdentity, currentProviderIdentity, currentChunkIdentity, features) {
|
|
159124
160373
|
if (currentProviderIdentity === OFF_PROVIDER_IDENTITY) {
|
|
159125
160374
|
return false;
|
|
159126
160375
|
}
|
|
@@ -159141,6 +160390,14 @@ function maybeWipeStaleEmbeddings(db, projectIdentity, currentProviderIdentity,
|
|
|
159141
160390
|
wiped = true;
|
|
159142
160391
|
}
|
|
159143
160392
|
}
|
|
160393
|
+
if (features.memoryEnabled) {
|
|
160394
|
+
repairMisScopedCompartmentChunkEmbeddingsForProject(db, projectIdentity);
|
|
160395
|
+
const chunkIds = getDistinctChunkEmbeddingModelIds(db, projectIdentity);
|
|
160396
|
+
if (anyStoredModelIdIsStale(chunkIds, currentChunkIdentity)) {
|
|
160397
|
+
clearChunkEmbeddingsForProject(db, projectIdentity);
|
|
160398
|
+
wiped = true;
|
|
160399
|
+
}
|
|
160400
|
+
}
|
|
159144
160401
|
})();
|
|
159145
160402
|
return wiped;
|
|
159146
160403
|
}
|
|
@@ -159148,10 +160405,11 @@ function registerProjectEmbeddingAndMaybeWipe(db, projectIdentity, config2, feat
|
|
|
159148
160405
|
const resolvedConfig = resolveEmbeddingConfig(config2);
|
|
159149
160406
|
const providerIdentity = getEmbeddingProviderIdentity(resolvedConfig);
|
|
159150
160407
|
const runtimeFingerprint = getRuntimeFingerprint(resolvedConfig);
|
|
160408
|
+
const chunkModelId = getChunkEmbeddingModelId(resolvedConfig, providerIdentity);
|
|
159151
160409
|
const prior = projectRegistrations.get(projectIdentity);
|
|
159152
160410
|
const canReuseProvider = prior !== undefined && !prior.observationMode && prior.runtimeFingerprint === runtimeFingerprint && prior.providerIdentity === providerIdentity;
|
|
159153
|
-
const wiped = maybeWipeStaleEmbeddings(db, projectIdentity, providerIdentity, features);
|
|
159154
|
-
const generationChanged = prior === undefined || prior.observationMode || prior.runtimeFingerprint !== runtimeFingerprint || !sameFeatures(prior.features, features) || wiped;
|
|
160411
|
+
const wiped = maybeWipeStaleEmbeddings(db, projectIdentity, providerIdentity, chunkModelId, features);
|
|
160412
|
+
const generationChanged = prior === undefined || prior.observationMode || prior.runtimeFingerprint !== runtimeFingerprint || prior.chunkModelId !== chunkModelId || !sameFeatures(prior.features, features) || wiped;
|
|
159155
160413
|
const generation = generationChanged ? ++globalRegistrationGeneration : prior.generation;
|
|
159156
160414
|
const registration = {
|
|
159157
160415
|
projectIdentity,
|
|
@@ -159163,6 +160421,7 @@ function registerProjectEmbeddingAndMaybeWipe(db, projectIdentity, config2, feat
|
|
|
159163
160421
|
generation,
|
|
159164
160422
|
features: { ...features },
|
|
159165
160423
|
modelId: providerIdentity === OFF_PROVIDER_IDENTITY ? "off" : providerIdentity,
|
|
160424
|
+
chunkModelId: providerIdentity === OFF_PROVIDER_IDENTITY ? "off" : chunkModelId,
|
|
159166
160425
|
observationMode: false
|
|
159167
160426
|
};
|
|
159168
160427
|
projectRegistrations.set(projectIdentity, registration);
|
|
@@ -159185,6 +160444,7 @@ function registerProjectInObservationMode(db, projectIdentity, sourceDirectory,
|
|
|
159185
160444
|
generation,
|
|
159186
160445
|
features: { memoryEnabled: false, gitCommitEnabled: false },
|
|
159187
160446
|
modelId: "off",
|
|
160447
|
+
chunkModelId: "off",
|
|
159188
160448
|
observationMode: true
|
|
159189
160449
|
};
|
|
159190
160450
|
projectRegistrations.set(projectIdentity, registration);
|
|
@@ -159195,6 +160455,15 @@ function getProjectEmbeddingSnapshot(projectIdentity) {
|
|
|
159195
160455
|
const registration = projectRegistrations.get(projectIdentity);
|
|
159196
160456
|
return registration ? snapshotFor(registration) : null;
|
|
159197
160457
|
}
|
|
160458
|
+
function getProjectChunkEmbeddingModelId(projectIdentity) {
|
|
160459
|
+
const registration = projectRegistrations.get(projectIdentity);
|
|
160460
|
+
return registration && !registration.observationMode ? registration.chunkModelId : "off";
|
|
160461
|
+
}
|
|
160462
|
+
function getProjectEmbeddingMaxInputTokens(projectIdentity) {
|
|
160463
|
+
const registration = projectRegistrations.get(projectIdentity);
|
|
160464
|
+
const configMax = registration?.config && "max_input_tokens" in registration.config ? registration.config.max_input_tokens : undefined;
|
|
160465
|
+
return normalizeCompartmentChunkMaxInputTokens(registration?.provider?.maxInputTokens ?? configMax);
|
|
160466
|
+
}
|
|
159198
160467
|
function getOrCreateProjectProvider(registration) {
|
|
159199
160468
|
if (registration.providerIdentity === OFF_PROVIDER_IDENTITY || registration.observationMode) {
|
|
159200
160469
|
return null;
|
|
@@ -159289,6 +160558,131 @@ async function embedUnembeddedMemoriesForProject(db, projectIdentity, batchSize
|
|
|
159289
160558
|
return 0;
|
|
159290
160559
|
}
|
|
159291
160560
|
}
|
|
160561
|
+
async function embedCandidateChunkBatch(db, projectIdentity, modelId, candidates, signal) {
|
|
160562
|
+
const noWork = [];
|
|
160563
|
+
if (candidates.length === 0)
|
|
160564
|
+
return { embedded: 0, noWork };
|
|
160565
|
+
const maxInputTokens = getProjectEmbeddingMaxInputTokens(projectIdentity);
|
|
160566
|
+
const prepared = [];
|
|
160567
|
+
for (const candidate of candidates) {
|
|
160568
|
+
const canonicalText = buildCanonicalChunkTextFromFts(db, candidate.sessionId, candidate.startMessage, candidate.endMessage);
|
|
160569
|
+
if (canonicalText.length === 0) {
|
|
160570
|
+
noWork.push(candidate.id);
|
|
160571
|
+
continue;
|
|
160572
|
+
}
|
|
160573
|
+
const windows = chunkCanonicalText(canonicalText, candidate.startMessage, candidate.endMessage, maxInputTokens);
|
|
160574
|
+
if (windows.length === 0 || chunkEmbeddingWindowsAreCurrent(db, candidate.id, modelId, windows, projectIdentity)) {
|
|
160575
|
+
noWork.push(candidate.id);
|
|
160576
|
+
continue;
|
|
160577
|
+
}
|
|
160578
|
+
prepared.push({ candidate, windows });
|
|
160579
|
+
}
|
|
160580
|
+
if (prepared.length === 0)
|
|
160581
|
+
return { embedded: 0, noWork };
|
|
160582
|
+
let embedded = 0;
|
|
160583
|
+
let i = 0;
|
|
160584
|
+
while (i < prepared.length) {
|
|
160585
|
+
if (signal?.aborted)
|
|
160586
|
+
break;
|
|
160587
|
+
const slice = [];
|
|
160588
|
+
let windowCount = 0;
|
|
160589
|
+
do {
|
|
160590
|
+
const item = prepared[i];
|
|
160591
|
+
slice.push(item);
|
|
160592
|
+
windowCount += item.windows.length;
|
|
160593
|
+
i += 1;
|
|
160594
|
+
} while (i < prepared.length && windowCount + prepared[i].windows.length <= MAX_WINDOWS_PER_EMBED_CALL);
|
|
160595
|
+
const texts = [];
|
|
160596
|
+
for (const item of slice)
|
|
160597
|
+
texts.push(...item.windows.map((w) => w.text));
|
|
160598
|
+
try {
|
|
160599
|
+
const result = await embedBatchForProject(projectIdentity, texts, signal);
|
|
160600
|
+
if (!result)
|
|
160601
|
+
continue;
|
|
160602
|
+
if (signal?.aborted)
|
|
160603
|
+
break;
|
|
160604
|
+
let offset = 0;
|
|
160605
|
+
for (const item of slice) {
|
|
160606
|
+
const vectors = result.vectors.slice(offset, offset + item.windows.length);
|
|
160607
|
+
offset += item.windows.length;
|
|
160608
|
+
if (vectors.length !== item.windows.length || vectors.some((v) => !v)) {
|
|
160609
|
+
continue;
|
|
160610
|
+
}
|
|
160611
|
+
const rows = item.windows.map((window, index) => ({
|
|
160612
|
+
compartmentId: item.candidate.id,
|
|
160613
|
+
sessionId: item.candidate.sessionId,
|
|
160614
|
+
projectPath: projectIdentity,
|
|
160615
|
+
window,
|
|
160616
|
+
modelId,
|
|
160617
|
+
vector: vectors[index]
|
|
160618
|
+
}));
|
|
160619
|
+
replaceCompartmentChunkEmbeddings(db, rows);
|
|
160620
|
+
embedded += 1;
|
|
160621
|
+
}
|
|
160622
|
+
} catch (error51) {
|
|
160623
|
+
log("[magic-context] failed to proactively embed compartment chunks:", error51);
|
|
160624
|
+
}
|
|
160625
|
+
}
|
|
160626
|
+
return { embedded, noWork };
|
|
160627
|
+
}
|
|
160628
|
+
async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, options) {
|
|
160629
|
+
const snapshot = getProjectEmbeddingSnapshot(projectIdentity);
|
|
160630
|
+
if (!snapshot?.enabled || snapshot.chunkModelId === "off") {
|
|
160631
|
+
return { status: "disabled", embedded: 0, total: 0 };
|
|
160632
|
+
}
|
|
160633
|
+
recordSessionProjectIdentity(db, sessionId, projectIdentity);
|
|
160634
|
+
const total = countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot.chunkModelId);
|
|
160635
|
+
if (total === 0)
|
|
160636
|
+
return { status: "nothing", embedded: 0, total: 0 };
|
|
160637
|
+
const holderId = `session-embed-${randomUUID()}`;
|
|
160638
|
+
const lease = acquireGitSweepLease(db, projectIdentity, holderId, { ignoreCooldown: true });
|
|
160639
|
+
if (!lease.acquired)
|
|
160640
|
+
return { status: "busy", embedded: 0, total };
|
|
160641
|
+
const renewal = setInterval(() => {
|
|
160642
|
+
try {
|
|
160643
|
+
renewGitSweepLease(db, projectIdentity, holderId);
|
|
160644
|
+
} catch {}
|
|
160645
|
+
}, SESSION_EMBED_LEASE_RENEWAL_MS);
|
|
160646
|
+
renewal.unref?.();
|
|
160647
|
+
const batchSize = Math.max(1, options?.batchSize ?? CHUNK_DRAIN_BATCH_SIZE);
|
|
160648
|
+
const skipIds = [];
|
|
160649
|
+
let embedded = 0;
|
|
160650
|
+
let aborted2 = false;
|
|
160651
|
+
let providerStalled = false;
|
|
160652
|
+
try {
|
|
160653
|
+
options?.onProgress?.({ embedded, total });
|
|
160654
|
+
for (;; ) {
|
|
160655
|
+
if (options?.signal?.aborted) {
|
|
160656
|
+
aborted2 = true;
|
|
160657
|
+
break;
|
|
160658
|
+
}
|
|
160659
|
+
const candidates = loadUnembeddedSessionChunkCandidates(db, projectIdentity, sessionId, snapshot.chunkModelId, batchSize, skipIds);
|
|
160660
|
+
if (candidates.length === 0)
|
|
160661
|
+
break;
|
|
160662
|
+
const { embedded: n, noWork } = await embedCandidateChunkBatch(db, projectIdentity, snapshot.chunkModelId, candidates, options?.signal);
|
|
160663
|
+
for (const id of noWork)
|
|
160664
|
+
skipIds.push(id);
|
|
160665
|
+
if (n === 0 && noWork.length === 0) {
|
|
160666
|
+
providerStalled = true;
|
|
160667
|
+
break;
|
|
160668
|
+
}
|
|
160669
|
+
embedded += n;
|
|
160670
|
+
options?.onProgress?.({ embedded: Math.min(embedded, total), total });
|
|
160671
|
+
await new Promise((resolve3) => setTimeout(resolve3, 0));
|
|
160672
|
+
}
|
|
160673
|
+
} finally {
|
|
160674
|
+
clearInterval(renewal);
|
|
160675
|
+
releaseGitSweepLease(db, projectIdentity, holderId);
|
|
160676
|
+
}
|
|
160677
|
+
if (aborted2)
|
|
160678
|
+
return { status: "aborted", embedded, total };
|
|
160679
|
+
if (providerStalled) {
|
|
160680
|
+
const remaining = Math.max(0, countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot.chunkModelId) - skipIds.length);
|
|
160681
|
+
if (remaining > 0)
|
|
160682
|
+
return { status: "stalled", embedded, total, remaining };
|
|
160683
|
+
}
|
|
160684
|
+
return { status: "done", embedded, total };
|
|
160685
|
+
}
|
|
159292
160686
|
|
|
159293
160687
|
// ../plugin/src/features/magic-context/memory/embedding.ts
|
|
159294
160688
|
var DEFAULT_EMBEDDING_CONFIG = {
|
|
@@ -159308,10 +160702,11 @@ function createProvider2(config2) {
|
|
|
159308
160702
|
model: config2.model,
|
|
159309
160703
|
apiKey: config2.api_key,
|
|
159310
160704
|
inputType: config2.input_type,
|
|
159311
|
-
truncate: config2.truncate
|
|
160705
|
+
truncate: config2.truncate,
|
|
160706
|
+
maxInputTokens: config2.max_input_tokens
|
|
159312
160707
|
});
|
|
159313
160708
|
}
|
|
159314
|
-
return new LocalEmbeddingProvider(config2.model);
|
|
160709
|
+
return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens);
|
|
159315
160710
|
}
|
|
159316
160711
|
function getOrCreateProvider() {
|
|
159317
160712
|
if (provider) {
|
|
@@ -159335,186 +160730,8 @@ async function embedText(text, signal) {
|
|
|
159335
160730
|
}
|
|
159336
160731
|
var SWEEP_MAX_WALL_CLOCK_MS2 = 10 * 60 * 1000;
|
|
159337
160732
|
|
|
159338
|
-
// ../plugin/src/features/magic-context/memory/project-identity.ts
|
|
159339
|
-
import { execFileSync } from "node:child_process";
|
|
159340
|
-
import { createHash as createHash4 } from "node:crypto";
|
|
159341
|
-
import { statSync } from "node:fs";
|
|
159342
|
-
import path3 from "node:path";
|
|
159343
|
-
var GIT_TIMEOUT_MS = 5000;
|
|
159344
|
-
var identityCache = new Map;
|
|
159345
|
-
var directoryFallbackCache = new Map;
|
|
159346
|
-
|
|
159347
|
-
class ProjectIdentityError extends Error {
|
|
159348
|
-
errorClass;
|
|
159349
|
-
rawDirectory;
|
|
159350
|
-
constructor(errorClass, rawDirectory, message, cause) {
|
|
159351
|
-
super(message);
|
|
159352
|
-
this.name = "ProjectIdentityError";
|
|
159353
|
-
this.errorClass = errorClass;
|
|
159354
|
-
this.rawDirectory = rawDirectory;
|
|
159355
|
-
if (cause) {
|
|
159356
|
-
this.cause = cause;
|
|
159357
|
-
}
|
|
159358
|
-
}
|
|
159359
|
-
}
|
|
159360
|
-
function asError(error51) {
|
|
159361
|
-
return error51 instanceof Error ? error51 : undefined;
|
|
159362
|
-
}
|
|
159363
|
-
function getErrorCode(error51) {
|
|
159364
|
-
if (error51 === null || typeof error51 !== "object" || !("code" in error51)) {
|
|
159365
|
-
return;
|
|
159366
|
-
}
|
|
159367
|
-
const code = error51.code;
|
|
159368
|
-
return typeof code === "string" ? code : undefined;
|
|
159369
|
-
}
|
|
159370
|
-
function getErrorSignal(error51) {
|
|
159371
|
-
if (error51 === null || typeof error51 !== "object" || !("signal" in error51)) {
|
|
159372
|
-
return;
|
|
159373
|
-
}
|
|
159374
|
-
const signal = error51.signal;
|
|
159375
|
-
return typeof signal === "string" ? signal : undefined;
|
|
159376
|
-
}
|
|
159377
|
-
function getErrorKilled(error51) {
|
|
159378
|
-
if (error51 === null || typeof error51 !== "object" || !("killed" in error51)) {
|
|
159379
|
-
return false;
|
|
159380
|
-
}
|
|
159381
|
-
return error51.killed === true;
|
|
159382
|
-
}
|
|
159383
|
-
function getErrorStderr(error51) {
|
|
159384
|
-
if (error51 === null || typeof error51 !== "object" || !("stderr" in error51)) {
|
|
159385
|
-
return "";
|
|
159386
|
-
}
|
|
159387
|
-
const stderr = error51.stderr;
|
|
159388
|
-
if (typeof stderr === "string") {
|
|
159389
|
-
return stderr;
|
|
159390
|
-
}
|
|
159391
|
-
if (Buffer.isBuffer(stderr)) {
|
|
159392
|
-
return stderr.toString("utf8");
|
|
159393
|
-
}
|
|
159394
|
-
return "";
|
|
159395
|
-
}
|
|
159396
|
-
function directoryFallback(directory) {
|
|
159397
|
-
const canonical = path3.resolve(directory);
|
|
159398
|
-
const hash2 = createHash4("md5").update(canonical, "utf8").digest("hex").slice(0, 12);
|
|
159399
|
-
return `dir:${hash2}`;
|
|
159400
|
-
}
|
|
159401
|
-
function assertDirectoryUsable(canonicalDirectory, rawDirectory) {
|
|
159402
|
-
try {
|
|
159403
|
-
const stat2 = statSync(canonicalDirectory);
|
|
159404
|
-
if (!stat2.isDirectory()) {
|
|
159405
|
-
throw new ProjectIdentityError("unknown", rawDirectory, `Project path is not a directory: ${canonicalDirectory}`);
|
|
159406
|
-
}
|
|
159407
|
-
} catch (error51) {
|
|
159408
|
-
if (error51 instanceof ProjectIdentityError) {
|
|
159409
|
-
throw error51;
|
|
159410
|
-
}
|
|
159411
|
-
const code = getErrorCode(error51);
|
|
159412
|
-
if (code === "EACCES" || code === "EPERM") {
|
|
159413
|
-
throw new ProjectIdentityError("permission_denied", rawDirectory, `Permission denied while accessing project directory: ${canonicalDirectory}`, asError(error51));
|
|
159414
|
-
}
|
|
159415
|
-
throw new ProjectIdentityError("unknown", rawDirectory, `Unable to access project directory: ${canonicalDirectory}`, asError(error51));
|
|
159416
|
-
}
|
|
159417
|
-
}
|
|
159418
|
-
function isGitTimeoutError(error51) {
|
|
159419
|
-
const code = getErrorCode(error51);
|
|
159420
|
-
const signal = getErrorSignal(error51);
|
|
159421
|
-
return code === "ETIMEDOUT" || signal === "SIGTERM" || signal === "SIGKILL" || getErrorKilled(error51);
|
|
159422
|
-
}
|
|
159423
|
-
function classifyGitError(error51, rawDirectory) {
|
|
159424
|
-
if (isGitTimeoutError(error51)) {
|
|
159425
|
-
return new ProjectIdentityError("git_timeout", rawDirectory, `git rev-list timed out after ${GIT_TIMEOUT_MS}ms`, asError(error51));
|
|
159426
|
-
}
|
|
159427
|
-
const code = getErrorCode(error51);
|
|
159428
|
-
if (code === "ENOENT") {
|
|
159429
|
-
return new ProjectIdentityError("git_missing", rawDirectory, "git binary is not available in PATH", asError(error51));
|
|
159430
|
-
}
|
|
159431
|
-
if (code === "EACCES" || code === "EPERM") {
|
|
159432
|
-
return new ProjectIdentityError("permission_denied", rawDirectory, "Permission denied while spawning git", asError(error51));
|
|
159433
|
-
}
|
|
159434
|
-
const stderr = getErrorStderr(error51).toLowerCase();
|
|
159435
|
-
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")) {
|
|
159436
|
-
return new ProjectIdentityError("not_git_repo", rawDirectory, "Directory has no git root commit; caller may use directory fallback", asError(error51));
|
|
159437
|
-
}
|
|
159438
|
-
return new ProjectIdentityError("unknown", rawDirectory, "git rev-list failed while resolving project identity", asError(error51));
|
|
159439
|
-
}
|
|
159440
|
-
function resolveProjectIdentityStrict(directory) {
|
|
159441
|
-
const canonical = path3.resolve(directory);
|
|
159442
|
-
const cached2 = identityCache.get(canonical);
|
|
159443
|
-
if (cached2 !== undefined) {
|
|
159444
|
-
return cached2;
|
|
159445
|
-
}
|
|
159446
|
-
assertDirectoryUsable(canonical, directory);
|
|
159447
|
-
let output;
|
|
159448
|
-
try {
|
|
159449
|
-
output = execFileSync("git", ["rev-list", "--max-parents=0", "HEAD"], {
|
|
159450
|
-
cwd: canonical,
|
|
159451
|
-
encoding: "utf8",
|
|
159452
|
-
env: { ...process.env, LC_ALL: "C", LANG: "C" },
|
|
159453
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
159454
|
-
timeout: GIT_TIMEOUT_MS
|
|
159455
|
-
});
|
|
159456
|
-
} catch (error51) {
|
|
159457
|
-
throw classifyGitError(error51, directory);
|
|
159458
|
-
}
|
|
159459
|
-
const firstLine = output.split(`
|
|
159460
|
-
`)[0]?.trim() ?? "";
|
|
159461
|
-
const rootCommit = firstLine.slice(0, 64);
|
|
159462
|
-
if (rootCommit.length < 7) {
|
|
159463
|
-
throw new ProjectIdentityError("unknown", directory, "git rev-list returned no valid root commit hash");
|
|
159464
|
-
}
|
|
159465
|
-
const identity = `git:${rootCommit}`;
|
|
159466
|
-
identityCache.set(canonical, identity);
|
|
159467
|
-
return identity;
|
|
159468
|
-
}
|
|
159469
|
-
function shouldUseDirectoryFallback(error51) {
|
|
159470
|
-
return error51.errorClass === "not_git_repo" || error51.errorClass === "unknown" && error51.message.startsWith("Unable to access project directory:");
|
|
159471
|
-
}
|
|
159472
|
-
function resolveProjectIdentity(directory) {
|
|
159473
|
-
const canonical = path3.resolve(directory);
|
|
159474
|
-
const cachedFallback = directoryFallbackCache.get(canonical);
|
|
159475
|
-
if (cachedFallback !== undefined) {
|
|
159476
|
-
if (!hasGitDir(canonical)) {
|
|
159477
|
-
return cachedFallback;
|
|
159478
|
-
}
|
|
159479
|
-
directoryFallbackCache.delete(canonical);
|
|
159480
|
-
}
|
|
159481
|
-
try {
|
|
159482
|
-
return resolveProjectIdentityStrict(directory);
|
|
159483
|
-
} catch (error51) {
|
|
159484
|
-
if (error51 instanceof ProjectIdentityError && shouldUseDirectoryFallback(error51)) {
|
|
159485
|
-
const fallback = directoryFallback(canonical);
|
|
159486
|
-
if (!hasGitDir(canonical)) {
|
|
159487
|
-
directoryFallbackCache.set(canonical, fallback);
|
|
159488
|
-
}
|
|
159489
|
-
return fallback;
|
|
159490
|
-
}
|
|
159491
|
-
throw error51;
|
|
159492
|
-
}
|
|
159493
|
-
}
|
|
159494
|
-
function hasGitDir(canonical) {
|
|
159495
|
-
try {
|
|
159496
|
-
statSync(path3.join(canonical, ".git"));
|
|
159497
|
-
return true;
|
|
159498
|
-
} catch {
|
|
159499
|
-
return false;
|
|
159500
|
-
}
|
|
159501
|
-
}
|
|
159502
|
-
function normalizeStoredProjectPath(rawOrStored) {
|
|
159503
|
-
if (rawOrStored.startsWith("git:") || rawOrStored.startsWith("dir:")) {
|
|
159504
|
-
return rawOrStored;
|
|
159505
|
-
}
|
|
159506
|
-
try {
|
|
159507
|
-
return resolveProjectIdentity(rawOrStored);
|
|
159508
|
-
} catch {
|
|
159509
|
-
return directoryFallback(rawOrStored);
|
|
159510
|
-
}
|
|
159511
|
-
}
|
|
159512
|
-
function storedPathBelongsToIdentity(storedProjectPath, projectIdentity) {
|
|
159513
|
-
return storedProjectPath === projectIdentity || normalizeStoredProjectPath(storedProjectPath) === projectIdentity;
|
|
159514
|
-
}
|
|
159515
|
-
|
|
159516
160733
|
// ../plugin/src/plugin/embedding-bootstrap-helpers.ts
|
|
159517
|
-
import { createHash as
|
|
160734
|
+
import { createHash as createHash7 } from "node:crypto";
|
|
159518
160735
|
init_logger();
|
|
159519
160736
|
var EMBEDDING_AFFECTING_KEYS = new Set([
|
|
159520
160737
|
"embedding.api_key",
|
|
@@ -159536,7 +160753,7 @@ var EMBEDDING_WARNING_TERMS = [
|
|
|
159536
160753
|
];
|
|
159537
160754
|
var loggedFailureSignatures = new Map;
|
|
159538
160755
|
function sha256Prefix2(value, length = 16) {
|
|
159539
|
-
return
|
|
160756
|
+
return createHash7("sha256").update(value).digest("hex").slice(0, length);
|
|
159540
160757
|
}
|
|
159541
160758
|
function warningLooksEmbeddingRelated(message) {
|
|
159542
160759
|
const lower = message.toLowerCase();
|
|
@@ -159697,6 +160914,7 @@ var SESSION_META_SELECT_COLUMNS = [
|
|
|
159697
160914
|
"cached_m0_bytes",
|
|
159698
160915
|
"cached_m1_bytes",
|
|
159699
160916
|
"cached_m0_project_memory_epoch",
|
|
160917
|
+
"cached_m0_workspace_fingerprint",
|
|
159700
160918
|
"cached_m0_project_user_profile_version",
|
|
159701
160919
|
"cached_m0_max_compartment_seq",
|
|
159702
160920
|
"cached_m0_max_memory_id",
|
|
@@ -159744,6 +160962,7 @@ var META_COLUMNS = {
|
|
|
159744
160962
|
cachedM0Bytes: "cached_m0_bytes",
|
|
159745
160963
|
cachedM1Bytes: "cached_m1_bytes",
|
|
159746
160964
|
cachedM0ProjectMemoryEpoch: "cached_m0_project_memory_epoch",
|
|
160965
|
+
cachedM0WorkspaceFingerprint: "cached_m0_workspace_fingerprint",
|
|
159747
160966
|
cachedM0ProjectUserProfileVersion: "cached_m0_project_user_profile_version",
|
|
159748
160967
|
cachedM0MaxCompartmentSeq: "cached_m0_max_compartment_seq",
|
|
159749
160968
|
cachedM0MaxMemoryId: "cached_m0_max_memory_id",
|
|
@@ -159773,6 +160992,7 @@ var NULL_BIND_META_KEYS = new Set([
|
|
|
159773
160992
|
"cachedM0Bytes",
|
|
159774
160993
|
"cachedM1Bytes",
|
|
159775
160994
|
"cachedM0ProjectMemoryEpoch",
|
|
160995
|
+
"cachedM0WorkspaceFingerprint",
|
|
159776
160996
|
"cachedM0ProjectUserProfileVersion",
|
|
159777
160997
|
"cachedM0MaxCompartmentSeq",
|
|
159778
160998
|
"cachedM0MaxMemoryId",
|
|
@@ -159806,7 +161026,7 @@ function isSessionMetaRow(row) {
|
|
|
159806
161026
|
if (row === null || typeof row !== "object")
|
|
159807
161027
|
return false;
|
|
159808
161028
|
const r = row;
|
|
159809
|
-
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);
|
|
161029
|
+
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);
|
|
159810
161030
|
}
|
|
159811
161031
|
function getDefaultSessionMeta(sessionId) {
|
|
159812
161032
|
return {
|
|
@@ -159833,6 +161053,7 @@ function getDefaultSessionMeta(sessionId) {
|
|
|
159833
161053
|
cachedM0Bytes: null,
|
|
159834
161054
|
cachedM1Bytes: null,
|
|
159835
161055
|
cachedM0ProjectMemoryEpoch: null,
|
|
161056
|
+
cachedM0WorkspaceFingerprint: null,
|
|
159836
161057
|
cachedM0ProjectUserProfileVersion: null,
|
|
159837
161058
|
cachedM0MaxCompartmentSeq: null,
|
|
159838
161059
|
cachedM0MaxMemoryId: null,
|
|
@@ -159895,6 +161116,7 @@ function toSessionMeta(row) {
|
|
|
159895
161116
|
cachedM0Bytes: toBufferOrNull(row.cached_m0_bytes),
|
|
159896
161117
|
cachedM1Bytes: toBufferOrNull(row.cached_m1_bytes),
|
|
159897
161118
|
cachedM0ProjectMemoryEpoch: numOrNull(row.cached_m0_project_memory_epoch),
|
|
161119
|
+
cachedM0WorkspaceFingerprint: stringOrNull(row.cached_m0_workspace_fingerprint),
|
|
159898
161120
|
cachedM0ProjectUserProfileVersion: numOrNull(row.cached_m0_project_user_profile_version),
|
|
159899
161121
|
cachedM0MaxCompartmentSeq: numOrNull(row.cached_m0_max_compartment_seq),
|
|
159900
161122
|
cachedM0MaxMemoryId: numOrNull(row.cached_m0_max_memory_id),
|
|
@@ -159925,6 +161147,7 @@ function persistCachedM0(db, sessionId, payload) {
|
|
|
159925
161147
|
db.prepare(`UPDATE session_meta SET
|
|
159926
161148
|
cached_m0_bytes = ?,
|
|
159927
161149
|
cached_m0_project_memory_epoch = ?,
|
|
161150
|
+
cached_m0_workspace_fingerprint = ?,
|
|
159928
161151
|
cached_m0_project_user_profile_version = ?,
|
|
159929
161152
|
cached_m0_max_compartment_seq = ?,
|
|
159930
161153
|
cached_m0_max_memory_id = ?,
|
|
@@ -159937,7 +161160,7 @@ function persistCachedM0(db, sessionId, payload) {
|
|
|
159937
161160
|
cached_m0_upgrade_state = ?,
|
|
159938
161161
|
cached_m0_system_hash = ?,
|
|
159939
161162
|
cached_m0_model_key = ?
|
|
159940
|
-
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);
|
|
161163
|
+
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);
|
|
159941
161164
|
}
|
|
159942
161165
|
function clearCachedM0M1(db, sessionId) {
|
|
159943
161166
|
ensureSessionMetaRow(db, sessionId);
|
|
@@ -159946,6 +161169,7 @@ function clearCachedM0M1(db, sessionId) {
|
|
|
159946
161169
|
["cached_m0_bytes", null],
|
|
159947
161170
|
["cached_m1_bytes", null],
|
|
159948
161171
|
["cached_m0_project_memory_epoch", null],
|
|
161172
|
+
["cached_m0_workspace_fingerprint", null],
|
|
159949
161173
|
["cached_m0_project_user_profile_version", null],
|
|
159950
161174
|
["cached_m0_max_compartment_seq", null],
|
|
159951
161175
|
["cached_m0_max_memory_id", null],
|
|
@@ -160281,8 +161505,8 @@ function readRawSessionTailFromDb(db, sessionId, baseOrdinal, anchorMessageId) {
|
|
|
160281
161505
|
const CHUNK = 800;
|
|
160282
161506
|
for (let i = 0;i < ids.length; i += CHUNK) {
|
|
160283
161507
|
const slice = ids.slice(i, i + CHUNK);
|
|
160284
|
-
const
|
|
160285
|
-
const partRows = db.prepare(`SELECT message_id, data, time_updated FROM part WHERE session_id = ? AND message_id IN (${
|
|
161508
|
+
const placeholders2 = slice.map(() => "?").join(",");
|
|
161509
|
+
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);
|
|
160286
161510
|
for (const part of partRows) {
|
|
160287
161511
|
const list = partsByMessageId.get(part.message_id) ?? [];
|
|
160288
161512
|
list.push(attachRawPartVersion(parseJsonUnknown(part.data), part.time_updated));
|
|
@@ -165179,53 +166403,6 @@ function createCtxExpandTool(deps) {
|
|
|
165179
166403
|
}
|
|
165180
166404
|
};
|
|
165181
166405
|
}
|
|
165182
|
-
|
|
165183
|
-
// ../plugin/src/features/magic-context/memory/constants.ts
|
|
165184
|
-
var V2_MEMORY_CATEGORIES = [
|
|
165185
|
-
"PROJECT_RULES",
|
|
165186
|
-
"ARCHITECTURE",
|
|
165187
|
-
"CONSTRAINTS",
|
|
165188
|
-
"CONFIG_VALUES",
|
|
165189
|
-
"NAMING"
|
|
165190
|
-
];
|
|
165191
|
-
var PROMOTABLE_CATEGORIES = [
|
|
165192
|
-
"PROJECT_RULES",
|
|
165193
|
-
"ARCHITECTURE",
|
|
165194
|
-
"CONSTRAINTS",
|
|
165195
|
-
"CONFIG_VALUES",
|
|
165196
|
-
"NAMING",
|
|
165197
|
-
"ARCHITECTURE_DECISIONS",
|
|
165198
|
-
"CONFIG_DEFAULTS",
|
|
165199
|
-
"USER_PREFERENCES",
|
|
165200
|
-
"USER_DIRECTIVES",
|
|
165201
|
-
"ENVIRONMENT",
|
|
165202
|
-
"WORKFLOW_RULES",
|
|
165203
|
-
"KNOWN_ISSUES"
|
|
165204
|
-
];
|
|
165205
|
-
var CATEGORY_PRIORITY = [
|
|
165206
|
-
"PROJECT_RULES",
|
|
165207
|
-
"ARCHITECTURE",
|
|
165208
|
-
"CONSTRAINTS",
|
|
165209
|
-
"CONFIG_VALUES",
|
|
165210
|
-
"NAMING",
|
|
165211
|
-
"USER_DIRECTIVES",
|
|
165212
|
-
"USER_PREFERENCES",
|
|
165213
|
-
"CONFIG_DEFAULTS",
|
|
165214
|
-
"ARCHITECTURE_DECISIONS",
|
|
165215
|
-
"ENVIRONMENT",
|
|
165216
|
-
"WORKFLOW_RULES",
|
|
165217
|
-
"KNOWN_ISSUES"
|
|
165218
|
-
];
|
|
165219
|
-
var MEMORY_CATEGORY_ORDER_UNKNOWN = 99;
|
|
165220
|
-
var MEMORY_CATEGORY_ORDER_PRIORITY = CATEGORY_PRIORITY.reduce((acc, category, index) => {
|
|
165221
|
-
acc[category] = index;
|
|
165222
|
-
return acc;
|
|
165223
|
-
}, {});
|
|
165224
|
-
var MEMORY_CATEGORY_ORDER_SQL = `CASE category ${CATEGORY_PRIORITY.map((category, index) => `WHEN '${category}' THEN ${index}`).join(" ")} ELSE ${MEMORY_CATEGORY_ORDER_UNKNOWN} END`;
|
|
165225
|
-
var CATEGORY_DEFAULT_TTL = {
|
|
165226
|
-
WORKFLOW_RULES: 90 * 24 * 60 * 60 * 1000,
|
|
165227
|
-
KNOWN_ISSUES: 30 * 24 * 60 * 60 * 1000
|
|
165228
|
-
};
|
|
165229
166406
|
// ../plugin/src/features/magic-context/memory/embedding-backfill.ts
|
|
165230
166407
|
init_logger();
|
|
165231
166408
|
async function ensureMemoryEmbeddings(args) {
|
|
@@ -165250,7 +166427,7 @@ async function ensureMemoryEmbeddings(args) {
|
|
|
165250
166427
|
continue;
|
|
165251
166428
|
}
|
|
165252
166429
|
saveEmbedding(args.db, memory2.id, embedding, result.modelId);
|
|
165253
|
-
staged.set(memory2.id, embedding);
|
|
166430
|
+
staged.set(memory2.id, { embedding, modelId: result.modelId });
|
|
165254
166431
|
}
|
|
165255
166432
|
})();
|
|
165256
166433
|
const currentSnapshot = getProjectEmbeddingSnapshot(args.projectIdentity);
|
|
@@ -165453,8 +166630,8 @@ function getMemoriesByProjectStatement(db, statuses) {
|
|
|
165453
166630
|
}
|
|
165454
166631
|
let stmt = statements.get(db);
|
|
165455
166632
|
if (!stmt) {
|
|
165456
|
-
const
|
|
165457
|
-
stmt = db.prepare(`SELECT ${getMemorySelectColumns(db)} FROM memories WHERE project_path = ? AND status IN (${
|
|
166633
|
+
const placeholders2 = statuses.map(() => "?").join(", ");
|
|
166634
|
+
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`);
|
|
165458
166635
|
statements.set(db, stmt);
|
|
165459
166636
|
}
|
|
165460
166637
|
return stmt;
|
|
@@ -165599,6 +166776,97 @@ function getMemoriesByProject(db, projectPath, statuses = ["active", "permanent"
|
|
|
165599
166776
|
const rows = getMemoriesByProjectStatement(db, statuses).all(projectPath, ...statuses, expiryCutoff).filter(isMemoryRow);
|
|
165600
166777
|
return rows.map(toMemory);
|
|
165601
166778
|
}
|
|
166779
|
+
function sqlPlaceholders(values) {
|
|
166780
|
+
return values.map(() => "?").join(", ");
|
|
166781
|
+
}
|
|
166782
|
+
function uniqueValues(values) {
|
|
166783
|
+
return [...new Set(values.filter((value) => value.length > 0))];
|
|
166784
|
+
}
|
|
166785
|
+
function buildWorkspaceMemorySqlFilter(args) {
|
|
166786
|
+
if (args.shareCategories === null || args.shareCategories === undefined) {
|
|
166787
|
+
return { clause: "", params: [], active: false };
|
|
166788
|
+
}
|
|
166789
|
+
const identities = uniqueValues(args.identities);
|
|
166790
|
+
const identitySet = new Set(identities);
|
|
166791
|
+
const ownSet = new Set(uniqueValues(args.ownIdentities ?? []).filter((identity) => identitySet.has(identity)));
|
|
166792
|
+
const foreignIdentities = identities.filter((identity) => !ownSet.has(identity));
|
|
166793
|
+
if (foreignIdentities.length === 0) {
|
|
166794
|
+
return { clause: "", params: [], active: false };
|
|
166795
|
+
}
|
|
166796
|
+
const ownIdentities = identities.filter((identity) => ownSet.has(identity));
|
|
166797
|
+
const shareCategories = uniqueValues([...args.shareCategories]);
|
|
166798
|
+
const qualifier = args.tableName ? `${args.tableName}.` : "";
|
|
166799
|
+
const predicates = [];
|
|
166800
|
+
const params = [];
|
|
166801
|
+
if (ownIdentities.length > 0) {
|
|
166802
|
+
predicates.push(`${qualifier}project_path IN (${sqlPlaceholders(ownIdentities)})`);
|
|
166803
|
+
params.push(...ownIdentities);
|
|
166804
|
+
}
|
|
166805
|
+
if (foreignIdentities.length > 0 && shareCategories.length > 0) {
|
|
166806
|
+
predicates.push(`(${qualifier}project_path IN (${sqlPlaceholders(foreignIdentities)}) AND ${qualifier}category IN (${sqlPlaceholders(shareCategories)}))`);
|
|
166807
|
+
params.push(...foreignIdentities, ...shareCategories);
|
|
166808
|
+
}
|
|
166809
|
+
if (predicates.length === 0) {
|
|
166810
|
+
return { clause: " AND 0 = 1", params: [], active: true };
|
|
166811
|
+
}
|
|
166812
|
+
return { clause: ` AND (${predicates.join(" OR ")})`, params, active: true };
|
|
166813
|
+
}
|
|
166814
|
+
function getMemoriesByProjects(db, projectPaths, statuses = ["active", "permanent"], expiryCutoff = Date.now(), ownIdentities, shareCategories) {
|
|
166815
|
+
const identities = uniqueValues(projectPaths);
|
|
166816
|
+
if (identities.length === 0 || statuses.length === 0)
|
|
166817
|
+
return [];
|
|
166818
|
+
const sharingFilter = buildWorkspaceMemorySqlFilter({
|
|
166819
|
+
identities,
|
|
166820
|
+
ownIdentities,
|
|
166821
|
+
shareCategories
|
|
166822
|
+
});
|
|
166823
|
+
if (identities.length === 1 && !sharingFilter.active) {
|
|
166824
|
+
return getMemoriesByProject(db, identities[0], statuses, expiryCutoff);
|
|
166825
|
+
}
|
|
166826
|
+
const rows = db.prepare(`SELECT ${getMemorySelectColumns(db)}
|
|
166827
|
+
FROM memories
|
|
166828
|
+
WHERE project_path IN (${sqlPlaceholders(identities)})
|
|
166829
|
+
AND status IN (${sqlPlaceholders(statuses)})
|
|
166830
|
+
AND (expires_at IS NULL OR expires_at > ?)${sharingFilter.clause}
|
|
166831
|
+
ORDER BY category ASC, updated_at DESC, id ASC`).all(...identities, ...statuses, expiryCutoff, ...sharingFilter.params).filter(isMemoryRow);
|
|
166832
|
+
return rows.map(toMemory);
|
|
166833
|
+
}
|
|
166834
|
+
function getMaxMemoryIdForProjects(db, projectPaths, ownIdentities, shareCategories) {
|
|
166835
|
+
const identities = uniqueValues(projectPaths);
|
|
166836
|
+
if (identities.length === 0)
|
|
166837
|
+
return 0;
|
|
166838
|
+
const sharingFilter = buildWorkspaceMemorySqlFilter({
|
|
166839
|
+
identities,
|
|
166840
|
+
ownIdentities,
|
|
166841
|
+
shareCategories
|
|
166842
|
+
});
|
|
166843
|
+
if (identities.length === 1 && !sharingFilter.active) {
|
|
166844
|
+
const row2 = db.prepare("SELECT COALESCE(MAX(id), 0) AS max_id FROM memories WHERE project_path = ?").get(identities[0]);
|
|
166845
|
+
return typeof row2?.max_id === "number" ? row2.max_id : 0;
|
|
166846
|
+
}
|
|
166847
|
+
const row = db.prepare(`SELECT COALESCE(MAX(id), 0) AS max_id
|
|
166848
|
+
FROM memories
|
|
166849
|
+
WHERE project_path IN (${sqlPlaceholders(identities)})${sharingFilter.clause}`).get(...identities, ...sharingFilter.params);
|
|
166850
|
+
return typeof row?.max_id === "number" ? row.max_id : 0;
|
|
166851
|
+
}
|
|
166852
|
+
function readNewMemoriesForM1Union(db, projectPaths, afterId, expiryCutoff, ownIdentities, shareCategories) {
|
|
166853
|
+
const identities = uniqueValues(projectPaths);
|
|
166854
|
+
if (identities.length === 0)
|
|
166855
|
+
return [];
|
|
166856
|
+
const sharingFilter = buildWorkspaceMemorySqlFilter({
|
|
166857
|
+
identities,
|
|
166858
|
+
ownIdentities,
|
|
166859
|
+
shareCategories
|
|
166860
|
+
});
|
|
166861
|
+
const rows = db.prepare(`SELECT ${getMemorySelectColumns(db)}
|
|
166862
|
+
FROM memories
|
|
166863
|
+
WHERE project_path IN (${sqlPlaceholders(identities)})
|
|
166864
|
+
AND id > ?
|
|
166865
|
+
AND status IN ('active', 'permanent')
|
|
166866
|
+
AND (expires_at IS NULL OR expires_at > ?)${sharingFilter.clause}
|
|
166867
|
+
ORDER BY ${MEMORY_CATEGORY_ORDER_SQL}, id ASC`).all(...identities, afterId, expiryCutoff, ...sharingFilter.params).filter(isMemoryRow);
|
|
166868
|
+
return rows.map(toMemory);
|
|
166869
|
+
}
|
|
165602
166870
|
function getAllActiveMemoriesForMigration(db, projectPath) {
|
|
165603
166871
|
const rows = getActiveMemoriesNoExpiryStatement(db).all(projectPath).filter(isMemoryRow);
|
|
165604
166872
|
return rows.map(toMemory);
|
|
@@ -165766,6 +167034,7 @@ async function embedAndStoreMemory(db, sessionId, projectPath, memoryId, content
|
|
|
165766
167034
|
// ../plugin/src/features/magic-context/memory/storage-memory-fts.ts
|
|
165767
167035
|
var DEFAULT_SEARCH_LIMIT = 10;
|
|
165768
167036
|
var searchStatements = new WeakMap;
|
|
167037
|
+
var unionSearchStatements = new Map;
|
|
165769
167038
|
function getSearchStatement(db) {
|
|
165770
167039
|
let stmt = searchStatements.get(db);
|
|
165771
167040
|
if (!stmt) {
|
|
@@ -165774,6 +167043,23 @@ function getSearchStatement(db) {
|
|
|
165774
167043
|
}
|
|
165775
167044
|
return stmt;
|
|
165776
167045
|
}
|
|
167046
|
+
function getUnionSearchStatement(db, arity) {
|
|
167047
|
+
let statements = unionSearchStatements.get(arity);
|
|
167048
|
+
if (!statements) {
|
|
167049
|
+
statements = new WeakMap;
|
|
167050
|
+
unionSearchStatements.set(arity, statements);
|
|
167051
|
+
}
|
|
167052
|
+
let stmt = statements.get(db);
|
|
167053
|
+
if (!stmt) {
|
|
167054
|
+
const placeholders2 = Array.from({ length: arity }, () => "?").join(", ");
|
|
167055
|
+
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 ?`);
|
|
167056
|
+
statements.set(db, stmt);
|
|
167057
|
+
}
|
|
167058
|
+
return stmt;
|
|
167059
|
+
}
|
|
167060
|
+
function uniqueProjectPaths(projectPaths) {
|
|
167061
|
+
return [...new Set(projectPaths.filter((path4) => path4.length > 0))];
|
|
167062
|
+
}
|
|
165777
167063
|
function sanitizeFtsQuery(query) {
|
|
165778
167064
|
const tokens = query.split(/\s+/).filter((token) => token.length > 0);
|
|
165779
167065
|
if (tokens.length === 0)
|
|
@@ -165792,6 +167078,28 @@ function searchMemoriesFTS(db, projectPath, query, limit = DEFAULT_SEARCH_LIMIT)
|
|
|
165792
167078
|
const rows = getSearchStatement(db).all(projectPath, Date.now(), sanitized, limit).filter(isMemoryRow);
|
|
165793
167079
|
return rows.map(toMemory);
|
|
165794
167080
|
}
|
|
167081
|
+
function searchMemoriesFTSUnion(db, projectPaths, query, limit = DEFAULT_SEARCH_LIMIT, ownIdentities, shareCategories) {
|
|
167082
|
+
const identities = uniqueProjectPaths(projectPaths);
|
|
167083
|
+
if (identities.length === 0)
|
|
167084
|
+
return [];
|
|
167085
|
+
const sharingFilter = buildWorkspaceMemorySqlFilter({
|
|
167086
|
+
identities,
|
|
167087
|
+
ownIdentities,
|
|
167088
|
+
shareCategories,
|
|
167089
|
+
tableName: "memories"
|
|
167090
|
+
});
|
|
167091
|
+
if (identities.length === 1 && !sharingFilter.active) {
|
|
167092
|
+
return searchMemoriesFTS(db, identities[0], query, limit);
|
|
167093
|
+
}
|
|
167094
|
+
const trimmedQuery = query.trim();
|
|
167095
|
+
if (trimmedQuery.length === 0 || limit <= 0)
|
|
167096
|
+
return [];
|
|
167097
|
+
const sanitized = sanitizeFtsQuery(trimmedQuery);
|
|
167098
|
+
if (sanitized.length === 0)
|
|
167099
|
+
return [];
|
|
167100
|
+
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);
|
|
167101
|
+
return rows.map(toMemory);
|
|
167102
|
+
}
|
|
165795
167103
|
// ../plugin/src/features/magic-context/message-index.ts
|
|
165796
167104
|
var lastIndexedStatements = new WeakMap;
|
|
165797
167105
|
var insertMessageStatements = new WeakMap;
|
|
@@ -165938,7 +167246,7 @@ function indexMessagesAfterOrdinal(db, sessionId, messages, lastIndexedOrdinal,
|
|
|
165938
167246
|
return inserted;
|
|
165939
167247
|
}
|
|
165940
167248
|
// ../plugin/src/features/magic-context/project-docs-hash.ts
|
|
165941
|
-
import { createHash as
|
|
167249
|
+
import { createHash as createHash8 } from "node:crypto";
|
|
165942
167250
|
import { lstatSync, readFileSync as readFileSync4, statSync as statSync2 } from "node:fs";
|
|
165943
167251
|
import path4 from "node:path";
|
|
165944
167252
|
var PROJECT_DOC_FILES = ["ARCHITECTURE.md", "STRUCTURE.md"];
|
|
@@ -166036,7 +167344,7 @@ function hashCanonicalPieces(hashPieces) {
|
|
|
166036
167344
|
if (hashPieces.length === 0) {
|
|
166037
167345
|
return "";
|
|
166038
167346
|
}
|
|
166039
|
-
return
|
|
167347
|
+
return createHash8("sha256").update(hashPieces.join(PROJECT_DOCS_DELIMITER), "utf8").digest("hex");
|
|
166040
167348
|
}
|
|
166041
167349
|
function readProjectDocsCanonical(projectDirectory) {
|
|
166042
167350
|
const canonicalDirectory = path4.resolve(projectDirectory);
|
|
@@ -166142,18 +167450,13 @@ function getMemoryMutation(db, id) {
|
|
|
166142
167450
|
WHERE id = ?`).get(id);
|
|
166143
167451
|
return row ? toMemoryMutation(row) : null;
|
|
166144
167452
|
}
|
|
166145
|
-
function
|
|
166146
|
-
|
|
166147
|
-
|
|
166148
|
-
|
|
166149
|
-
|
|
166150
|
-
|
|
166151
|
-
|
|
166152
|
-
FROM memory_mutation_log
|
|
166153
|
-
WHERE project_path = ?
|
|
166154
|
-
AND id > ?
|
|
166155
|
-
AND target_memory_id IN (${placeholders})
|
|
166156
|
-
ORDER BY id ASC`).all(projectPath, afterId ?? 0, ...uniqueIds);
|
|
167453
|
+
function uniqueProjectPaths2(projectPaths) {
|
|
167454
|
+
return [...new Set(projectPaths.filter((path5) => path5.length > 0))];
|
|
167455
|
+
}
|
|
167456
|
+
function placeholders2(values) {
|
|
167457
|
+
return values.map(() => "?").join(", ");
|
|
167458
|
+
}
|
|
167459
|
+
function coalesceMutations(rows) {
|
|
166157
167460
|
const chosenByTarget = new Map;
|
|
166158
167461
|
for (const dbRow of rows) {
|
|
166159
167462
|
const candidate = toMemoryMutation(dbRow);
|
|
@@ -166174,10 +167477,54 @@ function getMemoryMutationsForRender(db, projectPath, afterId, renderedMemoryIds
|
|
|
166174
167477
|
}
|
|
166175
167478
|
return [...chosenByTarget.values()].sort((left, right) => left.id - right.id);
|
|
166176
167479
|
}
|
|
167480
|
+
function getMemoryMutationsForRender(db, projectPath, afterId, renderedMemoryIds) {
|
|
167481
|
+
if (renderedMemoryIds.length === 0)
|
|
167482
|
+
return [];
|
|
167483
|
+
const uniqueIds = [...new Set(renderedMemoryIds)].sort((left, right) => left - right);
|
|
167484
|
+
const placeholders3 = uniqueIds.map(() => "?").join(", ");
|
|
167485
|
+
const rows = db.prepare(`SELECT id, project_path, mutation_type, target_memory_id,
|
|
167486
|
+
superseded_by_id, category, new_content, queued_at
|
|
167487
|
+
FROM memory_mutation_log
|
|
167488
|
+
WHERE project_path = ?
|
|
167489
|
+
AND id > ?
|
|
167490
|
+
AND target_memory_id IN (${placeholders3})
|
|
167491
|
+
ORDER BY id ASC`).all(projectPath, afterId ?? 0, ...uniqueIds);
|
|
167492
|
+
return coalesceMutations(rows);
|
|
167493
|
+
}
|
|
167494
|
+
function getMemoryMutationsForRenderByProjects(db, projectPaths, afterId, renderedMemoryIds) {
|
|
167495
|
+
if (renderedMemoryIds.length === 0)
|
|
167496
|
+
return [];
|
|
167497
|
+
const identities = uniqueProjectPaths2(projectPaths);
|
|
167498
|
+
if (identities.length === 0)
|
|
167499
|
+
return [];
|
|
167500
|
+
if (identities.length === 1) {
|
|
167501
|
+
return getMemoryMutationsForRender(db, identities[0], afterId, renderedMemoryIds);
|
|
167502
|
+
}
|
|
167503
|
+
const uniqueIds = [...new Set(renderedMemoryIds)].sort((left, right) => left - right);
|
|
167504
|
+
const rows = db.prepare(`SELECT id, project_path, mutation_type, target_memory_id,
|
|
167505
|
+
superseded_by_id, category, new_content, queued_at
|
|
167506
|
+
FROM memory_mutation_log
|
|
167507
|
+
WHERE project_path IN (${placeholders2(identities)})
|
|
167508
|
+
AND id > ?
|
|
167509
|
+
AND target_memory_id IN (${placeholders2(uniqueIds)})
|
|
167510
|
+
ORDER BY id ASC`).all(...identities, afterId ?? 0, ...uniqueIds);
|
|
167511
|
+
return coalesceMutations(rows);
|
|
167512
|
+
}
|
|
166177
167513
|
function getMaxMemoryMutationId(db, projectPath) {
|
|
166178
167514
|
const row = db.prepare("SELECT MAX(id) AS max_id FROM memory_mutation_log WHERE project_path = ?").get(projectPath);
|
|
166179
167515
|
return row?.max_id ?? null;
|
|
166180
167516
|
}
|
|
167517
|
+
function getMaxMemoryMutationIdForProjects(db, projectPaths) {
|
|
167518
|
+
const identities = uniqueProjectPaths2(projectPaths);
|
|
167519
|
+
if (identities.length === 0)
|
|
167520
|
+
return null;
|
|
167521
|
+
if (identities.length === 1)
|
|
167522
|
+
return getMaxMemoryMutationId(db, identities[0]);
|
|
167523
|
+
const row = db.prepare(`SELECT MAX(id) AS max_id
|
|
167524
|
+
FROM memory_mutation_log
|
|
167525
|
+
WHERE project_path IN (${placeholders2(identities)})`).get(...identities);
|
|
167526
|
+
return row?.max_id ?? null;
|
|
167527
|
+
}
|
|
166181
167528
|
// ../plugin/src/features/magic-context/storage-meta-persisted.ts
|
|
166182
167529
|
init_logger();
|
|
166183
167530
|
var CAS_RETRY_LIMIT = 5;
|
|
@@ -167023,9 +168370,9 @@ function buildStatusClause(status) {
|
|
|
167023
168370
|
if (statuses.length === 0) {
|
|
167024
168371
|
return null;
|
|
167025
168372
|
}
|
|
167026
|
-
const
|
|
168373
|
+
const placeholders3 = statuses.map(() => "?").join(", ");
|
|
167027
168374
|
return {
|
|
167028
|
-
sql: `status IN (${
|
|
168375
|
+
sql: `status IN (${placeholders3})`,
|
|
167029
168376
|
params: statuses
|
|
167030
168377
|
};
|
|
167031
168378
|
}
|
|
@@ -167221,19 +168568,6 @@ function getProjectState(db, projectPath) {
|
|
|
167221
168568
|
WHERE project_path = ?`).get(projectPath);
|
|
167222
168569
|
return row ? toProjectState(row) : null;
|
|
167223
168570
|
}
|
|
167224
|
-
function bumpProjectMemoryEpoch(db, projectPath, now = Date.now()) {
|
|
167225
|
-
db.prepare(`INSERT INTO project_state
|
|
167226
|
-
(project_path, project_memory_epoch, project_user_profile_version, updated_at)
|
|
167227
|
-
VALUES (?, 1, 0, ?)
|
|
167228
|
-
ON CONFLICT(project_path) DO UPDATE SET
|
|
167229
|
-
project_memory_epoch = project_memory_epoch + 1,
|
|
167230
|
-
updated_at = excluded.updated_at`).run(projectPath, now);
|
|
167231
|
-
const state = getProjectState(db, projectPath);
|
|
167232
|
-
if (!state) {
|
|
167233
|
-
throw new Error(`Failed to bump project memory epoch for ${projectPath}`);
|
|
167234
|
-
}
|
|
167235
|
-
return state;
|
|
167236
|
-
}
|
|
167237
168571
|
function bumpProjectUserProfileVersion(db, projectPath = GLOBAL_USER_PROFILE_PROJECT_PATH, now = Date.now()) {
|
|
167238
168572
|
db.prepare(`INSERT INTO project_state
|
|
167239
168573
|
(project_path, project_memory_epoch, project_user_profile_version, updated_at)
|
|
@@ -167267,8 +168601,8 @@ function getSourceContents(db, sessionId, tagIds) {
|
|
|
167267
168601
|
if (tagIds.length === 0) {
|
|
167268
168602
|
return new Map;
|
|
167269
168603
|
}
|
|
167270
|
-
const
|
|
167271
|
-
const rows = db.prepare(`SELECT tag_id, content FROM source_contents WHERE session_id = ? AND tag_id IN (${
|
|
168604
|
+
const placeholders3 = tagIds.map(() => "?").join(", ");
|
|
168605
|
+
const rows = db.prepare(`SELECT tag_id, content FROM source_contents WHERE session_id = ? AND tag_id IN (${placeholders3})`).all(sessionId, ...tagIds).filter(isSourceContentRow);
|
|
167272
168606
|
const sources = new Map;
|
|
167273
168607
|
for (const row of rows) {
|
|
167274
168608
|
sources.set(row.tag_id, row.content);
|
|
@@ -167359,15 +168693,22 @@ function ownerMessageIdForTagRow(row) {
|
|
|
167359
168693
|
}
|
|
167360
168694
|
return row.message_id.replace(CONTENT_ID_SUFFIX, "");
|
|
167361
168695
|
}
|
|
167362
|
-
function getActiveTagTokenAggregate(db, sessionId) {
|
|
167363
|
-
const
|
|
168696
|
+
function getActiveTagTokenAggregate(db, sessionId, protectedTags = 0) {
|
|
168697
|
+
const toolOutputExpr = protectedTags > 0 ? `COALESCE(SUM(CASE WHEN type = 'tool' AND tag_number < (
|
|
168698
|
+
SELECT tag_number FROM tags
|
|
168699
|
+
WHERE session_id = ? AND status = 'active'
|
|
168700
|
+
ORDER BY tag_number DESC LIMIT 1 OFFSET ?
|
|
168701
|
+
) THEN COALESCE(token_count, 0) ELSE 0 END), 0)` : `COALESCE(SUM(CASE WHEN type = 'tool' THEN COALESCE(token_count, 0) ELSE 0 END), 0)`;
|
|
168702
|
+
const sql = `SELECT
|
|
167364
168703
|
COALESCE(SUM(CASE WHEN type != 'tool' THEN COALESCE(token_count, 0) ELSE 0 END), 0)
|
|
167365
168704
|
+ COALESCE(SUM(COALESCE(reasoning_token_count, 0)), 0) AS conversation,
|
|
167366
168705
|
COALESCE(SUM(CASE WHEN type = 'tool' THEN COALESCE(token_count, 0) + COALESCE(input_token_count, 0) ELSE 0 END), 0) AS tool_call,
|
|
167367
|
-
|
|
168706
|
+
${toolOutputExpr} AS tool_output,
|
|
167368
168707
|
COALESCE(SUM(CASE WHEN token_count IS NULL THEN 1 ELSE 0 END), 0) AS null_count
|
|
167369
168708
|
FROM tags
|
|
167370
|
-
WHERE session_id = ? AND status = 'active'
|
|
168709
|
+
WHERE session_id = ? AND status = 'active'`;
|
|
168710
|
+
const params = protectedTags > 0 ? [sessionId, protectedTags - 1, sessionId] : [sessionId];
|
|
168711
|
+
const row = db.prepare(sql).get(...params);
|
|
167371
168712
|
return {
|
|
167372
168713
|
conversation: row?.conversation ?? 0,
|
|
167373
168714
|
toolCall: row?.tool_call ?? 0,
|
|
@@ -167558,8 +168899,8 @@ function getTagsByNumbers(db, sessionId, tagNumbers) {
|
|
|
167558
168899
|
}
|
|
167559
168900
|
return all;
|
|
167560
168901
|
}
|
|
167561
|
-
const
|
|
167562
|
-
const rows = db.prepare(`SELECT ${TAG_SELECT_COLUMNS} FROM tags WHERE session_id = ? AND tag_number IN (${
|
|
168902
|
+
const placeholders3 = tagNumbers.map(() => "?").join(",");
|
|
168903
|
+
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);
|
|
167563
168904
|
return rows.map(toTagEntry);
|
|
167564
168905
|
}
|
|
167565
168906
|
var getToolTagNumberByOwnerStatements = new WeakMap;
|
|
@@ -167749,6 +169090,23 @@ function createCtxMemoryTool(deps) {
|
|
|
167749
169090
|
}
|
|
167750
169091
|
const projectIdentity = resolveProjectIdentity(ctx.cwd);
|
|
167751
169092
|
await deps.ensureProjectRegistered?.(ctx.cwd, deps.db);
|
|
169093
|
+
const workspaceIdentitySet = resolveWorkspaceIdentitySet(deps.db, projectIdentity);
|
|
169094
|
+
const expandedWorkspace = expandWorkspaceIdentitySetWithAliases(deps.db, workspaceIdentitySet.identities);
|
|
169095
|
+
const workspaceVisibleIdentities = workspaceIdentitySet.identities.length > 1 ? expandedWorkspace.expandedIdentities : workspaceIdentitySet.identities;
|
|
169096
|
+
const targetIdentityForStoredPath = (rawProjectPath) => workspaceIdentitySet.identities.length > 1 ? resolveStoredPathWorkspaceIdentity(rawProjectPath, workspaceIdentitySet.identities, expandedWorkspace.canonicalIdentityByStoredPath) ?? normalizeStoredProjectPath(rawProjectPath) : normalizeStoredProjectPath(rawProjectPath);
|
|
169097
|
+
const toolShareCategories = workspaceIdentitySet.identities.length > 1 ? resolveWorkspaceShareCategories(deps.db, projectIdentity) : null;
|
|
169098
|
+
const memoryVisibleToTool = (memory2) => {
|
|
169099
|
+
if (workspaceIdentitySet.identities.length <= 1) {
|
|
169100
|
+
return storedPathBelongsToIdentity(memory2.projectPath, projectIdentity);
|
|
169101
|
+
}
|
|
169102
|
+
if (!storedPathBelongsToWorkspace(memory2.projectPath, workspaceIdentitySet.identities, workspaceVisibleIdentities, expandedWorkspace.canonicalIdentityByStoredPath)) {
|
|
169103
|
+
return false;
|
|
169104
|
+
}
|
|
169105
|
+
const isOwn = targetIdentityForStoredPath(memory2.projectPath) === projectIdentity;
|
|
169106
|
+
if (isOwn)
|
|
169107
|
+
return true;
|
|
169108
|
+
return toolShareCategories === null || toolShareCategories.includes(memory2.category);
|
|
169109
|
+
};
|
|
167752
169110
|
const snapshot = getProjectEmbeddingSnapshot(projectIdentity);
|
|
167753
169111
|
if (snapshot ? !snapshot.features.memoryEnabled : deps.memoryEnabled === false) {
|
|
167754
169112
|
return err2("Cross-session memory is disabled for this project.");
|
|
@@ -167795,28 +169153,34 @@ function createCtxMemoryTool(deps) {
|
|
|
167795
169153
|
return err2("Error: 'content' is required when action is 'update'.");
|
|
167796
169154
|
}
|
|
167797
169155
|
const memory2 = getMemoryById(deps.db, updateId);
|
|
167798
|
-
if (!memory2 || !
|
|
169156
|
+
if (!memory2 || !memoryVisibleToTool(memory2)) {
|
|
167799
169157
|
return err2(`Error: Memory with ID ${updateId} was not found.`);
|
|
167800
169158
|
}
|
|
167801
169159
|
if (!dreamerAllowed && !isPrimaryMutableMemory(memory2)) {
|
|
167802
169160
|
return err2(inactiveMemoryError(updateId, "updating"));
|
|
167803
169161
|
}
|
|
167804
169162
|
const normalizedHash = computeNormalizedHash(content);
|
|
167805
|
-
const
|
|
169163
|
+
const targetIdentity = targetIdentityForStoredPath(memory2.projectPath);
|
|
169164
|
+
const duplicate = getMemoryByHash(deps.db, targetIdentity, memory2.category, normalizedHash);
|
|
167806
169165
|
if (duplicate && duplicate.id !== memory2.id) {
|
|
167807
169166
|
return err2(`Error: Memory content already exists as ID ${duplicate.id}; merge or archive duplicates instead.`);
|
|
167808
169167
|
}
|
|
167809
169168
|
deps.db.transaction(() => {
|
|
167810
169169
|
updateMemoryContent(deps.db, memory2.id, content, normalizedHash);
|
|
167811
169170
|
queueMemoryMutation(deps.db, {
|
|
167812
|
-
projectPath:
|
|
169171
|
+
projectPath: targetIdentity,
|
|
167813
169172
|
mutationType: "update",
|
|
167814
169173
|
targetMemoryId: memory2.id,
|
|
167815
169174
|
category: memory2.category,
|
|
167816
169175
|
newContent: content
|
|
167817
169176
|
});
|
|
167818
169177
|
})();
|
|
167819
|
-
queueEmbedding({
|
|
169178
|
+
queueEmbedding({
|
|
169179
|
+
deps,
|
|
169180
|
+
projectIdentity: targetIdentity,
|
|
169181
|
+
memoryId: memory2.id,
|
|
169182
|
+
content
|
|
169183
|
+
});
|
|
167820
169184
|
return ok2(`Updated memory [ID: ${memory2.id}] in ${memory2.category}.`);
|
|
167821
169185
|
}
|
|
167822
169186
|
if (params.action === "merge") {
|
|
@@ -167836,7 +169200,7 @@ function createCtxMemoryTool(deps) {
|
|
|
167836
169200
|
return err2("Error: One or more source memories were not found.");
|
|
167837
169201
|
}
|
|
167838
169202
|
if (!dreamerAllowed) {
|
|
167839
|
-
const foreign = sourceMemories.find((memory2) => !
|
|
169203
|
+
const foreign = sourceMemories.find((memory2) => !memoryVisibleToTool(memory2));
|
|
167840
169204
|
if (foreign) {
|
|
167841
169205
|
return err2(`Error: Memory with ID ${foreign.id} was not found.`);
|
|
167842
169206
|
}
|
|
@@ -167925,26 +169289,36 @@ function createCtxMemoryTool(deps) {
|
|
|
167925
169289
|
return ok2(`Merged memories [${ids.join(", ")}] into canonical memory [ID: ${canonicalMemory.id}] in ${category}; superseded [${supersededIds.join(", ")}].`);
|
|
167926
169290
|
}
|
|
167927
169291
|
if (params.action === "archive") {
|
|
167928
|
-
const
|
|
167929
|
-
if (!
|
|
169292
|
+
const rawArchiveIds = params.ids;
|
|
169293
|
+
if (!rawArchiveIds || rawArchiveIds.length === 0 || !rawArchiveIds.every(Number.isInteger)) {
|
|
167930
169294
|
return err2("Error: 'ids' must contain at least one integer memory ID when action is 'archive'.");
|
|
167931
169295
|
}
|
|
169296
|
+
const archiveIds = [...new Set(rawArchiveIds)];
|
|
167932
169297
|
for (const memoryId of archiveIds) {
|
|
167933
169298
|
const memory2 = getMemoryById(deps.db, memoryId);
|
|
167934
|
-
if (!memory2 || !
|
|
169299
|
+
if (!memory2 || !memoryVisibleToTool(memory2)) {
|
|
167935
169300
|
return err2(`Error: Memory with ID ${memoryId} was not found.`);
|
|
167936
169301
|
}
|
|
167937
169302
|
if (!dreamerAllowed && !isPrimaryMutableMemory(memory2)) {
|
|
167938
169303
|
return err2(inactiveMemoryError(memoryId, "archiving"));
|
|
167939
169304
|
}
|
|
167940
169305
|
}
|
|
169306
|
+
const targets = archiveIds.map((memoryId) => {
|
|
169307
|
+
const memory2 = getMemoryById(deps.db, memoryId);
|
|
169308
|
+
if (!memory2)
|
|
169309
|
+
throw new Error(`validated memory ${memoryId} disappeared`);
|
|
169310
|
+
return {
|
|
169311
|
+
memoryId,
|
|
169312
|
+
projectIdentity: targetIdentityForStoredPath(memory2.projectPath)
|
|
169313
|
+
};
|
|
169314
|
+
});
|
|
167941
169315
|
deps.db.transaction(() => {
|
|
167942
|
-
for (const
|
|
167943
|
-
archiveMemory(deps.db, memoryId, params.reason);
|
|
169316
|
+
for (const target2 of targets) {
|
|
169317
|
+
archiveMemory(deps.db, target2.memoryId, params.reason);
|
|
167944
169318
|
queueMemoryMutation(deps.db, {
|
|
167945
|
-
projectPath: projectIdentity,
|
|
169319
|
+
projectPath: target2.projectIdentity,
|
|
167946
169320
|
mutationType: "archive",
|
|
167947
|
-
targetMemoryId: memoryId
|
|
169321
|
+
targetMemoryId: target2.memoryId
|
|
167948
169322
|
});
|
|
167949
169323
|
}
|
|
167950
169324
|
})();
|
|
@@ -168916,6 +170290,37 @@ function previewText(text) {
|
|
|
168916
170290
|
}
|
|
168917
170291
|
return `${normalized.slice(0, RESULT_PREVIEW_LIMIT - 1).trimEnd()}…`;
|
|
168918
170292
|
}
|
|
170293
|
+
function resolveSearchWorkspaceContext(db, projectPath, identitySet) {
|
|
170294
|
+
const resolved = identitySet ?? resolveWorkspaceIdentitySet(db, projectPath);
|
|
170295
|
+
const isWorkspaced = resolved.identities.length > 1;
|
|
170296
|
+
const expanded = expandWorkspaceIdentitySetWithAliases(db, resolved.identities);
|
|
170297
|
+
const expandedIdentities = isWorkspaced ? expanded.expandedIdentities : resolved.identities;
|
|
170298
|
+
const canonicalIdentityByStoredPath = isWorkspaced ? expanded.canonicalIdentityByStoredPath : new Map(resolved.identities.map((identity) => [identity, identity]));
|
|
170299
|
+
const ownIdentities = expandedIdentities.filter((identity) => canonicalIdentityByStoredPath.get(identity) === projectPath);
|
|
170300
|
+
return {
|
|
170301
|
+
identities: resolved.identities,
|
|
170302
|
+
expandedIdentities,
|
|
170303
|
+
ownIdentities,
|
|
170304
|
+
shareCategories: isWorkspaced ? resolveWorkspaceShareCategories(db, projectPath) : null,
|
|
170305
|
+
namesByIdentity: resolved.namesByIdentity,
|
|
170306
|
+
canonicalIdentityByStoredPath,
|
|
170307
|
+
isWorkspaced
|
|
170308
|
+
};
|
|
170309
|
+
}
|
|
170310
|
+
function memoryWorkspaceIdentity(memory2, workspace) {
|
|
170311
|
+
return resolveStoredPathWorkspaceIdentity(memory2.projectPath, workspace.identities, workspace.canonicalIdentityByStoredPath);
|
|
170312
|
+
}
|
|
170313
|
+
function sourceNamesForSearchMemories(args) {
|
|
170314
|
+
if (!args.workspace.isWorkspaced)
|
|
170315
|
+
return;
|
|
170316
|
+
const sourceNames = new Map;
|
|
170317
|
+
for (const memory2 of args.memories) {
|
|
170318
|
+
const source = sourceNameForMemory(memory2.projectPath, args.projectPath, args.workspace.identities, args.workspace.namesByIdentity, args.workspace.canonicalIdentityByStoredPath);
|
|
170319
|
+
if (source)
|
|
170320
|
+
sourceNames.set(memory2.id, source);
|
|
170321
|
+
}
|
|
170322
|
+
return sourceNames.size > 0 ? sourceNames : undefined;
|
|
170323
|
+
}
|
|
168919
170324
|
function getMessageSearchStatement(db) {
|
|
168920
170325
|
let stmt = messageSearchStatements.get(db);
|
|
168921
170326
|
if (!stmt) {
|
|
@@ -168924,6 +170329,30 @@ function getMessageSearchStatement(db) {
|
|
|
168924
170329
|
}
|
|
168925
170330
|
return stmt;
|
|
168926
170331
|
}
|
|
170332
|
+
var ftsRowCountStatements = new WeakMap;
|
|
170333
|
+
var ftsMatchCountStatements = new WeakMap;
|
|
170334
|
+
function getSessionFtsRowCount(db, sessionId) {
|
|
170335
|
+
let stmt = ftsRowCountStatements.get(db);
|
|
170336
|
+
if (!stmt) {
|
|
170337
|
+
stmt = db.prepare("SELECT COUNT(*) AS n FROM message_history_fts WHERE session_id = ?");
|
|
170338
|
+
ftsRowCountStatements.set(db, stmt);
|
|
170339
|
+
}
|
|
170340
|
+
const row = stmt.get(sessionId);
|
|
170341
|
+
return typeof row?.n === "number" ? row.n : 0;
|
|
170342
|
+
}
|
|
170343
|
+
function countSessionFtsMatches(db, sessionId, ftsQuery) {
|
|
170344
|
+
let stmt = ftsMatchCountStatements.get(db);
|
|
170345
|
+
if (!stmt) {
|
|
170346
|
+
stmt = db.prepare("SELECT COUNT(*) AS n FROM message_history_fts WHERE session_id = ? AND message_history_fts MATCH ?");
|
|
170347
|
+
ftsMatchCountStatements.set(db, stmt);
|
|
170348
|
+
}
|
|
170349
|
+
try {
|
|
170350
|
+
const row = stmt.get(sessionId, ftsQuery);
|
|
170351
|
+
return typeof row?.n === "number" ? row.n : 0;
|
|
170352
|
+
} catch {
|
|
170353
|
+
return 0;
|
|
170354
|
+
}
|
|
170355
|
+
}
|
|
168927
170356
|
function getMessageOrdinal(value) {
|
|
168928
170357
|
if (typeof value === "number" && Number.isFinite(value)) {
|
|
168929
170358
|
return value;
|
|
@@ -168939,25 +170368,63 @@ async function getSemanticScores(args) {
|
|
|
168939
170368
|
if (!args.queryEmbedding || args.memories.length === 0) {
|
|
168940
170369
|
return semanticScores;
|
|
168941
170370
|
}
|
|
168942
|
-
|
|
168943
|
-
|
|
168944
|
-
|
|
168945
|
-
|
|
168946
|
-
|
|
168947
|
-
|
|
168948
|
-
|
|
170371
|
+
if (!args.workspace?.isWorkspaced) {
|
|
170372
|
+
const cachedEmbeddings = getProjectEmbeddings(args.db, args.projectPath);
|
|
170373
|
+
const embeddings = await ensureMemoryEmbeddings({
|
|
170374
|
+
db: args.db,
|
|
170375
|
+
projectIdentity: args.projectPath,
|
|
170376
|
+
memories: args.memories,
|
|
170377
|
+
existingEmbeddings: cachedEmbeddings
|
|
170378
|
+
});
|
|
170379
|
+
for (const memory2 of args.memories) {
|
|
170380
|
+
const memoryEmbedding = embeddings.get(memory2.id);
|
|
170381
|
+
if (!memoryEmbedding) {
|
|
170382
|
+
continue;
|
|
170383
|
+
}
|
|
170384
|
+
semanticScores.set(memory2.id, normalizeCosineScore(cosineSimilarity(args.queryEmbedding, memoryEmbedding.embedding)));
|
|
170385
|
+
}
|
|
170386
|
+
return semanticScores;
|
|
170387
|
+
}
|
|
170388
|
+
if (!args.queryModelId || args.queryModelId === "off") {
|
|
170389
|
+
return semanticScores;
|
|
170390
|
+
}
|
|
170391
|
+
const workspace = args.workspace;
|
|
170392
|
+
const memoriesByIdentity = new Map;
|
|
168949
170393
|
for (const memory2 of args.memories) {
|
|
168950
|
-
const
|
|
168951
|
-
if (!
|
|
170394
|
+
const identity = memoryWorkspaceIdentity(memory2, workspace);
|
|
170395
|
+
if (!identity)
|
|
168952
170396
|
continue;
|
|
170397
|
+
const list = memoriesByIdentity.get(identity) ?? [];
|
|
170398
|
+
list.push(memory2);
|
|
170399
|
+
memoriesByIdentity.set(identity, list);
|
|
170400
|
+
}
|
|
170401
|
+
const ownMemories = memoriesByIdentity.get(args.projectPath) ?? [];
|
|
170402
|
+
if (ownMemories.length > 0) {
|
|
170403
|
+
const ownEmbeddings = getProjectEmbeddings(args.db, args.projectPath);
|
|
170404
|
+
await ensureMemoryEmbeddings({
|
|
170405
|
+
db: args.db,
|
|
170406
|
+
projectIdentity: args.projectPath,
|
|
170407
|
+
memories: ownMemories,
|
|
170408
|
+
existingEmbeddings: ownEmbeddings
|
|
170409
|
+
});
|
|
170410
|
+
}
|
|
170411
|
+
for (const identity of workspace.identities) {
|
|
170412
|
+
const memberMemories = memoriesByIdentity.get(identity) ?? [];
|
|
170413
|
+
if (memberMemories.length === 0)
|
|
170414
|
+
continue;
|
|
170415
|
+
const cachedEmbeddings = getProjectEmbeddings(args.db, identity);
|
|
170416
|
+
for (const memory2 of memberMemories) {
|
|
170417
|
+
const memoryEmbedding = cachedEmbeddings.get(memory2.id);
|
|
170418
|
+
if (!memoryEmbedding || memoryEmbedding.modelId !== args.queryModelId)
|
|
170419
|
+
continue;
|
|
170420
|
+
semanticScores.set(memory2.id, normalizeCosineScore(cosineSimilarity(args.queryEmbedding, memoryEmbedding.embedding)));
|
|
168953
170421
|
}
|
|
168954
|
-
semanticScores.set(memory2.id, normalizeCosineScore(cosineSimilarity(args.queryEmbedding, memoryEmbedding)));
|
|
168955
170422
|
}
|
|
168956
170423
|
return semanticScores;
|
|
168957
170424
|
}
|
|
168958
170425
|
function getFtsMatches(args) {
|
|
168959
170426
|
try {
|
|
168960
|
-
return searchMemoriesFTS(args.db, args.projectPath, args.query, args.limit);
|
|
170427
|
+
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);
|
|
168961
170428
|
} catch (error51) {
|
|
168962
170429
|
log(`[search] FTS query failed for "${args.query}": ${error51 instanceof Error ? error51.message : String(error51)}`);
|
|
168963
170430
|
return [];
|
|
@@ -168971,8 +170438,11 @@ function selectSemanticCandidates(args) {
|
|
|
168971
170438
|
return args.memories;
|
|
168972
170439
|
}
|
|
168973
170440
|
const candidateIds = new Set(args.ftsMatches.map((memory2) => memory2.id));
|
|
168974
|
-
const
|
|
168975
|
-
|
|
170441
|
+
const embeddingProjects = args.workspace?.isWorkspaced ? args.workspace.identities : [args.projectPath];
|
|
170442
|
+
for (const projectPath of embeddingProjects) {
|
|
170443
|
+
const cachedEmbeddings = peekProjectEmbeddings(projectPath);
|
|
170444
|
+
if (!cachedEmbeddings)
|
|
170445
|
+
continue;
|
|
168976
170446
|
for (const memoryId of cachedEmbeddings.keys()) {
|
|
168977
170447
|
candidateIds.add(memoryId);
|
|
168978
170448
|
}
|
|
@@ -169014,7 +170484,8 @@ function mergeMemoryResults(args) {
|
|
|
169014
170484
|
score,
|
|
169015
170485
|
memoryId: memory2.id,
|
|
169016
170486
|
category: memory2.category,
|
|
169017
|
-
matchType
|
|
170487
|
+
matchType,
|
|
170488
|
+
sourceName: args.sourceNameByMemoryId?.get(memory2.id)
|
|
169018
170489
|
});
|
|
169019
170490
|
}
|
|
169020
170491
|
return results.sort((left, right) => {
|
|
@@ -169028,7 +170499,7 @@ async function searchMemories(args) {
|
|
|
169028
170499
|
if (!args.memoryEnabled) {
|
|
169029
170500
|
return [];
|
|
169030
170501
|
}
|
|
169031
|
-
const memories = getMemoriesByProject(args.db, args.projectPath);
|
|
170502
|
+
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);
|
|
169032
170503
|
if (memories.length === 0) {
|
|
169033
170504
|
return [];
|
|
169034
170505
|
}
|
|
@@ -169036,26 +170507,43 @@ async function searchMemories(args) {
|
|
|
169036
170507
|
db: args.db,
|
|
169037
170508
|
projectPath: args.projectPath,
|
|
169038
170509
|
query: args.query,
|
|
169039
|
-
limit: FTS_SEMANTIC_CANDIDATE_LIMIT
|
|
170510
|
+
limit: FTS_SEMANTIC_CANDIDATE_LIMIT,
|
|
170511
|
+
workspace: args.workspace
|
|
169040
170512
|
});
|
|
169041
170513
|
const ftsScores = getFtsScores(ftsMatches);
|
|
169042
170514
|
const semanticCandidates = selectSemanticCandidates({
|
|
169043
170515
|
memories,
|
|
169044
170516
|
projectPath: args.projectPath,
|
|
169045
|
-
ftsMatches
|
|
170517
|
+
ftsMatches,
|
|
170518
|
+
workspace: args.workspace
|
|
169046
170519
|
});
|
|
169047
170520
|
const semanticScores = await getSemanticScores({
|
|
169048
170521
|
db: args.db,
|
|
169049
170522
|
projectPath: args.projectPath,
|
|
169050
170523
|
memories: semanticCandidates,
|
|
169051
|
-
queryEmbedding: args.queryEmbedding
|
|
170524
|
+
queryEmbedding: args.queryEmbedding,
|
|
170525
|
+
queryModelId: args.queryModelId,
|
|
170526
|
+
workspace: args.workspace
|
|
169052
170527
|
});
|
|
169053
170528
|
return mergeMemoryResults({
|
|
169054
170529
|
memories,
|
|
169055
170530
|
semanticScores,
|
|
169056
170531
|
ftsScores,
|
|
169057
170532
|
limit: args.limit,
|
|
169058
|
-
visibleMemoryIds: args.visibleMemoryIds
|
|
170533
|
+
visibleMemoryIds: args.visibleMemoryIds,
|
|
170534
|
+
sourceNameByMemoryId: sourceNamesForSearchMemories({
|
|
170535
|
+
memories,
|
|
170536
|
+
projectPath: args.projectPath,
|
|
170537
|
+
workspace: args.workspace ?? {
|
|
170538
|
+
identities: [args.projectPath],
|
|
170539
|
+
expandedIdentities: [args.projectPath],
|
|
170540
|
+
namesByIdentity: new Map,
|
|
170541
|
+
canonicalIdentityByStoredPath: new Map([[args.projectPath, args.projectPath]]),
|
|
170542
|
+
ownIdentities: [args.projectPath],
|
|
170543
|
+
shareCategories: null,
|
|
170544
|
+
isWorkspaced: false
|
|
170545
|
+
}
|
|
170546
|
+
})
|
|
169059
170547
|
});
|
|
169060
170548
|
}
|
|
169061
170549
|
function linearDecayScore(rank, total) {
|
|
@@ -169086,7 +170574,13 @@ function runMessageFtsQuery(db, sessionId, ftsQuery, fetchLimit, cutoff) {
|
|
|
169086
170574
|
return result;
|
|
169087
170575
|
}
|
|
169088
170576
|
var RRF_K = 60;
|
|
169089
|
-
var
|
|
170577
|
+
var VERBATIM_RANK_BONUS = 1 / RRF_K;
|
|
170578
|
+
var IDF_FALLOFF = 100;
|
|
170579
|
+
function probeDiscriminationWeight(df, corpusSize) {
|
|
170580
|
+
if (corpusSize <= 0 || df <= 0)
|
|
170581
|
+
return 1;
|
|
170582
|
+
return 1 / (1 + IDF_FALLOFF * df / corpusSize);
|
|
170583
|
+
}
|
|
169090
170584
|
function searchMessages(args) {
|
|
169091
170585
|
const cutoff = args.maxOrdinal != null && args.maxOrdinal >= 0 ? args.maxOrdinal : null;
|
|
169092
170586
|
const fetchLimit = args.maxOrdinal != null && args.maxOrdinal >= 0 ? args.limit * 3 : args.limit;
|
|
@@ -169103,20 +170597,31 @@ function searchMessages(args) {
|
|
|
169103
170597
|
role: row.role
|
|
169104
170598
|
}));
|
|
169105
170599
|
}
|
|
170600
|
+
const corpusSize = getSessionFtsRowCount(args.db, args.sessionId);
|
|
169106
170601
|
const queryLists = [];
|
|
169107
170602
|
if (baseQuery.length > 0) {
|
|
169108
|
-
queryLists.push(
|
|
170603
|
+
queryLists.push({
|
|
170604
|
+
rows: runMessageFtsQuery(args.db, args.sessionId, baseQuery, fetchLimit, cutoff),
|
|
170605
|
+
weight: 1
|
|
170606
|
+
});
|
|
169109
170607
|
}
|
|
170608
|
+
const probeWeights = new Map;
|
|
169110
170609
|
for (const probe of probes) {
|
|
169111
170610
|
const probeQuery = sanitizeFtsQuery(probe);
|
|
169112
170611
|
if (probeQuery.length === 0)
|
|
169113
170612
|
continue;
|
|
169114
|
-
|
|
170613
|
+
const df = countSessionFtsMatches(args.db, args.sessionId, probeQuery);
|
|
170614
|
+
const weight = probeDiscriminationWeight(df, corpusSize);
|
|
170615
|
+
probeWeights.set(probe, weight);
|
|
170616
|
+
queryLists.push({
|
|
170617
|
+
rows: runMessageFtsQuery(args.db, args.sessionId, probeQuery, fetchLimit, cutoff),
|
|
170618
|
+
weight
|
|
170619
|
+
});
|
|
169115
170620
|
}
|
|
169116
170621
|
const fused = new Map;
|
|
169117
170622
|
for (const list of queryLists) {
|
|
169118
|
-
list.forEach((row, rank) => {
|
|
169119
|
-
const rrf =
|
|
170623
|
+
list.rows.forEach((row, rank) => {
|
|
170624
|
+
const rrf = list.weight / (RRF_K + rank);
|
|
169120
170625
|
const existing = fused.get(row.messageId);
|
|
169121
170626
|
if (existing) {
|
|
169122
170627
|
existing.score += rrf;
|
|
@@ -169126,26 +170631,107 @@ function searchMessages(args) {
|
|
|
169126
170631
|
});
|
|
169127
170632
|
}
|
|
169128
170633
|
for (const entry of fused.values()) {
|
|
169129
|
-
|
|
169130
|
-
|
|
170634
|
+
let best = 0;
|
|
170635
|
+
for (const probe of probes) {
|
|
170636
|
+
const weight = probeWeights.get(probe) ?? 0;
|
|
170637
|
+
if (weight > best && containsProbeVerbatim(entry.row.content, [probe])) {
|
|
170638
|
+
best = weight;
|
|
170639
|
+
}
|
|
170640
|
+
}
|
|
170641
|
+
if (best > 0) {
|
|
170642
|
+
entry.score += best * VERBATIM_RANK_BONUS;
|
|
169131
170643
|
}
|
|
169132
170644
|
}
|
|
169133
170645
|
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);
|
|
169134
|
-
|
|
169135
|
-
return ranked.map((entry) => ({
|
|
170646
|
+
return ranked.map((entry, rank) => ({
|
|
169136
170647
|
source: "message",
|
|
169137
170648
|
content: previewText(entry.row.content),
|
|
169138
|
-
score:
|
|
170649
|
+
score: linearDecayScore(rank, ranked.length),
|
|
169139
170650
|
messageOrdinal: entry.row.messageOrdinal,
|
|
169140
170651
|
messageId: entry.row.messageId,
|
|
169141
170652
|
role: entry.row.role
|
|
169142
170653
|
}));
|
|
169143
170654
|
}
|
|
170655
|
+
function searchCompartmentChunks(args) {
|
|
170656
|
+
if (!args.queryEmbedding || args.limit <= 0)
|
|
170657
|
+
return [];
|
|
170658
|
+
const cutoff = args.maxOrdinal != null && args.maxOrdinal >= 0 ? args.maxOrdinal : null;
|
|
170659
|
+
const rows = loadCompartmentChunkEmbeddingsForSearch(args.db, args.sessionId, args.projectPath, args.modelId);
|
|
170660
|
+
if (rows.length === 0)
|
|
170661
|
+
return [];
|
|
170662
|
+
const byCompartment = new Map;
|
|
170663
|
+
for (const row of rows) {
|
|
170664
|
+
if (cutoff !== null && row.endOrdinal > cutoff) {
|
|
170665
|
+
continue;
|
|
170666
|
+
}
|
|
170667
|
+
const score = normalizeCosineScore(cosineSimilarity(args.queryEmbedding, row.vector));
|
|
170668
|
+
if (score <= 0)
|
|
170669
|
+
continue;
|
|
170670
|
+
const existing = byCompartment.get(row.compartmentId);
|
|
170671
|
+
if (!existing || score > existing.score) {
|
|
170672
|
+
byCompartment.set(row.compartmentId, { row, score });
|
|
170673
|
+
}
|
|
170674
|
+
}
|
|
170675
|
+
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 }) => ({
|
|
170676
|
+
source: "compartment",
|
|
170677
|
+
content: previewText(row.title),
|
|
170678
|
+
score: score * SINGLE_SOURCE_PENALTY,
|
|
170679
|
+
compartmentId: row.compartmentId,
|
|
170680
|
+
sessionId: row.sessionId,
|
|
170681
|
+
title: row.title,
|
|
170682
|
+
startOrdinal: row.startOrdinal,
|
|
170683
|
+
endOrdinal: row.endOrdinal,
|
|
170684
|
+
matchType: "semantic"
|
|
170685
|
+
}));
|
|
170686
|
+
}
|
|
170687
|
+
function mergeMessageAndCompartmentResults(args) {
|
|
170688
|
+
if (args.compartments.length === 0)
|
|
170689
|
+
return args.messages;
|
|
170690
|
+
if (args.messages.length === 0)
|
|
170691
|
+
return args.compartments;
|
|
170692
|
+
const fused = new Map;
|
|
170693
|
+
const add = (key, result, score, tieOrdinal) => {
|
|
170694
|
+
const existing = fused.get(key);
|
|
170695
|
+
if (existing) {
|
|
170696
|
+
existing.score += score;
|
|
170697
|
+
return existing;
|
|
170698
|
+
}
|
|
170699
|
+
const entry = { result, score, tieOrdinal, snippetScore: -1 };
|
|
170700
|
+
fused.set(key, entry);
|
|
170701
|
+
return entry;
|
|
170702
|
+
};
|
|
170703
|
+
args.compartments.forEach((compartment, rank) => {
|
|
170704
|
+
add(`compartment:${compartment.compartmentId}`, compartment, 1 / (RRF_K + rank), compartment.startOrdinal);
|
|
170705
|
+
});
|
|
170706
|
+
for (const [rank, message] of args.messages.entries()) {
|
|
170707
|
+
const containing = args.compartments.find((compartment) => message.messageOrdinal >= compartment.startOrdinal && message.messageOrdinal <= compartment.endOrdinal);
|
|
170708
|
+
const contribution = 1 / (RRF_K + rank);
|
|
170709
|
+
if (!containing) {
|
|
170710
|
+
add(`message:${message.messageId}`, message, contribution, message.messageOrdinal);
|
|
170711
|
+
continue;
|
|
170712
|
+
}
|
|
170713
|
+
const entry = add(`compartment:${containing.compartmentId}`, containing, contribution, containing.startOrdinal);
|
|
170714
|
+
if (message.score > entry.snippetScore && entry.result.source === "compartment") {
|
|
170715
|
+
entry.snippetScore = message.score;
|
|
170716
|
+
entry.result = {
|
|
170717
|
+
...entry.result,
|
|
170718
|
+
matchType: "hybrid",
|
|
170719
|
+
snippet: message.content
|
|
170720
|
+
};
|
|
170721
|
+
}
|
|
170722
|
+
}
|
|
170723
|
+
const ranked = [...fused.values()].sort((left, right) => right.score !== left.score ? right.score - left.score : left.tieOrdinal - right.tieOrdinal).slice(0, args.limit);
|
|
170724
|
+
return ranked.map((entry, rank) => ({
|
|
170725
|
+
...entry.result,
|
|
170726
|
+
score: linearDecayScore(rank, ranked.length)
|
|
170727
|
+
}));
|
|
170728
|
+
}
|
|
169144
170729
|
function getSourceBoost(result) {
|
|
169145
170730
|
switch (result.source) {
|
|
169146
170731
|
case "memory":
|
|
169147
170732
|
return MEMORY_SOURCE_BOOST;
|
|
169148
170733
|
case "message":
|
|
170734
|
+
case "compartment":
|
|
169149
170735
|
return MESSAGE_SOURCE_BOOST;
|
|
169150
170736
|
case "git_commit":
|
|
169151
170737
|
return GIT_COMMIT_SOURCE_BOOST;
|
|
@@ -169163,6 +170749,9 @@ function compareUnifiedResults(left, right) {
|
|
|
169163
170749
|
if (left.source === "message" && right.source === "message") {
|
|
169164
170750
|
return left.messageOrdinal - right.messageOrdinal;
|
|
169165
170751
|
}
|
|
170752
|
+
if (left.source === "compartment" && right.source === "compartment") {
|
|
170753
|
+
return left.startOrdinal - right.startOrdinal;
|
|
170754
|
+
}
|
|
169166
170755
|
if (left.source === "git_commit" && right.source === "git_commit") {
|
|
169167
170756
|
return right.committedAtMs - left.committedAtMs;
|
|
169168
170757
|
}
|
|
@@ -169213,10 +170802,12 @@ async function unifiedSearch(db, sessionId, projectPath, query, options3 = {}) {
|
|
|
169213
170802
|
const isEmbeddingRuntimeEnabled = options3.isEmbeddingRuntimeEnabled ?? isEmbeddingEnabled;
|
|
169214
170803
|
const gitCommitsEnabled = options3.gitCommitsEnabled ?? false;
|
|
169215
170804
|
const activeSources = resolveSources(options3.sources);
|
|
169216
|
-
const
|
|
170805
|
+
const memoryFeatureEnabled = options3.memoryEnabled ?? true;
|
|
170806
|
+
const runMemory = activeSources.has("memory") && memoryFeatureEnabled;
|
|
169217
170807
|
const runMessages = activeSources.has("message");
|
|
169218
170808
|
const runGitCommits = activeSources.has("git_commit") && gitCommitsEnabled;
|
|
169219
|
-
const
|
|
170809
|
+
const runCompartmentChunks = runMessages && memoryFeatureEnabled && embeddingEnabled;
|
|
170810
|
+
const needsEmbedding = (runMemory || runGitCommits || runCompartmentChunks) && embeddingEnabled && isEmbeddingRuntimeEnabled();
|
|
169220
170811
|
const queryEmbeddingPromise = needsEmbedding ? embedQuery(trimmedQuery, options3.signal).catch((error51) => {
|
|
169221
170812
|
log(`[search] query embedding failed: ${error51 instanceof Error ? error51.message : String(error51)}`);
|
|
169222
170813
|
return null;
|
|
@@ -169232,6 +170823,24 @@ async function unifiedSearch(db, sessionId, projectPath, query, options3 = {}) {
|
|
|
169232
170823
|
probes: messageProbes
|
|
169233
170824
|
}) : [];
|
|
169234
170825
|
const queryEmbedding = await queryEmbeddingPromise;
|
|
170826
|
+
const workspace = resolveSearchWorkspaceContext(db, projectPath);
|
|
170827
|
+
const embeddingSnapshot = getProjectEmbeddingSnapshot(projectPath);
|
|
170828
|
+
const embeddingModelId = embeddingSnapshot?.modelId;
|
|
170829
|
+
const chunkModelId = embeddingSnapshot?.chunkModelId;
|
|
170830
|
+
const compartmentResults = runCompartmentChunks ? searchCompartmentChunks({
|
|
170831
|
+
db,
|
|
170832
|
+
sessionId,
|
|
170833
|
+
projectPath,
|
|
170834
|
+
queryEmbedding,
|
|
170835
|
+
limit: tierLimit,
|
|
170836
|
+
maxOrdinal: options3.maxMessageOrdinal,
|
|
170837
|
+
modelId: chunkModelId && chunkModelId !== "off" ? chunkModelId : null
|
|
170838
|
+
}) : [];
|
|
170839
|
+
const messageLikeResults = mergeMessageAndCompartmentResults({
|
|
170840
|
+
messages: messageResults,
|
|
170841
|
+
compartments: compartmentResults,
|
|
170842
|
+
limit: tierLimit
|
|
170843
|
+
});
|
|
169235
170844
|
const [memoryResults, gitCommitResults] = await Promise.all([
|
|
169236
170845
|
runMemory ? searchMemories({
|
|
169237
170846
|
db,
|
|
@@ -169240,6 +170849,8 @@ async function unifiedSearch(db, sessionId, projectPath, query, options3 = {}) {
|
|
|
169240
170849
|
limit: tierLimit,
|
|
169241
170850
|
memoryEnabled: true,
|
|
169242
170851
|
queryEmbedding,
|
|
170852
|
+
queryModelId: embeddingModelId && embeddingModelId !== "off" ? embeddingModelId : null,
|
|
170853
|
+
workspace,
|
|
169243
170854
|
visibleMemoryIds: options3.visibleMemoryIds
|
|
169244
170855
|
}) : Promise.resolve([]),
|
|
169245
170856
|
runGitCommits ? Promise.resolve(searchGitCommits({
|
|
@@ -169250,7 +170861,7 @@ async function unifiedSearch(db, sessionId, projectPath, query, options3 = {}) {
|
|
|
169250
170861
|
queryEmbedding
|
|
169251
170862
|
})) : Promise.resolve([])
|
|
169252
170863
|
]);
|
|
169253
|
-
const results = [...memoryResults, ...
|
|
170864
|
+
const results = [...memoryResults, ...messageLikeResults, ...gitCommitResults].sort(compareUnifiedResults).slice(0, limit);
|
|
169254
170865
|
const countRetrievals = options3.countRetrievals ?? true;
|
|
169255
170866
|
if (countRetrievals) {
|
|
169256
170867
|
const memoryIds = results.filter((result) => result.source === "memory").map((result) => result.memoryId);
|
|
@@ -169291,8 +170902,8 @@ function getUserMemoryCandidates(db) {
|
|
|
169291
170902
|
function deleteUserMemoryCandidates(db, ids) {
|
|
169292
170903
|
if (ids.length === 0)
|
|
169293
170904
|
return;
|
|
169294
|
-
const
|
|
169295
|
-
db.prepare(`DELETE FROM user_memory_candidates WHERE id IN (${
|
|
170905
|
+
const placeholders3 = ids.map(() => "?").join(",");
|
|
170906
|
+
db.prepare(`DELETE FROM user_memory_candidates WHERE id IN (${placeholders3})`).run(...ids);
|
|
169296
170907
|
}
|
|
169297
170908
|
function insertUserMemory(db, content, sourceCandidateIds) {
|
|
169298
170909
|
const now = Date.now();
|
|
@@ -169844,26 +171455,40 @@ ${sections.join(`
|
|
|
169844
171455
|
var DEFAULT_MEMORY_BUDGET_TOKENS = 8000;
|
|
169845
171456
|
var MEMORY_BLOCK_WRAPPER_TOKENS = 6;
|
|
169846
171457
|
var DEFAULT_USER_PROFILE_BUDGET_TOKENS = 4000;
|
|
171458
|
+
function memoryCanonicalIdentity(memory2, workspace) {
|
|
171459
|
+
return resolveStoredPathWorkspaceIdentity(memory2.projectPath, workspace.identities, workspace.canonicalIdentityByStoredPath);
|
|
171460
|
+
}
|
|
171461
|
+
function memorySelectionOrder(left, right) {
|
|
171462
|
+
if (left.status === "permanent" && right.status !== "permanent")
|
|
171463
|
+
return -1;
|
|
171464
|
+
if (right.status === "permanent" && left.status !== "permanent")
|
|
171465
|
+
return 1;
|
|
171466
|
+
const leftImportance = left.importance ?? Number.NEGATIVE_INFINITY;
|
|
171467
|
+
const rightImportance = right.importance ?? Number.NEGATIVE_INFINITY;
|
|
171468
|
+
const importanceDiff = rightImportance - leftImportance;
|
|
171469
|
+
if (importanceDiff !== 0)
|
|
171470
|
+
return importanceDiff;
|
|
171471
|
+
return left.id - right.id;
|
|
171472
|
+
}
|
|
171473
|
+
function memoryRenderOrder(left, right) {
|
|
171474
|
+
const aPriority = MEMORY_CATEGORY_ORDER_PRIORITY[left.category] ?? MEMORY_CATEGORY_ORDER_UNKNOWN;
|
|
171475
|
+
const bPriority = MEMORY_CATEGORY_ORDER_PRIORITY[right.category] ?? MEMORY_CATEGORY_ORDER_UNKNOWN;
|
|
171476
|
+
const categoryDiff = aPriority - bPriority;
|
|
171477
|
+
if (categoryDiff !== 0)
|
|
171478
|
+
return categoryDiff;
|
|
171479
|
+
return left.id - right.id;
|
|
171480
|
+
}
|
|
169847
171481
|
var maxCompartmentSeqStatements = new WeakMap;
|
|
169848
171482
|
var maxMemoryIdStatements = new WeakMap;
|
|
169849
171483
|
var legacyCompartmentCountStatements = new WeakMap;
|
|
169850
171484
|
var m0CompartmentStatements = new WeakMap;
|
|
169851
171485
|
var newCompartmentStatements = new WeakMap;
|
|
169852
|
-
function trimMemoriesToBudgetV2(sessionId, memories, budgetTokens) {
|
|
169853
|
-
const selectionOrder = [...memories].sort(
|
|
169854
|
-
if (a.status === "permanent" && b.status !== "permanent")
|
|
169855
|
-
return -1;
|
|
169856
|
-
if (b.status === "permanent" && a.status !== "permanent")
|
|
169857
|
-
return 1;
|
|
169858
|
-
const importanceDiff = (b.importance ?? 50) - (a.importance ?? 50);
|
|
169859
|
-
if (importanceDiff !== 0)
|
|
169860
|
-
return importanceDiff;
|
|
169861
|
-
return a.id - b.id;
|
|
169862
|
-
});
|
|
171486
|
+
function trimMemoriesToBudgetV2(sessionId, memories, budgetTokens, renderOptions = {}) {
|
|
171487
|
+
const selectionOrder = [...memories].sort(memorySelectionOrder);
|
|
169863
171488
|
const selected = [];
|
|
169864
171489
|
let usedTokens = MEMORY_BLOCK_WRAPPER_TOKENS;
|
|
169865
171490
|
for (const memory2 of selectionOrder) {
|
|
169866
|
-
const memoryTokens = estimateTokens(renderMemoryLineV2(memory2));
|
|
171491
|
+
const memoryTokens = estimateTokens(renderMemoryLineV2(memory2, renderOptions.sourceNameByMemoryId?.get(memory2.id)));
|
|
169867
171492
|
if (usedTokens + memoryTokens > budgetTokens)
|
|
169868
171493
|
continue;
|
|
169869
171494
|
selected.push(memory2);
|
|
@@ -169872,16 +171497,70 @@ function trimMemoriesToBudgetV2(sessionId, memories, budgetTokens) {
|
|
|
169872
171497
|
if (selected.length < memories.length) {
|
|
169873
171498
|
sessionLog(sessionId, `v2 trimmed memories from ${memories.length} to ${selected.length} to fit injection budget of ${budgetTokens} tokens`);
|
|
169874
171499
|
}
|
|
169875
|
-
const renderOrder = [...selected].sort(
|
|
169876
|
-
const aPriority = MEMORY_CATEGORY_ORDER_PRIORITY[a.category] ?? MEMORY_CATEGORY_ORDER_UNKNOWN;
|
|
169877
|
-
const bPriority = MEMORY_CATEGORY_ORDER_PRIORITY[b.category] ?? MEMORY_CATEGORY_ORDER_UNKNOWN;
|
|
169878
|
-
const categoryDiff = aPriority - bPriority;
|
|
169879
|
-
if (categoryDiff !== 0)
|
|
169880
|
-
return categoryDiff;
|
|
169881
|
-
return a.id - b.id;
|
|
169882
|
-
});
|
|
171500
|
+
const renderOrder = [...selected].sort(memoryRenderOrder);
|
|
169883
171501
|
return { selected, renderOrder };
|
|
169884
171502
|
}
|
|
171503
|
+
function trimWorkspaceMemoriesToBudgetV2(sessionId, memories, budgetTokens, workspace, renderOptions = {}) {
|
|
171504
|
+
if (!workspace.isWorkspaced) {
|
|
171505
|
+
return trimMemoriesToBudgetV2(sessionId, memories, budgetTokens, renderOptions);
|
|
171506
|
+
}
|
|
171507
|
+
const selected = [];
|
|
171508
|
+
const selectedIds = new Set;
|
|
171509
|
+
let usedTokens = MEMORY_BLOCK_WRAPPER_TOKENS;
|
|
171510
|
+
const tokenCost = (memory2) => estimateTokens(renderMemoryLineV2(memory2, renderOptions.sourceNameByMemoryId?.get(memory2.id)));
|
|
171511
|
+
const trySelect = (memory2) => {
|
|
171512
|
+
if (selectedIds.has(memory2.id))
|
|
171513
|
+
return false;
|
|
171514
|
+
const tokens = tokenCost(memory2);
|
|
171515
|
+
if (usedTokens + tokens > budgetTokens)
|
|
171516
|
+
return false;
|
|
171517
|
+
selected.push(memory2);
|
|
171518
|
+
selectedIds.add(memory2.id);
|
|
171519
|
+
usedTokens += tokens;
|
|
171520
|
+
return true;
|
|
171521
|
+
};
|
|
171522
|
+
for (const memory2 of memories.filter((candidate) => candidate.status === "permanent").sort(memorySelectionOrder)) {
|
|
171523
|
+
trySelect(memory2);
|
|
171524
|
+
}
|
|
171525
|
+
const remainingAfterPermanent = Math.max(0, budgetTokens - usedTokens);
|
|
171526
|
+
const floorTokens = remainingAfterPermanent / Math.max(1, workspace.identities.length);
|
|
171527
|
+
const byIdentity = new Map;
|
|
171528
|
+
for (const memory2 of memories) {
|
|
171529
|
+
if (memory2.status === "permanent")
|
|
171530
|
+
continue;
|
|
171531
|
+
const identity = memoryCanonicalIdentity(memory2, workspace);
|
|
171532
|
+
if (!identity)
|
|
171533
|
+
continue;
|
|
171534
|
+
const list = byIdentity.get(identity) ?? [];
|
|
171535
|
+
list.push(memory2);
|
|
171536
|
+
byIdentity.set(identity, list);
|
|
171537
|
+
}
|
|
171538
|
+
for (const identity of workspace.identities) {
|
|
171539
|
+
let memberTokens = 0;
|
|
171540
|
+
const candidates2 = (byIdentity.get(identity) ?? []).sort(memorySelectionOrder);
|
|
171541
|
+
for (const memory2 of candidates2) {
|
|
171542
|
+
if (selectedIds.has(memory2.id))
|
|
171543
|
+
continue;
|
|
171544
|
+
const tokens = tokenCost(memory2);
|
|
171545
|
+
if (memberTokens + tokens > floorTokens)
|
|
171546
|
+
continue;
|
|
171547
|
+
if (usedTokens + tokens > budgetTokens)
|
|
171548
|
+
continue;
|
|
171549
|
+
selected.push(memory2);
|
|
171550
|
+
selectedIds.add(memory2.id);
|
|
171551
|
+
usedTokens += tokens;
|
|
171552
|
+
memberTokens += tokens;
|
|
171553
|
+
}
|
|
171554
|
+
}
|
|
171555
|
+
const remaining = memories.filter((memory2) => !selectedIds.has(memory2.id)).sort(memorySelectionOrder);
|
|
171556
|
+
for (const memory2 of remaining) {
|
|
171557
|
+
trySelect(memory2);
|
|
171558
|
+
}
|
|
171559
|
+
if (selected.length < memories.length) {
|
|
171560
|
+
sessionLog(sessionId, `v2 trimmed memories from ${memories.length} to ${selected.length} to fit injection budget of ${budgetTokens} tokens`);
|
|
171561
|
+
}
|
|
171562
|
+
return { selected, renderOrder: [...selected].sort(memoryRenderOrder) };
|
|
171563
|
+
}
|
|
169885
171564
|
function trimUserMemoriesToBudget(memories, budgetTokens) {
|
|
169886
171565
|
const selected = [];
|
|
169887
171566
|
let usedTokens = 0;
|
|
@@ -169894,15 +171573,16 @@ function trimUserMemoriesToBudget(memories, budgetTokens) {
|
|
|
169894
171573
|
}
|
|
169895
171574
|
return selected;
|
|
169896
171575
|
}
|
|
169897
|
-
function renderMemoryLineV2(memory2) {
|
|
169898
|
-
|
|
171576
|
+
function renderMemoryLineV2(memory2, sourceName) {
|
|
171577
|
+
const sourceAttr = sourceName ? ` source="${escapeXmlAttr(sourceName)}"` : "";
|
|
171578
|
+
return ` <memory id="${memory2.id}" category="${escapeXmlAttr(memory2.category)}"${sourceAttr} importance="${memory2.importance ?? 50}">${escapeXmlContent(memory2.content)}</memory>`;
|
|
169899
171579
|
}
|
|
169900
|
-
function renderMemoryBlockV2(memories, wrapper = "project-memory") {
|
|
171580
|
+
function renderMemoryBlockV2(memories, wrapper = "project-memory", renderOptions = {}) {
|
|
169901
171581
|
if (memories.length === 0)
|
|
169902
171582
|
return "";
|
|
169903
171583
|
const lines = [`<${wrapper}>`];
|
|
169904
171584
|
for (const memory2 of memories) {
|
|
169905
|
-
lines.push(renderMemoryLineV2(memory2));
|
|
171585
|
+
lines.push(renderMemoryLineV2(memory2, renderOptions.sourceNameByMemoryId?.get(memory2.id)));
|
|
169906
171586
|
}
|
|
169907
171587
|
lines.push(`</${wrapper}>`);
|
|
169908
171588
|
return lines.join(`
|
|
@@ -169967,8 +171647,9 @@ function formatAge(committedAtMs) {
|
|
|
169967
171647
|
}
|
|
169968
171648
|
function formatResult(result, index) {
|
|
169969
171649
|
if (result.source === "memory") {
|
|
171650
|
+
const source = result.sourceName ? ` source=${result.sourceName}` : "";
|
|
169970
171651
|
return [
|
|
169971
|
-
`[${index}] [memory] score=${result.score.toFixed(2)} id=${result.memoryId} category=${result.category} match=${result.matchType}`,
|
|
171652
|
+
`[${index}] [memory] score=${result.score.toFixed(2)} id=${result.memoryId} category=${result.category}${source} match=${result.matchType}`,
|
|
169972
171653
|
result.content
|
|
169973
171654
|
].join(`
|
|
169974
171655
|
`);
|
|
@@ -169978,6 +171659,13 @@ function formatResult(result, index) {
|
|
|
169978
171659
|
`[${index}] [git_commit] score=${result.score.toFixed(2)} sha=${result.shortSha} ${formatAge(result.committedAtMs)} match=${result.matchType}`,
|
|
169979
171660
|
result.content
|
|
169980
171661
|
].join(`
|
|
171662
|
+
`);
|
|
171663
|
+
}
|
|
171664
|
+
if (result.source === "compartment") {
|
|
171665
|
+
return [
|
|
171666
|
+
`[${index}] [message] score=${result.score.toFixed(2)} compartment_id=${result.compartmentId} range=${result.startOrdinal}-${result.endOrdinal} match=${result.matchType} title=${result.title}`,
|
|
171667
|
+
result.snippet ? `Snippet: ${result.snippet}` : result.content
|
|
171668
|
+
].join(`
|
|
169981
171669
|
`);
|
|
169982
171670
|
}
|
|
169983
171671
|
const expandStart = Math.max(1, result.messageOrdinal - 3);
|
|
@@ -169993,7 +171681,7 @@ function formatSearchResults(query, results) {
|
|
|
169993
171681
|
return `No results found for "${query}" across memories, git commits, or message history.`;
|
|
169994
171682
|
}
|
|
169995
171683
|
const bodyParts = results.map((result, index) => formatResult(result, index + 1));
|
|
169996
|
-
if (results.some((result) => result.source === "message")) {
|
|
171684
|
+
if (results.some((result) => result.source === "message" || result.source === "compartment")) {
|
|
169997
171685
|
bodyParts.push("Use ctx_expand(start, end) with the range from any message result above to read the full conversation context.");
|
|
169998
171686
|
}
|
|
169999
171687
|
const body = bodyParts.join(`
|