@askexenow/exe-os 0.8.1 → 0.8.2

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 (106) hide show
  1. package/package.json +1 -1
  2. package/dist/bin/backfill-responses.js +0 -2064
  3. package/dist/bin/backfill-vectors.js +0 -1771
  4. package/dist/bin/cleanup-stale-review-tasks.js +0 -1468
  5. package/dist/bin/cli.js +0 -21371
  6. package/dist/bin/exe-agent.js +0 -2016
  7. package/dist/bin/exe-assign.js +0 -2176
  8. package/dist/bin/exe-boot.js +0 -6332
  9. package/dist/bin/exe-call.js +0 -341
  10. package/dist/bin/exe-cloud.js +0 -861
  11. package/dist/bin/exe-dispatch.js +0 -1159
  12. package/dist/bin/exe-doctor.js +0 -1786
  13. package/dist/bin/exe-export-behaviors.js +0 -1637
  14. package/dist/bin/exe-forget.js +0 -1784
  15. package/dist/bin/exe-gateway.js +0 -8140
  16. package/dist/bin/exe-healthcheck.js +0 -207
  17. package/dist/bin/exe-heartbeat.js +0 -1778
  18. package/dist/bin/exe-kill.js +0 -1622
  19. package/dist/bin/exe-launch-agent.js +0 -1847
  20. package/dist/bin/exe-link.js +0 -192
  21. package/dist/bin/exe-new-employee.js +0 -1384
  22. package/dist/bin/exe-pending-messages.js +0 -1576
  23. package/dist/bin/exe-pending-notifications.js +0 -1450
  24. package/dist/bin/exe-pending-reviews.js +0 -1598
  25. package/dist/bin/exe-repo-drift.js +0 -95
  26. package/dist/bin/exe-review.js +0 -1742
  27. package/dist/bin/exe-search.js +0 -3116
  28. package/dist/bin/exe-session-cleanup.js +0 -3360
  29. package/dist/bin/exe-settings.js +0 -365
  30. package/dist/bin/exe-status.js +0 -1661
  31. package/dist/bin/exe-team.js +0 -1453
  32. package/dist/bin/git-sweep.js +0 -2441
  33. package/dist/bin/graph-backfill.js +0 -2111
  34. package/dist/bin/graph-export.js +0 -1747
  35. package/dist/bin/install.js +0 -661
  36. package/dist/bin/list-providers.js +0 -140
  37. package/dist/bin/scan-tasks.js +0 -2039
  38. package/dist/bin/setup.js +0 -2717
  39. package/dist/bin/shard-migrate.js +0 -1637
  40. package/dist/bin/update.js +0 -98
  41. package/dist/bin/wiki-sync.js +0 -1657
  42. package/dist/gateway/index.js +0 -9256
  43. package/dist/hooks/bug-report-worker.js +0 -2903
  44. package/dist/hooks/commit-complete.js +0 -2364
  45. package/dist/hooks/error-recall.js +0 -3327
  46. package/dist/hooks/exe-heartbeat-hook.js +0 -237
  47. package/dist/hooks/ingest-worker.js +0 -5065
  48. package/dist/hooks/ingest.js +0 -695
  49. package/dist/hooks/instructions-loaded.js +0 -2069
  50. package/dist/hooks/notification.js +0 -1915
  51. package/dist/hooks/post-compact.js +0 -1940
  52. package/dist/hooks/pre-compact.js +0 -1935
  53. package/dist/hooks/pre-tool-use.js +0 -2380
  54. package/dist/hooks/prompt-ingest-worker.js +0 -2291
  55. package/dist/hooks/prompt-submit.js +0 -5189
  56. package/dist/hooks/response-ingest-worker.js +0 -2431
  57. package/dist/hooks/session-end.js +0 -2196
  58. package/dist/hooks/session-start.js +0 -3259
  59. package/dist/hooks/stop.js +0 -2025
  60. package/dist/hooks/subagent-stop.js +0 -1915
  61. package/dist/hooks/summary-worker.js +0 -2931
  62. package/dist/index.js +0 -12327
  63. package/dist/lib/cloud-sync.js +0 -500
  64. package/dist/lib/config.js +0 -235
  65. package/dist/lib/consolidation.js +0 -481
  66. package/dist/lib/crypto.js +0 -51
  67. package/dist/lib/database.js +0 -834
  68. package/dist/lib/device-registry.js +0 -1009
  69. package/dist/lib/embedder.js +0 -645
  70. package/dist/lib/employee-templates.js +0 -570
  71. package/dist/lib/employees.js +0 -182
  72. package/dist/lib/error-detector.js +0 -156
  73. package/dist/lib/exe-daemon-client.js +0 -456
  74. package/dist/lib/exe-daemon.js +0 -8699
  75. package/dist/lib/file-grep.js +0 -215
  76. package/dist/lib/hybrid-search.js +0 -3064
  77. package/dist/lib/identity-templates.js +0 -441
  78. package/dist/lib/identity.js +0 -228
  79. package/dist/lib/keychain.js +0 -145
  80. package/dist/lib/license.js +0 -382
  81. package/dist/lib/messaging.js +0 -1389
  82. package/dist/lib/reminders.js +0 -63
  83. package/dist/lib/schedules.js +0 -1525
  84. package/dist/lib/session-registry.js +0 -52
  85. package/dist/lib/skill-learning.js +0 -488
  86. package/dist/lib/status-brief.js +0 -240
  87. package/dist/lib/store.js +0 -1740
  88. package/dist/lib/task-router.js +0 -128
  89. package/dist/lib/tasks.js +0 -2585
  90. package/dist/lib/tmux-routing.js +0 -2969
  91. package/dist/lib/tmux-status.js +0 -261
  92. package/dist/lib/tmux-transport.js +0 -83
  93. package/dist/lib/transport.js +0 -128
  94. package/dist/lib/ws-auth.js +0 -19
  95. package/dist/lib/ws-client.js +0 -160
  96. package/dist/mcp/server.js +0 -10812
  97. package/dist/mcp/tools/complete-reminder.js +0 -67
  98. package/dist/mcp/tools/create-reminder.js +0 -52
  99. package/dist/mcp/tools/create-task.js +0 -1903
  100. package/dist/mcp/tools/deactivate-behavior.js +0 -268
  101. package/dist/mcp/tools/list-reminders.js +0 -62
  102. package/dist/mcp/tools/list-tasks.js +0 -491
  103. package/dist/mcp/tools/send-message.js +0 -1395
  104. package/dist/mcp/tools/update-task.js +0 -1807
  105. package/dist/runtime/index.js +0 -7201
  106. package/dist/tui/App.js +0 -17874
