@indigoai-us/hq-cloud 6.16.53 → 6.16.54-alpha.0

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 (189) hide show
  1. package/dist/backup-prune.d.ts +1 -23
  2. package/dist/backup-prune.d.ts.map +1 -1
  3. package/dist/backup-prune.js +8 -92
  4. package/dist/backup-prune.js.map +1 -1
  5. package/dist/backup-prune.test.js +0 -36
  6. package/dist/backup-prune.test.js.map +1 -1
  7. package/dist/bin/rescue-runner.js +1 -2
  8. package/dist/bin/rescue-runner.js.map +1 -1
  9. package/dist/bin/sync-runner-company.d.ts +6 -0
  10. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  11. package/dist/bin/sync-runner-company.js +30 -1
  12. package/dist/bin/sync-runner-company.js.map +1 -1
  13. package/dist/bin/sync-runner-company.test.js +57 -126
  14. package/dist/bin/sync-runner-company.test.js.map +1 -1
  15. package/dist/bin/sync-runner-planning.d.ts +12 -3
  16. package/dist/bin/sync-runner-planning.d.ts.map +1 -1
  17. package/dist/bin/sync-runner-planning.js +65 -3
  18. package/dist/bin/sync-runner-planning.js.map +1 -1
  19. package/dist/bin/sync-runner-planning.test.js +108 -1
  20. package/dist/bin/sync-runner-planning.test.js.map +1 -1
  21. package/dist/bin/sync-runner-watch-loop.d.ts +48 -2
  22. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  23. package/dist/bin/sync-runner-watch-loop.js +318 -120
  24. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  25. package/dist/bin/sync-runner.d.ts +33 -5
  26. package/dist/bin/sync-runner.d.ts.map +1 -1
  27. package/dist/bin/sync-runner.js +34 -4
  28. package/dist/bin/sync-runner.js.map +1 -1
  29. package/dist/bin/sync-runner.test.js +1079 -79
  30. package/dist/bin/sync-runner.test.js.map +1 -1
  31. package/dist/cli/onepass-pull-planner-regressions.test.d.ts +2 -0
  32. package/dist/cli/onepass-pull-planner-regressions.test.d.ts.map +1 -0
  33. package/dist/cli/onepass-pull-planner-regressions.test.js +300 -0
  34. package/dist/cli/onepass-pull-planner-regressions.test.js.map +1 -0
  35. package/dist/cli/onepass-pull-planner.test.d.ts +2 -0
  36. package/dist/cli/onepass-pull-planner.test.d.ts.map +1 -0
  37. package/dist/cli/onepass-pull-planner.test.js +266 -0
  38. package/dist/cli/onepass-pull-planner.test.js.map +1 -0
  39. package/dist/cli/rescue-core.d.ts +0 -40
  40. package/dist/cli/rescue-core.d.ts.map +1 -1
  41. package/dist/cli/rescue-core.js +162 -1454
  42. package/dist/cli/rescue-core.js.map +1 -1
  43. package/dist/cli/rescue-rsync-preflight.test.js +4 -256
  44. package/dist/cli/rescue-rsync-preflight.test.js.map +1 -1
  45. package/dist/cli/rescue.d.ts +0 -4
  46. package/dist/cli/rescue.d.ts.map +1 -1
  47. package/dist/cli/rescue.js +1 -20
  48. package/dist/cli/rescue.js.map +1 -1
  49. package/dist/cli/rescue.test.js +1 -16
  50. package/dist/cli/rescue.test.js.map +1 -1
  51. package/dist/cli/share-presign-transport.test.js +0 -2
  52. package/dist/cli/share-presign-transport.test.js.map +1 -1
  53. package/dist/cli/share.d.ts +19 -0
  54. package/dist/cli/share.d.ts.map +1 -1
  55. package/dist/cli/share.js +150 -467
  56. package/dist/cli/share.js.map +1 -1
  57. package/dist/cli/share.test.js +95 -608
  58. package/dist/cli/share.test.js.map +1 -1
  59. package/dist/cli/streaming-pull-planner.bench.test.d.ts +15 -0
  60. package/dist/cli/streaming-pull-planner.bench.test.d.ts.map +1 -0
  61. package/dist/cli/streaming-pull-planner.bench.test.js +273 -0
  62. package/dist/cli/streaming-pull-planner.bench.test.js.map +1 -0
  63. package/dist/cli/sync-scope.test.js +8 -0
  64. package/dist/cli/sync-scope.test.js.map +1 -1
  65. package/dist/cli/sync.d.ts +13 -15
  66. package/dist/cli/sync.d.ts.map +1 -1
  67. package/dist/cli/sync.js +226 -118
  68. package/dist/cli/sync.js.map +1 -1
  69. package/dist/cli/sync.test.js +215 -2
  70. package/dist/cli/sync.test.js.map +1 -1
  71. package/dist/cli/watch-event-push-conflict.test.js +0 -2
  72. package/dist/cli/watch-event-push-conflict.test.js.map +1 -1
  73. package/dist/cognito-auth.d.ts +0 -30
  74. package/dist/cognito-auth.d.ts.map +1 -1
  75. package/dist/cognito-auth.js +0 -175
  76. package/dist/cognito-auth.js.map +1 -1
  77. package/dist/index.d.ts +2 -2
  78. package/dist/index.d.ts.map +1 -1
  79. package/dist/index.js +1 -1
  80. package/dist/index.js.map +1 -1
  81. package/dist/journal-row-store.d.ts +40 -5
  82. package/dist/journal-row-store.d.ts.map +1 -1
  83. package/dist/journal-row-store.js +240 -21
  84. package/dist/journal-row-store.js.map +1 -1
  85. package/dist/journal-row-store.test.js +54 -0
  86. package/dist/journal-row-store.test.js.map +1 -1
  87. package/dist/journal-snapshot-v4.d.ts.map +1 -1
  88. package/dist/journal-snapshot-v4.js +11 -6
  89. package/dist/journal-snapshot-v4.js.map +1 -1
  90. package/dist/journal-store.test.js +259 -7
  91. package/dist/journal-store.test.js.map +1 -1
  92. package/dist/journal.d.ts +43 -15
  93. package/dist/journal.d.ts.map +1 -1
  94. package/dist/journal.js +297 -79
  95. package/dist/journal.js.map +1 -1
  96. package/dist/lib/describe-error.d.ts.map +1 -1
  97. package/dist/lib/describe-error.js +0 -7
  98. package/dist/lib/describe-error.js.map +1 -1
  99. package/dist/lib/describe-error.test.js +0 -11
  100. package/dist/lib/describe-error.test.js.map +1 -1
  101. package/dist/lib/exit-codes.d.ts +15 -0
  102. package/dist/lib/exit-codes.d.ts.map +1 -1
  103. package/dist/lib/exit-codes.js +15 -0
  104. package/dist/lib/exit-codes.js.map +1 -1
  105. package/dist/machine-auth.test.js +0 -295
  106. package/dist/machine-auth.test.js.map +1 -1
  107. package/dist/object-io.d.ts +2 -2
  108. package/dist/object-io.d.ts.map +1 -1
  109. package/dist/object-io.js +3 -18
  110. package/dist/object-io.js.map +1 -1
  111. package/dist/personal-vault.d.ts.map +1 -1
  112. package/dist/personal-vault.js +10 -3
  113. package/dist/personal-vault.js.map +1 -1
  114. package/dist/personal-vault.test.js +35 -0
  115. package/dist/personal-vault.test.js.map +1 -1
  116. package/dist/remote-pull.d.ts.map +1 -1
  117. package/dist/remote-pull.js +9 -2
  118. package/dist/remote-pull.js.map +1 -1
  119. package/dist/remote-pull.test.js +59 -2
  120. package/dist/remote-pull.test.js.map +1 -1
  121. package/dist/s3.d.ts +1 -18
  122. package/dist/s3.d.ts.map +1 -1
  123. package/dist/s3.js +24 -47
  124. package/dist/s3.js.map +1 -1
  125. package/dist/s3.list-cost.test.d.ts +2 -0
  126. package/dist/s3.list-cost.test.d.ts.map +1 -0
  127. package/dist/s3.list-cost.test.js +51 -0
  128. package/dist/s3.list-cost.test.js.map +1 -0
  129. package/dist/s3.test.js +1 -27
  130. package/dist/s3.test.js.map +1 -1
  131. package/dist/sync/area-ledger.d.ts +2 -2
  132. package/dist/sync/area-ledger.d.ts.map +1 -1
  133. package/dist/sync/area-ledger.js +66 -34
  134. package/dist/sync/area-ledger.js.map +1 -1
  135. package/dist/sync/area-ledger.test.js +90 -5
  136. package/dist/sync/area-ledger.test.js.map +1 -1
  137. package/dist/sync/parallel-sync-coordinator.test.js +29 -0
  138. package/dist/sync/parallel-sync-coordinator.test.js.map +1 -1
  139. package/dist/sync/realtime-telemetry.d.ts +1 -1
  140. package/dist/sync/realtime-telemetry.d.ts.map +1 -1
  141. package/dist/sync/realtime-telemetry.js +14 -6
  142. package/dist/sync/realtime-telemetry.js.map +1 -1
  143. package/dist/sync/realtime-telemetry.test.js +136 -16
  144. package/dist/sync/realtime-telemetry.test.js.map +1 -1
  145. package/dist/sync/remote-file-spool.d.ts +16 -0
  146. package/dist/sync/remote-file-spool.d.ts.map +1 -0
  147. package/dist/sync/remote-file-spool.js +91 -0
  148. package/dist/sync/remote-file-spool.js.map +1 -0
  149. package/dist/sync/remote-file-spool.test.d.ts +2 -0
  150. package/dist/sync/remote-file-spool.test.d.ts.map +1 -0
  151. package/dist/sync/remote-file-spool.test.js +77 -0
  152. package/dist/sync/remote-file-spool.test.js.map +1 -0
  153. package/dist/sync/state-store.d.ts +6 -0
  154. package/dist/sync/state-store.d.ts.map +1 -1
  155. package/dist/sync/state-store.js +14 -2
  156. package/dist/sync/state-store.js.map +1 -1
  157. package/dist/sync-core.d.ts +4 -6
  158. package/dist/sync-core.d.ts.map +1 -1
  159. package/dist/sync-core.js +4 -10
  160. package/dist/sync-core.js.map +1 -1
  161. package/dist/sync-core.test.js +1 -25
  162. package/dist/sync-core.test.js.map +1 -1
  163. package/dist/vault-client.d.ts +5 -26
  164. package/dist/vault-client.d.ts.map +1 -1
  165. package/dist/vault-client.js +20 -43
  166. package/dist/vault-client.js.map +1 -1
  167. package/dist/vault-client.test.js +0 -32
  168. package/dist/vault-client.test.js.map +1 -1
  169. package/package.json +2 -1
  170. package/dist/cli/rescue-breaker.test.d.ts +0 -2
  171. package/dist/cli/rescue-breaker.test.d.ts.map +0 -1
  172. package/dist/cli/rescue-breaker.test.js +0 -334
  173. package/dist/cli/rescue-breaker.test.js.map +0 -1
  174. package/dist/cli/rescue-realpath.test.d.ts +0 -2
  175. package/dist/cli/rescue-realpath.test.d.ts.map +0 -1
  176. package/dist/cli/rescue-realpath.test.js +0 -109
  177. package/dist/cli/rescue-realpath.test.js.map +0 -1
  178. package/dist/cli/rescue-snapshot-copy.test.d.ts +0 -2
  179. package/dist/cli/rescue-snapshot-copy.test.d.ts.map +0 -1
  180. package/dist/cli/rescue-snapshot-copy.test.js +0 -627
  181. package/dist/cli/rescue-snapshot-copy.test.js.map +0 -1
  182. package/dist/cli/rescue-snapshot-recovery.test.d.ts +0 -2
  183. package/dist/cli/rescue-snapshot-recovery.test.d.ts.map +0 -1
  184. package/dist/cli/rescue-snapshot-recovery.test.js +0 -260
  185. package/dist/cli/rescue-snapshot-recovery.test.js.map +0 -1
  186. package/dist/cli/rescue-source-switch-floor.test.d.ts +0 -2
  187. package/dist/cli/rescue-source-switch-floor.test.d.ts.map +0 -1
  188. package/dist/cli/rescue-source-switch-floor.test.js +0 -210
  189. package/dist/cli/rescue-source-switch-floor.test.js.map +0 -1
