@cortexkit/pi-magic-context 0.49.20 → 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 (48) hide show
  1. package/dist/auto-search-worker.js +6 -6
  2. package/dist/chunk-embedding-write-worker.js +9 -9
  3. package/dist/{compaction-marker-candidates-fnm47jat.js → compaction-marker-candidates-p1kqkqhq.js} +2 -2
  4. package/dist/compaction-marker-candidates-worker.js +2 -2
  5. package/dist/{compartment-chunk-embedding-81qesfjs.js → compartment-chunk-embedding-y01m95c2.js} +5 -5
  6. package/dist/{embedding-backfill-56e871c6.js → embedding-backfill-2vh6m3bg.js} +11 -11
  7. package/dist/embedding-worker-process.js +1 -1
  8. package/dist/embedding-worker.js +4 -4
  9. package/dist/{index-dzem6gn9.js → index-29nw7n1r.js} +1 -1
  10. package/dist/{index-hws5f0z9.js → index-2qtx0q6h.js} +213 -4
  11. package/dist/{index-5vgt1zeh.js → index-3c3ztwgc.js} +2 -2
  12. package/dist/{index-rgfb753z.js → index-579m1751.js} +2 -2
  13. package/dist/{index-kqb3d8bs.js → index-5j6vwg4z.js} +148 -11
  14. package/dist/{index-6kb2xpw7.js → index-6wdwv3a6.js} +1 -1
  15. package/dist/{index-rfyvax95.js → index-at6py2k3.js} +1 -1
  16. package/dist/{index-bxmf4dhr.js → index-ccanzg8p.js} +2367 -1243
  17. package/dist/{index-mtvvhcw4.js → index-dbgg6hsf.js} +24 -10
  18. package/dist/{index-af9t637y.js → index-ftvm2acc.js} +121 -38
  19. package/dist/{index-zhwwp8zz.js → index-gqjxy2p7.js} +14 -4
  20. package/dist/{index-jxjr7kz9.js → index-gwxaz0z5.js} +2 -4
  21. package/dist/{index-p63nwn5v.js → index-hxp6q5f8.js} +1 -1
  22. package/dist/{index-qerzchrs.js → index-j3pz3a1b.js} +1 -1
  23. package/dist/{index-bttctq07.js → index-n8e5e9fp.js} +6 -3
  24. package/dist/{index-b8am7tns.js → index-pe5gz5c4.js} +4 -4
  25. package/dist/{index-tb3d9trm.js → index-st4gwt64.js} +1 -1
  26. package/dist/{index-5n5zxs8e.js → index-vx6f7wff.js} +2 -2
  27. package/dist/{index-v2epyzbm.js → index-w1twp2kp.js} +9 -7
  28. package/dist/{index-jt0g3hn0.js → index-w6r3ggnp.js} +35 -17
  29. package/dist/{index-vdy273xw.js → index-wgmhk91m.js} +10 -4
  30. package/dist/{index-e19qtjj0.js → index-wqpjhx1p.js} +35 -4
  31. package/dist/index.js +224 -206
  32. package/dist/{message-fts-rowid-map-bpexnsdj.js → message-fts-rowid-map-59y476bk.js} +3 -3
  33. package/dist/{message-index-mvag9crh.js → message-index-3snxf11x.js} +9 -9
  34. package/dist/{message-index-bounded-d4r3weh0.js → message-index-bounded-4ngt600v.js} +9 -9
  35. package/dist/{message-time-backfill-kc10yrcj.js → message-time-backfill-e3r8ddh1.js} +1 -1
  36. package/dist/migration-worker.js +6 -6
  37. package/dist/{project-embedding-registry-cfrg952x.js → project-embedding-registry-hfyzs654.js} +10 -10
  38. package/dist/raw-ordinal-warmup-worker.js +8 -8
  39. package/dist/{read-session-chunk-7g4zvpd8.js → read-session-chunk-5dmc77q2.js} +5 -5
  40. package/dist/{read-session-raw-0fsjh10f.js → read-session-raw-yxv2fv34.js} +2 -2
  41. package/dist/{search-archive-qbhp7e89.js → search-archive-3daj5ttc.js} +6 -6
  42. package/dist/{search-hp4g3g9v.js → search-jhfh3tsj.js} +17 -17
  43. package/dist/{sqlite-d67n6dtr.js → sqlite-v89bj0yy.js} +7 -1
  44. package/dist/{storage-memory-9g882j7a.js → storage-memory-0ndn65fm.js} +2 -2
  45. package/dist/{storage-tags-m2qcp1g2.js → storage-tags-r48vxthe.js} +2 -2
  46. package/dist/subagent-entry.js +22 -21
  47. package/dist/tag-snapshot-worker-pi.js +8 -8
  48. package/package.json +5 -2
