synabun 2.4.1 → 2.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (295) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +1 -1
  3. package/browser-extension/content.js +56 -14
  4. package/browser-extension/engine.js +158 -8
  5. package/browser-extension/manifest.json +3 -1
  6. package/browser-extension/protocol.js +91 -6
  7. package/browser-extension/service-worker.js +13 -5
  8. package/browser-extension/tokens.css +1 -1
  9. package/docs/browser-extension-bridge.md +24 -4
  10. package/lib/data-home-files.js +4 -2
  11. package/lib/first-launch-protection.js +8 -2
  12. package/lib/update-snapshot.js +11 -4
  13. package/mcp-server/dist/http.d.ts.map +1 -1
  14. package/mcp-server/dist/http.js +25 -2
  15. package/mcp-server/dist/http.js.map +1 -1
  16. package/mcp-server/dist/index.d.ts.map +1 -1
  17. package/mcp-server/dist/index.js +62 -17
  18. package/mcp-server/dist/index.js.map +1 -1
  19. package/mcp-server/dist/services/browser-bench.d.ts +22 -22
  20. package/mcp-server/dist/services/memory-maintenance.d.ts +1 -0
  21. package/mcp-server/dist/services/memory-maintenance.d.ts.map +1 -1
  22. package/mcp-server/dist/services/memory-maintenance.js +34 -45
  23. package/mcp-server/dist/services/memory-maintenance.js.map +1 -1
  24. package/mcp-server/dist/services/memory-migration-copy.d.ts +144 -0
  25. package/mcp-server/dist/services/memory-migration-copy.d.ts.map +1 -0
  26. package/mcp-server/dist/services/memory-migration-copy.js +467 -0
  27. package/mcp-server/dist/services/memory-migration-copy.js.map +1 -0
  28. package/mcp-server/dist/services/memory-migration-worker.d.ts +2 -0
  29. package/mcp-server/dist/services/memory-migration-worker.d.ts.map +1 -0
  30. package/mcp-server/dist/services/memory-migration-worker.js +60 -0
  31. package/mcp-server/dist/services/memory-migration-worker.js.map +1 -0
  32. package/mcp-server/dist/services/memory-retrieval.d.ts +4 -0
  33. package/mcp-server/dist/services/memory-retrieval.d.ts.map +1 -1
  34. package/mcp-server/dist/services/memory-retrieval.js +188 -40
  35. package/mcp-server/dist/services/memory-retrieval.js.map +1 -1
  36. package/mcp-server/dist/services/memory-schema.d.ts +58 -2
  37. package/mcp-server/dist/services/memory-schema.d.ts.map +1 -1
  38. package/mcp-server/dist/services/memory-schema.js +190 -13
  39. package/mcp-server/dist/services/memory-schema.js.map +1 -1
  40. package/mcp-server/dist/services/memory-sessions.d.ts +19 -1
  41. package/mcp-server/dist/services/memory-sessions.d.ts.map +1 -1
  42. package/mcp-server/dist/services/memory-sessions.js +56 -20
  43. package/mcp-server/dist/services/memory-sessions.js.map +1 -1
  44. package/mcp-server/dist/services/memory-verification.d.ts.map +1 -1
  45. package/mcp-server/dist/services/memory-verification.js +8 -5
  46. package/mcp-server/dist/services/memory-verification.js.map +1 -1
  47. package/mcp-server/dist/services/neural-interface.d.ts +5 -0
  48. package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
  49. package/mcp-server/dist/services/neural-interface.js +19 -2
  50. package/mcp-server/dist/services/neural-interface.js.map +1 -1
  51. package/mcp-server/dist/services/retrieval-reader-worker.d.ts +2 -0
  52. package/mcp-server/dist/services/retrieval-reader-worker.d.ts.map +1 -0
  53. package/mcp-server/dist/services/retrieval-reader-worker.js +76 -0
  54. package/mcp-server/dist/services/retrieval-reader-worker.js.map +1 -0
  55. package/mcp-server/dist/services/retrieval-reader.d.ts +38 -0
  56. package/mcp-server/dist/services/retrieval-reader.d.ts.map +1 -0
  57. package/mcp-server/dist/services/retrieval-reader.js +145 -0
  58. package/mcp-server/dist/services/retrieval-reader.js.map +1 -0
  59. package/mcp-server/dist/services/sqlite-write.d.ts +14 -0
  60. package/mcp-server/dist/services/sqlite-write.d.ts.map +1 -0
  61. package/mcp-server/dist/services/sqlite-write.js +94 -0
  62. package/mcp-server/dist/services/sqlite-write.js.map +1 -0
  63. package/mcp-server/dist/services/sqlite.d.ts +39 -0
  64. package/mcp-server/dist/services/sqlite.d.ts.map +1 -1
  65. package/mcp-server/dist/services/sqlite.js +249 -8
  66. package/mcp-server/dist/services/sqlite.js.map +1 -1
  67. package/mcp-server/dist/services/tool-catalog.d.ts +1 -1
  68. package/mcp-server/dist/services/typesafe-config.d.ts +7 -1
  69. package/mcp-server/dist/services/typesafe-config.d.ts.map +1 -1
  70. package/mcp-server/dist/services/typesafe-config.js +32 -10
  71. package/mcp-server/dist/services/typesafe-config.js.map +1 -1
  72. package/mcp-server/dist/services/typesafe.d.ts.map +1 -1
  73. package/mcp-server/dist/services/typesafe.js +9 -7
  74. package/mcp-server/dist/services/typesafe.js.map +1 -1
  75. package/mcp-server/dist/services/vector-cache.d.ts +32 -0
  76. package/mcp-server/dist/services/vector-cache.d.ts.map +1 -1
  77. package/mcp-server/dist/services/vector-cache.js +161 -8
  78. package/mcp-server/dist/services/vector-cache.js.map +1 -1
  79. package/mcp-server/dist/tools/agent-definition.d.ts +2 -2
  80. package/mcp-server/dist/tools/agents.d.ts +11 -7
  81. package/mcp-server/dist/tools/agents.d.ts.map +1 -1
  82. package/mcp-server/dist/tools/agents.js +27 -9
  83. package/mcp-server/dist/tools/agents.js.map +1 -1
  84. package/mcp-server/dist/tools/browser-advanced.d.ts +1 -1
  85. package/mcp-server/dist/tools/browser-advanced.d.ts.map +1 -1
  86. package/mcp-server/dist/tools/browser-advanced.js +10 -3
  87. package/mcp-server/dist/tools/browser-advanced.js.map +1 -1
  88. package/mcp-server/dist/tools/browser-extension-setup.d.ts +5 -0
  89. package/mcp-server/dist/tools/browser-extension-setup.d.ts.map +1 -0
  90. package/mcp-server/dist/tools/browser-extension-setup.js +23 -0
  91. package/mcp-server/dist/tools/browser-extension-setup.js.map +1 -0
  92. package/mcp-server/dist/tools/browser-interact.d.ts.map +1 -1
  93. package/mcp-server/dist/tools/browser-interact.js +6 -1
  94. package/mcp-server/dist/tools/browser-interact.js.map +1 -1
  95. package/mcp-server/dist/tools/fb-groups.d.ts +2 -2
  96. package/mcp-server/dist/tools/skill-tools.d.ts +4 -4
  97. package/mcp-server/dist/tools/tool-groups.d.ts +1 -1
  98. package/mcp-server/package-lock.json +2 -2
  99. package/mcp-server/package.json +1 -1
  100. package/mcp-server/src/http.ts +20 -2
  101. package/mcp-server/src/index.ts +59 -17
  102. package/mcp-server/src/services/memory-maintenance.ts +31 -28
  103. package/mcp-server/src/services/memory-migration-copy.ts +419 -0
  104. package/mcp-server/src/services/memory-migration-worker.ts +55 -0
  105. package/mcp-server/src/services/memory-retrieval.ts +173 -32
  106. package/mcp-server/src/services/memory-schema.ts +194 -12
  107. package/mcp-server/src/services/memory-sessions.ts +54 -21
  108. package/mcp-server/src/services/memory-verification.ts +7 -4
  109. package/mcp-server/src/services/neural-interface.ts +22 -2
  110. package/mcp-server/src/services/retrieval-reader-worker.ts +67 -0
  111. package/mcp-server/src/services/retrieval-reader.ts +131 -0
  112. package/mcp-server/src/services/sqlite-write.ts +60 -0
  113. package/mcp-server/src/services/sqlite.ts +246 -8
  114. package/mcp-server/src/services/typesafe-config.ts +25 -6
  115. package/mcp-server/src/services/typesafe.ts +9 -7
  116. package/mcp-server/src/services/vector-cache.ts +154 -9
  117. package/mcp-server/src/tools/agents.ts +26 -9
  118. package/mcp-server/src/tools/browser-advanced.ts +10 -3
  119. package/mcp-server/src/tools/browser-extension-setup.ts +30 -0
  120. package/mcp-server/src/tools/browser-interact.ts +5 -1
  121. package/neural-interface/i18n/en.json +263 -1
  122. package/neural-interface/i18n/pt-BR.json +264 -2
  123. package/neural-interface/lib/agents/split.js +32 -4
  124. package/neural-interface/lib/assistant-api.js +19 -8
  125. package/neural-interface/lib/assistant-brains/claude.js +2 -0
  126. package/neural-interface/lib/assistant-catalog.js +2 -2
  127. package/neural-interface/lib/assistant-config.js +0 -7
  128. package/neural-interface/lib/assistant-dispatch.js +257 -95
  129. package/neural-interface/lib/assistant-persona.js +16 -4
  130. package/neural-interface/lib/assistant-pricing.js +65 -14
  131. package/neural-interface/lib/assistant-router.js +131 -47
  132. package/neural-interface/lib/assistant-run-wait.js +45 -28
  133. package/neural-interface/lib/assistant-runtime.js +39 -33
  134. package/neural-interface/lib/backup-contents.js +158 -0
  135. package/neural-interface/lib/backup-crypto-io.js +330 -0
  136. package/neural-interface/lib/backup-crypto-worker.js +130 -0
  137. package/neural-interface/lib/backup-crypto.js +525 -0
  138. package/neural-interface/lib/backup-download.js +268 -0
  139. package/neural-interface/lib/backup-encryption.js +318 -0
  140. package/neural-interface/lib/backup-groups.js +287 -0
  141. package/neural-interface/lib/backup-restore-session.js +176 -0
  142. package/neural-interface/lib/backup-restore-stage.js +60 -0
  143. package/neural-interface/lib/backup-service.js +788 -233
  144. package/neural-interface/lib/backup-zip-reader.js +30 -18
  145. package/neural-interface/lib/backup-zip-writer.js +34 -10
  146. package/neural-interface/lib/browser-extension-adapter.js +89 -13
  147. package/neural-interface/lib/browser-extension-bridge.js +18 -7
  148. package/neural-interface/lib/claude-agent-bridge.js +1 -0
  149. package/neural-interface/lib/claude-session-cleanup.js +5 -1
  150. package/neural-interface/lib/cli-update-planner.js +105 -6
  151. package/neural-interface/lib/client-diagnostics.js +42 -0
  152. package/neural-interface/lib/confined-fs.js +7 -2
  153. package/neural-interface/lib/coordination/README.md +8 -2
  154. package/neural-interface/lib/coordination/contract.js +161 -0
  155. package/neural-interface/lib/coordination/database.js +999 -0
  156. package/neural-interface/lib/coordination/operations.js +436 -0
  157. package/neural-interface/lib/coordination/service.js +68 -537
  158. package/neural-interface/lib/coordination/storage-worker.js +101 -0
  159. package/neural-interface/lib/coordination/store.js +219 -1111
  160. package/neural-interface/lib/db.js +70 -32
  161. package/neural-interface/lib/health-count.js +37 -0
  162. package/neural-interface/lib/loop-stall.js +139 -0
  163. package/neural-interface/lib/maritaca/client.js +2 -2
  164. package/neural-interface/lib/maritaca/models.js +12 -17
  165. package/neural-interface/lib/maritaca/opencode.js +4 -2
  166. package/neural-interface/lib/memory-api.js +9 -1
  167. package/neural-interface/lib/memory-session-capture.js +42 -10
  168. package/neural-interface/lib/memory-store-gate.js +313 -0
  169. package/neural-interface/lib/memory-store-migrate.js +65 -0
  170. package/neural-interface/lib/memory-timers.js +7 -7
  171. package/neural-interface/lib/native-loop-providers.js +13 -16
  172. package/neural-interface/lib/native-loop-runtime.js +28 -7
  173. package/neural-interface/lib/opencode-build-preflight.js +18 -1
  174. package/neural-interface/lib/opencode-native-boundary.js +69 -8
  175. package/neural-interface/lib/opencode-provider-view.js +14 -3
  176. package/neural-interface/lib/opencode-v2-ws-requests.js +31 -1
  177. package/neural-interface/lib/opencode2-client.js +2 -1
  178. package/neural-interface/lib/recall-impact.js +122 -0
  179. package/neural-interface/lib/scratch-evidence.js +420 -0
  180. package/neural-interface/lib/scratch-process-guard.js +139 -0
  181. package/neural-interface/lib/scratch-references.js +279 -0
  182. package/neural-interface/lib/scratch-retention-core.js +385 -0
  183. package/neural-interface/lib/scratch-retention-worker.js +37 -0
  184. package/neural-interface/lib/scratch-retention.js +151 -0
  185. package/neural-interface/lib/session-cache-builder.js +31 -43
  186. package/neural-interface/lib/session-cache-worker.js +157 -0
  187. package/neural-interface/lib/session-indexer.js +28 -21
  188. package/neural-interface/lib/session-search-index.js +85 -0
  189. package/neural-interface/lib/skills/creation.js +1 -1
  190. package/neural-interface/lib/sqlite-integrity-worker.js +3 -1
  191. package/neural-interface/lib/style-guide/store.js +43 -3
  192. package/neural-interface/lib/system-restore.js +50 -0
  193. package/neural-interface/lib/teams/api.js +15 -6
  194. package/neural-interface/lib/teams/claims.js +67 -31
  195. package/neural-interface/lib/teams/index.js +7 -7
  196. package/neural-interface/lib/teams/integration.js +40 -40
  197. package/neural-interface/lib/teams/landing.js +6 -6
  198. package/neural-interface/lib/teams/recovery.js +24 -23
  199. package/neural-interface/lib/teams/scheduler.js +194 -95
  200. package/neural-interface/lib/teams/state.js +14 -14
  201. package/neural-interface/lib/teams/worktrees.js +27 -27
  202. package/neural-interface/lib/whatsapp/bridge.js +2 -1
  203. package/neural-interface/lib/worker-confinement.js +47 -2
  204. package/neural-interface/lib/worker-mcp-channel.js +2 -0
  205. package/neural-interface/lib/worker-resume.js +5 -0
  206. package/neural-interface/lib/worker-stop.js +86 -0
  207. package/neural-interface/package-lock.json +2 -2
  208. package/neural-interface/package.json +1 -1
  209. package/neural-interface/public/index.html +233 -24
  210. package/neural-interface/public/onboarding.html +259 -34
  211. package/neural-interface/public/shared/agents/studio/ax-library.js +0 -6
  212. package/neural-interface/public/shared/agents/studio/ax-main.js +14 -7
  213. package/neural-interface/public/shared/agy/agy-options.js +11 -0
  214. package/neural-interface/public/shared/agy/agy-panel.js +29 -10
  215. package/neural-interface/public/shared/agy/agy-styles.js +14 -12
  216. package/neural-interface/public/shared/agy/agy-tabs-logic.js +4 -1
  217. package/neural-interface/public/shared/api.js +1 -1
  218. package/neural-interface/public/shared/assistant/asst-api.js +3 -3
  219. package/neural-interface/public/shared/assistant/asst-brain-picker.js +13 -6
  220. package/neural-interface/public/shared/assistant/asst-control.js +2 -0
  221. package/neural-interface/public/shared/assistant/asst-motion-gate.js +78 -4
  222. package/neural-interface/public/shared/assistant/asst-panel.js +18 -2
  223. package/neural-interface/public/shared/assistant/asst-rack-styles.js +4 -0
  224. package/neural-interface/public/shared/assistant/asst-render.js +105 -38
  225. package/neural-interface/public/shared/assistant/asst-route.js +100 -21
  226. package/neural-interface/public/shared/assistant/asst-routes-editor.js +1 -1
  227. package/neural-interface/public/shared/assistant/asst-sidepanel.js +4 -2
  228. package/neural-interface/public/shared/assistant/asst-state.js +11 -0
  229. package/neural-interface/public/shared/assistant/asst-styles.js +10 -23
  230. package/neural-interface/public/shared/assistant/route-identity.js +2 -1
  231. package/neural-interface/public/shared/cdx/cdx-render.js +9 -0
  232. package/neural-interface/public/shared/cdx/cdx-settings.js +3 -1
  233. package/neural-interface/public/shared/cdx/cdx-styles.js +6 -0
  234. package/neural-interface/public/shared/cdx/cdx-tabs.js +29 -9
  235. package/neural-interface/public/shared/client-diagnostics.js +94 -0
  236. package/neural-interface/public/shared/cp/cp-markdown.js +7 -2
  237. package/neural-interface/public/shared/cp/cp-styles.js +1 -1
  238. package/neural-interface/public/shared/effort-i18n.js +118 -0
  239. package/neural-interface/public/shared/html-shell.js +4 -52
  240. package/neural-interface/public/shared/loading-diagnostic-entry.js +64 -0
  241. package/neural-interface/public/shared/memory-store-wait.js +52 -0
  242. package/neural-interface/public/shared/morelogin-profile-field.js +1 -1
  243. package/neural-interface/public/shared/mtc/mtc-logic.js +2 -1
  244. package/neural-interface/public/shared/mtc/mtc-panel.js +8 -1
  245. package/neural-interface/public/shared/mtc/mtc-styles.js +13 -12
  246. package/neural-interface/public/shared/ocp-v2/ocp-v2-childpanel.js +7 -0
  247. package/neural-interface/public/shared/ocp-v2/ocp-v2-location-logic.js +109 -0
  248. package/neural-interface/public/shared/ocp-v2/ocp-v2-modelpicker.js +8 -1
  249. package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +118 -6
  250. package/neural-interface/public/shared/ocp-v2/ocp-v2-render.js +27 -3
  251. package/neural-interface/public/shared/ocp-v2/ocp-v2-send.js +13 -0
  252. package/neural-interface/public/shared/ocp-v2/ocp-v2-session-actions.js +2 -1
  253. package/neural-interface/public/shared/ocp-v2/ocp-v2-styles.js +27 -93
  254. package/neural-interface/public/shared/ocp-v2/ocp-v2-variantpicker.js +17 -6
  255. package/neural-interface/public/shared/output-format.js +294 -0
  256. package/neural-interface/public/shared/provider-icons.js +6 -0
  257. package/neural-interface/public/shared/schedules-view.js +2 -1
  258. package/neural-interface/public/shared/server-presence.js +77 -0
  259. package/neural-interface/public/shared/settings/settings-backup-contents.js +241 -0
  260. package/neural-interface/public/shared/settings/settings-backup-download.js +298 -0
  261. package/neural-interface/public/shared/settings/settings-backup-encryption-pane.js +335 -0
  262. package/neural-interface/public/shared/settings/settings-backup-encryption.js +545 -0
  263. package/neural-interface/public/shared/settings/settings-ia.js +1 -1
  264. package/neural-interface/public/shared/skills/studio/sx-geometry.js +12 -3
  265. package/neural-interface/public/shared/skills/studio/sx-main.js +14 -7
  266. package/neural-interface/public/shared/studio-attention.js +6 -0
  267. package/neural-interface/public/shared/styles.css +341 -437
  268. package/neural-interface/public/shared/transcript-rows.js +28 -3
  269. package/neural-interface/public/shared/tray-auto-close.js +18 -5
  270. package/neural-interface/public/shared/ui-agents.js +1 -2
  271. package/neural-interface/public/shared/ui-automation-studio.js +168 -108
  272. package/neural-interface/public/shared/ui-claude-panel.js +42 -65
  273. package/neural-interface/public/shared/ui-loading.js +124 -17
  274. package/neural-interface/public/shared/ui-native-loop-router.js +10 -0
  275. package/neural-interface/public/shared/ui-navbar.js +2 -1
  276. package/neural-interface/public/shared/ui-output.js +533 -0
  277. package/neural-interface/public/shared/ui-schedules-studio.js +165 -128
  278. package/neural-interface/public/shared/ui-scroll-follow.js +97 -6
  279. package/neural-interface/public/shared/ui-sessions.js +2 -2
  280. package/neural-interface/public/shared/ui-settings-entry.js +18 -0
  281. package/neural-interface/public/shared/ui-settings.js +224 -65
  282. package/neural-interface/public/shared/ui-sidepanel-tray.js +3 -2
  283. package/neural-interface/public/shared/ui-tooltip.js +16 -0
  284. package/neural-interface/public/shared/ui-tutorial.js +5 -6
  285. package/neural-interface/public/shared/ui-worker-end.js +27 -8
  286. package/neural-interface/public/shared/worker-end.js +9 -4
  287. package/neural-interface/public/variant/2d/main.js +5 -0
  288. package/neural-interface/public/variant/3d/app.js +10 -0
  289. package/neural-interface/public/variant/3d/main.js +5 -1
  290. package/neural-interface/scripts/scratch-retention.mjs +20 -0
  291. package/neural-interface/server.js +477 -139
  292. package/npm-shrinkwrap.json +2 -2
  293. package/package.json +1 -1
  294. package/scripts/restore-data-home-from-backup.mjs +175 -8
  295. package/setup.js +18 -4