@@ -10,11 +10,11 @@ import { pickCanonicalPersonEntity } from "../vault-client.js";
10
10
  import { acquireOperationLock, withOperationLock, withOperationLockSync, OperationLockedError, OPERATION_LOCKED_EXIT, } from "../operation-lock.js";
11
11
  import { describeError } from "../lib/describe-error.js";
12
12
  import { TRANSIENT_NETWORK_EXIT } from "../lib/net-errors.js";
13
- import { PARTIAL_SYNC_EXIT } from "../lib/exit-codes.js";
13
+ import { PARTIAL_SYNC_EXIT, UNRESOLVED_COMPANY_EXIT, } from "../lib/exit-codes.js";
14
14
  import { getOrCreateMachineId } from "../lib/machine-id.js";
15
15
  import { localPathForVaultKey } from "../local-path-codec.js";
16
16
  import { TreeWatcher, resolveEventDebounceConfig, systemClock, } from "../watcher.js";
17
- import { clearLocalDeleteIntent, getStateDir, markLocalDeleteIntent, PERSONAL_VAULT_JOURNAL_SLUG, openJournalStoreSession, openJournalStoreSessionAsync, listJournalSlugs, listJournals, maintainAreaJournalSlice, readJournal, releaseCachedAreaJournalMigrations, journalCacheCensusSizes, journalRows, readJournalScoped, writeJournal, } from "../journal.js";
17
+ import { clearLocalDeleteIntent, getStateDir, markLocalDeleteIntent, PERSONAL_VAULT_JOURNAL_SLUG, openJournalStoreSession, openJournalStoreSessionAsync, listJournalSlugs, listJournals, maintainAreaJournalSlice, disposeScopedJournalStores, releaseScopedJournalStore, releaseCachedAreaJournalMigrations, journalCacheCensusSizes, journalRows, readJournalScoped, writeJournal, } from "../journal.js";
18
18
  import { isPersonalVaultDeletionExcluded } from "../personal-vault.js";
