@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
package/dist/bin/exe-forget.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
6
|
+
}) : x)(function(x) {
|
|
7
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
8
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
9
|
+
});
|
|
4
10
|
var __esm = (fn, res) => function __init() {
|
|
5
11
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
6
12
|
};
|
|
@@ -9,13 +15,6 @@ var __export = (target, all) => {
|
|
|
9
15
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
16
|
};
|
|
11
17
|
|
|
12
|
-
// src/types/memory.ts
|
|
13
|
-
var init_memory = __esm({
|
|
14
|
-
"src/types/memory.ts"() {
|
|
15
|
-
"use strict";
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
|
|
19
18
|
// src/lib/db-retry.ts
|
|
20
19
|
function isBusyError(err) {
|
|
21
20
|
if (err instanceof Error) {
|
|
@@ -65,9 +64,9 @@ var MAX_RETRIES, BASE_DELAY_MS, MAX_JITTER_MS;
|
|
|
65
64
|
var init_db_retry = __esm({
|
|
66
65
|
"src/lib/db-retry.ts"() {
|
|
67
66
|
"use strict";
|
|
68
|
-
MAX_RETRIES =
|
|
69
|
-
BASE_DELAY_MS =
|
|
70
|
-
MAX_JITTER_MS =
|
|
67
|
+
MAX_RETRIES = 5;
|
|
68
|
+
BASE_DELAY_MS = 250;
|
|
69
|
+
MAX_JITTER_MS = 400;
|
|
71
70
|
}
|
|
72
71
|
});
|
|
73
72
|
|
|
@@ -81,12 +80,25 @@ async function ensurePrivateDir(dirPath) {
|
|
|
81
80
|
} catch {
|
|
82
81
|
}
|
|
83
82
|
}
|
|
83
|
+
function ensurePrivateDirSync(dirPath) {
|
|
84
|
+
mkdirSync(dirPath, { recursive: true, mode: PRIVATE_DIR_MODE });
|
|
85
|
+
try {
|
|
86
|
+
chmodSync(dirPath, PRIVATE_DIR_MODE);
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
}
|
|
84
90
|
async function enforcePrivateFile(filePath) {
|
|
85
91
|
try {
|
|
86
92
|
await chmod(filePath, PRIVATE_FILE_MODE);
|
|
87
93
|
} catch {
|
|
88
94
|
}
|
|
89
95
|
}
|
|
96
|
+
function enforcePrivateFileSync(filePath) {
|
|
97
|
+
try {
|
|
98
|
+
if (existsSync(filePath)) chmodSync(filePath, PRIVATE_FILE_MODE);
|
|
99
|
+
} catch {
|
|
100
|
+
}
|
|
101
|
+
}
|
|
90
102
|
var PRIVATE_DIR_MODE, PRIVATE_FILE_MODE;
|
|
91
103
|
var init_secure_files = __esm({
|
|
92
104
|
"src/lib/secure-files.ts"() {
|
|
@@ -903,7 +915,762 @@ var init_database_adapter = __esm({
|
|
|
903
915
|
}
|
|
904
916
|
});
|
|
905
917
|
|
|
918
|
+
// src/types/memory.ts
|
|
919
|
+
var EMBEDDING_DIM;
|
|
920
|
+
var init_memory = __esm({
|
|
921
|
+
"src/types/memory.ts"() {
|
|
922
|
+
"use strict";
|
|
923
|
+
EMBEDDING_DIM = 1024;
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
// src/lib/daemon-auth.ts
|
|
928
|
+
import crypto from "crypto";
|
|
929
|
+
import path4 from "path";
|
|
930
|
+
import { existsSync as existsSync4, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
931
|
+
function normalizeToken(token) {
|
|
932
|
+
if (!token) return null;
|
|
933
|
+
const trimmed = token.trim();
|
|
934
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
935
|
+
}
|
|
936
|
+
function readDaemonToken() {
|
|
937
|
+
try {
|
|
938
|
+
if (!existsSync4(DAEMON_TOKEN_PATH)) return null;
|
|
939
|
+
return normalizeToken(readFileSync3(DAEMON_TOKEN_PATH, "utf8"));
|
|
940
|
+
} catch {
|
|
941
|
+
return null;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
function ensureDaemonToken(seed) {
|
|
945
|
+
const existing = readDaemonToken();
|
|
946
|
+
if (existing) return existing;
|
|
947
|
+
const token = normalizeToken(seed) ?? crypto.randomBytes(32).toString("hex");
|
|
948
|
+
ensurePrivateDirSync(EXE_AI_DIR);
|
|
949
|
+
writeFileSync2(DAEMON_TOKEN_PATH, `${token}
|
|
950
|
+
`, "utf8");
|
|
951
|
+
enforcePrivateFileSync(DAEMON_TOKEN_PATH);
|
|
952
|
+
return token;
|
|
953
|
+
}
|
|
954
|
+
var DAEMON_TOKEN_PATH;
|
|
955
|
+
var init_daemon_auth = __esm({
|
|
956
|
+
"src/lib/daemon-auth.ts"() {
|
|
957
|
+
"use strict";
|
|
958
|
+
init_config();
|
|
959
|
+
init_secure_files();
|
|
960
|
+
DAEMON_TOKEN_PATH = path4.join(EXE_AI_DIR, "exed.token");
|
|
961
|
+
}
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
// src/lib/exe-daemon-client.ts
|
|
965
|
+
var exe_daemon_client_exports = {};
|
|
966
|
+
__export(exe_daemon_client_exports, {
|
|
967
|
+
connectEmbedDaemon: () => connectEmbedDaemon,
|
|
968
|
+
disconnectClient: () => disconnectClient,
|
|
969
|
+
embedBatchViaClient: () => embedBatchViaClient,
|
|
970
|
+
embedViaClient: () => embedViaClient,
|
|
971
|
+
isClientConnected: () => isClientConnected,
|
|
972
|
+
pingDaemon: () => pingDaemon,
|
|
973
|
+
sendDaemonRequest: () => sendDaemonRequest,
|
|
974
|
+
sendIngestRequest: () => sendIngestRequest
|
|
975
|
+
});
|
|
976
|
+
import net from "net";
|
|
977
|
+
import os4 from "os";
|
|
978
|
+
import { spawn } from "child_process";
|
|
979
|
+
import { randomUUID } from "crypto";
|
|
980
|
+
import { existsSync as existsSync5, unlinkSync as unlinkSync2, readFileSync as readFileSync4, openSync, closeSync, statSync } from "fs";
|
|
981
|
+
import path5 from "path";
|
|
982
|
+
import { fileURLToPath } from "url";
|
|
983
|
+
function handleData(chunk) {
|
|
984
|
+
_buffer += chunk.toString();
|
|
985
|
+
if (_buffer.length > MAX_BUFFER) {
|
|
986
|
+
_buffer = "";
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
let newlineIdx;
|
|
990
|
+
while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
|
|
991
|
+
const line = _buffer.slice(0, newlineIdx).trim();
|
|
992
|
+
_buffer = _buffer.slice(newlineIdx + 1);
|
|
993
|
+
if (!line) continue;
|
|
994
|
+
try {
|
|
995
|
+
const response = JSON.parse(line);
|
|
996
|
+
const id = response.id;
|
|
997
|
+
if (!id) continue;
|
|
998
|
+
const entry = _pending.get(id);
|
|
999
|
+
if (entry) {
|
|
1000
|
+
clearTimeout(entry.timer);
|
|
1001
|
+
_pending.delete(id);
|
|
1002
|
+
entry.resolve(response);
|
|
1003
|
+
}
|
|
1004
|
+
} catch {
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
function cleanupStaleFiles() {
|
|
1009
|
+
if (existsSync5(PID_PATH)) {
|
|
1010
|
+
try {
|
|
1011
|
+
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
1012
|
+
if (pid > 0) {
|
|
1013
|
+
try {
|
|
1014
|
+
process.kill(pid, 0);
|
|
1015
|
+
return;
|
|
1016
|
+
} catch {
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
} catch {
|
|
1020
|
+
}
|
|
1021
|
+
try {
|
|
1022
|
+
unlinkSync2(PID_PATH);
|
|
1023
|
+
} catch {
|
|
1024
|
+
}
|
|
1025
|
+
try {
|
|
1026
|
+
unlinkSync2(SOCKET_PATH);
|
|
1027
|
+
} catch {
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
function findPackageRoot() {
|
|
1032
|
+
let dir = path5.dirname(fileURLToPath(import.meta.url));
|
|
1033
|
+
const { root } = path5.parse(dir);
|
|
1034
|
+
while (dir !== root) {
|
|
1035
|
+
if (existsSync5(path5.join(dir, "package.json"))) return dir;
|
|
1036
|
+
dir = path5.dirname(dir);
|
|
1037
|
+
}
|
|
1038
|
+
return null;
|
|
1039
|
+
}
|
|
1040
|
+
function getAvailableMemoryGB() {
|
|
1041
|
+
if (process.platform === "darwin") {
|
|
1042
|
+
try {
|
|
1043
|
+
const { execSync: execSync2 } = __require("child_process");
|
|
1044
|
+
const vmstat = execSync2("vm_stat", { encoding: "utf8" });
|
|
1045
|
+
const pageSize = 16384;
|
|
1046
|
+
const pageSizeMatch = vmstat.match(/page size of (\d+) bytes/);
|
|
1047
|
+
const actualPageSize = pageSizeMatch ? parseInt(pageSizeMatch[1], 10) : pageSize;
|
|
1048
|
+
const free = vmstat.match(/Pages free:\s+(\d+)/);
|
|
1049
|
+
const inactive = vmstat.match(/Pages inactive:\s+(\d+)/);
|
|
1050
|
+
const speculative = vmstat.match(/Pages speculative:\s+(\d+)/);
|
|
1051
|
+
const freePages = free ? parseInt(free[1], 10) : 0;
|
|
1052
|
+
const inactivePages = inactive ? parseInt(inactive[1], 10) : 0;
|
|
1053
|
+
const speculativePages = speculative ? parseInt(speculative[1], 10) : 0;
|
|
1054
|
+
return (freePages + inactivePages + speculativePages) * actualPageSize / (1024 * 1024 * 1024);
|
|
1055
|
+
} catch {
|
|
1056
|
+
return os4.freemem() / (1024 * 1024 * 1024);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
return os4.freemem() / (1024 * 1024 * 1024);
|
|
1060
|
+
}
|
|
1061
|
+
function spawnDaemon() {
|
|
1062
|
+
const freeGB = getAvailableMemoryGB();
|
|
1063
|
+
const totalGB = os4.totalmem() / (1024 * 1024 * 1024);
|
|
1064
|
+
if (totalGB <= 8) {
|
|
1065
|
+
process.stderr.write(
|
|
1066
|
+
`[exed-client] SKIP: ${totalGB.toFixed(0)}GB system \u2014 embedding daemon disabled. Using keyword search only. Minimum 16GB recommended for vector search.
|
|
1067
|
+
`
|
|
1068
|
+
);
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
if (totalGB <= 16 && freeGB < 2) {
|
|
1072
|
+
process.stderr.write(
|
|
1073
|
+
`[exed-client] SKIP: low memory (${freeGB.toFixed(1)}GB available / ${totalGB.toFixed(0)}GB total). Embedding daemon not started \u2014 using keyword search only.
|
|
1074
|
+
`
|
|
1075
|
+
);
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
const pkgRoot = findPackageRoot();
|
|
1079
|
+
if (!pkgRoot) {
|
|
1080
|
+
process.stderr.write("[exed-client] WARN: cannot find package root\n");
|
|
1081
|
+
return;
|
|
1082
|
+
}
|
|
1083
|
+
const daemonPath = path5.join(pkgRoot, "dist", "lib", "exe-daemon.js");
|
|
1084
|
+
if (!existsSync5(daemonPath)) {
|
|
1085
|
+
process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
|
|
1086
|
+
`);
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
const resolvedPath = daemonPath;
|
|
1090
|
+
const daemonToken = ensureDaemonToken(process.env[DAEMON_TOKEN_ENV] ?? null);
|
|
1091
|
+
process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
|
|
1092
|
+
`);
|
|
1093
|
+
const logPath = path5.join(path5.dirname(SOCKET_PATH), "exed.log");
|
|
1094
|
+
let stderrFd = "ignore";
|
|
1095
|
+
try {
|
|
1096
|
+
stderrFd = openSync(logPath, "a");
|
|
1097
|
+
} catch {
|
|
1098
|
+
}
|
|
1099
|
+
const heapCapMB = totalGB <= 8 ? 256 : 512;
|
|
1100
|
+
const nodeArgs = [`--max-old-space-size=${heapCapMB}`, resolvedPath];
|
|
1101
|
+
const child = spawn(process.execPath, nodeArgs, {
|
|
1102
|
+
detached: true,
|
|
1103
|
+
stdio: ["ignore", "ignore", stderrFd],
|
|
1104
|
+
env: {
|
|
1105
|
+
...process.env,
|
|
1106
|
+
TMUX: void 0,
|
|
1107
|
+
// Daemon is global — must not inherit session scope
|
|
1108
|
+
TMUX_PANE: void 0,
|
|
1109
|
+
// Prevents resolveExeSession() from scoping to one session
|
|
1110
|
+
EXE_DAEMON_SOCK: SOCKET_PATH,
|
|
1111
|
+
EXE_DAEMON_PID: PID_PATH,
|
|
1112
|
+
[DAEMON_TOKEN_ENV]: daemonToken
|
|
1113
|
+
}
|
|
1114
|
+
});
|
|
1115
|
+
child.unref();
|
|
1116
|
+
if (typeof stderrFd === "number") {
|
|
1117
|
+
try {
|
|
1118
|
+
closeSync(stderrFd);
|
|
1119
|
+
} catch {
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
function acquireSpawnLock() {
|
|
1124
|
+
try {
|
|
1125
|
+
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
1126
|
+
closeSync(fd);
|
|
1127
|
+
return true;
|
|
1128
|
+
} catch {
|
|
1129
|
+
try {
|
|
1130
|
+
const stat = statSync(SPAWN_LOCK_PATH);
|
|
1131
|
+
if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
|
|
1132
|
+
try {
|
|
1133
|
+
unlinkSync2(SPAWN_LOCK_PATH);
|
|
1134
|
+
} catch {
|
|
1135
|
+
}
|
|
1136
|
+
try {
|
|
1137
|
+
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
1138
|
+
closeSync(fd);
|
|
1139
|
+
return true;
|
|
1140
|
+
} catch {
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
} catch {
|
|
1144
|
+
}
|
|
1145
|
+
return false;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
function releaseSpawnLock() {
|
|
1149
|
+
try {
|
|
1150
|
+
unlinkSync2(SPAWN_LOCK_PATH);
|
|
1151
|
+
} catch {
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
function connectToSocket() {
|
|
1155
|
+
return new Promise((resolve) => {
|
|
1156
|
+
if (_socket && _connected) {
|
|
1157
|
+
resolve(true);
|
|
1158
|
+
return;
|
|
1159
|
+
}
|
|
1160
|
+
const socket = net.createConnection({ path: SOCKET_PATH });
|
|
1161
|
+
const connectTimeout = setTimeout(() => {
|
|
1162
|
+
socket.destroy();
|
|
1163
|
+
resolve(false);
|
|
1164
|
+
}, 2e3);
|
|
1165
|
+
socket.on("connect", () => {
|
|
1166
|
+
clearTimeout(connectTimeout);
|
|
1167
|
+
_socket = socket;
|
|
1168
|
+
_connected = true;
|
|
1169
|
+
_buffer = "";
|
|
1170
|
+
socket.on("data", handleData);
|
|
1171
|
+
socket.on("close", () => {
|
|
1172
|
+
_connected = false;
|
|
1173
|
+
_socket = null;
|
|
1174
|
+
for (const [id, entry] of _pending) {
|
|
1175
|
+
clearTimeout(entry.timer);
|
|
1176
|
+
_pending.delete(id);
|
|
1177
|
+
entry.resolve({ error: "Connection closed" });
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
socket.on("error", () => {
|
|
1181
|
+
_connected = false;
|
|
1182
|
+
_socket = null;
|
|
1183
|
+
});
|
|
1184
|
+
resolve(true);
|
|
1185
|
+
});
|
|
1186
|
+
socket.on("error", () => {
|
|
1187
|
+
clearTimeout(connectTimeout);
|
|
1188
|
+
resolve(false);
|
|
1189
|
+
});
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
async function connectEmbedDaemon() {
|
|
1193
|
+
if (_socket && _connected) return true;
|
|
1194
|
+
if (await connectToSocket()) return true;
|
|
1195
|
+
if (acquireSpawnLock()) {
|
|
1196
|
+
try {
|
|
1197
|
+
cleanupStaleFiles();
|
|
1198
|
+
spawnDaemon();
|
|
1199
|
+
} finally {
|
|
1200
|
+
releaseSpawnLock();
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
const start = Date.now();
|
|
1204
|
+
let delay2 = 100;
|
|
1205
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1206
|
+
await new Promise((r) => setTimeout(r, delay2));
|
|
1207
|
+
if (await connectToSocket()) return true;
|
|
1208
|
+
delay2 = Math.min(delay2 * 2, 3e3);
|
|
1209
|
+
}
|
|
1210
|
+
return false;
|
|
1211
|
+
}
|
|
1212
|
+
function sendRequest(texts, priority) {
|
|
1213
|
+
return sendDaemonRequest({ texts, priority });
|
|
1214
|
+
}
|
|
1215
|
+
function sendDaemonRequest(payload, timeoutMs = REQUEST_TIMEOUT_MS) {
|
|
1216
|
+
return new Promise((resolve) => {
|
|
1217
|
+
if (!_socket || !_connected) {
|
|
1218
|
+
resolve({ error: "Not connected" });
|
|
1219
|
+
return;
|
|
1220
|
+
}
|
|
1221
|
+
const id = randomUUID();
|
|
1222
|
+
const token = process.env[DAEMON_TOKEN_ENV] ?? readDaemonToken();
|
|
1223
|
+
const timer = setTimeout(() => {
|
|
1224
|
+
_pending.delete(id);
|
|
1225
|
+
resolve({ error: "Request timeout" });
|
|
1226
|
+
}, timeoutMs);
|
|
1227
|
+
_pending.set(id, { resolve, timer });
|
|
1228
|
+
try {
|
|
1229
|
+
_socket.write(JSON.stringify({ id, token, ...payload }) + "\n");
|
|
1230
|
+
} catch {
|
|
1231
|
+
clearTimeout(timer);
|
|
1232
|
+
_pending.delete(id);
|
|
1233
|
+
resolve({ error: "Write failed" });
|
|
1234
|
+
}
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
async function pingDaemon() {
|
|
1238
|
+
if (!_socket || !_connected) return null;
|
|
1239
|
+
const response = await sendDaemonRequest({ type: "health" }, 5e3);
|
|
1240
|
+
if (response.health) {
|
|
1241
|
+
return response.health;
|
|
1242
|
+
}
|
|
1243
|
+
return null;
|
|
1244
|
+
}
|
|
1245
|
+
function killAndRespawnDaemon() {
|
|
1246
|
+
if (!acquireSpawnLock()) {
|
|
1247
|
+
process.stderr.write("[exed-client] Another process is already restarting daemon \u2014 skipping\n");
|
|
1248
|
+
if (_socket) {
|
|
1249
|
+
_socket.destroy();
|
|
1250
|
+
_socket = null;
|
|
1251
|
+
}
|
|
1252
|
+
_connected = false;
|
|
1253
|
+
_buffer = "";
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
try {
|
|
1257
|
+
process.stderr.write("[exed-client] Killing daemon for restart...\n");
|
|
1258
|
+
if (existsSync5(PID_PATH)) {
|
|
1259
|
+
try {
|
|
1260
|
+
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
1261
|
+
if (pid > 0) {
|
|
1262
|
+
try {
|
|
1263
|
+
process.kill(pid, "SIGKILL");
|
|
1264
|
+
} catch {
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
} catch {
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
if (_socket) {
|
|
1271
|
+
_socket.destroy();
|
|
1272
|
+
_socket = null;
|
|
1273
|
+
}
|
|
1274
|
+
_connected = false;
|
|
1275
|
+
_buffer = "";
|
|
1276
|
+
try {
|
|
1277
|
+
unlinkSync2(PID_PATH);
|
|
1278
|
+
} catch {
|
|
1279
|
+
}
|
|
1280
|
+
try {
|
|
1281
|
+
unlinkSync2(SOCKET_PATH);
|
|
1282
|
+
} catch {
|
|
1283
|
+
}
|
|
1284
|
+
spawnDaemon();
|
|
1285
|
+
} finally {
|
|
1286
|
+
releaseSpawnLock();
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
function isDaemonTooYoung() {
|
|
1290
|
+
try {
|
|
1291
|
+
const stat = statSync(PID_PATH);
|
|
1292
|
+
return Date.now() - stat.mtimeMs < MIN_DAEMON_AGE_MS;
|
|
1293
|
+
} catch {
|
|
1294
|
+
return false;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
async function retryThenRestart(doRequest, label) {
|
|
1298
|
+
const result = await doRequest();
|
|
1299
|
+
if (!result.error) {
|
|
1300
|
+
_consecutiveFailures = 0;
|
|
1301
|
+
return result;
|
|
1302
|
+
}
|
|
1303
|
+
_consecutiveFailures++;
|
|
1304
|
+
for (let i = 0; i < MAX_RETRIES_BEFORE_RESTART; i++) {
|
|
1305
|
+
const delayMs = RETRY_DELAYS_MS[i] ?? 5e3;
|
|
1306
|
+
process.stderr.write(`[exed-client] ${label} failed (${result.error}), retry ${i + 1}/${MAX_RETRIES_BEFORE_RESTART} in ${delayMs}ms
|
|
1307
|
+
`);
|
|
1308
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
1309
|
+
if (!_connected) {
|
|
1310
|
+
if (!await connectToSocket()) continue;
|
|
1311
|
+
}
|
|
1312
|
+
const retry = await doRequest();
|
|
1313
|
+
if (!retry.error) {
|
|
1314
|
+
_consecutiveFailures = 0;
|
|
1315
|
+
return retry;
|
|
1316
|
+
}
|
|
1317
|
+
_consecutiveFailures++;
|
|
1318
|
+
}
|
|
1319
|
+
if (isDaemonTooYoung()) {
|
|
1320
|
+
process.stderr.write(`[exed-client] ${label}: daemon too young (< ${MIN_DAEMON_AGE_MS / 1e3}s) \u2014 skipping restart
|
|
1321
|
+
`);
|
|
1322
|
+
return { error: result.error };
|
|
1323
|
+
}
|
|
1324
|
+
process.stderr.write(`[exed-client] ${label}: ${_consecutiveFailures} consecutive failures \u2014 restarting daemon
|
|
1325
|
+
`);
|
|
1326
|
+
killAndRespawnDaemon();
|
|
1327
|
+
const start = Date.now();
|
|
1328
|
+
let delay2 = 200;
|
|
1329
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1330
|
+
await new Promise((r) => setTimeout(r, delay2));
|
|
1331
|
+
if (await connectToSocket()) break;
|
|
1332
|
+
delay2 = Math.min(delay2 * 2, 3e3);
|
|
1333
|
+
}
|
|
1334
|
+
if (!_connected) return { error: "Daemon restart failed" };
|
|
1335
|
+
const final = await doRequest();
|
|
1336
|
+
if (!final.error) _consecutiveFailures = 0;
|
|
1337
|
+
return final;
|
|
1338
|
+
}
|
|
1339
|
+
async function embedViaClient(text, priority = "high") {
|
|
1340
|
+
if (!_connected && !await connectEmbedDaemon()) return null;
|
|
1341
|
+
_requestCount++;
|
|
1342
|
+
if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
|
|
1343
|
+
const health = await pingDaemon();
|
|
1344
|
+
if (!health && !isDaemonTooYoung()) {
|
|
1345
|
+
process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
|
|
1346
|
+
`);
|
|
1347
|
+
killAndRespawnDaemon();
|
|
1348
|
+
const start = Date.now();
|
|
1349
|
+
let d = 200;
|
|
1350
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1351
|
+
await new Promise((r) => setTimeout(r, d));
|
|
1352
|
+
if (await connectToSocket()) break;
|
|
1353
|
+
d = Math.min(d * 2, 3e3);
|
|
1354
|
+
}
|
|
1355
|
+
if (!_connected) return null;
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
const result = await retryThenRestart(
|
|
1359
|
+
() => sendRequest([text], priority),
|
|
1360
|
+
"Embed"
|
|
1361
|
+
);
|
|
1362
|
+
return !result.error && result.vectors?.[0] ? result.vectors[0] : null;
|
|
1363
|
+
}
|
|
1364
|
+
async function embedBatchViaClient(texts, priority = "high") {
|
|
1365
|
+
if (!_connected && !await connectEmbedDaemon()) return null;
|
|
1366
|
+
_requestCount++;
|
|
1367
|
+
const result = await retryThenRestart(
|
|
1368
|
+
() => sendRequest(texts, priority),
|
|
1369
|
+
"Batch embed"
|
|
1370
|
+
);
|
|
1371
|
+
return !result.error && result.vectors ? result.vectors : null;
|
|
1372
|
+
}
|
|
1373
|
+
function disconnectClient() {
|
|
1374
|
+
if (_socket) {
|
|
1375
|
+
_socket.destroy();
|
|
1376
|
+
_socket = null;
|
|
1377
|
+
}
|
|
1378
|
+
_connected = false;
|
|
1379
|
+
_buffer = "";
|
|
1380
|
+
for (const [id, entry] of _pending) {
|
|
1381
|
+
clearTimeout(entry.timer);
|
|
1382
|
+
_pending.delete(id);
|
|
1383
|
+
entry.resolve({ error: "Client disconnected" });
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
function isClientConnected() {
|
|
1387
|
+
return _connected;
|
|
1388
|
+
}
|
|
1389
|
+
function sendIngestRequest(payload) {
|
|
1390
|
+
if (!_socket || !_connected) return false;
|
|
1391
|
+
try {
|
|
1392
|
+
const id = randomUUID();
|
|
1393
|
+
const token = process.env[DAEMON_TOKEN_ENV] ?? readDaemonToken();
|
|
1394
|
+
_socket.write(JSON.stringify({ id, token, type: "ingest", ...payload }) + "\n");
|
|
1395
|
+
return true;
|
|
1396
|
+
} catch {
|
|
1397
|
+
return false;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
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;
|
|
1401
|
+
var init_exe_daemon_client = __esm({
|
|
1402
|
+
"src/lib/exe-daemon-client.ts"() {
|
|
1403
|
+
"use strict";
|
|
1404
|
+
init_config();
|
|
1405
|
+
init_daemon_auth();
|
|
1406
|
+
SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path5.join(EXE_AI_DIR, "exed.sock");
|
|
1407
|
+
PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path5.join(EXE_AI_DIR, "exed.pid");
|
|
1408
|
+
SPAWN_LOCK_PATH = path5.join(EXE_AI_DIR, "exed-spawn.lock");
|
|
1409
|
+
SPAWN_LOCK_STALE_MS = 3e4;
|
|
1410
|
+
CONNECT_TIMEOUT_MS = 15e3;
|
|
1411
|
+
REQUEST_TIMEOUT_MS = 3e4;
|
|
1412
|
+
DAEMON_TOKEN_ENV = "EXE_DAEMON_TOKEN";
|
|
1413
|
+
_socket = null;
|
|
1414
|
+
_connected = false;
|
|
1415
|
+
_buffer = "";
|
|
1416
|
+
_requestCount = 0;
|
|
1417
|
+
_consecutiveFailures = 0;
|
|
1418
|
+
HEALTH_CHECK_INTERVAL = 100;
|
|
1419
|
+
MAX_RETRIES_BEFORE_RESTART = 3;
|
|
1420
|
+
RETRY_DELAYS_MS = [1e3, 3e3, 5e3];
|
|
1421
|
+
MIN_DAEMON_AGE_MS = 3e4;
|
|
1422
|
+
_pending = /* @__PURE__ */ new Map();
|
|
1423
|
+
MAX_BUFFER = 1e7;
|
|
1424
|
+
}
|
|
1425
|
+
});
|
|
1426
|
+
|
|
1427
|
+
// src/lib/daemon-protocol.ts
|
|
1428
|
+
var daemon_protocol_exports = {};
|
|
1429
|
+
__export(daemon_protocol_exports, {
|
|
1430
|
+
deserializeArgs: () => deserializeArgs,
|
|
1431
|
+
deserializeResultSet: () => deserializeResultSet,
|
|
1432
|
+
deserializeValue: () => deserializeValue,
|
|
1433
|
+
serializeArgs: () => serializeArgs,
|
|
1434
|
+
serializeResultSet: () => serializeResultSet,
|
|
1435
|
+
serializeValue: () => serializeValue
|
|
1436
|
+
});
|
|
1437
|
+
function serializeValue(v) {
|
|
1438
|
+
if (v === null || v === void 0) return null;
|
|
1439
|
+
if (typeof v === "bigint") return Number(v);
|
|
1440
|
+
if (typeof v === "boolean") return v ? 1 : 0;
|
|
1441
|
+
if (v instanceof Uint8Array) {
|
|
1442
|
+
return { __blob: Buffer.from(v).toString("base64") };
|
|
1443
|
+
}
|
|
1444
|
+
if (ArrayBuffer.isView(v)) {
|
|
1445
|
+
return { __blob: Buffer.from(v.buffer, v.byteOffset, v.byteLength).toString("base64") };
|
|
1446
|
+
}
|
|
1447
|
+
if (v instanceof ArrayBuffer) {
|
|
1448
|
+
return { __blob: Buffer.from(v).toString("base64") };
|
|
1449
|
+
}
|
|
1450
|
+
if (typeof v === "string" || typeof v === "number") return v;
|
|
1451
|
+
return String(v);
|
|
1452
|
+
}
|
|
1453
|
+
function deserializeValue(v) {
|
|
1454
|
+
if (v === null) return null;
|
|
1455
|
+
if (typeof v === "object" && v !== null && "__blob" in v) {
|
|
1456
|
+
const buf = Buffer.from(v.__blob, "base64");
|
|
1457
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
|
1458
|
+
}
|
|
1459
|
+
return v;
|
|
1460
|
+
}
|
|
1461
|
+
function serializeArgs(args) {
|
|
1462
|
+
return args.map(serializeValue);
|
|
1463
|
+
}
|
|
1464
|
+
function deserializeArgs(args) {
|
|
1465
|
+
return args.map(deserializeValue);
|
|
1466
|
+
}
|
|
1467
|
+
function serializeResultSet(rs) {
|
|
1468
|
+
const rows = [];
|
|
1469
|
+
for (const row of rs.rows) {
|
|
1470
|
+
const obj = {};
|
|
1471
|
+
for (let i = 0; i < rs.columns.length; i++) {
|
|
1472
|
+
const col = rs.columns[i];
|
|
1473
|
+
if (col !== void 0) {
|
|
1474
|
+
obj[col] = serializeValue(row[i]);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
rows.push(obj);
|
|
1478
|
+
}
|
|
1479
|
+
return {
|
|
1480
|
+
columns: [...rs.columns],
|
|
1481
|
+
columnTypes: [...rs.columnTypes ?? []],
|
|
1482
|
+
rows,
|
|
1483
|
+
rowsAffected: typeof rs.rowsAffected === "bigint" ? Number(rs.rowsAffected) : rs.rowsAffected ?? 0,
|
|
1484
|
+
lastInsertRowid: rs.lastInsertRowid != null ? typeof rs.lastInsertRowid === "bigint" ? Number(rs.lastInsertRowid) : rs.lastInsertRowid : null
|
|
1485
|
+
};
|
|
1486
|
+
}
|
|
1487
|
+
function deserializeResultSet(srs) {
|
|
1488
|
+
const rows = srs.rows.map((obj) => {
|
|
1489
|
+
const values = srs.columns.map(
|
|
1490
|
+
(col) => deserializeValue(obj[col] ?? null)
|
|
1491
|
+
);
|
|
1492
|
+
const row = values;
|
|
1493
|
+
for (let i = 0; i < srs.columns.length; i++) {
|
|
1494
|
+
const col = srs.columns[i];
|
|
1495
|
+
if (col !== void 0) {
|
|
1496
|
+
row[col] = values[i] ?? null;
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
Object.defineProperty(row, "length", {
|
|
1500
|
+
value: values.length,
|
|
1501
|
+
enumerable: false
|
|
1502
|
+
});
|
|
1503
|
+
return row;
|
|
1504
|
+
});
|
|
1505
|
+
return {
|
|
1506
|
+
columns: srs.columns,
|
|
1507
|
+
columnTypes: srs.columnTypes ?? [],
|
|
1508
|
+
rows,
|
|
1509
|
+
rowsAffected: srs.rowsAffected,
|
|
1510
|
+
lastInsertRowid: srs.lastInsertRowid != null ? BigInt(srs.lastInsertRowid) : void 0,
|
|
1511
|
+
toJSON: () => ({
|
|
1512
|
+
columns: srs.columns,
|
|
1513
|
+
columnTypes: srs.columnTypes ?? [],
|
|
1514
|
+
rows: srs.rows,
|
|
1515
|
+
rowsAffected: srs.rowsAffected,
|
|
1516
|
+
lastInsertRowid: srs.lastInsertRowid
|
|
1517
|
+
})
|
|
1518
|
+
};
|
|
1519
|
+
}
|
|
1520
|
+
var init_daemon_protocol = __esm({
|
|
1521
|
+
"src/lib/daemon-protocol.ts"() {
|
|
1522
|
+
"use strict";
|
|
1523
|
+
}
|
|
1524
|
+
});
|
|
1525
|
+
|
|
1526
|
+
// src/lib/db-daemon-client.ts
|
|
1527
|
+
var db_daemon_client_exports = {};
|
|
1528
|
+
__export(db_daemon_client_exports, {
|
|
1529
|
+
createDaemonDbClient: () => createDaemonDbClient,
|
|
1530
|
+
initDaemonDbClient: () => initDaemonDbClient
|
|
1531
|
+
});
|
|
1532
|
+
function normalizeStatement2(stmt) {
|
|
1533
|
+
if (typeof stmt === "string") {
|
|
1534
|
+
return { sql: stmt, args: [] };
|
|
1535
|
+
}
|
|
1536
|
+
const sql = stmt.sql;
|
|
1537
|
+
let args = [];
|
|
1538
|
+
if (Array.isArray(stmt.args)) {
|
|
1539
|
+
args = stmt.args.map((v) => serializeValue(v));
|
|
1540
|
+
} else if (stmt.args && typeof stmt.args === "object") {
|
|
1541
|
+
const named = {};
|
|
1542
|
+
for (const [key, val] of Object.entries(stmt.args)) {
|
|
1543
|
+
named[key] = serializeValue(val);
|
|
1544
|
+
}
|
|
1545
|
+
return { sql, args: named };
|
|
1546
|
+
}
|
|
1547
|
+
return { sql, args };
|
|
1548
|
+
}
|
|
1549
|
+
function createDaemonDbClient(fallbackClient) {
|
|
1550
|
+
let _useDaemon = false;
|
|
1551
|
+
const client = {
|
|
1552
|
+
async execute(stmt) {
|
|
1553
|
+
if (!_useDaemon || !isClientConnected()) {
|
|
1554
|
+
return fallbackClient.execute(stmt);
|
|
1555
|
+
}
|
|
1556
|
+
const { sql, args } = normalizeStatement2(stmt);
|
|
1557
|
+
const response = await sendDaemonRequest({
|
|
1558
|
+
type: "db-execute",
|
|
1559
|
+
sql,
|
|
1560
|
+
args
|
|
1561
|
+
});
|
|
1562
|
+
if (response.error) {
|
|
1563
|
+
const errMsg = String(response.error);
|
|
1564
|
+
if (errMsg === "Not connected" || errMsg === "Request timeout" || errMsg === "Write failed" || errMsg === "DB not initialized") {
|
|
1565
|
+
process.stderr.write(`[db-daemon] Transport error (${errMsg}), falling back to direct
|
|
1566
|
+
`);
|
|
1567
|
+
return fallbackClient.execute(stmt);
|
|
1568
|
+
}
|
|
1569
|
+
throw new Error(errMsg);
|
|
1570
|
+
}
|
|
1571
|
+
if (response.db) {
|
|
1572
|
+
return deserializeResultSet(response.db);
|
|
1573
|
+
}
|
|
1574
|
+
process.stderr.write("[db-daemon] Unexpected response shape, falling back to direct\n");
|
|
1575
|
+
return fallbackClient.execute(stmt);
|
|
1576
|
+
},
|
|
1577
|
+
async batch(stmts, mode) {
|
|
1578
|
+
if (!_useDaemon || !isClientConnected()) {
|
|
1579
|
+
return fallbackClient.batch(stmts, mode);
|
|
1580
|
+
}
|
|
1581
|
+
const statements = stmts.map(normalizeStatement2);
|
|
1582
|
+
const response = await sendDaemonRequest({
|
|
1583
|
+
type: "db-batch",
|
|
1584
|
+
statements,
|
|
1585
|
+
mode: mode ?? "deferred"
|
|
1586
|
+
});
|
|
1587
|
+
if (response.error) {
|
|
1588
|
+
const errMsg = String(response.error);
|
|
1589
|
+
if (errMsg === "Not connected" || errMsg === "Request timeout" || errMsg === "Write failed" || errMsg === "DB not initialized") {
|
|
1590
|
+
process.stderr.write(`[db-daemon] Batch transport error (${errMsg}), falling back to direct
|
|
1591
|
+
`);
|
|
1592
|
+
return fallbackClient.batch(stmts, mode);
|
|
1593
|
+
}
|
|
1594
|
+
throw new Error(errMsg);
|
|
1595
|
+
}
|
|
1596
|
+
const batchResults = response["db-batch"];
|
|
1597
|
+
if (batchResults) {
|
|
1598
|
+
return batchResults.map(deserializeResultSet);
|
|
1599
|
+
}
|
|
1600
|
+
process.stderr.write("[db-daemon] Unexpected batch response shape, falling back to direct\n");
|
|
1601
|
+
return fallbackClient.batch(stmts, mode);
|
|
1602
|
+
},
|
|
1603
|
+
// Transaction support — delegate to fallback (transactions need direct connection)
|
|
1604
|
+
async transaction(mode) {
|
|
1605
|
+
return fallbackClient.transaction(mode);
|
|
1606
|
+
},
|
|
1607
|
+
// executeMultiple — delegate to fallback (used only for schema migrations)
|
|
1608
|
+
async executeMultiple(sql) {
|
|
1609
|
+
return fallbackClient.executeMultiple(sql);
|
|
1610
|
+
},
|
|
1611
|
+
// migrate — delegate to fallback
|
|
1612
|
+
async migrate(stmts) {
|
|
1613
|
+
return fallbackClient.migrate(stmts);
|
|
1614
|
+
},
|
|
1615
|
+
// Sync mode — delegate to fallback
|
|
1616
|
+
sync() {
|
|
1617
|
+
return fallbackClient.sync();
|
|
1618
|
+
},
|
|
1619
|
+
close() {
|
|
1620
|
+
_useDaemon = false;
|
|
1621
|
+
},
|
|
1622
|
+
get closed() {
|
|
1623
|
+
return fallbackClient.closed;
|
|
1624
|
+
},
|
|
1625
|
+
get protocol() {
|
|
1626
|
+
return fallbackClient.protocol;
|
|
1627
|
+
}
|
|
1628
|
+
};
|
|
1629
|
+
return {
|
|
1630
|
+
...client,
|
|
1631
|
+
/** Enable daemon routing (call after confirming daemon is connected) */
|
|
1632
|
+
_enableDaemon() {
|
|
1633
|
+
_useDaemon = true;
|
|
1634
|
+
},
|
|
1635
|
+
/** Check if daemon routing is active */
|
|
1636
|
+
_isDaemonActive() {
|
|
1637
|
+
return _useDaemon && isClientConnected();
|
|
1638
|
+
}
|
|
1639
|
+
};
|
|
1640
|
+
}
|
|
1641
|
+
async function initDaemonDbClient(fallbackClient) {
|
|
1642
|
+
if (process.env.EXE_IS_DAEMON === "1") return null;
|
|
1643
|
+
const connected = await connectEmbedDaemon();
|
|
1644
|
+
if (!connected) {
|
|
1645
|
+
process.stderr.write("[db-daemon] Daemon unavailable \u2014 using direct SQLite\n");
|
|
1646
|
+
return null;
|
|
1647
|
+
}
|
|
1648
|
+
const client = createDaemonDbClient(fallbackClient);
|
|
1649
|
+
client._enableDaemon();
|
|
1650
|
+
process.stderr.write("[db-daemon] DB routing through daemon (single-writer)\n");
|
|
1651
|
+
return client;
|
|
1652
|
+
}
|
|
1653
|
+
var init_db_daemon_client = __esm({
|
|
1654
|
+
"src/lib/db-daemon-client.ts"() {
|
|
1655
|
+
"use strict";
|
|
1656
|
+
init_exe_daemon_client();
|
|
1657
|
+
init_daemon_protocol();
|
|
1658
|
+
}
|
|
1659
|
+
});
|
|
1660
|
+
|
|
906
1661
|
// src/lib/database.ts
|
|
1662
|
+
var database_exports = {};
|
|
1663
|
+
__export(database_exports, {
|
|
1664
|
+
disposeDatabase: () => disposeDatabase,
|
|
1665
|
+
disposeTurso: () => disposeTurso,
|
|
1666
|
+
ensureSchema: () => ensureSchema,
|
|
1667
|
+
getClient: () => getClient,
|
|
1668
|
+
getRawClient: () => getRawClient,
|
|
1669
|
+
initDaemonClient: () => initDaemonClient,
|
|
1670
|
+
initDatabase: () => initDatabase,
|
|
1671
|
+
initTurso: () => initTurso,
|
|
1672
|
+
isInitialized: () => isInitialized
|
|
1673
|
+
});
|
|
907
1674
|
import { createClient } from "@libsql/client";
|
|
908
1675
|
async function initDatabase(config) {
|
|
909
1676
|
if (_walCheckpointTimer) {
|
|
@@ -942,15 +1709,18 @@ async function initDatabase(config) {
|
|
|
942
1709
|
});
|
|
943
1710
|
}, 3e4);
|
|
944
1711
|
_walCheckpointTimer.unref();
|
|
945
|
-
if (process.env.DATABASE_URL) {
|
|
1712
|
+
if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") {
|
|
946
1713
|
_adapterClient = await createPrismaDbAdapter(_resilientClient);
|
|
947
1714
|
}
|
|
948
1715
|
}
|
|
1716
|
+
function isInitialized() {
|
|
1717
|
+
return _adapterClient !== null || _client !== null;
|
|
1718
|
+
}
|
|
949
1719
|
function getClient() {
|
|
950
1720
|
if (!_adapterClient) {
|
|
951
1721
|
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
952
1722
|
}
|
|
953
|
-
if (process.env.DATABASE_URL) {
|
|
1723
|
+
if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") {
|
|
954
1724
|
return _adapterClient;
|
|
955
1725
|
}
|
|
956
1726
|
if (process.env.EXE_IS_DAEMON === "1") {
|
|
@@ -961,6 +1731,22 @@ function getClient() {
|
|
|
961
1731
|
}
|
|
962
1732
|
return _resilientClient;
|
|
963
1733
|
}
|
|
1734
|
+
async function initDaemonClient() {
|
|
1735
|
+
if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") return;
|
|
1736
|
+
if (process.env.EXE_IS_DAEMON === "1") return;
|
|
1737
|
+
if (process.env.VITEST) return;
|
|
1738
|
+
if (!_resilientClient) return;
|
|
1739
|
+
if (_daemonClient) return;
|
|
1740
|
+
try {
|
|
1741
|
+
const { initDaemonDbClient: initDaemonDbClient2 } = await Promise.resolve().then(() => (init_db_daemon_client(), db_daemon_client_exports));
|
|
1742
|
+
_daemonClient = await initDaemonDbClient2(_resilientClient);
|
|
1743
|
+
} catch (err) {
|
|
1744
|
+
process.stderr.write(
|
|
1745
|
+
`[database] Daemon client init failed (non-fatal): ${err instanceof Error ? err.message : String(err)}
|
|
1746
|
+
`
|
|
1747
|
+
);
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
964
1750
|
function getRawClient() {
|
|
965
1751
|
if (!_client) {
|
|
966
1752
|
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
@@ -1099,6 +1885,13 @@ async function ensureSchema() {
|
|
|
1099
1885
|
});
|
|
1100
1886
|
} catch {
|
|
1101
1887
|
}
|
|
1888
|
+
try {
|
|
1889
|
+
await client.execute({
|
|
1890
|
+
sql: `ALTER TABLE behaviors ADD COLUMN vector F32_BLOB(${EMBEDDING_DIM})`,
|
|
1891
|
+
args: []
|
|
1892
|
+
});
|
|
1893
|
+
} catch {
|
|
1894
|
+
}
|
|
1102
1895
|
try {
|
|
1103
1896
|
await client.execute({
|
|
1104
1897
|
sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
|
|
@@ -1943,32 +2736,53 @@ async function ensureSchema() {
|
|
|
1943
2736
|
} catch {
|
|
1944
2737
|
}
|
|
1945
2738
|
}
|
|
1946
|
-
|
|
2739
|
+
async function disposeDatabase() {
|
|
2740
|
+
if (_walCheckpointTimer) {
|
|
2741
|
+
clearInterval(_walCheckpointTimer);
|
|
2742
|
+
_walCheckpointTimer = null;
|
|
2743
|
+
}
|
|
2744
|
+
if (_daemonClient) {
|
|
2745
|
+
_daemonClient.close();
|
|
2746
|
+
_daemonClient = null;
|
|
2747
|
+
}
|
|
2748
|
+
if (_adapterClient && _adapterClient !== _resilientClient) {
|
|
2749
|
+
_adapterClient.close();
|
|
2750
|
+
}
|
|
2751
|
+
_adapterClient = null;
|
|
2752
|
+
if (_client) {
|
|
2753
|
+
_client.close();
|
|
2754
|
+
_client = null;
|
|
2755
|
+
_resilientClient = null;
|
|
2756
|
+
}
|
|
2757
|
+
}
|
|
2758
|
+
var _client, _resilientClient, _walCheckpointTimer, _daemonClient, _adapterClient, initTurso, disposeTurso;
|
|
1947
2759
|
var init_database = __esm({
|
|
1948
2760
|
"src/lib/database.ts"() {
|
|
1949
2761
|
"use strict";
|
|
1950
2762
|
init_db_retry();
|
|
1951
2763
|
init_employees();
|
|
1952
2764
|
init_database_adapter();
|
|
2765
|
+
init_memory();
|
|
1953
2766
|
_client = null;
|
|
1954
2767
|
_resilientClient = null;
|
|
1955
2768
|
_walCheckpointTimer = null;
|
|
1956
2769
|
_daemonClient = null;
|
|
1957
2770
|
_adapterClient = null;
|
|
1958
2771
|
initTurso = initDatabase;
|
|
2772
|
+
disposeTurso = disposeDatabase;
|
|
1959
2773
|
}
|
|
1960
2774
|
});
|
|
1961
2775
|
|
|
1962
2776
|
// src/lib/keychain.ts
|
|
1963
2777
|
import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod as chmod2 } from "fs/promises";
|
|
1964
|
-
import { existsSync as
|
|
1965
|
-
import
|
|
1966
|
-
import
|
|
2778
|
+
import { existsSync as existsSync6 } from "fs";
|
|
2779
|
+
import path6 from "path";
|
|
2780
|
+
import os5 from "os";
|
|
1967
2781
|
function getKeyDir() {
|
|
1968
|
-
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ??
|
|
2782
|
+
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path6.join(os5.homedir(), ".exe-os");
|
|
1969
2783
|
}
|
|
1970
2784
|
function getKeyPath() {
|
|
1971
|
-
return
|
|
2785
|
+
return path6.join(getKeyDir(), "master.key");
|
|
1972
2786
|
}
|
|
1973
2787
|
async function tryKeytar() {
|
|
1974
2788
|
try {
|
|
@@ -1989,9 +2803,9 @@ async function getMasterKey() {
|
|
|
1989
2803
|
}
|
|
1990
2804
|
}
|
|
1991
2805
|
const keyPath = getKeyPath();
|
|
1992
|
-
if (!
|
|
2806
|
+
if (!existsSync6(keyPath)) {
|
|
1993
2807
|
process.stderr.write(
|
|
1994
|
-
`[keychain] Key not found at ${keyPath} (HOME=${
|
|
2808
|
+
`[keychain] Key not found at ${keyPath} (HOME=${os5.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
|
|
1995
2809
|
`
|
|
1996
2810
|
);
|
|
1997
2811
|
return null;
|
|
@@ -2085,12 +2899,12 @@ __export(shard_manager_exports, {
|
|
|
2085
2899
|
listShards: () => listShards,
|
|
2086
2900
|
shardExists: () => shardExists
|
|
2087
2901
|
});
|
|
2088
|
-
import
|
|
2089
|
-
import { existsSync as
|
|
2902
|
+
import path7 from "path";
|
|
2903
|
+
import { existsSync as existsSync7, mkdirSync as mkdirSync2, readdirSync } from "fs";
|
|
2090
2904
|
import { createClient as createClient2 } from "@libsql/client";
|
|
2091
2905
|
function initShardManager(encryptionKey) {
|
|
2092
2906
|
_encryptionKey = encryptionKey;
|
|
2093
|
-
if (!
|
|
2907
|
+
if (!existsSync7(SHARDS_DIR)) {
|
|
2094
2908
|
mkdirSync2(SHARDS_DIR, { recursive: true });
|
|
2095
2909
|
}
|
|
2096
2910
|
_shardingEnabled = true;
|
|
@@ -2120,7 +2934,7 @@ function getShardClient(projectName) {
|
|
|
2120
2934
|
while (_shards.size >= MAX_OPEN_SHARDS) {
|
|
2121
2935
|
evictLRU();
|
|
2122
2936
|
}
|
|
2123
|
-
const dbPath =
|
|
2937
|
+
const dbPath = path7.join(SHARDS_DIR, `${safeName}.db`);
|
|
2124
2938
|
const client = createClient2({
|
|
2125
2939
|
url: `file:${dbPath}`,
|
|
2126
2940
|
encryptionKey: _encryptionKey
|
|
@@ -2131,10 +2945,10 @@ function getShardClient(projectName) {
|
|
|
2131
2945
|
}
|
|
2132
2946
|
function shardExists(projectName) {
|
|
2133
2947
|
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
2134
|
-
return
|
|
2948
|
+
return existsSync7(path7.join(SHARDS_DIR, `${safeName}.db`));
|
|
2135
2949
|
}
|
|
2136
2950
|
function listShards() {
|
|
2137
|
-
if (!
|
|
2951
|
+
if (!existsSync7(SHARDS_DIR)) return [];
|
|
2138
2952
|
return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
2139
2953
|
}
|
|
2140
2954
|
async function ensureShardSchema(client) {
|
|
@@ -2381,7 +3195,7 @@ var init_shard_manager = __esm({
|
|
|
2381
3195
|
"src/lib/shard-manager.ts"() {
|
|
2382
3196
|
"use strict";
|
|
2383
3197
|
init_config();
|
|
2384
|
-
SHARDS_DIR =
|
|
3198
|
+
SHARDS_DIR = path7.join(EXE_AI_DIR, "shards");
|
|
2385
3199
|
SHARD_IDLE_MS = 5 * 60 * 1e3;
|
|
2386
3200
|
MAX_OPEN_SHARDS = 10;
|
|
2387
3201
|
EVICTION_INTERVAL_MS = 60 * 1e3;
|
|
@@ -2501,6 +3315,68 @@ var init_platform_procedures = __esm({
|
|
|
2501
3315
|
domain: "architecture",
|
|
2502
3316
|
priority: "p0",
|
|
2503
3317
|
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."
|
|
3318
|
+
},
|
|
3319
|
+
// --- MCP is the ONLY data interface ---
|
|
3320
|
+
{
|
|
3321
|
+
title: "MCP disconnect \u2014 ask the user, never work around it",
|
|
3322
|
+
domain: "workflow",
|
|
3323
|
+
priority: "p0",
|
|
3324
|
+
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."
|
|
3325
|
+
},
|
|
3326
|
+
// --- MCP Tool Catalog (Layer 0 — every agent knows what tools exist) ---
|
|
3327
|
+
{
|
|
3328
|
+
title: "MCP tools \u2014 memory and search",
|
|
3329
|
+
domain: "tool-use",
|
|
3330
|
+
priority: "p1",
|
|
3331
|
+
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)."
|
|
3332
|
+
},
|
|
3333
|
+
{
|
|
3334
|
+
title: "MCP tools \u2014 task orchestration",
|
|
3335
|
+
domain: "tool-use",
|
|
3336
|
+
priority: "p1",
|
|
3337
|
+
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."
|
|
3338
|
+
},
|
|
3339
|
+
{
|
|
3340
|
+
title: "MCP tools \u2014 knowledge graph (GraphRAG)",
|
|
3341
|
+
domain: "tool-use",
|
|
3342
|
+
priority: "p1",
|
|
3343
|
+
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."
|
|
3344
|
+
},
|
|
3345
|
+
{
|
|
3346
|
+
title: "MCP tools \u2014 identity, behavior, and decisions",
|
|
3347
|
+
domain: "tool-use",
|
|
3348
|
+
priority: "p1",
|
|
3349
|
+
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."
|
|
3350
|
+
},
|
|
3351
|
+
{
|
|
3352
|
+
title: "MCP tools \u2014 communication and messaging",
|
|
3353
|
+
domain: "tool-use",
|
|
3354
|
+
priority: "p1",
|
|
3355
|
+
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.)."
|
|
3356
|
+
},
|
|
3357
|
+
{
|
|
3358
|
+
title: "MCP tools \u2014 wiki, documents, and content",
|
|
3359
|
+
domain: "tool-use",
|
|
3360
|
+
priority: "p1",
|
|
3361
|
+
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."
|
|
3362
|
+
},
|
|
3363
|
+
{
|
|
3364
|
+
title: "MCP tools \u2014 system, operations, and admin",
|
|
3365
|
+
domain: "tool-use",
|
|
3366
|
+
priority: "p1",
|
|
3367
|
+
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."
|
|
3368
|
+
},
|
|
3369
|
+
{
|
|
3370
|
+
title: "MCP tools \u2014 config, licensing, and team",
|
|
3371
|
+
domain: "tool-use",
|
|
3372
|
+
priority: "p1",
|
|
3373
|
+
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."
|
|
3374
|
+
},
|
|
3375
|
+
{
|
|
3376
|
+
title: "MCP tools \u2014 advanced (triggers, skills, orchestration)",
|
|
3377
|
+
domain: "tool-use",
|
|
3378
|
+
priority: "p1",
|
|
3379
|
+
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."
|
|
2504
3380
|
}
|
|
2505
3381
|
];
|
|
2506
3382
|
PLATFORM_PROCEDURE_TITLES = new Set(
|
|
@@ -2517,7 +3393,7 @@ __export(global_procedures_exports, {
|
|
|
2517
3393
|
loadGlobalProcedures: () => loadGlobalProcedures,
|
|
2518
3394
|
storeGlobalProcedure: () => storeGlobalProcedure
|
|
2519
3395
|
});
|
|
2520
|
-
import { randomUUID } from "crypto";
|
|
3396
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
2521
3397
|
async function loadGlobalProcedures() {
|
|
2522
3398
|
const client = getClient();
|
|
2523
3399
|
const result = await client.execute({
|
|
@@ -2546,7 +3422,7 @@ ${sections.join("\n\n")}
|
|
|
2546
3422
|
`;
|
|
2547
3423
|
}
|
|
2548
3424
|
async function storeGlobalProcedure(input) {
|
|
2549
|
-
const id =
|
|
3425
|
+
const id = randomUUID2();
|
|
2550
3426
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
2551
3427
|
const client = getClient();
|
|
2552
3428
|
await client.execute({
|
|
@@ -2581,6 +3457,23 @@ ${p.content}`).join("\n\n");
|
|
|
2581
3457
|
});
|
|
2582
3458
|
|
|
2583
3459
|
// src/lib/store.ts
|
|
3460
|
+
var store_exports = {};
|
|
3461
|
+
__export(store_exports, {
|
|
3462
|
+
attachDocumentMetadata: () => attachDocumentMetadata,
|
|
3463
|
+
buildRawVisibilityFilter: () => buildRawVisibilityFilter,
|
|
3464
|
+
buildWikiScopeFilter: () => buildWikiScopeFilter,
|
|
3465
|
+
classifyTier: () => classifyTier,
|
|
3466
|
+
disposeStore: () => disposeStore,
|
|
3467
|
+
flushBatch: () => flushBatch,
|
|
3468
|
+
flushTier3: () => flushTier3,
|
|
3469
|
+
getMemoryCardinality: () => getMemoryCardinality,
|
|
3470
|
+
initStore: () => initStore,
|
|
3471
|
+
reserveVersions: () => reserveVersions,
|
|
3472
|
+
searchMemories: () => searchMemories,
|
|
3473
|
+
updateMemoryStatus: () => updateMemoryStatus,
|
|
3474
|
+
vectorToBlob: () => vectorToBlob,
|
|
3475
|
+
writeMemory: () => writeMemory
|
|
3476
|
+
});
|
|
2584
3477
|
import { createHash } from "crypto";
|
|
2585
3478
|
function isBusyError2(err) {
|
|
2586
3479
|
if (err instanceof Error) {
|
|
@@ -2633,6 +3526,11 @@ async function initStore(options) {
|
|
|
2633
3526
|
encryptionKey: hexKey
|
|
2634
3527
|
});
|
|
2635
3528
|
await retryOnBusy2(() => ensureSchema(), "ensureSchema");
|
|
3529
|
+
try {
|
|
3530
|
+
const { initDaemonClient: initDaemonClient2 } = await Promise.resolve().then(() => (init_database(), database_exports));
|
|
3531
|
+
await initDaemonClient2();
|
|
3532
|
+
} catch {
|
|
3533
|
+
}
|
|
2636
3534
|
if (!options?.lightweight) {
|
|
2637
3535
|
try {
|
|
2638
3536
|
const { initShardManager: initShardManager2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
@@ -2652,6 +3550,274 @@ async function initStore(options) {
|
|
|
2652
3550
|
}
|
|
2653
3551
|
}
|
|
2654
3552
|
}
|
|
3553
|
+
function classifyTier(record) {
|
|
3554
|
+
if (record.tool_name === "commit_to_long_term_memory" && (record.importance ?? 0) >= 8) return 1;
|
|
3555
|
+
if (["store_memory", "manual"].includes(record.tool_name ?? "") && (record.importance ?? 0) >= 5) return 2;
|
|
3556
|
+
return 3;
|
|
3557
|
+
}
|
|
3558
|
+
function inferFilePaths(record) {
|
|
3559
|
+
if (!["Read", "Write", "Edit"].includes(record.tool_name)) return null;
|
|
3560
|
+
const firstLine = record.raw_text.split("\n")[0] ?? "";
|
|
3561
|
+
const match = firstLine.match(/(\/[\w./-]+\.\w+)/);
|
|
3562
|
+
return match ? JSON.stringify([match[1]]) : null;
|
|
3563
|
+
}
|
|
3564
|
+
function inferCommitHash(record) {
|
|
3565
|
+
if (record.tool_name !== "Bash") return null;
|
|
3566
|
+
const match = record.raw_text.match(/\b([a-f0-9]{7,40})\b/);
|
|
3567
|
+
return match ? match[1] : null;
|
|
3568
|
+
}
|
|
3569
|
+
function inferLanguageType(record) {
|
|
3570
|
+
const text = record.raw_text;
|
|
3571
|
+
if (!text || text.length < 10) return null;
|
|
3572
|
+
const trimmed = text.trimStart();
|
|
3573
|
+
if (trimmed.startsWith("{") || trimmed.startsWith("[")) return "json";
|
|
3574
|
+
if (/\b(SELECT|INSERT|UPDATE|DELETE|CREATE TABLE|ALTER TABLE)\b/i.test(text)) return "sql";
|
|
3575
|
+
if (/\b(function |const |import |export |class |def |async |=>)\b/.test(text)) return "code";
|
|
3576
|
+
if (trimmed.startsWith("#") || trimmed.startsWith("*")) return "prose";
|
|
3577
|
+
return "mixed";
|
|
3578
|
+
}
|
|
3579
|
+
function inferDomain(record) {
|
|
3580
|
+
const proj = (record.project_name ?? "").toLowerCase();
|
|
3581
|
+
if (proj.includes("marketing") || proj.includes("content")) return "marketing";
|
|
3582
|
+
if (proj.includes("crm") || proj.includes("customer")) return "customer";
|
|
3583
|
+
return null;
|
|
3584
|
+
}
|
|
3585
|
+
async function writeMemory(record) {
|
|
3586
|
+
if (record.vector !== null && record.vector.length !== EMBEDDING_DIM) {
|
|
3587
|
+
throw new Error(
|
|
3588
|
+
`Expected ${EMBEDDING_DIM}-dim vector, got ${record.vector.length}`
|
|
3589
|
+
);
|
|
3590
|
+
}
|
|
3591
|
+
const contentHash = createHash("md5").update(record.raw_text).digest("hex");
|
|
3592
|
+
if (_pendingRecords.some((r) => r.content_hash === contentHash && r.agent_id === record.agent_id)) {
|
|
3593
|
+
return;
|
|
3594
|
+
}
|
|
3595
|
+
try {
|
|
3596
|
+
const client = getClient();
|
|
3597
|
+
const existing = await client.execute({
|
|
3598
|
+
sql: "SELECT id FROM memories WHERE content_hash = ? AND agent_id = ? LIMIT 1",
|
|
3599
|
+
args: [contentHash, record.agent_id]
|
|
3600
|
+
});
|
|
3601
|
+
if (existing.rows.length > 0) return;
|
|
3602
|
+
} catch {
|
|
3603
|
+
}
|
|
3604
|
+
const dbRow = {
|
|
3605
|
+
id: record.id,
|
|
3606
|
+
agent_id: record.agent_id,
|
|
3607
|
+
agent_role: record.agent_role,
|
|
3608
|
+
session_id: record.session_id,
|
|
3609
|
+
timestamp: record.timestamp,
|
|
3610
|
+
tool_name: record.tool_name,
|
|
3611
|
+
project_name: record.project_name,
|
|
3612
|
+
has_error: record.has_error ? 1 : 0,
|
|
3613
|
+
raw_text: record.raw_text,
|
|
3614
|
+
vector: record.vector,
|
|
3615
|
+
version: 0,
|
|
3616
|
+
// Placeholder — assigned atomically at flush time
|
|
3617
|
+
task_id: record.task_id ?? null,
|
|
3618
|
+
importance: record.importance ?? 5,
|
|
3619
|
+
status: record.status ?? "active",
|
|
3620
|
+
confidence: record.confidence ?? 0.7,
|
|
3621
|
+
last_accessed: record.last_accessed ?? record.timestamp,
|
|
3622
|
+
workspace_id: record.workspace_id ?? null,
|
|
3623
|
+
document_id: record.document_id ?? null,
|
|
3624
|
+
user_id: record.user_id ?? null,
|
|
3625
|
+
char_offset: record.char_offset ?? null,
|
|
3626
|
+
page_number: record.page_number ?? null,
|
|
3627
|
+
source_path: record.source_path ?? null,
|
|
3628
|
+
source_type: record.source_type ?? null,
|
|
3629
|
+
tier: record.tier ?? classifyTier(record),
|
|
3630
|
+
supersedes_id: record.supersedes_id ?? null,
|
|
3631
|
+
draft: record.draft ? 1 : 0,
|
|
3632
|
+
memory_type: record.memory_type ?? "raw",
|
|
3633
|
+
trajectory: record.trajectory ? JSON.stringify(record.trajectory) : null,
|
|
3634
|
+
content_hash: contentHash,
|
|
3635
|
+
intent: record.intent ?? null,
|
|
3636
|
+
outcome: record.outcome ?? null,
|
|
3637
|
+
domain: record.domain ?? inferDomain(record),
|
|
3638
|
+
referenced_entities: record.referenced_entities ?? null,
|
|
3639
|
+
retrieval_count: record.retrieval_count ?? 0,
|
|
3640
|
+
chain_position: record.chain_position ?? null,
|
|
3641
|
+
review_status: record.review_status ?? null,
|
|
3642
|
+
context_window_pct: record.context_window_pct ?? null,
|
|
3643
|
+
file_paths: record.file_paths ?? inferFilePaths(record),
|
|
3644
|
+
commit_hash: record.commit_hash ?? inferCommitHash(record),
|
|
3645
|
+
duration_ms: record.duration_ms ?? null,
|
|
3646
|
+
token_cost: record.token_cost ?? null,
|
|
3647
|
+
audience: record.audience ?? null,
|
|
3648
|
+
language_type: record.language_type ?? inferLanguageType(record),
|
|
3649
|
+
parent_memory_id: record.parent_memory_id ?? null
|
|
3650
|
+
};
|
|
3651
|
+
_pendingRecords.push(dbRow);
|
|
3652
|
+
orgBus.emit({
|
|
3653
|
+
type: "memory_stored",
|
|
3654
|
+
agentId: record.agent_id,
|
|
3655
|
+
project: record.project_name,
|
|
3656
|
+
timestamp: record.timestamp
|
|
3657
|
+
});
|
|
3658
|
+
const MAX_PENDING = 1e3;
|
|
3659
|
+
if (_pendingRecords.length > MAX_PENDING) {
|
|
3660
|
+
const dropped = _pendingRecords.length - MAX_PENDING;
|
|
3661
|
+
_pendingRecords = _pendingRecords.slice(-MAX_PENDING);
|
|
3662
|
+
console.warn(`[store] Dropped ${dropped} oldest pending records (overflow)`);
|
|
3663
|
+
}
|
|
3664
|
+
if (_flushTimer === null) {
|
|
3665
|
+
_flushTimer = setInterval(() => {
|
|
3666
|
+
void flushBatch();
|
|
3667
|
+
}, _flushIntervalMs);
|
|
3668
|
+
if (_flushTimer && typeof _flushTimer === "object" && "unref" in _flushTimer) {
|
|
3669
|
+
_flushTimer.unref();
|
|
3670
|
+
}
|
|
3671
|
+
}
|
|
3672
|
+
if (_pendingRecords.length >= _batchSize) {
|
|
3673
|
+
await flushBatch();
|
|
3674
|
+
}
|
|
3675
|
+
}
|
|
3676
|
+
async function flushBatch() {
|
|
3677
|
+
if (_flushing || _pendingRecords.length === 0) return 0;
|
|
3678
|
+
_flushing = true;
|
|
3679
|
+
try {
|
|
3680
|
+
const batch = _pendingRecords.slice(0);
|
|
3681
|
+
const client = getClient();
|
|
3682
|
+
const vResult = await client.execute("SELECT MAX(version) as max_v FROM memories");
|
|
3683
|
+
let baseVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
|
|
3684
|
+
for (const row of batch) {
|
|
3685
|
+
row.version = baseVersion++;
|
|
3686
|
+
}
|
|
3687
|
+
_nextVersion = baseVersion;
|
|
3688
|
+
const buildStmt = (row) => {
|
|
3689
|
+
const hasVector = row.vector !== null;
|
|
3690
|
+
const taskId = row.task_id ?? null;
|
|
3691
|
+
const importance = row.importance ?? 5;
|
|
3692
|
+
const status = row.status ?? "active";
|
|
3693
|
+
const confidence = row.confidence ?? 0.7;
|
|
3694
|
+
const lastAccessed = row.last_accessed ?? row.timestamp;
|
|
3695
|
+
const workspaceId = row.workspace_id ?? null;
|
|
3696
|
+
const documentId = row.document_id ?? null;
|
|
3697
|
+
const userId = row.user_id ?? null;
|
|
3698
|
+
const charOffset = row.char_offset ?? null;
|
|
3699
|
+
const pageNumber = row.page_number ?? null;
|
|
3700
|
+
const sourcePath = row.source_path ?? null;
|
|
3701
|
+
const sourceType = row.source_type ?? null;
|
|
3702
|
+
const tier = row.tier ?? 3;
|
|
3703
|
+
const supersedesId = row.supersedes_id ?? null;
|
|
3704
|
+
const draft = row.draft ? 1 : 0;
|
|
3705
|
+
const memoryType = row.memory_type ?? "raw";
|
|
3706
|
+
const trajectory = row.trajectory ?? null;
|
|
3707
|
+
const contentHash = row.content_hash ?? null;
|
|
3708
|
+
const intent = row.intent ?? null;
|
|
3709
|
+
const outcome = row.outcome ?? null;
|
|
3710
|
+
const domain = row.domain ?? null;
|
|
3711
|
+
const referencedEntities = row.referenced_entities ?? null;
|
|
3712
|
+
const retrievalCount = row.retrieval_count ?? 0;
|
|
3713
|
+
const chainPosition = row.chain_position ?? null;
|
|
3714
|
+
const reviewStatus = row.review_status ?? null;
|
|
3715
|
+
const contextWindowPct = row.context_window_pct ?? null;
|
|
3716
|
+
const filePaths = row.file_paths ?? null;
|
|
3717
|
+
const commitHash = row.commit_hash ?? null;
|
|
3718
|
+
const durationMs = row.duration_ms ?? null;
|
|
3719
|
+
const tokenCost = row.token_cost ?? null;
|
|
3720
|
+
const audience = row.audience ?? null;
|
|
3721
|
+
const languageType = row.language_type ?? null;
|
|
3722
|
+
const parentMemoryId = row.parent_memory_id ?? null;
|
|
3723
|
+
const cols = `id, agent_id, agent_role, session_id, timestamp,
|
|
3724
|
+
tool_name, project_name,
|
|
3725
|
+
has_error, raw_text, vector, version, task_id, importance, status,
|
|
3726
|
+
confidence, last_accessed,
|
|
3727
|
+
workspace_id, document_id, user_id, char_offset, page_number,
|
|
3728
|
+
source_path, source_type, tier, supersedes_id, draft, memory_type, trajectory, content_hash,
|
|
3729
|
+
intent, outcome, domain, referenced_entities, retrieval_count,
|
|
3730
|
+
chain_position, review_status, context_window_pct, file_paths, commit_hash,
|
|
3731
|
+
duration_ms, token_cost, audience, language_type, parent_memory_id`;
|
|
3732
|
+
const metaArgs = [
|
|
3733
|
+
intent,
|
|
3734
|
+
outcome,
|
|
3735
|
+
domain,
|
|
3736
|
+
referencedEntities,
|
|
3737
|
+
retrievalCount,
|
|
3738
|
+
chainPosition,
|
|
3739
|
+
reviewStatus,
|
|
3740
|
+
contextWindowPct,
|
|
3741
|
+
filePaths,
|
|
3742
|
+
commitHash,
|
|
3743
|
+
durationMs,
|
|
3744
|
+
tokenCost,
|
|
3745
|
+
audience,
|
|
3746
|
+
languageType,
|
|
3747
|
+
parentMemoryId
|
|
3748
|
+
];
|
|
3749
|
+
const baseArgs = [
|
|
3750
|
+
row.id,
|
|
3751
|
+
row.agent_id,
|
|
3752
|
+
row.agent_role,
|
|
3753
|
+
row.session_id,
|
|
3754
|
+
row.timestamp,
|
|
3755
|
+
row.tool_name,
|
|
3756
|
+
row.project_name,
|
|
3757
|
+
row.has_error,
|
|
3758
|
+
row.raw_text
|
|
3759
|
+
];
|
|
3760
|
+
const sharedArgs = [
|
|
3761
|
+
row.version,
|
|
3762
|
+
taskId,
|
|
3763
|
+
importance,
|
|
3764
|
+
status,
|
|
3765
|
+
confidence,
|
|
3766
|
+
lastAccessed,
|
|
3767
|
+
workspaceId,
|
|
3768
|
+
documentId,
|
|
3769
|
+
userId,
|
|
3770
|
+
charOffset,
|
|
3771
|
+
pageNumber,
|
|
3772
|
+
sourcePath,
|
|
3773
|
+
sourceType,
|
|
3774
|
+
tier,
|
|
3775
|
+
supersedesId,
|
|
3776
|
+
draft,
|
|
3777
|
+
memoryType,
|
|
3778
|
+
trajectory,
|
|
3779
|
+
contentHash
|
|
3780
|
+
];
|
|
3781
|
+
return {
|
|
3782
|
+
sql: hasVector ? `INSERT OR IGNORE INTO memories (${cols})
|
|
3783
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, vector32(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` : `INSERT OR IGNORE INTO memories (${cols})
|
|
3784
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
3785
|
+
args: hasVector ? [...baseArgs, vectorToBlob(row.vector), ...sharedArgs, ...metaArgs] : [...baseArgs, ...sharedArgs, ...metaArgs]
|
|
3786
|
+
};
|
|
3787
|
+
};
|
|
3788
|
+
const globalClient = getClient();
|
|
3789
|
+
const globalStmts = batch.map(buildStmt);
|
|
3790
|
+
await globalClient.batch(globalStmts, "write");
|
|
3791
|
+
_pendingRecords.splice(0, batch.length);
|
|
3792
|
+
try {
|
|
3793
|
+
const { isShardingEnabled: isShardingEnabled2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
3794
|
+
if (isShardingEnabled2()) {
|
|
3795
|
+
const byProject = /* @__PURE__ */ new Map();
|
|
3796
|
+
for (const row of batch) {
|
|
3797
|
+
const proj = row.project_name || "unknown";
|
|
3798
|
+
if (!byProject.has(proj)) byProject.set(proj, []);
|
|
3799
|
+
byProject.get(proj).push(row);
|
|
3800
|
+
}
|
|
3801
|
+
for (const [project, rows] of byProject) {
|
|
3802
|
+
try {
|
|
3803
|
+
const shardClient = await getReadyShardClient2(project);
|
|
3804
|
+
const shardStmts = rows.map(buildStmt);
|
|
3805
|
+
await shardClient.batch(shardStmts, "write");
|
|
3806
|
+
} catch (err) {
|
|
3807
|
+
process.stderr.write(
|
|
3808
|
+
`[store] Shard write failed for ${project}: ${err instanceof Error ? err.message : String(err)}
|
|
3809
|
+
`
|
|
3810
|
+
);
|
|
3811
|
+
}
|
|
3812
|
+
}
|
|
3813
|
+
}
|
|
3814
|
+
} catch {
|
|
3815
|
+
}
|
|
3816
|
+
return batch.length;
|
|
3817
|
+
} finally {
|
|
3818
|
+
_flushing = false;
|
|
3819
|
+
}
|
|
3820
|
+
}
|
|
2655
3821
|
function buildWikiScopeFilter(options, columnPrefix) {
|
|
2656
3822
|
const args = [];
|
|
2657
3823
|
let clause = "";
|
|
@@ -2678,6 +3844,90 @@ function buildRawVisibilityFilter(options, columnPrefix) {
|
|
|
2678
3844
|
}
|
|
2679
3845
|
return { clause: "", args: [] };
|
|
2680
3846
|
}
|
|
3847
|
+
async function searchMemories(queryVector, agentId, options) {
|
|
3848
|
+
let client;
|
|
3849
|
+
try {
|
|
3850
|
+
const { isShardingEnabled: isShardingEnabled2, shardExists: shardExists2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
3851
|
+
if (isShardingEnabled2() && options?.projectName && shardExists2(options.projectName)) {
|
|
3852
|
+
client = await getReadyShardClient2(options.projectName);
|
|
3853
|
+
} else {
|
|
3854
|
+
client = getClient();
|
|
3855
|
+
}
|
|
3856
|
+
} catch {
|
|
3857
|
+
client = getClient();
|
|
3858
|
+
}
|
|
3859
|
+
const limit = options?.limit ?? 10;
|
|
3860
|
+
const statusFilter = options?.includeArchived ? "" : `
|
|
3861
|
+
AND COALESCE(status, 'active') = 'active'`;
|
|
3862
|
+
const draftFilter = options?.includeDrafts ? "" : `
|
|
3863
|
+
AND (draft = 0 OR draft IS NULL)`;
|
|
3864
|
+
let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
|
|
3865
|
+
tool_name, project_name,
|
|
3866
|
+
has_error, raw_text, vector, importance, status,
|
|
3867
|
+
confidence, last_accessed,
|
|
3868
|
+
workspace_id, document_id, user_id,
|
|
3869
|
+
char_offset, page_number,
|
|
3870
|
+
source_path, source_type
|
|
3871
|
+
FROM memories
|
|
3872
|
+
WHERE agent_id = ?
|
|
3873
|
+
AND vector IS NOT NULL${statusFilter}${draftFilter}
|
|
3874
|
+
AND COALESCE(confidence, 0.7) >= 0.3`;
|
|
3875
|
+
const args = [agentId];
|
|
3876
|
+
const scope = buildWikiScopeFilter(options, "");
|
|
3877
|
+
sql += scope.clause;
|
|
3878
|
+
args.push(...scope.args);
|
|
3879
|
+
const rawVisibility = buildRawVisibilityFilter(options, "");
|
|
3880
|
+
sql += rawVisibility.clause;
|
|
3881
|
+
args.push(...rawVisibility.args);
|
|
3882
|
+
if (options?.projectName) {
|
|
3883
|
+
sql += ` AND project_name = ?`;
|
|
3884
|
+
args.push(options.projectName);
|
|
3885
|
+
}
|
|
3886
|
+
if (options?.toolName) {
|
|
3887
|
+
sql += ` AND tool_name = ?`;
|
|
3888
|
+
args.push(options.toolName);
|
|
3889
|
+
}
|
|
3890
|
+
if (options?.hasError !== void 0) {
|
|
3891
|
+
sql += ` AND has_error = ?`;
|
|
3892
|
+
args.push(options.hasError ? 1 : 0);
|
|
3893
|
+
}
|
|
3894
|
+
if (options?.since) {
|
|
3895
|
+
sql += ` AND timestamp >= ?`;
|
|
3896
|
+
args.push(options.since);
|
|
3897
|
+
}
|
|
3898
|
+
if (options?.memoryType) {
|
|
3899
|
+
sql += ` AND memory_type = ?`;
|
|
3900
|
+
args.push(options.memoryType);
|
|
3901
|
+
}
|
|
3902
|
+
sql += ` ORDER BY vector_distance_cos(vector, vector32(?))`;
|
|
3903
|
+
args.push(vectorToBlob(queryVector));
|
|
3904
|
+
sql += ` LIMIT ?`;
|
|
3905
|
+
args.push(limit);
|
|
3906
|
+
const result = await client.execute({ sql, args });
|
|
3907
|
+
return result.rows.map((row) => ({
|
|
3908
|
+
id: row.id,
|
|
3909
|
+
agent_id: row.agent_id,
|
|
3910
|
+
agent_role: row.agent_role,
|
|
3911
|
+
session_id: row.session_id,
|
|
3912
|
+
timestamp: row.timestamp,
|
|
3913
|
+
tool_name: row.tool_name,
|
|
3914
|
+
project_name: row.project_name,
|
|
3915
|
+
has_error: row.has_error === 1,
|
|
3916
|
+
raw_text: row.raw_text,
|
|
3917
|
+
vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
|
|
3918
|
+
importance: row.importance ?? 5,
|
|
3919
|
+
status: row.status ?? "active",
|
|
3920
|
+
confidence: row.confidence ?? 0.7,
|
|
3921
|
+
last_accessed: row.last_accessed ?? row.timestamp,
|
|
3922
|
+
workspace_id: row.workspace_id ?? null,
|
|
3923
|
+
document_id: row.document_id ?? null,
|
|
3924
|
+
user_id: row.user_id ?? null,
|
|
3925
|
+
char_offset: row.char_offset ?? null,
|
|
3926
|
+
page_number: row.page_number ?? null,
|
|
3927
|
+
source_path: row.source_path ?? null,
|
|
3928
|
+
source_type: row.source_type ?? null
|
|
3929
|
+
}));
|
|
3930
|
+
}
|
|
2681
3931
|
async function attachDocumentMetadata(records) {
|
|
2682
3932
|
const docIds = [
|
|
2683
3933
|
...new Set(
|
|
@@ -2713,6 +3963,67 @@ async function attachDocumentMetadata(records) {
|
|
|
2713
3963
|
}
|
|
2714
3964
|
return records;
|
|
2715
3965
|
}
|
|
3966
|
+
async function flushTier3(agentId, options) {
|
|
3967
|
+
const client = getClient();
|
|
3968
|
+
const maxAge = options?.maxAgeHours ?? 72;
|
|
3969
|
+
const cutoff = new Date(Date.now() - maxAge * 36e5).toISOString();
|
|
3970
|
+
if (options?.dryRun) {
|
|
3971
|
+
const result2 = await client.execute({
|
|
3972
|
+
sql: `SELECT COUNT(*) as cnt FROM memories
|
|
3973
|
+
WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
|
|
3974
|
+
args: [agentId, cutoff]
|
|
3975
|
+
});
|
|
3976
|
+
return { archived: Number(result2.rows[0]?.cnt ?? 0) };
|
|
3977
|
+
}
|
|
3978
|
+
const result = await client.execute({
|
|
3979
|
+
sql: `UPDATE memories SET status = 'archived'
|
|
3980
|
+
WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
|
|
3981
|
+
args: [agentId, cutoff]
|
|
3982
|
+
});
|
|
3983
|
+
return { archived: result.rowsAffected };
|
|
3984
|
+
}
|
|
3985
|
+
async function disposeStore() {
|
|
3986
|
+
if (_flushTimer !== null) {
|
|
3987
|
+
clearInterval(_flushTimer);
|
|
3988
|
+
_flushTimer = null;
|
|
3989
|
+
}
|
|
3990
|
+
if (_pendingRecords.length > 0) {
|
|
3991
|
+
await flushBatch();
|
|
3992
|
+
}
|
|
3993
|
+
await disposeTurso();
|
|
3994
|
+
_pendingRecords = [];
|
|
3995
|
+
_nextVersion = 1;
|
|
3996
|
+
}
|
|
3997
|
+
function vectorToBlob(vector) {
|
|
3998
|
+
const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
|
|
3999
|
+
return JSON.stringify(Array.from(f32));
|
|
4000
|
+
}
|
|
4001
|
+
async function updateMemoryStatus(id, status) {
|
|
4002
|
+
const client = getClient();
|
|
4003
|
+
await client.execute({
|
|
4004
|
+
sql: `UPDATE memories SET status = ? WHERE id = ?`,
|
|
4005
|
+
args: [status, id]
|
|
4006
|
+
});
|
|
4007
|
+
}
|
|
4008
|
+
function reserveVersions(count) {
|
|
4009
|
+
const reserved = [];
|
|
4010
|
+
for (let i = 0; i < count; i++) {
|
|
4011
|
+
reserved.push(_nextVersion++);
|
|
4012
|
+
}
|
|
4013
|
+
return reserved;
|
|
4014
|
+
}
|
|
4015
|
+
async function getMemoryCardinality(agentId) {
|
|
4016
|
+
try {
|
|
4017
|
+
const client = getClient();
|
|
4018
|
+
const result = await client.execute({
|
|
4019
|
+
sql: `SELECT COUNT(*) as cnt FROM memories WHERE agent_id = ? AND COALESCE(status, 'active') = 'active'`,
|
|
4020
|
+
args: [agentId]
|
|
4021
|
+
});
|
|
4022
|
+
return Number(result.rows[0]?.cnt) || 0;
|
|
4023
|
+
} catch {
|
|
4024
|
+
return 0;
|
|
4025
|
+
}
|
|
4026
|
+
}
|
|
2716
4027
|
var INIT_MAX_RETRIES, INIT_RETRY_DELAY_MS, _pendingRecords, _batchSize, _flushIntervalMs, _flushTimer, _flushing, _nextVersion;
|
|
2717
4028
|
var init_store = __esm({
|
|
2718
4029
|
"src/lib/store.ts"() {
|
|
@@ -2734,10 +4045,73 @@ var init_store = __esm({
|
|
|
2734
4045
|
});
|
|
2735
4046
|
|
|
2736
4047
|
// src/bin/exe-forget.ts
|
|
2737
|
-
init_store();
|
|
2738
|
-
init_database();
|
|
2739
4048
|
import { createInterface } from "readline";
|
|
2740
4049
|
|
|
4050
|
+
// src/bin/fast-db-init.ts
|
|
4051
|
+
async function fastDbInit() {
|
|
4052
|
+
const { isInitialized: isInitialized2, getClient: getClient2 } = await Promise.resolve().then(() => (init_database(), database_exports));
|
|
4053
|
+
if (isInitialized2()) {
|
|
4054
|
+
return getClient2();
|
|
4055
|
+
}
|
|
4056
|
+
try {
|
|
4057
|
+
const { connectEmbedDaemon: connectEmbedDaemon2, sendDaemonRequest: sendDaemonRequest2, isClientConnected: isClientConnected2 } = await Promise.resolve().then(() => (init_exe_daemon_client(), exe_daemon_client_exports));
|
|
4058
|
+
const { deserializeResultSet: deserializeResultSet2 } = await Promise.resolve().then(() => (init_daemon_protocol(), daemon_protocol_exports));
|
|
4059
|
+
await connectEmbedDaemon2();
|
|
4060
|
+
if (isClientConnected2()) {
|
|
4061
|
+
const daemonClient = {
|
|
4062
|
+
async execute(stmt) {
|
|
4063
|
+
const sql = typeof stmt === "string" ? stmt : stmt.sql;
|
|
4064
|
+
const args = typeof stmt === "string" ? [] : Array.isArray(stmt.args) ? stmt.args : [];
|
|
4065
|
+
const resp = await sendDaemonRequest2({ type: "db-execute", sql, args });
|
|
4066
|
+
if (resp.error) throw new Error(String(resp.error));
|
|
4067
|
+
if (resp.db) return deserializeResultSet2(resp.db);
|
|
4068
|
+
throw new Error("Unexpected daemon response");
|
|
4069
|
+
},
|
|
4070
|
+
async batch(stmts, mode) {
|
|
4071
|
+
const statements = stmts.map((s) => {
|
|
4072
|
+
const sql = typeof s === "string" ? s : s.sql;
|
|
4073
|
+
const args = typeof s === "string" ? [] : Array.isArray(s.args) ? s.args : [];
|
|
4074
|
+
return { sql, args };
|
|
4075
|
+
});
|
|
4076
|
+
const resp = await sendDaemonRequest2({ type: "db-batch", statements, mode: mode ?? "deferred" });
|
|
4077
|
+
if (resp.error) throw new Error(String(resp.error));
|
|
4078
|
+
const batchResults = resp["db-batch"];
|
|
4079
|
+
if (batchResults) return batchResults.map(deserializeResultSet2);
|
|
4080
|
+
throw new Error("Unexpected daemon batch response");
|
|
4081
|
+
},
|
|
4082
|
+
async transaction(_mode) {
|
|
4083
|
+
throw new Error("Transactions not supported via daemon socket");
|
|
4084
|
+
},
|
|
4085
|
+
async executeMultiple(_sql) {
|
|
4086
|
+
throw new Error("executeMultiple not supported via daemon socket");
|
|
4087
|
+
},
|
|
4088
|
+
async migrate(_stmts) {
|
|
4089
|
+
throw new Error("migrate not supported via daemon socket");
|
|
4090
|
+
},
|
|
4091
|
+
sync() {
|
|
4092
|
+
return Promise.resolve(void 0);
|
|
4093
|
+
},
|
|
4094
|
+
close() {
|
|
4095
|
+
},
|
|
4096
|
+
get closed() {
|
|
4097
|
+
return false;
|
|
4098
|
+
},
|
|
4099
|
+
get protocol() {
|
|
4100
|
+
return "file";
|
|
4101
|
+
}
|
|
4102
|
+
};
|
|
4103
|
+
return daemonClient;
|
|
4104
|
+
}
|
|
4105
|
+
} catch {
|
|
4106
|
+
}
|
|
4107
|
+
const { initStore: initStore2 } = await Promise.resolve().then(() => (init_store(), store_exports));
|
|
4108
|
+
await initStore2({ lightweight: true });
|
|
4109
|
+
return getClient2();
|
|
4110
|
+
}
|
|
4111
|
+
|
|
4112
|
+
// src/bin/exe-forget.ts
|
|
4113
|
+
init_database();
|
|
4114
|
+
|
|
2741
4115
|
// src/lib/hybrid-search.ts
|
|
2742
4116
|
init_store();
|
|
2743
4117
|
init_database();
|
|
@@ -2970,13 +4344,13 @@ function rowToMemoryRecord(row) {
|
|
|
2970
4344
|
|
|
2971
4345
|
// src/lib/is-main.ts
|
|
2972
4346
|
import { realpathSync } from "fs";
|
|
2973
|
-
import { fileURLToPath } from "url";
|
|
4347
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
2974
4348
|
function isMainModule(importMetaUrl) {
|
|
2975
4349
|
if (process.argv[1] == null) return false;
|
|
2976
4350
|
if (process.argv[1].includes("mcp/server")) return false;
|
|
2977
4351
|
try {
|
|
2978
4352
|
const scriptPath = realpathSync(process.argv[1]);
|
|
2979
|
-
const modulePath = realpathSync(
|
|
4353
|
+
const modulePath = realpathSync(fileURLToPath2(importMetaUrl));
|
|
2980
4354
|
return scriptPath === modulePath;
|
|
2981
4355
|
} catch {
|
|
2982
4356
|
return importMetaUrl === `file://${process.argv[1]}` || importMetaUrl === new URL(process.argv[1], "file://").href;
|
|
@@ -3011,7 +4385,7 @@ async function main() {
|
|
|
3011
4385
|
console.error(' node "$(npm root -g)/exe-os/dist/bin/exe-forget.js" --query "<search>"');
|
|
3012
4386
|
process.exit(1);
|
|
3013
4387
|
}
|
|
3014
|
-
await
|
|
4388
|
+
await fastDbInit();
|
|
3015
4389
|
const client = getClient();
|
|
3016
4390
|
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
3017
4391
|
try {
|