@mnemonik/shared 7.153.0 → 7.154.1
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.
- package/dist/gateOutbox.d.ts +65 -0
- package/dist/gateOutbox.d.ts.map +1 -0
- package/dist/gateOutbox.js +156 -0
- package/dist/gateOutbox.js.map +1 -0
- package/dist/hookConversation.d.ts +9 -0
- package/dist/hookConversation.d.ts.map +1 -1
- package/dist/hookConversation.js +35 -1
- package/dist/hookConversation.js.map +1 -1
- package/dist/hookRuntime.d.ts +1 -0
- package/dist/hookRuntime.d.ts.map +1 -1
- package/dist/hookRuntime.js +1 -0
- package/dist/hookRuntime.js.map +1 -1
- package/dist/hookTimeouts.d.ts +80 -42
- package/dist/hookTimeouts.d.ts.map +1 -1
- package/dist/hookTimeouts.js +245 -59
- package/dist/hookTimeouts.js.map +1 -1
- package/package.json +2 -2
- package/src/gateOutbox.ts +181 -0
- package/src/hookConversation.ts +32 -1
- package/src/hookRuntime.ts +1 -0
- package/src/hookTimeouts.ts +252 -61
- package/tests/gateOutbox.test.ts +121 -0
package/dist/hookTimeouts.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import diagnosticsChannel from 'node:diagnostics_channel';
|
|
1
2
|
/**
|
|
2
3
|
* Hook dispatcher HTTP timeout budgets + AbortSignal helper.
|
|
3
4
|
*
|
|
@@ -96,47 +97,59 @@ export function withHookTimeout(ms) {
|
|
|
96
97
|
return { signal: ac.signal, cleanup: () => clearTimeout(timer) };
|
|
97
98
|
}
|
|
98
99
|
/**
|
|
99
|
-
*
|
|
100
|
+
* The main hook call (POST /api/v1/hooks/injections) is hedged: when the first
|
|
101
|
+
* request has not answered by HOOK_HEDGE_SHARE of the budget, a second request
|
|
102
|
+
* starts beside it on a fresh connection, and whichever answers first is used.
|
|
100
103
|
*
|
|
101
|
-
* Why
|
|
102
|
-
* were lost for 50 minutes to a stall between Cloudflare
|
|
103
|
-
* node. The server had accepted those requests and was
|
|
104
|
-
* never finished arriving. A second request opens a new
|
|
105
|
-
* normally lands on a healthy path.
|
|
106
|
-
* injection bus dedups on the server), so repeating it is safe.
|
|
104
|
+
* Why a second request at all: on 24 October 2026 about a third of one
|
|
105
|
+
* machine's hook calls were lost for 50 minutes to a stall between Cloudflare
|
|
106
|
+
* and one load-balancer node. The server had accepted those requests and was
|
|
107
|
+
* waiting for a body that never finished arriving. A second request opens a new
|
|
108
|
+
* connection and normally lands on a healthy path.
|
|
107
109
|
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
110
|
+
* Why it is a hedge and not a retry: v7.149.0 abandoned the first request at
|
|
111
|
+
* 65% of the budget and then retried. Every hook process opens its own
|
|
112
|
+
* connection (DNS, TCP and TLS from the user's machine to Cloudflare), and the
|
|
113
|
+
* credential is read before the request leaves, so on 25 October that first
|
|
114
|
+
* request had often been on the wire for only a few hundred milliseconds when
|
|
115
|
+
* it was abandoned. Answers that were merely slow were thrown away, half the
|
|
116
|
+
* calls on the owner's machine needed two requests, about 8% lost both, and the
|
|
117
|
+
* load balancer counted each abandoned request as a client close (ELB 460). A
|
|
118
|
+
* hedge never abandons an answer that is on its way: the first request keeps
|
|
119
|
+
* running to the end of the budget.
|
|
113
120
|
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
121
|
+
* Both requests share one deadline, `budgetMs` from the call, so the budget
|
|
122
|
+
* every hook event is checked against (tests/HookEventBudgets.test.ts) and the
|
|
123
|
+
* host's kill limit do not move. A first request that fails fast (a connection
|
|
124
|
+
* error or a gateway status) starts the second at once instead of at the hedge
|
|
125
|
+
* point.
|
|
118
126
|
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
127
|
+
* What counts as an answer: any response other than a gateway status (408, 502,
|
|
128
|
+
* 503, 504, Cloudflare's 52x) and other than the server's "the first request is
|
|
129
|
+
* still working" reply (HOOK_REPLAY_HEADER: pending). A 4xx or a 500 is an
|
|
130
|
+
* answer (null to the caller): repeating it would repeat the failure.
|
|
131
|
+
*
|
|
132
|
+
* The server de-duplicates the two requests by HOOK_REQUEST_ID_HEADER
|
|
133
|
+
* (src/server/hookReplay.ts): the second request is handed the first one's
|
|
134
|
+
* answer, or waits for it, and nothing is counted twice. Nothing is queued or
|
|
135
|
+
* stored on the client; a call with no answer by the deadline is dropped.
|
|
122
136
|
*/
|
|
123
|
-
export const
|
|
124
|
-
/** Below this much budget left, a second
|
|
137
|
+
export const HOOK_HEDGE_SHARE = 0.65;
|
|
138
|
+
/** Below this much budget left, a second request cannot complete a round trip. */
|
|
125
139
|
export const HOOK_SECOND_ATTEMPT_MIN_MS = 300;
|
|
126
|
-
/** Sent on every
|
|
140
|
+
/** Sent on every request: 1 for the first, 2 for the hedge. */
|
|
127
141
|
export const HOOK_ATTEMPT_HEADER = 'x-mnemonik-hook-attempt';
|
|
128
142
|
/**
|
|
129
|
-
* Sent on both
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
143
|
+
* Sent on both requests with the same value. The server keeps the first
|
|
144
|
+
* request's answer for a few seconds under this id and hands it to the second,
|
|
145
|
+
* so an answer decided by one request is delivered through either, and nothing
|
|
146
|
+
* is counted twice (src/server/hookReplay.ts).
|
|
133
147
|
*/
|
|
134
148
|
export const HOOK_REQUEST_ID_HEADER = 'x-mnemonik-hook-request-id';
|
|
135
149
|
/**
|
|
136
|
-
* Sent on every
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* has already given up.
|
|
150
|
+
* Sent on every request: how many milliseconds the caller will still wait. The
|
|
151
|
+
* server fits its own work inside this less the return trip, so it never
|
|
152
|
+
* decides an answer the client will not be there to receive.
|
|
140
153
|
*/
|
|
141
154
|
export const HOOK_BUDGET_HEADER = 'x-mnemonik-hook-budget-ms';
|
|
142
155
|
/**
|
|
@@ -149,10 +162,61 @@ export const HOOK_BUDGET_HEADER = 'x-mnemonik-hook-budget-ms';
|
|
|
149
162
|
export const HOOK_VERSION_HEADER = 'x-mnemonik-hook-version';
|
|
150
163
|
/** `init` with the hook-version header added; the caller's headers are kept. */
|
|
151
164
|
export function withHookVersion(init, runtime, version) {
|
|
152
|
-
const
|
|
165
|
+
const raw = init.headers;
|
|
166
|
+
if (raw && typeof raw === 'object' && !(raw instanceof Headers) && !Array.isArray(raw)) {
|
|
167
|
+
// A plain record is copied with its property descriptors, not its values:
|
|
168
|
+
// the hedged hook client's budget header is a getter read when the
|
|
169
|
+
// request is sent, after the credential is read (hedgedHookRequest).
|
|
170
|
+
const headers = Object.defineProperties({}, Object.getOwnPropertyDescriptors(raw));
|
|
171
|
+
for (const name of Object.keys(headers)) {
|
|
172
|
+
if (name.toLowerCase() === HOOK_VERSION_HEADER)
|
|
173
|
+
delete headers[name];
|
|
174
|
+
}
|
|
175
|
+
headers[HOOK_VERSION_HEADER] = `${runtime}/${version}`;
|
|
176
|
+
return { ...init, headers };
|
|
177
|
+
}
|
|
178
|
+
const headers = new Headers(raw);
|
|
153
179
|
headers.set(HOOK_VERSION_HEADER, `${runtime}/${version}`);
|
|
154
180
|
return { ...init, headers: Object.fromEntries(headers.entries()) };
|
|
155
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Sent on every request by a hedging client (hedgedHookRequest). Its budget
|
|
184
|
+
* header is what the whole call still has; clients v7.149 to v7.151 sent the
|
|
185
|
+
* first attempt's own 1.3 s cut-off instead, so a server that sees no marker
|
|
186
|
+
* assumes the whole-call 2 s budget rather than trusting that header.
|
|
187
|
+
*/
|
|
188
|
+
export const HOOK_HEDGED_HEADER = 'x-mnemonik-hook-hedged';
|
|
189
|
+
/**
|
|
190
|
+
* Sent on the second request only: what had become of the first when the second
|
|
191
|
+
* left. `pending; sent=812` (still waiting, its body finished sending 812 ms
|
|
192
|
+
* earlier), `pending; sent=none` (it had not finished sending, so the client
|
|
193
|
+
* itself was slow), `gateway; sent=<ms>` (the edge answered 502/503/504/52x) or
|
|
194
|
+
* `transport; sent=<ms>` (the connection failed). The server uses it to count
|
|
195
|
+
* requests the network lost on the way in, which is what the edge alarm reads
|
|
196
|
+
* (src/server/hookReplay.ts, hookEdgeMetrics.ts).
|
|
197
|
+
*/
|
|
198
|
+
export const HOOK_FIRST_ATTEMPT_HEADER = 'x-mnemonik-hook-first-attempt';
|
|
199
|
+
/**
|
|
200
|
+
* Set by the server on a reply that is not an answer: the request found the
|
|
201
|
+
* other request of the same call still working on it and could not wait any
|
|
202
|
+
* longer. The client keeps waiting for the other request.
|
|
203
|
+
*/
|
|
204
|
+
export const HOOK_REPLAY_HEADER = 'x-mnemonik-hook-replay';
|
|
205
|
+
export const HOOK_REPLAY_PENDING = 'pending';
|
|
206
|
+
export function formatHookFirstAttempt(first) {
|
|
207
|
+
return `${first.state}; sent=${first.sentAgoMs === null ? 'none' : Math.max(0, Math.round(first.sentAgoMs))}`;
|
|
208
|
+
}
|
|
209
|
+
export function parseHookFirstAttempt(value) {
|
|
210
|
+
if (typeof value !== 'string')
|
|
211
|
+
return null;
|
|
212
|
+
const match = /^(pending|gateway|transport); sent=(none|\d{1,6})$/.exec(value.trim());
|
|
213
|
+
if (!match)
|
|
214
|
+
return null;
|
|
215
|
+
return {
|
|
216
|
+
state: match[1],
|
|
217
|
+
sentAgoMs: match[2] === 'none' ? null : Number(match[2]),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
156
220
|
/** Statuses that mean the request did not reach, or was not served by, the application. */
|
|
157
221
|
export function isRetryableHookStatus(status) {
|
|
158
222
|
return (status === 408 ||
|
|
@@ -174,36 +238,158 @@ export function isTransientHookFetchError(error) {
|
|
|
174
238
|
(error.message === 'fetch failed' || error.message === 'terminated'));
|
|
175
239
|
}
|
|
176
240
|
/**
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
241
|
+
* When the first request finished sending its body, read from Node's fetch
|
|
242
|
+
* (undici) diagnostics channels. Matched by the request id header, so other
|
|
243
|
+
* requests the process makes at the same time are ignored. Reports null when
|
|
244
|
+
* the channels are unavailable or the body never finished sending; nothing
|
|
245
|
+
* depends on it except the HOOK_FIRST_ATTEMPT_HEADER the server counts.
|
|
182
246
|
*/
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
const
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
return
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
finally {
|
|
197
|
-
cleanup();
|
|
247
|
+
function watchFirstRequestSent(requestId, clock) {
|
|
248
|
+
let sentAt = null;
|
|
249
|
+
let request = null;
|
|
250
|
+
const onCreate = (message) => {
|
|
251
|
+
const candidate = message?.request;
|
|
252
|
+
if (request || !candidate)
|
|
253
|
+
return;
|
|
254
|
+
if (String(candidate.headers ?? '').includes(requestId))
|
|
255
|
+
request = candidate;
|
|
256
|
+
};
|
|
257
|
+
const onBodySent = (message) => {
|
|
258
|
+
if (sentAt === null && request && message?.request === request) {
|
|
259
|
+
sentAt = clock();
|
|
198
260
|
}
|
|
199
261
|
};
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
return null;
|
|
206
|
-
|
|
207
|
-
return
|
|
262
|
+
try {
|
|
263
|
+
diagnosticsChannel.subscribe('undici:request:create', onCreate);
|
|
264
|
+
diagnosticsChannel.subscribe('undici:request:bodySent', onBodySent);
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
return { sentAt: () => null, stop: () => undefined };
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
sentAt: () => sentAt,
|
|
271
|
+
stop: () => {
|
|
272
|
+
try {
|
|
273
|
+
diagnosticsChannel.unsubscribe('undici:request:create', onCreate);
|
|
274
|
+
diagnosticsChannel.unsubscribe('undici:request:bodySent', onBodySent);
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
// Nothing to undo.
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Send one hook call as up to two hedged requests inside `budgetMs` (see
|
|
284
|
+
* HOOK_HEDGE_SHARE). `send` performs one request with the headers and signal it
|
|
285
|
+
* is given; `parse` reads an answer. Returns the first answer, null when no
|
|
286
|
+
* request answered in time or the answer was an error status, and rejects only
|
|
287
|
+
* when `send` or `parse` fails in a way that is not a transport failure.
|
|
288
|
+
*/
|
|
289
|
+
export function hedgedHookRequest(budgetMs, send, parse, clock = Date.now) {
|
|
290
|
+
const startedAt = clock();
|
|
291
|
+
const deadline = startedAt + budgetMs;
|
|
292
|
+
const requestId = globalThis.crypto.randomUUID();
|
|
293
|
+
const sent = watchFirstRequestSent(requestId, clock);
|
|
294
|
+
const controllers = [];
|
|
295
|
+
return new Promise((resolve, reject) => {
|
|
296
|
+
let settled = false;
|
|
297
|
+
let running = 0;
|
|
298
|
+
let secondStarted = false;
|
|
299
|
+
let firstFailure = null;
|
|
300
|
+
let hedgeTimer;
|
|
301
|
+
const finish = (value, error, winner) => {
|
|
302
|
+
if (settled)
|
|
303
|
+
return;
|
|
304
|
+
settled = true;
|
|
305
|
+
clearTimeout(deadlineTimer);
|
|
306
|
+
clearTimeout(hedgeTimer);
|
|
307
|
+
sent.stop();
|
|
308
|
+
// Cancel whatever is still in flight; the request that answered is done.
|
|
309
|
+
for (const controller of controllers)
|
|
310
|
+
if (controller !== winner)
|
|
311
|
+
controller.abort();
|
|
312
|
+
if (error !== undefined)
|
|
313
|
+
reject(error);
|
|
314
|
+
else
|
|
315
|
+
resolve(value);
|
|
316
|
+
};
|
|
317
|
+
const deadlineTimer = setTimeout(() => finish(null), budgetMs);
|
|
318
|
+
const startSecond = () => {
|
|
319
|
+
if (settled || secondStarted)
|
|
320
|
+
return secondStarted;
|
|
321
|
+
if (deadline - clock() < HOOK_SECOND_ATTEMPT_MIN_MS)
|
|
322
|
+
return false;
|
|
323
|
+
secondStarted = true;
|
|
324
|
+
clearTimeout(hedgeTimer);
|
|
325
|
+
launch(2);
|
|
326
|
+
return true;
|
|
327
|
+
};
|
|
328
|
+
const launch = (attemptNumber) => {
|
|
329
|
+
const controller = new AbortController();
|
|
330
|
+
controllers.push(controller);
|
|
331
|
+
const now = clock();
|
|
332
|
+
const headers = {
|
|
333
|
+
[HOOK_ATTEMPT_HEADER]: String(attemptNumber),
|
|
334
|
+
[HOOK_REQUEST_ID_HEADER]: requestId,
|
|
335
|
+
[HOOK_HEDGED_HEADER]: '1',
|
|
336
|
+
};
|
|
337
|
+
// Read when the request is actually built (after the credential read or
|
|
338
|
+
// rotation and the conversation lock), not when the attempt starts, so
|
|
339
|
+
// the server's deadline does not include time already spent here.
|
|
340
|
+
Object.defineProperty(headers, HOOK_BUDGET_HEADER, {
|
|
341
|
+
enumerable: true,
|
|
342
|
+
get: () => String(Math.max(0, Math.floor(deadline - clock()))),
|
|
343
|
+
});
|
|
344
|
+
if (attemptNumber === 2) {
|
|
345
|
+
const sentAt = sent.sentAt();
|
|
346
|
+
headers[HOOK_FIRST_ATTEMPT_HEADER] = formatHookFirstAttempt({
|
|
347
|
+
state: firstFailure ?? 'pending',
|
|
348
|
+
sentAgoMs: sentAt === null ? null : now - sentAt,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
running += 1;
|
|
352
|
+
void (async () => {
|
|
353
|
+
let outcome;
|
|
354
|
+
try {
|
|
355
|
+
const response = await send(headers, controller.signal);
|
|
356
|
+
if (isRetryableHookStatus(response.status))
|
|
357
|
+
outcome = { final: false, kind: 'gateway' };
|
|
358
|
+
else if (response.headers.get(HOOK_REPLAY_HEADER) === HOOK_REPLAY_PENDING)
|
|
359
|
+
outcome = { final: false, kind: 'pending' };
|
|
360
|
+
else if (!response.ok)
|
|
361
|
+
outcome = { final: true, value: null };
|
|
362
|
+
else
|
|
363
|
+
outcome = { final: true, value: await parse(response) };
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
if (!settled && !isTransientHookFetchError(error)) {
|
|
367
|
+
running -= 1;
|
|
368
|
+
finish(null, error);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
outcome = { final: false, kind: 'transport' };
|
|
372
|
+
}
|
|
373
|
+
running -= 1;
|
|
374
|
+
if (settled)
|
|
375
|
+
return;
|
|
376
|
+
if (outcome.final) {
|
|
377
|
+
finish(outcome.value, undefined, controller);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
// Not an answer. The first request's failure starts the second at once;
|
|
381
|
+
// with no request left running there is no answer to wait for.
|
|
382
|
+
if (attemptNumber === 1) {
|
|
383
|
+
if (outcome.kind !== 'pending')
|
|
384
|
+
firstFailure = outcome.kind;
|
|
385
|
+
startSecond();
|
|
386
|
+
}
|
|
387
|
+
if (running === 0)
|
|
388
|
+
finish(null);
|
|
389
|
+
})();
|
|
390
|
+
};
|
|
391
|
+
launch(1);
|
|
392
|
+
hedgeTimer = setTimeout(startSecond, Math.floor(budgetMs * HOOK_HEDGE_SHARE));
|
|
393
|
+
});
|
|
208
394
|
}
|
|
209
395
|
//# sourceMappingURL=hookTimeouts.js.map
|
package/dist/hookTimeouts.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hookTimeouts.js","sourceRoot":"","sources":["../src/hookTimeouts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAErC,2EAA2E;AAC3E,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAExC,2FAA2F;AAC3F,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAEzC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,CAAC;AAE5C;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG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CAAC,CAAC;wBACb,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;wBACpB,OAAO;oBACT,CAAC;oBACD,OAAO,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;gBAChD,CAAC;gBACD,OAAO,IAAI,CAAC,CAAC;gBACb,IAAI,OAAO;oBAAE,OAAO;gBACpB,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;oBAClB,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC;oBAC7C,OAAO;gBACT,CAAC;gBACD,wEAAwE;gBACxE,+DAA+D;gBAC/D,IAAI,aAAa,KAAK,CAAC,EAAE,CAAC;oBACxB,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS;wBAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;oBAC5D,WAAW,EAAE,CAAC;gBAChB,CAAC;gBACD,IAAI,OAAO,KAAK,CAAC;oBAAE,MAAM,CAAC,IAAI,CAAC,CAAC;YAClC,CAAC,CAAC,EAAE,CAAC;QACP,CAAC,CAAC;QAEF,MAAM,CAAC,CAAC,CAAC,CAAC;QACV,UAAU,GAAG,UAAU,CAAC,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,gBAAgB,CAAC,CAAC,CAAC;IAChF,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mnemonik/shared",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.154.1",
|
|
4
4
|
"description": "Shared constants and utilities for Mnemonik packages",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
}
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
|
-
"test": "tsx --test --test-concurrency=2 tests/repositoryFingerprint.test.ts tests/projectIdentity.test.ts tests/nearestProjectIdentity.test.ts tests/readiness.test.ts tests/scannerDisclosure.test.ts tests/hookRuntime.test.ts tests/grantHost.test.ts tests/hookTrace.test.ts",
|
|
48
|
+
"test": "tsx --test --test-concurrency=2 tests/repositoryFingerprint.test.ts tests/projectIdentity.test.ts tests/nearestProjectIdentity.test.ts tests/readiness.test.ts tests/scannerDisclosure.test.ts tests/hookRuntime.test.ts tests/grantHost.test.ts tests/hookTrace.test.ts tests/gateOutbox.test.ts",
|
|
49
49
|
"build": "node ../../node_modules/@typescript/native/bin/tsc",
|
|
50
50
|
"typecheck": "node ../../node_modules/@typescript/native/bin/tsc --noEmit -p tsconfig.test.json",
|
|
51
51
|
"prepublishOnly": "npm run build"
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gate telemetry rides the next injections request instead of its own.
|
|
3
|
+
*
|
|
4
|
+
* Why: every PreToolUse a hook gated used to end with an awaited POST
|
|
5
|
+
* /api/v1/hooks/gate-fired, a whole extra round trip (about 220 ms from New
|
|
6
|
+
* Zealand to us-east-1) after the decision was already made, because the host
|
|
7
|
+
* waits for the hook process to exit. Measured on 26 October 2026 it was about
|
|
8
|
+
* 30% of a PreToolUse hook's process time on an idle machine, and one in three
|
|
9
|
+
* hook requests the server received. The event is telemetry: who gated what,
|
|
10
|
+
* how, and the action observation for credit capture. Nothing on the agent's
|
|
11
|
+
* path waits for it.
|
|
12
|
+
*
|
|
13
|
+
* So the hook writes the event to a small per-session folder (synchronous, no
|
|
14
|
+
* network) and the next injections request of the same session claims the
|
|
15
|
+
* events and carries them as `gateEvents`; the server records them exactly as
|
|
16
|
+
* the gate-fired route does. A request that gets no answer puts its events
|
|
17
|
+
* back for the next one; each event carries an id the server records once, so
|
|
18
|
+
* a request that was recorded but whose answer was lost does not count twice.
|
|
19
|
+
* Events still queued when a session goes idle for GATE_OUTBOX_MAX_AGE_MS are
|
|
20
|
+
* dropped, and the folder holds at most GATE_OUTBOX_CAP events (the oldest
|
|
21
|
+
* go first), so a server that stays unreachable cannot grow it without
|
|
22
|
+
* bound. Temporary and claimed files a killed hook process left behind are
|
|
23
|
+
* swept after GATE_OUTBOX_STRAY_MS. Never throws.
|
|
24
|
+
*
|
|
25
|
+
* One file per event, written to a temporary name and renamed into the folder,
|
|
26
|
+
* and claimed by renaming it again: a reader never sees half an event, two
|
|
27
|
+
* hook processes never claim the same one, and a writer racing a reader never
|
|
28
|
+
* loses its event.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { randomUUID } from 'node:crypto';
|
|
32
|
+
import {
|
|
33
|
+
mkdirSync,
|
|
34
|
+
readFileSync,
|
|
35
|
+
readdirSync,
|
|
36
|
+
renameSync,
|
|
37
|
+
statSync,
|
|
38
|
+
unlinkSync,
|
|
39
|
+
writeFileSync,
|
|
40
|
+
} from 'node:fs';
|
|
41
|
+
import { join } from 'node:path';
|
|
42
|
+
|
|
43
|
+
/** The per-session folder name (pass it to the host's session state path helper). */
|
|
44
|
+
export const GATE_OUTBOX_FILE = 'gate-outbox';
|
|
45
|
+
|
|
46
|
+
/** The most events one request carries; the server accepts the same. */
|
|
47
|
+
export const GATE_OUTBOX_MAX_EVENTS = 32;
|
|
48
|
+
|
|
49
|
+
/** Events older than this are dropped rather than reported: the session went idle. */
|
|
50
|
+
export const GATE_OUTBOX_MAX_AGE_MS = 24 * 60 * 60 * 1000;
|
|
51
|
+
|
|
52
|
+
/** The most events the folder holds; past it the oldest are dropped. */
|
|
53
|
+
export const GATE_OUTBOX_CAP = 500;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A temporary or claimed file older than this belongs to a hook process that
|
|
57
|
+
* was killed between two renames (a live one holds it for milliseconds).
|
|
58
|
+
*/
|
|
59
|
+
export const GATE_OUTBOX_STRAY_MS = 10 * 60 * 1000;
|
|
60
|
+
|
|
61
|
+
export interface QueuedGateEvent {
|
|
62
|
+
/** Unique per event; the server records each id once. */
|
|
63
|
+
id: string;
|
|
64
|
+
tool: string;
|
|
65
|
+
mode: string;
|
|
66
|
+
outcome: string;
|
|
67
|
+
/** Epoch ms when the gate fired; the server stores it as the event's time. */
|
|
68
|
+
at: number;
|
|
69
|
+
host?: string;
|
|
70
|
+
cwd?: string;
|
|
71
|
+
projectId?: string;
|
|
72
|
+
projectRoot?: string;
|
|
73
|
+
actionObservation?: unknown;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function write(folder: string, event: QueuedGateEvent): void {
|
|
77
|
+
mkdirSync(folder, { recursive: true, mode: 0o700 });
|
|
78
|
+
const name = `${String(event.at).padStart(15, '0')}-${event.id}.json`;
|
|
79
|
+
const temporary = join(folder, `.${name}.tmp`);
|
|
80
|
+
writeFileSync(temporary, JSON.stringify(event), { mode: 0o600 });
|
|
81
|
+
renameSync(temporary, join(folder, name));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const unlinkQuietly = (path: string): void => {
|
|
85
|
+
try {
|
|
86
|
+
unlinkSync(path);
|
|
87
|
+
} catch {
|
|
88
|
+
// Already gone.
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** Event file names, oldest first (names start with the zero-padded time). */
|
|
93
|
+
function queuedNames(folder: string): string[] {
|
|
94
|
+
return readdirSync(folder)
|
|
95
|
+
.filter((name) => name.endsWith('.json') && !name.startsWith('.'))
|
|
96
|
+
.sort();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Drop the oldest events past GATE_OUTBOX_CAP. */
|
|
100
|
+
function enforceCap(folder: string): void {
|
|
101
|
+
try {
|
|
102
|
+
const names = queuedNames(folder);
|
|
103
|
+
for (const name of names.slice(0, Math.max(0, names.length - GATE_OUTBOX_CAP)))
|
|
104
|
+
unlinkQuietly(join(folder, name));
|
|
105
|
+
} catch {
|
|
106
|
+
// Telemetry.
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function queueGateEvent(folder: string, event: Omit<QueuedGateEvent, 'id'>): boolean {
|
|
111
|
+
try {
|
|
112
|
+
write(folder, { ...event, id: randomUUID() });
|
|
113
|
+
enforceCap(folder);
|
|
114
|
+
return true;
|
|
115
|
+
} catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Claim every queued event of this session, oldest first, at most
|
|
122
|
+
* GATE_OUTBOX_MAX_EVENTS (the rest wait for the next request).
|
|
123
|
+
*/
|
|
124
|
+
export function takeGateEvents(folder: string, now: number = Date.now()): QueuedGateEvent[] {
|
|
125
|
+
let entries: string[];
|
|
126
|
+
try {
|
|
127
|
+
entries = readdirSync(folder);
|
|
128
|
+
} catch {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
// Sweep what killed hook processes left between renames.
|
|
132
|
+
for (const name of entries) {
|
|
133
|
+
if (!name.startsWith('.') || !(name.endsWith('.tmp') || name.endsWith('.claimed'))) continue;
|
|
134
|
+
const path = join(folder, name);
|
|
135
|
+
try {
|
|
136
|
+
if (now - statSync(path).mtimeMs > GATE_OUTBOX_STRAY_MS) unlinkQuietly(path);
|
|
137
|
+
} catch {
|
|
138
|
+
// Gone meanwhile.
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const names = entries.filter((name) => name.endsWith('.json') && !name.startsWith('.')).sort();
|
|
142
|
+
const events: QueuedGateEvent[] = [];
|
|
143
|
+
for (const name of names) {
|
|
144
|
+
if (events.length >= GATE_OUTBOX_MAX_EVENTS) break;
|
|
145
|
+
const claimed = join(folder, `.${name}.${process.pid}.claimed`);
|
|
146
|
+
try {
|
|
147
|
+
renameSync(join(folder, name), claimed);
|
|
148
|
+
} catch {
|
|
149
|
+
continue; // Another hook process of this session took it.
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
const event = JSON.parse(readFileSync(claimed, 'utf8')) as QueuedGateEvent;
|
|
153
|
+
if (
|
|
154
|
+
typeof event?.id === 'string' &&
|
|
155
|
+
typeof event.tool === 'string' &&
|
|
156
|
+
typeof event.mode === 'string' &&
|
|
157
|
+
typeof event.outcome === 'string' &&
|
|
158
|
+
typeof event.at === 'number' &&
|
|
159
|
+
now - event.at <= GATE_OUTBOX_MAX_AGE_MS
|
|
160
|
+
)
|
|
161
|
+
events.push(event);
|
|
162
|
+
} catch {
|
|
163
|
+
// Unreadable; dropped.
|
|
164
|
+
} finally {
|
|
165
|
+
unlinkQuietly(claimed);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return events;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Put back events a request could not deliver, so the next request carries them. */
|
|
172
|
+
export function returnGateEvents(folder: string, events: readonly QueuedGateEvent[]): void {
|
|
173
|
+
for (const event of events) {
|
|
174
|
+
try {
|
|
175
|
+
write(folder, event);
|
|
176
|
+
} catch {
|
|
177
|
+
// Lost; telemetry.
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
enforceCap(folder);
|
|
181
|
+
}
|
package/src/hookConversation.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { constants } from 'node:fs';
|
|
|
3
3
|
import { lstat, mkdir, open, rename, rm, utimes } from 'node:fs/promises';
|
|
4
4
|
import { dirname } from 'node:path';
|
|
5
5
|
import { withFileLock } from './settingsIo.js';
|
|
6
|
+
import { HOOK_REQUEST_ID_HEADER } from './hookTimeouts.js';
|
|
6
7
|
import { ensureWindowsPrivateDirectorySync } from './runtimeSigners.js';
|
|
7
8
|
|
|
8
9
|
type HookFetchInit = NonNullable<Parameters<typeof fetch>[1]>;
|
|
@@ -27,6 +28,15 @@ function boundedHandle(value: unknown): value is string {
|
|
|
27
28
|
|
|
28
29
|
export class HookConversation {
|
|
29
30
|
private conversationId?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The hook call (x-mnemonik-hook-request-id) whose acquiring request holds
|
|
33
|
+
* the conversation lock in this process. Its hedged second request
|
|
34
|
+
* (hedgedHookRequest) is the same call: waiting for the lock would hold it
|
|
35
|
+
* until the first request finished, which is exactly what the hedge exists
|
|
36
|
+
* to route around. The server hands both requests the same answer, so the
|
|
37
|
+
* second may save the handle without the lock.
|
|
38
|
+
*/
|
|
39
|
+
private lockHeldFor?: string;
|
|
30
40
|
private readonly scope: string;
|
|
31
41
|
constructor(private readonly input: HookConversationInput) {
|
|
32
42
|
this.scope = createHash('sha256')
|
|
@@ -185,7 +195,20 @@ export class HookConversation {
|
|
|
185
195
|
} else if (this.conversationId) await this.save(this.conversationId);
|
|
186
196
|
return response;
|
|
187
197
|
};
|
|
188
|
-
const
|
|
198
|
+
const callId = hookCallId(init.headers);
|
|
199
|
+
let response: Response;
|
|
200
|
+
if (!acquisition || (callId !== undefined && this.lockHeldFor === callId)) {
|
|
201
|
+
response = await send();
|
|
202
|
+
} else {
|
|
203
|
+
response = await this.locked(async () => {
|
|
204
|
+
this.lockHeldFor = callId;
|
|
205
|
+
try {
|
|
206
|
+
return await send();
|
|
207
|
+
} finally {
|
|
208
|
+
if (this.lockHeldFor === callId) this.lockHeldFor = undefined;
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
189
212
|
// The server refused the cached handle (a signing-secret rotation, say).
|
|
190
213
|
// Forget it so the next acquiring event mints a fresh one; this request
|
|
191
214
|
// is never resent, and a handle another event acquired meanwhile stays.
|
|
@@ -263,6 +286,14 @@ export function conversationMarker(): string | undefined {
|
|
|
263
286
|
export async function adoptHookConversation(result: unknown): Promise<void> {
|
|
264
287
|
await active?.adopt(result);
|
|
265
288
|
}
|
|
289
|
+
function hookCallId(headers: HookFetchInit['headers']): string | undefined {
|
|
290
|
+
try {
|
|
291
|
+
return new Headers(headers).get(HOOK_REQUEST_ID_HEADER) ?? undefined;
|
|
292
|
+
} catch {
|
|
293
|
+
return undefined;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
266
297
|
export function withHookConversation(
|
|
267
298
|
url: string,
|
|
268
299
|
init: HookFetchInit,
|
package/src/hookRuntime.ts
CHANGED