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