@cortexkit/pi-magic-context 0.49.19 → 0.49.21

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 (60) hide show
  1. package/dist/auto-search-worker.js +48 -9
  2. package/dist/chunk-embedding-write-worker.js +29 -22
  3. package/dist/{compaction-marker-candidates-wjzqbspm.js → compaction-marker-candidates-p1kqkqhq.js} +3 -3
  4. package/dist/compaction-marker-candidates-worker.js +2 -2
  5. package/dist/compartment-chunk-embedding-y01m95c2.js +64 -0
  6. package/dist/embedding-backfill-2vh6m3bg.js +24 -0
  7. package/dist/embedding-worker-process.js +2 -2
  8. package/dist/embedding-worker.js +6 -6
  9. package/dist/historian-calibration-extension.js +1 -1
  10. package/dist/{index-e1rza8gp.js → index-29nw7n1r.js} +1 -1
  11. package/dist/{index-rrwv9y2k.js → index-2qtx0q6h.js} +214 -5
  12. package/dist/{index-msqrs4nf.js → index-3c3ztwgc.js} +5 -5
  13. package/dist/index-54wmg4rj.js +82 -0
  14. package/dist/{index-yygsh681.js → index-579m1751.js} +3 -3
  15. package/dist/{index-rc1dez8m.js → index-5j6vwg4z.js} +154 -13
  16. package/dist/{index-qpq6w67c.js → index-6wdwv3a6.js} +2 -2
  17. package/dist/{index-vktahpdj.js → index-at6py2k3.js} +9 -3
  18. package/dist/{index-bxmf4dhr.js → index-ccanzg8p.js} +2367 -1243
  19. package/dist/{index-sqt2haap.js → index-dbgg6hsf.js} +28 -14
  20. package/dist/{index-7bqrfyny.js → index-fgs78jyd.js} +12 -15
  21. package/dist/{index-58mj0dzc.js → index-ftvm2acc.js} +181 -46
  22. package/dist/{index-wveb51es.js → index-gqjxy2p7.js} +74 -9
  23. package/dist/{index-d8gc0112.js → index-gwxaz0z5.js} +4 -6
  24. package/dist/{index-d4dt19md.js → index-hqw0rt9k.js} +1 -1
  25. package/dist/{index-g7p75667.js → index-hxp6q5f8.js} +2 -2
  26. package/dist/{index-svgkv4g2.js → index-j3pz3a1b.js} +2 -2
  27. package/dist/{index-f18ec69t.js → index-jn44h81p.js} +1 -1
  28. package/dist/{index-1kcfwk9c.js → index-my0dn413.js} +1 -1
  29. package/dist/{index-apgwdby5.js → index-n4wyxcgr.js} +1 -1
  30. package/dist/{index-rhprywn6.js → index-n8e5e9fp.js} +28 -7
  31. package/dist/{index-z2rkv0mb.js → index-pe5gz5c4.js} +5 -5
  32. package/dist/{index-dtp1efvf.js → index-st4gwt64.js} +2 -2
  33. package/dist/{index-xnch62s6.js → index-vx6f7wff.js} +314 -127
  34. package/dist/{index-jrap43gc.js → index-w1avzkdt.js} +1 -1
  35. package/dist/{index-vmcgew7x.js → index-w1twp2kp.js} +78 -58
  36. package/dist/{index-ek9zvs4f.js → index-w6r3ggnp.js} +57 -27
  37. package/dist/{index-ym6nh2gk.js → index-wgmhk91m.js} +113 -20
  38. package/dist/{index-2mmy81qt.js → index-wqpjhx1p.js} +501 -20
  39. package/dist/index.js +1178 -417
  40. package/dist/{logger-dygt96rn.js → logger-shetp43v.js} +1 -1
  41. package/dist/{message-fts-rowid-map-cvpy2dmb.js → message-fts-rowid-map-59y476bk.js} +6 -5
  42. package/dist/{message-index-rc2x41k4.js → message-index-3snxf11x.js} +15 -14
  43. package/dist/{message-index-bounded-66saeq5j.js → message-index-bounded-4ngt600v.js} +15 -14
  44. package/dist/{message-time-backfill-ssshbdy8.js → message-time-backfill-e3r8ddh1.js} +3 -3
  45. package/dist/migration-worker.js +12 -11
  46. package/dist/{project-embedding-registry-vf1ncptn.js → project-embedding-registry-hfyzs654.js} +14 -13
  47. package/dist/raw-ordinal-warmup-worker.js +136 -101
  48. package/dist/{read-session-chunk-h5bzj91r.js → read-session-chunk-5dmc77q2.js} +10 -9
  49. package/dist/{read-session-raw-9s3xn7fc.js → read-session-raw-yxv2fv34.js} +4 -3
  50. package/dist/{safe-notification-target-2rkvkmw8.js → safe-notification-target-zcpchea6.js} +2 -2
  51. package/dist/search-archive-3daj5ttc.js +21 -0
  52. package/dist/{search-wh7b9zht.js → search-jhfh3tsj.js} +23 -22
  53. package/dist/{sqlite-fsp0sf3r.js → sqlite-v89bj0yy.js} +7 -1
  54. package/dist/{storage-memory-yxxc5v4n.js → storage-memory-0ndn65fm.js} +2 -2
  55. package/dist/{storage-tags-1kets0q8.js → storage-tags-r48vxthe.js} +7 -5
  56. package/dist/subagent-entry.js +28 -26
  57. package/dist/tag-snapshot-worker-pi.js +22 -14
  58. package/package.json +5 -2
  59. package/dist/embedding-backfill-vwag34t0.js +0 -23
  60. package/dist/search-archive-4817pqev.js +0 -20
