@askexenow/exe-os 0.8.0 → 0.8.1

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 (90) hide show
  1. package/README.md +178 -79
  2. package/dist/bin/backfill-responses.js +160 -8
  3. package/dist/bin/backfill-vectors.js +130 -1
  4. package/dist/bin/cleanup-stale-review-tasks.js +130 -1
  5. package/dist/bin/cli.js +10111 -7540
  6. package/dist/bin/exe-agent.js +159 -1
  7. package/dist/bin/exe-assign.js +235 -16
  8. package/dist/bin/exe-boot.js +344 -472
  9. package/dist/bin/exe-call.js +145 -1
  10. package/dist/bin/exe-cloud.js +11 -0
  11. package/dist/bin/exe-dispatch.js +37 -24
  12. package/dist/bin/exe-doctor.js +130 -1
  13. package/dist/bin/exe-export-behaviors.js +150 -7
  14. package/dist/bin/exe-forget.js +822 -665
  15. package/dist/bin/exe-gateway.js +470 -62
  16. package/dist/bin/exe-heartbeat.js +133 -2
  17. package/dist/bin/exe-kill.js +150 -7
  18. package/dist/bin/exe-launch-agent.js +150 -7
  19. package/dist/bin/exe-new-employee.js +756 -224
  20. package/dist/bin/exe-pending-messages.js +132 -2
  21. package/dist/bin/exe-pending-notifications.js +130 -1
  22. package/dist/bin/exe-pending-reviews.js +132 -2
  23. package/dist/bin/exe-review.js +160 -8
  24. package/dist/bin/exe-search.js +2473 -2008
  25. package/dist/bin/exe-session-cleanup.js +238 -51
  26. package/dist/bin/exe-settings.js +11 -0
  27. package/dist/bin/exe-status.js +130 -1
  28. package/dist/bin/exe-team.js +130 -1
  29. package/dist/bin/git-sweep.js +272 -16
  30. package/dist/bin/graph-backfill.js +150 -7
  31. package/dist/bin/graph-export.js +150 -7
  32. package/dist/bin/install.js +5 -0
  33. package/dist/bin/scan-tasks.js +238 -19
  34. package/dist/bin/setup.js +1776 -10
  35. package/dist/bin/shard-migrate.js +150 -7
  36. package/dist/bin/update.js +9 -6
  37. package/dist/bin/wiki-sync.js +150 -7
  38. package/dist/gateway/index.js +470 -62
  39. package/dist/hooks/bug-report-worker.js +195 -35
  40. package/dist/hooks/commit-complete.js +272 -16
  41. package/dist/hooks/error-recall.js +2313 -1847
  42. package/dist/hooks/exe-heartbeat-hook.js +5 -0
  43. package/dist/hooks/ingest-worker.js +330 -58
  44. package/dist/hooks/ingest.js +11 -0
  45. package/dist/hooks/instructions-loaded.js +199 -10
  46. package/dist/hooks/notification.js +199 -10
  47. package/dist/hooks/post-compact.js +199 -10
  48. package/dist/hooks/pre-compact.js +199 -10
  49. package/dist/hooks/pre-tool-use.js +199 -10
  50. package/dist/hooks/prompt-ingest-worker.js +179 -14
  51. package/dist/hooks/prompt-submit.js +781 -285
  52. package/dist/hooks/response-ingest-worker.js +1900 -1405
  53. package/dist/hooks/session-end.js +456 -12
  54. package/dist/hooks/session-start.js +2188 -1724
  55. package/dist/hooks/stop.js +200 -10
  56. package/dist/hooks/subagent-stop.js +199 -10
  57. package/dist/hooks/summary-worker.js +604 -334
  58. package/dist/index.js +554 -61
  59. package/dist/lib/cloud-sync.js +5 -0
  60. package/dist/lib/config.js +13 -0
  61. package/dist/lib/consolidation.js +5 -0
  62. package/dist/lib/database.js +104 -0
  63. package/dist/lib/device-registry.js +109 -0
  64. package/dist/lib/embedder.js +13 -0
  65. package/dist/lib/employee-templates.js +53 -26
  66. package/dist/lib/employees.js +5 -0
  67. package/dist/lib/exe-daemon-client.js +5 -0
  68. package/dist/lib/exe-daemon.js +493 -79
  69. package/dist/lib/file-grep.js +20 -4
  70. package/dist/lib/hybrid-search.js +1435 -190
  71. package/dist/lib/identity-templates.js +126 -5
  72. package/dist/lib/identity.js +5 -0
  73. package/dist/lib/license.js +5 -0
  74. package/dist/lib/messaging.js +37 -24
  75. package/dist/lib/schedules.js +130 -1
  76. package/dist/lib/skill-learning.js +11 -0
  77. package/dist/lib/status-brief.js +5 -0
  78. package/dist/lib/store.js +199 -10
  79. package/dist/lib/task-router.js +72 -6
  80. package/dist/lib/tasks.js +179 -50
  81. package/dist/lib/tmux-routing.js +179 -46
  82. package/dist/mcp/server.js +2129 -1855
  83. package/dist/mcp/tools/create-task.js +86 -36
  84. package/dist/mcp/tools/deactivate-behavior.js +5 -0
  85. package/dist/mcp/tools/list-tasks.js +39 -11
  86. package/dist/mcp/tools/send-message.js +37 -24
  87. package/dist/mcp/tools/update-task.js +153 -38
  88. package/dist/runtime/index.js +451 -59
  89. package/dist/tui/App.js +454 -59
  90. package/package.json +1 -1
@@ -23,6 +23,884 @@ var init_memory = __esm({
23
23
  }
24
24
  });
25
25
 
