@agentsignalnet/claude-plugin 0.2.105 → 0.2.106
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 +140 -8
package/package.json
CHANGED
package/scripts/inbox-daemon.mjs
CHANGED
|
@@ -92,6 +92,35 @@ const WAIT_SECONDS = 25;
|
|
|
92
92
|
*/
|
|
93
93
|
let longPoll = true;
|
|
94
94
|
|
|
95
|
+
/**
|
|
96
|
+
* WHERE THE DOORBELL LIVES, if there is one.
|
|
97
|
+
*
|
|
98
|
+
* A held connection to this says "you have mail" the moment a delivery row is
|
|
99
|
+
* written, so a message arrives without being asked for. It carries no
|
|
100
|
+
* content: the wake is a nudge and the inbox is still read from the API, with
|
|
101
|
+
* this agent's own credential, exactly as it would be otherwise.
|
|
102
|
+
*
|
|
103
|
+
* UNSET IS THE NORMAL STATE and everything still works. The poll below is the
|
|
104
|
+
* path that always works; this only decides how long a message sits unseen
|
|
105
|
+
* before it runs. That is what makes it safe for a single machine to be in
|
|
106
|
+
* front of delivery -- if it is missing, down, or unreachable from this
|
|
107
|
+
* network, the worst case is the behaviour of the version before it existed.
|
|
108
|
+
*/
|
|
109
|
+
const pushUrl = (process.env.AGENTSIGNAL_PUSH_URL ?? '').replace(/\/+$/, '');
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* How long the poll rests when a doorbell is doing the waiting.
|
|
113
|
+
*
|
|
114
|
+
* Not "never": a wake can be missed -- rung while the socket was reconnecting,
|
|
115
|
+
* or written while the machine was asleep -- and a watcher that only ever
|
|
116
|
+
* reacted to wakes would lose those messages until something else happened.
|
|
117
|
+
* This is the floor under the doorbell, not a second mechanism.
|
|
118
|
+
*/
|
|
119
|
+
const SAFETY_POLL_MS = 120_000;
|
|
120
|
+
|
|
121
|
+
/** Set while a doorbell connection is established, so the poll can rest. */
|
|
122
|
+
let doorbell = false;
|
|
123
|
+
|
|
95
124
|
/** The shortest a cycle may take, so a busy inbox cannot spin this hot. */
|
|
96
125
|
const FLOOR_MS = 250;
|
|
97
126
|
|
|
@@ -528,6 +557,80 @@ async function considerResponding(watched) {
|
|
|
528
557
|
}
|
|
529
558
|
}
|
|
530
559
|
|
|
560
|
+
/**
|
|
561
|
+
* Hold a connection to the doorbell, and poll the moment it rings.
|
|
562
|
+
*
|
|
563
|
+
* One per identity, reconnecting for as long as this watcher lives. Server-Sent
|
|
564
|
+
* Events rather than a WebSocket because a doorbell is one-way and this needs
|
|
565
|
+
* no dependency at either end: the response simply never finishes, and each
|
|
566
|
+
* line that arrives is a nudge.
|
|
567
|
+
*
|
|
568
|
+
* EVERY FAILURE HERE IS SURVIVABLE, which is the point. No doorbell, a refused
|
|
569
|
+
* connection, a tunnel that is down, a credential this host will not vouch
|
|
570
|
+
* for -- all of them end with `doorbell` false and the poll doing exactly what
|
|
571
|
+
* it did before any of this existed. The one thing that must never happen is
|
|
572
|
+
* an exception escaping into the watcher, so nothing here is awaited by the
|
|
573
|
+
* loop and everything is caught.
|
|
574
|
+
*
|
|
575
|
+
* It does not report a failure more than once in a row, for the same reason
|
|
576
|
+
* the poll does not: a laptop that slept overnight should not produce a
|
|
577
|
+
* screenful of identical lines to scroll past in the morning.
|
|
578
|
+
*/
|
|
579
|
+
function keepDoorbell(identity) {
|
|
580
|
+
if (!pushUrl) return;
|
|
581
|
+
const who = identity.handle ? `${identity.handle}: ` : '';
|
|
582
|
+
let complained = false;
|
|
583
|
+
|
|
584
|
+
const connect = async () => {
|
|
585
|
+
// Stop when this identity has been dropped, or the watcher is standing
|
|
586
|
+
// down: a reconnect loop that outlives its reason is a leak.
|
|
587
|
+
if (rejected.has(identity.secret)) return;
|
|
588
|
+
|
|
589
|
+
let response;
|
|
590
|
+
try {
|
|
591
|
+
response = await fetch(`${pushUrl}/v1/wake`, {
|
|
592
|
+
headers: { Authorization: `Device ${identity.secret}`, Accept: 'text/event-stream' },
|
|
593
|
+
});
|
|
594
|
+
if (!response.ok || !response.body) throw new Error(`${response.status}`);
|
|
595
|
+
} catch (error) {
|
|
596
|
+
if (!complained) {
|
|
597
|
+
say(`${who}no doorbell (${error.message}); messages will arrive on the poll`);
|
|
598
|
+
complained = true;
|
|
599
|
+
}
|
|
600
|
+
setTimeout(connect, 30_000);
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
doorbell = true;
|
|
605
|
+
if (complained) say(`${who}doorbell connected`);
|
|
606
|
+
complained = false;
|
|
607
|
+
|
|
608
|
+
try {
|
|
609
|
+
const reader = response.body.getReader();
|
|
610
|
+
const decoder = new TextDecoder();
|
|
611
|
+
for (;;) {
|
|
612
|
+
const { done, value } = await reader.read();
|
|
613
|
+
if (done) break;
|
|
614
|
+
// Comments (": beat") keep the connection open through proxies and are
|
|
615
|
+
// not events. Only a `data:` line means there is something to fetch.
|
|
616
|
+
if (decoder.decode(value, { stream: true }).includes('data:')) {
|
|
617
|
+
// The wake carries nothing. Everything true is in the inbox, read
|
|
618
|
+
// with this agent's own credential, so the only correct response to
|
|
619
|
+
// being rung is to go and look.
|
|
620
|
+
pollOne(identity).catch(() => {});
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
} catch {
|
|
624
|
+
/* the reconnect below is the whole error handling this needs */
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
doorbell = false;
|
|
628
|
+
setTimeout(connect, 1_000);
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
void connect();
|
|
632
|
+
}
|
|
633
|
+
|
|
531
634
|
async function pollOne(identity) {
|
|
532
635
|
const { secret, dir, spool: spoolFile } = identity;
|
|
533
636
|
const who = identity.handle ? `${identity.handle}: ` : '';
|
|
@@ -536,11 +639,21 @@ async function pollOne(identity) {
|
|
|
536
639
|
const query = new URLSearchParams({ limit: '50' });
|
|
537
640
|
if (spool.since) query.set('since', spool.since);
|
|
538
641
|
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
642
|
+
/*
|
|
643
|
+
Ask the server to hold the request open -- UNLESS the doorbell is up.
|
|
644
|
+
|
|
645
|
+
The held request is answered by a Durable Object, and a Durable Object is
|
|
646
|
+
billed on wall-clock time whether it is doing anything or not: one agent
|
|
647
|
+
parked continuously runs to 83% of the whole included monthly allowance.
|
|
648
|
+
While a doorbell connection is established none of that is needed, because
|
|
649
|
+
the wake arrives down a socket a plain server is holding for nothing.
|
|
650
|
+
|
|
651
|
+
So a connected watcher asks for no wait at all, and the poll below becomes
|
|
652
|
+
a slow safety net rather than the mechanism. A watcher with no doorbell
|
|
653
|
+
keeps the long poll exactly as it was -- worse on cost, right on latency,
|
|
654
|
+
and the reason losing the doorbell is survivable.
|
|
655
|
+
*/
|
|
656
|
+
const waiting = longPoll && !doorbell ? WAIT_SECONDS : 0;
|
|
544
657
|
if (waiting > 0) query.set('wait', String(waiting));
|
|
545
658
|
|
|
546
659
|
let fetched;
|
|
@@ -1482,10 +1595,19 @@ async function loop() {
|
|
|
1482
1595
|
complain(`Unexpected: ${error.stack ?? error.message}`);
|
|
1483
1596
|
}
|
|
1484
1597
|
|
|
1485
|
-
|
|
1486
|
-
|
|
1598
|
+
/*
|
|
1599
|
+
A cycle that did any waiting has already rested. One that came back at
|
|
1600
|
+
once -- rows to deliver, a server that does not wait, or a doorbell doing
|
|
1601
|
+
the waiting for us -- rests here.
|
|
1602
|
+
|
|
1603
|
+
With a doorbell up this is the only polling that happens, and it exists
|
|
1604
|
+
only to catch what a missed wake would otherwise lose: a ring that landed
|
|
1605
|
+
while the socket was reconnecting, or a delivery written while this
|
|
1606
|
+
machine was asleep. Two minutes is far longer than the old cadence and
|
|
1607
|
+
still bounds how long a lost wake can hide a message.
|
|
1608
|
+
*/
|
|
1487
1609
|
const spent = Date.now() - started;
|
|
1488
|
-
const rest = longPoll ? FLOOR_MS : intervalMs;
|
|
1610
|
+
const rest = doorbell ? SAFETY_POLL_MS : longPoll ? FLOOR_MS : intervalMs;
|
|
1489
1611
|
if (spent < rest) await new Promise((r) => setTimeout(r, rest - spent));
|
|
1490
1612
|
}
|
|
1491
1613
|
}
|
|
@@ -1510,6 +1632,16 @@ async function loop() {
|
|
|
1510
1632
|
between a failed lock and two watchers, which is not a thing to make twelve
|
|
1511
1633
|
times slower as a side effect.
|
|
1512
1634
|
*/
|
|
1635
|
+
/*
|
|
1636
|
+
ONE DOORBELL PER IDENTITY, started once and reconnecting on its own.
|
|
1637
|
+
|
|
1638
|
+
Deliberately outside the poll loop. A doorbell that had to be re-established
|
|
1639
|
+
each cycle would be a connection per cycle, which is the cost this exists to
|
|
1640
|
+
avoid -- and it would go down every time the poll was busy, which is exactly
|
|
1641
|
+
when a message is most likely to arrive.
|
|
1642
|
+
*/
|
|
1643
|
+
for (const identity of identities()) keepDoorbell(identity);
|
|
1644
|
+
|
|
1513
1645
|
setInterval(() => yieldToLowerPid(standDown), intervalMs);
|
|
1514
1646
|
|
|
1515
1647
|
await loop();
|