@theokit/sdk 4.61.0 → 4.63.0

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 (114) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/dist/{agent-P42GA6RM.js → agent-536OOESZ.js} +8 -6
  3. package/dist/{agent-P42GA6RM.js.map → agent-536OOESZ.js.map} +1 -1
  4. package/dist/{agent-M53C3PAA.cjs → agent-5KYMG63I.cjs} +9 -7
  5. package/dist/{agent-M53C3PAA.cjs.map → agent-5KYMG63I.cjs.map} +1 -1
  6. package/dist/{agent-DIu6FooJ.d.ts → agent-BFno7Sfn.d.ts} +36 -21
  7. package/dist/{agent-D3Xr_-6Z.d.cts → agent-X0DKA943.d.cts} +36 -21
  8. package/dist/chunk-A4RAL2ER.cjs +51 -0
  9. package/dist/chunk-A4RAL2ER.cjs.map +1 -0
  10. package/dist/chunk-A4VHSE56.cjs +21 -0
  11. package/dist/chunk-A4VHSE56.cjs.map +1 -0
  12. package/dist/{chunk-CQ2TQ32Y.js → chunk-AMFXSENK.js} +3 -48
  13. package/dist/chunk-AMFXSENK.js.map +1 -0
  14. package/dist/chunk-C7QPML3L.js +831 -0
  15. package/dist/chunk-C7QPML3L.js.map +1 -0
  16. package/dist/{chunk-5IT6DUOO.cjs → chunk-CASUEY42.cjs} +4 -4
  17. package/dist/{chunk-5IT6DUOO.cjs.map → chunk-CASUEY42.cjs.map} +1 -1
  18. package/dist/{chunk-GQZKDGZM.js → chunk-CHKH5452.js} +90 -70
  19. package/dist/chunk-CHKH5452.js.map +1 -0
  20. package/dist/chunk-EHJEZAOO.cjs +864 -0
  21. package/dist/chunk-EHJEZAOO.cjs.map +1 -0
  22. package/dist/chunk-IRCJ7EHV.cjs +201 -0
  23. package/dist/chunk-IRCJ7EHV.cjs.map +1 -0
  24. package/dist/chunk-LW7G5DYW.js +19 -0
  25. package/dist/chunk-LW7G5DYW.js.map +1 -0
  26. package/dist/{chunk-532CSLYU.js → chunk-MG3SOM3M.js} +3 -3
  27. package/dist/{chunk-532CSLYU.js.map → chunk-MG3SOM3M.js.map} +1 -1
  28. package/dist/{chunk-R7YIVL3K.cjs → chunk-NLXOGBRJ.cjs} +98 -78
  29. package/dist/chunk-NLXOGBRJ.cjs.map +1 -0
  30. package/dist/{chunk-SKXBJ2NU.cjs → chunk-NXH4GPAQ.cjs} +2 -49
  31. package/dist/chunk-NXH4GPAQ.cjs.map +1 -0
  32. package/dist/chunk-R6TA2DRJ.js +194 -0
  33. package/dist/chunk-R6TA2DRJ.js.map +1 -0
  34. package/dist/{chunk-6OBIWHDR.cjs → chunk-UUP3MUZ6.cjs} +20 -361
  35. package/dist/chunk-UUP3MUZ6.cjs.map +1 -0
  36. package/dist/chunk-WE22OXQA.js +48 -0
  37. package/dist/chunk-WE22OXQA.js.map +1 -0
  38. package/dist/{chunk-QEKI3YKI.js → chunk-YTR3RBUZ.js} +13 -348
  39. package/dist/chunk-YTR3RBUZ.js.map +1 -0
  40. package/dist/{cron-BcWmzWzT.d.cts → cron-BuiRPrvt.d.cts} +1 -1
  41. package/dist/{cron-De6hzWCF.d.ts → cron-C2SIi31n.d.ts} +1 -1
  42. package/dist/cron.cjs +8 -6
  43. package/dist/cron.d.cts +2 -2
  44. package/dist/cron.d.ts +2 -2
  45. package/dist/cron.js +7 -5
  46. package/dist/eval.cjs +7 -5
  47. package/dist/eval.cjs.map +1 -1
  48. package/dist/eval.js +6 -4
  49. package/dist/eval.js.map +1 -1
  50. package/dist/{index-manager-RHPFVFSC.cjs → index-manager-6TEEIWM7.cjs} +7 -6
  51. package/dist/{index-manager-RHPFVFSC.cjs.map → index-manager-6TEEIWM7.cjs.map} +1 -1
  52. package/dist/{index-manager-AHAYJ33H.js → index-manager-ACEQG3IJ.js} +6 -5
  53. package/dist/{index-manager-AHAYJ33H.js.map → index-manager-ACEQG3IJ.js.map} +1 -1
  54. package/dist/index.cjs +35 -208
  55. package/dist/index.cjs.map +1 -1
  56. package/dist/index.d.cts +8 -3
  57. package/dist/index.d.ts +8 -3
  58. package/dist/index.js +22 -195
  59. package/dist/index.js.map +1 -1
  60. package/dist/internal/memory/active-memory.d.ts +8 -1
  61. package/dist/internal/memory/dreaming/diary.d.ts +39 -2
  62. package/dist/internal/memory/index-db.d.ts +44 -1
  63. package/dist/internal/memory/index-manager-contract.d.ts +10 -0
  64. package/dist/internal/memory/index-manager-helpers.d.ts +20 -1
  65. package/dist/internal/memory/index-manager.d.ts +1 -1
  66. package/dist/internal/memory/lance-index.d.ts +15 -1
  67. package/dist/internal/memory/migrate-sqlite-to-lance.d.ts +43 -0
  68. package/dist/internal/memory/storage/index.cjs +95 -12
  69. package/dist/internal/memory/storage/index.d.cts +11 -1
  70. package/dist/internal/memory/storage/index.d.ts +11 -1
  71. package/dist/internal/memory/storage/index.js +4 -1
  72. package/dist/internal/memory/storage/markdown-store.d.cts +13 -42
  73. package/dist/internal/memory/storage/markdown-store.d.ts +13 -42
  74. package/dist/internal/memory/storage/memory-root.d.cts +93 -0
  75. package/dist/internal/memory/storage/memory-root.d.ts +93 -0
  76. package/dist/internal/memory/storage/session-loader.d.cts +35 -1
  77. package/dist/internal/memory/storage/session-loader.d.ts +35 -1
  78. package/dist/internal/memory/storage/session-summary-writer.d.cts +50 -2
  79. package/dist/internal/memory/storage/session-summary-writer.d.ts +50 -2
  80. package/dist/internal/memory/storage/transcript-store.d.cts +38 -1
  81. package/dist/internal/memory/storage/transcript-store.d.ts +38 -1
  82. package/dist/internal/memory/storage/wiki-loader.d.cts +27 -2
  83. package/dist/internal/memory/storage/wiki-loader.d.ts +27 -2
  84. package/dist/internal/memory/tools.d.ts +8 -1
  85. package/dist/internal/memory/types.d.ts +5 -0
  86. package/dist/internal/persistence/index.cjs +12 -11
  87. package/dist/internal/persistence/index.cjs.map +1 -1
  88. package/dist/internal/persistence/index.js +2 -1
  89. package/dist/internal/persistence/index.js.map +1 -1
  90. package/dist/internal/persistence/session-dir.d.cts +0 -15
  91. package/dist/internal/persistence/session-dir.d.ts +0 -15
  92. package/dist/internal/runtime/memory/memory-store.d.ts +2 -2
  93. package/dist/persistence.cjs +9 -8
  94. package/dist/persistence.cjs.map +1 -1
  95. package/dist/persistence.js +2 -1
  96. package/dist/persistence.js.map +1 -1
  97. package/dist/subagents-loader.d.cts +1 -1
  98. package/dist/subagents-loader.d.ts +1 -1
  99. package/dist/types/agent.d.ts +11 -0
  100. package/dist/types/memory-provider.d.ts +8 -0
  101. package/docs/error-codes.md +10 -9
  102. package/docs/harness-capability-map.md +57 -17
  103. package/docs/memory-decisions.md +44 -7
  104. package/package.json +1 -1
  105. package/dist/chunk-2UCFUSPW.cjs +0 -468
  106. package/dist/chunk-2UCFUSPW.cjs.map +0 -1
  107. package/dist/chunk-6OBIWHDR.cjs.map +0 -1
  108. package/dist/chunk-CQ2TQ32Y.js.map +0 -1
  109. package/dist/chunk-GQZKDGZM.js.map +0 -1
  110. package/dist/chunk-QEKI3YKI.js.map +0 -1
  111. package/dist/chunk-R7YIVL3K.cjs.map +0 -1
  112. package/dist/chunk-SKXBJ2NU.cjs.map +0 -1
  113. package/dist/chunk-WCLDJSMY.js +0 -456
  114. package/dist/chunk-WCLDJSMY.js.map +0 -1
