@cortexkit/pi-magic-context 0.49.18 → 0.49.20

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 (59) hide show
  1. package/dist/auto-search-worker.js +53 -10
  2. package/dist/chunk-embedding-write-worker.js +29 -22
  3. package/dist/{compaction-marker-candidates-wjzqbspm.js → compaction-marker-candidates-fnm47jat.js} +3 -3
  4. package/dist/compaction-marker-candidates-worker.js +2 -2
  5. package/dist/compartment-chunk-embedding-81qesfjs.js +64 -0
  6. package/dist/embedding-backfill-56e871c6.js +24 -0
  7. package/dist/embedding-worker-process.js +2 -2
  8. package/dist/embedding-worker.js +5 -5
  9. package/dist/historian-calibration-extension.js +1 -1
  10. package/dist/index-54wmg4rj.js +82 -0
  11. package/dist/{index-xnch62s6.js → index-5n5zxs8e.js} +314 -127
  12. package/dist/{index-gdjv1nnd.js → index-5vgt1zeh.js} +6 -7
  13. package/dist/{index-qpq6w67c.js → index-6kb2xpw7.js} +2 -2
  14. package/dist/{index-d43hmn31.js → index-af9t637y.js} +68 -16
  15. package/dist/{index-z2rkv0mb.js → index-b8am7tns.js} +4 -4
  16. package/dist/{index-rhprywn6.js → index-bttctq07.js} +23 -5
  17. package/dist/{index-e1rza8gp.js → index-dzem6gn9.js} +1 -1
  18. package/dist/{index-2mmy81qt.js → index-e19qtjj0.js} +468 -18
  19. package/dist/{index-7bqrfyny.js → index-fgs78jyd.js} +12 -15
  20. package/dist/{index-d4dt19md.js → index-hqw0rt9k.js} +1 -1
  21. package/dist/{index-rrwv9y2k.js → index-hws5f0z9.js} +1 -1
  22. package/dist/{index-f18ec69t.js → index-jn44h81p.js} +1 -1
  23. package/dist/{index-xxn1tpnv.js → index-jt0g3hn0.js} +45 -28
  24. package/dist/{index-d8gc0112.js → index-jxjr7kz9.js} +3 -3
  25. package/dist/{index-4bv5bttd.js → index-kqb3d8bs.js} +9 -5
  26. package/dist/{index-gp928w10.js → index-mtvvhcw4.js} +10 -10
  27. package/dist/{index-1kcfwk9c.js → index-my0dn413.js} +1 -1
  28. package/dist/{index-apgwdby5.js → index-n4wyxcgr.js} +1 -1
  29. package/dist/{index-g7p75667.js → index-p63nwn5v.js} +2 -2
  30. package/dist/{index-svgkv4g2.js → index-qerzchrs.js} +2 -2
  31. package/dist/{index-vktahpdj.js → index-rfyvax95.js} +9 -3
  32. package/dist/{index-yygsh681.js → index-rgfb753z.js} +3 -3
  33. package/dist/{index-d21pdr8v.js → index-tb3d9trm.js} +2 -2
  34. package/dist/{index-vmcgew7x.js → index-v2epyzbm.js} +75 -57
  35. package/dist/{index-fvjthct0.js → index-vdy273xw.js} +107 -20
  36. package/dist/{index-jrap43gc.js → index-w1avzkdt.js} +1 -1
  37. package/dist/{index-r9f4f6s1.js → index-zhwwp8zz.js} +64 -8
  38. package/dist/index.js +1119 -247
  39. package/dist/{logger-dygt96rn.js → logger-shetp43v.js} +1 -1
  40. package/dist/{message-fts-rowid-map-cvpy2dmb.js → message-fts-rowid-map-bpexnsdj.js} +6 -5
  41. package/dist/{message-index-bounded-g9hgresf.js → message-index-bounded-d4r3weh0.js} +14 -13
  42. package/dist/{message-index-74qdqmq7.js → message-index-mvag9crh.js} +14 -13
  43. package/dist/{message-time-backfill-ssshbdy8.js → message-time-backfill-kc10yrcj.js} +3 -3
  44. package/dist/migration-worker.js +12 -11
  45. package/dist/{project-embedding-registry-vf1ncptn.js → project-embedding-registry-cfrg952x.js} +13 -12
  46. package/dist/raw-ordinal-warmup-worker.js +136 -101
  47. package/dist/{read-session-chunk-chepqmw5.js → read-session-chunk-7g4zvpd8.js} +9 -8
  48. package/dist/{read-session-raw-9s3xn7fc.js → read-session-raw-0fsjh10f.js} +4 -3
  49. package/dist/{safe-notification-target-2rkvkmw8.js → safe-notification-target-zcpchea6.js} +2 -2
  50. package/dist/search-archive-qbhp7e89.js +21 -0
  51. package/dist/{search-6ztbx25v.js → search-hp4g3g9v.js} +22 -21
  52. package/dist/{sqlite-fsp0sf3r.js → sqlite-d67n6dtr.js} +1 -1
  53. package/dist/{storage-memory-yxxc5v4n.js → storage-memory-9g882j7a.js} +2 -2
  54. package/dist/{storage-tags-1kets0q8.js → storage-tags-m2qcp1g2.js} +7 -5
  55. package/dist/subagent-entry.js +26 -25
  56. package/dist/tag-snapshot-worker-pi.js +66 -55
  57. package/package.json +1 -1
  58. package/dist/embedding-backfill-vwag34t0.js +0 -23
  59. package/dist/search-archive-jg4a1kcx.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-56e871c6.js"),
