@askexenow/exe-os 0.9.7 → 0.9.9
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/bin/backfill-conversations.js +953 -105
- package/dist/bin/backfill-responses.js +952 -104
- package/dist/bin/backfill-vectors.js +956 -108
- package/dist/bin/cleanup-stale-review-tasks.js +802 -58
- package/dist/bin/cli.js +2292 -1070
- package/dist/bin/exe-agent-config.js +157 -101
- package/dist/bin/exe-agent.js +55 -29
- package/dist/bin/exe-assign.js +940 -92
- package/dist/bin/exe-boot.js +1424 -442
- package/dist/bin/exe-call.js +240 -141
- package/dist/bin/exe-cloud.js +198 -70
- package/dist/bin/exe-dispatch.js +951 -192
- package/dist/bin/exe-doctor.js +791 -51
- package/dist/bin/exe-export-behaviors.js +790 -42
- package/dist/bin/exe-forget.js +771 -31
- package/dist/bin/exe-gateway.js +1592 -521
- package/dist/bin/exe-heartbeat.js +850 -109
- package/dist/bin/exe-kill.js +783 -35
- package/dist/bin/exe-launch-agent.js +1030 -107
- package/dist/bin/exe-link.js +916 -110
- package/dist/bin/exe-new-employee.js +526 -217
- package/dist/bin/exe-pending-messages.js +1046 -62
- package/dist/bin/exe-pending-notifications.js +1318 -111
- package/dist/bin/exe-pending-reviews.js +1040 -72
- package/dist/bin/exe-rename.js +772 -59
- package/dist/bin/exe-review.js +772 -32
- package/dist/bin/exe-search.js +982 -128
- package/dist/bin/exe-session-cleanup.js +1180 -306
- package/dist/bin/exe-settings.js +185 -105
- package/dist/bin/exe-start-codex.js +886 -132
- package/dist/bin/exe-start-opencode.js +873 -119
- package/dist/bin/exe-status.js +803 -59
- package/dist/bin/exe-team.js +772 -32
- package/dist/bin/git-sweep.js +1046 -223
- package/dist/bin/graph-backfill.js +779 -31
- package/dist/bin/graph-export.js +785 -37
- package/dist/bin/install.js +632 -200
- package/dist/bin/scan-tasks.js +1055 -232
- package/dist/bin/setup.js +1419 -320
- package/dist/bin/shard-migrate.js +783 -35
- package/dist/bin/update.js +138 -49
- package/dist/bin/wiki-sync.js +782 -34
- package/dist/gateway/index.js +1444 -449
- package/dist/hooks/bug-report-worker.js +1141 -269
- package/dist/hooks/codex-stop-task-finalizer.js +4678 -0
- package/dist/hooks/commit-complete.js +1044 -221
- package/dist/hooks/error-recall.js +989 -135
- package/dist/hooks/exe-heartbeat-hook.js +99 -75
- package/dist/hooks/ingest-worker.js +4176 -3226
- package/dist/hooks/ingest.js +920 -168
- package/dist/hooks/instructions-loaded.js +874 -70
- package/dist/hooks/notification.js +860 -56
- package/dist/hooks/post-compact.js +881 -73
- package/dist/hooks/pre-compact.js +1050 -227
- package/dist/hooks/pre-tool-use.js +1084 -159
- package/dist/hooks/prompt-ingest-worker.js +1089 -164
- package/dist/hooks/prompt-submit.js +1469 -515
- package/dist/hooks/response-ingest-worker.js +1104 -179
- package/dist/hooks/session-end.js +1085 -251
- package/dist/hooks/session-start.js +1241 -231
- package/dist/hooks/stop.js +935 -109
- package/dist/hooks/subagent-stop.js +881 -73
- package/dist/hooks/summary-worker.js +1323 -307
- package/dist/index.js +1449 -452
- package/dist/lib/agent-config.js +28 -6
- package/dist/lib/cloud-sync.js +909 -115
- package/dist/lib/config.js +30 -10
- package/dist/lib/consolidation.js +42 -9
- package/dist/lib/database.js +739 -33
- package/dist/lib/db-daemon-client.js +73 -19
- package/dist/lib/db.js +2359 -0
- package/dist/lib/device-registry.js +760 -47
- package/dist/lib/embedder.js +201 -73
- package/dist/lib/employee-templates.js +30 -4
- package/dist/lib/employees.js +290 -86
- package/dist/lib/exe-daemon-client.js +187 -83
- package/dist/lib/exe-daemon.js +1696 -616
- package/dist/lib/hybrid-search.js +982 -128
- package/dist/lib/identity.js +43 -13
- package/dist/lib/license.js +133 -48
- package/dist/lib/messaging.js +167 -80
- package/dist/lib/reminders.js +35 -5
- package/dist/lib/schedules.js +772 -32
- package/dist/lib/skill-learning.js +54 -7
- package/dist/lib/store.js +779 -31
- package/dist/lib/task-router.js +94 -73
- package/dist/lib/tasks.js +298 -225
- package/dist/lib/tmux-routing.js +246 -172
- package/dist/lib/token-spend.js +52 -14
- package/dist/mcp/server.js +2893 -850
- package/dist/mcp/tools/complete-reminder.js +35 -5
- package/dist/mcp/tools/create-reminder.js +35 -5
- package/dist/mcp/tools/create-task.js +507 -323
- package/dist/mcp/tools/deactivate-behavior.js +40 -10
- package/dist/mcp/tools/list-reminders.js +35 -5
- package/dist/mcp/tools/list-tasks.js +277 -104
- package/dist/mcp/tools/send-message.js +129 -56
- package/dist/mcp/tools/update-task.js +1864 -188
- package/dist/runtime/index.js +1083 -259
- package/dist/tui/App.js +1501 -434
- package/package.json +3 -2
|
@@ -70,9 +70,47 @@ var init_db_retry = __esm({
|
|
|
70
70
|
}
|
|
71
71
|
});
|
|
72
72
|
|
|
73
|
+
// src/lib/secure-files.ts
|
|
74
|
+
import { chmodSync, existsSync, mkdirSync } from "fs";
|
|
75
|
+
import { chmod, mkdir } from "fs/promises";
|
|
76
|
+
async function ensurePrivateDir(dirPath) {
|
|
77
|
+
await mkdir(dirPath, { recursive: true, mode: PRIVATE_DIR_MODE });
|
|
78
|
+
try {
|
|
79
|
+
await chmod(dirPath, PRIVATE_DIR_MODE);
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function ensurePrivateDirSync(dirPath) {
|
|
84
|
+
mkdirSync(dirPath, { recursive: true, mode: PRIVATE_DIR_MODE });
|
|
85
|
+
try {
|
|
86
|
+
chmodSync(dirPath, PRIVATE_DIR_MODE);
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async function enforcePrivateFile(filePath) {
|
|
91
|
+
try {
|
|
92
|
+
await chmod(filePath, PRIVATE_FILE_MODE);
|
|
93
|
+
} catch {
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function enforcePrivateFileSync(filePath) {
|
|
97
|
+
try {
|
|
98
|
+
if (existsSync(filePath)) chmodSync(filePath, PRIVATE_FILE_MODE);
|
|
99
|
+
} catch {
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
var PRIVATE_DIR_MODE, PRIVATE_FILE_MODE;
|
|
103
|
+
var init_secure_files = __esm({
|
|
104
|
+
"src/lib/secure-files.ts"() {
|
|
105
|
+
"use strict";
|
|
106
|
+
PRIVATE_DIR_MODE = 448;
|
|
107
|
+
PRIVATE_FILE_MODE = 384;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
73
111
|
// src/lib/config.ts
|
|
74
|
-
import { readFile, writeFile
|
|
75
|
-
import { readFileSync, existsSync, renameSync } from "fs";
|
|
112
|
+
import { readFile, writeFile } from "fs/promises";
|
|
113
|
+
import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
|
|
76
114
|
import path from "path";
|
|
77
115
|
import os from "os";
|
|
78
116
|
function resolveDataDir() {
|
|
@@ -80,7 +118,7 @@ function resolveDataDir() {
|
|
|
80
118
|
if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
|
|
81
119
|
const newDir = path.join(os.homedir(), ".exe-os");
|
|
82
120
|
const legacyDir = path.join(os.homedir(), ".exe-mem");
|
|
83
|
-
if (!
|
|
121
|
+
if (!existsSync2(newDir) && existsSync2(legacyDir)) {
|
|
84
122
|
try {
|
|
85
123
|
renameSync(legacyDir, newDir);
|
|
86
124
|
process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
|
|
@@ -143,9 +181,9 @@ function normalizeAutoUpdate(raw) {
|
|
|
143
181
|
}
|
|
144
182
|
async function loadConfig() {
|
|
145
183
|
const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
|
|
146
|
-
await
|
|
184
|
+
await ensurePrivateDir(dir);
|
|
147
185
|
const configPath = path.join(dir, "config.json");
|
|
148
|
-
if (!
|
|
186
|
+
if (!existsSync2(configPath)) {
|
|
149
187
|
return { ...DEFAULT_CONFIG, dbPath: path.join(dir, "memories.db") };
|
|
150
188
|
}
|
|
151
189
|
const raw = await readFile(configPath, "utf-8");
|
|
@@ -158,6 +196,7 @@ async function loadConfig() {
|
|
|
158
196
|
`);
|
|
159
197
|
try {
|
|
160
198
|
await writeFile(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
|
|
199
|
+
await enforcePrivateFile(configPath);
|
|
161
200
|
} catch {
|
|
162
201
|
}
|
|
163
202
|
}
|
|
@@ -177,6 +216,7 @@ var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, LEGACY_LANCE_PATH, CURRENT_CON
|
|
|
177
216
|
var init_config = __esm({
|
|
178
217
|
"src/lib/config.ts"() {
|
|
179
218
|
"use strict";
|
|
219
|
+
init_secure_files();
|
|
180
220
|
EXE_AI_DIR = resolveDataDir();
|
|
181
221
|
DB_PATH = path.join(EXE_AI_DIR, "memories.db");
|
|
182
222
|
MODELS_DIR = path.join(EXE_AI_DIR, "models");
|
|
@@ -255,7 +295,7 @@ var init_config = __esm({
|
|
|
255
295
|
|
|
256
296
|
// src/lib/employees.ts
|
|
257
297
|
import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
|
|
258
|
-
import { existsSync as
|
|
298
|
+
import { existsSync as existsSync3, symlinkSync, readlinkSync, readFileSync as readFileSync2, renameSync as renameSync2, unlinkSync, writeFileSync } from "fs";
|
|
259
299
|
import { execSync } from "child_process";
|
|
260
300
|
import path2 from "path";
|
|
261
301
|
import os2 from "os";
|
|
@@ -272,14 +312,14 @@ function getCoordinatorName(employees = loadEmployeesSync()) {
|
|
|
272
312
|
return getCoordinatorEmployee(employees)?.name ?? DEFAULT_COORDINATOR_TEMPLATE_NAME;
|
|
273
313
|
}
|
|
274
314
|
function loadEmployeesSync(employeesPath = EMPLOYEES_PATH) {
|
|
275
|
-
if (!
|
|
315
|
+
if (!existsSync3(employeesPath)) return [];
|
|
276
316
|
try {
|
|
277
317
|
return JSON.parse(readFileSync2(employeesPath, "utf-8"));
|
|
278
318
|
} catch {
|
|
279
319
|
return [];
|
|
280
320
|
}
|
|
281
321
|
}
|
|
282
|
-
var EMPLOYEES_PATH, DEFAULT_COORDINATOR_TEMPLATE_NAME, COORDINATOR_ROLE;
|
|
322
|
+
var EMPLOYEES_PATH, DEFAULT_COORDINATOR_TEMPLATE_NAME, COORDINATOR_ROLE, IDENTITY_DIR;
|
|
283
323
|
var init_employees = __esm({
|
|
284
324
|
"src/lib/employees.ts"() {
|
|
285
325
|
"use strict";
|
|
@@ -287,12 +327,609 @@ var init_employees = __esm({
|
|
|
287
327
|
EMPLOYEES_PATH = path2.join(EXE_AI_DIR, "exe-employees.json");
|
|
288
328
|
DEFAULT_COORDINATOR_TEMPLATE_NAME = "exe";
|
|
289
329
|
COORDINATOR_ROLE = "COO";
|
|
330
|
+
IDENTITY_DIR = path2.join(EXE_AI_DIR, "identity");
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
// src/lib/database-adapter.ts
|
|
335
|
+
import os3 from "os";
|
|
336
|
+
import path3 from "path";
|
|
337
|
+
import { createRequire } from "module";
|
|
338
|
+
import { pathToFileURL } from "url";
|
|
339
|
+
function quotedIdentifier(identifier) {
|
|
340
|
+
return `"${identifier.replace(/"/g, '""')}"`;
|
|
341
|
+
}
|
|
342
|
+
function unqualifiedTableName(name) {
|
|
343
|
+
const raw = name.trim().replace(/^"|"$/g, "");
|
|
344
|
+
const parts = raw.split(".");
|
|
345
|
+
return parts[parts.length - 1].replace(/^"|"$/g, "").toLowerCase();
|
|
346
|
+
}
|
|
347
|
+
function stripTrailingSemicolon(sql) {
|
|
348
|
+
return sql.trim().replace(/;+\s*$/u, "");
|
|
349
|
+
}
|
|
350
|
+
function appendClause(sql, clause) {
|
|
351
|
+
const trimmed = stripTrailingSemicolon(sql);
|
|
352
|
+
const returningMatch = /\sRETURNING\b[\s\S]*$/iu.exec(trimmed);
|
|
353
|
+
if (!returningMatch) {
|
|
354
|
+
return `${trimmed}${clause}`;
|
|
355
|
+
}
|
|
356
|
+
const idx = returningMatch.index;
|
|
357
|
+
return `${trimmed.slice(0, idx)}${clause}${trimmed.slice(idx)}`;
|
|
358
|
+
}
|
|
359
|
+
function normalizeStatement(stmt) {
|
|
360
|
+
if (typeof stmt === "string") {
|
|
361
|
+
return { kind: "positional", sql: stmt, args: [] };
|
|
362
|
+
}
|
|
363
|
+
const sql = stmt.sql;
|
|
364
|
+
if (Array.isArray(stmt.args) || stmt.args === void 0) {
|
|
365
|
+
return { kind: "positional", sql, args: stmt.args ?? [] };
|
|
366
|
+
}
|
|
367
|
+
return { kind: "named", sql, args: stmt.args };
|
|
368
|
+
}
|
|
369
|
+
function rewriteBooleanLiterals(sql) {
|
|
370
|
+
let out = sql;
|
|
371
|
+
for (const column of BOOLEAN_COLUMN_NAMES) {
|
|
372
|
+
const scoped = `((?:\\b[a-z_][a-z0-9_]*\\.)?${column})`;
|
|
373
|
+
out = out.replace(new RegExp(`${scoped}\\s*=\\s*0\\b`, "giu"), "$1 = FALSE");
|
|
374
|
+
out = out.replace(new RegExp(`${scoped}\\s*=\\s*1\\b`, "giu"), "$1 = TRUE");
|
|
375
|
+
out = out.replace(new RegExp(`${scoped}\\s*!=\\s*0\\b`, "giu"), "$1 != FALSE");
|
|
376
|
+
out = out.replace(new RegExp(`${scoped}\\s*!=\\s*1\\b`, "giu"), "$1 != TRUE");
|
|
377
|
+
out = out.replace(new RegExp(`${scoped}\\s*<>\\s*0\\b`, "giu"), "$1 <> FALSE");
|
|
378
|
+
out = out.replace(new RegExp(`${scoped}\\s*<>\\s*1\\b`, "giu"), "$1 <> TRUE");
|
|
379
|
+
}
|
|
380
|
+
return out;
|
|
381
|
+
}
|
|
382
|
+
function rewriteInsertOrIgnore(sql) {
|
|
383
|
+
if (!/^\s*INSERT\s+OR\s+IGNORE\s+INTO\b/iu.test(sql)) {
|
|
384
|
+
return sql;
|
|
385
|
+
}
|
|
386
|
+
const replaced = sql.replace(/^\s*INSERT\s+OR\s+IGNORE\s+INTO\b/iu, "INSERT INTO");
|
|
387
|
+
return /\bON\s+CONFLICT\b/iu.test(replaced) ? replaced : appendClause(replaced, " ON CONFLICT DO NOTHING");
|
|
388
|
+
}
|
|
389
|
+
function rewriteInsertOrReplace(sql) {
|
|
390
|
+
const match = /^\s*INSERT\s+OR\s+REPLACE\s+INTO\s+([A-Za-z0-9_."]+)\s*\(([^)]+)\)([\s\S]*)$/iu.exec(sql);
|
|
391
|
+
if (!match) {
|
|
392
|
+
return sql;
|
|
393
|
+
}
|
|
394
|
+
const rawTable = match[1];
|
|
395
|
+
const rawColumns = match[2];
|
|
396
|
+
const remainder = match[3];
|
|
397
|
+
const tableName = unqualifiedTableName(rawTable);
|
|
398
|
+
const conflictKeys = UPSERT_KEYS[tableName];
|
|
399
|
+
if (!conflictKeys?.length) {
|
|
400
|
+
return sql;
|
|
401
|
+
}
|
|
402
|
+
const columns = rawColumns.split(",").map((col) => col.trim().replace(/^"|"$/g, ""));
|
|
403
|
+
const updateColumns = columns.filter((col) => !conflictKeys.includes(col));
|
|
404
|
+
const conflictTarget = conflictKeys.map(quotedIdentifier).join(", ");
|
|
405
|
+
const updateClause = updateColumns.length === 0 ? " DO NOTHING" : ` DO UPDATE SET ${updateColumns.map((col) => `${quotedIdentifier(col)} = EXCLUDED.${quotedIdentifier(col)}`).join(", ")}`;
|
|
406
|
+
return `INSERT INTO ${rawTable} (${rawColumns})${appendClause(remainder, ` ON CONFLICT (${conflictTarget})${updateClause}`)}`;
|
|
407
|
+
}
|
|
408
|
+
function rewriteSql(sql) {
|
|
409
|
+
let out = sql;
|
|
410
|
+
out = out.replace(/\bdatetime\(\s*['"]now['"]\s*\)/giu, "CURRENT_TIMESTAMP");
|
|
411
|
+
out = out.replace(/\bvector32\s*\(\s*\?\s*\)/giu, "?");
|
|
412
|
+
out = rewriteBooleanLiterals(out);
|
|
413
|
+
out = rewriteInsertOrReplace(out);
|
|
414
|
+
out = rewriteInsertOrIgnore(out);
|
|
415
|
+
return stripTrailingSemicolon(out);
|
|
416
|
+
}
|
|
417
|
+
function toBoolean(value) {
|
|
418
|
+
if (value === null || value === void 0) return value;
|
|
419
|
+
if (typeof value === "boolean") return value;
|
|
420
|
+
if (typeof value === "number") return value !== 0;
|
|
421
|
+
if (typeof value === "bigint") return value !== 0n;
|
|
422
|
+
if (typeof value === "string") {
|
|
423
|
+
const normalized = value.trim().toLowerCase();
|
|
424
|
+
if (normalized === "0" || normalized === "false") return false;
|
|
425
|
+
if (normalized === "1" || normalized === "true") return true;
|
|
426
|
+
}
|
|
427
|
+
return Boolean(value);
|
|
428
|
+
}
|
|
429
|
+
function countQuestionMarks(sql, end) {
|
|
430
|
+
let count = 0;
|
|
431
|
+
let inSingle = false;
|
|
432
|
+
let inDouble = false;
|
|
433
|
+
let inLineComment = false;
|
|
434
|
+
let inBlockComment = false;
|
|
435
|
+
for (let i = 0; i < end; i++) {
|
|
436
|
+
const ch = sql[i];
|
|
437
|
+
const next = sql[i + 1];
|
|
438
|
+
if (inLineComment) {
|
|
439
|
+
if (ch === "\n") inLineComment = false;
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
if (inBlockComment) {
|
|
443
|
+
if (ch === "*" && next === "/") {
|
|
444
|
+
inBlockComment = false;
|
|
445
|
+
i += 1;
|
|
446
|
+
}
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (!inSingle && !inDouble && ch === "-" && next === "-") {
|
|
450
|
+
inLineComment = true;
|
|
451
|
+
i += 1;
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
if (!inSingle && !inDouble && ch === "/" && next === "*") {
|
|
455
|
+
inBlockComment = true;
|
|
456
|
+
i += 1;
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (!inDouble && ch === "'" && sql[i - 1] !== "\\") {
|
|
460
|
+
inSingle = !inSingle;
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (!inSingle && ch === '"' && sql[i - 1] !== "\\") {
|
|
464
|
+
inDouble = !inDouble;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (!inSingle && !inDouble && ch === "?") {
|
|
468
|
+
count += 1;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return count;
|
|
472
|
+
}
|
|
473
|
+
function findBooleanPlaceholderIndexes(sql) {
|
|
474
|
+
const indexes = /* @__PURE__ */ new Set();
|
|
475
|
+
for (const column of BOOLEAN_COLUMN_NAMES) {
|
|
476
|
+
const pattern = new RegExp(`(?:\\b[a-z_][a-z0-9_]*\\.)?${column}\\s*=\\s*\\?`, "giu");
|
|
477
|
+
for (const match of sql.matchAll(pattern)) {
|
|
478
|
+
const matchText = match[0];
|
|
479
|
+
const qIndex = match.index + matchText.lastIndexOf("?");
|
|
480
|
+
indexes.add(countQuestionMarks(sql, qIndex + 1));
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return indexes;
|
|
484
|
+
}
|
|
485
|
+
function coerceInsertBooleanArgs(sql, args) {
|
|
486
|
+
const match = /^\s*INSERT(?:\s+OR\s+(?:IGNORE|REPLACE))?\s+INTO\s+([A-Za-z0-9_."]+)\s*\(([^)]+)\)/iu.exec(sql);
|
|
487
|
+
if (!match) return;
|
|
488
|
+
const rawTable = match[1];
|
|
489
|
+
const rawColumns = match[2];
|
|
490
|
+
const boolColumns = BOOLEAN_COLUMNS_BY_TABLE[unqualifiedTableName(rawTable)];
|
|
491
|
+
if (!boolColumns?.size) return;
|
|
492
|
+
const columns = rawColumns.split(",").map((col) => col.trim().replace(/^"|"$/g, ""));
|
|
493
|
+
for (const [index, column] of columns.entries()) {
|
|
494
|
+
if (boolColumns.has(column) && index < args.length) {
|
|
495
|
+
args[index] = toBoolean(args[index]);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
function coerceUpdateBooleanArgs(sql, args) {
|
|
500
|
+
const match = /^\s*UPDATE\s+([A-Za-z0-9_."]+)\s+SET\s+([\s\S]+?)(?:\s+WHERE\b|$)/iu.exec(sql);
|
|
501
|
+
if (!match) return;
|
|
502
|
+
const rawTable = match[1];
|
|
503
|
+
const setClause = match[2];
|
|
504
|
+
const boolColumns = BOOLEAN_COLUMNS_BY_TABLE[unqualifiedTableName(rawTable)];
|
|
505
|
+
if (!boolColumns?.size) return;
|
|
506
|
+
const assignments = setClause.split(",");
|
|
507
|
+
let placeholderIndex = 0;
|
|
508
|
+
for (const assignment of assignments) {
|
|
509
|
+
if (!assignment.includes("?")) continue;
|
|
510
|
+
placeholderIndex += 1;
|
|
511
|
+
const colMatch = /^\s*(?:[A-Za-z_][A-Za-z0-9_]*\.)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*\?/iu.exec(assignment);
|
|
512
|
+
if (colMatch && boolColumns.has(colMatch[1])) {
|
|
513
|
+
args[placeholderIndex - 1] = toBoolean(args[placeholderIndex - 1]);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
function coerceBooleanArgs(sql, args) {
|
|
518
|
+
const nextArgs = [...args];
|
|
519
|
+
coerceInsertBooleanArgs(sql, nextArgs);
|
|
520
|
+
coerceUpdateBooleanArgs(sql, nextArgs);
|
|
521
|
+
const placeholderIndexes = findBooleanPlaceholderIndexes(sql);
|
|
522
|
+
for (const index of placeholderIndexes) {
|
|
523
|
+
if (index > 0 && index <= nextArgs.length) {
|
|
524
|
+
nextArgs[index - 1] = toBoolean(nextArgs[index - 1]);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return nextArgs;
|
|
528
|
+
}
|
|
529
|
+
function convertQuestionMarksToDollarParams(sql) {
|
|
530
|
+
let out = "";
|
|
531
|
+
let placeholder = 0;
|
|
532
|
+
let inSingle = false;
|
|
533
|
+
let inDouble = false;
|
|
534
|
+
let inLineComment = false;
|
|
535
|
+
let inBlockComment = false;
|
|
536
|
+
for (let i = 0; i < sql.length; i++) {
|
|
537
|
+
const ch = sql[i];
|
|
538
|
+
const next = sql[i + 1];
|
|
539
|
+
if (inLineComment) {
|
|
540
|
+
out += ch;
|
|
541
|
+
if (ch === "\n") inLineComment = false;
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
if (inBlockComment) {
|
|
545
|
+
out += ch;
|
|
546
|
+
if (ch === "*" && next === "/") {
|
|
547
|
+
out += next;
|
|
548
|
+
inBlockComment = false;
|
|
549
|
+
i += 1;
|
|
550
|
+
}
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
if (!inSingle && !inDouble && ch === "-" && next === "-") {
|
|
554
|
+
out += ch + next;
|
|
555
|
+
inLineComment = true;
|
|
556
|
+
i += 1;
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
if (!inSingle && !inDouble && ch === "/" && next === "*") {
|
|
560
|
+
out += ch + next;
|
|
561
|
+
inBlockComment = true;
|
|
562
|
+
i += 1;
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
if (!inDouble && ch === "'" && sql[i - 1] !== "\\") {
|
|
566
|
+
inSingle = !inSingle;
|
|
567
|
+
out += ch;
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
if (!inSingle && ch === '"' && sql[i - 1] !== "\\") {
|
|
571
|
+
inDouble = !inDouble;
|
|
572
|
+
out += ch;
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
if (!inSingle && !inDouble && ch === "?") {
|
|
576
|
+
placeholder += 1;
|
|
577
|
+
out += `$${placeholder}`;
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
out += ch;
|
|
581
|
+
}
|
|
582
|
+
return out;
|
|
583
|
+
}
|
|
584
|
+
function translateStatementForPostgres(stmt) {
|
|
585
|
+
const normalized = normalizeStatement(stmt);
|
|
586
|
+
if (normalized.kind === "named") {
|
|
587
|
+
throw new Error("Named SQL parameters are not supported by the Prisma adapter.");
|
|
588
|
+
}
|
|
589
|
+
const rewrittenSql = rewriteSql(normalized.sql);
|
|
590
|
+
const coercedArgs = coerceBooleanArgs(rewrittenSql, normalized.args);
|
|
591
|
+
return {
|
|
592
|
+
sql: convertQuestionMarksToDollarParams(rewrittenSql),
|
|
593
|
+
args: coercedArgs
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
function shouldBypassPostgres(stmt) {
|
|
597
|
+
const normalized = normalizeStatement(stmt);
|
|
598
|
+
if (normalized.kind === "named") {
|
|
599
|
+
return true;
|
|
600
|
+
}
|
|
601
|
+
return IMMEDIATE_FALLBACK_PATTERNS.some((pattern) => pattern.test(normalized.sql));
|
|
602
|
+
}
|
|
603
|
+
function shouldFallbackOnError(error) {
|
|
604
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
605
|
+
return /42P01|42883|42601|does not exist|syntax error|not supported|Named SQL parameters are not supported/iu.test(message);
|
|
606
|
+
}
|
|
607
|
+
function isReadQuery(sql) {
|
|
608
|
+
const trimmed = sql.trimStart();
|
|
609
|
+
return /^(SELECT|WITH|SHOW|EXPLAIN|VALUES)\b/iu.test(trimmed) || /\bRETURNING\b/iu.test(trimmed);
|
|
610
|
+
}
|
|
611
|
+
function buildRow(row, columns) {
|
|
612
|
+
const values = columns.map((column) => row[column]);
|
|
613
|
+
return Object.assign(values, row);
|
|
614
|
+
}
|
|
615
|
+
function buildResultSet(rows, rowsAffected = 0) {
|
|
616
|
+
const columns = rows[0] ? Object.keys(rows[0]) : [];
|
|
617
|
+
const resultRows = rows.map((row) => buildRow(row, columns));
|
|
618
|
+
return {
|
|
619
|
+
columns,
|
|
620
|
+
columnTypes: columns.map(() => ""),
|
|
621
|
+
rows: resultRows,
|
|
622
|
+
rowsAffected,
|
|
623
|
+
lastInsertRowid: void 0,
|
|
624
|
+
toJSON() {
|
|
625
|
+
return {
|
|
626
|
+
columns,
|
|
627
|
+
columnTypes: columns.map(() => ""),
|
|
628
|
+
rows,
|
|
629
|
+
rowsAffected,
|
|
630
|
+
lastInsertRowid: void 0
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
async function loadPrismaClient() {
|
|
636
|
+
if (!prismaClientPromise) {
|
|
637
|
+
prismaClientPromise = (async () => {
|
|
638
|
+
const explicitPath = process.env.EXE_OS_PRISMA_CLIENT_PATH;
|
|
639
|
+
if (explicitPath) {
|
|
640
|
+
const module2 = await import(pathToFileURL(explicitPath).href);
|
|
641
|
+
const PrismaClient2 = module2.PrismaClient ?? module2.default?.PrismaClient;
|
|
642
|
+
if (!PrismaClient2) {
|
|
643
|
+
throw new Error(`No PrismaClient export found at ${explicitPath}`);
|
|
644
|
+
}
|
|
645
|
+
return new PrismaClient2();
|
|
646
|
+
}
|
|
647
|
+
const exeDbRoot = process.env.EXE_DB_ROOT ?? path3.join(os3.homedir(), "exe-db");
|
|
648
|
+
const requireFromExeDb = createRequire(path3.join(exeDbRoot, "package.json"));
|
|
649
|
+
const prismaEntry = requireFromExeDb.resolve("@prisma/client");
|
|
650
|
+
const module = await import(pathToFileURL(prismaEntry).href);
|
|
651
|
+
const PrismaClient = module.PrismaClient ?? module.default?.PrismaClient;
|
|
652
|
+
if (!PrismaClient) {
|
|
653
|
+
throw new Error(`No PrismaClient export found in ${prismaEntry}`);
|
|
654
|
+
}
|
|
655
|
+
return new PrismaClient();
|
|
656
|
+
})();
|
|
657
|
+
}
|
|
658
|
+
return prismaClientPromise;
|
|
659
|
+
}
|
|
660
|
+
async function ensureCompatibilityViews(prisma) {
|
|
661
|
+
if (!compatibilityBootstrapPromise) {
|
|
662
|
+
compatibilityBootstrapPromise = (async () => {
|
|
663
|
+
for (const mapping of VIEW_MAPPINGS) {
|
|
664
|
+
const relation = mapping.source.replace(/"/g, "");
|
|
665
|
+
const rows = await prisma.$queryRawUnsafe(
|
|
666
|
+
"SELECT to_regclass($1) AS regclass",
|
|
667
|
+
relation
|
|
668
|
+
);
|
|
669
|
+
if (!rows[0]?.regclass) {
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
await prisma.$executeRawUnsafe(
|
|
673
|
+
`CREATE OR REPLACE VIEW public.${quotedIdentifier(mapping.view)} AS SELECT * FROM ${mapping.source}`
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
})();
|
|
677
|
+
}
|
|
678
|
+
return compatibilityBootstrapPromise;
|
|
679
|
+
}
|
|
680
|
+
async function executeOnPrisma(executor, stmt) {
|
|
681
|
+
const translated = translateStatementForPostgres(stmt);
|
|
682
|
+
if (isReadQuery(translated.sql)) {
|
|
683
|
+
const rows = await executor.$queryRawUnsafe(
|
|
684
|
+
translated.sql,
|
|
685
|
+
...translated.args
|
|
686
|
+
);
|
|
687
|
+
return buildResultSet(rows, /\bRETURNING\b/iu.test(translated.sql) ? rows.length : 0);
|
|
688
|
+
}
|
|
689
|
+
const rowsAffected = await executor.$executeRawUnsafe(translated.sql, ...translated.args);
|
|
690
|
+
return buildResultSet([], rowsAffected);
|
|
691
|
+
}
|
|
692
|
+
function splitSqlStatements(sql) {
|
|
693
|
+
const parts = [];
|
|
694
|
+
let current = "";
|
|
695
|
+
let inSingle = false;
|
|
696
|
+
let inDouble = false;
|
|
697
|
+
let inLineComment = false;
|
|
698
|
+
let inBlockComment = false;
|
|
699
|
+
for (let i = 0; i < sql.length; i++) {
|
|
700
|
+
const ch = sql[i];
|
|
701
|
+
const next = sql[i + 1];
|
|
702
|
+
if (inLineComment) {
|
|
703
|
+
current += ch;
|
|
704
|
+
if (ch === "\n") inLineComment = false;
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
if (inBlockComment) {
|
|
708
|
+
current += ch;
|
|
709
|
+
if (ch === "*" && next === "/") {
|
|
710
|
+
current += next;
|
|
711
|
+
inBlockComment = false;
|
|
712
|
+
i += 1;
|
|
713
|
+
}
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
if (!inSingle && !inDouble && ch === "-" && next === "-") {
|
|
717
|
+
current += ch + next;
|
|
718
|
+
inLineComment = true;
|
|
719
|
+
i += 1;
|
|
720
|
+
continue;
|
|
721
|
+
}
|
|
722
|
+
if (!inSingle && !inDouble && ch === "/" && next === "*") {
|
|
723
|
+
current += ch + next;
|
|
724
|
+
inBlockComment = true;
|
|
725
|
+
i += 1;
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
if (!inDouble && ch === "'" && sql[i - 1] !== "\\") {
|
|
729
|
+
inSingle = !inSingle;
|
|
730
|
+
current += ch;
|
|
731
|
+
continue;
|
|
732
|
+
}
|
|
733
|
+
if (!inSingle && ch === '"' && sql[i - 1] !== "\\") {
|
|
734
|
+
inDouble = !inDouble;
|
|
735
|
+
current += ch;
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
if (!inSingle && !inDouble && ch === ";") {
|
|
739
|
+
if (current.trim()) {
|
|
740
|
+
parts.push(current.trim());
|
|
741
|
+
}
|
|
742
|
+
current = "";
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
current += ch;
|
|
746
|
+
}
|
|
747
|
+
if (current.trim()) {
|
|
748
|
+
parts.push(current.trim());
|
|
749
|
+
}
|
|
750
|
+
return parts;
|
|
751
|
+
}
|
|
752
|
+
async function createPrismaDbAdapter(fallbackClient) {
|
|
753
|
+
const prisma = await loadPrismaClient();
|
|
754
|
+
await ensureCompatibilityViews(prisma);
|
|
755
|
+
let closed = false;
|
|
756
|
+
let adapter;
|
|
757
|
+
const fallbackExecute = async (stmt, error) => {
|
|
758
|
+
if (!fallbackClient) {
|
|
759
|
+
if (error) throw error;
|
|
760
|
+
throw new Error("No fallback SQLite client is available for this Prisma-routed query.");
|
|
761
|
+
}
|
|
762
|
+
if (error) {
|
|
763
|
+
process.stderr.write(
|
|
764
|
+
`[database-adapter] Falling back to SQLite: ${error instanceof Error ? error.message : String(error)}
|
|
765
|
+
`
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
return fallbackClient.execute(stmt);
|
|
769
|
+
};
|
|
770
|
+
adapter = {
|
|
771
|
+
async execute(stmt) {
|
|
772
|
+
if (shouldBypassPostgres(stmt)) {
|
|
773
|
+
return fallbackExecute(stmt);
|
|
774
|
+
}
|
|
775
|
+
try {
|
|
776
|
+
return await executeOnPrisma(prisma, stmt);
|
|
777
|
+
} catch (error) {
|
|
778
|
+
if (shouldFallbackOnError(error)) {
|
|
779
|
+
return fallbackExecute(stmt, error);
|
|
780
|
+
}
|
|
781
|
+
throw error;
|
|
782
|
+
}
|
|
783
|
+
},
|
|
784
|
+
async batch(stmts, mode) {
|
|
785
|
+
if (stmts.some((stmt) => shouldBypassPostgres(stmt))) {
|
|
786
|
+
if (!fallbackClient) {
|
|
787
|
+
throw new Error("Cannot batch unsupported SQLite-only statements without a fallback client.");
|
|
788
|
+
}
|
|
789
|
+
return fallbackClient.batch(stmts, mode);
|
|
790
|
+
}
|
|
791
|
+
try {
|
|
792
|
+
if (prisma.$transaction) {
|
|
793
|
+
return await prisma.$transaction(async (tx) => {
|
|
794
|
+
const results2 = [];
|
|
795
|
+
for (const stmt of stmts) {
|
|
796
|
+
results2.push(await executeOnPrisma(tx, stmt));
|
|
797
|
+
}
|
|
798
|
+
return results2;
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
const results = [];
|
|
802
|
+
for (const stmt of stmts) {
|
|
803
|
+
results.push(await executeOnPrisma(prisma, stmt));
|
|
804
|
+
}
|
|
805
|
+
return results;
|
|
806
|
+
} catch (error) {
|
|
807
|
+
if (fallbackClient && shouldFallbackOnError(error)) {
|
|
808
|
+
process.stderr.write(
|
|
809
|
+
`[database-adapter] Falling back batch to SQLite: ${error instanceof Error ? error.message : String(error)}
|
|
810
|
+
`
|
|
811
|
+
);
|
|
812
|
+
return fallbackClient.batch(stmts, mode);
|
|
813
|
+
}
|
|
814
|
+
throw error;
|
|
815
|
+
}
|
|
816
|
+
},
|
|
817
|
+
async migrate(stmts) {
|
|
818
|
+
if (fallbackClient) {
|
|
819
|
+
return fallbackClient.migrate(stmts);
|
|
820
|
+
}
|
|
821
|
+
return adapter.batch(stmts, "deferred");
|
|
822
|
+
},
|
|
823
|
+
async transaction(mode) {
|
|
824
|
+
if (!fallbackClient) {
|
|
825
|
+
throw new Error("Interactive transactions are only supported on the SQLite fallback client.");
|
|
826
|
+
}
|
|
827
|
+
return fallbackClient.transaction(mode);
|
|
828
|
+
},
|
|
829
|
+
async executeMultiple(sql) {
|
|
830
|
+
if (fallbackClient && shouldBypassPostgres(sql)) {
|
|
831
|
+
return fallbackClient.executeMultiple(sql);
|
|
832
|
+
}
|
|
833
|
+
for (const statement of splitSqlStatements(sql)) {
|
|
834
|
+
await adapter.execute(statement);
|
|
835
|
+
}
|
|
836
|
+
},
|
|
837
|
+
async sync() {
|
|
838
|
+
if (fallbackClient) {
|
|
839
|
+
return fallbackClient.sync();
|
|
840
|
+
}
|
|
841
|
+
return { frame_no: 0, frames_synced: 0 };
|
|
842
|
+
},
|
|
843
|
+
close() {
|
|
844
|
+
closed = true;
|
|
845
|
+
prismaClientPromise = null;
|
|
846
|
+
compatibilityBootstrapPromise = null;
|
|
847
|
+
void prisma.$disconnect?.();
|
|
848
|
+
},
|
|
849
|
+
get closed() {
|
|
850
|
+
return closed;
|
|
851
|
+
},
|
|
852
|
+
get protocol() {
|
|
853
|
+
return "prisma-postgres";
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
return adapter;
|
|
857
|
+
}
|
|
858
|
+
var VIEW_MAPPINGS, UPSERT_KEYS, BOOLEAN_COLUMNS_BY_TABLE, BOOLEAN_COLUMN_NAMES, IMMEDIATE_FALLBACK_PATTERNS, prismaClientPromise, compatibilityBootstrapPromise;
|
|
859
|
+
var init_database_adapter = __esm({
|
|
860
|
+
"src/lib/database-adapter.ts"() {
|
|
861
|
+
"use strict";
|
|
862
|
+
VIEW_MAPPINGS = [
|
|
863
|
+
{ view: "memories", source: "memory.memory_records" },
|
|
864
|
+
{ view: "tasks", source: "memory.tasks" },
|
|
865
|
+
{ view: "behaviors", source: "memory.behaviors" },
|
|
866
|
+
{ view: "entities", source: "memory.entities" },
|
|
867
|
+
{ view: "relationships", source: "memory.relationships" },
|
|
868
|
+
{ view: "entity_memories", source: "memory.entity_memories" },
|
|
869
|
+
{ view: "entity_aliases", source: "memory.entity_aliases" },
|
|
870
|
+
{ view: "notifications", source: "memory.notifications" },
|
|
871
|
+
{ view: "messages", source: "memory.messages" },
|
|
872
|
+
{ view: "users", source: "wiki.users" },
|
|
873
|
+
{ view: "workspaces", source: "wiki.workspaces" },
|
|
874
|
+
{ view: "workspace_users", source: "wiki.workspace_users" },
|
|
875
|
+
{ view: "documents", source: "wiki.workspace_documents" },
|
|
876
|
+
{ view: "chats", source: "wiki.workspace_chats" }
|
|
877
|
+
];
|
|
878
|
+
UPSERT_KEYS = {
|
|
879
|
+
memories: ["id"],
|
|
880
|
+
tasks: ["id"],
|
|
881
|
+
behaviors: ["id"],
|
|
882
|
+
entities: ["id"],
|
|
883
|
+
relationships: ["id"],
|
|
884
|
+
entity_aliases: ["alias"],
|
|
885
|
+
notifications: ["id"],
|
|
886
|
+
messages: ["id"],
|
|
887
|
+
users: ["id"],
|
|
888
|
+
workspaces: ["id"],
|
|
889
|
+
workspace_users: ["id"],
|
|
890
|
+
documents: ["id"],
|
|
891
|
+
chats: ["id"]
|
|
892
|
+
};
|
|
893
|
+
BOOLEAN_COLUMNS_BY_TABLE = {
|
|
894
|
+
memories: /* @__PURE__ */ new Set(["has_error", "draft"]),
|
|
895
|
+
behaviors: /* @__PURE__ */ new Set(["active"]),
|
|
896
|
+
notifications: /* @__PURE__ */ new Set(["read"]),
|
|
897
|
+
users: /* @__PURE__ */ new Set(["has_personal_memory"])
|
|
898
|
+
};
|
|
899
|
+
BOOLEAN_COLUMN_NAMES = new Set(
|
|
900
|
+
Object.values(BOOLEAN_COLUMNS_BY_TABLE).flatMap((cols) => [...cols])
|
|
901
|
+
);
|
|
902
|
+
IMMEDIATE_FALLBACK_PATTERNS = [
|
|
903
|
+
/\bPRAGMA\b/i,
|
|
904
|
+
/\bsqlite_master\b/i,
|
|
905
|
+
/(?:^|[.\s])(?:memories|conversations|entities)_fts\b/i,
|
|
906
|
+
/\bMATCH\b/i,
|
|
907
|
+
/\bvector_distance_cos\s*\(/i,
|
|
908
|
+
/\bjson_extract\s*\(/i,
|
|
909
|
+
/\bjulianday\s*\(/i,
|
|
910
|
+
/\bstrftime\s*\(/i,
|
|
911
|
+
/\blast_insert_rowid\s*\(/i
|
|
912
|
+
];
|
|
913
|
+
prismaClientPromise = null;
|
|
914
|
+
compatibilityBootstrapPromise = null;
|
|
290
915
|
}
|
|
291
916
|
});
|
|
292
917
|
|
|
293
918
|
// src/lib/database.ts
|
|
294
919
|
import { createClient } from "@libsql/client";
|
|
295
920
|
async function initDatabase(config) {
|
|
921
|
+
if (_walCheckpointTimer) {
|
|
922
|
+
clearInterval(_walCheckpointTimer);
|
|
923
|
+
_walCheckpointTimer = null;
|
|
924
|
+
}
|
|
925
|
+
if (_daemonClient) {
|
|
926
|
+
_daemonClient.close();
|
|
927
|
+
_daemonClient = null;
|
|
928
|
+
}
|
|
929
|
+
if (_adapterClient && _adapterClient !== _resilientClient) {
|
|
930
|
+
_adapterClient.close();
|
|
931
|
+
}
|
|
932
|
+
_adapterClient = null;
|
|
296
933
|
if (_client) {
|
|
297
934
|
_client.close();
|
|
298
935
|
_client = null;
|
|
@@ -306,6 +943,7 @@ async function initDatabase(config) {
|
|
|
306
943
|
}
|
|
307
944
|
_client = createClient(opts);
|
|
308
945
|
_resilientClient = wrapWithRetry(_client);
|
|
946
|
+
_adapterClient = _resilientClient;
|
|
309
947
|
_client.execute("PRAGMA busy_timeout = 30000").catch(() => {
|
|
310
948
|
});
|
|
311
949
|
_client.execute("PRAGMA journal_mode = WAL").catch(() => {
|
|
@@ -316,11 +954,17 @@ async function initDatabase(config) {
|
|
|
316
954
|
});
|
|
317
955
|
}, 3e4);
|
|
318
956
|
_walCheckpointTimer.unref();
|
|
957
|
+
if (process.env.DATABASE_URL) {
|
|
958
|
+
_adapterClient = await createPrismaDbAdapter(_resilientClient);
|
|
959
|
+
}
|
|
319
960
|
}
|
|
320
961
|
function getClient() {
|
|
321
|
-
if (!
|
|
962
|
+
if (!_adapterClient) {
|
|
322
963
|
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
323
964
|
}
|
|
965
|
+
if (process.env.DATABASE_URL) {
|
|
966
|
+
return _adapterClient;
|
|
967
|
+
}
|
|
324
968
|
if (process.env.EXE_IS_DAEMON === "1") {
|
|
325
969
|
return _resilientClient;
|
|
326
970
|
}
|
|
@@ -613,6 +1257,7 @@ async function ensureSchema() {
|
|
|
613
1257
|
project TEXT NOT NULL,
|
|
614
1258
|
summary TEXT NOT NULL,
|
|
615
1259
|
task_file TEXT,
|
|
1260
|
+
session_scope TEXT,
|
|
616
1261
|
read INTEGER NOT NULL DEFAULT 0,
|
|
617
1262
|
created_at TEXT NOT NULL
|
|
618
1263
|
);
|
|
@@ -621,7 +1266,7 @@ async function ensureSchema() {
|
|
|
621
1266
|
ON notifications(read);
|
|
622
1267
|
|
|
623
1268
|
CREATE INDEX IF NOT EXISTS idx_notifications_agent
|
|
624
|
-
ON notifications(agent_id);
|
|
1269
|
+
ON notifications(agent_id, session_scope);
|
|
625
1270
|
|
|
626
1271
|
CREATE INDEX IF NOT EXISTS idx_notifications_task_file
|
|
627
1272
|
ON notifications(task_file);
|
|
@@ -659,6 +1304,7 @@ async function ensureSchema() {
|
|
|
659
1304
|
target_agent TEXT NOT NULL,
|
|
660
1305
|
target_project TEXT,
|
|
661
1306
|
target_device TEXT NOT NULL DEFAULT 'local',
|
|
1307
|
+
session_scope TEXT,
|
|
662
1308
|
content TEXT NOT NULL,
|
|
663
1309
|
priority TEXT DEFAULT 'normal',
|
|
664
1310
|
status TEXT DEFAULT 'pending',
|
|
@@ -672,10 +1318,31 @@ async function ensureSchema() {
|
|
|
672
1318
|
);
|
|
673
1319
|
|
|
674
1320
|
CREATE INDEX IF NOT EXISTS idx_messages_target
|
|
675
|
-
ON messages(target_agent, status);
|
|
1321
|
+
ON messages(target_agent, session_scope, status);
|
|
676
1322
|
|
|
677
1323
|
CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
|
|
678
|
-
ON messages(target_agent, from_agent, server_seq);
|
|
1324
|
+
ON messages(target_agent, session_scope, from_agent, server_seq);
|
|
1325
|
+
`);
|
|
1326
|
+
try {
|
|
1327
|
+
await client.execute({
|
|
1328
|
+
sql: `ALTER TABLE notifications ADD COLUMN session_scope TEXT`,
|
|
1329
|
+
args: []
|
|
1330
|
+
});
|
|
1331
|
+
} catch {
|
|
1332
|
+
}
|
|
1333
|
+
try {
|
|
1334
|
+
await client.execute({
|
|
1335
|
+
sql: `ALTER TABLE messages ADD COLUMN session_scope TEXT`,
|
|
1336
|
+
args: []
|
|
1337
|
+
});
|
|
1338
|
+
} catch {
|
|
1339
|
+
}
|
|
1340
|
+
await client.executeMultiple(`
|
|
1341
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_agent_scope_read
|
|
1342
|
+
ON notifications(agent_id, session_scope, read, created_at);
|
|
1343
|
+
|
|
1344
|
+
CREATE INDEX IF NOT EXISTS idx_messages_target_scope_status
|
|
1345
|
+
ON messages(target_agent, session_scope, status, created_at);
|
|
679
1346
|
`);
|
|
680
1347
|
try {
|
|
681
1348
|
await client.execute({
|
|
@@ -1259,17 +1926,26 @@ async function ensureSchema() {
|
|
|
1259
1926
|
} catch {
|
|
1260
1927
|
}
|
|
1261
1928
|
}
|
|
1929
|
+
try {
|
|
1930
|
+
await client.execute({
|
|
1931
|
+
sql: `UPDATE tasks SET status = 'closed' WHERE status = 'done' AND result IS NOT NULL`,
|
|
1932
|
+
args: []
|
|
1933
|
+
});
|
|
1934
|
+
} catch {
|
|
1935
|
+
}
|
|
1262
1936
|
}
|
|
1263
|
-
var _client, _resilientClient, _walCheckpointTimer, _daemonClient, initTurso;
|
|
1937
|
+
var _client, _resilientClient, _walCheckpointTimer, _daemonClient, _adapterClient, initTurso;
|
|
1264
1938
|
var init_database = __esm({
|
|
1265
1939
|
"src/lib/database.ts"() {
|
|
1266
1940
|
"use strict";
|
|
1267
1941
|
init_db_retry();
|
|
1268
1942
|
init_employees();
|
|
1943
|
+
init_database_adapter();
|
|
1269
1944
|
_client = null;
|
|
1270
1945
|
_resilientClient = null;
|
|
1271
1946
|
_walCheckpointTimer = null;
|
|
1272
1947
|
_daemonClient = null;
|
|
1948
|
+
_adapterClient = null;
|
|
1273
1949
|
initTurso = initDatabase;
|
|
1274
1950
|
}
|
|
1275
1951
|
});
|
|
@@ -1279,6 +1955,7 @@ var shard_manager_exports = {};
|
|
|
1279
1955
|
__export(shard_manager_exports, {
|
|
1280
1956
|
disposeShards: () => disposeShards,
|
|
1281
1957
|
ensureShardSchema: () => ensureShardSchema,
|
|
1958
|
+
getOpenShardCount: () => getOpenShardCount,
|
|
1282
1959
|
getReadyShardClient: () => getReadyShardClient,
|
|
1283
1960
|
getShardClient: () => getShardClient,
|
|
1284
1961
|
getShardsDir: () => getShardsDir,
|
|
@@ -1287,15 +1964,18 @@ __export(shard_manager_exports, {
|
|
|
1287
1964
|
listShards: () => listShards,
|
|
1288
1965
|
shardExists: () => shardExists
|
|
1289
1966
|
});
|
|
1290
|
-
import
|
|
1291
|
-
import { existsSync as
|
|
1967
|
+
import path5 from "path";
|
|
1968
|
+
import { existsSync as existsSync5, mkdirSync as mkdirSync2, readdirSync } from "fs";
|
|
1292
1969
|
import { createClient as createClient2 } from "@libsql/client";
|
|
1293
1970
|
function initShardManager(encryptionKey) {
|
|
1294
1971
|
_encryptionKey = encryptionKey;
|
|
1295
|
-
if (!
|
|
1296
|
-
|
|
1972
|
+
if (!existsSync5(SHARDS_DIR)) {
|
|
1973
|
+
mkdirSync2(SHARDS_DIR, { recursive: true });
|
|
1297
1974
|
}
|
|
1298
1975
|
_shardingEnabled = true;
|
|
1976
|
+
if (_evictionTimer) clearInterval(_evictionTimer);
|
|
1977
|
+
_evictionTimer = setInterval(evictIdleShards, EVICTION_INTERVAL_MS);
|
|
1978
|
+
_evictionTimer.unref();
|
|
1299
1979
|
}
|
|
1300
1980
|
function isShardingEnabled() {
|
|
1301
1981
|
return _shardingEnabled;
|
|
@@ -1312,21 +1992,28 @@ function getShardClient(projectName) {
|
|
|
1312
1992
|
throw new Error(`Invalid project name for shard: "${projectName}"`);
|
|
1313
1993
|
}
|
|
1314
1994
|
const cached = _shards.get(safeName);
|
|
1315
|
-
if (cached)
|
|
1316
|
-
|
|
1995
|
+
if (cached) {
|
|
1996
|
+
_shardLastAccess.set(safeName, Date.now());
|
|
1997
|
+
return cached;
|
|
1998
|
+
}
|
|
1999
|
+
while (_shards.size >= MAX_OPEN_SHARDS) {
|
|
2000
|
+
evictLRU();
|
|
2001
|
+
}
|
|
2002
|
+
const dbPath = path5.join(SHARDS_DIR, `${safeName}.db`);
|
|
1317
2003
|
const client = createClient2({
|
|
1318
2004
|
url: `file:${dbPath}`,
|
|
1319
2005
|
encryptionKey: _encryptionKey
|
|
1320
2006
|
});
|
|
1321
2007
|
_shards.set(safeName, client);
|
|
2008
|
+
_shardLastAccess.set(safeName, Date.now());
|
|
1322
2009
|
return client;
|
|
1323
2010
|
}
|
|
1324
2011
|
function shardExists(projectName) {
|
|
1325
2012
|
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
1326
|
-
return
|
|
2013
|
+
return existsSync5(path5.join(SHARDS_DIR, `${safeName}.db`));
|
|
1327
2014
|
}
|
|
1328
2015
|
function listShards() {
|
|
1329
|
-
if (!
|
|
2016
|
+
if (!existsSync5(SHARDS_DIR)) return [];
|
|
1330
2017
|
return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
1331
2018
|
}
|
|
1332
2019
|
async function ensureShardSchema(client) {
|
|
@@ -1378,6 +2065,8 @@ async function ensureShardSchema(client) {
|
|
|
1378
2065
|
for (const col of [
|
|
1379
2066
|
"ALTER TABLE memories ADD COLUMN task_id TEXT",
|
|
1380
2067
|
"ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
|
|
2068
|
+
"ALTER TABLE memories ADD COLUMN author_device_id TEXT",
|
|
2069
|
+
"ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'",
|
|
1381
2070
|
"ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
|
|
1382
2071
|
"ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
|
|
1383
2072
|
"ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
|
|
@@ -1400,7 +2089,23 @@ async function ensureShardSchema(client) {
|
|
|
1400
2089
|
// MS-11: draft staging, MS-6a: memory_type, MS-7: trajectory
|
|
1401
2090
|
"ALTER TABLE memories ADD COLUMN draft INTEGER DEFAULT 0",
|
|
1402
2091
|
"ALTER TABLE memories ADD COLUMN memory_type TEXT DEFAULT 'raw'",
|
|
1403
|
-
"ALTER TABLE memories ADD COLUMN trajectory TEXT"
|
|
2092
|
+
"ALTER TABLE memories ADD COLUMN trajectory TEXT",
|
|
2093
|
+
// Metadata enrichment columns (must match database.ts)
|
|
2094
|
+
"ALTER TABLE memories ADD COLUMN intent TEXT",
|
|
2095
|
+
"ALTER TABLE memories ADD COLUMN outcome TEXT",
|
|
2096
|
+
"ALTER TABLE memories ADD COLUMN domain TEXT",
|
|
2097
|
+
"ALTER TABLE memories ADD COLUMN referenced_entities TEXT",
|
|
2098
|
+
"ALTER TABLE memories ADD COLUMN retrieval_count INTEGER DEFAULT 0",
|
|
2099
|
+
"ALTER TABLE memories ADD COLUMN chain_position TEXT",
|
|
2100
|
+
"ALTER TABLE memories ADD COLUMN review_status TEXT",
|
|
2101
|
+
"ALTER TABLE memories ADD COLUMN context_window_pct INTEGER",
|
|
2102
|
+
"ALTER TABLE memories ADD COLUMN file_paths TEXT",
|
|
2103
|
+
"ALTER TABLE memories ADD COLUMN commit_hash TEXT",
|
|
2104
|
+
"ALTER TABLE memories ADD COLUMN duration_ms INTEGER",
|
|
2105
|
+
"ALTER TABLE memories ADD COLUMN token_cost REAL",
|
|
2106
|
+
"ALTER TABLE memories ADD COLUMN audience TEXT",
|
|
2107
|
+
"ALTER TABLE memories ADD COLUMN language_type TEXT",
|
|
2108
|
+
"ALTER TABLE memories ADD COLUMN parent_memory_id TEXT"
|
|
1404
2109
|
]) {
|
|
1405
2110
|
try {
|
|
1406
2111
|
await client.execute(col);
|
|
@@ -1499,21 +2204,69 @@ async function getReadyShardClient(projectName) {
|
|
|
1499
2204
|
await ensureShardSchema(client);
|
|
1500
2205
|
return client;
|
|
1501
2206
|
}
|
|
2207
|
+
function evictLRU() {
|
|
2208
|
+
let oldest = null;
|
|
2209
|
+
let oldestTime = Infinity;
|
|
2210
|
+
for (const [name, time] of _shardLastAccess) {
|
|
2211
|
+
if (time < oldestTime) {
|
|
2212
|
+
oldestTime = time;
|
|
2213
|
+
oldest = name;
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
if (oldest) {
|
|
2217
|
+
const client = _shards.get(oldest);
|
|
2218
|
+
if (client) {
|
|
2219
|
+
client.close();
|
|
2220
|
+
}
|
|
2221
|
+
_shards.delete(oldest);
|
|
2222
|
+
_shardLastAccess.delete(oldest);
|
|
2223
|
+
}
|
|
2224
|
+
}
|
|
2225
|
+
function evictIdleShards() {
|
|
2226
|
+
const now = Date.now();
|
|
2227
|
+
const toEvict = [];
|
|
2228
|
+
for (const [name, lastAccess] of _shardLastAccess) {
|
|
2229
|
+
if (now - lastAccess > SHARD_IDLE_MS) {
|
|
2230
|
+
toEvict.push(name);
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
for (const name of toEvict) {
|
|
2234
|
+
const client = _shards.get(name);
|
|
2235
|
+
if (client) {
|
|
2236
|
+
client.close();
|
|
2237
|
+
}
|
|
2238
|
+
_shards.delete(name);
|
|
2239
|
+
_shardLastAccess.delete(name);
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
function getOpenShardCount() {
|
|
2243
|
+
return _shards.size;
|
|
2244
|
+
}
|
|
1502
2245
|
function disposeShards() {
|
|
2246
|
+
if (_evictionTimer) {
|
|
2247
|
+
clearInterval(_evictionTimer);
|
|
2248
|
+
_evictionTimer = null;
|
|
2249
|
+
}
|
|
1503
2250
|
for (const [, client] of _shards) {
|
|
1504
2251
|
client.close();
|
|
1505
2252
|
}
|
|
1506
2253
|
_shards.clear();
|
|
2254
|
+
_shardLastAccess.clear();
|
|
1507
2255
|
_shardingEnabled = false;
|
|
1508
2256
|
_encryptionKey = null;
|
|
1509
2257
|
}
|
|
1510
|
-
var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
|
|
2258
|
+
var SHARDS_DIR, SHARD_IDLE_MS, MAX_OPEN_SHARDS, EVICTION_INTERVAL_MS, _shards, _shardLastAccess, _evictionTimer, _encryptionKey, _shardingEnabled;
|
|
1511
2259
|
var init_shard_manager = __esm({
|
|
1512
2260
|
"src/lib/shard-manager.ts"() {
|
|
1513
2261
|
"use strict";
|
|
1514
2262
|
init_config();
|
|
1515
|
-
SHARDS_DIR =
|
|
2263
|
+
SHARDS_DIR = path5.join(EXE_AI_DIR, "shards");
|
|
2264
|
+
SHARD_IDLE_MS = 5 * 60 * 1e3;
|
|
2265
|
+
MAX_OPEN_SHARDS = 10;
|
|
2266
|
+
EVICTION_INTERVAL_MS = 60 * 1e3;
|
|
1516
2267
|
_shards = /* @__PURE__ */ new Map();
|
|
2268
|
+
_shardLastAccess = /* @__PURE__ */ new Map();
|
|
2269
|
+
_evictionTimer = null;
|
|
1517
2270
|
_encryptionKey = null;
|
|
1518
2271
|
_shardingEnabled = false;
|
|
1519
2272
|
}
|
|
@@ -1706,13 +2459,50 @@ ${p.content}`).join("\n\n");
|
|
|
1706
2459
|
}
|
|
1707
2460
|
});
|
|
1708
2461
|
|
|
2462
|
+
// src/lib/daemon-auth.ts
|
|
2463
|
+
import crypto from "crypto";
|
|
2464
|
+
import path6 from "path";
|
|
2465
|
+
import { existsSync as existsSync6, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
2466
|
+
function normalizeToken(token) {
|
|
2467
|
+
if (!token) return null;
|
|
2468
|
+
const trimmed = token.trim();
|
|
2469
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
2470
|
+
}
|
|
2471
|
+
function readDaemonToken() {
|
|
2472
|
+
try {
|
|
2473
|
+
if (!existsSync6(DAEMON_TOKEN_PATH)) return null;
|
|
2474
|
+
return normalizeToken(readFileSync3(DAEMON_TOKEN_PATH, "utf8"));
|
|
2475
|
+
} catch {
|
|
2476
|
+
return null;
|
|
2477
|
+
}
|
|
2478
|
+
}
|
|
2479
|
+
function ensureDaemonToken(seed) {
|
|
2480
|
+
const existing = readDaemonToken();
|
|
2481
|
+
if (existing) return existing;
|
|
2482
|
+
const token = normalizeToken(seed) ?? crypto.randomBytes(32).toString("hex");
|
|
2483
|
+
ensurePrivateDirSync(EXE_AI_DIR);
|
|
2484
|
+
writeFileSync2(DAEMON_TOKEN_PATH, `${token}
|
|
2485
|
+
`, "utf8");
|
|
2486
|
+
enforcePrivateFileSync(DAEMON_TOKEN_PATH);
|
|
2487
|
+
return token;
|
|
2488
|
+
}
|
|
2489
|
+
var DAEMON_TOKEN_PATH;
|
|
2490
|
+
var init_daemon_auth = __esm({
|
|
2491
|
+
"src/lib/daemon-auth.ts"() {
|
|
2492
|
+
"use strict";
|
|
2493
|
+
init_config();
|
|
2494
|
+
init_secure_files();
|
|
2495
|
+
DAEMON_TOKEN_PATH = path6.join(EXE_AI_DIR, "exed.token");
|
|
2496
|
+
}
|
|
2497
|
+
});
|
|
2498
|
+
|
|
1709
2499
|
// src/lib/exe-daemon-client.ts
|
|
1710
2500
|
import net from "net";
|
|
1711
|
-
import
|
|
2501
|
+
import os5 from "os";
|
|
1712
2502
|
import { spawn } from "child_process";
|
|
1713
2503
|
import { randomUUID as randomUUID2 } from "crypto";
|
|
1714
|
-
import { existsSync as
|
|
1715
|
-
import
|
|
2504
|
+
import { existsSync as existsSync7, unlinkSync as unlinkSync2, readFileSync as readFileSync4, openSync, closeSync, statSync } from "fs";
|
|
2505
|
+
import path7 from "path";
|
|
1716
2506
|
import { fileURLToPath } from "url";
|
|
1717
2507
|
function handleData(chunk) {
|
|
1718
2508
|
_buffer += chunk.toString();
|
|
@@ -1740,9 +2530,9 @@ function handleData(chunk) {
|
|
|
1740
2530
|
}
|
|
1741
2531
|
}
|
|
1742
2532
|
function cleanupStaleFiles() {
|
|
1743
|
-
if (
|
|
2533
|
+
if (existsSync7(PID_PATH)) {
|
|
1744
2534
|
try {
|
|
1745
|
-
const pid = parseInt(
|
|
2535
|
+
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
1746
2536
|
if (pid > 0) {
|
|
1747
2537
|
try {
|
|
1748
2538
|
process.kill(pid, 0);
|
|
@@ -1763,17 +2553,17 @@ function cleanupStaleFiles() {
|
|
|
1763
2553
|
}
|
|
1764
2554
|
}
|
|
1765
2555
|
function findPackageRoot() {
|
|
1766
|
-
let dir =
|
|
1767
|
-
const { root } =
|
|
2556
|
+
let dir = path7.dirname(fileURLToPath(import.meta.url));
|
|
2557
|
+
const { root } = path7.parse(dir);
|
|
1768
2558
|
while (dir !== root) {
|
|
1769
|
-
if (
|
|
1770
|
-
dir =
|
|
2559
|
+
if (existsSync7(path7.join(dir, "package.json"))) return dir;
|
|
2560
|
+
dir = path7.dirname(dir);
|
|
1771
2561
|
}
|
|
1772
2562
|
return null;
|
|
1773
2563
|
}
|
|
1774
2564
|
function spawnDaemon() {
|
|
1775
|
-
const freeGB =
|
|
1776
|
-
const totalGB =
|
|
2565
|
+
const freeGB = os5.freemem() / (1024 * 1024 * 1024);
|
|
2566
|
+
const totalGB = os5.totalmem() / (1024 * 1024 * 1024);
|
|
1777
2567
|
if (totalGB <= 8) {
|
|
1778
2568
|
process.stderr.write(
|
|
1779
2569
|
`[exed-client] SKIP: ${totalGB.toFixed(0)}GB system \u2014 embedding daemon disabled. Using keyword search only. Minimum 16GB recommended for vector search.
|
|
@@ -1793,16 +2583,17 @@ function spawnDaemon() {
|
|
|
1793
2583
|
process.stderr.write("[exed-client] WARN: cannot find package root\n");
|
|
1794
2584
|
return;
|
|
1795
2585
|
}
|
|
1796
|
-
const daemonPath =
|
|
1797
|
-
if (!
|
|
2586
|
+
const daemonPath = path7.join(pkgRoot, "dist", "lib", "exe-daemon.js");
|
|
2587
|
+
if (!existsSync7(daemonPath)) {
|
|
1798
2588
|
process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
|
|
1799
2589
|
`);
|
|
1800
2590
|
return;
|
|
1801
2591
|
}
|
|
1802
2592
|
const resolvedPath = daemonPath;
|
|
2593
|
+
const daemonToken = ensureDaemonToken(process.env[DAEMON_TOKEN_ENV] ?? null);
|
|
1803
2594
|
process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
|
|
1804
2595
|
`);
|
|
1805
|
-
const logPath =
|
|
2596
|
+
const logPath = path7.join(path7.dirname(SOCKET_PATH), "exed.log");
|
|
1806
2597
|
let stderrFd = "ignore";
|
|
1807
2598
|
try {
|
|
1808
2599
|
stderrFd = openSync(logPath, "a");
|
|
@@ -1820,7 +2611,8 @@ function spawnDaemon() {
|
|
|
1820
2611
|
TMUX_PANE: void 0,
|
|
1821
2612
|
// Prevents resolveExeSession() from scoping to one session
|
|
1822
2613
|
EXE_DAEMON_SOCK: SOCKET_PATH,
|
|
1823
|
-
EXE_DAEMON_PID: PID_PATH
|
|
2614
|
+
EXE_DAEMON_PID: PID_PATH,
|
|
2615
|
+
[DAEMON_TOKEN_ENV]: daemonToken
|
|
1824
2616
|
}
|
|
1825
2617
|
});
|
|
1826
2618
|
child.unref();
|
|
@@ -1930,13 +2722,14 @@ function sendDaemonRequest(payload, timeoutMs = REQUEST_TIMEOUT_MS) {
|
|
|
1930
2722
|
return;
|
|
1931
2723
|
}
|
|
1932
2724
|
const id = randomUUID2();
|
|
2725
|
+
const token = process.env[DAEMON_TOKEN_ENV] ?? readDaemonToken();
|
|
1933
2726
|
const timer = setTimeout(() => {
|
|
1934
2727
|
_pending.delete(id);
|
|
1935
2728
|
resolve({ error: "Request timeout" });
|
|
1936
2729
|
}, timeoutMs);
|
|
1937
2730
|
_pending.set(id, { resolve, timer });
|
|
1938
2731
|
try {
|
|
1939
|
-
_socket.write(JSON.stringify({ id, ...payload }) + "\n");
|
|
2732
|
+
_socket.write(JSON.stringify({ id, token, ...payload }) + "\n");
|
|
1940
2733
|
} catch {
|
|
1941
2734
|
clearTimeout(timer);
|
|
1942
2735
|
_pending.delete(id);
|
|
@@ -1953,91 +2746,146 @@ async function pingDaemon() {
|
|
|
1953
2746
|
return null;
|
|
1954
2747
|
}
|
|
1955
2748
|
function killAndRespawnDaemon() {
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
2749
|
+
if (!acquireSpawnLock()) {
|
|
2750
|
+
process.stderr.write("[exed-client] Another process is already restarting daemon \u2014 skipping\n");
|
|
2751
|
+
if (_socket) {
|
|
2752
|
+
_socket.destroy();
|
|
2753
|
+
_socket = null;
|
|
2754
|
+
}
|
|
2755
|
+
_connected = false;
|
|
2756
|
+
_buffer = "";
|
|
2757
|
+
return;
|
|
2758
|
+
}
|
|
2759
|
+
try {
|
|
2760
|
+
process.stderr.write("[exed-client] Killing daemon for restart...\n");
|
|
2761
|
+
if (existsSync7(PID_PATH)) {
|
|
2762
|
+
try {
|
|
2763
|
+
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
2764
|
+
if (pid > 0) {
|
|
2765
|
+
try {
|
|
2766
|
+
process.kill(pid, "SIGKILL");
|
|
2767
|
+
} catch {
|
|
2768
|
+
}
|
|
1964
2769
|
}
|
|
2770
|
+
} catch {
|
|
1965
2771
|
}
|
|
2772
|
+
}
|
|
2773
|
+
if (_socket) {
|
|
2774
|
+
_socket.destroy();
|
|
2775
|
+
_socket = null;
|
|
2776
|
+
}
|
|
2777
|
+
_connected = false;
|
|
2778
|
+
_buffer = "";
|
|
2779
|
+
try {
|
|
2780
|
+
unlinkSync2(PID_PATH);
|
|
1966
2781
|
} catch {
|
|
1967
2782
|
}
|
|
2783
|
+
try {
|
|
2784
|
+
unlinkSync2(SOCKET_PATH);
|
|
2785
|
+
} catch {
|
|
2786
|
+
}
|
|
2787
|
+
spawnDaemon();
|
|
2788
|
+
} finally {
|
|
2789
|
+
releaseSpawnLock();
|
|
1968
2790
|
}
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
_socket = null;
|
|
1972
|
-
}
|
|
1973
|
-
_connected = false;
|
|
1974
|
-
_buffer = "";
|
|
2791
|
+
}
|
|
2792
|
+
function isDaemonTooYoung() {
|
|
1975
2793
|
try {
|
|
1976
|
-
|
|
2794
|
+
const stat = statSync(PID_PATH);
|
|
2795
|
+
return Date.now() - stat.mtimeMs < MIN_DAEMON_AGE_MS;
|
|
1977
2796
|
} catch {
|
|
2797
|
+
return false;
|
|
1978
2798
|
}
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
2799
|
+
}
|
|
2800
|
+
async function retryThenRestart(doRequest, label) {
|
|
2801
|
+
const result = await doRequest();
|
|
2802
|
+
if (!result.error) {
|
|
2803
|
+
_consecutiveFailures = 0;
|
|
2804
|
+
return result;
|
|
2805
|
+
}
|
|
2806
|
+
_consecutiveFailures++;
|
|
2807
|
+
for (let i = 0; i < MAX_RETRIES_BEFORE_RESTART; i++) {
|
|
2808
|
+
const delayMs = RETRY_DELAYS_MS[i] ?? 5e3;
|
|
2809
|
+
process.stderr.write(`[exed-client] ${label} failed (${result.error}), retry ${i + 1}/${MAX_RETRIES_BEFORE_RESTART} in ${delayMs}ms
|
|
2810
|
+
`);
|
|
2811
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
2812
|
+
if (!_connected) {
|
|
2813
|
+
if (!await connectToSocket()) continue;
|
|
2814
|
+
}
|
|
2815
|
+
const retry = await doRequest();
|
|
2816
|
+
if (!retry.error) {
|
|
2817
|
+
_consecutiveFailures = 0;
|
|
2818
|
+
return retry;
|
|
2819
|
+
}
|
|
2820
|
+
_consecutiveFailures++;
|
|
2821
|
+
}
|
|
2822
|
+
if (isDaemonTooYoung()) {
|
|
2823
|
+
process.stderr.write(`[exed-client] ${label}: daemon too young (< ${MIN_DAEMON_AGE_MS / 1e3}s) \u2014 skipping restart
|
|
2824
|
+
`);
|
|
2825
|
+
return { error: result.error };
|
|
1982
2826
|
}
|
|
1983
|
-
|
|
2827
|
+
process.stderr.write(`[exed-client] ${label}: ${_consecutiveFailures} consecutive failures \u2014 restarting daemon
|
|
2828
|
+
`);
|
|
2829
|
+
killAndRespawnDaemon();
|
|
2830
|
+
const start = Date.now();
|
|
2831
|
+
let delay2 = 200;
|
|
2832
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
2833
|
+
await new Promise((r) => setTimeout(r, delay2));
|
|
2834
|
+
if (await connectToSocket()) break;
|
|
2835
|
+
delay2 = Math.min(delay2 * 2, 3e3);
|
|
2836
|
+
}
|
|
2837
|
+
if (!_connected) return { error: "Daemon restart failed" };
|
|
2838
|
+
const final = await doRequest();
|
|
2839
|
+
if (!final.error) _consecutiveFailures = 0;
|
|
2840
|
+
return final;
|
|
1984
2841
|
}
|
|
1985
2842
|
async function embedViaClient(text, priority = "high") {
|
|
1986
2843
|
if (!_connected && !await connectEmbedDaemon()) return null;
|
|
1987
2844
|
_requestCount++;
|
|
1988
2845
|
if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
|
|
1989
2846
|
const health = await pingDaemon();
|
|
1990
|
-
if (!health) {
|
|
2847
|
+
if (!health && !isDaemonTooYoung()) {
|
|
1991
2848
|
process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
|
|
1992
2849
|
`);
|
|
1993
2850
|
killAndRespawnDaemon();
|
|
1994
2851
|
const start = Date.now();
|
|
1995
|
-
let
|
|
2852
|
+
let d = 200;
|
|
1996
2853
|
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1997
|
-
await new Promise((r) => setTimeout(r,
|
|
2854
|
+
await new Promise((r) => setTimeout(r, d));
|
|
1998
2855
|
if (await connectToSocket()) break;
|
|
1999
|
-
|
|
2856
|
+
d = Math.min(d * 2, 3e3);
|
|
2000
2857
|
}
|
|
2001
2858
|
if (!_connected) return null;
|
|
2002
2859
|
}
|
|
2003
2860
|
}
|
|
2004
|
-
const result = await
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
killAndRespawnDaemon();
|
|
2010
|
-
const start = Date.now();
|
|
2011
|
-
let delay2 = 200;
|
|
2012
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
2013
|
-
await new Promise((r) => setTimeout(r, delay2));
|
|
2014
|
-
if (await connectToSocket()) break;
|
|
2015
|
-
delay2 = Math.min(delay2 * 2, 3e3);
|
|
2016
|
-
}
|
|
2017
|
-
if (!_connected) return null;
|
|
2018
|
-
const retry = await sendRequest([text], priority);
|
|
2019
|
-
if (!retry.error && retry.vectors?.[0]) return retry.vectors[0];
|
|
2020
|
-
process.stderr.write(`[exed-client] Embed retry also failed: ${retry.error ?? "no vector"}
|
|
2021
|
-
`);
|
|
2022
|
-
}
|
|
2023
|
-
return null;
|
|
2861
|
+
const result = await retryThenRestart(
|
|
2862
|
+
() => sendRequest([text], priority),
|
|
2863
|
+
"Embed"
|
|
2864
|
+
);
|
|
2865
|
+
return !result.error && result.vectors?.[0] ? result.vectors[0] : null;
|
|
2024
2866
|
}
|
|
2025
|
-
var SOCKET_PATH, PID_PATH, SPAWN_LOCK_PATH, SPAWN_LOCK_STALE_MS, CONNECT_TIMEOUT_MS, REQUEST_TIMEOUT_MS, _socket, _connected, _buffer, _requestCount, HEALTH_CHECK_INTERVAL, _pending, MAX_BUFFER;
|
|
2867
|
+
var SOCKET_PATH, PID_PATH, SPAWN_LOCK_PATH, SPAWN_LOCK_STALE_MS, CONNECT_TIMEOUT_MS, REQUEST_TIMEOUT_MS, DAEMON_TOKEN_ENV, _socket, _connected, _buffer, _requestCount, _consecutiveFailures, HEALTH_CHECK_INTERVAL, MAX_RETRIES_BEFORE_RESTART, RETRY_DELAYS_MS, MIN_DAEMON_AGE_MS, _pending, MAX_BUFFER;
|
|
2026
2868
|
var init_exe_daemon_client = __esm({
|
|
2027
2869
|
"src/lib/exe-daemon-client.ts"() {
|
|
2028
2870
|
"use strict";
|
|
2029
2871
|
init_config();
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2872
|
+
init_daemon_auth();
|
|
2873
|
+
SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path7.join(EXE_AI_DIR, "exed.sock");
|
|
2874
|
+
PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path7.join(EXE_AI_DIR, "exed.pid");
|
|
2875
|
+
SPAWN_LOCK_PATH = path7.join(EXE_AI_DIR, "exed-spawn.lock");
|
|
2033
2876
|
SPAWN_LOCK_STALE_MS = 3e4;
|
|
2034
2877
|
CONNECT_TIMEOUT_MS = 15e3;
|
|
2035
2878
|
REQUEST_TIMEOUT_MS = 3e4;
|
|
2879
|
+
DAEMON_TOKEN_ENV = "EXE_DAEMON_TOKEN";
|
|
2036
2880
|
_socket = null;
|
|
2037
2881
|
_connected = false;
|
|
2038
2882
|
_buffer = "";
|
|
2039
2883
|
_requestCount = 0;
|
|
2884
|
+
_consecutiveFailures = 0;
|
|
2040
2885
|
HEALTH_CHECK_INTERVAL = 100;
|
|
2886
|
+
MAX_RETRIES_BEFORE_RESTART = 3;
|
|
2887
|
+
RETRY_DELAYS_MS = [1e3, 3e3, 5e3];
|
|
2888
|
+
MIN_DAEMON_AGE_MS = 3e4;
|
|
2041
2889
|
_pending = /* @__PURE__ */ new Map();
|
|
2042
2890
|
MAX_BUFFER = 1e7;
|
|
2043
2891
|
}
|
|
@@ -2049,16 +2897,16 @@ init_database();
|
|
|
2049
2897
|
|
|
2050
2898
|
// src/lib/keychain.ts
|
|
2051
2899
|
import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod as chmod2 } from "fs/promises";
|
|
2052
|
-
import { existsSync as
|
|
2053
|
-
import
|
|
2054
|
-
import
|
|
2900
|
+
import { existsSync as existsSync4 } from "fs";
|
|
2901
|
+
import path4 from "path";
|
|
2902
|
+
import os4 from "os";
|
|
2055
2903
|
var SERVICE = "exe-mem";
|
|
2056
2904
|
var ACCOUNT = "master-key";
|
|
2057
2905
|
function getKeyDir() {
|
|
2058
|
-
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ??
|
|
2906
|
+
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path4.join(os4.homedir(), ".exe-os");
|
|
2059
2907
|
}
|
|
2060
2908
|
function getKeyPath() {
|
|
2061
|
-
return
|
|
2909
|
+
return path4.join(getKeyDir(), "master.key");
|
|
2062
2910
|
}
|
|
2063
2911
|
async function tryKeytar() {
|
|
2064
2912
|
try {
|
|
@@ -2079,9 +2927,9 @@ async function getMasterKey() {
|
|
|
2079
2927
|
}
|
|
2080
2928
|
}
|
|
2081
2929
|
const keyPath = getKeyPath();
|
|
2082
|
-
if (!
|
|
2930
|
+
if (!existsSync4(keyPath)) {
|
|
2083
2931
|
process.stderr.write(
|
|
2084
|
-
`[keychain] Key not found at ${keyPath} (HOME=${
|
|
2932
|
+
`[keychain] Key not found at ${keyPath} (HOME=${os4.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
|
|
2085
2933
|
`
|
|
2086
2934
|
);
|
|
2087
2935
|
return null;
|
|
@@ -2255,32 +3103,32 @@ function isMainModule(importMetaUrl) {
|
|
|
2255
3103
|
}
|
|
2256
3104
|
|
|
2257
3105
|
// src/bin/backfill-vectors.ts
|
|
2258
|
-
import { existsSync as
|
|
2259
|
-
import
|
|
3106
|
+
import { existsSync as existsSync9, unlinkSync as unlinkSync4 } from "fs";
|
|
3107
|
+
import path9 from "path";
|
|
2260
3108
|
|
|
2261
3109
|
// src/lib/worker-gate.ts
|
|
2262
3110
|
init_config();
|
|
2263
|
-
import { readdirSync as readdirSync2, writeFileSync as
|
|
2264
|
-
import
|
|
2265
|
-
var WORKER_PID_DIR =
|
|
3111
|
+
import { readdirSync as readdirSync2, writeFileSync as writeFileSync3, unlinkSync as unlinkSync3, mkdirSync as mkdirSync3, existsSync as existsSync8 } from "fs";
|
|
3112
|
+
import path8 from "path";
|
|
3113
|
+
var WORKER_PID_DIR = path8.join(EXE_AI_DIR, "worker-pids");
|
|
2266
3114
|
function registerWorkerPid(pid) {
|
|
2267
3115
|
try {
|
|
2268
|
-
|
|
2269
|
-
|
|
3116
|
+
mkdirSync3(WORKER_PID_DIR, { recursive: true });
|
|
3117
|
+
writeFileSync3(path8.join(WORKER_PID_DIR, `worker-${pid}.pid`), String(pid));
|
|
2270
3118
|
} catch {
|
|
2271
3119
|
}
|
|
2272
3120
|
}
|
|
2273
3121
|
function cleanupWorkerPid() {
|
|
2274
3122
|
try {
|
|
2275
|
-
unlinkSync3(
|
|
3123
|
+
unlinkSync3(path8.join(WORKER_PID_DIR, `worker-${process.pid}.pid`));
|
|
2276
3124
|
} catch {
|
|
2277
3125
|
}
|
|
2278
3126
|
}
|
|
2279
|
-
var BACKFILL_LOCK =
|
|
3127
|
+
var BACKFILL_LOCK = path8.join(WORKER_PID_DIR, "backfill.lock");
|
|
2280
3128
|
function tryAcquireBackfillLock() {
|
|
2281
3129
|
try {
|
|
2282
|
-
|
|
2283
|
-
if (
|
|
3130
|
+
mkdirSync3(WORKER_PID_DIR, { recursive: true });
|
|
3131
|
+
if (existsSync8(BACKFILL_LOCK)) {
|
|
2284
3132
|
try {
|
|
2285
3133
|
const pid = parseInt(
|
|
2286
3134
|
__require("fs").readFileSync(BACKFILL_LOCK, "utf8").trim(),
|
|
@@ -2296,7 +3144,7 @@ function tryAcquireBackfillLock() {
|
|
|
2296
3144
|
} catch {
|
|
2297
3145
|
}
|
|
2298
3146
|
}
|
|
2299
|
-
|
|
3147
|
+
writeFileSync3(BACKFILL_LOCK, String(process.pid));
|
|
2300
3148
|
return true;
|
|
2301
3149
|
} catch {
|
|
2302
3150
|
return true;
|
|
@@ -2311,7 +3159,7 @@ function releaseBackfillLock() {
|
|
|
2311
3159
|
|
|
2312
3160
|
// src/bin/backfill-vectors.ts
|
|
2313
3161
|
var BATCH_SIZE = 100;
|
|
2314
|
-
var BACKFILL_FLAG =
|
|
3162
|
+
var BACKFILL_FLAG = path9.join(EXE_AI_DIR, "session-cache", "needs-backfill");
|
|
2315
3163
|
async function backfillVectors() {
|
|
2316
3164
|
if (!tryAcquireBackfillLock()) {
|
|
2317
3165
|
process.stderr.write("[backfill] Another backfill is already running \u2014 exiting\n");
|
|
@@ -2389,7 +3237,7 @@ async function backfillVectors() {
|
|
|
2389
3237
|
return { processed: totalProcessed, failed: totalFailed, remaining: remainingCount };
|
|
2390
3238
|
}
|
|
2391
3239
|
function isBackfillNeeded() {
|
|
2392
|
-
return
|
|
3240
|
+
return existsSync9(BACKFILL_FLAG);
|
|
2393
3241
|
}
|
|
2394
3242
|
if (isMainModule(import.meta.url)) {
|
|
2395
3243
|
backfillVectors().then((result) => {
|