my-durable 0.0.119 → 0.0.121

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.
@@ -223,8 +223,43 @@ let gitSpawnLive = 0;
223
223
  // low holds every fan-out (status-batch, the boot project-seed walk,
224
224
  // git-files turns). FIFO within each lane; a freed slot wakes high first;
225
225
  // nobody is ever refused and no spawn changes shape — only the wake order.
226
- const gitSpawnQueueHigh: (() => void)[] = [];
227
- const gitSpawnQueueLow: (() => void)[] = [];
226
+ //
227
+ // run-13 (run-11 cold-review f4): the lanes fixed singles-vs-fan-out but
228
+ // left the inverse starvation — releaseGitSpawn wakes the HIGH lane first,
229
+ // always, so sustained interactive traffic keeps the high lane non-empty
230
+ // and a low waiter (a status-batch fan-out, the boot seed walk) queues
231
+ // FOREVER. AGING closes it: a low waiter queued longer than
232
+ // GIT_SPAWN_AGE_MS is promoted into the high lane, ahead of the fresh high
233
+ // traffic that would otherwise starve it. No timers — ages are checked at
234
+ // the only two moments the queue state is decided anyway: when a new low
235
+ // waiter is about to park (acquireGitSpawn) and before each lane choice
236
+ // (releaseGitSpawn). Promotion is pure queue surgery: it moves a callback
237
+ // between arrays, holds no slot, wakes nobody, refuses nobody, and changes
238
+ // nothing for waiters that are not aged.
239
+ const GIT_SPAWN_AGE_MS = 10_000;
240
+ /** Queued waiters — run-13 (f4) gave the low lane an aging clock: a bare
241
+ * `() => void` could not say HOW LONG it had waited. The high lane shares
242
+ * the shape so both queues stay one type; its timestamp is unused. */
243
+ interface GitSpawnWaiter {
244
+ wake: () => void;
245
+ /** When this waiter entered its lane (gitSpawnNow) — the aging clock. */
246
+ enqueuedAt: number;
247
+ }
248
+ const gitSpawnQueueHigh: GitSpawnWaiter[] = [];
249
+ const gitSpawnQueueLow: GitSpawnWaiter[] = [];
250
+ /** How many PROMOTED waiters sit at the HEAD of the high queue: new
251
+ * promotions splice in after them (still ahead of fresh high traffic), so
252
+ * promoted waiters keep arrival FIFO among themselves — run-13 (f4). */
253
+ let gitSpawnPromotedHead = 0;
254
+ /** Cumulative low→high promotions — gitSpawnStats() surfaces this as
255
+ * queuedAged (run-13 f4: the f4 starvation gauge). */
256
+ let gitSpawnQueuedAged = 0;
257
+ /** The aging clock, injectable like setGitExecForTests so tests cross the
258
+ * 10s boundary instantly instead of sleeping the suite (run-13). */
259
+ let gitSpawnNow: () => number = Date.now;
260
+ export function setGitClockForTests(fn?: () => number) {
261
+ gitSpawnNow = fn ?? Date.now;
262
+ }
228
263
 
229
264
  /** Take a slot (or queue FIFO in your lane for one) — the caller owns the slot
230
265
  * until releaseGitSpawn(). No nesting: a slot is held across exactly ONE
@@ -235,26 +270,66 @@ function acquireGitSpawn(priority?: "high" | "low"): Promise<void> {
235
270
  gitSpawnLive++;
236
271
  return Promise.resolve();
237
272
  }
238
- return new Promise<void>((wake) => (priority === "high" ? gitSpawnQueueHigh : gitSpawnQueueLow).push(wake));
273
+ // run-13 (f4): before parking a NEW low waiter, promote every low waiter
274
+ // already past the age — the aged ones escape to the high lane and the
275
+ // newcomer queues behind whatever low traffic remains. A high arrival
276
+ // does NOT trigger this: releases re-check ages on every freed slot,
277
+ // which is what actually drains promotions under load.
278
+ if (priority !== "high") promoteAgedLowWaiters();
279
+ return new Promise<void>((wake) =>
280
+ (priority === "high" ? gitSpawnQueueHigh : gitSpawnQueueLow).push({ wake, enqueuedAt: gitSpawnNow() }),
281
+ );
282
+ }
283
+
284
+ /** run-13 (run-11 cold-review f4): move every low waiter older than
285
+ * GIT_SPAWN_AGE_MS into the high lane. enqueuedAt is monotone along the
286
+ * low queue (FIFO tail push), so the aged waiters are always a PREFIX —
287
+ * spliced off whole, arrival order intact, and inserted behind the
288
+ * already-promoted head so promotions stay globally FIFO among themselves
289
+ * while still waking ahead of fresh high traffic. */
290
+ function promoteAgedLowWaiters(): void {
291
+ const now = gitSpawnNow();
292
+ let aged = 0;
293
+ while (aged < gitSpawnQueueLow.length && now - gitSpawnQueueLow[aged]!.enqueuedAt > GIT_SPAWN_AGE_MS) aged++;
294
+ if (!aged) return;
295
+ const moving = gitSpawnQueueLow.splice(0, aged);
296
+ gitSpawnQueueHigh.splice(gitSpawnPromotedHead, 0, ...moving);
297
+ gitSpawnPromotedHead += moving.length;
298
+ gitSpawnQueuedAged += moving.length;
239
299
  }