26
+ // src/lib/database.ts
27
+ import { createClient } from "@libsql/client";
28
+ async function initDatabase(config) {
29
+ if (_client) {
30
+ _client.close();
31
+ _client = null;
32
+ }
33
+ const opts = {
34
+ url: `file:${config.dbPath}`
35
+ };
36
+ if (config.encryptionKey) {
37
+ opts.encryptionKey = config.encryptionKey;
38
+ }
39
+ _client = createClient(opts);
40
+ }
41
+ function getClient() {
42
+ if (!_client) {
43
+ throw new Error("Database client not initialized. Call initDatabase() first.");
44
+ }
45
+ return _client;
46
+ }
47
+ async function ensureSchema() {
48
+ const client = getClient();
49
+ await client.execute("PRAGMA journal_mode = WAL");
50
+ await client.execute("PRAGMA busy_timeout = 5000");
51
+ try {
52
+ await client.execute("PRAGMA libsql_vector_search_ef = 128");
53
+ } catch {
54
+ }
55
+ await client.executeMultiple(`
56
+ CREATE TABLE IF NOT EXISTS memories (
57
+ id TEXT PRIMARY KEY,
58
+ agent_id TEXT NOT NULL,
59
+ agent_role TEXT NOT NULL,
60
+ session_id TEXT NOT NULL,
61
+ timestamp TEXT NOT NULL,
62
+ tool_name TEXT NOT NULL,
63
+ project_name TEXT NOT NULL,
64
+ has_error INTEGER NOT NULL DEFAULT 0,
65
+ raw_text TEXT NOT NULL,
66
+ vector F32_BLOB(1024),
67
+ version INTEGER NOT NULL DEFAULT 0
68
+ );
69
+
70
+ CREATE INDEX IF NOT EXISTS idx_memories_agent
71
+ ON memories(agent_id);
72
+
73
+ CREATE INDEX IF NOT EXISTS idx_memories_timestamp
74
+ ON memories(timestamp);
75
+
76
+ CREATE INDEX IF NOT EXISTS idx_memories_session
77
+ ON memories(session_id);
78
+
79
+ CREATE INDEX IF NOT EXISTS idx_memories_project
80
+ ON memories(project_name);
81
+
82
+ CREATE INDEX IF NOT EXISTS idx_memories_tool
83
+ ON memories(tool_name);
84
+
85
+ CREATE INDEX IF NOT EXISTS idx_memories_version
86
+ ON memories(version);
87
+
88
+ CREATE INDEX IF NOT EXISTS idx_memories_agent_project
89
+ ON memories(agent_id, project_name);
90
+ `);
91
+ await client.executeMultiple(`
92
+ CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
93
+ raw_text,
94
+ content='memories',
95
+ content_rowid='rowid'
96
+ );
97
+
98
+ CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
99
+ INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
100
+ END;
101
+
102
+ CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
103
+ INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
104
+ END;
105
+
106
+ CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
107
+ INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
108
+ INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
109
+ END;
110
+ `);
111
+ await client.executeMultiple(`
112
+ CREATE TABLE IF NOT EXISTS sync_meta (
113
+ key TEXT PRIMARY KEY,
114
+ value TEXT NOT NULL
115
+ );
116
+ `);
117
+ await client.executeMultiple(`
118
+ CREATE TABLE IF NOT EXISTS tasks (
119
+ id TEXT PRIMARY KEY,
120
+ title TEXT NOT NULL,
121
+ assigned_to TEXT NOT NULL,
122
+ assigned_by TEXT NOT NULL,
123
+ project_name TEXT NOT NULL,
124
+ priority TEXT NOT NULL DEFAULT 'p1',
125
+ status TEXT NOT NULL DEFAULT 'open',
126
+ task_file TEXT,
127
+ created_at TEXT NOT NULL,
128
+ updated_at TEXT NOT NULL
129
+ );
130
+
131
+ CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
132
+ ON tasks(assigned_to, status);
133
+ `);
134
+ await client.executeMultiple(`
135
+ CREATE TABLE IF NOT EXISTS behaviors (
136
+ id TEXT PRIMARY KEY,
137
+ agent_id TEXT NOT NULL,
138
+ project_name TEXT,
139
+ domain TEXT,
140
+ content TEXT NOT NULL,
141
+ active INTEGER NOT NULL DEFAULT 1,
142
+ created_at TEXT NOT NULL,
143
+ updated_at TEXT NOT NULL
144
+ );
145
+
146
+ CREATE INDEX IF NOT EXISTS idx_behaviors_agent
147
+ ON behaviors(agent_id, active);
148
+ `);
149
+ try {
150
+ const existing = await client.execute({
151
+ sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
152
+ args: []
153
+ });
154
+ if (Number(existing.rows[0]?.cnt) === 0) {
155
+ await client.executeMultiple(`
156
+ INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
157
+ VALUES
158
+ (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');
159
+ INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
160
+ VALUES
161
+ (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');
162
+ INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
163
+ VALUES
164
+ (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');
165
+ `);
166
+ }
167
+ } catch {
168
+ }
169
+ try {
170
+ await client.execute({
171
+ sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
172
+ args: []
173
+ });
174
+ } catch {
175
+ }
176
+ try {
177
+ await client.execute({
178
+ sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
179
+ args: []
180
+ });
181
+ } catch {
182
+ }
183
+ try {
184
+ await client.execute({
185
+ sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
186
+ args: []
187
+ });
188
+ } catch {
189
+ }
190
+ try {
191
+ await client.execute({
192
+ sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
193
+ ON tasks(parent_task_id)
194
+ WHERE parent_task_id IS NOT NULL`,
195
+ args: []
196
+ });
197
+ } catch {
198
+ }
199
+ try {
200
+ await client.execute({
201
+ sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
202
+ args: []
203
+ });
204
+ } catch {
205
+ }
206
+ try {
207
+ await client.execute({
208
+ sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
209
+ args: []
210
+ });
211
+ } catch {
212
+ }
213
+ try {
214
+ await client.execute({
215
+ sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
216
+ args: []
217
+ });
218
+ } catch {
219
+ }
220
+ try {
221
+ await client.execute({
222
+ sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
223
+ args: []
224
+ });
225
+ } catch {
226
+ }
227
+ try {
228
+ await client.execute({
229
+ sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
230
+ args: []
231
+ });
232
+ } catch {
233
+ }
234
+ try {
235
+ await client.execute({
236
+ sql: `ALTER TABLE tasks ADD COLUMN checkpoint TEXT`,
237
+ args: []
238
+ });
239
+ } catch {
240
+ }
241
+ try {
242
+ await client.execute({
243
+ sql: `ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER NOT NULL DEFAULT 0`,
244
+ args: []
245
+ });
246
+ } catch {
247
+ }
248
+ try {
249
+ await client.execute({
250
+ sql: `ALTER TABLE tasks ADD COLUMN complexity TEXT NOT NULL DEFAULT 'standard'`,
251
+ args: []
252
+ });
253
+ } catch {
254
+ }
255
+ try {
256
+ await client.execute({
257
+ sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
258
+ args: []
259
+ });
260
+ } catch {
261
+ }
262
+ try {
263
+ await client.execute({
264
+ sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
265
+ args: []
266
+ });
267
+ } catch {
268
+ }
269
+ try {
270
+ await client.execute({
271
+ sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
272
+ args: []
273
+ });
274
+ } catch {
275
+ }
276
+ try {
277
+ await client.execute({
278
+ sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
279
+ args: []
280
+ });
281
+ } catch {
282
+ }
283
+ await client.executeMultiple(`
284
+ CREATE TABLE IF NOT EXISTS consolidations (
285
+ id TEXT PRIMARY KEY,
286
+ consolidated_memory_id TEXT NOT NULL,
287
+ source_memory_id TEXT NOT NULL,
288
+ created_at TEXT NOT NULL
289
+ );
290
+
291
+ CREATE INDEX IF NOT EXISTS idx_consolidations_source
292
+ ON consolidations(source_memory_id);
293
+
294
+ CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
295
+ ON consolidations(consolidated_memory_id);
296
+ `);
297
+ await client.executeMultiple(`
298
+ CREATE TABLE IF NOT EXISTS reminders (
299
+ id TEXT PRIMARY KEY,
300
+ text TEXT NOT NULL,
301
+ created_at TEXT NOT NULL,
302
+ due_date TEXT,
303
+ completed_at TEXT
304
+ );
305
+ `);
306
+ await client.executeMultiple(`
307
+ CREATE TABLE IF NOT EXISTS notifications (
308
+ id TEXT PRIMARY KEY,
309
+ agent_id TEXT NOT NULL,
310
+ agent_role TEXT NOT NULL,
311
+ event TEXT NOT NULL,
312
+ project TEXT NOT NULL,
313
+ summary TEXT NOT NULL,
314
+ task_file TEXT,
315
+ read INTEGER NOT NULL DEFAULT 0,
316
+ created_at TEXT NOT NULL
317
+ );
318
+
319
+ CREATE INDEX IF NOT EXISTS idx_notifications_read
320
+ ON notifications(read);
321
+
322
+ CREATE INDEX IF NOT EXISTS idx_notifications_agent
323
+ ON notifications(agent_id);
324
+
325
+ CREATE INDEX IF NOT EXISTS idx_notifications_task_file
326
+ ON notifications(task_file);
327
+ `);
328
+ await client.executeMultiple(`
329
+ CREATE TABLE IF NOT EXISTS schedules (
330
+ id TEXT PRIMARY KEY,
331
+ cron TEXT NOT NULL,
332
+ description TEXT NOT NULL,
333
+ job_type TEXT NOT NULL DEFAULT 'report',
334
+ prompt TEXT,
335
+ assigned_to TEXT,
336
+ project_name TEXT,
337
+ active INTEGER NOT NULL DEFAULT 1,
338
+ use_crontab INTEGER NOT NULL DEFAULT 0,
339
+ created_at TEXT NOT NULL
340
+ );
341
+ `);
342
+ await client.executeMultiple(`
343
+ CREATE TABLE IF NOT EXISTS device_registry (
344
+ device_id TEXT PRIMARY KEY,
345
+ friendly_name TEXT NOT NULL,
346
+ hostname TEXT NOT NULL,
347
+ projects TEXT NOT NULL DEFAULT '[]',
348
+ agents TEXT NOT NULL DEFAULT '[]',
349
+ connected INTEGER DEFAULT 0,
350
+ last_seen TEXT NOT NULL
351
+ );
352
+ `);
353
+ await client.executeMultiple(`
354
+ CREATE TABLE IF NOT EXISTS messages (
355
+ id TEXT PRIMARY KEY,
356
+ from_agent TEXT NOT NULL,
357
+ from_device TEXT NOT NULL DEFAULT 'local',
358
+ target_agent TEXT NOT NULL,
359
+ target_project TEXT,
360
+ target_device TEXT NOT NULL DEFAULT 'local',
361
+ content TEXT NOT NULL,
362
+ priority TEXT DEFAULT 'normal',
363
+ status TEXT DEFAULT 'pending',
364
+ server_seq INTEGER,
365
+ retry_count INTEGER DEFAULT 0,
366
+ created_at TEXT NOT NULL,
367
+ delivered_at TEXT,
368
+ processed_at TEXT,
369
+ failed_at TEXT,
370
+ failure_reason TEXT
371
+ );
372
+
373
+ CREATE INDEX IF NOT EXISTS idx_messages_target
374
+ ON messages(target_agent, status);
375
+
376
+ CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
377
+ ON messages(target_agent, from_agent, server_seq);
378
+ `);
379
+ try {
380
+ await client.execute({
381
+ sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
382
+ args: []
383
+ });
384
+ await client.execute({
385
+ sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
386
+ args: []
387
+ });
388
+ await client.execute({
389
+ sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
390
+ args: []
391
+ });
392
+ await client.execute({
393
+ sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
394
+ args: []
395
+ });
396
+ } catch {
397
+ }
398
+ await client.executeMultiple(`
399
+ CREATE TABLE IF NOT EXISTS trajectories (
400
+ id TEXT PRIMARY KEY,
401
+ task_id TEXT NOT NULL,
402
+ agent_id TEXT NOT NULL,
403
+ project_name TEXT NOT NULL,
404
+ task_title TEXT NOT NULL,
405
+ signature TEXT NOT NULL,
406
+ signature_hash TEXT NOT NULL,
407
+ tool_count INTEGER NOT NULL,
408
+ skill_id TEXT,
409
+ created_at TEXT NOT NULL
410
+ );
411
+
412
+ CREATE INDEX IF NOT EXISTS idx_trajectories_hash
413
+ ON trajectories(signature_hash);
414
+
415
+ CREATE INDEX IF NOT EXISTS idx_trajectories_agent
416
+ ON trajectories(agent_id);
417
+ `);
418
+ try {
419
+ await client.execute("ALTER TABLE trajectories ADD COLUMN skill_id TEXT");
420
+ } catch {
421
+ }
422
+ await client.executeMultiple(`
423
+ CREATE TABLE IF NOT EXISTS consolidations (
424
+ id TEXT PRIMARY KEY,
425
+ consolidated_memory_id TEXT NOT NULL,
426
+ source_memory_id TEXT NOT NULL,
427
+ created_at TEXT NOT NULL
428
+ );
429
+
430
+ CREATE INDEX IF NOT EXISTS idx_consolidations_source
431
+ ON consolidations(source_memory_id);
432
+ `);
433
+ await client.executeMultiple(`
434
+ CREATE TABLE IF NOT EXISTS audit_trail (
435
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
436
+ timestamp TEXT NOT NULL,
437
+ session_id TEXT NOT NULL,
438
+ agent_id TEXT NOT NULL,
439
+ tool TEXT NOT NULL,
440
+ input TEXT,
441
+ decision TEXT NOT NULL,
442
+ reason TEXT,
443
+ is_customer_facing INTEGER NOT NULL DEFAULT 0
444
+ );
445
+
446
+ CREATE INDEX IF NOT EXISTS idx_audit_trail_agent
447
+ ON audit_trail(agent_id, timestamp);
448
+
449
+ CREATE INDEX IF NOT EXISTS idx_audit_trail_session
450
+ ON audit_trail(session_id);
451
+ `);
452
+ try {
453
+ await client.execute({
454
+ sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
455
+ args: []
456
+ });
457
+ } catch {
458
+ }
459
+ try {
460
+ await client.execute({
461
+ sql: `ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5`,
462
+ args: []
463
+ });
464
+ } catch {
465
+ }
466
+ try {
467
+ await client.execute({
468
+ sql: `ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'`,
469
+ args: []
470
+ });
471
+ } catch {
472
+ }
473
+ try {
474
+ await client.execute({
475
+ sql: `ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7`,
476
+ args: []
477
+ });
478
+ } catch {
479
+ }
480
+ try {
481
+ await client.execute({
482
+ sql: `ALTER TABLE memories ADD COLUMN last_accessed TEXT`,
483
+ args: []
484
+ });
485
+ } catch {
486
+ }
487
+ try {
488
+ await client.execute({
489
+ sql: `UPDATE memories SET last_accessed = timestamp WHERE last_accessed IS NULL`,
490
+ args: []
491
+ });
492
+ } catch {
493
+ }
494
+ try {
495
+ await client.execute({
496
+ sql: `ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0`,
497
+ args: []
498
+ });
499
+ } catch {
500
+ }
501
+ try {
502
+ await client.execute({
503
+ sql: `ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0`,
504
+ args: []
505
+ });
506
+ } catch {
507
+ }
508
+ for (const col of [
509
+ "ALTER TABLE memories ADD COLUMN content_hash TEXT",
510
+ "ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT"
511
+ ]) {
512
+ try {
513
+ await client.execute(col);
514
+ } catch {
515
+ }
516
+ }
517
+ await client.executeMultiple(`
518
+ CREATE TABLE IF NOT EXISTS entities (
519
+ id TEXT PRIMARY KEY,
520
+ name TEXT NOT NULL,
521
+ type TEXT NOT NULL,
522
+ first_seen TEXT NOT NULL,
523
+ last_seen TEXT NOT NULL,
524
+ properties TEXT DEFAULT '{}',
525
+ UNIQUE(name, type)
526
+ );
527
+
528
+ CREATE TABLE IF NOT EXISTS relationships (
529
+ id TEXT PRIMARY KEY,
530
+ source_entity_id TEXT NOT NULL,
531
+ target_entity_id TEXT NOT NULL,
532
+ type TEXT NOT NULL,
533
+ weight REAL DEFAULT 1.0,
534
+ timestamp TEXT NOT NULL,
535
+ properties TEXT DEFAULT '{}',
536
+ UNIQUE(source_entity_id, target_entity_id, type)
537
+ );
538
+
539
+ CREATE TABLE IF NOT EXISTS entity_memories (
540
+ entity_id TEXT NOT NULL,
541
+ memory_id TEXT NOT NULL,
542
+ PRIMARY KEY (entity_id, memory_id)
543
+ );
544
+
545
+ CREATE TABLE IF NOT EXISTS relationship_memories (
546
+ relationship_id TEXT NOT NULL,
547
+ memory_id TEXT NOT NULL,
548
+ PRIMARY KEY (relationship_id, memory_id)
549
+ );
550
+
551
+ CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
552
+ CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
553
+ CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
554
+ CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
555
+
556
+ CREATE TABLE IF NOT EXISTS hyperedges (
557
+ id TEXT PRIMARY KEY,
558
+ label TEXT NOT NULL,
559
+ relation TEXT NOT NULL,
560
+ confidence REAL DEFAULT 1.0,
561
+ timestamp TEXT NOT NULL
562
+ );
563
+
564
+ CREATE TABLE IF NOT EXISTS hyperedge_nodes (
565
+ hyperedge_id TEXT NOT NULL,
566
+ entity_id TEXT NOT NULL,
567
+ PRIMARY KEY (hyperedge_id, entity_id)
568
+ );
569
+ `);
570
+ await client.executeMultiple(`
571
+ CREATE TABLE IF NOT EXISTS entity_aliases (
572
+ alias TEXT NOT NULL PRIMARY KEY,
573
+ canonical_entity_id TEXT NOT NULL
574
+ );
575
+ CREATE INDEX IF NOT EXISTS idx_entity_aliases_canonical ON entity_aliases(canonical_entity_id);
576
+ `);
577
+ for (const col of [
578
+ "ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
579
+ "ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
580
+ ]) {
581
+ try {
582
+ await client.execute(col);
583
+ } catch {
584
+ }
585
+ }
586
+ try {
587
+ await client.execute(
588
+ `CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)`
589
+ );
590
+ } catch {
591
+ }
592
+ await client.executeMultiple(`
593
+ CREATE TABLE IF NOT EXISTS identity (
594
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
595
+ agent_id TEXT NOT NULL UNIQUE,
596
+ content_hash TEXT NOT NULL,
597
+ updated_at TEXT NOT NULL,
598
+ updated_by TEXT NOT NULL
599
+ );
600
+
601
+ CREATE INDEX IF NOT EXISTS idx_identity_agent ON identity(agent_id);
602
+ `);
603
+ await client.executeMultiple(`
604
+ CREATE TABLE IF NOT EXISTS chat_history (
605
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
606
+ session_id TEXT NOT NULL,
607
+ role TEXT NOT NULL,
608
+ content TEXT NOT NULL,
609
+ tool_name TEXT,
610
+ tool_id TEXT,
611
+ is_error INTEGER NOT NULL DEFAULT 0,
612
+ timestamp INTEGER NOT NULL
613
+ );
614
+
615
+ CREATE INDEX IF NOT EXISTS idx_chat_history_session
616
+ ON chat_history(session_id, id);
617
+ `);
618
+ await client.executeMultiple(`
619
+ CREATE TABLE IF NOT EXISTS workspaces (
620
+ id TEXT PRIMARY KEY,
621
+ slug TEXT NOT NULL UNIQUE,
622
+ name TEXT NOT NULL,
623
+ owner_agent_id TEXT,
624
+ created_at TEXT NOT NULL,
625
+ metadata TEXT
626
+ );
627
+
628
+ CREATE INDEX IF NOT EXISTS idx_workspaces_slug
629
+ ON workspaces(slug);
630
+ `);
631
+ await client.executeMultiple(`
632
+ CREATE TABLE IF NOT EXISTS documents (
633
+ id TEXT PRIMARY KEY,
634
+ workspace_id TEXT NOT NULL,
635
+ filename TEXT NOT NULL,
636
+ mime TEXT,
637
+ source_type TEXT,
638
+ user_id TEXT,
639
+ uploaded_at TEXT NOT NULL,
640
+ metadata TEXT,
641
+ FOREIGN KEY (workspace_id) REFERENCES workspaces(id)
642
+ );
643
+
644
+ CREATE INDEX IF NOT EXISTS idx_documents_workspace
645
+ ON documents(workspace_id);
646
+
647
+ CREATE INDEX IF NOT EXISTS idx_documents_user
648
+ ON documents(user_id);
649
+ `);
650
+ for (const column of [
651
+ "workspace_id TEXT",
652
+ "document_id TEXT",
653
+ "user_id TEXT",
654
+ "char_offset INTEGER",
655
+ "page_number INTEGER"
656
+ ]) {
657
+ try {
658
+ await client.execute({
659
+ sql: `ALTER TABLE memories ADD COLUMN ${column}`,
660
+ args: []
661
+ });
662
+ } catch {
663
+ }
664
+ }
665
+ for (const col of [
666
+ "ALTER TABLE memories ADD COLUMN source_path TEXT",
667
+ "ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'"
668
+ ]) {
669
+ try {
670
+ await client.execute(col);
671
+ } catch {
672
+ }
673
+ }
674
+ await client.executeMultiple(`
675
+ CREATE INDEX IF NOT EXISTS idx_memories_workspace
676
+ ON memories(workspace_id);
677
+
678
+ CREATE INDEX IF NOT EXISTS idx_memories_document
679
+ ON memories(document_id);
680
+
681
+ CREATE INDEX IF NOT EXISTS idx_memories_user
682
+ ON memories(user_id);
683
+ `);
684
+ await client.executeMultiple(`
685
+ CREATE TABLE IF NOT EXISTS session_kills (
686
+ id TEXT PRIMARY KEY,
687
+ session_name TEXT NOT NULL,
688
+ agent_id TEXT NOT NULL,
689
+ killed_at TIMESTAMP NOT NULL,
690
+ reason TEXT NOT NULL,
691
+ ticks_idle INTEGER,
692
+ estimated_tokens_saved INTEGER
693
+ );
694
+
695
+ CREATE INDEX IF NOT EXISTS idx_session_kills_killed_at
696
+ ON session_kills(killed_at);
697
+
698
+ CREATE INDEX IF NOT EXISTS idx_session_kills_agent
699
+ ON session_kills(agent_id);
700
+ `);
701
+ await client.executeMultiple(`
702
+ CREATE TABLE IF NOT EXISTS conversations (
703
+ id TEXT PRIMARY KEY,
704
+ platform TEXT NOT NULL,
705
+ external_id TEXT,
706
+ sender_id TEXT NOT NULL,
707
+ sender_name TEXT,
708
+ sender_phone TEXT,
709
+ sender_email TEXT,
710
+ recipient_id TEXT,
711
+ channel_id TEXT NOT NULL,
712
+ thread_id TEXT,
713
+ reply_to_id TEXT,
714
+ content_text TEXT,
715
+ content_media TEXT,
716
+ content_metadata TEXT,
717
+ agent_response TEXT,
718
+ agent_name TEXT,
719
+ timestamp TEXT NOT NULL,
720
+ ingested_at TEXT NOT NULL
721
+ );
722
+
723
+ CREATE INDEX IF NOT EXISTS idx_conversations_platform
724
+ ON conversations(platform);
725
+
726
+ CREATE INDEX IF NOT EXISTS idx_conversations_sender
727
+ ON conversations(sender_id);
728
+
729
+ CREATE INDEX IF NOT EXISTS idx_conversations_timestamp
730
+ ON conversations(timestamp);
731
+
732
+ CREATE INDEX IF NOT EXISTS idx_conversations_thread
733
+ ON conversations(thread_id);
734
+
735
+ CREATE INDEX IF NOT EXISTS idx_conversations_channel
736
+ ON conversations(channel_id);
737
+ `);
738
+ try {
739
+ await client.execute({
740
+ sql: `ALTER TABLE tasks ADD COLUMN budget_tokens INTEGER`,
741
+ args: []
742
+ });
743
+ } catch {
744
+ }
745
+ try {
746
+ await client.execute({
747
+ sql: `ALTER TABLE tasks ADD COLUMN budget_fallback_model TEXT`,
748
+ args: []
749
+ });
750
+ } catch {
751
+ }
752
+ try {
753
+ await client.execute({
754
+ sql: `ALTER TABLE tasks ADD COLUMN tokens_used INTEGER DEFAULT 0`,
755
+ args: []
756
+ });
757
+ } catch {
758
+ }
759
+ try {
760
+ await client.execute({
761
+ sql: `ALTER TABLE tasks ADD COLUMN tokens_warned_at INTEGER`,
762
+ args: []
763
+ });
764
+ } catch {
765
+ }
766
+ await client.executeMultiple(`
767
+ CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
768
+ content_text,
769
+ sender_name,
770
+ agent_response,
771
+ content='conversations',
772
+ content_rowid='rowid'
773
+ );
774
+
775
+ CREATE TRIGGER IF NOT EXISTS conversations_fts_ai AFTER INSERT ON conversations BEGIN
776
+ INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
777
+ VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
778
+ END;
779
+
780
+ CREATE TRIGGER IF NOT EXISTS conversations_fts_ad AFTER DELETE ON conversations BEGIN
781
+ INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
782
+ VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
783
+ END;
784
+
785
+ CREATE TRIGGER IF NOT EXISTS conversations_fts_au AFTER UPDATE ON conversations BEGIN
786
+ INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
787
+ VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
788
+ INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
789
+ VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
790
+ END;
791
+ `);
792
+ try {
793
+ await client.execute({
794
+ sql: `ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3`,
795
+ args: []
796
+ });
797
+ } catch {
798
+ }
799
+ try {
800
+ await client.execute(
801
+ `CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)`
802
+ );
803
+ } catch {
804
+ }
805
+ try {
806
+ await client.execute({
807
+ sql: `UPDATE memories SET tier = 1 WHERE tool_name = 'commit_to_long_term_memory' AND importance >= 8 AND tier = 3`,
808
+ args: []
809
+ });
810
+ await client.execute({
811
+ sql: `UPDATE memories SET tier = 2 WHERE tool_name IN ('store_memory', 'manual') AND importance >= 5 AND tier = 3`,
812
+ args: []
813
+ });
814
+ } catch {
815
+ }
816
+ try {
817
+ await client.execute({
818
+ sql: `ALTER TABLE memories ADD COLUMN supersedes_id TEXT`,
819
+ args: []
820
+ });
821
+ } catch {
822
+ }
823
+ try {
824
+ await client.execute(
825
+ `CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL`
826
+ );
827
+ } catch {
828
+ }
829
+ for (const col of [
830
+ "ALTER TABLE tasks ADD COLUMN checkpoint TEXT",
831
+ "ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER DEFAULT 0"
832
+ ]) {
833
+ try {
834
+ await client.execute(col);
835
+ } catch {
836
+ }
837
+ }
838
+ }
839
+ async function disposeDatabase() {
840
+ if (_client) {
841
+ _client.close();
842
+ _client = null;
843
+ }
844
+ }
845
+ var _client, initTurso, disposeTurso;
846
+ var init_database = __esm({
847
+ "src/lib/database.ts"() {
848
+ "use strict";
849
+ _client = null;
850
+ initTurso = initDatabase;
851
+ disposeTurso = disposeDatabase;
852
+ }
853
+ });
854
+
855
+ // src/lib/keychain.ts
856
+ import { readFile, writeFile, unlink, mkdir, chmod } from "fs/promises";
857
+ import { existsSync } from "fs";
858
+ import path from "path";
859
+ import crypto from "crypto";
860
+ function getKeyDir() {
861
+ return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path.join(process.env.HOME ?? "/tmp", ".exe-os");
862
+ }
863
+ function getKeyPath() {
864
+ return path.join(getKeyDir(), "master.key");
865
+ }
866
+ async function tryKeytar() {
867
+ try {
868
+ return await import("keytar");
869
+ } catch {
870
+ return null;
871
+ }
872
+ }
873
+ async function getMasterKey() {
874
+ const keytar = await tryKeytar();
875
+ if (keytar) {
876
+ try {
877
+ const stored = await keytar.getPassword(SERVICE, ACCOUNT);
878
+ if (stored) {
879
+ return Buffer.from(stored, "base64");
880
+ }
881
+ } catch {
882
+ }
883
+ }
884
+ const keyPath = getKeyPath();
885
+ if (!existsSync(keyPath)) {
886
+ return null;
887
+ }
888
+ try {
889
+ const content = await readFile(keyPath, "utf-8");
890
+ return Buffer.from(content.trim(), "base64");
891
+ } catch {
892
+ return null;
893
+ }
894
+ }
895
+ var SERVICE, ACCOUNT;
896
+ var init_keychain = __esm({
897
+ "src/lib/keychain.ts"() {
898
+ "use strict";
899
+ SERVICE = "exe-mem";
900
+ ACCOUNT = "master-key";
901
+ }
902
+ });
903
+
26
904
  // src/lib/config.ts
