@askexenow/exe-os 0.9.16 → 0.9.18

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.
Files changed (97) hide show
  1. package/dist/bin/backfill-conversations.js +1242 -909
  2. package/dist/bin/backfill-responses.js +1245 -912
  3. package/dist/bin/backfill-vectors.js +1244 -906
  4. package/dist/bin/cleanup-stale-review-tasks.js +1870 -417
  5. package/dist/bin/cli.js +217 -107
  6. package/dist/bin/exe-agent-config.js +2 -2
  7. package/dist/bin/exe-agent.js +62 -0
  8. package/dist/bin/exe-assign.js +346 -10
  9. package/dist/bin/exe-boot.js +387 -32
  10. package/dist/bin/exe-call.js +72 -2
  11. package/dist/bin/exe-cloud.js +8 -0
  12. package/dist/bin/exe-dispatch.js +1821 -225
  13. package/dist/bin/exe-doctor.js +720 -52
  14. package/dist/bin/exe-export-behaviors.js +1429 -148
  15. package/dist/bin/exe-forget.js +1408 -34
  16. package/dist/bin/exe-gateway.js +1629 -1295
  17. package/dist/bin/exe-heartbeat.js +1899 -448
  18. package/dist/bin/exe-kill.js +1624 -346
  19. package/dist/bin/exe-launch-agent.js +726 -90
  20. package/dist/bin/exe-link.js +27 -8
  21. package/dist/bin/exe-new-employee.js +75 -9
  22. package/dist/bin/exe-pending-messages.js +2769 -1316
  23. package/dist/bin/exe-pending-notifications.js +2829 -1376
  24. package/dist/bin/exe-pending-reviews.js +2847 -1392
  25. package/dist/bin/exe-rename.js +89 -8
  26. package/dist/bin/exe-review.js +1494 -312
  27. package/dist/bin/exe-search.js +1608 -1300
  28. package/dist/bin/exe-session-cleanup.js +194 -91
  29. package/dist/bin/exe-settings.js +10 -2
  30. package/dist/bin/exe-start-codex.js +769 -120
  31. package/dist/bin/exe-start-opencode.js +763 -108
  32. package/dist/bin/exe-status.js +1887 -434
  33. package/dist/bin/exe-team.js +1782 -324
  34. package/dist/bin/git-sweep.js +408 -33
  35. package/dist/bin/graph-backfill.js +681 -27
  36. package/dist/bin/graph-export.js +1419 -141
  37. package/dist/bin/install.js +4 -8
  38. package/dist/bin/intercom-check.js +8641 -0
  39. package/dist/bin/scan-tasks.js +553 -38
  40. package/dist/bin/setup.js +82 -10
  41. package/dist/bin/shard-migrate.js +682 -28
  42. package/dist/gateway/index.js +1629 -1295
  43. package/dist/hooks/bug-report-worker.js +1136 -183
  44. package/dist/hooks/codex-stop-task-finalizer.js +1060 -107
  45. package/dist/hooks/commit-complete.js +408 -33
  46. package/dist/hooks/error-recall.js +1608 -1300
  47. package/dist/hooks/ingest-worker.js +250 -7966
  48. package/dist/hooks/ingest.js +707 -119
  49. package/dist/hooks/instructions-loaded.js +383 -20
  50. package/dist/hooks/notification.js +383 -20
  51. package/dist/hooks/post-compact.js +384 -21
  52. package/dist/hooks/{response-ingest-worker.js → post-tool-combined.js} +4157 -1716
  53. package/dist/hooks/pre-compact.js +486 -45
  54. package/dist/hooks/pre-tool-use.js +385 -22
  55. package/dist/hooks/prompt-submit.js +291 -96
  56. package/dist/hooks/session-end.js +490 -48
  57. package/dist/hooks/session-start.js +236 -22
  58. package/dist/hooks/stop.js +192 -50
  59. package/dist/hooks/subagent-stop.js +95 -18
  60. package/dist/hooks/summary-worker.js +205 -361
  61. package/dist/index.js +221 -105
  62. package/dist/lib/agent-config.js +2 -2
  63. package/dist/lib/cloud-sync.js +27 -8
  64. package/dist/lib/consolidation.js +437 -41
  65. package/dist/lib/database.js +20 -8
  66. package/dist/lib/db-daemon-client.js +2 -2
  67. package/dist/lib/db.js +20 -8
  68. package/dist/lib/device-registry.js +27 -8
  69. package/dist/lib/employee-templates.js +62 -0
  70. package/dist/lib/employees.js +2 -2
  71. package/dist/lib/exe-daemon.js +6703 -6259
  72. package/dist/lib/hybrid-search.js +1608 -1300
  73. package/dist/lib/identity.js +1 -1
  74. package/dist/lib/messaging.js +11 -3
  75. package/dist/lib/reminders.js +1 -1
  76. package/dist/lib/schedules.js +718 -26
  77. package/dist/lib/session-registry.js +11 -0
  78. package/dist/lib/skill-learning.js +639 -9
  79. package/dist/lib/store.js +676 -27
  80. package/dist/lib/tasks.js +665 -27
  81. package/dist/lib/tmux-routing.js +732 -94
  82. package/dist/lib/token-spend.js +1 -1
  83. package/dist/mcp/server.js +856 -383
  84. package/dist/mcp/tools/complete-reminder.js +1 -1
  85. package/dist/mcp/tools/create-reminder.js +1 -1
  86. package/dist/mcp/tools/create-task.js +616 -46
  87. package/dist/mcp/tools/deactivate-behavior.js +9 -1
  88. package/dist/mcp/tools/list-reminders.js +1 -1
  89. package/dist/mcp/tools/list-tasks.js +10 -2
  90. package/dist/mcp/tools/send-message.js +11 -3
  91. package/dist/mcp/tools/update-task.js +677 -39
  92. package/dist/runtime/index.js +425 -37
  93. package/dist/tui/App.js +365 -34
  94. package/package.json +5 -2
  95. package/src/commands/exe/intercom.md +6 -17
  96. package/dist/bin/wiki-sync.js +0 -2991
  97. package/dist/hooks/prompt-ingest-worker.js +0 -3979
@@ -73,9 +73,9 @@ var MAX_RETRIES, BASE_DELAY_MS, MAX_JITTER_MS;
73
73
  var init_db_retry = __esm({
74
74
  "src/lib/db-retry.ts"() {
75
75
  "use strict";
76
- MAX_RETRIES = 3;
77
- BASE_DELAY_MS = 200;
78
- MAX_JITTER_MS = 300;
76
+ MAX_RETRIES = 5;
77
+ BASE_DELAY_MS = 250;
78
+ MAX_JITTER_MS = 400;
79
79
  }
80
80
  });
81
81
 
@@ -979,456 +979,1162 @@ var init_database_adapter = __esm({
979
979
  }
980
980
  });
981
981
 
