@agentsignalnet/claude-plugin 0.2.105 → 0.2.107
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 +182 -8
package/package.json
CHANGED
package/scripts/inbox-daemon.mjs
CHANGED
|
@@ -92,6 +92,66 @@ 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 pushOverride = (process.env.AGENTSIGNAL_PUSH_URL ?? '').replace(/\/+$/, '');
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Where the doorbell is, ASKED FOR rather than configured.
|
|
113
|
+
*
|
|
114
|
+
* The API answers this on /v1/devices/me, so moving the doorbell or switching
|
|
115
|
+
* it off is a Worker secret rather than a plugin release -- unset it there and
|
|
116
|
+
* every watcher falls back to polling on its next reconnect, with nothing to
|
|
117
|
+
* install. A version that had to be told at install time could only learn a
|
|
118
|
+
* new address by being reinstalled, which is the wrong shape for a thing that
|
|
119
|
+
* is deliberately allowed to be absent.
|
|
120
|
+
*
|
|
121
|
+
* Asked on every reconnect, not once: a doorbell turned on this afternoon
|
|
122
|
+
* should be found by a watcher that started this morning.
|
|
123
|
+
*
|
|
124
|
+
* The environment variable stays as an override, for tests and for pointing a
|
|
125
|
+
* watcher at a doorbell of your own.
|
|
126
|
+
*/
|
|
127
|
+
async function findDoorbell(identity) {
|
|
128
|
+
if (pushOverride) return pushOverride;
|
|
129
|
+
try {
|
|
130
|
+
const response = await fetch(`${baseUrl}/v1/devices/me`, {
|
|
131
|
+
headers: { Authorization: `Device ${identity.secret}` },
|
|
132
|
+
signal: AbortSignal.timeout(10_000),
|
|
133
|
+
});
|
|
134
|
+
if (!response.ok) return '';
|
|
135
|
+
const body = await response.json();
|
|
136
|
+
return typeof body.push_url === 'string' ? body.push_url.replace(/\/+$/, '') : '';
|
|
137
|
+
} catch {
|
|
138
|
+
return '';
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* How long the poll rests when a doorbell is doing the waiting.
|
|
144
|
+
*
|
|
145
|
+
* Not "never": a wake can be missed -- rung while the socket was reconnecting,
|
|
146
|
+
* or written while the machine was asleep -- and a watcher that only ever
|
|
147
|
+
* reacted to wakes would lose those messages until something else happened.
|
|
148
|
+
* This is the floor under the doorbell, not a second mechanism.
|
|
149
|
+
*/
|
|
150
|
+
const SAFETY_POLL_MS = 120_000;
|
|
151
|
+
|
|
152
|
+
/** Set while a doorbell connection is established, so the poll can rest. */
|
|
153
|
+
let doorbell = false;
|
|
154
|
+
|
|
95
155
|
/** The shortest a cycle may take, so a busy inbox cannot spin this hot. */
|
|
96
156
|
const FLOOR_MS = 250;
|
|
97
157
|
|
|
@@ -528,6 +588,91 @@ async function considerResponding(watched) {
|
|
|
528
588
|
}
|
|
529
589
|
}
|
|
530
590
|
|
|
591
|
+
/**
|
|
592
|
+
* Hold a connection to the doorbell, and poll the moment it rings.
|
|
593
|
+
*
|
|
594
|
+
* One per identity, reconnecting for as long as this watcher lives. Server-Sent
|
|
595
|
+
* Events rather than a WebSocket because a doorbell is one-way and this needs
|
|
596
|
+
* no dependency at either end: the response simply never finishes, and each
|
|
597
|
+
* line that arrives is a nudge.
|
|
598
|
+
*
|
|
599
|
+
* EVERY FAILURE HERE IS SURVIVABLE, which is the point. No doorbell, a refused
|
|
600
|
+
* connection, a tunnel that is down, a credential this host will not vouch
|
|
601
|
+
* for -- all of them end with `doorbell` false and the poll doing exactly what
|
|
602
|
+
* it did before any of this existed. The one thing that must never happen is
|
|
603
|
+
* an exception escaping into the watcher, so nothing here is awaited by the
|
|
604
|
+
* loop and everything is caught.
|
|
605
|
+
*
|
|
606
|
+
* It does not report a failure more than once in a row, for the same reason
|
|
607
|
+
* the poll does not: a laptop that slept overnight should not produce a
|
|
608
|
+
* screenful of identical lines to scroll past in the morning.
|
|
609
|
+
*/
|
|
610
|
+
function keepDoorbell(identity) {
|
|
611
|
+
const who = identity.handle ? `${identity.handle}: ` : '';
|
|
612
|
+
let complained = false;
|
|
613
|
+
|
|
614
|
+
const connect = async () => {
|
|
615
|
+
// Stop when this identity has been dropped, or the watcher is standing
|
|
616
|
+
// down: a reconnect loop that outlives its reason is a leak.
|
|
617
|
+
if (rejected.has(identity.secret)) return;
|
|
618
|
+
|
|
619
|
+
const where = await findDoorbell(identity);
|
|
620
|
+
if (!where) {
|
|
621
|
+
// No doorbell configured for this deployment, which is a supported state
|
|
622
|
+
// and not worth saying twice. Ask again later in case one appears.
|
|
623
|
+
if (!complained) {
|
|
624
|
+
say(`${who}no doorbell offered; messages will arrive on the poll`);
|
|
625
|
+
complained = true;
|
|
626
|
+
}
|
|
627
|
+
setTimeout(connect, 300_000);
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
let response;
|
|
632
|
+
try {
|
|
633
|
+
response = await fetch(`${where}/v1/wake`, {
|
|
634
|
+
headers: { Authorization: `Device ${identity.secret}`, Accept: 'text/event-stream' },
|
|
635
|
+
});
|
|
636
|
+
if (!response.ok || !response.body) throw new Error(`${response.status}`);
|
|
637
|
+
} catch (error) {
|
|
638
|
+
if (!complained) {
|
|
639
|
+
say(`${who}no doorbell (${error.message}); messages will arrive on the poll`);
|
|
640
|
+
complained = true;
|
|
641
|
+
}
|
|
642
|
+
setTimeout(connect, 30_000);
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
doorbell = true;
|
|
647
|
+
if (complained) say(`${who}doorbell connected`);
|
|
648
|
+
complained = false;
|
|
649
|
+
|
|
650
|
+
try {
|
|
651
|
+
const reader = response.body.getReader();
|
|
652
|
+
const decoder = new TextDecoder();
|
|
653
|
+
for (;;) {
|
|
654
|
+
const { done, value } = await reader.read();
|
|
655
|
+
if (done) break;
|
|
656
|
+
// Comments (": beat") keep the connection open through proxies and are
|
|
657
|
+
// not events. Only a `data:` line means there is something to fetch.
|
|
658
|
+
if (decoder.decode(value, { stream: true }).includes('data:')) {
|
|
659
|
+
// The wake carries nothing. Everything true is in the inbox, read
|
|
660
|
+
// with this agent's own credential, so the only correct response to
|
|
661
|
+
// being rung is to go and look.
|
|
662
|
+
pollOne(identity).catch(() => {});
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
} catch {
|
|
666
|
+
/* the reconnect below is the whole error handling this needs */
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
doorbell = false;
|
|
670
|
+
setTimeout(connect, 1_000);
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
void connect();
|
|
674
|
+
}
|
|
675
|
+
|
|
531
676
|
async function pollOne(identity) {
|
|
532
677
|
const { secret, dir, spool: spoolFile } = identity;
|
|
533
678
|
const who = identity.handle ? `${identity.handle}: ` : '';
|
|
@@ -536,11 +681,21 @@ async function pollOne(identity) {
|
|
|
536
681
|
const query = new URLSearchParams({ limit: '50' });
|
|
537
682
|
if (spool.since) query.set('since', spool.since);
|
|
538
683
|
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
684
|
+
/*
|
|
685
|
+
Ask the server to hold the request open -- UNLESS the doorbell is up.
|
|
686
|
+
|
|
687
|
+
The held request is answered by a Durable Object, and a Durable Object is
|
|
688
|
+
billed on wall-clock time whether it is doing anything or not: one agent
|
|
689
|
+
parked continuously runs to 83% of the whole included monthly allowance.
|
|
690
|
+
While a doorbell connection is established none of that is needed, because
|
|
691
|
+
the wake arrives down a socket a plain server is holding for nothing.
|
|
692
|
+
|
|
693
|
+
So a connected watcher asks for no wait at all, and the poll below becomes
|
|
694
|
+
a slow safety net rather than the mechanism. A watcher with no doorbell
|
|
695
|
+
keeps the long poll exactly as it was -- worse on cost, right on latency,
|
|
696
|
+
and the reason losing the doorbell is survivable.
|
|
697
|
+
*/
|
|
698
|
+
const waiting = longPoll && !doorbell ? WAIT_SECONDS : 0;
|
|
544
699
|
if (waiting > 0) query.set('wait', String(waiting));
|
|
545
700
|
|
|
546
701
|
let fetched;
|
|
@@ -1482,10 +1637,19 @@ async function loop() {
|
|
|
1482
1637
|
complain(`Unexpected: ${error.stack ?? error.message}`);
|
|
1483
1638
|
}
|
|
1484
1639
|
|
|
1485
|
-
|
|
1486
|
-
|
|
1640
|
+
/*
|
|
1641
|
+
A cycle that did any waiting has already rested. One that came back at
|
|
1642
|
+
once -- rows to deliver, a server that does not wait, or a doorbell doing
|
|
1643
|
+
the waiting for us -- rests here.
|
|
1644
|
+
|
|
1645
|
+
With a doorbell up this is the only polling that happens, and it exists
|
|
1646
|
+
only to catch what a missed wake would otherwise lose: a ring that landed
|
|
1647
|
+
while the socket was reconnecting, or a delivery written while this
|
|
1648
|
+
machine was asleep. Two minutes is far longer than the old cadence and
|
|
1649
|
+
still bounds how long a lost wake can hide a message.
|
|
1650
|
+
*/
|
|
1487
1651
|
const spent = Date.now() - started;
|
|
1488
|
-
const rest = longPoll ? FLOOR_MS : intervalMs;
|
|
1652
|
+
const rest = doorbell ? SAFETY_POLL_MS : longPoll ? FLOOR_MS : intervalMs;
|
|
1489
1653
|
if (spent < rest) await new Promise((r) => setTimeout(r, rest - spent));
|
|
1490
1654
|
}
|
|
1491
1655
|
}
|
|
@@ -1510,6 +1674,16 @@ async function loop() {
|
|
|
1510
1674
|
between a failed lock and two watchers, which is not a thing to make twelve
|
|
1511
1675
|
times slower as a side effect.
|
|
1512
1676
|
*/
|
|
1677
|
+
/*
|
|
1678
|
+
ONE DOORBELL PER IDENTITY, started once and reconnecting on its own.
|
|
1679
|
+
|
|
1680
|
+
Deliberately outside the poll loop. A doorbell that had to be re-established
|
|
1681
|
+
each cycle would be a connection per cycle, which is the cost this exists to
|
|
1682
|
+
avoid -- and it would go down every time the poll was busy, which is exactly
|
|
1683
|
+
when a message is most likely to arrive.
|
|
1684
|
+
*/
|
|
1685
|
+
for (const identity of identities()) keepDoorbell(identity);
|
|
1686
|
+
|
|
1513
1687
|
setInterval(() => yieldToLowerPid(standDown), intervalMs);
|
|
1514
1688
|
|
|
1515
1689
|
await loop();
|