@@ -1,3 +1,7 @@
1
+ import {
2
+ exitOnWorkerClose
3
+ } from "./index-54wmg4rj.js";
4
+
1
5
  // ../plugin/src/hooks/magic-context/auto-search-worker.ts
2
6
  import { parentPort, workerData } from "node:worker_threads";
3
7
  if (!parentPort)
@@ -14,20 +18,22 @@ var [
14
18
  { getMemoriesByProject },
15
19
  { installProjectEmbeddingSearchBridge },
16
20
  { searchMessageHistory, unifiedSearch },
21
+ { getLastCompartmentChunkPoolWarm, maintainCompartmentChunkSearchPools },
17
22
  { setEmbeddingSessionBusy },
18
23
  { getHarness, setHarness },
19
24
  { Database, registerSqliteDiagnosticSink },
20
25
  { log }
21
26
  ] = await Promise.all([
22
- import("./embedding-backfill-vwag34t0.js"),
27
+ import("./embedding-backfill-2vh6m3bg.js"),
23
28
  import("./embedding-cache-7rp4gqzr.js"),
24
- import("./storage-memory-yxxc5v4n.js"),
25
- import("./project-embedding-registry-vf1ncptn.js"),
26
- import("./search-wh7b9zht.js"),
29
+ import("./storage-memory-0ndn65fm.js"),
30
+ import("./project-embedding-registry-hfyzs654.js"),
31
+ import("./search-jhfh3tsj.js"),
32
+ import("./compartment-chunk-embedding-y01m95c2.js"),
27
33
  import("./embedding-activity-ew6dshfd.js"),
28
34
  import("./harness-9872e0ym.js"),
29
- import("./sqlite-fsp0sf3r.js"),
30
- import("./logger-dygt96rn.js")
35
+ import("./sqlite-v89bj0yy.js"),
36
+ import("./logger-shetp43v.js")
31
37
  ]);
32
38
  setHarness(input.harness);
33
39
  registerSqliteDiagnosticSink(log, getHarness);
@@ -53,6 +59,27 @@ function request(message) {
53
59
  }
54
60
  db = new Database(input.path, input.job === "backfill" ? undefined : { readonly: true });
55
61
  db.exec("PRAGMA busy_timeout = 250");