@@ -0,0 +1,419 @@
1
+ /**
2
+ * The safety copy a memory schema migration is made behind, and who may make it.
3
+ *
4
+ * 2026-11-21, migration v8 on a 2.6 GB store: every process that started ran `VACUUM INTO memory.db.pre-v8-….bak`
5
+ * by itself, on its own thread, before anything serialized them. 13 processes wrote 23.4 GB in 41 minutes; 12 of them
6
+ * were stopped by their MCP clients (the server could not answer `initialize` while it copied) and left a partial
7
+ * `.bak` with a `.bak-journal` that nothing ever removed. This module is what stands in front of that now:
8
+ *
9
+ * one copy a lock held by the operating system, an empty SQLite file beside the database
10
+ * (`<db>.migrate-lock`, taken with BEGIN IMMEDIATE; nothing is ever stored in it, and SQLite keeps
11
+ * a 512-byte `-journal` beside it while it is held, which the next holder clears when the last one
12
+ * was killed). It is released when its holder ends, however it ends, so there is no owner record
13
+ * to go stale and no pid to be reused. Never delete that file: a process that waits on the old one
14
+ * and a process that creates a new one would both hold "the" lock (the sweep below still spares a
15
+ * file whose writer is alive, and a copy that lost its file fails its check: nothing is migrated).
16
+ * whole or not the copy is written as `<db>.pre-v<N>-<ms>-<pid>.bak.partial`, read back (journal gone, SQLite
17
+ * header, quick_check, the tables it must hold), flushed, and only then renamed to `….bak`. A name
18
+ * that ends in `.bak` with no journal beside it is a complete copy.
19
+ * leftovers whoever holds the lock removes every `.partial` and every copy of the old naming that still has
20
+ * its `.bak-journal`, when the process its name carries is provably gone. A complete copy, a copy
21
+ * `memory_migrations` names, and a file whose writer may be alive are left alone, and the log says
22
+ * which and why.
23
+ * room the copy is not started unless the disk has room for it plus a margin.
24
+ *
25
+ * Node built-ins only, on purpose: the migration worker (memory-migration-worker.ts) loads this file as it is.
26
+ */
27
+ import { DatabaseSync } from 'node:sqlite';
28
+ import { spawnSync } from 'node:child_process';
29
+ import fs from 'node:fs';
30
+ import path from 'node:path';
31
+
32
+ export type MemoryMigrationErrorCode =
33
+ | 'MEMORY_MIGRATION_NO_SPACE'
34
+ | 'MEMORY_MIGRATION_BUSY'
35
+ | 'MEMORY_MIGRATION_COPY_FAILED'
36
+ | 'MEMORY_MIGRATION_LOCK_UNAVAILABLE';
37
+
38
+ /** A migration that was not started, or was stopped before the schema changed. The database is as it was. */
39
+ export class MemoryMigrationError extends Error {
40
+ readonly code: MemoryMigrationErrorCode;
41
+ constructor(code: MemoryMigrationErrorCode, message: string, cause?: unknown) {
42
+ super(message);
43
+ this.name = 'MemoryMigrationError';
44
+ this.code = code;
45
+ if (cause !== undefined) (this as { cause?: unknown }).cause = cause;
46
+ }
47
+ }
48
+
49
+ export type MigrationLog = (line: string) => void;
50
+ export const defaultMigrationLog: MigrationLog = line => console.error(`[SynaBun] memory migration: ${line}`);
51
+
52
+ /** How long a process waits for another one's copy and migration before it gives up (the database is then as it was). */
53
+ export const LOCK_WAIT_MS = 30 * 60_000;
54
+ const LOCK_POLL_MS = 200;
55
+ const LOCK_NOTE_EVERY_MS = 15_000;
56
+ /** Room asked for beyond the copy itself: a tenth of it, and never less than this. */
57
+ export const MIN_SPACE_MARGIN_BYTES = 64 * 1024 * 1024;
58
+ /** A complete copy nobody recorded (its maker ended before the migration committed) is used again while this fresh. */
59
+ export const REUSE_COPY_MAX_AGE_MS = 10 * 60_000;
60
+
61
+ const mb = (bytes: number) => `${Math.ceil(bytes / (1024 * 1024))} MB`;
62
+ const escapeRe = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
63
+ const isBusy = (error: unknown) => (Number((error as { errcode?: number })?.errcode) & 0xff) === 5;
64
+ const reason = (error: unknown) => (error instanceof Error ? error.message : String(error));
65
+
66
+ function sleepSync(ms: number): void {
67
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, Math.max(1, ms));
68
+ }
69
+
70
+ // --- The lock ---
71
+
72
+ export const migrationLockPath = (dbPath: string) => `${dbPath}.migrate-lock`;
73
+
74
+ export interface MigrationLock { readonly path: string; release(): void }
75
+
76
+ /** Which file `file` leads to right now ("<dev>:<ino>"), or null when there is none. */
77
+ function identity(file: string): string | null {
78
+ try { const info = fs.statSync(file, { bigint: true }); return `${info.dev}:${info.ino}`; }
79
+ catch { return null; }
80
+ }
81
+
82
+ /**
83
+ * The migration lock of `dbPath`, when nobody holds it; null when another connection does (this process's too).
84
+ * Throws LOCK_UNAVAILABLE when the lock file cannot be used at all: the caller must then change nothing.
85
+ */
86
+ export function tryMigrationLock(dbPath: string): MigrationLock | null {
87
+ const lockPath = migrationLockPath(dbPath);
88
+ const unavailable = (cause: unknown) => new MemoryMigrationError('MEMORY_MIGRATION_LOCK_UNAVAILABLE',
89
+ `The memory database was not upgraded: its migration lock (${lockPath}) cannot be used: ${reason(cause)}. Nothing was changed.`, cause);
90
+ for (let attempt = 0; attempt < 3; attempt++) {
91
+ const before = identity(lockPath);
92
+ let handle: DatabaseSync;
93
+ try { handle = new DatabaseSync(lockPath); } catch (cause) { throw unavailable(cause); }
94
+ try {
95
+ handle.exec('PRAGMA busy_timeout = 0');
96
+ handle.exec('BEGIN IMMEDIATE');
97
+ } catch (cause) {
98
+ try { handle.close(); } catch { /* nothing to close */ }
99
+ if (isBusy(cause)) return null;
100
+ throw unavailable(cause);
101
+ }
102
+ // Replaced or removed between the open and the lock: what was just locked is a file nobody else will look at.
103
+ const after = identity(lockPath);
104
+ if (after !== null && (before === null || before === after)) {
105
+ let held = true;
106
+ return {
107
+ path: lockPath,
108
+ release() {
109
+ if (!held) return;
110
+ held = false;
111
+ try { handle.exec('ROLLBACK'); } catch { /* the close below lets go of it either way */ }
112
+ try { handle.close(); } catch { /* closed already */ }
113
+ },
114
+ };
115
+ }
116
+ try { handle.exec('ROLLBACK'); } catch { /* closed next */ }
117
+ try { handle.close(); } catch { /* closed already */ }
118
+ }
119
+ return null;
120
+ }
121
+
122
+ export interface LockWait {
123
+ timeoutMs?: number;
124
+ pollMs?: number;
125
+ /** Asked between attempts: false ends the wait with null (the holder did the work). */
126
+ stillNeeded?: () => boolean;
127
+ log?: MigrationLog;
128
+ }
129
+
130
+ /**
131
+ * The migration lock, waited for. Null when `stillNeeded` turned false while another process held it. Throws BUSY
132
+ * when the holder kept it for `timeoutMs`. The wait sleeps on the calling thread: servers call this from the
133
+ * migration worker (sqlite.ts ensureDatabase), never from the thread that answers requests.
134
+ */
135
+ export function acquireMigrationLock(dbPath: string, { timeoutMs = LOCK_WAIT_MS, pollMs = LOCK_POLL_MS, stillNeeded, log = defaultMigrationLog }: LockWait = {}): MigrationLock | null {
136
+ const started = Date.now();
137
+ let told = 0;
138
+ for (;;) {
139
+ const lock = tryMigrationLock(dbPath);
140
+ if (lock) return lock;
141
+ if (stillNeeded && !stillNeeded()) return null;
142
+ const waited = Date.now() - started;
143
+ if (waited >= timeoutMs) {
144
+ throw new MemoryMigrationError('MEMORY_MIGRATION_BUSY',
145
+ `The memory database was not upgraded: another SynaBun process has been upgrading it for more than ${Math.round(waited / 1000)} s. ` +
146
+ 'This process made no copy and changed nothing. Start it again once the other one is done.');
147
+ }
148
+ if (!told || Date.now() - told >= LOCK_NOTE_EVERY_MS) {
149
+ told = Date.now();
150
+ log(`another SynaBun process is copying or upgrading ${path.basename(dbPath)}; waiting for it (no second copy is made)`);
151
+ }
152
+ sleepSync(pollMs);
153
+ }
154
+ }
155
+
156
+ // --- Names ---
157
+
158
+ interface CopyName { file: string; version: number; ms: number; pid: number; partial: boolean; suffix: '' | '-journal' | '-wal' | '-shm' }
159
+
160
+ /** `<db>.pre-v<N>-<ms>-<pid>.bak`, its `.partial`, and what SQLite puts beside either; null for any other name. */
161
+ function parseCopyName(dbPath: string, name: string): CopyName | null {
162
+ const match = new RegExp(`^${escapeRe(path.basename(dbPath))}\\.pre-v(\\d+)-(\\d+)-(\\d+)\\.bak(\\.partial)?(-journal|-wal|-shm)?$`).exec(name);
163
+ if (!match) return null;
164
+ return { file: path.join(path.dirname(dbPath), name), version: Number(match[1]), ms: Number(match[2]), pid: Number(match[3]), partial: Boolean(match[4]), suffix: (match[5] ?? '') as CopyName['suffix'] };
165
+ }
166
+
167
+ function listCopyNames(dbPath: string): CopyName[] {
168
+ let names: string[];
169
+ try { names = fs.readdirSync(path.dirname(dbPath)); } catch { return []; }
170
+ const prefix = `${path.basename(dbPath)}.pre-v`;
171
+ return names.filter(name => name.startsWith(prefix)).map(name => parseCopyName(dbPath, name)).filter((entry): entry is CopyName => entry !== null);
172
+ }
173
+
174
+ /** True when the folder holds something sweepInterruptedCopies might remove (a cheap look, no lock needed). */
175
+ export function hasInterruptedCopies(dbPath: string): boolean {
176
+ return listCopyNames(dbPath).some(entry => entry.partial || entry.suffix === '-journal');
177
+ }
178
+
179
+ // --- Leftovers ---
180
+
181
+ function pidAlive(pid: number): boolean {
182
+ if (!Number.isInteger(pid) || pid <= 0) return false;
183
+ try { process.kill(pid, 0); return true; }
184
+ catch (error) { return (error as { code?: string })?.code === 'EPERM'; }
185
+ }
186
+
187
+ /** When process `pid` started, in ms, or null when that cannot be read (Windows, a sandbox that refuses ps). */
188
+ function pidStartedMs(pid: number): number | null {
189
+ if (process.platform === 'win32') return null;
190
+ try {
191
+ const out = spawnSync('ps', ['-o', 'lstart=', '-p', String(pid)], { encoding: 'utf8', timeout: 5000, env: { ...process.env, LC_ALL: 'C', LANG: 'C' } });
192
+ if (out.status !== 0 || !out.stdout) return null;
193
+ const at = Date.parse(out.stdout.trim());
194
+ return Number.isFinite(at) ? at : null;
195
+ } catch { return null; }
196
+ }
197
+
198
+ export interface SweepOptions {
199
+ /** The `backup` values of memory_migrations: a file one of them names is never removed. */
200
+ recorded?: Iterable<string>;
201
+ log?: MigrationLog;
202
+ isAlive?: (pid: number) => boolean;
203
+ startedMs?: (pid: number) => number | null;
204
+ }
205
+ export interface SweepResult { removed: string[]; kept: Array<{ file: string; why: string }> }
206
+
207
+ /**
208
+ * Remove what interrupted copies left beside `dbPath`. ONLY while holding the migration lock: that is what says no
209
+ * current build is writing a `.partial`. A `.partial`, and a copy of the old naming (`….bak` with a `….bak-journal`
210
+ * beside it, written in place by a build from before the lock), is removed when the process its name carries is
211
+ * gone, or is another process that started after the copy did (a reused pid). Nothing else is touched: no complete
212
+ * copy, no copy `memory_migrations` names, no file whose writer cannot be shown to be gone (a pid that is in use
213
+ * where `ps` cannot be asked: it stays, and the log says so at every start).
214
+ */
215
+ export function sweepInterruptedCopies(dbPath: string, { recorded = [], log = defaultMigrationLog, isAlive = pidAlive, startedMs = pidStartedMs }: SweepOptions = {}): SweepResult {
216
+ const result: SweepResult = { removed: [], kept: [] };
217
+ const named = new Set([...recorded].filter(Boolean).map(value => path.basename(String(value))));
218
+ const entries = listCopyNames(dbPath);
219
+ const remove = (file: string) => {
220
+ try { fs.rmSync(file, { force: true }); result.removed.push(file); }
221
+ catch (error) { result.kept.push({ file, why: `could not be removed: ${reason(error)}` }); }
222
+ };
223
+ const copyOf = (entry: CopyName) => entry.file.slice(0, entry.file.length - entry.suffix.length);
224
+ // Is the process a name carries provably not writing that file any more? This process is not (it holds the lock and
225
+ // is here, not copying). A pid nobody has is not. A pid somebody has is the writer only when that process was
226
+ // already running when the name was made: the writer was. Each pid is asked once.
227
+ const alive = new Map<number, boolean>();
228
+ const starts = new Map<number, number | null>();
229
+ const writerGone = (entry: CopyName): boolean => {
230
+ if (entry.pid === process.pid) return true;
231
+ if (!alive.has(entry.pid)) alive.set(entry.pid, isAlive(entry.pid));
232
+ if (!alive.get(entry.pid)) return true;
233
+ if (!starts.has(entry.pid)) starts.set(entry.pid, startedMs(entry.pid));
234
+ const started = starts.get(entry.pid) ?? null;
235
+ return started !== null && started > entry.ms + 5000;
236
+ };
237
+
238
+ // Partial copies. The lock already says nobody is writing one; the pid in the name is asked as well, so that a
239
+ // lock file somebody deleted under its holder (two "holders") still costs no file that is being written.
240
+ const keptPartials = new Set<string>();
241
+ for (const entry of entries) {
242
+ if (!entry.partial) continue;
243
+ const copy = copyOf(entry);
244
+ if (named.has(path.basename(copy)) || named.has(path.basename(copy).replace(/\.partial$/, ''))) { result.kept.push({ file: entry.file, why: 'memory_migrations names it' }); continue; }
245
+ if (!writerGone(entry)) {
246
+ if (!keptPartials.has(copy)) { keptPartials.add(copy); result.kept.push({ file: copy, why: `process ${entry.pid}, which was writing it, may still be running` }); }
247
+ continue;
248
+ }
249
+ remove(entry.file);
250
+ }
251
+ for (const journal of entries) {
252
+ if (journal.partial || journal.suffix !== '-journal') continue;
253
+ const copy = copyOf(journal);
254
+ if (named.has(path.basename(copy))) { result.kept.push({ file: journal.file, why: 'it lies beside a copy memory_migrations names' }); continue; }
255
+ if (!writerGone(journal)) { result.kept.push({ file: copy, why: `process ${journal.pid}, which was writing it, may still be running` }); continue; }
256
+ if (fs.existsSync(copy)) remove(copy);
257
+ remove(journal.file);
258
+ }
259
+ if (result.removed.length) log(`removed ${result.removed.length} file(s) an interrupted copy left behind: ${result.removed.map(file => path.basename(file)).join(', ')}`);
260
+ for (const kept of result.kept) log(`left ${path.basename(kept.file)} in place: ${kept.why}`);
261
+ return result;
262
+ }
263
+
264
+ // --- Room ---
265
+
266
+ export interface SpaceOptions {
267
+ /** Free bytes of the folder's disk for an unprivileged process; null when it cannot be told. */
268
+ freeBytes?: (dir: string) => number | null;
269
+ }
270
+
271
+ function diskFreeBytes(dir: string): number | null {
272
+ try {
273
+ const info = fs.statfsSync(dir, { bigint: true });
274
+ return Number(info.bavail * info.bsize);
275
+ } catch { return null; }
276
+ }
277
+
278
+ /**
279
+ * Refuse a copy the disk has no room for. The copy is at most the database and its write-ahead log; the margin on top
280
+ * (a tenth, at least 64 MB) is for the migration's own writes and for everything else that uses the disk meanwhile.
281
+ * A disk whose free space cannot be read is not held against the migration: the copy itself then fails cleanly if it
282
+ * does not fit (makeVerifiedCopy removes what it wrote).
283
+ */
284
+ export function assertRoomForCopy(dbPath: string, { freeBytes = diskFreeBytes }: SpaceOptions = {}): { needed: number; margin: number; free: number | null } {
285
+ const size = (file: string) => { try { return fs.statSync(file).size; } catch { return 0; } };
286
+ const needed = size(dbPath) + size(`${dbPath}-wal`);
287
+ const margin = Math.max(MIN_SPACE_MARGIN_BYTES, Math.ceil(needed / 10));
288
+ const dir = path.dirname(dbPath);
289
+ const free = freeBytes(dir);
290
+ if (free !== null && free < needed + margin) {
291
+ throw new MemoryMigrationError('MEMORY_MIGRATION_NO_SPACE',
292
+ `The memory database was not upgraded: there is no room for its safety copy. The copy needs about ${mb(needed)} plus a ${mb(margin)} margin, ` +
293
+ `and ${mb(free)} are free in ${dir}. Nothing was copied and nothing was changed; free ${mb(needed + margin - free)} and start SynaBun again.`);
294
+ }
295
+ return { needed, margin, free };
296
+ }
297
+
298
+ // --- The copy ---
299
+
300
+ /** Throws with the reason when `file` is not a complete, readable SQLite database holding `tables`. */
301
+ export function verifyCopy(file: string, tables: readonly string[] = []): void {
302
+ if (fs.existsSync(`${file}-journal`)) throw new Error('its rollback journal is still there (the copy did not finish)');
303
+ const head = Buffer.alloc(16);
304
+ const fd = fs.openSync(file, 'r');
305
+ try { fs.readSync(fd, head, 0, 16, 0); } finally { fs.closeSync(fd); }
306
+ if (head.toString('latin1') !== 'SQLite format 3\u0000') throw new Error('it is not an SQLite database');
307
+ const copy = new DatabaseSync(file, { readOnly: true });
308
+ try {
309
+ const check = String(Object.values(copy.prepare('PRAGMA quick_check(1)').get() ?? {})[0]);
310
+ if (check !== 'ok') throw new Error(`quick_check answered ${check}`);
311
+ const present = new Set(copy.prepare("SELECT name FROM sqlite_master WHERE type='table'").all().map(row => String(row.name)));
312
+ const missing = tables.filter(table => !present.has(table));
313
+ if (missing.length) throw new Error(`it lacks the table(s) ${missing.join(', ')}`);
314
+ } finally { copy.close(); }
315
+ }
316
+
317
+ function flush(file: string): void {
318
+ const fd = fs.openSync(file, 'r+');
319
+ try { fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
320
+ }
321
+
322
+ export interface CopyOptions {
323
+ /** Tables the copy must hold to count as one of this database. */
324
+ tables?: readonly string[];
325
+ log?: MigrationLog;
326
+ now?: () => number;
327
+ /** Test seam: called with the partial file once it is written and before it is checked and published. */
328
+ written?: (partial: string) => void;
329
+ }
330
+
331
+ /**
332
+ * Copy the database `d` (open on `dbPath`) and return the published copy's path. ONLY while holding the migration
333
+ * lock. VACUUM INTO writes a consistent copy that includes committed WAL pages. It is written under a `.partial`
334
+ * name, so a process that ends anywhere before the rename leaves a name the next sweep removes, never a `.bak`.
335
+ */
336
+ export function makeVerifiedCopy(d: DatabaseSync, dbPath: string, version: number, { tables = [], log = defaultMigrationLog, now = Date.now, written }: CopyOptions = {}): string {
337
+ const published = `${dbPath}.pre-v${version}-${now()}-${process.pid}.bak`;
338
+ const partial = `${published}.partial`;
339
+ const discard = () => { for (const suffix of ['', '-journal', '-wal', '-shm']) { try { fs.rmSync(partial + suffix, { force: true }); } catch { /* the next sweep removes it */ } } };
340
+ const started = Date.now();
341
+ try {
342
+ if (fs.existsSync(published) || fs.existsSync(partial)) throw new Error(`${path.basename(published)} already exists`);
343
+ d.prepare('VACUUM INTO ?').run(partial);
344
+ written?.(partial);
345
+ verifyCopy(partial, tables);
346
+ flush(partial);
347
+ fs.renameSync(partial, published);
348
+ } catch (error) {
349
+ discard();
350
+ if (error instanceof MemoryMigrationError) throw error;
351
+ throw new MemoryMigrationError('MEMORY_MIGRATION_COPY_FAILED',
352
+ `The memory database was not upgraded: its safety copy could not be made (${reason(error)}). What was written of it was removed and nothing was changed.`, error);
353
+ }
354
+ try { const dir = fs.openSync(path.dirname(published), 'r'); try { fs.fsyncSync(dir); } finally { fs.closeSync(dir); } } catch { /* not every system flushes a folder */ }
355
+ let size = 0;
356
+ try { size = fs.statSync(published).size; } catch { /* reported without its size */ }
357
+ log(`copied ${path.basename(dbPath)} to ${path.basename(published)} (${mb(size)}, ${((Date.now() - started) / 1000).toFixed(1)} s), checked`);
358
+ return published;
359
+ }
360
+
361
+ export interface ReuseOptions {
362
+ recorded?: Iterable<string>;
363
+ tables?: readonly string[];
364
+ maxAgeMs?: number;
365
+ log?: MigrationLog;
366
+ now?: () => number;
367
+ /**
368
+ * What the database holds, as one comparable value (null: it cannot be told). With `current`, a copy is used only
369
+ * when it answers the same as the database does now: a copy from before a later write is not the copy of what is
370
+ * about to be migrated.
371
+ */
372
+ position?: (database: DatabaseSync) => string | null;
373
+ /** The open database the copy must match (with `position`). */
374
+ current?: DatabaseSync;
375
+ }
376
+
377
+ /**
378
+ * A complete copy for `version` that no migration recorded, that is still fresh, and that holds what the database
379
+ * holds now: its maker ended between publishing it and committing the migration (or a build from before the lock
380
+ * made it and lost the race). Using it is what keeps a process that dies at that step from costing a second copy.
381
+ * A copy that fails any of this is left where it is (it is complete) and another is made. ONLY while holding the
382
+ * migration lock.
383
+ */
384
+ export function findReusableCopy(dbPath: string, version: number, { recorded = [], tables = [], maxAgeMs = REUSE_COPY_MAX_AGE_MS, log = defaultMigrationLog, now = Date.now, position, current }: ReuseOptions = {}): string | null {
385
+ const named = new Set([...recorded].filter(Boolean).map(value => path.basename(String(value))));
386
+ const at = now();
387
+ const candidates = listCopyNames(dbPath)
388
+ .filter(entry => !entry.partial && entry.suffix === '' && entry.version === version && !named.has(path.basename(entry.file)))
389
+ .filter(entry => entry.ms <= at + 60_000 && at - entry.ms <= maxAgeMs)
390
+ .sort((a, b) => b.ms - a.ms);
391
+ if (!candidates.length) return null;
392
+ let wanted: string | null = null;
393
+ if (position && current) {
394
+ try { wanted = position(current); } catch { wanted = null; }
395
+ if (wanted === null) {
396
+ log(`${candidates.map(entry => path.basename(entry.file)).join(', ')}: not used as the safety copy, because what the database holds now cannot be compared with it; another copy is made`);
397
+ return null;
398
+ }
399
+ }
400
+ for (const entry of candidates) {
401
+ const name = path.basename(entry.file);
402
+ try { verifyCopy(entry.file, tables); }
403
+ catch (error) { log(`${name} is not used as the safety copy: ${reason(error)}`); continue; }
404
+ if (position && current) {
405
+ let held: string | null = null;
406
+ try {
407
+ const copy = new DatabaseSync(entry.file, { readOnly: true });
408
+ try { held = position(copy); } finally { copy.close(); }
409
+ } catch { held = null; }
410
+ if (held === null || held !== wanted) {
411
+ log(`${name} is not used as the safety copy: the database was written to after it was made. It is complete and stays where it is; another copy is made`);
412
+ continue;
413
+ }
414
+ }
415
+ log(`using ${path.basename(entry.file)}, a complete copy made ${Math.round((at - entry.ms) / 1000)} s ago that no migration recorded, instead of making another`);
416
+ return entry.file;
417
+ }
418
+ return null;
419
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * The thread a server migrates its memory store on (started by sqlite.ts ensureDatabase, never imported).
3
+ *
4
+ * The safety copy of a large store takes tens of seconds of synchronous SQLite work. On the thread that speaks MCP
5
+ * that is longer than clients wait for `initialize`, and they stop a server that does not answer. Here it holds no
6
+ * one up: this thread opens its own connection, sets it up exactly as getDb() does (`setup`, handed over by
7
+ * sqlite.ts so there is one list), and runs the same migrateMemory() a script would run in place.
8
+ *
9
+ * The outcome goes back on `port`, and only then is `flags` set: [1] when this thread runs, [0] when it is done,
10
+ * whatever the outcome. `flags` is shared with the starting thread, which may be waiting on it without an event loop
11
+ * (a synchronous getDb() during the migration) and then reads the outcome out of the port in place. An error is
12
+ * reported, never thrown: the starting thread goes by what the migration answered (sqlite.ts upgradeVerdict), and
13
+ * for anything else opens the store the ordinary way and meets the same condition itself.
14
+ */
15
+ import { workerData, type MessagePort } from 'node:worker_threads';
16
+ import { DatabaseSync } from 'node:sqlite';
17
+
18
+ interface Job { dbPath: string; setup: string[]; flags: SharedArrayBuffer; port: MessagePort }
19
+ type Outcome = { ok: true; applied: number[]; backup: string | null; deferred?: { versions: number[]; reason: string } }
20
+ | { ok: false; error: { message: string; code?: string } };
21
+
22
+ const job = workerData as Job;
23
+ const flags = new Int32Array(job.flags);
24
+ Atomics.store(flags, 1, 1);
25
+
26
+ let outcome: Outcome;
27
+ try {
28
+ // The test runner loads these modules from src/, where the worker is the TypeScript file and Node runs it as it
29
+ // is; the './x.js' its modules import is then './x.ts'. A build has only .js files and none of this runs.
30
+ const fromSource = import.meta.url.endsWith('.ts');
31
+ if (fromSource) {
32
+ const { registerHooks } = await import('node:module') as unknown as { registerHooks?: (hooks: { resolve: (specifier: string, context: unknown, next: (specifier: string, context: unknown) => unknown) => unknown }) => void };
33
+ registerHooks?.({
34
+ resolve(specifier, context, next) {
35
+ if (/^\.{1,2}\//.test(specifier) && specifier.endsWith('.js')) {
36
+ try { return next(`${specifier.slice(0, -3)}.ts`, context); } catch { /* a real .js file */ }
37
+ }
38
+ return next(specifier, context);
39
+ },
40
+ });
41
+ }
42
+ const { migrateMemory } = await import(new URL(fromSource ? './memory-schema.ts' : './memory-schema.js', import.meta.url).href) as typeof import('./memory-schema.js');
43
+ const d = new DatabaseSync(job.dbPath);
44
+ try {
45
+ for (const sql of job.setup) d.exec(sql);
46
+ outcome = { ok: true, ...migrateMemory(d, job.dbPath) };
47
+ } finally { d.close(); }
48
+ } catch (error) {
49
+ outcome = { ok: false, error: { message: error instanceof Error ? error.message : String(error), code: (error as { code?: string })?.code } };
50
+ }
51
+ try { job.port.postMessage(outcome); }
52
+ finally {
53
+ Atomics.store(flags, 0, 1);
54
+ Atomics.notify(flags, 0);
55
+ }