@cleocode/core 2026.8.1 → 2026.8.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 (174) hide show
  1. package/dist/bootstrap.d.ts.map +1 -1
  2. package/dist/bootstrap.js +57 -27
  3. package/dist/bootstrap.js.map +1 -1
  4. package/dist/db/index.d.ts +13 -0
  5. package/dist/db/index.d.ts.map +1 -1
  6. package/dist/db/index.js +13 -0
  7. package/dist/db/index.js.map +1 -1
  8. package/dist/docs/display-alias.js +1 -1
  9. package/dist/docs/display-alias.js.map +1 -1
  10. package/dist/docs/export-document.js +434 -365
  11. package/dist/docs/export-document.js.map +3 -3
  12. package/dist/docs/numbering.js +1 -1
  13. package/dist/docs/numbering.js.map +1 -1
  14. package/dist/docs/supersede.js +1 -1
  15. package/dist/docs/supersede.js.map +1 -1
  16. package/dist/injection.d.ts.map +1 -1
  17. package/dist/injection.js +44 -3
  18. package/dist/injection.js.map +1 -1
  19. package/dist/lifecycle/index.js +1 -1
  20. package/dist/lifecycle/index.js.map +1 -1
  21. package/dist/llm/plugin-facade.js +232 -96
  22. package/dist/llm/plugin-facade.js.map +3 -3
  23. package/dist/memory/brain-backfill.js +3 -3
  24. package/dist/memory/brain-backfill.js.map +1 -1
  25. package/dist/memory/brain-consolidator.js +2 -2
  26. package/dist/memory/brain-consolidator.js.map +1 -1
  27. package/dist/memory/brain-doctor.js +1 -1
  28. package/dist/memory/brain-doctor.js.map +1 -1
  29. package/dist/memory/brain-health-dashboard.js +1 -1
  30. package/dist/memory/brain-health-dashboard.js.map +1 -1
  31. package/dist/memory/brain-lifecycle.js +10 -10
  32. package/dist/memory/brain-lifecycle.js.map +1 -1
  33. package/dist/memory/brain-maintenance.js +1 -1
  34. package/dist/memory/brain-maintenance.js.map +1 -1
  35. package/dist/memory/brain-page-nodes.js +4 -4
  36. package/dist/memory/brain-page-nodes.js.map +1 -1
  37. package/dist/memory/brain-reasoning.js +1 -1
  38. package/dist/memory/brain-reasoning.js.map +1 -1
  39. package/dist/memory/brain-search.js +1 -1
  40. package/dist/memory/brain-search.js.map +1 -1
  41. package/dist/memory/brain-similarity.js +1 -1
  42. package/dist/memory/brain-similarity.js.map +1 -1
  43. package/dist/memory/brain-stdp.js +6 -6
  44. package/dist/memory/brain-stdp.js.map +1 -1
  45. package/dist/memory/brain-sweep-executor.js +1 -1
  46. package/dist/memory/brain-sweep-executor.js.map +1 -1
  47. package/dist/memory/brain-writer-handlers.js +2 -2
  48. package/dist/memory/brain-writer-handlers.js.map +1 -1
  49. package/dist/memory/claude-mem-migration.d.ts.map +1 -1
  50. package/dist/memory/claude-mem-migration.js +2 -2
  51. package/dist/memory/claude-mem-migration.js.map +1 -1
  52. package/dist/memory/engine-compat.js +4 -4
  53. package/dist/memory/engine-compat.js.map +1 -1
  54. package/dist/memory/extraction-gate.js +3 -3
  55. package/dist/memory/extraction-gate.js.map +1 -1
  56. package/dist/memory/graph-memory-bridge.js +9 -9
  57. package/dist/memory/graph-memory-bridge.js.map +1 -1
  58. package/dist/memory/graph-queries.js +4 -4
  59. package/dist/memory/graph-queries.js.map +1 -1
  60. package/dist/memory/llm-extraction.js +1 -1
  61. package/dist/memory/llm-extraction.js.map +1 -1
  62. package/dist/memory/memory-bridge.js +1 -1
  63. package/dist/memory/memory-bridge.js.map +1 -1
  64. package/dist/memory/nexus-plasticity.js +1 -1
  65. package/dist/memory/nexus-plasticity.js.map +1 -1
  66. package/dist/memory/observer-reflector.js +1 -1
  67. package/dist/memory/observer-reflector.js.map +1 -1
  68. package/dist/memory/pipeline-manifest-sqlite.d.ts.map +1 -1
  69. package/dist/memory/pipeline-manifest-sqlite.js +13 -3
  70. package/dist/memory/pipeline-manifest-sqlite.js.map +1 -1
  71. package/dist/memory/quality-feedback.js +4 -4
  72. package/dist/memory/quality-feedback.js.map +1 -1
  73. package/dist/memory/retrieval/increment-citation-counts.js +1 -1
  74. package/dist/memory/retrieval/increment-citation-counts.js.map +1 -1
  75. package/dist/memory/retrieval/log-retrieval.js +1 -1
  76. package/dist/memory/retrieval/log-retrieval.js.map +1 -1
  77. package/dist/memory/retrieval/observe.js +1 -1
  78. package/dist/memory/retrieval/observe.js.map +1 -1
  79. package/dist/memory/retrieval/search.js +1 -1
  80. package/dist/memory/retrieval/search.js.map +1 -1
  81. package/dist/memory/retrieval/timeline.js +1 -1
  82. package/dist/memory/retrieval/timeline.js.map +1 -1
  83. package/dist/memory/sleep-consolidation.js +4 -4
  84. package/dist/memory/sleep-consolidation.js.map +1 -1
  85. package/dist/memory/temporal-supersession.js +5 -5
  86. package/dist/memory/temporal-supersession.js.map +1 -1
  87. package/dist/memory/transcript-extractor.js +1 -1
  88. package/dist/memory/transcript-extractor.js.map +1 -1
  89. package/dist/memory/transcript-ingestor.js +1 -1
  90. package/dist/memory/transcript-ingestor.js.map +1 -1
  91. package/dist/memory/transcript-scanner.js +1 -1
  92. package/dist/memory/transcript-scanner.js.map +1 -1
  93. package/dist/nexus/living-brain.js +7 -7
  94. package/dist/nexus/living-brain.js.map +1 -1
  95. package/dist/nexus/tasks-bridge.js +4 -4
  96. package/dist/nexus/tasks-bridge.js.map +1 -1
  97. package/dist/release/reconcile.d.ts.map +1 -1
  98. package/dist/release/reconcile.js +5 -1
  99. package/dist/release/reconcile.js.map +1 -1
  100. package/dist/sagas/migrate-containment.d.ts.map +1 -1
  101. package/dist/sagas/migrate-containment.js +4 -2
  102. package/dist/sagas/migrate-containment.js.map +1 -1
  103. package/dist/scaffold/ensure-dirs.js +1 -1
  104. package/dist/scaffold/ensure-dirs.js.map +1 -1
  105. package/dist/selfimprove/fix-gen.js +235 -99
  106. package/dist/selfimprove/fix-gen.js.map +3 -3
  107. package/dist/sentient/dream-cycle.js +1 -1
  108. package/dist/sentient/dream-cycle.js.map +1 -1
  109. package/dist/sentient/hygiene-scan.js +1 -1
  110. package/dist/sentient/hygiene-scan.js.map +1 -1
  111. package/dist/sentient/propose-tick.js +3 -3
  112. package/dist/sentient/propose-tick.js.map +1 -1
  113. package/dist/sentient/stage-drift-tick.js +1 -1
  114. package/dist/sentient/stage-drift-tick.js.map +1 -1
  115. package/dist/sequence/index.js +1 -1
  116. package/dist/sequence/index.js.map +1 -1
  117. package/dist/store/agent-registry-store.d.ts.map +1 -1
  118. package/dist/store/agent-registry-store.js +71 -28
  119. package/dist/store/agent-registry-store.js.map +1 -1
  120. package/dist/store/attachment-store.js +3 -3
  121. package/dist/store/attachment-store.js.map +1 -1
  122. package/dist/store/conduit-sqlite.d.ts +21 -1
  123. package/dist/store/conduit-sqlite.d.ts.map +1 -1
  124. package/dist/store/conduit-sqlite.js +42 -56
  125. package/dist/store/conduit-sqlite.js.map +1 -1
  126. package/dist/store/dual-scope-db.d.ts +39 -0
  127. package/dist/store/dual-scope-db.d.ts.map +1 -1
  128. package/dist/store/dual-scope-db.js +26 -8
  129. package/dist/store/dual-scope-db.js.map +1 -1
  130. package/dist/store/memory-sqlite.d.ts +57 -3
  131. package/dist/store/memory-sqlite.d.ts.map +1 -1
  132. package/dist/store/memory-sqlite.js +70 -89
  133. package/dist/store/memory-sqlite.js.map +1 -1
  134. package/dist/store/nexus-sqlite.d.ts +42 -0
  135. package/dist/store/nexus-sqlite.d.ts.map +1 -1
  136. package/dist/store/nexus-sqlite.js +79 -115
  137. package/dist/store/nexus-sqlite.js.map +1 -1
  138. package/dist/store/ports/domain-binding.d.ts +233 -0
  139. package/dist/store/ports/domain-binding.d.ts.map +1 -0
  140. package/dist/store/ports/domain-binding.js +500 -0
  141. package/dist/store/ports/domain-binding.js.map +1 -0
  142. package/dist/store/ports/fk-integrity.d.ts +155 -0
  143. package/dist/store/ports/fk-integrity.d.ts.map +1 -0
  144. package/dist/store/ports/fk-integrity.js +182 -0
  145. package/dist/store/ports/fk-integrity.js.map +1 -0
  146. package/dist/store/role-accessors-impl.js +2 -2
  147. package/dist/store/role-accessors-impl.js.map +1 -1
  148. package/dist/store/skills-db.d.ts +19 -0
  149. package/dist/store/skills-db.d.ts.map +1 -1
  150. package/dist/store/skills-db.js +80 -72
  151. package/dist/store/skills-db.js.map +1 -1
  152. package/dist/store/sqlite-backup.js +2 -2
  153. package/dist/store/sqlite-backup.js.map +1 -1
  154. package/dist/store/sqlite-data-accessor.js +1 -1
  155. package/dist/store/sqlite-data-accessor.js.map +1 -1
  156. package/dist/store/sqlite.d.ts +72 -10
  157. package/dist/store/sqlite.d.ts.map +1 -1
  158. package/dist/store/sqlite.js +173 -160
  159. package/dist/store/sqlite.js.map +1 -1
  160. package/dist/store/tasks-sqlite.js +2 -2
  161. package/dist/store/tasks-sqlite.js.map +1 -1
  162. package/dist/system/health.d.ts +0 -4
  163. package/dist/system/health.d.ts.map +1 -1
  164. package/dist/system/health.js +58 -2
  165. package/dist/system/health.js.map +1 -1
  166. package/dist/upgrade.js +2 -2
  167. package/dist/upgrade.js.map +1 -1
  168. package/dist/validation/doctor/checks.d.ts +23 -1
  169. package/dist/validation/doctor/checks.d.ts.map +1 -1
  170. package/dist/validation/doctor/checks.js +79 -19
  171. package/dist/validation/doctor/checks.js.map +1 -1
  172. package/dist/worktree/list.js +1 -1
  173. package/dist/worktree/list.js.map +1 -1
  174. package/package.json +22 -12
