@askexenow/exe-os 0.8.41 → 0.8.43

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.
Files changed (76) hide show
  1. package/dist/bin/backfill-conversations.js +805 -642
  2. package/dist/bin/backfill-responses.js +804 -641
  3. package/dist/bin/backfill-vectors.js +791 -634
  4. package/dist/bin/cleanup-stale-review-tasks.js +788 -631
  5. package/dist/bin/cli.js +1345 -660
  6. package/dist/bin/exe-agent.js +20 -1
  7. package/dist/bin/exe-assign.js +1503 -1343
  8. package/dist/bin/exe-boot.js +2518 -1798
  9. package/dist/bin/exe-call.js +39 -1
  10. package/dist/bin/exe-cloud.js +15 -1
  11. package/dist/bin/exe-dispatch.js +39 -2
  12. package/dist/bin/exe-doctor.js +790 -633
  13. package/dist/bin/exe-export-behaviors.js +792 -637
  14. package/dist/bin/exe-forget.js +145 -0
  15. package/dist/bin/exe-gateway.js +2500 -1877
  16. package/dist/bin/exe-heartbeat.js +147 -1
  17. package/dist/bin/exe-kill.js +795 -640
  18. package/dist/bin/exe-launch-agent.js +2168 -2008
  19. package/dist/bin/exe-link.js +28 -2
  20. package/dist/bin/exe-new-employee.js +25 -3
  21. package/dist/bin/exe-pending-messages.js +146 -1
  22. package/dist/bin/exe-pending-notifications.js +788 -631
  23. package/dist/bin/exe-pending-reviews.js +147 -1
  24. package/dist/bin/exe-rename.js +23 -0
  25. package/dist/bin/exe-review.js +490 -327
  26. package/dist/bin/exe-search.js +154 -3
  27. package/dist/bin/exe-session-cleanup.js +2466 -413
  28. package/dist/bin/exe-status.js +474 -317
  29. package/dist/bin/exe-team.js +474 -317
  30. package/dist/bin/git-sweep.js +2690 -150
  31. package/dist/bin/graph-backfill.js +794 -637
  32. package/dist/bin/graph-export.js +798 -641
  33. package/dist/bin/scan-tasks.js +2951 -44
  34. package/dist/bin/setup.js +62 -26
  35. package/dist/bin/shard-migrate.js +792 -637
  36. package/dist/bin/wiki-sync.js +794 -637
  37. package/dist/gateway/index.js +2504 -1895
  38. package/dist/hooks/bug-report-worker.js +2118 -576
  39. package/dist/hooks/commit-complete.js +2689 -149
  40. package/dist/hooks/error-recall.js +154 -3
  41. package/dist/hooks/ingest-worker.js +1439 -815
  42. package/dist/hooks/instructions-loaded.js +151 -0
  43. package/dist/hooks/notification.js +153 -2
  44. package/dist/hooks/post-compact.js +164 -0
  45. package/dist/hooks/pre-compact.js +3073 -101
  46. package/dist/hooks/pre-tool-use.js +151 -0
  47. package/dist/hooks/prompt-ingest-worker.js +1714 -1537
  48. package/dist/hooks/prompt-submit.js +2658 -1113
  49. package/dist/hooks/response-ingest-worker.js +170 -6
  50. package/dist/hooks/session-end.js +153 -2
  51. package/dist/hooks/session-start.js +154 -3
  52. package/dist/hooks/stop.js +151 -0
  53. package/dist/hooks/subagent-stop.js +151 -0
  54. package/dist/hooks/summary-worker.js +179 -7
  55. package/dist/index.js +278 -100
  56. package/dist/lib/cloud-sync.js +28 -2
  57. package/dist/lib/consolidation.js +69 -2
  58. package/dist/lib/database.js +19 -0
  59. package/dist/lib/device-registry.js +19 -0
  60. package/dist/lib/employee-templates.js +20 -1
  61. package/dist/lib/exe-daemon.js +236 -16
  62. package/dist/lib/hybrid-search.js +154 -3
  63. package/dist/lib/license.js +15 -1
  64. package/dist/lib/messaging.js +39 -2
  65. package/dist/lib/schedules.js +792 -637
  66. package/dist/lib/store.js +796 -636
  67. package/dist/lib/tasks.js +1614 -1091
  68. package/dist/lib/tmux-routing.js +149 -9
  69. package/dist/mcp/server.js +1825 -1138
  70. package/dist/mcp/tools/create-task.js +2280 -828
  71. package/dist/mcp/tools/list-tasks.js +2788 -159
  72. package/dist/mcp/tools/send-message.js +39 -2
  73. package/dist/mcp/tools/update-task.js +64 -0
  74. package/dist/runtime/index.js +235 -67
  75. package/dist/tui/App.js +1452 -644
  76. package/package.json +3 -2
@@ -23,314 +23,100 @@ var init_memory = __esm({
23
23
  }
24
24
  });
25
25
 