23
28
  import("./embedding-cache-7rp4gqzr.js"),
24
- import("./storage-memory-yxxc5v4n.js"),
25
- import("./project-embedding-registry-vf1ncptn.js"),
26
- import("./search-6ztbx25v.js"),
29
+ import("./storage-memory-9g882j7a.js"),
30
+ import("./project-embedding-registry-cfrg952x.js"),
31
+ import("./search-hp4g3g9v.js"),
32
+ import("./compartment-chunk-embedding-81qesfjs.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-d67n6dtr.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
108
138
  });
109
- port.postMessage({ kind: "result", results });
139
+ port.postMessage({
140
+ kind: "result",
141
+ results,
142
+ timings: {
143
+ steps,
144
+ jobMs: performance.now() - jobStarted,
145
+ lastPoolWarm: getLastCompartmentChunkPoolWarm()
146
+ }
147
+ });
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
  }
@@ -125,4 +164,8 @@ port.on("message", (message) => {
125
164
  if (message.kind === "run" && message.input)
126
165
  run(message.input);
127
166
  });
128
- await run(input);
167
+ run(input).catch((error) => {
168
+ setTimeout(() => {
169
+ throw error;
170
+ });
171
+ });
@@ -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-hws5f0z9.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-jxjr7kz9.js";
13
17
  import"./index-399p688g.js";
14
- import"./index-vktahpdj.js";
15
- import"./index-yygsh681.js";
18
+ import"./index-rfyvax95.js";
19
+ import"./index-rgfb753z.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-5n5zxs8e.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-qerzchrs.js";
32
+ import"./index-p63nwn5v.js";
33
+ import"./index-n4wyxcgr.js";
30
34
  import {
31
35
  LATEST_SUPPORTED_VERSION,
32
36
  enforceSchemaFence
33
- } from "./index-gp928w10.js";
34
- import"./index-gdjv1nnd.js";
35
- import"./index-jrap43gc.js";
37
+ } from "./index-mtvvhcw4.js";
38
+ import"./index-5vgt1zeh.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-6kb2xpw7.js";
8
8
  import"./index-399p688g.js";
