@indigoai-us/hq-cloud 6.16.50 → 6.16.51-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 (147) hide show
  1. package/dist/bin/rescue-runner.js +1 -2
  2. package/dist/bin/rescue-runner.js.map +1 -1
  3. package/dist/bin/sync-runner-company.d.ts +6 -0
  4. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  5. package/dist/bin/sync-runner-company.js +30 -1
  6. package/dist/bin/sync-runner-company.js.map +1 -1
  7. package/dist/bin/sync-runner-company.test.js +55 -0
  8. package/dist/bin/sync-runner-company.test.js.map +1 -1
  9. package/dist/bin/sync-runner-planning.d.ts +12 -3
  10. package/dist/bin/sync-runner-planning.d.ts.map +1 -1
  11. package/dist/bin/sync-runner-planning.js +65 -3
  12. package/dist/bin/sync-runner-planning.js.map +1 -1
  13. package/dist/bin/sync-runner-planning.test.js +108 -1
  14. package/dist/bin/sync-runner-planning.test.js.map +1 -1
  15. package/dist/bin/sync-runner-watch-loop.d.ts +48 -2
  16. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  17. package/dist/bin/sync-runner-watch-loop.js +281 -113
  18. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  19. package/dist/bin/sync-runner.d.ts +33 -5
  20. package/dist/bin/sync-runner.d.ts.map +1 -1
  21. package/dist/bin/sync-runner.js +34 -4
  22. package/dist/bin/sync-runner.js.map +1 -1
  23. package/dist/bin/sync-runner.test.js +555 -78
  24. package/dist/bin/sync-runner.test.js.map +1 -1
  25. package/dist/cli/onepass-pull-planner-regressions.test.d.ts +2 -0
  26. package/dist/cli/onepass-pull-planner-regressions.test.d.ts.map +1 -0
  27. package/dist/cli/onepass-pull-planner-regressions.test.js +300 -0
  28. package/dist/cli/onepass-pull-planner-regressions.test.js.map +1 -0
  29. package/dist/cli/onepass-pull-planner.test.d.ts +2 -0
  30. package/dist/cli/onepass-pull-planner.test.d.ts.map +1 -0
  31. package/dist/cli/onepass-pull-planner.test.js +266 -0
  32. package/dist/cli/onepass-pull-planner.test.js.map +1 -0
  33. package/dist/cli/rescue-core.d.ts +0 -31
  34. package/dist/cli/rescue-core.d.ts.map +1 -1
  35. package/dist/cli/rescue-core.js +63 -596
  36. package/dist/cli/rescue-core.js.map +1 -1
  37. package/dist/cli/rescue-rsync-preflight.test.js +4 -256
  38. package/dist/cli/rescue-rsync-preflight.test.js.map +1 -1
  39. package/dist/cli/rescue.d.ts +0 -4
  40. package/dist/cli/rescue.d.ts.map +1 -1
  41. package/dist/cli/rescue.js +1 -20
  42. package/dist/cli/rescue.js.map +1 -1
  43. package/dist/cli/rescue.test.js +1 -16
  44. package/dist/cli/rescue.test.js.map +1 -1
  45. package/dist/cli/share-presign-transport.test.js +0 -2
  46. package/dist/cli/share-presign-transport.test.js.map +1 -1
  47. package/dist/cli/share.d.ts +19 -0
  48. package/dist/cli/share.d.ts.map +1 -1
  49. package/dist/cli/share.js +57 -37
  50. package/dist/cli/share.js.map +1 -1
  51. package/dist/cli/share.test.js +87 -93
  52. package/dist/cli/share.test.js.map +1 -1
  53. package/dist/cli/streaming-pull-planner.bench.test.d.ts +15 -0
  54. package/dist/cli/streaming-pull-planner.bench.test.d.ts.map +1 -0
  55. package/dist/cli/streaming-pull-planner.bench.test.js +273 -0
  56. package/dist/cli/streaming-pull-planner.bench.test.js.map +1 -0
  57. package/dist/cli/sync-scope.test.js +8 -0
  58. package/dist/cli/sync-scope.test.js.map +1 -1
  59. package/dist/cli/sync.d.ts +12 -0
  60. package/dist/cli/sync.d.ts.map +1 -1
  61. package/dist/cli/sync.js +226 -118
  62. package/dist/cli/sync.js.map +1 -1
  63. package/dist/cli/sync.test.js +215 -2
  64. package/dist/cli/sync.test.js.map +1 -1
  65. package/dist/cli/watch-event-push-conflict.test.js +0 -2
  66. package/dist/cli/watch-event-push-conflict.test.js.map +1 -1
  67. package/dist/cognito-auth.d.ts +0 -30
  68. package/dist/cognito-auth.d.ts.map +1 -1
  69. package/dist/cognito-auth.js +0 -175
  70. package/dist/cognito-auth.js.map +1 -1
  71. package/dist/index.d.ts +2 -2
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +1 -1
  74. package/dist/index.js.map +1 -1
  75. package/dist/journal-store.test.js +130 -7
  76. package/dist/journal-store.test.js.map +1 -1
  77. package/dist/journal.d.ts +9 -1
  78. package/dist/journal.d.ts.map +1 -1
  79. package/dist/journal.js +30 -21
  80. package/dist/journal.js.map +1 -1
  81. package/dist/lib/exit-codes.d.ts +15 -0
  82. package/dist/lib/exit-codes.d.ts.map +1 -1
  83. package/dist/lib/exit-codes.js +15 -0
  84. package/dist/lib/exit-codes.js.map +1 -1
  85. package/dist/machine-auth.test.js +0 -295
  86. package/dist/machine-auth.test.js.map +1 -1
  87. package/dist/object-io.d.ts +2 -2
  88. package/dist/object-io.d.ts.map +1 -1
  89. package/dist/object-io.js +3 -18
  90. package/dist/object-io.js.map +1 -1
  91. package/dist/personal-vault.d.ts.map +1 -1
  92. package/dist/personal-vault.js +10 -3
  93. package/dist/personal-vault.js.map +1 -1
  94. package/dist/personal-vault.test.js +35 -0
  95. package/dist/personal-vault.test.js.map +1 -1
  96. package/dist/remote-pull.d.ts.map +1 -1
  97. package/dist/remote-pull.js +9 -2
  98. package/dist/remote-pull.js.map +1 -1
  99. package/dist/remote-pull.test.js +59 -2
  100. package/dist/remote-pull.test.js.map +1 -1
  101. package/dist/s3.d.ts +1 -18
  102. package/dist/s3.d.ts.map +1 -1
  103. package/dist/s3.js +24 -47
  104. package/dist/s3.js.map +1 -1
  105. package/dist/s3.list-cost.test.d.ts +2 -0
  106. package/dist/s3.list-cost.test.d.ts.map +1 -0
  107. package/dist/s3.list-cost.test.js +51 -0
  108. package/dist/s3.list-cost.test.js.map +1 -0
  109. package/dist/s3.test.js +1 -27
  110. package/dist/s3.test.js.map +1 -1
  111. package/dist/sync/realtime-telemetry.d.ts +1 -1
  112. package/dist/sync/realtime-telemetry.d.ts.map +1 -1
  113. package/dist/sync/realtime-telemetry.js +14 -6
  114. package/dist/sync/realtime-telemetry.js.map +1 -1
  115. package/dist/sync/realtime-telemetry.test.js +136 -16
  116. package/dist/sync/realtime-telemetry.test.js.map +1 -1
  117. package/dist/sync/remote-file-spool.d.ts +16 -0
  118. package/dist/sync/remote-file-spool.d.ts.map +1 -0
  119. package/dist/sync/remote-file-spool.js +91 -0
  120. package/dist/sync/remote-file-spool.js.map +1 -0
  121. package/dist/sync/remote-file-spool.test.d.ts +2 -0
  122. package/dist/sync/remote-file-spool.test.d.ts.map +1 -0
  123. package/dist/sync/remote-file-spool.test.js +77 -0
  124. package/dist/sync/remote-file-spool.test.js.map +1 -0
  125. package/dist/sync-core.d.ts +4 -6
  126. package/dist/sync-core.d.ts.map +1 -1
  127. package/dist/sync-core.js +4 -10
  128. package/dist/sync-core.js.map +1 -1
  129. package/dist/sync-core.test.js +1 -25
  130. package/dist/sync-core.test.js.map +1 -1
  131. package/package.json +2 -1
  132. package/dist/cli/rescue-breaker.test.d.ts +0 -2
  133. package/dist/cli/rescue-breaker.test.d.ts.map +0 -1
  134. package/dist/cli/rescue-breaker.test.js +0 -334
  135. package/dist/cli/rescue-breaker.test.js.map +0 -1
  136. package/dist/cli/rescue-realpath.test.d.ts +0 -2
  137. package/dist/cli/rescue-realpath.test.d.ts.map +0 -1
  138. package/dist/cli/rescue-realpath.test.js +0 -109
  139. package/dist/cli/rescue-realpath.test.js.map +0 -1
  140. package/dist/cli/rescue-snapshot-recovery.test.d.ts +0 -2
  141. package/dist/cli/rescue-snapshot-recovery.test.d.ts.map +0 -1
  142. package/dist/cli/rescue-snapshot-recovery.test.js +0 -260
  143. package/dist/cli/rescue-snapshot-recovery.test.js.map +0 -1
  144. package/dist/cli/rescue-source-switch-floor.test.d.ts +0 -2
  145. package/dist/cli/rescue-source-switch-floor.test.d.ts.map +0 -1
  146. package/dist/cli/rescue-source-switch-floor.test.js +0 -210
  147. package/dist/cli/rescue-source-switch-floor.test.js.map +0 -1
