@askexenow/exe-os 0.8.40 → 0.8.42
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 +805 -642
- package/dist/bin/backfill-responses.js +804 -641
- package/dist/bin/backfill-vectors.js +791 -634
- package/dist/bin/cleanup-stale-review-tasks.js +788 -631
- package/dist/bin/cli.js +1376 -659
- package/dist/bin/exe-agent.js +20 -1
- package/dist/bin/exe-assign.js +1503 -1343
- package/dist/bin/exe-boot.js +2549 -1784
- package/dist/bin/exe-call.js +39 -1
- package/dist/bin/exe-cloud.js +12 -2
- package/dist/bin/exe-dispatch.js +39 -2
- package/dist/bin/exe-doctor.js +791 -634
- package/dist/bin/exe-export-behaviors.js +792 -637
- package/dist/bin/exe-forget.js +145 -0
- package/dist/bin/exe-gateway.js +2501 -1846
- package/dist/bin/exe-heartbeat.js +147 -1
- package/dist/bin/exe-kill.js +795 -640
- package/dist/bin/exe-launch-agent.js +2168 -2008
- package/dist/bin/exe-link.js +44 -12
- package/dist/bin/exe-new-employee.js +6 -2
- package/dist/bin/exe-pending-messages.js +146 -1
- package/dist/bin/exe-pending-notifications.js +788 -631
- package/dist/bin/exe-pending-reviews.js +176 -1
- package/dist/bin/exe-rename.js +23 -0
- package/dist/bin/exe-review.js +490 -327
- package/dist/bin/exe-search.js +157 -4
- package/dist/bin/exe-session-cleanup.js +2487 -403
- package/dist/bin/exe-settings.js +2 -1
- package/dist/bin/exe-status.js +474 -317
- package/dist/bin/exe-team.js +474 -317
- package/dist/bin/git-sweep.js +2691 -151
- package/dist/bin/graph-backfill.js +794 -637
- package/dist/bin/graph-export.js +798 -641
- package/dist/bin/scan-tasks.js +2951 -44
- package/dist/bin/setup.js +50 -26
- package/dist/bin/shard-migrate.js +792 -637
- package/dist/bin/wiki-sync.js +794 -637
- package/dist/gateway/index.js +2542 -1887
- package/dist/hooks/bug-report-worker.js +2118 -576
- package/dist/hooks/commit-complete.js +2690 -150
- package/dist/hooks/error-recall.js +157 -4
- package/dist/hooks/ingest-worker.js +1455 -803
- package/dist/hooks/instructions-loaded.js +151 -0
- package/dist/hooks/notification.js +153 -2
- package/dist/hooks/post-compact.js +164 -0
- package/dist/hooks/pre-compact.js +3073 -101
- package/dist/hooks/pre-tool-use.js +151 -0
- package/dist/hooks/prompt-ingest-worker.js +1670 -1509
- package/dist/hooks/prompt-submit.js +2650 -1074
- package/dist/hooks/response-ingest-worker.js +154 -6
- package/dist/hooks/session-end.js +153 -2
- package/dist/hooks/session-start.js +157 -4
- package/dist/hooks/stop.js +151 -0
- package/dist/hooks/subagent-stop.js +155 -2
- package/dist/hooks/summary-worker.js +190 -21
- package/dist/index.js +326 -102
- package/dist/lib/cloud-sync.js +31 -10
- package/dist/lib/config.js +2 -0
- package/dist/lib/consolidation.js +69 -2
- package/dist/lib/database.js +19 -0
- package/dist/lib/device-registry.js +19 -0
- package/dist/lib/embedder.js +3 -1
- package/dist/lib/employee-templates.js +20 -1
- package/dist/lib/exe-daemon.js +285 -18
- package/dist/lib/hybrid-search.js +157 -4
- package/dist/lib/messaging.js +39 -2
- package/dist/lib/schedules.js +792 -637
- package/dist/lib/store.js +796 -636
- package/dist/lib/tasks.js +1485 -918
- package/dist/lib/tmux-routing.js +194 -10
- package/dist/mcp/server.js +1643 -924
- package/dist/mcp/tools/create-task.js +2283 -829
- package/dist/mcp/tools/list-tasks.js +2788 -159
- package/dist/mcp/tools/send-message.js +39 -2
- package/dist/mcp/tools/update-task.js +79 -0
- package/dist/runtime/index.js +280 -68
- package/dist/tui/App.js +1485 -645
- package/package.json +3 -2
|
@@ -9,253 +9,95 @@ var __export = (target, all) => {
|
|
|
9
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
// src/lib/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
function resolveDataDir() {
|
|
18
|
-
if (process.env.EXE_OS_DIR) return process.env.EXE_OS_DIR;
|
|
19
|
-
if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
|
|
20
|
-
const newDir = path2.join(os2.homedir(), ".exe-os");
|
|
21
|
-
const legacyDir = path2.join(os2.homedir(), ".exe-mem");
|
|
22
|
-
if (!existsSync2(newDir) && existsSync2(legacyDir)) {
|
|
23
|
-
try {
|
|
24
|
-
renameSync(legacyDir, newDir);
|
|
25
|
-
process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
|
|
26
|
-
`);
|
|
27
|
-
} catch {
|
|
28
|
-
return legacyDir;
|
|
29
|
-
}
|
|
12
|
+
// src/lib/db-retry.ts
|
|
13
|
+
function isBusyError(err) {
|
|
14
|
+
if (err instanceof Error) {
|
|
15
|
+
const msg = err.message.toLowerCase();
|
|
16
|
+
return msg.includes("sqlite_busy") || msg.includes("database is locked");
|
|
30
17
|
}
|
|
31
|
-
return
|
|
18
|
+
return false;
|
|
32
19
|
}
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
process.stderr.write(
|
|
36
|
-
"[exe-os] Warning: config.json contains deprecated 'r2' field from v1.0. R2 sync has been replaced in v1.1. The 'r2' field will be ignored.\n"
|
|
37
|
-
);
|
|
38
|
-
delete raw.r2;
|
|
39
|
-
}
|
|
40
|
-
if ("syncIntervalMs" in raw) {
|
|
41
|
-
delete raw.syncIntervalMs;
|
|
42
|
-
}
|
|
43
|
-
return raw;
|
|
20
|
+
function delay(ms) {
|
|
21
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
44
22
|
}
|
|
45
|
-
function
|
|
46
|
-
|
|
47
|
-
let
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
23
|
+
async function retryOnBusy(fn, label) {
|
|
24
|
+
let lastError;
|
|
25
|
+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
26
|
+
try {
|
|
27
|
+
return await fn();
|
|
28
|
+
} catch (err) {
|
|
29
|
+
lastError = err;
|
|
30
|
+
if (!isBusyError(err) || attempt === MAX_RETRIES) {
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
const backoff = BASE_DELAY_MS * Math.pow(2, attempt);
|
|
34
|
+
const jitter = Math.floor(Math.random() * MAX_JITTER_MS);
|
|
35
|
+
process.stderr.write(
|
|
36
|
+
`[exe-os] SQLITE_BUSY ${label} retry ${attempt + 1}/${MAX_RETRIES} \u2014 waiting ${backoff + jitter}ms
|
|
37
|
+
`
|
|
38
|
+
);
|
|
39
|
+
await delay(backoff + jitter);
|
|
57
40
|
}
|
|
58
41
|
}
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
function normalizeScalingRoadmap(raw) {
|
|
62
|
-
const defaultAuto = DEFAULT_CONFIG.scalingRoadmap.rerankerAutoTrigger;
|
|
63
|
-
const userRoadmap = raw.scalingRoadmap ?? {};
|
|
64
|
-
const userAuto = userRoadmap.rerankerAutoTrigger ?? {};
|
|
65
|
-
if (userAuto.enabled === void 0 && raw.rerankerEnabled !== void 0) {
|
|
66
|
-
userAuto.enabled = raw.rerankerEnabled;
|
|
67
|
-
}
|
|
68
|
-
raw.scalingRoadmap = {
|
|
69
|
-
...userRoadmap,
|
|
70
|
-
rerankerAutoTrigger: { ...defaultAuto, ...userAuto }
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
function normalizeSessionLifecycle(raw) {
|
|
74
|
-
const defaultSL = DEFAULT_CONFIG.sessionLifecycle;
|
|
75
|
-
const userSL = raw.sessionLifecycle ?? {};
|
|
76
|
-
raw.sessionLifecycle = { ...defaultSL, ...userSL };
|
|
77
|
-
}
|
|
78
|
-
function normalizeAutoUpdate(raw) {
|
|
79
|
-
const defaultAU = DEFAULT_CONFIG.autoUpdate;
|
|
80
|
-
const userAU = raw.autoUpdate ?? {};
|
|
81
|
-
raw.autoUpdate = { ...defaultAU, ...userAU };
|
|
42
|
+
throw lastError;
|
|
82
43
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
89
|
-
}
|
|
90
|
-
const raw = await readFile2(configPath, "utf-8");
|
|
91
|
-
try {
|
|
92
|
-
let parsed = JSON.parse(raw);
|
|
93
|
-
parsed = migrateLegacyConfig(parsed);
|
|
94
|
-
const { config: migratedCfg, migrated, fromVersion } = migrateConfig(parsed);
|
|
95
|
-
if (migrated) {
|
|
96
|
-
process.stderr.write(`[exe-os] Config migrated from v${fromVersion} to v${migratedCfg.config_version}
|
|
97
|
-
`);
|
|
98
|
-
try {
|
|
99
|
-
await writeFile2(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
|
|
100
|
-
} catch {
|
|
44
|
+
function wrapWithRetry(client) {
|
|
45
|
+
return new Proxy(client, {
|
|
46
|
+
get(target, prop, receiver) {
|
|
47
|
+
if (prop === "execute") {
|
|
48
|
+
return (sql) => retryOnBusy(() => target.execute(sql), "execute");
|
|
101
49
|
}
|
|
50
|
+
if (prop === "batch") {
|
|
51
|
+
return (stmts) => retryOnBusy(() => target.batch(stmts), "batch");
|
|
52
|
+
}
|
|
53
|
+
return Reflect.get(target, prop, receiver);
|
|
102
54
|
}
|
|
103
|
-
|
|
104
|
-
normalizeSessionLifecycle(migratedCfg);
|
|
105
|
-
normalizeAutoUpdate(migratedCfg);
|
|
106
|
-
const config = { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
|
|
107
|
-
if (config.dbPath.startsWith("~")) {
|
|
108
|
-
config.dbPath = config.dbPath.replace(/^~/, os2.homedir());
|
|
109
|
-
}
|
|
110
|
-
return config;
|
|
111
|
-
} catch {
|
|
112
|
-
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
113
|
-
}
|
|
55
|
+
});
|
|
114
56
|
}
|
|
115
|
-
var
|
|
116
|
-
var
|
|
117
|
-
"src/lib/
|
|
57
|
+
var MAX_RETRIES, BASE_DELAY_MS, MAX_JITTER_MS;
|
|
58
|
+
var init_db_retry = __esm({
|
|
59
|
+
"src/lib/db-retry.ts"() {
|
|
118
60
|
"use strict";
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
CONFIG_PATH = path2.join(EXE_AI_DIR, "config.json");
|
|
123
|
-
LEGACY_LANCE_PATH = path2.join(EXE_AI_DIR, "local.lance");
|
|
124
|
-
CURRENT_CONFIG_VERSION = 1;
|
|
125
|
-
DEFAULT_CONFIG = {
|
|
126
|
-
config_version: CURRENT_CONFIG_VERSION,
|
|
127
|
-
dbPath: DB_PATH,
|
|
128
|
-
modelFile: "jina-embeddings-v5-small-q4_k_m.gguf",
|
|
129
|
-
embeddingDim: 1024,
|
|
130
|
-
batchSize: 20,
|
|
131
|
-
flushIntervalMs: 1e4,
|
|
132
|
-
autoIngestion: true,
|
|
133
|
-
autoRetrieval: true,
|
|
134
|
-
searchMode: "hybrid",
|
|
135
|
-
hookSearchMode: "hybrid",
|
|
136
|
-
fileGrepEnabled: true,
|
|
137
|
-
splashEffect: true,
|
|
138
|
-
consolidationEnabled: true,
|
|
139
|
-
consolidationIntervalMs: 6 * 60 * 60 * 1e3,
|
|
140
|
-
consolidationModel: "claude-haiku-4-5-20251001",
|
|
141
|
-
consolidationMaxCallsPerRun: 20,
|
|
142
|
-
selfQueryRouter: true,
|
|
143
|
-
selfQueryModel: "claude-haiku-4-5-20251001",
|
|
144
|
-
rerankerEnabled: true,
|
|
145
|
-
scalingRoadmap: {
|
|
146
|
-
rerankerAutoTrigger: {
|
|
147
|
-
enabled: true,
|
|
148
|
-
broadQueryMinCardinality: 5e4,
|
|
149
|
-
fetchTopK: 150,
|
|
150
|
-
returnTopK: 5
|
|
151
|
-
}
|
|
152
|
-
},
|
|
153
|
-
graphRagEnabled: true,
|
|
154
|
-
wikiEnabled: false,
|
|
155
|
-
wikiUrl: "",
|
|
156
|
-
wikiApiKey: "",
|
|
157
|
-
wikiSyncIntervalMs: 30 * 60 * 1e3,
|
|
158
|
-
wikiWorkspaceMapping: {
|
|
159
|
-
exe: "Executive",
|
|
160
|
-
yoshi: "Engineering",
|
|
161
|
-
mari: "Marketing",
|
|
162
|
-
tom: "Engineering",
|
|
163
|
-
sasha: "Production"
|
|
164
|
-
},
|
|
165
|
-
wikiAutoUpdate: true,
|
|
166
|
-
wikiAutoUpdateThreshold: 0.5,
|
|
167
|
-
wikiAutoUpdateCreateNew: true,
|
|
168
|
-
skillLearning: true,
|
|
169
|
-
skillThreshold: 3,
|
|
170
|
-
skillModel: "claude-haiku-4-5-20251001",
|
|
171
|
-
exeHeartbeat: {
|
|
172
|
-
enabled: true,
|
|
173
|
-
intervalSeconds: 60,
|
|
174
|
-
staleInProgressThresholdHours: 2
|
|
175
|
-
},
|
|
176
|
-
sessionLifecycle: {
|
|
177
|
-
idleKillEnabled: true,
|
|
178
|
-
idleKillTicksRequired: 3,
|
|
179
|
-
idleKillIntercomAckWindowMs: 1e4,
|
|
180
|
-
maxAutoInstances: 10
|
|
181
|
-
},
|
|
182
|
-
autoUpdate: {
|
|
183
|
-
checkOnBoot: true,
|
|
184
|
-
autoInstall: false,
|
|
185
|
-
checkIntervalMs: 24 * 60 * 60 * 1e3
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
CONFIG_MIGRATIONS = [
|
|
189
|
-
{
|
|
190
|
-
from: 0,
|
|
191
|
-
to: 1,
|
|
192
|
-
migrate: (cfg) => {
|
|
193
|
-
cfg.config_version = 1;
|
|
194
|
-
return cfg;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
];
|
|
61
|
+
MAX_RETRIES = 3;
|
|
62
|
+
BASE_DELAY_MS = 200;
|
|
63
|
+
MAX_JITTER_MS = 300;
|
|
198
64
|
}
|
|
199
65
|
});
|
|
200
66
|
|
|
201
|
-
// src/lib/
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
getShardsDir: () => getShardsDir,
|
|
209
|
-
initShardManager: () => initShardManager,
|
|
210
|
-
isShardingEnabled: () => isShardingEnabled,
|
|
211
|
-
listShards: () => listShards,
|
|
212
|
-
shardExists: () => shardExists
|
|
213
|
-
});
|
|
214
|
-
import path3 from "path";
|
|
215
|
-
import { existsSync as existsSync3, mkdirSync, readdirSync } from "fs";
|
|
216
|
-
import { createClient as createClient2 } from "@libsql/client";
|
|
217
|
-
function initShardManager(encryptionKey) {
|
|
218
|
-
_encryptionKey = encryptionKey;
|
|
219
|
-
if (!existsSync3(SHARDS_DIR)) {
|
|
220
|
-
mkdirSync(SHARDS_DIR, { recursive: true });
|
|
67
|
+
// src/lib/database.ts
|
|
68
|
+
import { createClient } from "@libsql/client";
|
|
69
|
+
async function initDatabase(config) {
|
|
70
|
+
if (_client) {
|
|
71
|
+
_client.close();
|
|
72
|
+
_client = null;
|
|
73
|
+
_resilientClient = null;
|
|
221
74
|
}
|
|
222
|
-
|
|
75
|
+
const opts = {
|
|
76
|
+
url: `file:${config.dbPath}`
|
|
77
|
+
};
|
|
78
|
+
if (config.encryptionKey) {
|
|
79
|
+
opts.encryptionKey = config.encryptionKey;
|
|
80
|
+
}
|
|
81
|
+
_client = createClient(opts);
|
|
82
|
+
_resilientClient = wrapWithRetry(_client);
|
|
223
83
|
}
|
|
224
|
-
function
|
|
225
|
-
|
|
84
|
+
function getClient() {
|
|
85
|
+
if (!_resilientClient) {
|
|
86
|
+
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
87
|
+
}
|
|
88
|
+
return _resilientClient;
|
|
226
89
|
}
|
|
227
|
-
function
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
function getShardClient(projectName) {
|
|
231
|
-
if (!_encryptionKey) {
|
|
232
|
-
throw new Error("Shard manager not initialized. Call initShardManager() first.");
|
|
233
|
-
}
|
|
234
|
-
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
235
|
-
if (!safeName) {
|
|
236
|
-
throw new Error(`Invalid project name for shard: "${projectName}"`);
|
|
90
|
+
function getRawClient() {
|
|
91
|
+
if (!_client) {
|
|
92
|
+
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
237
93
|
}
|
|
238
|
-
|
|
239
|
-
if (cached) return cached;
|
|
240
|
-
const dbPath = path3.join(SHARDS_DIR, `${safeName}.db`);
|
|
241
|
-
const client = createClient2({
|
|
242
|
-
url: `file:${dbPath}`,
|
|
243
|
-
encryptionKey: _encryptionKey
|
|
244
|
-
});
|
|
245
|
-
_shards.set(safeName, client);
|
|
246
|
-
return client;
|
|
247
|
-
}
|
|
248
|
-
function shardExists(projectName) {
|
|
249
|
-
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
250
|
-
return existsSync3(path3.join(SHARDS_DIR, `${safeName}.db`));
|
|
251
|
-
}
|
|
252
|
-
function listShards() {
|
|
253
|
-
if (!existsSync3(SHARDS_DIR)) return [];
|
|
254
|
-
return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
94
|
+
return _client;
|
|
255
95
|
}
|
|
256
|
-
async function
|
|
96
|
+
async function ensureSchema() {
|
|
97
|
+
const client = getRawClient();
|
|
257
98
|
await client.execute("PRAGMA journal_mode = WAL");
|
|
258
99
|
await client.execute("PRAGMA busy_timeout = 30000");
|
|
100
|
+
await client.execute("PRAGMA wal_autocheckpoint = 1000");
|
|
259
101
|
try {
|
|
260
102
|
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
261
103
|
} catch {
|
|
@@ -275,9 +117,26 @@ async function ensureShardSchema(client) {
|
|
|
275
117
|
version INTEGER NOT NULL DEFAULT 0
|
|
276
118
|
);
|
|
277
119
|
|
|
278
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent
|
|
279
|
-
|
|
280
|
-
|
|
120
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent
|
|
121
|
+
ON memories(agent_id);
|
|
122
|
+
|
|
123
|
+
CREATE INDEX IF NOT EXISTS idx_memories_timestamp
|
|
124
|
+
ON memories(timestamp);
|
|
125
|
+
|
|
126
|
+
CREATE INDEX IF NOT EXISTS idx_memories_session
|
|
127
|
+
ON memories(session_id);
|
|
128
|
+
|
|
129
|
+
CREATE INDEX IF NOT EXISTS idx_memories_project
|
|
130
|
+
ON memories(project_name);
|
|
131
|
+
|
|
132
|
+
CREATE INDEX IF NOT EXISTS idx_memories_tool
|
|
133
|
+
ON memories(tool_name);
|
|
134
|
+
|
|
135
|
+
CREATE INDEX IF NOT EXISTS idx_memories_version
|
|
136
|
+
ON memories(version);
|
|
137
|
+
|
|
138
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent_project
|
|
139
|
+
ON memories(agent_id, project_name);
|
|
281
140
|
`);
|
|
282
141
|
await client.executeMultiple(`
|
|
283
142
|
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
@@ -299,377 +158,77 @@ async function ensureShardSchema(client) {
|
|
|
299
158
|
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
300
159
|
END;
|
|
301
160
|
`);
|
|
302
|
-
for (const col of [
|
|
303
|
-
"ALTER TABLE memories ADD COLUMN task_id TEXT",
|
|
304
|
-
"ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
|
|
305
|
-
"ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
|
|
306
|
-
"ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
|
|
307
|
-
"ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
|
|
308
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0",
|
|
309
|
-
"ALTER TABLE memories ADD COLUMN content_hash TEXT",
|
|
310
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT",
|
|
311
|
-
"ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7",
|
|
312
|
-
"ALTER TABLE memories ADD COLUMN last_accessed TEXT",
|
|
313
|
-
// Wiki linkage columns (must match database.ts)
|
|
314
|
-
"ALTER TABLE memories ADD COLUMN workspace_id TEXT",
|
|
315
|
-
"ALTER TABLE memories ADD COLUMN document_id TEXT",
|
|
316
|
-
"ALTER TABLE memories ADD COLUMN user_id TEXT",
|
|
317
|
-
"ALTER TABLE memories ADD COLUMN char_offset INTEGER",
|
|
318
|
-
"ALTER TABLE memories ADD COLUMN page_number INTEGER",
|
|
319
|
-
// Source provenance columns (must match database.ts)
|
|
320
|
-
"ALTER TABLE memories ADD COLUMN source_path TEXT",
|
|
321
|
-
"ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'",
|
|
322
|
-
"ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3",
|
|
323
|
-
"ALTER TABLE memories ADD COLUMN supersedes_id TEXT"
|
|
324
|
-
]) {
|
|
325
|
-
try {
|
|
326
|
-
await client.execute(col);
|
|
327
|
-
} catch {
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
for (const idx of [
|
|
331
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)",
|
|
332
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL"
|
|
333
|
-
]) {
|
|
334
|
-
try {
|
|
335
|
-
await client.execute(idx);
|
|
336
|
-
} catch {
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
try {
|
|
340
|
-
await client.execute("CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)");
|
|
341
|
-
} catch {
|
|
342
|
-
}
|
|
343
|
-
for (const idx of [
|
|
344
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_workspace ON memories(workspace_id)",
|
|
345
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_document ON memories(document_id)",
|
|
346
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)"
|
|
347
|
-
]) {
|
|
348
|
-
try {
|
|
349
|
-
await client.execute(idx);
|
|
350
|
-
} catch {
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
161
|
await client.executeMultiple(`
|
|
354
|
-
CREATE TABLE IF NOT EXISTS
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
type TEXT NOT NULL,
|
|
358
|
-
first_seen TEXT NOT NULL,
|
|
359
|
-
last_seen TEXT NOT NULL,
|
|
360
|
-
properties TEXT DEFAULT '{}',
|
|
361
|
-
UNIQUE(name, type)
|
|
362
|
-
);
|
|
363
|
-
|
|
364
|
-
CREATE TABLE IF NOT EXISTS relationships (
|
|
365
|
-
id TEXT PRIMARY KEY,
|
|
366
|
-
source_entity_id TEXT NOT NULL,
|
|
367
|
-
target_entity_id TEXT NOT NULL,
|
|
368
|
-
type TEXT NOT NULL,
|
|
369
|
-
weight REAL DEFAULT 1.0,
|
|
370
|
-
timestamp TEXT NOT NULL,
|
|
371
|
-
properties TEXT DEFAULT '{}',
|
|
372
|
-
UNIQUE(source_entity_id, target_entity_id, type)
|
|
373
|
-
);
|
|
374
|
-
|
|
375
|
-
CREATE TABLE IF NOT EXISTS entity_memories (
|
|
376
|
-
entity_id TEXT NOT NULL,
|
|
377
|
-
memory_id TEXT NOT NULL,
|
|
378
|
-
PRIMARY KEY (entity_id, memory_id)
|
|
162
|
+
CREATE TABLE IF NOT EXISTS sync_meta (
|
|
163
|
+
key TEXT PRIMARY KEY,
|
|
164
|
+
value TEXT NOT NULL
|
|
379
165
|
);
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
166
|
+
`);
|
|
167
|
+
await client.executeMultiple(`
|
|
168
|
+
CREATE TABLE IF NOT EXISTS tasks (
|
|
169
|
+
id TEXT PRIMARY KEY,
|
|
170
|
+
title TEXT NOT NULL,
|
|
171
|
+
assigned_to TEXT NOT NULL,
|
|
172
|
+
assigned_by TEXT NOT NULL,
|
|
173
|
+
project_name TEXT NOT NULL,
|
|
174
|
+
priority TEXT NOT NULL DEFAULT 'p1',
|
|
175
|
+
status TEXT NOT NULL DEFAULT 'open',
|
|
176
|
+
task_file TEXT,
|
|
177
|
+
created_at TEXT NOT NULL,
|
|
178
|
+
updated_at TEXT NOT NULL
|
|
385
179
|
);
|
|
386
180
|
|
|
387
|
-
CREATE INDEX IF NOT EXISTS
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
CREATE
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
181
|
+
CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
|
|
182
|
+
ON tasks(assigned_to, status);
|
|
183
|
+
`);
|
|
184
|
+
await client.executeMultiple(`
|
|
185
|
+
CREATE TABLE IF NOT EXISTS behaviors (
|
|
186
|
+
id TEXT PRIMARY KEY,
|
|
187
|
+
agent_id TEXT NOT NULL,
|
|
188
|
+
project_name TEXT,
|
|
189
|
+
domain TEXT,
|
|
190
|
+
content TEXT NOT NULL,
|
|
191
|
+
active INTEGER NOT NULL DEFAULT 1,
|
|
192
|
+
created_at TEXT NOT NULL,
|
|
193
|
+
updated_at TEXT NOT NULL
|
|
399
194
|
);
|
|
400
195
|
|
|
401
|
-
CREATE
|
|
402
|
-
|
|
403
|
-
entity_id TEXT NOT NULL,
|
|
404
|
-
PRIMARY KEY (hyperedge_id, entity_id)
|
|
405
|
-
);
|
|
196
|
+
CREATE INDEX IF NOT EXISTS idx_behaviors_agent
|
|
197
|
+
ON behaviors(agent_id, active);
|
|
406
198
|
`);
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
199
|
+
try {
|
|
200
|
+
const existing = await client.execute({
|
|
201
|
+
sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
|
|
202
|
+
args: []
|
|
203
|
+
});
|
|
204
|
+
if (Number(existing.rows[0]?.cnt) === 0) {
|
|
205
|
+
await client.executeMultiple(`
|
|
206
|
+
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
207
|
+
VALUES
|
|
208
|
+
(hex(randomblob(16)), 'exe', NULL, 'workflow', 'Don''t ask "keep going?" \u2014 just keep executing phases/plans autonomously', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
209
|
+
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
210
|
+
VALUES
|
|
211
|
+
(hex(randomblob(16)), 'exe', NULL, 'tool-use', 'Always use create_task MCP tool, never write .md files directly for task creation', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
212
|
+
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
213
|
+
VALUES
|
|
214
|
+
(hex(randomblob(16)), 'exe', NULL, 'workflow', 'Auto-start reviewing when idle and reviews are pending \u2014 never ask founder for permission', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
215
|
+
`);
|
|
414
216
|
}
|
|
217
|
+
} catch {
|
|
415
218
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
function disposeShards() {
|
|
423
|
-
for (const [, client] of _shards) {
|
|
424
|
-
client.close();
|
|
219
|
+
try {
|
|
220
|
+
await client.execute({
|
|
221
|
+
sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
|
|
222
|
+
args: []
|
|
223
|
+
});
|
|
224
|
+
} catch {
|
|
425
225
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
"src/lib/shard-manager.ts"() {
|
|
433
|
-
"use strict";
|
|
434
|
-
init_config();
|
|
435
|
-
SHARDS_DIR = path3.join(EXE_AI_DIR, "shards");
|
|
436
|
-
_shards = /* @__PURE__ */ new Map();
|
|
437
|
-
_encryptionKey = null;
|
|
438
|
-
_shardingEnabled = false;
|
|
439
|
-
}
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
// src/bin/backfill-conversations.ts
|
|
443
|
-
import crypto2 from "crypto";
|
|
444
|
-
import { createReadStream } from "fs";
|
|
445
|
-
import { readdir, stat } from "fs/promises";
|
|
446
|
-
import path5 from "path";
|
|
447
|
-
import { createInterface } from "readline";
|
|
448
|
-
import { homedir } from "os";
|
|
449
|
-
import { parseArgs } from "util";
|
|
450
|
-
|
|
451
|
-
// src/types/memory.ts
|
|
452
|
-
var EMBEDDING_DIM = 1024;
|
|
453
|
-
|
|
454
|
-
// src/lib/database.ts
|
|
455
|
-
import { createClient } from "@libsql/client";
|
|
456
|
-
|
|
457
|
-
// src/lib/db-retry.ts
|
|
458
|
-
var MAX_RETRIES = 3;
|
|
459
|
-
var BASE_DELAY_MS = 200;
|
|
460
|
-
var MAX_JITTER_MS = 300;
|
|
461
|
-
function isBusyError(err) {
|
|
462
|
-
if (err instanceof Error) {
|
|
463
|
-
const msg = err.message.toLowerCase();
|
|
464
|
-
return msg.includes("sqlite_busy") || msg.includes("database is locked");
|
|
465
|
-
}
|
|
466
|
-
return false;
|
|
467
|
-
}
|
|
468
|
-
function delay(ms) {
|
|
469
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
470
|
-
}
|
|
471
|
-
async function retryOnBusy(fn, label) {
|
|
472
|
-
let lastError;
|
|
473
|
-
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
474
|
-
try {
|
|
475
|
-
return await fn();
|
|
476
|
-
} catch (err) {
|
|
477
|
-
lastError = err;
|
|
478
|
-
if (!isBusyError(err) || attempt === MAX_RETRIES) {
|
|
479
|
-
throw err;
|
|
480
|
-
}
|
|
481
|
-
const backoff = BASE_DELAY_MS * Math.pow(2, attempt);
|
|
482
|
-
const jitter = Math.floor(Math.random() * MAX_JITTER_MS);
|
|
483
|
-
process.stderr.write(
|
|
484
|
-
`[exe-os] SQLITE_BUSY ${label} retry ${attempt + 1}/${MAX_RETRIES} \u2014 waiting ${backoff + jitter}ms
|
|
485
|
-
`
|
|
486
|
-
);
|
|
487
|
-
await delay(backoff + jitter);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
throw lastError;
|
|
491
|
-
}
|
|
492
|
-
function wrapWithRetry(client) {
|
|
493
|
-
return new Proxy(client, {
|
|
494
|
-
get(target, prop, receiver) {
|
|
495
|
-
if (prop === "execute") {
|
|
496
|
-
return (sql) => retryOnBusy(() => target.execute(sql), "execute");
|
|
497
|
-
}
|
|
498
|
-
if (prop === "batch") {
|
|
499
|
-
return (stmts) => retryOnBusy(() => target.batch(stmts), "batch");
|
|
500
|
-
}
|
|
501
|
-
return Reflect.get(target, prop, receiver);
|
|
502
|
-
}
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
// src/lib/database.ts
|
|
507
|
-
var _client = null;
|
|
508
|
-
var _resilientClient = null;
|
|
509
|
-
var initTurso = initDatabase;
|
|
510
|
-
async function initDatabase(config) {
|
|
511
|
-
if (_client) {
|
|
512
|
-
_client.close();
|
|
513
|
-
_client = null;
|
|
514
|
-
_resilientClient = null;
|
|
515
|
-
}
|
|
516
|
-
const opts = {
|
|
517
|
-
url: `file:${config.dbPath}`
|
|
518
|
-
};
|
|
519
|
-
if (config.encryptionKey) {
|
|
520
|
-
opts.encryptionKey = config.encryptionKey;
|
|
521
|
-
}
|
|
522
|
-
_client = createClient(opts);
|
|
523
|
-
_resilientClient = wrapWithRetry(_client);
|
|
524
|
-
}
|
|
525
|
-
function getClient() {
|
|
526
|
-
if (!_resilientClient) {
|
|
527
|
-
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
528
|
-
}
|
|
529
|
-
return _resilientClient;
|
|
530
|
-
}
|
|
531
|
-
function getRawClient() {
|
|
532
|
-
if (!_client) {
|
|
533
|
-
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
534
|
-
}
|
|
535
|
-
return _client;
|
|
536
|
-
}
|
|
537
|
-
async function ensureSchema() {
|
|
538
|
-
const client = getRawClient();
|
|
539
|
-
await client.execute("PRAGMA journal_mode = WAL");
|
|
540
|
-
await client.execute("PRAGMA busy_timeout = 30000");
|
|
541
|
-
await client.execute("PRAGMA wal_autocheckpoint = 1000");
|
|
542
|
-
try {
|
|
543
|
-
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
544
|
-
} catch {
|
|
545
|
-
}
|
|
546
|
-
await client.executeMultiple(`
|
|
547
|
-
CREATE TABLE IF NOT EXISTS memories (
|
|
548
|
-
id TEXT PRIMARY KEY,
|
|
549
|
-
agent_id TEXT NOT NULL,
|
|
550
|
-
agent_role TEXT NOT NULL,
|
|
551
|
-
session_id TEXT NOT NULL,
|
|
552
|
-
timestamp TEXT NOT NULL,
|
|
553
|
-
tool_name TEXT NOT NULL,
|
|
554
|
-
project_name TEXT NOT NULL,
|
|
555
|
-
has_error INTEGER NOT NULL DEFAULT 0,
|
|
556
|
-
raw_text TEXT NOT NULL,
|
|
557
|
-
vector F32_BLOB(1024),
|
|
558
|
-
version INTEGER NOT NULL DEFAULT 0
|
|
559
|
-
);
|
|
560
|
-
|
|
561
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent
|
|
562
|
-
ON memories(agent_id);
|
|
563
|
-
|
|
564
|
-
CREATE INDEX IF NOT EXISTS idx_memories_timestamp
|
|
565
|
-
ON memories(timestamp);
|
|
566
|
-
|
|
567
|
-
CREATE INDEX IF NOT EXISTS idx_memories_session
|
|
568
|
-
ON memories(session_id);
|
|
569
|
-
|
|
570
|
-
CREATE INDEX IF NOT EXISTS idx_memories_project
|
|
571
|
-
ON memories(project_name);
|
|
572
|
-
|
|
573
|
-
CREATE INDEX IF NOT EXISTS idx_memories_tool
|
|
574
|
-
ON memories(tool_name);
|
|
575
|
-
|
|
576
|
-
CREATE INDEX IF NOT EXISTS idx_memories_version
|
|
577
|
-
ON memories(version);
|
|
578
|
-
|
|
579
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent_project
|
|
580
|
-
ON memories(agent_id, project_name);
|
|
581
|
-
`);
|
|
582
|
-
await client.executeMultiple(`
|
|
583
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
584
|
-
raw_text,
|
|
585
|
-
content='memories',
|
|
586
|
-
content_rowid='rowid'
|
|
587
|
-
);
|
|
588
|
-
|
|
589
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
590
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
591
|
-
END;
|
|
592
|
-
|
|
593
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
594
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
595
|
-
END;
|
|
596
|
-
|
|
597
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
598
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
599
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
600
|
-
END;
|
|
601
|
-
`);
|
|
602
|
-
await client.executeMultiple(`
|
|
603
|
-
CREATE TABLE IF NOT EXISTS sync_meta (
|
|
604
|
-
key TEXT PRIMARY KEY,
|
|
605
|
-
value TEXT NOT NULL
|
|
606
|
-
);
|
|
607
|
-
`);
|
|
608
|
-
await client.executeMultiple(`
|
|
609
|
-
CREATE TABLE IF NOT EXISTS tasks (
|
|
610
|
-
id TEXT PRIMARY KEY,
|
|
611
|
-
title TEXT NOT NULL,
|
|
612
|
-
assigned_to TEXT NOT NULL,
|
|
613
|
-
assigned_by TEXT NOT NULL,
|
|
614
|
-
project_name TEXT NOT NULL,
|
|
615
|
-
priority TEXT NOT NULL DEFAULT 'p1',
|
|
616
|
-
status TEXT NOT NULL DEFAULT 'open',
|
|
617
|
-
task_file TEXT,
|
|
618
|
-
created_at TEXT NOT NULL,
|
|
619
|
-
updated_at TEXT NOT NULL
|
|
620
|
-
);
|
|
621
|
-
|
|
622
|
-
CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
|
|
623
|
-
ON tasks(assigned_to, status);
|
|
624
|
-
`);
|
|
625
|
-
await client.executeMultiple(`
|
|
626
|
-
CREATE TABLE IF NOT EXISTS behaviors (
|
|
627
|
-
id TEXT PRIMARY KEY,
|
|
628
|
-
agent_id TEXT NOT NULL,
|
|
629
|
-
project_name TEXT,
|
|
630
|
-
domain TEXT,
|
|
631
|
-
content TEXT NOT NULL,
|
|
632
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
633
|
-
created_at TEXT NOT NULL,
|
|
634
|
-
updated_at TEXT NOT NULL
|
|
635
|
-
);
|
|
636
|
-
|
|
637
|
-
CREATE INDEX IF NOT EXISTS idx_behaviors_agent
|
|
638
|
-
ON behaviors(agent_id, active);
|
|
639
|
-
`);
|
|
640
|
-
try {
|
|
641
|
-
const existing = await client.execute({
|
|
642
|
-
sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
|
|
643
|
-
args: []
|
|
644
|
-
});
|
|
645
|
-
if (Number(existing.rows[0]?.cnt) === 0) {
|
|
646
|
-
await client.executeMultiple(`
|
|
647
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
648
|
-
VALUES
|
|
649
|
-
(hex(randomblob(16)), 'exe', NULL, 'workflow', 'Don''t ask "keep going?" \u2014 just keep executing phases/plans autonomously', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
650
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
651
|
-
VALUES
|
|
652
|
-
(hex(randomblob(16)), 'exe', NULL, 'tool-use', 'Always use create_task MCP tool, never write .md files directly for task creation', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
653
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
654
|
-
VALUES
|
|
655
|
-
(hex(randomblob(16)), 'exe', NULL, 'workflow', 'Auto-start reviewing when idle and reviews are pending \u2014 never ask founder for permission', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
656
|
-
`);
|
|
657
|
-
}
|
|
658
|
-
} catch {
|
|
659
|
-
}
|
|
660
|
-
try {
|
|
661
|
-
await client.execute({
|
|
662
|
-
sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
|
|
663
|
-
args: []
|
|
664
|
-
});
|
|
665
|
-
} catch {
|
|
666
|
-
}
|
|
667
|
-
try {
|
|
668
|
-
await client.execute({
|
|
669
|
-
sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
|
|
670
|
-
args: []
|
|
671
|
-
});
|
|
672
|
-
} catch {
|
|
226
|
+
try {
|
|
227
|
+
await client.execute({
|
|
228
|
+
sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
|
|
229
|
+
args: []
|
|
230
|
+
});
|
|
231
|
+
} catch {
|
|
673
232
|
}
|
|
674
233
|
try {
|
|
675
234
|
await client.execute({
|
|
@@ -743,6 +302,13 @@ async function ensureSchema() {
|
|
|
743
302
|
});
|
|
744
303
|
} catch {
|
|
745
304
|
}
|
|
305
|
+
try {
|
|
306
|
+
await client.execute({
|
|
307
|
+
sql: `ALTER TABLE tasks ADD COLUMN session_scope TEXT`,
|
|
308
|
+
args: []
|
|
309
|
+
});
|
|
310
|
+
} catch {
|
|
311
|
+
}
|
|
746
312
|
try {
|
|
747
313
|
await client.execute({
|
|
748
314
|
sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
|
|
@@ -1189,6 +755,18 @@ async function ensureSchema() {
|
|
|
1189
755
|
CREATE INDEX IF NOT EXISTS idx_session_kills_agent
|
|
1190
756
|
ON session_kills(agent_id);
|
|
1191
757
|
`);
|
|
758
|
+
await client.execute(`
|
|
759
|
+
CREATE TABLE IF NOT EXISTS global_procedures (
|
|
760
|
+
id TEXT PRIMARY KEY,
|
|
761
|
+
title TEXT NOT NULL,
|
|
762
|
+
content TEXT NOT NULL,
|
|
763
|
+
priority TEXT NOT NULL DEFAULT 'p0',
|
|
764
|
+
domain TEXT,
|
|
765
|
+
active INTEGER NOT NULL DEFAULT 1,
|
|
766
|
+
created_at TEXT NOT NULL,
|
|
767
|
+
updated_at TEXT NOT NULL
|
|
768
|
+
)
|
|
769
|
+
`);
|
|
1192
770
|
await client.executeMultiple(`
|
|
1193
771
|
CREATE TABLE IF NOT EXISTS conversations (
|
|
1194
772
|
id TEXT PRIMARY KEY,
|
|
@@ -1255,71 +833,487 @@ async function ensureSchema() {
|
|
|
1255
833
|
} catch {
|
|
1256
834
|
}
|
|
1257
835
|
await client.executeMultiple(`
|
|
1258
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
|
|
1259
|
-
content_text,
|
|
1260
|
-
sender_name,
|
|
1261
|
-
agent_response,
|
|
1262
|
-
content='conversations',
|
|
836
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
|
|
837
|
+
content_text,
|
|
838
|
+
sender_name,
|
|
839
|
+
agent_response,
|
|
840
|
+
content='conversations',
|
|
841
|
+
content_rowid='rowid'
|
|
842
|
+
);
|
|
843
|
+
|
|
844
|
+
CREATE TRIGGER IF NOT EXISTS conversations_fts_ai AFTER INSERT ON conversations BEGIN
|
|
845
|
+
INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
|
|
846
|
+
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
847
|
+
END;
|
|
848
|
+
|
|
849
|
+
CREATE TRIGGER IF NOT EXISTS conversations_fts_ad AFTER DELETE ON conversations BEGIN
|
|
850
|
+
INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
|
|
851
|
+
VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
|
|
852
|
+
END;
|
|
853
|
+
|
|
854
|
+
CREATE TRIGGER IF NOT EXISTS conversations_fts_au AFTER UPDATE ON conversations BEGIN
|
|
855
|
+
INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
|
|
856
|
+
VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
|
|
857
|
+
INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
|
|
858
|
+
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
859
|
+
END;
|
|
860
|
+
`);
|
|
861
|
+
try {
|
|
862
|
+
await client.execute({
|
|
863
|
+
sql: `ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3`,
|
|
864
|
+
args: []
|
|
865
|
+
});
|
|
866
|
+
} catch {
|
|
867
|
+
}
|
|
868
|
+
try {
|
|
869
|
+
await client.execute(
|
|
870
|
+
`CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)`
|
|
871
|
+
);
|
|
872
|
+
} catch {
|
|
873
|
+
}
|
|
874
|
+
try {
|
|
875
|
+
await client.execute({
|
|
876
|
+
sql: `UPDATE memories SET tier = 1 WHERE tool_name = 'commit_to_long_term_memory' AND importance >= 8 AND tier = 3`,
|
|
877
|
+
args: []
|
|
878
|
+
});
|
|
879
|
+
await client.execute({
|
|
880
|
+
sql: `UPDATE memories SET tier = 2 WHERE tool_name IN ('store_memory', 'manual') AND importance >= 5 AND tier = 3`,
|
|
881
|
+
args: []
|
|
882
|
+
});
|
|
883
|
+
} catch {
|
|
884
|
+
}
|
|
885
|
+
try {
|
|
886
|
+
await client.execute({
|
|
887
|
+
sql: `ALTER TABLE memories ADD COLUMN supersedes_id TEXT`,
|
|
888
|
+
args: []
|
|
889
|
+
});
|
|
890
|
+
} catch {
|
|
891
|
+
}
|
|
892
|
+
try {
|
|
893
|
+
await client.execute(
|
|
894
|
+
`CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL`
|
|
895
|
+
);
|
|
896
|
+
} catch {
|
|
897
|
+
}
|
|
898
|
+
for (const col of [
|
|
899
|
+
"ALTER TABLE tasks ADD COLUMN checkpoint TEXT",
|
|
900
|
+
"ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER DEFAULT 0"
|
|
901
|
+
]) {
|
|
902
|
+
try {
|
|
903
|
+
await client.execute(col);
|
|
904
|
+
} catch {
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
var _client, _resilientClient, initTurso;
|
|
909
|
+
var init_database = __esm({
|
|
910
|
+
"src/lib/database.ts"() {
|
|
911
|
+
"use strict";
|
|
912
|
+
init_db_retry();
|
|
913
|
+
_client = null;
|
|
914
|
+
_resilientClient = null;
|
|
915
|
+
initTurso = initDatabase;
|
|
916
|
+
}
|
|
917
|
+
});
|
|
918
|
+
|
|
919
|
+
// src/lib/config.ts
|
|
920
|
+
import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2, chmod as chmod2 } from "fs/promises";
|
|
921
|
+
import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
|
|
922
|
+
import path2 from "path";
|
|
923
|
+
import os2 from "os";
|
|
924
|
+
function resolveDataDir() {
|
|
925
|
+
if (process.env.EXE_OS_DIR) return process.env.EXE_OS_DIR;
|
|
926
|
+
if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
|
|
927
|
+
const newDir = path2.join(os2.homedir(), ".exe-os");
|
|
928
|
+
const legacyDir = path2.join(os2.homedir(), ".exe-mem");
|
|
929
|
+
if (!existsSync2(newDir) && existsSync2(legacyDir)) {
|
|
930
|
+
try {
|
|
931
|
+
renameSync(legacyDir, newDir);
|
|
932
|
+
process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
|
|
933
|
+
`);
|
|
934
|
+
} catch {
|
|
935
|
+
return legacyDir;
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
return newDir;
|
|
939
|
+
}
|
|
940
|
+
function migrateLegacyConfig(raw) {
|
|
941
|
+
if ("r2" in raw) {
|
|
942
|
+
process.stderr.write(
|
|
943
|
+
"[exe-os] Warning: config.json contains deprecated 'r2' field from v1.0. R2 sync has been replaced in v1.1. The 'r2' field will be ignored.\n"
|
|
944
|
+
);
|
|
945
|
+
delete raw.r2;
|
|
946
|
+
}
|
|
947
|
+
if ("syncIntervalMs" in raw) {
|
|
948
|
+
delete raw.syncIntervalMs;
|
|
949
|
+
}
|
|
950
|
+
return raw;
|
|
951
|
+
}
|
|
952
|
+
function migrateConfig(raw) {
|
|
953
|
+
const fromVersion = typeof raw.config_version === "number" ? raw.config_version : 0;
|
|
954
|
+
let currentVersion = fromVersion;
|
|
955
|
+
let migrated = false;
|
|
956
|
+
if (currentVersion > CURRENT_CONFIG_VERSION) {
|
|
957
|
+
return { config: raw, migrated: false, fromVersion };
|
|
958
|
+
}
|
|
959
|
+
for (const migration of CONFIG_MIGRATIONS) {
|
|
960
|
+
if (currentVersion === migration.from && migration.to <= CURRENT_CONFIG_VERSION) {
|
|
961
|
+
raw = migration.migrate(raw);
|
|
962
|
+
currentVersion = migration.to;
|
|
963
|
+
migrated = true;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
return { config: raw, migrated, fromVersion };
|
|
967
|
+
}
|
|
968
|
+
function normalizeScalingRoadmap(raw) {
|
|
969
|
+
const defaultAuto = DEFAULT_CONFIG.scalingRoadmap.rerankerAutoTrigger;
|
|
970
|
+
const userRoadmap = raw.scalingRoadmap ?? {};
|
|
971
|
+
const userAuto = userRoadmap.rerankerAutoTrigger ?? {};
|
|
972
|
+
if (userAuto.enabled === void 0 && raw.rerankerEnabled !== void 0) {
|
|
973
|
+
userAuto.enabled = raw.rerankerEnabled;
|
|
974
|
+
}
|
|
975
|
+
raw.scalingRoadmap = {
|
|
976
|
+
...userRoadmap,
|
|
977
|
+
rerankerAutoTrigger: { ...defaultAuto, ...userAuto }
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
function normalizeSessionLifecycle(raw) {
|
|
981
|
+
const defaultSL = DEFAULT_CONFIG.sessionLifecycle;
|
|
982
|
+
const userSL = raw.sessionLifecycle ?? {};
|
|
983
|
+
raw.sessionLifecycle = { ...defaultSL, ...userSL };
|
|
984
|
+
}
|
|
985
|
+
function normalizeAutoUpdate(raw) {
|
|
986
|
+
const defaultAU = DEFAULT_CONFIG.autoUpdate;
|
|
987
|
+
const userAU = raw.autoUpdate ?? {};
|
|
988
|
+
raw.autoUpdate = { ...defaultAU, ...userAU };
|
|
989
|
+
}
|
|
990
|
+
async function loadConfig() {
|
|
991
|
+
const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
|
|
992
|
+
await mkdir2(dir, { recursive: true });
|
|
993
|
+
const configPath = path2.join(dir, "config.json");
|
|
994
|
+
if (!existsSync2(configPath)) {
|
|
995
|
+
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
996
|
+
}
|
|
997
|
+
const raw = await readFile2(configPath, "utf-8");
|
|
998
|
+
try {
|
|
999
|
+
let parsed = JSON.parse(raw);
|
|
1000
|
+
parsed = migrateLegacyConfig(parsed);
|
|
1001
|
+
const { config: migratedCfg, migrated, fromVersion } = migrateConfig(parsed);
|
|
1002
|
+
if (migrated) {
|
|
1003
|
+
process.stderr.write(`[exe-os] Config migrated from v${fromVersion} to v${migratedCfg.config_version}
|
|
1004
|
+
`);
|
|
1005
|
+
try {
|
|
1006
|
+
await writeFile2(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
|
|
1007
|
+
} catch {
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
normalizeScalingRoadmap(migratedCfg);
|
|
1011
|
+
normalizeSessionLifecycle(migratedCfg);
|
|
1012
|
+
normalizeAutoUpdate(migratedCfg);
|
|
1013
|
+
const config = { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
|
|
1014
|
+
if (config.dbPath.startsWith("~")) {
|
|
1015
|
+
config.dbPath = config.dbPath.replace(/^~/, os2.homedir());
|
|
1016
|
+
}
|
|
1017
|
+
return config;
|
|
1018
|
+
} catch {
|
|
1019
|
+
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, LEGACY_LANCE_PATH, CURRENT_CONFIG_VERSION, DEFAULT_CONFIG, CONFIG_MIGRATIONS;
|
|
1023
|
+
var init_config = __esm({
|
|
1024
|
+
"src/lib/config.ts"() {
|
|
1025
|
+
"use strict";
|
|
1026
|
+
EXE_AI_DIR = resolveDataDir();
|
|
1027
|
+
DB_PATH = path2.join(EXE_AI_DIR, "memories.db");
|
|
1028
|
+
MODELS_DIR = path2.join(EXE_AI_DIR, "models");
|
|
1029
|
+
CONFIG_PATH = path2.join(EXE_AI_DIR, "config.json");
|
|
1030
|
+
LEGACY_LANCE_PATH = path2.join(EXE_AI_DIR, "local.lance");
|
|
1031
|
+
CURRENT_CONFIG_VERSION = 1;
|
|
1032
|
+
DEFAULT_CONFIG = {
|
|
1033
|
+
config_version: CURRENT_CONFIG_VERSION,
|
|
1034
|
+
dbPath: DB_PATH,
|
|
1035
|
+
modelFile: "jina-embeddings-v5-small-q4_k_m.gguf",
|
|
1036
|
+
embeddingDim: 1024,
|
|
1037
|
+
batchSize: 20,
|
|
1038
|
+
flushIntervalMs: 1e4,
|
|
1039
|
+
autoIngestion: true,
|
|
1040
|
+
autoRetrieval: true,
|
|
1041
|
+
searchMode: "hybrid",
|
|
1042
|
+
hookSearchMode: "hybrid",
|
|
1043
|
+
fileGrepEnabled: true,
|
|
1044
|
+
splashEffect: true,
|
|
1045
|
+
consolidationEnabled: true,
|
|
1046
|
+
consolidationIntervalMs: 6 * 60 * 60 * 1e3,
|
|
1047
|
+
consolidationModel: "claude-haiku-4-5-20251001",
|
|
1048
|
+
consolidationMaxCallsPerRun: 20,
|
|
1049
|
+
selfQueryRouter: true,
|
|
1050
|
+
selfQueryModel: "claude-haiku-4-5-20251001",
|
|
1051
|
+
rerankerEnabled: true,
|
|
1052
|
+
scalingRoadmap: {
|
|
1053
|
+
rerankerAutoTrigger: {
|
|
1054
|
+
enabled: true,
|
|
1055
|
+
broadQueryMinCardinality: 5e4,
|
|
1056
|
+
fetchTopK: 150,
|
|
1057
|
+
returnTopK: 5
|
|
1058
|
+
}
|
|
1059
|
+
},
|
|
1060
|
+
graphRagEnabled: true,
|
|
1061
|
+
wikiEnabled: false,
|
|
1062
|
+
wikiUrl: "",
|
|
1063
|
+
wikiApiKey: "",
|
|
1064
|
+
wikiSyncIntervalMs: 30 * 60 * 1e3,
|
|
1065
|
+
wikiWorkspaceMapping: {
|
|
1066
|
+
exe: "Executive",
|
|
1067
|
+
yoshi: "Engineering",
|
|
1068
|
+
mari: "Marketing",
|
|
1069
|
+
tom: "Engineering",
|
|
1070
|
+
sasha: "Production"
|
|
1071
|
+
},
|
|
1072
|
+
wikiAutoUpdate: true,
|
|
1073
|
+
wikiAutoUpdateThreshold: 0.5,
|
|
1074
|
+
wikiAutoUpdateCreateNew: true,
|
|
1075
|
+
skillLearning: true,
|
|
1076
|
+
skillThreshold: 3,
|
|
1077
|
+
skillModel: "claude-haiku-4-5-20251001",
|
|
1078
|
+
exeHeartbeat: {
|
|
1079
|
+
enabled: true,
|
|
1080
|
+
intervalSeconds: 60,
|
|
1081
|
+
staleInProgressThresholdHours: 2
|
|
1082
|
+
},
|
|
1083
|
+
sessionLifecycle: {
|
|
1084
|
+
idleKillEnabled: true,
|
|
1085
|
+
idleKillTicksRequired: 3,
|
|
1086
|
+
idleKillIntercomAckWindowMs: 1e4,
|
|
1087
|
+
maxAutoInstances: 10
|
|
1088
|
+
},
|
|
1089
|
+
autoUpdate: {
|
|
1090
|
+
checkOnBoot: true,
|
|
1091
|
+
autoInstall: false,
|
|
1092
|
+
checkIntervalMs: 24 * 60 * 60 * 1e3
|
|
1093
|
+
}
|
|
1094
|
+
};
|
|
1095
|
+
CONFIG_MIGRATIONS = [
|
|
1096
|
+
{
|
|
1097
|
+
from: 0,
|
|
1098
|
+
to: 1,
|
|
1099
|
+
migrate: (cfg) => {
|
|
1100
|
+
cfg.config_version = 1;
|
|
1101
|
+
return cfg;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
];
|
|
1105
|
+
}
|
|
1106
|
+
});
|
|
1107
|
+
|
|
1108
|
+
// src/lib/shard-manager.ts
|
|
1109
|
+
var shard_manager_exports = {};
|
|
1110
|
+
__export(shard_manager_exports, {
|
|
1111
|
+
disposeShards: () => disposeShards,
|
|
1112
|
+
ensureShardSchema: () => ensureShardSchema,
|
|
1113
|
+
getReadyShardClient: () => getReadyShardClient,
|
|
1114
|
+
getShardClient: () => getShardClient,
|
|
1115
|
+
getShardsDir: () => getShardsDir,
|
|
1116
|
+
initShardManager: () => initShardManager,
|
|
1117
|
+
isShardingEnabled: () => isShardingEnabled,
|
|
1118
|
+
listShards: () => listShards,
|
|
1119
|
+
shardExists: () => shardExists
|
|
1120
|
+
});
|
|
1121
|
+
import path3 from "path";
|
|
1122
|
+
import { existsSync as existsSync3, mkdirSync, readdirSync } from "fs";
|
|
1123
|
+
import { createClient as createClient2 } from "@libsql/client";
|
|
1124
|
+
function initShardManager(encryptionKey) {
|
|
1125
|
+
_encryptionKey = encryptionKey;
|
|
1126
|
+
if (!existsSync3(SHARDS_DIR)) {
|
|
1127
|
+
mkdirSync(SHARDS_DIR, { recursive: true });
|
|
1128
|
+
}
|
|
1129
|
+
_shardingEnabled = true;
|
|
1130
|
+
}
|
|
1131
|
+
function isShardingEnabled() {
|
|
1132
|
+
return _shardingEnabled;
|
|
1133
|
+
}
|
|
1134
|
+
function getShardsDir() {
|
|
1135
|
+
return SHARDS_DIR;
|
|
1136
|
+
}
|
|
1137
|
+
function getShardClient(projectName) {
|
|
1138
|
+
if (!_encryptionKey) {
|
|
1139
|
+
throw new Error("Shard manager not initialized. Call initShardManager() first.");
|
|
1140
|
+
}
|
|
1141
|
+
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
1142
|
+
if (!safeName) {
|
|
1143
|
+
throw new Error(`Invalid project name for shard: "${projectName}"`);
|
|
1144
|
+
}
|
|
1145
|
+
const cached = _shards.get(safeName);
|
|
1146
|
+
if (cached) return cached;
|
|
1147
|
+
const dbPath = path3.join(SHARDS_DIR, `${safeName}.db`);
|
|
1148
|
+
const client = createClient2({
|
|
1149
|
+
url: `file:${dbPath}`,
|
|
1150
|
+
encryptionKey: _encryptionKey
|
|
1151
|
+
});
|
|
1152
|
+
_shards.set(safeName, client);
|
|
1153
|
+
return client;
|
|
1154
|
+
}
|
|
1155
|
+
function shardExists(projectName) {
|
|
1156
|
+
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
1157
|
+
return existsSync3(path3.join(SHARDS_DIR, `${safeName}.db`));
|
|
1158
|
+
}
|
|
1159
|
+
function listShards() {
|
|
1160
|
+
if (!existsSync3(SHARDS_DIR)) return [];
|
|
1161
|
+
return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
1162
|
+
}
|
|
1163
|
+
async function ensureShardSchema(client) {
|
|
1164
|
+
await client.execute("PRAGMA journal_mode = WAL");
|
|
1165
|
+
await client.execute("PRAGMA busy_timeout = 30000");
|
|
1166
|
+
try {
|
|
1167
|
+
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
1168
|
+
} catch {
|
|
1169
|
+
}
|
|
1170
|
+
await client.executeMultiple(`
|
|
1171
|
+
CREATE TABLE IF NOT EXISTS memories (
|
|
1172
|
+
id TEXT PRIMARY KEY,
|
|
1173
|
+
agent_id TEXT NOT NULL,
|
|
1174
|
+
agent_role TEXT NOT NULL,
|
|
1175
|
+
session_id TEXT NOT NULL,
|
|
1176
|
+
timestamp TEXT NOT NULL,
|
|
1177
|
+
tool_name TEXT NOT NULL,
|
|
1178
|
+
project_name TEXT NOT NULL,
|
|
1179
|
+
has_error INTEGER NOT NULL DEFAULT 0,
|
|
1180
|
+
raw_text TEXT NOT NULL,
|
|
1181
|
+
vector F32_BLOB(1024),
|
|
1182
|
+
version INTEGER NOT NULL DEFAULT 0
|
|
1183
|
+
);
|
|
1184
|
+
|
|
1185
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
|
|
1186
|
+
CREATE INDEX IF NOT EXISTS idx_memories_timestamp ON memories(timestamp);
|
|
1187
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent_project ON memories(agent_id, project_name);
|
|
1188
|
+
`);
|
|
1189
|
+
await client.executeMultiple(`
|
|
1190
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
1191
|
+
raw_text,
|
|
1192
|
+
content='memories',
|
|
1263
1193
|
content_rowid='rowid'
|
|
1264
1194
|
);
|
|
1265
1195
|
|
|
1266
|
-
CREATE TRIGGER IF NOT EXISTS
|
|
1267
|
-
INSERT INTO
|
|
1268
|
-
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
1196
|
+
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
1197
|
+
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
1269
1198
|
END;
|
|
1270
1199
|
|
|
1271
|
-
CREATE TRIGGER IF NOT EXISTS
|
|
1272
|
-
INSERT INTO
|
|
1273
|
-
VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
|
|
1200
|
+
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
1201
|
+
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
1274
1202
|
END;
|
|
1275
1203
|
|
|
1276
|
-
CREATE TRIGGER IF NOT EXISTS
|
|
1277
|
-
INSERT INTO
|
|
1278
|
-
|
|
1279
|
-
INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
|
|
1280
|
-
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
1204
|
+
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
1205
|
+
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
1206
|
+
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
1281
1207
|
END;
|
|
1282
1208
|
`);
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1209
|
+
for (const col of [
|
|
1210
|
+
"ALTER TABLE memories ADD COLUMN task_id TEXT",
|
|
1211
|
+
"ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
|
|
1212
|
+
"ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
|
|
1213
|
+
"ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
|
|
1214
|
+
"ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
|
|
1215
|
+
"ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0",
|
|
1216
|
+
"ALTER TABLE memories ADD COLUMN content_hash TEXT",
|
|
1217
|
+
"ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT",
|
|
1218
|
+
"ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7",
|
|
1219
|
+
"ALTER TABLE memories ADD COLUMN last_accessed TEXT",
|
|
1220
|
+
// Wiki linkage columns (must match database.ts)
|
|
1221
|
+
"ALTER TABLE memories ADD COLUMN workspace_id TEXT",
|
|
1222
|
+
"ALTER TABLE memories ADD COLUMN document_id TEXT",
|
|
1223
|
+
"ALTER TABLE memories ADD COLUMN user_id TEXT",
|
|
1224
|
+
"ALTER TABLE memories ADD COLUMN char_offset INTEGER",
|
|
1225
|
+
"ALTER TABLE memories ADD COLUMN page_number INTEGER",
|
|
1226
|
+
// Source provenance columns (must match database.ts)
|
|
1227
|
+
"ALTER TABLE memories ADD COLUMN source_path TEXT",
|
|
1228
|
+
"ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'",
|
|
1229
|
+
"ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3",
|
|
1230
|
+
"ALTER TABLE memories ADD COLUMN supersedes_id TEXT"
|
|
1231
|
+
]) {
|
|
1232
|
+
try {
|
|
1233
|
+
await client.execute(col);
|
|
1234
|
+
} catch {
|
|
1235
|
+
}
|
|
1289
1236
|
}
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1237
|
+
for (const idx of [
|
|
1238
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)",
|
|
1239
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL"
|
|
1240
|
+
]) {
|
|
1241
|
+
try {
|
|
1242
|
+
await client.execute(idx);
|
|
1243
|
+
} catch {
|
|
1244
|
+
}
|
|
1295
1245
|
}
|
|
1296
1246
|
try {
|
|
1297
|
-
await client.execute(
|
|
1298
|
-
sql: `UPDATE memories SET tier = 1 WHERE tool_name = 'commit_to_long_term_memory' AND importance >= 8 AND tier = 3`,
|
|
1299
|
-
args: []
|
|
1300
|
-
});
|
|
1301
|
-
await client.execute({
|
|
1302
|
-
sql: `UPDATE memories SET tier = 2 WHERE tool_name IN ('store_memory', 'manual') AND importance >= 5 AND tier = 3`,
|
|
1303
|
-
args: []
|
|
1304
|
-
});
|
|
1247
|
+
await client.execute("CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)");
|
|
1305
1248
|
} catch {
|
|
1306
1249
|
}
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1250
|
+
for (const idx of [
|
|
1251
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_workspace ON memories(workspace_id)",
|
|
1252
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_document ON memories(document_id)",
|
|
1253
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)"
|
|
1254
|
+
]) {
|
|
1255
|
+
try {
|
|
1256
|
+
await client.execute(idx);
|
|
1257
|
+
} catch {
|
|
1258
|
+
}
|
|
1313
1259
|
}
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1260
|
+
await client.executeMultiple(`
|
|
1261
|
+
CREATE TABLE IF NOT EXISTS entities (
|
|
1262
|
+
id TEXT PRIMARY KEY,
|
|
1263
|
+
name TEXT NOT NULL,
|
|
1264
|
+
type TEXT NOT NULL,
|
|
1265
|
+
first_seen TEXT NOT NULL,
|
|
1266
|
+
last_seen TEXT NOT NULL,
|
|
1267
|
+
properties TEXT DEFAULT '{}',
|
|
1268
|
+
UNIQUE(name, type)
|
|
1317
1269
|
);
|
|
1318
|
-
|
|
1319
|
-
|
|
1270
|
+
|
|
1271
|
+
CREATE TABLE IF NOT EXISTS relationships (
|
|
1272
|
+
id TEXT PRIMARY KEY,
|
|
1273
|
+
source_entity_id TEXT NOT NULL,
|
|
1274
|
+
target_entity_id TEXT NOT NULL,
|
|
1275
|
+
type TEXT NOT NULL,
|
|
1276
|
+
weight REAL DEFAULT 1.0,
|
|
1277
|
+
timestamp TEXT NOT NULL,
|
|
1278
|
+
properties TEXT DEFAULT '{}',
|
|
1279
|
+
UNIQUE(source_entity_id, target_entity_id, type)
|
|
1280
|
+
);
|
|
1281
|
+
|
|
1282
|
+
CREATE TABLE IF NOT EXISTS entity_memories (
|
|
1283
|
+
entity_id TEXT NOT NULL,
|
|
1284
|
+
memory_id TEXT NOT NULL,
|
|
1285
|
+
PRIMARY KEY (entity_id, memory_id)
|
|
1286
|
+
);
|
|
1287
|
+
|
|
1288
|
+
CREATE TABLE IF NOT EXISTS relationship_memories (
|
|
1289
|
+
relationship_id TEXT NOT NULL,
|
|
1290
|
+
memory_id TEXT NOT NULL,
|
|
1291
|
+
PRIMARY KEY (relationship_id, memory_id)
|
|
1292
|
+
);
|
|
1293
|
+
|
|
1294
|
+
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
|
|
1295
|
+
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
1296
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
|
|
1297
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
|
|
1298
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_type ON relationships(type);
|
|
1299
|
+
|
|
1300
|
+
CREATE TABLE IF NOT EXISTS hyperedges (
|
|
1301
|
+
id TEXT PRIMARY KEY,
|
|
1302
|
+
label TEXT NOT NULL,
|
|
1303
|
+
relation TEXT NOT NULL,
|
|
1304
|
+
confidence REAL DEFAULT 1.0,
|
|
1305
|
+
timestamp TEXT NOT NULL
|
|
1306
|
+
);
|
|
1307
|
+
|
|
1308
|
+
CREATE TABLE IF NOT EXISTS hyperedge_nodes (
|
|
1309
|
+
hyperedge_id TEXT NOT NULL,
|
|
1310
|
+
entity_id TEXT NOT NULL,
|
|
1311
|
+
PRIMARY KEY (hyperedge_id, entity_id)
|
|
1312
|
+
);
|
|
1313
|
+
`);
|
|
1320
1314
|
for (const col of [
|
|
1321
|
-
"ALTER TABLE
|
|
1322
|
-
"ALTER TABLE
|
|
1315
|
+
"ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
|
|
1316
|
+
"ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
|
|
1323
1317
|
]) {
|
|
1324
1318
|
try {
|
|
1325
1319
|
await client.execute(col);
|
|
@@ -1327,6 +1321,110 @@ async function ensureSchema() {
|
|
|
1327
1321
|
}
|
|
1328
1322
|
}
|
|
1329
1323
|
}
|
|
1324
|
+
async function getReadyShardClient(projectName) {
|
|
1325
|
+
const client = getShardClient(projectName);
|
|
1326
|
+
await ensureShardSchema(client);
|
|
1327
|
+
return client;
|
|
1328
|
+
}
|
|
1329
|
+
function disposeShards() {
|
|
1330
|
+
for (const [, client] of _shards) {
|
|
1331
|
+
client.close();
|
|
1332
|
+
}
|
|
1333
|
+
_shards.clear();
|
|
1334
|
+
_shardingEnabled = false;
|
|
1335
|
+
_encryptionKey = null;
|
|
1336
|
+
}
|
|
1337
|
+
var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
|
|
1338
|
+
var init_shard_manager = __esm({
|
|
1339
|
+
"src/lib/shard-manager.ts"() {
|
|
1340
|
+
"use strict";
|
|
1341
|
+
init_config();
|
|
1342
|
+
SHARDS_DIR = path3.join(EXE_AI_DIR, "shards");
|
|
1343
|
+
_shards = /* @__PURE__ */ new Map();
|
|
1344
|
+
_encryptionKey = null;
|
|
1345
|
+
_shardingEnabled = false;
|
|
1346
|
+
}
|
|
1347
|
+
});
|
|
1348
|
+
|
|
1349
|
+
// src/lib/global-procedures.ts
|
|
1350
|
+
var global_procedures_exports = {};
|
|
1351
|
+
__export(global_procedures_exports, {
|
|
1352
|
+
deactivateGlobalProcedure: () => deactivateGlobalProcedure,
|
|
1353
|
+
getGlobalProceduresBlock: () => getGlobalProceduresBlock,
|
|
1354
|
+
loadGlobalProcedures: () => loadGlobalProcedures,
|
|
1355
|
+
storeGlobalProcedure: () => storeGlobalProcedure
|
|
1356
|
+
});
|
|
1357
|
+
import { randomUUID } from "crypto";
|
|
1358
|
+
async function loadGlobalProcedures() {
|
|
1359
|
+
const client = getClient();
|
|
1360
|
+
const result = await client.execute({
|
|
1361
|
+
sql: "SELECT * FROM global_procedures WHERE active = 1 ORDER BY priority ASC, created_at ASC",
|
|
1362
|
+
args: []
|
|
1363
|
+
});
|
|
1364
|
+
const procedures = result.rows;
|
|
1365
|
+
if (procedures.length > 0) {
|
|
1366
|
+
_cache = procedures.map((p) => `### ${p.title}
|
|
1367
|
+
${p.content}`).join("\n\n");
|
|
1368
|
+
} else {
|
|
1369
|
+
_cache = "";
|
|
1370
|
+
}
|
|
1371
|
+
_cacheLoaded = true;
|
|
1372
|
+
return procedures;
|
|
1373
|
+
}
|
|
1374
|
+
function getGlobalProceduresBlock() {
|
|
1375
|
+
if (!_cacheLoaded) return "";
|
|
1376
|
+
if (!_cache) return "";
|
|
1377
|
+
return `## Organization-Wide Procedures (MANDATORY \u2014 supersedes all other rules)
|
|
1378
|
+
|
|
1379
|
+
${_cache}
|
|
1380
|
+
`;
|
|
1381
|
+
}
|
|
1382
|
+
async function storeGlobalProcedure(input) {
|
|
1383
|
+
const id = randomUUID();
|
|
1384
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1385
|
+
const client = getClient();
|
|
1386
|
+
await client.execute({
|
|
1387
|
+
sql: `INSERT INTO global_procedures (id, title, content, priority, domain, active, created_at, updated_at)
|
|
1388
|
+
VALUES (?, ?, ?, ?, ?, 1, ?, ?)`,
|
|
1389
|
+
args: [id, input.title, input.content, input.priority ?? "p0", input.domain ?? null, now, now]
|
|
1390
|
+
});
|
|
1391
|
+
await loadGlobalProcedures();
|
|
1392
|
+
return id;
|
|
1393
|
+
}
|
|
1394
|
+
async function deactivateGlobalProcedure(id) {
|
|
1395
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1396
|
+
const client = getClient();
|
|
1397
|
+
const result = await client.execute({
|
|
1398
|
+
sql: "UPDATE global_procedures SET active = 0, updated_at = ? WHERE id = ?",
|
|
1399
|
+
args: [now, id]
|
|
1400
|
+
});
|
|
1401
|
+
await loadGlobalProcedures();
|
|
1402
|
+
return result.rowsAffected > 0;
|
|
1403
|
+
}
|
|
1404
|
+
var _cache, _cacheLoaded;
|
|
1405
|
+
var init_global_procedures = __esm({
|
|
1406
|
+
"src/lib/global-procedures.ts"() {
|
|
1407
|
+
"use strict";
|
|
1408
|
+
init_database();
|
|
1409
|
+
_cache = "";
|
|
1410
|
+
_cacheLoaded = false;
|
|
1411
|
+
}
|
|
1412
|
+
});
|
|
1413
|
+
|
|
1414
|
+
// src/bin/backfill-conversations.ts
|
|
1415
|
+
import crypto2 from "crypto";
|
|
1416
|
+
import { createReadStream } from "fs";
|
|
1417
|
+
import { readdir, stat } from "fs/promises";
|
|
1418
|
+
import path5 from "path";
|
|
1419
|
+
import { createInterface } from "readline";
|
|
1420
|
+
import { homedir } from "os";
|
|
1421
|
+
import { parseArgs } from "util";
|
|
1422
|
+
|
|
1423
|
+
// src/types/memory.ts
|
|
1424
|
+
var EMBEDDING_DIM = 1024;
|
|
1425
|
+
|
|
1426
|
+
// src/lib/store.ts
|
|
1427
|
+
init_database();
|
|
1330
1428
|
|
|
1331
1429
|
// src/lib/keychain.ts
|
|
1332
1430
|
import { readFile, writeFile, unlink, mkdir, chmod } from "fs/promises";
|
|
@@ -1374,6 +1472,57 @@ async function getMasterKey() {
|
|
|
1374
1472
|
|
|
1375
1473
|
// src/lib/store.ts
|
|
1376
1474
|
init_config();
|
|
1475
|
+
|
|
1476
|
+
// src/lib/state-bus.ts
|
|
1477
|
+
var StateBus = class {
|
|
1478
|
+
handlers = /* @__PURE__ */ new Map();
|
|
1479
|
+
globalHandlers = /* @__PURE__ */ new Set();
|
|
1480
|
+
/** Emit an event to all subscribers */
|
|
1481
|
+
emit(event) {
|
|
1482
|
+
const typeHandlers = this.handlers.get(event.type);
|
|
1483
|
+
if (typeHandlers) {
|
|
1484
|
+
for (const handler of typeHandlers) {
|
|
1485
|
+
try {
|
|
1486
|
+
handler(event);
|
|
1487
|
+
} catch {
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
for (const handler of this.globalHandlers) {
|
|
1492
|
+
try {
|
|
1493
|
+
handler(event);
|
|
1494
|
+
} catch {
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
/** Subscribe to a specific event type */
|
|
1499
|
+
on(type, handler) {
|
|
1500
|
+
if (!this.handlers.has(type)) {
|
|
1501
|
+
this.handlers.set(type, /* @__PURE__ */ new Set());
|
|
1502
|
+
}
|
|
1503
|
+
this.handlers.get(type).add(handler);
|
|
1504
|
+
}
|
|
1505
|
+
/** Subscribe to ALL events */
|
|
1506
|
+
onAny(handler) {
|
|
1507
|
+
this.globalHandlers.add(handler);
|
|
1508
|
+
}
|
|
1509
|
+
/** Unsubscribe from a specific event type */
|
|
1510
|
+
off(type, handler) {
|
|
1511
|
+
this.handlers.get(type)?.delete(handler);
|
|
1512
|
+
}
|
|
1513
|
+
/** Unsubscribe from ALL events */
|
|
1514
|
+
offAny(handler) {
|
|
1515
|
+
this.globalHandlers.delete(handler);
|
|
1516
|
+
}
|
|
1517
|
+
/** Remove all listeners */
|
|
1518
|
+
clear() {
|
|
1519
|
+
this.handlers.clear();
|
|
1520
|
+
this.globalHandlers.clear();
|
|
1521
|
+
}
|
|
1522
|
+
};
|
|
1523
|
+
var orgBus = new StateBus();
|
|
1524
|
+
|
|
1525
|
+
// src/lib/store.ts
|
|
1377
1526
|
var INIT_MAX_RETRIES = 3;
|
|
1378
1527
|
var INIT_RETRY_DELAY_MS = 1e3;
|
|
1379
1528
|
function isBusyError2(err) {
|
|
@@ -1444,6 +1593,11 @@ async function initStore(options) {
|
|
|
1444
1593
|
"version-query"
|
|
1445
1594
|
);
|
|
1446
1595
|
_nextVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
|
|
1596
|
+
try {
|
|
1597
|
+
const { loadGlobalProcedures: loadGlobalProcedures2 } = await Promise.resolve().then(() => (init_global_procedures(), global_procedures_exports));
|
|
1598
|
+
await loadGlobalProcedures2();
|
|
1599
|
+
} catch {
|
|
1600
|
+
}
|
|
1447
1601
|
}
|
|
1448
1602
|
function classifyTier(record) {
|
|
1449
1603
|
if (record.tool_name === "commit_to_long_term_memory" && (record.importance ?? 0) >= 8) return 1;
|
|
@@ -1485,6 +1639,12 @@ async function writeMemory(record) {
|
|
|
1485
1639
|
supersedes_id: record.supersedes_id ?? null
|
|
1486
1640
|
};
|
|
1487
1641
|
_pendingRecords.push(dbRow);
|
|
1642
|
+
orgBus.emit({
|
|
1643
|
+
type: "memory_stored",
|
|
1644
|
+
agentId: record.agent_id,
|
|
1645
|
+
project: record.project_name,
|
|
1646
|
+
timestamp: record.timestamp
|
|
1647
|
+
});
|
|
1488
1648
|
const MAX_PENDING = 1e3;
|
|
1489
1649
|
if (_pendingRecords.length > MAX_PENDING) {
|
|
1490
1650
|
const dropped = _pendingRecords.length - MAX_PENDING;
|
|
@@ -1643,7 +1803,7 @@ function vectorToBlob(vector) {
|
|
|
1643
1803
|
init_config();
|
|
1644
1804
|
import net from "net";
|
|
1645
1805
|
import { spawn } from "child_process";
|
|
1646
|
-
import { randomUUID } from "crypto";
|
|
1806
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
1647
1807
|
import { existsSync as existsSync4, unlinkSync, readFileSync as readFileSync2, openSync, closeSync, statSync } from "fs";
|
|
1648
1808
|
import path4 from "path";
|
|
1649
1809
|
import { fileURLToPath } from "url";
|
|
@@ -1848,7 +2008,7 @@ function sendRequest(texts, priority) {
|
|
|
1848
2008
|
resolve({ error: "Not connected" });
|
|
1849
2009
|
return;
|
|
1850
2010
|
}
|
|
1851
|
-
const id =
|
|
2011
|
+
const id = randomUUID2();
|
|
1852
2012
|
const timer = setTimeout(() => {
|
|
1853
2013
|
_pending.delete(id);
|
|
1854
2014
|
resolve({ error: "Request timeout" });
|
|
@@ -1866,7 +2026,7 @@ function sendRequest(texts, priority) {
|
|
|
1866
2026
|
async function pingDaemon() {
|
|
1867
2027
|
if (!_socket || !_connected) return null;
|
|
1868
2028
|
return new Promise((resolve) => {
|
|
1869
|
-
const id =
|
|
2029
|
+
const id = randomUUID2();
|
|
1870
2030
|
const timer = setTimeout(() => {
|
|
1871
2031
|
_pending.delete(id);
|
|
1872
2032
|
resolve(null);
|
|
@@ -1961,6 +2121,9 @@ async function embedViaClient(text, priority = "high") {
|
|
|
1961
2121
|
return null;
|
|
1962
2122
|
}
|
|
1963
2123
|
|
|
2124
|
+
// src/bin/backfill-conversations.ts
|
|
2125
|
+
init_database();
|
|
2126
|
+
|
|
1964
2127
|
// src/lib/is-main.ts
|
|
1965
2128
|
import { realpathSync } from "fs";
|
|
1966
2129
|
import { fileURLToPath as fileURLToPath2 } from "url";
|