maxpool 1.24.91 → 1.24.93

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.
@@ -68,21 +68,33 @@ export async function runReloadBaton({
68
68
  releaseTimeoutMs = 20_000,
69
69
  takeoverTimeoutMs = 10_000,
70
70
  log = () => {},
71
+ reloadId,
71
72
  }) {
73
+ // Every rollback notice names the reload it is about, so the old worker can tell a
74
+ // late notice for an EARLIER reload from one for the reload it has open.
75
+ const rolledBack = reason => {
76
+ try { oldWorker.send({ type: MSG_ROLLED_BACK, reason, reloadId }); } catch { /* old worker gone */ }
77
+ };
72
78
  // (a) Confirm the freshly-spawned headless worker booted the new code.
73
79
  let ready;
74
80
  try {
75
81
  newWorker.send({ type: MSG_PROBE_READY });
76
82
  ready = await newWorker.waitFor([MSG_READY, MSG_FAILED], readyTimeoutMs);
77
83
  } catch (err) {
84
+ // Tell the old worker BEFORE logging: it resumes serving on this notice, and a log
85
+ // line must never stand between the two (2026-11-13: a blocked terminal write did).
86
+ // Only an expired timer is 'timeout' (merely slow: a cold restart may retry it). A
87
+ // new worker that exited or could not be spawned is 'failed' — as is any rejection
88
+ // whose cause is unknown, since a cold restart into a build that cannot boot leaves
89
+ // no working proxy.
90
+ rolledBack(err?.reason === 'timeout' ? 'timeout' : 'failed');
78
91
  log(`reload: readiness wait failed (${err.message}); rolling back`);
79
- try { oldWorker.send({ type: MSG_ROLLED_BACK, reason: 'timeout' }); } catch { /* old worker gone */ }
80
92
  return RELOAD_ROLLED_BACK;
81
93
  }
82
94
  // (b) New worker did not come up cleanly → roll back fully, old stays primary.
83
95
  if (!ready || ready.type === MSG_FAILED) {
96
+ rolledBack('failed');
84
97
  log(`reload: new worker reported failed (${ready?.reason || 'no ready'}); rolling back`);
85
- try { oldWorker.send({ type: MSG_ROLLED_BACK, reason: 'failed' }); } catch { /* old worker gone */ }
86
98
  return RELOAD_ROLLED_BACK;
87
99
  }
88
100
 
@@ -128,6 +140,58 @@ export async function runReloadBaton({
128
140
  return RELOAD_SWAPPED;
129
141
  }
130
142
 
143
+ /**
144
+ * Wrap a child process in a small IPC channel: `.send(msg[,handle])` and an
145
+ * async `.waitFor([types], timeoutMs)` that resolves with the first matching
146
+ * message, or rejects on timeout / premature child exit.
147
+ */
148
+ export function makeWorkerChannel(child) {
149
+ return {
150
+ child,
151
+ send(msg, handle) {
152
+ try {
153
+ if (handle) child.send(msg, handle);
154
+ else child.send(msg);
155
+ } catch { /* IPC may be torn down mid-swap; baton timeouts cover it */ }
156
+ },
157
+ // A rejection carries `.reason`: 'timeout' (the timer expired), 'exited' (the child
158
+ // exited first — or had already exited) or 'spawn-error'.
159
+ waitFor(types, timeoutMs) {
160
+ const want = Array.isArray(types) ? types : [types];
161
+ const fail = (reason, message) => Object.assign(new Error(message), { reason });
162
+ if (child.exitCode != null || child.signalCode != null) {
163
+ return Promise.reject(fail('exited', `worker already exited (code ${child.exitCode}, signal ${child.signalCode}) before ${want.join('/')}`));
164
+ }
165
+ return new Promise((resolve, reject) => {
166
+ const cleanup = () => {
167
+ clearTimeout(timer);
168
+ child.removeListener('message', onMsg);
169
+ child.removeListener('exit', onExit);
170
+ child.removeListener('error', onError);
171
+ };
172
+ const onMsg = msg => {
173
+ if (msg && want.includes(msg.type)) { cleanup(); resolve(msg); }
174
+ };
175
+ const onExit = (code, signal) => {
176
+ cleanup();
177
+ reject(fail('exited', `worker exited (code ${code}, signal ${signal}) before ${want.join('/')}`));
178
+ };
179
+ // A spawn failure (EMFILE/EAGAIN) surfaces as 'error', not 'exit' — the
180
+ // baton must see it as a failed step (→ ROLLED_BACK/FALLBACK), not hang.
181
+ const onError = err => { cleanup(); reject(fail('spawn-error', `worker spawn error before ${want.join('/')}: ${err.message}`)); };
182
+ const timer = setTimeout(() => {
183
+ cleanup();
184
+ reject(fail('timeout', `timed out waiting for ${want.join('/')}`));
185
+ }, timeoutMs);
186
+ timer.unref?.();
187
+ child.on('message', onMsg);
188
+ child.once('exit', onExit);
189
+ child.once('error', onError);
190
+ });
191
+ },
192
+ };
193
+ }
194
+
131
195
  // Sent to the NEW primary worker once the OLD worker has actually exited. Node calls
132
196
  // libuv's uv_tty_reset_mode() on process exit, which restores the termios the dying
133
197
  // process saved when IT first enabled raw mode — clobbering the live worker's terminal
@@ -135,3 +199,29 @@ export async function runReloadBaton({
135
199
  // in-process can detect it; the TUI keeps rendering while the kernel line-buffers, so
136
200
  // every keystroke (r / q / p) is swallowed. Measured on a real pty 2026-08-07.
137
201
  export const MSG_TTY_REASSERT = 'tty-reassert';
202
+
203
+ /** One log line for the update-window record (release B, B11): when the old worker exited and how. */
204
+ export function describeOldWorkerExit({ swappedAt, exitedAt, killed = false }) {
205
+ const after = ((exitedAt - swappedAt) / 1000).toFixed(1);
206
+ return `reload: old worker exited at ${new Date(exitedAt).toISOString()} (${after}s after swap, ${killed ? 'SIGKILL after the drain cap' : 'clean exit'})`;
207
+ }
208
+
209
+ /**
210
+ * What the old worker does with a rollback notice. `noticeId` is the reload id the
211
+ * supervisor echoed (absent from a supervisor that predates ids), `currentId` the id
212
+ * of the reload this worker requested last, `open` whether that reload is still
213
+ * waiting for an outcome.
214
+ * 'settle' — the notice names the open reload: act on it now
215
+ * 'settle-idless' — an id-less notice (a supervisor that predates ids — it survives
216
+ * seamless updates) while a reload is open: resume serving now, but
217
+ * its reason never authorizes a cold restart. That supervisor tags
218
+ * every readiness rejection 'timeout', a new build that died before
219
+ * ready included, and cannot say which reload it means (red-team
220
+ * round 2, 2026-11-13)
221
+ * 'ignore' — a notice about an earlier reload, or nothing is open
222
+ */
223
+ export function rollbackNoticeAction({ noticeId, currentId, open }) {
224
+ if (!open) return 'ignore';
225
+ if (noticeId == null) return 'settle-idless';
226
+ return noticeId === currentId ? 'settle' : 'ignore';
227
+ }