@@ -10,7 +10,7 @@ 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";
@@ -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
- }
921
- };
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;
1123
+ passScheduler.stop();
956
1124
  };
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
@@ -1981,7 +2141,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
1981
2141
  }
1982
2142
  return false;
1983
2143
  };
1984
- const runScopedDrain = async (batch, watcherBatchGeneration, wakeOnPushFailure = false) => {
2144
+ const runScopedDrain = async (batch, watcherBatchGeneration, wakeOnPushFailure = false, allowFastRealtimeOvertake = false) => {
1985
2145
  if (batch)
1986
2146
  await resolveDeleteSnapshotCaptures(batch);
1987
2147
  const grouped = batch && batch.paths.size > 0 ? groupBatchByRoute(batch) : null;
@@ -2010,11 +2170,11 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2010
2170
  const scopedArgv = buildScopedPushArgv(group.route, admittedPaths, passArgv, group.deleteScopeRoots);
2011
2171
  return runGuarded(scopedArgv, admittedChanges.size > 0
2012
2172
  ? () => prepareWatcherChanges(admittedChanges)
2013
- : undefined, lane);
2173
+ : undefined, lane, "realtime", allowFastRealtimeOvertake);
2014
2174
  })
2015
2175
  : { result: await runGuarded(buildScopedPushArgv(group.route, group.relPaths, passArgv, group.deleteScopeRoots), group.changes.size > 0
2016
2176
  ? () => prepareWatcherChanges(group.changes)
2017
- : undefined, lane), accepted: true, coordinatorRefusedPaths: [] };
2177
+ : undefined, lane, "realtime", allowFastRealtimeOvertake), accepted: true, coordinatorRefusedPaths: [] };
2018
2178
  const result = execution.result;