@@ -7,18 +7,136 @@ import {
7
7
  import {
8
8
  getTagsBySession2,
9
9
  getTagsByNumbers2
10
- } from "./index-bttctq07.js";
10
+ } from "./index-n8e5e9fp.js";
11
11
  import {
12
12
  inRootAsyncContext2,
13
- getSqliteDatabasePath2
14
- } from "./index-hws5f0z9.js";
13
+ getSqliteDatabasePath2,
14
+ withSqliteTurnDeadline2
15
+ } from "./index-2qtx0q6h.js";
15
16
  import {
16
17
  createFailClosedBlockingError
17
- } from "./index-mtvvhcw4.js";
18
+ } from "./index-dbgg6hsf.js";
18
19
  import {
19
20
  createWorkerShutdown
20
21
  } from "./index-54wmg4rj.js";
21
22
 
23
+ // src/pi-context-budget.ts
24
+ var PI_CONTEXT_BUDGET = {
25
+ hostDeadlineMs: 30000,
26
+ hostMarginMs: 5000,
27
+ outcomeMs: 25000,
28
+ fallbackObservedMs: 4,
29
+ workMs: 21000,
30
+ writerMs: 16500,
31
+ completionReserveMs: 2000,
32
+ searchMs: 3000
33
+ };
34
+
35
+ class PiContextDeadlineError extends Error {
36
+ }
37
+
38
+ class PiContextSupersededError extends Error {
39
+ origin;
40
+ constructor(message, origin = "context ownership") {
41
+ super(message);
42
+ this.origin = origin;
43
+ }
44
+ }
45
+
46
+ class PiContextBudget {
47
+ startedAt;
48
+ now;
49
+ sideTurn = false;
50
+ stage = "entry";
51
+ recovery = "not attempted";
52
+ abandoned = false;
53
+ completed = false;
54
+ failureReason;
55
+ signal;
56
+ cancelled = false;
57
+ isCancelled(error) {
58
+ return this.cancelled || this.signal?.aborted === true || !this.signal && typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
59
+ }
60
+ cancel() {
61
+ this.cancelled = true;
62
+ this.abandoned = true;
63
+ this.completed = false;
64
+ this.failureReason = undefined;
65
+ }
66
+ assertOwner = () => {};
67
+ constructor(startedAt = performance.now(), now = () => performance.now()) {
68
+ this.startedAt = startedAt;
69
+ this.now = now;
70
+ }
71
+ elapsed() {
72
+ return this.now() - this.startedAt;
73
+ }
74
+ remainingWork = () => {
75
+ return Math.max(0, PI_CONTEXT_BUDGET.workMs - this.elapsed());
76
+ };
77
+ remainingOutcome = () => Math.max(0, PI_CONTEXT_BUDGET.outcomeMs - this.elapsed());
78
+ writerAllowance() {
79
+ return Math.max(0, Math.min(PI_CONTEXT_BUDGET.writerMs, this.remainingWork() - PI_CONTEXT_BUDGET.completionReserveMs));
80
+ }
81
+ hydrationAllowance() {
82
+ return Math.max(0, Math.min(this.remainingWork() - PI_CONTEXT_BUDGET.completionReserveMs, this.remainingOutcome() - PI_CONTEXT_BUDGET.completionReserveMs));
83
+ }
84
+ assertOutcome = () => {
85
+ this.assertOwner();
86
+ this.signal?.throwIfAborted();
87
+ if (this.abandoned || this.elapsed() >= PI_CONTEXT_BUDGET.outcomeMs)
88
+ throw new PiContextDeadlineError(this.diagnostic());
89
+ };
90
+ assert = () => {
91
+ this.assertOutcome();
92
+ if (this.elapsed() >= PI_CONTEXT_BUDGET.workMs)
93
+ throw new PiContextDeadlineError(this.diagnostic());
94
+ };
95
+ assertDecisionPublication = () => {
96
+ this.signal?.throwIfAborted();
97
+ if (!this.completed)
98
+ this.assertOwner();
99
+ if (this.abandoned || !this.completed && this.elapsed() >= PI_CONTEXT_BUDGET.outcomeMs)
100
+ throw new PiContextDeadlineError(this.diagnostic());
101
+ };
102
+ diagnostic() {
103
+ return `stage=${this.stage} elapsed=${Math.round(this.elapsed())}ms recovery=${this.recovery}`;
104
+ }
105
+ waitUntil = async (pending, assert, remaining) => {
106
+ try {
107
+ assert();
108
+ } catch (error) {
109
+ pending.catch(() => {});
110
+ throw error;
111
+ }
112
+ let timer;
113
+ let onAbort;
114
+ try {
115
+ return await Promise.race([
116
+ pending,
117
+ ...this.signal ? [
118
+ new Promise((_, reject) => {
119
+ onAbort = () => reject(this.signal?.reason);
120
+ this.signal?.addEventListener("abort", onAbort, {
121
+ once: true
122
+ });
123
+ })
124
+ ] : [],
125
+ new Promise((_, reject) => {
126
+ timer = setTimeout(() => reject(new PiContextDeadlineError(this.diagnostic())), remaining());
127
+ })
128
+ ]);
129
+ } finally {
130
+ clearTimeout(timer);
131
+ if (onAbort)
132
+ this.signal?.removeEventListener("abort", onAbort);
133
+ assert();
134
+ }
135
+ };
136
+ wait = (pending) => this.waitUntil(pending, this.assert, () => this.remainingWork());
137
+ waitMandatory = (pending) => this.waitUntil(pending, this.assertOutcome, this.remainingOutcome);
138
+ }
139
+
22
140
  // src/tag-snapshot-worker-client-pi.ts