62
+ var { closePort } = exitOnWorkerClose(port, () => db.close());
63
+ var CHUNK_POOL_MAINTENANCE_INTERVAL_MS = 5000;
64
+ var maintenanceTimer;
65
+ var maintaining = false;
66
+ function maintainChunkPools() {
67
+ if (maintaining)
68
+ return;
69
+ maintaining = true;
70
+ maintainCompartmentChunkSearchPools(db).catch(() => {}).finally(() => {
71
+ maintaining = false;
72
+ });
73
+ }
74
+ function scheduleChunkPoolMaintenance() {
75
+ if (!persistent)
76
+ return;
77
+ if (!maintenanceTimer) {
78
+ maintenanceTimer = setInterval(maintainChunkPools, CHUNK_POOL_MAINTENANCE_INTERVAL_MS);
79
+ maintenanceTimer.unref?.();
80
+ }
81
+ setTimeout(maintainChunkPools, 0);
82
+ }
56
83
  async function run(jobInput) {
57
84
  input = jobInput;
58
85
  queryDimensions = null;
@@ -98,15 +125,27 @@ async function run(jobInput) {
98
125
  const outcome = searchMessageHistory(db, input.messageRequest);
99
126
  port.postMessage({ kind: "messages", outcome });
100
127
  } else {
128
+ const jobStarted = performance.now();
129
+ const steps = {};
101
130
  const results = await unifiedSearch(db, input.sessionId, input.projectPath, input.query, {
102
131
  ...input.options,
103
132
  backfillMemoryEmbeddings: false,
104
133
  countRetrievals: false,
105
134
  measurementDisabled: true,
106
135
  embedQuery: async (text) => (await request({ kind: "query", text })).result ?? null,
107
- isEmbeddingRuntimeEnabled: () => input.embeddingRuntimeEnabled
136
+ isEmbeddingRuntimeEnabled: () => input.embeddingRuntimeEnabled,
137
+ stepTimings: steps
138
+ });
139
+ port.postMessage({
140
+ kind: "result",
141
+ results,
142
+ timings: {
143
+ steps,
144
+ jobMs: performance.now() - jobStarted,
145
+ lastPoolWarm: getLastCompartmentChunkPoolWarm()
146
+ }
108
147
  });
109
- port.postMessage({ kind: "result", results });
148
+ scheduleChunkPoolMaintenance();
110
149
  }
111
150
  } catch (error) {
112
151
  port.postMessage({
@@ -117,7 +156,7 @@ async function run(jobInput) {
117
156
  setEmbeddingSessionBusy(input.sessionId, false);
118
157
  if (!persistent) {
119
158
  db.close();
120
- port?.close();
159
+ closePort();
121
160
  }
122
161
  }
123
162
  }
@@ -3,39 +3,43 @@ import {
3
3
  } from "./index-0h2mkab2.js";
4
4
  import {
5
5
  Database2
6
- } from "./index-rrwv9y2k.js";
6
+ } from "./index-2qtx0q6h.js";
7
7
  import {
8
8
  configureContextDatabasePragmas2
9
9
  } from "./index-fapenpch.js";
10
- import"./index-1kcfwk9c.js";
11
- import"./index-7bqrfyny.js";
12
- import"./index-d8gc0112.js";
10
+ import {
11
+ isWorkerCloseMessage,
12
+ exitOnWorkerClose
13
+ } from "./index-54wmg4rj.js";
14
+ import"./index-my0dn413.js";
15
+ import"./index-fgs78jyd.js";
16
+ import"./index-gwxaz0z5.js";
13
17
  import"./index-399p688g.js";
14
- import"./index-vktahpdj.js";
15
- import"./index-yygsh681.js";
18
+ import"./index-at6py2k3.js";
19
+ import"./index-579m1751.js";
16
20
  import {
17
- replaceCompartmentChunkEmbeddings,
18
- recordChunkEmbedBackoff,
19
- loadUnembeddedSessionChunkCandidates,
20
- countUnembeddedSessionCompartments
21
- } from "./index-xnch62s6.js";
21
+ replaceCompartmentChunkEmbeddings2,
22
+ recordChunkEmbedBackoff2,
23
+ loadUnembeddedSessionChunkCandidates2,
24
+ countUnembeddedSessionCompartments2
25
+ } from "./index-vx6f7wff.js";
22
26
  import {
23
27
  recordSessionProjectIdentity,
24
28
  acquireGitSweepLease,
25
29
  renewGitSweepLease,
26
30
  releaseGitSweepLease
27
- } from "./index-svgkv4g2.js";
28
- import"./index-g7p75667.js";
29
- import"./index-apgwdby5.js";
31
+ } from "./index-j3pz3a1b.js";
32
+ import"./index-hxp6q5f8.js";
33
+ import"./index-n4wyxcgr.js";
30
34
  import {
31
35
  LATEST_SUPPORTED_VERSION,
32
36
  enforceSchemaFence
33
- } from "./index-sqt2haap.js";
34
- import"./index-msqrs4nf.js";
35
- import"./index-jrap43gc.js";
37
+ } from "./index-dbgg6hsf.js";
38
+ import"./index-3c3ztwgc.js";
39
+ import"./index-w1avzkdt.js";
36
40
  import"./index-135e8hvg.js";
37
41
  import"./index-8976mezv.js";
38
- import"./index-d4dt19md.js";
42
+ import"./index-hqw0rt9k.js";
39
43
 
40
44
  // ../plugin/src/features/magic-context/chunk-embedding-write-worker.ts
41
45
  import { parentPort, workerData } from "node:worker_threads";
