@agentsignalnet/claude-plugin 0.2.71 → 0.2.73

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.71",
4
+ "version": "0.2.73",
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.71",
3
+ "version": "0.2.73",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -146,6 +146,72 @@ const token = randomUUID();
146
146
  /** Where the token lives after the first visit. Namespaced: localhost is shared. */
147
147
  const COOKIE = 'agentsignal_viewer';
148
148
 
149
+ /**
150
+ * RE-READING A PENDING ROW, briefly, because delivery state changes after the
151
+ * row is cached and `since` guarantees it is never looked at again.
152
+ *
153
+ * A message is first seen a second or two after it is sent -- when everything
154
+ * is pending -- and cached with those counts. It delivers a moment later and
155
+ * nothing ever asks again, so the dashboard showed "1 pending" for hours about
156
+ * messages that had landed. Its own note calls pending "the one state that
157
+ * reads, to somebody watching a conversation, as it never arrived", which is
158
+ * exactly how it was read: a working channel was reported as broken.
159
+ *
160
+ * Why not simply re-read everything: the log endpoint CHARGES A CREDIT per call
161
+ * that returns rows and refunds one that returns none. That refund is what makes
162
+ * watching free, and re-fetching the last hundred every two seconds is the
163
+ * eighteen-hundred-calls-an-hour bill the local copy was built to stop.
164
+ *
165
+ * So the chase is bounded on both axes. CHASE_FOR is how long a pending row is
166
+ * worth money at all -- delivery settles in seconds, and a row still pending
167
+ * after two minutes is waiting on a device that is off, which no amount of
168
+ * asking changes. CHASE_EVERY is how often, so a chase costs about six credits
169
+ * rather than sixty. Past the window the row stops being chased and stops
170
+ * claiming to be pending; see `state` on the page.
171
+ */
172
+ const CHASE_FOR = 120_000;
173
+ const CHASE_EVERY = 20_000;
174
+
175
+ /** When each channel was last re-read for status, so the throttle is per channel. */
176
+ const lastChase = new Map();
177
+
178
+ /**
179
+ * Channels whose stored counts have been reconciled once in THIS run.
180
+ *
181
+ * Every row cached before the chase existed carries whatever its counts were a
182
+ * second after it was sent, and the chase only reaches back two minutes -- so
183
+ * without this, a message delivered last week reads as "sent" forever, when the
184
+ * answer is one call away. The first poll of each channel therefore drops
185
+ * `since` and takes the last hundred rows as the API currently sees them.
186
+ *
187
+ * Once per process, not once per poll: a cold cache already paid for exactly
188
+ * this call before any of it was written, so the cost is one billed read per
189
+ * channel per dashboard start and nothing after it.
190
+ */
191
+ const repaired = new Set();
192
+
193
+ /**
194
+ * ANSWERS, which are not messages and were therefore invisible.
195
+ *
196
+ * `ask_agent` sends a question and the reply comes back as a RESPONSE attached
197
+ * to it, not as a message of its own. The channel log carries messages, so a
198
+ * conversation held in questions rendered as an unbroken column of outbound
199
+ * rows -- and read exactly as it looks: as somebody who never replied. It was
200
+ * reported that way, by somebody who had in fact been answered twice.
201
+ *
202
+ * The log cannot tell a question from a statement either: `agent_message_log`
203
+ * does not carry `options`. So each recent row is asked about once, and the
204
+ * answer is cached by message id.
205
+ *
206
+ * WHAT THIS COSTS, since every call to that endpoint is priced: a row that was
207
+ * never a question is refunded (`not_a_question`), and so is a question nobody
208
+ * has answered yet. The only charged call is the one that first comes back with
209
+ * an answer -- after which the row is settled and never asked about again. One
210
+ * credit per answered question, once.
211
+ */
212
+ const ANSWER_SCAN = 25;
213
+ const answerCache = new Map();
214
+
149
215
  /** The routes a person navigates to, as opposed to the ones the page polls. */
