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