26
- // src/lib/config.ts
27
- var config_exports = {};
28
- __export(config_exports, {
29
- CONFIG_MIGRATIONS: () => CONFIG_MIGRATIONS,
30
- CONFIG_PATH: () => CONFIG_PATH,
31
- COO_AGENT_NAME: () => COO_AGENT_NAME,
32
- CURRENT_CONFIG_VERSION: () => CURRENT_CONFIG_VERSION,
33
- DB_PATH: () => DB_PATH,
34
- EXE_AI_DIR: () => EXE_AI_DIR,
35
- LEGACY_LANCE_PATH: () => LEGACY_LANCE_PATH,
36
- MODELS_DIR: () => MODELS_DIR,
37
- loadConfig: () => loadConfig,
38
- loadConfigFrom: () => loadConfigFrom,
39
- loadConfigSync: () => loadConfigSync,
40
- migrateConfig: () => migrateConfig,
41
- saveConfig: () => saveConfig
42
- });
43
- import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2, chmod as chmod2 } from "fs/promises";
44
- import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
45
- import path3 from "path";
46
- import os2 from "os";
47
- function resolveDataDir() {
48
- if (process.env.EXE_OS_DIR) return process.env.EXE_OS_DIR;
49
- if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
50
- const newDir = path3.join(os2.homedir(), ".exe-os");
51
- const legacyDir = path3.join(os2.homedir(), ".exe-mem");
52
- if (!existsSync2(newDir) && existsSync2(legacyDir)) {
53
- try {
54
- renameSync(legacyDir, newDir);
55
- process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
56
- `);
57
- } catch {
58
- return legacyDir;
59
- }
26
+ // src/lib/db-retry.ts
27
+ function isBusyError(err) {
28
+ if (err instanceof Error) {
29
+ const msg = err.message.toLowerCase();
30
+ return msg.includes("sqlite_busy") || msg.includes("database is locked");
60
31
  }
61
- return newDir;
32
+ return false;
62
33
  }
63
- function migrateLegacyConfig(raw) {
64
- if ("r2" in raw) {
65
- process.stderr.write(
66
- "[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"
67
- );
68
- delete raw.r2;
69
- }
70
- if ("syncIntervalMs" in raw) {
71
- delete raw.syncIntervalMs;
72
- }
73
- return raw;
34
+ function delay(ms) {
35
+ return new Promise((resolve) => setTimeout(resolve, ms));
74
36
  }
75
- function migrateConfig(raw) {
76
- const fromVersion = typeof raw.config_version === "number" ? raw.config_version : 0;
77
- let currentVersion = fromVersion;
78
- let migrated = false;
79
- if (currentVersion > CURRENT_CONFIG_VERSION) {
80
- return { config: raw, migrated: false, fromVersion };
81
- }
82
- for (const migration of CONFIG_MIGRATIONS) {
83
- if (currentVersion === migration.from && migration.to <= CURRENT_CONFIG_VERSION) {
84
- raw = migration.migrate(raw);
85
- currentVersion = migration.to;
86
- migrated = true;
37
+ async function retryOnBusy(fn, label) {
38
+ let lastError;
39
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
40
+ try {
41
+ return await fn();
42
+ } catch (err) {
43
+ lastError = err;
44
+ if (!isBusyError(err) || attempt === MAX_RETRIES) {
45
+ throw err;
46
+ }
47
+ const backoff = BASE_DELAY_MS * Math.pow(2, attempt);
48
+ const jitter = Math.floor(Math.random() * MAX_JITTER_MS);
49
+ process.stderr.write(
50
+ `[exe-os] SQLITE_BUSY ${label} retry ${attempt + 1}/${MAX_RETRIES} \u2014 waiting ${backoff + jitter}ms
51
+ `
52
+ );
53
+ await delay(backoff + jitter);
87
54
  }
88
55
  }
89
- return { config: raw, migrated, fromVersion };
90
- }
91
- function normalizeScalingRoadmap(raw) {
92
- const defaultAuto = DEFAULT_CONFIG.scalingRoadmap.rerankerAutoTrigger;
93
- const userRoadmap = raw.scalingRoadmap ?? {};
94
- const userAuto = userRoadmap.rerankerAutoTrigger ?? {};
95
- if (userAuto.enabled === void 0 && raw.rerankerEnabled !== void 0) {
96
- userAuto.enabled = raw.rerankerEnabled;
97
- }
98
- raw.scalingRoadmap = {
99
- ...userRoadmap,
100
- rerankerAutoTrigger: { ...defaultAuto, ...userAuto }
101
- };
102
- }
103
- function normalizeSessionLifecycle(raw) {
104
- const defaultSL = DEFAULT_CONFIG.sessionLifecycle;
105
- const userSL = raw.sessionLifecycle ?? {};
106
- raw.sessionLifecycle = { ...defaultSL, ...userSL };
107
- }
108
- function normalizeAutoUpdate(raw) {
109
- const defaultAU = DEFAULT_CONFIG.autoUpdate;
110
- const userAU = raw.autoUpdate ?? {};
111
- raw.autoUpdate = { ...defaultAU, ...userAU };
56
+ throw lastError;
112
57
  }
113
- async function loadConfig() {
114
- const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
115
- await mkdir2(dir, { recursive: true });
116
- const configPath = path3.join(dir, "config.json");
117
- if (!existsSync2(configPath)) {
118
- return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db") };
119
- }
120
- const raw = await readFile2(configPath, "utf-8");
121
- try {
122
- let parsed = JSON.parse(raw);
123
- parsed = migrateLegacyConfig(parsed);
124
- const { config: migratedCfg, migrated, fromVersion } = migrateConfig(parsed);
125
- if (migrated) {
126
- process.stderr.write(`[exe-os] Config migrated from v${fromVersion} to v${migratedCfg.config_version}
127
- `);
128
- try {
129
- await writeFile2(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
130
- } catch {
58
+ function wrapWithRetry(client) {
59
+ return new Proxy(client, {
60
+ get(target, prop, receiver) {
61
+ if (prop === "execute") {
62
+ return (sql) => retryOnBusy(() => target.execute(sql), "execute");
131
63
  }
64
+ if (prop === "batch") {
65
+ return (stmts) => retryOnBusy(() => target.batch(stmts), "batch");
66
+ }
67
+ return Reflect.get(target, prop, receiver);
132
68
  }
133
- normalizeScalingRoadmap(migratedCfg);
134
- normalizeSessionLifecycle(migratedCfg);
135
- normalizeAutoUpdate(migratedCfg);
136
- const config = { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db"), ...migratedCfg };
137
- if (config.dbPath.startsWith("~")) {
138
- config.dbPath = config.dbPath.replace(/^~/, os2.homedir());
139
- }
140
- return config;
141
- } catch {
142
- return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db") };
143
- }
69
+ });
144
70
  }
145
- function loadConfigSync() {
146
- const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
147
- const configPath = path3.join(dir, "config.json");
148
- if (!existsSync2(configPath)) {
149
- return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db") };
71
+ var MAX_RETRIES, BASE_DELAY_MS, MAX_JITTER_MS;
72
+ var init_db_retry = __esm({
73
+ "src/lib/db-retry.ts"() {
74
+ "use strict";
75
+ MAX_RETRIES = 3;
76
+ BASE_DELAY_MS = 200;
77
+ MAX_JITTER_MS = 300;
150
78
  }
151
- try {
152
- const raw = readFileSync(configPath, "utf-8");
153
- let parsed = JSON.parse(raw);
154
- parsed = migrateLegacyConfig(parsed);
155
- const { config: migratedCfg } = migrateConfig(parsed);
156
- normalizeScalingRoadmap(migratedCfg);
157
- normalizeSessionLifecycle(migratedCfg);
158
- normalizeAutoUpdate(migratedCfg);
159
- return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db"), ...migratedCfg };
160
- } catch {
161
- return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db") };
79
+ });
80
+
81
+ // src/lib/database.ts
82
+ import { createClient } from "@libsql/client";
83
+ async function initDatabase(config) {
84
+ if (_client) {
85
+ _client.close();
86
+ _client = null;
87
+ _resilientClient = null;
88
+ }
89
+ const opts = {
90
+ url: `file:${config.dbPath}`
91
+ };
92
+ if (config.encryptionKey) {
93
+ opts.encryptionKey = config.encryptionKey;
162
94
  }
95
+ _client = createClient(opts);
96
+ _resilientClient = wrapWithRetry(_client);
163
97
  }
164
- async function saveConfig(config) {
165
- const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
166
- await mkdir2(dir, { recursive: true });
167
- const configPath = path3.join(dir, "config.json");
168
- await writeFile2(configPath, JSON.stringify(config, null, 2) + "\n");
169
- if (config.cloud?.apiKey) {
170
- await chmod2(configPath, 384);
98
+ function isInitialized() {
99
+ return _client !== null;
100
+ }
101
+ function getClient() {
102
+ if (!_resilientClient) {
103
+ throw new Error("Database client not initialized. Call initDatabase() first.");
171
104
  }
105
+ return _resilientClient;
172
106
  }
173
- async function loadConfigFrom(configPath) {
174
- const raw = await readFile2(configPath, "utf-8");
107
+ function getRawClient() {
108
+ if (!_client) {
109
+ throw new Error("Database client not initialized. Call initDatabase() first.");
110
+ }
111
+ return _client;
112
+ }
113
+ async function ensureSchema() {
114
+ const client = getRawClient();
115
+ await client.execute("PRAGMA journal_mode = WAL");
116
+ await client.execute("PRAGMA busy_timeout = 30000");
117
+ await client.execute("PRAGMA wal_autocheckpoint = 1000");
175
118
  try {
176
- let parsed = JSON.parse(raw);
177
- parsed = migrateLegacyConfig(parsed);
178
- const { config: migratedCfg } = migrateConfig(parsed);
179
- normalizeScalingRoadmap(migratedCfg);
180
- normalizeSessionLifecycle(migratedCfg);
181
- normalizeAutoUpdate(migratedCfg);
182
- return { ...DEFAULT_CONFIG, ...migratedCfg };
183
- } catch {
184
- return { ...DEFAULT_CONFIG };
185
- }
186
- }
187
- var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, COO_AGENT_NAME, LEGACY_LANCE_PATH, CURRENT_CONFIG_VERSION, DEFAULT_CONFIG, CONFIG_MIGRATIONS;
188
- var init_config = __esm({
189
- "src/lib/config.ts"() {
190
- "use strict";
191
- EXE_AI_DIR = resolveDataDir();
192
- DB_PATH = path3.join(EXE_AI_DIR, "memories.db");
193
- MODELS_DIR = path3.join(EXE_AI_DIR, "models");
194
- CONFIG_PATH = path3.join(EXE_AI_DIR, "config.json");
195
- COO_AGENT_NAME = "exe";
196
- LEGACY_LANCE_PATH = path3.join(EXE_AI_DIR, "local.lance");
197
- CURRENT_CONFIG_VERSION = 1;
198
- DEFAULT_CONFIG = {
199
- config_version: CURRENT_CONFIG_VERSION,
200
- dbPath: DB_PATH,
201
- modelFile: "jina-embeddings-v5-small-q4_k_m.gguf",
202
- embeddingDim: 1024,
203
- batchSize: 20,
204
- flushIntervalMs: 1e4,
205
- autoIngestion: true,
206
- autoRetrieval: true,
207
- searchMode: "hybrid",
208
- hookSearchMode: "hybrid",
209
- fileGrepEnabled: true,
210
- splashEffect: true,
211
- consolidationEnabled: true,
212
- consolidationIntervalMs: 6 * 60 * 60 * 1e3,
213
- consolidationModel: "claude-haiku-4-5-20251001",
214
- consolidationMaxCallsPerRun: 20,
215
- selfQueryRouter: true,
216
- selfQueryModel: "claude-haiku-4-5-20251001",
217
- rerankerEnabled: true,
218
- scalingRoadmap: {
219
- rerankerAutoTrigger: {
220
- enabled: true,
221
- broadQueryMinCardinality: 5e4,
222
- fetchTopK: 150,
223
- returnTopK: 5
224
- }
225
- },
226
- graphRagEnabled: true,
227
- wikiEnabled: false,
228
- wikiUrl: "",
229
- wikiApiKey: "",
230
- wikiSyncIntervalMs: 30 * 60 * 1e3,
231
- wikiWorkspaceMapping: {
232
- exe: "Executive",
233
- yoshi: "Engineering",
234
- mari: "Marketing",
235
- tom: "Engineering",
236
- sasha: "Production"
237
- },
238
- wikiAutoUpdate: true,
239
- wikiAutoUpdateThreshold: 0.5,
240
- wikiAutoUpdateCreateNew: true,
241
- skillLearning: true,
242
- skillThreshold: 3,
243
- skillModel: "claude-haiku-4-5-20251001",
244
- exeHeartbeat: {
245
- enabled: true,
246
- intervalSeconds: 60,
247
- staleInProgressThresholdHours: 2
248
- },
249
- sessionLifecycle: {
250
- idleKillEnabled: true,
251
- idleKillTicksRequired: 3,
252
- idleKillIntercomAckWindowMs: 1e4,
253
- maxAutoInstances: 10
254
- },
255
- autoUpdate: {
256
- checkOnBoot: true,
257
- autoInstall: false,
258
- checkIntervalMs: 24 * 60 * 60 * 1e3
259
- }
260
- };
261
- CONFIG_MIGRATIONS = [
262
- {
263
- from: 0,
264
- to: 1,
265
- migrate: (cfg) => {
266
- cfg.config_version = 1;
267
- return cfg;
268
- }
269
- }
270
- ];
271
- }
272
- });
273
-
274
- // src/lib/shard-manager.ts
275
- var shard_manager_exports = {};
276
- __export(shard_manager_exports, {
277
- disposeShards: () => disposeShards,
278
- ensureShardSchema: () => ensureShardSchema,
279
- getReadyShardClient: () => getReadyShardClient,
280
- getShardClient: () => getShardClient,
281
- getShardsDir: () => getShardsDir,
282
- initShardManager: () => initShardManager,
283
- isShardingEnabled: () => isShardingEnabled,
284
- listShards: () => listShards,
285
- shardExists: () => shardExists
286
- });
287
- import path4 from "path";
288
- import { existsSync as existsSync3, mkdirSync, readdirSync } from "fs";
289
- import { createClient as createClient2 } from "@libsql/client";
290
- function initShardManager(encryptionKey) {
291
- _encryptionKey = encryptionKey;
292
- if (!existsSync3(SHARDS_DIR)) {
293
- mkdirSync(SHARDS_DIR, { recursive: true });
294
- }
295
- _shardingEnabled = true;
296
- }
297
- function isShardingEnabled() {
298
- return _shardingEnabled;
299
- }
300
- function getShardsDir() {
301
- return SHARDS_DIR;
302
- }
303
- function getShardClient(projectName) {
304
- if (!_encryptionKey) {
305
- throw new Error("Shard manager not initialized. Call initShardManager() first.");
306
- }
307
- const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
308
- if (!safeName) {
309
- throw new Error(`Invalid project name for shard: "${projectName}"`);
310
- }
311
- const cached = _shards.get(safeName);
312
- if (cached) return cached;
313
- const dbPath = path4.join(SHARDS_DIR, `${safeName}.db`);
314
- const client = createClient2({
315
- url: `file:${dbPath}`,
316
- encryptionKey: _encryptionKey
317
- });
318
- _shards.set(safeName, client);
319
- return client;
320
- }
321
- function shardExists(projectName) {
322
- const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
323
- return existsSync3(path4.join(SHARDS_DIR, `${safeName}.db`));
324
- }
325
- function listShards() {
326
- if (!existsSync3(SHARDS_DIR)) return [];
327
- return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
328
- }
329
- async function ensureShardSchema(client) {
330
- await client.execute("PRAGMA journal_mode = WAL");
331
- await client.execute("PRAGMA busy_timeout = 30000");
332
- try {
333
- await client.execute("PRAGMA libsql_vector_search_ef = 128");
119
+ await client.execute("PRAGMA libsql_vector_search_ef = 128");
334
120
  } catch {
335
121
  }
336
122
  await client.executeMultiple(`
@@ -348,9 +134,26 @@ async function ensureShardSchema(client) {
348
134
  version INTEGER NOT NULL DEFAULT 0
349
135
  );
350
136
 
351
- CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
352
- CREATE INDEX IF NOT EXISTS idx_memories_timestamp ON memories(timestamp);
353
- CREATE INDEX IF NOT EXISTS idx_memories_agent_project ON memories(agent_id, project_name);
137
+ CREATE INDEX IF NOT EXISTS idx_memories_agent
138
+ ON memories(agent_id);
139
+
140
+ CREATE INDEX IF NOT EXISTS idx_memories_timestamp
141
+ ON memories(timestamp);
142
+
143
+ CREATE INDEX IF NOT EXISTS idx_memories_session
144
+ ON memories(session_id);
145
+
146
+ CREATE INDEX IF NOT EXISTS idx_memories_project
147
+ ON memories(project_name);
148
+
149
+ CREATE INDEX IF NOT EXISTS idx_memories_tool
150
+ ON memories(tool_name);
151
+
152
+ CREATE INDEX IF NOT EXISTS idx_memories_version
153
+ ON memories(version);
154
+
155
+ CREATE INDEX IF NOT EXISTS idx_memories_agent_project
156
+ ON memories(agent_id, project_name);
354
157
  `);
355
158
  await client.executeMultiple(`
356
159
  CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
@@ -372,1153 +175,1161 @@ async function ensureShardSchema(client) {
372
175
  INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
373
176
  END;
374
177
  `);
375
- for (const col of [
376
- "ALTER TABLE memories ADD COLUMN task_id TEXT",
377
- "ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
378
- "ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
379
- "ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
380
- "ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
381
- "ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0",
382
- "ALTER TABLE memories ADD COLUMN content_hash TEXT",
383
- "ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT",
384
- "ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7",
385
- "ALTER TABLE memories ADD COLUMN last_accessed TEXT",
386
- // Wiki linkage columns (must match database.ts)
387
- "ALTER TABLE memories ADD COLUMN workspace_id TEXT",
388
- "ALTER TABLE memories ADD COLUMN document_id TEXT",
389
- "ALTER TABLE memories ADD COLUMN user_id TEXT",
390
- "ALTER TABLE memories ADD COLUMN char_offset INTEGER",
391
- "ALTER TABLE memories ADD COLUMN page_number INTEGER",
392
- // Source provenance columns (must match database.ts)
393
- "ALTER TABLE memories ADD COLUMN source_path TEXT",
394
- "ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'",
395
- "ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3",
396
- "ALTER TABLE memories ADD COLUMN supersedes_id TEXT"
397
- ]) {
398
- try {
399
- await client.execute(col);
400
- } catch {
178
+ await client.executeMultiple(`
179
+ CREATE TABLE IF NOT EXISTS sync_meta (
180
+ key TEXT PRIMARY KEY,
181
+ value TEXT NOT NULL
182
+ );
183
+ `);
184
+ await client.executeMultiple(`
185
+ CREATE TABLE IF NOT EXISTS tasks (
186
+ id TEXT PRIMARY KEY,
187
+ title TEXT NOT NULL,
188
+ assigned_to TEXT NOT NULL,
189
+ assigned_by TEXT NOT NULL,
190
+ project_name TEXT NOT NULL,
191
+ priority TEXT NOT NULL DEFAULT 'p1',
192
+ status TEXT NOT NULL DEFAULT 'open',
193
+ task_file TEXT,
194
+ created_at TEXT NOT NULL,
195
+ updated_at TEXT NOT NULL
196
+ );
197
+
198
+ CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
199
+ ON tasks(assigned_to, status);
200
+ `);
201
+ await client.executeMultiple(`
202
+ CREATE TABLE IF NOT EXISTS behaviors (
203
+ id TEXT PRIMARY KEY,
204
+ agent_id TEXT NOT NULL,
205
+ project_name TEXT,
206
+ domain TEXT,
207
+ content TEXT NOT NULL,
208
+ active INTEGER NOT NULL DEFAULT 1,
209
+ created_at TEXT NOT NULL,
210
+ updated_at TEXT NOT NULL
211
+ );
212
+
213
+ CREATE INDEX IF NOT EXISTS idx_behaviors_agent
214
+ ON behaviors(agent_id, active);
215
+ `);
216
+ try {
217
+ const existing = await client.execute({
218
+ sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
219
+ args: []
220
+ });
221
+ if (Number(existing.rows[0]?.cnt) === 0) {
222
+ await client.executeMultiple(`
223
+ INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
224
+ VALUES
225
+ (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');
226
+ INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
227
+ VALUES
228
+ (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');
229
+ INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
230
+ VALUES
231
+ (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');
232
+ `);
401
233
  }
234
+ } catch {
402
235
  }
403
- for (const idx of [
404
- "CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)",
405
- "CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL"
406
- ]) {
407
- try {
408
- await client.execute(idx);
409
- } catch {
410
- }
236
+ try {
237
+ await client.execute({
238
+ sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
239
+ args: []
240
+ });
241
+ } catch {
411
242
  }
412
243
  try {
413
- await client.execute("CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)");
244
+ await client.execute({
245
+ sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
246
+ args: []
247
+ });
414
248
  } catch {
415
249
  }
416
- for (const idx of [
417
- "CREATE INDEX IF NOT EXISTS idx_memories_workspace ON memories(workspace_id)",
418
- "CREATE INDEX IF NOT EXISTS idx_memories_document ON memories(document_id)",
419
- "CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)"
420
- ]) {
421
- try {
422
- await client.execute(idx);
423
- } catch {
424
- }
250
+ try {
251
+ await client.execute({
252
+ sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
253
+ args: []
254
+ });
255
+ } catch {
425
256
  }
426
- await client.executeMultiple(`
427
- CREATE TABLE IF NOT EXISTS entities (
428
- id TEXT PRIMARY KEY,
429
- name TEXT NOT NULL,
430
- type TEXT NOT NULL,
431
- first_seen TEXT NOT NULL,
432
- last_seen TEXT NOT NULL,
433
- properties TEXT DEFAULT '{}',
434
- UNIQUE(name, type)
435
- );
436
-
437
- CREATE TABLE IF NOT EXISTS relationships (
438
- id TEXT PRIMARY KEY,
439
- source_entity_id TEXT NOT NULL,
440
- target_entity_id TEXT NOT NULL,
441
- type TEXT NOT NULL,
442
- weight REAL DEFAULT 1.0,
443
- timestamp TEXT NOT NULL,
444
- properties TEXT DEFAULT '{}',
445
- UNIQUE(source_entity_id, target_entity_id, type)
446
- );
447
-
448
- CREATE TABLE IF NOT EXISTS entity_memories (
449
- entity_id TEXT NOT NULL,
450
- memory_id TEXT NOT NULL,
451
- PRIMARY KEY (entity_id, memory_id)
452
- );
453
-
454
- CREATE TABLE IF NOT EXISTS relationship_memories (
455
- relationship_id TEXT NOT NULL,
456
- memory_id TEXT NOT NULL,
457
- PRIMARY KEY (relationship_id, memory_id)
458
- );
459
-
460
- CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
461
- CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
462
- CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
463
- CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
464
- CREATE INDEX IF NOT EXISTS idx_relationships_type ON relationships(type);
465
-
466
- CREATE TABLE IF NOT EXISTS hyperedges (
467
- id TEXT PRIMARY KEY,
468
- label TEXT NOT NULL,
469
- relation TEXT NOT NULL,
470
- confidence REAL DEFAULT 1.0,
471
- timestamp TEXT NOT NULL
472
- );
473
-
474
- CREATE TABLE IF NOT EXISTS hyperedge_nodes (
475
- hyperedge_id TEXT NOT NULL,
476
- entity_id TEXT NOT NULL,
477
- PRIMARY KEY (hyperedge_id, entity_id)
478
- );
479
- `);
480
- for (const col of [
481
- "ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
482
- "ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
483
- ]) {
484
- try {
485
- await client.execute(col);
486
- } catch {
487
- }
488
- }
489
- }
490
- async function getReadyShardClient(projectName) {
491
- const client = getShardClient(projectName);
492
- await ensureShardSchema(client);
493
- return client;
494
- }
495
- function disposeShards() {
496
- for (const [, client] of _shards) {
497
- client.close();
498
- }
499
- _shards.clear();
500
- _shardingEnabled = false;
501
- _encryptionKey = null;
502
- }
503
- var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
504
- var init_shard_manager = __esm({
505
- "src/lib/shard-manager.ts"() {
506
- "use strict";
507
- init_config();
508
- SHARDS_DIR = path4.join(EXE_AI_DIR, "shards");
509
- _shards = /* @__PURE__ */ new Map();
510
- _encryptionKey = null;
511
- _shardingEnabled = false;
257
+ try {
258
+ await client.execute({
259
+ sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
260
+ ON tasks(parent_task_id)
261
+ WHERE parent_task_id IS NOT NULL`,
262
+ args: []
263
+ });
264
+ } catch {
512
265
  }
513
- });
514
-
515
- // src/lib/exe-daemon-client.ts
516
- import net from "net";
517
- import { spawn } from "child_process";
518
- import { randomUUID as randomUUID2 } from "crypto";
519
- import { existsSync as existsSync7, unlinkSync, readFileSync as readFileSync5, openSync, closeSync, statSync } from "fs";
520
- import path8 from "path";
521
- import { fileURLToPath } from "url";
522
- function handleData(chunk) {
523
- _buffer += chunk.toString();
524
- if (_buffer.length > MAX_BUFFER) {
525
- _buffer = "";
526
- return;
266
+ try {
267
+ await client.execute({
268
+ sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
269
+ args: []
270
+ });
271
+ } catch {
527
272
  }
528
- let newlineIdx;
529
- while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
530
- const line = _buffer.slice(0, newlineIdx).trim();
531
- _buffer = _buffer.slice(newlineIdx + 1);
532
- if (!line) continue;
533
- try {
534
- const response = JSON.parse(line);
535
- const entry = _pending.get(response.id);
536
- if (entry) {
537
- clearTimeout(entry.timer);
538
- _pending.delete(response.id);
539
- entry.resolve(response);
540
- }
541
- } catch {
542
- }
273
+ try {
274
+ await client.execute({
275
+ sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
276
+ args: []
277
+ });
278
+ } catch {
543
279
  }
544
- }
545
- function cleanupStaleFiles() {
546
- if (existsSync7(PID_PATH)) {
547
- try {
548
- const pid = parseInt(readFileSync5(PID_PATH, "utf8").trim(), 10);
549
- if (pid > 0) {
550
- try {
551
- process.kill(pid, 0);
552
- return;
553
- } catch {
554
- }
555
- }
556
- } catch {
557
- }
558
- try {
559
- unlinkSync(PID_PATH);
560
- } catch {
561
- }
562
- try {
563
- unlinkSync(SOCKET_PATH);
564
- } catch {
565
- }
280
+ try {
281
+ await client.execute({
282
+ sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
283
+ args: []
284
+ });
285
+ } catch {
566
286
  }
567
- }
568
- function findPackageRoot() {
569
- let dir = path8.dirname(fileURLToPath(import.meta.url));
570
- const { root } = path8.parse(dir);
571
- while (dir !== root) {
572
- if (existsSync7(path8.join(dir, "package.json"))) return dir;
573
- dir = path8.dirname(dir);
287
+ try {
288
+ await client.execute({
289
+ sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
290
+ args: []
291
+ });
292
+ } catch {
574
293
  }
575
- return null;
576
- }
577
- function spawnDaemon() {
578
- const pkgRoot = findPackageRoot();
579
- if (!pkgRoot) {
580
- process.stderr.write("[exed-client] WARN: cannot find package root\n");
581
- return;
294
+ try {
295
+ await client.execute({
296
+ sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
297
+ args: []
298
+ });
299
+ } catch {
582
300
  }
583
- const daemonPath = path8.join(pkgRoot, "dist", "lib", "exe-daemon.js");
584
- if (!existsSync7(daemonPath)) {
585
- process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
586
- `);
587
- return;
301
+ try {
302
+ await client.execute({
303
+ sql: `ALTER TABLE tasks ADD COLUMN checkpoint TEXT`,
304
+ args: []
305
+ });
306
+ } catch {
588
307
  }
589
- const resolvedPath = daemonPath;
590
- process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
591
- `);
592
- const logPath = path8.join(path8.dirname(SOCKET_PATH), "exed.log");
593
- let stderrFd = "ignore";
594
308
  try {
595
- stderrFd = openSync(logPath, "a");
309
+ await client.execute({
310
+ sql: `ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER NOT NULL DEFAULT 0`,
311
+ args: []
312
+ });
596
313
  } catch {
597
314
  }
598
- const child = spawn(process.execPath, [resolvedPath], {
599
- detached: true,
600
- stdio: ["ignore", "ignore", stderrFd],
601
- env: {
602
- ...process.env,
603
- EXE_DAEMON_SOCK: SOCKET_PATH,
604
- EXE_DAEMON_PID: PID_PATH
605
- }
606
- });
607
- child.unref();
608
- if (typeof stderrFd === "number") {
609
- try {
610
- closeSync(stderrFd);
611
- } catch {
612
- }
315
+ try {
316
+ await client.execute({
317
+ sql: `ALTER TABLE tasks ADD COLUMN complexity TEXT NOT NULL DEFAULT 'standard'`,
318
+ args: []
319
+ });
320
+ } catch {
613
321
  }
614
- }
615
- function acquireSpawnLock() {
616
322
  try {
617
- const fd = openSync(SPAWN_LOCK_PATH, "wx");
618
- closeSync(fd);
619
- return true;
323
+ await client.execute({
324
+ sql: `ALTER TABLE tasks ADD COLUMN session_scope TEXT`,
325
+ args: []
326
+ });
620
327
  } catch {
621
- try {
622
- const stat = statSync(SPAWN_LOCK_PATH);
623
- if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
624
- try {
625
- unlinkSync(SPAWN_LOCK_PATH);
626
- } catch {
627
- }
628
- try {
629
- const fd = openSync(SPAWN_LOCK_PATH, "wx");
630
- closeSync(fd);
631
- return true;
632
- } catch {
633
- }
634
- }
635
- } catch {
636
- }
637
- return false;
638
328
  }
639
- }
640
- function releaseSpawnLock() {
641
329
  try {
642
- unlinkSync(SPAWN_LOCK_PATH);
330
+ await client.execute({
331
+ sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
332
+ args: []
333
+ });
643
334
  } catch {
644
335
  }
645
- }
646
- function connectToSocket() {
647
- return new Promise((resolve) => {
648
- if (_socket && _connected) {
649
- resolve(true);
650
- return;
651
- }
652
- const socket = net.createConnection({ path: SOCKET_PATH });
653
- const connectTimeout = setTimeout(() => {
654
- socket.destroy();
655
- resolve(false);
656
- }, 2e3);
657
- socket.on("connect", () => {
658
- clearTimeout(connectTimeout);
659
- _socket = socket;
660
- _connected = true;
661
- _buffer = "";
662
- socket.on("data", handleData);
663
- socket.on("close", () => {
664
- _connected = false;
665
- _socket = null;
666
- for (const [id, entry] of _pending) {
667
- clearTimeout(entry.timer);
668
- _pending.delete(id);
669
- entry.resolve({ error: "Connection closed" });
670
- }
671
- });
672
- socket.on("error", () => {
673
- _connected = false;
674
- _socket = null;
675
- });
676
- resolve(true);
677
- });
678
- socket.on("error", () => {
679
- clearTimeout(connectTimeout);
680
- resolve(false);
681
- });
682
- });
683
- }
684
- async function connectEmbedDaemon() {
685
- if (_socket && _connected) return true;
686
- if (await connectToSocket()) return true;
687
- if (acquireSpawnLock()) {
688
- try {
689
- cleanupStaleFiles();
690
- spawnDaemon();
691
- } finally {
692
- releaseSpawnLock();
693
- }
694
- }
695
- const start = Date.now();
696
- let delay2 = 100;
697
- while (Date.now() - start < CONNECT_TIMEOUT_MS) {
698
- await new Promise((r) => setTimeout(r, delay2));
699
- if (await connectToSocket()) return true;
700
- delay2 = Math.min(delay2 * 2, 3e3);
701
- }
702
- return false;
703
- }
704
- function sendRequest(texts, priority) {
705
- return new Promise((resolve) => {
706
- if (!_socket || !_connected) {
707
- resolve({ error: "Not connected" });
708
- return;
709
- }
710
- const id = randomUUID2();
711
- const timer = setTimeout(() => {
712
- _pending.delete(id);
713
- resolve({ error: "Request timeout" });
714
- }, REQUEST_TIMEOUT_MS);
715
- _pending.set(id, { resolve, timer });
716
- try {
717
- _socket.write(JSON.stringify({ id, texts, priority }) + "\n");
718
- } catch {
719
- clearTimeout(timer);
720
- _pending.delete(id);
721
- resolve({ error: "Write failed" });
722
- }
723
- });
724
- }
725
- async function pingDaemon() {
726
- if (!_socket || !_connected) return null;
727
- return new Promise((resolve) => {
728
- const id = randomUUID2();
729
- const timer = setTimeout(() => {
730
- _pending.delete(id);
731
- resolve(null);
732
- }, 5e3);
733
- _pending.set(id, {
734
- resolve: (data) => {
735
- if (data.health) {
736
- resolve(data.health);
737
- } else {
738
- resolve(null);
739
- }
740
- },
741
- timer
336
+ try {
337
+ await client.execute({
338
+ sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
339
+ args: []
742
340
  });
743
- try {
744
- _socket.write(JSON.stringify({ id, type: "health" }) + "\n");
745
- } catch {
746
- clearTimeout(timer);
747
- _pending.delete(id);
748
- resolve(null);
749
- }
750
- });
751
- }
752
- function killAndRespawnDaemon() {
753
- process.stderr.write("[exed-client] Killing daemon for restart...\n");
754
- if (existsSync7(PID_PATH)) {
755
- try {
756
- const pid = parseInt(readFileSync5(PID_PATH, "utf8").trim(), 10);
757
- if (pid > 0) {
758
- try {
759
- process.kill(pid, "SIGKILL");
760
- } catch {
761
- }
762
- }
763
- } catch {
764
- }
765
- }
766
- if (_socket) {
767
- _socket.destroy();
768
- _socket = null;
341
+ } catch {
769
342
  }
770
- _connected = false;
771
- _buffer = "";
772
343
  try {
773
- unlinkSync(PID_PATH);
344
+ await client.execute({
345
+ sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
346
+ args: []
347
+ });
774
348
  } catch {
775
349
  }
776
350
  try {
777
- unlinkSync(SOCKET_PATH);
351
+ await client.execute({
352
+ sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
353
+ args: []
354
+ });
778
355
  } catch {
779
356
  }
780
- spawnDaemon();
781
- }
782
- async function embedViaClient(text, priority = "high") {
783
- if (!_connected && !await connectEmbedDaemon()) return null;
784
- _requestCount++;
785
- if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
786
- const health = await pingDaemon();
787
- if (!health) {
788
- process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
789
- `);
790
- killAndRespawnDaemon();
791
- const start = Date.now();
792
- let delay2 = 200;
793
- while (Date.now() - start < CONNECT_TIMEOUT_MS) {
794
- await new Promise((r) => setTimeout(r, delay2));
795
- if (await connectToSocket()) break;
796
- delay2 = Math.min(delay2 * 2, 3e3);
797
- }
798
- if (!_connected) return null;
799
- }
800
- }
801
- const result = await sendRequest([text], priority);
802
- if (!result.error && result.vectors?.[0]) return result.vectors[0];
803
- if (result.error) {
804
- process.stderr.write(`[exed-client] Embed failed (${result.error}) \u2014 attempting restart
805
- `);
806
- killAndRespawnDaemon();
807
- const start = Date.now();
808
- let delay2 = 200;
809
- while (Date.now() - start < CONNECT_TIMEOUT_MS) {
810
- await new Promise((r) => setTimeout(r, delay2));
811
- if (await connectToSocket()) break;
812
- delay2 = Math.min(delay2 * 2, 3e3);
813
- }
814
- if (!_connected) return null;
815
- const retry = await sendRequest([text], priority);
816
- if (!retry.error && retry.vectors?.[0]) return retry.vectors[0];
817
- process.stderr.write(`[exed-client] Embed retry also failed: ${retry.error ?? "no vector"}
818
- `);
819
- }
820
- return null;
821
- }
822
- function disconnectClient() {
823
- if (_socket) {
824
- _socket.destroy();
825
- _socket = null;
826
- }
827
- _connected = false;
828
- _buffer = "";
829
- for (const [id, entry] of _pending) {
830
- clearTimeout(entry.timer);
831
- _pending.delete(id);
832
- entry.resolve({ error: "Client disconnected" });
833
- }
834
- }
835
- var SOCKET_PATH, PID_PATH, SPAWN_LOCK_PATH, SPAWN_LOCK_STALE_MS, CONNECT_TIMEOUT_MS, REQUEST_TIMEOUT_MS, _socket, _connected, _buffer, _requestCount, HEALTH_CHECK_INTERVAL, _pending, MAX_BUFFER;
836
- var init_exe_daemon_client = __esm({
837
- "src/lib/exe-daemon-client.ts"() {
838
- "use strict";
839
- init_config();
840
- SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path8.join(EXE_AI_DIR, "exed.sock");
841
- PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path8.join(EXE_AI_DIR, "exed.pid");
842
- SPAWN_LOCK_PATH = path8.join(EXE_AI_DIR, "exed-spawn.lock");
843
- SPAWN_LOCK_STALE_MS = 3e4;
844
- CONNECT_TIMEOUT_MS = 15e3;
845
- REQUEST_TIMEOUT_MS = 3e4;
846
- _socket = null;
847
- _connected = false;
848
- _buffer = "";
849
- _requestCount = 0;
850
- HEALTH_CHECK_INTERVAL = 100;
851
- _pending = /* @__PURE__ */ new Map();
852
- MAX_BUFFER = 1e7;
853
- }
854
- });
357
+ await client.executeMultiple(`
358
+ CREATE TABLE IF NOT EXISTS consolidations (
359
+ id TEXT PRIMARY KEY,
360
+ consolidated_memory_id TEXT NOT NULL,
361
+ source_memory_id TEXT NOT NULL,
362
+ created_at TEXT NOT NULL
363
+ );
855
364
 
856
- // src/lib/embedder.ts
857
- var embedder_exports = {};
858
- __export(embedder_exports, {
859
- disposeEmbedder: () => disposeEmbedder,
860
- embed: () => embed,
861
- embedDirect: () => embedDirect,
862
- getEmbedder: () => getEmbedder
863
- });
864
- async function getEmbedder() {
865
- const ok = await connectEmbedDaemon();
866
- if (!ok) {
867
- throw new Error(
868
- "Could not connect to embedding daemon. Ensure the model is installed (run /exe-setup)."
365
+ CREATE INDEX IF NOT EXISTS idx_consolidations_source
366
+ ON consolidations(source_memory_id);
367
+
368
+ CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
369
+ ON consolidations(consolidated_memory_id);
370
+ `);
371
+ await client.executeMultiple(`
372
+ CREATE TABLE IF NOT EXISTS reminders (
373
+ id TEXT PRIMARY KEY,
374
+ text TEXT NOT NULL,
375
+ created_at TEXT NOT NULL,
376
+ due_date TEXT,
377
+ completed_at TEXT
869
378
  );
870
- }
871
- }
872
- async function embed(text) {
873
- const priority = process.env.EXE_EMBED_PRIORITY === "low" ? "low" : "high";
874
- const vector = await embedViaClient(text, priority);
875
- if (!vector) {
876
- throw new Error(
877
- "Embedding failed: daemon unavailable. Run /exe-setup to verify model installation."
379
+ `);
380
+ await client.executeMultiple(`
381
+ CREATE TABLE IF NOT EXISTS notifications (
382
+ id TEXT PRIMARY KEY,
383
+ agent_id TEXT NOT NULL,
384
+ agent_role TEXT NOT NULL,
385
+ event TEXT NOT NULL,
386
+ project TEXT NOT NULL,
387
+ summary TEXT NOT NULL,
388
+ task_file TEXT,
389
+ read INTEGER NOT NULL DEFAULT 0,
390
+ created_at TEXT NOT NULL
878
391
  );
879
- }
880
- if (vector.length !== EMBEDDING_DIM) {
881
- throw new Error(
882
- `Embedding dimension mismatch: expected ${EMBEDDING_DIM}, got ${vector.length}. Ensure the correct Jina v5-small Q4_K_M GGUF model is installed.`
392
+
393
+ CREATE INDEX IF NOT EXISTS idx_notifications_read
394
+ ON notifications(read);
395
+
396
+ CREATE INDEX IF NOT EXISTS idx_notifications_agent
397
+ ON notifications(agent_id);
398
+
399
+ CREATE INDEX IF NOT EXISTS idx_notifications_task_file
400
+ ON notifications(task_file);
401
+ `);
402
+ await client.executeMultiple(`
403
+ CREATE TABLE IF NOT EXISTS schedules (
404
+ id TEXT PRIMARY KEY,
405
+ cron TEXT NOT NULL,
406
+ description TEXT NOT NULL,
407
+ job_type TEXT NOT NULL DEFAULT 'report',
408
+ prompt TEXT,
409
+ assigned_to TEXT,
410
+ project_name TEXT,
411
+ active INTEGER NOT NULL DEFAULT 1,
412
+ use_crontab INTEGER NOT NULL DEFAULT 0,
413
+ created_at TEXT NOT NULL
414
+ );
415
+ `);
416
+ await client.executeMultiple(`
417
+ CREATE TABLE IF NOT EXISTS device_registry (
418
+ device_id TEXT PRIMARY KEY,
419
+ friendly_name TEXT NOT NULL,
420
+ hostname TEXT NOT NULL,
421
+ projects TEXT NOT NULL DEFAULT '[]',
422
+ agents TEXT NOT NULL DEFAULT '[]',
423
+ connected INTEGER DEFAULT 0,
424
+ last_seen TEXT NOT NULL
425
+ );
426
+ `);
427
+ await client.executeMultiple(`
428
+ CREATE TABLE IF NOT EXISTS messages (
429
+ id TEXT PRIMARY KEY,
430
+ from_agent TEXT NOT NULL,
431
+ from_device TEXT NOT NULL DEFAULT 'local',
432
+ target_agent TEXT NOT NULL,
433
+ target_project TEXT,
434
+ target_device TEXT NOT NULL DEFAULT 'local',
435
+ content TEXT NOT NULL,
436
+ priority TEXT DEFAULT 'normal',
437
+ status TEXT DEFAULT 'pending',
438
+ server_seq INTEGER,
439
+ retry_count INTEGER DEFAULT 0,
440
+ created_at TEXT NOT NULL,
441
+ delivered_at TEXT,
442
+ processed_at TEXT,
443
+ failed_at TEXT,
444
+ failure_reason TEXT
883
445
  );
884
- }
885
- return vector;
886
- }
887
- async function disposeEmbedder() {
888
- disconnectClient();
889
- }
890
- async function embedDirect(text) {
891
- const llamaCpp = await import("node-llama-cpp");
892
- const { MODELS_DIR: MODELS_DIR2 } = await Promise.resolve().then(() => (init_config(), config_exports));
893
- const { existsSync: existsSync8 } = await import("fs");
894
- const path10 = await import("path");
895
- const modelPath = path10.join(MODELS_DIR2, "jina-embeddings-v5-small-q4_k_m.gguf");
896
- if (!existsSync8(modelPath)) {
897
- throw new Error(`Embedding model not found at ${modelPath}. Run '/exe-setup' to download it.`);
898
- }
899
- const llama = await llamaCpp.getLlama();
900
- const model = await llama.loadModel({ modelPath });
901
- const context = await model.createEmbeddingContext();
446
+
447
+ CREATE INDEX IF NOT EXISTS idx_messages_target
448
+ ON messages(target_agent, status);
449
+
450
+ CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
451
+ ON messages(target_agent, from_agent, server_seq);
452
+ `);
902
453
  try {
903
- const embedding = await context.getEmbeddingFor(text);
904
- const vector = Array.from(embedding.vector);
905
- if (vector.length !== EMBEDDING_DIM) {
906
- throw new Error(
907
- `Embedding dimension mismatch: expected ${EMBEDDING_DIM}, got ${vector.length}.`
908
- );
909
- }
910
- return vector;
911
- } finally {
912
- await context.dispose();
913
- await model.dispose();
914
- }
915
- }
916
- var init_embedder = __esm({
917
- "src/lib/embedder.ts"() {
918
- "use strict";
919
- init_memory();
920
- init_exe_daemon_client();
454
+ await client.execute({
455
+ sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
456
+ args: []
457
+ });
458
+ await client.execute({
459
+ sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
460
+ args: []
461
+ });
462
+ await client.execute({
463
+ sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
464
+ args: []
465
+ });
466
+ await client.execute({
467
+ sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
468
+ args: []
469
+ });
470
+ } catch {
921
471
  }
922
- });
472
+ await client.executeMultiple(`
473
+ CREATE TABLE IF NOT EXISTS trajectories (
474
+ id TEXT PRIMARY KEY,
475
+ task_id TEXT NOT NULL,
476
+ agent_id TEXT NOT NULL,
477
+ project_name TEXT NOT NULL,
478
+ task_title TEXT NOT NULL,
479
+ signature TEXT NOT NULL,
480
+ signature_hash TEXT NOT NULL,
481
+ tool_count INTEGER NOT NULL,
482
+ skill_id TEXT,
483
+ created_at TEXT NOT NULL
484
+ );
923
485
 
924
- // src/adapters/claude/hooks/prompt-ingest-worker.ts
925
- import crypto2 from "crypto";
926
- import { writeFileSync as writeFileSync2 } from "fs";
927
- import path9 from "path";
486
+ CREATE INDEX IF NOT EXISTS idx_trajectories_hash
487
+ ON trajectories(signature_hash);
928
488
 
929
- // src/lib/project-name.ts
930
- import { execSync } from "child_process";
931
- import path from "path";
932
- var _cached = null;
933
- var _cachedCwd = null;
934
- function getProjectName(cwd) {
935
- const dir = cwd ?? process.cwd();
936
- if (_cached && _cachedCwd === dir) return _cached;
489
+ CREATE INDEX IF NOT EXISTS idx_trajectories_agent
490
+ ON trajectories(agent_id);
491
+ `);
937
492
  try {
938
- let repoRoot;
939
- try {
940
- const gitCommonDir = execSync("git rev-parse --path-format=absolute --git-common-dir", {
941
- cwd: dir,
942
- encoding: "utf8",
943
- timeout: 2e3,
944
- stdio: ["pipe", "pipe", "pipe"]
945
- }).trim();
946
- repoRoot = path.dirname(gitCommonDir);
947
- } catch {
948
- repoRoot = execSync("git rev-parse --show-toplevel", {
949
- cwd: dir,
950
- encoding: "utf8",
951
- timeout: 2e3,
952
- stdio: ["pipe", "pipe", "pipe"]
953
- }).trim();
954
- }
955
- _cached = path.basename(repoRoot);
956
- _cachedCwd = dir;
957
- return _cached;
493
+ await client.execute("ALTER TABLE trajectories ADD COLUMN skill_id TEXT");
958
494
  } catch {
959
- _cached = path.basename(dir);
960
- _cachedCwd = dir;
961
- return _cached;
962
495
  }
963
- }
496
+ await client.executeMultiple(`
497
+ CREATE TABLE IF NOT EXISTS consolidations (
498
+ id TEXT PRIMARY KEY,
499
+ consolidated_memory_id TEXT NOT NULL,
500
+ source_memory_id TEXT NOT NULL,
501
+ created_at TEXT NOT NULL
502
+ );
964
503
 
965
- // src/lib/store.ts
966
- init_memory();
504
+ CREATE INDEX IF NOT EXISTS idx_consolidations_source
505
+ ON consolidations(source_memory_id);
506
+ `);
507
+ await client.executeMultiple(`
508
+ CREATE TABLE IF NOT EXISTS audit_trail (
509
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
510
+ timestamp TEXT NOT NULL,
511
+ session_id TEXT NOT NULL,
512
+ agent_id TEXT NOT NULL,
513
+ tool TEXT NOT NULL,
514
+ input TEXT,
515
+ decision TEXT NOT NULL,
516
+ reason TEXT,
517
+ is_customer_facing INTEGER NOT NULL DEFAULT 0
518
+ );
967
519
 
968
- // src/lib/database.ts
969
- import { createClient } from "@libsql/client";
520
+ CREATE INDEX IF NOT EXISTS idx_audit_trail_agent
521
+ ON audit_trail(agent_id, timestamp);
970
522
 
971
- // src/lib/db-retry.ts
972
- var MAX_RETRIES = 3;
973
- var BASE_DELAY_MS = 200;
974
- var MAX_JITTER_MS = 300;
975
- function isBusyError(err) {
976
- if (err instanceof Error) {
977
- const msg = err.message.toLowerCase();
978
- return msg.includes("sqlite_busy") || msg.includes("database is locked");
523
+ CREATE INDEX IF NOT EXISTS idx_audit_trail_session
524
+ ON audit_trail(session_id);
525
+ `);
526
+ try {
527
+ await client.execute({
528
+ sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
529
+ args: []
530
+ });
531
+ } catch {
979
532
  }
980
- return false;
981
- }
982
- function delay(ms) {
983
- return new Promise((resolve) => setTimeout(resolve, ms));
984
- }
985
- async function retryOnBusy(fn, label) {
986
- let lastError;
987
- for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
988
- try {
989
- return await fn();
990
- } catch (err) {
991
- lastError = err;
992
- if (!isBusyError(err) || attempt === MAX_RETRIES) {
993
- throw err;
994
- }
995
- const backoff = BASE_DELAY_MS * Math.pow(2, attempt);
996
- const jitter = Math.floor(Math.random() * MAX_JITTER_MS);
997
- process.stderr.write(
998
- `[exe-os] SQLITE_BUSY ${label} retry ${attempt + 1}/${MAX_RETRIES} \u2014 waiting ${backoff + jitter}ms
999
- `
1000
- );
1001
- await delay(backoff + jitter);
1002
- }
533
+ try {
534
+ await client.execute({
535
+ sql: `ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5`,
536
+ args: []
537
+ });
538
+ } catch {
1003
539
  }
1004
- throw lastError;
1005
- }
1006
- function wrapWithRetry(client) {
1007
- return new Proxy(client, {
1008
- get(target, prop, receiver) {
1009
- if (prop === "execute") {
1010
- return (sql) => retryOnBusy(() => target.execute(sql), "execute");
1011
- }
1012
- if (prop === "batch") {
1013
- return (stmts) => retryOnBusy(() => target.batch(stmts), "batch");
1014
- }
1015
- return Reflect.get(target, prop, receiver);
1016
- }
1017
- });
1018
- }
1019
-
1020
- // src/lib/database.ts
1021
- var _client = null;
1022
- var _resilientClient = null;
1023
- var initTurso = initDatabase;
1024
- async function initDatabase(config) {
1025
- if (_client) {
1026
- _client.close();
1027
- _client = null;
1028
- _resilientClient = null;
540
+ try {
541
+ await client.execute({
542
+ sql: `ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'`,
543
+ args: []
544
+ });
545
+ } catch {
1029
546
  }
1030
- const opts = {
1031
- url: `file:${config.dbPath}`
1032
- };
1033
- if (config.encryptionKey) {
1034
- opts.encryptionKey = config.encryptionKey;
547
+ try {
548
+ await client.execute({
549
+ sql: `ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7`,
550
+ args: []
551
+ });
552
+ } catch {
1035
553
  }
1036
- _client = createClient(opts);
1037
- _resilientClient = wrapWithRetry(_client);
1038
- }
1039
- function isInitialized() {
1040
- return _client !== null;
1041
- }
1042
- function getClient() {
1043
- if (!_resilientClient) {
1044
- throw new Error("Database client not initialized. Call initDatabase() first.");
554
+ try {
555
+ await client.execute({
556
+ sql: `ALTER TABLE memories ADD COLUMN last_accessed TEXT`,
557
+ args: []
558
+ });
559
+ } catch {
1045
560
  }
1046
- return _resilientClient;
1047
- }
1048
- function getRawClient() {
1049
- if (!_client) {
1050
- throw new Error("Database client not initialized. Call initDatabase() first.");
561
+ try {
562
+ await client.execute({
563
+ sql: `UPDATE memories SET last_accessed = timestamp WHERE last_accessed IS NULL`,
564
+ args: []
565
+ });
566
+ } catch {
1051
567
  }
1052
- return _client;
1053
- }
1054
- async function ensureSchema() {
1055
- const client = getRawClient();
1056
- await client.execute("PRAGMA journal_mode = WAL");
1057
- await client.execute("PRAGMA busy_timeout = 30000");
1058
- await client.execute("PRAGMA wal_autocheckpoint = 1000");
1059
568
  try {
1060
- await client.execute("PRAGMA libsql_vector_search_ef = 128");
569
+ await client.execute({
570
+ sql: `ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0`,
571
+ args: []
572
+ });
573
+ } catch {
574
+ }
575
+ try {
576
+ await client.execute({
577
+ sql: `ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0`,
578
+ args: []
579
+ });
1061
580
  } catch {
1062
581
  }
582
+ for (const col of [
583
+ "ALTER TABLE memories ADD COLUMN content_hash TEXT",
584
+ "ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT"
585
+ ]) {
586
+ try {
587
+ await client.execute(col);
588
+ } catch {
589
+ }
590
+ }
1063
591
  await client.executeMultiple(`
1064
- CREATE TABLE IF NOT EXISTS memories (
1065
- id TEXT PRIMARY KEY,
1066
- agent_id TEXT NOT NULL,
1067
- agent_role TEXT NOT NULL,
1068
- session_id TEXT NOT NULL,
1069
- timestamp TEXT NOT NULL,
1070
- tool_name TEXT NOT NULL,
1071
- project_name TEXT NOT NULL,
1072
- has_error INTEGER NOT NULL DEFAULT 0,
1073
- raw_text TEXT NOT NULL,
1074
- vector F32_BLOB(1024),
1075
- version INTEGER NOT NULL DEFAULT 0
592
+ CREATE TABLE IF NOT EXISTS entities (
593
+ id TEXT PRIMARY KEY,
594
+ name TEXT NOT NULL,
595
+ type TEXT NOT NULL,
596
+ first_seen TEXT NOT NULL,
597
+ last_seen TEXT NOT NULL,
598
+ properties TEXT DEFAULT '{}',
599
+ UNIQUE(name, type)
1076
600
  );
1077
601
 
1078
- CREATE INDEX IF NOT EXISTS idx_memories_agent
1079
- ON memories(agent_id);
1080
-
1081
- CREATE INDEX IF NOT EXISTS idx_memories_timestamp
1082
- ON memories(timestamp);
602
+ CREATE TABLE IF NOT EXISTS relationships (
603
+ id TEXT PRIMARY KEY,
604
+ source_entity_id TEXT NOT NULL,
605
+ target_entity_id TEXT NOT NULL,
606
+ type TEXT NOT NULL,
607
+ weight REAL DEFAULT 1.0,
608
+ timestamp TEXT NOT NULL,
609
+ properties TEXT DEFAULT '{}',
610
+ UNIQUE(source_entity_id, target_entity_id, type)
611
+ );
1083
612
 
1084
- CREATE INDEX IF NOT EXISTS idx_memories_session
1085
- ON memories(session_id);
613
+ CREATE TABLE IF NOT EXISTS entity_memories (
614
+ entity_id TEXT NOT NULL,
615
+ memory_id TEXT NOT NULL,
616
+ PRIMARY KEY (entity_id, memory_id)
617
+ );
1086
618
 
1087
- CREATE INDEX IF NOT EXISTS idx_memories_project
1088
- ON memories(project_name);
619
+ CREATE TABLE IF NOT EXISTS relationship_memories (
620
+ relationship_id TEXT NOT NULL,
621
+ memory_id TEXT NOT NULL,
622
+ PRIMARY KEY (relationship_id, memory_id)
623
+ );
1089
624
 
1090
- CREATE INDEX IF NOT EXISTS idx_memories_tool
1091
- ON memories(tool_name);
625
+ CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
626
+ CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
627
+ CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
628
+ CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
1092
629
 
1093
- CREATE INDEX IF NOT EXISTS idx_memories_version
1094
- ON memories(version);
630
+ CREATE TABLE IF NOT EXISTS hyperedges (
631
+ id TEXT PRIMARY KEY,
632
+ label TEXT NOT NULL,
633
+ relation TEXT NOT NULL,
634
+ confidence REAL DEFAULT 1.0,
635
+ timestamp TEXT NOT NULL
636
+ );
1095
637
 
1096
- CREATE INDEX IF NOT EXISTS idx_memories_agent_project
1097
- ON memories(agent_id, project_name);
638
+ CREATE TABLE IF NOT EXISTS hyperedge_nodes (
639
+ hyperedge_id TEXT NOT NULL,
640
+ entity_id TEXT NOT NULL,
641
+ PRIMARY KEY (hyperedge_id, entity_id)
642
+ );
1098
643
  `);
1099
644
  await client.executeMultiple(`
1100
- CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
1101
- raw_text,
1102
- content='memories',
1103
- content_rowid='rowid'
645
+ CREATE TABLE IF NOT EXISTS entity_aliases (
646
+ alias TEXT NOT NULL PRIMARY KEY,
647
+ canonical_entity_id TEXT NOT NULL
648
+ );
649
+ CREATE INDEX IF NOT EXISTS idx_entity_aliases_canonical ON entity_aliases(canonical_entity_id);
650
+ `);
651
+ for (const col of [
652
+ "ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
653
+ "ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
654
+ ]) {
655
+ try {
656
+ await client.execute(col);
657
+ } catch {
658
+ }
659
+ }
660
+ try {
661
+ await client.execute(
662
+ `CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)`
663
+ );
664
+ } catch {
665
+ }
666
+ await client.executeMultiple(`
667
+ CREATE TABLE IF NOT EXISTS identity (
668
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
669
+ agent_id TEXT NOT NULL UNIQUE,
670
+ content_hash TEXT NOT NULL,
671
+ updated_at TEXT NOT NULL,
672
+ updated_by TEXT NOT NULL
1104
673
  );
1105
674
 
1106
- CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
1107
- INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1108
- END;
1109
-
1110
- CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
1111
- INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1112
- END;
675
+ CREATE INDEX IF NOT EXISTS idx_identity_agent ON identity(agent_id);
676
+ `);
677
+ await client.executeMultiple(`
678
+ CREATE TABLE IF NOT EXISTS chat_history (
679
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
680
+ session_id TEXT NOT NULL,
681
+ role TEXT NOT NULL,
682
+ content TEXT NOT NULL,
683
+ tool_name TEXT,
684
+ tool_id TEXT,
685
+ is_error INTEGER NOT NULL DEFAULT 0,
686
+ timestamp INTEGER NOT NULL
687
+ );
1113
688
 
1114
- CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
1115
- INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1116
- INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1117
- END;
689
+ CREATE INDEX IF NOT EXISTS idx_chat_history_session
690
+ ON chat_history(session_id, id);
1118
691
  `);
1119
692
  await client.executeMultiple(`
1120
- CREATE TABLE IF NOT EXISTS sync_meta (
1121
- key TEXT PRIMARY KEY,
1122
- value TEXT NOT NULL
693
+ CREATE TABLE IF NOT EXISTS workspaces (
694
+ id TEXT PRIMARY KEY,
695
+ slug TEXT NOT NULL UNIQUE,
696
+ name TEXT NOT NULL,
697
+ owner_agent_id TEXT,
698
+ created_at TEXT NOT NULL,
699
+ metadata TEXT
1123
700
  );
701
+
702
+ CREATE INDEX IF NOT EXISTS idx_workspaces_slug
703
+ ON workspaces(slug);
1124
704
  `);
1125
705
  await client.executeMultiple(`
1126
- CREATE TABLE IF NOT EXISTS tasks (
706
+ CREATE TABLE IF NOT EXISTS documents (
1127
707
  id TEXT PRIMARY KEY,
1128
- title TEXT NOT NULL,
1129
- assigned_to TEXT NOT NULL,
1130
- assigned_by TEXT NOT NULL,
1131
- project_name TEXT NOT NULL,
1132
- priority TEXT NOT NULL DEFAULT 'p1',
1133
- status TEXT NOT NULL DEFAULT 'open',
1134
- task_file TEXT,
1135
- created_at TEXT NOT NULL,
1136
- updated_at TEXT NOT NULL
708
+ workspace_id TEXT NOT NULL,
709
+ filename TEXT NOT NULL,
710
+ mime TEXT,
711
+ source_type TEXT,
712
+ user_id TEXT,
713
+ uploaded_at TEXT NOT NULL,
714
+ metadata TEXT,
715
+ FOREIGN KEY (workspace_id) REFERENCES workspaces(id)
716
+ );
717
+
718
+ CREATE INDEX IF NOT EXISTS idx_documents_workspace
719
+ ON documents(workspace_id);
720
+
721
+ CREATE INDEX IF NOT EXISTS idx_documents_user
722
+ ON documents(user_id);
723
+ `);
724
+ for (const column of [
725
+ "workspace_id TEXT",
726
+ "document_id TEXT",
727
+ "user_id TEXT",
728
+ "char_offset INTEGER",
729
+ "page_number INTEGER"
730
+ ]) {
731
+ try {
732
+ await client.execute({
733
+ sql: `ALTER TABLE memories ADD COLUMN ${column}`,
734
+ args: []
735
+ });
736
+ } catch {
737
+ }
738
+ }
739
+ for (const col of [
740
+ "ALTER TABLE memories ADD COLUMN source_path TEXT",
741
+ "ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'"
742
+ ]) {
743
+ try {
744
+ await client.execute(col);
745
+ } catch {
746
+ }
747
+ }
748
+ await client.executeMultiple(`
749
+ CREATE INDEX IF NOT EXISTS idx_memories_workspace
750
+ ON memories(workspace_id);
751
+
752
+ CREATE INDEX IF NOT EXISTS idx_memories_document
753
+ ON memories(document_id);
754
+
755
+ CREATE INDEX IF NOT EXISTS idx_memories_user
756
+ ON memories(user_id);
757
+ `);
758
+ await client.executeMultiple(`
759
+ CREATE TABLE IF NOT EXISTS session_kills (
760
+ id TEXT PRIMARY KEY,
761
+ session_name TEXT NOT NULL,
762
+ agent_id TEXT NOT NULL,
763
+ killed_at TIMESTAMP NOT NULL,
764
+ reason TEXT NOT NULL,
765
+ ticks_idle INTEGER,
766
+ estimated_tokens_saved INTEGER
1137
767
  );
1138
768
 
1139
- CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
1140
- ON tasks(assigned_to, status);
769
+ CREATE INDEX IF NOT EXISTS idx_session_kills_killed_at
770
+ ON session_kills(killed_at);
771
+
772
+ CREATE INDEX IF NOT EXISTS idx_session_kills_agent
773
+ ON session_kills(agent_id);
774
+ `);
775
+ await client.execute(`
776
+ CREATE TABLE IF NOT EXISTS global_procedures (
777
+ id TEXT PRIMARY KEY,
778
+ title TEXT NOT NULL,
779
+ content TEXT NOT NULL,
780
+ priority TEXT NOT NULL DEFAULT 'p0',
781
+ domain TEXT,
782
+ active INTEGER NOT NULL DEFAULT 1,
783
+ created_at TEXT NOT NULL,
784
+ updated_at TEXT NOT NULL
785
+ )
1141
786
  `);
1142
787
  await client.executeMultiple(`
1143
- CREATE TABLE IF NOT EXISTS behaviors (
1144
- id TEXT PRIMARY KEY,
1145
- agent_id TEXT NOT NULL,
1146
- project_name TEXT,
1147
- domain TEXT,
1148
- content TEXT NOT NULL,
1149
- active INTEGER NOT NULL DEFAULT 1,
1150
- created_at TEXT NOT NULL,
1151
- updated_at TEXT NOT NULL
788
+ CREATE TABLE IF NOT EXISTS conversations (
789
+ id TEXT PRIMARY KEY,
790
+ platform TEXT NOT NULL,
791
+ external_id TEXT,
792
+ sender_id TEXT NOT NULL,
793
+ sender_name TEXT,
794
+ sender_phone TEXT,
795
+ sender_email TEXT,
796
+ recipient_id TEXT,
797
+ channel_id TEXT NOT NULL,
798
+ thread_id TEXT,
799
+ reply_to_id TEXT,
800
+ content_text TEXT,
801
+ content_media TEXT,
802
+ content_metadata TEXT,
803
+ agent_response TEXT,
804
+ agent_name TEXT,
805
+ timestamp TEXT NOT NULL,
806
+ ingested_at TEXT NOT NULL
1152
807
  );
1153
808
 
1154
- CREATE INDEX IF NOT EXISTS idx_behaviors_agent
1155
- ON behaviors(agent_id, active);
809
+ CREATE INDEX IF NOT EXISTS idx_conversations_platform
810
+ ON conversations(platform);
811
+
812
+ CREATE INDEX IF NOT EXISTS idx_conversations_sender
813
+ ON conversations(sender_id);
814
+
815
+ CREATE INDEX IF NOT EXISTS idx_conversations_timestamp
816
+ ON conversations(timestamp);
817
+
818
+ CREATE INDEX IF NOT EXISTS idx_conversations_thread
819
+ ON conversations(thread_id);
820
+
821
+ CREATE INDEX IF NOT EXISTS idx_conversations_channel
822
+ ON conversations(channel_id);
1156
823
  `);
1157
- try {
1158
- const existing = await client.execute({
1159
- sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
1160
- args: []
1161
- });
1162
- if (Number(existing.rows[0]?.cnt) === 0) {
1163
- await client.executeMultiple(`
1164
- INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
1165
- VALUES
1166
- (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');
1167
- INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
1168
- VALUES
1169
- (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');
1170
- INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
1171
- VALUES
1172
- (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');
1173
- `);
1174
- }
1175
- } catch {
1176
- }
1177
- try {
1178
- await client.execute({
1179
- sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
1180
- args: []
1181
- });
1182
- } catch {
1183
- }
1184
- try {
1185
- await client.execute({
1186
- sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
1187
- args: []
1188
- });
1189
- } catch {
1190
- }
1191
- try {
1192
- await client.execute({
1193
- sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
1194
- args: []
1195
- });
1196
- } catch {
1197
- }
1198
- try {
1199
- await client.execute({
1200
- sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
1201
- ON tasks(parent_task_id)
1202
- WHERE parent_task_id IS NOT NULL`,
1203
- args: []
1204
- });
1205
- } catch {
1206
- }
1207
- try {
1208
- await client.execute({
1209
- sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
1210
- args: []
1211
- });
1212
- } catch {
1213
- }
1214
- try {
1215
- await client.execute({
1216
- sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
1217
- args: []
1218
- });
1219
- } catch {
1220
- }
1221
- try {
1222
- await client.execute({
1223
- sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
1224
- args: []
1225
- });
1226
- } catch {
1227
- }
1228
- try {
1229
- await client.execute({
1230
- sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
1231
- args: []
1232
- });
1233
- } catch {
1234
- }
1235
- try {
1236
- await client.execute({
1237
- sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
1238
- args: []
1239
- });
1240
- } catch {
1241
- }
1242
- try {
1243
- await client.execute({
1244
- sql: `ALTER TABLE tasks ADD COLUMN checkpoint TEXT`,
1245
- args: []
1246
- });
1247
- } catch {
1248
- }
1249
824
  try {
1250
825
  await client.execute({
1251
- sql: `ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER NOT NULL DEFAULT 0`,
1252
- args: []
1253
- });
1254
- } catch {
1255
- }
1256
- try {
1257
- await client.execute({
1258
- sql: `ALTER TABLE tasks ADD COLUMN complexity TEXT NOT NULL DEFAULT 'standard'`,
1259
- args: []
1260
- });
1261
- } catch {
1262
- }
1263
- try {
1264
- await client.execute({
1265
- sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
826
+ sql: `ALTER TABLE tasks ADD COLUMN budget_tokens INTEGER`,
1266
827
  args: []
1267
828
  });
1268
829
  } catch {
1269
830
  }
1270
831
  try {
1271
832
  await client.execute({
1272
- sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
833
+ sql: `ALTER TABLE tasks ADD COLUMN budget_fallback_model TEXT`,
1273
834
  args: []
1274
835
  });
1275
836
  } catch {
1276
837
  }
1277
838
  try {
1278
839
  await client.execute({
1279
- sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
840
+ sql: `ALTER TABLE tasks ADD COLUMN tokens_used INTEGER DEFAULT 0`,
1280
841
  args: []
1281
842
  });
1282
843
  } catch {
1283
844
  }
1284
845
  try {
1285
846
  await client.execute({
1286
- sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
847
+ sql: `ALTER TABLE tasks ADD COLUMN tokens_warned_at INTEGER`,
1287
848
  args: []
1288
849
  });
1289
850
  } catch {
1290
851
  }
1291
852
  await client.executeMultiple(`
1292
- CREATE TABLE IF NOT EXISTS consolidations (
1293
- id TEXT PRIMARY KEY,
1294
- consolidated_memory_id TEXT NOT NULL,
1295
- source_memory_id TEXT NOT NULL,
1296
- created_at TEXT NOT NULL
1297
- );
1298
-
1299
- CREATE INDEX IF NOT EXISTS idx_consolidations_source
1300
- ON consolidations(source_memory_id);
1301
-
1302
- CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
1303
- ON consolidations(consolidated_memory_id);
1304
- `);
1305
- await client.executeMultiple(`
1306
- CREATE TABLE IF NOT EXISTS reminders (
1307
- id TEXT PRIMARY KEY,
1308
- text TEXT NOT NULL,
1309
- created_at TEXT NOT NULL,
1310
- due_date TEXT,
1311
- completed_at TEXT
1312
- );
1313
- `);
1314
- await client.executeMultiple(`
1315
- CREATE TABLE IF NOT EXISTS notifications (
1316
- id TEXT PRIMARY KEY,
1317
- agent_id TEXT NOT NULL,
1318
- agent_role TEXT NOT NULL,
1319
- event TEXT NOT NULL,
1320
- project TEXT NOT NULL,
1321
- summary TEXT NOT NULL,
1322
- task_file TEXT,
1323
- read INTEGER NOT NULL DEFAULT 0,
1324
- created_at TEXT NOT NULL
1325
- );
1326
-
1327
- CREATE INDEX IF NOT EXISTS idx_notifications_read
1328
- ON notifications(read);
1329
-
1330
- CREATE INDEX IF NOT EXISTS idx_notifications_agent
1331
- ON notifications(agent_id);
1332
-
1333
- CREATE INDEX IF NOT EXISTS idx_notifications_task_file
1334
- ON notifications(task_file);
1335
- `);
1336
- await client.executeMultiple(`
1337
- CREATE TABLE IF NOT EXISTS schedules (
1338
- id TEXT PRIMARY KEY,
1339
- cron TEXT NOT NULL,
1340
- description TEXT NOT NULL,
1341
- job_type TEXT NOT NULL DEFAULT 'report',
1342
- prompt TEXT,
1343
- assigned_to TEXT,
1344
- project_name TEXT,
1345
- active INTEGER NOT NULL DEFAULT 1,
1346
- use_crontab INTEGER NOT NULL DEFAULT 0,
1347
- created_at TEXT NOT NULL
1348
- );
1349
- `);
1350
- await client.executeMultiple(`
1351
- CREATE TABLE IF NOT EXISTS device_registry (
1352
- device_id TEXT PRIMARY KEY,
1353
- friendly_name TEXT NOT NULL,
1354
- hostname TEXT NOT NULL,
1355
- projects TEXT NOT NULL DEFAULT '[]',
1356
- agents TEXT NOT NULL DEFAULT '[]',
1357
- connected INTEGER DEFAULT 0,
1358
- last_seen TEXT NOT NULL
1359
- );
1360
- `);
1361
- await client.executeMultiple(`
1362
- CREATE TABLE IF NOT EXISTS messages (
1363
- id TEXT PRIMARY KEY,
1364
- from_agent TEXT NOT NULL,
1365
- from_device TEXT NOT NULL DEFAULT 'local',
1366
- target_agent TEXT NOT NULL,
1367
- target_project TEXT,
1368
- target_device TEXT NOT NULL DEFAULT 'local',
1369
- content TEXT NOT NULL,
1370
- priority TEXT DEFAULT 'normal',
1371
- status TEXT DEFAULT 'pending',
1372
- server_seq INTEGER,
1373
- retry_count INTEGER DEFAULT 0,
1374
- created_at TEXT NOT NULL,
1375
- delivered_at TEXT,
1376
- processed_at TEXT,
1377
- failed_at TEXT,
1378
- failure_reason TEXT
853
+ CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
854
+ content_text,
855
+ sender_name,
856
+ agent_response,
857
+ content='conversations',
858
+ content_rowid='rowid'
1379
859
  );
1380
860
 
1381
- CREATE INDEX IF NOT EXISTS idx_messages_target
1382
- ON messages(target_agent, status);
861
+ CREATE TRIGGER IF NOT EXISTS conversations_fts_ai AFTER INSERT ON conversations BEGIN
862
+ INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
863
+ VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
864
+ END;
1383
865
 
1384
- CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
1385
- ON messages(target_agent, from_agent, server_seq);
866
+ CREATE TRIGGER IF NOT EXISTS conversations_fts_ad AFTER DELETE ON conversations BEGIN
867
+ INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
868
+ VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
869
+ END;
870
+
871
+ CREATE TRIGGER IF NOT EXISTS conversations_fts_au AFTER UPDATE ON conversations BEGIN
872
+ INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
873
+ VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
874
+ INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
875
+ VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
876
+ END;
1386
877
  `);
1387
878
  try {
1388
879
  await client.execute({
1389
- sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
880
+ sql: `ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3`,
1390
881
  args: []
1391
882
  });
883
+ } catch {
884
+ }
885
+ try {
886
+ await client.execute(
887
+ `CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)`
888
+ );
889
+ } catch {
890
+ }
891
+ try {
1392
892
  await client.execute({
1393
- sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
893
+ sql: `UPDATE memories SET tier = 1 WHERE tool_name = 'commit_to_long_term_memory' AND importance >= 8 AND tier = 3`,
1394
894
  args: []
1395
895
  });
1396
896
  await client.execute({
1397
- sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
897
+ sql: `UPDATE memories SET tier = 2 WHERE tool_name IN ('store_memory', 'manual') AND importance >= 5 AND tier = 3`,
1398
898
  args: []
1399
899
  });
900
+ } catch {
901
+ }
902
+ try {
1400
903
  await client.execute({
1401
- sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
904
+ sql: `ALTER TABLE memories ADD COLUMN supersedes_id TEXT`,
1402
905
  args: []
1403
906
  });
1404
907
  } catch {
1405
908
  }
1406
- await client.executeMultiple(`
1407
- CREATE TABLE IF NOT EXISTS trajectories (
1408
- id TEXT PRIMARY KEY,
1409
- task_id TEXT NOT NULL,
1410
- agent_id TEXT NOT NULL,
1411
- project_name TEXT NOT NULL,
1412
- task_title TEXT NOT NULL,
1413
- signature TEXT NOT NULL,
1414
- signature_hash TEXT NOT NULL,
1415
- tool_count INTEGER NOT NULL,
1416
- skill_id TEXT,
1417
- created_at TEXT NOT NULL
1418
- );
1419
-
1420
- CREATE INDEX IF NOT EXISTS idx_trajectories_hash
1421
- ON trajectories(signature_hash);
909
+ try {
910
+ await client.execute(
911
+ `CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL`
912
+ );
913
+ } catch {
914
+ }
915
+ for (const col of [
916
+ "ALTER TABLE tasks ADD COLUMN checkpoint TEXT",
917
+ "ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER DEFAULT 0"
918
+ ]) {
919
+ try {
920
+ await client.execute(col);
921
+ } catch {
922
+ }
923
+ }
924
+ }
925
+ var _client, _resilientClient, initTurso;
926
+ var init_database = __esm({
927
+ "src/lib/database.ts"() {
928
+ "use strict";
929
+ init_db_retry();
930
+ _client = null;
931
+ _resilientClient = null;
932
+ initTurso = initDatabase;
933
+ }
934
+ });
935
+
936
+ // src/lib/config.ts
937
+ var config_exports = {};
938
+ __export(config_exports, {
939
+ CONFIG_MIGRATIONS: () => CONFIG_MIGRATIONS,
940
+ CONFIG_PATH: () => CONFIG_PATH,
941
+ COO_AGENT_NAME: () => COO_AGENT_NAME,
942
+ CURRENT_CONFIG_VERSION: () => CURRENT_CONFIG_VERSION,
943
+ DB_PATH: () => DB_PATH,
944
+ EXE_AI_DIR: () => EXE_AI_DIR,
945
+ LEGACY_LANCE_PATH: () => LEGACY_LANCE_PATH,
946
+ MODELS_DIR: () => MODELS_DIR,
947
+ loadConfig: () => loadConfig,
948
+ loadConfigFrom: () => loadConfigFrom,
949
+ loadConfigSync: () => loadConfigSync,
950
+ migrateConfig: () => migrateConfig,
951
+ saveConfig: () => saveConfig
952
+ });
953
+ import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2, chmod as chmod2 } from "fs/promises";
954
+ import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
955
+ import path3 from "path";
956
+ import os2 from "os";
957
+ function resolveDataDir() {
958
+ if (process.env.EXE_OS_DIR) return process.env.EXE_OS_DIR;
959
+ if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
960
+ const newDir = path3.join(os2.homedir(), ".exe-os");
961
+ const legacyDir = path3.join(os2.homedir(), ".exe-mem");
962
+ if (!existsSync2(newDir) && existsSync2(legacyDir)) {
963
+ try {
964
+ renameSync(legacyDir, newDir);
965
+ process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
966
+ `);
967
+ } catch {
968
+ return legacyDir;
969
+ }
970
+ }
971
+ return newDir;
972
+ }
973
+ function migrateLegacyConfig(raw) {
974
+ if ("r2" in raw) {
975
+ process.stderr.write(
976
+ "[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"
977
+ );
978
+ delete raw.r2;
979
+ }
980
+ if ("syncIntervalMs" in raw) {
981
+ delete raw.syncIntervalMs;
982
+ }
983
+ return raw;
984
+ }
985
+ function migrateConfig(raw) {
986
+ const fromVersion = typeof raw.config_version === "number" ? raw.config_version : 0;
987
+ let currentVersion = fromVersion;
988
+ let migrated = false;
989
+ if (currentVersion > CURRENT_CONFIG_VERSION) {
990
+ return { config: raw, migrated: false, fromVersion };
991
+ }
992
+ for (const migration of CONFIG_MIGRATIONS) {
993
+ if (currentVersion === migration.from && migration.to <= CURRENT_CONFIG_VERSION) {
994
+ raw = migration.migrate(raw);
995
+ currentVersion = migration.to;
996
+ migrated = true;
997
+ }
998
+ }
999
+ return { config: raw, migrated, fromVersion };
1000
+ }
1001
+ function normalizeScalingRoadmap(raw) {
1002
+ const defaultAuto = DEFAULT_CONFIG.scalingRoadmap.rerankerAutoTrigger;
1003
+ const userRoadmap = raw.scalingRoadmap ?? {};
1004
+ const userAuto = userRoadmap.rerankerAutoTrigger ?? {};
1005
+ if (userAuto.enabled === void 0 && raw.rerankerEnabled !== void 0) {
1006
+ userAuto.enabled = raw.rerankerEnabled;
1007
+ }
1008
+ raw.scalingRoadmap = {
1009
+ ...userRoadmap,
1010
+ rerankerAutoTrigger: { ...defaultAuto, ...userAuto }
1011
+ };
1012
+ }
1013
+ function normalizeSessionLifecycle(raw) {
1014
+ const defaultSL = DEFAULT_CONFIG.sessionLifecycle;
1015
+ const userSL = raw.sessionLifecycle ?? {};
1016
+ raw.sessionLifecycle = { ...defaultSL, ...userSL };
1017
+ }
1018
+ function normalizeAutoUpdate(raw) {
1019
+ const defaultAU = DEFAULT_CONFIG.autoUpdate;
1020
+ const userAU = raw.autoUpdate ?? {};
1021
+ raw.autoUpdate = { ...defaultAU, ...userAU };
1022
+ }
1023
+ async function loadConfig() {
1024
+ const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
1025
+ await mkdir2(dir, { recursive: true });
1026
+ const configPath = path3.join(dir, "config.json");
1027
+ if (!existsSync2(configPath)) {
1028
+ return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db") };
1029
+ }
1030
+ const raw = await readFile2(configPath, "utf-8");
1031
+ try {
1032
+ let parsed = JSON.parse(raw);
1033
+ parsed = migrateLegacyConfig(parsed);
1034
+ const { config: migratedCfg, migrated, fromVersion } = migrateConfig(parsed);
1035
+ if (migrated) {
1036
+ process.stderr.write(`[exe-os] Config migrated from v${fromVersion} to v${migratedCfg.config_version}
1037
+ `);
1038
+ try {
1039
+ await writeFile2(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
1040
+ } catch {
1041
+ }
1042
+ }
1043
+ normalizeScalingRoadmap(migratedCfg);
1044
+ normalizeSessionLifecycle(migratedCfg);
1045
+ normalizeAutoUpdate(migratedCfg);
1046
+ const config = { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db"), ...migratedCfg };
1047
+ if (config.dbPath.startsWith("~")) {
1048
+ config.dbPath = config.dbPath.replace(/^~/, os2.homedir());
1049
+ }
1050
+ return config;
1051
+ } catch {
1052
+ return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db") };
1053
+ }
1054
+ }
1055
+ function loadConfigSync() {
1056
+ const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
1057
+ const configPath = path3.join(dir, "config.json");
1058
+ if (!existsSync2(configPath)) {
1059
+ return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db") };
1060
+ }
1061
+ try {
1062
+ const raw = readFileSync(configPath, "utf-8");
1063
+ let parsed = JSON.parse(raw);
1064
+ parsed = migrateLegacyConfig(parsed);
1065
+ const { config: migratedCfg } = migrateConfig(parsed);
1066
+ normalizeScalingRoadmap(migratedCfg);
1067
+ normalizeSessionLifecycle(migratedCfg);
1068
+ normalizeAutoUpdate(migratedCfg);
1069
+ return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db"), ...migratedCfg };
1070
+ } catch {
1071
+ return { ...DEFAULT_CONFIG, dbPath: path3.join(dir, "memories.db") };
1072
+ }
1073
+ }
1074
+ async function saveConfig(config) {
1075
+ const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
1076
+ await mkdir2(dir, { recursive: true });
1077
+ const configPath = path3.join(dir, "config.json");
1078
+ await writeFile2(configPath, JSON.stringify(config, null, 2) + "\n");
1079
+ if (config.cloud?.apiKey) {
1080
+ await chmod2(configPath, 384);
1081
+ }
1082
+ }
1083
+ async function loadConfigFrom(configPath) {
1084
+ const raw = await readFile2(configPath, "utf-8");
1085
+ try {
1086
+ let parsed = JSON.parse(raw);
1087
+ parsed = migrateLegacyConfig(parsed);
1088
+ const { config: migratedCfg } = migrateConfig(parsed);
1089
+ normalizeScalingRoadmap(migratedCfg);
1090
+ normalizeSessionLifecycle(migratedCfg);
1091
+ normalizeAutoUpdate(migratedCfg);
1092
+ return { ...DEFAULT_CONFIG, ...migratedCfg };
1093
+ } catch {
1094
+ return { ...DEFAULT_CONFIG };
1095
+ }
1096
+ }
1097
+ var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, COO_AGENT_NAME, LEGACY_LANCE_PATH, CURRENT_CONFIG_VERSION, DEFAULT_CONFIG, CONFIG_MIGRATIONS;
1098
+ var init_config = __esm({
1099
+ "src/lib/config.ts"() {
1100
+ "use strict";
1101
+ EXE_AI_DIR = resolveDataDir();
1102
+ DB_PATH = path3.join(EXE_AI_DIR, "memories.db");
1103
+ MODELS_DIR = path3.join(EXE_AI_DIR, "models");
1104
+ CONFIG_PATH = path3.join(EXE_AI_DIR, "config.json");
1105
+ COO_AGENT_NAME = "exe";
1106
+ LEGACY_LANCE_PATH = path3.join(EXE_AI_DIR, "local.lance");
1107
+ CURRENT_CONFIG_VERSION = 1;
1108
+ DEFAULT_CONFIG = {
1109
+ config_version: CURRENT_CONFIG_VERSION,
1110
+ dbPath: DB_PATH,
1111
+ modelFile: "jina-embeddings-v5-small-q4_k_m.gguf",
1112
+ embeddingDim: 1024,
1113
+ batchSize: 20,
1114
+ flushIntervalMs: 1e4,
1115
+ autoIngestion: true,
1116
+ autoRetrieval: true,
1117
+ searchMode: "hybrid",
1118
+ hookSearchMode: "hybrid",
1119
+ fileGrepEnabled: true,
1120
+ splashEffect: true,
1121
+ consolidationEnabled: true,
1122
+ consolidationIntervalMs: 6 * 60 * 60 * 1e3,
1123
+ consolidationModel: "claude-haiku-4-5-20251001",
1124
+ consolidationMaxCallsPerRun: 20,
1125
+ selfQueryRouter: true,
1126
+ selfQueryModel: "claude-haiku-4-5-20251001",
1127
+ rerankerEnabled: true,
1128
+ scalingRoadmap: {
1129
+ rerankerAutoTrigger: {
1130
+ enabled: true,
1131
+ broadQueryMinCardinality: 5e4,
1132
+ fetchTopK: 150,
1133
+ returnTopK: 5
1134
+ }
1135
+ },
1136
+ graphRagEnabled: true,
1137
+ wikiEnabled: false,
1138
+ wikiUrl: "",
1139
+ wikiApiKey: "",
1140
+ wikiSyncIntervalMs: 30 * 60 * 1e3,
1141
+ wikiWorkspaceMapping: {
1142
+ exe: "Executive",
1143
+ yoshi: "Engineering",
1144
+ mari: "Marketing",
1145
+ tom: "Engineering",
1146
+ sasha: "Production"
1147
+ },
1148
+ wikiAutoUpdate: true,
1149
+ wikiAutoUpdateThreshold: 0.5,
1150
+ wikiAutoUpdateCreateNew: true,
1151
+ skillLearning: true,
1152
+ skillThreshold: 3,
1153
+ skillModel: "claude-haiku-4-5-20251001",
1154
+ exeHeartbeat: {
1155
+ enabled: true,
1156
+ intervalSeconds: 60,
1157
+ staleInProgressThresholdHours: 2
1158
+ },
1159
+ sessionLifecycle: {
1160
+ idleKillEnabled: true,
1161
+ idleKillTicksRequired: 3,
1162
+ idleKillIntercomAckWindowMs: 1e4,
1163
+ maxAutoInstances: 10
1164
+ },
1165
+ autoUpdate: {
1166
+ checkOnBoot: true,
1167
+ autoInstall: false,
1168
+ checkIntervalMs: 24 * 60 * 60 * 1e3
1169
+ }
1170
+ };
1171
+ CONFIG_MIGRATIONS = [
1172
+ {
1173
+ from: 0,
1174
+ to: 1,
1175
+ migrate: (cfg) => {
1176
+ cfg.config_version = 1;
1177
+ return cfg;
1178
+ }
1179
+ }
1180
+ ];
1181
+ }
1182
+ });
1422
1183
 
1423
- CREATE INDEX IF NOT EXISTS idx_trajectories_agent
1424
- ON trajectories(agent_id);
1425
- `);
1184
+ // src/lib/shard-manager.ts
1185
+ var shard_manager_exports = {};
1186
+ __export(shard_manager_exports, {
1187
+ disposeShards: () => disposeShards,
1188
+ ensureShardSchema: () => ensureShardSchema,
1189
+ getReadyShardClient: () => getReadyShardClient,
1190
+ getShardClient: () => getShardClient,
1191
+ getShardsDir: () => getShardsDir,
1192
+ initShardManager: () => initShardManager,
1193
+ isShardingEnabled: () => isShardingEnabled,
1194
+ listShards: () => listShards,
1195
+ shardExists: () => shardExists
1196
+ });
1197
+ import path4 from "path";
1198
+ import { existsSync as existsSync3, mkdirSync, readdirSync } from "fs";
1199
+ import { createClient as createClient2 } from "@libsql/client";
1200
+ function initShardManager(encryptionKey) {
1201
+ _encryptionKey = encryptionKey;
1202
+ if (!existsSync3(SHARDS_DIR)) {
1203
+ mkdirSync(SHARDS_DIR, { recursive: true });
1204
+ }
1205
+ _shardingEnabled = true;
1206
+ }
1207
+ function isShardingEnabled() {
1208
+ return _shardingEnabled;
1209
+ }
1210
+ function getShardsDir() {
1211
+ return SHARDS_DIR;
1212
+ }
1213
+ function getShardClient(projectName) {
1214
+ if (!_encryptionKey) {
1215
+ throw new Error("Shard manager not initialized. Call initShardManager() first.");
1216
+ }
1217
+ const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
1218
+ if (!safeName) {
1219
+ throw new Error(`Invalid project name for shard: "${projectName}"`);
1220
+ }
1221
+ const cached = _shards.get(safeName);
1222
+ if (cached) return cached;
1223
+ const dbPath = path4.join(SHARDS_DIR, `${safeName}.db`);
1224
+ const client = createClient2({
1225
+ url: `file:${dbPath}`,
1226
+ encryptionKey: _encryptionKey
1227
+ });
1228
+ _shards.set(safeName, client);
1229
+ return client;
1230
+ }
1231
+ function shardExists(projectName) {
1232
+ const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
1233
+ return existsSync3(path4.join(SHARDS_DIR, `${safeName}.db`));
1234
+ }
1235
+ function listShards() {
1236
+ if (!existsSync3(SHARDS_DIR)) return [];
1237
+ return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
1238
+ }
1239
+ async function ensureShardSchema(client) {
1240
+ await client.execute("PRAGMA journal_mode = WAL");
1241
+ await client.execute("PRAGMA busy_timeout = 30000");
1426
1242
  try {
1427
- await client.execute("ALTER TABLE trajectories ADD COLUMN skill_id TEXT");
1243
+ await client.execute("PRAGMA libsql_vector_search_ef = 128");
1428
1244
  } catch {
1429
1245
  }
1430
1246
  await client.executeMultiple(`
1431
- CREATE TABLE IF NOT EXISTS consolidations (
1432
- id TEXT PRIMARY KEY,
1433
- consolidated_memory_id TEXT NOT NULL,
1434
- source_memory_id TEXT NOT NULL,
1435
- created_at TEXT NOT NULL
1247
+ CREATE TABLE IF NOT EXISTS memories (
1248
+ id TEXT PRIMARY KEY,
1249
+ agent_id TEXT NOT NULL,
1250
+ agent_role TEXT NOT NULL,
1251
+ session_id TEXT NOT NULL,
1252
+ timestamp TEXT NOT NULL,
1253
+ tool_name TEXT NOT NULL,
1254
+ project_name TEXT NOT NULL,
1255
+ has_error INTEGER NOT NULL DEFAULT 0,
1256
+ raw_text TEXT NOT NULL,
1257
+ vector F32_BLOB(1024),
1258
+ version INTEGER NOT NULL DEFAULT 0
1436
1259
  );
1437
1260
 
1438
- CREATE INDEX IF NOT EXISTS idx_consolidations_source
1439
- ON consolidations(source_memory_id);
1261
+ CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
1262
+ CREATE INDEX IF NOT EXISTS idx_memories_timestamp ON memories(timestamp);
1263
+ CREATE INDEX IF NOT EXISTS idx_memories_agent_project ON memories(agent_id, project_name);
1440
1264
  `);
1441
1265
  await client.executeMultiple(`
1442
- CREATE TABLE IF NOT EXISTS audit_trail (
1443
- id INTEGER PRIMARY KEY AUTOINCREMENT,
1444
- timestamp TEXT NOT NULL,
1445
- session_id TEXT NOT NULL,
1446
- agent_id TEXT NOT NULL,
1447
- tool TEXT NOT NULL,
1448
- input TEXT,
1449
- decision TEXT NOT NULL,
1450
- reason TEXT,
1451
- is_customer_facing INTEGER NOT NULL DEFAULT 0
1266
+ CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
1267
+ raw_text,
1268
+ content='memories',
1269
+ content_rowid='rowid'
1452
1270
  );
1453
1271
 
1454
- CREATE INDEX IF NOT EXISTS idx_audit_trail_agent
1455
- ON audit_trail(agent_id, timestamp);
1272
+ CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
1273
+ INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1274
+ END;
1456
1275
 
1457
- CREATE INDEX IF NOT EXISTS idx_audit_trail_session
1458
- ON audit_trail(session_id);
1276
+ CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
1277
+ INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1278
+ END;
1279
+
1280
+ CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
1281
+ INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1282
+ INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1283
+ END;
1459
1284
  `);
1460
- try {
1461
- await client.execute({
1462
- sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
1463
- args: []
1464
- });
1465
- } catch {
1466
- }
1467
- try {
1468
- await client.execute({
1469
- sql: `ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5`,
1470
- args: []
1471
- });
1472
- } catch {
1473
- }
1474
- try {
1475
- await client.execute({
1476
- sql: `ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'`,
1477
- args: []
1478
- });
1479
- } catch {
1480
- }
1481
- try {
1482
- await client.execute({
1483
- sql: `ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7`,
1484
- args: []
1485
- });
1486
- } catch {
1487
- }
1488
- try {
1489
- await client.execute({
1490
- sql: `ALTER TABLE memories ADD COLUMN last_accessed TEXT`,
1491
- args: []
1492
- });
1493
- } catch {
1494
- }
1495
- try {
1496
- await client.execute({
1497
- sql: `UPDATE memories SET last_accessed = timestamp WHERE last_accessed IS NULL`,
1498
- args: []
1499
- });
1500
- } catch {
1501
- }
1502
- try {
1503
- await client.execute({
1504
- sql: `ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0`,
1505
- args: []
1506
- });
1507
- } catch {
1285
+ for (const col of [
1286
+ "ALTER TABLE memories ADD COLUMN task_id TEXT",
1287
+ "ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
1288
+ "ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
1289
+ "ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
1290
+ "ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
1291
+ "ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0",
1292
+ "ALTER TABLE memories ADD COLUMN content_hash TEXT",
1293
+ "ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT",
1294
+ "ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7",
1295
+ "ALTER TABLE memories ADD COLUMN last_accessed TEXT",
1296
+ // Wiki linkage columns (must match database.ts)
1297
+ "ALTER TABLE memories ADD COLUMN workspace_id TEXT",
1298
+ "ALTER TABLE memories ADD COLUMN document_id TEXT",
1299
+ "ALTER TABLE memories ADD COLUMN user_id TEXT",
1300
+ "ALTER TABLE memories ADD COLUMN char_offset INTEGER",
1301
+ "ALTER TABLE memories ADD COLUMN page_number INTEGER",
1302
+ // Source provenance columns (must match database.ts)
1303
+ "ALTER TABLE memories ADD COLUMN source_path TEXT",
1304
+ "ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'",
1305
+ "ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3",
1306
+ "ALTER TABLE memories ADD COLUMN supersedes_id TEXT"
1307
+ ]) {
1308
+ try {
1309
+ await client.execute(col);
1310
+ } catch {
1311
+ }
1312
+ }
1313
+ for (const idx of [
1314
+ "CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)",
1315
+ "CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL"
1316
+ ]) {
1317
+ try {
1318
+ await client.execute(idx);
1319
+ } catch {
1320
+ }
1508
1321
  }
1509
1322
  try {
1510
- await client.execute({
1511
- sql: `ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0`,
1512
- args: []
1513
- });
1323
+ await client.execute("CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)");
1514
1324
  } catch {
1515
1325
  }
1516
- for (const col of [
1517
- "ALTER TABLE memories ADD COLUMN content_hash TEXT",
1518
- "ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT"
1326
+ for (const idx of [
1327
+ "CREATE INDEX IF NOT EXISTS idx_memories_workspace ON memories(workspace_id)",
1328
+ "CREATE INDEX IF NOT EXISTS idx_memories_document ON memories(document_id)",
1329
+ "CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)"
1519
1330
  ]) {
1520
1331
  try {
1521
- await client.execute(col);
1332
+ await client.execute(idx);
1522
1333
  } catch {
1523
1334
  }
1524
1335
  }
@@ -1560,6 +1371,7 @@ async function ensureSchema() {
1560
1371
  CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
1561
1372
  CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
1562
1373
  CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
1374
+ CREATE INDEX IF NOT EXISTS idx_relationships_type ON relationships(type);
1563
1375
 
1564
1376
  CREATE TABLE IF NOT EXISTS hyperedges (
1565
1377
  id TEXT PRIMARY KEY,
@@ -1575,276 +1387,560 @@ async function ensureSchema() {
1575
1387
  PRIMARY KEY (hyperedge_id, entity_id)
1576
1388
  );
1577
1389
  `);
1578
- await client.executeMultiple(`
1579
- CREATE TABLE IF NOT EXISTS entity_aliases (
1580
- alias TEXT NOT NULL PRIMARY KEY,
1581
- canonical_entity_id TEXT NOT NULL
1582
- );
1583
- CREATE INDEX IF NOT EXISTS idx_entity_aliases_canonical ON entity_aliases(canonical_entity_id);
1584
- `);
1585
1390
  for (const col of [
1586
1391
  "ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
1587
1392
  "ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
1588
1393
  ]) {
1589
1394
  try {
1590
- await client.execute(col);
1591
- } catch {
1395
+ await client.execute(col);
1396
+ } catch {
1397
+ }
1398
+ }
1399
+ }
1400
+ async function getReadyShardClient(projectName) {
1401
+ const client = getShardClient(projectName);
1402
+ await ensureShardSchema(client);
1403
+ return client;
1404
+ }
1405
+ function disposeShards() {
1406
+ for (const [, client] of _shards) {
1407
+ client.close();
1408
+ }
1409
+ _shards.clear();
1410
+ _shardingEnabled = false;
1411
+ _encryptionKey = null;
1412
+ }
1413
+ var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
1414
+ var init_shard_manager = __esm({
1415
+ "src/lib/shard-manager.ts"() {
1416
+ "use strict";
1417
+ init_config();
1418
+ SHARDS_DIR = path4.join(EXE_AI_DIR, "shards");
1419
+ _shards = /* @__PURE__ */ new Map();
1420
+ _encryptionKey = null;
1421
+ _shardingEnabled = false;
1422
+ }
1423
+ });
1424
+
1425
+ // src/lib/global-procedures.ts
1426
+ var global_procedures_exports = {};
1427
+ __export(global_procedures_exports, {
1428
+ deactivateGlobalProcedure: () => deactivateGlobalProcedure,
1429
+ getGlobalProceduresBlock: () => getGlobalProceduresBlock,
1430
+ loadGlobalProcedures: () => loadGlobalProcedures,
1431
+ storeGlobalProcedure: () => storeGlobalProcedure
1432
+ });
1433
+ import { randomUUID } from "crypto";
1434
+ async function loadGlobalProcedures() {
1435
+ const client = getClient();
1436
+ const result = await client.execute({
1437
+ sql: "SELECT * FROM global_procedures WHERE active = 1 ORDER BY priority ASC, created_at ASC",
1438
+ args: []
1439
+ });
1440
+ const procedures = result.rows;
1441
+ if (procedures.length > 0) {
1442
+ _cache = procedures.map((p) => `### ${p.title}
1443
+ ${p.content}`).join("\n\n");
1444
+ } else {
1445
+ _cache = "";
1446
+ }
1447
+ _cacheLoaded = true;
1448
+ return procedures;
1449
+ }
1450
+ function getGlobalProceduresBlock() {
1451
+ if (!_cacheLoaded) return "";
1452
+ if (!_cache) return "";
1453
+ return `## Organization-Wide Procedures (MANDATORY \u2014 supersedes all other rules)
1454
+
1455
+ ${_cache}
1456
+ `;
1457
+ }
1458
+ async function storeGlobalProcedure(input) {
1459
+ const id = randomUUID();
1460
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1461
+ const client = getClient();
1462
+ await client.execute({
1463
+ sql: `INSERT INTO global_procedures (id, title, content, priority, domain, active, created_at, updated_at)
1464
+ VALUES (?, ?, ?, ?, ?, 1, ?, ?)`,
1465
+ args: [id, input.title, input.content, input.priority ?? "p0", input.domain ?? null, now, now]
1466
+ });
1467
+ await loadGlobalProcedures();
1468
+ return id;
1469
+ }
1470
+ async function deactivateGlobalProcedure(id) {
1471
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1472
+ const client = getClient();
1473
+ const result = await client.execute({
1474
+ sql: "UPDATE global_procedures SET active = 0, updated_at = ? WHERE id = ?",
1475
+ args: [now, id]
1476
+ });
1477
+ await loadGlobalProcedures();
1478
+ return result.rowsAffected > 0;
1479
+ }
1480
+ var _cache, _cacheLoaded;
1481
+ var init_global_procedures = __esm({
1482
+ "src/lib/global-procedures.ts"() {
1483
+ "use strict";
1484
+ init_database();
1485
+ _cache = "";
1486
+ _cacheLoaded = false;
1487
+ }
1488
+ });
1489
+
1490
+ // src/lib/exe-daemon-client.ts
1491
+ import net from "net";
1492
+ import { spawn } from "child_process";
1493
+ import { randomUUID as randomUUID3 } from "crypto";
1494
+ import { existsSync as existsSync7, unlinkSync, readFileSync as readFileSync5, openSync, closeSync, statSync } from "fs";
1495
+ import path8 from "path";
1496
+ import { fileURLToPath } from "url";
1497
+ function handleData(chunk) {
1498
+ _buffer += chunk.toString();
1499
+ if (_buffer.length > MAX_BUFFER) {
1500
+ _buffer = "";
1501
+ return;
1502
+ }
1503
+ let newlineIdx;
1504
+ while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
1505
+ const line = _buffer.slice(0, newlineIdx).trim();
1506
+ _buffer = _buffer.slice(newlineIdx + 1);
1507
+ if (!line) continue;
1508
+ try {
1509
+ const response = JSON.parse(line);
1510
+ const entry = _pending.get(response.id);
1511
+ if (entry) {
1512
+ clearTimeout(entry.timer);
1513
+ _pending.delete(response.id);
1514
+ entry.resolve(response);
1515
+ }
1516
+ } catch {
1517
+ }
1518
+ }
1519
+ }
1520
+ function cleanupStaleFiles() {
1521
+ if (existsSync7(PID_PATH)) {
1522
+ try {
1523
+ const pid = parseInt(readFileSync5(PID_PATH, "utf8").trim(), 10);
1524
+ if (pid > 0) {
1525
+ try {
1526
+ process.kill(pid, 0);
1527
+ return;
1528
+ } catch {
1529
+ }
1530
+ }
1531
+ } catch {
1532
+ }
1533
+ try {
1534
+ unlinkSync(PID_PATH);
1535
+ } catch {
1536
+ }
1537
+ try {
1538
+ unlinkSync(SOCKET_PATH);
1539
+ } catch {
1540
+ }
1541
+ }
1542
+ }
1543
+ function findPackageRoot() {
1544
+ let dir = path8.dirname(fileURLToPath(import.meta.url));
1545
+ const { root } = path8.parse(dir);
1546
+ while (dir !== root) {
1547
+ if (existsSync7(path8.join(dir, "package.json"))) return dir;
1548
+ dir = path8.dirname(dir);
1549
+ }
1550
+ return null;
1551
+ }
1552
+ function spawnDaemon() {
1553
+ const pkgRoot = findPackageRoot();
1554
+ if (!pkgRoot) {
1555
+ process.stderr.write("[exed-client] WARN: cannot find package root\n");
1556
+ return;
1557
+ }
1558
+ const daemonPath = path8.join(pkgRoot, "dist", "lib", "exe-daemon.js");
1559
+ if (!existsSync7(daemonPath)) {
1560
+ process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
1561
+ `);
1562
+ return;
1563
+ }
1564
+ const resolvedPath = daemonPath;
1565
+ process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
1566
+ `);
1567
+ const logPath = path8.join(path8.dirname(SOCKET_PATH), "exed.log");
1568
+ let stderrFd = "ignore";
1569
+ try {
1570
+ stderrFd = openSync(logPath, "a");
1571
+ } catch {
1572
+ }
1573
+ const child = spawn(process.execPath, [resolvedPath], {
1574
+ detached: true,
1575
+ stdio: ["ignore", "ignore", stderrFd],
1576
+ env: {
1577
+ ...process.env,
1578
+ EXE_DAEMON_SOCK: SOCKET_PATH,
1579
+ EXE_DAEMON_PID: PID_PATH
1580
+ }
1581
+ });
1582
+ child.unref();
1583
+ if (typeof stderrFd === "number") {
1584
+ try {
1585
+ closeSync(stderrFd);
1586
+ } catch {
1587
+ }
1588
+ }
1589
+ }
1590
+ function acquireSpawnLock() {
1591
+ try {
1592
+ const fd = openSync(SPAWN_LOCK_PATH, "wx");
1593
+ closeSync(fd);
1594
+ return true;
1595
+ } catch {
1596
+ try {
1597
+ const stat = statSync(SPAWN_LOCK_PATH);
1598
+ if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
1599
+ try {
1600
+ unlinkSync(SPAWN_LOCK_PATH);
1601
+ } catch {
1602
+ }
1603
+ try {
1604
+ const fd = openSync(SPAWN_LOCK_PATH, "wx");
1605
+ closeSync(fd);
1606
+ return true;
1607
+ } catch {
1608
+ }
1609
+ }
1610
+ } catch {
1611
+ }
1612
+ return false;
1613
+ }
1614
+ }
1615
+ function releaseSpawnLock() {
1616
+ try {
1617
+ unlinkSync(SPAWN_LOCK_PATH);
1618
+ } catch {
1619
+ }
1620
+ }
1621
+ function connectToSocket() {
1622
+ return new Promise((resolve) => {
1623
+ if (_socket && _connected) {
1624
+ resolve(true);
1625
+ return;
1626
+ }
1627
+ const socket = net.createConnection({ path: SOCKET_PATH });
1628
+ const connectTimeout = setTimeout(() => {
1629
+ socket.destroy();
1630
+ resolve(false);
1631
+ }, 2e3);
1632
+ socket.on("connect", () => {
1633
+ clearTimeout(connectTimeout);
1634
+ _socket = socket;
1635
+ _connected = true;
1636
+ _buffer = "";
1637
+ socket.on("data", handleData);
1638
+ socket.on("close", () => {
1639
+ _connected = false;
1640
+ _socket = null;
1641
+ for (const [id, entry] of _pending) {
1642
+ clearTimeout(entry.timer);
1643
+ _pending.delete(id);
1644
+ entry.resolve({ error: "Connection closed" });
1645
+ }
1646
+ });
1647
+ socket.on("error", () => {
1648
+ _connected = false;
1649
+ _socket = null;
1650
+ });
1651
+ resolve(true);
1652
+ });
1653
+ socket.on("error", () => {
1654
+ clearTimeout(connectTimeout);
1655
+ resolve(false);
1656
+ });
1657
+ });
1658
+ }
1659
+ async function connectEmbedDaemon() {
1660
+ if (_socket && _connected) return true;
1661
+ if (await connectToSocket()) return true;
1662
+ if (acquireSpawnLock()) {
1663
+ try {
1664
+ cleanupStaleFiles();
1665
+ spawnDaemon();
1666
+ } finally {
1667
+ releaseSpawnLock();
1592
1668
  }
1593
1669
  }
1594
- try {
1595
- await client.execute(
1596
- `CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)`
1597
- );
1598
- } catch {
1670
+ const start = Date.now();
1671
+ let delay2 = 100;
1672
+ while (Date.now() - start < CONNECT_TIMEOUT_MS) {
1673
+ await new Promise((r) => setTimeout(r, delay2));
1674
+ if (await connectToSocket()) return true;
1675
+ delay2 = Math.min(delay2 * 2, 3e3);
1599
1676
  }
1600
- await client.executeMultiple(`
1601
- CREATE TABLE IF NOT EXISTS identity (
1602
- id INTEGER PRIMARY KEY AUTOINCREMENT,
1603
- agent_id TEXT NOT NULL UNIQUE,
1604
- content_hash TEXT NOT NULL,
1605
- updated_at TEXT NOT NULL,
1606
- updated_by TEXT NOT NULL
1607
- );
1608
-
1609
- CREATE INDEX IF NOT EXISTS idx_identity_agent ON identity(agent_id);
1610
- `);
1611
- await client.executeMultiple(`
1612
- CREATE TABLE IF NOT EXISTS chat_history (
1613
- id INTEGER PRIMARY KEY AUTOINCREMENT,
1614
- session_id TEXT NOT NULL,
1615
- role TEXT NOT NULL,
1616
- content TEXT NOT NULL,
1617
- tool_name TEXT,
1618
- tool_id TEXT,
1619
- is_error INTEGER NOT NULL DEFAULT 0,
1620
- timestamp INTEGER NOT NULL
1621
- );
1622
-
1623
- CREATE INDEX IF NOT EXISTS idx_chat_history_session
1624
- ON chat_history(session_id, id);
1625
- `);
1626
- await client.executeMultiple(`
1627
- CREATE TABLE IF NOT EXISTS workspaces (
1628
- id TEXT PRIMARY KEY,
1629
- slug TEXT NOT NULL UNIQUE,
1630
- name TEXT NOT NULL,
1631
- owner_agent_id TEXT,
1632
- created_at TEXT NOT NULL,
1633
- metadata TEXT
1634
- );
1635
-
1636
- CREATE INDEX IF NOT EXISTS idx_workspaces_slug
1637
- ON workspaces(slug);
1638
- `);
1639
- await client.executeMultiple(`
1640
- CREATE TABLE IF NOT EXISTS documents (
1641
- id TEXT PRIMARY KEY,
1642
- workspace_id TEXT NOT NULL,
1643
- filename TEXT NOT NULL,
1644
- mime TEXT,
1645
- source_type TEXT,
1646
- user_id TEXT,
1647
- uploaded_at TEXT NOT NULL,
1648
- metadata TEXT,
1649
- FOREIGN KEY (workspace_id) REFERENCES workspaces(id)
1650
- );
1651
-
1652
- CREATE INDEX IF NOT EXISTS idx_documents_workspace
1653
- ON documents(workspace_id);
1654
-
1655
- CREATE INDEX IF NOT EXISTS idx_documents_user
1656
- ON documents(user_id);
1657
- `);
1658
- for (const column of [
1659
- "workspace_id TEXT",
1660
- "document_id TEXT",
1661
- "user_id TEXT",
1662
- "char_offset INTEGER",
1663
- "page_number INTEGER"
1664
- ]) {
1677
+ return false;
1678
+ }
1679
+ function sendRequest(texts, priority) {
1680
+ return new Promise((resolve) => {
1681
+ if (!_socket || !_connected) {
1682
+ resolve({ error: "Not connected" });
1683
+ return;
1684
+ }
1685
+ const id = randomUUID3();
1686
+ const timer = setTimeout(() => {
1687
+ _pending.delete(id);
1688
+ resolve({ error: "Request timeout" });
1689
+ }, REQUEST_TIMEOUT_MS);
1690
+ _pending.set(id, { resolve, timer });
1665
1691
  try {
1666
- await client.execute({
1667
- sql: `ALTER TABLE memories ADD COLUMN ${column}`,
1668
- args: []
1669
- });
1692
+ _socket.write(JSON.stringify({ id, texts, priority }) + "\n");
1670
1693
  } catch {
1694
+ clearTimeout(timer);
1695
+ _pending.delete(id);
1696
+ resolve({ error: "Write failed" });
1671
1697
  }
1672
- }
1673
- for (const col of [
1674
- "ALTER TABLE memories ADD COLUMN source_path TEXT",
1675
- "ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'"
1676
- ]) {
1698
+ });
1699
+ }
1700
+ async function pingDaemon() {
1701
+ if (!_socket || !_connected) return null;
1702
+ return new Promise((resolve) => {
1703
+ const id = randomUUID3();
1704
+ const timer = setTimeout(() => {
1705
+ _pending.delete(id);
1706
+ resolve(null);
1707
+ }, 5e3);
1708
+ _pending.set(id, {
1709
+ resolve: (data) => {
1710
+ if (data.health) {
1711
+ resolve(data.health);
1712
+ } else {
1713
+ resolve(null);
1714
+ }
1715
+ },
1716
+ timer
1717
+ });
1677
1718
  try {
1678
- await client.execute(col);
1719
+ _socket.write(JSON.stringify({ id, type: "health" }) + "\n");
1720
+ } catch {
1721
+ clearTimeout(timer);
1722
+ _pending.delete(id);
1723
+ resolve(null);
1724
+ }
1725
+ });
1726
+ }
1727
+ function killAndRespawnDaemon() {
1728
+ process.stderr.write("[exed-client] Killing daemon for restart...\n");
1729
+ if (existsSync7(PID_PATH)) {
1730
+ try {
1731
+ const pid = parseInt(readFileSync5(PID_PATH, "utf8").trim(), 10);
1732
+ if (pid > 0) {
1733
+ try {
1734
+ process.kill(pid, "SIGKILL");
1735
+ } catch {
1736
+ }
1737
+ }
1679
1738
  } catch {
1680
1739
  }
1681
1740
  }
1682
- await client.executeMultiple(`
1683
- CREATE INDEX IF NOT EXISTS idx_memories_workspace
1684
- ON memories(workspace_id);
1685
-
1686
- CREATE INDEX IF NOT EXISTS idx_memories_document
1687
- ON memories(document_id);
1688
-
1689
- CREATE INDEX IF NOT EXISTS idx_memories_user
1690
- ON memories(user_id);
1691
- `);
1692
- await client.executeMultiple(`
1693
- CREATE TABLE IF NOT EXISTS session_kills (
1694
- id TEXT PRIMARY KEY,
1695
- session_name TEXT NOT NULL,
1696
- agent_id TEXT NOT NULL,
1697
- killed_at TIMESTAMP NOT NULL,
1698
- reason TEXT NOT NULL,
1699
- ticks_idle INTEGER,
1700
- estimated_tokens_saved INTEGER
1701
- );
1702
-
1703
- CREATE INDEX IF NOT EXISTS idx_session_kills_killed_at
1704
- ON session_kills(killed_at);
1705
-
1706
- CREATE INDEX IF NOT EXISTS idx_session_kills_agent
1707
- ON session_kills(agent_id);
1708
- `);
1709
- await client.executeMultiple(`
1710
- CREATE TABLE IF NOT EXISTS conversations (
1711
- id TEXT PRIMARY KEY,
1712
- platform TEXT NOT NULL,
1713
- external_id TEXT,
1714
- sender_id TEXT NOT NULL,
1715
- sender_name TEXT,
1716
- sender_phone TEXT,
1717
- sender_email TEXT,
1718
- recipient_id TEXT,
1719
- channel_id TEXT NOT NULL,
1720
- thread_id TEXT,
1721
- reply_to_id TEXT,
1722
- content_text TEXT,
1723
- content_media TEXT,
1724
- content_metadata TEXT,
1725
- agent_response TEXT,
1726
- agent_name TEXT,
1727
- timestamp TEXT NOT NULL,
1728
- ingested_at TEXT NOT NULL
1729
- );
1730
-
1731
- CREATE INDEX IF NOT EXISTS idx_conversations_platform
1732
- ON conversations(platform);
1733
-
1734
- CREATE INDEX IF NOT EXISTS idx_conversations_sender
1735
- ON conversations(sender_id);
1736
-
1737
- CREATE INDEX IF NOT EXISTS idx_conversations_timestamp
1738
- ON conversations(timestamp);
1739
-
1740
- CREATE INDEX IF NOT EXISTS idx_conversations_thread
1741
- ON conversations(thread_id);
1742
-
1743
- CREATE INDEX IF NOT EXISTS idx_conversations_channel
1744
- ON conversations(channel_id);
1745
- `);
1741
+ if (_socket) {
1742
+ _socket.destroy();
1743
+ _socket = null;
1744
+ }
1745
+ _connected = false;
1746
+ _buffer = "";
1746
1747
  try {
1747
- await client.execute({
1748
- sql: `ALTER TABLE tasks ADD COLUMN budget_tokens INTEGER`,
1749
- args: []
1750
- });
1748
+ unlinkSync(PID_PATH);
1751
1749
  } catch {
1752
1750
  }
1753
- try {
1754
- await client.execute({
1755
- sql: `ALTER TABLE tasks ADD COLUMN budget_fallback_model TEXT`,
1756
- args: []
1757
- });
1758
- } catch {
1751
+ try {
1752
+ unlinkSync(SOCKET_PATH);
1753
+ } catch {
1754
+ }
1755
+ spawnDaemon();
1756
+ }
1757
+ async function embedViaClient(text, priority = "high") {
1758
+ if (!_connected && !await connectEmbedDaemon()) return null;
1759
+ _requestCount++;
1760
+ if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
1761
+ const health = await pingDaemon();
1762
+ if (!health) {
1763
+ process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
1764
+ `);
1765
+ killAndRespawnDaemon();
1766
+ const start = Date.now();
1767
+ let delay2 = 200;
1768
+ while (Date.now() - start < CONNECT_TIMEOUT_MS) {
1769
+ await new Promise((r) => setTimeout(r, delay2));
1770
+ if (await connectToSocket()) break;
1771
+ delay2 = Math.min(delay2 * 2, 3e3);
1772
+ }
1773
+ if (!_connected) return null;
1774
+ }
1775
+ }
1776
+ const result = await sendRequest([text], priority);
1777
+ if (!result.error && result.vectors?.[0]) return result.vectors[0];
1778
+ if (result.error) {
1779
+ process.stderr.write(`[exed-client] Embed failed (${result.error}) \u2014 attempting restart
1780
+ `);
1781
+ killAndRespawnDaemon();
1782
+ const start = Date.now();
1783
+ let delay2 = 200;
1784
+ while (Date.now() - start < CONNECT_TIMEOUT_MS) {
1785
+ await new Promise((r) => setTimeout(r, delay2));
1786
+ if (await connectToSocket()) break;
1787
+ delay2 = Math.min(delay2 * 2, 3e3);
1788
+ }
1789
+ if (!_connected) return null;
1790
+ const retry = await sendRequest([text], priority);
1791
+ if (!retry.error && retry.vectors?.[0]) return retry.vectors[0];
1792
+ process.stderr.write(`[exed-client] Embed retry also failed: ${retry.error ?? "no vector"}
1793
+ `);
1794
+ }
1795
+ return null;
1796
+ }
1797
+ function disconnectClient() {
1798
+ if (_socket) {
1799
+ _socket.destroy();
1800
+ _socket = null;
1759
1801
  }
1760
- try {
1761
- await client.execute({
1762
- sql: `ALTER TABLE tasks ADD COLUMN tokens_used INTEGER DEFAULT 0`,
1763
- args: []
1764
- });
1765
- } catch {
1802
+ _connected = false;
1803
+ _buffer = "";
1804
+ for (const [id, entry] of _pending) {
1805
+ clearTimeout(entry.timer);
1806
+ _pending.delete(id);
1807
+ entry.resolve({ error: "Client disconnected" });
1766
1808
  }
1767
- try {
1768
- await client.execute({
1769
- sql: `ALTER TABLE tasks ADD COLUMN tokens_warned_at INTEGER`,
1770
- args: []
1771
- });
1772
- } catch {
1809
+ }
1810
+ var SOCKET_PATH, PID_PATH, SPAWN_LOCK_PATH, SPAWN_LOCK_STALE_MS, CONNECT_TIMEOUT_MS, REQUEST_TIMEOUT_MS, _socket, _connected, _buffer, _requestCount, HEALTH_CHECK_INTERVAL, _pending, MAX_BUFFER;
1811
+ var init_exe_daemon_client = __esm({
1812
+ "src/lib/exe-daemon-client.ts"() {
1813
+ "use strict";
1814
+ init_config();
1815
+ SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path8.join(EXE_AI_DIR, "exed.sock");
1816
+ PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path8.join(EXE_AI_DIR, "exed.pid");
1817
+ SPAWN_LOCK_PATH = path8.join(EXE_AI_DIR, "exed-spawn.lock");
1818
+ SPAWN_LOCK_STALE_MS = 3e4;
1819
+ CONNECT_TIMEOUT_MS = 15e3;
1820
+ REQUEST_TIMEOUT_MS = 3e4;
1821
+ _socket = null;
1822
+ _connected = false;
1823
+ _buffer = "";
1824
+ _requestCount = 0;
1825
+ HEALTH_CHECK_INTERVAL = 100;
1826
+ _pending = /* @__PURE__ */ new Map();
1827
+ MAX_BUFFER = 1e7;
1773
1828
  }
1774
- await client.executeMultiple(`
1775
- CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
1776
- content_text,
1777
- sender_name,
1778
- agent_response,
1779
- content='conversations',
1780
- content_rowid='rowid'
1781
- );
1782
-
1783
- CREATE TRIGGER IF NOT EXISTS conversations_fts_ai AFTER INSERT ON conversations BEGIN
1784
- INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
1785
- VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
1786
- END;
1787
-
1788
- CREATE TRIGGER IF NOT EXISTS conversations_fts_ad AFTER DELETE ON conversations BEGIN
1789
- INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
1790
- VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
1791
- END;
1829
+ });
1792
1830
 
1793
- CREATE TRIGGER IF NOT EXISTS conversations_fts_au AFTER UPDATE ON conversations BEGIN
1794
- INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
1795
- VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
1796
- INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
1797
- VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
1798
- END;
1799
- `);
1800
- try {
1801
- await client.execute({
1802
- sql: `ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3`,
1803
- args: []
1804
- });
1805
- } catch {
1831
+ // src/lib/embedder.ts
1832
+ var embedder_exports = {};
1833
+ __export(embedder_exports, {
1834
+ disposeEmbedder: () => disposeEmbedder,
1835
+ embed: () => embed,
1836
+ embedDirect: () => embedDirect,
1837
+ getEmbedder: () => getEmbedder
1838
+ });
1839
+ async function getEmbedder() {
1840
+ const ok = await connectEmbedDaemon();
1841
+ if (!ok) {
1842
+ throw new Error(
1843
+ "Could not connect to embedding daemon. Ensure the model is installed (run /exe-setup)."
1844
+ );
1806
1845
  }
1807
- try {
1808
- await client.execute(
1809
- `CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)`
1846
+ }
1847
+ async function embed(text) {
1848
+ const priority = process.env.EXE_EMBED_PRIORITY === "low" ? "low" : "high";
1849
+ const vector = await embedViaClient(text, priority);
1850
+ if (!vector) {
1851
+ throw new Error(
1852
+ "Embedding failed: daemon unavailable. Run /exe-setup to verify model installation."
1810
1853
  );
1811
- } catch {
1812
1854
  }
1813
- try {
1814
- await client.execute({
1815
- sql: `UPDATE memories SET tier = 1 WHERE tool_name = 'commit_to_long_term_memory' AND importance >= 8 AND tier = 3`,
1816
- args: []
1817
- });
1818
- await client.execute({
1819
- sql: `UPDATE memories SET tier = 2 WHERE tool_name IN ('store_memory', 'manual') AND importance >= 5 AND tier = 3`,
1820
- args: []
1821
- });
1822
- } catch {
1855
+ if (vector.length !== EMBEDDING_DIM) {
1856
+ throw new Error(
1857
+ `Embedding dimension mismatch: expected ${EMBEDDING_DIM}, got ${vector.length}. Ensure the correct Jina v5-small Q4_K_M GGUF model is installed.`
1858
+ );
1823
1859
  }
1824
- try {
1825
- await client.execute({
1826
- sql: `ALTER TABLE memories ADD COLUMN supersedes_id TEXT`,
1827
- args: []
1828
- });
1829
- } catch {
1860
+ return vector;
1861
+ }
1862
+ async function disposeEmbedder() {
1863
+ disconnectClient();
1864
+ }
1865
+ async function embedDirect(text) {
1866
+ const llamaCpp = await import("node-llama-cpp");
1867
+ const { MODELS_DIR: MODELS_DIR2 } = await Promise.resolve().then(() => (init_config(), config_exports));
1868
+ const { existsSync: existsSync8 } = await import("fs");
1869
+ const path10 = await import("path");
1870
+ const modelPath = path10.join(MODELS_DIR2, "jina-embeddings-v5-small-q4_k_m.gguf");
1871
+ if (!existsSync8(modelPath)) {
1872
+ throw new Error(`Embedding model not found at ${modelPath}. Run '/exe-setup' to download it.`);
1830
1873
  }
1874
+ const llama = await llamaCpp.getLlama();
1875
+ const model = await llama.loadModel({ modelPath });
1876
+ const context = await model.createEmbeddingContext();
1831
1877
  try {
1832
- await client.execute(
1833
- `CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL`
1834
- );
1835
- } catch {
1878
+ const embedding = await context.getEmbeddingFor(text);
1879
+ const vector = Array.from(embedding.vector);
1880
+ if (vector.length !== EMBEDDING_DIM) {
1881
+ throw new Error(
1882
+ `Embedding dimension mismatch: expected ${EMBEDDING_DIM}, got ${vector.length}.`
1883
+ );
1884
+ }
1885
+ return vector;
1886
+ } finally {
1887
+ await context.dispose();
1888
+ await model.dispose();
1836
1889
  }
1837
- for (const col of [
1838
- "ALTER TABLE tasks ADD COLUMN checkpoint TEXT",
1839
- "ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER DEFAULT 0"
1840
- ]) {
1890
+ }
1891
+ var init_embedder = __esm({
1892
+ "src/lib/embedder.ts"() {
1893
+ "use strict";
1894
+ init_memory();
1895
+ init_exe_daemon_client();
1896
+ }
1897
+ });
1898
+
1899
+ // src/adapters/claude/hooks/prompt-ingest-worker.ts
1900
+ import crypto2 from "crypto";
1901
+ import { writeFileSync as writeFileSync2 } from "fs";
1902
+ import path9 from "path";
1903
+
1904
+ // src/lib/project-name.ts
1905
+ import { execSync } from "child_process";
1906
+ import path from "path";
1907
+ var _cached = null;
1908
+ var _cachedCwd = null;
1909
+ function getProjectName(cwd) {
1910
+ const dir = cwd ?? process.cwd();
1911
+ if (_cached && _cachedCwd === dir) return _cached;
1912
+ try {
1913
+ let repoRoot;
1841
1914
  try {
1842
- await client.execute(col);
1915
+ const gitCommonDir = execSync("git rev-parse --path-format=absolute --git-common-dir", {
1916
+ cwd: dir,
1917
+ encoding: "utf8",
1918
+ timeout: 2e3,
1919
+ stdio: ["pipe", "pipe", "pipe"]
1920
+ }).trim();
1921
+ repoRoot = path.dirname(gitCommonDir);
1843
1922
  } catch {
1923
+ repoRoot = execSync("git rev-parse --show-toplevel", {
1924
+ cwd: dir,
1925
+ encoding: "utf8",
1926
+ timeout: 2e3,
1927
+ stdio: ["pipe", "pipe", "pipe"]
1928
+ }).trim();
1844
1929
  }
1930
+ _cached = path.basename(repoRoot);
1931
+ _cachedCwd = dir;
1932
+ return _cached;
1933
+ } catch {
1934
+ _cached = path.basename(dir);
1935
+ _cachedCwd = dir;
1936
+ return _cached;
1845
1937
  }
1846
1938
  }
1847
1939
 
1940
+ // src/lib/store.ts
1941
+ init_memory();
1942
+ init_database();
1943
+
1848
1944
  // src/lib/keychain.ts
1849
1945
  import { readFile, writeFile, unlink, mkdir, chmod } from "fs/promises";
1850
1946
  import { existsSync } from "fs";
@@ -1891,6 +1987,57 @@ async function getMasterKey() {
1891
1987
 
1892
1988
  // src/lib/store.ts
1893
1989
  init_config();
1990
+
1991
+ // src/lib/state-bus.ts
1992
+ var StateBus = class {
1993
+ handlers = /* @__PURE__ */ new Map();
1994
+ globalHandlers = /* @__PURE__ */ new Set();
1995
+ /** Emit an event to all subscribers */
1996
+ emit(event) {
1997
+ const typeHandlers = this.handlers.get(event.type);
1998
+ if (typeHandlers) {
1999
+ for (const handler of typeHandlers) {
2000
+ try {
2001
+ handler(event);
2002
+ } catch {
2003
+ }
2004
+ }
2005
+ }
2006
+ for (const handler of this.globalHandlers) {
2007
+ try {
2008
+ handler(event);
2009
+ } catch {
2010
+ }
2011
+ }
2012
+ }
2013
+ /** Subscribe to a specific event type */
2014
+ on(type, handler) {
2015
+ if (!this.handlers.has(type)) {
2016
+ this.handlers.set(type, /* @__PURE__ */ new Set());
2017
+ }
2018
+ this.handlers.get(type).add(handler);
2019
+ }
2020
+ /** Subscribe to ALL events */
2021
+ onAny(handler) {
2022
+ this.globalHandlers.add(handler);
2023
+ }
2024
+ /** Unsubscribe from a specific event type */
2025
+ off(type, handler) {
2026
+ this.handlers.get(type)?.delete(handler);
2027
+ }
2028
+ /** Unsubscribe from ALL events */
2029
+ offAny(handler) {
2030
+ this.globalHandlers.delete(handler);
2031
+ }
2032
+ /** Remove all listeners */
2033
+ clear() {
2034
+ this.handlers.clear();
2035
+ this.globalHandlers.clear();
2036
+ }
2037
+ };
2038
+ var orgBus = new StateBus();
2039
+
2040
+ // src/lib/store.ts
1894
2041
  var INIT_MAX_RETRIES = 3;
1895
2042
  var INIT_RETRY_DELAY_MS = 1e3;
1896
2043
  function isBusyError2(err) {
@@ -1961,6 +2108,11 @@ async function initStore(options) {
1961
2108
  "version-query"
1962
2109
  );
1963
2110
  _nextVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
2111
+ try {
2112
+ const { loadGlobalProcedures: loadGlobalProcedures2 } = await Promise.resolve().then(() => (init_global_procedures(), global_procedures_exports));
2113
+ await loadGlobalProcedures2();
2114
+ } catch {
2115
+ }
1964
2116
  }
1965
2117
  function classifyTier(record) {
1966
2118
  if (record.tool_name === "commit_to_long_term_memory" && (record.importance ?? 0) >= 8) return 1;
@@ -2002,6 +2154,12 @@ async function writeMemory(record) {
2002
2154
  supersedes_id: record.supersedes_id ?? null
2003
2155
  };
2004
2156
  _pendingRecords.push(dbRow);
2157
+ orgBus.emit({
2158
+ type: "memory_stored",
2159
+ agentId: record.agent_id,
2160
+ project: record.project_name,
2161
+ timestamp: record.timestamp
2162
+ });
2005
2163
  const MAX_PENDING = 1e3;
2006
2164
  if (_pendingRecords.length > MAX_PENDING) {
2007
2165
  const dropped = _pendingRecords.length - MAX_PENDING;
@@ -2157,6 +2315,7 @@ function vectorToBlob(vector) {
2157
2315
  }
2158
2316
 
2159
2317
  // src/lib/plan-limits.ts
2318
+ init_database();
2160
2319
  import { readFileSync as readFileSync4, existsSync as existsSync6 } from "fs";
2161
2320
  import path7 from "path";
2162
2321
 
@@ -2171,7 +2330,7 @@ var EMPLOYEES_PATH = path5.join(EXE_AI_DIR, "exe-employees.json");
2171
2330
  // src/lib/license.ts
2172
2331
  init_config();
2173
2332
  import { readFileSync as readFileSync3, writeFileSync, existsSync as existsSync5, mkdirSync as mkdirSync2 } from "fs";
2174
- import { randomUUID } from "crypto";
2333
+ import { randomUUID as randomUUID2 } from "crypto";
2175
2334
  import path6 from "path";
2176
2335
  import { jwtVerify, importSPKI } from "jose";
2177
2336
  var LICENSE_PATH = path6.join(EXE_AI_DIR, "license.key");
@@ -2224,7 +2383,7 @@ function loadDeviceId() {
2224
2383
  }
2225
2384
  } catch {
2226
2385
  }
2227
- const id = randomUUID();
2386
+ const id = randomUUID2();
2228
2387
  mkdirSync2(EXE_AI_DIR, { recursive: true });
2229
2388
  writeFileSync(DEVICE_ID_PATH, id, "utf8");
2230
2389
  return id;
@@ -2237,6 +2396,10 @@ function loadLicense() {
2237
2396
  return null;
2238
2397
  }
2239
2398
  }
2399
+ function saveLicense(apiKey) {
2400
+ mkdirSync2(EXE_AI_DIR, { recursive: true });
2401
+ writeFileSync(LICENSE_PATH, apiKey.trim(), { encoding: "utf8", mode: 384 });
2402
+ }
2240
2403
  async function verifyLicenseJwt(token) {
2241
2404
  try {
2242
2405
  const key = await importSPKI(LICENSE_PUBLIC_KEY_PEM, LICENSE_JWT_ALG);
@@ -2327,7 +2490,21 @@ function getCacheAgeMs() {
2327
2490
  }
2328
2491
  }
2329
2492
  async function checkLicense() {
2330
- const key = loadLicense();
2493
+ let key = loadLicense();
2494
+ if (!key) {
2495
+ try {
2496
+ const configPath = path6.join(EXE_AI_DIR, "config.json");
2497
+ if (existsSync5(configPath)) {
2498
+ const raw = JSON.parse(readFileSync3(configPath, "utf8"));
2499
+ const cloud = raw.cloud;
2500
+ if (cloud?.apiKey) {
2501
+ key = cloud.apiKey;
2502
+ saveLicense(key);
2503
+ }
2504
+ }
2505
+ } catch {
2506
+ }
2507
+ }
2331
2508
  if (!key) return FREE_LICENSE;
2332
2509
  const cached = await getCachedLicense();
2333
2510
  if (cached && getCacheAgeMs() < CACHE_MAX_AGE_MS) return cached;