@@ -51,24 +55,27 @@ if (!enforceSchemaFence(db, workerData.path, LATEST_SUPPORTED_VERSION)) {
51
55
  var timeout = Math.max(0, Math.floor(workerData.timeout));
52
56
  db.exec(`PRAGMA busy_timeout=${timeout}; PRAGMA foreign_keys=ON`);
53
57
  configureContextDatabasePragmas2(db);
58
+ exitOnWorkerClose(port, () => db.close());
54
59
  port.on("message", (input) => {
60
+ if (isWorkerCloseMessage(input))
61
+ return;
55
62
  try {
56
63
  let value;
57
64
  switch (input.kind) {
58
65
  case "replace":
59
- replaceCompartmentChunkEmbeddings(db, input.rows, input.sourceKey);
66
+ replaceCompartmentChunkEmbeddings2(db, input.rows, input.sourceKey);
60
67
  break;
61
68
  case "project":
62
69
  recordSessionProjectIdentity(db, input.session, input.project);
63
70
  break;
64
71
  case "count":
65
- value = countUnembeddedSessionCompartments(db, input.project, input.session, input.model, input.tokens);
72
+ value = countUnembeddedSessionCompartments2(db, input.project, input.session, input.model, input.tokens);
66
73
  break;
67
74
  case "candidates":
68
- value = loadUnembeddedSessionChunkCandidates(db, input.project, input.session, input.model, input.limit, input.excluded, input.tokens, true);
75
+ value = loadUnembeddedSessionChunkCandidates2(db, input.project, input.session, input.model, input.limit, input.excluded, input.tokens, true);
69
76
  break;
70
77
  case "backoff":
71
- recordChunkEmbedBackoff(db, input.candidate, input.project, input.model);
78
+ recordChunkEmbedBackoff2(db, input.candidate, input.project, input.model);
72
79
  break;
73
80
  case "acquire":
74
81
  value = acquireGitSweepLease(db, input.project, input.holder, {
@@ -4,10 +4,10 @@ import {
4
4
  readMarkerCandidateBasis2,
5
5
  prepareMarkerCandidateBasis2,
6
6
  scanMarkerCandidateSnapshot2
7
- } from "./index-qpq6w67c.js";
7
+ } from "./index-6wdwv3a6.js";
8
8
  import"./index-399p688g.js";
9
- import"./index-rrwv9y2k.js";
10
- import"./index-7bqrfyny.js";
9
+ import"./index-2qtx0q6h.js";
10
+ import"./index-fgs78jyd.js";
11
11
  import"./index-0h2mkab2.js";
12
12
  export {
13
13
  invalidateMarkerCandidateBasis2 as invalidateMarkerCandidateBasis,
@@ -5,8 +5,8 @@ if (!port)
5
5
  throw new Error("marker candidate worker requires a parent port");
6
6
  port.postMessage({ kind: "accepted" });
7
7
  var [{ Database }, { scanMarkerCandidateSnapshot }] = await Promise.all([
8
- import("./sqlite-fsp0sf3r.js"),
9
- import("./compaction-marker-candidates-wjzqbspm.js")
8
+ import("./sqlite-v89bj0yy.js"),
9
+ import("./compaction-marker-candidates-p1kqkqhq.js")
10
10
  ]);
11
11
  var input = workerData;
12
12
  var db;
@@ -0,0 +1,64 @@
1
+ import {
2
+ DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS2,
3
+ CHUNK_WINDOW_SAFETY_RATIO2,
4
+ normalizeCompartmentChunkMaxInputTokens2,
5
+ buildCanonicalChunkTextFromFts2,
6
+ buildCompartmentSummaryFallbackText2,
7
+ canonicalizeInMemoryChunkTextForEmbedding2,
8
+ chunkCanonicalText2,
9
+ chunkEmbeddingWindowsAreCurrent2,
10
+ replaceCompartmentChunkEmbeddings2,
11
+ loadCompartmentChunkEmbeddingsForSearch2,
12
+ getLastCompartmentChunkPoolWarm2,
13
+ peekCompartmentChunkSearchPool2,
14
+ warmCompartmentChunkSearchPool2,
15
+ maintainCompartmentChunkSearchPools2,
16
+ loadUnembeddedShadowChunkCandidates2,
17
+ loadUnembeddedCompartmentChunkCandidatesPolite2,
18
+ deleteChunkEmbedBackoffForSession2,
19
+ recordChunkEmbedBackoff2,
20
+ chunkWindowSourceKey2,
21
+ loadUnembeddedSessionChunkCandidates2,
22
+ loadUnembeddedSessionChunkCandidatesPolite2,
23
+ countUnembeddedSessionCompartments2,
24
+ countSessionCompartmentEmbedCoverage2,
25
+ countSessionCompartmentEmbedCoveragePolite2,
26
+ countUnembeddedSessionCompartmentsPolite2
27
+ } from "./index-vx6f7wff.js";
28
+ import"./index-gwxaz0z5.js";
29
+ import"./index-579m1751.js";
30
+ import"./index-at6py2k3.js";
31
+ import"./index-399p688g.js";
32
+ import"./index-hqw0rt9k.js";
33
+ import"./index-my0dn413.js";
34
+ import"./index-2qtx0q6h.js";
35
+ import"./index-fgs78jyd.js";
36
+ import"./index-0h2mkab2.js";
37
+ import"./index-54wmg4rj.js";
38
+ export {
39
+ CHUNK_WINDOW_SAFETY_RATIO2 as CHUNK_WINDOW_SAFETY_RATIO,
40
+ DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS2 as DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS,
41
+ buildCanonicalChunkTextFromFts2 as buildCanonicalChunkTextFromFts,
42
+ buildCompartmentSummaryFallbackText2 as buildCompartmentSummaryFallbackText,
43
+ canonicalizeInMemoryChunkTextForEmbedding2 as canonicalizeInMemoryChunkTextForEmbedding,
44
+ chunkCanonicalText2 as chunkCanonicalText,
45
+ chunkEmbeddingWindowsAreCurrent2 as chunkEmbeddingWindowsAreCurrent,
46
+ chunkWindowSourceKey2 as chunkWindowSourceKey,
47
+ countSessionCompartmentEmbedCoverage2 as countSessionCompartmentEmbedCoverage,
48
+ countSessionCompartmentEmbedCoveragePolite2 as countSessionCompartmentEmbedCoveragePolite,
49
+ countUnembeddedSessionCompartments2 as countUnembeddedSessionCompartments,
50
+ countUnembeddedSessionCompartmentsPolite2 as countUnembeddedSessionCompartmentsPolite,
51
+ deleteChunkEmbedBackoffForSession2 as deleteChunkEmbedBackoffForSession,
52
+ getLastCompartmentChunkPoolWarm2 as getLastCompartmentChunkPoolWarm,
53
+ loadCompartmentChunkEmbeddingsForSearch2 as loadCompartmentChunkEmbeddingsForSearch,
54
+ loadUnembeddedCompartmentChunkCandidatesPolite2 as loadUnembeddedCompartmentChunkCandidatesPolite,
55
+ loadUnembeddedSessionChunkCandidates2 as loadUnembeddedSessionChunkCandidates,
56
+ loadUnembeddedSessionChunkCandidatesPolite2 as loadUnembeddedSessionChunkCandidatesPolite,
57
+ loadUnembeddedShadowChunkCandidates2 as loadUnembeddedShadowChunkCandidates,
58
+ maintainCompartmentChunkSearchPools2 as maintainCompartmentChunkSearchPools,
59
+ normalizeCompartmentChunkMaxInputTokens2 as normalizeCompartmentChunkMaxInputTokens,
60
+ peekCompartmentChunkSearchPool2 as peekCompartmentChunkSearchPool,
61
+ recordChunkEmbedBackoff2 as recordChunkEmbedBackoff,
62
+ replaceCompartmentChunkEmbeddings2 as replaceCompartmentChunkEmbeddings,
63
+ warmCompartmentChunkSearchPool2 as warmCompartmentChunkSearchPool
64
+ };
@@ -0,0 +1,24 @@
1
+ import {
2
+ ensureMemoryEmbeddings2
3
+ } from "./index-pe5gz5c4.js";
4
+ import"./index-w1twp2kp.js";
5
+ import"./index-9p40dgkm.js";
6
+ import"./index-yhzhdp3m.js";
7
+ import"./index-j3pz3a1b.js";
8
+ import"./index-vx6f7wff.js";
9
+ import"./index-wqpjhx1p.js";
10
+ import"./index-gwxaz0z5.js";
11
+ import"./index-ccanzg8p.js";
12
+ import"./index-hxp6q5f8.js";
13
+ import"./index-579m1751.js";
14
+ import"./index-at6py2k3.js";
15
+ import"./index-399p688g.js";
16
+ import"./index-hqw0rt9k.js";
17
+ import"./index-my0dn413.js";
18
+ import"./index-2qtx0q6h.js";
19
+ import"./index-fgs78jyd.js";
20
+ import"./index-0h2mkab2.js";
21
+ import"./index-54wmg4rj.js";
22
+ export {
23
+ ensureMemoryEmbeddings2 as ensureMemoryEmbeddings
24
+ };
@@ -1,10 +1,10 @@
1
1
  import"./index-0h2mkab2.js";
2
2
  import {
3
3
  log2
4
- } from "./index-7bqrfyny.js";
4
+ } from "./index-fgs78jyd.js";
5
5
  import {
6
6
  inRootAsyncContext2
7
- } from "./index-rrwv9y2k.js";
7
+ } from "./index-2qtx0q6h.js";
8
8
 
9
9
  // ../plugin/src/features/magic-context/memory/embedding-worker-process.ts
10
10
  import { Worker } from "node:worker_threads";
@@ -3,16 +3,16 @@ import {
3
3
  } from "./index-0h2mkab2.js";
4
4
  import {
5
5
  log2
6
- } from "./index-7bqrfyny.js";
7
- import"./index-bxmf4dhr.js";
8
- import"./index-d8gc0112.js";
9
- import"./index-1kcfwk9c.js";
10
- import"./index-rrwv9y2k.js";
6
+ } from "./index-fgs78jyd.js";
7
+ import"./index-ccanzg8p.js";
8
+ import"./index-gwxaz0z5.js";
9
+ import"./index-my0dn413.js";
10
+ import"./index-2qtx0q6h.js";
11
11
  import {
12
12
  resolveLocalEmbeddingWorkerRuntime,
13
13
  getLocalEmbeddingNativeMemoryStats,
14
14
  LocalEmbeddingProvider
15
- } from "./index-2mmy81qt.js";
15
+ } from "./index-wqpjhx1p.js";
16
16
  import {
17
17
  setStoragePrivatePermissionEnforcement
18
18
  } from "./index-81nthk8j.js";
@@ -10,7 +10,7 @@ import {
10
10
  } from "./index-fa485mqs.js";
11
11
  import"./index-4knyh1kt.js";
12
12
  import"./index-6x556c2q.js";
13
- import"./index-7bqrfyny.js";
13
+ import"./index-fgs78jyd.js";
14
14
  import"./index-0h2mkab2.js";
15
15
  export {
16
16
  HISTORIAN_CACHE_TAIL_ENV2 as HISTORIAN_CACHE_TAIL_ENV,
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  withPrivilegedWriter2
3
- } from "./index-rrwv9y2k.js";
3
+ } from "./index-2qtx0q6h.js";
4
4
  import {
5
5
  MEMORY_CATEGORY_ORDER_SQL
6
6
  } from "./index-135e8hvg.js";
@@ -174,6 +174,177 @@ function topLevelCommaSegments(text) {
174
174
  function getTagIdentityWriteGeneration2() {
175
175
  return tagIdentityWriteGeneration;
176
176
  }
177
+ var busyTimeoutStates = new WeakMap;
178
+ var BUSY_TIMEOUT_QUERY = /^\s*PRAGMA\s+(?:(?:\w+|"[^"]+"|`[^`]+`|\[[^\]]+\])\s*\.\s*)?(?:busy_timeout|"busy_timeout"|`busy_timeout`|\[busy_timeout\])\s*;?\s*$/i;
179
+ var BUSY_TIMEOUT_SETTER = /^\s*PRAGMA\s+(?:(?:\w+|"[^"]+"|`[^`]+`|\[[^\]]+\])\s*\.\s*)?(?:busy_timeout|"busy_timeout"|`busy_timeout`|\[busy_timeout\])\s*(?==|\()/i;
180
+ function literalBusyTimeout(sql) {
181
+ const head = BUSY_TIMEOUT_SETTER.exec(sql);
182
+ if (!head)
183
+ return;
184
+ const match = /^(?:=\s*|\(\s*)(?:'(-?\d+)'|"(-?\d+)"|(-?\d+))\s*\)?\s*;?\s*$/.exec(sql.slice(head[0].length));
185
+ if (!match)
186
+ return;
187
+ const value = Number(match[1] ?? match[2] ?? match[3]);
188
+ return value < 0 ? 0 : value <= 2147483647 ? value : undefined;
189
+ }
190
+ function busyTimeoutStatements(sql) {
191
+ const statements = [];
192
+ let statement = "";
193
+ let quote = "";
194
+ for (let i = 0;i < sql.length; i++) {
195
+ const char = sql[i];
196
+ if (quote) {
197
+ statement += char;
198
+ if (char === quote) {
199
+ if (sql[i + 1] === quote)
200
+ statement += sql[++i];
201
+ else
202
+ quote = "";
203
+ }
204
+ } else if (char === "'" || char === '"' || char === "`" || char === "[") {
205
+ quote = char === "[" ? "]" : char;
206
+ statement += char;
207
+ } else if (char === "-" && sql[i + 1] === "-") {
208
+ while (i + 1 < sql.length && sql[i + 1] !== `
209
+ `)
210
+ i++;
211
+ statement += " ";
212
+ } else if (char === "/" && sql[i + 1] === "*") {
213
+ i += 2;
214
+ while (i < sql.length && !(sql[i] === "*" && sql[i + 1] === "/"))
215
+ i++;
216
+ i++;
217
+ statement += " ";
218
+ } else if (char === ";") {
219
+ statements.push(statement);
220
+ statement = "";
221
+ } else
222
+ statement += char;
223
+ }
224
+ statements.push(statement);
225
+ return statements.filter((part) => part.trim().length > 0);
226
+ }
227
+ function installNativeDeadlineRouting(db) {
228
+ const rawExec = db.exec.bind(db);
229
+ const rawPrepare = db.prepare.bind(db);
230
+ const readTimeout = () => {
231
+ const statement = rawPrepare("PRAGMA busy_timeout");
232
+ try {
233
+ return Object.values(statement.get())[0];
234
+ } finally {
235
+ statement.finalize?.();
236
+ }
237
+ };
238
+ const timeoutState = { current: readTimeout() };
239
+ busyTimeoutStates.set(db, timeoutState);
240
+ const setBusyTimeout = (timeout) => {
241
+ rawExec(`PRAGMA busy_timeout=${timeout}`);
242
+ timeoutState.current = timeout;
243
+ };
244
+ const withinDeadline = (operation) => {
245
+ const deadline = nativeWaitScope?.getStore();
246
+ if (!deadline?.active || !deadline.remainingMs)
247
+ return operation();
248
+ const remaining = deadline.remainingMs();
249
+ if (remaining === Infinity)
250
+ return operation();
251
+ const previous = timeoutState.current;
252
+ const timeout = Math.min(previous, Math.max(0, Math.floor(remaining)));
253
+ if (timeout === previous)
254
+ return operation();
255
+ setBusyTimeout(timeout);
256
+ try {
257
+ return operation();
258
+ } finally {
259
+ setBusyTimeout(previous);
260
+ }
261
+ };
262
+ Object.defineProperty(db, "exec", {
263
+ configurable: true,
264
+ writable: true,
265
+ value: (sql) => {
266
+ const parts = /\bbusy_timeout\b/i.test(sql) ? busyTimeoutStatements(sql) : [];
267
+ if (parts.length === 1 && BUSY_TIMEOUT_QUERY.test(parts[0]))
268
+ return rawExec(sql);
269
+ const singleSetter = parts.length === 1 && BUSY_TIMEOUT_SETTER.test(parts[0]);
270
+ const setting = singleSetter ? literalBusyTimeout(parts[0]) : undefined;
271
+ if (singleSetter) {
272
+ const result = rawExec(sql);
273
+ timeoutState.current = setting ?? readTimeout();
274
+ return result;
275
+ }
276
+ if (!/\bbusy_timeout\b/i.test(sql))
277
+ return withinDeadline(() => rawExec(sql));
278
+ const setters = parts.filter((part) => BUSY_TIMEOUT_SETTER.test(part));
279
+ return withinDeadline(() => {
280
+ try {
281
+ const result = rawExec(sql);
282
+ if (setters.length)
283
+ timeoutState.current = literalBusyTimeout(setters[setters.length - 1]) ?? readTimeout();
284
+ return result;
285
+ } catch (error) {
286
+ if (setters.length)
287
+ timeoutState.current = readTimeout();
288
+ throw error;
289
+ }
290
+ });
291
+ }
292
+ });
293
+ const nativePrepare = (sql) => BUSY_TIMEOUT_QUERY.test(sql) || BUSY_TIMEOUT_SETTER.test(sql) ? rawPrepare(sql) : withinDeadline(() => rawPrepare(sql));
294
+ Object.defineProperty(db, "prepare", {
295
+ configurable: true,
296
+ writable: true,
297
+ value: (sql) => {
298
+ const statement = nativePrepare(sql);
299
+ const normalized = /\bbusy_timeout\b/i.test(sql) ? busyTimeoutStatements(sql)[0] ?? sql : sql;
300
+ const setting = literalBusyTimeout(normalized);
301
+ const timeoutSetter = BUSY_TIMEOUT_SETTER.test(normalized);
302
+ const timeoutQuery = BUSY_TIMEOUT_QUERY.test(normalized);
303
+ if (timeoutSetter)
304
+ timeoutState.current = setting ?? readTimeout();
305
+ for (const method of ["run", "get", "all", "values"]) {
306
+ const execute = statement[method]?.bind(statement);
307
+ if (!execute)
308
+ continue;
309
+ Object.defineProperty(statement, method, {
310
+ configurable: true,
311
+ value: (...args) => {
312
+ if (timeoutQuery)
313
+ return execute(...args);
314
+ if (!timeoutSetter)
315
+ return withinDeadline(() => execute(...args));
316
+ const result = execute(...args);
317
+ timeoutState.current = readTimeout();
318
+ return result;
319
+ }
320
+ });
321
+ }
322
+ if (typeof statement.iterate === "function") {
323
+ const iterate = statement.iterate.bind(statement);
324
+ Object.defineProperty(statement, "iterate", {
325
+ configurable: true,
326
+ value: (...args) => {
327
+ const iterator = !timeoutSetter && !timeoutQuery ? withinDeadline(() => iterate(...args)) : iterate(...args);
328
+ const next = iterator.next.bind(iterator);
329
+ if (timeoutSetter)
330
+ timeoutState.current = readTimeout();
331
+ iterator.next = () => {
332
+ if (timeoutQuery)
333
+ return next();
334
+ if (!timeoutSetter)
335
+ return withinDeadline(next);
336
+ const result = next();
337
+ timeoutState.current = readTimeout();
338
+ return result;
339
+ };
340
+ return iterator;
341
+ }
342
+ });
343
+ }
344
+ return statement;
345
+ }
346
+ });
347
+ }
177
348
  function installTransactionRouting(db, readonly) {
178
349
  const nativeExec = db.exec.bind(db);
179
350
  Object.defineProperty(db, "exec", {
@@ -311,7 +482,7 @@ function wrapRemainingTagWriteForms(statement) {
311
482
  });
312
483
  }
313
484
  }
314
- function trackSqliteConnection(db, filename, options) {
485
+ function trackSqliteConnection(db, filename, options, turnDeadlines = false) {
315
486
  const originalClose = db.close.bind(db);
316
487
  const sequence = nextSqliteConnectionSequence++;
317
488
  const metadata = {
@@ -319,6 +490,8 @@ function trackSqliteConnection(db, filename, options) {
319
490
  filename: typeof filename === "string" ? filename : Buffer.isBuffer(filename) ? "<buffer>" : ":memory:",
320
491
  readonly: Boolean(options) && typeof options === "object" && (options.readonly === true || options.readOnly === true)
321
492
  };
493
+ if (turnDeadlines)
494
+ installNativeDeadlineRouting(db);
322
495
  installTransactionRouting(db, metadata.readonly);
323
496
  Object.defineProperty(db, "close", {
324
497
  configurable: true,
@@ -371,6 +544,12 @@ function buildNodeSqliteDatabaseClass(DatabaseSync) {
371
544
  return NodeSqliteDatabase;
372
545
  }
373
546
  var Database2 = TrackedDatabase;
547
+ function createSqliteTurnDatabase2(filename, options) {
548
+ return trackSqliteConnection(new DatabaseImpl(filename, options), filename, options, true);
549
+ }
550
+ function hasSqliteTurnDeadlineRouting2(db) {
551
+ return busyTimeoutStates.has(db);
552
+ }
374
553
  function getSqliteDatabasePath2(db) {
375
554
  for (const connection of trackedSqliteConnections.values()) {
376
555
  if (connection.reference.deref() === db) {
@@ -390,6 +569,11 @@ function pragmaValue(db, name) {
390
569
  return row[name] ?? Object.values(row)[0];
391
570
  }
392
571
  function pragmaNumber(db, name) {
572
+ if (name === "busy_timeout") {
573
+ const state = busyTimeoutStates.get(db);
574
+ if (state)
575
+ return state.current;
576
+ }
393
577
  try {
394
578
  const value = pragmaValue(db, name);
395
579
  return typeof value === "number" && Number.isFinite(value) ? value : null;
@@ -455,11 +639,34 @@ function prepareCachedStatement2(db, sql) {
455
639
  return ownPreparedStatement(db, db.prepare(sql));
456
640
  }
457
641
  var privilegeDepth = new WeakMap;
642
+ var nativeWaitScope;
643
+ function releaseNativeWaitLease(lease) {
644
+ lease.active = false;
645
+ lease.remainingMs = undefined;
646
+ }
647
+ function withSqliteTurnDeadline2(remainingMs, operation) {
648
+ const scope = nativeWaitScope ??= new AsyncLocalStorage;
649
+ const lease = { active: true, remainingMs };
650
+ return scope.run(lease, () => {
651
+ try {
652
+ const result = operation();
653
+ if (result && typeof result.then === "function")
654
+ return Promise.resolve(result).finally(releaseNativeWaitLease.bind(null, lease));
655
+ releaseNativeWaitLease(lease);
656
+ return result;
657
+ } catch (error) {
658
+ releaseNativeWaitLease(lease);
659
+ throw error;
660
+ }
661
+ });
662
+ }
458
663
  var transformPassScope = new AsyncLocalStorage;
459
664
  var admissionScope = new AsyncLocalStorage;
460
665
  var backgroundWriterScope = new AsyncLocalStorage;
461
666
  function withSqliteBackgroundWriter2(operation) {
462
- return backgroundWriterScope.run(true, operation);
667
+ if (!nativeWaitScope?.getStore())
668
+ return backgroundWriterScope.run(true, operation);
669
+ return nativeWaitScope.run(undefined, () => backgroundWriterScope.run(true, operation));
463
670
  }
464
671
  function withSqliteTransformPass2(operation) {
465
672
  const existing = transformPassScope.getStore();
@@ -490,7 +697,9 @@ function withSqliteTransformPass2(operation) {
490
697
  return typeof reservation === "function" ? run(reservation) : reservation.then(run);
491
698
  }
492
699
  function withoutSqliteTransformPass2(operation) {
493
- return transformPassScope.run(undefined, operation);
700
+ if (!nativeWaitScope?.getStore())
701
+ return transformPassScope.run(undefined, operation);
702
+ return nativeWaitScope.run(undefined, () => transformPassScope.run(undefined, operation));
494
703
  }
495
704
  function guardSqliteTransformPass2(guard) {
496
705
  const lease = transformPassScope.getStore();
@@ -691,7 +900,7 @@ async function withAsyncPrivilegedWriter2(db, operation, options = {}) {
691
900
  } catch (error) {
692
901
  if (entered || !(error instanceof SqliteAcquisitionBusyError))
693
902
  throw error;
694
- options.beforeRetry?.(error);
903
+ await options.beforeRetry?.(error);
695
904
  const elapsed = performance.now() - started;
696
905
  if (elapsed >= budgetMs) {
697
906
  reportSqliteDiagnostic(`sqlite writer site=privileged_writer lane=foreground acquire_ms=${Math.round(elapsed)} attempts=${attempts} outcome=busy`);
@@ -829,4 +1038,4 @@ async function yieldIndexChunk2() {
829
1038
  await yieldIndexTurn();
830
1039
  }
831
1040
 
832
- export { inRootAsyncContext2, registerSlowWriteReporter2, registerSqliteDiagnosticSink2, detectSqliteRuntime2, getTagIdentityWriteGeneration2, Database2, getSqliteDatabasePath2, prepareCachedStatement2, withSqliteBackgroundWriter2, withSqliteTransformPass2, withoutSqliteTransformPass2, guardSqliteTransformPass2, assertTransformWrite2, isTransientSqliteError2, beginSqliteWriterAsync2, withAsyncPrivilegedWriter2, withPrivilegedWriter2, indexCoordinationInput2, releaseIndexOwner2, withIndexChunk2, yieldIndexChunk2 };
1041
+ export { inRootAsyncContext2, registerSlowWriteReporter2, registerSqliteDiagnosticSink2, detectSqliteRuntime2, getTagIdentityWriteGeneration2, Database2, createSqliteTurnDatabase2, hasSqliteTurnDeadlineRouting2, getSqliteDatabasePath2, prepareCachedStatement2, withSqliteTurnDeadline2, withSqliteBackgroundWriter2, withSqliteTransformPass2, withoutSqliteTransformPass2, guardSqliteTransformPass2, assertTransformWrite2, isTransientSqliteError2, beginSqliteWriterAsync2, withAsyncPrivilegedWriter2, withPrivilegedWriter2, indexCoordinationInput2, releaseIndexOwner2, withIndexChunk2, yieldIndexChunk2 };