982
- // src/lib/database.ts
983
- import { createClient } from "@libsql/client";
984
- async function initDatabase(config) {
985
- if (_walCheckpointTimer) {
986
- clearInterval(_walCheckpointTimer);
987
- _walCheckpointTimer = null;
982
+ // src/lib/daemon-auth.ts
983
+ import crypto from "crypto";
984
+ import path4 from "path";
985
+ import { existsSync as existsSync4, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
986
+ function normalizeToken(token) {
987
+ if (!token) return null;
988
+ const trimmed = token.trim();
989
+ return trimmed.length > 0 ? trimmed : null;
990
+ }
991
+ function readDaemonToken() {
992
+ try {
993
+ if (!existsSync4(DAEMON_TOKEN_PATH)) return null;
994
+ return normalizeToken(readFileSync3(DAEMON_TOKEN_PATH, "utf8"));
995
+ } catch {
996
+ return null;
988
997
  }
989
- if (_daemonClient) {
990
- _daemonClient.close();
991
- _daemonClient = null;
998
+ }
999
+ function ensureDaemonToken(seed) {
1000
+ const existing = readDaemonToken();
1001
+ if (existing) return existing;
1002
+ const token = normalizeToken(seed) ?? crypto.randomBytes(32).toString("hex");
1003
+ ensurePrivateDirSync(EXE_AI_DIR);
1004
+ writeFileSync2(DAEMON_TOKEN_PATH, `${token}
1005
+ `, "utf8");
1006
+ enforcePrivateFileSync(DAEMON_TOKEN_PATH);
1007
+ return token;
1008
+ }
1009
+ var DAEMON_TOKEN_PATH;
1010
+ var init_daemon_auth = __esm({
1011
+ "src/lib/daemon-auth.ts"() {
1012
+ "use strict";
1013
+ init_config();
1014
+ init_secure_files();
1015
+ DAEMON_TOKEN_PATH = path4.join(EXE_AI_DIR, "exed.token");
992
1016
  }
993
- if (_adapterClient && _adapterClient !== _resilientClient) {
994
- _adapterClient.close();
1017
+ });
1018
+
1019
+ // src/lib/exe-daemon-client.ts
1020
+ import net from "net";
1021
+ import os4 from "os";
1022
+ import { spawn } from "child_process";
1023
+ import { randomUUID } from "crypto";
1024
+ import { existsSync as existsSync5, unlinkSync as unlinkSync2, readFileSync as readFileSync4, openSync, closeSync, statSync } from "fs";
1025
+ import path5 from "path";
1026
+ import { fileURLToPath } from "url";
1027
+ function handleData(chunk) {
1028
+ _buffer += chunk.toString();
1029
+ if (_buffer.length > MAX_BUFFER) {
1030
+ _buffer = "";
1031
+ return;
995
1032
  }
996
- _adapterClient = null;
997
- if (_client) {
998
- _client.close();
999
- _client = null;
1000
- _resilientClient = null;
1033
+ let newlineIdx;
1034
+ while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
1035
+ const line = _buffer.slice(0, newlineIdx).trim();
1036
+ _buffer = _buffer.slice(newlineIdx + 1);
1037
+ if (!line) continue;
1038
+ try {
1039
+ const response = JSON.parse(line);
1040
+ const id = response.id;
1041
+ if (!id) continue;
1042
+ const entry = _pending.get(id);
1043
+ if (entry) {
1044
+ clearTimeout(entry.timer);
1045
+ _pending.delete(id);
1046
+ entry.resolve(response);
1047
+ }
1048
+ } catch {
1049
+ }
1001
1050
  }
1002
- const opts = {
1003
- url: `file:${config.dbPath}`
1004
- };
1005
- if (config.encryptionKey) {
1006
- opts.encryptionKey = config.encryptionKey;
1051
+ }
1052
+ function cleanupStaleFiles() {
1053
+ if (existsSync5(PID_PATH)) {
1054
+ try {
1055
+ const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
1056
+ if (pid > 0) {
1057
+ try {
1058
+ process.kill(pid, 0);
1059
+ return;
1060
+ } catch {
1061
+ }
1062
+ }
1063
+ } catch {
1064
+ }
1065
+ try {
1066
+ unlinkSync2(PID_PATH);
1067
+ } catch {
1068
+ }
1069
+ try {
1070
+ unlinkSync2(SOCKET_PATH);
1071
+ } catch {
1072
+ }
1007
1073
  }
1008
- _client = createClient(opts);
1009
- _resilientClient = wrapWithRetry(_client);
1010
- _adapterClient = _resilientClient;
1011
- _client.execute("PRAGMA busy_timeout = 30000").catch(() => {
1012
- });
1013
- _client.execute("PRAGMA journal_mode = WAL").catch(() => {
1014
- });
1015
- if (_walCheckpointTimer) clearInterval(_walCheckpointTimer);
1016
- _walCheckpointTimer = setInterval(() => {
1017
- _client?.execute("PRAGMA wal_checkpoint(PASSIVE)").catch(() => {
1018
- });
1019
- }, 3e4);
1020
- _walCheckpointTimer.unref();
1021
- if (process.env.DATABASE_URL) {
1022
- _adapterClient = await createPrismaDbAdapter(_resilientClient);
1074
+ }
1075
+ function findPackageRoot() {
1076
+ let dir = path5.dirname(fileURLToPath(import.meta.url));
1077
+ const { root } = path5.parse(dir);
1078
+ while (dir !== root) {
1079
+ if (existsSync5(path5.join(dir, "package.json"))) return dir;
1080
+ dir = path5.dirname(dir);
1023
1081
  }
1082
+ return null;
1024
1083
  }
1025
- function getClient() {
1026
- if (!_adapterClient) {
1027
- throw new Error("Database client not initialized. Call initDatabase() first.");
1084
+ function getAvailableMemoryGB() {
1085
+ if (process.platform === "darwin") {
1086
+ try {
1087
+ const { execSync: execSync4 } = __require("child_process");
1088
+ const vmstat = execSync4("vm_stat", { encoding: "utf8" });
1089
+ const pageSize = 16384;
1090
+ const pageSizeMatch = vmstat.match(/page size of (\d+) bytes/);
1091
+ const actualPageSize = pageSizeMatch ? parseInt(pageSizeMatch[1], 10) : pageSize;
1092
+ const free = vmstat.match(/Pages free:\s+(\d+)/);
1093
+ const inactive = vmstat.match(/Pages inactive:\s+(\d+)/);
1094
+ const speculative = vmstat.match(/Pages speculative:\s+(\d+)/);
1095
+ const freePages = free ? parseInt(free[1], 10) : 0;
1096
+ const inactivePages = inactive ? parseInt(inactive[1], 10) : 0;
1097
+ const speculativePages = speculative ? parseInt(speculative[1], 10) : 0;
1098
+ return (freePages + inactivePages + speculativePages) * actualPageSize / (1024 * 1024 * 1024);
1099
+ } catch {
1100
+ return os4.freemem() / (1024 * 1024 * 1024);
1101
+ }
1028
1102
  }
1029
- if (process.env.DATABASE_URL) {
1030
- return _adapterClient;
1103
+ return os4.freemem() / (1024 * 1024 * 1024);
1104
+ }
1105
+ function spawnDaemon() {
1106
+ const freeGB = getAvailableMemoryGB();
1107
+ const totalGB = os4.totalmem() / (1024 * 1024 * 1024);
1108
+ if (totalGB <= 8) {
1109
+ process.stderr.write(
1110
+ `[exed-client] SKIP: ${totalGB.toFixed(0)}GB system \u2014 embedding daemon disabled. Using keyword search only. Minimum 16GB recommended for vector search.
1111
+ `
1112
+ );
1113
+ return;
1031
1114
  }
1032
- if (process.env.EXE_IS_DAEMON === "1") {
1033
- return _resilientClient;
1115
+ if (totalGB <= 16 && freeGB < 2) {
1116
+ process.stderr.write(
1117
+ `[exed-client] SKIP: low memory (${freeGB.toFixed(1)}GB available / ${totalGB.toFixed(0)}GB total). Embedding daemon not started \u2014 using keyword search only.
1118
+ `
1119
+ );
1120
+ return;
1034
1121
  }
1035
- if (_daemonClient && _daemonClient._isDaemonActive()) {
1036
- return _daemonClient;
1122
+ const pkgRoot = findPackageRoot();
1123
+ if (!pkgRoot) {
1124
+ process.stderr.write("[exed-client] WARN: cannot find package root\n");
1125
+ return;
1126
+ }
1127
+ const daemonPath = path5.join(pkgRoot, "dist", "lib", "exe-daemon.js");
1128
+ if (!existsSync5(daemonPath)) {
1129
+ process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
1130
+ `);
1131
+ return;
1132
+ }
1133
+ const resolvedPath = daemonPath;
1134
+ const daemonToken = ensureDaemonToken(process.env[DAEMON_TOKEN_ENV] ?? null);
1135
+ process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
1136
+ `);
1137
+ const logPath = path5.join(path5.dirname(SOCKET_PATH), "exed.log");
1138
+ let stderrFd = "ignore";
1139
+ try {
1140
+ stderrFd = openSync(logPath, "a");
1141
+ } catch {
1142
+ }
1143
+ const heapCapMB = totalGB <= 8 ? 256 : 512;
1144
+ const nodeArgs = [`--max-old-space-size=${heapCapMB}`, resolvedPath];
1145
+ const child = spawn(process.execPath, nodeArgs, {
1146
+ detached: true,
1147
+ stdio: ["ignore", "ignore", stderrFd],
1148
+ env: {
1149
+ ...process.env,
1150
+ TMUX: void 0,
1151
+ // Daemon is global — must not inherit session scope
1152
+ TMUX_PANE: void 0,
1153
+ // Prevents resolveExeSession() from scoping to one session
1154
+ EXE_DAEMON_SOCK: SOCKET_PATH,
1155
+ EXE_DAEMON_PID: PID_PATH,
1156
+ [DAEMON_TOKEN_ENV]: daemonToken
1157
+ }
1158
+ });
1159
+ child.unref();
1160
+ if (typeof stderrFd === "number") {
1161
+ try {
1162
+ closeSync(stderrFd);
1163
+ } catch {
1164
+ }
1037
1165
  }
1038
- return _resilientClient;
1039
1166
  }
1040
- function getRawClient() {
1041
- if (!_client) {
1042
- throw new Error("Database client not initialized. Call initDatabase() first.");
1167
+ function acquireSpawnLock() {
1168
+ try {
1169
+ const fd = openSync(SPAWN_LOCK_PATH, "wx");
1170
+ closeSync(fd);
1171
+ return true;
1172
+ } catch {
1173
+ try {
1174
+ const stat = statSync(SPAWN_LOCK_PATH);
1175
+ if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
1176
+ try {
1177
+ unlinkSync2(SPAWN_LOCK_PATH);
1178
+ } catch {
1179
+ }
1180
+ try {
1181
+ const fd = openSync(SPAWN_LOCK_PATH, "wx");
1182
+ closeSync(fd);
1183
+ return true;
1184
+ } catch {
1185
+ }
1186
+ }
1187
+ } catch {
1188
+ }
1189
+ return false;
1043
1190
  }
1044
- return _client;
1045
1191
  }
1046
- async function ensureSchema() {
1047
- const client = getRawClient();
1048
- await client.execute("PRAGMA journal_mode = WAL");
1049
- await client.execute("PRAGMA busy_timeout = 30000");
1050
- await client.execute("PRAGMA wal_autocheckpoint = 1000");
1192
+ function releaseSpawnLock() {
1051
1193
  try {
1052
- await client.execute("PRAGMA libsql_vector_search_ef = 128");
1194
+ unlinkSync2(SPAWN_LOCK_PATH);
1053
1195
  } catch {
1054
1196
  }
1055
- await client.executeMultiple(`
1056
- CREATE TABLE IF NOT EXISTS memories (
1057
- id TEXT PRIMARY KEY,
1058
- agent_id TEXT NOT NULL,
1059
- agent_role TEXT NOT NULL,
1060
- session_id TEXT NOT NULL,
1061
- timestamp TEXT NOT NULL,
1062
- tool_name TEXT NOT NULL,
1063
- project_name TEXT NOT NULL,
1064
- has_error INTEGER NOT NULL DEFAULT 0,
1065
- raw_text TEXT NOT NULL,
1066
- vector F32_BLOB(1024),
1067
- version INTEGER NOT NULL DEFAULT 0
1068
- );
1069
-
1070
- CREATE INDEX IF NOT EXISTS idx_memories_agent
1071
- ON memories(agent_id);
1072
-
1073
- CREATE INDEX IF NOT EXISTS idx_memories_timestamp
1074
- ON memories(timestamp);
1075
-
1076
- CREATE INDEX IF NOT EXISTS idx_memories_session
1077
- ON memories(session_id);
1078
-
1079
- CREATE INDEX IF NOT EXISTS idx_memories_project
1080
- ON memories(project_name);
1081
-
1082
- CREATE INDEX IF NOT EXISTS idx_memories_tool
1083
- ON memories(tool_name);
1084
-
1085
- CREATE INDEX IF NOT EXISTS idx_memories_version
1086
- ON memories(version);
1087
-
1088
- CREATE INDEX IF NOT EXISTS idx_memories_agent_project
1089
- ON memories(agent_id, project_name);
1090
- `);
1091
- await client.executeMultiple(`
1092
- CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
1093
- raw_text,
1094
- content='memories',
1095
- content_rowid='rowid'
1096
- );
1097
-
1098
- CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
1099
- INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1100
- END;
1101
-
1102
- CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
1103
- INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1104
- END;
1105
-
1106
- CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
1107
- INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1108
- INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1109
- END;
1110
- `);
1111
- await client.executeMultiple(`
1112
- CREATE TABLE IF NOT EXISTS sync_meta (
1113
- key TEXT PRIMARY KEY,
1114
- value TEXT NOT NULL
1115
- );
1116
- `);
1117
- await client.executeMultiple(`
1118
- CREATE TABLE IF NOT EXISTS tasks (
1119
- id TEXT PRIMARY KEY,
1120
- title TEXT NOT NULL,
1121
- assigned_to TEXT NOT NULL,
1122
- assigned_by TEXT NOT NULL,
1123
- project_name TEXT NOT NULL,
1124
- priority TEXT NOT NULL DEFAULT 'p1',
1125
- status TEXT NOT NULL DEFAULT 'open',
1126
- task_file TEXT,
1127
- created_at TEXT NOT NULL,
1128
- updated_at TEXT NOT NULL
1129
- );
1130
-
1131
- CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
1132
- ON tasks(assigned_to, status);
1133
- `);
1134
- await client.executeMultiple(`
1135
- CREATE TABLE IF NOT EXISTS behaviors (
1136
- id TEXT PRIMARY KEY,
1137
- agent_id TEXT NOT NULL,
1138
- project_name TEXT,
1139
- domain TEXT,
1140
- content TEXT NOT NULL,
1141
- active INTEGER NOT NULL DEFAULT 1,
1142
- created_at TEXT NOT NULL,
1143
- updated_at TEXT NOT NULL
1144
- );
1145
-
1146
- CREATE INDEX IF NOT EXISTS idx_behaviors_agent
1147
- ON behaviors(agent_id, active);
1148
- `);
1149
- try {
1150
- const coordinatorName = getCoordinatorName();
1151
- const existing = await client.execute({
1152
- sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = ?",
1153
- args: [coordinatorName]
1197
+ }
1198
+ function connectToSocket() {
1199
+ return new Promise((resolve) => {
1200
+ if (_socket && _connected) {
1201
+ resolve(true);
1202
+ return;
1203
+ }
1204
+ const socket = net.createConnection({ path: SOCKET_PATH });
1205
+ const connectTimeout = setTimeout(() => {
1206
+ socket.destroy();
1207
+ resolve(false);
1208
+ }, 2e3);
1209
+ socket.on("connect", () => {
1210
+ clearTimeout(connectTimeout);
1211
+ _socket = socket;
1212
+ _connected = true;
1213
+ _buffer = "";
1214
+ socket.on("data", handleData);
1215
+ socket.on("close", () => {
1216
+ _connected = false;
1217
+ _socket = null;
1218
+ for (const [id, entry] of _pending) {
1219
+ clearTimeout(entry.timer);
1220
+ _pending.delete(id);
1221
+ entry.resolve({ error: "Connection closed" });
1222
+ }
1223
+ });
1224
+ socket.on("error", () => {
1225
+ _connected = false;
1226
+ _socket = null;
1227
+ });
1228
+ resolve(true);
1154
1229
  });
1155
- if (Number(existing.rows[0]?.cnt) === 0) {
1156
- const seededAt = "2026-03-25T00:00:00Z";
1157
- for (const [domain, content] of [
1158
- ["workflow", `Don't ask "keep going?" \u2014 just keep executing phases/plans autonomously`],
1159
- ["tool-use", "Always use create_task MCP tool, never write .md files directly for task creation"],
1160
- ["workflow", "Auto-start reviewing when idle and reviews are pending \u2014 never ask founder for permission"]
1161
- ]) {
1162
- await client.execute({
1163
- sql: `INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
1164
- VALUES (hex(randomblob(16)), ?, NULL, ?, ?, 1, ?, ?)`,
1165
- args: [coordinatorName, domain, content, seededAt, seededAt]
1166
- });
1167
- }
1230
+ socket.on("error", () => {
1231
+ clearTimeout(connectTimeout);
1232
+ resolve(false);
1233
+ });
1234
+ });
1235
+ }
1236
+ async function connectEmbedDaemon() {
1237
+ if (_socket && _connected) return true;
1238
+ if (await connectToSocket()) return true;
1239
+ if (acquireSpawnLock()) {
1240
+ try {
1241
+ cleanupStaleFiles();
1242
+ spawnDaemon();
1243
+ } finally {
1244
+ releaseSpawnLock();
1168
1245
  }
1169
- } catch {
1170
1246
  }
1171
- try {
1172
- await client.execute({
1173
- sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
1174
- args: []
1175
- });
1176
- } catch {
1247
+ const start = Date.now();
1248
+ let delay2 = 100;
1249
+ while (Date.now() - start < CONNECT_TIMEOUT_MS) {
1250
+ await new Promise((r) => setTimeout(r, delay2));
1251
+ if (await connectToSocket()) return true;
1252
+ delay2 = Math.min(delay2 * 2, 3e3);
1177
1253
  }
1178
- try {
1179
- await client.execute({
1180
- sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
1181
- args: []
1182
- });
1183
- } catch {
1254
+ return false;
1255
+ }
1256
+ function sendRequest(texts, priority) {
1257
+ return sendDaemonRequest({ texts, priority });
1258
+ }
1259
+ function sendDaemonRequest(payload, timeoutMs = REQUEST_TIMEOUT_MS) {
1260
+ return new Promise((resolve) => {
1261
+ if (!_socket || !_connected) {
1262
+ resolve({ error: "Not connected" });
1263
+ return;
1264
+ }
1265
+ const id = randomUUID();
1266
+ const token = process.env[DAEMON_TOKEN_ENV] ?? readDaemonToken();
1267
+ const timer = setTimeout(() => {
1268
+ _pending.delete(id);
1269
+ resolve({ error: "Request timeout" });
1270
+ }, timeoutMs);
1271
+ _pending.set(id, { resolve, timer });
1272
+ try {
1273
+ _socket.write(JSON.stringify({ id, token, ...payload }) + "\n");
1274
+ } catch {
1275
+ clearTimeout(timer);
1276
+ _pending.delete(id);
1277
+ resolve({ error: "Write failed" });
1278
+ }
1279
+ });
1280
+ }
1281
+ async function pingDaemon() {
1282
+ if (!_socket || !_connected) return null;
1283
+ const response = await sendDaemonRequest({ type: "health" }, 5e3);
1284
+ if (response.health) {
1285
+ return response.health;
1184
1286
  }
1185
- try {
1186
- await client.execute({
1187
- sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
1188
- args: []
1189
- });
1190
- } catch {
1287
+ return null;
1288
+ }
1289
+ function killAndRespawnDaemon() {
1290
+ if (!acquireSpawnLock()) {
1291
+ process.stderr.write("[exed-client] Another process is already restarting daemon \u2014 skipping\n");
1292
+ if (_socket) {
1293
+ _socket.destroy();
1294
+ _socket = null;
1295
+ }
1296
+ _connected = false;
1297
+ _buffer = "";
1298
+ return;
1191
1299
  }
1192
1300
  try {
1193
- await client.execute({
1194
- sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
1195
- ON tasks(parent_task_id)
1196
- WHERE parent_task_id IS NOT NULL`,
1197
- args: []
1198
- });
1199
- } catch {
1301
+ process.stderr.write("[exed-client] Killing daemon for restart...\n");
1302
+ if (existsSync5(PID_PATH)) {
1303
+ try {
1304
+ const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
1305
+ if (pid > 0) {
1306
+ try {
1307
+ process.kill(pid, "SIGKILL");
1308
+ } catch {
1309
+ }
1310
+ }
1311
+ } catch {
1312
+ }
1313
+ }
1314
+ if (_socket) {
1315
+ _socket.destroy();
1316
+ _socket = null;
1317
+ }
1318
+ _connected = false;
1319
+ _buffer = "";
1320
+ try {
1321
+ unlinkSync2(PID_PATH);
1322
+ } catch {
1323
+ }
1324
+ try {
1325
+ unlinkSync2(SOCKET_PATH);
1326
+ } catch {
1327
+ }
1328
+ spawnDaemon();
1329
+ } finally {
1330
+ releaseSpawnLock();
1200
1331
  }
1332
+ }
1333
+ function isDaemonTooYoung() {
1201
1334
  try {
1202
- await client.execute({
1203
- sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
1204
- args: []
1205
- });
1335
+ const stat = statSync(PID_PATH);
1336
+ return Date.now() - stat.mtimeMs < MIN_DAEMON_AGE_MS;
1206
1337
  } catch {
1338
+ return false;
1207
1339
  }
1208
- try {
1209
- await client.execute({
1210
- sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
1211
- args: []
1212
- });
1213
- } catch {
1340
+ }
1341
+ async function retryThenRestart(doRequest, label) {
1342
+ const result = await doRequest();
1343
+ if (!result.error) {
1344
+ _consecutiveFailures = 0;
1345
+ return result;
1214
1346
  }
1215
- try {
1216
- await client.execute({
1217
- sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
1218
- args: []
1219
- });
1220
- } catch {
1347
+ _consecutiveFailures++;
1348
+ for (let i = 0; i < MAX_RETRIES_BEFORE_RESTART; i++) {
1349
+ const delayMs = RETRY_DELAYS_MS[i] ?? 5e3;
1350
+ process.stderr.write(`[exed-client] ${label} failed (${result.error}), retry ${i + 1}/${MAX_RETRIES_BEFORE_RESTART} in ${delayMs}ms
1351
+ `);
1352
+ await new Promise((r) => setTimeout(r, delayMs));
1353
+ if (!_connected) {
1354
+ if (!await connectToSocket()) continue;
1355
+ }
1356
+ const retry = await doRequest();
1357
+ if (!retry.error) {
1358
+ _consecutiveFailures = 0;
1359
+ return retry;
1360
+ }
1361
+ _consecutiveFailures++;
1221
1362
  }
1222
- try {
1223
- await client.execute({
1224
- sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
1225
- args: []
1226
- });
1227
- } catch {
1363
+ if (isDaemonTooYoung()) {
1364
+ process.stderr.write(`[exed-client] ${label}: daemon too young (< ${MIN_DAEMON_AGE_MS / 1e3}s) \u2014 skipping restart
1365
+ `);
1366
+ return { error: result.error };
1228
1367
  }
1229
- try {
1230
- await client.execute({
1231
- sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
1232
- args: []
1233
- });
1234
- } catch {
1368
+ process.stderr.write(`[exed-client] ${label}: ${_consecutiveFailures} consecutive failures \u2014 restarting daemon
1369
+ `);
1370
+ killAndRespawnDaemon();
1371
+ const start = Date.now();
1372
+ let delay2 = 200;
1373
+ while (Date.now() - start < CONNECT_TIMEOUT_MS) {
1374
+ await new Promise((r) => setTimeout(r, delay2));
1375
+ if (await connectToSocket()) break;
1376
+ delay2 = Math.min(delay2 * 2, 3e3);
1235
1377
  }
1236
- try {
1237
- await client.execute({
1238
- sql: `ALTER TABLE tasks ADD COLUMN checkpoint TEXT`,
1239
- args: []
1240
- });
1241
- } catch {
1378
+ if (!_connected) return { error: "Daemon restart failed" };
1379
+ const final = await doRequest();
1380
+ if (!final.error) _consecutiveFailures = 0;
1381
+ return final;
1382
+ }
1383
+ async function embedViaClient(text, priority = "high") {
1384
+ if (!_connected && !await connectEmbedDaemon()) return null;
1385
+ _requestCount++;
1386
+ if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
1387
+ const health = await pingDaemon();
1388
+ if (!health && !isDaemonTooYoung()) {
1389
+ process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
1390
+ `);
1391
+ killAndRespawnDaemon();
1392
+ const start = Date.now();
1393
+ let d = 200;
1394
+ while (Date.now() - start < CONNECT_TIMEOUT_MS) {
1395
+ await new Promise((r) => setTimeout(r, d));
1396
+ if (await connectToSocket()) break;
1397
+ d = Math.min(d * 2, 3e3);
1398
+ }
1399
+ if (!_connected) return null;
1400
+ }
1242
1401
  }
1243
- try {
1244
- await client.execute({
1245
- sql: `ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER NOT NULL DEFAULT 0`,
1246
- args: []
1247
- });
1248
- } catch {
1402
+ const result = await retryThenRestart(
1403
+ () => sendRequest([text], priority),
1404
+ "Embed"
1405
+ );
1406
+ return !result.error && result.vectors?.[0] ? result.vectors[0] : null;
1407
+ }
1408
+ function disconnectClient() {
1409
+ if (_socket) {
1410
+ _socket.destroy();
1411
+ _socket = null;
1249
1412
  }
1250
- try {
1251
- await client.execute({
1252
- sql: `ALTER TABLE tasks ADD COLUMN complexity TEXT NOT NULL DEFAULT 'standard'`,
1253
- args: []
1254
- });
1255
- } catch {
1413
+ _connected = false;
1414
+ _buffer = "";
1415
+ for (const [id, entry] of _pending) {
1416
+ clearTimeout(entry.timer);
1417
+ _pending.delete(id);
1418
+ entry.resolve({ error: "Client disconnected" });
1256
1419
  }
1257
- try {
1258
- await client.execute({
1259
- sql: `ALTER TABLE tasks ADD COLUMN session_scope TEXT`,
1260
- args: []
1261
- });
1262
- } catch {
1420
+ }
1421
+ function isClientConnected() {
1422
+ return _connected;
1423
+ }
1424
+ 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;
1425
+ var init_exe_daemon_client = __esm({
1426
+ "src/lib/exe-daemon-client.ts"() {
1427
+ "use strict";
1428
+ init_config();
1429
+ init_daemon_auth();
1430
+ SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path5.join(EXE_AI_DIR, "exed.sock");
1431
+ PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path5.join(EXE_AI_DIR, "exed.pid");
1432
+ SPAWN_LOCK_PATH = path5.join(EXE_AI_DIR, "exed-spawn.lock");
1433
+ SPAWN_LOCK_STALE_MS = 3e4;
1434
+ CONNECT_TIMEOUT_MS = 15e3;
1435
+ REQUEST_TIMEOUT_MS = 3e4;
1436
+ DAEMON_TOKEN_ENV = "EXE_DAEMON_TOKEN";
1437
+ _socket = null;
1438
+ _connected = false;
1439
+ _buffer = "";
1440
+ _requestCount = 0;
1441
+ _consecutiveFailures = 0;
1442
+ HEALTH_CHECK_INTERVAL = 100;
1443
+ MAX_RETRIES_BEFORE_RESTART = 3;
1444
+ RETRY_DELAYS_MS = [1e3, 3e3, 5e3];
1445
+ MIN_DAEMON_AGE_MS = 3e4;
1446
+ _pending = /* @__PURE__ */ new Map();
1447
+ MAX_BUFFER = 1e7;
1263
1448
  }
1264
- try {
1265
- await client.execute({
1266
- sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
1267
- args: []
1449
+ });
1450
+
1451
+ // src/lib/daemon-protocol.ts
1452
+ function serializeValue(v) {
1453
+ if (v === null || v === void 0) return null;
1454
+ if (typeof v === "bigint") return Number(v);
1455
+ if (typeof v === "boolean") return v ? 1 : 0;
1456
+ if (v instanceof Uint8Array) {
1457
+ return { __blob: Buffer.from(v).toString("base64") };
1458
+ }
1459
+ if (ArrayBuffer.isView(v)) {
1460
+ return { __blob: Buffer.from(v.buffer, v.byteOffset, v.byteLength).toString("base64") };
1461
+ }
1462
+ if (v instanceof ArrayBuffer) {
1463
+ return { __blob: Buffer.from(v).toString("base64") };
1464
+ }
1465
+ if (typeof v === "string" || typeof v === "number") return v;
1466
+ return String(v);
1467
+ }
1468
+ function deserializeValue(v) {
1469
+ if (v === null) return null;
1470
+ if (typeof v === "object" && v !== null && "__blob" in v) {
1471
+ const buf = Buffer.from(v.__blob, "base64");
1472
+ return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
1473
+ }
1474
+ return v;
1475
+ }
1476
+ function deserializeResultSet(srs) {
1477
+ const rows = srs.rows.map((obj) => {
1478
+ const values = srs.columns.map(
1479
+ (col) => deserializeValue(obj[col] ?? null)
1480
+ );
1481
+ const row = values;
1482
+ for (let i = 0; i < srs.columns.length; i++) {
1483
+ const col = srs.columns[i];
1484
+ if (col !== void 0) {
1485
+ row[col] = values[i] ?? null;
1486
+ }
1487
+ }
1488
+ Object.defineProperty(row, "length", {
1489
+ value: values.length,
1490
+ enumerable: false
1268
1491
  });
1269
- } catch {
1492
+ return row;
1493
+ });
1494
+ return {
1495
+ columns: srs.columns,
1496
+ columnTypes: srs.columnTypes ?? [],
1497
+ rows,
1498
+ rowsAffected: srs.rowsAffected,
1499
+ lastInsertRowid: srs.lastInsertRowid != null ? BigInt(srs.lastInsertRowid) : void 0,
1500
+ toJSON: () => ({
1501
+ columns: srs.columns,
1502
+ columnTypes: srs.columnTypes ?? [],
1503
+ rows: srs.rows,
1504
+ rowsAffected: srs.rowsAffected,
1505
+ lastInsertRowid: srs.lastInsertRowid
1506
+ })
1507
+ };
1508
+ }
1509
+ var init_daemon_protocol = __esm({
1510
+ "src/lib/daemon-protocol.ts"() {
1511
+ "use strict";
1270
1512
  }
1271
- try {
1272
- await client.execute({
1273
- sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
1274
- args: []
1275
- });
1276
- } catch {
1513
+ });
1514
+
1515
+ // src/lib/db-daemon-client.ts
1516
+ var db_daemon_client_exports = {};
1517
+ __export(db_daemon_client_exports, {
1518
+ createDaemonDbClient: () => createDaemonDbClient,
1519
+ initDaemonDbClient: () => initDaemonDbClient
1520
+ });
1521
+ function normalizeStatement2(stmt) {
1522
+ if (typeof stmt === "string") {
1523
+ return { sql: stmt, args: [] };
1277
1524
  }
1278
- try {
1279
- await client.execute({
1280
- sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
1281
- args: []
1282
- });
1283
- } catch {
1525
+ const sql = stmt.sql;
1526
+ let args = [];
1527
+ if (Array.isArray(stmt.args)) {
1528
+ args = stmt.args.map((v) => serializeValue(v));
1529
+ } else if (stmt.args && typeof stmt.args === "object") {
1530
+ const named = {};
1531
+ for (const [key, val] of Object.entries(stmt.args)) {
1532
+ named[key] = serializeValue(val);
1533
+ }
1534
+ return { sql, args: named };
1284
1535
  }
1285
- try {
1286
- await client.execute({
1287
- sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
1288
- args: []
1289
- });
1290
- } catch {
1536
+ return { sql, args };
1537
+ }
1538
+ function createDaemonDbClient(fallbackClient) {
1539
+ let _useDaemon = false;
1540
+ const client = {
1541
+ async execute(stmt) {
1542
+ if (!_useDaemon || !isClientConnected()) {
1543
+ return fallbackClient.execute(stmt);
1544
+ }
1545
+ const { sql, args } = normalizeStatement2(stmt);
1546
+ const response = await sendDaemonRequest({
1547
+ type: "db-execute",
1548
+ sql,
1549
+ args
1550
+ });
1551
+ if (response.error) {
1552
+ const errMsg = String(response.error);
1553
+ if (errMsg === "Not connected" || errMsg === "Request timeout" || errMsg === "Write failed" || errMsg === "DB not initialized") {
1554
+ process.stderr.write(`[db-daemon] Transport error (${errMsg}), falling back to direct
1555
+ `);
1556
+ return fallbackClient.execute(stmt);
1557
+ }
1558
+ throw new Error(errMsg);
1559
+ }
1560
+ if (response.db) {
1561
+ return deserializeResultSet(response.db);
1562
+ }
1563
+ process.stderr.write("[db-daemon] Unexpected response shape, falling back to direct\n");
1564
+ return fallbackClient.execute(stmt);
1565
+ },
1566
+ async batch(stmts, mode) {
1567
+ if (!_useDaemon || !isClientConnected()) {
1568
+ return fallbackClient.batch(stmts, mode);
1569
+ }
1570
+ const statements = stmts.map(normalizeStatement2);
1571
+ const response = await sendDaemonRequest({
1572
+ type: "db-batch",
1573
+ statements,
1574
+ mode: mode ?? "deferred"
1575
+ });
1576
+ if (response.error) {
1577
+ const errMsg = String(response.error);
1578
+ if (errMsg === "Not connected" || errMsg === "Request timeout" || errMsg === "Write failed" || errMsg === "DB not initialized") {
1579
+ process.stderr.write(`[db-daemon] Batch transport error (${errMsg}), falling back to direct
1580
+ `);
1581
+ return fallbackClient.batch(stmts, mode);
1582
+ }
1583
+ throw new Error(errMsg);
1584
+ }
1585
+ const batchResults = response["db-batch"];
1586
+ if (batchResults) {
1587
+ return batchResults.map(deserializeResultSet);
1588
+ }
1589
+ process.stderr.write("[db-daemon] Unexpected batch response shape, falling back to direct\n");
1590
+ return fallbackClient.batch(stmts, mode);
1591
+ },
1592
+ // Transaction support — delegate to fallback (transactions need direct connection)
1593
+ async transaction(mode) {
1594
+ return fallbackClient.transaction(mode);
1595
+ },
1596
+ // executeMultiple — delegate to fallback (used only for schema migrations)
1597
+ async executeMultiple(sql) {
1598
+ return fallbackClient.executeMultiple(sql);
1599
+ },
1600
+ // migrate — delegate to fallback
1601
+ async migrate(stmts) {
1602
+ return fallbackClient.migrate(stmts);
1603
+ },
1604
+ // Sync mode — delegate to fallback
1605
+ sync() {
1606
+ return fallbackClient.sync();
1607
+ },
1608
+ close() {
1609
+ _useDaemon = false;
1610
+ },
1611
+ get closed() {
1612
+ return fallbackClient.closed;
1613
+ },
1614
+ get protocol() {
1615
+ return fallbackClient.protocol;
1616
+ }
1617
+ };
1618
+ return {
1619
+ ...client,
1620
+ /** Enable daemon routing (call after confirming daemon is connected) */
1621
+ _enableDaemon() {
1622
+ _useDaemon = true;
1623
+ },
1624
+ /** Check if daemon routing is active */
1625
+ _isDaemonActive() {
1626
+ return _useDaemon && isClientConnected();
1627
+ }
1628
+ };
1629
+ }
1630
+ async function initDaemonDbClient(fallbackClient) {
1631
+ if (process.env.EXE_IS_DAEMON === "1") return null;
1632
+ const connected = await connectEmbedDaemon();
1633
+ if (!connected) {
1634
+ process.stderr.write("[db-daemon] Daemon unavailable \u2014 using direct SQLite\n");
1635
+ return null;
1291
1636
  }
1292
- await client.executeMultiple(`
1293
- CREATE TABLE IF NOT EXISTS consolidations (
1294
- id TEXT PRIMARY KEY,
1295
- consolidated_memory_id TEXT NOT NULL,
1296
- source_memory_id TEXT NOT NULL,
1297
- created_at TEXT NOT NULL
1298
- );
1299
-
1300
- CREATE INDEX IF NOT EXISTS idx_consolidations_source
1301
- ON consolidations(source_memory_id);
1637
+ const client = createDaemonDbClient(fallbackClient);
1638
+ client._enableDaemon();
1639
+ process.stderr.write("[db-daemon] DB routing through daemon (single-writer)\n");
1640
+ return client;
1641
+ }
1642
+ var init_db_daemon_client = __esm({
1643
+ "src/lib/db-daemon-client.ts"() {
1644
+ "use strict";
1645
+ init_exe_daemon_client();
1646
+ init_daemon_protocol();
1647
+ }
1648
+ });
1302
1649
 
1303
- CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
1304
- ON consolidations(consolidated_memory_id);
1305
- `);
1306
- await client.executeMultiple(`
1307
- CREATE TABLE IF NOT EXISTS reminders (
1308
- id TEXT PRIMARY KEY,
1309
- text TEXT NOT NULL,
1310
- created_at TEXT NOT NULL,
1311
- due_date TEXT,
1312
- completed_at TEXT
1313
- );
1314
- `);
1315
- await client.executeMultiple(`
1316
- CREATE TABLE IF NOT EXISTS notifications (
1317
- id TEXT PRIMARY KEY,
1318
- agent_id TEXT NOT NULL,
1319
- agent_role TEXT NOT NULL,
1320
- event TEXT NOT NULL,
1321
- project TEXT NOT NULL,
1322
- summary TEXT NOT NULL,
1323
- task_file TEXT,
1324
- session_scope TEXT,
1325
- read INTEGER NOT NULL DEFAULT 0,
1326
- created_at TEXT NOT NULL
1650
+ // src/lib/database.ts
1651
+ var database_exports = {};
1652
+ __export(database_exports, {
1653
+ disposeDatabase: () => disposeDatabase,
1654
+ disposeTurso: () => disposeTurso,
1655
+ ensureSchema: () => ensureSchema,
1656
+ getClient: () => getClient,
1657
+ getRawClient: () => getRawClient,
1658
+ initDaemonClient: () => initDaemonClient,
1659
+ initDatabase: () => initDatabase,
1660
+ initTurso: () => initTurso,
1661
+ isInitialized: () => isInitialized
1662
+ });
1663
+ import { createClient } from "@libsql/client";
1664
+ async function initDatabase(config) {
1665
+ if (_walCheckpointTimer) {
1666
+ clearInterval(_walCheckpointTimer);
1667
+ _walCheckpointTimer = null;
1668
+ }
1669
+ if (_daemonClient) {
1670
+ _daemonClient.close();
1671
+ _daemonClient = null;
1672
+ }
1673
+ if (_adapterClient && _adapterClient !== _resilientClient) {
1674
+ _adapterClient.close();
1675
+ }
1676
+ _adapterClient = null;
1677
+ if (_client) {
1678
+ _client.close();
1679
+ _client = null;
1680
+ _resilientClient = null;
1681
+ }
1682
+ const opts = {
1683
+ url: `file:${config.dbPath}`
1684
+ };
1685
+ if (config.encryptionKey) {
1686
+ opts.encryptionKey = config.encryptionKey;
1687
+ }
1688
+ _client = createClient(opts);
1689
+ _resilientClient = wrapWithRetry(_client);
1690
+ _adapterClient = _resilientClient;
1691
+ _client.execute("PRAGMA busy_timeout = 30000").catch(() => {
1692
+ });
1693
+ _client.execute("PRAGMA journal_mode = WAL").catch(() => {
1694
+ });
1695
+ if (_walCheckpointTimer) clearInterval(_walCheckpointTimer);
1696
+ _walCheckpointTimer = setInterval(() => {
1697
+ _client?.execute("PRAGMA wal_checkpoint(PASSIVE)").catch(() => {
1698
+ });
1699
+ }, 3e4);
1700
+ _walCheckpointTimer.unref();
1701
+ if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") {
1702
+ _adapterClient = await createPrismaDbAdapter(_resilientClient);
1703
+ }
1704
+ }
1705
+ function isInitialized() {
1706
+ return _adapterClient !== null || _client !== null;
1707
+ }
1708
+ function getClient() {
1709
+ if (!_adapterClient) {
1710
+ throw new Error("Database client not initialized. Call initDatabase() first.");
1711
+ }
1712
+ if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") {
1713
+ return _adapterClient;
1714
+ }
1715
+ if (process.env.EXE_IS_DAEMON === "1") {
1716
+ return _resilientClient;
1717
+ }
1718
+ if (_daemonClient && _daemonClient._isDaemonActive()) {
1719
+ return _daemonClient;
1720
+ }
1721
+ return _resilientClient;
1722
+ }
1723
+ async function initDaemonClient() {
1724
+ if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") return;
1725
+ if (process.env.EXE_IS_DAEMON === "1") return;
1726
+ if (process.env.VITEST) return;
1727
+ if (!_resilientClient) return;
1728
+ if (_daemonClient) return;
1729
+ try {
1730
+ const { initDaemonDbClient: initDaemonDbClient2 } = await Promise.resolve().then(() => (init_db_daemon_client(), db_daemon_client_exports));
1731
+ _daemonClient = await initDaemonDbClient2(_resilientClient);
1732
+ } catch (err) {
1733
+ process.stderr.write(
1734
+ `[database] Daemon client init failed (non-fatal): ${err instanceof Error ? err.message : String(err)}
1735
+ `
1736
+ );
1737
+ }
1738
+ }
1739
+ function getRawClient() {
1740
+ if (!_client) {
1741
+ throw new Error("Database client not initialized. Call initDatabase() first.");
1742
+ }
1743
+ return _client;
1744
+ }
1745
+ async function ensureSchema() {
1746
+ const client = getRawClient();
1747
+ await client.execute("PRAGMA journal_mode = WAL");
1748
+ await client.execute("PRAGMA busy_timeout = 30000");
1749
+ await client.execute("PRAGMA wal_autocheckpoint = 1000");
1750
+ try {
1751
+ await client.execute("PRAGMA libsql_vector_search_ef = 128");
1752
+ } catch {
1753
+ }
1754
+ await client.executeMultiple(`
1755
+ CREATE TABLE IF NOT EXISTS memories (
1756
+ id TEXT PRIMARY KEY,
1757
+ agent_id TEXT NOT NULL,
1758
+ agent_role TEXT NOT NULL,
1759
+ session_id TEXT NOT NULL,
1760
+ timestamp TEXT NOT NULL,
1761
+ tool_name TEXT NOT NULL,
1762
+ project_name TEXT NOT NULL,
1763
+ has_error INTEGER NOT NULL DEFAULT 0,
1764
+ raw_text TEXT NOT NULL,
1765
+ vector F32_BLOB(1024),
1766
+ version INTEGER NOT NULL DEFAULT 0
1327
1767
  );
1328
1768
 
1329
- CREATE INDEX IF NOT EXISTS idx_notifications_read
1330
- ON notifications(read);
1769
+ CREATE INDEX IF NOT EXISTS idx_memories_agent
1770
+ ON memories(agent_id);
1331
1771
 
1332
- CREATE INDEX IF NOT EXISTS idx_notifications_agent
1333
- ON notifications(agent_id, session_scope);
1772
+ CREATE INDEX IF NOT EXISTS idx_memories_timestamp
1773
+ ON memories(timestamp);
1334
1774
 
1335
- CREATE INDEX IF NOT EXISTS idx_notifications_task_file
1336
- ON notifications(task_file);
1775
+ CREATE INDEX IF NOT EXISTS idx_memories_session
1776
+ ON memories(session_id);
1777
+
1778
+ CREATE INDEX IF NOT EXISTS idx_memories_project
1779
+ ON memories(project_name);
1780
+
1781
+ CREATE INDEX IF NOT EXISTS idx_memories_tool
1782
+ ON memories(tool_name);
1783
+
1784
+ CREATE INDEX IF NOT EXISTS idx_memories_version
1785
+ ON memories(version);
1786
+
1787
+ CREATE INDEX IF NOT EXISTS idx_memories_agent_project
1788
+ ON memories(agent_id, project_name);
1337
1789
  `);
1338
1790
  await client.executeMultiple(`
1339
- CREATE TABLE IF NOT EXISTS schedules (
1340
- id TEXT PRIMARY KEY,
1341
- cron TEXT NOT NULL,
1342
- description TEXT NOT NULL,
1343
- job_type TEXT NOT NULL DEFAULT 'report',
1344
- prompt TEXT,
1345
- assigned_to TEXT,
1346
- project_name TEXT,
1347
- active INTEGER NOT NULL DEFAULT 1,
1348
- use_crontab INTEGER NOT NULL DEFAULT 0,
1349
- created_at TEXT NOT NULL
1791
+ CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
1792
+ raw_text,
1793
+ content='memories',
1794
+ content_rowid='rowid'
1350
1795
  );
1796
+
1797
+ CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
1798
+ INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1799
+ END;
1800
+
1801
+ CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
1802
+ INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1803
+ END;
1804
+
1805
+ CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
1806
+ INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1807
+ INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1808
+ END;
1351
1809
  `);
1352
1810
  await client.executeMultiple(`
1353
- CREATE TABLE IF NOT EXISTS device_registry (
1354
- device_id TEXT PRIMARY KEY,
1355
- friendly_name TEXT NOT NULL,
1356
- hostname TEXT NOT NULL,
1357
- projects TEXT NOT NULL DEFAULT '[]',
1358
- agents TEXT NOT NULL DEFAULT '[]',
1359
- connected INTEGER DEFAULT 0,
1360
- last_seen TEXT NOT NULL
1811
+ CREATE TABLE IF NOT EXISTS sync_meta (
1812
+ key TEXT PRIMARY KEY,
1813
+ value TEXT NOT NULL
1361
1814
  );
1362
1815
  `);
1363
1816
  await client.executeMultiple(`
1364
- CREATE TABLE IF NOT EXISTS messages (
1365
- id TEXT PRIMARY KEY,
1366
- from_agent TEXT NOT NULL,
1367
- from_device TEXT NOT NULL DEFAULT 'local',
1368
- target_agent TEXT NOT NULL,
1369
- target_project TEXT,
1370
- target_device TEXT NOT NULL DEFAULT 'local',
1371
- session_scope TEXT,
1372
- content TEXT NOT NULL,
1373
- priority TEXT DEFAULT 'normal',
1374
- status TEXT DEFAULT 'pending',
1375
- server_seq INTEGER,
1376
- retry_count INTEGER DEFAULT 0,
1377
- created_at TEXT NOT NULL,
1378
- delivered_at TEXT,
1379
- processed_at TEXT,
1380
- failed_at TEXT,
1381
- failure_reason TEXT
1817
+ CREATE TABLE IF NOT EXISTS tasks (
1818
+ id TEXT PRIMARY KEY,
1819
+ title TEXT NOT NULL,
1820
+ assigned_to TEXT NOT NULL,
1821
+ assigned_by TEXT NOT NULL,
1822
+ project_name TEXT NOT NULL,
1823
+ priority TEXT NOT NULL DEFAULT 'p1',
1824
+ status TEXT NOT NULL DEFAULT 'open',
1825
+ task_file TEXT,
1826
+ created_at TEXT NOT NULL,
1827
+ updated_at TEXT NOT NULL
1382
1828
  );
1383
1829
 
1384
- CREATE INDEX IF NOT EXISTS idx_messages_target
1385
- ON messages(target_agent, session_scope, status);
1830
+ CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
1831
+ ON tasks(assigned_to, status);
1832
+ `);
1833
+ await client.executeMultiple(`
1834
+ CREATE TABLE IF NOT EXISTS behaviors (
1835
+ id TEXT PRIMARY KEY,
1836
+ agent_id TEXT NOT NULL,
1837
+ project_name TEXT,
1838
+ domain TEXT,
1839
+ content TEXT NOT NULL,
1840
+ active INTEGER NOT NULL DEFAULT 1,
1841
+ created_at TEXT NOT NULL,
1842
+ updated_at TEXT NOT NULL
1843
+ );
1386
1844
 
1387
- CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
1388
- ON messages(target_agent, session_scope, from_agent, server_seq);
1845
+ CREATE INDEX IF NOT EXISTS idx_behaviors_agent
1846
+ ON behaviors(agent_id, active);
1389
1847
  `);
1390
1848
  try {
1391
- await client.execute({
1392
- sql: `ALTER TABLE notifications ADD COLUMN session_scope TEXT`,
1393
- args: []
1849
+ const coordinatorName = getCoordinatorName();
1850
+ const existing = await client.execute({
1851
+ sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = ?",
1852
+ args: [coordinatorName]
1394
1853
  });
1854
+ if (Number(existing.rows[0]?.cnt) === 0) {
1855
+ const seededAt = "2026-03-25T00:00:00Z";
1856
+ for (const [domain, content] of [
1857
+ ["workflow", `Don't ask "keep going?" \u2014 just keep executing phases/plans autonomously`],
1858
+ ["tool-use", "Always use create_task MCP tool, never write .md files directly for task creation"],
1859
+ ["workflow", "Auto-start reviewing when idle and reviews are pending \u2014 never ask founder for permission"]
1860
+ ]) {
1861
+ await client.execute({
1862
+ sql: `INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
1863
+ VALUES (hex(randomblob(16)), ?, NULL, ?, ?, 1, ?, ?)`,
1864
+ args: [coordinatorName, domain, content, seededAt, seededAt]
1865
+ });
1866
+ }
1867
+ }
1395
1868
  } catch {
1396
1869
  }
1397
1870
  try {
1398
1871
  await client.execute({
1399
- sql: `ALTER TABLE messages ADD COLUMN session_scope TEXT`,
1872
+ sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
1400
1873
  args: []
1401
1874
  });
1402
1875
  } catch {
1403
1876
  }
1404
- await client.executeMultiple(`
1405
- CREATE INDEX IF NOT EXISTS idx_notifications_agent_scope_read
1406
- ON notifications(agent_id, session_scope, read, created_at);
1407
-
1408
- CREATE INDEX IF NOT EXISTS idx_messages_target_scope_status
1409
- ON messages(target_agent, session_scope, status, created_at);
1410
- `);
1411
1877
  try {
1412
1878
  await client.execute({
1413
- sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
1414
- args: []
1415
- });
1416
- await client.execute({
1417
- sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
1879
+ sql: `ALTER TABLE behaviors ADD COLUMN vector F32_BLOB(${EMBEDDING_DIM})`,
1418
1880
  args: []
1419
1881
  });
1882
+ } catch {
1883
+ }
1884
+ try {
1420
1885
  await client.execute({
1421
- sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
1886
+ sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
1422
1887
  args: []
1423
1888
  });
1889
+ } catch {
1890
+ }
1891
+ try {
1424
1892
  await client.execute({
1425
- sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
1893
+ sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
1426
1894
  args: []
1427
1895
  });
1428
1896
  } catch {
1429
1897
  }
1430
- await client.executeMultiple(`
1431
- CREATE TABLE IF NOT EXISTS trajectories (
1898
+ try {
1899
+ await client.execute({
1900
+ sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
1901
+ ON tasks(parent_task_id)
1902
+ WHERE parent_task_id IS NOT NULL`,
1903
+ args: []
1904
+ });
1905
+ } catch {
1906
+ }
1907
+ try {
1908
+ await client.execute({
1909
+ sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
1910
+ args: []
1911
+ });
1912
+ } catch {
1913
+ }
1914
+ try {
1915
+ await client.execute({
1916
+ sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
1917
+ args: []
1918
+ });
1919
+ } catch {
1920
+ }
1921
+ try {
1922
+ await client.execute({
1923
+ sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
1924
+ args: []
1925
+ });
1926
+ } catch {
1927
+ }
1928
+ try {
1929
+ await client.execute({
1930
+ sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
1931
+ args: []
1932
+ });
1933
+ } catch {
1934
+ }
1935
+ try {
1936
+ await client.execute({
1937
+ sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
1938
+ args: []
1939
+ });
1940
+ } catch {
1941
+ }
1942
+ try {
1943
+ await client.execute({
1944
+ sql: `ALTER TABLE tasks ADD COLUMN checkpoint TEXT`,
1945
+ args: []
1946
+ });
1947
+ } catch {
1948
+ }
1949
+ try {
1950
+ await client.execute({
1951
+ sql: `ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER NOT NULL DEFAULT 0`,
1952
+ args: []
1953
+ });
1954
+ } catch {
1955
+ }
1956
+ try {
1957
+ await client.execute({
1958
+ sql: `ALTER TABLE tasks ADD COLUMN complexity TEXT NOT NULL DEFAULT 'standard'`,
1959
+ args: []
1960
+ });
1961
+ } catch {
1962
+ }
1963
+ try {
1964
+ await client.execute({
1965
+ sql: `ALTER TABLE tasks ADD COLUMN session_scope TEXT`,
1966
+ args: []
1967
+ });
1968
+ } catch {
1969
+ }
1970
+ try {
1971
+ await client.execute({
1972
+ sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
1973
+ args: []
1974
+ });
1975
+ } catch {
1976
+ }
1977
+ try {
1978
+ await client.execute({
1979
+ sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
1980
+ args: []
1981
+ });
1982
+ } catch {
1983
+ }
1984
+ try {
1985
+ await client.execute({
1986
+ sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
1987
+ args: []
1988
+ });
1989
+ } catch {
1990
+ }
1991
+ try {
1992
+ await client.execute({
1993
+ sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
1994
+ args: []
1995
+ });
1996
+ } catch {
1997
+ }
1998
+ await client.executeMultiple(`
1999
+ CREATE TABLE IF NOT EXISTS consolidations (
2000
+ id TEXT PRIMARY KEY,
2001
+ consolidated_memory_id TEXT NOT NULL,
2002
+ source_memory_id TEXT NOT NULL,
2003
+ created_at TEXT NOT NULL
2004
+ );
2005
+
2006
+ CREATE INDEX IF NOT EXISTS idx_consolidations_source
2007
+ ON consolidations(source_memory_id);
2008
+
2009
+ CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
2010
+ ON consolidations(consolidated_memory_id);
2011
+ `);
2012
+ await client.executeMultiple(`
2013
+ CREATE TABLE IF NOT EXISTS reminders (
2014
+ id TEXT PRIMARY KEY,
2015
+ text TEXT NOT NULL,
2016
+ created_at TEXT NOT NULL,
2017
+ due_date TEXT,
2018
+ completed_at TEXT
2019
+ );
2020
+ `);
2021
+ await client.executeMultiple(`
2022
+ CREATE TABLE IF NOT EXISTS notifications (
2023
+ id TEXT PRIMARY KEY,
2024
+ agent_id TEXT NOT NULL,
2025
+ agent_role TEXT NOT NULL,
2026
+ event TEXT NOT NULL,
2027
+ project TEXT NOT NULL,
2028
+ summary TEXT NOT NULL,
2029
+ task_file TEXT,
2030
+ session_scope TEXT,
2031
+ read INTEGER NOT NULL DEFAULT 0,
2032
+ created_at TEXT NOT NULL
2033
+ );
2034
+
2035
+ CREATE INDEX IF NOT EXISTS idx_notifications_read
2036
+ ON notifications(read);
2037
+
2038
+ CREATE INDEX IF NOT EXISTS idx_notifications_agent
2039
+ ON notifications(agent_id, session_scope);
2040
+
2041
+ CREATE INDEX IF NOT EXISTS idx_notifications_task_file
2042
+ ON notifications(task_file);
2043
+ `);
2044
+ await client.executeMultiple(`
2045
+ CREATE TABLE IF NOT EXISTS schedules (
2046
+ id TEXT PRIMARY KEY,
2047
+ cron TEXT NOT NULL,
2048
+ description TEXT NOT NULL,
2049
+ job_type TEXT NOT NULL DEFAULT 'report',
2050
+ prompt TEXT,
2051
+ assigned_to TEXT,
2052
+ project_name TEXT,
2053
+ active INTEGER NOT NULL DEFAULT 1,
2054
+ use_crontab INTEGER NOT NULL DEFAULT 0,
2055
+ created_at TEXT NOT NULL
2056
+ );
2057
+ `);
2058
+ await client.executeMultiple(`
2059
+ CREATE TABLE IF NOT EXISTS device_registry (
2060
+ device_id TEXT PRIMARY KEY,
2061
+ friendly_name TEXT NOT NULL,
2062
+ hostname TEXT NOT NULL,
2063
+ projects TEXT NOT NULL DEFAULT '[]',
2064
+ agents TEXT NOT NULL DEFAULT '[]',
2065
+ connected INTEGER DEFAULT 0,
2066
+ last_seen TEXT NOT NULL
2067
+ );
2068
+ `);
2069
+ await client.executeMultiple(`
2070
+ CREATE TABLE IF NOT EXISTS messages (
2071
+ id TEXT PRIMARY KEY,
2072
+ from_agent TEXT NOT NULL,
2073
+ from_device TEXT NOT NULL DEFAULT 'local',
2074
+ target_agent TEXT NOT NULL,
2075
+ target_project TEXT,
2076
+ target_device TEXT NOT NULL DEFAULT 'local',
2077
+ session_scope TEXT,
2078
+ content TEXT NOT NULL,
2079
+ priority TEXT DEFAULT 'normal',
2080
+ status TEXT DEFAULT 'pending',
2081
+ server_seq INTEGER,
2082
+ retry_count INTEGER DEFAULT 0,
2083
+ created_at TEXT NOT NULL,
2084
+ delivered_at TEXT,
2085
+ processed_at TEXT,
2086
+ failed_at TEXT,
2087
+ failure_reason TEXT
2088
+ );
2089
+
2090
+ CREATE INDEX IF NOT EXISTS idx_messages_target
2091
+ ON messages(target_agent, session_scope, status);
2092
+
2093
+ CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
2094
+ ON messages(target_agent, session_scope, from_agent, server_seq);
2095
+ `);
2096
+ try {
2097
+ await client.execute({
2098
+ sql: `ALTER TABLE notifications ADD COLUMN session_scope TEXT`,
2099
+ args: []
2100
+ });
2101
+ } catch {
2102
+ }
2103
+ try {
2104
+ await client.execute({
2105
+ sql: `ALTER TABLE messages ADD COLUMN session_scope TEXT`,
2106
+ args: []
2107
+ });
2108
+ } catch {
2109
+ }
2110
+ await client.executeMultiple(`
2111
+ CREATE INDEX IF NOT EXISTS idx_notifications_agent_scope_read
2112
+ ON notifications(agent_id, session_scope, read, created_at);
2113
+
2114
+ CREATE INDEX IF NOT EXISTS idx_messages_target_scope_status
2115
+ ON messages(target_agent, session_scope, status, created_at);
2116
+ `);
2117
+ try {
2118
+ await client.execute({
2119
+ sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
2120
+ args: []
2121
+ });
2122
+ await client.execute({
2123
+ sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
2124
+ args: []
2125
+ });
2126
+ await client.execute({
2127
+ sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
2128
+ args: []
2129
+ });
2130
+ await client.execute({
2131
+ sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
2132
+ args: []
2133
+ });
2134
+ } catch {
2135
+ }
2136
+ await client.executeMultiple(`
2137
+ CREATE TABLE IF NOT EXISTS trajectories (
1432
2138
  id TEXT PRIMARY KEY,
1433
2139
  task_id TEXT NOT NULL,
1434
2140
  agent_id TEXT NOT NULL,
@@ -2045,6 +2751,7 @@ var init_database = __esm({
2045
2751
  init_db_retry();
2046
2752
  init_employees();
2047
2753
  init_database_adapter();
2754
+ init_memory();
2048
2755
  _client = null;
2049
2756
  _resilientClient = null;
2050
2757
  _walCheckpointTimer = null;
@@ -2057,14 +2764,14 @@ var init_database = __esm({
2057
2764
 
2058
2765
  // src/lib/keychain.ts
2059
2766
  import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod as chmod2 } from "fs/promises";
2060
- import { existsSync as existsSync4 } from "fs";
2061
- import path4 from "path";
2062
- import os4 from "os";
2767
+ import { existsSync as existsSync6 } from "fs";
2768
+ import path6 from "path";
2769
+ import os5 from "os";
2063
2770
  function getKeyDir() {
2064
- return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path4.join(os4.homedir(), ".exe-os");
2771
+ return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path6.join(os5.homedir(), ".exe-os");
2065
2772
  }
2066
2773
  function getKeyPath() {
2067
- return path4.join(getKeyDir(), "master.key");
2774
+ return path6.join(getKeyDir(), "master.key");
2068
2775
  }
2069
2776
  async function tryKeytar() {
2070
2777
  try {
@@ -2085,9 +2792,9 @@ async function getMasterKey() {
2085
2792
  }
2086
2793
  }
2087
2794
  const keyPath = getKeyPath();
2088
- if (!existsSync4(keyPath)) {
2795
+ if (!existsSync6(keyPath)) {
2089
2796
  process.stderr.write(
2090
- `[keychain] Key not found at ${keyPath} (HOME=${os4.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
2797
+ `[keychain] Key not found at ${keyPath} (HOME=${os5.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
2091
2798
  `
2092
2799
  );
2093
2800
  return null;
@@ -2181,12 +2888,12 @@ __export(shard_manager_exports, {
2181
2888
  listShards: () => listShards,
2182
2889
  shardExists: () => shardExists
2183
2890
  });
2184
- import path5 from "path";
2185
- import { existsSync as existsSync5, mkdirSync as mkdirSync2, readdirSync } from "fs";
2891
+ import path7 from "path";
2892
+ import { existsSync as existsSync7, mkdirSync as mkdirSync2, readdirSync } from "fs";
2186
2893
  import { createClient as createClient2 } from "@libsql/client";
2187
2894
  function initShardManager(encryptionKey) {
2188
2895
  _encryptionKey = encryptionKey;
2189
- if (!existsSync5(SHARDS_DIR)) {
2896
+ if (!existsSync7(SHARDS_DIR)) {
2190
2897
  mkdirSync2(SHARDS_DIR, { recursive: true });
2191
2898
  }
2192
2899
  _shardingEnabled = true;
@@ -2216,7 +2923,7 @@ function getShardClient(projectName) {
2216
2923
  while (_shards.size >= MAX_OPEN_SHARDS) {
2217
2924
  evictLRU();
2218
2925
  }
2219
- const dbPath = path5.join(SHARDS_DIR, `${safeName}.db`);
2926
+ const dbPath = path7.join(SHARDS_DIR, `${safeName}.db`);
2220
2927
  const client = createClient2({
2221
2928
  url: `file:${dbPath}`,
2222
2929
  encryptionKey: _encryptionKey
@@ -2227,10 +2934,10 @@ function getShardClient(projectName) {
2227
2934
  }
2228
2935
  function shardExists(projectName) {
2229
2936
  const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
2230
- return existsSync5(path5.join(SHARDS_DIR, `${safeName}.db`));
2937
+ return existsSync7(path7.join(SHARDS_DIR, `${safeName}.db`));
2231
2938
  }
2232
2939
  function listShards() {
2233
- if (!existsSync5(SHARDS_DIR)) return [];
2940
+ if (!existsSync7(SHARDS_DIR)) return [];
2234
2941
  return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
2235
2942
  }
2236
2943
  async function ensureShardSchema(client) {
@@ -2477,7 +3184,7 @@ var init_shard_manager = __esm({
2477
3184
  "src/lib/shard-manager.ts"() {
2478
3185
  "use strict";
2479
3186
  init_config();
2480
- SHARDS_DIR = path5.join(EXE_AI_DIR, "shards");
3187
+ SHARDS_DIR = path7.join(EXE_AI_DIR, "shards");
2481
3188
  SHARD_IDLE_MS = 5 * 60 * 1e3;
2482
3189
  MAX_OPEN_SHARDS = 10;
2483
3190
  EVICTION_INTERVAL_MS = 60 * 1e3;
@@ -2597,14 +3304,76 @@ var init_platform_procedures = __esm({
2597
3304
  domain: "architecture",
2598
3305
  priority: "p0",
2599
3306
  content: "Tasks live in the DB. Intercom (tmux send-keys) is fire-and-forget \u2014 it may fail, get garbled, or arrive mid-work. Never rely on intercom for task delivery. The UserPromptSubmit hook checks the DB for new tasks on every prompt. Your operating procedures step 7 says check for next work. The daemon nudges idle agents as a speedup. If you have no tasks, you found them all."
2600
- }
2601
- ];
2602
- PLATFORM_PROCEDURE_TITLES = new Set(
2603
- PLATFORM_PROCEDURES.map((p) => p.title)
2604
- );
2605
- }
2606
- });
2607
-
3307
+ },
3308
+ // --- MCP is the ONLY data interface ---
3309
+ {
3310
+ title: "MCP disconnect \u2014 ask the user, never work around it",
3311
+ domain: "workflow",
3312
+ priority: "p0",
3313
+ content: "If MCP tools are unavailable, disconnected, or returning connection errors: STOP. Tell the user clearly: 'MCP server is disconnected. Please run /mcp to reconnect.' Do NOT attempt workarounds \u2014 no raw Node imports, no direct DB access, no CLI hacks, no daemon socket calls. MCP is the ONLY data interface. Working around it wastes time, hits bundling issues, and bypasses the contract boundary. Ask once, wait, proceed when reconnected."
3314
+ },
3315
+ // --- MCP Tool Catalog (Layer 0 — every agent knows what tools exist) ---
3316
+ {
3317
+ title: "MCP tools \u2014 memory and search",
3318
+ domain: "tool-use",
3319
+ priority: "p1",
3320
+ content: "recall_my_memory: search your own memories (semantic + FTS). ask_team_memory: search a colleague's memories by agent name. store_memory: persist a memory (decisions, summaries, context). commit_memory: high-importance memory that survives consolidation. search_everything: unified search across memories, tasks, entities, conversations. get_session_context: temporal window of memories around a timestamp. consolidate_memories: merge duplicate/related memories into insights. get_memory_cardinality: count memories per agent (health check)."
3321
+ },
3322
+ {
3323
+ title: "MCP tools \u2014 task orchestration",
3324
+ domain: "tool-use",
3325
+ priority: "p1",
3326
+ content: "create_task: dispatch work to an employee (auto-spawns session). The ONLY dispatch path. list_tasks: query tasks by status, assignee, project. get_task: fetch full task details by ID. update_task: change status (in_progress, done, blocked, cancelled) + add result summary. close_task: finalize a reviewed task (COO only). checkpoint_task: save progress state for crash recovery. resume_employee: re-spawn an employee session for an existing task."
3327
+ },
3328
+ {
3329
+ title: "MCP tools \u2014 knowledge graph (GraphRAG)",
3330
+ domain: "tool-use",
3331
+ priority: "p1",
3332
+ content: "query_relationships: find connections between entities in the knowledge graph. get_entity_neighbors: explore an entity's direct connections. get_hot_entities: find most-referenced entities (trending topics). get_graph_stats: graph health \u2014 entity/relationship counts, density. export_graph: export graph data for visualization. merge_entities: deduplicate entities (alias resolution). find_similar_trajectories: match tool-call patterns to past task solutions."
3333
+ },
3334
+ {
3335
+ title: "MCP tools \u2014 identity, behavior, and decisions",
3336
+ domain: "tool-use",
3337
+ priority: "p1",
3338
+ content: "get_identity: read an agent's exe.md (Layer 1 identity). update_identity: write an agent's exe.md. Identity > behavior \u2014 use for permanent rules. store_behavior: record a correction or pattern for an agent (Layer 2 expertise). list_behaviors: view an agent's active behaviors. deactivate_behavior: soft-delete a stale or conflicting behavior. store_decision: record an ADR (architectural decision record). get_decision: retrieve a past decision by query."
3339
+ },
3340
+ {
3341
+ title: "MCP tools \u2014 communication and messaging",
3342
+ domain: "tool-use",
3343
+ priority: "p1",
3344
+ content: "send_message: send supplementary context to another agent (NOT for actionable work \u2014 use create_task). acknowledge_messages: mark messages as read. send_whatsapp: send WhatsApp message via gateway (customer-facing alerts). query_conversations: search ingested conversations across all channels (WhatsApp, email, etc.)."
3345
+ },
3346
+ {
3347
+ title: "MCP tools \u2014 wiki, documents, and content",
3348
+ domain: "tool-use",
3349
+ priority: "p1",
3350
+ content: "create_wiki_page: create a wiki page in exe-wiki. list_wiki_pages: browse wiki pages. get_wiki_page: read a wiki page. update_wiki_page: edit a wiki page. ingest_document: import a file (PDF, MD, etc.) into memory as chunks. list_documents: browse ingested documents by workspace. purge_document: remove a document and its memory chunks. set_document_importance: adjust chunk importance scores. rerank_documents: re-score document relevance for a query."
3351
+ },
3352
+ {
3353
+ title: "MCP tools \u2014 system, operations, and admin",
3354
+ domain: "tool-use",
3355
+ priority: "p1",
3356
+ content: "get_agent_spend: token usage per agent/session (cost tracking). list_agent_sessions: view agent session history. get_session_kills: audit log of killed sessions. get_daemon_health: check exed daemon status. get_auto_wake_status: daemon auto-wake configuration. get_worker_gate: check worker deployment gates. run_memory_audit: health check \u2014 duplicates, null vectors, orphaned rows. run_consolidation: trigger sleep-time memory consolidation. cloud_sync: force a cloud sync cycle. backup_vps: trigger VPS backup."
3357
+ },
3358
+ {
3359
+ title: "MCP tools \u2014 config, licensing, and team",
3360
+ domain: "tool-use",
3361
+ priority: "p1",
3362
+ content: "set_agent_config: view/change per-agent runtime and model settings. list_employees: view the employee roster. add_person: add a person to the CRM contacts roster. list_people: browse CRM contacts. get_person: fetch contact details. get_license_status: check license validity. create_license: generate a new license key (admin). list_licenses: view all issued licenses. activate_license: activate a license on a device."
3363
+ },
3364
+ {
3365
+ title: "MCP tools \u2014 advanced (triggers, skills, orchestration)",
3366
+ domain: "tool-use",
3367
+ priority: "p1",
3368
+ content: "create_trigger: set up a scheduled recurring agent job (cron). list_triggers: view active triggers. load_skill: load a slash-command skill dynamically. apply_starter_pack: import a pre-built behavior + identity pack for a role. export_orchestration: export full org state (tasks, behaviors, identities) as portable JSON. import_orchestration: import org state into a new instance. deploy_client: deploy a customer client instance. query_company_brain: unified RAG query across all company knowledge. create_reminder: set a text reminder (shown in boot brief). list_reminders: view pending reminders. complete_reminder: mark a reminder done. global_procedure: manage Layer 0 procedures (actions: store, list, deactivate). Legacy aliases: store_global_procedure, list_global_procedures, deactivate_global_procedure."
3369
+ }
3370
+ ];
3371
+ PLATFORM_PROCEDURE_TITLES = new Set(
3372
+ PLATFORM_PROCEDURES.map((p) => p.title)
3373
+ );
3374
+ }
3375
+ });
3376
+
2608
3377
  // src/lib/global-procedures.ts
2609
3378
  var global_procedures_exports = {};
2610
3379
  __export(global_procedures_exports, {
@@ -2613,7 +3382,7 @@ __export(global_procedures_exports, {
2613
3382
  loadGlobalProcedures: () => loadGlobalProcedures,
2614
3383
  storeGlobalProcedure: () => storeGlobalProcedure
2615
3384
  });
2616
- import { randomUUID } from "crypto";
3385
+ import { randomUUID as randomUUID2 } from "crypto";
2617
3386
  async function loadGlobalProcedures() {
2618
3387
  const client = getClient();
2619
3388
  const result = await client.execute({
@@ -2642,7 +3411,7 @@ ${sections.join("\n\n")}
2642
3411
  `;
2643
3412
  }
2644
3413
  async function storeGlobalProcedure(input) {
2645
- const id = randomUUID();
3414
+ const id = randomUUID2();
2646
3415
  const now = (/* @__PURE__ */ new Date()).toISOString();
2647
3416
  const client = getClient();
2648
3417
  await client.execute({
@@ -2746,6 +3515,11 @@ async function initStore(options) {
2746
3515
  encryptionKey: hexKey
2747
3516
  });
2748
3517
  await retryOnBusy2(() => ensureSchema(), "ensureSchema");
3518
+ try {
3519
+ const { initDaemonClient: initDaemonClient2 } = await Promise.resolve().then(() => (init_database(), database_exports));
3520
+ await initDaemonClient2();
3521
+ } catch {
3522
+ }
2749
3523
  if (!options?.lightweight) {
2750
3524
  try {
2751
3525
  const { initShardManager: initShardManager2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
@@ -2984,962 +3758,496 @@ async function flushBatch() {
2984
3758
  userId,
2985
3759
  charOffset,
2986
3760
  pageNumber,
2987
- sourcePath,
2988
- sourceType,
2989
- tier,
2990
- supersedesId,
2991
- draft,
2992
- memoryType,
2993
- trajectory,
2994
- contentHash
2995
- ];
2996
- return {
2997
- sql: hasVector ? `INSERT OR IGNORE INTO memories (${cols})
2998
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, vector32(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` : `INSERT OR IGNORE INTO memories (${cols})
2999
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
3000
- args: hasVector ? [...baseArgs, vectorToBlob(row.vector), ...sharedArgs, ...metaArgs] : [...baseArgs, ...sharedArgs, ...metaArgs]
3001
- };
3002
- };
3003
- const globalClient = getClient();
3004
- const globalStmts = batch.map(buildStmt);
3005
- await globalClient.batch(globalStmts, "write");
3006
- _pendingRecords.splice(0, batch.length);
3007
- try {
3008
- const { isShardingEnabled: isShardingEnabled2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
3009
- if (isShardingEnabled2()) {
3010
- const byProject = /* @__PURE__ */ new Map();
3011
- for (const row of batch) {
3012
- const proj = row.project_name || "unknown";
3013
- if (!byProject.has(proj)) byProject.set(proj, []);
3014
- byProject.get(proj).push(row);
3015
- }
3016
- for (const [project, rows] of byProject) {
3017
- try {
3018
- const shardClient = await getReadyShardClient2(project);
3019
- const shardStmts = rows.map(buildStmt);
3020
- await shardClient.batch(shardStmts, "write");
3021
- } catch (err) {
3022
- process.stderr.write(
3023
- `[store] Shard write failed for ${project}: ${err instanceof Error ? err.message : String(err)}
3024
- `
3025
- );
3026
- }
3027
- }
3028
- }
3029
- } catch {
3030
- }
3031
- return batch.length;
3032
- } finally {
3033
- _flushing = false;
3034
- }
3035
- }
3036
- function buildWikiScopeFilter(options, columnPrefix) {
3037
- const args = [];
3038
- let clause = "";
3039
- if (options?.workspaceId !== void 0) {
3040
- clause += ` AND ${columnPrefix}workspace_id = ?`;
3041
- args.push(options.workspaceId);
3042
- }
3043
- if (options?.userId === void 0) {
3044
- clause += ` AND ${columnPrefix}user_id IS NULL`;
3045
- } else if (options.userId === null) {
3046
- clause += ` AND ${columnPrefix}user_id IS NULL`;
3047
- } else {
3048
- clause += ` AND (${columnPrefix}user_id = ? OR ${columnPrefix}user_id IS NULL)`;
3049
- args.push(options.userId);
3050
- }
3051
- return { clause, args };
3052
- }
3053
- function buildRawVisibilityFilter(options, columnPrefix) {
3054
- if (options?.includeRaw === false) {
3055
- return {
3056
- clause: ` AND COALESCE(${columnPrefix}memory_type, 'raw') != 'raw'`,
3057
- args: []
3058
- };
3059
- }
3060
- return { clause: "", args: [] };
3061
- }
3062
- async function searchMemories(queryVector, agentId, options) {
3063
- let client;
3064
- try {
3065
- const { isShardingEnabled: isShardingEnabled2, shardExists: shardExists2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
3066
- if (isShardingEnabled2() && options?.projectName && shardExists2(options.projectName)) {
3067
- client = await getReadyShardClient2(options.projectName);
3068
- } else {
3069
- client = getClient();
3070
- }
3071
- } catch {
3072
- client = getClient();
3073
- }
3074
- const limit = options?.limit ?? 10;
3075
- const statusFilter = options?.includeArchived ? "" : `
3076
- AND COALESCE(status, 'active') = 'active'`;
3077
- const draftFilter = options?.includeDrafts ? "" : `
3078
- AND (draft = 0 OR draft IS NULL)`;
3079
- let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
3080
- tool_name, project_name,
3081
- has_error, raw_text, vector, importance, status,
3082
- confidence, last_accessed,
3083
- workspace_id, document_id, user_id,
3084
- char_offset, page_number,
3085
- source_path, source_type
3086
- FROM memories
3087
- WHERE agent_id = ?
3088
- AND vector IS NOT NULL${statusFilter}${draftFilter}
3089
- AND COALESCE(confidence, 0.7) >= 0.3`;
3090
- const args = [agentId];
3091
- const scope = buildWikiScopeFilter(options, "");
3092
- sql += scope.clause;
3093
- args.push(...scope.args);
3094
- const rawVisibility = buildRawVisibilityFilter(options, "");
3095
- sql += rawVisibility.clause;
3096
- args.push(...rawVisibility.args);
3097
- if (options?.projectName) {
3098
- sql += ` AND project_name = ?`;
3099
- args.push(options.projectName);
3100
- }
3101
- if (options?.toolName) {
3102
- sql += ` AND tool_name = ?`;
3103
- args.push(options.toolName);
3104
- }
3105
- if (options?.hasError !== void 0) {
3106
- sql += ` AND has_error = ?`;
3107
- args.push(options.hasError ? 1 : 0);
3108
- }
3109
- if (options?.since) {
3110
- sql += ` AND timestamp >= ?`;
3111
- args.push(options.since);
3112
- }
3113
- if (options?.memoryType) {
3114
- sql += ` AND memory_type = ?`;
3115
- args.push(options.memoryType);
3116
- }
3117
- sql += ` ORDER BY vector_distance_cos(vector, vector32(?))`;
3118
- args.push(vectorToBlob(queryVector));
3119
- sql += ` LIMIT ?`;
3120
- args.push(limit);
3121
- const result = await client.execute({ sql, args });
3122
- return result.rows.map((row) => ({
3123
- id: row.id,
3124
- agent_id: row.agent_id,
3125
- agent_role: row.agent_role,
3126
- session_id: row.session_id,
3127
- timestamp: row.timestamp,
3128
- tool_name: row.tool_name,
3129
- project_name: row.project_name,
3130
- has_error: row.has_error === 1,
3131
- raw_text: row.raw_text,
3132
- vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
3133
- importance: row.importance ?? 5,
3134
- status: row.status ?? "active",
3135
- confidence: row.confidence ?? 0.7,
3136
- last_accessed: row.last_accessed ?? row.timestamp,
3137
- workspace_id: row.workspace_id ?? null,
3138
- document_id: row.document_id ?? null,
3139
- user_id: row.user_id ?? null,
3140
- char_offset: row.char_offset ?? null,
3141
- page_number: row.page_number ?? null,
3142
- source_path: row.source_path ?? null,
3143
- source_type: row.source_type ?? null
3144
- }));
3145
- }
3146
- async function attachDocumentMetadata(records) {
3147
- const docIds = [
3148
- ...new Set(
3149
- records.map((r) => r.document_id).filter((id) => typeof id === "string" && id.length > 0)
3150
- )
3151
- ];
3152
- if (docIds.length === 0) return records;
3153
- try {
3154
- const client = getClient();
3155
- const placeholders = docIds.map(() => "?").join(",");
3156
- const result = await client.execute({
3157
- sql: `SELECT id, filename, mime, source_type, uploaded_at
3158
- FROM documents
3159
- WHERE id IN (${placeholders})`,
3160
- args: docIds
3161
- });
3162
- const byId = /* @__PURE__ */ new Map();
3163
- for (const row of result.rows) {
3164
- const id = row.id;
3165
- byId.set(id, {
3166
- document_id: id,
3167
- filename: row.filename,
3168
- mime: row.mime ?? null,
3169
- source_type: row.source_type ?? null,
3170
- uploaded_at: row.uploaded_at
3171
- });
3172
- }
3173
- for (const record of records) {
3174
- if (!record.document_id) continue;
3175
- record.document_metadata = byId.get(record.document_id) ?? null;
3176
- }
3177
- } catch {
3178
- }
3179
- return records;
3180
- }
3181
- async function flushTier3(agentId, options) {
3182
- const client = getClient();
3183
- const maxAge = options?.maxAgeHours ?? 72;
3184
- const cutoff = new Date(Date.now() - maxAge * 36e5).toISOString();
3185
- if (options?.dryRun) {
3186
- const result2 = await client.execute({
3187
- sql: `SELECT COUNT(*) as cnt FROM memories
3188
- WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
3189
- args: [agentId, cutoff]
3190
- });
3191
- return { archived: Number(result2.rows[0]?.cnt ?? 0) };
3192
- }
3193
- const result = await client.execute({
3194
- sql: `UPDATE memories SET status = 'archived'
3195
- WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
3196
- args: [agentId, cutoff]
3197
- });
3198
- return { archived: result.rowsAffected };
3199
- }
3200
- async function disposeStore() {
3201
- if (_flushTimer !== null) {
3202
- clearInterval(_flushTimer);
3203
- _flushTimer = null;
3204
- }
3205
- if (_pendingRecords.length > 0) {
3206
- await flushBatch();
3207
- }
3208
- await disposeTurso();
3209
- _pendingRecords = [];
3210
- _nextVersion = 1;
3211
- }
3212
- function vectorToBlob(vector) {
3213
- const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
3214
- return JSON.stringify(Array.from(f32));
3215
- }
3216
- async function updateMemoryStatus(id, status) {
3217
- const client = getClient();
3218
- await client.execute({
3219
- sql: `UPDATE memories SET status = ? WHERE id = ?`,
3220
- args: [status, id]
3221
- });
3222
- }
3223
- function reserveVersions(count) {
3224
- const reserved = [];
3225
- for (let i = 0; i < count; i++) {
3226
- reserved.push(_nextVersion++);
3227
- }
3228
- return reserved;
3229
- }
3230
- async function getMemoryCardinality(agentId) {
3231
- try {
3232
- const client = getClient();
3233
- const result = await client.execute({
3234
- sql: `SELECT COUNT(*) as cnt FROM memories WHERE agent_id = ? AND COALESCE(status, 'active') = 'active'`,
3235
- args: [agentId]
3236
- });
3237
- return Number(result.rows[0]?.cnt) || 0;
3238
- } catch {
3239
- return 0;
3240
- }
3241
- }
3242
- var INIT_MAX_RETRIES, INIT_RETRY_DELAY_MS, _pendingRecords, _batchSize, _flushIntervalMs, _flushTimer, _flushing, _nextVersion;
3243
- var init_store = __esm({
3244
- "src/lib/store.ts"() {
3245
- "use strict";
3246
- init_memory();
3247
- init_database();
3248
- init_keychain();
3249
- init_config();
3250
- init_state_bus();
3251
- INIT_MAX_RETRIES = 3;
3252
- INIT_RETRY_DELAY_MS = 1e3;
3253
- _pendingRecords = [];
3254
- _batchSize = 20;
3255
- _flushIntervalMs = 1e4;
3256
- _flushTimer = null;
3257
- _flushing = false;
3258
- _nextVersion = 1;
3259
- }
3260
- });
3261
-
3262
- // src/lib/self-query-router.ts
3263
- var self_query_router_exports = {};
3264
- __export(self_query_router_exports, {
3265
- routeQuery: () => routeQuery
3266
- });
3267
- async function routeQuery(query, model = "claude-haiku-4-5-20251001") {
3268
- if (query.length < 10) {
3269
- return {
3270
- semanticQuery: query,
3271
- projectFilter: null,
3272
- roleFilter: null,
3273
- timeFilter: null,
3274
- isBroadQuery: false
3275
- };
3276
- }
3277
- try {
3278
- const Anthropic = (await import("@anthropic-ai/sdk")).default;
3279
- const client = new Anthropic();
3280
- const now = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
3281
- const response = await client.messages.create({
3282
- model,
3283
- max_tokens: 256,
3284
- system: `You are a search query router. Extract metadata filters from the user's memory search query. Today is ${now}. Convert relative time references (yesterday, last week) to ISO timestamps.`,
3285
- messages: [{ role: "user", content: query }],
3286
- tools: [EXTRACT_TOOL],
3287
- tool_choice: { type: "tool", name: "extract_search_filters" }
3288
- });
3289
- const toolBlock = response.content.find((b) => b.type === "tool_use");
3290
- if (toolBlock && toolBlock.type === "tool_use") {
3291
- const input = toolBlock.input;
3292
- return {
3293
- semanticQuery: input.semantic_query || query,
3294
- projectFilter: input.project_filter || null,
3295
- roleFilter: input.role_filter || null,
3296
- timeFilter: input.time_filter || null,
3297
- isBroadQuery: Boolean(input.is_broad_query)
3298
- };
3299
- }
3300
- } catch (err) {
3301
- process.stderr.write(
3302
- `[self-query-router] LLM extraction failed, using passthrough: ${err instanceof Error ? err.message : String(err)}
3303
- `
3304
- );
3305
- }
3306
- return {
3307
- semanticQuery: query,
3308
- projectFilter: null,
3309
- roleFilter: null,
3310
- timeFilter: null,
3311
- isBroadQuery: false
3312
- };
3313
- }
3314
- var EXTRACT_TOOL;
3315
- var init_self_query_router = __esm({
3316
- "src/lib/self-query-router.ts"() {
3317
- "use strict";
3318
- EXTRACT_TOOL = {
3319
- name: "extract_search_filters",
3320
- description: "Extract metadata filters from a memory search query to improve retrieval precision.",
3321
- input_schema: {
3322
- type: "object",
3323
- properties: {
3324
- semantic_query: {
3325
- type: "string",
3326
- description: "The core semantic meaning of the query, stripped of metadata references. This is used for embedding search."
3327
- },
3328
- project_filter: {
3329
- type: ["string", "null"],
3330
- description: "Project name if the query references a specific project (e.g., 'exe-os', 'exe-create'). Null if no project specified."
3331
- },
3332
- role_filter: {
3333
- type: ["string", "null"],
3334
- description: "Agent role if the query targets a specific role (e.g., 'CTO', 'CMO'). Null if no role specified."
3335
- },
3336
- time_filter: {
3337
- type: ["string", "null"],
3338
- description: "ISO 8601 timestamp lower bound if the query references a time period (e.g., 'last week', 'yesterday'). Null if no time reference."
3339
- },
3340
- is_broad_query: {
3341
- type: "boolean",
3342
- description: "True if the query is exploratory/broad (e.g., 'what has the CTO been working on', 'summarize recent activity'). False if targeted (e.g., 'how did we fix the auth bug')."
3343
- }
3344
- },
3345
- required: ["semantic_query", "project_filter", "role_filter", "time_filter", "is_broad_query"]
3346
- }
3347
- };
3348
- }
3349
- });
3350
-
3351
- // src/lib/reranker.ts
3352
- var reranker_exports = {};
3353
- __export(reranker_exports, {
3354
- disposeReranker: () => disposeReranker,
3355
- getRerankerModelPath: () => getRerankerModelPath,
3356
- isRerankerAvailable: () => isRerankerAvailable,
3357
- rerank: () => rerank,
3358
- rerankWithContext: () => rerankWithContext,
3359
- rerankWithScores: () => rerankWithScores
3360
- });
3361
- import path6 from "path";
3362
- import { existsSync as existsSync6 } from "fs";
3363
- function resetIdleTimer() {
3364
- if (_idleTimer) clearTimeout(_idleTimer);
3365
- _idleTimer = setTimeout(() => {
3366
- void disposeReranker();
3367
- }, IDLE_TIMEOUT_MS);
3368
- if (_idleTimer && typeof _idleTimer === "object" && "unref" in _idleTimer) {
3369
- _idleTimer.unref();
3370
- }
3371
- }
3372
- function isRerankerAvailable() {
3373
- return existsSync6(path6.join(MODELS_DIR, RERANKER_MODEL_FILE));
3374
- }
3375
- function getRerankerModelPath() {
3376
- return path6.join(MODELS_DIR, RERANKER_MODEL_FILE);
3377
- }
3378
- async function ensureLoaded() {
3379
- if (_rerankerContext) {
3380
- resetIdleTimer();
3381
- return;
3382
- }
3383
- const modelPath = path6.join(MODELS_DIR, RERANKER_MODEL_FILE);
3384
- if (!existsSync6(modelPath)) {
3385
- throw new Error(
3386
- `Reranker model not found at ${modelPath}. Run /exe-setup to download it.`
3387
- );
3388
- }
3389
- process.stderr.write("[reranker] Loading Jina Reranker v3...\n");
3390
- const { getLlama } = await import("node-llama-cpp");
3391
- const llama = await getLlama();
3392
- _rerankerModel = await llama.loadModel({ modelPath });
3393
- _rerankerContext = await _rerankerModel.createEmbeddingContext();
3394
- process.stderr.write("[reranker] Jina Reranker v3 loaded.\n");
3395
- resetIdleTimer();
3396
- }
3397
- async function disposeReranker() {
3398
- if (_idleTimer) {
3399
- clearTimeout(_idleTimer);
3400
- _idleTimer = null;
3401
- }
3402
- if (_rerankerContext) {
3403
- try {
3404
- await _rerankerContext.dispose();
3405
- } catch {
3406
- }
3407
- _rerankerContext = null;
3408
- }
3409
- if (_rerankerModel) {
3410
- try {
3411
- await _rerankerModel.dispose();
3412
- } catch {
3413
- }
3414
- _rerankerModel = null;
3415
- }
3416
- process.stderr.write("[reranker] Unloaded (idle timeout).\n");
3417
- }
3418
- async function rerankWithScores(query, texts, topK) {
3419
- if (texts.length === 0) return [];
3420
- await ensureLoaded();
3421
- const ctx = _rerankerContext;
3422
- const scored = [];
3423
- for (let i = 0; i < texts.length; i++) {
3424
- const text = texts[i] ?? "";
3425
- try {
3426
- const input = `query: ${query} document: ${text.slice(0, 512)}`;
3427
- const embedding = await ctx.getEmbeddingFor(input);
3428
- const score = embedding.vector[0] ?? 0;
3429
- scored.push({ text, score, index: i });
3430
- } catch {
3431
- scored.push({ text, score: -1, index: i });
3432
- }
3433
- }
3434
- scored.sort((a, b) => b.score - a.score);
3435
- return typeof topK === "number" ? scored.slice(0, topK) : scored;
3436
- }
3437
- async function rerank(query, candidates, topK = 5) {
3438
- if (candidates.length === 0) return [];
3439
- if (candidates.length <= topK) return candidates;
3440
- const scored = await rerankWithScores(
3441
- query,
3442
- candidates.map((c) => c.raw_text),
3443
- topK
3444
- );
3445
- return scored.map((s) => candidates[s.index]);
3446
- }
3447
- async function rerankWithContext(query, candidates, topK) {
3448
- if (candidates.length === 0) return [];
3449
- await ensureLoaded();
3450
- const ctx = _rerankerContext;
3451
- const scored = [];
3452
- for (let i = 0; i < candidates.length; i++) {
3453
- const candidate = candidates[i];
3454
- try {
3455
- const docText = candidate.context ? `[${candidate.context}] ${candidate.text.slice(0, 460)}` : candidate.text.slice(0, 512);
3456
- const input = `query: ${query} document: ${docText}`;
3457
- const embedding = await ctx.getEmbeddingFor(input);
3458
- const score = embedding.vector[0] ?? 0;
3459
- scored.push({ text: candidate.text, score, index: i });
3460
- } catch {
3461
- scored.push({ text: candidate.text, score: -1, index: i });
3462
- }
3463
- }
3464
- scored.sort((a, b) => b.score - a.score);
3465
- return typeof topK === "number" ? scored.slice(0, topK) : scored;
3466
- }
3467
- var RERANKER_MODEL_FILE, IDLE_TIMEOUT_MS, _rerankerContext, _rerankerModel, _idleTimer;
3468
- var init_reranker = __esm({
3469
- "src/lib/reranker.ts"() {
3470
- "use strict";
3471
- init_config();
3472
- RERANKER_MODEL_FILE = "jina-reranker-v3-q4_k_m.gguf";
3473
- IDLE_TIMEOUT_MS = 6e4;
3474
- _rerankerContext = null;
3475
- _rerankerModel = null;
3476
- _idleTimer = null;
3477
- }
3478
- });
3479
-
3480
- // src/lib/daemon-auth.ts
3481
- import crypto from "crypto";
3482
- import path7 from "path";
3483
- import { existsSync as existsSync7, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
3484
- function normalizeToken(token) {
3485
- if (!token) return null;
3486
- const trimmed = token.trim();
3487
- return trimmed.length > 0 ? trimmed : null;
3488
- }
3489
- function readDaemonToken() {
3490
- try {
3491
- if (!existsSync7(DAEMON_TOKEN_PATH)) return null;
3492
- return normalizeToken(readFileSync3(DAEMON_TOKEN_PATH, "utf8"));
3493
- } catch {
3494
- return null;
3495
- }
3496
- }
3497
- function ensureDaemonToken(seed) {
3498
- const existing = readDaemonToken();
3499
- if (existing) return existing;
3500
- const token = normalizeToken(seed) ?? crypto.randomBytes(32).toString("hex");
3501
- ensurePrivateDirSync(EXE_AI_DIR);
3502
- writeFileSync2(DAEMON_TOKEN_PATH, `${token}
3503
- `, "utf8");
3504
- enforcePrivateFileSync(DAEMON_TOKEN_PATH);
3505
- return token;
3506
- }
3507
- var DAEMON_TOKEN_PATH;
3508
- var init_daemon_auth = __esm({
3509
- "src/lib/daemon-auth.ts"() {
3510
- "use strict";
3511
- init_config();
3512
- init_secure_files();
3513
- DAEMON_TOKEN_PATH = path7.join(EXE_AI_DIR, "exed.token");
3514
- }
3515
- });
3516
-
3517
- // src/lib/exe-daemon-client.ts
3518
- import net from "net";
3519
- import os5 from "os";
3520
- import { spawn } from "child_process";
3521
- import { randomUUID as randomUUID2 } from "crypto";
3522
- import { existsSync as existsSync8, unlinkSync as unlinkSync2, readFileSync as readFileSync4, openSync, closeSync, statSync } from "fs";
3523
- import path8 from "path";
3524
- import { fileURLToPath } from "url";
3525
- function handleData(chunk) {
3526
- _buffer += chunk.toString();
3527
- if (_buffer.length > MAX_BUFFER) {
3528
- _buffer = "";
3529
- return;
3530
- }
3531
- let newlineIdx;
3532
- while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
3533
- const line = _buffer.slice(0, newlineIdx).trim();
3534
- _buffer = _buffer.slice(newlineIdx + 1);
3535
- if (!line) continue;
3761
+ sourcePath,
3762
+ sourceType,
3763
+ tier,
3764
+ supersedesId,
3765
+ draft,
3766
+ memoryType,
3767
+ trajectory,
3768
+ contentHash
3769
+ ];
3770
+ return {
3771
+ sql: hasVector ? `INSERT OR IGNORE INTO memories (${cols})
3772
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, vector32(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` : `INSERT OR IGNORE INTO memories (${cols})
3773
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
3774
+ args: hasVector ? [...baseArgs, vectorToBlob(row.vector), ...sharedArgs, ...metaArgs] : [...baseArgs, ...sharedArgs, ...metaArgs]
3775
+ };
3776
+ };
3777
+ const globalClient = getClient();
3778
+ const globalStmts = batch.map(buildStmt);
3779
+ await globalClient.batch(globalStmts, "write");
3780
+ _pendingRecords.splice(0, batch.length);
3536
3781
  try {
3537
- const response = JSON.parse(line);
3538
- const id = response.id;
3539
- if (!id) continue;
3540
- const entry = _pending.get(id);
3541
- if (entry) {
3542
- clearTimeout(entry.timer);
3543
- _pending.delete(id);
3544
- entry.resolve(response);
3782
+ const { isShardingEnabled: isShardingEnabled2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
3783
+ if (isShardingEnabled2()) {
3784
+ const byProject = /* @__PURE__ */ new Map();
3785
+ for (const row of batch) {
3786
+ const proj = row.project_name || "unknown";
3787
+ if (!byProject.has(proj)) byProject.set(proj, []);
3788
+ byProject.get(proj).push(row);
3789
+ }
3790
+ for (const [project, rows] of byProject) {
3791
+ try {
3792
+ const shardClient = await getReadyShardClient2(project);
3793
+ const shardStmts = rows.map(buildStmt);
3794
+ await shardClient.batch(shardStmts, "write");
3795
+ } catch (err) {
3796
+ process.stderr.write(
3797
+ `[store] Shard write failed for ${project}: ${err instanceof Error ? err.message : String(err)}
3798
+ `
3799
+ );
3800
+ }
3801
+ }
3545
3802
  }
3546
3803
  } catch {
3547
3804
  }
3805
+ return batch.length;
3806
+ } finally {
3807
+ _flushing = false;
3548
3808
  }
3549
3809
  }
3550
- function cleanupStaleFiles() {
3551
- if (existsSync8(PID_PATH)) {
3552
- try {
3553
- const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
3554
- if (pid > 0) {
3555
- try {
3556
- process.kill(pid, 0);
3557
- return;
3558
- } catch {
3559
- }
3560
- }
3561
- } catch {
3562
- }
3563
- try {
3564
- unlinkSync2(PID_PATH);
3565
- } catch {
3566
- }
3567
- try {
3568
- unlinkSync2(SOCKET_PATH);
3569
- } catch {
3570
- }
3810
+ function buildWikiScopeFilter(options, columnPrefix) {
3811
+ const args = [];
3812
+ let clause = "";
3813
+ if (options?.workspaceId !== void 0) {
3814
+ clause += ` AND ${columnPrefix}workspace_id = ?`;
3815
+ args.push(options.workspaceId);
3816
+ }
3817
+ if (options?.userId === void 0) {
3818
+ clause += ` AND ${columnPrefix}user_id IS NULL`;
3819
+ } else if (options.userId === null) {
3820
+ clause += ` AND ${columnPrefix}user_id IS NULL`;
3821
+ } else {
3822
+ clause += ` AND (${columnPrefix}user_id = ? OR ${columnPrefix}user_id IS NULL)`;
3823
+ args.push(options.userId);
3571
3824
  }
3825
+ return { clause, args };
3572
3826
  }
3573
- function findPackageRoot() {
3574
- let dir = path8.dirname(fileURLToPath(import.meta.url));
3575
- const { root } = path8.parse(dir);
3576
- while (dir !== root) {
3577
- if (existsSync8(path8.join(dir, "package.json"))) return dir;
3578
- dir = path8.dirname(dir);
3827
+ function buildRawVisibilityFilter(options, columnPrefix) {
3828
+ if (options?.includeRaw === false) {
3829
+ return {
3830
+ clause: ` AND COALESCE(${columnPrefix}memory_type, 'raw') != 'raw'`,
3831
+ args: []
3832
+ };
3579
3833
  }
3580
- return null;
3834
+ return { clause: "", args: [] };
3581
3835
  }
3582
- function getAvailableMemoryGB() {
3583
- if (process.platform === "darwin") {
3584
- try {
3585
- const { execSync: execSync4 } = __require("child_process");
3586
- const vmstat = execSync4("vm_stat", { encoding: "utf8" });
3587
- const pageSize = 16384;
3588
- const pageSizeMatch = vmstat.match(/page size of (\d+) bytes/);
3589
- const actualPageSize = pageSizeMatch ? parseInt(pageSizeMatch[1], 10) : pageSize;
3590
- const free = vmstat.match(/Pages free:\s+(\d+)/);
3591
- const inactive = vmstat.match(/Pages inactive:\s+(\d+)/);
3592
- const speculative = vmstat.match(/Pages speculative:\s+(\d+)/);
3593
- const freePages = free ? parseInt(free[1], 10) : 0;
3594
- const inactivePages = inactive ? parseInt(inactive[1], 10) : 0;
3595
- const speculativePages = speculative ? parseInt(speculative[1], 10) : 0;
3596
- return (freePages + inactivePages + speculativePages) * actualPageSize / (1024 * 1024 * 1024);
3597
- } catch {
3598
- return os5.freemem() / (1024 * 1024 * 1024);
3836
+ async function searchMemories(queryVector, agentId, options) {
3837
+ let client;
3838
+ try {
3839
+ const { isShardingEnabled: isShardingEnabled2, shardExists: shardExists2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
3840
+ if (isShardingEnabled2() && options?.projectName && shardExists2(options.projectName)) {
3841
+ client = await getReadyShardClient2(options.projectName);
3842
+ } else {
3843
+ client = getClient();
3599
3844
  }
3845
+ } catch {
3846
+ client = getClient();
3600
3847
  }
3601
- return os5.freemem() / (1024 * 1024 * 1024);
3602
- }
3603
- function spawnDaemon() {
3604
- const freeGB = getAvailableMemoryGB();
3605
- const totalGB = os5.totalmem() / (1024 * 1024 * 1024);
3606
- if (totalGB <= 8) {
3607
- process.stderr.write(
3608
- `[exed-client] SKIP: ${totalGB.toFixed(0)}GB system \u2014 embedding daemon disabled. Using keyword search only. Minimum 16GB recommended for vector search.
3609
- `
3610
- );
3611
- return;
3848
+ const limit = options?.limit ?? 10;
3849
+ const statusFilter = options?.includeArchived ? "" : `
3850
+ AND COALESCE(status, 'active') = 'active'`;
3851
+ const draftFilter = options?.includeDrafts ? "" : `
3852
+ AND (draft = 0 OR draft IS NULL)`;
3853
+ let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
3854
+ tool_name, project_name,
3855
+ has_error, raw_text, vector, importance, status,
3856
+ confidence, last_accessed,
3857
+ workspace_id, document_id, user_id,
3858
+ char_offset, page_number,
3859
+ source_path, source_type
3860
+ FROM memories
3861
+ WHERE agent_id = ?
3862
+ AND vector IS NOT NULL${statusFilter}${draftFilter}
3863
+ AND COALESCE(confidence, 0.7) >= 0.3`;
3864
+ const args = [agentId];
3865
+ const scope = buildWikiScopeFilter(options, "");
3866
+ sql += scope.clause;
3867
+ args.push(...scope.args);
3868
+ const rawVisibility = buildRawVisibilityFilter(options, "");
3869
+ sql += rawVisibility.clause;
3870
+ args.push(...rawVisibility.args);
3871
+ if (options?.projectName) {
3872
+ sql += ` AND project_name = ?`;
3873
+ args.push(options.projectName);
3612
3874
  }
3613
- if (totalGB <= 16 && freeGB < 2) {
3614
- process.stderr.write(
3615
- `[exed-client] SKIP: low memory (${freeGB.toFixed(1)}GB available / ${totalGB.toFixed(0)}GB total). Embedding daemon not started \u2014 using keyword search only.
3616
- `
3617
- );
3618
- return;
3875
+ if (options?.toolName) {
3876
+ sql += ` AND tool_name = ?`;
3877
+ args.push(options.toolName);
3619
3878
  }
3620
- const pkgRoot = findPackageRoot();
3621
- if (!pkgRoot) {
3622
- process.stderr.write("[exed-client] WARN: cannot find package root\n");
3623
- return;
3879
+ if (options?.hasError !== void 0) {
3880
+ sql += ` AND has_error = ?`;
3881
+ args.push(options.hasError ? 1 : 0);
3624
3882
  }
3625
- const daemonPath = path8.join(pkgRoot, "dist", "lib", "exe-daemon.js");
3626
- if (!existsSync8(daemonPath)) {
3627
- process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
3628
- `);
3629
- return;
3883
+ if (options?.since) {
3884
+ sql += ` AND timestamp >= ?`;
3885
+ args.push(options.since);
3630
3886
  }
3631
- const resolvedPath = daemonPath;
3632
- const daemonToken = ensureDaemonToken(process.env[DAEMON_TOKEN_ENV] ?? null);
3633
- process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
3634
- `);
3635
- const logPath = path8.join(path8.dirname(SOCKET_PATH), "exed.log");
3636
- let stderrFd = "ignore";
3637
- try {
3638
- stderrFd = openSync(logPath, "a");
3639
- } catch {
3887
+ if (options?.memoryType) {
3888
+ sql += ` AND memory_type = ?`;
3889
+ args.push(options.memoryType);
3640
3890
  }
3641
- const heapCapMB = totalGB <= 8 ? 256 : 512;
3642
- const nodeArgs = [`--max-old-space-size=${heapCapMB}`, resolvedPath];
3643
- const child = spawn(process.execPath, nodeArgs, {
3644
- detached: true,
3645
- stdio: ["ignore", "ignore", stderrFd],
3646
- env: {
3647
- ...process.env,
3648
- TMUX: void 0,
3649
- // Daemon is global — must not inherit session scope
3650
- TMUX_PANE: void 0,
3651
- // Prevents resolveExeSession() from scoping to one session
3652
- EXE_DAEMON_SOCK: SOCKET_PATH,
3653
- EXE_DAEMON_PID: PID_PATH,
3654
- [DAEMON_TOKEN_ENV]: daemonToken
3891
+ sql += ` ORDER BY vector_distance_cos(vector, vector32(?))`;
3892
+ args.push(vectorToBlob(queryVector));
3893
+ sql += ` LIMIT ?`;
3894
+ args.push(limit);
3895
+ const result = await client.execute({ sql, args });
3896
+ return result.rows.map((row) => ({
3897
+ id: row.id,
3898
+ agent_id: row.agent_id,
3899
+ agent_role: row.agent_role,
3900
+ session_id: row.session_id,
3901
+ timestamp: row.timestamp,
3902
+ tool_name: row.tool_name,
3903
+ project_name: row.project_name,
3904
+ has_error: row.has_error === 1,
3905
+ raw_text: row.raw_text,
3906
+ vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
3907
+ importance: row.importance ?? 5,
3908
+ status: row.status ?? "active",
3909
+ confidence: row.confidence ?? 0.7,
3910
+ last_accessed: row.last_accessed ?? row.timestamp,
3911
+ workspace_id: row.workspace_id ?? null,
3912
+ document_id: row.document_id ?? null,
3913
+ user_id: row.user_id ?? null,
3914
+ char_offset: row.char_offset ?? null,
3915
+ page_number: row.page_number ?? null,
3916
+ source_path: row.source_path ?? null,
3917
+ source_type: row.source_type ?? null
3918
+ }));
3919
+ }
3920
+ async function attachDocumentMetadata(records) {
3921
+ const docIds = [
3922
+ ...new Set(
3923
+ records.map((r) => r.document_id).filter((id) => typeof id === "string" && id.length > 0)
3924
+ )
3925
+ ];
3926
+ if (docIds.length === 0) return records;
3927
+ try {
3928
+ const client = getClient();
3929
+ const placeholders = docIds.map(() => "?").join(",");
3930
+ const result = await client.execute({
3931
+ sql: `SELECT id, filename, mime, source_type, uploaded_at
3932
+ FROM documents
3933
+ WHERE id IN (${placeholders})`,
3934
+ args: docIds
3935
+ });
3936
+ const byId = /* @__PURE__ */ new Map();
3937
+ for (const row of result.rows) {
3938
+ const id = row.id;
3939
+ byId.set(id, {
3940
+ document_id: id,
3941
+ filename: row.filename,
3942
+ mime: row.mime ?? null,
3943
+ source_type: row.source_type ?? null,
3944
+ uploaded_at: row.uploaded_at
3945
+ });
3655
3946
  }
3656
- });
3657
- child.unref();
3658
- if (typeof stderrFd === "number") {
3659
- try {
3660
- closeSync(stderrFd);
3661
- } catch {
3947
+ for (const record of records) {
3948
+ if (!record.document_id) continue;
3949
+ record.document_metadata = byId.get(record.document_id) ?? null;
3662
3950
  }
3951
+ } catch {
3663
3952
  }
3953
+ return records;
3664
3954
  }
3665
- function acquireSpawnLock() {
3666
- try {
3667
- const fd = openSync(SPAWN_LOCK_PATH, "wx");
3668
- closeSync(fd);
3669
- return true;
3670
- } catch {
3671
- try {
3672
- const stat = statSync(SPAWN_LOCK_PATH);
3673
- if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
3674
- try {
3675
- unlinkSync2(SPAWN_LOCK_PATH);
3676
- } catch {
3677
- }
3678
- try {
3679
- const fd = openSync(SPAWN_LOCK_PATH, "wx");
3680
- closeSync(fd);
3681
- return true;
3682
- } catch {
3683
- }
3684
- }
3685
- } catch {
3686
- }
3687
- return false;
3955
+ async function flushTier3(agentId, options) {
3956
+ const client = getClient();
3957
+ const maxAge = options?.maxAgeHours ?? 72;
3958
+ const cutoff = new Date(Date.now() - maxAge * 36e5).toISOString();
3959
+ if (options?.dryRun) {
3960
+ const result2 = await client.execute({
3961
+ sql: `SELECT COUNT(*) as cnt FROM memories
3962
+ WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
3963
+ args: [agentId, cutoff]
3964
+ });
3965
+ return { archived: Number(result2.rows[0]?.cnt ?? 0) };
3688
3966
  }
3967
+ const result = await client.execute({
3968
+ sql: `UPDATE memories SET status = 'archived'
3969
+ WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
3970
+ args: [agentId, cutoff]
3971
+ });
3972
+ return { archived: result.rowsAffected };
3689
3973
  }
3690
- function releaseSpawnLock() {
3974
+ async function disposeStore() {
3975
+ if (_flushTimer !== null) {
3976
+ clearInterval(_flushTimer);
3977
+ _flushTimer = null;
3978
+ }
3979
+ if (_pendingRecords.length > 0) {
3980
+ await flushBatch();
3981
+ }
3982
+ await disposeTurso();
3983
+ _pendingRecords = [];
3984
+ _nextVersion = 1;
3985
+ }
3986
+ function vectorToBlob(vector) {
3987
+ const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
3988
+ return JSON.stringify(Array.from(f32));
3989
+ }
3990
+ async function updateMemoryStatus(id, status) {
3991
+ const client = getClient();
3992
+ await client.execute({
3993
+ sql: `UPDATE memories SET status = ? WHERE id = ?`,
3994
+ args: [status, id]
3995
+ });
3996
+ }
3997
+ function reserveVersions(count) {
3998
+ const reserved = [];
3999
+ for (let i = 0; i < count; i++) {
4000
+ reserved.push(_nextVersion++);
4001
+ }
4002
+ return reserved;
4003
+ }
4004
+ async function getMemoryCardinality(agentId) {
3691
4005
  try {
3692
- unlinkSync2(SPAWN_LOCK_PATH);
4006
+ const client = getClient();
4007
+ const result = await client.execute({
4008
+ sql: `SELECT COUNT(*) as cnt FROM memories WHERE agent_id = ? AND COALESCE(status, 'active') = 'active'`,
4009
+ args: [agentId]
4010
+ });
4011
+ return Number(result.rows[0]?.cnt) || 0;
3693
4012
  } catch {
4013
+ return 0;
3694
4014
  }
3695
4015
  }
3696
- function connectToSocket() {
3697
- return new Promise((resolve) => {
3698
- if (_socket && _connected) {
3699
- resolve(true);
3700
- return;
3701
- }
3702
- const socket = net.createConnection({ path: SOCKET_PATH });
3703
- const connectTimeout = setTimeout(() => {
3704
- socket.destroy();
3705
- resolve(false);
3706
- }, 2e3);
3707
- socket.on("connect", () => {
3708
- clearTimeout(connectTimeout);
3709
- _socket = socket;
3710
- _connected = true;
3711
- _buffer = "";
3712
- socket.on("data", handleData);
3713
- socket.on("close", () => {
3714
- _connected = false;
3715
- _socket = null;
3716
- for (const [id, entry] of _pending) {
3717
- clearTimeout(entry.timer);
3718
- _pending.delete(id);
3719
- entry.resolve({ error: "Connection closed" });
3720
- }
3721
- });
3722
- socket.on("error", () => {
3723
- _connected = false;
3724
- _socket = null;
3725
- });
3726
- resolve(true);
3727
- });
3728
- socket.on("error", () => {
3729
- clearTimeout(connectTimeout);
3730
- resolve(false);
4016
+ var INIT_MAX_RETRIES, INIT_RETRY_DELAY_MS, _pendingRecords, _batchSize, _flushIntervalMs, _flushTimer, _flushing, _nextVersion;
4017
+ var init_store = __esm({
4018
+ "src/lib/store.ts"() {
4019
+ "use strict";
4020
+ init_memory();
4021
+ init_database();
4022
+ init_keychain();
4023
+ init_config();
4024
+ init_state_bus();
4025
+ INIT_MAX_RETRIES = 3;
4026
+ INIT_RETRY_DELAY_MS = 1e3;
4027
+ _pendingRecords = [];
4028
+ _batchSize = 20;
4029
+ _flushIntervalMs = 1e4;
4030
+ _flushTimer = null;
4031
+ _flushing = false;
4032
+ _nextVersion = 1;
4033
+ }
4034
+ });
4035
+
4036
+ // src/lib/self-query-router.ts
4037
+ var self_query_router_exports = {};
4038
+ __export(self_query_router_exports, {
4039
+ routeQuery: () => routeQuery
4040
+ });
4041
+ async function routeQuery(query, model = "claude-haiku-4-5-20251001") {
4042
+ if (query.length < 10) {
4043
+ return {
4044
+ semanticQuery: query,
4045
+ projectFilter: null,
4046
+ roleFilter: null,
4047
+ timeFilter: null,
4048
+ isBroadQuery: false
4049
+ };
4050
+ }
4051
+ try {
4052
+ const Anthropic = (await import("@anthropic-ai/sdk")).default;
4053
+ const client = new Anthropic();
4054
+ const now = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
4055
+ const response = await client.messages.create({
4056
+ model,
4057
+ max_tokens: 256,
4058
+ system: `You are a search query router. Extract metadata filters from the user's memory search query. Today is ${now}. Convert relative time references (yesterday, last week) to ISO timestamps.`,
4059
+ messages: [{ role: "user", content: query }],
4060
+ tools: [EXTRACT_TOOL],
4061
+ tool_choice: { type: "tool", name: "extract_search_filters" }
3731
4062
  });
3732
- });
3733
- }
3734
- async function connectEmbedDaemon() {
3735
- if (_socket && _connected) return true;
3736
- if (await connectToSocket()) return true;
3737
- if (acquireSpawnLock()) {
3738
- try {
3739
- cleanupStaleFiles();
3740
- spawnDaemon();
3741
- } finally {
3742
- releaseSpawnLock();
4063
+ const toolBlock = response.content.find((b) => b.type === "tool_use");
4064
+ if (toolBlock && toolBlock.type === "tool_use") {
4065
+ const input = toolBlock.input;
4066
+ return {
4067
+ semanticQuery: input.semantic_query || query,
4068
+ projectFilter: input.project_filter || null,
4069
+ roleFilter: input.role_filter || null,
4070
+ timeFilter: input.time_filter || null,
4071
+ isBroadQuery: Boolean(input.is_broad_query)
4072
+ };
3743
4073
  }
4074
+ } catch (err) {
4075
+ process.stderr.write(
4076
+ `[self-query-router] LLM extraction failed, using passthrough: ${err instanceof Error ? err.message : String(err)}
4077
+ `
4078
+ );
3744
4079
  }
3745
- const start = Date.now();
3746
- let delay2 = 100;
3747
- while (Date.now() - start < CONNECT_TIMEOUT_MS) {
3748
- await new Promise((r) => setTimeout(r, delay2));
3749
- if (await connectToSocket()) return true;
3750
- delay2 = Math.min(delay2 * 2, 3e3);
4080
+ return {
4081
+ semanticQuery: query,
4082
+ projectFilter: null,
4083
+ roleFilter: null,
4084
+ timeFilter: null,
4085
+ isBroadQuery: false
4086
+ };
4087
+ }
4088
+ var EXTRACT_TOOL;
4089
+ var init_self_query_router = __esm({
4090
+ "src/lib/self-query-router.ts"() {
4091
+ "use strict";
4092
+ EXTRACT_TOOL = {
4093
+ name: "extract_search_filters",
4094
+ description: "Extract metadata filters from a memory search query to improve retrieval precision.",
4095
+ input_schema: {
4096
+ type: "object",
4097
+ properties: {
4098
+ semantic_query: {
4099
+ type: "string",
4100
+ description: "The core semantic meaning of the query, stripped of metadata references. This is used for embedding search."
4101
+ },
4102
+ project_filter: {
4103
+ type: ["string", "null"],
4104
+ description: "Project name if the query references a specific project (e.g., 'exe-os', 'exe-create'). Null if no project specified."
4105
+ },
4106
+ role_filter: {
4107
+ type: ["string", "null"],
4108
+ description: "Agent role if the query targets a specific role (e.g., 'CTO', 'CMO'). Null if no role specified."
4109
+ },
4110
+ time_filter: {
4111
+ type: ["string", "null"],
4112
+ description: "ISO 8601 timestamp lower bound if the query references a time period (e.g., 'last week', 'yesterday'). Null if no time reference."
4113
+ },
4114
+ is_broad_query: {
4115
+ type: "boolean",
4116
+ description: "True if the query is exploratory/broad (e.g., 'what has the CTO been working on', 'summarize recent activity'). False if targeted (e.g., 'how did we fix the auth bug')."
4117
+ }
4118
+ },
4119
+ required: ["semantic_query", "project_filter", "role_filter", "time_filter", "is_broad_query"]
4120
+ }
4121
+ };
4122
+ }
4123
+ });
4124
+
4125
+ // src/lib/reranker.ts
4126
+ var reranker_exports = {};
4127
+ __export(reranker_exports, {
4128
+ disposeReranker: () => disposeReranker,
4129
+ getRerankerModelPath: () => getRerankerModelPath,
4130
+ isRerankerAvailable: () => isRerankerAvailable,
4131
+ rerank: () => rerank,
4132
+ rerankWithContext: () => rerankWithContext,
4133
+ rerankWithScores: () => rerankWithScores
4134
+ });
4135
+ import path8 from "path";
4136
+ import { existsSync as existsSync8 } from "fs";
4137
+ function resetIdleTimer() {
4138
+ if (_idleTimer) clearTimeout(_idleTimer);
4139
+ _idleTimer = setTimeout(() => {
4140
+ void disposeReranker();
4141
+ }, IDLE_TIMEOUT_MS);
4142
+ if (_idleTimer && typeof _idleTimer === "object" && "unref" in _idleTimer) {
4143
+ _idleTimer.unref();
3751
4144
  }
3752
- return false;
3753
4145
  }
3754
- function sendRequest(texts, priority) {
3755
- return sendDaemonRequest({ texts, priority });
4146
+ function isRerankerAvailable() {
4147
+ return existsSync8(path8.join(MODELS_DIR, RERANKER_MODEL_FILE));
3756
4148
  }
3757
- function sendDaemonRequest(payload, timeoutMs = REQUEST_TIMEOUT_MS) {
3758
- return new Promise((resolve) => {
3759
- if (!_socket || !_connected) {
3760
- resolve({ error: "Not connected" });
3761
- return;
3762
- }
3763
- const id = randomUUID2();
3764
- const token = process.env[DAEMON_TOKEN_ENV] ?? readDaemonToken();
3765
- const timer = setTimeout(() => {
3766
- _pending.delete(id);
3767
- resolve({ error: "Request timeout" });
3768
- }, timeoutMs);
3769
- _pending.set(id, { resolve, timer });
3770
- try {
3771
- _socket.write(JSON.stringify({ id, token, ...payload }) + "\n");
3772
- } catch {
3773
- clearTimeout(timer);
3774
- _pending.delete(id);
3775
- resolve({ error: "Write failed" });
3776
- }
3777
- });
4149
+ function getRerankerModelPath() {
4150
+ return path8.join(MODELS_DIR, RERANKER_MODEL_FILE);
3778
4151
  }
3779
- async function pingDaemon() {
3780
- if (!_socket || !_connected) return null;
3781
- const response = await sendDaemonRequest({ type: "health" }, 5e3);
3782
- if (response.health) {
3783
- return response.health;
4152
+ async function ensureLoaded() {
4153
+ if (_rerankerContext) {
4154
+ resetIdleTimer();
4155
+ return;
3784
4156
  }
3785
- return null;
4157
+ const modelPath = path8.join(MODELS_DIR, RERANKER_MODEL_FILE);
4158
+ if (!existsSync8(modelPath)) {
4159
+ throw new Error(
4160
+ `Reranker model not found at ${modelPath}. Run /exe-setup to download it.`
4161
+ );
4162
+ }
4163
+ process.stderr.write("[reranker] Loading Jina Reranker v3...\n");
4164
+ const { getLlama } = await import("node-llama-cpp");
4165
+ const llama = await getLlama();
4166
+ _rerankerModel = await llama.loadModel({ modelPath });
4167
+ _rerankerContext = await _rerankerModel.createEmbeddingContext();
4168
+ process.stderr.write("[reranker] Jina Reranker v3 loaded.\n");
4169
+ resetIdleTimer();
3786
4170
  }
3787
- function killAndRespawnDaemon() {
3788
- if (!acquireSpawnLock()) {
3789
- process.stderr.write("[exed-client] Another process is already restarting daemon \u2014 skipping\n");
3790
- if (_socket) {
3791
- _socket.destroy();
3792
- _socket = null;
3793
- }
3794
- _connected = false;
3795
- _buffer = "";
3796
- return;
4171
+ async function disposeReranker() {
4172
+ if (_idleTimer) {
4173
+ clearTimeout(_idleTimer);
4174
+ _idleTimer = null;
3797
4175
  }
3798
- try {
3799
- process.stderr.write("[exed-client] Killing daemon for restart...\n");
3800
- if (existsSync8(PID_PATH)) {
3801
- try {
3802
- const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
3803
- if (pid > 0) {
3804
- try {
3805
- process.kill(pid, "SIGKILL");
3806
- } catch {
3807
- }
3808
- }
3809
- } catch {
3810
- }
3811
- }
3812
- if (_socket) {
3813
- _socket.destroy();
3814
- _socket = null;
3815
- }
3816
- _connected = false;
3817
- _buffer = "";
4176
+ if (_rerankerContext) {
3818
4177
  try {
3819
- unlinkSync2(PID_PATH);
4178
+ await _rerankerContext.dispose();
3820
4179
  } catch {
3821
4180
  }
4181
+ _rerankerContext = null;
4182
+ }
4183
+ if (_rerankerModel) {
3822
4184
  try {
3823
- unlinkSync2(SOCKET_PATH);
4185
+ await _rerankerModel.dispose();
3824
4186
  } catch {
3825
4187
  }
3826
- spawnDaemon();
3827
- } finally {
3828
- releaseSpawnLock();
3829
- }
3830
- }
3831
- function isDaemonTooYoung() {
3832
- try {
3833
- const stat = statSync(PID_PATH);
3834
- return Date.now() - stat.mtimeMs < MIN_DAEMON_AGE_MS;
3835
- } catch {
3836
- return false;
4188
+ _rerankerModel = null;
3837
4189
  }
4190
+ process.stderr.write("[reranker] Unloaded (idle timeout).\n");
3838
4191
  }
3839
- async function retryThenRestart(doRequest, label) {
3840
- const result = await doRequest();
3841
- if (!result.error) {
3842
- _consecutiveFailures = 0;
3843
- return result;
3844
- }
3845
- _consecutiveFailures++;
3846
- for (let i = 0; i < MAX_RETRIES_BEFORE_RESTART; i++) {
3847
- const delayMs = RETRY_DELAYS_MS[i] ?? 5e3;
3848
- process.stderr.write(`[exed-client] ${label} failed (${result.error}), retry ${i + 1}/${MAX_RETRIES_BEFORE_RESTART} in ${delayMs}ms
3849
- `);
3850
- await new Promise((r) => setTimeout(r, delayMs));
3851
- if (!_connected) {
3852
- if (!await connectToSocket()) continue;
3853
- }
3854
- const retry = await doRequest();
3855
- if (!retry.error) {
3856
- _consecutiveFailures = 0;
3857
- return retry;
4192
+ async function rerankWithScores(query, texts, topK) {
4193
+ if (texts.length === 0) return [];
4194
+ await ensureLoaded();
4195
+ const ctx = _rerankerContext;
4196
+ const scored = [];
4197
+ for (let i = 0; i < texts.length; i++) {
4198
+ const text = texts[i] ?? "";
4199
+ try {
4200
+ const input = `query: ${query} document: ${text.slice(0, 512)}`;
4201
+ const embedding = await ctx.getEmbeddingFor(input);
4202
+ const score = embedding.vector[0] ?? 0;
4203
+ scored.push({ text, score, index: i });
4204
+ } catch {
4205
+ scored.push({ text, score: -1, index: i });
3858
4206
  }
3859
- _consecutiveFailures++;
3860
- }
3861
- if (isDaemonTooYoung()) {
3862
- process.stderr.write(`[exed-client] ${label}: daemon too young (< ${MIN_DAEMON_AGE_MS / 1e3}s) \u2014 skipping restart
3863
- `);
3864
- return { error: result.error };
3865
- }
3866
- process.stderr.write(`[exed-client] ${label}: ${_consecutiveFailures} consecutive failures \u2014 restarting daemon
3867
- `);
3868
- killAndRespawnDaemon();
3869
- const start = Date.now();
3870
- let delay2 = 200;
3871
- while (Date.now() - start < CONNECT_TIMEOUT_MS) {
3872
- await new Promise((r) => setTimeout(r, delay2));
3873
- if (await connectToSocket()) break;
3874
- delay2 = Math.min(delay2 * 2, 3e3);
3875
4207
  }
3876
- if (!_connected) return { error: "Daemon restart failed" };
3877
- const final = await doRequest();
3878
- if (!final.error) _consecutiveFailures = 0;
3879
- return final;
4208
+ scored.sort((a, b) => b.score - a.score);
4209
+ return typeof topK === "number" ? scored.slice(0, topK) : scored;
3880
4210
  }
3881
- async function embedViaClient(text, priority = "high") {
3882
- if (!_connected && !await connectEmbedDaemon()) return null;
3883
- _requestCount++;
3884
- if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
3885
- const health = await pingDaemon();
3886
- if (!health && !isDaemonTooYoung()) {
3887
- process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
3888
- `);
3889
- killAndRespawnDaemon();
3890
- const start = Date.now();
3891
- let d = 200;
3892
- while (Date.now() - start < CONNECT_TIMEOUT_MS) {
3893
- await new Promise((r) => setTimeout(r, d));
3894
- if (await connectToSocket()) break;
3895
- d = Math.min(d * 2, 3e3);
3896
- }
3897
- if (!_connected) return null;
3898
- }
3899
- }
3900
- const result = await retryThenRestart(
3901
- () => sendRequest([text], priority),
3902
- "Embed"
4211
+ async function rerank(query, candidates, topK = 5) {
4212
+ if (candidates.length === 0) return [];
4213
+ if (candidates.length <= topK) return candidates;
4214
+ const scored = await rerankWithScores(
4215
+ query,
4216
+ candidates.map((c) => c.raw_text),
4217
+ topK
3903
4218
  );
3904
- return !result.error && result.vectors?.[0] ? result.vectors[0] : null;
4219
+ return scored.map((s) => candidates[s.index]);
3905
4220
  }
3906
- function disconnectClient() {
3907
- if (_socket) {
3908
- _socket.destroy();
3909
- _socket = null;
3910
- }
3911
- _connected = false;
3912
- _buffer = "";
3913
- for (const [id, entry] of _pending) {
3914
- clearTimeout(entry.timer);
3915
- _pending.delete(id);
3916
- entry.resolve({ error: "Client disconnected" });
4221
+ async function rerankWithContext(query, candidates, topK) {
4222
+ if (candidates.length === 0) return [];
4223
+ await ensureLoaded();
4224
+ const ctx = _rerankerContext;
4225
+ const scored = [];
4226
+ for (let i = 0; i < candidates.length; i++) {
4227
+ const candidate = candidates[i];
4228
+ try {
4229
+ const docText = candidate.context ? `[${candidate.context}] ${candidate.text.slice(0, 460)}` : candidate.text.slice(0, 512);
4230
+ const input = `query: ${query} document: ${docText}`;
4231
+ const embedding = await ctx.getEmbeddingFor(input);
4232
+ const score = embedding.vector[0] ?? 0;
4233
+ scored.push({ text: candidate.text, score, index: i });
4234
+ } catch {
4235
+ scored.push({ text: candidate.text, score: -1, index: i });
4236
+ }
3917
4237
  }
4238
+ scored.sort((a, b) => b.score - a.score);
4239
+ return typeof topK === "number" ? scored.slice(0, topK) : scored;
3918
4240
  }
3919
- 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;
3920
- var init_exe_daemon_client = __esm({
3921
- "src/lib/exe-daemon-client.ts"() {
4241
+ var RERANKER_MODEL_FILE, IDLE_TIMEOUT_MS, _rerankerContext, _rerankerModel, _idleTimer;
4242
+ var init_reranker = __esm({
4243
+ "src/lib/reranker.ts"() {
3922
4244
  "use strict";
3923
4245
  init_config();
3924
- init_daemon_auth();
3925
- SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path8.join(EXE_AI_DIR, "exed.sock");
3926
- PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path8.join(EXE_AI_DIR, "exed.pid");
3927
- SPAWN_LOCK_PATH = path8.join(EXE_AI_DIR, "exed-spawn.lock");
3928
- SPAWN_LOCK_STALE_MS = 3e4;
3929
- CONNECT_TIMEOUT_MS = 15e3;
3930
- REQUEST_TIMEOUT_MS = 3e4;
3931
- DAEMON_TOKEN_ENV = "EXE_DAEMON_TOKEN";
3932
- _socket = null;
3933
- _connected = false;
3934
- _buffer = "";
3935
- _requestCount = 0;
3936
- _consecutiveFailures = 0;
3937
- HEALTH_CHECK_INTERVAL = 100;
3938
- MAX_RETRIES_BEFORE_RESTART = 3;
3939
- RETRY_DELAYS_MS = [1e3, 3e3, 5e3];
3940
- MIN_DAEMON_AGE_MS = 3e4;
3941
- _pending = /* @__PURE__ */ new Map();
3942
- MAX_BUFFER = 1e7;
4246
+ RERANKER_MODEL_FILE = "jina-reranker-v3-q4_k_m.gguf";
4247
+ IDLE_TIMEOUT_MS = 6e4;
4248
+ _rerankerContext = null;
4249
+ _rerankerModel = null;
4250
+ _idleTimer = null;
3943
4251
  }
3944
4252
  });
3945
4253
 
@@ -5134,7 +5442,7 @@ function rrfMergeMulti(lists, limit, k = RRF_K, weights) {
5134
5442
  const importance = normalizedImportance(e.record.importance);
5135
5443
  const confidence = sourceConfidence(e.record);
5136
5444
  const frecency = frecencyBoost(e.record.last_accessed, e.record.timestamp);
5137
- const baseScore = e.rrfScore * 0.35 + recency * 0.14 + importance * 0.21 + confidence * 0.3;
5445
+ const baseScore = e.rrfScore * 0.45 + recency * 0.24 + importance * 0.21 + confidence * 0.1;
5138
5446
  const finalScore = baseScore * (1 + 0.3 * frecency);
5139
5447
  return { score: finalScore, record: e.record };
5140
5448
  });