@askexenow/exe-os 0.9.8 → 0.9.9

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 (101) hide show
  1. package/dist/bin/backfill-conversations.js +222 -49
  2. package/dist/bin/backfill-responses.js +221 -48
  3. package/dist/bin/backfill-vectors.js +225 -52
  4. package/dist/bin/cleanup-stale-review-tasks.js +150 -28
  5. package/dist/bin/cli.js +1295 -856
  6. package/dist/bin/exe-agent-config.js +36 -8
  7. package/dist/bin/exe-agent.js +14 -4
  8. package/dist/bin/exe-assign.js +221 -48
  9. package/dist/bin/exe-boot.js +778 -427
  10. package/dist/bin/exe-call.js +41 -13
  11. package/dist/bin/exe-cloud.js +163 -58
  12. package/dist/bin/exe-dispatch.js +276 -139
  13. package/dist/bin/exe-doctor.js +145 -27
  14. package/dist/bin/exe-export-behaviors.js +141 -23
  15. package/dist/bin/exe-forget.js +137 -19
  16. package/dist/bin/exe-gateway.js +677 -388
  17. package/dist/bin/exe-heartbeat.js +227 -108
  18. package/dist/bin/exe-kill.js +138 -20
  19. package/dist/bin/exe-launch-agent.js +172 -39
  20. package/dist/bin/exe-link.js +291 -100
  21. package/dist/bin/exe-new-employee.js +214 -106
  22. package/dist/bin/exe-pending-messages.js +395 -33
  23. package/dist/bin/exe-pending-notifications.js +684 -99
  24. package/dist/bin/exe-pending-reviews.js +420 -74
  25. package/dist/bin/exe-rename.js +147 -49
  26. package/dist/bin/exe-review.js +138 -20
  27. package/dist/bin/exe-search.js +240 -69
  28. package/dist/bin/exe-session-cleanup.js +440 -250
  29. package/dist/bin/exe-settings.js +61 -17
  30. package/dist/bin/exe-start-codex.js +158 -39
  31. package/dist/bin/exe-start-opencode.js +157 -38
  32. package/dist/bin/exe-status.js +151 -29
  33. package/dist/bin/exe-team.js +138 -20
  34. package/dist/bin/git-sweep.js +404 -212
  35. package/dist/bin/graph-backfill.js +137 -19
  36. package/dist/bin/graph-export.js +140 -22
  37. package/dist/bin/install.js +90 -61
  38. package/dist/bin/scan-tasks.js +412 -220
  39. package/dist/bin/setup.js +564 -293
  40. package/dist/bin/shard-migrate.js +139 -21
  41. package/dist/bin/update.js +138 -49
  42. package/dist/bin/wiki-sync.js +137 -19
  43. package/dist/gateway/index.js +533 -320
  44. package/dist/hooks/bug-report-worker.js +344 -193
  45. package/dist/hooks/codex-stop-task-finalizer.js +4678 -0
  46. package/dist/hooks/commit-complete.js +402 -210
  47. package/dist/hooks/error-recall.js +245 -74
  48. package/dist/hooks/exe-heartbeat-hook.js +16 -6
  49. package/dist/hooks/ingest-worker.js +3423 -3157
  50. package/dist/hooks/ingest.js +832 -97
  51. package/dist/hooks/instructions-loaded.js +227 -54
  52. package/dist/hooks/notification.js +216 -43
  53. package/dist/hooks/post-compact.js +239 -62
  54. package/dist/hooks/pre-compact.js +408 -216
  55. package/dist/hooks/pre-tool-use.js +268 -90
  56. package/dist/hooks/prompt-ingest-worker.js +352 -102
  57. package/dist/hooks/prompt-submit.js +541 -328
  58. package/dist/hooks/response-ingest-worker.js +372 -122
  59. package/dist/hooks/session-end.js +443 -240
  60. package/dist/hooks/session-start.js +313 -127
  61. package/dist/hooks/stop.js +293 -98
  62. package/dist/hooks/subagent-stop.js +239 -62
  63. package/dist/hooks/summary-worker.js +568 -236
  64. package/dist/index.js +538 -324
  65. package/dist/lib/agent-config.js +28 -6
  66. package/dist/lib/cloud-sync.js +284 -105
  67. package/dist/lib/config.js +30 -10
  68. package/dist/lib/consolidation.js +16 -6
  69. package/dist/lib/database.js +123 -25
  70. package/dist/lib/db-daemon-client.js +73 -19
  71. package/dist/lib/db.js +123 -25
  72. package/dist/lib/device-registry.js +133 -35
  73. package/dist/lib/embedder.js +107 -32
  74. package/dist/lib/employee-templates.js +14 -4
  75. package/dist/lib/employees.js +41 -13
  76. package/dist/lib/exe-daemon-client.js +88 -22
  77. package/dist/lib/exe-daemon.js +935 -587
  78. package/dist/lib/hybrid-search.js +240 -69
  79. package/dist/lib/identity.js +18 -8
  80. package/dist/lib/license.js +133 -48
  81. package/dist/lib/messaging.js +116 -56
  82. package/dist/lib/reminders.js +14 -4
  83. package/dist/lib/schedules.js +137 -19
  84. package/dist/lib/skill-learning.js +33 -6
  85. package/dist/lib/store.js +137 -19
  86. package/dist/lib/task-router.js +14 -4
  87. package/dist/lib/tasks.js +280 -234
  88. package/dist/lib/tmux-routing.js +172 -125
  89. package/dist/lib/token-spend.js +26 -8
  90. package/dist/mcp/server.js +1326 -609
  91. package/dist/mcp/tools/complete-reminder.js +14 -4
  92. package/dist/mcp/tools/create-reminder.js +14 -4
  93. package/dist/mcp/tools/create-task.js +306 -248
  94. package/dist/mcp/tools/deactivate-behavior.js +16 -6
  95. package/dist/mcp/tools/list-reminders.js +14 -4
  96. package/dist/mcp/tools/list-tasks.js +123 -107
  97. package/dist/mcp/tools/send-message.js +75 -29
  98. package/dist/mcp/tools/update-task.js +1848 -199
  99. package/dist/runtime/index.js +441 -248
  100. package/dist/tui/App.js +761 -424
  101. package/package.json +1 -1
