@agentsignalnet/claude-plugin 0.2.93 → 0.2.95
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/.claude-plugin/plugin.json +1 -1
- package/package.json +1 -1
- package/scripts/inbox-daemon.mjs +125 -13
package/package.json
CHANGED
package/scripts/inbox-daemon.mjs
CHANGED
|
@@ -73,6 +73,28 @@ const complain = (...parts) => console.error(stamp('ERROR', ...parts));
|
|
|
73
73
|
const baseUrl = apiBase();
|
|
74
74
|
const intervalMs = Math.max(1, Number(flag('interval', '2'))) * 1000;
|
|
75
75
|
|
|
76
|
+
/**
|
|
77
|
+
* How long the server is asked to hold a request open.
|
|
78
|
+
*
|
|
79
|
+
* Twenty-five seconds rather than the five minutes the API would allow: idle
|
|
80
|
+
* connections get cut by proxies and load balancers long before that, and a
|
|
81
|
+
* connection reaped mid-wait is indistinguishable here from a quiet channel.
|
|
82
|
+
* At twenty-five it is about 3,500 requests an identity a day instead of
|
|
83
|
+
* 43,200, and the ceiling that matters -- how fast a message arrives -- is set
|
|
84
|
+
* by the doorbell, not by this.
|
|
85
|
+
*/
|
|
86
|
+
const WAIT_SECONDS = 25;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Whether this server holds requests open. Latched off the first time one
|
|
90
|
+
* answers an empty inbox instantly; see pollOne for why that has to be
|
|
91
|
+
* detected rather than assumed.
|
|
92
|
+
*/
|
|
93
|
+
let longPoll = true;
|
|
94
|
+
|
|
95
|
+
/** The shortest a cycle may take, so a busy inbox cannot spin this hot. */
|
|
96
|
+
const FLOOR_MS = 250;
|
|
97
|
+
|
|
76
98
|
/**
|
|
77
99
|
* Where the identity lives -- ALL the places, matching the MCP server exactly.
|
|
78
100
|
*
|
|
@@ -263,10 +285,22 @@ async function tick() {
|
|
|
263
285
|
process.exit(1);
|
|
264
286
|
}
|
|
265
287
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
288
|
+
/*
|
|
289
|
+
CONCURRENT ONCE THE REQUESTS ARE HELD OPEN, which reverses what this said.
|
|
290
|
+
|
|
291
|
+
Sequential was right when each poll was one small request that returned at
|
|
292
|
+
once: a handful of them cost a few hundred milliseconds, and a burst of
|
|
293
|
+
parallel fetches off a laptop waking from sleep is a worse neighbour. None
|
|
294
|
+
of that survives the request being HELD. Waiting on identities one after
|
|
295
|
+
another makes the second one's messages arrive up to twenty-five seconds
|
|
296
|
+
late, the third fifty, and turns a latency fix into a latency bug that gets
|
|
297
|
+
worse the more agents a machine runs.
|
|
298
|
+
|
|
299
|
+
The neighbour argument does not carry over either. These are idle held
|
|
300
|
+
connections, not a burst of work -- a handful of them cost nothing while
|
|
301
|
+
they wait, which is nearly all of the time.
|
|
302
|
+
*/
|
|
303
|
+
await Promise.all(watched.map((identity) => pollOne(identity)));
|
|
270
304
|
|
|
271
305
|
// AFTER polling, not during: the question is "did a live session take this",
|
|
272
306
|
// and the grace period has to elapse before that has an answer.
|
|
@@ -347,10 +381,22 @@ async function pollOne(identity) {
|
|
|
347
381
|
const query = new URLSearchParams({ limit: '50' });
|
|
348
382
|
if (spool.since) query.set('since', spool.since);
|
|
349
383
|
|
|
384
|
+
// Ask the server to hold the request open. It answers the moment a delivery
|
|
385
|
+
// row is written -- see the doorbell in dispatch.ts -- so this is not a
|
|
386
|
+
// slower poll, it is the same answer arriving sooner and roughly twelve
|
|
387
|
+
// times fewer requests to get it.
|
|
388
|
+
const waiting = longPoll ? WAIT_SECONDS : 0;
|
|
389
|
+
if (waiting > 0) query.set('wait', String(waiting));
|
|
390
|
+
|
|
350
391
|
let fetched;
|
|
351
392
|
try {
|
|
352
393
|
const response = await fetch(`${baseUrl}/v1/devices/me/messages?${query}`, {
|
|
353
394
|
headers: { Authorization: `Device ${secret}` },
|
|
395
|
+
// A held request that is never answered would stall this identity
|
|
396
|
+
// forever, and a watcher that has stopped watching looks exactly like a
|
|
397
|
+
// quiet channel. Ten seconds past the server's own ceiling: late enough
|
|
398
|
+
// never to cut off a legitimate wait, early enough to notice a dead one.
|
|
399
|
+
signal: AbortSignal.timeout((waiting || 30) * 1_000 + 10_000),
|
|
354
400
|
});
|
|
355
401
|
if (!response.ok) {
|
|
356
402
|
// 401 will not fix itself: the credential was revoked or the agent was
|
|
@@ -370,7 +416,26 @@ async function pollOne(identity) {
|
|
|
370
416
|
}
|
|
371
417
|
throw new Error(`${response.status}`);
|
|
372
418
|
}
|
|
373
|
-
|
|
419
|
+
const payload = await response.json();
|
|
420
|
+
fetched = payload.messages ?? [];
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* DOES THIS SERVER HOLD THE REQUEST OPEN?
|
|
424
|
+
*
|
|
425
|
+
* It matters more than it looks. The loop below has no timer of its own any
|
|
426
|
+
* more -- the wait IS the delay -- so against an older API that answers an
|
|
427
|
+
* empty inbox instantly, it would spin at the floor instead of resting for
|
|
428
|
+
* twenty-five seconds. That is not a degraded fallback, it is several times
|
|
429
|
+
* MORE traffic than the polling this replaced.
|
|
430
|
+
*
|
|
431
|
+
* `waited_ms` is the tell, and only on an empty answer: a server that
|
|
432
|
+
* waited says how long, and one that returned rows was right to answer at
|
|
433
|
+
* once. Asked for once, latched off, and the interval floor takes over.
|
|
434
|
+
*/
|
|
435
|
+
if (waiting > 0 && fetched.length === 0 && payload.waited_ms === undefined) {
|
|
436
|
+
longPoll = false;
|
|
437
|
+
say('this server answers immediately; falling back to polling on a timer');
|
|
438
|
+
}
|
|
374
439
|
// Say when it comes back, not only when it breaks. A log that records the
|
|
375
440
|
// start of an outage and never its end cannot tell you whether the thing
|
|
376
441
|
// is working now, which is the only question anyone reads it to answer.
|
|
@@ -1012,18 +1077,24 @@ function standDownEarly() {
|
|
|
1012
1077
|
*
|
|
1013
1078
|
* Only among rivals sharing this mtime. A genuinely newer build has already
|
|
1014
1079
|
* been dealt with above and must not be stood down to on a pid comparison.
|
|
1080
|
+
*
|
|
1081
|
+
* Called at startup AND on every tick, because one look is not enough: see the
|
|
1082
|
+
* note at the interval below for the interleaving a single look loses.
|
|
1015
1083
|
*/
|
|
1016
|
-
|
|
1017
|
-
const
|
|
1018
|
-
|
|
1084
|
+
function yieldToLowerPid(exit) {
|
|
1085
|
+
const twins = livingWatchers(lockRoot).filter((rival) => rival.mtime === me.mtime);
|
|
1086
|
+
const usurper = twins.find((rival) => rival.pid < process.pid);
|
|
1087
|
+
if (!usurper) return false;
|
|
1019
1088
|
say(
|
|
1020
1089
|
`pid ${usurper.pid} is watching the same build; standing down ` +
|
|
1021
1090
|
'(the startup lock did not serialise this, which is a separate bug)',
|
|
1022
1091
|
);
|
|
1023
|
-
|
|
1092
|
+
exit();
|
|
1024
1093
|
process.exit(0);
|
|
1025
1094
|
}
|
|
1026
1095
|
|
|
1096
|
+
yieldToLowerPid(standDownEarly);
|
|
1097
|
+
|
|
1027
1098
|
// Best effort, and correctness does not depend on it -- a start that finds
|
|
1028
1099
|
// this entry checks the pid before believing it. It is here so an ordinary
|
|
1029
1100
|
// stop leaves nothing behind, rather than leaving the next start to reason
|
|
@@ -1062,7 +1133,48 @@ for (const identity of identities()) {
|
|
|
1062
1133
|
}
|
|
1063
1134
|
}
|
|
1064
1135
|
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1136
|
+
/**
|
|
1137
|
+
* SELF-SCHEDULING, because the wait is now the delay.
|
|
1138
|
+
*
|
|
1139
|
+
* `setInterval` was right when a tick was a request that returned at once and
|
|
1140
|
+
* the timer decided when to ask again. With the request held open, a timer
|
|
1141
|
+
* would either fire underneath a wait that had not finished -- stacking a
|
|
1142
|
+
* second request on the first every two seconds -- or, if it did not, be doing
|
|
1143
|
+
* nothing that the wait was not already doing better. So the cycle waits for
|
|
1144
|
+
* itself: poll, and when the poll comes back, poll again.
|
|
1145
|
+
*
|
|
1146
|
+
* The FLOOR is the part that is not obvious. Nothing here rests when messages
|
|
1147
|
+
* are arriving, because a poll that returns rows returns immediately -- so a
|
|
1148
|
+
* busy inbox, or a server that turns out not to hold requests open at all,
|
|
1149
|
+
* would spin as fast as the network allows. `longPoll` catches the second
|
|
1150
|
+
* case; the floor catches the first, and costs nothing when the wait is doing
|
|
1151
|
+
* its job.
|
|
1152
|
+
*/
|
|
1153
|
+
async function loop() {
|
|
1154
|
+
for (;;) {
|
|
1155
|
+
// Reconcile on every cycle, not only at startup. Registering and looking
|
|
1156
|
+
// are two steps, so a one-shot check at startup loses a race it is
|
|
1157
|
+
// supposed to settle: if the higher pid registers first and looks before
|
|
1158
|
+
// the lower one has registered, it sees an empty registry and stays -- and
|
|
1159
|
+
// the lower pid, looking afterwards, is the one entitled to stay. Both
|
|
1160
|
+
// keep watching, which is the exact duplicate this check exists to
|
|
1161
|
+
// prevent. Looking again each cycle converges whatever the order was,
|
|
1162
|
+
// because the late registration is visible by the next one.
|
|
1163
|
+
if (yieldToLowerPid(standDown)) return;
|
|
1164
|
+
|
|
1165
|
+
const started = Date.now();
|
|
1166
|
+
try {
|
|
1167
|
+
await tick();
|
|
1168
|
+
} catch (error) {
|
|
1169
|
+
complain(`Unexpected: ${error.stack ?? error.message}`);
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
// A cycle that did any waiting has already rested. One that came back at
|
|
1173
|
+
// once -- rows to deliver, or a server that does not wait -- rests here.
|
|
1174
|
+
const spent = Date.now() - started;
|
|
1175
|
+
const rest = longPoll ? FLOOR_MS : intervalMs;
|
|
1176
|
+
if (spent < rest) await new Promise((r) => setTimeout(r, rest - spent));
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
await loop();
|