@@ -5,51 +5,6 @@ var chunkNUKRL3I6_cjs = require('./chunk-NUKRL3I6.cjs');
5
5
  var promises = require('fs/promises');
6
6
  var path = require('path');
7
7
 
8
- // src/internal/persistence/fts5-sanitize.ts
9
- var PHRASE_OPEN = "";
10
- var PHRASE_CLOSE = "";
11
- function sanitizeFts5Query(query) {
12
- if (query.length === 0) return query;
13
- const phrases = [];
14
- let text = query.replace(/"[^"]+"/g, (match) => {
15
- phrases.push(match);
16
- return `${PHRASE_OPEN}${phrases.length - 1}${PHRASE_CLOSE}`;
17
- });
18
- text = text.replace(/[[\]{}()"^]/g, " ");
19
- text = text.replace(/\*+/g, "*");
20
- text = text.replace(/^\s*(AND|OR|NOT)\s+/i, "");
21
- text = text.replace(/\s+(AND|OR|NOT)\s*$/i, "");
22
- text = text.replace(/\b\w+[-._]\w[\w\-._]*\b/g, (match) => `"${match}"`);
23
- for (let i = 0; i < phrases.length; i += 1) {
24
- text = text.replace(`${PHRASE_OPEN}${i}${PHRASE_CLOSE}`, phrases[i] ?? "");
25
- }
26
- return text.trim();
27
- }
28
- var CJK_RANGES = [
29
- [12288, 12351],
30
- // CJK Symbols and Punctuation
31
- [12352, 12447],
32
- // Hiragana
33
- [12448, 12543],
34
- // Katakana
35
- [13312, 19903],
36
- // CJK Unified Ideographs Extension A
37
- [19968, 40959],
38
- // CJK Unified Ideographs
39
- [44032, 55215]
40
- // Hangul Syllables
41
- ];
42
- function containsCjk(text) {
43
- for (const char of text) {
44
- const cp = char.codePointAt(0);
45
- if (cp === void 0) continue;
46
- for (const [lo, hi] of CJK_RANGES) {
47
- if (cp >= lo && cp <= hi) return true;
48
- }
49
- }
50
- return false;
51
- }
52
-
53
8
  // src/internal/persistence/sqlite-wal.ts
54
9
  var warnedLabels = /* @__PURE__ */ new Set();
55
10
  function applyWalWithFallback(db, label) {
@@ -164,9 +119,7 @@ async function renameAside(filePath, label) {
164
119
  }
165
120
 
166
121
  exports.applyWalWithFallback = applyWalWithFallback;
167
- exports.containsCjk = containsCjk;
168
122
  exports.isCorruptionError = isCorruptionError;
169
123
  exports.openSqliteResilient = openSqliteResilient;
170
- exports.sanitizeFts5Query = sanitizeFts5Query;
171
- //# sourceMappingURL=chunk-SKXBJ2NU.cjs.map
172
- //# sourceMappingURL=chunk-SKXBJ2NU.cjs.map
124
+ //# sourceMappingURL=chunk-NXH4GPAQ.cjs.map
125
+ //# sourceMappingURL=chunk-NXH4GPAQ.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/internal/persistence/sqlite-wal.ts","../src/internal/persistence/sqlite-open.ts"],"names":["diag","mkdir","dirname","ConfigurationError","rename"],"mappings":";;;;;;;;AA+CA,IAAM,YAAA,uBAAmB,GAAA,EAAY;AAU9B,SAAS,oBAAA,CAAqB,I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,EAAiB,KAAK,CAAA,oCAAA,EAAuC,SAAS,CAAA;AAAA;AAAA,GACxE;AACF","file":"chunk-NXH4GPAQ.cjs","sourcesContent":["/**\n * SQLite WAL mode helper with NFS/SMB/FUSE fallback to DELETE (ADR D63).\n *\n * WAL is faster (concurrent readers + one writer) but unsupported on some\n * network/FUSE filesystems. Try WAL; if the pragma returns something else\n * or throws, fall back to DELETE journal mode. Warn one time per label.\n *\n * This block is the MODULE header and must stay at offset 0. Sitting below the import it became a\n * leading comment of the first declaration instead, which is how its tag deleted `PragmaCapable`\n * from the emitted `.d.ts` while `applyWalWithFallback` — which names it — survived.\n */\n\nimport { diag } from \"../diagnostics.js\";\n\n/**\n * The narrow slice of a SQLite handle this module needs: anything with a `pragma()` method, which\n * `better-sqlite3` satisfies without being named here.\n *\n * Not exported, but it appears in the signature of `applyWalWithFallback`, so it is emitted into\n * the published declarations and a consumer can structurally satisfy it.\n */\ninterface PragmaCapable {\n pragma: (statement: string, options?: { simple?: boolean }) => unknown;\n}\n\n/**\n * What journal mode a connection ended up in after `applyWalWithFallback`.\n *\n * `mode` is the mode actually in effect, never the one that was requested. `fellBack` is `true`\n * only when WAL was attempted and refused — either the pragma threw or it reported a mode other\n * than `wal` — and the connection was put into DELETE instead.\n *\n * `fellBack: true` is normal on NFS, SMB and FUSE, where WAL needs shared memory the filesystem\n * does not provide. It is not an error and nothing further is required of the caller; the\n * consequence is slower concurrent access, since DELETE mode does not allow readers alongside a\n * writer. Treat it as a signal about the storage, not about the database.\n *\n * @public — re-exported from the semver-protected `@theokit/sdk/persistence` barrel, and (for\n * back-compat) from the semver-exempt `@theokit/sdk/internal/persistence` alias.\n */\nexport interface WalApplyResult {\n /** Final journal_mode actually in effect. */\n mode: \"wal\" | \"delete\";\n /** True if we wanted WAL but the filesystem refused. */\n fellBack: boolean;\n}\n\nconst warnedLabels = new Set<string>();\n\n/**\n * Apply WAL mode with DELETE fallback. Idempotent — safe to call multiple\n * times on the same connection.\n *\n * @param db any `pragma()`-capable SQLite handle (e.g., `better-sqlite3`)\n * @param label short identifier used in the warning (e.g., \"memory-index\")\n *\n */\nexport function applyWalWithFallback(db: PragmaCapable, label: string): WalApplyResult {\n try {\n const result = db.pragma(\"journal_mode = WAL\", { simple: true });\n if (typeof result === \"string\" && result.toLowerCase() === \"wal\") {\n return { mode: \"wal\", fellBack: false };\n }\n logFallback(label, `got \"${String(result)}\" instead of \"wal\"`);\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n logFallback(label, msg);\n }\n\n db.pragma(\"journal_mode = DELETE\");\n return { mode: \"delete\", fellBack: true };\n}\n\nfunction logFallback(label: string, reason: string): void {\n if (warnedLabels.has(label)) return;\n warnedLabels.add(label);\n diag(\n `[theokit-sdk] ${label}: WAL unavailable (${reason}); using DELETE journal mode. ` +\n \"This is normal on NFS/SMB/FUSE; expect slightly slower concurrent access.\\n\",\n );\n}\n\n/**\n * Test helper — clears the warn-once registry.\n *\n * @internal\n */\nexport function _resetWalWarnings(): void {\n warnedLabels.clear();\n}\n","/**\n * Resilient SQLite open (plan m0-foundation-expose-primitives, M0-5).\n *\n * Generalizes the driver-load + WAL-apply + corruption-recovery logic that was\n * duplicated (byte-identical) across `sdk/internal/memory/index-db.ts` and\n * `sdk-memory/internal/index/index-db.ts`. Schema-agnostic: the caller applies\n * its own PRAGMA/SCHEMA via the `onOpen` callback.\n *\n * Corruption recovery (EC-7): when opening fails with a \"malformed\" / \"not a\n * database\" / \"encrypted\" error and `recoverCorrupt` is not false, the file is\n * renamed aside to `<path>.corrupt-<ts>` (plus its WAL/SHM siblings) and a fresh\n * DB is opened. The corrupt file is renamed, NOT backed up — the timestamped\n * `.corrupt-*` file is kept for manual recovery.\n *\n * @internal — public via `@theokit/sdk/internal/persistence` (semver-exempt)\n */\n\nimport { mkdir, rename } from \"node:fs/promises\";\nimport { dirname } from \"node:path\";\n\nimport { ConfigurationError } from \"../../errors.js\";\nimport { diag } from \"../diagnostics.js\";\nimport { applyWalWithFallback } from \"./sqlite-wal.js\";\n\n/** Minimal SQLite handle surface every driver (`better-sqlite3`) exposes. */\nexport interface ResilientSqliteDb {\n /** SQLite `pragma()` access (used by `applyWalWithFallback`). */\n pragma(statement: string, options?: { simple?: boolean }): unknown;\n exec(sql: string): void;\n close(): void;\n}\n\n/**\n * Input to {@link openSqliteResilient}.\n *\n * The trap is `recoverCorrupt`, whose default is ON. When the driver reports a corrupt or encrypted\n * database, the file is renamed aside and a FRESH one is opened — so the call SUCCEEDS and hands\n * back an EMPTY database. The old bytes survive on disk under the renamed path, but a caller that\n * treats a resolved promise as \"my data is here\" is wrong exactly when it matters. Pass\n * `recoverCorrupt: false` to get the corruption error thrown instead.\n *\n * Semver-exempt: reachable via the '@theokit/sdk/internal/persistence' sub-path, which the package\n * declares in 'exports' but does NOT cover with its semver contract.\n *\n * @typeParam T - the concrete DB handle the driver returns; defaults to {@link ResilientSqliteDb}\n */\nexport interface OpenSqliteResilientOptions<T extends ResilientSqliteDb> {\n /** Absolute path to the SQLite file. Parent directories are created. */\n filePath: string;\n /**\n * Called after the driver is open and WAL is applied, before the handle is\n * returned. Apply PRAGMA/SCHEMA statements here. Errors propagate.\n */\n onOpen?: (db: T) => void | Promise<void>;\n /** Label used in the WAL-fallback warning and corruption-recovery log. Default \"sqlite\". */\n label?: string;\n /** When true (default) a corruption error renames the file aside and rebuilds. */\n recoverCorrupt?: boolean;\n}\n\n/**\n * Open a SQLite file with WAL (+ DELETE fallback) and corruption recovery.\n *\n * @typeParam T - the concrete DB handle type the driver returns (defaults to the\n * minimal {@link ResilientSqliteDb} surface)\n */\nexport async function openSqliteResilient<T extends ResilientSqliteDb = ResilientSqliteDb>(\n options: OpenSqliteResilientOptions<T>,\n): Promise<T> {\n await mkdir(dirname(options.filePath), { recursive: true });\n try {\n return await openConcrete(options);\n } catch (cause) {\n if (options.recoverCorrupt !== false && isCorruptionError(cause)) {\n await renameAside(options.filePath, options.label ?? \"sqlite\");\n return await openConcrete(options);\n }\n throw cause;\n }\n}\n\nasync function openConcrete<T extends ResilientSqliteDb>(\n options: OpenSqliteResilientOptions<T>,\n): Promise<T> {\n const db = await loadDriver<T>(options.filePath);\n // Apply WAL with NFS/SMB/FUSE fallback BEFORE schema so the journal mode is\n // set for the whole session.\n applyWalWithFallback(db, options.label ?? \"sqlite\");\n await options.onOpen?.(db);\n return db;\n}\n\n/** Injectable driver loaders (tests simulate a consumer env without better-sqlite3). */\ninterface DriverLoaders {\n betterSqlite3?: () => Promise<unknown>;\n nodeSqlite?: () => Promise<unknown>;\n}\nlet driverLoaderOverrides: DriverLoaders | undefined;\n\n/** Test-only. @internal */\nexport function _setDriverLoadersForTests(overrides: DriverLoaders | undefined): void {\n driverLoaderOverrides = overrides;\n}\n\n/**\n * Adapt `node:sqlite`'s `DatabaseSync` to the better-sqlite3 surface this module's callers use\n * (`prepare/get/all/run`, `exec`, `close`, `pragma`, `loadExtension`). The error message below has\n * ALWAYS promised this fallback (\"or run on Node 22.5+ for built-in node:sqlite\") — before the\n * flicker-bug fix the promise was fabricated: only better-sqlite3 was ever tried, so every consumer\n * without the optional native dep lost memory tools AND got a per-turn stderr WARN.\n */\nfunction adaptNodeSqlite(db: {\n prepare(sql: string): unknown;\n exec(sql: string): void;\n close(): void;\n loadExtension?: (path: string) => void;\n}): ResilientSqliteDb {\n const pragma = (statement: string, options?: { simple?: boolean }): unknown => {\n const stmt = db.prepare(`PRAGMA ${statement}`) as {\n get(): Record<string, unknown> | undefined;\n };\n const row = stmt.get();\n if (options?.simple === true) {\n return row === undefined ? undefined : Object.values(row)[0];\n }\n return row === undefined ? [] : [row];\n };\n return new Proxy(db as unknown as ResilientSqliteDb, {\n get(target, prop, receiver) {\n if (prop === \"pragma\") return pragma;\n if (prop === \"loadExtension\" && typeof db.loadExtension !== \"function\") {\n return () => {\n throw new Error(\n \"SQLite extension loading is unavailable on the node:sqlite fallback — install better-sqlite3 for sqlite-vec\",\n );\n };\n }\n const value = Reflect.get(target, prop, receiver) as unknown;\n return typeof value === \"function\"\n ? (value as (...a: unknown[]) => unknown).bind(target)\n : value;\n },\n });\n}\n\n// PRE-EXISTING debt, exposed when M75 fixed the Biome config that used to abort before\n// sweeping these files (a nested root under refactor/). It is not new code and was not touched\n// by M75; refactoring SDK internals without review would trade a visible problem for a diff\n// risky. Tracked in usetheodev/theokit-sdk#151.\n// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: see the reason just above\nasync function loadDriver<T extends ResilientSqliteDb>(filePath: string): Promise<T> {\n let betterSqliteCause: unknown;\n try {\n const mod = (await (driverLoaderOverrides?.betterSqlite3?.() ?? import(\"better-sqlite3\"))) as {\n default?: unknown;\n };\n const Ctor = mod.default ?? mod;\n if (typeof Ctor !== \"function\") {\n throw new Error(`better-sqlite3 export is not a constructor (got ${typeof Ctor})`);\n }\n return new (Ctor as new (path: string) => unknown)(filePath) as T;\n } catch (cause) {\n betterSqliteCause = cause;\n }\n // Fallback: the Node 22.5+ built-in driver (the path the error message documents).\n // `process.getBuiltinModule` (Node 22.3+) instead of `import(\"node:sqlite\")` — bundlers that\n // predate the sqlite builtin rewrite the import specifier to a bare \"sqlite\" package (proven in\n // the published dist: \"Cannot find package 'sqlite'\"), while getBuiltinModule is opaque to them.\n try {\n const mod = (await (driverLoaderOverrides?.nodeSqlite?.() ??\n Promise.resolve(\n (process as { getBuiltinModule?: (id: string) => unknown }).getBuiltinModule?.(\n \"node:sqlite\",\n ) ??\n (() => {\n throw new Error(\"node:sqlite built-in unavailable (Node < 22.3)\");\n })(),\n ))) as {\n DatabaseSync: new (\n path: string,\n ) => {\n prepare(sql: string): unknown;\n exec(sql: string): void;\n close(): void;\n };\n };\n return adaptNodeSqlite(new mod.DatabaseSync(filePath)) as T;\n } catch (nodeSqliteCause) {\n const b =\n betterSqliteCause instanceof Error ? betterSqliteCause.message : String(betterSqliteCause);\n const n = nodeSqliteCause instanceof Error ? nodeSqliteCause.message : String(nodeSqliteCause);\n throw new ConfigurationError(\n `Failed to load SQLite driver. Install \\`better-sqlite3\\` or run on Node 22.5+ for built-in \\`node:sqlite\\`. better-sqlite3: ${b}; node:sqlite: ${n}`,\n { code: \"sqlite_driver_unavailable\", cause: nodeSqliteCause },\n );\n }\n}\n\n/** True when an open error indicates an unreadable / corrupt database file. */\nexport function isCorruptionError(cause: unknown): boolean {\n if (!(cause instanceof Error)) return false;\n const msg = cause.message.toLowerCase();\n return (\n msg.includes(\"malformed\") ||\n msg.includes(\"not a database\") ||\n msg.includes(\"encrypted\") ||\n msg.includes(\"disk image is malformed\")\n );\n}\n\nasync function renameAside(filePath: string, label: string): Promise<void> {\n const asidePath = `${filePath}.corrupt-${Date.now()}`;\n await rename(filePath, asidePath).catch(() => undefined);\n await rename(`${filePath}-wal`, `${asidePath}-wal`).catch(() => undefined);\n await rename(`${filePath}-shm`, `${asidePath}-shm`).catch(() => undefined);\n diag(\n `[theokit-sdk] ${label} database corrupt; renamed aside to ${asidePath} and rebuilt schema\\n`,\n );\n}\n"]}
@@ -0,0 +1,194 @@
1
+ import { defaultIndexPath, openMemoryDb, resolveMemoryRoot, lanceStoragePath, LanceIndex } from './chunk-C7QPML3L.js';
2
+ import { replaceFileAtomic } from './chunk-VF7EWVDG.js';
3
+ import { ConfigurationError } from './chunk-IDCKSLYH.js';
4
+ import { redactSecrets } from './chunk-T7XEKOVW.js';
5
+ import { createHash } from 'crypto';
6
+ import { readFile } from 'fs/promises';
7
+ import { join } from 'path';
8
+ import { existsSync, rmSync, mkdirSync, renameSync } from 'fs';
9
+
10
+ function diaryPath(root) {
11
+ return join(root, "dream-diary.md");
12
+ }
13
+ function renderDiaryEntry(entry) {
14
+ const stamp = new Date(entry.timestampMs).toISOString();
15
+ const hash = entryHash(entry).slice(0, 8);
16
+ return [
17
+ `## ${stamp}`,
18
+ "",
19
+ `- entry-hash: ${hash}`,
20
+ `- facts before: ${entry.factsBefore}`,
21
+ `- facts after: ${entry.factsAfter}`,
22
+ `- duplicates removed: ${entry.duplicatesRemoved}`,
23
+ `- clusters created: ${entry.clustersCreated}`,
24
+ `- notes written: ${entry.notesWritten}`,
25
+ ""
26
+ ].join("\n");
27
+ }
28
+ async function appendDiaryEntry(root, entry) {
29
+ const path = diaryPath(root);
30
+ let raw = "";
31
+ try {
32
+ raw = await readFile(path, "utf8");
33
+ } catch {
34
+ raw = "# Dream Diary\n\n";
35
+ }
36
+ const next = `${raw.endsWith("\n") ? raw : `${raw}
37
+ `}${renderDiaryEntry(entry)}`;
38
+ await replaceFileAtomic(path, next);
39
+ }
40
+ function entryHash(entry) {
41
+ return createHash("sha256").update(
42
+ [
43
+ entry.factsBefore,
44
+ entry.factsAfter,
45
+ entry.duplicatesRemoved,
46
+ entry.clustersCreated,
47
+ entry.notesWritten
48
+ ].join("|")
49
+ ).digest("hex");
50
+ }
51
+ async function readAllSqliteFacts(root) {
52
+ const dbPath = defaultIndexPath(root);
53
+ if (!existsSync(dbPath)) return [];
54
+ const db = await openMemoryDb({ filePath: dbPath });
55
+ try {
56
+ const stmt = db.prepare("SELECT id, path, source, start_line, end_line, text FROM chunks");
57
+ const rows = stmt.all();
58
+ return rows.map((r) => ({
59
+ ...r,
60
+ namespace: "default",
61
+ scope: "agent",
62
+ user_id: "default"
63
+ }));
64
+ } finally {
65
+ db.close();
66
+ }
67
+ }
68
+ function nfcEqual(a, b) {
69
+ return a.normalize("NFC") === b.normalize("NFC");
70
+ }
71
+ async function migrateSqliteToLance(opts) {
72
+ const cwd = opts.cwd;
73
+ const memoryRoot = resolveMemoryRoot(cwd, { directory: opts.directory });
74
+ const finalPath = lanceStoragePath(memoryRoot);
75
+ const newPath = join(memoryRoot, "lance-new");
76
+ const rawLog = opts.logger ?? ((m) => console.log(m));
77
+ const log = (m) => rawLog(redactSecrets(m));
78
+ if (existsSync(finalPath)) {
79
+ throw new ConfigurationError(
80
+ `Destination already exists: ${finalPath}. Remove it manually (\`rm -rf ${finalPath}\`) and re-run.`,
81
+ { code: "migration_destination_exists" }
82
+ );
83
+ }
84
+ if (existsSync(newPath)) {
85
+ log(`Removing stale ${newPath} from a previous run...`);
86
+ rmSync(newPath, { recursive: true, force: true });
87
+ }
88
+ log(`Reading SQLite facts from ${cwd}/.theokit/memory/index.sqlite ...`);
89
+ const sqliteFacts = await readAllSqliteFacts(memoryRoot);
90
+ log(`SQLite has ${sqliteFacts.length} facts.`);
91
+ if (sqliteFacts.length === 0) {
92
+ return {
93
+ countSqlite: 0,
94
+ countLance: 0,
95
+ validated: true,
96
+ sampleComparisons: [],
97
+ lancePath: finalPath,
98
+ committed: false
99
+ };
100
+ }
101
+ const placeholderEmbedding = {
102
+ id: "migration-placeholder",
103
+ model: "deterministic-hash",
104
+ dimension: 8,
105
+ embed: async (texts) => {
106
+ return texts.map((t) => {
107
+ const arr = new Array(8).fill(0);
108
+ for (let i = 0; i < t.length; i += 1) {
109
+ arr[i % 8] += t.charCodeAt(i);
110
+ }
111
+ return arr.map((x) => x / 1e3);
112
+ });
113
+ },
114
+ stats: () => ({ cacheHits: 0, cacheMisses: 0, httpCalls: 0, retries: 0 })
115
+ };
116
+ mkdirSync(newPath, { recursive: true });
117
+ log(`Writing Lance index to ${newPath} ...`);
118
+ const lance = await LanceIndex.open({
119
+ cwd,
120
+ embedding: placeholderEmbedding,
121
+ storagePath: newPath
122
+ });
123
+ const batchSize = opts.batchSize ?? 100;
124
+ for (let i = 0; i < sqliteFacts.length; i += batchSize) {
125
+ const batch = sqliteFacts.slice(i, i + batchSize).map((f) => ({
126
+ id: f.id,
127
+ text: f.text,
128
+ source: f.source,
129
+ namespace: f.namespace ?? "default",
130
+ scope: f.scope ?? "agent",
131
+ user_id: f.user_id ?? "default",
132
+ timestamp: 0
133
+ }));
134
+ await lance.addFacts(batch);
135
+ log(` Migrated ${Math.min(i + batchSize, sqliteFacts.length)}/${sqliteFacts.length}`);
136
+ }
137
+ const lanceCount = await lance.countFacts("default");
138
+ log(`Lance has ${lanceCount} facts (expected ${sqliteFacts.length}).`);
139
+ const sampleSize = Math.min(10, sqliteFacts.length);
140
+ const sampleComparisons = [];
141
+ for (let i = 0; i < sampleSize; i += 1) {
142
+ const sqliteFact = sqliteFacts[Math.floor(i * sqliteFacts.length / sampleSize)];
143
+ if (sqliteFact === void 0) continue;
144
+ const hits = await lance.search(sqliteFact.text, {
145
+ namespace: sqliteFact.namespace ?? "default",
146
+ limit: 1
147
+ });
148
+ const hit = hits[0];
149
+ const match = hit !== void 0 && nfcEqual(hit.text, sqliteFact.text);
150
+ sampleComparisons.push({ id: sqliteFact.id, match });
151
+ }
152
+ const allMatch = sampleComparisons.every((c) => c.match);
153
+ const countMatch = lanceCount === sqliteFacts.length;
154
+ const validated = allMatch && countMatch;
155
+ await lance.close();
156
+ if (opts.dryRun === true) {
157
+ log("Dry-run mode \u2014 discarding Lance staging dir.");
158
+ rmSync(newPath, { recursive: true, force: true });
159
+ return {
160
+ countSqlite: sqliteFacts.length,
161
+ countLance: lanceCount,
162
+ validated,
163
+ sampleComparisons,
164
+ lancePath: finalPath,
165
+ committed: false
166
+ };
167
+ }
168
+ if (!validated) {
169
+ log("Validation FAILED \u2014 leaving SQLite intact, removing Lance staging dir.");
170
+ rmSync(newPath, { recursive: true, force: true });
171
+ return {
172
+ countSqlite: sqliteFacts.length,
173
+ countLance: lanceCount,
174
+ validated: false,
175
+ sampleComparisons,
176
+ lancePath: finalPath,
177
+ committed: false
178
+ };
179
+ }
180
+ renameSync(newPath, finalPath);
181
+ log(`Migration committed to ${finalPath}.`);
182
+ return {
183
+ countSqlite: sqliteFacts.length,
184
+ countLance: lanceCount,
185
+ validated: true,
186
+ sampleComparisons,
187
+ lancePath: finalPath,
188
+ committed: true
189
+ };
190
+ }
191
+
192
+ export { appendDiaryEntry, diaryPath, entryHash, migrateSqliteToLance, readAllSqliteFacts, renderDiaryEntry };
193
+ //# sourceMappingURL=chunk-R6TA2DRJ.js.map
194
+ //# sourceMappingURL=chunk-R6TA2DRJ.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/internal/memory/dreaming/diary.ts","../src/internal/memory/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AW,KAAA;AAAA,MACX,iBAAA;AAAA,MACA,SAAA,EAAW,SAAA;AAAA,MACX,SAAA,EAAW;AAAA,KACb;AAAA,EACF;AAGA,EAAA,UAAA,CAAW,SAAS,SAAS,CAAA;AAC7B,EAAA,GAAA,CAAI,CAAA,uBAAA,EAA0B,SAAS,CAAA,CAAA,CAAG,CAAA;AAC1C,EAAA,OAAO;AAAA,IACL,aAAa,WAAA,CAAY,MAAA;AAAA,IACzB,UAAA,EAAY,UAAA;AAAA,IACZ,SAAA,EAAW,IAAA;AAAA,IACX,iBAAA;AAAA,IACA,SAAA,EAAW,SAAA;AAAA,IACX,SAAA,EAAW;AAAA,GACb;AACF","file":"chunk-R6TA2DRJ.js","sourcesContent":["import { createHash } from \"node:crypto\";\nimport { readFile } from \"node:fs/promises\";\nimport { join } from \"node:path\";\nimport { replaceFileAtomic } from \"../../persistence/atomic-write.js\";\nimport type { MemoryRoot } from \"../storage/memory-root.js\";\n\n/**\n * Dream-diary append (ADR D7).\n *\n * Diary lives at `.theokit/memory/dream-diary.md` and grows with one entry\n * per sweep. Each entry is content-hashed so the same input produces the\n * same entry — idempotency contract.\n *\n * Writes go through `replaceFileAtomic` (EC-3 of edge-case review) so a\n * crash mid-write can never leave a half-written diary.\n *\n * Shared with `@theokit/sdk-memory`; see the memory-store barrel.\n */\n\nexport interface DiaryEntry {\n timestampMs: number;\n factsBefore: number;\n factsAfter: number;\n duplicatesRemoved: number;\n clustersCreated: number;\n notesWritten: number;\n}\n\n/** `<memory root>/dream-diary.md`. Takes the RESOLVED ROOT — see `storage/memory-root.ts` (#463). */\nexport function diaryPath(root: MemoryRoot): string {\n return join(root, \"dream-diary.md\");\n}\n\n/**\n * One diary entry as the markdown that gets appended: a timestamp heading, the short entry hash,\n * and the counts the sweep produced. The hash is what makes a re-run recognisable as the same\n * sweep rather than a new one.\n */\nexport function renderDiaryEntry(entry: DiaryEntry): string {\n const stamp = new Date(entry.timestampMs).toISOString();\n const hash = entryHash(entry).slice(0, 8);\n return [\n `## ${stamp}`,\n \"\",\n `- entry-hash: ${hash}`,\n `- facts before: ${entry.factsBefore}`,\n `- facts after: ${entry.factsAfter}`,\n `- duplicates removed: ${entry.duplicatesRemoved}`,\n `- clusters created: ${entry.clustersCreated}`,\n `- notes written: ${entry.notesWritten}`,\n \"\",\n ].join(\"\\n\");\n}\n\n/**\n * Append one sweep's entry to `<memory root>/dream-diary.md`, creating the file with its header\n * when this is the first sweep. The diary is a human-readable record of what dreaming changed —\n * consolidations are otherwise invisible, because they alter the notes rather than announce\n * themselves.\n */\nexport async function appendDiaryEntry(root: MemoryRoot, entry: DiaryEntry): Promise<void> {\n const path = diaryPath(root);\n let raw = \"\";\n try {\n raw = await readFile(path, \"utf8\");\n } catch {\n raw = \"# Dream Diary\\n\\n\";\n }\n const next = `${raw.endsWith(\"\\n\") ? raw : `${raw}\\n`}${renderDiaryEntry(entry)}`;\n await replaceFileAtomic(path, next);\n}\n\n/**\n * A stable hash of one entry's counts, so two sweeps that did the same work read as the same work.\n * Rendered truncated in the entry; the full value is what callers compare.\n */\nexport function entryHash(entry: DiaryEntry): string {\n return createHash(\"sha256\")\n .update(\n [\n entry.factsBefore,\n entry.factsAfter,\n entry.duplicatesRemoved,\n entry.clustersCreated,\n entry.notesWritten,\n ].join(\"|\"),\n )\n .digest(\"hex\");\n}\n","import { existsSync, mkdirSync, renameSync, rmSync } from \"node:fs\";\nimport { join } from \"node:path\";\n\nimport { ConfigurationError } from \"../../errors.js\";\nimport { redactSecrets } from \"../security/index.js\";\nimport { defaultIndexPath, openMemoryDb } from \"./index-db.js\";\nimport { LanceIndex, lanceStoragePath } from \"./lance-index.js\";\nimport { type MemoryRoot, resolveMemoryRoot } from \"./storage/memory-root.js\";\n\n/**\n * Migrate Memory.index from SQLite to LanceDB (ADR D44).\n *\n * EC-3 MUST FIX: validation uses NFC unicode normalization on both sides.\n *\n * Shared with `@theokit/sdk-memory` through the memory-store barrel (#463).\n */\n\nexport interface MigrateOptions {\n cwd: string;\n /**\n * Absolute path (or `~/`-prefixed) of the memory root to migrate, when the agent that wrote it\n * set `memory.directory`. Defaults to `<cwd>/.theokit/memory` (#463).\n */\n directory?: string;\n dryRun?: boolean;\n batchSize?: number;\n /** Inject for tests; defaults to console.log. */\n logger?: (msg: string) => void;\n}\n\nexport interface MigrateResult {\n countSqlite: number;\n countLance: number;\n /** Set when validation succeeded (or dryRun); false on validation failure. */\n validated: boolean;\n /** Per-fact compare results (size = sample count, up to 10). */\n sampleComparisons: ReadonlyArray<{ id: string; match: boolean }>;\n /** Lance storage location (final, after migration). */\n lancePath: string;\n /** Was the migration committed (false for dry-run). */\n committed: boolean;\n}\n\n/**\n * One row of the SQLite memory index, as the Lance migration reads it: the chunk plus the tenant\n * columns the target table needs. Shared with `@theokit/sdk-memory`, which migrates the same table.\n */\nexport interface SqliteFactRow {\n id: string;\n path: string;\n source: \"memory\" | \"sessions\" | \"wiki\";\n start_line: number;\n end_line: number;\n text: string;\n namespace?: string | null;\n scope?: string | null;\n user_id?: string | null;\n}\n\n/**\n * Read all facts from the SQLite memory index. Returns empty array if the\n * SQLite db file does not exist (workspace never used Memory).\n *\n * Shared with `@theokit/sdk-memory` through the memory-store barrel, so it carries no visibility\n * tag — naming that tag in this block would delete the function below it (#463).\n */\nexport async function readAllSqliteFacts(root: MemoryRoot): Promise<SqliteFactRow[]> {\n const dbPath = defaultIndexPath(root);\n if (!existsSync(dbPath)) return [];\n const db = await openMemoryDb({ filePath: dbPath });\n try {\n // SQLite schema: chunks table holds the facts. Schema may vary; we\n // probe column existence and fall back to safe defaults.\n const stmt = db.prepare(\"SELECT id, path, source, start_line, end_line, text FROM chunks\");\n const rows = stmt.all() as Array<{\n id: string;\n path: string;\n source: \"memory\" | \"sessions\" | \"wiki\";\n start_line: number;\n end_line: number;\n text: string;\n }>;\n return rows.map((r) => ({\n ...r,\n namespace: \"default\",\n scope: \"agent\",\n user_id: \"default\",\n }));\n } finally {\n db.close();\n }\n}\n\n/**\n * Compare two strings via NFC normalization (EC-3 MUST FIX). Required\n * because SQLite/Lance native bindings can normalize unicode differently\n * (NFC vs NFD), producing false negatives on facts with accents/emojis.\n *\n * @internal\n */\nfunction nfcEqual(a: string, b: string): boolean {\n return a.normalize(\"NFC\") === b.normalize(\"NFC\");\n}\n\n/**\n * Run the migration. Writes Lance to `<cwd>/.theokit/memory/lance-new/`\n * first, validates round-trip, then renames to `lance/` on success.\n * SQLite db is preserved (CLI prompts user to delete).\n *\n * @internal\n */\n// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: migration is a single transaction (read → write → validate → commit-or-rollback); splitting harms atomicity reasoning.\nexport async function migrateSqliteToLance(opts: MigrateOptions): Promise<MigrateResult> {\n const cwd = opts.cwd;\n // One resolution, reused by every path this migration touches (#463).\n const memoryRoot = resolveMemoryRoot(cwd, { directory: opts.directory });\n const finalPath = lanceStoragePath(memoryRoot);\n const newPath = join(memoryRoot, \"lance-new\");\n // T1.4 (ADR D68): wrap logger so any fact text containing secrets is\n // masked before reaching the destination (console or user-supplied sink).\n // Caller-supplied loggers cannot bypass — by design (D70).\n const rawLog = opts.logger ?? ((m: string) => console.log(m));\n const log = (m: string) => rawLog(redactSecrets(m));\n\n // EC: destination exists → typed error.\n if (existsSync(finalPath)) {\n throw new ConfigurationError(\n `Destination already exists: ${finalPath}. Remove it manually (\\`rm -rf ${finalPath}\\`) and re-run.`,\n { code: \"migration_destination_exists\" },\n );\n }\n if (existsSync(newPath)) {\n // Leftover from a previous failed/interrupted migration. Safe to delete.\n log(`Removing stale ${newPath} from a previous run...`);\n rmSync(newPath, { recursive: true, force: true });\n }\n\n log(`Reading SQLite facts from ${cwd}/.theokit/memory/index.sqlite ...`);\n const sqliteFacts = await readAllSqliteFacts(memoryRoot);\n log(`SQLite has ${sqliteFacts.length} facts.`);\n if (sqliteFacts.length === 0) {\n return {\n countSqlite: 0,\n countLance: 0,\n validated: true,\n sampleComparisons: [],\n lancePath: finalPath,\n committed: false,\n };\n }\n\n // Reuse the embedding runtime configured in the workspace? For v1.2 we\n // require the caller to pass embedding explicitly via env. The migration\n // path is documented as: \"set OPENAI_API_KEY (or similar) before running.\"\n // Here we ship a placeholder embedder that uses a deterministic\n // content-hash — sufficient for migration validation; consumers should\n // re-embed on first real query after migration if needed.\n const placeholderEmbedding = {\n id: \"migration-placeholder\",\n model: \"deterministic-hash\",\n dimension: 8,\n embed: async (texts: ReadonlyArray<string>) => {\n // Simple deterministic hash → 8-dim vector (migration validation only).\n return texts.map((t) => {\n const arr = new Array(8).fill(0);\n for (let i = 0; i < t.length; i += 1) {\n arr[i % 8] += t.charCodeAt(i);\n }\n return arr.map((x) => x / 1000);\n });\n },\n stats: () => ({ cacheHits: 0, cacheMisses: 0, httpCalls: 0, retries: 0 }),\n };\n\n mkdirSync(newPath, { recursive: true });\n log(`Writing Lance index to ${newPath} ...`);\n\n const lance = await LanceIndex.open({\n cwd,\n embedding: placeholderEmbedding,\n storagePath: newPath,\n });\n\n const batchSize = opts.batchSize ?? 100;\n for (let i = 0; i < sqliteFacts.length; i += batchSize) {\n const batch = sqliteFacts.slice(i, i + batchSize).map((f) => ({\n id: f.id,\n text: f.text,\n source: f.source,\n namespace: f.namespace ?? \"default\",\n scope: f.scope ?? \"agent\",\n user_id: f.user_id ?? \"default\",\n timestamp: 0,\n }));\n await lance.addFacts(batch);\n log(` Migrated ${Math.min(i + batchSize, sqliteFacts.length)}/${sqliteFacts.length}`);\n }\n\n // Validation: count + sample compare (10 random) with NFC normalization.\n const lanceCount = await lance.countFacts(\"default\");\n log(`Lance has ${lanceCount} facts (expected ${sqliteFacts.length}).`);\n\n const sampleSize = Math.min(10, sqliteFacts.length);\n const sampleComparisons: { id: string; match: boolean }[] = [];\n for (let i = 0; i < sampleSize; i += 1) {\n const sqliteFact = sqliteFacts[Math.floor((i * sqliteFacts.length) / sampleSize)];\n if (sqliteFact === undefined) continue;\n // Search Lance for this fact's text — best-effort round-trip.\n const hits = await lance.search(sqliteFact.text, {\n namespace: sqliteFact.namespace ?? \"default\",\n limit: 1,\n });\n // EC-3: compare with NFC normalization.\n const hit = hits[0];\n const match = hit !== undefined && nfcEqual(hit.text, sqliteFact.text);\n sampleComparisons.push({ id: sqliteFact.id, match });\n }\n\n const allMatch = sampleComparisons.every((c) => c.match);\n const countMatch = lanceCount === sqliteFacts.length;\n const validated = allMatch && countMatch;\n\n await lance.close();\n\n if (opts.dryRun === true) {\n log(\"Dry-run mode — discarding Lance staging dir.\");\n rmSync(newPath, { recursive: true, force: true });\n return {\n countSqlite: sqliteFacts.length,\n countLance: lanceCount,\n validated,\n sampleComparisons,\n lancePath: finalPath,\n committed: false,\n };\n }\n\n if (!validated) {\n log(\"Validation FAILED — leaving SQLite intact, removing Lance staging dir.\");\n rmSync(newPath, { recursive: true, force: true });\n return {\n countSqlite: sqliteFacts.length,\n countLance: lanceCount,\n validated: false,\n sampleComparisons,\n lancePath: finalPath,\n committed: false,\n };\n }\n\n // Atomic commit via rename.\n renameSync(newPath, finalPath);\n log(`Migration committed to ${finalPath}.`);\n return {\n countSqlite: sqliteFacts.length,\n countLance: lanceCount,\n validated: true,\n sampleComparisons,\n lancePath: finalPath,\n committed: true,\n };\n}\n"]}