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