@agentsignalnet/claude-plugin 0.2.94 → 0.2.96

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "agentsignal",
3
3
  "displayName": "AgentSignal",
4
- "version": "0.2.94",
4
+ "version": "0.2.96",
5
5
  "description": "Page a human from inside Claude Code, or become an addressable agent that other agents can reach.",
6
6
  "author": {
7
7
  "name": "Data Group AI",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentsignalnet/claude-plugin",
3
- "version": "0.2.94",
3
+ "version": "0.2.96",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -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
- // Sequential rather than concurrent. There are a handful of these at most,
267
- // each is one small request, and a burst of parallel fetches off a laptop
268
- // waking from sleep is a worse neighbour than a few hundred milliseconds.
269
- for (const identity of watched) await pollOne(identity);
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
- fetched = (await response.json()).messages ?? [];
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.
@@ -1056,7 +1121,12 @@ sweepOldVersions();
1056
1121
 
1057
1122
  const watching = identities();
1058
1123
  say(
1059
- `started (pid ${process.pid}) watching ${baseUrl} every ${intervalMs / 1000}s; ` +
1124
+ // Says what it actually does. "every 2s" was true of the poll loop and is
1125
+ // not true of a held request -- and a log line that describes a cadence the
1126
+ // process no longer has is how somebody later concludes the wait is broken
1127
+ // because the requests are not there.
1128
+ `started (pid ${process.pid}) watching ${baseUrl} ` +
1129
+ (longPoll ? `(waiting up to ${WAIT_SECONDS}s per request); ` : `every ${intervalMs / 1000}s; `) +
1060
1130
  `${watching.length} identit${watching.length === 1 ? 'y' : 'ies'}: ` +
1061
1131
  watching.map((i) => i.spool).join(', '),
1062
1132
  );
@@ -1068,16 +1138,48 @@ for (const identity of identities()) {
1068
1138
  }
1069
1139
  }
1070
1140
 
1071
- await tick();
1072
- setInterval(() => {
1073
- // Reconcile on every tick, not only at startup. Registering and looking are
1074
- // two steps, so a one-shot check at startup loses a race it is supposed to
1075
- // settle: if the higher pid registers first and looks before the lower one
1076
- // has registered, it sees an empty registry and stays -- and the lower pid,
1077
- // looking afterwards, is the one entitled to stay. Both keep watching, which
1078
- // is the exact duplicate this check exists to prevent. Looking again each
1079
- // tick converges whatever the order was, because the late registration is
1080
- // visible by the next one.
1081
- if (yieldToLowerPid(standDown)) return;
1082
- tick().catch((error) => complain(`Unexpected: ${error.stack ?? error.message}`));
1083
- }, intervalMs);
1141
+ /**
1142
+ * SELF-SCHEDULING, because the wait is now the delay.
1143
+ *
1144
+ * `setInterval` was right when a tick was a request that returned at once and
1145
+ * the timer decided when to ask again. With the request held open, a timer
1146
+ * would either fire underneath a wait that had not finished -- stacking a
1147
+ * second request on the first every two seconds -- or, if it did not, be doing
1148
+ * nothing that the wait was not already doing better. So the cycle waits for
1149
+ * itself: poll, and when the poll comes back, poll again.
1150
+ *
1151
+ * The FLOOR is the part that is not obvious. Nothing here rests when messages
1152
+ * are arriving, because a poll that returns rows returns immediately -- so a
1153
+ * busy inbox, or a server that turns out not to hold requests open at all,
1154
+ * would spin as fast as the network allows. `longPoll` catches the second
1155
+ * case; the floor catches the first, and costs nothing when the wait is doing
1156
+ * its job.
1157
+ */
1158
+ async function loop() {
1159
+ for (;;) {
1160
+ // Reconcile on every cycle, not only at startup. Registering and looking
1161
+ // are two steps, so a one-shot check at startup loses a race it is
1162
+ // supposed to settle: if the higher pid registers first and looks before
1163
+ // the lower one has registered, it sees an empty registry and stays -- and
1164
+ // the lower pid, looking afterwards, is the one entitled to stay. Both
1165
+ // keep watching, which is the exact duplicate this check exists to
1166
+ // prevent. Looking again each cycle converges whatever the order was,
1167
+ // because the late registration is visible by the next one.
1168
+ if (yieldToLowerPid(standDown)) return;
1169
+
1170
+ const started = Date.now();
1171
+ try {
1172
+ await tick();
1173
+ } catch (error) {
1174
+ complain(`Unexpected: ${error.stack ?? error.message}`);
1175
+ }
1176
+
1177
+ // A cycle that did any waiting has already rested. One that came back at
1178
+ // once -- rows to deliver, or a server that does not wait -- rests here.
1179
+ const spent = Date.now() - started;
1180
+ const rest = longPoll ? FLOOR_MS : intervalMs;
1181
+ if (spent < rest) await new Promise((r) => setTimeout(r, rest - spent));
1182
+ }
1183
+ }
1184
+
1185
+ await loop();