@@ -15670,6 +15670,18 @@ var init_service_provider = __esm({
15670
15670
  }
15671
15671
  });
15672
15672
 
15673
+ // packages/contracts/src/caamp-markers.ts
15674
+ var H, damagedMarkerSource, CAAMP_DAMAGED_START_PATTERN_SOURCE, CAAMP_DAMAGED_END_PATTERN_SOURCE;
15675
+ var init_caamp_markers = __esm({
15676
+ "packages/contracts/src/caamp-markers.ts"() {
15677
+ "use strict";
15678
+ H = "[ \\t\\r]";
15679
+ damagedMarkerSource = (keyword) => `^${H}*[<!-]{1,4}${H}*CAAMP${H}*:${H}*${keyword}${H}*[->]{1,3}${H}*$`;
15680
+ CAAMP_DAMAGED_START_PATTERN_SOURCE = damagedMarkerSource("START");
15681
+ CAAMP_DAMAGED_END_PATTERN_SOURCE = damagedMarkerSource("END");
15682
+ }
15683
+ });
15684
+
15673
15685
  // packages/contracts/src/index.ts
15674
15686
  var init_src = __esm({
15675
15687
  "packages/contracts/src/index.ts"() {
@@ -15737,6 +15749,7 @@ var init_src = __esm({
15737
15749
  init_validator();
15738
15750
  init_service_provider();
15739
15751
  init_workgraph();
15752
+ init_caamp_markers();
15740
15753
  }
15741
15754
  });
15742
15755
 
@@ -22852,7 +22865,7 @@ async function withWriteRetry(fn, opts = {}) {
22852
22865
  throw contentionErr;
22853
22866
  }
22854
22867
  function sleep(ms) {
22855
- return new Promise((resolve7) => setTimeout(resolve7, ms));
22868
+ return new Promise((resolve8) => setTimeout(resolve8, ms));
22856
22869
  }
22857
22870
  var DEFAULT_MAX_ATTEMPTS, DEFAULT_BASE_DELAY_MS, DEFAULT_JITTER_MS;
22858
22871
  var init_with_retry = __esm({
@@ -23673,7 +23686,7 @@ function resolveDbIdentity(db) {
23673
23686
  return identity;
23674
23687
  }
23675
23688
  function sleepAsync(ms) {
23676
- return new Promise((resolve7) => setTimeout(resolve7, Math.max(0, Math.round(ms))));
23689
+ return new Promise((resolve8) => setTimeout(resolve8, Math.max(0, Math.round(ms))));
23677
23690
  }
23678
23691
  function isPidAlive(pid) {
23679
23692
  try {
@@ -24491,7 +24504,7 @@ async function sendResourceRelease(socketPath, cls, holderId) {
24491
24504
  return { unavailable: true, reason: `unexpected response kind: ${result.kind}` };
24492
24505
  }
24493
24506
  function sendLeaseRequest(socketPath, request, expectedKind) {
24494
- return new Promise((resolve7) => {
24507
+ return new Promise((resolve8) => {
24495
24508
  const id = `ra-${process.pid}-${request.kind}`;
24496
24509
  const envelope = LeaseIpcRequestEnvelopeSchema.parse({
24497
24510
  protocol_version: LEASE_IPC_PROTOCOL_VERSION,
@@ -24509,7 +24522,7 @@ function sendLeaseRequest(socketPath, request, expectedKind) {
24509
24522
  settled = true;
24510
24523
  socket.removeAllListeners();
24511
24524
  socket.destroy();
24512
- resolve7(value);
24525
+ resolve8(value);
24513
24526
  };
24514
24527
  const degrade = (reason) => done({ unavailable: true, reason });
24515
24528
  socket.setTimeout(REQUEST_TIMEOUT_MS);
@@ -28062,8 +28075,8 @@ async function openDualScopeDbAtPath(scope, dbPath, exodusCwd, options) {
28062
28075
  }
28063
28076
  let initResolve = null;
28064
28077
  let initReject = null;
28065
- const initPromise = new Promise((resolve7, reject) => {
28066
- initResolve = resolve7;
28078
+ const initPromise = new Promise((resolve8, reject) => {
28079
+ initResolve = resolve8;
28067
28080
  initReject = reject;
28068
28081
  });
28069
28082
  _cache.set(key, {
@@ -28316,24 +28329,35 @@ var init_dual_scope_db = __esm({
28316
28329
  async openProject(dbPath, options) {
28317
28330
  const normalized = resolve3(dbPath);
28318
28331
  if (options?.dedicated) {
28319
- const handle = await openDualScopeDbAtPath("project", normalized, void 0, {
28332
+ const handle = await openDualScopeDbAtPath("project", normalized, options?.exodusCwd, {
28320
28333
  dedicated: true
28321
28334
  });
28322
28335
  return this.buildStore("project", handle, 0);
28323
28336
  }
28324
- return this.openEntry("project", normalized);
28337
+ return this.openEntry("project", normalized, 0, options?.exodusCwd);
28325
28338
  }
28326
28339
  /** @inheritdoc */
28327
28340
  async openGlobal(options) {
28328
28341
  const dbPath = resolveDualScopeDbPath("global");
28329
28342
  const normalized = resolve3(dbPath);
28330
28343
  if (options?.dedicated) {
28331
- const handle = await openDualScopeDbAtPath("global", normalized, void 0, {
28344
+ const handle = await openDualScopeDbAtPath("global", normalized, options?.exodusCwd, {
28345
+ dedicated: true
28346
+ });
28347
+ return this.buildStore("global", handle, 0);
28348
+ }
28349
+ return this.openEntry("global", normalized, 0, options?.exodusCwd);
28350
+ }
28351
+ /** @inheritdoc */
28352
+ async openGlobalAt(dbPath, options) {
28353
+ const normalized = resolve3(dbPath);
28354
+ if (options?.dedicated) {
28355
+ const handle = await openDualScopeDbAtPath("global", normalized, options?.exodusCwd, {
28332
28356
  dedicated: true
28333
28357
  });
28334
28358
  return this.buildStore("global", handle, 0);
28335
28359
  }
28336
- return this.openEntry("global", normalized);
28360
+ return this.openEntry("global", normalized, 0, options?.exodusCwd);
28337
28361
  }
28338
28362
  /** @inheritdoc */
28339
28363
  closeProject(dbPath) {
@@ -28359,7 +28383,7 @@ var init_dual_scope_db = __esm({
28359
28383
  * retry calls this with `depth + 1`. A second dead handle at depth 1
28360
28384
  * throws {@link LivenessExhaustedError} instead of recursing further.
28361
28385
  */
28362
- async openEntry(scope, dbPath, depth = 0) {
28386
+ async openEntry(scope, dbPath, depth = 0, exodusCwd) {
28363
28387
  const key = cacheKey(scope, dbPath);
28364
28388
  const existing = this._registry.get(key);
28365
28389
  if (existing) {
@@ -28374,8 +28398,8 @@ var init_dual_scope_db = __esm({
28374
28398
  const entryId = ++this._nextId;
28375
28399
  let initResolve;
28376
28400
  let initReject;
28377
- const initPromise = new Promise((resolve7, reject) => {
28378
- initResolve = resolve7;
28401
+ const initPromise = new Promise((resolve8, reject) => {
28402
+ initResolve = resolve8;
28379
28403
  initReject = reject;
28380
28404
  });
28381
28405
  this._registry.set(key, { id: entryId, store: null, initPromise });
@@ -28386,7 +28410,7 @@ var init_dual_scope_db = __esm({
28386
28410
  }
28387
28411
  });
28388
28412
  try {
28389
- const dualHandle = scope === "project" ? await this.openDualScopeFn("project", dbPath) : await this.openDualScopeFn("global", dbPath);
28413
+ const dualHandle = scope === "project" ? await this.openDualScopeFn("project", dbPath, exodusCwd) : await this.openDualScopeFn("global", dbPath, exodusCwd);
28390
28414
  const current = this._registry.get(key);
28391
28415
  if (!current || current.id !== entryId) {
28392
28416
  throw new EntryCancelledError(scope, dbPath);
@@ -28397,7 +28421,7 @@ var init_dual_scope_db = __esm({
28397
28421
  if (depth >= 1) {
28398
28422
  throw new LivenessExhaustedError(scope, dbPath);
28399
28423
  }
28400
- const retryStore = await this.openEntry(scope, dbPath, depth + 1);
28424
+ const retryStore = await this.openEntry(scope, dbPath, depth + 1, exodusCwd);
28401
28425
  initResolve(retryStore);
28402
28426
  return retryStore;
28403
28427
  }
@@ -28493,6 +28517,197 @@ var init_dual_scope_db = __esm({
28493
28517
  }
28494
28518
  });
28495
28519
 
28520
+ // packages/core/src/store/ports/domain-binding.ts
28521
+ import { resolve as resolve4 } from "node:path";
28522
+ function getCleoRuntime() {
28523
+ if (!_runtime) {
28524
+ _runtime = createCleoRuntime();
28525
+ }
28526
+ return _runtime;
28527
+ }
28528
+ function resetCleoRuntime() {
28529
+ releaseAllDomainBindings();
28530
+ if (_runtime) {
28531
+ try {
28532
+ _runtime.closeAll();
28533
+ } catch {
28534
+ }
28535
+ _runtime = null;
28536
+ }
28537
+ }
28538
+ function bindingKey(scope, dbPath, domain) {
28539
+ return `${scope}::${dbPath}::${domain}`;
28540
+ }
28541
+ function nativeOf(store) {
28542
+ const native = store.db.$client;
28543
+ if (!native) {
28544
+ throw new Error(
28545
+ `T12037: CleoRuntime store for ${store.scope}::${store.dbPath} exposed no $client \u2014 cannot bind a domain schema to it.`
28546
+ );
28547
+ }
28548
+ return native;
28549
+ }
28550
+ function reuseValid(key, store) {
28551
+ const row = _bindings.get(key);
28552
+ if (!row) return void 0;
28553
+ if (row.store !== store || !store.isOpen || !row.native.isOpen) {
28554
+ _bindings.delete(key);
28555
+ return void 0;
28556
+ }
28557
+ return row;
28558
+ }
28559
+ async function bindAgainst(key, store, establish) {
28560
+ const cached = reuseValid(key, store);
28561
+ if (cached) return cached;
28562
+ const pending = _inflight.get(key);
28563
+ if (pending) {
28564
+ const row = await pending;
28565
+ if (row.store === store && row.native.isOpen) return row;
28566
+ return bindAgainst(key, store, establish);
28567
+ }
28568
+ const native = nativeOf(store);
28569
+ const flight = (async () => {
28570
+ const db = await establish(native, store);
28571
+ const row = { store, native, db };
28572
+ if (store.isOpen && native.isOpen) {
28573
+ _bindings.set(key, row);
28574
+ }
28575
+ return row;
28576
+ })();
28577
+ _inflight.set(key, flight);
28578
+ try {
28579
+ return await flight;
28580
+ } finally {
28581
+ if (_inflight.get(key) === flight) {
28582
+ _inflight.delete(key);
28583
+ }
28584
+ }
28585
+ }
28586
+ async function bindLive(scope, key, dbPath, openStore, establish) {
28587
+ let lastError;
28588
+ for (let attempt = 0; attempt < MAX_LIVENESS_ATTEMPTS; attempt += 1) {
28589
+ let store;
28590
+ try {
28591
+ store = await openStore();
28592
+ } catch (err) {
28593
+ lastError = err;
28594
+ releaseDomainBindings({ scope, dbPath });
28595
+ if (attempt + 1 >= MAX_LIVENESS_ATTEMPTS) throw err;
28596
+ continue;
28597
+ }
28598
+ try {
28599
+ const row = await bindAgainst(key, store, establish);
28600
+ if (row.store.isOpen && row.native.isOpen) return row;
28601
+ } catch (err) {
28602
+ lastError = err;
28603
+ if (store.isOpen) throw err;
28604
+ }
28605
+ releaseDomainBindings({ scope, dbPath });
28606
+ try {
28607
+ store.close();
28608
+ } catch {
28609
+ }
28610
+ }
28611
+ if (lastError !== void 0) throw lastError;
28612
+ throw new Error(
28613
+ `T12037: could not bind ${key} over a live connection after ${MAX_LIVENESS_ATTEMPTS} attempts \u2014 the consolidated handle was closed each time.`
28614
+ );
28615
+ }
28616
+ async function bindProjectDomain(domain, cwd, establish) {
28617
+ const dbPath = resolve4(resolveDualScopeDbPath("project", cwd));
28618
+ const row = await bindLive(
28619
+ "project",
28620
+ bindingKey("project", dbPath, domain),
28621
+ dbPath,
28622
+ () => getCleoRuntime().openProject(dbPath, { exodusCwd: cwd }),
28623
+ establish
28624
+ );
28625
+ return row;
28626
+ }
28627
+ async function bindGlobalDomain(domain, establish, cwd) {
28628
+ const dbPath = resolve4(resolveDualScopeDbPath("global"));
28629
+ const row = await bindLive(
28630
+ "global",
28631
+ bindingKey("global", dbPath, domain),
28632
+ dbPath,
28633
+ () => getCleoRuntime().openGlobal({ exodusCwd: cwd }),
28634
+ establish
28635
+ );
28636
+ return row;
28637
+ }
28638
+ async function bindGlobalDomainAtPath(domain, dbPath, establish) {
28639
+ const normalized = resolve4(dbPath);
28640
+ const row = await bindLive(
28641
+ "global",
28642
+ bindingKey("global", normalized, domain),
28643
+ normalized,
28644
+ () => getCleoRuntime().openGlobalAt(normalized),
28645
+ establish
28646
+ );
28647
+ return row;
28648
+ }
28649
+ function peekDomainAtPath(scope, dbPath, domain) {
28650
+ const row = _bindings.get(bindingKey(scope, resolve4(dbPath), domain));
28651
+ if (!row?.native.isOpen) return null;
28652
+ return row;
28653
+ }
28654
+ function releaseDomainBindings(filter) {
28655
+ if (!filter) {
28656
+ _bindings.clear();
28657
+ return;
28658
+ }
28659
+ const wantPath = filter.dbPath === void 0 ? void 0 : resolve4(filter.dbPath);
28660
+ for (const key of [..._bindings.keys()]) {
28661
+ const sep2 = key.indexOf("::");
28662
+ const lastSep = key.lastIndexOf("::");
28663
+ const scope = key.slice(0, sep2);
28664
+ const dbPath = key.slice(sep2 + 2, lastSep);
28665
+ const domain = key.slice(lastSep + 2);
28666
+ if (filter.scope !== void 0 && scope !== filter.scope) continue;
28667
+ if (wantPath !== void 0 && dbPath !== wantPath) continue;
28668
+ if (filter.domain !== void 0 && domain !== filter.domain) continue;
28669
+ _bindings.delete(key);
28670
+ }
28671
+ }
28672
+ function releaseAllDomainBindings() {
28673
+ _bindings.clear();
28674
+ }
28675
+ function boundProjectNative(domain, cwd) {
28676
+ return peekProjectDomain(domain, cwd)?.native ?? null;
28677
+ }
28678
+ function peekProjectDomain(domain, cwd) {
28679
+ const dbPath = resolve4(resolveDualScopeDbPath("project", cwd));
28680
+ const row = _bindings.get(bindingKey("project", dbPath, domain));
28681
+ if (row?.native.isOpen) return row;
28682
+ if (cwd !== void 0) return null;
28683
+ const suffix = `::${domain}`;
28684
+ let sole;
28685
+ for (const [key, candidate] of _bindings) {
28686
+ if (!key.startsWith("project::") || !key.endsWith(suffix)) continue;
28687
+ if (!candidate.native.isOpen) continue;
28688
+ if (sole) return null;
28689
+ sole = candidate;
28690
+ }
28691
+ return sole ?? null;
28692
+ }
28693
+ function peekGlobalDomain(domain) {
28694
+ const dbPath = resolve4(resolveDualScopeDbPath("global"));
28695
+ const row = _bindings.get(bindingKey("global", dbPath, domain));
28696
+ if (!row || !row.native.isOpen) return null;
28697
+ return row;
28698
+ }
28699
+ var _runtime, _bindings, _inflight, MAX_LIVENESS_ATTEMPTS;
28700
+ var init_domain_binding = __esm({
28701
+ "packages/core/src/store/ports/domain-binding.ts"() {
28702
+ "use strict";
28703
+ init_dual_scope_db();
28704
+ _runtime = null;
28705
+ _bindings = /* @__PURE__ */ new Map();
28706
+ _inflight = /* @__PURE__ */ new Map();
28707
+ MAX_LIVENESS_ATTEMPTS = 2;
28708
+ }
28709
+ });
28710
+
28496
28711
  // packages/core/src/store/schema/nexus-schema.ts
28497
28712
  var nexus_schema_exports = {};
28498
28713
  __export(nexus_schema_exports, {
@@ -29001,6 +29216,7 @@ var nexus_sqlite_exports = {};
29001
29216
  __export(nexus_sqlite_exports, {
29002
29217
  NEXUS_SCHEMA_VERSION: () => NEXUS_SCHEMA_VERSION,
29003
29218
  _resetNestedNexusWarningGate: () => _resetNestedNexusWarningGate,
29219
+ bindNexusDomain: () => bindNexusDomain,
29004
29220
  closeNexusDb: () => closeNexusDb,
29005
29221
  detectAndWarnOnNestedNexus: () => detectAndWarnOnNestedNexus,
29006
29222
  getNestedNexusSentinelPath: () => getNestedNexusSentinelPath,
@@ -29209,13 +29425,13 @@ function ensureNexusRelationWeights(nativeDb) {
29209
29425
  nativeDb.exec(`PRAGMA foreign_keys=${fkWasOn ? "ON" : "OFF"}`);
29210
29426
  }
29211
29427
  }
29212
- function runNexusMigrations(nativeDb, db) {
29428
+ function runNexusMigrations(nativeDb, db, dbPath) {
29213
29429
  const migrationsFolder = resolveNexusMigrationsFolder();
29214
- if (tableExists4(nativeDb, "nexus_nodes") && _nexusDbPath) {
29215
- const backupPath = _nexusDbPath.replace(/\.db$/, "-pre-cleo.db.bak");
29430
+ if (tableExists4(nativeDb, "nexus_nodes")) {
29431
+ const backupPath = dbPath.replace(/\.db$/, "-pre-cleo.db.bak");
29216
29432
  if (!existsSync16(backupPath)) {
29217
29433
  try {
29218
- copyFileSync4(_nexusDbPath, backupPath);
29434
+ copyFileSync4(dbPath, backupPath);
29219
29435
  } catch {
29220
29436
  }
29221
29437
  }
@@ -29257,75 +29473,45 @@ function runNexusMigrations(nativeDb, db) {
29257
29473
  ensureNexusFts5(nativeDb);
29258
29474
  }
29259
29475
  async function getNexusDb() {
29260
- const requestedPath = getNexusDbPath();
29476
+ return (await bindNexusDomain()).db.drizzle;
29477
+ }
29478
+ async function bindNexusDomain() {
29261
29479
  const requestedRegistryPath = getNexusRegistryDbPath();
29262
- if (_nexusDb && _nexusDbPath !== requestedPath) {
29263
- resetNexusDbState();
29480
+ const bound = peekProjectDomain("nexus");
29481
+ if (bound && bound.db.registryPath !== requestedRegistryPath) {
29482
+ releaseDomainBindings({ scope: "project", domain: "nexus" });
29264
29483
  }
29265
- if (_nexusDb && _nexusRegistryPath !== requestedRegistryPath) {
29266
- resetNexusDbState();
29267
- }
29268
- if (_nexusDb && (_nexusNativeDb === null || !_nexusNativeDb.isOpen)) {
29269
- resetNexusDbState();
29270
- }
29271
- if (_nexusDb) {
29272
- if (_nexusNativeDb) {
29273
- try {
29274
- ensureGlobalRegistryAttached(_nexusNativeDb);
29275
- } catch {
29276
- resetNexusDbState();
29277
- }
29278
- }
29279
- if (_nexusDb) return _nexusDb;
29280
- }
29281
- if (_nexusInitPromise) return _nexusInitPromise;
29282
- _nexusInitPromise = (async () => {
29283
- const dbPath = requestedPath;
29284
- _nexusDbPath = dbPath;
29285
- _nexusRegistryPath = requestedRegistryPath;
29286
- detectAndWarnOnNestedNexus();
29287
- await openDualScopeDb("global");
29288
- const dualHandle = await openDualScopeDb("project");
29289
- const nativeDb = dualHandle.db.$client ?? null;
29290
- if (!nativeDb) {
29291
- throw new Error(
29292
- "T11648: openDualScopeDb returned a handle without $client \u2014 cannot extract DatabaseSync for the nexus-schema drizzle wrapping."
29293
- );
29294
- }
29295
- _nexusNativeDb = nativeDb;
29484
+ detectAndWarnOnNestedNexus();
29485
+ await openDualScopeDb("global");
29486
+ const binding = await bindProjectDomain("nexus", void 0, (nativeDb, store) => {
29296
29487
  ensureGlobalRegistryAttached(nativeDb);
29297
29488
  const db = drizzle({ client: nativeDb });
29298
- runNexusMigrations(nativeDb, db);
29489
+ runNexusMigrations(nativeDb, db, store.dbPath);
29299
29490
  nativeDb.prepare(
29300
29491
  `INSERT OR IGNORE INTO nexus_schema_meta (key, value) VALUES ('schemaVersion', '${NEXUS_SCHEMA_VERSION}')`
29301
29492
  ).run();
29302
- _nexusDb = db;
29303
- return db;
29304
- })();
29493
+ return { drizzle: db, registryPath: requestedRegistryPath };
29494
+ });
29305
29495
  try {
29306
- return await _nexusInitPromise;
29307
- } finally {
29308
- _nexusInitPromise = null;
29496
+ ensureGlobalRegistryAttached(binding.native);
29497
+ } catch {
29498
+ releaseDomainBindings({ scope: "project", domain: "nexus" });
29499
+ throw new Error(
29500
+ "T12039: nexus binding lost its GLOBAL registry ATTACH and could not re-attach."
29501
+ );
29309
29502
  }
29503
+ return binding;
29310
29504
  }
29311
29505
  function closeNexusDb() {
29312
- _nexusNativeDb = null;
29313
- _nexusDb = null;
29314
- _nexusDbPath = null;
29315
- _nexusRegistryPath = null;
29316
- _nexusInitPromise = null;
29506
+ releaseDomainBindings({ scope: "project", domain: "nexus" });
29317
29507
  }
29318
29508
  function resetNexusDbState() {
29319
- _nexusNativeDb = null;
29320
- _nexusDb = null;
29321
- _nexusDbPath = null;
29322
- _nexusRegistryPath = null;
29323
- _nexusInitPromise = null;
29509
+ releaseDomainBindings({ scope: "project", domain: "nexus" });
29324
29510
  }
29325
29511
  function getNexusNativeDb() {
29326
- return _nexusNativeDb;
29512
+ return boundProjectNative("nexus");
29327
29513
  }
29328
- var NEXUS_SCHEMA_VERSION, _nexusDb, _nexusNativeDb, _nexusDbPath, _nexusRegistryPath, _nexusInitPromise, NEXUS_GLOBAL_ATTACH_ALIAS, NESTED_NEXUS_SENTINEL, _warnedNestedPaths;
29514
+ var NEXUS_SCHEMA_VERSION, NEXUS_GLOBAL_ATTACH_ALIAS, NESTED_NEXUS_SENTINEL, _warnedNestedPaths;
29329
29515
  var init_nexus_sqlite = __esm({
29330
29516
  "packages/core/src/store/nexus-sqlite.ts"() {
29331
29517
  "use strict";
@@ -29334,15 +29520,11 @@ var init_nexus_sqlite = __esm({
29334
29520
  init_paths();
29335
29521
  init_dual_scope_db();
29336
29522
  init_migration_manager();
29523
+ init_domain_binding();
29337
29524
  init_resolve_migrations_folder();
29338
29525
  init_nexus_schema();
29339
29526
  init_with_retry();
29340
29527
  NEXUS_SCHEMA_VERSION = "1.0.0";
29341
- _nexusDb = null;
29342
- _nexusNativeDb = null;
29343
- _nexusDbPath = null;
29344
- _nexusRegistryPath = null;
29345
- _nexusInitPromise = null;
29346
29528
  NEXUS_GLOBAL_ATTACH_ALIAS = "nexus_global";
29347
29529
  NESTED_NEXUS_SENTINEL = "nexus.db";
29348
29530
  _warnedNestedPaths = /* @__PURE__ */ new Set();
@@ -29354,7 +29536,7 @@ import { AsyncLocalStorage } from "node:async_hooks";
29354
29536
  import { existsSync as existsSync17, readFileSync as readFileSync5, statSync as statSync6 } from "node:fs";
29355
29537
  import { createRequire as createRequire5 } from "node:module";
29356
29538
  import { homedir } from "node:os";
29357
- import { basename as basename4, dirname as dirname7, join as join17, resolve as resolve4 } from "node:path";
29539
+ import { basename as basename4, dirname as dirname7, join as join17, resolve as resolve5 } from "node:path";
29358
29540
  import {
29359
29541
  computeCanonicalProjectId as _computeCanonicalProjectId,
29360
29542
  getCanonicalTemplatesTildePath as _getCanonicalTemplatesTildePath,
@@ -29421,24 +29603,24 @@ function getCleoDirAbsolute(cwd, opts) {
29421
29603
  try {
29422
29604
  const projectRoot = getProjectRoot(cwd);
29423
29605
  const canonical = _resolveCanonicalCleoDir(project.projectId);
29424
- if (canonical !== null && canonical === resolve4(projectRoot, cleoDir)) {
29606
+ if (canonical !== null && canonical === resolve5(projectRoot, cleoDir)) {
29425
29607
  return canonical;
29426
29608
  }
29427
- return resolve4(projectRoot, cleoDir);
29609
+ return resolve5(projectRoot, cleoDir);
29428
29610
  } catch {
29429
- return resolve4(project.projectRoot, cleoDir);
29611
+ return resolve5(project.projectRoot, cleoDir);
29430
29612
  }
29431
29613
  }
29432
29614
  try {
29433
- return resolve4(getProjectRoot(cwd), cleoDir);
29615
+ return resolve5(getProjectRoot(cwd), cleoDir);
29434
29616
  } catch (err) {
29435
29617
  if (opts?.bootstrap) {
29436
- return resolve4(cwd ?? process.cwd(), cleoDir);
29618
+ return resolve5(cwd ?? process.cwd(), cleoDir);
29437
29619
  }
29438
29620
  if (_cwdHasGitAncestor(cwd)) {
29439
29621
  throw err;
29440
29622
  }
29441
- return resolve4(cwd ?? process.cwd(), cleoDir);
29623
+ return resolve5(cwd ?? process.cwd(), cleoDir);
29442
29624
  }
29443
29625
  }
29444
29626
  function _resolveProjectByCwdFromNexus(cwd) {
@@ -29446,7 +29628,7 @@ function _resolveProjectByCwdFromNexus(cwd) {
29446
29628
  const cleoHome = getCleoHome();
29447
29629
  const globalDbPath = join17(cleoHome, "cleo.db");
29448
29630
  if (!existsSync17(globalDbPath)) return null;
29449
- const start = resolve4(cwd ?? process.cwd());
29631
+ const start = resolve5(cwd ?? process.cwd());
29450
29632
  let current = start;
29451
29633
  const DatabaseSync3 = _getDatabaseSyncCtor();
29452
29634
  const db = new DatabaseSync3(globalDbPath, { readOnly: true });
@@ -29479,7 +29661,7 @@ function _cleoDirEnvOverride() {
29479
29661
  }
29480
29662
  function _findCleoDirRoot(cwd) {
29481
29663
  const homeRoot = homedir();
29482
- let current = resolve4(cwd ?? process.cwd());
29664
+ let current = resolve5(cwd ?? process.cwd());
29483
29665
  while (true) {
29484
29666
  const isDangerousRoot = current === homeRoot || current === "/" || current === "";
29485
29667
  if (!isDangerousRoot) {
@@ -29509,7 +29691,7 @@ function resolveCleoDir(cwd) {
29509
29691
  if (root !== null) {
29510
29692
  return join17(root, ".cleo");
29511
29693
  }
29512
- const start = resolve4(cwd ?? process.cwd());
29694
+ const start = resolve5(cwd ?? process.cwd());
29513
29695
  let current = start;
29514
29696
  while (true) {
29515
29697
  const mainRepo = _resolveMainRepoFromGitlink(current);
@@ -29545,15 +29727,15 @@ function _resolveCleoDir(cwd) {
29545
29727
  const project = _pathsResolveProjectByCwd(cwd);
29546
29728
  if (project !== null) {
29547
29729
  try {
29548
- return resolve4(getProjectRoot(cwd), cleoDir);
29730
+ return resolve5(getProjectRoot(cwd), cleoDir);
29549
29731
  } catch {
29550
- return resolve4(project.projectRoot, cleoDir);
29732
+ return resolve5(project.projectRoot, cleoDir);
29551
29733
  }
29552
29734
  }
29553
29735
  return getCleoDirAbsolute(cwd);
29554
29736
  }
29555
29737
  function _cwdHasGitAncestor(cwd) {
29556
- const start = resolve4(cwd ?? process.cwd());
29738
+ const start = resolve5(cwd ?? process.cwd());
29557
29739
  let current = start;
29558
29740
  while (true) {
29559
29741
  const gitMarker = join17(current, ".git");
@@ -29653,7 +29835,7 @@ function getProjectRoot(cwd) {
29653
29835
  }
29654
29836
  return cleoDirEnv;
29655
29837
  }
29656
- const start = resolve4(cwd ?? process.cwd());
29838
+ const start = resolve5(cwd ?? process.cwd());
29657
29839
  let current = start;
29658
29840
  const mainRepoFromStart = _resolveMainRepoFromGitlink(start);
29659
29841
  if (mainRepoFromStart !== null) return mainRepoFromStart;
@@ -29746,7 +29928,7 @@ async function registerProjectOnEncounter(projectRoot, infoProjectId) {
29746
29928
  const db = await getNexusDb2();
29747
29929
  const existingRows = await db.select().from(projectRegistry2).where(eq11(projectRegistry2.projectId, immutableId)).limit(1);
29748
29930
  const now = (/* @__PURE__ */ new Date()).toISOString();
29749
- const resolvedPath = resolve4(projectRoot);
29931
+ const resolvedPath = resolve5(projectRoot);
29750
29932
  const projectName = _readProjectNameFromInfo(resolvedPath) || basename4(projectRoot) || "unnamed";
29751
29933
  if (existingRows.length > 0) {
29752
29934
  const existingPath = existingRows[0].projectPath;
@@ -32315,6 +32497,7 @@ var init_brain_embedding = __esm({
32315
32497
  var memory_sqlite_exports = {};
32316
32498
  __export(memory_sqlite_exports, {
32317
32499
  BRAIN_SCHEMA_VERSION: () => BRAIN_SCHEMA_VERSION,
32500
+ bindBrainDomain: () => bindBrainDomain,
32318
32501
  brainSchema: () => memory_schema_exports,
32319
32502
  closeBrainDb: () => closeBrainDb,
32320
32503
  getBrainDb: () => getBrainDb,
@@ -32344,7 +32527,7 @@ function brainTablesAreConsolidatedShape(nativeDb) {
32344
32527
  const createdAt = cols.find((c) => c.name === "created_at");
32345
32528
  return createdAt !== void 0 && createdAt.type.toUpperCase() !== "INTEGER";
32346
32529
  }
32347
- function establishLegacyBrainSchema(nativeDb, db) {
32530
+ function establishLegacyBrainSchema(nativeDb, db, dbPath) {
32348
32531
  const log12 = getLogger("brain-schema");
32349
32532
  if (brainTablesAreConsolidatedShape(nativeDb)) {
32350
32533
  const fkRow = nativeDb.prepare("PRAGMA foreign_keys").get();
@@ -32381,8 +32564,8 @@ function establishLegacyBrainSchema(nativeDb, db) {
32381
32564
  );
32382
32565
  return;
32383
32566
  }
32384
- if (tableExists(nativeDb, "brain_decisions") && _dbPath) {
32385
- createSafetyBackup(_dbPath);
32567
+ if (tableExists(nativeDb, "brain_decisions")) {
32568
+ createSafetyBackup(dbPath);
32386
32569
  }
32387
32570
  reconcileJournal(nativeDb, migrationsFolder, "brain_decisions", "brain");
32388
32571
  migrateWithRetry(db, migrationsFolder, nativeDb, "brain_decisions", "brain");
@@ -32401,8 +32584,8 @@ function initializeBrainVec(nativeDb) {
32401
32584
  "CREATE VIRTUAL TABLE IF NOT EXISTS brain_embeddings USING vec0(id TEXT PRIMARY KEY, embedding FLOAT[384])"
32402
32585
  ).run();
32403
32586
  }
32404
- function isBrainVecLoaded() {
32405
- return _vecLoaded;
32587
+ function isBrainVecLoaded(cwd) {
32588
+ return peekProjectDomain("brain", cwd)?.db.vecLoaded ?? false;
32406
32589
  }
32407
32590
  async function initEmbeddingProvider(cwd) {
32408
32591
  try {
@@ -32416,92 +32599,52 @@ async function initEmbeddingProvider(cwd) {
32416
32599
  }
32417
32600
  }
32418
32601
  async function getBrainDb(cwd) {
32419
- const requestedPath = getBrainDbPath(cwd);
32602
+ return (await bindBrainDomain(cwd)).db.drizzle;
32603
+ }
32604
+ async function bindBrainDomain(cwd) {
32420
32605
  const { assertTestEnv: assertTestEnv2 } = await Promise.resolve().then(() => (init_data_accessor(), data_accessor_exports));
32421
- assertTestEnv2(requestedPath);
32422
- if (_db && _dbPath !== requestedPath) {
32423
- resetBrainDbState();
32424
- }
32425
- if (_db && (_nativeDb === null || !_nativeDb.isOpen)) {
32426
- resetBrainDbState();
32427
- }
32428
- if (_db) return _db;
32429
- if (_initPromise) return _initPromise;
32430
- _initPromise = (async () => {
32431
- let nativeDb = null;
32432
- for (let attempt = 0; attempt < 2; attempt += 1) {
32433
- const dualHandle = await openDualScopeDb("project", cwd);
32434
- nativeDb = dualHandle.db.$client ?? null;
32435
- if (!nativeDb) {
32436
- throw new Error(
32437
- "E6-L2: openDualScopeDb returned a handle without $client \u2014 cannot extract DatabaseSync for legacy brain-schema wrapping."
32438
- );
32439
- }
32440
- if (nativeDb.isOpen) break;
32441
- }
32442
- if (!nativeDb?.isOpen) {
32443
- throw new Error("E6-L2: openDualScopeDb repeatedly returned a closed DatabaseSync handle.");
32444
- }
32445
- _nativeDb = nativeDb;
32446
- _dbPath = requestedPath;
32447
- _vecLoaded = loadBrainVecExtension(nativeDb);
32448
- const db = _getDrizzle()({ client: nativeDb });
32449
- establishLegacyBrainSchema(nativeDb, db);
32450
- if (_vecLoaded) {
32606
+ assertTestEnv2(getBrainDbPath(cwd));
32607
+ return bindProjectDomain("brain", cwd, (nativeDb, store) => {
32608
+ const vecLoaded = loadBrainVecExtension(nativeDb);
32609
+ const drizzle2 = _getDrizzle()({ client: nativeDb });
32610
+ establishLegacyBrainSchema(nativeDb, drizzle2, store.dbPath);
32611
+ if (vecLoaded) {
32451
32612
  initializeBrainVec(nativeDb);
32452
32613
  }
32453
32614
  nativeDb.prepare(
32454
32615
  `INSERT OR IGNORE INTO brain_schema_meta (key, value) VALUES ('schemaVersion', '${BRAIN_SCHEMA_VERSION}')`
32455
32616
  ).run();
32456
- _db = db;
32457
- if (_vecLoaded) {
32617
+ if (vecLoaded) {
32458
32618
  setImmediate(() => {
32459
32619
  initEmbeddingProvider(cwd).catch(() => {
32460
32620
  });
32461
32621
  });
32462
32622
  }
32463
- return db;
32464
- })();
32465
- try {
32466
- return await _initPromise;
32467
- } finally {
32468
- _initPromise = null;
32469
- }
32623
+ return { drizzle: drizzle2, vecLoaded };
32624
+ });
32470
32625
  }
32471
32626
  function closeBrainDb() {
32472
- _nativeDb = null;
32473
- _db = null;
32474
- _dbPath = null;
32475
- _initPromise = null;
32476
- _vecLoaded = false;
32627
+ releaseDomainBindings({ scope: "project", domain: "brain" });
32477
32628
  }
32478
32629
  function resetBrainDbState() {
32479
- _nativeDb = null;
32480
- _db = null;
32481
- _dbPath = null;
32482
- _initPromise = null;
32483
- _vecLoaded = false;
32630
+ releaseDomainBindings({ scope: "project", domain: "brain" });
32484
32631
  }
32485
- function getBrainNativeDb() {
32486
- return _nativeDb;
32632
+ function getBrainNativeDb(cwd) {
32633
+ return boundProjectNative("brain", cwd);
32487
32634
  }
32488
- var _require3, _drizzle2, BRAIN_SCHEMA_VERSION, _db, _nativeDb, _dbPath, _initPromise, _vecLoaded, CONSOLIDATED_BRAIN_TABLES;
32635
+ var _require3, _drizzle2, BRAIN_SCHEMA_VERSION, CONSOLIDATED_BRAIN_TABLES;
32489
32636
  var init_memory_sqlite = __esm({
32490
32637
  "packages/core/src/store/memory-sqlite.ts"() {
32491
32638
  "use strict";
32492
32639
  init_logger2();
32493
32640
  init_dual_scope_db();
32494
32641
  init_migration_manager();
32642
+ init_domain_binding();
32495
32643
  init_resolve_migrations_folder();
32496
32644
  init_memory_schema();
32497
32645
  _require3 = createRequire6(import.meta.url);
32498
32646
  _drizzle2 = null;
32499
32647
  BRAIN_SCHEMA_VERSION = "1.0.0";
32500
- _db = null;
32501
- _nativeDb = null;
32502
- _dbPath = null;
32503
- _initPromise = null;
32504
- _vecLoaded = false;
32505
32648
  CONSOLIDATED_BRAIN_TABLES = [
32506
32649
  "brain_attention",
32507
32650
  "brain_backfill_runs",
@@ -36324,41 +36467,41 @@ function runTelemetryMigrations(nativeDb, db) {
36324
36467
  }
36325
36468
  function resetTelemetryDbState() {
36326
36469
  try {
36327
- _nativeDb2?.close();
36470
+ _nativeDb?.close();
36328
36471
  } catch {
36329
36472
  }
36330
- _db2 = null;
36331
- _nativeDb2 = null;
36332
- _dbPath2 = null;
36333
- _initPromise2 = null;
36473
+ _db = null;
36474
+ _nativeDb = null;
36475
+ _dbPath = null;
36476
+ _initPromise = null;
36334
36477
  }
36335
36478
  function getTelemetryNativeDb() {
36336
- return _nativeDb2;
36479
+ return _nativeDb;
36337
36480
  }
36338
36481
  async function getTelemetryDb() {
36339
36482
  const requestedPath = getTelemetryDbPath();
36340
- if (_db2 && _dbPath2 !== requestedPath) {
36483
+ if (_db && _dbPath !== requestedPath) {
36341
36484
  resetTelemetryDbState();
36342
36485
  }
36343
- if (_db2) return _db2;
36344
- if (_initPromise2) return _initPromise2;
36345
- _initPromise2 = (async () => {
36486
+ if (_db) return _db;
36487
+ if (_initPromise) return _initPromise;
36488
+ _initPromise = (async () => {
36346
36489
  const dbPath = requestedPath;
36347
- _dbPath2 = dbPath;
36490
+ _dbPath = dbPath;
36348
36491
  mkdirSync5(dirname8(dbPath), { recursive: true });
36349
36492
  const nativeDb = openNativeDatabase(dbPath);
36350
- _nativeDb2 = nativeDb;
36493
+ _nativeDb = nativeDb;
36351
36494
  const db = drizzle({ client: nativeDb });
36352
36495
  runTelemetryMigrations(nativeDb, db);
36353
36496
  nativeDb.prepare(
36354
36497
  `INSERT OR IGNORE INTO telemetry_schema_meta (key, value) VALUES ('schemaVersion', '${TELEMETRY_SCHEMA_VERSION}')`
36355
36498
  ).run();
36356
- _db2 = db;
36499
+ _db = db;
36357
36500
  return db;
36358
36501
  })();
36359
- return _initPromise2;
36502
+ return _initPromise;
36360
36503
  }
36361
- var DB_FILENAME, TELEMETRY_SCHEMA_VERSION, _db2, _nativeDb2, _dbPath2, _initPromise2;
36504
+ var DB_FILENAME, TELEMETRY_SCHEMA_VERSION, _db, _nativeDb, _dbPath, _initPromise;
36362
36505
  var init_sqlite2 = __esm({
36363
36506
  "packages/core/src/telemetry/sqlite.ts"() {
36364
36507
  "use strict";
@@ -36369,10 +36512,10 @@ var init_sqlite2 = __esm({
36369
36512
  init_sqlite3();
36370
36513
  DB_FILENAME = "telemetry.db";
36371
36514
  TELEMETRY_SCHEMA_VERSION = "1.0.0";
36372
- _db2 = null;
36373
- _nativeDb2 = null;
36374
- _dbPath2 = null;
36375
- _initPromise2 = null;
36515
+ _db = null;
36516
+ _nativeDb = null;
36517
+ _dbPath = null;
36518
+ _initPromise = null;
36376
36519
  }
36377
36520
  });
36378
36521
 
@@ -36404,6 +36547,9 @@ function runAgentRegistryMigrations(nativeDb) {
36404
36547
  const db = drizzle({ client: nativeDb });
36405
36548
  migrateSanitized(db, { migrationsFolder });
36406
36549
  }
36550
+ function boundAgentRegistryNative() {
36551
+ return peekDomainAtPath("global", getGlobalAgentRegistryDbPath(), AGENT_REGISTRY_DOMAIN)?.native ?? null;
36552
+ }
36407
36553
  function readAgentRegistrySchemaVersionSentinel(db) {
36408
36554
  try {
36409
36555
  const row = db.prepare("SELECT value FROM _agent_registry_meta WHERE key = 'schema_version'").get();
@@ -36426,16 +36572,36 @@ function writeAgentRegistrySchemaVersionSentinel(db) {
36426
36572
  } catch {
36427
36573
  }
36428
36574
  }
36575
+ function establishAgentRegistrySchema(native) {
36576
+ const hasSchema = (() => {
36577
+ try {
36578
+ return !!native.prepare(
36579
+ "SELECT name FROM sqlite_master WHERE type='table' AND name='agent_registry_agents'"
36580
+ ).get();
36581
+ } catch {
36582
+ return false;
36583
+ }
36584
+ })();
36585
+ if (hasSchema && readAgentRegistrySchemaVersionSentinel(native) === GLOBAL_AGENT_REGISTRY_SCHEMA_VERSION) {
36586
+ return false;
36587
+ }
36588
+ runAgentRegistryMigrations(native);
36589
+ writeAgentRegistrySchemaVersionSentinel(native);
36590
+ return true;
36591
+ }
36429
36592
  async function ensureGlobalAgentRegistryDb() {
36430
36593
  const dbPath = getGlobalAgentRegistryDbPath();
36431
36594
  const alreadyExists = existsSync19(dbPath);
36432
- const dualHandle = await openDualScopeDbAtPath("global", dbPath);
36433
- const nativeDb = dualHandle.db.$client ?? null;
36434
- if (!nativeDb) {
36435
- throw new Error(
36436
- "E6-L5: openDualScopeDb returned a handle without $client \u2014 cannot extract DatabaseSync for the Agent Registry ledger reconcile."
36437
- );
36438
- }
36595
+ let created = false;
36596
+ const { native: nativeDb } = await bindGlobalDomainAtPath(
36597
+ AGENT_REGISTRY_DOMAIN,
36598
+ dbPath,
36599
+ (native) => {
36600
+ const applied = establishAgentRegistrySchema(native);
36601
+ created = created || applied;
36602
+ return native;
36603
+ }
36604
+ );
36439
36605
  const hasSchema = (() => {
36440
36606
  try {
36441
36607
  const result = nativeDb.prepare(
@@ -36446,22 +36612,15 @@ async function ensureGlobalAgentRegistryDb() {
36446
36612
  return false;
36447
36613
  }
36448
36614
  })();
36449
- if (hasSchema && readAgentRegistrySchemaVersionSentinel(nativeDb) === GLOBAL_AGENT_REGISTRY_SCHEMA_VERSION) {
36450
- _globalAgentRegistryNativeDb = nativeDb;
36451
- return { action: "exists", path: dbPath };
36452
- }
36453
- runAgentRegistryMigrations(nativeDb);
36454
- writeAgentRegistrySchemaVersionSentinel(nativeDb);
36455
- _globalAgentRegistryNativeDb = nativeDb;
36456
36615
  return {
36457
36616
  action: alreadyExists && hasSchema ? "exists" : "created",
36458
36617
  path: dbPath
36459
36618
  };
36460
36619
  }
36461
36620
  function getGlobalAgentRegistryNativeDb() {
36462
- return _globalAgentRegistryNativeDb;
36621
+ return boundAgentRegistryNative();
36463
36622
  }
36464
- var GLOBAL_AGENT_REGISTRY_SCHEMA_VERSION, _globalAgentRegistryNativeDb;
36623
+ var GLOBAL_AGENT_REGISTRY_SCHEMA_VERSION, AGENT_REGISTRY_DOMAIN;
36465
36624
  var init_agent_registry_store = __esm({
36466
36625
  "packages/core/src/store/agent-registry-store.ts"() {
36467
36626
  "use strict";
@@ -36469,9 +36628,10 @@ var init_agent_registry_store = __esm({
36469
36628
  init_paths();
36470
36629
  init_dual_scope_db();
36471
36630
  init_migration_manager();
36631
+ init_domain_binding();
36472
36632
  init_resolve_migrations_folder();
36473
36633
  GLOBAL_AGENT_REGISTRY_SCHEMA_VERSION = "2026.4.12";
36474
- _globalAgentRegistryNativeDb = null;
36634
+ AGENT_REGISTRY_DOMAIN = "agents";
36475
36635
  }
36476
36636
  });
36477
36637
 
@@ -36505,60 +36665,36 @@ function runConduitMigrations(nativeDb) {
36505
36665
  }
36506
36666
  async function ensureConduitDb(projectRoot) {
36507
36667
  const dbPath = getConduitDbPath(projectRoot);
36508
- if (_conduitNativeDb && (!_conduitNativeDb.isOpen || _conduitDbPath !== dbPath)) {
36509
- resetConduitDbState();
36510
- }
36511
- if (_conduitNativeDb && _conduitDbPath === dbPath) {
36512
- return { action: "exists", path: dbPath };
36513
- }
36514
- if (_initPromise3) return _initPromise3;
36515
- _initPromise3 = (async () => {
36516
- const alreadyExists = existsSync20(dbPath);
36517
- const dualHandle = await openDualScopeDb("project", projectRoot);
36518
- const nativeDb = dualHandle.db.$client ?? null;
36519
- if (!nativeDb) {
36520
- throw new Error(
36521
- "E6-L3: openDualScopeDb returned a handle without $client \u2014 cannot extract DatabaseSync for legacy conduit-schema wrapping."
36522
- );
36523
- }
36668
+ const alreadyExists = peekProjectDomain("conduit", projectRoot) !== null || existsSync20(dbPath);
36669
+ await bindConduitDomain(projectRoot);
36670
+ return { action: alreadyExists ? "exists" : "created", path: dbPath };
36671
+ }
36672
+ async function bindConduitDomain(projectRoot) {
36673
+ return bindProjectDomain("conduit", projectRoot, (nativeDb) => {
36524
36674
  runConduitMigrations(nativeDb);
36525
36675
  nativeDb.exec(
36526
36676
  `INSERT OR REPLACE INTO _conduit_meta (key, value, updated_at)
36527
36677
  VALUES ('schema_version', '${CONDUIT_SCHEMA_VERSION}', strftime('%s', 'now'))`
36528
36678
  );
36529
- _conduitNativeDb = nativeDb;
36530
- _conduitDbPath = dbPath;
36531
- return { action: alreadyExists ? "exists" : "created", path: dbPath };
36532
- })();
36533
- try {
36534
- return await _initPromise3;
36535
- } finally {
36536
- _initPromise3 = null;
36537
- }
36538
- }
36539
- function getConduitNativeDb() {
36540
- return _conduitNativeDb;
36679
+ return nativeDb;
36680
+ });
36541
36681
  }
36542
- function resetConduitDbState() {
36543
- _conduitNativeDb = null;
36544
- _conduitDbPath = null;
36545
- _initPromise3 = null;
36682
+ function getConduitNativeDb(projectRoot) {
36683
+ return boundProjectNative("conduit", projectRoot);
36546
36684
  }
36547
- var _require4, DatabaseSync2, _drizzle3, CONDUIT_SCHEMA_VERSION, _conduitNativeDb, _conduitDbPath, _initPromise3;
36685
+ var _require4, DatabaseSync2, _drizzle3, CONDUIT_SCHEMA_VERSION;
36548
36686
  var init_conduit_sqlite = __esm({
36549
36687
  "packages/core/src/store/conduit-sqlite.ts"() {
36550
36688
  "use strict";
36551
36689
  init_dual_scope_db();
36552
36690
  init_migration_manager();
36691
+ init_domain_binding();
36553
36692
  init_resolve_migrations_folder();
36554
36693
  init_sqlite_pragmas();
36555
36694
  _require4 = createRequire7(import.meta.url);
36556
36695
  ({ DatabaseSync: DatabaseSync2 } = _require4("node:sqlite"));
36557
36696
  _drizzle3 = null;
36558
36697
  CONDUIT_SCHEMA_VERSION = "2026.4.23";
36559
- _conduitNativeDb = null;
36560
- _conduitDbPath = null;
36561
- _initPromise3 = null;
36562
36698
  }
36563
36699
  });
36564
36700
 
@@ -36738,47 +36874,26 @@ function getDefaultSkillsDbPath() {
36738
36874
  return dbPath;
36739
36875
  }
36740
36876
  async function openSkillsDb(options) {
36741
- if (_skillsDb && !options?.path) return _skillsDb;
36742
- const requestedPath = options?.path ?? getDefaultSkillsDbPath();
36743
- if (_skillsDb && _skillsDbPath !== requestedPath) {
36744
- resetSkillsDbState();
36745
- }
36746
- if (_skillsDb && (_skillsNativeDb === null || !_skillsNativeDb.isOpen)) {
36747
- resetSkillsDbState();
36748
- }
36749
- if (_skillsDb) return _skillsDb;
36750
- if (_skillsInitPromise) return _skillsInitPromise;
36751
- _skillsInitPromise = (async () => {
36752
- _skillsDbPath = requestedPath;
36753
- const dualHandle = options?.path ? await openDualScopeDbAtPath("global", options.path) : await openDualScopeDb("global");
36754
- const nativeDb = dualHandle.db.$client ?? null;
36755
- if (!nativeDb) {
36756
- throw new Error(
36757
- "E6-L5: openDualScopeDb returned a handle without $client \u2014 cannot extract DatabaseSync for the skills-schema wrapping."
36758
- );
36759
- }
36760
- _skillsNativeDb = nativeDb;
36761
- const db = drizzle({ client: nativeDb });
36762
- _skillsDb = db;
36763
- return db;
36764
- })();
36765
- try {
36766
- return await _skillsInitPromise;
36767
- } finally {
36768
- _skillsInitPromise = null;
36877
+ return (await bindSkillsDomain(options)).db;
36878
+ }
36879
+ function resolveSkillsBindPath(options) {
36880
+ if (options?.path) {
36881
+ _skillsOverridePath = options.path;
36882
+ return { path: options.path, canonical: false };
36769
36883
  }
36884
+ if (_skillsOverridePath) return { path: _skillsOverridePath, canonical: false };
36885
+ return { path: getDefaultSkillsDbPath(), canonical: true };
36770
36886
  }
36771
- function resetSkillsDbState() {
36772
- _skillsNativeDb = null;
36773
- _skillsDb = null;
36774
- _skillsDbPath = null;
36775
- _skillsInitPromise = null;
36776
- _resetDualScopeDbCache("global");
36887
+ async function bindSkillsDomain(options) {
36888
+ const { path: path2, canonical } = resolveSkillsBindPath(options);
36889
+ const establish = (nativeDb) => drizzle({ client: nativeDb });
36890
+ return canonical ? bindGlobalDomain("skills", establish) : bindGlobalDomainAtPath("skills", path2, establish);
36777
36891
  }
36778
36892
  function getSkillsNativeDb() {
36779
- return _skillsNativeDb;
36893
+ const bound = _skillsOverridePath ? peekDomainAtPath("global", _skillsOverridePath, "skills") : peekGlobalDomain("skills");
36894
+ return bound?.native ?? null;
36780
36895
  }
36781
- var _skillsDb, _skillsNativeDb, _skillsDbPath, _skillsInitPromise;
36896
+ var _skillsOverridePath;
36782
36897
  var init_skills_db = __esm({
36783
36898
  "packages/core/src/store/skills-db.ts"() {
36784
36899
  "use strict";
@@ -36786,12 +36901,10 @@ var init_skills_db = __esm({
36786
36901
  init_paths();
36787
36902
  init_skill_provenance();
36788
36903
  init_dual_scope_db();
36904
+ init_domain_binding();
36789
36905
  init_skills_schema();
36790
36906
  init_skills_schema();
36791
- _skillsDb = null;
36792
- _skillsNativeDb = null;
36793
- _skillsDbPath = null;
36794
- _skillsInitPromise = null;
36907
+ _skillsOverridePath = null;
36795
36908
  }
36796
36909
  });
36797
36910
 
@@ -36820,11 +36933,11 @@ function resolveInventoryPath(entry, cwd) {
36820
36933
  }
36821
36934
  async function openBrainDbForSnapshot(cwd) {
36822
36935
  await getBrainDb(cwd);
36823
- return getBrainNativeDb();
36936
+ return getBrainNativeDb(cwd);
36824
36937
  }
36825
36938
  async function openTasksDbForSnapshot(cwd) {
36826
36939
  await getDb(cwd);
36827
- return getNativeDb();
36940
+ return getNativeDb(cwd);
36828
36941
  }
36829
36942
  async function openConduitDbForSnapshot(cwd) {
36830
36943
  await ensureConduitDb(resolveOrCwd(cwd));
@@ -37067,7 +37180,7 @@ __export(sqlite_native_exports, {
37067
37180
  import { realpathSync } from "node:fs";
37068
37181
  import { createRequire as createRequire8 } from "node:module";
37069
37182
  import { tmpdir } from "node:os";
37070
- import { delimiter, dirname as dirname9, isAbsolute, join as join22, resolve as resolve5, sep } from "node:path";
37183
+ import { delimiter, dirname as dirname9, isAbsolute, join as join22, resolve as resolve6, sep } from "node:path";
37071
37184
  function getDbSyncConstructor() {
37072
37185
  if (_ctor === null) {
37073
37186
  const mod = _require5("node:sqlite");
@@ -37076,7 +37189,7 @@ function getDbSyncConstructor() {
37076
37189
  return _ctor;
37077
37190
  }
37078
37191
  function resolveAbsoluteSafe(p) {
37079
- const abs = isAbsolute(p) ? p : resolve5(p);
37192
+ const abs = isAbsolute(p) ? p : resolve6(p);
37080
37193
  try {
37081
37194
  return realpathSync(abs);
37082
37195
  } catch {
@@ -37180,6 +37293,7 @@ var sqlite_exports = {};
37180
37293
  __export(sqlite_exports, {
37181
37294
  SQLITE_SCHEMA_VERSION: () => SQLITE_SCHEMA_VERSION,
37182
37295
  autoRecoverFromBackup: () => autoRecoverFromBackup,
37296
+ bindTasksDomain: () => bindTasksDomain,
37183
37297
  closeAllDatabases: () => closeAllDatabases,
37184
37298
  closeDb: () => closeDb,
37185
37299
  dbExists: () => dbExists,
@@ -37237,13 +37351,14 @@ function countBackupTasks(backupDb) {
37237
37351
  async function autoRecoverFromBackup(nativeDb, dbPath, cwd) {
37238
37352
  const { openNativeDatabase: openNativeDatabase2 } = await Promise.resolve().then(() => (init_sqlite_native(), sqlite_native_exports));
37239
37353
  const log12 = getLogger("sqlite");
37354
+ let restored = false;
37240
37355
  try {
37241
37356
  const countResult = nativeDb.prepare("SELECT COUNT(*) as cnt FROM tasks_tasks").get();
37242
37357
  const taskCount = countResult?.cnt ?? 0;
37243
- if (taskCount > 0) return;
37358
+ if (taskCount > 0) return false;
37244
37359
  const backups = listSqliteBackups(cwd);
37245
37360
  if (backups.length === 0) {
37246
- return;
37361
+ return false;
37247
37362
  }
37248
37363
  const newestBackup = backups[0];
37249
37364
  const backupDb = openNativeDatabase2(newestBackup.path, { readonly: true, enableWal: false });
@@ -37254,7 +37369,7 @@ async function autoRecoverFromBackup(nativeDb, dbPath, cwd) {
37254
37369
  backupDb.close();
37255
37370
  }
37256
37371
  if (backupTaskCount < MIN_BACKUP_TASK_COUNT) {
37257
- return;
37372
+ return false;
37258
37373
  }
37259
37374
  const lockPath2 = dbPath + FIRST_OPEN_LOCK_SUFFIX;
37260
37375
  await withLock(
@@ -37293,11 +37408,7 @@ async function autoRecoverFromBackup(nativeDb, dbPath, cwd) {
37293
37408
  "Database auto-recovered from backup successfully."
37294
37409
  );
37295
37410
  _resetDualScopeDbCache();
37296
- const restoredNativeDb = openNativeDatabase2(dbPath);
37297
- _nativeDb3 = restoredNativeDb;
37298
- const restoredDb = _getDrizzle3()({ client: restoredNativeDb });
37299
- runMigrations(restoredNativeDb, restoredDb);
37300
- _db3 = restoredDb;
37411
+ restored = true;
37301
37412
  },
37302
37413
  // Allow a generous stale window + retries: an exodus migration holding this
37303
37414
  // lock can take a while on a large fleet (matches on-open.ts), so a blocked
@@ -37307,64 +37418,46 @@ async function autoRecoverFromBackup(nativeDb, dbPath, cwd) {
37307
37418
  } catch (err) {
37308
37419
  log12.error({ err, dbPath }, "Auto-recovery from backup failed. Continuing with empty database.");
37309
37420
  }
37421
+ return restored;
37310
37422
  }
37311
37423
  async function getDb(cwd) {
37424
+ return (await bindTasksDomain(cwd)).db;
37425
+ }
37426
+ async function bindTasksDomain(cwd) {
37312
37427
  assertDbPathIsNotWorktreeResident("tasks", cwd);
37313
- const requestedPath = resolveDualScopeDbPath("project", cwd);
37314
- if (_db3 && _dbPath3 !== requestedPath) {
37315
- resetDbState();
37316
- }
37317
- if (_db3 && (_nativeDb3 === null || !_nativeDb3.isOpen)) {
37318
- resetDbState();
37319
- }
37320
- if (_db3) return _db3;
37321
- if (_initPromise4) return _initPromise4;
37322
- _initPromise4 = (async () => {
37323
- let nativeDb = null;
37324
- for (let attempt = 0; attempt < 2; attempt += 1) {
37325
- const dualHandle = await openDualScopeDb("project", cwd);
37326
- nativeDb = dualHandle.db.$client ?? null;
37327
- if (!nativeDb) {
37328
- throw new Error(
37329
- "E6-L1: openDualScopeDb returned a handle without $client \u2014 cannot extract DatabaseSync for legacy tasks-schema wrapping."
37330
- );
37331
- }
37332
- if (nativeDb.isOpen) break;
37333
- dualHandle.close();
37334
- }
37335
- if (!nativeDb?.isOpen) {
37336
- throw new Error("E6-L1: openDualScopeDb repeatedly returned a closed DatabaseSync handle.");
37337
- }
37338
- _nativeDb3 = nativeDb;
37339
- _dbPath3 = requestedPath;
37340
- const db = _getDrizzle3()({ client: nativeDb });
37341
- runMigrations(nativeDb, db);
37342
- if (process.env.VITEST) {
37343
- nativeDb.exec("PRAGMA foreign_keys=OFF");
37344
- }
37345
- nativeDb.exec(
37346
- `INSERT OR IGNORE INTO schema_meta (key, value) VALUES ('schemaVersion', '${SCHEMA_VERSION}')`
37347
- );
37348
- nativeDb.exec(
37349
- `INSERT OR IGNORE INTO schema_meta (key, value) VALUES ('task_id_sequence', '{"counter":0,"lastId":"T000","checksum":"seed"}')`
37350
- );
37351
- await autoRecoverFromBackup(nativeDb, requestedPath, cwd);
37352
- _db3 = db;
37353
- return db;
37354
- })();
37355
- try {
37356
- return await _initPromise4;
37357
- } finally {
37358
- _initPromise4 = null;
37428
+ let coldBind = false;
37429
+ const binding = await bindProjectDomain("tasks", cwd, (nativeDb, store) => {
37430
+ coldBind = true;
37431
+ return establishTasksSchema(nativeDb, store);
37432
+ });
37433
+ if (!coldBind) return binding;
37434
+ if (await autoRecoverFromBackup(binding.native, binding.store.dbPath, cwd)) {
37435
+ releaseDomainBindings({ scope: "project", dbPath: binding.store.dbPath });
37436
+ return bindProjectDomain("tasks", cwd, establishTasksSchema);
37359
37437
  }
37438
+ return binding;
37439
+ }
37440
+ function establishTasksSchema(nativeDb, store) {
37441
+ const db = _getDrizzle3()({ client: nativeDb });
37442
+ runMigrations(nativeDb, db, store.dbPath);
37443
+ if (process.env.VITEST) {
37444
+ nativeDb.exec("PRAGMA foreign_keys=OFF");
37445
+ }
37446
+ nativeDb.exec(
37447
+ `INSERT OR IGNORE INTO schema_meta (key, value) VALUES ('schemaVersion', '${SCHEMA_VERSION}')`
37448
+ );
37449
+ nativeDb.exec(
37450
+ `INSERT OR IGNORE INTO schema_meta (key, value) VALUES ('task_id_sequence', '{"counter":0,"lastId":"T000","checksum":"seed"}')`
37451
+ );
37452
+ return db;
37360
37453
  }
37361
37454
  function resolveMigrationsFolder() {
37362
37455
  return resolveCorePackageMigrationsFolder("drizzle-tasks");
37363
37456
  }
37364
- function runMigrations(nativeDb, db) {
37457
+ function runMigrations(nativeDb, db, dbPath) {
37365
37458
  const migrationsFolder = resolveMigrationsFolder();
37366
- if (tableExists(nativeDb, "tasks") && _dbPath3) {
37367
- createSafetyBackup(_dbPath3);
37459
+ if (tableExists(nativeDb, "tasks")) {
37460
+ createSafetyBackup(dbPath);
37368
37461
  }
37369
37462
  reconcileJournal(
37370
37463
  nativeDb,
@@ -37388,33 +37481,11 @@ function runMigrations(nativeDb, db) {
37388
37481
  }
37389
37482
  function closeDb() {
37390
37483
  _resetDualScopeDbCache("project");
37391
- if (_nativeDb3) {
37392
- try {
37393
- if (_nativeDb3.isOpen) {
37394
- _nativeDb3.close();
37395
- }
37396
- } catch {
37397
- }
37398
- _nativeDb3 = null;
37399
- }
37400
- _db3 = null;
37401
- _dbPath3 = null;
37402
- _initPromise4 = null;
37484
+ releaseDomainBindings({ scope: "project" });
37403
37485
  }
37404
37486
  function resetDbState() {
37405
37487
  _resetDualScopeDbCache("project");
37406
- if (_nativeDb3) {
37407
- try {
37408
- if (_nativeDb3.isOpen) {
37409
- _nativeDb3.close();
37410
- }
37411
- } catch {
37412
- }
37413
- _nativeDb3 = null;
37414
- }
37415
- _db3 = null;
37416
- _dbPath3 = null;
37417
- _initPromise4 = null;
37488
+ releaseDomainBindings({ scope: "project" });
37418
37489
  }
37419
37490
  async function getSchemaVersion(cwd) {
37420
37491
  const db = await getDb(cwd);
@@ -37424,11 +37495,11 @@ async function getSchemaVersion(cwd) {
37424
37495
  function dbExists(cwd) {
37425
37496
  return existsSync22(resolveDualScopeDbPath("project", cwd));
37426
37497
  }
37427
- function getNativeDb() {
37428
- return _nativeDb3;
37498
+ function getNativeDb(cwd) {
37499
+ return boundProjectNative("tasks", cwd);
37429
37500
  }
37430
- function getNativeTasksDb() {
37431
- return _nativeDb3;
37501
+ function getNativeTasksDb(cwd) {
37502
+ return boundProjectNative("tasks", cwd);
37432
37503
  }
37433
37504
  async function closeAllDatabases() {
37434
37505
  closeDb();
@@ -37442,8 +37513,9 @@ async function closeAllDatabases() {
37442
37513
  closeNexusDb2();
37443
37514
  } catch {
37444
37515
  }
37516
+ resetCleoRuntime();
37445
37517
  }
37446
- var _drizzle4, SQLITE_SCHEMA_VERSION, SCHEMA_VERSION, _db3, _nativeDb3, _dbPath3, _initPromise4, MIN_BACKUP_TASK_COUNT, FIRST_OPEN_LOCK_SUFFIX, REQUIRED_TASK_COLUMNS, REQUIRED_SESSION_COLUMNS;
37518
+ var _drizzle4, SQLITE_SCHEMA_VERSION, SCHEMA_VERSION, MIN_BACKUP_TASK_COUNT, FIRST_OPEN_LOCK_SUFFIX, REQUIRED_TASK_COLUMNS, REQUIRED_SESSION_COLUMNS;
37447
37519
  var init_sqlite3 = __esm({
37448
37520
  "packages/core/src/store/sqlite.ts"() {
37449
37521
  "use strict";
@@ -37451,6 +37523,7 @@ var init_sqlite3 = __esm({
37451
37523
  init_dual_scope_db();
37452
37524
  init_lock();
37453
37525
  init_migration_manager();
37526
+ init_domain_binding();
37454
37527
  init_resolve_migrations_folder();
37455
37528
  init_sqlite_backup();
37456
37529
  init_worktree_isolation_guard();
@@ -37460,10 +37533,6 @@ var init_sqlite3 = __esm({
37460
37533
  _drizzle4 = null;
37461
37534
  SQLITE_SCHEMA_VERSION = "2.0.0";
37462
37535
  SCHEMA_VERSION = SQLITE_SCHEMA_VERSION;
37463
- _db3 = null;
37464
- _nativeDb3 = null;
37465
- _dbPath3 = null;
37466
- _initPromise4 = null;
37467
37536
  MIN_BACKUP_TASK_COUNT = 10;
37468
37537
  FIRST_OPEN_LOCK_SUFFIX = ".exodus-on-open.lock";
37469
37538
  REQUIRED_TASK_COLUMNS = [
@@ -38403,7 +38472,7 @@ async function createSqliteDataAccessor(cwd) {
38403
38472
  },
38404
38473
  async transaction(fn) {
38405
38474
  const db = await getDb(cwd);
38406
- const nativeDb = getNativeTasksDb();
38475
+ const nativeDb = getNativeTasksDb(cwd);
38407
38476
  if (!nativeDb) {
38408
38477
  throw new Error("Native database not initialized");
38409
38478
  }
@@ -38846,10 +38915,10 @@ var init_sequence = __esm({
38846
38915
  import { execFile as execFile2 } from "node:child_process";
38847
38916
  import { existsSync as existsSync24 } from "node:fs";
38848
38917
  import { readFile as readFile4, writeFile } from "node:fs/promises";
38849
- import { join as join24, resolve as resolve6 } from "node:path";
38918
+ import { join as join24, resolve as resolve7 } from "node:path";
38850
38919
  import { promisify as promisify2 } from "node:util";
38851
38920
  function makeCleoGitEnv(cleoDir) {
38852
- const abs = resolve6(cleoDir);
38921
+ const abs = resolve7(cleoDir);
38853
38922
  return {
38854
38923
  ...process.env,
38855
38924
  GIT_DIR: join24(abs, ".git"),
@@ -38859,7 +38928,7 @@ function makeCleoGitEnv(cleoDir) {
38859
38928
  async function cleoGitCommand(args, cleoDir) {
38860
38929
  try {
38861
38930
  const result = await execFileAsync2("git", args, {
38862
- cwd: resolve6(cleoDir),
38931
+ cwd: resolve7(cleoDir),
38863
38932
  // absolute cwd so relative paths in args resolve correctly
38864
38933
  env: makeCleoGitEnv(cleoDir),
38865
38934
  timeout: 1e4