19
19
  import { NoopPushReceiver, } from "../sync/push-receiver.js";
20
20
  import { resolveEventSync, startEventSync as defaultStartEventSync, } from "../sync/event-sync.js";
@@ -30,6 +30,219 @@ import { InlineSyncWorkerAdapter, } from "../sync/sync-work.js";
30
30
  import { buildRoutePullArgv, buildScopedPushArgv, buildScopedDrainPullArgv, buildTargetedPullArgv, isWatchRouteSelected, routeChangeToTarget, routeKey, } from "./sync-runner-watch-routes.js";
31
31
  import { resolveSkipCompanies } from "./sync-runner-planning.js";
32
32
  const FULL_RECONCILE_EVERY_TICKS = 10;
33
+ /** A deferred full chore must get the next admission after this long. */
34
+ export const CHORE_STARVATION_CAP_MS = 15 * 60 * 1000;
35
+ /**
36
+ * One durable-state owner at a time for a full chore. Realtime work never runs
37
+ * beside that chore: it explicitly awaits `checkpoint()` after a completed
38
+ * durable unit, then resumes once the hand-off is finished. A separately
39
+ * tracked, explicitly opted-in watcher fast pass may use the pre-existing
40
+ * scoped fast-over-slow exception, but never while a chore owns durable state.
41
+ */
42
+ export class CooperativePassScheduler {
43
+ now;
44
+ starvationCapMs;
45
+ pending = [];
46
+ active = null;
47
+ // Scoped fast realtime work has the established in-process lock exception
48
+ // while a scoped slow realtime upload is in flight. Keep that slot separate
49
+ // from `active`: a chore still has exactly one durable-state owner.
50
+ activeFastRealtime = null;
51
+ stopped = false;
52
+ _handoffActive = false;
53
+ constructor(now, starvationCapMs = CHORE_STARVATION_CAP_MS) {
54
+ this.now = now;
55
+ this.starvationCapMs = starvationCapMs;
56
+ }
57
+ get handoffActive() {
58
+ return this._handoffActive;
59
+ }
60
+ get hasActiveChore() {
61
+ return this.active?.workClass === "chore";
62
+ }
63
+ get hasActiveSlowRealtime() {
64
+ return this.active?.workClass === "realtime" && this.active.lane === "slow";
65
+ }
66
+ pendingLaneCount(lane) {
67
+ return this.pending.filter((entry) => entry.lane === lane).length;
68
+ }
69
+ submit(task, workClass, lane = "slow", allowFastRealtimeOvertake = false) {
70
+ if (this.stopped)
71
+ return Promise.resolve(0);
72
+ const directEntry = {
73
+ task,
74
+ workClass,
75
+ lane,
76
+ allowFastRealtimeOvertake,
77
+ // An immediately admitted task cannot starve, so avoid sampling the
78
+ // cadence clock merely to stamp it. This preserves the loop's deadline
79
+ // accounting and keeps the timestamp meaningful only for real queues.
80
+ enqueuedAt: 0,
81
+ resolve: () => undefined,
82
+ reject: () => undefined,
83
+ };
84
+ if (this.canStartFastRealtime(directEntry)) {
85
+ return this.startFastRealtime(directEntry);
86
+ }
87
+ if (this.active === null &&
88
+ this.activeFastRealtime === null &&
89
+ this.pending.length === 0) {
90
+ return this.start(directEntry);
91
+ }
92
+ return new Promise((resolve, reject) => {
93
+ const entry = { ...directEntry, enqueuedAt: this.now(), resolve, reject };
94
+ this.pending.push(entry);
95
+ this.drain();
96
+ });
97
+ }
98
+ stop() {
99
+ this.stopped = true;
100
+ while (this.pending.length > 0)
101
+ this.pending.shift()?.resolve(0);
102
+ }
103
+ drain() {
104
+ if (this.stopped || this.pending.length === 0)
105
+ return;
106
+ if (this.active !== null) {
107
+ const fastRealtime = this.nextFastRealtime();
108
+ if (fastRealtime && this.canStartFastRealtime(fastRealtime)) {
109
+ this.pending.splice(this.pending.indexOf(fastRealtime), 1);
110
+ this.startFastRealtime(fastRealtime);
111
+ }
112
+ return;
113
+ }
114
+ if (this.activeFastRealtime !== null)
115
+ return;
116
+ const next = this.takeNext();
117
+ if (next)
118
+ this.start(next);
119
+ }
120
+ takeNext() {
121
+ const now = this.now();
122
+ const starvedChore = this.pending.findIndex((entry) => entry.workClass === "chore" && now - entry.enqueuedAt >= this.starvationCapMs);
123
+ if (starvedChore >= 0)
124
+ return this.pending.splice(starvedChore, 1)[0];
125
+ // A receiver's bulk drain is still realtime work, but its slow lane is the
126
+ // progress boundary for that delivery. Preserve its established turn
127
+ // before refilling later fast realtime arrivals; this does not let a
128
+ // chore overtake any realtime work.
129
+ const realtime = this.nextRealtimeIndex();
130
+ if (realtime >= 0)
131
+ return this.pending.splice(realtime, 1)[0];
132
+ return this.pending.shift();
133
+ }
134
+ nextRealtimeIndex() {
135
+ const slowRealtime = this.pending.findIndex((entry) => entry.workClass === "realtime" && entry.lane === "slow");
136
+ return slowRealtime >= 0
137
+ ? slowRealtime
138
+ : this.pending.findIndex((entry) => entry.workClass === "realtime");
139
+ }
140
+ nextFastRealtime() {
141
+ return this.pending.find((entry) => entry.workClass === "realtime" && entry.lane === "fast");
142
+ }
143
+ hasStarvedChore() {
144
+ const now = this.now();
145
+ return this.pending.some((entry) => entry.workClass === "chore" && now - entry.enqueuedAt >= this.starvationCapMs);
146
+ }
147
+ canStartFastRealtime(entry) {
148
+ return (!this._handoffActive &&
149
+ this.activeFastRealtime === null &&
150
+ entry.workClass === "realtime" &&
151
+ entry.lane === "fast" &&
152
+ entry.allowFastRealtimeOvertake &&
153
+ this.hasActiveSlowRealtime &&
154
+ !this.hasStarvedChore());
155
+ }
156
+ start(entry) {
157
+ this.active = entry;
158
+ let current;
159
+ try {
160
+ current = this.execute(entry);
161
+ }
162
+ catch (error) {
163
+ current = Promise.reject(error);
164
+ }
165
+ void current.then((outcome) => {
166
+ entry.resolve(outcome);
167
+ this.complete(entry);
168
+ }, (error) => {
169
+ entry.reject(error);
170
+ this.complete(entry);
171
+ }).catch(() => undefined);
172
+ return current;
173
+ }
174
+ complete(entry) {
175
+ if (this.active === entry)
176
+ this.active = null;
177
+ this.drain();
178
+ }
179
+ startFastRealtime(entry) {
180
+ this.activeFastRealtime = entry;
181
+ let current;
182
+ try {
183
+ current = this.execute(entry);
184
+ }
185
+ catch (error) {
186
+ current = Promise.reject(error);
187
+ }
188
+ void current.then((outcome) => {
189
+ entry.resolve(outcome);
190
+ this.completeFastRealtime(entry);
191
+ }, (error) => {
192
+ entry.reject(error);
193
+ this.completeFastRealtime(entry);
194
+ });
195
+ return current;
196
+ }
197
+ completeFastRealtime(entry) {
198
+ if (this.activeFastRealtime === entry)
199
+ this.activeFastRealtime = null;
200
+ this.drain();
201
+ }
202
+ execute(entry) {
203
+ return entry.task({ checkpoint: () => this.checkpoint(entry) });
204
+ }
205
+ async checkpoint(entry) {
206
+ if (this.stopped ||
207
+ entry.workClass !== "chore" ||
208
+ this.active !== entry ||
209
+ !this.pending.some((pending) => pending.workClass === "realtime")) {
210
+ return false;
211
+ }
212
+ // `entry` is suspended at a caller-declared durable boundary. It remains
213
+ // the sole full-pass continuation and will resume after this bounded burst.
214
+ const deferredAt = this.now();
215
+ this.active = null;
216
+ this._handoffActive = true;
217
+ let handedOff = false;
218
+ try {
219
+ while (!this.stopped && this.now() - deferredAt < this.starvationCapMs) {
220
+ const realtimeIndex = this.nextRealtimeIndex();
221
+ if (realtimeIndex < 0)
222
+ break;
223
+ const realtime = this.pending.splice(realtimeIndex, 1)[0];
224
+ handedOff = true;
225
+ this.active = realtime;
226
+ try {
227
+ realtime.resolve(await this.execute(realtime));
228
+ }
229
+ catch (error) {
230
+ realtime.reject(error);
231
+ }
232
+ finally {
233
+ if (this.active === realtime)
234
+ this.active = null;
235
+ }
236
+ }
237
+ }
238
+ finally {
239
+ this._handoffActive = false;
240
+ if (!this.stopped)
241
+ this.active = entry;
242
+ }
243
+ return handedOff;
244
+ }
245
+ }
33
246
  const MAX_FULL_RECONCILE_REASONS = 7;