@@ -1,3116 +0,0 @@
1
- #!/usr/bin/env node
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
5
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
6
- }) : x)(function(x) {
7
- if (typeof require !== "undefined") return require.apply(this, arguments);
8
- throw Error('Dynamic require of "' + x + '" is not supported');
9
- });
10
- var __esm = (fn, res) => function __init() {
11
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
12
- };
13
- var __export = (target, all) => {
14
- for (var name in all)
15
- __defProp(target, name, { get: all[name], enumerable: true });
16
- };
17
-
18
- // src/types/memory.ts
19
- var EMBEDDING_DIM;
20
- var init_memory = __esm({
21
- "src/types/memory.ts"() {
22
- "use strict";
23
- EMBEDDING_DIM = 1024;
24
- }
25
- });
26
-
27
- // src/lib/database.ts
28
- import { createClient } from "@libsql/client";
29
- async function initDatabase(config) {
30
- if (_client) {
31
- _client.close();
32
- _client = null;
33
- }
34
- const opts = {
35
- url: `file:${config.dbPath}`
36
- };
37
- if (config.encryptionKey) {
38
- opts.encryptionKey = config.encryptionKey;
39
- }
40
- _client = createClient(opts);
41
- }
42
- function getClient() {
43
- if (!_client) {
44
- throw new Error("Database client not initialized. Call initDatabase() first.");
45
- }
46
- return _client;
47
- }
48
- async function ensureSchema() {
49
- const client = getClient();
50
- await client.execute("PRAGMA journal_mode = WAL");
51
- await client.execute("PRAGMA busy_timeout = 5000");
52
- try {
53
- await client.execute("PRAGMA libsql_vector_search_ef = 128");
54
- } catch {
55
- }
56
- await client.executeMultiple(`
57
- CREATE TABLE IF NOT EXISTS memories (
58
- id TEXT PRIMARY KEY,
59
- agent_id TEXT NOT NULL,
60
- agent_role TEXT NOT NULL,
61
- session_id TEXT NOT NULL,
62
- timestamp TEXT NOT NULL,
63
- tool_name TEXT NOT NULL,
64
- project_name TEXT NOT NULL,
65
- has_error INTEGER NOT NULL DEFAULT 0,
66
- raw_text TEXT NOT NULL,
67
- vector F32_BLOB(1024),
68
- version INTEGER NOT NULL DEFAULT 0
69
- );
70
-
71
- CREATE INDEX IF NOT EXISTS idx_memories_agent
72
- ON memories(agent_id);
73
-
74
- CREATE INDEX IF NOT EXISTS idx_memories_timestamp
75
- ON memories(timestamp);
76
-
77
- CREATE INDEX IF NOT EXISTS idx_memories_session
78
- ON memories(session_id);
79
-
80
- CREATE INDEX IF NOT EXISTS idx_memories_project
81
- ON memories(project_name);
82
-
83
- CREATE INDEX IF NOT EXISTS idx_memories_tool
84
- ON memories(tool_name);
85
-
86
- CREATE INDEX IF NOT EXISTS idx_memories_version
87
- ON memories(version);
88
-
89
- CREATE INDEX IF NOT EXISTS idx_memories_agent_project
90
- ON memories(agent_id, project_name);
91
- `);
92
- await client.executeMultiple(`
93
- CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
94
- raw_text,
95
- content='memories',
96
- content_rowid='rowid'
97
- );
98
-
99
- CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
100
- INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
101
- END;
102
-
103
- CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
104
- INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
105
- END;
106
-
107
- CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
108
- INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
109
- INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
110
- END;
111
- `);
112
- await client.executeMultiple(`
113
- CREATE TABLE IF NOT EXISTS sync_meta (
114
- key TEXT PRIMARY KEY,
115
- value TEXT NOT NULL
116
- );
117
- `);
118
- await client.executeMultiple(`
119
- CREATE TABLE IF NOT EXISTS tasks (
120
- id TEXT PRIMARY KEY,
121
- title TEXT NOT NULL,
122
- assigned_to TEXT NOT NULL,
123
- assigned_by TEXT NOT NULL,
124
- project_name TEXT NOT NULL,
125
- priority TEXT NOT NULL DEFAULT 'p1',
126
- status TEXT NOT NULL DEFAULT 'open',
127
- task_file TEXT,
128
- created_at TEXT NOT NULL,
129
- updated_at TEXT NOT NULL
130
- );
131
-
132
- CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
133
- ON tasks(assigned_to, status);
134
- `);
135
- await client.executeMultiple(`
136
- CREATE TABLE IF NOT EXISTS behaviors (
137
- id TEXT PRIMARY KEY,
138
- agent_id TEXT NOT NULL,
139
- project_name TEXT,
140
- domain TEXT,
141
- content TEXT NOT NULL,
142
- active INTEGER NOT NULL DEFAULT 1,
143
- created_at TEXT NOT NULL,
144
- updated_at TEXT NOT NULL
145
- );
146
-
147
- CREATE INDEX IF NOT EXISTS idx_behaviors_agent
148
- ON behaviors(agent_id, active);
149
- `);
150
- try {
151
- const existing = await client.execute({
152
- sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
153
- args: []
154
- });
155
- if (Number(existing.rows[0]?.cnt) === 0) {
156
- await client.executeMultiple(`
157
- INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
158
- VALUES
159
- (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');
160
- INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
161
- VALUES
162
- (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');
163
- INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
164
- VALUES
165
- (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');
166
- `);
167
- }
168
- } catch {
169
- }
170
- try {
171
- await client.execute({
172
- sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
173
- args: []
174
- });
175
- } catch {
176
- }
177
- try {
178
- await client.execute({
179
- sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
180
- args: []
181
- });
182
- } catch {
183
- }
184
- try {
185
- await client.execute({
186
- sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
187
- args: []
188
- });
189
- } catch {
190
- }
191
- try {
192
- await client.execute({
193
- sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
194
- ON tasks(parent_task_id)
195
- WHERE parent_task_id IS NOT NULL`,
196
- args: []
197
- });
198
- } catch {
199
- }
200
- try {
201
- await client.execute({
202
- sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
203
- args: []
204
- });
205
- } catch {
206
- }
207
- try {
208
- await client.execute({
209
- sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
210
- args: []
211
- });
212
- } catch {
213
- }
214
- try {
215
- await client.execute({
216
- sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
217
- args: []
218
- });
219
- } catch {
220
- }
221
- try {
222
- await client.execute({
223
- sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
224
- args: []
225
- });
226
- } catch {
227
- }
228
- try {
229
- await client.execute({
230
- sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
231
- args: []
232
- });
233
- } catch {
234
- }
235
- try {
236
- await client.execute({
237
- sql: `ALTER TABLE tasks ADD COLUMN checkpoint TEXT`,
238
- args: []
239
- });
240
- } catch {
241
- }
242
- try {
243
- await client.execute({
244
- sql: `ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER NOT NULL DEFAULT 0`,
245
- args: []
246
- });
247
- } catch {
248
- }
249
- try {
250
- await client.execute({
251
- sql: `ALTER TABLE tasks ADD COLUMN complexity TEXT NOT NULL DEFAULT 'standard'`,
252
- args: []
253
- });
254
- } catch {
255
- }
256
- try {
257
- await client.execute({
258
- sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
259
- args: []
260
- });
261
- } catch {
262
- }
263
- try {
264
- await client.execute({
265
- sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
266
- args: []
267
- });
268
- } catch {
269
- }
270
- try {
271
- await client.execute({
272
- sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
273
- args: []
274
- });
275
- } catch {
276
- }
277
- try {
278
- await client.execute({
279
- sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
280
- args: []
281
- });
282
- } catch {
283
- }
284
- await client.executeMultiple(`
285
- CREATE TABLE IF NOT EXISTS consolidations (
286
- id TEXT PRIMARY KEY,
287
- consolidated_memory_id TEXT NOT NULL,
288
- source_memory_id TEXT NOT NULL,
289
- created_at TEXT NOT NULL
290
- );
291
-
292
- CREATE INDEX IF NOT EXISTS idx_consolidations_source
293
- ON consolidations(source_memory_id);
294
-
295
- CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
296
- ON consolidations(consolidated_memory_id);
297
- `);
298
- await client.executeMultiple(`
299
- CREATE TABLE IF NOT EXISTS reminders (
300
- id TEXT PRIMARY KEY,
301
- text TEXT NOT NULL,
302
- created_at TEXT NOT NULL,
303
- due_date TEXT,
304
- completed_at TEXT
305
- );
306
- `);
307
- await client.executeMultiple(`
308
- CREATE TABLE IF NOT EXISTS notifications (
309
- id TEXT PRIMARY KEY,
310
- agent_id TEXT NOT NULL,
311
- agent_role TEXT NOT NULL,
312
- event TEXT NOT NULL,
313
- project TEXT NOT NULL,
314
- summary TEXT NOT NULL,
315
- task_file TEXT,
316
- read INTEGER NOT NULL DEFAULT 0,
317
- created_at TEXT NOT NULL
318
- );
319
-
320
- CREATE INDEX IF NOT EXISTS idx_notifications_read
321
- ON notifications(read);
322
-
323
- CREATE INDEX IF NOT EXISTS idx_notifications_agent
324
- ON notifications(agent_id);
325
-
326
- CREATE INDEX IF NOT EXISTS idx_notifications_task_file
327
- ON notifications(task_file);
328
- `);
329
- await client.executeMultiple(`
330
- CREATE TABLE IF NOT EXISTS schedules (
331
- id TEXT PRIMARY KEY,
332
- cron TEXT NOT NULL,
333
- description TEXT NOT NULL,
334
- job_type TEXT NOT NULL DEFAULT 'report',
335
- prompt TEXT,
336
- assigned_to TEXT,
337
- project_name TEXT,
338
- active INTEGER NOT NULL DEFAULT 1,
339
- use_crontab INTEGER NOT NULL DEFAULT 0,
340
- created_at TEXT NOT NULL
341
- );
342
- `);
343
- await client.executeMultiple(`
344
- CREATE TABLE IF NOT EXISTS device_registry (
345
- device_id TEXT PRIMARY KEY,
346
- friendly_name TEXT NOT NULL,
347
- hostname TEXT NOT NULL,
348
- projects TEXT NOT NULL DEFAULT '[]',
349
- agents TEXT NOT NULL DEFAULT '[]',
350
- connected INTEGER DEFAULT 0,
351
- last_seen TEXT NOT NULL
352
- );
353
- `);
354
- await client.executeMultiple(`
355
- CREATE TABLE IF NOT EXISTS messages (
356
- id TEXT PRIMARY KEY,
357
- from_agent TEXT NOT NULL,
358
- from_device TEXT NOT NULL DEFAULT 'local',
359
- target_agent TEXT NOT NULL,
360
- target_project TEXT,
361
- target_device TEXT NOT NULL DEFAULT 'local',
362
- content TEXT NOT NULL,
363
- priority TEXT DEFAULT 'normal',
364
- status TEXT DEFAULT 'pending',
365
- server_seq INTEGER,
366
- retry_count INTEGER DEFAULT 0,
367
- created_at TEXT NOT NULL,
368
- delivered_at TEXT,
369
- processed_at TEXT,
370
- failed_at TEXT,
371
- failure_reason TEXT
372
- );
373
-
374
- CREATE INDEX IF NOT EXISTS idx_messages_target
375
- ON messages(target_agent, status);
376
-
377
- CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
378
- ON messages(target_agent, from_agent, server_seq);
379
- `);
380
- try {
381
- await client.execute({
382
- sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
383
- args: []
384
- });
385
- await client.execute({
386
- sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
387
- args: []
388
- });
389
- await client.execute({
390
- sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
391
- args: []
392
- });
393
- await client.execute({
394
- sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
395
- args: []
396
- });
397
- } catch {
398
- }
399
- await client.executeMultiple(`
400
- CREATE TABLE IF NOT EXISTS trajectories (
401
- id TEXT PRIMARY KEY,
402
- task_id TEXT NOT NULL,
403
- agent_id TEXT NOT NULL,
404
- project_name TEXT NOT NULL,
405
- task_title TEXT NOT NULL,
406
- signature TEXT NOT NULL,
407
- signature_hash TEXT NOT NULL,
408
- tool_count INTEGER NOT NULL,
409
- skill_id TEXT,
410
- created_at TEXT NOT NULL
411
- );
412
-
413
- CREATE INDEX IF NOT EXISTS idx_trajectories_hash
414
- ON trajectories(signature_hash);
415
-
416
- CREATE INDEX IF NOT EXISTS idx_trajectories_agent
417
- ON trajectories(agent_id);
418
- `);
419
- try {
420
- await client.execute("ALTER TABLE trajectories ADD COLUMN skill_id TEXT");
421
- } catch {
422
- }
423
- await client.executeMultiple(`
424
- CREATE TABLE IF NOT EXISTS consolidations (
425
- id TEXT PRIMARY KEY,
426
- consolidated_memory_id TEXT NOT NULL,
427
- source_memory_id TEXT NOT NULL,
428
- created_at TEXT NOT NULL
429
- );
430
-
431
- CREATE INDEX IF NOT EXISTS idx_consolidations_source
432
- ON consolidations(source_memory_id);
433
- `);
434
- await client.executeMultiple(`
435
- CREATE TABLE IF NOT EXISTS audit_trail (
436
- id INTEGER PRIMARY KEY AUTOINCREMENT,
437
- timestamp TEXT NOT NULL,
438
- session_id TEXT NOT NULL,
439
- agent_id TEXT NOT NULL,
440
- tool TEXT NOT NULL,
441
- input TEXT,
442
- decision TEXT NOT NULL,
443
- reason TEXT,
444
- is_customer_facing INTEGER NOT NULL DEFAULT 0
445
- );
446
-
447
- CREATE INDEX IF NOT EXISTS idx_audit_trail_agent
448
- ON audit_trail(agent_id, timestamp);
449
-
450
- CREATE INDEX IF NOT EXISTS idx_audit_trail_session
451
- ON audit_trail(session_id);
452
- `);
453
- try {
454
- await client.execute({
455
- sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
456
- args: []
457
- });
458
- } catch {
459
- }
460
- try {
461
- await client.execute({
462
- sql: `ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5`,
463
- args: []
464
- });
465
- } catch {
466
- }
467
- try {
468
- await client.execute({
469
- sql: `ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'`,
470
- args: []
471
- });
472
- } catch {
473
- }
474
- try {
475
- await client.execute({
476
- sql: `ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7`,
477
- args: []
478
- });
479
- } catch {
480
- }
481
- try {
482
- await client.execute({
483
- sql: `ALTER TABLE memories ADD COLUMN last_accessed TEXT`,
484
- args: []
485
- });
486
- } catch {
487
- }
488
- try {
489
- await client.execute({
490
- sql: `UPDATE memories SET last_accessed = timestamp WHERE last_accessed IS NULL`,
491
- args: []
492
- });
493
- } catch {
494
- }
495
- try {
496
- await client.execute({
497
- sql: `ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0`,
498
- args: []
499
- });
500
- } catch {
501
- }
502
- try {
503
- await client.execute({
504
- sql: `ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0`,
505
- args: []
506
- });
507
- } catch {
508
- }
509
- for (const col of [
510
- "ALTER TABLE memories ADD COLUMN content_hash TEXT",
511
- "ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT"
512
- ]) {
513
- try {
514
- await client.execute(col);
515
- } catch {
516
- }
517
- }
518
- await client.executeMultiple(`
519
- CREATE TABLE IF NOT EXISTS entities (
520
- id TEXT PRIMARY KEY,
521
- name TEXT NOT NULL,
522
- type TEXT NOT NULL,
523
- first_seen TEXT NOT NULL,
524
- last_seen TEXT NOT NULL,
525
- properties TEXT DEFAULT '{}',
526
- UNIQUE(name, type)
527
- );
528
-
529
- CREATE TABLE IF NOT EXISTS relationships (
530
- id TEXT PRIMARY KEY,
531
- source_entity_id TEXT NOT NULL,
532
- target_entity_id TEXT NOT NULL,
533
- type TEXT NOT NULL,
534
- weight REAL DEFAULT 1.0,
535
- timestamp TEXT NOT NULL,
536
- properties TEXT DEFAULT '{}',
537
- UNIQUE(source_entity_id, target_entity_id, type)
538
- );
539
-
540
- CREATE TABLE IF NOT EXISTS entity_memories (
541
- entity_id TEXT NOT NULL,
542
- memory_id TEXT NOT NULL,
543
- PRIMARY KEY (entity_id, memory_id)
544
- );
545
-
546
- CREATE TABLE IF NOT EXISTS relationship_memories (
547
- relationship_id TEXT NOT NULL,
548
- memory_id TEXT NOT NULL,
549
- PRIMARY KEY (relationship_id, memory_id)
550
- );
551
-
552
- CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
553
- CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
554
- CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
555
- CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
556
-
557
- CREATE TABLE IF NOT EXISTS hyperedges (
558
- id TEXT PRIMARY KEY,
559
- label TEXT NOT NULL,
560
- relation TEXT NOT NULL,
561
- confidence REAL DEFAULT 1.0,
562
- timestamp TEXT NOT NULL
563
- );
564
-
565
- CREATE TABLE IF NOT EXISTS hyperedge_nodes (
566
- hyperedge_id TEXT NOT NULL,
567
- entity_id TEXT NOT NULL,
568
- PRIMARY KEY (hyperedge_id, entity_id)
569
- );
570
- `);
571
- await client.executeMultiple(`
572
- CREATE TABLE IF NOT EXISTS entity_aliases (
573
- alias TEXT NOT NULL PRIMARY KEY,
574
- canonical_entity_id TEXT NOT NULL
575
- );
576
- CREATE INDEX IF NOT EXISTS idx_entity_aliases_canonical ON entity_aliases(canonical_entity_id);
577
- `);
578
- for (const col of [
579
- "ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
580
- "ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
581
- ]) {
582
- try {
583
- await client.execute(col);
584
- } catch {
585
- }
586
- }
587
- try {
588
- await client.execute(
589
- `CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)`
590
- );
591
- } catch {
592
- }
593
- await client.executeMultiple(`
594
- CREATE TABLE IF NOT EXISTS identity (
595
- id INTEGER PRIMARY KEY AUTOINCREMENT,
596
- agent_id TEXT NOT NULL UNIQUE,
597
- content_hash TEXT NOT NULL,
598
- updated_at TEXT NOT NULL,
599
- updated_by TEXT NOT NULL
600
- );
601
-
602
- CREATE INDEX IF NOT EXISTS idx_identity_agent ON identity(agent_id);
603
- `);
604
- await client.executeMultiple(`
605
- CREATE TABLE IF NOT EXISTS chat_history (
606
- id INTEGER PRIMARY KEY AUTOINCREMENT,
607
- session_id TEXT NOT NULL,
608
- role TEXT NOT NULL,
609
- content TEXT NOT NULL,
610
- tool_name TEXT,
611
- tool_id TEXT,
612
- is_error INTEGER NOT NULL DEFAULT 0,
613
- timestamp INTEGER NOT NULL
614
- );
615
-
616
- CREATE INDEX IF NOT EXISTS idx_chat_history_session
617
- ON chat_history(session_id, id);
618
- `);
619
- await client.executeMultiple(`
620
- CREATE TABLE IF NOT EXISTS workspaces (
621
- id TEXT PRIMARY KEY,
622
- slug TEXT NOT NULL UNIQUE,
623
- name TEXT NOT NULL,
624
- owner_agent_id TEXT,
625
- created_at TEXT NOT NULL,
626
- metadata TEXT
627
- );
628
-
629
- CREATE INDEX IF NOT EXISTS idx_workspaces_slug
630
- ON workspaces(slug);
631
- `);
632
- await client.executeMultiple(`
633
- CREATE TABLE IF NOT EXISTS documents (
634
- id TEXT PRIMARY KEY,
635
- workspace_id TEXT NOT NULL,
636
- filename TEXT NOT NULL,
637
- mime TEXT,
638
- source_type TEXT,
639
- user_id TEXT,
640
- uploaded_at TEXT NOT NULL,
641
- metadata TEXT,
642
- FOREIGN KEY (workspace_id) REFERENCES workspaces(id)
643
- );
644
-
645
- CREATE INDEX IF NOT EXISTS idx_documents_workspace
646
- ON documents(workspace_id);
647
-
648
- CREATE INDEX IF NOT EXISTS idx_documents_user
649
- ON documents(user_id);
650
- `);
651
- for (const column of [
652
- "workspace_id TEXT",
653
- "document_id TEXT",
654
- "user_id TEXT",
655
- "char_offset INTEGER",
656
- "page_number INTEGER"
657
- ]) {
658
- try {
659
- await client.execute({
660
- sql: `ALTER TABLE memories ADD COLUMN ${column}`,
661
- args: []
662
- });
663
- } catch {
664
- }
665
- }
666
- for (const col of [
667
- "ALTER TABLE memories ADD COLUMN source_path TEXT",
668
- "ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'"
669
- ]) {
670
- try {
671
- await client.execute(col);
672
- } catch {
673
- }
674
- }
675
- await client.executeMultiple(`
676
- CREATE INDEX IF NOT EXISTS idx_memories_workspace
677
- ON memories(workspace_id);
678
-
679
- CREATE INDEX IF NOT EXISTS idx_memories_document
680
- ON memories(document_id);
681
-
682
- CREATE INDEX IF NOT EXISTS idx_memories_user
683
- ON memories(user_id);
684
- `);
685
- await client.executeMultiple(`
686
- CREATE TABLE IF NOT EXISTS session_kills (
687
- id TEXT PRIMARY KEY,
688
- session_name TEXT NOT NULL,
689
- agent_id TEXT NOT NULL,
690
- killed_at TIMESTAMP NOT NULL,
691
- reason TEXT NOT NULL,
692
- ticks_idle INTEGER,
693
- estimated_tokens_saved INTEGER
694
- );
695
-
696
- CREATE INDEX IF NOT EXISTS idx_session_kills_killed_at
697
- ON session_kills(killed_at);
698
-
699
- CREATE INDEX IF NOT EXISTS idx_session_kills_agent
700
- ON session_kills(agent_id);
701
- `);
702
- await client.executeMultiple(`
703
- CREATE TABLE IF NOT EXISTS conversations (
704
- id TEXT PRIMARY KEY,
705
- platform TEXT NOT NULL,
706
- external_id TEXT,
707
- sender_id TEXT NOT NULL,
708
- sender_name TEXT,
709
- sender_phone TEXT,
710
- sender_email TEXT,
711
- recipient_id TEXT,
712
- channel_id TEXT NOT NULL,
713
- thread_id TEXT,
714
- reply_to_id TEXT,
715
- content_text TEXT,
716
- content_media TEXT,
717
- content_metadata TEXT,
718
- agent_response TEXT,
719
- agent_name TEXT,
720
- timestamp TEXT NOT NULL,
721
- ingested_at TEXT NOT NULL
722
- );
723
-
724
- CREATE INDEX IF NOT EXISTS idx_conversations_platform
725
- ON conversations(platform);
726
-
727
- CREATE INDEX IF NOT EXISTS idx_conversations_sender
728
- ON conversations(sender_id);
729
-
730
- CREATE INDEX IF NOT EXISTS idx_conversations_timestamp
731
- ON conversations(timestamp);
732
-
733
- CREATE INDEX IF NOT EXISTS idx_conversations_thread
734
- ON conversations(thread_id);
735
-
736
- CREATE INDEX IF NOT EXISTS idx_conversations_channel
737
- ON conversations(channel_id);
738
- `);
739
- try {
740
- await client.execute({
741
- sql: `ALTER TABLE tasks ADD COLUMN budget_tokens INTEGER`,
742
- args: []
743
- });
744
- } catch {
745
- }
746
- try {
747
- await client.execute({
748
- sql: `ALTER TABLE tasks ADD COLUMN budget_fallback_model TEXT`,
749
- args: []
750
- });
751
- } catch {
752
- }
753
- try {
754
- await client.execute({
755
- sql: `ALTER TABLE tasks ADD COLUMN tokens_used INTEGER DEFAULT 0`,
756
- args: []
757
- });
758
- } catch {
759
- }
760
- try {
761
- await client.execute({
762
- sql: `ALTER TABLE tasks ADD COLUMN tokens_warned_at INTEGER`,
763
- args: []
764
- });
765
- } catch {
766
- }
767
- await client.executeMultiple(`
768
- CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
769
- content_text,
770
- sender_name,
771
- agent_response,
772
- content='conversations',
773
- content_rowid='rowid'
774
- );
775
-
776
- CREATE TRIGGER IF NOT EXISTS conversations_fts_ai AFTER INSERT ON conversations BEGIN
777
- INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
778
- VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
779
- END;
780
-
781
- CREATE TRIGGER IF NOT EXISTS conversations_fts_ad AFTER DELETE ON conversations BEGIN
782
- INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
783
- VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
784
- END;
785
-
786
- CREATE TRIGGER IF NOT EXISTS conversations_fts_au AFTER UPDATE ON conversations BEGIN
787
- INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
788
- VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
789
- INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
790
- VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
791
- END;
792
- `);
793
- try {
794
- await client.execute({
795
- sql: `ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3`,
796
- args: []
797
- });
798
- } catch {
799
- }
800
- try {
801
- await client.execute(
802
- `CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)`
803
- );
804
- } catch {
805
- }
806
- try {
807
- await client.execute({
808
- sql: `UPDATE memories SET tier = 1 WHERE tool_name = 'commit_to_long_term_memory' AND importance >= 8 AND tier = 3`,
809
- args: []
810
- });
811
- await client.execute({
812
- sql: `UPDATE memories SET tier = 2 WHERE tool_name IN ('store_memory', 'manual') AND importance >= 5 AND tier = 3`,
813
- args: []
814
- });
815
- } catch {
816
- }
817
- try {
818
- await client.execute({
819
- sql: `ALTER TABLE memories ADD COLUMN supersedes_id TEXT`,
820
- args: []
821
- });
822
- } catch {
823
- }
824
- try {
825
- await client.execute(
826
- `CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL`
827
- );
828
- } catch {
829
- }
830
- for (const col of [
831
- "ALTER TABLE tasks ADD COLUMN checkpoint TEXT",
832
- "ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER DEFAULT 0"
833
- ]) {
834
- try {
835
- await client.execute(col);
836
- } catch {
837
- }
838
- }
839
- }
840
- async function disposeDatabase() {
841
- if (_client) {
842
- _client.close();
843
- _client = null;
844
- }
845
- }
846
- var _client, initTurso, disposeTurso;
847
- var init_database = __esm({
848
- "src/lib/database.ts"() {
849
- "use strict";
850
- _client = null;
851
- initTurso = initDatabase;
852
- disposeTurso = disposeDatabase;
853
- }
854
- });
855
-
856
- // src/lib/keychain.ts
857
- import { readFile, writeFile, unlink, mkdir, chmod } from "fs/promises";
858
- import { existsSync } from "fs";
859
- import path from "path";
860
- import crypto from "crypto";
861
- function getKeyDir() {
862
- return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path.join(process.env.HOME ?? "/tmp", ".exe-os");
863
- }
864
- function getKeyPath() {
865
- return path.join(getKeyDir(), "master.key");
866
- }
867
- async function tryKeytar() {
868
- try {
869
- return await import("keytar");
870
- } catch {
871
- return null;
872
- }
873
- }
874
- async function getMasterKey() {
875
- const keytar = await tryKeytar();
876
- if (keytar) {
877
- try {
878
- const stored = await keytar.getPassword(SERVICE, ACCOUNT);
879
- if (stored) {
880
- return Buffer.from(stored, "base64");
881
- }
882
- } catch {
883
- }
884
- }
885
- const keyPath = getKeyPath();
886
- if (!existsSync(keyPath)) {
887
- return null;
888
- }
889
- try {
890
- const content = await readFile(keyPath, "utf-8");
891
- return Buffer.from(content.trim(), "base64");
892
- } catch {
893
- return null;
894
- }
895
- }
896
- var SERVICE, ACCOUNT;
897
- var init_keychain = __esm({
898
- "src/lib/keychain.ts"() {
899
- "use strict";
900
- SERVICE = "exe-mem";
901
- ACCOUNT = "master-key";
902
- }
903
- });
904
-
905
- // src/lib/config.ts
906
- var config_exports = {};
907
- __export(config_exports, {
908
- CONFIG_MIGRATIONS: () => CONFIG_MIGRATIONS,
909
- CONFIG_PATH: () => CONFIG_PATH,
910
- CURRENT_CONFIG_VERSION: () => CURRENT_CONFIG_VERSION,
911
- DB_PATH: () => DB_PATH,
912
- EXE_AI_DIR: () => EXE_AI_DIR,
913
- LEGACY_LANCE_PATH: () => LEGACY_LANCE_PATH,
914
- MODELS_DIR: () => MODELS_DIR,
915
- loadConfig: () => loadConfig,
916
- loadConfigFrom: () => loadConfigFrom,
917
- loadConfigSync: () => loadConfigSync,
918
- migrateConfig: () => migrateConfig,
919
- saveConfig: () => saveConfig
920
- });
921
- import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
922
- import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
923
- import path2 from "path";
924
- import os from "os";
925
- function resolveDataDir() {
926
- if (process.env.EXE_OS_DIR) return process.env.EXE_OS_DIR;
927
- if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
928
- const newDir = path2.join(os.homedir(), ".exe-os");
929
- const legacyDir = path2.join(os.homedir(), ".exe-mem");
930
- if (!existsSync2(newDir) && existsSync2(legacyDir)) {
931
- try {
932
- renameSync(legacyDir, newDir);
933
- process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
934
- `);
935
- } catch {
936
- return legacyDir;
937
- }
938
- }
939
- return newDir;
940
- }
941
- function migrateLegacyConfig(raw) {
942
- if ("r2" in raw) {
943
- process.stderr.write(
944
- "[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"
945
- );
946
- delete raw.r2;
947
- }
948
- if ("syncIntervalMs" in raw) {
949
- delete raw.syncIntervalMs;
950
- }
951
- return raw;
952
- }
953
- function migrateConfig(raw) {
954
- const fromVersion = typeof raw.config_version === "number" ? raw.config_version : 0;
955
- let currentVersion = fromVersion;
956
- let migrated = false;
957
- if (currentVersion > CURRENT_CONFIG_VERSION) {
958
- return { config: raw, migrated: false, fromVersion };
959
- }
960
- for (const migration of CONFIG_MIGRATIONS) {
961
- if (currentVersion === migration.from && migration.to <= CURRENT_CONFIG_VERSION) {
962
- raw = migration.migrate(raw);
963
- currentVersion = migration.to;
964
- migrated = true;
965
- }
966
- }
967
- return { config: raw, migrated, fromVersion };
968
- }
969
- function normalizeScalingRoadmap(raw) {
970
- const defaultAuto = DEFAULT_CONFIG.scalingRoadmap.rerankerAutoTrigger;
971
- const userRoadmap = raw.scalingRoadmap ?? {};
972
- const userAuto = userRoadmap.rerankerAutoTrigger ?? {};
973
- if (userAuto.enabled === void 0 && raw.rerankerEnabled !== void 0) {
974
- userAuto.enabled = raw.rerankerEnabled;
975
- }
976
- raw.scalingRoadmap = {
977
- ...userRoadmap,
978
- rerankerAutoTrigger: { ...defaultAuto, ...userAuto }
979
- };
980
- }
981
- function normalizeSessionLifecycle(raw) {
982
- const defaultSL = DEFAULT_CONFIG.sessionLifecycle;
983
- const userSL = raw.sessionLifecycle ?? {};
984
- raw.sessionLifecycle = { ...defaultSL, ...userSL };
985
- }
986
- function normalizeAutoUpdate(raw) {
987
- const defaultAU = DEFAULT_CONFIG.autoUpdate;
988
- const userAU = raw.autoUpdate ?? {};
989
- raw.autoUpdate = { ...defaultAU, ...userAU };
990
- }
991
- async function loadConfig() {
992
- const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
993
- await mkdir2(dir, { recursive: true });
994
- const configPath = path2.join(dir, "config.json");
995
- if (!existsSync2(configPath)) {
996
- return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
997
- }
998
- const raw = await readFile2(configPath, "utf-8");
999
- try {
1000
- let parsed = JSON.parse(raw);
1001
- parsed = migrateLegacyConfig(parsed);
1002
- const { config: migratedCfg, migrated, fromVersion } = migrateConfig(parsed);
1003
- if (migrated) {
1004
- process.stderr.write(`[exe-os] Config migrated from v${fromVersion} to v${migratedCfg.config_version}
1005
- `);
1006
- try {
1007
- await writeFile2(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
1008
- } catch {
1009
- }
1010
- }
1011
- normalizeScalingRoadmap(migratedCfg);
1012
- normalizeSessionLifecycle(migratedCfg);
1013
- normalizeAutoUpdate(migratedCfg);
1014
- const config = { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
1015
- if (config.dbPath.startsWith("~")) {
1016
- config.dbPath = config.dbPath.replace(/^~/, os.homedir());
1017
- }
1018
- return config;
1019
- } catch {
1020
- return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
1021
- }
1022
- }
1023
- function loadConfigSync() {
1024
- const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
1025
- const configPath = path2.join(dir, "config.json");
1026
- if (!existsSync2(configPath)) {
1027
- return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
1028
- }
1029
- try {
1030
- const raw = readFileSync(configPath, "utf-8");
1031
- let parsed = JSON.parse(raw);
1032
- parsed = migrateLegacyConfig(parsed);
1033
- const { config: migratedCfg } = migrateConfig(parsed);
1034
- normalizeScalingRoadmap(migratedCfg);
1035
- normalizeSessionLifecycle(migratedCfg);
1036
- normalizeAutoUpdate(migratedCfg);
1037
- return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
1038
- } catch {
1039
- return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
1040
- }
1041
- }
1042
- async function saveConfig(config) {
1043
- const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
1044
- await mkdir2(dir, { recursive: true });
1045
- const configPath = path2.join(dir, "config.json");
1046
- await writeFile2(configPath, JSON.stringify(config, null, 2) + "\n");
1047
- }
1048
- async function loadConfigFrom(configPath) {
1049
- const raw = await readFile2(configPath, "utf-8");
1050
- try {
1051
- let parsed = JSON.parse(raw);
1052
- parsed = migrateLegacyConfig(parsed);
1053
- const { config: migratedCfg } = migrateConfig(parsed);
1054
- normalizeScalingRoadmap(migratedCfg);
1055
- normalizeSessionLifecycle(migratedCfg);
1056
- normalizeAutoUpdate(migratedCfg);
1057
- return { ...DEFAULT_CONFIG, ...migratedCfg };
1058
- } catch {
1059
- return { ...DEFAULT_CONFIG };
1060
- }
1061
- }
1062
- var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, LEGACY_LANCE_PATH, CURRENT_CONFIG_VERSION, DEFAULT_CONFIG, CONFIG_MIGRATIONS;
1063
- var init_config = __esm({
1064
- "src/lib/config.ts"() {
1065
- "use strict";
1066
- EXE_AI_DIR = resolveDataDir();
1067
- DB_PATH = path2.join(EXE_AI_DIR, "memories.db");
1068
- MODELS_DIR = path2.join(EXE_AI_DIR, "models");
1069
- CONFIG_PATH = path2.join(EXE_AI_DIR, "config.json");
1070
- LEGACY_LANCE_PATH = path2.join(EXE_AI_DIR, "local.lance");
1071
- CURRENT_CONFIG_VERSION = 1;
1072
- DEFAULT_CONFIG = {
1073
- config_version: CURRENT_CONFIG_VERSION,
1074
- dbPath: DB_PATH,
1075
- modelFile: "jina-embeddings-v5-small-q4_k_m.gguf",
1076
- embeddingDim: 1024,
1077
- batchSize: 20,
1078
- flushIntervalMs: 1e4,
1079
- autoIngestion: true,
1080
- autoRetrieval: true,
1081
- searchMode: "hybrid",
1082
- hookSearchMode: "hybrid",
1083
- fileGrepEnabled: true,
1084
- splashEffect: true,
1085
- consolidationEnabled: true,
1086
- consolidationIntervalMs: 6 * 60 * 60 * 1e3,
1087
- consolidationModel: "claude-haiku-4-5-20251001",
1088
- consolidationMaxCallsPerRun: 20,
1089
- selfQueryRouter: true,
1090
- selfQueryModel: "claude-haiku-4-5-20251001",
1091
- rerankerEnabled: true,
1092
- scalingRoadmap: {
1093
- rerankerAutoTrigger: {
1094
- enabled: true,
1095
- broadQueryMinCardinality: 5e4,
1096
- fetchTopK: 150,
1097
- returnTopK: 5
1098
- }
1099
- },
1100
- graphRagEnabled: true,
1101
- wikiEnabled: false,
1102
- wikiUrl: "",
1103
- wikiApiKey: "",
1104
- wikiSyncIntervalMs: 30 * 60 * 1e3,
1105
- wikiWorkspaceMapping: {
1106
- exe: "Executive",
1107
- yoshi: "Engineering",
1108
- mari: "Marketing",
1109
- tom: "Engineering",
1110
- sasha: "Production"
1111
- },
1112
- wikiAutoUpdate: true,
1113
- wikiAutoUpdateThreshold: 0.5,
1114
- wikiAutoUpdateCreateNew: true,
1115
- skillLearning: true,
1116
- skillThreshold: 3,
1117
- skillModel: "claude-haiku-4-5-20251001",
1118
- exeHeartbeat: {
1119
- enabled: true,
1120
- intervalSeconds: 60,
1121
- staleInProgressThresholdHours: 2
1122
- },
1123
- sessionLifecycle: {
1124
- idleKillEnabled: true,
1125
- idleKillTicksRequired: 3,
1126
- idleKillIntercomAckWindowMs: 1e4,
1127
- maxAutoInstances: 10
1128
- },
1129
- autoUpdate: {
1130
- checkOnBoot: true,
1131
- autoInstall: false,
1132
- checkIntervalMs: 24 * 60 * 60 * 1e3
1133
- }
1134
- };
1135
- CONFIG_MIGRATIONS = [
1136
- {
1137
- from: 0,
1138
- to: 1,
1139
- migrate: (cfg) => {
1140
- cfg.config_version = 1;
1141
- return cfg;
1142
- }
1143
- }
1144
- ];
1145
- }
1146
- });
1147
-
1148
- // src/lib/shard-manager.ts
1149
- var shard_manager_exports = {};
1150
- __export(shard_manager_exports, {
1151
- disposeShards: () => disposeShards,
1152
- ensureShardSchema: () => ensureShardSchema,
1153
- getReadyShardClient: () => getReadyShardClient,
1154
- getShardClient: () => getShardClient,
1155
- getShardsDir: () => getShardsDir,
1156
- initShardManager: () => initShardManager,
1157
- isShardingEnabled: () => isShardingEnabled,
1158
- listShards: () => listShards,
1159
- shardExists: () => shardExists
1160
- });
1161
- import path3 from "path";
1162
- import { existsSync as existsSync3, mkdirSync } from "fs";
1163
- import { createClient as createClient2 } from "@libsql/client";
1164
- function initShardManager(encryptionKey) {
1165
- _encryptionKey = encryptionKey;
1166
- if (!existsSync3(SHARDS_DIR)) {
1167
- mkdirSync(SHARDS_DIR, { recursive: true });
1168
- }
1169
- _shardingEnabled = true;
1170
- }
1171
- function isShardingEnabled() {
1172
- return _shardingEnabled;
1173
- }
1174
- function getShardsDir() {
1175
- return SHARDS_DIR;
1176
- }
1177
- function getShardClient(projectName) {
1178
- if (!_encryptionKey) {
1179
- throw new Error("Shard manager not initialized. Call initShardManager() first.");
1180
- }
1181
- const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
1182
- if (!safeName) {
1183
- throw new Error(`Invalid project name for shard: "${projectName}"`);
1184
- }
1185
- const cached = _shards.get(safeName);
1186
- if (cached) return cached;
1187
- const dbPath = path3.join(SHARDS_DIR, `${safeName}.db`);
1188
- const client = createClient2({
1189
- url: `file:${dbPath}`,
1190
- encryptionKey: _encryptionKey
1191
- });
1192
- _shards.set(safeName, client);
1193
- return client;
1194
- }
1195
- function shardExists(projectName) {
1196
- const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
1197
- return existsSync3(path3.join(SHARDS_DIR, `${safeName}.db`));
1198
- }
1199
- function listShards() {
1200
- if (!existsSync3(SHARDS_DIR)) return [];
1201
- const { readdirSync: readdirSync2 } = __require("fs");
1202
- return readdirSync2(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
1203
- }
1204
- async function ensureShardSchema(client) {
1205
- await client.execute("PRAGMA journal_mode = WAL");
1206
- await client.execute("PRAGMA busy_timeout = 5000");
1207
- try {
1208
- await client.execute("PRAGMA libsql_vector_search_ef = 128");
1209
- } catch {
1210
- }
1211
- await client.executeMultiple(`
1212
- CREATE TABLE IF NOT EXISTS memories (
1213
- id TEXT PRIMARY KEY,
1214
- agent_id TEXT NOT NULL,
1215
- agent_role TEXT NOT NULL,
1216
- session_id TEXT NOT NULL,
1217
- timestamp TEXT NOT NULL,
1218
- tool_name TEXT NOT NULL,
1219
- project_name TEXT NOT NULL,
1220
- has_error INTEGER NOT NULL DEFAULT 0,
1221
- raw_text TEXT NOT NULL,
1222
- vector F32_BLOB(1024),
1223
- version INTEGER NOT NULL DEFAULT 0
1224
- );
1225
-
1226
- CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
1227
- CREATE INDEX IF NOT EXISTS idx_memories_timestamp ON memories(timestamp);
1228
- CREATE INDEX IF NOT EXISTS idx_memories_agent_project ON memories(agent_id, project_name);
1229
- `);
1230
- await client.executeMultiple(`
1231
- CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
1232
- raw_text,
1233
- content='memories',
1234
- content_rowid='rowid'
1235
- );
1236
-
1237
- CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
1238
- INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1239
- END;
1240
-
1241
- CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
1242
- INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1243
- END;
1244
-
1245
- CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
1246
- INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
1247
- INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
1248
- END;
1249
- `);
1250
- for (const col of [
1251
- "ALTER TABLE memories ADD COLUMN task_id TEXT",
1252
- "ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
1253
- "ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
1254
- "ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
1255
- "ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
1256
- "ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0",
1257
- "ALTER TABLE memories ADD COLUMN content_hash TEXT",
1258
- "ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT",
1259
- "ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7",
1260
- "ALTER TABLE memories ADD COLUMN last_accessed TEXT",
1261
- // Wiki linkage columns (must match database.ts)
1262
- "ALTER TABLE memories ADD COLUMN workspace_id TEXT",
1263
- "ALTER TABLE memories ADD COLUMN document_id TEXT",
1264
- "ALTER TABLE memories ADD COLUMN user_id TEXT",
1265
- "ALTER TABLE memories ADD COLUMN char_offset INTEGER",
1266
- "ALTER TABLE memories ADD COLUMN page_number INTEGER",
1267
- // Source provenance columns (must match database.ts)
1268
- "ALTER TABLE memories ADD COLUMN source_path TEXT",
1269
- "ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'",
1270
- "ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3",
1271
- "ALTER TABLE memories ADD COLUMN supersedes_id TEXT"
1272
- ]) {
1273
- try {
1274
- await client.execute(col);
1275
- } catch {
1276
- }
1277
- }
1278
- for (const idx of [
1279
- "CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)",
1280
- "CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL"
1281
- ]) {
1282
- try {
1283
- await client.execute(idx);
1284
- } catch {
1285
- }
1286
- }
1287
- try {
1288
- await client.execute("CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)");
1289
- } catch {
1290
- }
1291
- for (const idx of [
1292
- "CREATE INDEX IF NOT EXISTS idx_memories_workspace ON memories(workspace_id)",
1293
- "CREATE INDEX IF NOT EXISTS idx_memories_document ON memories(document_id)",
1294
- "CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)"
1295
- ]) {
1296
- try {
1297
- await client.execute(idx);
1298
- } catch {
1299
- }
1300
- }
1301
- await client.executeMultiple(`
1302
- CREATE TABLE IF NOT EXISTS entities (
1303
- id TEXT PRIMARY KEY,
1304
- name TEXT NOT NULL,
1305
- type TEXT NOT NULL,
1306
- first_seen TEXT NOT NULL,
1307
- last_seen TEXT NOT NULL,
1308
- properties TEXT DEFAULT '{}',
1309
- UNIQUE(name, type)
1310
- );
1311
-
1312
- CREATE TABLE IF NOT EXISTS relationships (
1313
- id TEXT PRIMARY KEY,
1314
- source_entity_id TEXT NOT NULL,
1315
- target_entity_id TEXT NOT NULL,
1316
- type TEXT NOT NULL,
1317
- weight REAL DEFAULT 1.0,
1318
- timestamp TEXT NOT NULL,
1319
- properties TEXT DEFAULT '{}',
1320
- UNIQUE(source_entity_id, target_entity_id, type)
1321
- );
1322
-
1323
- CREATE TABLE IF NOT EXISTS entity_memories (
1324
- entity_id TEXT NOT NULL,
1325
- memory_id TEXT NOT NULL,
1326
- PRIMARY KEY (entity_id, memory_id)
1327
- );
1328
-
1329
- CREATE TABLE IF NOT EXISTS relationship_memories (
1330
- relationship_id TEXT NOT NULL,
1331
- memory_id TEXT NOT NULL,
1332
- PRIMARY KEY (relationship_id, memory_id)
1333
- );
1334
-
1335
- CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
1336
- CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
1337
- CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
1338
- CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
1339
- CREATE INDEX IF NOT EXISTS idx_relationships_type ON relationships(type);
1340
-
1341
- CREATE TABLE IF NOT EXISTS hyperedges (
1342
- id TEXT PRIMARY KEY,
1343
- label TEXT NOT NULL,
1344
- relation TEXT NOT NULL,
1345
- confidence REAL DEFAULT 1.0,
1346
- timestamp TEXT NOT NULL
1347
- );
1348
-
1349
- CREATE TABLE IF NOT EXISTS hyperedge_nodes (
1350
- hyperedge_id TEXT NOT NULL,
1351
- entity_id TEXT NOT NULL,
1352
- PRIMARY KEY (hyperedge_id, entity_id)
1353
- );
1354
- `);
1355
- for (const col of [
1356
- "ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
1357
- "ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
1358
- ]) {
1359
- try {
1360
- await client.execute(col);
1361
- } catch {
1362
- }
1363
- }
1364
- }
1365
- async function getReadyShardClient(projectName) {
1366
- const client = getShardClient(projectName);
1367
- await ensureShardSchema(client);
1368
- return client;
1369
- }
1370
- function disposeShards() {
1371
- for (const [, client] of _shards) {
1372
- client.close();
1373
- }
1374
- _shards.clear();
1375
- _shardingEnabled = false;
1376
- _encryptionKey = null;
1377
- }
1378
- var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
1379
- var init_shard_manager = __esm({
1380
- "src/lib/shard-manager.ts"() {
1381
- "use strict";
1382
- init_config();
1383
- SHARDS_DIR = path3.join(EXE_AI_DIR, "shards");
1384
- _shards = /* @__PURE__ */ new Map();
1385
- _encryptionKey = null;
1386
- _shardingEnabled = false;
1387
- }
1388
- });
1389
-
1390
- // src/lib/store.ts
1391
- var store_exports = {};
1392
- __export(store_exports, {
1393
- attachDocumentMetadata: () => attachDocumentMetadata,
1394
- buildWikiScopeFilter: () => buildWikiScopeFilter,
1395
- classifyTier: () => classifyTier,
1396
- disposeStore: () => disposeStore,
1397
- flushBatch: () => flushBatch,
1398
- flushTier3: () => flushTier3,
1399
- getMemoryCardinality: () => getMemoryCardinality,
1400
- initStore: () => initStore,
1401
- reserveVersions: () => reserveVersions,
1402
- searchMemories: () => searchMemories,
1403
- updateMemoryStatus: () => updateMemoryStatus,
1404
- vectorToBlob: () => vectorToBlob,
1405
- writeMemory: () => writeMemory
1406
- });
1407
- async function initStore(options) {
1408
- if (_flushTimer !== null) {
1409
- clearInterval(_flushTimer);
1410
- _flushTimer = null;
1411
- }
1412
- _pendingRecords = [];
1413
- _flushing = false;
1414
- _batchSize = options?.batchSize ?? 20;
1415
- _flushIntervalMs = options?.flushIntervalMs ?? 1e4;
1416
- let dbPath = options?.dbPath;
1417
- if (!dbPath) {
1418
- const config = await loadConfig();
1419
- dbPath = config.dbPath;
1420
- }
1421
- let masterKey = options?.masterKey ?? null;
1422
- if (!masterKey) {
1423
- masterKey = await getMasterKey();
1424
- if (!masterKey) {
1425
- throw new Error(
1426
- "No encryption key found. Run /exe-setup to generate one."
1427
- );
1428
- }
1429
- }
1430
- const hexKey = masterKey.toString("hex");
1431
- await initTurso({
1432
- dbPath,
1433
- encryptionKey: hexKey
1434
- });
1435
- await ensureSchema();
1436
- try {
1437
- const { initShardManager: initShardManager2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
1438
- initShardManager2(hexKey);
1439
- } catch {
1440
- }
1441
- const client = getClient();
1442
- const vResult = await client.execute("SELECT MAX(version) as max_v FROM memories");
1443
- _nextVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
1444
- }
1445
- function classifyTier(record) {
1446
- if (record.tool_name === "commit_to_long_term_memory" && (record.importance ?? 0) >= 8) return 1;
1447
- if (["store_memory", "manual"].includes(record.tool_name ?? "") && (record.importance ?? 0) >= 5) return 2;
1448
- return 3;
1449
- }
1450
- async function writeMemory(record) {
1451
- if (record.vector !== null && record.vector.length !== EMBEDDING_DIM) {
1452
- throw new Error(
1453
- `Expected ${EMBEDDING_DIM}-dim vector, got ${record.vector.length}`
1454
- );
1455
- }
1456
- const dbRow = {
1457
- id: record.id,
1458
- agent_id: record.agent_id,
1459
- agent_role: record.agent_role,
1460
- session_id: record.session_id,
1461
- timestamp: record.timestamp,
1462
- tool_name: record.tool_name,
1463
- project_name: record.project_name,
1464
- has_error: record.has_error ? 1 : 0,
1465
- raw_text: record.raw_text,
1466
- vector: record.vector,
1467
- version: _nextVersion++,
1468
- task_id: record.task_id ?? null,
1469
- importance: record.importance ?? 5,
1470
- status: record.status ?? "active",
1471
- confidence: record.confidence ?? 0.7,
1472
- last_accessed: record.last_accessed ?? record.timestamp,
1473
- workspace_id: record.workspace_id ?? null,
1474
- document_id: record.document_id ?? null,
1475
- user_id: record.user_id ?? null,
1476
- char_offset: record.char_offset ?? null,
1477
- page_number: record.page_number ?? null,
1478
- source_path: record.source_path ?? null,
1479
- source_type: record.source_type ?? null,
1480
- tier: record.tier ?? classifyTier(record),
1481
- supersedes_id: record.supersedes_id ?? null
1482
- };
1483
- _pendingRecords.push(dbRow);
1484
- if (_flushTimer === null) {
1485
- _flushTimer = setInterval(() => {
1486
- void flushBatch();
1487
- }, _flushIntervalMs);
1488
- if (_flushTimer && typeof _flushTimer === "object" && "unref" in _flushTimer) {
1489
- _flushTimer.unref();
1490
- }
1491
- }
1492
- if (_pendingRecords.length >= _batchSize) {
1493
- await flushBatch();
1494
- }
1495
- }
1496
- async function flushBatch() {
1497
- if (_flushing || _pendingRecords.length === 0) return 0;
1498
- _flushing = true;
1499
- try {
1500
- const batch = _pendingRecords.slice(0);
1501
- const buildStmt = (row) => {
1502
- const hasVector = row.vector !== null;
1503
- const taskId = row.task_id ?? null;
1504
- const importance = row.importance ?? 5;
1505
- const status = row.status ?? "active";
1506
- const confidence = row.confidence ?? 0.7;
1507
- const lastAccessed = row.last_accessed ?? row.timestamp;
1508
- const workspaceId = row.workspace_id ?? null;
1509
- const documentId = row.document_id ?? null;
1510
- const userId = row.user_id ?? null;
1511
- const charOffset = row.char_offset ?? null;
1512
- const pageNumber = row.page_number ?? null;
1513
- const sourcePath = row.source_path ?? null;
1514
- const sourceType = row.source_type ?? null;
1515
- const tier = row.tier ?? 3;
1516
- const supersedesId = row.supersedes_id ?? null;
1517
- return {
1518
- sql: hasVector ? `INSERT OR IGNORE INTO memories
1519
- (id, agent_id, agent_role, session_id, timestamp,
1520
- tool_name, project_name,
1521
- has_error, raw_text, vector, version, task_id, importance, status,
1522
- confidence, last_accessed,
1523
- workspace_id, document_id, user_id, char_offset, page_number,
1524
- source_path, source_type, tier, supersedes_id)
1525
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, vector32(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` : `INSERT OR IGNORE INTO memories
1526
- (id, agent_id, agent_role, session_id, timestamp,
1527
- tool_name, project_name,
1528
- has_error, raw_text, vector, version, task_id, importance, status,
1529
- confidence, last_accessed,
1530
- workspace_id, document_id, user_id, char_offset, page_number,
1531
- source_path, source_type, tier, supersedes_id)
1532
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1533
- args: hasVector ? [
1534
- row.id,
1535
- row.agent_id,
1536
- row.agent_role,
1537
- row.session_id,
1538
- row.timestamp,
1539
- row.tool_name,
1540
- row.project_name,
1541
- row.has_error,
1542
- row.raw_text,
1543
- vectorToBlob(row.vector),
1544
- row.version,
1545
- taskId,
1546
- importance,
1547
- status,
1548
- confidence,
1549
- lastAccessed,
1550
- workspaceId,
1551
- documentId,
1552
- userId,
1553
- charOffset,
1554
- pageNumber,
1555
- sourcePath,
1556
- sourceType,
1557
- tier,
1558
- supersedesId
1559
- ] : [
1560
- row.id,
1561
- row.agent_id,
1562
- row.agent_role,
1563
- row.session_id,
1564
- row.timestamp,
1565
- row.tool_name,
1566
- row.project_name,
1567
- row.has_error,
1568
- row.raw_text,
1569
- row.version,
1570
- taskId,
1571
- importance,
1572
- status,
1573
- confidence,
1574
- lastAccessed,
1575
- workspaceId,
1576
- documentId,
1577
- userId,
1578
- charOffset,
1579
- pageNumber,
1580
- sourcePath,
1581
- sourceType,
1582
- tier,
1583
- supersedesId
1584
- ]
1585
- };
1586
- };
1587
- const globalClient = getClient();
1588
- const globalStmts = batch.map(buildStmt);
1589
- await globalClient.batch(globalStmts, "write");
1590
- _pendingRecords.splice(0, batch.length);
1591
- try {
1592
- const { isShardingEnabled: isShardingEnabled2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
1593
- if (isShardingEnabled2()) {
1594
- const byProject = /* @__PURE__ */ new Map();
1595
- for (const row of batch) {
1596
- const proj = row.project_name || "unknown";
1597
- if (!byProject.has(proj)) byProject.set(proj, []);
1598
- byProject.get(proj).push(row);
1599
- }
1600
- for (const [project, rows] of byProject) {
1601
- try {
1602
- const shardClient = await getReadyShardClient2(project);
1603
- const shardStmts = rows.map(buildStmt);
1604
- await shardClient.batch(shardStmts, "write");
1605
- } catch (err) {
1606
- process.stderr.write(
1607
- `[store] Shard write failed for ${project}: ${err instanceof Error ? err.message : String(err)}
1608
- `
1609
- );
1610
- }
1611
- }
1612
- }
1613
- } catch {
1614
- }
1615
- return batch.length;
1616
- } finally {
1617
- _flushing = false;
1618
- }
1619
- }
1620
- function buildWikiScopeFilter(options, columnPrefix) {
1621
- const args = [];
1622
- let clause = "";
1623
- if (options?.workspaceId !== void 0) {
1624
- clause += ` AND ${columnPrefix}workspace_id = ?`;
1625
- args.push(options.workspaceId);
1626
- }
1627
- if (options?.userId === void 0) {
1628
- clause += ` AND ${columnPrefix}user_id IS NULL`;
1629
- } else if (options.userId === null) {
1630
- clause += ` AND ${columnPrefix}user_id IS NULL`;
1631
- } else {
1632
- clause += ` AND (${columnPrefix}user_id = ? OR ${columnPrefix}user_id IS NULL)`;
1633
- args.push(options.userId);
1634
- }
1635
- return { clause, args };
1636
- }
1637
- async function searchMemories(queryVector, agentId, options) {
1638
- let client;
1639
- try {
1640
- const { isShardingEnabled: isShardingEnabled2, shardExists: shardExists2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
1641
- if (isShardingEnabled2() && options?.projectName && shardExists2(options.projectName)) {
1642
- client = await getReadyShardClient2(options.projectName);
1643
- } else {
1644
- client = getClient();
1645
- }
1646
- } catch {
1647
- client = getClient();
1648
- }
1649
- const limit = options?.limit ?? 10;
1650
- const statusFilter = options?.includeArchived ? "" : `
1651
- AND COALESCE(status, 'active') = 'active'`;
1652
- let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
1653
- tool_name, project_name,
1654
- has_error, raw_text, vector, importance, status,
1655
- confidence, last_accessed,
1656
- workspace_id, document_id, user_id,
1657
- char_offset, page_number,
1658
- source_path, source_type
1659
- FROM memories
1660
- WHERE agent_id = ?
1661
- AND vector IS NOT NULL${statusFilter}
1662
- AND COALESCE(confidence, 0.7) >= 0.3`;
1663
- const args = [agentId];
1664
- const scope = buildWikiScopeFilter(options, "");
1665
- sql += scope.clause;
1666
- args.push(...scope.args);
1667
- if (options?.projectName) {
1668
- sql += ` AND project_name = ?`;
1669
- args.push(options.projectName);
1670
- }
1671
- if (options?.toolName) {
1672
- sql += ` AND tool_name = ?`;
1673
- args.push(options.toolName);
1674
- }
1675
- if (options?.hasError !== void 0) {
1676
- sql += ` AND has_error = ?`;
1677
- args.push(options.hasError ? 1 : 0);
1678
- }
1679
- if (options?.since) {
1680
- sql += ` AND timestamp >= ?`;
1681
- args.push(options.since);
1682
- }
1683
- sql += ` ORDER BY vector_distance_cos(vector, vector32(?))`;
1684
- args.push(vectorToBlob(queryVector));
1685
- sql += ` LIMIT ?`;
1686
- args.push(limit);
1687
- const result = await client.execute({ sql, args });
1688
- return result.rows.map((row) => ({
1689
- id: row.id,
1690
- agent_id: row.agent_id,
1691
- agent_role: row.agent_role,
1692
- session_id: row.session_id,
1693
- timestamp: row.timestamp,
1694
- tool_name: row.tool_name,
1695
- project_name: row.project_name,
1696
- has_error: row.has_error === 1,
1697
- raw_text: row.raw_text,
1698
- vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
1699
- importance: row.importance ?? 5,
1700
- status: row.status ?? "active",
1701
- confidence: row.confidence ?? 0.7,
1702
- last_accessed: row.last_accessed ?? row.timestamp,
1703
- workspace_id: row.workspace_id ?? null,
1704
- document_id: row.document_id ?? null,
1705
- user_id: row.user_id ?? null,
1706
- char_offset: row.char_offset ?? null,
1707
- page_number: row.page_number ?? null,
1708
- source_path: row.source_path ?? null,
1709
- source_type: row.source_type ?? null
1710
- }));
1711
- }
1712
- async function attachDocumentMetadata(records) {
1713
- const docIds = [
1714
- ...new Set(
1715
- records.map((r) => r.document_id).filter((id) => typeof id === "string" && id.length > 0)
1716
- )
1717
- ];
1718
- if (docIds.length === 0) return records;
1719
- try {
1720
- const client = getClient();
1721
- const placeholders = docIds.map(() => "?").join(",");
1722
- const result = await client.execute({
1723
- sql: `SELECT id, filename, mime, source_type, uploaded_at
1724
- FROM documents
1725
- WHERE id IN (${placeholders})`,
1726
- args: docIds
1727
- });
1728
- const byId = /* @__PURE__ */ new Map();
1729
- for (const row of result.rows) {
1730
- const id = row.id;
1731
- byId.set(id, {
1732
- document_id: id,
1733
- filename: row.filename,
1734
- mime: row.mime ?? null,
1735
- source_type: row.source_type ?? null,
1736
- uploaded_at: row.uploaded_at
1737
- });
1738
- }
1739
- for (const record of records) {
1740
- if (!record.document_id) continue;
1741
- record.document_metadata = byId.get(record.document_id) ?? null;
1742
- }
1743
- } catch {
1744
- }
1745
- return records;
1746
- }
1747
- async function flushTier3(agentId, options) {
1748
- const client = getClient();
1749
- const maxAge = options?.maxAgeHours ?? 72;
1750
- const cutoff = new Date(Date.now() - maxAge * 36e5).toISOString();
1751
- if (options?.dryRun) {
1752
- const result2 = await client.execute({
1753
- sql: `SELECT COUNT(*) as cnt FROM memories
1754
- WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
1755
- args: [agentId, cutoff]
1756
- });
1757
- return { archived: Number(result2.rows[0]?.cnt ?? 0) };
1758
- }
1759
- const result = await client.execute({
1760
- sql: `UPDATE memories SET status = 'archived'
1761
- WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
1762
- args: [agentId, cutoff]
1763
- });
1764
- return { archived: result.rowsAffected };
1765
- }
1766
- async function disposeStore() {
1767
- if (_flushTimer !== null) {
1768
- clearInterval(_flushTimer);
1769
- _flushTimer = null;
1770
- }
1771
- if (_pendingRecords.length > 0) {
1772
- await flushBatch();
1773
- }
1774
- await disposeTurso();
1775
- _pendingRecords = [];
1776
- _nextVersion = 1;
1777
- }
1778
- function vectorToBlob(vector) {
1779
- const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
1780
- return JSON.stringify(Array.from(f32));
1781
- }
1782
- async function updateMemoryStatus(id, status) {
1783
- const client = getClient();
1784
- await client.execute({
1785
- sql: `UPDATE memories SET status = ? WHERE id = ?`,
1786
- args: [status, id]
1787
- });
1788
- }
1789
- function reserveVersions(count) {
1790
- const reserved = [];
1791
- for (let i = 0; i < count; i++) {
1792
- reserved.push(_nextVersion++);
1793
- }
1794
- return reserved;
1795
- }
1796
- async function getMemoryCardinality(agentId) {
1797
- try {
1798
- const client = getClient();
1799
- const result = await client.execute({
1800
- sql: `SELECT COUNT(*) as cnt FROM memories WHERE agent_id = ? AND COALESCE(status, 'active') = 'active'`,
1801
- args: [agentId]
1802
- });
1803
- return Number(result.rows[0]?.cnt) || 0;
1804
- } catch {
1805
- return 0;
1806
- }
1807
- }
1808
- var _pendingRecords, _batchSize, _flushIntervalMs, _flushTimer, _flushing, _nextVersion;
1809
- var init_store = __esm({
1810
- "src/lib/store.ts"() {
1811
- "use strict";
1812
- init_memory();
1813
- init_database();
1814
- init_keychain();
1815
- init_config();
1816
- _pendingRecords = [];
1817
- _batchSize = 20;
1818
- _flushIntervalMs = 1e4;
1819
- _flushTimer = null;
1820
- _flushing = false;
1821
- _nextVersion = 1;
1822
- }
1823
- });
1824
-
1825
- // src/lib/self-query-router.ts
1826
- var self_query_router_exports = {};
1827
- __export(self_query_router_exports, {
1828
- routeQuery: () => routeQuery
1829
- });
1830
- async function routeQuery(query, model = "claude-haiku-4-5-20251001") {
1831
- if (query.length < 10) {
1832
- return {
1833
- semanticQuery: query,
1834
- projectFilter: null,
1835
- roleFilter: null,
1836
- timeFilter: null,
1837
- isBroadQuery: false
1838
- };
1839
- }
1840
- try {
1841
- const Anthropic = (await import("@anthropic-ai/sdk")).default;
1842
- const client = new Anthropic();
1843
- const now = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
1844
- const response = await client.messages.create({
1845
- model,
1846
- max_tokens: 256,
1847
- system: `You are a search query router. Extract metadata filters from the user's memory search query. Today is ${now}. Convert relative time references (yesterday, last week) to ISO timestamps.`,
1848
- messages: [{ role: "user", content: query }],
1849
- tools: [EXTRACT_TOOL],
1850
- tool_choice: { type: "tool", name: "extract_search_filters" }
1851
- });
1852
- const toolBlock = response.content.find((b) => b.type === "tool_use");
1853
- if (toolBlock && toolBlock.type === "tool_use") {
1854
- const input = toolBlock.input;
1855
- return {
1856
- semanticQuery: input.semantic_query || query,
1857
- projectFilter: input.project_filter || null,
1858
- roleFilter: input.role_filter || null,
1859
- timeFilter: input.time_filter || null,
1860
- isBroadQuery: Boolean(input.is_broad_query)
1861
- };
1862
- }
1863
- } catch (err) {
1864
- process.stderr.write(
1865
- `[self-query-router] LLM extraction failed, using passthrough: ${err instanceof Error ? err.message : String(err)}
1866
- `
1867
- );
1868
- }
1869
- return {
1870
- semanticQuery: query,
1871
- projectFilter: null,
1872
- roleFilter: null,
1873
- timeFilter: null,
1874
- isBroadQuery: false
1875
- };
1876
- }
1877
- var EXTRACT_TOOL;
1878
- var init_self_query_router = __esm({
1879
- "src/lib/self-query-router.ts"() {
1880
- "use strict";
1881
- EXTRACT_TOOL = {
1882
- name: "extract_search_filters",
1883
- description: "Extract metadata filters from a memory search query to improve retrieval precision.",
1884
- input_schema: {
1885
- type: "object",
1886
- properties: {
1887
- semantic_query: {
1888
- type: "string",
1889
- description: "The core semantic meaning of the query, stripped of metadata references. This is used for embedding search."
1890
- },
1891
- project_filter: {
1892
- type: ["string", "null"],
1893
- description: "Project name if the query references a specific project (e.g., 'exe-os', 'exe-create'). Null if no project specified."
1894
- },
1895
- role_filter: {
1896
- type: ["string", "null"],
1897
- description: "Agent role if the query targets a specific role (e.g., 'CTO', 'CMO'). Null if no role specified."
1898
- },
1899
- time_filter: {
1900
- type: ["string", "null"],
1901
- description: "ISO 8601 timestamp lower bound if the query references a time period (e.g., 'last week', 'yesterday'). Null if no time reference."
1902
- },
1903
- is_broad_query: {
1904
- type: "boolean",
1905
- description: "True if the query is exploratory/broad (e.g., 'what has yoshi been working on', 'summarize recent activity'). False if targeted (e.g., 'how did we fix the auth bug')."
1906
- }
1907
- },
1908
- required: ["semantic_query", "project_filter", "role_filter", "time_filter", "is_broad_query"]
1909
- }
1910
- };
1911
- }
1912
- });
1913
-
1914
- // src/lib/exe-daemon-client.ts
1915
- import net from "net";
1916
- import { spawn } from "child_process";
1917
- import { randomUUID } from "crypto";
1918
- import { existsSync as existsSync4, unlinkSync, readFileSync as readFileSync2, openSync, closeSync, statSync } from "fs";
1919
- import path4 from "path";
1920
- import { fileURLToPath } from "url";
1921
- function handleData(chunk) {
1922
- _buffer += chunk.toString();
1923
- let newlineIdx;
1924
- while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
1925
- const line = _buffer.slice(0, newlineIdx).trim();
1926
- _buffer = _buffer.slice(newlineIdx + 1);
1927
- if (!line) continue;
1928
- try {
1929
- const response = JSON.parse(line);
1930
- const entry = _pending.get(response.id);
1931
- if (entry) {
1932
- clearTimeout(entry.timer);
1933
- _pending.delete(response.id);
1934
- entry.resolve(response);
1935
- }
1936
- } catch {
1937
- }
1938
- }
1939
- }
1940
- function cleanupStaleFiles() {
1941
- if (existsSync4(PID_PATH)) {
1942
- try {
1943
- const pid = parseInt(readFileSync2(PID_PATH, "utf8").trim(), 10);
1944
- if (pid > 0) {
1945
- try {
1946
- process.kill(pid, 0);
1947
- return;
1948
- } catch {
1949
- }
1950
- }
1951
- } catch {
1952
- }
1953
- try {
1954
- unlinkSync(PID_PATH);
1955
- } catch {
1956
- }
1957
- try {
1958
- unlinkSync(SOCKET_PATH);
1959
- } catch {
1960
- }
1961
- }
1962
- }
1963
- function findPackageRoot() {
1964
- let dir = path4.dirname(fileURLToPath(import.meta.url));
1965
- const { root } = path4.parse(dir);
1966
- while (dir !== root) {
1967
- if (existsSync4(path4.join(dir, "package.json"))) return dir;
1968
- dir = path4.dirname(dir);
1969
- }
1970
- return null;
1971
- }
1972
- function spawnDaemon() {
1973
- const pkgRoot = findPackageRoot();
1974
- if (!pkgRoot) {
1975
- process.stderr.write("[exed-client] WARN: cannot find package root\n");
1976
- return;
1977
- }
1978
- const daemonPath = path4.join(pkgRoot, "dist", "lib", "exe-daemon.js");
1979
- if (!existsSync4(daemonPath)) {
1980
- process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
1981
- `);
1982
- return;
1983
- }
1984
- const resolvedPath = daemonPath;
1985
- process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
1986
- `);
1987
- const logPath = path4.join(path4.dirname(SOCKET_PATH), "exed.log");
1988
- let stderrFd = "ignore";
1989
- try {
1990
- stderrFd = openSync(logPath, "a");
1991
- } catch {
1992
- }
1993
- const child = spawn(process.execPath, [resolvedPath], {
1994
- detached: true,
1995
- stdio: ["ignore", "ignore", stderrFd],
1996
- env: {
1997
- ...process.env,
1998
- EXE_DAEMON_SOCK: SOCKET_PATH,
1999
- EXE_DAEMON_PID: PID_PATH
2000
- }
2001
- });
2002
- child.unref();
2003
- if (typeof stderrFd === "number") {
2004
- try {
2005
- closeSync(stderrFd);
2006
- } catch {
2007
- }
2008
- }
2009
- }
2010
- function acquireSpawnLock() {
2011
- try {
2012
- const fd = openSync(SPAWN_LOCK_PATH, "wx");
2013
- closeSync(fd);
2014
- return true;
2015
- } catch {
2016
- try {
2017
- const stat = statSync(SPAWN_LOCK_PATH);
2018
- if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
2019
- try {
2020
- unlinkSync(SPAWN_LOCK_PATH);
2021
- } catch {
2022
- }
2023
- try {
2024
- const fd = openSync(SPAWN_LOCK_PATH, "wx");
2025
- closeSync(fd);
2026
- return true;
2027
- } catch {
2028
- }
2029
- }
2030
- } catch {
2031
- }
2032
- return false;
2033
- }
2034
- }
2035
- function releaseSpawnLock() {
2036
- try {
2037
- unlinkSync(SPAWN_LOCK_PATH);
2038
- } catch {
2039
- }
2040
- }
2041
- function connectToSocket() {
2042
- return new Promise((resolve) => {
2043
- if (_socket && _connected) {
2044
- resolve(true);
2045
- return;
2046
- }
2047
- const socket = net.createConnection({ path: SOCKET_PATH });
2048
- const connectTimeout = setTimeout(() => {
2049
- socket.destroy();
2050
- resolve(false);
2051
- }, 2e3);
2052
- socket.on("connect", () => {
2053
- clearTimeout(connectTimeout);
2054
- _socket = socket;
2055
- _connected = true;
2056
- _buffer = "";
2057
- socket.on("data", handleData);
2058
- socket.on("close", () => {
2059
- _connected = false;
2060
- _socket = null;
2061
- for (const [id, entry] of _pending) {
2062
- clearTimeout(entry.timer);
2063
- _pending.delete(id);
2064
- entry.resolve({ error: "Connection closed" });
2065
- }
2066
- });
2067
- socket.on("error", () => {
2068
- _connected = false;
2069
- _socket = null;
2070
- });
2071
- resolve(true);
2072
- });
2073
- socket.on("error", () => {
2074
- clearTimeout(connectTimeout);
2075
- resolve(false);
2076
- });
2077
- });
2078
- }
2079
- async function connectEmbedDaemon() {
2080
- if (_socket && _connected) return true;
2081
- if (await connectToSocket()) return true;
2082
- if (acquireSpawnLock()) {
2083
- try {
2084
- cleanupStaleFiles();
2085
- spawnDaemon();
2086
- } finally {
2087
- releaseSpawnLock();
2088
- }
2089
- }
2090
- const start = Date.now();
2091
- let delay = 100;
2092
- while (Date.now() - start < CONNECT_TIMEOUT_MS) {
2093
- await new Promise((r) => setTimeout(r, delay));
2094
- if (await connectToSocket()) return true;
2095
- delay = Math.min(delay * 2, 3e3);
2096
- }
2097
- return false;
2098
- }
2099
- function sendRequest(texts, priority) {
2100
- return new Promise((resolve) => {
2101
- if (!_socket || !_connected) {
2102
- resolve({ error: "Not connected" });
2103
- return;
2104
- }
2105
- const id = randomUUID();
2106
- const timer = setTimeout(() => {
2107
- _pending.delete(id);
2108
- resolve({ error: "Request timeout" });
2109
- }, REQUEST_TIMEOUT_MS);
2110
- _pending.set(id, { resolve, timer });
2111
- try {
2112
- _socket.write(JSON.stringify({ id, texts, priority }) + "\n");
2113
- } catch {
2114
- clearTimeout(timer);
2115
- _pending.delete(id);
2116
- resolve({ error: "Write failed" });
2117
- }
2118
- });
2119
- }
2120
- async function pingDaemon() {
2121
- if (!_socket || !_connected) return null;
2122
- return new Promise((resolve) => {
2123
- const id = randomUUID();
2124
- const timer = setTimeout(() => {
2125
- _pending.delete(id);
2126
- resolve(null);
2127
- }, 5e3);
2128
- _pending.set(id, {
2129
- resolve: (data) => {
2130
- if (data.health) {
2131
- resolve(data.health);
2132
- } else {
2133
- resolve(null);
2134
- }
2135
- },
2136
- timer
2137
- });
2138
- try {
2139
- _socket.write(JSON.stringify({ id, type: "health" }) + "\n");
2140
- } catch {
2141
- clearTimeout(timer);
2142
- _pending.delete(id);
2143
- resolve(null);
2144
- }
2145
- });
2146
- }
2147
- function killAndRespawnDaemon() {
2148
- process.stderr.write("[exed-client] Killing daemon for restart...\n");
2149
- if (existsSync4(PID_PATH)) {
2150
- try {
2151
- const pid = parseInt(readFileSync2(PID_PATH, "utf8").trim(), 10);
2152
- if (pid > 0) {
2153
- try {
2154
- process.kill(pid, "SIGKILL");
2155
- } catch {
2156
- }
2157
- }
2158
- } catch {
2159
- }
2160
- }
2161
- if (_socket) {
2162
- _socket.destroy();
2163
- _socket = null;
2164
- }
2165
- _connected = false;
2166
- _buffer = "";
2167
- try {
2168
- unlinkSync(PID_PATH);
2169
- } catch {
2170
- }
2171
- try {
2172
- unlinkSync(SOCKET_PATH);
2173
- } catch {
2174
- }
2175
- spawnDaemon();
2176
- }
2177
- async function embedViaClient(text, priority = "high") {
2178
- if (!_connected && !await connectEmbedDaemon()) return null;
2179
- _requestCount++;
2180
- if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
2181
- const health = await pingDaemon();
2182
- if (!health) {
2183
- process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
2184
- `);
2185
- killAndRespawnDaemon();
2186
- const start = Date.now();
2187
- let delay = 200;
2188
- while (Date.now() - start < CONNECT_TIMEOUT_MS) {
2189
- await new Promise((r) => setTimeout(r, delay));
2190
- if (await connectToSocket()) break;
2191
- delay = Math.min(delay * 2, 3e3);
2192
- }
2193
- if (!_connected) return null;
2194
- }
2195
- }
2196
- const result = await sendRequest([text], priority);
2197
- if (!result.error && result.vectors?.[0]) return result.vectors[0];
2198
- if (result.error) {
2199
- process.stderr.write(`[exed-client] Embed failed (${result.error}) \u2014 attempting restart
2200
- `);
2201
- killAndRespawnDaemon();
2202
- const start = Date.now();
2203
- let delay = 200;
2204
- while (Date.now() - start < CONNECT_TIMEOUT_MS) {
2205
- await new Promise((r) => setTimeout(r, delay));
2206
- if (await connectToSocket()) break;
2207
- delay = Math.min(delay * 2, 3e3);
2208
- }
2209
- if (!_connected) return null;
2210
- const retry = await sendRequest([text], priority);
2211
- if (!retry.error && retry.vectors?.[0]) return retry.vectors[0];
2212
- process.stderr.write(`[exed-client] Embed retry also failed: ${retry.error ?? "no vector"}
2213
- `);
2214
- }
2215
- return null;
2216
- }
2217
- function disconnectClient() {
2218
- if (_socket) {
2219
- _socket.destroy();
2220
- _socket = null;
2221
- }
2222
- _connected = false;
2223
- _buffer = "";
2224
- for (const [id, entry] of _pending) {
2225
- clearTimeout(entry.timer);
2226
- _pending.delete(id);
2227
- entry.resolve({ error: "Client disconnected" });
2228
- }
2229
- }
2230
- 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;
2231
- var init_exe_daemon_client = __esm({
2232
- "src/lib/exe-daemon-client.ts"() {
2233
- "use strict";
2234
- init_config();
2235
- SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path4.join(EXE_AI_DIR, "exed.sock");
2236
- PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path4.join(EXE_AI_DIR, "exed.pid");
2237
- SPAWN_LOCK_PATH = path4.join(EXE_AI_DIR, "exed-spawn.lock");
2238
- SPAWN_LOCK_STALE_MS = 3e4;
2239
- CONNECT_TIMEOUT_MS = 15e3;
2240
- REQUEST_TIMEOUT_MS = 3e4;
2241
- _socket = null;
2242
- _connected = false;
2243
- _buffer = "";
2244
- _requestCount = 0;
2245
- HEALTH_CHECK_INTERVAL = 100;
2246
- _pending = /* @__PURE__ */ new Map();
2247
- }
2248
- });
2249
-
2250
- // src/lib/embedder.ts
2251
- var embedder_exports = {};
2252
- __export(embedder_exports, {
2253
- disposeEmbedder: () => disposeEmbedder,
2254
- embed: () => embed,
2255
- embedDirect: () => embedDirect,
2256
- getEmbedder: () => getEmbedder
2257
- });
2258
- async function getEmbedder() {
2259
- const ok = await connectEmbedDaemon();
2260
- if (!ok) {
2261
- throw new Error(
2262
- "Could not connect to embedding daemon. Ensure the model is installed (run /exe-setup)."
2263
- );
2264
- }
2265
- }
2266
- async function embed(text) {
2267
- const priority = process.env.EXE_EMBED_PRIORITY === "low" ? "low" : "high";
2268
- const vector = await embedViaClient(text, priority);
2269
- if (!vector) {
2270
- throw new Error(
2271
- "Embedding failed: daemon unavailable. Run /exe-setup to verify model installation."
2272
- );
2273
- }
2274
- if (vector.length !== EMBEDDING_DIM) {
2275
- throw new Error(
2276
- `Embedding dimension mismatch: expected ${EMBEDDING_DIM}, got ${vector.length}. Ensure the correct Jina v5-small Q4_K_M GGUF model is installed.`
2277
- );
2278
- }
2279
- return vector;
2280
- }
2281
- async function disposeEmbedder() {
2282
- disconnectClient();
2283
- }
2284
- async function embedDirect(text) {
2285
- const llamaCpp = await import("node-llama-cpp");
2286
- const { MODELS_DIR: MODELS_DIR2 } = await Promise.resolve().then(() => (init_config(), config_exports));
2287
- const { existsSync: existsSync7 } = await import("fs");
2288
- const path8 = await import("path");
2289
- const modelPath = path8.join(MODELS_DIR2, "jina-embeddings-v5-small-q4_k_m.gguf");
2290
- if (!existsSync7(modelPath)) {
2291
- throw new Error(`Embedding model not found at ${modelPath}. Run '/exe-setup' to download it.`);
2292
- }
2293
- const llama = await llamaCpp.getLlama();
2294
- const model = await llama.loadModel({ modelPath });
2295
- const context = await model.createEmbeddingContext();
2296
- try {
2297
- const embedding = await context.getEmbeddingFor(text);
2298
- const vector = Array.from(embedding.vector);
2299
- if (vector.length !== EMBEDDING_DIM) {
2300
- throw new Error(
2301
- `Embedding dimension mismatch: expected ${EMBEDDING_DIM}, got ${vector.length}.`
2302
- );
2303
- }
2304
- return vector;
2305
- } finally {
2306
- await context.dispose();
2307
- await model.dispose();
2308
- }
2309
- }
2310
- var init_embedder = __esm({
2311
- "src/lib/embedder.ts"() {
2312
- "use strict";
2313
- init_memory();
2314
- init_exe_daemon_client();
2315
- }
2316
- });
2317
-
2318
- // src/lib/project-name.ts
2319
- var project_name_exports = {};
2320
- __export(project_name_exports, {
2321
- _resetCache: () => _resetCache,
2322
- getProjectName: () => getProjectName
2323
- });
2324
- import { execSync } from "child_process";
2325
- import path5 from "path";
2326
- function getProjectName(cwd) {
2327
- const dir = cwd ?? process.cwd();
2328
- if (_cached && _cachedCwd === dir) return _cached;
2329
- try {
2330
- let repoRoot;
2331
- try {
2332
- const gitCommonDir = execSync("git rev-parse --path-format=absolute --git-common-dir", {
2333
- cwd: dir,
2334
- encoding: "utf8",
2335
- timeout: 2e3,
2336
- stdio: ["pipe", "pipe", "pipe"]
2337
- }).trim();
2338
- repoRoot = path5.dirname(gitCommonDir);
2339
- } catch {
2340
- repoRoot = execSync("git rev-parse --show-toplevel", {
2341
- cwd: dir,
2342
- encoding: "utf8",
2343
- timeout: 2e3,
2344
- stdio: ["pipe", "pipe", "pipe"]
2345
- }).trim();
2346
- }
2347
- _cached = path5.basename(repoRoot);
2348
- _cachedCwd = dir;
2349
- return _cached;
2350
- } catch {
2351
- _cached = path5.basename(dir);
2352
- _cachedCwd = dir;
2353
- return _cached;
2354
- }
2355
- }
2356
- function _resetCache() {
2357
- _cached = null;
2358
- _cachedCwd = null;
2359
- }
2360
- var _cached, _cachedCwd;
2361
- var init_project_name = __esm({
2362
- "src/lib/project-name.ts"() {
2363
- "use strict";
2364
- _cached = null;
2365
- _cachedCwd = null;
2366
- }
2367
- });
2368
-
2369
- // src/lib/file-grep.ts
2370
- var file_grep_exports = {};
2371
- __export(file_grep_exports, {
2372
- grepProjectFiles: () => grepProjectFiles
2373
- });
2374
- import { execSync as execSync2 } from "child_process";
2375
- import { readFileSync as readFileSync3, readdirSync, statSync as statSync2, existsSync as existsSync5 } from "fs";
2376
- import path6 from "path";
2377
- import crypto2 from "crypto";
2378
- function hasRipgrep() {
2379
- if (_hasRg === null) {
2380
- try {
2381
- execSync2("rg --version", { stdio: "ignore", timeout: 2e3 });
2382
- _hasRg = true;
2383
- } catch {
2384
- _hasRg = false;
2385
- }
2386
- }
2387
- return _hasRg;
2388
- }
2389
- async function grepProjectFiles(query, projectRoot, options) {
2390
- const maxResults = options?.maxResults ?? 10;
2391
- const terms = query.toLowerCase().split(/\s+/).filter((t) => t.length >= 3).map((t) => t.replace(/[^a-z0-9_-]/g, "")).filter((t) => t.length >= 3);
2392
- if (terms.length === 0) return [];
2393
- const pattern = terms.join("|");
2394
- let hits;
2395
- if (hasRipgrep()) {
2396
- try {
2397
- hits = grepWithRipgrep(pattern, projectRoot, options?.patterns);
2398
- } catch {
2399
- hits = grepWithNodeFs(pattern, projectRoot, options?.patterns);
2400
- }
2401
- } else {
2402
- hits = grepWithNodeFs(pattern, projectRoot, options?.patterns);
2403
- }
2404
- hits.sort((a, b) => b.density - a.density);
2405
- const topHits = hits.slice(0, maxResults);
2406
- return topHits.map((hit) => {
2407
- const chunkCtx = getChunkContext(hit.filePath, hit.lineNumber);
2408
- const prefix = chunkCtx ? `[file: ${hit.filePath}:${hit.lineNumber} in ${chunkCtx}]` : `[file: ${hit.filePath}:${hit.lineNumber}]`;
2409
- return {
2410
- id: crypto2.createHash("sha256").update(`${hit.filePath}:${hit.lineNumber}`).digest("hex").slice(0, 36),
2411
- agent_id: "project",
2412
- agent_role: "file",
2413
- session_id: "file-grep",
2414
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2415
- tool_name: "file_grep",
2416
- project_name: path6.basename(projectRoot),
2417
- has_error: false,
2418
- raw_text: `${prefix} ${buildSnippet(hit, projectRoot)}`,
2419
- vector: null,
2420
- task_id: null
2421
- };
2422
- });
2423
- }
2424
- function getChunkContext(filePath, lineNumber) {
2425
- try {
2426
- const ext = filePath.split(".").pop()?.toLowerCase();
2427
- if (ext !== "ts" && ext !== "tsx" && ext !== "js" && ext !== "jsx") return "";
2428
- const source = readFileSync3(filePath, "utf8");
2429
- const lines = source.split("\n");
2430
- for (let i = Math.min(lineNumber - 1, lines.length - 1); i >= 0; i--) {
2431
- const line = lines[i];
2432
- const fnMatch = line.match(/(?:export\s+)?(?:async\s+)?function\s+(\w+)/);
2433
- if (fnMatch) return `function ${fnMatch[1]}`;
2434
- const classMatch = line.match(/(?:export\s+)?class\s+(\w+)/);
2435
- if (classMatch) return `class ${classMatch[1]}`;
2436
- const arrowMatch = line.match(/(?:export\s+)?(?:const|let)\s+(\w+)\s*=\s*(?:async\s+)?\(/);
2437
- if (arrowMatch) return `function ${arrowMatch[1]}`;
2438
- }
2439
- } catch {
2440
- }
2441
- return "";
2442
- }
2443
- function grepWithRipgrep(pattern, projectRoot, patterns) {
2444
- const globs = (patterns ?? DEFAULT_PATTERNS).map((p) => `--glob '${p}'`).join(" ");
2445
- const excludes = EXCLUDE_DIRS.map((d) => `--glob '!${d}'`).join(" ");
2446
- const cmd = `rg -i -c --hidden --no-config --no-ignore '${pattern.replace(/'/g, "\\'")}' . ${globs} ${excludes} --max-filesize ${MAX_FILE_SIZE} 2>/dev/null || true`;
2447
- const output = execSync2(cmd, {
2448
- cwd: projectRoot,
2449
- encoding: "utf8",
2450
- timeout: 3e3,
2451
- maxBuffer: 1024 * 1024
2452
- });
2453
- if (!output.trim()) return [];
2454
- const hits = [];
2455
- for (const line of output.trim().split("\n")) {
2456
- const colonIdx = line.lastIndexOf(":");
2457
- if (colonIdx === -1) continue;
2458
- const filePath = line.slice(0, colonIdx);
2459
- const matchCount = parseInt(line.slice(colonIdx + 1));
2460
- if (isNaN(matchCount) || matchCount === 0) continue;
2461
- try {
2462
- const firstMatch = execSync2(
2463
- `rg -i -n --hidden '${pattern.replace(/'/g, "\\'")}' '${filePath}' --max-count 1 2>/dev/null | head -1`,
2464
- { cwd: projectRoot, encoding: "utf8", timeout: 1e3 }
2465
- ).trim();
2466
- const lineNum = parseInt(firstMatch.split(":")[0] ?? "1");
2467
- const totalLines = execSync2(`wc -l < '${filePath}'`, {
2468
- cwd: projectRoot,
2469
- encoding: "utf8",
2470
- timeout: 1e3
2471
- }).trim();
2472
- hits.push({
2473
- filePath,
2474
- lineNumber: isNaN(lineNum) ? 1 : lineNum,
2475
- matchLine: firstMatch.slice(firstMatch.indexOf(":") + 1).slice(0, 200),
2476
- matchCount,
2477
- totalLines: parseInt(totalLines) || 1,
2478
- density: matchCount / (parseInt(totalLines) || 1)
2479
- });
2480
- } catch {
2481
- }
2482
- }
2483
- return hits;
2484
- }
2485
- function grepWithNodeFs(pattern, projectRoot, patterns) {
2486
- const regex = new RegExp(pattern, "gi");
2487
- const files = collectFiles(projectRoot, patterns ?? DEFAULT_PATTERNS);
2488
- const hits = [];
2489
- for (const filePath of files.slice(0, MAX_FILES)) {
2490
- const absPath = path6.join(projectRoot, filePath);
2491
- try {
2492
- const stat = statSync2(absPath);
2493
- if (stat.size > MAX_FILE_SIZE) continue;
2494
- const content = readFileSync3(absPath, "utf8");
2495
- const lines = content.split("\n");
2496
- const matches = content.match(regex);
2497
- if (!matches || matches.length === 0) continue;
2498
- const firstMatchIdx = lines.findIndex((l) => regex.test(l));
2499
- regex.lastIndex = 0;
2500
- hits.push({
2501
- filePath,
2502
- lineNumber: firstMatchIdx >= 0 ? firstMatchIdx + 1 : 1,
2503
- matchLine: firstMatchIdx >= 0 ? lines[firstMatchIdx].slice(0, 200) : "",
2504
- matchCount: matches.length,
2505
- totalLines: lines.length,
2506
- density: matches.length / lines.length
2507
- });
2508
- } catch {
2509
- }
2510
- }
2511
- return hits;
2512
- }
2513
- function collectFiles(root, patterns) {
2514
- const files = [];
2515
- function walk(dir, relative) {
2516
- if (files.length >= MAX_FILES) return;
2517
- const basename = path6.basename(dir);
2518
- if (EXCLUDE_DIRS.includes(basename)) return;
2519
- try {
2520
- const entries = readdirSync(dir, { withFileTypes: true });
2521
- for (const entry of entries) {
2522
- if (files.length >= MAX_FILES) return;
2523
- const rel = path6.join(relative, entry.name);
2524
- if (entry.isDirectory()) {
2525
- walk(path6.join(dir, entry.name), rel);
2526
- } else if (entry.isFile()) {
2527
- for (const pat of patterns) {
2528
- if (matchGlob(rel, pat)) {
2529
- files.push(rel);
2530
- break;
2531
- }
2532
- }
2533
- }
2534
- }
2535
- } catch {
2536
- }
2537
- }
2538
- walk(root, "");
2539
- return files;
2540
- }
2541
- function matchGlob(filePath, pattern) {
2542
- if (!pattern.includes("*")) return filePath === pattern;
2543
- const doubleStarMatch = pattern.match(/^(.+?)\/\*\*\/\*(\.\w+)$/);
2544
- if (doubleStarMatch) {
2545
- const dir = doubleStarMatch[1];
2546
- const ext2 = doubleStarMatch[2];
2547
- return filePath.startsWith(dir + "/") && filePath.endsWith(ext2);
2548
- }
2549
- if (pattern.startsWith("**/")) {
2550
- const ext2 = pattern.slice(3).replace("*", "");
2551
- return filePath.endsWith(ext2);
2552
- }
2553
- const slashIdx = pattern.lastIndexOf("/");
2554
- if (slashIdx !== -1) {
2555
- const dir = pattern.slice(0, slashIdx);
2556
- const ext2 = pattern.slice(slashIdx + 1).replace("*", "");
2557
- const fileDir = path6.dirname(filePath);
2558
- return fileDir === dir && filePath.endsWith(ext2);
2559
- }
2560
- const ext = pattern.replace("*", "");
2561
- return filePath.endsWith(ext) && !filePath.includes("/");
2562
- }
2563
- function buildSnippet(hit, projectRoot) {
2564
- try {
2565
- const absPath = path6.join(projectRoot, hit.filePath);
2566
- if (!existsSync5(absPath)) return hit.matchLine;
2567
- const lines = readFileSync3(absPath, "utf8").split("\n");
2568
- const start = Math.max(0, hit.lineNumber - 3);
2569
- const end = Math.min(lines.length, hit.lineNumber + 2);
2570
- return lines.slice(start, end).join("\n").slice(0, 500);
2571
- } catch {
2572
- return hit.matchLine;
2573
- }
2574
- }
2575
- var _hasRg, DEFAULT_PATTERNS, EXCLUDE_DIRS, MAX_FILE_SIZE, MAX_FILES;
2576
- var init_file_grep = __esm({
2577
- "src/lib/file-grep.ts"() {
2578
- "use strict";
2579
- _hasRg = null;
2580
- DEFAULT_PATTERNS = [
2581
- ".planning/*.md",
2582
- "exe/output/*.md",
2583
- "README.md",
2584
- "src/**/*.ts"
2585
- ];
2586
- EXCLUDE_DIRS = ["node_modules", "dist", ".git", "coverage", ".worktrees"];
2587
- MAX_FILE_SIZE = 100 * 1024;
2588
- MAX_FILES = 500;
2589
- }
2590
- });
2591
-
2592
- // src/lib/reranker.ts
2593
- var reranker_exports = {};
2594
- __export(reranker_exports, {
2595
- disposeReranker: () => disposeReranker,
2596
- getRerankerModelPath: () => getRerankerModelPath,
2597
- isRerankerAvailable: () => isRerankerAvailable,
2598
- rerank: () => rerank,
2599
- rerankWithScores: () => rerankWithScores
2600
- });
2601
- import path7 from "path";
2602
- import { existsSync as existsSync6 } from "fs";
2603
- function resetIdleTimer() {
2604
- if (_idleTimer) clearTimeout(_idleTimer);
2605
- _idleTimer = setTimeout(() => {
2606
- void disposeReranker();
2607
- }, IDLE_TIMEOUT_MS);
2608
- if (_idleTimer && typeof _idleTimer === "object" && "unref" in _idleTimer) {
2609
- _idleTimer.unref();
2610
- }
2611
- }
2612
- function isRerankerAvailable() {
2613
- return existsSync6(path7.join(MODELS_DIR, RERANKER_MODEL_FILE));
2614
- }
2615
- function getRerankerModelPath() {
2616
- return path7.join(MODELS_DIR, RERANKER_MODEL_FILE);
2617
- }
2618
- async function ensureLoaded() {
2619
- if (_rerankerContext) {
2620
- resetIdleTimer();
2621
- return;
2622
- }
2623
- const modelPath = path7.join(MODELS_DIR, RERANKER_MODEL_FILE);
2624
- if (!existsSync6(modelPath)) {
2625
- throw new Error(
2626
- `Reranker model not found at ${modelPath}. Run /exe-setup to download it.`
2627
- );
2628
- }
2629
- process.stderr.write("[reranker] Loading Jina Reranker v3...\n");
2630
- const { getLlama } = await import("node-llama-cpp");
2631
- const llama = await getLlama();
2632
- _rerankerModel = await llama.loadModel({ modelPath });
2633
- _rerankerContext = await _rerankerModel.createEmbeddingContext();
2634
- process.stderr.write("[reranker] Jina Reranker v3 loaded.\n");
2635
- resetIdleTimer();
2636
- }
2637
- async function disposeReranker() {
2638
- if (_idleTimer) {
2639
- clearTimeout(_idleTimer);
2640
- _idleTimer = null;
2641
- }
2642
- if (_rerankerContext) {
2643
- try {
2644
- await _rerankerContext.dispose();
2645
- } catch {
2646
- }
2647
- _rerankerContext = null;
2648
- }
2649
- if (_rerankerModel) {
2650
- try {
2651
- await _rerankerModel.dispose();
2652
- } catch {
2653
- }
2654
- _rerankerModel = null;
2655
- }
2656
- process.stderr.write("[reranker] Unloaded (idle timeout).\n");
2657
- }
2658
- async function rerankWithScores(query, texts, topK) {
2659
- if (texts.length === 0) return [];
2660
- await ensureLoaded();
2661
- const ctx = _rerankerContext;
2662
- const scored = [];
2663
- for (let i = 0; i < texts.length; i++) {
2664
- const text = texts[i] ?? "";
2665
- try {
2666
- const input = `query: ${query} document: ${text.slice(0, 512)}`;
2667
- const embedding = await ctx.getEmbeddingFor(input);
2668
- const score = embedding.vector[0] ?? 0;
2669
- scored.push({ text, score, index: i });
2670
- } catch {
2671
- scored.push({ text, score: -1, index: i });
2672
- }
2673
- }
2674
- scored.sort((a, b) => b.score - a.score);
2675
- return typeof topK === "number" ? scored.slice(0, topK) : scored;
2676
- }
2677
- async function rerank(query, candidates, topK = 5) {
2678
- if (candidates.length === 0) return [];
2679
- if (candidates.length <= topK) return candidates;
2680
- const scored = await rerankWithScores(
2681
- query,
2682
- candidates.map((c) => c.raw_text),
2683
- topK
2684
- );
2685
- return scored.map((s) => candidates[s.index]);
2686
- }
2687
- var RERANKER_MODEL_FILE, IDLE_TIMEOUT_MS, _rerankerContext, _rerankerModel, _idleTimer;
2688
- var init_reranker = __esm({
2689
- "src/lib/reranker.ts"() {
2690
- "use strict";
2691
- init_config();
2692
- RERANKER_MODEL_FILE = "jina-reranker-v3-q4_k_m.gguf";
2693
- IDLE_TIMEOUT_MS = 6e4;
2694
- _rerankerContext = null;
2695
- _rerankerModel = null;
2696
- _idleTimer = null;
2697
- }
2698
- });
2699
-
2700
- // src/bin/exe-search.ts
2701
- init_store();
2702
-
2703
- // src/lib/hybrid-search.ts
2704
- init_store();
2705
- init_database();
2706
- var RRF_K = 60;
2707
- async function hybridSearch(queryText, agentId, options) {
2708
- const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_config(), config_exports));
2709
- const config = await loadConfig2();
2710
- if (config.searchMode === "fts") {
2711
- return lightweightSearch(queryText, agentId, options);
2712
- }
2713
- const limit = options?.limit ?? 10;
2714
- let effectiveQuery = queryText;
2715
- let effectiveOptions = { ...options };
2716
- let _isBroadQuery = false;
2717
- if (config.selfQueryRouter && process.env.ANTHROPIC_API_KEY) {
2718
- try {
2719
- const { routeQuery: routeQuery2 } = await Promise.resolve().then(() => (init_self_query_router(), self_query_router_exports));
2720
- const routed = await routeQuery2(queryText, config.selfQueryModel);
2721
- effectiveQuery = routed.semanticQuery;
2722
- _isBroadQuery = routed.isBroadQuery;
2723
- if (routed.projectFilter && !effectiveOptions.projectName) {
2724
- effectiveOptions.projectName = routed.projectFilter;
2725
- }
2726
- if (routed.timeFilter && !effectiveOptions.since) {
2727
- effectiveOptions.since = routed.timeFilter;
2728
- }
2729
- } catch {
2730
- }
2731
- }
2732
- const { getMemoryCardinality: getMemoryCardinality2 } = await Promise.resolve().then(() => (init_store(), store_exports));
2733
- const cardinality = await getMemoryCardinality2(agentId);
2734
- const { rerankerAutoTrigger } = config.scalingRoadmap ?? {};
2735
- const minCardForBroad = rerankerAutoTrigger?.broadQueryMinCardinality ?? 5e4;
2736
- const useNarrowPath = cardinality < 1e4;
2737
- const useBroadPath = cardinality > minCardForBroad || _isBroadQuery && cardinality >= 1e4;
2738
- const effectiveIsBroad = useBroadPath && !useNarrowPath;
2739
- if (effectiveIsBroad !== _isBroadQuery) {
2740
- process.stderr.write(
2741
- `[hybrid-search] Adaptive routing override: cardinality=${cardinality}, router=${_isBroadQuery ? "broad" : "narrow"} \u2192 ${effectiveIsBroad ? "broad" : "narrow"}
2742
- `
2743
- );
2744
- }
2745
- const broadFetchTopK = config.scalingRoadmap?.rerankerAutoTrigger?.fetchTopK ?? 150;
2746
- const fetchLimit = effectiveIsBroad ? Math.max(limit * 5, broadFetchTopK) : Math.max(limit * 3, 30);
2747
- const fetchOptions = { ...effectiveOptions, limit: fetchLimit, includeSource: false };
2748
- let queryVector = null;
2749
- try {
2750
- const { embed: embed2 } = await Promise.resolve().then(() => (init_embedder(), embedder_exports));
2751
- queryVector = await embed2(effectiveQuery);
2752
- } catch {
2753
- process.stderr.write("[hybrid-search] Embed daemon unavailable \u2014 FTS-only mode\n");
2754
- }
2755
- let grepPromise = Promise.resolve([]);
2756
- if (config.fileGrepEnabled !== false) {
2757
- try {
2758
- const { getProjectName: getProjectName2 } = await Promise.resolve().then(() => (init_project_name(), project_name_exports));
2759
- const projectRoot = process.cwd();
2760
- const projectName = getProjectName2(projectRoot);
2761
- if (projectName && projectName !== "tmp") {
2762
- const { grepProjectFiles: grepProjectFiles2 } = await Promise.resolve().then(() => (init_file_grep(), file_grep_exports));
2763
- grepPromise = grepProjectFiles2(effectiveQuery, projectRoot, {
2764
- maxResults: 10
2765
- }).catch(() => []);
2766
- }
2767
- } catch {
2768
- }
2769
- }
2770
- const [ftsResults, vectorResults, grepResults] = await Promise.all([
2771
- lightweightSearch(effectiveQuery, agentId, fetchOptions),
2772
- queryVector ? searchMemories(queryVector, agentId, fetchOptions) : Promise.resolve([]),
2773
- grepPromise
2774
- ]);
2775
- const lists = [];
2776
- const weights = [];
2777
- if (ftsResults.length > 0) {
2778
- lists.push(ftsResults);
2779
- weights.push(1);
2780
- }
2781
- if (vectorResults.length > 0) {
2782
- lists.push(vectorResults);
2783
- weights.push(1);
2784
- }
2785
- if (grepResults.length > 0) {
2786
- lists.push(grepResults);
2787
- weights.push(0.5);
2788
- }
2789
- if (lists.length === 0) return [];
2790
- if (lists.length === 1 && !effectiveIsBroad) return lists[0].slice(0, limit);
2791
- const rrfLimit = effectiveIsBroad ? Math.max(limit * 5, 150) : limit;
2792
- const merged = lists.length === 1 ? lists[0].slice(0, rrfLimit) : rrfMergeMulti(lists, rrfLimit, RRF_K, weights);
2793
- const auto = config.scalingRoadmap?.rerankerAutoTrigger ?? {
2794
- enabled: config.rerankerEnabled ?? true,
2795
- broadQueryMinCardinality: 5e4,
2796
- fetchTopK: 150,
2797
- returnTopK: 5
2798
- };
2799
- let rerankedAndBlended = null;
2800
- if (effectiveIsBroad && auto.enabled) {
2801
- const cardinality2 = await estimateCardinality(agentId, effectiveOptions);
2802
- if (cardinality2 > auto.broadQueryMinCardinality) {
2803
- try {
2804
- const { isRerankerAvailable: isRerankerAvailable2, rerank: rerank2 } = await Promise.resolve().then(() => (init_reranker(), reranker_exports));
2805
- if (isRerankerAvailable2()) {
2806
- const reranked = await rerank2(effectiveQuery, merged, auto.returnTopK);
2807
- if (reranked.length > 0) {
2808
- rerankedAndBlended = rrfMergeMulti(
2809
- [reranked],
2810
- auto.returnTopK,
2811
- RRF_K
2812
- );
2813
- }
2814
- }
2815
- } catch {
2816
- }
2817
- }
2818
- }
2819
- const finalResults = (rerankedAndBlended ?? merged).slice(
2820
- 0,
2821
- rerankedAndBlended ? auto.returnTopK : limit
2822
- );
2823
- if (options?.includeSource && finalResults.length > 0) {
2824
- await attachDocumentMetadata(finalResults);
2825
- }
2826
- if (finalResults.length > 0) {
2827
- const now = (/* @__PURE__ */ new Date()).toISOString();
2828
- const ids = finalResults.map((r) => r.id);
2829
- const placeholders = ids.map(() => "?").join(",");
2830
- try {
2831
- const client = getClient();
2832
- void client.execute({
2833
- sql: `UPDATE memories SET last_accessed = ? WHERE id IN (${placeholders})`,
2834
- args: [now, ...ids]
2835
- }).catch(() => {
2836
- });
2837
- } catch {
2838
- }
2839
- }
2840
- return finalResults;
2841
- }
2842
- async function estimateCardinality(agentId, options) {
2843
- const client = getClient();
2844
- let sql = `SELECT COUNT(*) as cnt FROM memories
2845
- WHERE agent_id = ?
2846
- AND COALESCE(status, 'active') = 'active'
2847
- AND COALESCE(confidence, 0.7) >= 0.3`;
2848
- const args = [agentId];
2849
- if (options?.projectName) {
2850
- sql += ` AND project_name = ?`;
2851
- args.push(options.projectName);
2852
- }
2853
- if (options?.toolName) {
2854
- sql += ` AND tool_name = ?`;
2855
- args.push(options.toolName);
2856
- }
2857
- if (options?.hasError !== void 0) {
2858
- sql += ` AND has_error = ?`;
2859
- args.push(options.hasError ? 1 : 0);
2860
- }
2861
- if (options?.since) {
2862
- sql += ` AND timestamp >= ?`;
2863
- args.push(options.since);
2864
- }
2865
- try {
2866
- const result = await client.execute({ sql, args });
2867
- return Number(result.rows[0]?.cnt) || 0;
2868
- } catch {
2869
- return 0;
2870
- }
2871
- }
2872
- function recencyScore(timestamp) {
2873
- const daysSince = (Date.now() - new Date(timestamp).getTime()) / (1e3 * 60 * 60 * 24);
2874
- return 1 / (1 + daysSince * 0.01);
2875
- }
2876
- function normalizedImportance(importance) {
2877
- return ((importance ?? 5) - 1) / 9;
2878
- }
2879
- function frecencyBoost(lastAccessed, timestamp) {
2880
- const accessTime = lastAccessed ? new Date(lastAccessed).getTime() : new Date(timestamp).getTime();
2881
- const hoursSince = Math.max(0, (Date.now() - accessTime) / (1e3 * 60 * 60));
2882
- return Math.exp(-0.01 * hoursSince);
2883
- }
2884
- function rrfMergeMulti(lists, limit, k = RRF_K, weights) {
2885
- const scores = /* @__PURE__ */ new Map();
2886
- for (let listIdx = 0; listIdx < lists.length; listIdx++) {
2887
- const list = lists[listIdx];
2888
- const weight = weights?.[listIdx] ?? 1;
2889
- for (let i = 0; i < list.length; i++) {
2890
- const rec = list[i];
2891
- const entry = scores.get(rec.id) ?? { rrfScore: 0, record: rec };
2892
- entry.rrfScore += weight * (1 / (k + i + 1));
2893
- scores.set(rec.id, entry);
2894
- }
2895
- }
2896
- const entries = Array.from(scores.values()).map((e) => {
2897
- const recency = recencyScore(e.record.timestamp);
2898
- const importance = normalizedImportance(e.record.importance);
2899
- const confidence = e.record.confidence ?? 0.7;
2900
- const frecency = frecencyBoost(e.record.last_accessed, e.record.timestamp);
2901
- const baseScore = e.rrfScore * 0.35 + recency * 0.14 + importance * 0.21 + confidence * 0.3;
2902
- const finalScore = baseScore * (1 + 0.3 * frecency);
2903
- return { score: finalScore, record: e.record };
2904
- });
2905
- return entries.sort((a, b) => b.score - a.score).slice(0, limit).map((e) => e.record);
2906
- }
2907
- async function lightweightSearch(queryText, agentId, options) {
2908
- const client = getClient();
2909
- const limit = options?.limit ?? 5;
2910
- const terms = queryText.toLowerCase().split(/\s+/).filter((t) => t.length >= 3).map((t) => t.replace(/[^a-z0-9_]/g, "")).filter((t) => t.length >= 3);
2911
- if (terms.length === 0) {
2912
- return recentRecords(agentId, options, limit);
2913
- }
2914
- const prefixTerms = terms.map((t) => `${t}*`);
2915
- const useAnd = terms.length >= 3;
2916
- const matchExpr = useAnd ? prefixTerms.join(" AND ") : prefixTerms.join(" OR ");
2917
- const results = await ftsQuery(client, matchExpr, agentId, options, limit);
2918
- if (useAnd && results.length < limit) {
2919
- const orExpr = prefixTerms.join(" OR ");
2920
- const orResults = await ftsQuery(client, orExpr, agentId, options, limit);
2921
- const seen = new Set(results.map((r) => r.id));
2922
- for (const r of orResults) {
2923
- if (!seen.has(r.id) && results.length < limit) {
2924
- results.push(r);
2925
- seen.add(r.id);
2926
- }
2927
- }
2928
- }
2929
- if (options?.includeSource && results.length > 0) {
2930
- await attachDocumentMetadata(results);
2931
- }
2932
- return results;
2933
- }
2934
- async function ftsQuery(client, matchExpr, agentId, options, limit) {
2935
- const statusFilter = options?.includeArchived ? "" : `
2936
- AND COALESCE(m.status, 'active') = 'active'`;
2937
- let sql = `SELECT m.id, m.agent_id, m.agent_role, m.session_id, m.timestamp,
2938
- m.tool_name, m.project_name,
2939
- m.has_error, m.raw_text, m.vector, m.task_id,
2940
- m.importance, m.status, m.confidence, m.last_accessed,
2941
- m.workspace_id, m.document_id, m.user_id,
2942
- m.char_offset, m.page_number,
2943
- m.source_path, m.source_type
2944
- FROM memories m
2945
- JOIN memories_fts fts ON m.rowid = fts.rowid
2946
- WHERE memories_fts MATCH ?
2947
- AND m.agent_id = ?${statusFilter}
2948
- AND COALESCE(m.confidence, 0.7) >= 0.3`;
2949
- const args = [matchExpr, agentId];
2950
- const scope = buildWikiScopeFilter(options, "m.");
2951
- sql += scope.clause;
2952
- args.push(...scope.args);
2953
- if (options?.projectName) {
2954
- sql += ` AND m.project_name = ?`;
2955
- args.push(options.projectName);
2956
- }
2957
- if (options?.toolName) {
2958
- sql += ` AND m.tool_name = ?`;
2959
- args.push(options.toolName);
2960
- }
2961
- if (options?.hasError !== void 0) {
2962
- sql += ` AND m.has_error = ?`;
2963
- args.push(options.hasError ? 1 : 0);
2964
- }
2965
- if (options?.since) {
2966
- sql += ` AND m.timestamp >= ?`;
2967
- args.push(options.since);
2968
- }
2969
- sql += ` ORDER BY rank LIMIT ?`;
2970
- args.push(limit);
2971
- const result = await client.execute({ sql, args });
2972
- return result.rows.map((row) => ({
2973
- id: row.id,
2974
- agent_id: row.agent_id,
2975
- agent_role: row.agent_role,
2976
- session_id: row.session_id,
2977
- timestamp: row.timestamp,
2978
- tool_name: row.tool_name,
2979
- project_name: row.project_name,
2980
- has_error: row.has_error === 1,
2981
- raw_text: row.raw_text,
2982
- vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
2983
- task_id: row.task_id ?? null,
2984
- importance: row.importance ?? 5,
2985
- status: row.status ?? "active",
2986
- confidence: row.confidence ?? 0.7,
2987
- last_accessed: row.last_accessed ?? row.timestamp,
2988
- workspace_id: row.workspace_id ?? null,
2989
- document_id: row.document_id ?? null,
2990
- user_id: row.user_id ?? null,
2991
- char_offset: row.char_offset ?? null,
2992
- page_number: row.page_number ?? null,
2993
- source_path: row.source_path ?? null,
2994
- source_type: row.source_type ?? null
2995
- }));
2996
- }
2997
- async function recentRecords(agentId, options, limit) {
2998
- const client = getClient();
2999
- const statusFilter = options?.includeArchived ? "" : `
3000
- AND COALESCE(status, 'active') = 'active'`;
3001
- let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
3002
- tool_name, project_name,
3003
- has_error, raw_text, vector, task_id,
3004
- importance, status, confidence, last_accessed,
3005
- workspace_id, document_id, user_id,
3006
- char_offset, page_number,
3007
- source_path, source_type
3008
- FROM memories
3009
- WHERE agent_id = ?${statusFilter}
3010
- AND COALESCE(confidence, 0.7) >= 0.3`;
3011
- const args = [agentId];
3012
- const scope = buildWikiScopeFilter(options, "");
3013
- sql += scope.clause;
3014
- args.push(...scope.args);
3015
- if (options?.projectName) {
3016
- sql += ` AND project_name = ?`;
3017
- args.push(options.projectName);
3018
- }
3019
- if (options?.toolName) {
3020
- sql += ` AND tool_name = ?`;
3021
- args.push(options.toolName);
3022
- }
3023
- if (options?.hasError !== void 0) {
3024
- sql += ` AND has_error = ?`;
3025
- args.push(options.hasError ? 1 : 0);
3026
- }
3027
- if (options?.since) {
3028
- sql += ` AND timestamp >= ?`;
3029
- args.push(options.since);
3030
- }
3031
- sql += ` ORDER BY timestamp DESC LIMIT ?`;
3032
- args.push(limit);
3033
- const result = await client.execute({ sql, args });
3034
- return result.rows.map((row) => ({
3035
- id: row.id,
3036
- agent_id: row.agent_id,
3037
- agent_role: row.agent_role,
3038
- session_id: row.session_id,
3039
- timestamp: row.timestamp,
3040
- tool_name: row.tool_name,
3041
- project_name: row.project_name,
3042
- has_error: row.has_error === 1,
3043
- raw_text: row.raw_text,
3044
- vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
3045
- task_id: row.task_id ?? null,
3046
- importance: row.importance ?? 5,
3047
- status: row.status ?? "active",
3048
- confidence: row.confidence ?? 0.7,
3049
- last_accessed: row.last_accessed ?? row.timestamp,
3050
- workspace_id: row.workspace_id ?? null,
3051
- document_id: row.document_id ?? null,
3052
- user_id: row.user_id ?? null,
3053
- char_offset: row.char_offset ?? null,
3054
- page_number: row.page_number ?? null,
3055
- source_path: row.source_path ?? null,
3056
- source_type: row.source_type ?? null
3057
- }));
3058
- }
3059
-
3060
- // src/bin/exe-search.ts
3061
- init_config();
3062
-
3063
- // src/lib/is-main.ts
3064
- import { realpathSync } from "fs";
3065
- import { fileURLToPath as fileURLToPath2 } from "url";
3066
- function isMainModule(importMetaUrl) {
3067
- if (process.argv[1] == null) return false;
3068
- try {
3069
- const scriptPath = realpathSync(process.argv[1]);
3070
- const modulePath = realpathSync(fileURLToPath2(importMetaUrl));
3071
- return scriptPath === modulePath;
3072
- } catch {
3073
- return importMetaUrl === `file://${process.argv[1]}` || importMetaUrl === new URL(process.argv[1], "file://").href;
3074
- }
3075
- }
3076
-
3077
- // src/bin/exe-search.ts
3078
- async function main() {
3079
- const query = process.argv.slice(2).join(" ");
3080
- if (!query) {
3081
- console.error('Usage: exe-search <query>\nExample: exe-search "auth bug JWT"');
3082
- process.exit(1);
3083
- }
3084
- const agentId = process.env.AGENT_ID;
3085
- if (!agentId) {
3086
- console.error("No AGENT_ID set. Run this inside an employee session (/exe-call <name>).");
3087
- process.exit(1);
3088
- }
3089
- await initStore();
3090
- const config = await loadConfig();
3091
- const search = config.searchMode === "hybrid" ? hybridSearch : lightweightSearch;
3092
- const memories = await search(query, agentId, { limit: 10 });
3093
- if (memories.length === 0) {
3094
- console.log("No memories found.");
3095
- return;
3096
- }
3097
- console.log(`Found ${memories.length} memories:
3098
- `);
3099
- for (const m of memories) {
3100
- const errorTag = m.has_error ? " [ERROR]" : "";
3101
- const text = m.raw_text.length > 300 ? m.raw_text.slice(0, 300) + "..." : m.raw_text;
3102
- console.log(`[${m.timestamp}] ${m.tool_name} (${m.project_name})${errorTag}`);
3103
- console.log(` ${text}`);
3104
- console.log(` ID: ${m.id}`);
3105
- console.log("");
3106
- }
3107
- }
3108
- if (isMainModule(import.meta.url)) {
3109
- main().catch((err) => {
3110
- console.error(err instanceof Error ? err.message : String(err));
3111
- process.exit(1);
3112
- });
3113
- }
3114
- export {
3115
- main
3116
- };