2019
2179
  if (execution.accepted && passExitCode(result) === 0) {
2020
2180
  const refusedPaths = new Set(typeof result === "number"
@@ -2084,8 +2244,8 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2084
2244
  ? "fast"
2085
2245
  : "slow";
2086
2246
  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 };
2247
+ ? await runRealtimePass("watcher", "pull", group.relPaths, lane, (slice) => runGuarded(buildTargetedPullArgv(group.route, passArgv, slice.paths.map((path) => path.canonicalPath)), undefined, lane, "realtime"))
2248
+ : { result: await runGuarded(pullArgv, undefined, lane, "realtime"), accepted: true };
2089
2249
  const exit = execution.accepted ? passExitCode(execution.result) : 1;
2090
2250
  if (exit !== 0 && worstExit === 0)
2091
2251
  worstExit = exit;
@@ -2290,13 +2450,13 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2290
2450
  return;
2291
2451
  }
2292
2452
  if (lanes?.fast &&
2293
- activeSlowPass !== null) {
2453
+ (passScheduler.hasActiveChore || passScheduler.hasActiveSlowRealtime)) {
2294
2454
  const generation = ++nextWatcherBatchGeneration;
2295
2455
  // Fast dispatch bypasses the pending-batch ingress, so preserve both
2296
2456
  // of its contracts here: only selected routes may run, and V2 sees
2297
2457
  // the same dirty signal as the compatibility drain.
2298
2458
  realtimeState.scheduler?.signal("watcher");
2299
- void runScopedDrain(lanes.fast, generation, true).then((exit) => {
2459
+ void runScopedDrain(lanes.fast, generation, true, true).then((exit) => {
2300
2460
  if (exit === 0)
2301
2461
  return;
2302
2462
  restorePendingWatcherPaths(lanes.fast.paths, generation);
@@ -2415,7 +2575,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2415
2575
  // Submit every route in this lane before awaiting any result. In
2416
2576
  // particular, a later route's fast receipt must never wait for an
2417
2577
  // 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))));
2578
+ 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
2579
  for (const execution of completed) {
2420
2580
  if (!execution.accepted || passExitCode(execution.result) !== 0) {
2421
2581
  throw new Error(`targeted pull failed with exit code ${passExitCode(execution.result)}`);
@@ -2469,7 +2629,7 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2469
2629
  }
2470
2630
  for (const { route, paths } of pathsByRoute.values()) {
2471
2631
  const targetedArgv = buildTargetedPullArgv(route, passArgv, paths);
2472
- const result = await runGuarded(targetedArgv, undefined, lane);
2632
+ const result = await runGuarded(targetedArgv, undefined, lane, "realtime");
2473
2633
  if (passExitCode(result) !== 0) {
2474
2634
  throw new Error(`targeted pull failed with exit code ${passExitCode(result)}`);
2475
2635
  }
@@ -2517,14 +2677,14 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2517
2677
  const execution = await runRealtimePass("wake", "pull", [], "slow", async () => runGuardedTask(() => runTaskWithLock(async () => {
2518
2678
  await drain();
2519
2679
  return 0;
2520
- })));
2680
+ }, "realtime"), "slow", "realtime"));
2521
2681
  outcome = execution.accepted ? execution.result : 1;
2522
2682
  }
2523
2683
  else {
2524
2684
  outcome = await runGuardedTask(() => runTaskWithLock(async () => {
2525
2685
  await drain();
2526
2686
  return 0;
2527
- }));
2687
+ }, "realtime"), "slow", "realtime");
2528
2688
  }
