@askexenow/exe-os 0.9.15 → 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 +1880 -417
- package/dist/bin/cli.js +252 -113
- package/dist/bin/exe-agent-config.js +6 -4
- package/dist/bin/exe-agent.js +62 -0
- package/dist/bin/exe-assign.js +358 -10
- package/dist/bin/exe-boot.js +405 -36
- package/dist/bin/exe-call.js +76 -4
- package/dist/bin/exe-cloud.js +8 -0
- package/dist/bin/exe-dispatch.js +1839 -229
- package/dist/bin/exe-doctor.js +720 -52
- package/dist/bin/exe-export-behaviors.js +1429 -148
- package/dist/bin/exe-forget.js +1424 -34
- package/dist/bin/exe-gateway.js +1663 -1300
- package/dist/bin/exe-heartbeat.js +1909 -448
- package/dist/bin/exe-kill.js +1624 -346
- package/dist/bin/exe-launch-agent.js +730 -92
- package/dist/bin/exe-link.js +27 -8
- package/dist/bin/exe-new-employee.js +79 -11
- package/dist/bin/exe-pending-messages.js +2769 -1306
- package/dist/bin/exe-pending-notifications.js +2826 -1363
- package/dist/bin/exe-pending-reviews.js +2846 -1381
- package/dist/bin/exe-rename.js +89 -8
- package/dist/bin/exe-review.js +1494 -312
- package/dist/bin/exe-search.js +1670 -1289
- package/dist/bin/exe-session-cleanup.js +225 -95
- package/dist/bin/exe-settings.js +14 -4
- package/dist/bin/exe-start-codex.js +773 -120
- package/dist/bin/exe-start-opencode.js +763 -108
- package/dist/bin/exe-status.js +1897 -434
- package/dist/bin/exe-team.js +1782 -324
- package/dist/bin/git-sweep.js +439 -37
- package/dist/bin/graph-backfill.js +681 -27
- package/dist/bin/graph-export.js +1419 -141
- package/dist/bin/install.js +8 -10
- package/dist/bin/intercom-check.js +8641 -0
- package/dist/bin/scan-tasks.js +584 -42
- package/dist/bin/setup.js +86 -12
- package/dist/bin/shard-migrate.js +682 -28
- package/dist/gateway/index.js +1663 -1300
- package/dist/hooks/bug-report-worker.js +1158 -189
- package/dist/hooks/codex-stop-task-finalizer.js +1081 -118
- package/dist/hooks/commit-complete.js +439 -37
- package/dist/hooks/error-recall.js +1665 -1284
- package/dist/hooks/ingest-worker.js +250 -7952
- package/dist/hooks/ingest.js +724 -121
- package/dist/hooks/instructions-loaded.js +396 -20
- package/dist/hooks/notification.js +396 -20
- package/dist/hooks/post-compact.js +407 -21
- package/dist/hooks/{response-ingest-worker.js → post-tool-combined.js} +4157 -1716
- package/dist/hooks/pre-compact.js +517 -49
- package/dist/hooks/pre-tool-use.js +412 -24
- package/dist/hooks/prompt-submit.js +384 -100
- package/dist/hooks/session-end.js +521 -52
- package/dist/hooks/session-start.js +324 -25
- package/dist/hooks/stop.js +215 -50
- package/dist/hooks/subagent-stop.js +118 -18
- package/dist/hooks/summary-worker.js +215 -361
- package/dist/index.js +256 -111
- package/dist/lib/agent-config.js +6 -4
- 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 +6 -4
- package/dist/lib/exe-daemon.js +6721 -6184
- package/dist/lib/hybrid-search.js +1670 -1289
- package/dist/lib/identity.js +1 -1
- package/dist/lib/messaging.js +26 -8
- 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 +689 -27
- package/dist/lib/tasks.js +683 -31
- package/dist/lib/tmux-routing.js +750 -98
- package/dist/lib/tmux-transport.js +10 -0
- package/dist/lib/token-spend.js +1 -1
- package/dist/lib/transport.js +10 -0
- package/dist/mcp/server.js +961 -399
- 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 +638 -52
- 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 +20 -2
- package/dist/mcp/tools/send-message.js +28 -10
- package/dist/mcp/tools/update-task.js +695 -43
- package/dist/runtime/index.js +456 -41
- package/dist/tui/App.js +400 -40
- 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
|
@@ -15,6 +15,15 @@ var __export = (target, all) => {
|
|
|
15
15
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
+
// src/types/memory.ts
|
|
19
|
+
var EMBEDDING_DIM;
|
|
20
|
+
var init_memory = __esm({
|
|
21
|
+
"src/types/memory.ts"() {
|
|
22
|
+
"use strict";
|
|
23
|
+
EMBEDDING_DIM = 1024;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
18
27
|
// src/lib/db-retry.ts
|
|
19
28
|
function isBusyError(err) {
|
|
20
29
|
if (err instanceof Error) {
|
|
@@ -64,9 +73,9 @@ var MAX_RETRIES, BASE_DELAY_MS, MAX_JITTER_MS;
|
|
|
64
73
|
var init_db_retry = __esm({
|
|
65
74
|
"src/lib/db-retry.ts"() {
|
|
66
75
|
"use strict";
|
|
67
|
-
MAX_RETRIES =
|
|
68
|
-
BASE_DELAY_MS =
|
|
69
|
-
MAX_JITTER_MS =
|
|
76
|
+
MAX_RETRIES = 5;
|
|
77
|
+
BASE_DELAY_MS = 250;
|
|
78
|
+
MAX_JITTER_MS = 400;
|
|
70
79
|
}
|
|
71
80
|
});
|
|
72
81
|
|
|
@@ -919,450 +928,1143 @@ var init_database_adapter = __esm({
|
|
|
919
928
|
}
|
|
920
929
|
});
|
|
921
930
|
|
|
922
|
-
// src/lib/
|
|
923
|
-
import
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
931
|
+
// src/lib/daemon-auth.ts
|
|
932
|
+
import crypto from "crypto";
|
|
933
|
+
import path4 from "path";
|
|
934
|
+
import { existsSync as existsSync4, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
935
|
+
function normalizeToken(token) {
|
|
936
|
+
if (!token) return null;
|
|
937
|
+
const trimmed = token.trim();
|
|
938
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
939
|
+
}
|
|
940
|
+
function readDaemonToken() {
|
|
941
|
+
try {
|
|
942
|
+
if (!existsSync4(DAEMON_TOKEN_PATH)) return null;
|
|
943
|
+
return normalizeToken(readFileSync3(DAEMON_TOKEN_PATH, "utf8"));
|
|
944
|
+
} catch {
|
|
945
|
+
return null;
|
|
928
946
|
}
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
947
|
+
}
|
|
948
|
+
function ensureDaemonToken(seed) {
|
|
949
|
+
const existing = readDaemonToken();
|
|
950
|
+
if (existing) return existing;
|
|
951
|
+
const token = normalizeToken(seed) ?? crypto.randomBytes(32).toString("hex");
|
|
952
|
+
ensurePrivateDirSync(EXE_AI_DIR);
|
|
953
|
+
writeFileSync2(DAEMON_TOKEN_PATH, `${token}
|
|
954
|
+
`, "utf8");
|
|
955
|
+
enforcePrivateFileSync(DAEMON_TOKEN_PATH);
|
|
956
|
+
return token;
|
|
957
|
+
}
|
|
958
|
+
var DAEMON_TOKEN_PATH;
|
|
959
|
+
var init_daemon_auth = __esm({
|
|
960
|
+
"src/lib/daemon-auth.ts"() {
|
|
961
|
+
"use strict";
|
|
962
|
+
init_config();
|
|
963
|
+
init_secure_files();
|
|
964
|
+
DAEMON_TOKEN_PATH = path4.join(EXE_AI_DIR, "exed.token");
|
|
932
965
|
}
|
|
933
|
-
|
|
934
|
-
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
// src/lib/exe-daemon-client.ts
|
|
969
|
+
import net from "net";
|
|
970
|
+
import os4 from "os";
|
|
971
|
+
import { spawn } from "child_process";
|
|
972
|
+
import { randomUUID } from "crypto";
|
|
973
|
+
import { existsSync as existsSync5, unlinkSync as unlinkSync2, readFileSync as readFileSync4, openSync, closeSync, statSync } from "fs";
|
|
974
|
+
import path5 from "path";
|
|
975
|
+
import { fileURLToPath } from "url";
|
|
976
|
+
function handleData(chunk) {
|
|
977
|
+
_buffer += chunk.toString();
|
|
978
|
+
if (_buffer.length > MAX_BUFFER) {
|
|
979
|
+
_buffer = "";
|
|
980
|
+
return;
|
|
935
981
|
}
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
982
|
+
let newlineIdx;
|
|
983
|
+
while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
|
|
984
|
+
const line = _buffer.slice(0, newlineIdx).trim();
|
|
985
|
+
_buffer = _buffer.slice(newlineIdx + 1);
|
|
986
|
+
if (!line) continue;
|
|
987
|
+
try {
|
|
988
|
+
const response = JSON.parse(line);
|
|
989
|
+
const id = response.id;
|
|
990
|
+
if (!id) continue;
|
|
991
|
+
const entry = _pending.get(id);
|
|
992
|
+
if (entry) {
|
|
993
|
+
clearTimeout(entry.timer);
|
|
994
|
+
_pending.delete(id);
|
|
995
|
+
entry.resolve(response);
|
|
996
|
+
}
|
|
997
|
+
} catch {
|
|
998
|
+
}
|
|
941
999
|
}
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
1000
|
+
}
|
|
1001
|
+
function cleanupStaleFiles() {
|
|
1002
|
+
if (existsSync5(PID_PATH)) {
|
|
1003
|
+
try {
|
|
1004
|
+
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
1005
|
+
if (pid > 0) {
|
|
1006
|
+
try {
|
|
1007
|
+
process.kill(pid, 0);
|
|
1008
|
+
return;
|
|
1009
|
+
} catch {
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
} catch {
|
|
1013
|
+
}
|
|
1014
|
+
try {
|
|
1015
|
+
unlinkSync2(PID_PATH);
|
|
1016
|
+
} catch {
|
|
1017
|
+
}
|
|
1018
|
+
try {
|
|
1019
|
+
unlinkSync2(SOCKET_PATH);
|
|
1020
|
+
} catch {
|
|
1021
|
+
}
|
|
947
1022
|
}
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
if (_walCheckpointTimer) clearInterval(_walCheckpointTimer);
|
|
956
|
-
_walCheckpointTimer = setInterval(() => {
|
|
957
|
-
_client?.execute("PRAGMA wal_checkpoint(PASSIVE)").catch(() => {
|
|
958
|
-
});
|
|
959
|
-
}, 3e4);
|
|
960
|
-
_walCheckpointTimer.unref();
|
|
961
|
-
if (process.env.DATABASE_URL) {
|
|
962
|
-
_adapterClient = await createPrismaDbAdapter(_resilientClient);
|
|
1023
|
+
}
|
|
1024
|
+
function findPackageRoot() {
|
|
1025
|
+
let dir = path5.dirname(fileURLToPath(import.meta.url));
|
|
1026
|
+
const { root } = path5.parse(dir);
|
|
1027
|
+
while (dir !== root) {
|
|
1028
|
+
if (existsSync5(path5.join(dir, "package.json"))) return dir;
|
|
1029
|
+
dir = path5.dirname(dir);
|
|
963
1030
|
}
|
|
1031
|
+
return null;
|
|
964
1032
|
}
|
|
965
|
-
function
|
|
966
|
-
if (
|
|
967
|
-
|
|
1033
|
+
function getAvailableMemoryGB() {
|
|
1034
|
+
if (process.platform === "darwin") {
|
|
1035
|
+
try {
|
|
1036
|
+
const { execSync: execSync2 } = __require("child_process");
|
|
1037
|
+
const vmstat = execSync2("vm_stat", { encoding: "utf8" });
|
|
1038
|
+
const pageSize = 16384;
|
|
1039
|
+
const pageSizeMatch = vmstat.match(/page size of (\d+) bytes/);
|
|
1040
|
+
const actualPageSize = pageSizeMatch ? parseInt(pageSizeMatch[1], 10) : pageSize;
|
|
1041
|
+
const free = vmstat.match(/Pages free:\s+(\d+)/);
|
|
1042
|
+
const inactive = vmstat.match(/Pages inactive:\s+(\d+)/);
|
|
1043
|
+
const speculative = vmstat.match(/Pages speculative:\s+(\d+)/);
|
|
1044
|
+
const freePages = free ? parseInt(free[1], 10) : 0;
|
|
1045
|
+
const inactivePages = inactive ? parseInt(inactive[1], 10) : 0;
|
|
1046
|
+
const speculativePages = speculative ? parseInt(speculative[1], 10) : 0;
|
|
1047
|
+
return (freePages + inactivePages + speculativePages) * actualPageSize / (1024 * 1024 * 1024);
|
|
1048
|
+
} catch {
|
|
1049
|
+
return os4.freemem() / (1024 * 1024 * 1024);
|
|
1050
|
+
}
|
|
968
1051
|
}
|
|
969
|
-
|
|
970
|
-
|
|
1052
|
+
return os4.freemem() / (1024 * 1024 * 1024);
|
|
1053
|
+
}
|
|
1054
|
+
function spawnDaemon() {
|
|
1055
|
+
const freeGB = getAvailableMemoryGB();
|
|
1056
|
+
const totalGB = os4.totalmem() / (1024 * 1024 * 1024);
|
|
1057
|
+
if (totalGB <= 8) {
|
|
1058
|
+
process.stderr.write(
|
|
1059
|
+
`[exed-client] SKIP: ${totalGB.toFixed(0)}GB system \u2014 embedding daemon disabled. Using keyword search only. Minimum 16GB recommended for vector search.
|
|
1060
|
+
`
|
|
1061
|
+
);
|
|
1062
|
+
return;
|
|
971
1063
|
}
|
|
972
|
-
if (
|
|
973
|
-
|
|
1064
|
+
if (totalGB <= 16 && freeGB < 2) {
|
|
1065
|
+
process.stderr.write(
|
|
1066
|
+
`[exed-client] SKIP: low memory (${freeGB.toFixed(1)}GB available / ${totalGB.toFixed(0)}GB total). Embedding daemon not started \u2014 using keyword search only.
|
|
1067
|
+
`
|
|
1068
|
+
);
|
|
1069
|
+
return;
|
|
974
1070
|
}
|
|
975
|
-
|
|
976
|
-
|
|
1071
|
+
const pkgRoot = findPackageRoot();
|
|
1072
|
+
if (!pkgRoot) {
|
|
1073
|
+
process.stderr.write("[exed-client] WARN: cannot find package root\n");
|
|
1074
|
+
return;
|
|
1075
|
+
}
|
|
1076
|
+
const daemonPath = path5.join(pkgRoot, "dist", "lib", "exe-daemon.js");
|
|
1077
|
+
if (!existsSync5(daemonPath)) {
|
|
1078
|
+
process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
|
|
1079
|
+
`);
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
const resolvedPath = daemonPath;
|
|
1083
|
+
const daemonToken = ensureDaemonToken(process.env[DAEMON_TOKEN_ENV] ?? null);
|
|
1084
|
+
process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
|
|
1085
|
+
`);
|
|
1086
|
+
const logPath = path5.join(path5.dirname(SOCKET_PATH), "exed.log");
|
|
1087
|
+
let stderrFd = "ignore";
|
|
1088
|
+
try {
|
|
1089
|
+
stderrFd = openSync(logPath, "a");
|
|
1090
|
+
} catch {
|
|
1091
|
+
}
|
|
1092
|
+
const heapCapMB = totalGB <= 8 ? 256 : 512;
|
|
1093
|
+
const nodeArgs = [`--max-old-space-size=${heapCapMB}`, resolvedPath];
|
|
1094
|
+
const child = spawn(process.execPath, nodeArgs, {
|
|
1095
|
+
detached: true,
|
|
1096
|
+
stdio: ["ignore", "ignore", stderrFd],
|
|
1097
|
+
env: {
|
|
1098
|
+
...process.env,
|
|
1099
|
+
TMUX: void 0,
|
|
1100
|
+
// Daemon is global — must not inherit session scope
|
|
1101
|
+
TMUX_PANE: void 0,
|
|
1102
|
+
// Prevents resolveExeSession() from scoping to one session
|
|
1103
|
+
EXE_DAEMON_SOCK: SOCKET_PATH,
|
|
1104
|
+
EXE_DAEMON_PID: PID_PATH,
|
|
1105
|
+
[DAEMON_TOKEN_ENV]: daemonToken
|
|
1106
|
+
}
|
|
1107
|
+
});
|
|
1108
|
+
child.unref();
|
|
1109
|
+
if (typeof stderrFd === "number") {
|
|
1110
|
+
try {
|
|
1111
|
+
closeSync(stderrFd);
|
|
1112
|
+
} catch {
|
|
1113
|
+
}
|
|
977
1114
|
}
|
|
978
|
-
return _resilientClient;
|
|
979
1115
|
}
|
|
980
|
-
function
|
|
981
|
-
|
|
982
|
-
|
|
1116
|
+
function acquireSpawnLock() {
|
|
1117
|
+
try {
|
|
1118
|
+
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
1119
|
+
closeSync(fd);
|
|
1120
|
+
return true;
|
|
1121
|
+
} catch {
|
|
1122
|
+
try {
|
|
1123
|
+
const stat2 = statSync(SPAWN_LOCK_PATH);
|
|
1124
|
+
if (Date.now() - stat2.mtimeMs > SPAWN_LOCK_STALE_MS) {
|
|
1125
|
+
try {
|
|
1126
|
+
unlinkSync2(SPAWN_LOCK_PATH);
|
|
1127
|
+
} catch {
|
|
1128
|
+
}
|
|
1129
|
+
try {
|
|
1130
|
+
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
1131
|
+
closeSync(fd);
|
|
1132
|
+
return true;
|
|
1133
|
+
} catch {
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
} catch {
|
|
1137
|
+
}
|
|
1138
|
+
return false;
|
|
983
1139
|
}
|
|
984
|
-
return _client;
|
|
985
1140
|
}
|
|
986
|
-
|
|
987
|
-
const client = getRawClient();
|
|
988
|
-
await client.execute("PRAGMA journal_mode = WAL");
|
|
989
|
-
await client.execute("PRAGMA busy_timeout = 30000");
|
|
990
|
-
await client.execute("PRAGMA wal_autocheckpoint = 1000");
|
|
1141
|
+
function releaseSpawnLock() {
|
|
991
1142
|
try {
|
|
992
|
-
|
|
1143
|
+
unlinkSync2(SPAWN_LOCK_PATH);
|
|
993
1144
|
} catch {
|
|
994
1145
|
}
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent_project
|
|
1029
|
-
ON memories(agent_id, project_name);
|
|
1030
|
-
`);
|
|
1031
|
-
await client.executeMultiple(`
|
|
1032
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
1033
|
-
raw_text,
|
|
1034
|
-
content='memories',
|
|
1035
|
-
content_rowid='rowid'
|
|
1036
|
-
);
|
|
1037
|
-
|
|
1038
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
1039
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
1040
|
-
END;
|
|
1041
|
-
|
|
1042
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
1043
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
1044
|
-
END;
|
|
1045
|
-
|
|
1046
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
1047
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
1048
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
1049
|
-
END;
|
|
1050
|
-
`);
|
|
1051
|
-
await client.executeMultiple(`
|
|
1052
|
-
CREATE TABLE IF NOT EXISTS sync_meta (
|
|
1053
|
-
key TEXT PRIMARY KEY,
|
|
1054
|
-
value TEXT NOT NULL
|
|
1055
|
-
);
|
|
1056
|
-
`);
|
|
1057
|
-
await client.executeMultiple(`
|
|
1058
|
-
CREATE TABLE IF NOT EXISTS tasks (
|
|
1059
|
-
id TEXT PRIMARY KEY,
|
|
1060
|
-
title TEXT NOT NULL,
|
|
1061
|
-
assigned_to TEXT NOT NULL,
|
|
1062
|
-
assigned_by TEXT NOT NULL,
|
|
1063
|
-
project_name TEXT NOT NULL,
|
|
1064
|
-
priority TEXT NOT NULL DEFAULT 'p1',
|
|
1065
|
-
status TEXT NOT NULL DEFAULT 'open',
|
|
1066
|
-
task_file TEXT,
|
|
1067
|
-
created_at TEXT NOT NULL,
|
|
1068
|
-
updated_at TEXT NOT NULL
|
|
1069
|
-
);
|
|
1070
|
-
|
|
1071
|
-
CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
|
|
1072
|
-
ON tasks(assigned_to, status);
|
|
1073
|
-
`);
|
|
1074
|
-
await client.executeMultiple(`
|
|
1075
|
-
CREATE TABLE IF NOT EXISTS behaviors (
|
|
1076
|
-
id TEXT PRIMARY KEY,
|
|
1077
|
-
agent_id TEXT NOT NULL,
|
|
1078
|
-
project_name TEXT,
|
|
1079
|
-
domain TEXT,
|
|
1080
|
-
content TEXT NOT NULL,
|
|
1081
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
1082
|
-
created_at TEXT NOT NULL,
|
|
1083
|
-
updated_at TEXT NOT NULL
|
|
1084
|
-
);
|
|
1085
|
-
|
|
1086
|
-
CREATE INDEX IF NOT EXISTS idx_behaviors_agent
|
|
1087
|
-
ON behaviors(agent_id, active);
|
|
1088
|
-
`);
|
|
1089
|
-
try {
|
|
1090
|
-
const coordinatorName = getCoordinatorName();
|
|
1091
|
-
const existing = await client.execute({
|
|
1092
|
-
sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = ?",
|
|
1093
|
-
args: [coordinatorName]
|
|
1146
|
+
}
|
|
1147
|
+
function connectToSocket() {
|
|
1148
|
+
return new Promise((resolve) => {
|
|
1149
|
+
if (_socket && _connected) {
|
|
1150
|
+
resolve(true);
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
const socket = net.createConnection({ path: SOCKET_PATH });
|
|
1154
|
+
const connectTimeout = setTimeout(() => {
|
|
1155
|
+
socket.destroy();
|
|
1156
|
+
resolve(false);
|
|
1157
|
+
}, 2e3);
|
|
1158
|
+
socket.on("connect", () => {
|
|
1159
|
+
clearTimeout(connectTimeout);
|
|
1160
|
+
_socket = socket;
|
|
1161
|
+
_connected = true;
|
|
1162
|
+
_buffer = "";
|
|
1163
|
+
socket.on("data", handleData);
|
|
1164
|
+
socket.on("close", () => {
|
|
1165
|
+
_connected = false;
|
|
1166
|
+
_socket = null;
|
|
1167
|
+
for (const [id, entry] of _pending) {
|
|
1168
|
+
clearTimeout(entry.timer);
|
|
1169
|
+
_pending.delete(id);
|
|
1170
|
+
entry.resolve({ error: "Connection closed" });
|
|
1171
|
+
}
|
|
1172
|
+
});
|
|
1173
|
+
socket.on("error", () => {
|
|
1174
|
+
_connected = false;
|
|
1175
|
+
_socket = null;
|
|
1176
|
+
});
|
|
1177
|
+
resolve(true);
|
|
1094
1178
|
});
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1179
|
+
socket.on("error", () => {
|
|
1180
|
+
clearTimeout(connectTimeout);
|
|
1181
|
+
resolve(false);
|
|
1182
|
+
});
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
async function connectEmbedDaemon() {
|
|
1186
|
+
if (_socket && _connected) return true;
|
|
1187
|
+
if (await connectToSocket()) return true;
|
|
1188
|
+
if (acquireSpawnLock()) {
|
|
1189
|
+
try {
|
|
1190
|
+
cleanupStaleFiles();
|
|
1191
|
+
spawnDaemon();
|
|
1192
|
+
} finally {
|
|
1193
|
+
releaseSpawnLock();
|
|
1108
1194
|
}
|
|
1109
|
-
} catch {
|
|
1110
1195
|
}
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1196
|
+
const start = Date.now();
|
|
1197
|
+
let delay2 = 100;
|
|
1198
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1199
|
+
await new Promise((r) => setTimeout(r, delay2));
|
|
1200
|
+
if (await connectToSocket()) return true;
|
|
1201
|
+
delay2 = Math.min(delay2 * 2, 3e3);
|
|
1117
1202
|
}
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1203
|
+
return false;
|
|
1204
|
+
}
|
|
1205
|
+
function sendRequest(texts, priority) {
|
|
1206
|
+
return sendDaemonRequest({ texts, priority });
|
|
1207
|
+
}
|
|
1208
|
+
function sendDaemonRequest(payload, timeoutMs = REQUEST_TIMEOUT_MS) {
|
|
1209
|
+
return new Promise((resolve) => {
|
|
1210
|
+
if (!_socket || !_connected) {
|
|
1211
|
+
resolve({ error: "Not connected" });
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
const id = randomUUID();
|
|
1215
|
+
const token = process.env[DAEMON_TOKEN_ENV] ?? readDaemonToken();
|
|
1216
|
+
const timer = setTimeout(() => {
|
|
1217
|
+
_pending.delete(id);
|
|
1218
|
+
resolve({ error: "Request timeout" });
|
|
1219
|
+
}, timeoutMs);
|
|
1220
|
+
_pending.set(id, { resolve, timer });
|
|
1221
|
+
try {
|
|
1222
|
+
_socket.write(JSON.stringify({ id, token, ...payload }) + "\n");
|
|
1223
|
+
} catch {
|
|
1224
|
+
clearTimeout(timer);
|
|
1225
|
+
_pending.delete(id);
|
|
1226
|
+
resolve({ error: "Write failed" });
|
|
1227
|
+
}
|
|
1228
|
+
});
|
|
1229
|
+
}
|
|
1230
|
+
async function pingDaemon() {
|
|
1231
|
+
if (!_socket || !_connected) return null;
|
|
1232
|
+
const response = await sendDaemonRequest({ type: "health" }, 5e3);
|
|
1233
|
+
if (response.health) {
|
|
1234
|
+
return response.health;
|
|
1235
|
+
}
|
|
1236
|
+
return null;
|
|
1237
|
+
}
|
|
1238
|
+
function killAndRespawnDaemon() {
|
|
1239
|
+
if (!acquireSpawnLock()) {
|
|
1240
|
+
process.stderr.write("[exed-client] Another process is already restarting daemon \u2014 skipping\n");
|
|
1241
|
+
if (_socket) {
|
|
1242
|
+
_socket.destroy();
|
|
1243
|
+
_socket = null;
|
|
1244
|
+
}
|
|
1245
|
+
_connected = false;
|
|
1246
|
+
_buffer = "";
|
|
1247
|
+
return;
|
|
1124
1248
|
}
|
|
1125
1249
|
try {
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1250
|
+
process.stderr.write("[exed-client] Killing daemon for restart...\n");
|
|
1251
|
+
if (existsSync5(PID_PATH)) {
|
|
1252
|
+
try {
|
|
1253
|
+
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
1254
|
+
if (pid > 0) {
|
|
1255
|
+
try {
|
|
1256
|
+
process.kill(pid, "SIGKILL");
|
|
1257
|
+
} catch {
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
} catch {
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
if (_socket) {
|
|
1264
|
+
_socket.destroy();
|
|
1265
|
+
_socket = null;
|
|
1266
|
+
}
|
|
1267
|
+
_connected = false;
|
|
1268
|
+
_buffer = "";
|
|
1269
|
+
try {
|
|
1270
|
+
unlinkSync2(PID_PATH);
|
|
1271
|
+
} catch {
|
|
1272
|
+
}
|
|
1273
|
+
try {
|
|
1274
|
+
unlinkSync2(SOCKET_PATH);
|
|
1275
|
+
} catch {
|
|
1276
|
+
}
|
|
1277
|
+
spawnDaemon();
|
|
1278
|
+
} finally {
|
|
1279
|
+
releaseSpawnLock();
|
|
1131
1280
|
}
|
|
1281
|
+
}
|
|
1282
|
+
function isDaemonTooYoung() {
|
|
1132
1283
|
try {
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
ON tasks(parent_task_id)
|
|
1136
|
-
WHERE parent_task_id IS NOT NULL`,
|
|
1137
|
-
args: []
|
|
1138
|
-
});
|
|
1284
|
+
const stat2 = statSync(PID_PATH);
|
|
1285
|
+
return Date.now() - stat2.mtimeMs < MIN_DAEMON_AGE_MS;
|
|
1139
1286
|
} catch {
|
|
1287
|
+
return false;
|
|
1140
1288
|
}
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
}
|
|
1148
|
-
try {
|
|
1149
|
-
await client.execute({
|
|
1150
|
-
sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
|
|
1151
|
-
args: []
|
|
1152
|
-
});
|
|
1153
|
-
} catch {
|
|
1289
|
+
}
|
|
1290
|
+
async function retryThenRestart(doRequest, label) {
|
|
1291
|
+
const result = await doRequest();
|
|
1292
|
+
if (!result.error) {
|
|
1293
|
+
_consecutiveFailures = 0;
|
|
1294
|
+
return result;
|
|
1154
1295
|
}
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1296
|
+
_consecutiveFailures++;
|
|
1297
|
+
for (let i = 0; i < MAX_RETRIES_BEFORE_RESTART; i++) {
|
|
1298
|
+
const delayMs = RETRY_DELAYS_MS[i] ?? 5e3;
|
|
1299
|
+
process.stderr.write(`[exed-client] ${label} failed (${result.error}), retry ${i + 1}/${MAX_RETRIES_BEFORE_RESTART} in ${delayMs}ms
|
|
1300
|
+
`);
|
|
1301
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
1302
|
+
if (!_connected) {
|
|
1303
|
+
if (!await connectToSocket()) continue;
|
|
1304
|
+
}
|
|
1305
|
+
const retry = await doRequest();
|
|
1306
|
+
if (!retry.error) {
|
|
1307
|
+
_consecutiveFailures = 0;
|
|
1308
|
+
return retry;
|
|
1309
|
+
}
|
|
1310
|
+
_consecutiveFailures++;
|
|
1161
1311
|
}
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
});
|
|
1167
|
-
} catch {
|
|
1312
|
+
if (isDaemonTooYoung()) {
|
|
1313
|
+
process.stderr.write(`[exed-client] ${label}: daemon too young (< ${MIN_DAEMON_AGE_MS / 1e3}s) \u2014 skipping restart
|
|
1314
|
+
`);
|
|
1315
|
+
return { error: result.error };
|
|
1168
1316
|
}
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1317
|
+
process.stderr.write(`[exed-client] ${label}: ${_consecutiveFailures} consecutive failures \u2014 restarting daemon
|
|
1318
|
+
`);
|
|
1319
|
+
killAndRespawnDaemon();
|
|
1320
|
+
const start = Date.now();
|
|
1321
|
+
let delay2 = 200;
|
|
1322
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1323
|
+
await new Promise((r) => setTimeout(r, delay2));
|
|
1324
|
+
if (await connectToSocket()) break;
|
|
1325
|
+
delay2 = Math.min(delay2 * 2, 3e3);
|
|
1175
1326
|
}
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1327
|
+
if (!_connected) return { error: "Daemon restart failed" };
|
|
1328
|
+
const final = await doRequest();
|
|
1329
|
+
if (!final.error) _consecutiveFailures = 0;
|
|
1330
|
+
return final;
|
|
1331
|
+
}
|
|
1332
|
+
async function embedViaClient(text, priority = "high") {
|
|
1333
|
+
if (!_connected && !await connectEmbedDaemon()) return null;
|
|
1334
|
+
_requestCount++;
|
|
1335
|
+
if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
|
|
1336
|
+
const health = await pingDaemon();
|
|
1337
|
+
if (!health && !isDaemonTooYoung()) {
|
|
1338
|
+
process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
|
|
1339
|
+
`);
|
|
1340
|
+
killAndRespawnDaemon();
|
|
1341
|
+
const start = Date.now();
|
|
1342
|
+
let d = 200;
|
|
1343
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1344
|
+
await new Promise((r) => setTimeout(r, d));
|
|
1345
|
+
if (await connectToSocket()) break;
|
|
1346
|
+
d = Math.min(d * 2, 3e3);
|
|
1347
|
+
}
|
|
1348
|
+
if (!_connected) return null;
|
|
1349
|
+
}
|
|
1182
1350
|
}
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1351
|
+
const result = await retryThenRestart(
|
|
1352
|
+
() => sendRequest([text], priority),
|
|
1353
|
+
"Embed"
|
|
1354
|
+
);
|
|
1355
|
+
return !result.error && result.vectors?.[0] ? result.vectors[0] : null;
|
|
1356
|
+
}
|
|
1357
|
+
function isClientConnected() {
|
|
1358
|
+
return _connected;
|
|
1359
|
+
}
|
|
1360
|
+
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;
|
|
1361
|
+
var init_exe_daemon_client = __esm({
|
|
1362
|
+
"src/lib/exe-daemon-client.ts"() {
|
|
1363
|
+
"use strict";
|
|
1364
|
+
init_config();
|
|
1365
|
+
init_daemon_auth();
|
|
1366
|
+
SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path5.join(EXE_AI_DIR, "exed.sock");
|
|
1367
|
+
PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path5.join(EXE_AI_DIR, "exed.pid");
|
|
1368
|
+
SPAWN_LOCK_PATH = path5.join(EXE_AI_DIR, "exed-spawn.lock");
|
|
1369
|
+
SPAWN_LOCK_STALE_MS = 3e4;
|
|
1370
|
+
CONNECT_TIMEOUT_MS = 15e3;
|
|
1371
|
+
REQUEST_TIMEOUT_MS = 3e4;
|
|
1372
|
+
DAEMON_TOKEN_ENV = "EXE_DAEMON_TOKEN";
|
|
1373
|
+
_socket = null;
|
|
1374
|
+
_connected = false;
|
|
1375
|
+
_buffer = "";
|
|
1376
|
+
_requestCount = 0;
|
|
1377
|
+
_consecutiveFailures = 0;
|
|
1378
|
+
HEALTH_CHECK_INTERVAL = 100;
|
|
1379
|
+
MAX_RETRIES_BEFORE_RESTART = 3;
|
|
1380
|
+
RETRY_DELAYS_MS = [1e3, 3e3, 5e3];
|
|
1381
|
+
MIN_DAEMON_AGE_MS = 3e4;
|
|
1382
|
+
_pending = /* @__PURE__ */ new Map();
|
|
1383
|
+
MAX_BUFFER = 1e7;
|
|
1189
1384
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1385
|
+
});
|
|
1386
|
+
|
|
1387
|
+
// src/lib/daemon-protocol.ts
|
|
1388
|
+
function serializeValue(v) {
|
|
1389
|
+
if (v === null || v === void 0) return null;
|
|
1390
|
+
if (typeof v === "bigint") return Number(v);
|
|
1391
|
+
if (typeof v === "boolean") return v ? 1 : 0;
|
|
1392
|
+
if (v instanceof Uint8Array) {
|
|
1393
|
+
return { __blob: Buffer.from(v).toString("base64") };
|
|
1394
|
+
}
|
|
1395
|
+
if (ArrayBuffer.isView(v)) {
|
|
1396
|
+
return { __blob: Buffer.from(v.buffer, v.byteOffset, v.byteLength).toString("base64") };
|
|
1397
|
+
}
|
|
1398
|
+
if (v instanceof ArrayBuffer) {
|
|
1399
|
+
return { __blob: Buffer.from(v).toString("base64") };
|
|
1400
|
+
}
|
|
1401
|
+
if (typeof v === "string" || typeof v === "number") return v;
|
|
1402
|
+
return String(v);
|
|
1403
|
+
}
|
|
1404
|
+
function deserializeValue(v) {
|
|
1405
|
+
if (v === null) return null;
|
|
1406
|
+
if (typeof v === "object" && v !== null && "__blob" in v) {
|
|
1407
|
+
const buf = Buffer.from(v.__blob, "base64");
|
|
1408
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
|
1196
1409
|
}
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1410
|
+
return v;
|
|
1411
|
+
}
|
|
1412
|
+
function deserializeResultSet(srs) {
|
|
1413
|
+
const rows = srs.rows.map((obj) => {
|
|
1414
|
+
const values = srs.columns.map(
|
|
1415
|
+
(col) => deserializeValue(obj[col] ?? null)
|
|
1416
|
+
);
|
|
1417
|
+
const row = values;
|
|
1418
|
+
for (let i = 0; i < srs.columns.length; i++) {
|
|
1419
|
+
const col = srs.columns[i];
|
|
1420
|
+
if (col !== void 0) {
|
|
1421
|
+
row[col] = values[i] ?? null;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
Object.defineProperty(row, "length", {
|
|
1425
|
+
value: values.length,
|
|
1426
|
+
enumerable: false
|
|
1201
1427
|
});
|
|
1202
|
-
|
|
1428
|
+
return row;
|
|
1429
|
+
});
|
|
1430
|
+
return {
|
|
1431
|
+
columns: srs.columns,
|
|
1432
|
+
columnTypes: srs.columnTypes ?? [],
|
|
1433
|
+
rows,
|
|
1434
|
+
rowsAffected: srs.rowsAffected,
|
|
1435
|
+
lastInsertRowid: srs.lastInsertRowid != null ? BigInt(srs.lastInsertRowid) : void 0,
|
|
1436
|
+
toJSON: () => ({
|
|
1437
|
+
columns: srs.columns,
|
|
1438
|
+
columnTypes: srs.columnTypes ?? [],
|
|
1439
|
+
rows: srs.rows,
|
|
1440
|
+
rowsAffected: srs.rowsAffected,
|
|
1441
|
+
lastInsertRowid: srs.lastInsertRowid
|
|
1442
|
+
})
|
|
1443
|
+
};
|
|
1444
|
+
}
|
|
1445
|
+
var init_daemon_protocol = __esm({
|
|
1446
|
+
"src/lib/daemon-protocol.ts"() {
|
|
1447
|
+
"use strict";
|
|
1203
1448
|
}
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1449
|
+
});
|
|
1450
|
+
|
|
1451
|
+
// src/lib/db-daemon-client.ts
|
|
1452
|
+
var db_daemon_client_exports = {};
|
|
1453
|
+
__export(db_daemon_client_exports, {
|
|
1454
|
+
createDaemonDbClient: () => createDaemonDbClient,
|
|
1455
|
+
initDaemonDbClient: () => initDaemonDbClient
|
|
1456
|
+
});
|
|
1457
|
+
function normalizeStatement2(stmt) {
|
|
1458
|
+
if (typeof stmt === "string") {
|
|
1459
|
+
return { sql: stmt, args: [] };
|
|
1210
1460
|
}
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1461
|
+
const sql = stmt.sql;
|
|
1462
|
+
let args = [];
|
|
1463
|
+
if (Array.isArray(stmt.args)) {
|
|
1464
|
+
args = stmt.args.map((v) => serializeValue(v));
|
|
1465
|
+
} else if (stmt.args && typeof stmt.args === "object") {
|
|
1466
|
+
const named = {};
|
|
1467
|
+
for (const [key, val] of Object.entries(stmt.args)) {
|
|
1468
|
+
named[key] = serializeValue(val);
|
|
1469
|
+
}
|
|
1470
|
+
return { sql, args: named };
|
|
1217
1471
|
}
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1472
|
+
return { sql, args };
|
|
1473
|
+
}
|
|
1474
|
+
function createDaemonDbClient(fallbackClient) {
|
|
1475
|
+
let _useDaemon = false;
|
|
1476
|
+
const client = {
|
|
1477
|
+
async execute(stmt) {
|
|
1478
|
+
if (!_useDaemon || !isClientConnected()) {
|
|
1479
|
+
return fallbackClient.execute(stmt);
|
|
1480
|
+
}
|
|
1481
|
+
const { sql, args } = normalizeStatement2(stmt);
|
|
1482
|
+
const response = await sendDaemonRequest({
|
|
1483
|
+
type: "db-execute",
|
|
1484
|
+
sql,
|
|
1485
|
+
args
|
|
1486
|
+
});
|
|
1487
|
+
if (response.error) {
|
|
1488
|
+
const errMsg = String(response.error);
|
|
1489
|
+
if (errMsg === "Not connected" || errMsg === "Request timeout" || errMsg === "Write failed" || errMsg === "DB not initialized") {
|
|
1490
|
+
process.stderr.write(`[db-daemon] Transport error (${errMsg}), falling back to direct
|
|
1491
|
+
`);
|
|
1492
|
+
return fallbackClient.execute(stmt);
|
|
1493
|
+
}
|
|
1494
|
+
throw new Error(errMsg);
|
|
1495
|
+
}
|
|
1496
|
+
if (response.db) {
|
|
1497
|
+
return deserializeResultSet(response.db);
|
|
1498
|
+
}
|
|
1499
|
+
process.stderr.write("[db-daemon] Unexpected response shape, falling back to direct\n");
|
|
1500
|
+
return fallbackClient.execute(stmt);
|
|
1501
|
+
},
|
|
1502
|
+
async batch(stmts, mode) {
|
|
1503
|
+
if (!_useDaemon || !isClientConnected()) {
|
|
1504
|
+
return fallbackClient.batch(stmts, mode);
|
|
1505
|
+
}
|
|
1506
|
+
const statements = stmts.map(normalizeStatement2);
|
|
1507
|
+
const response = await sendDaemonRequest({
|
|
1508
|
+
type: "db-batch",
|
|
1509
|
+
statements,
|
|
1510
|
+
mode: mode ?? "deferred"
|
|
1511
|
+
});
|
|
1512
|
+
if (response.error) {
|
|
1513
|
+
const errMsg = String(response.error);
|
|
1514
|
+
if (errMsg === "Not connected" || errMsg === "Request timeout" || errMsg === "Write failed" || errMsg === "DB not initialized") {
|
|
1515
|
+
process.stderr.write(`[db-daemon] Batch transport error (${errMsg}), falling back to direct
|
|
1516
|
+
`);
|
|
1517
|
+
return fallbackClient.batch(stmts, mode);
|
|
1518
|
+
}
|
|
1519
|
+
throw new Error(errMsg);
|
|
1520
|
+
}
|
|
1521
|
+
const batchResults = response["db-batch"];
|
|
1522
|
+
if (batchResults) {
|
|
1523
|
+
return batchResults.map(deserializeResultSet);
|
|
1524
|
+
}
|
|
1525
|
+
process.stderr.write("[db-daemon] Unexpected batch response shape, falling back to direct\n");
|
|
1526
|
+
return fallbackClient.batch(stmts, mode);
|
|
1527
|
+
},
|
|
1528
|
+
// Transaction support — delegate to fallback (transactions need direct connection)
|
|
1529
|
+
async transaction(mode) {
|
|
1530
|
+
return fallbackClient.transaction(mode);
|
|
1531
|
+
},
|
|
1532
|
+
// executeMultiple — delegate to fallback (used only for schema migrations)
|
|
1533
|
+
async executeMultiple(sql) {
|
|
1534
|
+
return fallbackClient.executeMultiple(sql);
|
|
1535
|
+
},
|
|
1536
|
+
// migrate — delegate to fallback
|
|
1537
|
+
async migrate(stmts) {
|
|
1538
|
+
return fallbackClient.migrate(stmts);
|
|
1539
|
+
},
|
|
1540
|
+
// Sync mode — delegate to fallback
|
|
1541
|
+
sync() {
|
|
1542
|
+
return fallbackClient.sync();
|
|
1543
|
+
},
|
|
1544
|
+
close() {
|
|
1545
|
+
_useDaemon = false;
|
|
1546
|
+
},
|
|
1547
|
+
get closed() {
|
|
1548
|
+
return fallbackClient.closed;
|
|
1549
|
+
},
|
|
1550
|
+
get protocol() {
|
|
1551
|
+
return fallbackClient.protocol;
|
|
1552
|
+
}
|
|
1553
|
+
};
|
|
1554
|
+
return {
|
|
1555
|
+
...client,
|
|
1556
|
+
/** Enable daemon routing (call after confirming daemon is connected) */
|
|
1557
|
+
_enableDaemon() {
|
|
1558
|
+
_useDaemon = true;
|
|
1559
|
+
},
|
|
1560
|
+
/** Check if daemon routing is active */
|
|
1561
|
+
_isDaemonActive() {
|
|
1562
|
+
return _useDaemon && isClientConnected();
|
|
1563
|
+
}
|
|
1564
|
+
};
|
|
1565
|
+
}
|
|
1566
|
+
async function initDaemonDbClient(fallbackClient) {
|
|
1567
|
+
if (process.env.EXE_IS_DAEMON === "1") return null;
|
|
1568
|
+
const connected = await connectEmbedDaemon();
|
|
1569
|
+
if (!connected) {
|
|
1570
|
+
process.stderr.write("[db-daemon] Daemon unavailable \u2014 using direct SQLite\n");
|
|
1571
|
+
return null;
|
|
1224
1572
|
}
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1573
|
+
const client = createDaemonDbClient(fallbackClient);
|
|
1574
|
+
client._enableDaemon();
|
|
1575
|
+
process.stderr.write("[db-daemon] DB routing through daemon (single-writer)\n");
|
|
1576
|
+
return client;
|
|
1577
|
+
}
|
|
1578
|
+
var init_db_daemon_client = __esm({
|
|
1579
|
+
"src/lib/db-daemon-client.ts"() {
|
|
1580
|
+
"use strict";
|
|
1581
|
+
init_exe_daemon_client();
|
|
1582
|
+
init_daemon_protocol();
|
|
1231
1583
|
}
|
|
1232
|
-
|
|
1233
|
-
CREATE TABLE IF NOT EXISTS consolidations (
|
|
1234
|
-
id TEXT PRIMARY KEY,
|
|
1235
|
-
consolidated_memory_id TEXT NOT NULL,
|
|
1236
|
-
source_memory_id TEXT NOT NULL,
|
|
1237
|
-
created_at TEXT NOT NULL
|
|
1238
|
-
);
|
|
1239
|
-
|
|
1240
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
1241
|
-
ON consolidations(source_memory_id);
|
|
1584
|
+
});
|
|
1242
1585
|
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1586
|
+
// src/lib/database.ts
|
|
1587
|
+
var database_exports = {};
|
|
1588
|
+
__export(database_exports, {
|
|
1589
|
+
disposeDatabase: () => disposeDatabase,
|
|
1590
|
+
disposeTurso: () => disposeTurso,
|
|
1591
|
+
ensureSchema: () => ensureSchema,
|
|
1592
|
+
getClient: () => getClient,
|
|
1593
|
+
getRawClient: () => getRawClient,
|
|
1594
|
+
initDaemonClient: () => initDaemonClient,
|
|
1595
|
+
initDatabase: () => initDatabase,
|
|
1596
|
+
initTurso: () => initTurso,
|
|
1597
|
+
isInitialized: () => isInitialized
|
|
1598
|
+
});
|
|
1599
|
+
import { createClient } from "@libsql/client";
|
|
1600
|
+
async function initDatabase(config) {
|
|
1601
|
+
if (_walCheckpointTimer) {
|
|
1602
|
+
clearInterval(_walCheckpointTimer);
|
|
1603
|
+
_walCheckpointTimer = null;
|
|
1604
|
+
}
|
|
1605
|
+
if (_daemonClient) {
|
|
1606
|
+
_daemonClient.close();
|
|
1607
|
+
_daemonClient = null;
|
|
1608
|
+
}
|
|
1609
|
+
if (_adapterClient && _adapterClient !== _resilientClient) {
|
|
1610
|
+
_adapterClient.close();
|
|
1611
|
+
}
|
|
1612
|
+
_adapterClient = null;
|
|
1613
|
+
if (_client) {
|
|
1614
|
+
_client.close();
|
|
1615
|
+
_client = null;
|
|
1616
|
+
_resilientClient = null;
|
|
1617
|
+
}
|
|
1618
|
+
const opts = {
|
|
1619
|
+
url: `file:${config.dbPath}`
|
|
1620
|
+
};
|
|
1621
|
+
if (config.encryptionKey) {
|
|
1622
|
+
opts.encryptionKey = config.encryptionKey;
|
|
1623
|
+
}
|
|
1624
|
+
_client = createClient(opts);
|
|
1625
|
+
_resilientClient = wrapWithRetry(_client);
|
|
1626
|
+
_adapterClient = _resilientClient;
|
|
1627
|
+
_client.execute("PRAGMA busy_timeout = 30000").catch(() => {
|
|
1628
|
+
});
|
|
1629
|
+
_client.execute("PRAGMA journal_mode = WAL").catch(() => {
|
|
1630
|
+
});
|
|
1631
|
+
if (_walCheckpointTimer) clearInterval(_walCheckpointTimer);
|
|
1632
|
+
_walCheckpointTimer = setInterval(() => {
|
|
1633
|
+
_client?.execute("PRAGMA wal_checkpoint(PASSIVE)").catch(() => {
|
|
1634
|
+
});
|
|
1635
|
+
}, 3e4);
|
|
1636
|
+
_walCheckpointTimer.unref();
|
|
1637
|
+
if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") {
|
|
1638
|
+
_adapterClient = await createPrismaDbAdapter(_resilientClient);
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
function isInitialized() {
|
|
1642
|
+
return _adapterClient !== null || _client !== null;
|
|
1643
|
+
}
|
|
1644
|
+
function getClient() {
|
|
1645
|
+
if (!_adapterClient) {
|
|
1646
|
+
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
1647
|
+
}
|
|
1648
|
+
if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") {
|
|
1649
|
+
return _adapterClient;
|
|
1650
|
+
}
|
|
1651
|
+
if (process.env.EXE_IS_DAEMON === "1") {
|
|
1652
|
+
return _resilientClient;
|
|
1653
|
+
}
|
|
1654
|
+
if (_daemonClient && _daemonClient._isDaemonActive()) {
|
|
1655
|
+
return _daemonClient;
|
|
1656
|
+
}
|
|
1657
|
+
return _resilientClient;
|
|
1658
|
+
}
|
|
1659
|
+
async function initDaemonClient() {
|
|
1660
|
+
if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") return;
|
|
1661
|
+
if (process.env.EXE_IS_DAEMON === "1") return;
|
|
1662
|
+
if (process.env.VITEST) return;
|
|
1663
|
+
if (!_resilientClient) return;
|
|
1664
|
+
if (_daemonClient) return;
|
|
1665
|
+
try {
|
|
1666
|
+
const { initDaemonDbClient: initDaemonDbClient2 } = await Promise.resolve().then(() => (init_db_daemon_client(), db_daemon_client_exports));
|
|
1667
|
+
_daemonClient = await initDaemonDbClient2(_resilientClient);
|
|
1668
|
+
} catch (err) {
|
|
1669
|
+
process.stderr.write(
|
|
1670
|
+
`[database] Daemon client init failed (non-fatal): ${err instanceof Error ? err.message : String(err)}
|
|
1671
|
+
`
|
|
1672
|
+
);
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
function getRawClient() {
|
|
1676
|
+
if (!_client) {
|
|
1677
|
+
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
1678
|
+
}
|
|
1679
|
+
return _client;
|
|
1680
|
+
}
|
|
1681
|
+
async function ensureSchema() {
|
|
1682
|
+
const client = getRawClient();
|
|
1683
|
+
await client.execute("PRAGMA journal_mode = WAL");
|
|
1684
|
+
await client.execute("PRAGMA busy_timeout = 30000");
|
|
1685
|
+
await client.execute("PRAGMA wal_autocheckpoint = 1000");
|
|
1686
|
+
try {
|
|
1687
|
+
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
1688
|
+
} catch {
|
|
1689
|
+
}
|
|
1690
|
+
await client.executeMultiple(`
|
|
1691
|
+
CREATE TABLE IF NOT EXISTS memories (
|
|
1692
|
+
id TEXT PRIMARY KEY,
|
|
1693
|
+
agent_id TEXT NOT NULL,
|
|
1694
|
+
agent_role TEXT NOT NULL,
|
|
1695
|
+
session_id TEXT NOT NULL,
|
|
1696
|
+
timestamp TEXT NOT NULL,
|
|
1697
|
+
tool_name TEXT NOT NULL,
|
|
1698
|
+
project_name TEXT NOT NULL,
|
|
1699
|
+
has_error INTEGER NOT NULL DEFAULT 0,
|
|
1700
|
+
raw_text TEXT NOT NULL,
|
|
1701
|
+
vector F32_BLOB(1024),
|
|
1702
|
+
version INTEGER NOT NULL DEFAULT 0
|
|
1267
1703
|
);
|
|
1268
1704
|
|
|
1269
|
-
CREATE INDEX IF NOT EXISTS
|
|
1270
|
-
ON
|
|
1705
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent
|
|
1706
|
+
ON memories(agent_id);
|
|
1271
1707
|
|
|
1272
|
-
CREATE INDEX IF NOT EXISTS
|
|
1273
|
-
ON
|
|
1708
|
+
CREATE INDEX IF NOT EXISTS idx_memories_timestamp
|
|
1709
|
+
ON memories(timestamp);
|
|
1274
1710
|
|
|
1275
|
-
CREATE INDEX IF NOT EXISTS
|
|
1276
|
-
ON
|
|
1711
|
+
CREATE INDEX IF NOT EXISTS idx_memories_session
|
|
1712
|
+
ON memories(session_id);
|
|
1713
|
+
|
|
1714
|
+
CREATE INDEX IF NOT EXISTS idx_memories_project
|
|
1715
|
+
ON memories(project_name);
|
|
1716
|
+
|
|
1717
|
+
CREATE INDEX IF NOT EXISTS idx_memories_tool
|
|
1718
|
+
ON memories(tool_name);
|
|
1719
|
+
|
|
1720
|
+
CREATE INDEX IF NOT EXISTS idx_memories_version
|
|
1721
|
+
ON memories(version);
|
|
1722
|
+
|
|
1723
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent_project
|
|
1724
|
+
ON memories(agent_id, project_name);
|
|
1277
1725
|
`);
|
|
1278
1726
|
await client.executeMultiple(`
|
|
1279
|
-
CREATE TABLE IF NOT EXISTS
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
job_type TEXT NOT NULL DEFAULT 'report',
|
|
1284
|
-
prompt TEXT,
|
|
1285
|
-
assigned_to TEXT,
|
|
1286
|
-
project_name TEXT,
|
|
1287
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
1288
|
-
use_crontab INTEGER NOT NULL DEFAULT 0,
|
|
1289
|
-
created_at TEXT NOT NULL
|
|
1727
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
1728
|
+
raw_text,
|
|
1729
|
+
content='memories',
|
|
1730
|
+
content_rowid='rowid'
|
|
1290
1731
|
);
|
|
1732
|
+
|
|
1733
|
+
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
1734
|
+
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
1735
|
+
END;
|
|
1736
|
+
|
|
1737
|
+
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
1738
|
+
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
1739
|
+
END;
|
|
1740
|
+
|
|
1741
|
+
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
1742
|
+
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
1743
|
+
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
1744
|
+
END;
|
|
1291
1745
|
`);
|
|
1292
1746
|
await client.executeMultiple(`
|
|
1293
|
-
CREATE TABLE IF NOT EXISTS
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
hostname TEXT NOT NULL,
|
|
1297
|
-
projects TEXT NOT NULL DEFAULT '[]',
|
|
1298
|
-
agents TEXT NOT NULL DEFAULT '[]',
|
|
1299
|
-
connected INTEGER DEFAULT 0,
|
|
1300
|
-
last_seen TEXT NOT NULL
|
|
1747
|
+
CREATE TABLE IF NOT EXISTS sync_meta (
|
|
1748
|
+
key TEXT PRIMARY KEY,
|
|
1749
|
+
value TEXT NOT NULL
|
|
1301
1750
|
);
|
|
1302
1751
|
`);
|
|
1303
1752
|
await client.executeMultiple(`
|
|
1304
|
-
CREATE TABLE IF NOT EXISTS
|
|
1305
|
-
id
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
server_seq INTEGER,
|
|
1316
|
-
retry_count INTEGER DEFAULT 0,
|
|
1317
|
-
created_at TEXT NOT NULL,
|
|
1318
|
-
delivered_at TEXT,
|
|
1319
|
-
processed_at TEXT,
|
|
1320
|
-
failed_at TEXT,
|
|
1321
|
-
failure_reason TEXT
|
|
1753
|
+
CREATE TABLE IF NOT EXISTS tasks (
|
|
1754
|
+
id TEXT PRIMARY KEY,
|
|
1755
|
+
title TEXT NOT NULL,
|
|
1756
|
+
assigned_to TEXT NOT NULL,
|
|
1757
|
+
assigned_by TEXT NOT NULL,
|
|
1758
|
+
project_name TEXT NOT NULL,
|
|
1759
|
+
priority TEXT NOT NULL DEFAULT 'p1',
|
|
1760
|
+
status TEXT NOT NULL DEFAULT 'open',
|
|
1761
|
+
task_file TEXT,
|
|
1762
|
+
created_at TEXT NOT NULL,
|
|
1763
|
+
updated_at TEXT NOT NULL
|
|
1322
1764
|
);
|
|
1323
1765
|
|
|
1324
|
-
CREATE INDEX IF NOT EXISTS
|
|
1325
|
-
ON
|
|
1766
|
+
CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
|
|
1767
|
+
ON tasks(assigned_to, status);
|
|
1768
|
+
`);
|
|
1769
|
+
await client.executeMultiple(`
|
|
1770
|
+
CREATE TABLE IF NOT EXISTS behaviors (
|
|
1771
|
+
id TEXT PRIMARY KEY,
|
|
1772
|
+
agent_id TEXT NOT NULL,
|
|
1773
|
+
project_name TEXT,
|
|
1774
|
+
domain TEXT,
|
|
1775
|
+
content TEXT NOT NULL,
|
|
1776
|
+
active INTEGER NOT NULL DEFAULT 1,
|
|
1777
|
+
created_at TEXT NOT NULL,
|
|
1778
|
+
updated_at TEXT NOT NULL
|
|
1779
|
+
);
|
|
1326
1780
|
|
|
1327
|
-
CREATE INDEX IF NOT EXISTS
|
|
1328
|
-
ON
|
|
1781
|
+
CREATE INDEX IF NOT EXISTS idx_behaviors_agent
|
|
1782
|
+
ON behaviors(agent_id, active);
|
|
1329
1783
|
`);
|
|
1784
|
+
try {
|
|
1785
|
+
const coordinatorName = getCoordinatorName();
|
|
1786
|
+
const existing = await client.execute({
|
|
1787
|
+
sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = ?",
|
|
1788
|
+
args: [coordinatorName]
|
|
1789
|
+
});
|
|
1790
|
+
if (Number(existing.rows[0]?.cnt) === 0) {
|
|
1791
|
+
const seededAt = "2026-03-25T00:00:00Z";
|
|
1792
|
+
for (const [domain, content] of [
|
|
1793
|
+
["workflow", `Don't ask "keep going?" \u2014 just keep executing phases/plans autonomously`],
|
|
1794
|
+
["tool-use", "Always use create_task MCP tool, never write .md files directly for task creation"],
|
|
1795
|
+
["workflow", "Auto-start reviewing when idle and reviews are pending \u2014 never ask founder for permission"]
|
|
1796
|
+
]) {
|
|
1797
|
+
await client.execute({
|
|
1798
|
+
sql: `INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
1799
|
+
VALUES (hex(randomblob(16)), ?, NULL, ?, ?, 1, ?, ?)`,
|
|
1800
|
+
args: [coordinatorName, domain, content, seededAt, seededAt]
|
|
1801
|
+
});
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
} catch {
|
|
1805
|
+
}
|
|
1330
1806
|
try {
|
|
1331
1807
|
await client.execute({
|
|
1332
|
-
sql: `ALTER TABLE
|
|
1808
|
+
sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
|
|
1333
1809
|
args: []
|
|
1334
1810
|
});
|
|
1335
1811
|
} catch {
|
|
1336
1812
|
}
|
|
1337
1813
|
try {
|
|
1338
1814
|
await client.execute({
|
|
1339
|
-
sql: `ALTER TABLE
|
|
1815
|
+
sql: `ALTER TABLE behaviors ADD COLUMN vector F32_BLOB(${EMBEDDING_DIM})`,
|
|
1340
1816
|
args: []
|
|
1341
1817
|
});
|
|
1342
1818
|
} catch {
|
|
1343
1819
|
}
|
|
1344
|
-
await client.executeMultiple(`
|
|
1345
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_agent_scope_read
|
|
1346
|
-
ON notifications(agent_id, session_scope, read, created_at);
|
|
1347
|
-
|
|
1348
|
-
CREATE INDEX IF NOT EXISTS idx_messages_target_scope_status
|
|
1349
|
-
ON messages(target_agent, session_scope, status, created_at);
|
|
1350
|
-
`);
|
|
1351
1820
|
try {
|
|
1352
1821
|
await client.execute({
|
|
1353
|
-
sql: `
|
|
1822
|
+
sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
|
|
1354
1823
|
args: []
|
|
1355
1824
|
});
|
|
1825
|
+
} catch {
|
|
1826
|
+
}
|
|
1827
|
+
try {
|
|
1356
1828
|
await client.execute({
|
|
1357
|
-
sql: `
|
|
1829
|
+
sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
|
|
1358
1830
|
args: []
|
|
1359
1831
|
});
|
|
1832
|
+
} catch {
|
|
1833
|
+
}
|
|
1834
|
+
try {
|
|
1360
1835
|
await client.execute({
|
|
1361
|
-
sql: `
|
|
1836
|
+
sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
|
|
1837
|
+
ON tasks(parent_task_id)
|
|
1838
|
+
WHERE parent_task_id IS NOT NULL`,
|
|
1362
1839
|
args: []
|
|
1363
1840
|
});
|
|
1841
|
+
} catch {
|
|
1842
|
+
}
|
|
1843
|
+
try {
|
|
1364
1844
|
await client.execute({
|
|
1365
|
-
sql: `UPDATE tasks SET
|
|
1845
|
+
sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
|
|
1846
|
+
args: []
|
|
1847
|
+
});
|
|
1848
|
+
} catch {
|
|
1849
|
+
}
|
|
1850
|
+
try {
|
|
1851
|
+
await client.execute({
|
|
1852
|
+
sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
|
|
1853
|
+
args: []
|
|
1854
|
+
});
|
|
1855
|
+
} catch {
|
|
1856
|
+
}
|
|
1857
|
+
try {
|
|
1858
|
+
await client.execute({
|
|
1859
|
+
sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
|
|
1860
|
+
args: []
|
|
1861
|
+
});
|
|
1862
|
+
} catch {
|
|
1863
|
+
}
|
|
1864
|
+
try {
|
|
1865
|
+
await client.execute({
|
|
1866
|
+
sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
|
|
1867
|
+
args: []
|
|
1868
|
+
});
|
|
1869
|
+
} catch {
|
|
1870
|
+
}
|
|
1871
|
+
try {
|
|
1872
|
+
await client.execute({
|
|
1873
|
+
sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
|
|
1874
|
+
args: []
|
|
1875
|
+
});
|
|
1876
|
+
} catch {
|
|
1877
|
+
}
|
|
1878
|
+
try {
|
|
1879
|
+
await client.execute({
|
|
1880
|
+
sql: `ALTER TABLE tasks ADD COLUMN checkpoint TEXT`,
|
|
1881
|
+
args: []
|
|
1882
|
+
});
|
|
1883
|
+
} catch {
|
|
1884
|
+
}
|
|
1885
|
+
try {
|
|
1886
|
+
await client.execute({
|
|
1887
|
+
sql: `ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER NOT NULL DEFAULT 0`,
|
|
1888
|
+
args: []
|
|
1889
|
+
});
|
|
1890
|
+
} catch {
|
|
1891
|
+
}
|
|
1892
|
+
try {
|
|
1893
|
+
await client.execute({
|
|
1894
|
+
sql: `ALTER TABLE tasks ADD COLUMN complexity TEXT NOT NULL DEFAULT 'standard'`,
|
|
1895
|
+
args: []
|
|
1896
|
+
});
|
|
1897
|
+
} catch {
|
|
1898
|
+
}
|
|
1899
|
+
try {
|
|
1900
|
+
await client.execute({
|
|
1901
|
+
sql: `ALTER TABLE tasks ADD COLUMN session_scope TEXT`,
|
|
1902
|
+
args: []
|
|
1903
|
+
});
|
|
1904
|
+
} catch {
|
|
1905
|
+
}
|
|
1906
|
+
try {
|
|
1907
|
+
await client.execute({
|
|
1908
|
+
sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
|
|
1909
|
+
args: []
|
|
1910
|
+
});
|
|
1911
|
+
} catch {
|
|
1912
|
+
}
|
|
1913
|
+
try {
|
|
1914
|
+
await client.execute({
|
|
1915
|
+
sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
|
|
1916
|
+
args: []
|
|
1917
|
+
});
|
|
1918
|
+
} catch {
|
|
1919
|
+
}
|
|
1920
|
+
try {
|
|
1921
|
+
await client.execute({
|
|
1922
|
+
sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
|
|
1923
|
+
args: []
|
|
1924
|
+
});
|
|
1925
|
+
} catch {
|
|
1926
|
+
}
|
|
1927
|
+
try {
|
|
1928
|
+
await client.execute({
|
|
1929
|
+
sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
|
|
1930
|
+
args: []
|
|
1931
|
+
});
|
|
1932
|
+
} catch {
|
|
1933
|
+
}
|
|
1934
|
+
await client.executeMultiple(`
|
|
1935
|
+
CREATE TABLE IF NOT EXISTS consolidations (
|
|
1936
|
+
id TEXT PRIMARY KEY,
|
|
1937
|
+
consolidated_memory_id TEXT NOT NULL,
|
|
1938
|
+
source_memory_id TEXT NOT NULL,
|
|
1939
|
+
created_at TEXT NOT NULL
|
|
1940
|
+
);
|
|
1941
|
+
|
|
1942
|
+
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
1943
|
+
ON consolidations(source_memory_id);
|
|
1944
|
+
|
|
1945
|
+
CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
|
|
1946
|
+
ON consolidations(consolidated_memory_id);
|
|
1947
|
+
`);
|
|
1948
|
+
await client.executeMultiple(`
|
|
1949
|
+
CREATE TABLE IF NOT EXISTS reminders (
|
|
1950
|
+
id TEXT PRIMARY KEY,
|
|
1951
|
+
text TEXT NOT NULL,
|
|
1952
|
+
created_at TEXT NOT NULL,
|
|
1953
|
+
due_date TEXT,
|
|
1954
|
+
completed_at TEXT
|
|
1955
|
+
);
|
|
1956
|
+
`);
|
|
1957
|
+
await client.executeMultiple(`
|
|
1958
|
+
CREATE TABLE IF NOT EXISTS notifications (
|
|
1959
|
+
id TEXT PRIMARY KEY,
|
|
1960
|
+
agent_id TEXT NOT NULL,
|
|
1961
|
+
agent_role TEXT NOT NULL,
|
|
1962
|
+
event TEXT NOT NULL,
|
|
1963
|
+
project TEXT NOT NULL,
|
|
1964
|
+
summary TEXT NOT NULL,
|
|
1965
|
+
task_file TEXT,
|
|
1966
|
+
session_scope TEXT,
|
|
1967
|
+
read INTEGER NOT NULL DEFAULT 0,
|
|
1968
|
+
created_at TEXT NOT NULL
|
|
1969
|
+
);
|
|
1970
|
+
|
|
1971
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_read
|
|
1972
|
+
ON notifications(read);
|
|
1973
|
+
|
|
1974
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_agent
|
|
1975
|
+
ON notifications(agent_id, session_scope);
|
|
1976
|
+
|
|
1977
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_task_file
|
|
1978
|
+
ON notifications(task_file);
|
|
1979
|
+
`);
|
|
1980
|
+
await client.executeMultiple(`
|
|
1981
|
+
CREATE TABLE IF NOT EXISTS schedules (
|
|
1982
|
+
id TEXT PRIMARY KEY,
|
|
1983
|
+
cron TEXT NOT NULL,
|
|
1984
|
+
description TEXT NOT NULL,
|
|
1985
|
+
job_type TEXT NOT NULL DEFAULT 'report',
|
|
1986
|
+
prompt TEXT,
|
|
1987
|
+
assigned_to TEXT,
|
|
1988
|
+
project_name TEXT,
|
|
1989
|
+
active INTEGER NOT NULL DEFAULT 1,
|
|
1990
|
+
use_crontab INTEGER NOT NULL DEFAULT 0,
|
|
1991
|
+
created_at TEXT NOT NULL
|
|
1992
|
+
);
|
|
1993
|
+
`);
|
|
1994
|
+
await client.executeMultiple(`
|
|
1995
|
+
CREATE TABLE IF NOT EXISTS device_registry (
|
|
1996
|
+
device_id TEXT PRIMARY KEY,
|
|
1997
|
+
friendly_name TEXT NOT NULL,
|
|
1998
|
+
hostname TEXT NOT NULL,
|
|
1999
|
+
projects TEXT NOT NULL DEFAULT '[]',
|
|
2000
|
+
agents TEXT NOT NULL DEFAULT '[]',
|
|
2001
|
+
connected INTEGER DEFAULT 0,
|
|
2002
|
+
last_seen TEXT NOT NULL
|
|
2003
|
+
);
|
|
2004
|
+
`);
|
|
2005
|
+
await client.executeMultiple(`
|
|
2006
|
+
CREATE TABLE IF NOT EXISTS messages (
|
|
2007
|
+
id TEXT PRIMARY KEY,
|
|
2008
|
+
from_agent TEXT NOT NULL,
|
|
2009
|
+
from_device TEXT NOT NULL DEFAULT 'local',
|
|
2010
|
+
target_agent TEXT NOT NULL,
|
|
2011
|
+
target_project TEXT,
|
|
2012
|
+
target_device TEXT NOT NULL DEFAULT 'local',
|
|
2013
|
+
session_scope TEXT,
|
|
2014
|
+
content TEXT NOT NULL,
|
|
2015
|
+
priority TEXT DEFAULT 'normal',
|
|
2016
|
+
status TEXT DEFAULT 'pending',
|
|
2017
|
+
server_seq INTEGER,
|
|
2018
|
+
retry_count INTEGER DEFAULT 0,
|
|
2019
|
+
created_at TEXT NOT NULL,
|
|
2020
|
+
delivered_at TEXT,
|
|
2021
|
+
processed_at TEXT,
|
|
2022
|
+
failed_at TEXT,
|
|
2023
|
+
failure_reason TEXT
|
|
2024
|
+
);
|
|
2025
|
+
|
|
2026
|
+
CREATE INDEX IF NOT EXISTS idx_messages_target
|
|
2027
|
+
ON messages(target_agent, session_scope, status);
|
|
2028
|
+
|
|
2029
|
+
CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
|
|
2030
|
+
ON messages(target_agent, session_scope, from_agent, server_seq);
|
|
2031
|
+
`);
|
|
2032
|
+
try {
|
|
2033
|
+
await client.execute({
|
|
2034
|
+
sql: `ALTER TABLE notifications ADD COLUMN session_scope TEXT`,
|
|
2035
|
+
args: []
|
|
2036
|
+
});
|
|
2037
|
+
} catch {
|
|
2038
|
+
}
|
|
2039
|
+
try {
|
|
2040
|
+
await client.execute({
|
|
2041
|
+
sql: `ALTER TABLE messages ADD COLUMN session_scope TEXT`,
|
|
2042
|
+
args: []
|
|
2043
|
+
});
|
|
2044
|
+
} catch {
|
|
2045
|
+
}
|
|
2046
|
+
await client.executeMultiple(`
|
|
2047
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_agent_scope_read
|
|
2048
|
+
ON notifications(agent_id, session_scope, read, created_at);
|
|
2049
|
+
|
|
2050
|
+
CREATE INDEX IF NOT EXISTS idx_messages_target_scope_status
|
|
2051
|
+
ON messages(target_agent, session_scope, status, created_at);
|
|
2052
|
+
`);
|
|
2053
|
+
try {
|
|
2054
|
+
await client.execute({
|
|
2055
|
+
sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
|
|
2056
|
+
args: []
|
|
2057
|
+
});
|
|
2058
|
+
await client.execute({
|
|
2059
|
+
sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
|
|
2060
|
+
args: []
|
|
2061
|
+
});
|
|
2062
|
+
await client.execute({
|
|
2063
|
+
sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
|
|
2064
|
+
args: []
|
|
2065
|
+
});
|
|
2066
|
+
await client.execute({
|
|
2067
|
+
sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
|
|
1366
2068
|
args: []
|
|
1367
2069
|
});
|
|
1368
2070
|
} catch {
|
|
@@ -1959,19 +2661,40 @@ async function ensureSchema() {
|
|
|
1959
2661
|
} catch {
|
|
1960
2662
|
}
|
|
1961
2663
|
}
|
|
1962
|
-
|
|
2664
|
+
async function disposeDatabase() {
|
|
2665
|
+
if (_walCheckpointTimer) {
|
|
2666
|
+
clearInterval(_walCheckpointTimer);
|
|
2667
|
+
_walCheckpointTimer = null;
|
|
2668
|
+
}
|
|
2669
|
+
if (_daemonClient) {
|
|
2670
|
+
_daemonClient.close();
|
|
2671
|
+
_daemonClient = null;
|
|
2672
|
+
}
|
|
2673
|
+
if (_adapterClient && _adapterClient !== _resilientClient) {
|
|
2674
|
+
_adapterClient.close();
|
|
2675
|
+
}
|
|
2676
|
+
_adapterClient = null;
|
|
2677
|
+
if (_client) {
|
|
2678
|
+
_client.close();
|
|
2679
|
+
_client = null;
|
|
2680
|
+
_resilientClient = null;
|
|
2681
|
+
}
|
|
2682
|
+
}
|
|
2683
|
+
var _client, _resilientClient, _walCheckpointTimer, _daemonClient, _adapterClient, initTurso, disposeTurso;
|
|
1963
2684
|
var init_database = __esm({
|
|
1964
2685
|
"src/lib/database.ts"() {
|
|
1965
2686
|
"use strict";
|
|
1966
2687
|
init_db_retry();
|
|
1967
2688
|
init_employees();
|
|
1968
2689
|
init_database_adapter();
|
|
2690
|
+
init_memory();
|
|
1969
2691
|
_client = null;
|
|
1970
2692
|
_resilientClient = null;
|
|
1971
2693
|
_walCheckpointTimer = null;
|
|
1972
2694
|
_daemonClient = null;
|
|
1973
2695
|
_adapterClient = null;
|
|
1974
2696
|
initTurso = initDatabase;
|
|
2697
|
+
disposeTurso = disposeDatabase;
|
|
1975
2698
|
}
|
|
1976
2699
|
});
|
|
1977
2700
|
|
|
@@ -1989,12 +2712,12 @@ __export(shard_manager_exports, {
|
|
|
1989
2712
|
listShards: () => listShards,
|
|
1990
2713
|
shardExists: () => shardExists
|
|
1991
2714
|
});
|
|
1992
|
-
import
|
|
1993
|
-
import { existsSync as
|
|
2715
|
+
import path7 from "path";
|
|
2716
|
+
import { existsSync as existsSync7, mkdirSync as mkdirSync2, readdirSync } from "fs";
|
|
1994
2717
|
import { createClient as createClient2 } from "@libsql/client";
|
|
1995
2718
|
function initShardManager(encryptionKey) {
|
|
1996
2719
|
_encryptionKey = encryptionKey;
|
|
1997
|
-
if (!
|
|
2720
|
+
if (!existsSync7(SHARDS_DIR)) {
|
|
1998
2721
|
mkdirSync2(SHARDS_DIR, { recursive: true });
|
|
1999
2722
|
}
|
|
2000
2723
|
_shardingEnabled = true;
|
|
@@ -2024,7 +2747,7 @@ function getShardClient(projectName) {
|
|
|
2024
2747
|
while (_shards.size >= MAX_OPEN_SHARDS) {
|
|
2025
2748
|
evictLRU();
|
|
2026
2749
|
}
|
|
2027
|
-
const dbPath =
|
|
2750
|
+
const dbPath = path7.join(SHARDS_DIR, `${safeName}.db`);
|
|
2028
2751
|
const client = createClient2({
|
|
2029
2752
|
url: `file:${dbPath}`,
|
|
2030
2753
|
encryptionKey: _encryptionKey
|
|
@@ -2035,10 +2758,10 @@ function getShardClient(projectName) {
|
|
|
2035
2758
|
}
|
|
2036
2759
|
function shardExists(projectName) {
|
|
2037
2760
|
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
2038
|
-
return
|
|
2761
|
+
return existsSync7(path7.join(SHARDS_DIR, `${safeName}.db`));
|
|
2039
2762
|
}
|
|
2040
2763
|
function listShards() {
|
|
2041
|
-
if (!
|
|
2764
|
+
if (!existsSync7(SHARDS_DIR)) return [];
|
|
2042
2765
|
return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
2043
2766
|
}
|
|
2044
2767
|
async function ensureShardSchema(client) {
|
|
@@ -2285,7 +3008,7 @@ var init_shard_manager = __esm({
|
|
|
2285
3008
|
"src/lib/shard-manager.ts"() {
|
|
2286
3009
|
"use strict";
|
|
2287
3010
|
init_config();
|
|
2288
|
-
SHARDS_DIR =
|
|
3011
|
+
SHARDS_DIR = path7.join(EXE_AI_DIR, "shards");
|
|
2289
3012
|
SHARD_IDLE_MS = 5 * 60 * 1e3;
|
|
2290
3013
|
MAX_OPEN_SHARDS = 10;
|
|
2291
3014
|
EVICTION_INTERVAL_MS = 60 * 1e3;
|
|
@@ -2405,535 +3128,144 @@ var init_platform_procedures = __esm({
|
|
|
2405
3128
|
domain: "architecture",
|
|
2406
3129
|
priority: "p0",
|
|
2407
3130
|
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."
|
|
2408
|
-
}
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
//
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
var init_global_procedures = __esm({
|
|
2476
|
-
"src/lib/global-procedures.ts"() {
|
|
2477
|
-
"use strict";
|
|
2478
|
-
init_database();
|
|
2479
|
-
init_platform_procedures();
|
|
2480
|
-
_customerCache = "";
|
|
2481
|
-
_cacheLoaded = false;
|
|
2482
|
-
_platformCache = PLATFORM_PROCEDURES.map((p) => `### ${p.title}
|
|
2483
|
-
${p.content}`).join("\n\n");
|
|
2484
|
-
}
|
|
2485
|
-
});
|
|
2486
|
-
|
|
2487
|
-
// src/lib/daemon-auth.ts
|
|
2488
|
-
import crypto from "crypto";
|
|
2489
|
-
import path6 from "path";
|
|
2490
|
-
import { existsSync as existsSync6, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
2491
|
-
function normalizeToken(token) {
|
|
2492
|
-
if (!token) return null;
|
|
2493
|
-
const trimmed = token.trim();
|
|
2494
|
-
return trimmed.length > 0 ? trimmed : null;
|
|
2495
|
-
}
|
|
2496
|
-
function readDaemonToken() {
|
|
2497
|
-
try {
|
|
2498
|
-
if (!existsSync6(DAEMON_TOKEN_PATH)) return null;
|
|
2499
|
-
return normalizeToken(readFileSync3(DAEMON_TOKEN_PATH, "utf8"));
|
|
2500
|
-
} catch {
|
|
2501
|
-
return null;
|
|
2502
|
-
}
|
|
2503
|
-
}
|
|
2504
|
-
function ensureDaemonToken(seed) {
|
|
2505
|
-
const existing = readDaemonToken();
|
|
2506
|
-
if (existing) return existing;
|
|
2507
|
-
const token = normalizeToken(seed) ?? crypto.randomBytes(32).toString("hex");
|
|
2508
|
-
ensurePrivateDirSync(EXE_AI_DIR);
|
|
2509
|
-
writeFileSync2(DAEMON_TOKEN_PATH, `${token}
|
|
2510
|
-
`, "utf8");
|
|
2511
|
-
enforcePrivateFileSync(DAEMON_TOKEN_PATH);
|
|
2512
|
-
return token;
|
|
2513
|
-
}
|
|
2514
|
-
var DAEMON_TOKEN_PATH;
|
|
2515
|
-
var init_daemon_auth = __esm({
|
|
2516
|
-
"src/lib/daemon-auth.ts"() {
|
|
2517
|
-
"use strict";
|
|
2518
|
-
init_config();
|
|
2519
|
-
init_secure_files();
|
|
2520
|
-
DAEMON_TOKEN_PATH = path6.join(EXE_AI_DIR, "exed.token");
|
|
2521
|
-
}
|
|
2522
|
-
});
|
|
2523
|
-
|
|
2524
|
-
// src/lib/exe-daemon-client.ts
|
|
2525
|
-
import net from "net";
|
|
2526
|
-
import os5 from "os";
|
|
2527
|
-
import { spawn } from "child_process";
|
|
2528
|
-
import { randomUUID as randomUUID2 } from "crypto";
|
|
2529
|
-
import { existsSync as existsSync7, unlinkSync as unlinkSync2, readFileSync as readFileSync4, openSync, closeSync, statSync } from "fs";
|
|
2530
|
-
import path7 from "path";
|
|
2531
|
-
import { fileURLToPath } from "url";
|
|
2532
|
-
function handleData(chunk) {
|
|
2533
|
-
_buffer += chunk.toString();
|
|
2534
|
-
if (_buffer.length > MAX_BUFFER) {
|
|
2535
|
-
_buffer = "";
|
|
2536
|
-
return;
|
|
2537
|
-
}
|
|
2538
|
-
let newlineIdx;
|
|
2539
|
-
while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
|
|
2540
|
-
const line = _buffer.slice(0, newlineIdx).trim();
|
|
2541
|
-
_buffer = _buffer.slice(newlineIdx + 1);
|
|
2542
|
-
if (!line) continue;
|
|
2543
|
-
try {
|
|
2544
|
-
const response = JSON.parse(line);
|
|
2545
|
-
const id = response.id;
|
|
2546
|
-
if (!id) continue;
|
|
2547
|
-
const entry = _pending.get(id);
|
|
2548
|
-
if (entry) {
|
|
2549
|
-
clearTimeout(entry.timer);
|
|
2550
|
-
_pending.delete(id);
|
|
2551
|
-
entry.resolve(response);
|
|
2552
|
-
}
|
|
2553
|
-
} catch {
|
|
2554
|
-
}
|
|
2555
|
-
}
|
|
2556
|
-
}
|
|
2557
|
-
function cleanupStaleFiles() {
|
|
2558
|
-
if (existsSync7(PID_PATH)) {
|
|
2559
|
-
try {
|
|
2560
|
-
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
2561
|
-
if (pid > 0) {
|
|
2562
|
-
try {
|
|
2563
|
-
process.kill(pid, 0);
|
|
2564
|
-
return;
|
|
2565
|
-
} catch {
|
|
2566
|
-
}
|
|
2567
|
-
}
|
|
2568
|
-
} catch {
|
|
2569
|
-
}
|
|
2570
|
-
try {
|
|
2571
|
-
unlinkSync2(PID_PATH);
|
|
2572
|
-
} catch {
|
|
2573
|
-
}
|
|
2574
|
-
try {
|
|
2575
|
-
unlinkSync2(SOCKET_PATH);
|
|
2576
|
-
} catch {
|
|
2577
|
-
}
|
|
2578
|
-
}
|
|
2579
|
-
}
|
|
2580
|
-
function findPackageRoot() {
|
|
2581
|
-
let dir = path7.dirname(fileURLToPath(import.meta.url));
|
|
2582
|
-
const { root } = path7.parse(dir);
|
|
2583
|
-
while (dir !== root) {
|
|
2584
|
-
if (existsSync7(path7.join(dir, "package.json"))) return dir;
|
|
2585
|
-
dir = path7.dirname(dir);
|
|
2586
|
-
}
|
|
2587
|
-
return null;
|
|
2588
|
-
}
|
|
2589
|
-
function getAvailableMemoryGB() {
|
|
2590
|
-
if (process.platform === "darwin") {
|
|
2591
|
-
try {
|
|
2592
|
-
const { execSync: execSync2 } = __require("child_process");
|
|
2593
|
-
const vmstat = execSync2("vm_stat", { encoding: "utf8" });
|
|
2594
|
-
const pageSize = 16384;
|
|
2595
|
-
const pageSizeMatch = vmstat.match(/page size of (\d+) bytes/);
|
|
2596
|
-
const actualPageSize = pageSizeMatch ? parseInt(pageSizeMatch[1], 10) : pageSize;
|
|
2597
|
-
const free = vmstat.match(/Pages free:\s+(\d+)/);
|
|
2598
|
-
const inactive = vmstat.match(/Pages inactive:\s+(\d+)/);
|
|
2599
|
-
const speculative = vmstat.match(/Pages speculative:\s+(\d+)/);
|
|
2600
|
-
const freePages = free ? parseInt(free[1], 10) : 0;
|
|
2601
|
-
const inactivePages = inactive ? parseInt(inactive[1], 10) : 0;
|
|
2602
|
-
const speculativePages = speculative ? parseInt(speculative[1], 10) : 0;
|
|
2603
|
-
return (freePages + inactivePages + speculativePages) * actualPageSize / (1024 * 1024 * 1024);
|
|
2604
|
-
} catch {
|
|
2605
|
-
return os5.freemem() / (1024 * 1024 * 1024);
|
|
2606
|
-
}
|
|
2607
|
-
}
|
|
2608
|
-
return os5.freemem() / (1024 * 1024 * 1024);
|
|
2609
|
-
}
|
|
2610
|
-
function spawnDaemon() {
|
|
2611
|
-
const freeGB = getAvailableMemoryGB();
|
|
2612
|
-
const totalGB = os5.totalmem() / (1024 * 1024 * 1024);
|
|
2613
|
-
if (totalGB <= 8) {
|
|
2614
|
-
process.stderr.write(
|
|
2615
|
-
`[exed-client] SKIP: ${totalGB.toFixed(0)}GB system \u2014 embedding daemon disabled. Using keyword search only. Minimum 16GB recommended for vector search.
|
|
2616
|
-
`
|
|
2617
|
-
);
|
|
2618
|
-
return;
|
|
2619
|
-
}
|
|
2620
|
-
if (totalGB <= 16 && freeGB < 2) {
|
|
2621
|
-
process.stderr.write(
|
|
2622
|
-
`[exed-client] SKIP: low memory (${freeGB.toFixed(1)}GB available / ${totalGB.toFixed(0)}GB total). Embedding daemon not started \u2014 using keyword search only.
|
|
2623
|
-
`
|
|
2624
|
-
);
|
|
2625
|
-
return;
|
|
2626
|
-
}
|
|
2627
|
-
const pkgRoot = findPackageRoot();
|
|
2628
|
-
if (!pkgRoot) {
|
|
2629
|
-
process.stderr.write("[exed-client] WARN: cannot find package root\n");
|
|
2630
|
-
return;
|
|
2631
|
-
}
|
|
2632
|
-
const daemonPath = path7.join(pkgRoot, "dist", "lib", "exe-daemon.js");
|
|
2633
|
-
if (!existsSync7(daemonPath)) {
|
|
2634
|
-
process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
|
|
2635
|
-
`);
|
|
2636
|
-
return;
|
|
2637
|
-
}
|
|
2638
|
-
const resolvedPath = daemonPath;
|
|
2639
|
-
const daemonToken = ensureDaemonToken(process.env[DAEMON_TOKEN_ENV] ?? null);
|
|
2640
|
-
process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
|
|
2641
|
-
`);
|
|
2642
|
-
const logPath = path7.join(path7.dirname(SOCKET_PATH), "exed.log");
|
|
2643
|
-
let stderrFd = "ignore";
|
|
2644
|
-
try {
|
|
2645
|
-
stderrFd = openSync(logPath, "a");
|
|
2646
|
-
} catch {
|
|
2647
|
-
}
|
|
2648
|
-
const heapCapMB = totalGB <= 8 ? 256 : 512;
|
|
2649
|
-
const nodeArgs = [`--max-old-space-size=${heapCapMB}`, resolvedPath];
|
|
2650
|
-
const child = spawn(process.execPath, nodeArgs, {
|
|
2651
|
-
detached: true,
|
|
2652
|
-
stdio: ["ignore", "ignore", stderrFd],
|
|
2653
|
-
env: {
|
|
2654
|
-
...process.env,
|
|
2655
|
-
TMUX: void 0,
|
|
2656
|
-
// Daemon is global — must not inherit session scope
|
|
2657
|
-
TMUX_PANE: void 0,
|
|
2658
|
-
// Prevents resolveExeSession() from scoping to one session
|
|
2659
|
-
EXE_DAEMON_SOCK: SOCKET_PATH,
|
|
2660
|
-
EXE_DAEMON_PID: PID_PATH,
|
|
2661
|
-
[DAEMON_TOKEN_ENV]: daemonToken
|
|
2662
|
-
}
|
|
2663
|
-
});
|
|
2664
|
-
child.unref();
|
|
2665
|
-
if (typeof stderrFd === "number") {
|
|
2666
|
-
try {
|
|
2667
|
-
closeSync(stderrFd);
|
|
2668
|
-
} catch {
|
|
2669
|
-
}
|
|
2670
|
-
}
|
|
2671
|
-
}
|
|
2672
|
-
function acquireSpawnLock() {
|
|
2673
|
-
try {
|
|
2674
|
-
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
2675
|
-
closeSync(fd);
|
|
2676
|
-
return true;
|
|
2677
|
-
} catch {
|
|
2678
|
-
try {
|
|
2679
|
-
const stat2 = statSync(SPAWN_LOCK_PATH);
|
|
2680
|
-
if (Date.now() - stat2.mtimeMs > SPAWN_LOCK_STALE_MS) {
|
|
2681
|
-
try {
|
|
2682
|
-
unlinkSync2(SPAWN_LOCK_PATH);
|
|
2683
|
-
} catch {
|
|
2684
|
-
}
|
|
2685
|
-
try {
|
|
2686
|
-
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
2687
|
-
closeSync(fd);
|
|
2688
|
-
return true;
|
|
2689
|
-
} catch {
|
|
2690
|
-
}
|
|
2691
|
-
}
|
|
2692
|
-
} catch {
|
|
2693
|
-
}
|
|
2694
|
-
return false;
|
|
2695
|
-
}
|
|
2696
|
-
}
|
|
2697
|
-
function releaseSpawnLock() {
|
|
2698
|
-
try {
|
|
2699
|
-
unlinkSync2(SPAWN_LOCK_PATH);
|
|
2700
|
-
} catch {
|
|
2701
|
-
}
|
|
2702
|
-
}
|
|
2703
|
-
function connectToSocket() {
|
|
2704
|
-
return new Promise((resolve) => {
|
|
2705
|
-
if (_socket && _connected) {
|
|
2706
|
-
resolve(true);
|
|
2707
|
-
return;
|
|
2708
|
-
}
|
|
2709
|
-
const socket = net.createConnection({ path: SOCKET_PATH });
|
|
2710
|
-
const connectTimeout = setTimeout(() => {
|
|
2711
|
-
socket.destroy();
|
|
2712
|
-
resolve(false);
|
|
2713
|
-
}, 2e3);
|
|
2714
|
-
socket.on("connect", () => {
|
|
2715
|
-
clearTimeout(connectTimeout);
|
|
2716
|
-
_socket = socket;
|
|
2717
|
-
_connected = true;
|
|
2718
|
-
_buffer = "";
|
|
2719
|
-
socket.on("data", handleData);
|
|
2720
|
-
socket.on("close", () => {
|
|
2721
|
-
_connected = false;
|
|
2722
|
-
_socket = null;
|
|
2723
|
-
for (const [id, entry] of _pending) {
|
|
2724
|
-
clearTimeout(entry.timer);
|
|
2725
|
-
_pending.delete(id);
|
|
2726
|
-
entry.resolve({ error: "Connection closed" });
|
|
2727
|
-
}
|
|
2728
|
-
});
|
|
2729
|
-
socket.on("error", () => {
|
|
2730
|
-
_connected = false;
|
|
2731
|
-
_socket = null;
|
|
2732
|
-
});
|
|
2733
|
-
resolve(true);
|
|
2734
|
-
});
|
|
2735
|
-
socket.on("error", () => {
|
|
2736
|
-
clearTimeout(connectTimeout);
|
|
2737
|
-
resolve(false);
|
|
2738
|
-
});
|
|
2739
|
-
});
|
|
2740
|
-
}
|
|
2741
|
-
async function connectEmbedDaemon() {
|
|
2742
|
-
if (_socket && _connected) return true;
|
|
2743
|
-
if (await connectToSocket()) return true;
|
|
2744
|
-
if (acquireSpawnLock()) {
|
|
2745
|
-
try {
|
|
2746
|
-
cleanupStaleFiles();
|
|
2747
|
-
spawnDaemon();
|
|
2748
|
-
} finally {
|
|
2749
|
-
releaseSpawnLock();
|
|
2750
|
-
}
|
|
2751
|
-
}
|
|
2752
|
-
const start = Date.now();
|
|
2753
|
-
let delay2 = 100;
|
|
2754
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
2755
|
-
await new Promise((r) => setTimeout(r, delay2));
|
|
2756
|
-
if (await connectToSocket()) return true;
|
|
2757
|
-
delay2 = Math.min(delay2 * 2, 3e3);
|
|
3131
|
+
},
|
|
3132
|
+
// --- MCP is the ONLY data interface ---
|
|
3133
|
+
{
|
|
3134
|
+
title: "MCP disconnect \u2014 ask the user, never work around it",
|
|
3135
|
+
domain: "workflow",
|
|
3136
|
+
priority: "p0",
|
|
3137
|
+
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."
|
|
3138
|
+
},
|
|
3139
|
+
// --- MCP Tool Catalog (Layer 0 — every agent knows what tools exist) ---
|
|
3140
|
+
{
|
|
3141
|
+
title: "MCP tools \u2014 memory and search",
|
|
3142
|
+
domain: "tool-use",
|
|
3143
|
+
priority: "p1",
|
|
3144
|
+
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)."
|
|
3145
|
+
},
|
|
3146
|
+
{
|
|
3147
|
+
title: "MCP tools \u2014 task orchestration",
|
|
3148
|
+
domain: "tool-use",
|
|
3149
|
+
priority: "p1",
|
|
3150
|
+
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."
|
|
3151
|
+
},
|
|
3152
|
+
{
|
|
3153
|
+
title: "MCP tools \u2014 knowledge graph (GraphRAG)",
|
|
3154
|
+
domain: "tool-use",
|
|
3155
|
+
priority: "p1",
|
|
3156
|
+
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."
|
|
3157
|
+
},
|
|
3158
|
+
{
|
|
3159
|
+
title: "MCP tools \u2014 identity, behavior, and decisions",
|
|
3160
|
+
domain: "tool-use",
|
|
3161
|
+
priority: "p1",
|
|
3162
|
+
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."
|
|
3163
|
+
},
|
|
3164
|
+
{
|
|
3165
|
+
title: "MCP tools \u2014 communication and messaging",
|
|
3166
|
+
domain: "tool-use",
|
|
3167
|
+
priority: "p1",
|
|
3168
|
+
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.)."
|
|
3169
|
+
},
|
|
3170
|
+
{
|
|
3171
|
+
title: "MCP tools \u2014 wiki, documents, and content",
|
|
3172
|
+
domain: "tool-use",
|
|
3173
|
+
priority: "p1",
|
|
3174
|
+
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."
|
|
3175
|
+
},
|
|
3176
|
+
{
|
|
3177
|
+
title: "MCP tools \u2014 system, operations, and admin",
|
|
3178
|
+
domain: "tool-use",
|
|
3179
|
+
priority: "p1",
|
|
3180
|
+
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."
|
|
3181
|
+
},
|
|
3182
|
+
{
|
|
3183
|
+
title: "MCP tools \u2014 config, licensing, and team",
|
|
3184
|
+
domain: "tool-use",
|
|
3185
|
+
priority: "p1",
|
|
3186
|
+
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."
|
|
3187
|
+
},
|
|
3188
|
+
{
|
|
3189
|
+
title: "MCP tools \u2014 advanced (triggers, skills, orchestration)",
|
|
3190
|
+
domain: "tool-use",
|
|
3191
|
+
priority: "p1",
|
|
3192
|
+
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."
|
|
3193
|
+
}
|
|
3194
|
+
];
|
|
3195
|
+
PLATFORM_PROCEDURE_TITLES = new Set(
|
|
3196
|
+
PLATFORM_PROCEDURES.map((p) => p.title)
|
|
3197
|
+
);
|
|
2758
3198
|
}
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
}, timeoutMs);
|
|
2776
|
-
_pending.set(id, { resolve, timer });
|
|
2777
|
-
try {
|
|
2778
|
-
_socket.write(JSON.stringify({ id, token, ...payload }) + "\n");
|
|
2779
|
-
} catch {
|
|
2780
|
-
clearTimeout(timer);
|
|
2781
|
-
_pending.delete(id);
|
|
2782
|
-
resolve({ error: "Write failed" });
|
|
2783
|
-
}
|
|
3199
|
+
});
|
|
3200
|
+
|
|
3201
|
+
// src/lib/global-procedures.ts
|
|
3202
|
+
var global_procedures_exports = {};
|
|
3203
|
+
__export(global_procedures_exports, {
|
|
3204
|
+
deactivateGlobalProcedure: () => deactivateGlobalProcedure,
|
|
3205
|
+
getGlobalProceduresBlock: () => getGlobalProceduresBlock,
|
|
3206
|
+
loadGlobalProcedures: () => loadGlobalProcedures,
|
|
3207
|
+
storeGlobalProcedure: () => storeGlobalProcedure
|
|
3208
|
+
});
|
|
3209
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
3210
|
+
async function loadGlobalProcedures() {
|
|
3211
|
+
const client = getClient();
|
|
3212
|
+
const result = await client.execute({
|
|
3213
|
+
sql: "SELECT * FROM global_procedures WHERE active = 1 ORDER BY priority ASC, created_at ASC",
|
|
3214
|
+
args: []
|
|
2784
3215
|
});
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
if (
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
return null;
|
|
2793
|
-
}
|
|
2794
|
-
function killAndRespawnDaemon() {
|
|
2795
|
-
if (!acquireSpawnLock()) {
|
|
2796
|
-
process.stderr.write("[exed-client] Another process is already restarting daemon \u2014 skipping\n");
|
|
2797
|
-
if (_socket) {
|
|
2798
|
-
_socket.destroy();
|
|
2799
|
-
_socket = null;
|
|
2800
|
-
}
|
|
2801
|
-
_connected = false;
|
|
2802
|
-
_buffer = "";
|
|
2803
|
-
return;
|
|
2804
|
-
}
|
|
2805
|
-
try {
|
|
2806
|
-
process.stderr.write("[exed-client] Killing daemon for restart...\n");
|
|
2807
|
-
if (existsSync7(PID_PATH)) {
|
|
2808
|
-
try {
|
|
2809
|
-
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
2810
|
-
if (pid > 0) {
|
|
2811
|
-
try {
|
|
2812
|
-
process.kill(pid, "SIGKILL");
|
|
2813
|
-
} catch {
|
|
2814
|
-
}
|
|
2815
|
-
}
|
|
2816
|
-
} catch {
|
|
2817
|
-
}
|
|
2818
|
-
}
|
|
2819
|
-
if (_socket) {
|
|
2820
|
-
_socket.destroy();
|
|
2821
|
-
_socket = null;
|
|
2822
|
-
}
|
|
2823
|
-
_connected = false;
|
|
2824
|
-
_buffer = "";
|
|
2825
|
-
try {
|
|
2826
|
-
unlinkSync2(PID_PATH);
|
|
2827
|
-
} catch {
|
|
2828
|
-
}
|
|
2829
|
-
try {
|
|
2830
|
-
unlinkSync2(SOCKET_PATH);
|
|
2831
|
-
} catch {
|
|
2832
|
-
}
|
|
2833
|
-
spawnDaemon();
|
|
2834
|
-
} finally {
|
|
2835
|
-
releaseSpawnLock();
|
|
3216
|
+
const allRows = result.rows;
|
|
3217
|
+
const customerOnly = allRows.filter((p) => !PLATFORM_PROCEDURE_TITLES.has(p.title));
|
|
3218
|
+
if (customerOnly.length > 0) {
|
|
3219
|
+
_customerCache = customerOnly.map((p) => `### ${p.title}
|
|
3220
|
+
${p.content}`).join("\n\n");
|
|
3221
|
+
} else {
|
|
3222
|
+
_customerCache = "";
|
|
2836
3223
|
}
|
|
3224
|
+
_cacheLoaded = true;
|
|
3225
|
+
return customerOnly;
|
|
2837
3226
|
}
|
|
2838
|
-
function
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
3227
|
+
function getGlobalProceduresBlock() {
|
|
3228
|
+
const sections = [];
|
|
3229
|
+
if (_platformCache) sections.push(_platformCache);
|
|
3230
|
+
if (_cacheLoaded && _customerCache) sections.push(_customerCache);
|
|
3231
|
+
if (sections.length === 0) return "";
|
|
3232
|
+
return `## Organization-Wide Procedures (MANDATORY \u2014 supersedes all other rules)
|
|
3233
|
+
|
|
3234
|
+
${sections.join("\n\n")}
|
|
3235
|
+
`;
|
|
2845
3236
|
}
|
|
2846
|
-
async function
|
|
2847
|
-
const
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
await new Promise((r) => setTimeout(r, delayMs));
|
|
2858
|
-
if (!_connected) {
|
|
2859
|
-
if (!await connectToSocket()) continue;
|
|
2860
|
-
}
|
|
2861
|
-
const retry = await doRequest();
|
|
2862
|
-
if (!retry.error) {
|
|
2863
|
-
_consecutiveFailures = 0;
|
|
2864
|
-
return retry;
|
|
2865
|
-
}
|
|
2866
|
-
_consecutiveFailures++;
|
|
2867
|
-
}
|
|
2868
|
-
if (isDaemonTooYoung()) {
|
|
2869
|
-
process.stderr.write(`[exed-client] ${label}: daemon too young (< ${MIN_DAEMON_AGE_MS / 1e3}s) \u2014 skipping restart
|
|
2870
|
-
`);
|
|
2871
|
-
return { error: result.error };
|
|
2872
|
-
}
|
|
2873
|
-
process.stderr.write(`[exed-client] ${label}: ${_consecutiveFailures} consecutive failures \u2014 restarting daemon
|
|
2874
|
-
`);
|
|
2875
|
-
killAndRespawnDaemon();
|
|
2876
|
-
const start = Date.now();
|
|
2877
|
-
let delay2 = 200;
|
|
2878
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
2879
|
-
await new Promise((r) => setTimeout(r, delay2));
|
|
2880
|
-
if (await connectToSocket()) break;
|
|
2881
|
-
delay2 = Math.min(delay2 * 2, 3e3);
|
|
2882
|
-
}
|
|
2883
|
-
if (!_connected) return { error: "Daemon restart failed" };
|
|
2884
|
-
const final = await doRequest();
|
|
2885
|
-
if (!final.error) _consecutiveFailures = 0;
|
|
2886
|
-
return final;
|
|
3237
|
+
async function storeGlobalProcedure(input) {
|
|
3238
|
+
const id = randomUUID2();
|
|
3239
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
3240
|
+
const client = getClient();
|
|
3241
|
+
await client.execute({
|
|
3242
|
+
sql: `INSERT INTO global_procedures (id, title, content, priority, domain, active, created_at, updated_at)
|
|
3243
|
+
VALUES (?, ?, ?, ?, ?, 1, ?, ?)`,
|
|
3244
|
+
args: [id, input.title, input.content, input.priority ?? "p0", input.domain ?? null, now, now]
|
|
3245
|
+
});
|
|
3246
|
+
await loadGlobalProcedures();
|
|
3247
|
+
return id;
|
|
2887
3248
|
}
|
|
2888
|
-
async function
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
const start = Date.now();
|
|
2898
|
-
let d = 200;
|
|
2899
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
2900
|
-
await new Promise((r) => setTimeout(r, d));
|
|
2901
|
-
if (await connectToSocket()) break;
|
|
2902
|
-
d = Math.min(d * 2, 3e3);
|
|
2903
|
-
}
|
|
2904
|
-
if (!_connected) return null;
|
|
2905
|
-
}
|
|
2906
|
-
}
|
|
2907
|
-
const result = await retryThenRestart(
|
|
2908
|
-
() => sendRequest([text], priority),
|
|
2909
|
-
"Embed"
|
|
2910
|
-
);
|
|
2911
|
-
return !result.error && result.vectors?.[0] ? result.vectors[0] : null;
|
|
3249
|
+
async function deactivateGlobalProcedure(id) {
|
|
3250
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
3251
|
+
const client = getClient();
|
|
3252
|
+
const result = await client.execute({
|
|
3253
|
+
sql: "UPDATE global_procedures SET active = 0, updated_at = ? WHERE id = ?",
|
|
3254
|
+
args: [now, id]
|
|
3255
|
+
});
|
|
3256
|
+
await loadGlobalProcedures();
|
|
3257
|
+
return result.rowsAffected > 0;
|
|
2912
3258
|
}
|
|
2913
|
-
var
|
|
2914
|
-
var
|
|
2915
|
-
"src/lib/
|
|
3259
|
+
var _customerCache, _cacheLoaded, _platformCache;
|
|
3260
|
+
var init_global_procedures = __esm({
|
|
3261
|
+
"src/lib/global-procedures.ts"() {
|
|
2916
3262
|
"use strict";
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
CONNECT_TIMEOUT_MS = 15e3;
|
|
2924
|
-
REQUEST_TIMEOUT_MS = 3e4;
|
|
2925
|
-
DAEMON_TOKEN_ENV = "EXE_DAEMON_TOKEN";
|
|
2926
|
-
_socket = null;
|
|
2927
|
-
_connected = false;
|
|
2928
|
-
_buffer = "";
|
|
2929
|
-
_requestCount = 0;
|
|
2930
|
-
_consecutiveFailures = 0;
|
|
2931
|
-
HEALTH_CHECK_INTERVAL = 100;
|
|
2932
|
-
MAX_RETRIES_BEFORE_RESTART = 3;
|
|
2933
|
-
RETRY_DELAYS_MS = [1e3, 3e3, 5e3];
|
|
2934
|
-
MIN_DAEMON_AGE_MS = 3e4;
|
|
2935
|
-
_pending = /* @__PURE__ */ new Map();
|
|
2936
|
-
MAX_BUFFER = 1e7;
|
|
3263
|
+
init_database();
|
|
3264
|
+
init_platform_procedures();
|
|
3265
|
+
_customerCache = "";
|
|
3266
|
+
_cacheLoaded = false;
|
|
3267
|
+
_platformCache = PLATFORM_PROCEDURES.map((p) => `### ${p.title}
|
|
3268
|
+
${p.content}`).join("\n\n");
|
|
2937
3269
|
}
|
|
2938
3270
|
});
|
|
2939
3271
|
|
|
@@ -2947,26 +3279,22 @@ import { homedir } from "os";
|
|
|
2947
3279
|
import { parseArgs } from "util";
|
|
2948
3280
|
|
|
2949
3281
|
// src/lib/store.ts
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
// src/types/memory.ts
|
|
2953
|
-
var EMBEDDING_DIM = 1024;
|
|
2954
|
-
|
|
2955
|
-
// src/lib/store.ts
|
|
3282
|
+
init_memory();
|
|
2956
3283
|
init_database();
|
|
3284
|
+
import { createHash } from "crypto";
|
|
2957
3285
|
|
|
2958
3286
|
// src/lib/keychain.ts
|
|
2959
3287
|
import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod as chmod2 } from "fs/promises";
|
|
2960
|
-
import { existsSync as
|
|
2961
|
-
import
|
|
2962
|
-
import
|
|
3288
|
+
import { existsSync as existsSync6 } from "fs";
|
|
3289
|
+
import path6 from "path";
|
|
3290
|
+
import os5 from "os";
|
|
2963
3291
|
var SERVICE = "exe-mem";
|
|
2964
3292
|
var ACCOUNT = "master-key";
|
|
2965
3293
|
function getKeyDir() {
|
|
2966
|
-
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ??
|
|
3294
|
+
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path6.join(os5.homedir(), ".exe-os");
|
|
2967
3295
|
}
|
|
2968
3296
|
function getKeyPath() {
|
|
2969
|
-
return
|
|
3297
|
+
return path6.join(getKeyDir(), "master.key");
|
|
2970
3298
|
}
|
|
2971
3299
|
async function tryKeytar() {
|
|
2972
3300
|
try {
|
|
@@ -2987,9 +3315,9 @@ async function getMasterKey() {
|
|
|
2987
3315
|
}
|
|
2988
3316
|
}
|
|
2989
3317
|
const keyPath = getKeyPath();
|
|
2990
|
-
if (!
|
|
3318
|
+
if (!existsSync6(keyPath)) {
|
|
2991
3319
|
process.stderr.write(
|
|
2992
|
-
`[keychain] Key not found at ${keyPath} (HOME=${
|
|
3320
|
+
`[keychain] Key not found at ${keyPath} (HOME=${os5.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
|
|
2993
3321
|
`
|
|
2994
3322
|
);
|
|
2995
3323
|
return null;
|
|
@@ -3118,6 +3446,11 @@ async function initStore(options) {
|
|
|
3118
3446
|
encryptionKey: hexKey
|
|
3119
3447
|
});
|
|
3120
3448
|
await retryOnBusy2(() => ensureSchema(), "ensureSchema");
|
|
3449
|
+
try {
|
|
3450
|
+
const { initDaemonClient: initDaemonClient2 } = await Promise.resolve().then(() => (init_database(), database_exports));
|
|
3451
|
+
await initDaemonClient2();
|
|
3452
|
+
} catch {
|
|
3453
|
+
}
|
|
3121
3454
|
if (!options?.lightweight) {
|
|
3122
3455
|
try {
|
|
3123
3456
|
const { initShardManager: initShardManager2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|