240
300
 
241
301
  /** Free a slot — handed straight to the longest-waiting HIGH caller first
242
302
  * (run-11 h2-2), then the longest-waiting low one, when either exists (the
243
- * live count already covers the handover), else returned. */
303
+ * live count already covers the handover), else returned. run-13 (f4):
304
+ * the age check runs BEFORE the lane choice, so a waiter promoted this
305
+ * instant sits at the high head and wakes on THIS release — exactly the
306
+ * sustained-high-traffic starvation f4 describes.
307
+ */
244
308
  function releaseGitSpawn() {
245
- const next = gitSpawnQueueHigh.shift() ?? gitSpawnQueueLow.shift();
246
- if (next) next();
309
+ promoteAgedLowWaiters();
310
+ let next: GitSpawnWaiter | undefined;
311
+ if (gitSpawnQueueHigh.length) {
312
+ next = gitSpawnQueueHigh.shift();
313
+ // The head waiter was a promoted one — one fewer in the promoted prefix.
314
+ if (gitSpawnPromotedHead > 0) gitSpawnPromotedHead--;
315
+ } else {
316
+ next = gitSpawnQueueLow.shift();
317
+ }
318
+ if (next) next.wake();
247
319
  else gitSpawnLive--;
248
320
  }
249
321
 
250
322
  /** run-11 h2-2: the semaphore's state for deviceVitals — live children,
251
323
  * waiting callers, and how many of those waiters are interactive ("high")
252
- * ops parked behind a fan-out. Pure read, no side effects. */
253
- export function gitSpawnStats(): { live: number; queued: number; queuedHigh: number } {
324
+ * ops parked behind a fan-out. Pure read, no side effects. run-13 (f4):
325
+ * queuedAged counts how many low waiters aging has had to rescue —
326
+ * deviceVitals carries this object through untouched, field and all. */
327
+ export function gitSpawnStats(): { live: number; queued: number; queuedHigh: number; queuedAged: number } {
254
328
  return {
255
329
  live: gitSpawnLive,
256
330
  queued: gitSpawnQueueHigh.length + gitSpawnQueueLow.length,
257
331
  queuedHigh: gitSpawnQueueHigh.length,
332
+ queuedAged: gitSpawnQueuedAged,
258
333
  };
259
334
  }
260
335
 
@@ -100,10 +100,11 @@ export interface DeviceVitals {
100
100
  disk: DiskVitals;
101
101
  /** Event-loop lag — the number `loadavg` cannot express: how long ready JS waited to run. */
102
102
  eventLoop: EventLoopLagVitals;
103
- /** Git spawn semaphore (run-11 h2-2): children live now, callers waiting
104
- * for a slot, and how many waiters sit in the interactive "high" lane —
105
- * the number that says "a status-batch fan-out is stalling the panel". */
106
- gitSpawns: { live: number; queued: number; queuedHigh: number };
103
+ /** Git spawn semaphore (run-11 h2-2 + run-13 f4): children live now,
104
+ * callers waiting for a slot, how many waiters sit in the interactive
105
+ * "high" lane, and how many low waiters aging (>10s) has had to rescue
106
+ * from high-lane starvation — the f4 gauge. */
107
+ gitSpawns: { live: number; queued: number; queuedHigh: number; queuedAged: number };
107
108
  pid: number;
108
109
  at: number;
109
110
  }