27
905
  var config_exports = {};
28
906
  __export(config_exports, {
@@ -104,6 +982,11 @@ function normalizeSessionLifecycle(raw) {
104
982
  const userSL = raw.sessionLifecycle ?? {};
105
983
  raw.sessionLifecycle = { ...defaultSL, ...userSL };
106
984
  }
985
+ function normalizeAutoUpdate(raw) {
986
+ const defaultAU = DEFAULT_CONFIG.autoUpdate;
987
+ const userAU = raw.autoUpdate ?? {};
988
+ raw.autoUpdate = { ...defaultAU, ...userAU };
989
+ }
107
990
  async function loadConfig() {
108
991
  const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
109
992
  await mkdir2(dir, { recursive: true });
@@ -126,6 +1009,7 @@ async function loadConfig() {
126
1009
  }
127
1010
  normalizeScalingRoadmap(migratedCfg);
128
1011
  normalizeSessionLifecycle(migratedCfg);
1012
+ normalizeAutoUpdate(migratedCfg);
129
1013
  const config = { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
130
1014
  if (config.dbPath.startsWith("~")) {
131
1015
  config.dbPath = config.dbPath.replace(/^~/, os.homedir());
@@ -148,6 +1032,7 @@ function loadConfigSync() {
148
1032
  const { config: migratedCfg } = migrateConfig(parsed);
149
1033
  normalizeScalingRoadmap(migratedCfg);
150
1034
  normalizeSessionLifecycle(migratedCfg);
1035
+ normalizeAutoUpdate(migratedCfg);
151
1036
  return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
152
1037
  } catch {
153
1038
  return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
@@ -167,6 +1052,7 @@ async function loadConfigFrom(configPath) {
167
1052
  const { config: migratedCfg } = migrateConfig(parsed);
168
1053
  normalizeScalingRoadmap(migratedCfg);
169
1054
  normalizeSessionLifecycle(migratedCfg);
1055
+ normalizeAutoUpdate(migratedCfg);
170
1056
  return { ...DEFAULT_CONFIG, ...migratedCfg };
171
1057
  } catch {
172
1058
  return { ...DEFAULT_CONFIG };
@@ -238,6 +1124,11 @@ var init_config = __esm({
238
1124
  idleKillTicksRequired: 3,
239
1125
  idleKillIntercomAckWindowMs: 1e4,
240
1126
  maxAutoInstances: 10
1127
+ },
1128
+ autoUpdate: {
1129
+ checkOnBoot: true,
1130
+ autoInstall: false,
1131
+ checkIntervalMs: 24 * 60 * 60 * 1e3
241
1132
  }
242
1133
  };
243
1134
  CONFIG_MIGRATIONS = [
@@ -371,13 +1262,27 @@ async function ensureShardSchema(client) {
371
1262
  "ALTER TABLE memories ADD COLUMN document_id TEXT",
372
1263
  "ALTER TABLE memories ADD COLUMN user_id TEXT",
373
1264
  "ALTER TABLE memories ADD COLUMN char_offset INTEGER",
374
- "ALTER TABLE memories ADD COLUMN page_number INTEGER"
1265
+ "ALTER TABLE memories ADD COLUMN page_number INTEGER",
1266
+ // Source provenance columns (must match database.ts)
1267
+ "ALTER TABLE memories ADD COLUMN source_path TEXT",
1268
+ "ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'",
1269
+ "ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3",
1270
+ "ALTER TABLE memories ADD COLUMN supersedes_id TEXT"
375
1271
  ]) {
376
1272
  try {
377
1273
  await client.execute(col);
378
1274
  } catch {
379
1275
  }
380
1276
  }
1277
+ for (const idx of [
1278
+ "CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)",
1279
+ "CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL"
1280
+ ]) {
1281
+ try {
1282
+ await client.execute(idx);
1283
+ } catch {
1284
+ }
1285
+ }
381
1286
  try {
382
1287
  await client.execute("CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)");
383
1288
  } catch {
@@ -456,28 +1361,463 @@ async function ensureShardSchema(client) {
456
1361
  }
457
1362
  }
458
1363
  }
459
- async function getReadyShardClient(projectName) {
460
- const client = getShardClient(projectName);
461
- await ensureShardSchema(client);
462
- return client;
1364
+ async function getReadyShardClient(projectName) {
1365
+ const client = getShardClient(projectName);
1366
+ await ensureShardSchema(client);
1367
+ return client;
1368
+ }
1369
+ function disposeShards() {
1370
+ for (const [, client] of _shards) {
1371
+ client.close();
1372
+ }
1373
+ _shards.clear();
1374
+ _shardingEnabled = false;
1375
+ _encryptionKey = null;
1376
+ }
1377
+ var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
1378
+ var init_shard_manager = __esm({
1379
+ "src/lib/shard-manager.ts"() {
1380
+ "use strict";
1381
+ init_config();
1382
+ SHARDS_DIR = path3.join(EXE_AI_DIR, "shards");
1383
+ _shards = /* @__PURE__ */ new Map();
1384
+ _encryptionKey = null;
1385
+ _shardingEnabled = false;
1386
+ }
1387
+ });
1388
+
1389
+ // src/lib/store.ts
1390
+ var store_exports = {};
1391
+ __export(store_exports, {
1392
+ attachDocumentMetadata: () => attachDocumentMetadata,
1393
+ buildWikiScopeFilter: () => buildWikiScopeFilter,
1394
+ classifyTier: () => classifyTier,
1395
+ disposeStore: () => disposeStore,
1396
+ flushBatch: () => flushBatch,
1397
+ flushTier3: () => flushTier3,
1398
+ getMemoryCardinality: () => getMemoryCardinality,
1399
+ initStore: () => initStore,
1400
+ reserveVersions: () => reserveVersions,
1401
+ searchMemories: () => searchMemories,
1402
+ updateMemoryStatus: () => updateMemoryStatus,
1403
+ vectorToBlob: () => vectorToBlob,
1404
+ writeMemory: () => writeMemory
1405
+ });
1406
+ async function initStore(options) {
1407
+ if (_flushTimer !== null) {
1408
+ clearInterval(_flushTimer);
1409
+ _flushTimer = null;
1410
+ }
1411
+ _pendingRecords = [];
1412
+ _flushing = false;
1413
+ _batchSize = options?.batchSize ?? 20;
1414
+ _flushIntervalMs = options?.flushIntervalMs ?? 1e4;
1415
+ let dbPath = options?.dbPath;
1416
+ if (!dbPath) {
1417
+ const config = await loadConfig();
1418
+ dbPath = config.dbPath;
1419
+ }
1420
+ let masterKey = options?.masterKey ?? null;
1421
+ if (!masterKey) {
1422
+ masterKey = await getMasterKey();
1423
+ if (!masterKey) {
1424
+ throw new Error(
1425
+ "No encryption key found. Run /exe-setup to generate one."
1426
+ );
1427
+ }
1428
+ }
1429
+ const hexKey = masterKey.toString("hex");
1430
+ await initTurso({
1431
+ dbPath,
1432
+ encryptionKey: hexKey
1433
+ });
1434
+ await ensureSchema();
1435
+ try {
1436
+ const { initShardManager: initShardManager2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
1437
+ initShardManager2(hexKey);
1438
+ } catch {
1439
+ }
1440
+ const client = getClient();
1441
+ const vResult = await client.execute("SELECT MAX(version) as max_v FROM memories");
1442
+ _nextVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
1443
+ }
1444
+ function classifyTier(record) {
1445
+ if (record.tool_name === "commit_to_long_term_memory" && (record.importance ?? 0) >= 8) return 1;
1446
+ if (["store_memory", "manual"].includes(record.tool_name ?? "") && (record.importance ?? 0) >= 5) return 2;
1447
+ return 3;
1448
+ }
1449
+ async function writeMemory(record) {
1450
+ if (record.vector !== null && record.vector.length !== EMBEDDING_DIM) {
1451
+ throw new Error(
1452
+ `Expected ${EMBEDDING_DIM}-dim vector, got ${record.vector.length}`
1453
+ );
1454
+ }
1455
+ const dbRow = {
1456
+ id: record.id,
1457
+ agent_id: record.agent_id,
1458
+ agent_role: record.agent_role,
1459
+ session_id: record.session_id,
1460
+ timestamp: record.timestamp,
1461
+ tool_name: record.tool_name,
1462
+ project_name: record.project_name,
1463
+ has_error: record.has_error ? 1 : 0,
1464
+ raw_text: record.raw_text,
1465
+ vector: record.vector,
1466
+ version: _nextVersion++,
1467
+ task_id: record.task_id ?? null,
1468
+ importance: record.importance ?? 5,
1469
+ status: record.status ?? "active",
1470
+ confidence: record.confidence ?? 0.7,
1471
+ last_accessed: record.last_accessed ?? record.timestamp,
1472
+ workspace_id: record.workspace_id ?? null,
1473
+ document_id: record.document_id ?? null,
1474
+ user_id: record.user_id ?? null,
1475
+ char_offset: record.char_offset ?? null,
1476
+ page_number: record.page_number ?? null,
1477
+ source_path: record.source_path ?? null,
1478
+ source_type: record.source_type ?? null,
1479
+ tier: record.tier ?? classifyTier(record),
1480
+ supersedes_id: record.supersedes_id ?? null
1481
+ };
1482
+ _pendingRecords.push(dbRow);
1483
+ if (_flushTimer === null) {
1484
+ _flushTimer = setInterval(() => {
1485
+ void flushBatch();
1486
+ }, _flushIntervalMs);
1487
+ if (_flushTimer && typeof _flushTimer === "object" && "unref" in _flushTimer) {
1488
+ _flushTimer.unref();
1489
+ }
1490
+ }
1491
+ if (_pendingRecords.length >= _batchSize) {
1492
+ await flushBatch();
1493
+ }
1494
+ }
1495
+ async function flushBatch() {
1496
+ if (_flushing || _pendingRecords.length === 0) return 0;
1497
+ _flushing = true;
1498
+ try {
1499
+ const batch = _pendingRecords.slice(0);
1500
+ const buildStmt = (row) => {
1501
+ const hasVector = row.vector !== null;
1502
+ const taskId = row.task_id ?? null;
1503
+ const importance = row.importance ?? 5;
1504
+ const status = row.status ?? "active";
1505
+ const confidence = row.confidence ?? 0.7;
1506
+ const lastAccessed = row.last_accessed ?? row.timestamp;
1507
+ const workspaceId = row.workspace_id ?? null;
1508
+ const documentId = row.document_id ?? null;
1509
+ const userId = row.user_id ?? null;
1510
+ const charOffset = row.char_offset ?? null;
1511
+ const pageNumber = row.page_number ?? null;
1512
+ const sourcePath = row.source_path ?? null;
1513
+ const sourceType = row.source_type ?? null;
1514
+ const tier = row.tier ?? 3;
1515
+ const supersedesId = row.supersedes_id ?? null;
1516
+ return {
1517
+ sql: hasVector ? `INSERT OR IGNORE INTO memories
1518
+ (id, agent_id, agent_role, session_id, timestamp,
1519
+ tool_name, project_name,
1520
+ has_error, raw_text, vector, version, task_id, importance, status,
1521
+ confidence, last_accessed,
1522
+ workspace_id, document_id, user_id, char_offset, page_number,
1523
+ source_path, source_type, tier, supersedes_id)
1524
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, vector32(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` : `INSERT OR IGNORE INTO memories
1525
+ (id, agent_id, agent_role, session_id, timestamp,
1526
+ tool_name, project_name,
1527
+ has_error, raw_text, vector, version, task_id, importance, status,
1528
+ confidence, last_accessed,
1529
+ workspace_id, document_id, user_id, char_offset, page_number,
1530
+ source_path, source_type, tier, supersedes_id)
1531
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1532
+ args: hasVector ? [
1533
+ row.id,
1534
+ row.agent_id,
1535
+ row.agent_role,
1536
+ row.session_id,
1537
+ row.timestamp,
1538
+ row.tool_name,
1539
+ row.project_name,
1540
+ row.has_error,
1541
+ row.raw_text,
1542
+ vectorToBlob(row.vector),
1543
+ row.version,
1544
+ taskId,
1545
+ importance,
1546
+ status,
1547
+ confidence,
1548
+ lastAccessed,
1549
+ workspaceId,
1550
+ documentId,
1551
+ userId,
1552
+ charOffset,
1553
+ pageNumber,
1554
+ sourcePath,
1555
+ sourceType,
1556
+ tier,
1557
+ supersedesId
1558
+ ] : [
1559
+ row.id,
1560
+ row.agent_id,
1561
+ row.agent_role,
1562
+ row.session_id,
1563
+ row.timestamp,
1564
+ row.tool_name,
1565
+ row.project_name,
1566
+ row.has_error,
1567
+ row.raw_text,
1568
+ row.version,
1569
+ taskId,
1570
+ importance,
1571
+ status,
1572
+ confidence,
1573
+ lastAccessed,
1574
+ workspaceId,
1575
+ documentId,
1576
+ userId,
1577
+ charOffset,
1578
+ pageNumber,
1579
+ sourcePath,
1580
+ sourceType,
1581
+ tier,
1582
+ supersedesId
1583
+ ]
1584
+ };
1585
+ };
1586
+ const globalClient = getClient();
1587
+ const globalStmts = batch.map(buildStmt);
1588
+ await globalClient.batch(globalStmts, "write");
1589
+ _pendingRecords.splice(0, batch.length);
1590
+ try {
1591
+ const { isShardingEnabled: isShardingEnabled2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
1592
+ if (isShardingEnabled2()) {
1593
+ const byProject = /* @__PURE__ */ new Map();
1594
+ for (const row of batch) {
1595
+ const proj = row.project_name || "unknown";
1596
+ if (!byProject.has(proj)) byProject.set(proj, []);
1597
+ byProject.get(proj).push(row);
1598
+ }
1599
+ for (const [project, rows] of byProject) {
1600
+ try {
1601
+ const shardClient = await getReadyShardClient2(project);
1602
+ const shardStmts = rows.map(buildStmt);
1603
+ await shardClient.batch(shardStmts, "write");
1604
+ } catch (err) {
1605
+ process.stderr.write(
1606
+ `[store] Shard write failed for ${project}: ${err instanceof Error ? err.message : String(err)}
1607
+ `
1608
+ );
1609
+ }
1610
+ }
1611
+ }
1612
+ } catch {
1613
+ }
1614
+ return batch.length;
1615
+ } finally {
1616
+ _flushing = false;
1617
+ }
1618
+ }
1619
+ function buildWikiScopeFilter(options, columnPrefix) {
1620
+ const args = [];
1621
+ let clause = "";
1622
+ if (options?.workspaceId !== void 0) {
1623
+ clause += ` AND ${columnPrefix}workspace_id = ?`;
1624
+ args.push(options.workspaceId);
1625
+ }
1626
+ if (options?.userId === void 0) {
1627
+ clause += ` AND ${columnPrefix}user_id IS NULL`;
1628
+ } else if (options.userId === null) {
1629
+ clause += ` AND ${columnPrefix}user_id IS NULL`;
1630
+ } else {
1631
+ clause += ` AND (${columnPrefix}user_id = ? OR ${columnPrefix}user_id IS NULL)`;
1632
+ args.push(options.userId);
1633
+ }
1634
+ return { clause, args };
1635
+ }
1636
+ async function searchMemories(queryVector, agentId, options) {
1637
+ let client;
1638
+ try {
1639
+ const { isShardingEnabled: isShardingEnabled2, shardExists: shardExists2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
1640
+ if (isShardingEnabled2() && options?.projectName && shardExists2(options.projectName)) {
1641
+ client = await getReadyShardClient2(options.projectName);
1642
+ } else {
1643
+ client = getClient();
1644
+ }
1645
+ } catch {
1646
+ client = getClient();
1647
+ }
1648
+ const limit = options?.limit ?? 10;
1649
+ const statusFilter = options?.includeArchived ? "" : `
1650
+ AND COALESCE(status, 'active') = 'active'`;
1651
+ let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
1652
+ tool_name, project_name,
1653
+ has_error, raw_text, vector, importance, status,
1654
+ confidence, last_accessed,
1655
+ workspace_id, document_id, user_id,
1656
+ char_offset, page_number,
1657
+ source_path, source_type
1658
+ FROM memories
1659
+ WHERE agent_id = ?
1660
+ AND vector IS NOT NULL${statusFilter}
1661
+ AND COALESCE(confidence, 0.7) >= 0.3`;
1662
+ const args = [agentId];
1663
+ const scope = buildWikiScopeFilter(options, "");
1664
+ sql += scope.clause;
1665
+ args.push(...scope.args);
1666
+ if (options?.projectName) {
1667
+ sql += ` AND project_name = ?`;
1668
+ args.push(options.projectName);
1669
+ }
1670
+ if (options?.toolName) {
1671
+ sql += ` AND tool_name = ?`;
1672
+ args.push(options.toolName);
1673
+ }
1674
+ if (options?.hasError !== void 0) {
1675
+ sql += ` AND has_error = ?`;
1676
+ args.push(options.hasError ? 1 : 0);
1677
+ }
1678
+ if (options?.since) {
1679
+ sql += ` AND timestamp >= ?`;
1680
+ args.push(options.since);
1681
+ }
1682
+ sql += ` ORDER BY vector_distance_cos(vector, vector32(?))`;
1683
+ args.push(vectorToBlob(queryVector));
1684
+ sql += ` LIMIT ?`;
1685
+ args.push(limit);
1686
+ const result = await client.execute({ sql, args });
1687
+ return result.rows.map((row) => ({
1688
+ id: row.id,
1689
+ agent_id: row.agent_id,
1690
+ agent_role: row.agent_role,
1691
+ session_id: row.session_id,
1692
+ timestamp: row.timestamp,
1693
+ tool_name: row.tool_name,
1694
+ project_name: row.project_name,
1695
+ has_error: row.has_error === 1,
1696
+ raw_text: row.raw_text,
1697
+ vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
1698
+ importance: row.importance ?? 5,
1699
+ status: row.status ?? "active",
1700
+ confidence: row.confidence ?? 0.7,
1701
+ last_accessed: row.last_accessed ?? row.timestamp,
1702
+ workspace_id: row.workspace_id ?? null,
1703
+ document_id: row.document_id ?? null,
1704
+ user_id: row.user_id ?? null,
1705
+ char_offset: row.char_offset ?? null,
1706
+ page_number: row.page_number ?? null,
1707
+ source_path: row.source_path ?? null,
1708
+ source_type: row.source_type ?? null
1709
+ }));
1710
+ }
1711
+ async function attachDocumentMetadata(records) {
1712
+ const docIds = [
1713
+ ...new Set(
1714
+ records.map((r) => r.document_id).filter((id) => typeof id === "string" && id.length > 0)
1715
+ )
1716
+ ];
1717
+ if (docIds.length === 0) return records;
1718
+ try {
1719
+ const client = getClient();
1720
+ const placeholders = docIds.map(() => "?").join(",");
1721
+ const result = await client.execute({
1722
+ sql: `SELECT id, filename, mime, source_type, uploaded_at
1723
+ FROM documents
1724
+ WHERE id IN (${placeholders})`,
1725
+ args: docIds
1726
+ });
1727
+ const byId = /* @__PURE__ */ new Map();
1728
+ for (const row of result.rows) {
1729
+ const id = row.id;
1730
+ byId.set(id, {
1731
+ document_id: id,
1732
+ filename: row.filename,
1733
+ mime: row.mime ?? null,
1734
+ source_type: row.source_type ?? null,
1735
+ uploaded_at: row.uploaded_at
1736
+ });
1737
+ }
1738
+ for (const record of records) {
1739
+ if (!record.document_id) continue;
1740
+ record.document_metadata = byId.get(record.document_id) ?? null;
1741
+ }
1742
+ } catch {
1743
+ }
1744
+ return records;
1745
+ }
1746
+ async function flushTier3(agentId, options) {
1747
+ const client = getClient();
1748
+ const maxAge = options?.maxAgeHours ?? 72;
1749
+ const cutoff = new Date(Date.now() - maxAge * 36e5).toISOString();
1750
+ if (options?.dryRun) {
1751
+ const result2 = await client.execute({
1752
+ sql: `SELECT COUNT(*) as cnt FROM memories
1753
+ WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
1754
+ args: [agentId, cutoff]
1755
+ });
1756
+ return { archived: Number(result2.rows[0]?.cnt ?? 0) };
1757
+ }
1758
+ const result = await client.execute({
1759
+ sql: `UPDATE memories SET status = 'archived'
1760
+ WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
1761
+ args: [agentId, cutoff]
1762
+ });
1763
+ return { archived: result.rowsAffected };
463
1764
  }
464
- function disposeShards() {
465
- for (const [, client] of _shards) {
466
- client.close();
1765
+ async function disposeStore() {
1766
+ if (_flushTimer !== null) {
1767
+ clearInterval(_flushTimer);
1768
+ _flushTimer = null;
467
1769
  }
468
- _shards.clear();
469
- _shardingEnabled = false;
470
- _encryptionKey = null;
1770
+ if (_pendingRecords.length > 0) {
1771
+ await flushBatch();
1772
+ }
1773
+ await disposeTurso();
1774
+ _pendingRecords = [];
1775
+ _nextVersion = 1;
471
1776
  }
472
- var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
473
- var init_shard_manager = __esm({
474
- "src/lib/shard-manager.ts"() {
1777
+ function vectorToBlob(vector) {
1778
+ const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
1779
+ return JSON.stringify(Array.from(f32));
1780
+ }
1781
+ async function updateMemoryStatus(id, status) {
1782
+ const client = getClient();
1783
+ await client.execute({
1784
+ sql: `UPDATE memories SET status = ? WHERE id = ?`,
1785
+ args: [status, id]
1786
+ });
1787
+ }
1788
+ function reserveVersions(count) {
1789
+ const reserved = [];
1790
+ for (let i = 0; i < count; i++) {
1791
+ reserved.push(_nextVersion++);
1792
+ }
1793
+ return reserved;
1794
+ }
1795
+ async function getMemoryCardinality(agentId) {
1796
+ try {
1797
+ const client = getClient();
1798
+ const result = await client.execute({
1799
+ sql: `SELECT COUNT(*) as cnt FROM memories WHERE agent_id = ? AND COALESCE(status, 'active') = 'active'`,
1800
+ args: [agentId]
1801
+ });
1802
+ return Number(result.rows[0]?.cnt) || 0;
1803
+ } catch {
1804
+ return 0;
1805
+ }
1806
+ }
1807
+ var _pendingRecords, _batchSize, _flushIntervalMs, _flushTimer, _flushing, _nextVersion;
1808
+ var init_store = __esm({
1809
+ "src/lib/store.ts"() {
475
1810
  "use strict";
1811
+ init_memory();
1812
+ init_database();
1813
+ init_keychain();
476
1814
  init_config();
477
- SHARDS_DIR = path3.join(EXE_AI_DIR, "shards");
478
- _shards = /* @__PURE__ */ new Map();
479
- _encryptionKey = null;
480
- _shardingEnabled = false;
1815
+ _pendingRecords = [];
1816
+ _batchSize = 20;
1817
+ _flushIntervalMs = 1e4;
1818
+ _flushTimer = null;
1819
+ _flushing = false;
1820
+ _nextVersion = 1;
481
1821
  }
482
1822
  });
483
1823
 
@@ -986,12 +2326,23 @@ function getProjectName(cwd) {
986
2326
  const dir = cwd ?? process.cwd();
987
2327
  if (_cached && _cachedCwd === dir) return _cached;
988
2328
  try {
989
- const repoRoot = execSync("git rev-parse --show-toplevel", {
990
- cwd: dir,
991
- encoding: "utf8",
992
- timeout: 2e3,
993
- stdio: ["pipe", "pipe", "pipe"]
994
- }).trim();
2329
+ let repoRoot;
2330
+ try {
2331
+ const gitCommonDir = execSync("git rev-parse --path-format=absolute --git-common-dir", {
2332
+ cwd: dir,
2333
+ encoding: "utf8",
2334
+ timeout: 2e3,
2335
+ stdio: ["pipe", "pipe", "pipe"]
2336
+ }).trim();
2337
+ repoRoot = path5.dirname(gitCommonDir);
2338
+ } catch {
2339
+ repoRoot = execSync("git rev-parse --show-toplevel", {
2340
+ cwd: dir,
2341
+ encoding: "utf8",
2342
+ timeout: 2e3,
2343
+ stdio: ["pipe", "pipe", "pipe"]
2344
+ }).trim();
2345
+ }
995
2346
  _cached = path5.basename(repoRoot);
996
2347
  _cachedCwd = dir;
997
2348
  return _cached;
@@ -1023,15 +2374,30 @@ import { execSync as execSync2 } from "child_process";
1023
2374
  import { readFileSync as readFileSync3, readdirSync, statSync as statSync2, existsSync as existsSync5 } from "fs";
1024
2375
  import path6 from "path";
1025
2376
  import crypto2 from "crypto";
2377
+ function hasRipgrep() {
2378
+ if (_hasRg === null) {
2379
+ try {
2380
+ execSync2("rg --version", { stdio: "ignore", timeout: 2e3 });
2381
+ _hasRg = true;
2382
+ } catch {
2383
+ _hasRg = false;
2384
+ }
2385
+ }
2386
+ return _hasRg;
2387
+ }
1026
2388
  async function grepProjectFiles(query, projectRoot, options) {
1027
2389
  const maxResults = options?.maxResults ?? 10;
1028
2390
  const terms = query.toLowerCase().split(/\s+/).filter((t) => t.length >= 3).map((t) => t.replace(/[^a-z0-9_-]/g, "")).filter((t) => t.length >= 3);
1029
2391
  if (terms.length === 0) return [];
1030
2392
  const pattern = terms.join("|");
1031
2393
  let hits;
1032
- try {
1033
- hits = grepWithRipgrep(pattern, projectRoot, options?.patterns);
1034
- } catch {
2394
+ if (hasRipgrep()) {
2395
+ try {
2396
+ hits = grepWithRipgrep(pattern, projectRoot, options?.patterns);
2397
+ } catch {
2398
+ hits = grepWithNodeFs(pattern, projectRoot, options?.patterns);
2399
+ }
2400
+ } else {
1035
2401
  hits = grepWithNodeFs(pattern, projectRoot, options?.patterns);
1036
2402
  }
1037
2403
  hits.sort((a, b) => b.density - a.density);
@@ -1076,7 +2442,7 @@ function getChunkContext(filePath, lineNumber) {
1076
2442
  function grepWithRipgrep(pattern, projectRoot, patterns) {
1077
2443
  const globs = (patterns ?? DEFAULT_PATTERNS).map((p) => `--glob '${p}'`).join(" ");
1078
2444
  const excludes = EXCLUDE_DIRS.map((d) => `--glob '!${d}'`).join(" ");
1079
- const cmd = `rg -i -c --hidden '${pattern.replace(/'/g, "\\'")}' . ${globs} ${excludes} --max-filesize ${MAX_FILE_SIZE} 2>/dev/null || true`;
2445
+ const cmd = `rg -i -c --hidden --no-config --no-ignore '${pattern.replace(/'/g, "\\'")}' . ${globs} ${excludes} --max-filesize ${MAX_FILE_SIZE} 2>/dev/null || true`;
1080
2446
  const output = execSync2(cmd, {
1081
2447
  cwd: projectRoot,
1082
2448
  encoding: "utf8",
@@ -1205,10 +2571,11 @@ function buildSnippet(hit, projectRoot) {
1205
2571
  return hit.matchLine;
1206
2572
  }
1207
2573
  }
1208
- var DEFAULT_PATTERNS, EXCLUDE_DIRS, MAX_FILE_SIZE, MAX_FILES;
2574
+ var _hasRg, DEFAULT_PATTERNS, EXCLUDE_DIRS, MAX_FILE_SIZE, MAX_FILES;
1209
2575
  var init_file_grep = __esm({
1210
2576
  "src/lib/file-grep.ts"() {
1211
2577
  "use strict";
2578
+ _hasRg = null;
1212
2579
  DEFAULT_PATTERNS = [
1213
2580
  ".planning/*.md",
1214
2581
  "exe/output/*.md",
@@ -1329,157 +2696,9 @@ var init_reranker = __esm({
1329
2696
  }
1330
2697
  });
1331
2698
 
1332
- // src/lib/store.ts
1333
- init_memory();
1334
-
1335
- // src/lib/database.ts
1336
- import { createClient } from "@libsql/client";
1337
- var _client = null;
1338
- function getClient() {
1339
- if (!_client) {
1340
- throw new Error("Database client not initialized. Call initDatabase() first.");
1341
- }
1342
- return _client;
1343
- }
1344
-
1345
- // src/lib/keychain.ts
1346
- import { readFile, writeFile, unlink, mkdir, chmod } from "fs/promises";
1347
- import { existsSync } from "fs";
1348
- import path from "path";
1349
- import crypto from "crypto";
1350
-
1351
- // src/lib/store.ts
1352
- init_config();
1353
- function buildWikiScopeFilter(options, columnPrefix) {
1354
- const args = [];
1355
- let clause = "";
1356
- if (options?.workspaceId !== void 0) {
1357
- clause += ` AND ${columnPrefix}workspace_id = ?`;
1358
- args.push(options.workspaceId);
1359
- }
1360
- if (options?.userId === void 0) {
1361
- clause += ` AND ${columnPrefix}user_id IS NULL`;
1362
- } else if (options.userId === null) {
1363
- clause += ` AND ${columnPrefix}user_id IS NULL`;
1364
- } else {
1365
- clause += ` AND (${columnPrefix}user_id = ? OR ${columnPrefix}user_id IS NULL)`;
1366
- args.push(options.userId);
1367
- }
1368
- return { clause, args };
1369
- }
1370
- async function searchMemories(queryVector, agentId, options) {
1371
- let client;
1372
- try {
1373
- const { isShardingEnabled: isShardingEnabled2, shardExists: shardExists2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
1374
- if (isShardingEnabled2() && options?.projectName && shardExists2(options.projectName)) {
1375
- client = await getReadyShardClient2(options.projectName);
1376
- } else {
1377
- client = getClient();
1378
- }
1379
- } catch {
1380
- client = getClient();
1381
- }
1382
- const limit = options?.limit ?? 10;
1383
- const statusFilter = options?.includeArchived ? "" : `
1384
- AND COALESCE(status, 'active') = 'active'`;
1385
- let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
1386
- tool_name, project_name,
1387
- has_error, raw_text, vector, importance, status,
1388
- confidence, last_accessed,
1389
- workspace_id, document_id, user_id,
1390
- char_offset, page_number
1391
- FROM memories
1392
- WHERE agent_id = ?
1393
- AND vector IS NOT NULL${statusFilter}
1394
- AND COALESCE(confidence, 0.7) >= 0.3`;
1395
- const args = [agentId];
1396
- const scope = buildWikiScopeFilter(options, "");
1397
- sql += scope.clause;
1398
- args.push(...scope.args);
1399
- if (options?.projectName) {
1400
- sql += ` AND project_name = ?`;
1401
- args.push(options.projectName);
1402
- }
1403
- if (options?.toolName) {
1404
- sql += ` AND tool_name = ?`;
1405
- args.push(options.toolName);
1406
- }
1407
- if (options?.hasError !== void 0) {
1408
- sql += ` AND has_error = ?`;
1409
- args.push(options.hasError ? 1 : 0);
1410
- }
1411
- if (options?.since) {
1412
- sql += ` AND timestamp >= ?`;
1413
- args.push(options.since);
1414
- }
1415
- sql += ` ORDER BY vector_distance_cos(vector, vector32(?))`;
1416
- args.push(vectorToBlob(queryVector));
1417
- sql += ` LIMIT ?`;
1418
- args.push(limit);
1419
- const result = await client.execute({ sql, args });
1420
- return result.rows.map((row) => ({
1421
- id: row.id,
1422
- agent_id: row.agent_id,
1423
- agent_role: row.agent_role,
1424
- session_id: row.session_id,
1425
- timestamp: row.timestamp,
1426
- tool_name: row.tool_name,
1427
- project_name: row.project_name,
1428
- has_error: row.has_error === 1,
1429
- raw_text: row.raw_text,
1430
- vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
1431
- importance: row.importance ?? 5,
1432
- status: row.status ?? "active",
1433
- confidence: row.confidence ?? 0.7,
1434
- last_accessed: row.last_accessed ?? row.timestamp,
1435
- workspace_id: row.workspace_id ?? null,
1436
- document_id: row.document_id ?? null,
1437
- user_id: row.user_id ?? null,
1438
- char_offset: row.char_offset ?? null,
1439
- page_number: row.page_number ?? null
1440
- }));
1441
- }
1442
- async function attachDocumentMetadata(records) {
1443
- const docIds = [
1444
- ...new Set(
1445
- records.map((r) => r.document_id).filter((id) => typeof id === "string" && id.length > 0)
1446
- )
1447
- ];
1448
- if (docIds.length === 0) return records;
1449
- try {
1450
- const client = getClient();
1451
- const placeholders = docIds.map(() => "?").join(",");
1452
- const result = await client.execute({
1453
- sql: `SELECT id, filename, mime, source_type, uploaded_at
1454
- FROM documents
1455
- WHERE id IN (${placeholders})`,
1456
- args: docIds
1457
- });
1458
- const byId = /* @__PURE__ */ new Map();
1459
- for (const row of result.rows) {
1460
- const id = row.id;
1461
- byId.set(id, {
1462
- document_id: id,
1463
- filename: row.filename,
1464
- mime: row.mime ?? null,
1465
- source_type: row.source_type ?? null,
1466
- uploaded_at: row.uploaded_at
1467
- });
1468
- }
1469
- for (const record of records) {
1470
- if (!record.document_id) continue;
1471
- record.document_metadata = byId.get(record.document_id) ?? null;
1472
- }
1473
- } catch {
1474
- }
1475
- return records;
1476
- }
1477
- function vectorToBlob(vector) {
1478
- const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
1479
- return JSON.stringify(Array.from(f32));
1480
- }
1481
-
1482
2699
  // src/lib/hybrid-search.ts
2700
+ init_store();
2701
+ init_database();
1483
2702
  var RRF_K = 60;
1484
2703
  async function hybridSearch(queryText, agentId, options) {
1485
2704
  const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_config(), config_exports));
@@ -1506,8 +2725,21 @@ async function hybridSearch(queryText, agentId, options) {
1506
2725
  } catch {
1507
2726
  }
1508
2727
  }
2728
+ const { getMemoryCardinality: getMemoryCardinality2 } = await Promise.resolve().then(() => (init_store(), store_exports));
2729
+ const cardinality = await getMemoryCardinality2(agentId);
2730
+ const { rerankerAutoTrigger } = config.scalingRoadmap ?? {};
2731
+ const minCardForBroad = rerankerAutoTrigger?.broadQueryMinCardinality ?? 5e4;
2732
+ const useNarrowPath = cardinality < 1e4;
2733
+ const useBroadPath = cardinality > minCardForBroad || _isBroadQuery && cardinality >= 1e4;
2734
+ const effectiveIsBroad = useBroadPath && !useNarrowPath;
2735
+ if (effectiveIsBroad !== _isBroadQuery) {
2736
+ process.stderr.write(
2737
+ `[hybrid-search] Adaptive routing override: cardinality=${cardinality}, router=${_isBroadQuery ? "broad" : "narrow"} \u2192 ${effectiveIsBroad ? "broad" : "narrow"}
2738
+ `
2739
+ );
2740
+ }
1509
2741
  const broadFetchTopK = config.scalingRoadmap?.rerankerAutoTrigger?.fetchTopK ?? 150;
1510
- const fetchLimit = _isBroadQuery ? Math.max(limit * 5, broadFetchTopK) : Math.max(limit * 3, 30);
2742
+ const fetchLimit = effectiveIsBroad ? Math.max(limit * 5, broadFetchTopK) : Math.max(limit * 3, 30);
1511
2743
  const fetchOptions = { ...effectiveOptions, limit: fetchLimit, includeSource: false };
1512
2744
  let queryVector = null;
1513
2745
  try {
@@ -1551,8 +2783,8 @@ async function hybridSearch(queryText, agentId, options) {
1551
2783
  weights.push(0.5);
1552
2784
  }
1553
2785
  if (lists.length === 0) return [];
1554
- if (lists.length === 1 && !_isBroadQuery) return lists[0].slice(0, limit);
1555
- const rrfLimit = _isBroadQuery ? Math.max(limit * 5, 150) : limit;
2786
+ if (lists.length === 1 && !effectiveIsBroad) return lists[0].slice(0, limit);
2787
+ const rrfLimit = effectiveIsBroad ? Math.max(limit * 5, 150) : limit;
1556
2788
  const merged = lists.length === 1 ? lists[0].slice(0, rrfLimit) : rrfMergeMulti(lists, rrfLimit, RRF_K, weights);
1557
2789
  const auto = config.scalingRoadmap?.rerankerAutoTrigger ?? {
1558
2790
  enabled: config.rerankerEnabled ?? true,
@@ -1561,9 +2793,9 @@ async function hybridSearch(queryText, agentId, options) {
1561
2793
  returnTopK: 5
1562
2794
  };
1563
2795
  let rerankedAndBlended = null;
1564
- if (_isBroadQuery && auto.enabled) {
1565
- const cardinality = await estimateCardinality(agentId, effectiveOptions);
1566
- if (cardinality > auto.broadQueryMinCardinality) {
2796
+ if (effectiveIsBroad && auto.enabled) {
2797
+ const cardinality2 = await estimateCardinality(agentId, effectiveOptions);
2798
+ if (cardinality2 > auto.broadQueryMinCardinality) {
1567
2799
  try {
1568
2800
  const { isRerankerAvailable: isRerankerAvailable2, rerank: rerank2 } = await Promise.resolve().then(() => (init_reranker(), reranker_exports));
1569
2801
  if (isRerankerAvailable2()) {
@@ -1643,6 +2875,11 @@ function recencyScore(timestamp) {
1643
2875
  function normalizedImportance(importance) {
1644
2876
  return ((importance ?? 5) - 1) / 9;
1645
2877
  }
2878
+ function frecencyBoost(lastAccessed, timestamp) {
2879
+ const accessTime = lastAccessed ? new Date(lastAccessed).getTime() : new Date(timestamp).getTime();
2880
+ const hoursSince = Math.max(0, (Date.now() - accessTime) / (1e3 * 60 * 60));
2881
+ return Math.exp(-0.01 * hoursSince);
2882
+ }
1646
2883
  function rrfMergeMulti(lists, limit, k = RRF_K, weights) {
1647
2884
  const scores = /* @__PURE__ */ new Map();
1648
2885
  for (let listIdx = 0; listIdx < lists.length; listIdx++) {
@@ -1659,7 +2896,9 @@ function rrfMergeMulti(lists, limit, k = RRF_K, weights) {
1659
2896
  const recency = recencyScore(e.record.timestamp);
1660
2897
  const importance = normalizedImportance(e.record.importance);
1661
2898
  const confidence = e.record.confidence ?? 0.7;
1662
- const finalScore = e.rrfScore * 0.35 + recency * 0.14 + importance * 0.21 + confidence * 0.3;
2899
+ const frecency = frecencyBoost(e.record.last_accessed, e.record.timestamp);
2900
+ const baseScore = e.rrfScore * 0.35 + recency * 0.14 + importance * 0.21 + confidence * 0.3;
2901
+ const finalScore = baseScore * (1 + 0.3 * frecency);
1663
2902
  return { score: finalScore, record: e.record };
1664
2903
  });
1665
2904
  return entries.sort((a, b) => b.score - a.score).slice(0, limit).map((e) => e.record);
@@ -1699,7 +2938,8 @@ async function ftsQuery(client, matchExpr, agentId, options, limit) {
1699
2938
  m.has_error, m.raw_text, m.vector, m.task_id,
1700
2939
  m.importance, m.status, m.confidence, m.last_accessed,
1701
2940
  m.workspace_id, m.document_id, m.user_id,
1702
- m.char_offset, m.page_number
2941
+ m.char_offset, m.page_number,
2942
+ m.source_path, m.source_type
1703
2943
  FROM memories m
1704
2944
  JOIN memories_fts fts ON m.rowid = fts.rowid
1705
2945
  WHERE memories_fts MATCH ?
@@ -1748,7 +2988,9 @@ async function ftsQuery(client, matchExpr, agentId, options, limit) {
1748
2988
  document_id: row.document_id ?? null,
1749
2989
  user_id: row.user_id ?? null,
1750
2990
  char_offset: row.char_offset ?? null,
1751
- page_number: row.page_number ?? null
2991
+ page_number: row.page_number ?? null,
2992
+ source_path: row.source_path ?? null,
2993
+ source_type: row.source_type ?? null
1752
2994
  }));
1753
2995
  }
1754
2996
  async function recentRecords(agentId, options, limit) {
@@ -1760,7 +3002,8 @@ async function recentRecords(agentId, options, limit) {
1760
3002
  has_error, raw_text, vector, task_id,
1761
3003
  importance, status, confidence, last_accessed,
1762
3004
  workspace_id, document_id, user_id,
1763
- char_offset, page_number
3005
+ char_offset, page_number,
3006
+ source_path, source_type
1764
3007
  FROM memories
1765
3008
  WHERE agent_id = ?${statusFilter}
1766
3009
  AND COALESCE(confidence, 0.7) >= 0.3`;
@@ -1807,7 +3050,9 @@ async function recentRecords(agentId, options, limit) {
1807
3050
  document_id: row.document_id ?? null,
1808
3051
  user_id: row.user_id ?? null,
1809
3052
  char_offset: row.char_offset ?? null,
1810
- page_number: row.page_number ?? null
3053
+ page_number: row.page_number ?? null,
3054
+ source_path: row.source_path ?? null,
3055
+ source_type: row.source_type ?? null
1811
3056
  }));
1812
3057
  }
1813
3058
  export {