150
216
  function isPage(pathname) {
151
217
  return pathname === '/' || pathname.startsWith('/a/');
@@ -638,6 +704,20 @@ const PAGE = `<!doctype html>
638
704
  margin: 0 0 6px; padding: 0 2px;
639
705
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
640
706
  }
707
+ /* An answer belongs WITH its question, not as a row of its own further down:
708
+ it is a reply to this and to nothing else, and a separate line would put an
709
+ unrelated message between them the moment one arrives in between. */
710
+ .answer {
711
+ margin: 6px 0 0; padding: 5px 8px 5px 10px;
712
+ border-left: 2px solid var(--accent, var(--line));
713
+ background: var(--surface-2, transparent);
714
+ border-radius: 0 6px 6px 0;
715
+ font-size: 13px; color: var(--fg);
716
+ display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap;
717
+ }
718
+ .answer .who { color: var(--dim); font-weight: 500; }
719
+ .answer .time { color: var(--dim); font-size: 12px; margin-left: auto; }
720
+
641
721
  .chan { margin-bottom: 4px; }
642
722
  .chan-head { display: flex; align-items: center; gap: 2px; }
643
723
  .twist {
@@ -972,7 +1052,19 @@ const PAGE = `<!doctype html>
972
1052
  if (m.failed > 0) return ['bad', m.failed + ' failed'];
973
1053
  if (m.read > 0) return ['', m.read + ' read'];
974
1054
  if (m.delivered > 0) return ['', m.delivered + ' delivered'];
975
- if (m.pending > 0) return ['wait', m.pending + ' pending'];
1055
+ /*
1056
+ A PENDING COUNT NOTHING IS RE-READING IS NOT NEWS, and calling it
1057
+ "pending" is a claim this page cannot support. The row was cached seconds
1058
+ after it was sent, when everything is pending, and the log endpoint bills
1059
+ per re-read so it is not re-read forever. Messages sat here marked pending
1060
+ for hours after they were delivered, and read exactly as the note above
1061
+ predicts -- as "it never arrived".
1062
+
1063
+ So a row nobody is chasing any more says the thing that is still true: it
1064
+ was sent. The server sets stale, because it is the side that knows when it
1065
+ stopped looking.
1066
+ */
1067
+ if (m.pending > 0) return m.stale ? ['', 'sent'] : ['wait', m.pending + ' pending'];
976
1068
  return ['', 'no devices'];
977
1069
  }
978
1070
 