@@ -64,9 +64,34 @@ var init_db_retry = __esm({
64
64
  }
65
65
  });
66
66
 
67
+ // src/lib/secure-files.ts
68
+ import { chmodSync, existsSync, mkdirSync } from "fs";
69
+ import { chmod, mkdir } from "fs/promises";
70
+ async function ensurePrivateDir(dirPath) {
71
+ await mkdir(dirPath, { recursive: true, mode: PRIVATE_DIR_MODE });
72
+ try {
73
+ await chmod(dirPath, PRIVATE_DIR_MODE);
74
+ } catch {
75
+ }
76
+ }
77
+ async function enforcePrivateFile(filePath) {
78
+ try {
79
+ await chmod(filePath, PRIVATE_FILE_MODE);
80
+ } catch {
81
+ }
82
+ }
83
+ var PRIVATE_DIR_MODE, PRIVATE_FILE_MODE;
84
+ var init_secure_files = __esm({
85
+ "src/lib/secure-files.ts"() {
86
+ "use strict";
87
+ PRIVATE_DIR_MODE = 448;
88
+ PRIVATE_FILE_MODE = 384;
89
+ }
90
+ });
91
+
67
92
  // src/lib/config.ts
68
- import { readFile, writeFile, mkdir, chmod } from "fs/promises";
69
- import { readFileSync, existsSync, renameSync } from "fs";
93
+ import { readFile, writeFile } from "fs/promises";
94
+ import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
70
95
  import path from "path";
71
96
  import os from "os";