9
- import"./index-rrwv9y2k.js";
10
- import"./index-7bqrfyny.js";
9
+ import"./index-hws5f0z9.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-d67n6dtr.js"),
9
+ import("./compaction-marker-candidates-fnm47jat.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-5n5zxs8e.js";
28
+ import"./index-jxjr7kz9.js";
29
+ import"./index-rgfb753z.js";
30
+ import"./index-rfyvax95.js";
31
+ import"./index-399p688g.js";
32
+ import"./index-hqw0rt9k.js";
33
+ import"./index-my0dn413.js";
34
+ import"./index-hws5f0z9.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-b8am7tns.js";
4
+ import"./index-v2epyzbm.js";
5
+ import"./index-9p40dgkm.js";
6
+ import"./index-yhzhdp3m.js";
7
+ import"./index-qerzchrs.js";
8
+ import"./index-5n5zxs8e.js";
9
+ import"./index-e19qtjj0.js";
10
+ import"./index-jxjr7kz9.js";
11
+ import"./index-bxmf4dhr.js";
12
+ import"./index-p63nwn5v.js";
13
+ import"./index-rgfb753z.js";
14
+ import"./index-rfyvax95.js";
15
+ import"./index-399p688g.js";
16
+ import"./index-hqw0rt9k.js";
17
+ import"./index-my0dn413.js";
18
+ import"./index-hws5f0z9.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-hws5f0z9.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";
6
+ } from "./index-fgs78jyd.js";
7
7
  import"./index-bxmf4dhr.js";
8
- import"./index-d8gc0112.js";
9
- import"./index-1kcfwk9c.js";
10
- import"./index-rrwv9y2k.js";
8
+ import"./index-jxjr7kz9.js";
9
+ import"./index-my0dn413.js";
10
+ import"./index-hws5f0z9.js";
11
11
  import {
12
12
  resolveLocalEmbeddingWorkerRuntime,
13
13
  getLocalEmbeddingNativeMemoryStats,
14
14
  LocalEmbeddingProvider
15
- } from "./index-2mmy81qt.js";
15
+ } from "./index-e19qtjj0.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,
@@ -0,0 +1,82 @@
1
+ // ../plugin/src/shared/worker-shutdown.ts
2
+ var WORKER_CLOSE_MESSAGE = { kind: "close" };
3
+ function isWorkerCloseMessage(message) {
4
+ return typeof message === "object" && message !== null && message.kind === WORKER_CLOSE_MESSAGE.kind;
5
+ }
6
+ function exitOnWorkerClose(port, closeConnections) {
7
+ const listener = (message) => {
8
+ if (!isWorkerCloseMessage(message))
9
+ return;
10
+ try {
11
+ closeConnections();
12
+ } finally {
13
+ process.exit(0);
14
+ }
15
+ };
16
+ if (!port)
17
+ return { closePort() {} };
18
+ port.on("message", listener);
19
+ let closed = false;
20
+ return {
21
+ closePort() {
22
+ if (closed)
23
+ return;
24
+ closed = true;
25
+ setTimeout(() => {
26
+ port.off("message", listener);
27
+ port.close();
28
+ });
29
+ }
30
+ };
31
+ }
32
+ var outcomes = new Map;
33
+ function createWorkerShutdown(worker, site, deadlineMs) {
34
+ let exited = false;
35
+ let forced = false;
36
+ let closing;
37
+ const exit = new Promise((resolve) => {
38
+ worker.once("exit", () => {
39
+ exited = true;
40
+ resolve();
41
+ });
42
+ });
43
+ const record = () => {
44
+ const counts = outcomes.get(site) ?? { graceful: 0, forced: 0 };
45
+ if (forced)
46
+ counts.forced += 1;
47
+ else
48
+ counts.graceful += 1;
49
+ outcomes.set(site, counts);
50
+ };
51
+ const terminate = () => {
52
+ forced = true;
53
+ worker.terminate().catch(() => {});
54
+ };
55
+ return {
56
+ close() {
57
+ if (closing)
58
+ return closing;
59
+ if (exited) {
60
+ closing = Promise.resolve();
61
+ return closing;
62
+ }
63
+ try {
64
+ worker.postMessage(WORKER_CLOSE_MESSAGE);
65
+ } catch {
66
+ terminate();
67
+ }
68
+ const fallback = setTimeout(() => {
69
+ if (!exited)
70
+ terminate();
71
+ }, deadlineMs);
72
+ fallback.unref?.();
73
+ closing = exit.then(() => {
74
+ clearTimeout(fallback);
75
+ record();
76
+ });
77
+ return closing;
78
+ }
79
+ };
80
+ }
81
+
82
+ export { isWorkerCloseMessage, exitOnWorkerClose, createWorkerShutdown };