2529
2689
  }
2530
2690
  catch (err) {
@@ -2720,8 +2880,8 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
2720
2880
  targetedJournalSessions: () => targetedJournalSessionCensusSizes().targetedJournalSessions,
2721
2881
  targetedJournalRows: () => targetedJournalSessionCensusSizes().targetedJournalRows,
2722
2882
  cachedAreaJournalMigrations: () => journalCacheCensusSizes().cachedAreaJournalMigrations,
2723
- pendingSlowPasses: () => pendingSlowPasses.length,
2724
- pendingFastPasses: () => pendingFastPasses.length,
2883
+ pendingSlowPasses: () => passScheduler.pendingLaneCount("slow"),
2884
+ pendingFastPasses: () => passScheduler.pendingLaneCount("fast"),
2725
2885
  },
2726
2886
  eventSyncSizes: () => eventSync?.censusSizes?.(),
2727
2887
  watcherSizes: () => watcher?.censusSizes?.(),
@@ -3110,6 +3270,14 @@ export async function runWatchLoop(argv, parsed, deps, runtime) {
3110
3270
  : "") +
3111
3271
  "; staying alive, retrying next poll\n");
3112
3272
  }
3273
+ else if (exitCode === UNRESOLVED_COMPANY_EXIT) {
3274
+ // HQ-SYNC-COMPANY-ROUTE-UID: routed paths are derived from local
3275
+ // company directory names, which may not be memberships. The runner
3276
+ // has already emitted the resolution error; terminating here would
3277
+ // restart the outpost watcher every 60 seconds for each such route.
3278
+ // Keep the watcher alive while preserving the one-shot non-zero code.
3279
+ process.stderr.write("hq-sync-runner: watch pass skipped — unresolved company target; retrying next poll\n");
3280
+ }
3113
3281
  else if (exitCode !== 0) {
3114
3282
  return exitCode;
3115
3283
  }