72
97
  function resolveDataDir() {
@@ -74,7 +99,7 @@ function resolveDataDir() {
74
99
  if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
75
100
  const newDir = path.join(os.homedir(), ".exe-os");
76
101
  const legacyDir = path.join(os.homedir(), ".exe-mem");
77
- if (!existsSync(newDir) && existsSync(legacyDir)) {
102
+ if (!existsSync2(newDir) && existsSync2(legacyDir)) {
78
103
  try {
79
104
  renameSync(legacyDir, newDir);
80
105
  process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
@@ -137,9 +162,9 @@ function normalizeAutoUpdate(raw) {
137
162
  }
138
163
  async function loadConfig() {
139
164
  const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
140
- await mkdir(dir, { recursive: true });
165
+ await ensurePrivateDir(dir);
141
166
  const configPath = path.join(dir, "config.json");
142
- if (!existsSync(configPath)) {
167
+ if (!existsSync2(configPath)) {
143
168
  return { ...DEFAULT_CONFIG, dbPath: path.join(dir, "memories.db") };
144
169
  }
145
170
  const raw = await readFile(configPath, "utf-8");
@@ -152,6 +177,7 @@ async function loadConfig() {
152
177
  `);
153
178
  try {
154
179
  await writeFile(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
180
+ await enforcePrivateFile(configPath);
155
181
  } catch {
156
182
  }
157
183
  }
@@ -171,6 +197,7 @@ var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, LEGACY_LANCE_PATH, CURRENT_CON
171
197
  var init_config = __esm({
172
198
  "src/lib/config.ts"() {
173
199
  "use strict";
200
+ init_secure_files();
174
201
  EXE_AI_DIR = resolveDataDir();
175
202
  DB_PATH = path.join(EXE_AI_DIR, "memories.db");
176
203
  MODELS_DIR = path.join(EXE_AI_DIR, "models");
@@ -249,7 +276,7 @@ var init_config = __esm({
249
276
 
250
277
  // src/lib/employees.ts
251
278
  import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
252
- import { existsSync as existsSync2, symlinkSync, readlinkSync, readFileSync as readFileSync2, renameSync as renameSync2, unlinkSync, writeFileSync } from "fs";
279
+ import { existsSync as existsSync3, symlinkSync, readlinkSync, readFileSync as readFileSync2, renameSync as renameSync2, unlinkSync, writeFileSync } from "fs";
253
280
  import { execSync } from "child_process";
254
281
  import path2 from "path";
255
282
  import os2 from "os";
@@ -266,7 +293,7 @@ function getCoordinatorName(employees = loadEmployeesSync()) {
266
293
  return getCoordinatorEmployee(employees)?.name ?? DEFAULT_COORDINATOR_TEMPLATE_NAME;
267
294
  }
268
295
  function loadEmployeesSync(employeesPath = EMPLOYEES_PATH) {
269
- if (!existsSync2(employeesPath)) return [];
296
+ if (!existsSync3(employeesPath)) return [];
270
297
  try {
271
298
  return JSON.parse(readFileSync2(employeesPath, "utf-8"));
272
299
  } catch {
@@ -1211,6 +1238,7 @@ async function ensureSchema() {
1211
1238
  project TEXT NOT NULL,
1212
1239
  summary TEXT NOT NULL,
1213
1240
  task_file TEXT,
1241
+ session_scope TEXT,
1214
1242
  read INTEGER NOT NULL DEFAULT 0,
1215
1243
  created_at TEXT NOT NULL
1216
1244
  );
@@ -1219,7 +1247,7 @@ async function ensureSchema() {
1219
1247
  ON notifications(read);
1220
1248
 
1221
1249
  CREATE INDEX IF NOT EXISTS idx_notifications_agent
1222
- ON notifications(agent_id);
1250
+ ON notifications(agent_id, session_scope);
1223
1251
 
1224
1252
  CREATE INDEX IF NOT EXISTS idx_notifications_task_file
1225
1253
  ON notifications(task_file);
@@ -1257,6 +1285,7 @@ async function ensureSchema() {
1257
1285
  target_agent TEXT NOT NULL,
1258
1286
  target_project TEXT,
1259
1287
  target_device TEXT NOT NULL DEFAULT 'local',
1288
+ session_scope TEXT,
1260
1289
  content TEXT NOT NULL,
1261
1290
  priority TEXT DEFAULT 'normal',
1262
1291
  status TEXT DEFAULT 'pending',
@@ -1270,10 +1299,31 @@ async function ensureSchema() {
1270
1299
  );
1271
1300
 
1272
1301
  CREATE INDEX IF NOT EXISTS idx_messages_target
1273
- ON messages(target_agent, status);
1302
+ ON messages(target_agent, session_scope, status);
1274
1303
 
1275
1304
  CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
1276
- ON messages(target_agent, from_agent, server_seq);
1305
+ ON messages(target_agent, session_scope, from_agent, server_seq);
1306
+ `);
1307
+ try {
1308
+ await client.execute({
1309
+ sql: `ALTER TABLE notifications ADD COLUMN session_scope TEXT`,
1310
+ args: []
1311
+ });
1312
+ } catch {
1313
+ }
1314
+ try {
1315
+ await client.execute({
1316
+ sql: `ALTER TABLE messages ADD COLUMN session_scope TEXT`,
1317
+ args: []
1318
+ });
1319
+ } catch {
1320
+ }
1321
+ await client.executeMultiple(`
1322
+ CREATE INDEX IF NOT EXISTS idx_notifications_agent_scope_read
1323
+ ON notifications(agent_id, session_scope, read, created_at);
1324
+
1325
+ CREATE INDEX IF NOT EXISTS idx_messages_target_scope_status
1326
+ ON messages(target_agent, session_scope, status, created_at);
1277
1327
  `);
1278
1328
  try {
1279
1329
  await client.execute({
@@ -1857,6 +1907,13 @@ async function ensureSchema() {
1857
1907
  } catch {
1858
1908
  }
1859
1909
  }
1910
+ try {
1911
+ await client.execute({
1912
+ sql: `UPDATE tasks SET status = 'closed' WHERE status = 'done' AND result IS NOT NULL`,
1913
+ args: []
1914
+ });
1915
+ } catch {
1916
+ }
1860
1917
  }
1861
1918
  async function disposeDatabase() {
1862
1919
  if (_walCheckpointTimer) {
@@ -1899,6 +1956,7 @@ var shard_manager_exports = {};
1899
1956
  __export(shard_manager_exports, {
1900
1957
  disposeShards: () => disposeShards,
1901
1958
  ensureShardSchema: () => ensureShardSchema,
1959
+ getOpenShardCount: () => getOpenShardCount,
1902
1960
  getReadyShardClient: () => getReadyShardClient,
1903
1961
  getShardClient: () => getShardClient,
1904
1962
  getShardsDir: () => getShardsDir,
@@ -1908,14 +1966,17 @@ __export(shard_manager_exports, {
1908
1966
  shardExists: () => shardExists
1909
1967
  });
1910
1968
  import path5 from "path";
1911
- import { existsSync as existsSync4, mkdirSync, readdirSync } from "fs";
1969
+ import { existsSync as existsSync5, mkdirSync as mkdirSync2, readdirSync } from "fs";
1912
1970
  import { createClient as createClient2 } from "@libsql/client";
1913
1971
  function initShardManager(encryptionKey) {
1914
1972
  _encryptionKey = encryptionKey;
1915
- if (!existsSync4(SHARDS_DIR)) {
1916
- mkdirSync(SHARDS_DIR, { recursive: true });
1973
+ if (!existsSync5(SHARDS_DIR)) {
1974
+ mkdirSync2(SHARDS_DIR, { recursive: true });
1917
1975
  }
1918
1976
  _shardingEnabled = true;
1977
+ if (_evictionTimer) clearInterval(_evictionTimer);
1978
+ _evictionTimer = setInterval(evictIdleShards, EVICTION_INTERVAL_MS);
1979
+ _evictionTimer.unref();
1919
1980
  }
1920
1981
  function isShardingEnabled() {
1921
1982
  return _shardingEnabled;
@@ -1932,21 +1993,28 @@ function getShardClient(projectName) {
1932
1993
  throw new Error(`Invalid project name for shard: "${projectName}"`);
1933
1994
  }
1934
1995
  const cached = _shards.get(safeName);
1935
- if (cached) return cached;
1996
+ if (cached) {
1997
+ _shardLastAccess.set(safeName, Date.now());
1998
+ return cached;
1999
+ }
2000
+ while (_shards.size >= MAX_OPEN_SHARDS) {
2001
+ evictLRU();
2002
+ }
1936
2003
  const dbPath = path5.join(SHARDS_DIR, `${safeName}.db`);
1937
2004
  const client = createClient2({
1938
2005
  url: `file:${dbPath}`,
1939
2006
  encryptionKey: _encryptionKey
1940
2007
  });
1941
2008
  _shards.set(safeName, client);
2009
+ _shardLastAccess.set(safeName, Date.now());
1942
2010
  return client;
1943
2011
  }
1944
2012
  function shardExists(projectName) {
1945
2013
  const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
1946
- return existsSync4(path5.join(SHARDS_DIR, `${safeName}.db`));
2014
+ return existsSync5(path5.join(SHARDS_DIR, `${safeName}.db`));
1947
2015
  }
1948
2016
  function listShards() {
1949
- if (!existsSync4(SHARDS_DIR)) return [];
2017
+ if (!existsSync5(SHARDS_DIR)) return [];
1950
2018
  return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
1951
2019
  }
1952
2020
  async function ensureShardSchema(client) {
@@ -1998,6 +2066,8 @@ async function ensureShardSchema(client) {
1998
2066
  for (const col of [
1999
2067
  "ALTER TABLE memories ADD COLUMN task_id TEXT",
2000
2068
  "ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
2069
+ "ALTER TABLE memories ADD COLUMN author_device_id TEXT",
2070
+ "ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'",
2001
2071
  "ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
2002
2072
  "ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
2003
2073
  "ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
@@ -2135,21 +2205,69 @@ async function getReadyShardClient(projectName) {
2135
2205
  await ensureShardSchema(client);
2136
2206
  return client;
2137
2207
  }
2208
+ function evictLRU() {
2209
+ let oldest = null;
2210
+ let oldestTime = Infinity;
2211
+ for (const [name, time] of _shardLastAccess) {
2212
+ if (time < oldestTime) {
2213
+ oldestTime = time;
2214
+ oldest = name;
2215
+ }
2216
+ }
2217
+ if (oldest) {
2218
+ const client = _shards.get(oldest);
2219
+ if (client) {
2220
+ client.close();
2221
+ }
2222
+ _shards.delete(oldest);
2223
+ _shardLastAccess.delete(oldest);
2224
+ }
2225
+ }
2226
+ function evictIdleShards() {
2227
+ const now = Date.now();
2228
+ const toEvict = [];
2229
+ for (const [name, lastAccess] of _shardLastAccess) {
2230
+ if (now - lastAccess > SHARD_IDLE_MS) {
2231
+ toEvict.push(name);
2232
+ }
2233
+ }
2234
+ for (const name of toEvict) {
2235
+ const client = _shards.get(name);
2236
+ if (client) {
2237
+ client.close();
2238
+ }
2239
+ _shards.delete(name);
2240
+ _shardLastAccess.delete(name);
2241
+ }
2242
+ }
2243
+ function getOpenShardCount() {
2244
+ return _shards.size;
2245
+ }
2138
2246
  function disposeShards() {
2247
+ if (_evictionTimer) {
2248
+ clearInterval(_evictionTimer);
2249
+ _evictionTimer = null;
2250
+ }
2139
2251
  for (const [, client] of _shards) {
2140
2252
  client.close();
2141
2253
  }
2142
2254
  _shards.clear();
2255
+ _shardLastAccess.clear();
2143
2256
  _shardingEnabled = false;
2144
2257
  _encryptionKey = null;
2145
2258
  }
2146
- var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
2259
+ var SHARDS_DIR, SHARD_IDLE_MS, MAX_OPEN_SHARDS, EVICTION_INTERVAL_MS, _shards, _shardLastAccess, _evictionTimer, _encryptionKey, _shardingEnabled;
2147
2260
  var init_shard_manager = __esm({
2148
2261
  "src/lib/shard-manager.ts"() {
2149
2262
  "use strict";
2150
2263
  init_config();
2151
2264
  SHARDS_DIR = path5.join(EXE_AI_DIR, "shards");
2265
+ SHARD_IDLE_MS = 5 * 60 * 1e3;
2266
+ MAX_OPEN_SHARDS = 10;
2267
+ EVICTION_INTERVAL_MS = 60 * 1e3;
2152
2268
  _shards = /* @__PURE__ */ new Map();
2269
+ _shardLastAccess = /* @__PURE__ */ new Map();
2270
+ _evictionTimer = null;
2153
2271
  _encryptionKey = null;
2154
2272
  _shardingEnabled = false;
2155
2273
  }
@@ -2348,7 +2466,7 @@ init_database();
2348
2466
 
2349
2467
  // src/lib/keychain.ts
2350
2468
  import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod as chmod2 } from "fs/promises";
2351
- import { existsSync as existsSync3 } from "fs";
2469
+ import { existsSync as existsSync4 } from "fs";
2352
2470
  import path4 from "path";
2353
2471
  import os4 from "os";
2354
2472
  var SERVICE = "exe-mem";
@@ -2378,7 +2496,7 @@ async function getMasterKey() {
2378
2496
  }
2379
2497
  }
2380
2498
  const keyPath = getKeyPath();
2381
- if (!existsSync3(keyPath)) {
2499
+ if (!existsSync4(keyPath)) {
2382
2500
  process.stderr.write(
2383
2501
  `[keychain] Key not found at ${keyPath} (HOME=${os4.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
2384
2502
  `
@@ -64,9 +64,34 @@ var init_db_retry = __esm({
64
64
  }
65
65
  });
66
66
 
67
+ // src/lib/secure-files.ts
68
+ import { chmodSync, existsSync, mkdirSync } from "fs";
69
+ import { chmod, mkdir } from "fs/promises";
70
+ async function ensurePrivateDir(dirPath) {
71
+ await mkdir(dirPath, { recursive: true, mode: PRIVATE_DIR_MODE });
72
+ try {
73
+ await chmod(dirPath, PRIVATE_DIR_MODE);
74
+ } catch {
75
+ }
76
+ }
77
+ async function enforcePrivateFile(filePath) {
78
+ try {
79
+ await chmod(filePath, PRIVATE_FILE_MODE);
80
+ } catch {
81
+ }
82
+ }
83
+ var PRIVATE_DIR_MODE, PRIVATE_FILE_MODE;
84
+ var init_secure_files = __esm({
85
+ "src/lib/secure-files.ts"() {
86
+ "use strict";
87
+ PRIVATE_DIR_MODE = 448;
88
+ PRIVATE_FILE_MODE = 384;
89
+ }
90
+ });
91
+
67
92
  // src/lib/config.ts
68
- import { readFile, writeFile, mkdir, chmod } from "fs/promises";
69
- import { readFileSync, existsSync, renameSync } from "fs";
93
+ import { readFile, writeFile } from "fs/promises";
94
+ import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
70
95
  import path from "path";
71
96
  import os from "os";
72
97
  function resolveDataDir() {
@@ -74,7 +99,7 @@ function resolveDataDir() {
74
99
  if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
75
100
  const newDir = path.join(os.homedir(), ".exe-os");
76
101
  const legacyDir = path.join(os.homedir(), ".exe-mem");
77
- if (!existsSync(newDir) && existsSync(legacyDir)) {
102
+ if (!existsSync2(newDir) && existsSync2(legacyDir)) {
78
103
  try {
79
104
  renameSync(legacyDir, newDir);
80
105
  process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
@@ -137,9 +162,9 @@ function normalizeAutoUpdate(raw) {
137
162
  }
138
163
  async function loadConfig() {
139
164
  const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
140
- await mkdir(dir, { recursive: true });
165
+ await ensurePrivateDir(dir);
141
166
  const configPath = path.join(dir, "config.json");
142
- if (!existsSync(configPath)) {
167
+ if (!existsSync2(configPath)) {
143
168
  return { ...DEFAULT_CONFIG, dbPath: path.join(dir, "memories.db") };
144
169
  }
145
170
  const raw = await readFile(configPath, "utf-8");
@@ -152,6 +177,7 @@ async function loadConfig() {
152
177
  `);
153
178
  try {
154
179
  await writeFile(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
180
+ await enforcePrivateFile(configPath);
155
181
  } catch {
156
182
  }
157
183
  }
@@ -171,6 +197,7 @@ var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, LEGACY_LANCE_PATH, CURRENT_CON
171
197
  var init_config = __esm({
172
198
  "src/lib/config.ts"() {
173
199
  "use strict";
200
+ init_secure_files();
174
201
  EXE_AI_DIR = resolveDataDir();
175
202
  DB_PATH = path.join(EXE_AI_DIR, "memories.db");
176
203
  MODELS_DIR = path.join(EXE_AI_DIR, "models");
@@ -249,7 +276,7 @@ var init_config = __esm({
249
276
 
250
277
  // src/lib/employees.ts
251
278
  import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
252
- import { existsSync as existsSync2, symlinkSync, readlinkSync, readFileSync as readFileSync2, renameSync as renameSync2, unlinkSync, writeFileSync } from "fs";
279
+ import { existsSync as existsSync3, symlinkSync, readlinkSync, readFileSync as readFileSync2, renameSync as renameSync2, unlinkSync, writeFileSync } from "fs";
253
280
  import { execSync } from "child_process";
254
281
  import path2 from "path";
255
282
  import os2 from "os";
@@ -266,7 +293,7 @@ function getCoordinatorName(employees = loadEmployeesSync()) {
266
293
  return getCoordinatorEmployee(employees)?.name ?? DEFAULT_COORDINATOR_TEMPLATE_NAME;
267
294
  }
268
295
  function loadEmployeesSync(employeesPath = EMPLOYEES_PATH) {
269
- if (!existsSync2(employeesPath)) return [];
296
+ if (!existsSync3(employeesPath)) return [];
270
297
  try {
271
298
  return JSON.parse(readFileSync2(employeesPath, "utf-8"));
272
299
  } catch {
@@ -1211,6 +1238,7 @@ async function ensureSchema() {
1211
1238
  project TEXT NOT NULL,
1212
1239
  summary TEXT NOT NULL,
1213
1240
  task_file TEXT,
1241
+ session_scope TEXT,
1214
1242
  read INTEGER NOT NULL DEFAULT 0,
1215
1243
  created_at TEXT NOT NULL
1216
1244
  );
@@ -1219,7 +1247,7 @@ async function ensureSchema() {
1219
1247
  ON notifications(read);
1220
1248
 
1221
1249
  CREATE INDEX IF NOT EXISTS idx_notifications_agent
1222
- ON notifications(agent_id);
1250
+ ON notifications(agent_id, session_scope);
1223
1251
 
1224
1252
  CREATE INDEX IF NOT EXISTS idx_notifications_task_file
1225
1253
  ON notifications(task_file);
@@ -1257,6 +1285,7 @@ async function ensureSchema() {
1257
1285
  target_agent TEXT NOT NULL,
1258
1286
  target_project TEXT,
1259
1287
  target_device TEXT NOT NULL DEFAULT 'local',
1288
+ session_scope TEXT,
1260
1289
  content TEXT NOT NULL,
1261
1290
  priority TEXT DEFAULT 'normal',
1262
1291
  status TEXT DEFAULT 'pending',
@@ -1270,10 +1299,31 @@ async function ensureSchema() {
1270
1299
  );
1271
1300
 
1272
1301
  CREATE INDEX IF NOT EXISTS idx_messages_target
1273
- ON messages(target_agent, status);
1302
+ ON messages(target_agent, session_scope, status);
1274
1303
 
1275
1304
  CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
1276
- ON messages(target_agent, from_agent, server_seq);
1305
+ ON messages(target_agent, session_scope, from_agent, server_seq);
1306
+ `);
1307
+ try {
1308
+ await client.execute({
1309
+ sql: `ALTER TABLE notifications ADD COLUMN session_scope TEXT`,
1310
+ args: []
1311
+ });
1312
+ } catch {
1313
+ }
1314
+ try {
1315
+ await client.execute({
1316
+ sql: `ALTER TABLE messages ADD COLUMN session_scope TEXT`,
1317
+ args: []
1318
+ });
1319
+ } catch {
1320
+ }
1321
+ await client.executeMultiple(`
1322
+ CREATE INDEX IF NOT EXISTS idx_notifications_agent_scope_read
1323
+ ON notifications(agent_id, session_scope, read, created_at);
1324
+
1325
+ CREATE INDEX IF NOT EXISTS idx_messages_target_scope_status
1326
+ ON messages(target_agent, session_scope, status, created_at);
1277
1327
  `);
1278
1328
  try {
1279
1329
  await client.execute({
@@ -1857,6 +1907,13 @@ async function ensureSchema() {
1857
1907
  } catch {
1858
1908
  }
1859
1909
  }
1910
+ try {
1911
+ await client.execute({
1912
+ sql: `UPDATE tasks SET status = 'closed' WHERE status = 'done' AND result IS NOT NULL`,
1913
+ args: []
1914
+ });
1915
+ } catch {
1916
+ }
1860
1917
  }
1861
1918
  async function disposeDatabase() {
1862
1919
  if (_walCheckpointTimer) {
@@ -1899,6 +1956,7 @@ var shard_manager_exports = {};
1899
1956
  __export(shard_manager_exports, {
1900
1957
  disposeShards: () => disposeShards,
1901
1958
  ensureShardSchema: () => ensureShardSchema,
1959
+ getOpenShardCount: () => getOpenShardCount,
1902
1960
  getReadyShardClient: () => getReadyShardClient,
1903
1961
  getShardClient: () => getShardClient,
1904
1962
  getShardsDir: () => getShardsDir,
@@ -1908,14 +1966,17 @@ __export(shard_manager_exports, {
1908
1966
  shardExists: () => shardExists
1909
1967
  });
1910
1968
  import path5 from "path";
1911
- import { existsSync as existsSync4, mkdirSync, readdirSync } from "fs";
1969
+ import { existsSync as existsSync5, mkdirSync as mkdirSync2, readdirSync } from "fs";
1912
1970
  import { createClient as createClient2 } from "@libsql/client";
1913
1971
  function initShardManager(encryptionKey) {
1914
1972
  _encryptionKey = encryptionKey;
1915
- if (!existsSync4(SHARDS_DIR)) {
1916
- mkdirSync(SHARDS_DIR, { recursive: true });
1973
+ if (!existsSync5(SHARDS_DIR)) {
1974
+ mkdirSync2(SHARDS_DIR, { recursive: true });
1917
1975
  }
1918
1976
  _shardingEnabled = true;
1977
+ if (_evictionTimer) clearInterval(_evictionTimer);
1978
+ _evictionTimer = setInterval(evictIdleShards, EVICTION_INTERVAL_MS);
1979
+ _evictionTimer.unref();
1919
1980
  }
1920
1981
  function isShardingEnabled() {
1921
1982
  return _shardingEnabled;
@@ -1932,21 +1993,28 @@ function getShardClient(projectName) {
1932
1993
  throw new Error(`Invalid project name for shard: "${projectName}"`);
1933
1994
  }
1934
1995
  const cached = _shards.get(safeName);
1935
- if (cached) return cached;
1996
+ if (cached) {
1997
+ _shardLastAccess.set(safeName, Date.now());
1998
+ return cached;
1999
+ }
2000
+ while (_shards.size >= MAX_OPEN_SHARDS) {
2001
+ evictLRU();
2002
+ }
1936
2003
  const dbPath = path5.join(SHARDS_DIR, `${safeName}.db`);
1937
2004
  const client = createClient2({
1938
2005
  url: `file:${dbPath}`,
1939
2006
  encryptionKey: _encryptionKey
1940
2007
  });
1941
2008
  _shards.set(safeName, client);
2009
+ _shardLastAccess.set(safeName, Date.now());
1942
2010
  return client;
1943
2011
  }
1944
2012
  function shardExists(projectName) {
1945
2013
  const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
1946
- return existsSync4(path5.join(SHARDS_DIR, `${safeName}.db`));
2014
+ return existsSync5(path5.join(SHARDS_DIR, `${safeName}.db`));
1947
2015
  }
1948
2016
  function listShards() {
1949
- if (!existsSync4(SHARDS_DIR)) return [];
2017
+ if (!existsSync5(SHARDS_DIR)) return [];
1950
2018
  return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
1951
2019
  }
1952
2020
  async function ensureShardSchema(client) {
@@ -1998,6 +2066,8 @@ async function ensureShardSchema(client) {
1998
2066
  for (const col of [
1999
2067
  "ALTER TABLE memories ADD COLUMN task_id TEXT",
2000
2068
  "ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
2069
+ "ALTER TABLE memories ADD COLUMN author_device_id TEXT",
2070
+ "ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'",
2001
2071
  "ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
2002
2072
  "ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
2003
2073
  "ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
@@ -2135,21 +2205,69 @@ async function getReadyShardClient(projectName) {
2135
2205
  await ensureShardSchema(client);
2136
2206
  return client;
2137
2207
  }
2208
+ function evictLRU() {
2209
+ let oldest = null;
2210
+ let oldestTime = Infinity;
2211
+ for (const [name, time] of _shardLastAccess) {
2212
+ if (time < oldestTime) {
2213
+ oldestTime = time;
2214
+ oldest = name;
2215
+ }
2216
+ }
2217
+ if (oldest) {
2218
+ const client = _shards.get(oldest);
2219
+ if (client) {
2220
+ client.close();
2221
+ }
2222
+ _shards.delete(oldest);
2223
+ _shardLastAccess.delete(oldest);
2224
+ }
2225
+ }
2226
+ function evictIdleShards() {
2227
+ const now = Date.now();
2228
+ const toEvict = [];
2229
+ for (const [name, lastAccess] of _shardLastAccess) {
2230
+ if (now - lastAccess > SHARD_IDLE_MS) {
2231
+ toEvict.push(name);
2232
+ }
2233
+ }
2234
+ for (const name of toEvict) {
2235
+ const client = _shards.get(name);
2236
+ if (client) {
2237
+ client.close();
2238
+ }
2239
+ _shards.delete(name);
2240
+ _shardLastAccess.delete(name);
2241
+ }
2242
+ }
2243
+ function getOpenShardCount() {
2244
+ return _shards.size;
2245
+ }
2138
2246
  function disposeShards() {
2247
+ if (_evictionTimer) {
2248
+ clearInterval(_evictionTimer);
2249
+ _evictionTimer = null;
2250
+ }
2139
2251
  for (const [, client] of _shards) {
2140
2252
  client.close();
2141
2253
  }
2142
2254
  _shards.clear();
2255
+ _shardLastAccess.clear();
2143
2256
  _shardingEnabled = false;
2144
2257
  _encryptionKey = null;
2145
2258
  }
2146
- var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
2259
+ var SHARDS_DIR, SHARD_IDLE_MS, MAX_OPEN_SHARDS, EVICTION_INTERVAL_MS, _shards, _shardLastAccess, _evictionTimer, _encryptionKey, _shardingEnabled;
2147
2260
  var init_shard_manager = __esm({
2148
2261
  "src/lib/shard-manager.ts"() {
2149
2262
  "use strict";
2150
2263
  init_config();
2151
2264
  SHARDS_DIR = path5.join(EXE_AI_DIR, "shards");
2265
+ SHARD_IDLE_MS = 5 * 60 * 1e3;
2266
+ MAX_OPEN_SHARDS = 10;
2267
+ EVICTION_INTERVAL_MS = 60 * 1e3;
2152
2268
  _shards = /* @__PURE__ */ new Map();
2269
+ _shardLastAccess = /* @__PURE__ */ new Map();
2270
+ _evictionTimer = null;
2153
2271
  _encryptionKey = null;
2154
2272
  _shardingEnabled = false;
2155
2273
  }
@@ -2343,7 +2461,7 @@ ${p.content}`).join("\n\n");
2343
2461
  });
2344
2462
 
2345
2463
  // src/bin/graph-export.ts
2346
- import { writeFileSync as writeFileSync2, mkdirSync as mkdirSync2, existsSync as existsSync5 } from "fs";
2464
+ import { writeFileSync as writeFileSync2, mkdirSync as mkdirSync3, existsSync as existsSync6 } from "fs";
2347
2465
  import path6 from "path";
2348
2466
 
2349
2467
  // src/lib/store.ts
@@ -2352,7 +2470,7 @@ init_database();
2352
2470
 
2353
2471
  // src/lib/keychain.ts
2354
2472
  import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod as chmod2 } from "fs/promises";
2355
- import { existsSync as existsSync3 } from "fs";
2473
+ import { existsSync as existsSync4 } from "fs";
2356
2474
  import path4 from "path";
2357
2475
  import os4 from "os";
2358
2476
  var SERVICE = "exe-mem";
@@ -2382,7 +2500,7 @@ async function getMasterKey() {
2382
2500
  }
2383
2501
  }
2384
2502
  const keyPath = getKeyPath();
2385
- if (!existsSync3(keyPath)) {
2503
+ if (!existsSync4(keyPath)) {
2386
2504
  process.stderr.write(
2387
2505
  `[keychain] Key not found at ${keyPath} (HOME=${os4.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
2388
2506
  `
@@ -2914,7 +3032,7 @@ async function main() {
2914
3032
  const report = await generateGraphReport(client, project);
2915
3033
  const reportPath = output ?? "exe/output/GRAPH_REPORT.md";
2916
3034
  const dir = path6.dirname(reportPath);
2917
- if (!existsSync5(dir)) mkdirSync2(dir, { recursive: true });
3035
+ if (!existsSync6(dir)) mkdirSync3(dir, { recursive: true });
2918
3036
  writeFileSync2(reportPath, report);
2919
3037
  process.stderr.write(`[graph-export] Report written to ${reportPath}
2920
3038
  `);
@@ -2922,7 +3040,7 @@ async function main() {
2922
3040
  const html = await exportGraphHTML(client, project);
2923
3041
  const htmlPath = output ?? "exe/output/graph.html";
2924
3042
  const dir = path6.dirname(htmlPath);
2925
- if (!existsSync5(dir)) mkdirSync2(dir, { recursive: true });
3043
+ if (!existsSync6(dir)) mkdirSync3(dir, { recursive: true });
2926
3044
  writeFileSync2(htmlPath, html);
2927
3045
  process.stderr.write(`[graph-export] HTML written to ${htmlPath}
2928
3046
  `);