34
247
  /**
35
248
  * Area storage is AUTO by default and only applies after durable cutover.
@@ -509,6 +722,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
509
722
  const idleHeartbeatIntervalMs = deps.idleHeartbeatIntervalMs ?? WATCH_IDLE_HEARTBEAT_INTERVAL_MS;
510
723
  const monotonicNow = deps.monotonicNow ?? (() => performance.now());
511
724
  const runPass = deps.runPass ?? runtime.runPassWithOperationLockAlreadyHeld;
725
+ const runPassSupportsCooperativeCheckpoint = deps.runPass === undefined || deps.runPassSupportsCooperativeCheckpoint === true;
512
726
  const pollIdx = argv.indexOf("--poll-remote-ms");
513
727
  // An explicit --poll-remote-ms pins a fixed interval (menubar/CLI still set
514
728
  // one). When it is omitted, the interval is resolved fresh each cycle from
@@ -672,7 +886,11 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
672
886
  resolveNoOperationLockUsers = resolve;
673
887
  });
674
888
  };
675
- const runPassWithLock = async (passArgvForRun, prepare, lane = "slow") => {
889
+ const passScheduler = new CooperativePassScheduler(monotonicNow);
890
+ const runPassWithLock = async (passArgvForRun, prepare, lane = "slow", workClass = "chore", checkpoint) => {
891
+ const invokePass = () => checkpoint !== undefined && runPassSupportsCooperativeCheckpoint
892
+ ? runPass(passArgvForRun, checkpoint)
893
+ : runPass(passArgvForRun);
676
894
  // Observe the heap at pass boundaries — a gated checkpoint immediately before
677
895
  // admitting a pass and immediately after it returns. A pass that runs a long
678
896
  // synchronous stretch starves the interval timer, so these boundary
@@ -695,13 +913,31 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
695
913
  // its keyed journal delta and per-object mutation are independent from the
696
914
  // slow pass's bulk paths, and the slow pass already owns the root lock.
697
915
  // Never bypass the lock when no slow pass is active.
916
+ if (
917
+ // A full chore is paused at a caller-declared durable checkpoint. Keep
918
+ // the root lock held by this process, but lend it to exactly one
919
+ // realtime pass; the suspended chore cannot mutate until this returns.
920
+ workClass === "realtime" &&
921
+ passScheduler.handoffActive &&
922
+ processOwnsOperationLock) {
923
+ acquireOperationLockUser();
924
+ try {
925
+ prepare?.();
926
+ const outcome = await invokePass();
927
+ recordAppliedUpserts(outcome);
928
+ return outcome;
929
+ }
930
+ finally {
931
+ releaseOperationLockUser();
932
+ }
933
+ }
698
934
  if (lane === "fast" &&
699
935
  processOwnsOperationLock &&
700
936
  coordinatorWholeMutationChores === 0) {
701
937
  acquireOperationLockUser();
702
938
  try {
703
939
  prepare?.();
704
- const outcome = await runPass(passArgvForRun);
940
+ const outcome = await invokePass();
705
941
  recordAppliedUpserts(outcome);
706
942
  return outcome;
707
943
  }
@@ -721,7 +957,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
721
957
  acquireOperationLockUser();
722
958
  }
723
959
  prepare?.();
724
- const outcome = await runPass(passArgvForRun);
960
+ const outcome = await invokePass();
725
961
  recordAppliedUpserts(outcome);
726
962
  return outcome;
727
963
  }
@@ -755,7 +991,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
755
991
  }
756
992
  try {
757
993
  prepare?.();
758
- const outcome = await runPass(passArgvForRun);
994
+ const outcome = await invokePass();
759
995
  recordAppliedUpserts(outcome);
760
996
  return outcome;
761
997
  }
@@ -778,7 +1014,18 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
778
1014
  throw err;
779
1015
  }
780
1016
  };
781
- const runTaskWithLock = async (task) => {
1017
+ const runTaskWithLock = async (task, workClass = "chore") => {
1018
+ if (workClass === "realtime" &&
1019
+ passScheduler.handoffActive &&
1020
+ processOwnsOperationLock) {
1021
+ acquireOperationLockUser();
1022
+ try {
1023
+ return await task();
1024
+ }
1025
+ finally {
1026
+ releaseOperationLockUser();
1027
+ }
1028
+ }
782
1029
  try {
783
1030
  const handle = acquireOperationLock(hqRoot, "sync", {
784
1031
  timeoutSec: 0,
@@ -872,102 +1119,15 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
872
1119
  heapCheckpoint?.();
873
1120
  }
874
1121
  };
875
- let activeSlowPass = null;
876
- let activeFastPass = null;
877
- const pendingSlowPasses = [];
878
- const pendingFastPasses = [];
879
1122
  const resolveStoppedQueue = () => {
880
- for (const queue of [pendingFastPasses, pendingSlowPasses]) {
881
- while (queue.length > 0)
882
- queue.shift()?.resolve(0);
883
- }
884
- };
885
- const drainQueuedPasses = () => {
886
- if (stopped) {
887
- resolveStoppedQueue();
888
- return;
889
- }
890
- // A queued slow pass is the bulk-drain progress boundary. Admit it before
891
- // refilling the realtime queue; otherwise a steady stream of fast work
892
- // perpetually leapfrogs a receiver's bulk batch and its SQS poll loop
893
- // cannot issue another receive.
894
- if (activeSlowPass === null &&
895
- activeFastPass === null &&
896
- pendingSlowPasses.length > 0) {
897
- const slow = pendingSlowPasses.shift();
898
- if (slow) {
899
- const current = startGuardedTask(slow.task, "slow");
900
- void current.then(slow.resolve, slow.reject);
901
- }
902
- }
903
- if (activeFastPass === null) {
904
- const fast = pendingFastPasses.shift();
905
- if (fast) {
906
- const current = startGuardedTask(fast.task, "fast");
907
- void current.then(fast.resolve, fast.reject);
908
- }
909
- }
910
- // A fast pass that acquired the operation lock normally (rather than
911
- // bypassing an in-process slow holder) still owns the cross-process
912
- // boundary. Slow work must queue behind it; only fast work is allowed to
913
- // overlap an already-lock-owning slow pass.
914
- if (activeSlowPass === null && activeFastPass === null) {
915
- const slow = pendingSlowPasses.shift();
916
- if (slow) {
917
- const current = startGuardedTask(slow.task, "slow");
918
- void current.then(slow.resolve, slow.reject);
919
- }
920
- }
1123
+ passScheduler.stop();
921
1124
  };
922
- const startGuardedTask = (task, lane) => {
923
- // Claim the lane before invoking `task`: a task can synchronously activate
924
- // a watcher/scheduler callback before returning its promise. Without this
925
- // placeholder that re-entrant callback sees an idle lane and starts a
926
- // second lock contender.
927
- const placeholder = Promise.resolve(0);
928
- if (lane === "fast")
929
- activeFastPass = placeholder;
930
- else
931
- activeSlowPass = placeholder;
932
- let current;
933
- try {
934
- current = stopped ? placeholder : task();
935
- }
936
- catch (err) {
937
- current = Promise.reject(err);
938
- }
939
- if (lane === "fast")
940
- activeFastPass = current;
941
- else
942
- activeSlowPass = current;
943
- void current
944
- .finally(() => {
945
- if (lane === "fast" && activeFastPass === current) {
946
- activeFastPass = null;
947
- drainQueuedPasses();
948
- }
949
- else if (lane === "slow" && activeSlowPass === current) {
950
- activeSlowPass = null;
951
- drainQueuedPasses();
952
- }
953
- })
954
- .catch(() => undefined);
955
- return current;
956
- };
957
- const runGuardedTask = (task, lane = "slow") => {
958
- const active = lane === "fast"
959
- ? activeFastPass
960
- : activeSlowPass ?? activeFastPass;
961
- const queued = lane === "fast" ? pendingFastPasses : pendingSlowPasses;
962
- if (active === null && queued.length === 0) {
963
- return startGuardedTask(task, lane);
964
- }
965
- return new Promise((resolve, reject) => {
966
- queued.push({ task, lane, resolve, reject });
967
- drainQueuedPasses();
968
- });
969
- };
970
- const runGuarded = (passArgvForRun, prepare, lane = "slow") => runGuardedTask(() => runPassWithLock(passArgvForRun, prepare, lane), lane);
1125
+ const runGuardedTask = (task, lane = "slow", workClass = "chore", allowFastRealtimeOvertake = false) =>
1126
+ // Lanes still choose the pass execution profile, but no longer decide
1127
+ // chore ownership. Realtime normally waits for a chore hand-off; the
1128
+ // watcher fast path explicitly opts into its established scoped exception.
1129
+ passScheduler.submit(task, workClass, lane, allowFastRealtimeOvertake);
1130
+ const runGuarded = (passArgvForRun, prepare, lane = "slow", workClass = "chore", allowFastRealtimeOvertake = false) => runGuardedTask((context) => runPassWithLock(passArgvForRun, prepare, lane, workClass, workClass === "chore" ? context.checkpoint : undefined), lane, workClass, allowFastRealtimeOvertake);
971
1131
  /*
972
1132
  * Area storage and the parallel coordinator default to AUTO. AUTO turns
973
1133
  * them on after durable personal-vault cutover; explicit off is the kill
@@ -1159,6 +1319,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
1159
1319
  });
1160
1320
  },
1161
1321
  onCutOver: () => {
1322
+ releaseScopedJournalStore(PERSONAL_VAULT_JOURNAL_SLUG);
1162
1323
  parallelCoordinatorEnabled = refreshCoordinatorEnabled();
1163
1324
  if (parallelCoordinatorEnabled && coordinator === null) {
1164
1325
  coordinator = createCoordinator();
@@ -1506,7 +1667,8 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
1506
1667
  declaredSize >= 0) {
1507
1668
  return declaredSize;
1508
1669
  }
1509
- const size = journalRows(readJournal(coordinates.slug)).get(coordinates.key)?.size;
1670
+ const size = journalRows(readJournalScoped(coordinates.slug, [coordinates.key]))
1671
+ .get(coordinates.key)?.size;
1510
1672
  return typeof size === "number" && Number.isSafeInteger(size) && size >= 0 ? size : null;
1511
1673
  };
1512
1674
  /**
@@ -1820,13 +1982,34 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
1820
1982
  return publishedContentHashFromJournalEntryBeforeSession(entry, serviceStartedAt);
1821
1983
  };
1822
1984
  const prepareWatcherChanges = (changes) => {
1985
+ const keysByJournal = new Map();
1986
+ const rememberKey = (slug, key) => {
1987
+ const keys = keysByJournal.get(slug) ?? new Set();
1988
+ keys.add(key);
1989
+ keysByJournal.set(slug, keys);
1990
+ };
1991
+ for (const [absolutePath, change] of changes.entries()) {
1992
+ if (change.kind === "unlink" || change.kind === "unlinkDir") {
1993
+ for (const snapshot of change.deleteSnapshots ?? []) {
1994
+ rememberKey(snapshot.journalSlug, snapshot.journalPath);
1995
+ }
1996
+ continue;
1997
+ }
1998
+ const relativePath = path
1999
+ .relative(hqRoot, absolutePath)
2000
+ .split(path.sep)
2001
+ .join("/");
2002
+ const coordinates = journalCoordinates(relativePath);
2003
+ if (coordinates)
2004
+ rememberKey(coordinates.slug, coordinates.key);
2005
+ }
1823
2006
  const journals = new Map();
1824
2007
  const dirty = new Set();
1825
2008
  const journalFor = (slug) => {
1826
2009
  const loaded = journals.get(slug);
1827
2010
  if (loaded)
1828
2011
  return loaded;
1829
- const journal = readJournal(slug);
2012
+ const journal = readJournalScoped(slug, [...(keysByJournal.get(slug) ?? [])]);
1830
2013
  journals.set(slug, journal);
1831
2014
  return journal;
1832
2015
  };
@@ -1981,7 +2164,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
1981
2164
  }
1982
2165
  return false;
1983
2166
  };
1984
- const runScopedDrain = async (batch, watcherBatchGeneration, wakeOnPushFailure = false) => {
2167
+ const runScopedDrain = async (batch, watcherBatchGeneration, wakeOnPushFailure = false, allowFastRealtimeOvertake = false) => {
1985
2168
  if (batch)
1986
2169
  await resolveDeleteSnapshotCaptures(batch);
1987
2170
  const grouped = batch && batch.paths.size > 0 ? groupBatchByRoute(batch) : null;
@@ -2010,11 +2193,11 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2010
2193
  const scopedArgv = buildScopedPushArgv(group.route, admittedPaths, passArgv, group.deleteScopeRoots);
2011
2194
  return runGuarded(scopedArgv, admittedChanges.size > 0
2012
2195
  ? () => prepareWatcherChanges(admittedChanges)
2013
- : undefined, lane);
2196
+ : undefined, lane, "realtime", allowFastRealtimeOvertake);
2014
2197
  })
2015
2198
  : { result: await runGuarded(buildScopedPushArgv(group.route, group.relPaths, passArgv, group.deleteScopeRoots), group.changes.size > 0
2016
2199
  ? () => prepareWatcherChanges(group.changes)
2017
- : undefined, lane), accepted: true, coordinatorRefusedPaths: [] };
2200
+ : undefined, lane, "realtime", allowFastRealtimeOvertake), accepted: true, coordinatorRefusedPaths: [] };
2018
2201
  const result = execution.result;
2019
2202
  if (execution.accepted && passExitCode(result) === 0) {
2020
2203
  const refusedPaths = new Set(typeof result === "number"
@@ -2084,8 +2267,8 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2084
2267
  ? "fast"
2085
2268
  : "slow";
2086
2269
  const execution = parallelCoordinatorEnabled
2087
- ? await runRealtimePass("watcher", "pull", group.relPaths, lane, (slice) => runGuarded(buildTargetedPullArgv(group.route, passArgv, slice.paths.map((path) => path.canonicalPath)), undefined, lane))
2088
- : { result: await runGuarded(pullArgv, undefined, lane), accepted: true };
2270
+ ? await runRealtimePass("watcher", "pull", group.relPaths, lane, (slice) => runGuarded(buildTargetedPullArgv(group.route, passArgv, slice.paths.map((path) => path.canonicalPath)), undefined, lane, "realtime"))
2271
+ : { result: await runGuarded(pullArgv, undefined, lane, "realtime"), accepted: true };
2089
2272
  const exit = execution.accepted ? passExitCode(execution.result) : 1;
2090
2273
  if (exit !== 0 && worstExit === 0)
2091
2274
  worstExit = exit;
@@ -2290,13 +2473,13 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2290
2473
  return;
2291
2474
  }
2292
2475
  if (lanes?.fast &&
2293
- activeSlowPass !== null) {
2476
+ (passScheduler.hasActiveChore || passScheduler.hasActiveSlowRealtime)) {
2294
2477
  const generation = ++nextWatcherBatchGeneration;
2295
2478
  // Fast dispatch bypasses the pending-batch ingress, so preserve both
2296
2479
  // of its contracts here: only selected routes may run, and V2 sees
2297
2480
  // the same dirty signal as the compatibility drain.
2298
2481
  realtimeState.scheduler?.signal("watcher");
2299
- void runScopedDrain(lanes.fast, generation, true).then((exit) => {
2482
+ void runScopedDrain(lanes.fast, generation, true, true).then((exit) => {
2300
2483
  if (exit === 0)
2301
2484
  return;
2302
2485
  restorePendingWatcherPaths(lanes.fast.paths, generation);
@@ -2415,7 +2598,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2415
2598
  // Submit every route in this lane before awaiting any result. In
2416
2599
  // particular, a later route's fast receipt must never wait for an
2417
2600
  // earlier route's slow pull to finish.
2418
- const completed = await Promise.all([...eventsByRoute.values()].map(({ route, events }) => runRealtimePass("receiver", "pull", events.map((event) => event.relativePath), lane, (slice) => runGuarded(buildTargetedPullArgv(route, passArgv, slice.paths.map((path) => path.canonicalPath)), undefined, lane))));
2601
+ const completed = await Promise.all([...eventsByRoute.values()].map(({ route, events }) => runRealtimePass("receiver", "pull", events.map((event) => event.relativePath), lane, (slice) => runGuarded(buildTargetedPullArgv(route, passArgv, slice.paths.map((path) => path.canonicalPath)), undefined, lane, "realtime"))));
2419
2602
  for (const execution of completed) {
2420
2603
  if (!execution.accepted || passExitCode(execution.result) !== 0) {
2421
2604
  throw new Error(`targeted pull failed with exit code ${passExitCode(execution.result)}`);
@@ -2469,7 +2652,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2469
2652
  }
2470
2653
  for (const { route, paths } of pathsByRoute.values()) {
2471
2654
  const targetedArgv = buildTargetedPullArgv(route, passArgv, paths);
2472
- const result = await runGuarded(targetedArgv, undefined, lane);
2655
+ const result = await runGuarded(targetedArgv, undefined, lane, "realtime");
2473
2656
  if (passExitCode(result) !== 0) {
2474
2657
  throw new Error(`targeted pull failed with exit code ${passExitCode(result)}`);
2475
2658
  }
@@ -2517,14 +2700,14 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2517
2700
  const execution = await runRealtimePass("wake", "pull", [], "slow", async () => runGuardedTask(() => runTaskWithLock(async () => {
2518
2701
  await drain();
2519
2702
  return 0;
2520
- })));
2703
+ }, "realtime"), "slow", "realtime"));
2521
2704
  outcome = execution.accepted ? execution.result : 1;
2522
2705
  }
2523
2706
  else {
2524
2707
  outcome = await runGuardedTask(() => runTaskWithLock(async () => {
2525
2708
  await drain();
2526
2709
  return 0;
2527
- }));
2710
+ }, "realtime"), "slow", "realtime");
2528
2711
  }
2529
2712
  }
2530
2713
  catch (err) {
@@ -2720,8 +2903,8 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2720
2903
  targetedJournalSessions: () => targetedJournalSessionCensusSizes().targetedJournalSessions,
2721
2904
  targetedJournalRows: () => targetedJournalSessionCensusSizes().targetedJournalRows,
2722
2905
  cachedAreaJournalMigrations: () => journalCacheCensusSizes().cachedAreaJournalMigrations,
2723
- pendingSlowPasses: () => pendingSlowPasses.length,
2724
- pendingFastPasses: () => pendingFastPasses.length,
2906
+ pendingSlowPasses: () => passScheduler.pendingLaneCount("slow"),
2907
+ pendingFastPasses: () => passScheduler.pendingLaneCount("fast"),
2725
2908
  },
2726
2909
  eventSyncSizes: () => eventSync?.censusSizes?.(),
2727
2910
  watcherSizes: () => watcher?.censusSizes?.(),
@@ -3071,11 +3254,14 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3071
3254
  quarantinedDeleteRoots.delete(root);
3072
3255
  }
3073
3256
  // Delete snapshot capture needs a fresh keyed view while an episode is
3074
- // being assembled, but completed watch cycles do not. Releasing it here
3075
- // prevents the watcher from retaining another full-vault projection next
3076
- // to the targeted-pull and area-ledger views.
3257
+ // being assembled, but completed watch cycles do not. Area-authoritative
3258
+ // journals are different: their bridge owns the packed aggregate that a
3259
+ // later watcher callback reuses. Dropping that bridge at every poll
3260
+ // makes the next scoped callback synchronously re-pack the whole vault.
3261
+ // Keep it for this runner's lifetime; layout/phase changes still close
3262
+ // it in openAreaMigrationIfAuthoritative(), and final shutdown releases
3263
+ // every bridge below.
3077
3264
  releaseWatcherJournalStores();
3078
- releaseCachedAreaJournalMigrations();
3079
3265
  const exitCode = passExitCode(result);
3080
3266
  if (exitCode === runtime.authRequiredPassExit) {
3081
3267
  // The pass already emitted auth-error. Hand the sentinel to the
@@ -3110,6 +3296,14 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3110
3296
  : "") +
3111
3297
  "; staying alive, retrying next poll\n");
3112
3298
  }
3299
+ else if (exitCode === UNRESOLVED_COMPANY_EXIT) {
3300
+ // HQ-SYNC-COMPANY-ROUTE-UID: routed paths are derived from local
3301
+ // company directory names, which may not be memberships. The runner
3302
+ // has already emitted the resolution error; terminating here would
3303
+ // restart the outpost watcher every 60 seconds for each such route.
3304
+ // Keep the watcher alive while preserving the one-shot non-zero code.
3305
+ process.stderr.write("hq-sync-runner: watch pass skipped — unresolved company target; retrying next poll\n");
3306
+ }
3113
3307
  else if (exitCode !== 0) {
3114
3308
  return exitCode;
3115
3309
  }
@@ -3215,6 +3409,10 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3215
3409
  releaseWatcherJournalStores();
3216
3410
  releaseCachedAreaJournalMigrations();
3217
3411
  await shutdown();
3412
+ // Coordinator shutdown waits for any active pass before the retained
3413
+ // projections are dropped. A shutdown must never evict a store that a
3414
+ // final scoped writer is still refreshing or committing through.
3415
+ disposeScopedJournalStores();
3218
3416
  detachSignal?.();
3219
3417
  }
3220
3418
  }