23
141
  import { setImmediate as yieldImmediate } from "node:timers/promises";
24
142
  import { Worker } from "node:worker_threads";
@@ -310,6 +428,13 @@ function createPiTagSnapshotReader(db) {
310
428
  const existing = readers.get(db);
311
429
  if (existing)
312
430
  return existing;
431
+ const nativeWaiters = new Set;
432
+ const inSnapshotRoot = (operation) => inRootAsyncContext2(() => withSqliteTurnDeadline2(() => {
433
+ let remaining = Infinity;
434
+ for (const waiter of nativeWaiters)
435
+ remaining = Math.min(remaining, waiter());
436
+ return remaining;
437
+ }, operation));
313
438
  db.exec(`
314
439
  CREATE TEMP TABLE IF NOT EXISTS pi_tag_revision (revision INTEGER NOT NULL);
315
440
  INSERT INTO pi_tag_revision SELECT 0 WHERE NOT EXISTS (SELECT 1 FROM pi_tag_revision);
@@ -403,7 +528,8 @@ function createPiTagSnapshotReader(db) {
403
528
  const ownFirstAllocation = db.prepare(`SELECT 1 FROM pi_tag_first_allocation allocation
404
529
  WHERE session_id = ? AND seen = 1 AND incarnation || ':0' = ?
405
530
  AND EXISTS (SELECT 1 FROM pi_tag_watch WHERE session_id = allocation.session_id)
406
- AND EXISTS (SELECT 1 FROM pi_tag_first_rows WHERE session_id = allocation.session_id)
531
+ AND EXISTS (SELECT 1 FROM pi_tag_changes WHERE session_id = allocation.session_id)
532
+ AND (? = 'legacy' OR EXISTS (SELECT 1 FROM pi_tag_first_rows WHERE session_id = allocation.session_id))
407
533
  AND NOT EXISTS (SELECT 1 FROM pi_tag_first_rows first_row WHERE first_row.session_id = allocation.session_id
408
534
  AND NOT EXISTS (SELECT 1 FROM pi_tag_changes change WHERE change.session_id = first_row.session_id AND change.tag_number IS first_row.tag_number))`);
409
535
  const managed = new Set;
@@ -414,6 +540,7 @@ function createPiTagSnapshotReader(db) {
414
540
  let desiredSession;
415
541
  let failedUntil = 0;
416
542
  let lastFailure;
543
+ let failedSession;
417
544
  const totalRevision = db.prepare("SELECT COALESCE(value, '0') AS revision, value IS NOT NULL AS present FROM (SELECT 1) LEFT JOIN schema_migrations_meta ON key = ?");
418
545
  const assertOutsideTransaction = () => {
419
546
  const connection = db;
@@ -458,7 +585,7 @@ function createPiTagSnapshotReader(db) {
458
585
  if (current?.sessionId === sessionId)
459
586
  current.cancel();
460
587
  };
461
- const load = (sessionId) => inRootAsyncContext2(async () => {
588
+ const load = (sessionId) => inSnapshotRoot(async () => {
462
589
  managed.add(sessionId);
463
590
  if (ready(sessionId))
464
591
  return;
@@ -472,7 +599,7 @@ function createPiTagSnapshotReader(db) {
472
599
  throw new PiTagHydrationError("session hydration superseded", "stale");
473
600
  return load(sessionId);
474
601
  }
475
- if (performance.now() < failedUntil)
602
+ if (failedSession === sessionId && performance.now() < failedUntil)
476
603
  throw lastFailure;
477
604
  watch.run(sessionId);
478
605
  const token = stamps(sessionId, ++jobId);
@@ -516,7 +643,7 @@ function createPiTagSnapshotReader(db) {
516
643
  if (current !== destination || desiredSession !== sessionId || !samePiTagSnapshotToken(token, live) || token.totalRevision !== live.totalRevision || generation.revision !== token.totalRevision || generation.durable !== token.durableVersion)
517
644
  throw new PiTagHydrationError("owner generation or lifetime changed", "stale");
518
645
  const snapshot = {
519
- firstAllocationPending: !live.revisionPresent && candidate.tags.length === 0 && !db.prepare("SELECT 1 FROM tags WHERE session_id = ? LIMIT 1").get(sessionId),
646
+ firstAllocationPending: live.revisionPresent ? undefined : candidate.tags.length === 0 && !db.prepare("SELECT 1 FROM tags WHERE session_id = ? LIMIT 1").get(sessionId) ? "empty" : "legacy",
520
647
  revision: legacyFallbackOnly ? previous.revision : live.localRevision,
521
648
  foreignRevision: live.foreignRevision,
522
649
  tags: legacyFallbackOnly ? previous.tags : candidate.tags,
@@ -545,6 +672,7 @@ function createPiTagSnapshotReader(db) {
545
672
  if (error instanceof PiTagHydrationError && error.code === "stale")
546
673
  destination.stale = true;
547
674
  if (current === destination && desiredSession === sessionId && token.sessionEpoch === (sessionEpochs.get(sessionId) ?? 0)) {
675
+ failedSession = sessionId;
548
676
  lastFailure = error;
549
677
  failedUntil = performance.now() + (error instanceof PiTagHydrationError && error.code === "stale" ? 0 : 500);
550
678
  }
@@ -562,6 +690,12 @@ function createPiTagSnapshotReader(db) {
562
690
  owners.set(db, {
563
691
  ready,
564
692
  load,
693
+ addNativeWaiter: (remaining) => {
694
+ nativeWaiters.add(remaining);
695
+ return () => {
696
+ nativeWaiters.delete(remaining);
697
+ };
698
+ },
565
699
  manage: (sessionId) => managed.add(sessionId),
566
700
  cancel
567
701
  });
@@ -578,9 +712,9 @@ function createPiTagSnapshotReader(db) {
578
712
  ];
579
713
  const stamp = versionStamp.get(sessionId, sessionId);
580
714
  let snapshot = cache.get(sessionId);
581
- if (snapshot?.firstAllocationPending && snapshot.foreignRevision !== external && snapshot.revision !== local && stamp.durable - snapshot.durableVersion === stamp.own - snapshot.ownVersion && ownFirstAllocation.get(sessionId, external)) {
715
+ if (snapshot?.firstAllocationPending && snapshot.foreignRevision !== external && snapshot.revision !== local && stamp.durable - snapshot.durableVersion === stamp.own - snapshot.ownVersion && ownFirstAllocation.get(sessionId, external, snapshot.firstAllocationPending)) {
582
716
  snapshot.foreignRevision = external;
583
- snapshot.firstAllocationPending = false;
717
+ snapshot.firstAllocationPending = undefined;
584
718
  db.prepare("DELETE FROM pi_tag_first_watch WHERE session_id = ?").run(sessionId);
585
719
  db.prepare("DELETE FROM pi_tag_first_rows WHERE session_id = ?").run(sessionId);
586
720
  }
@@ -662,6 +796,7 @@ function createPiTagSnapshotReader(db) {
662
796
  writable: true,
663
797
  value: (...args) => {
664
798
  desiredSession = undefined;
799
+ nativeWaiters.clear();
665
800
  current?.cancel();
666
801
  cache.clear();
667
802
  entryReaders.delete(db);
@@ -885,6 +1020,7 @@ function ensurePiTagSnapshotReady(db, sessionId, budget) {
885
1020
  if (allowance <= 0)
886
1021
  throw new PiTagHydrationError("remaining pass budget exhausted waiting for tag snapshot; turn not sent, retry", "deadline");
887
1022
  let timer;
1023
+ const releaseNativeWaiter = owner.addNativeWaiter(() => budget.remainingOutcome() - PI_CONTEXT_BUDGET.completionReserveMs);
888
1024
  try {
889
1025
  await budget.wait(Promise.race([
890
1026
  owner.load(sessionId),
@@ -901,9 +1037,10 @@ function ensurePiTagSnapshotReady(db, sessionId, budget) {
901
1037
  throw error;
902
1038
  } finally {
903
1039
  clearTimeout(timer);
1040
+ releaseNativeWaiter();
904
1041
  }
905
1042
  }
906
1043
  })();
907
1044
  }
908
1045
 
909
- export { encodePiHydrationOperation, PiTagHydrationError, PI_FALLBACK_ROW_COLUMNS, PI_FALLBACK_ROW_PREDICATE, getPiTagSnapshotView, getPiActiveTagSnapshotView, readPiFallbackTagRows, prewarmPiTagSnapshot, cancelPiTagSnapshotSession, drainPiTagSnapshotSession, assertPiTagSnapshotReady, ensurePiTagSnapshotReady };
1046
+ export { PI_CONTEXT_BUDGET, PiContextDeadlineError, PiContextSupersededError, PiContextBudget, encodePiHydrationOperation, PiTagHydrationError, PI_FALLBACK_ROW_COLUMNS, PI_FALLBACK_ROW_PREDICATE, getPiTagSnapshotView, getPiActiveTagSnapshotView, readPiFallbackTagRows, prewarmPiTagSnapshot, cancelPiTagSnapshotSession, drainPiTagSnapshotSession, assertPiTagSnapshotReady, ensurePiTagSnapshotReady };
@@ -7,7 +7,7 @@ import {
7
7
  import {
8
8
  inRootAsyncContext2,
9
9
  getSqliteDatabasePath2
10
- } from "./index-hws5f0z9.js";
10
+ } from "./index-2qtx0q6h.js";
11
11
 
12
12
  // ../plugin/src/features/magic-context/compaction-marker-candidates.ts
13
13
  import { existsSync, realpathSync } from "node:fs";
@@ -2,7 +2,7 @@ import {
2
2
  inRootAsyncContext2,
3
3
  getSqliteDatabasePath2,
4
4
  releaseIndexOwner2
5
- } from "./index-hws5f0z9.js";
5
+ } from "./index-2qtx0q6h.js";
6
6
  import {
7
7
  registerExitAbort
8
8
  } from "./index-399p688g.js";