@@ -1714,6 +1806,9 @@ const PAGE = `<!doctype html>
1714
1806
  </div>
1715
1807
  \${m.title && m.title !== who ? '<div class="title">' + esc(m.title) + '</div>' : ''}
1716
1808
  <div class="clipwrap" data-wrap="\${esc(m.id)}"><div class="body" data-body="\${esc(m.id)}">\${esc(m.body ?? m.preview)}\${m.truncated ? '…' : ''}</div></div>
1809
+ \${(m.answers ?? []).map((a) =>
1810
+ '<div class="answer"><span class="who">' + esc(a.who) + '</span> answered <b>' +
1811
+ esc(a.option) + '</b><span class="time">' + esc(stamp(a.at)) + '</span></div>').join('')}
1717
1812
  </div>\`;
1718
1813
  }).join('');
1719
1814
 
@@ -2469,6 +2564,51 @@ function channelFor(ref) {
2469
2564
  }
2470
2565
 
2471
2566
  /** One upstream call, one place that turns a non-2xx into something readable. */
2567
+ /**
2568
+ * Attach answers to the rows that have them.
2569
+ *
2570
+ * Only the newest ANSWER_SCAN rows: an answer arrives minutes after its
2571
+ * question, not days, and probing the whole history on every poll would be a
2572
+ * request per row forever. Settled rows -- not a question, or a question every
2573
+ * recipient has answered -- are cached and never asked about again, which is
2574
+ * what keeps this from being a per-poll cost.
2575
+ */
2576
+ async function withAnswers(key, messages) {
2577
+ const recent = messages.slice(0, ANSWER_SCAN);
2578
+
2579
+ await Promise.all(
2580
+ recent.map(async (m) => {
2581
+ const known = answerCache.get(m.id);
2582
+ // Settled: either it was never a question, or nobody is still to reply.
2583
+ if (known && (known.settled || Date.now() - known.at < CHASE_EVERY)) return;
2584
+
2585
+ const body = await upstream('/v1/messages/' + encodeURIComponent(m.id) + '/responses', key);
2586
+ if (body.error) {
2587
+ // A 400 here is the API saying "sent without options", which is a
2588
+ // permanent fact about the row rather than a failure to retry.
2589
+ const notAQuestion = /without options/i.test(body.error);
2590
+ answerCache.set(m.id, { at: Date.now(), settled: notAQuestion, answers: null });
2591
+ return;
2592
+ }
2593
+ answerCache.set(m.id, {
2594
+ at: Date.now(),
2595
+ settled: (body.awaiting ?? 0) === 0,
2596
+ answers: (body.responses ?? []).map((r) => ({
2597
+ who: r.name || r.recipient_key,
2598
+ option: (body.options ?? [])[r.option_index] ?? String(r.option_index),
2599
+ at: r.responded_at,
2600
+ })),
2601
+ awaiting: body.awaiting ?? 0,
2602
+ });
2603
+ }),
2604
+ );
2605
+
2606
+ return messages.map((m) => {
2607
+ const known = answerCache.get(m.id);
2608
+ return known?.answers?.length ? { ...m, answers: known.answers } : m;
2609
+ });
2610
+ }
2611
+
2472
2612
  async function upstream(path, key = apiKey) {
2473
2613
  try {
2474
2614
  const response = await fetch(`${baseUrl}${path}`, {
@@ -3452,8 +3592,44 @@ const server = createServer(async (req, res) => {
3452
3592
  // leave a hole in whichever is quieter.
3453
3593
  const kept = readHistory(on.ref ?? channelRef);
3454
3594
  const newest = kept.length ? kept[0].created_at : null;
3595
+
3596
+ /**
3597
+ * Wind `since` back to the oldest row still worth asking about.
3598
+ *
3599
+ * Normally it is the newest row held, which is what keeps an idle poll
3600
+ * empty and free. When something recent is still pending, it becomes that
3601
+ * row instead -- so the answer includes it and its counts are refreshed --
3602
+ * and then only once per CHASE_EVERY, because that answer is not empty and
3603
+ * is therefore billed.
3604
+ *
3605
+ * `kept` is newest-first, so the last match is the oldest one.
3606
+ */
3607
+ const key = on.ref ?? channelRef ?? '';
3608
+ const now = Date.now();
3609
+ const chasing = kept.filter(
3610
+ (m) => (m.pending ?? 0) > 0 && now - Date.parse(m.created_at) < CHASE_FOR,
3611
+ );
3612
+ const due = chasing.length > 0 && now - (lastChase.get(key) ?? 0) >= CHASE_EVERY;
3613
+ if (due) lastChase.set(key, now);
3614
+
3615
+ /*
3616
+ A MILLISECOND BEFORE IT, because `since` is EXCLUSIVE: the SQL is
3617
+ `created_at > p_since`, so asking from a row's own stamp asks for
3618
+ everything after it and returns everything except the row we came for.
3619
+ Date.parse truncates the microseconds Postgres stores, which would happen
3620
+ to work here, but relying on a rounding artefact for correctness is how
3621
+ this breaks the day the column changes precision.
3622
+ */
3623
+ const repair = !repaired.has(key);
3624
+ repaired.add(key);
3625
+
3626
+ const from = repair
3627
+ ? null
3628
+ : due
3629
+ ? new Date(Date.parse(chasing[chasing.length - 1].created_at) - 1).toISOString()
3630
+ : newest;
3455
3631
  const body = await upstream(
3456
- '/v1/messages?limit=100' + (newest ? '&since=' + encodeURIComponent(newest) : ''),
3632
+ '/v1/messages?limit=100' + (from ? '&since=' + encodeURIComponent(from) : ''),
3457
3633
  on.key,
3458
3634
  );
3459
3635
 
@@ -3472,13 +3648,44 @@ const server = createServer(async (req, res) => {
3472
3648
  }
3473
3649
  channelRef = on.ref ?? channelRef;
3474
3650
 
3651
+ /*
3652
+ Mark the repair under the ref the SERVER resolved as well as the one we
3653
+ asked with. At boot there is no ref yet, so the first poll is keyed on
3654
+ the empty string and the second -- now knowing the real ref -- would
3655
+ find nothing, repair again, and re-download the last hundred rows every
3656
+ poll forever. The roster cache had this exact bug and it presented as
3657
+ "reading..." that never finished; here it would present as a bill.
3658
+ */
3659
+ repaired.add(on.ref ?? channelRef);
3660
+
3475
3661
  const fresh = (body.messages ?? []).map(keepable).filter(Boolean);
3476
- // Deduplicated by id, because `since` is inclusive at the boundary on a
3477
- // shared timestamp and two messages in the same second are ordinary.
3662
+ // Deduplicated by id: a chase deliberately asks from before a row it
3663
+ // already holds, so the same message arrives twice by design. (An older
3664
+ // comment here said `since` was inclusive at the boundary. It is not --
3665
+ // the SQL is a strict `>` -- and the dedup is what makes the re-ask safe
3666
+ // rather than what compensates for the boundary.)
3478
3667
  const byId = new Map(kept.map((m) => [m.id, m]));
3479
3668
  for (const m of fresh) byId.set(m.id, m);
3480
3669
 
3481
- body.messages = writeHistory(on.ref ?? channelRef, [...byId.values()], settings().history_days);
3670
+ const merged = writeHistory(on.ref ?? channelRef, [...byId.values()], settings().history_days);
3671
+
3672
+ /**
3673
+ * Mark what is no longer being chased, so the page can stop calling it
3674
+ * pending. Computed here rather than stored, because it is a fact about
3675
+ * NOW -- a row written to disk as fresh is stale an hour later, and a
3676
+ * cached flag would be the same frozen answer one level down.
3677
+ */
3678
+ const marked = merged.map((m) =>
3679
+ (m.pending ?? 0) > 0 && Date.now() - Date.parse(m.created_at) >= CHASE_FOR
3680
+ ? { ...m, stale: true }
3681
+ : m,
3682
+ );
3683
+
3684
+ // Answers last, so a row carries both what became of it and what came
3685
+ // back. Not written to history: an answer can arrive after the row is
3686
+ // stored, and a cached copy would freeze exactly the way the delivery
3687
+ // counts did.
3688
+ body.messages = await withAnswers(on.key, marked);
3482
3689
  // Every channel this dashboard can open, so the page can offer them. The
3483
3690
  // key never leaves this process -- only the ref and the name.
3484
3691