@cotal-ai/connector-core 0.166.3 → 0.168.0

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/dist/agent.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { EventEmitter } from "node:events";
3
3
  import { hostname } from "node:os";
4
- import { normalizeMentions, RUN_LAUNCH_DEADLINE_MS, subjectMatches, isConcreteChannel, isChannelFamily, assertValidChannel, channelInAllow, eventChannelPrincipal, resolvePeer as resolvePeerInRoster, peerLabel, shownPeerName, CotalEndpoint, DmDurableMissingError, acquireLocalConsumer, BASELINE_LIFECYCLE_ENDPOINT, assertLifecycleToken, EpEnvelopeError, isPublishPermissionDenied, unansweredRequest, undeclaredArg, undeclaredArgInstance, EP_UNANSWERED, controlReplyFrom, controlReplyFromThrown, runAgentBearer, ANYCAST_NOTICE_KEY, MENTION_NOTICE_KEY, partsToText, canonicalJson, assertValidRole, turnNoticeFilter, renderOnceTask, } from "@cotal-ai/core";
4
+ import { normalizeMentions, RUN_LAUNCH_DEADLINE_MS, subjectMatches, isConcreteChannel, isChannelFamily, assertValidChannel, channelInAllow, eventChannelPrincipal, resolvePeer as resolvePeerInRoster, peerLabel, shownPeerName, CotalEndpoint, DmDurableMissingError, acquireLocalConsumer, BASELINE_LIFECYCLE_ENDPOINT, assertLifecycleToken, EpEnvelopeError, isPublishPermissionDenied, unansweredRequest, undeclaredArg, undeclaredArgInstance, EP_UNANSWERED, controlReplyFrom, controlReplyFromThrown, runAgentBearer, canonicalJson, assertValidRole, turnNoticeFilter, renderOnceTask, } from "@cotal-ai/core";
5
5
  import { eventPlaneArmed } from "./config.js";
6
6
  import { checkEventStartup, EventStateConflictError } from "./event-startup.js";
7
7
  import { TURN_PULL_RETRY_MS } from "./turn-poll.js";
@@ -9,6 +9,8 @@ import { ONCE_HANDOFF_MS, claimOnceHandoff, confirmOnceHandoff, endOnceHandoff,
9
9
  import { attributionSafe } from "./framing.js";
10
10
  import { invokeUserManager, withUserManagerListing } from "./manager-call.js";
11
11
  import { CONNECTOR_VERSION } from "./version.generated.js";
12
+ import { AgentInbox, deliveryNoticeOf } from "./agent-inbox.js";
13
+ export { afterRecallMark, MAX_INBOX } from "./agent-inbox.js";
12
14
  /** Client-side floor for a spawn action's submit + follow. The manager acceptance carries the exact
13
15
  * connector-selected readiness budget, and core extends the follow through that budget plus
14
16
  * delivery margin. This floor still outlives the manager's generic 30s default and protects
@@ -69,44 +71,6 @@ function buildMeta(config, processCwd, eventsArmed) {
69
71
  delete meta.cwd;
70
72
  return Object.keys(meta).length ? meta : undefined;
71
73
  }
72
- /** Total order on {@link RecallMark}: by time, then by id, so items sharing a millisecond still queue. */
73
- export function afterRecallMark(a, b) {
74
- // #624: recall marks and items are addressed by RECEIVE key (never the wire id), and a minted
75
- // key is never the empty string, so the tie-break comparator never sees an empty id on either
76
- // side: the only empty id in a RecallMark is the initial cursor {ts: 0, id: ""}, whose ts
77
- // differs from every real item and so never reaches the id comparison. (The empty-id guard this
78
- // comparator once carried was removed after a root chase showed it unreachable: with the wire id
79
- // out of the marks, no real path can put two empty ids, or an empty id against a real one, into
80
- // the comparison.)
81
- return a.ts !== b.ts ? a.ts > b.ts : a.id > b.id;
82
- }
83
- /** How many pending messages one session buffers before the overflow valve in
84
- * {@link MeshAgent.buffer} evicts. Exported so the overflow smokes read the bound they exercise. */
85
- export const MAX_INBOX = 200;
86
- /** How long {@link MeshAgent.takeChosen} waits for the delivery daemon before it lets a caller show
87
- * what is already taken. A take not decided by then stays held for a later read. */
88
- const TAKE_WAIT_MS = 2_000;
89
- /** How long a role request whose take could not be decided waits before it is asked about again. */
90
- const TAKE_RETRY_MS = 10_000;
91
- /** How many receive keys {@link MeshAgent.holdInFlight} protects at once. The overflow valve never
92
- * evicts a held entry, so this ceiling also bounds the inbox while holds are open, which is why it
93
- * is derived from {@link MAX_INBOX}. It sits above the inbox because a frame keeps its holds until
94
- * its own verdict, even on entries an overlapping frame has already committed. */
95
- const MAX_IN_FLIGHT = MAX_INBOX * 2;
96
- /** How many future-stamped recall ids one session will remember having handed over. See
97
- * {@link MeshAgent.recallAheadRoom}. */
98
- const MAX_AHEAD = 256;
99
- /** How many overflow-evicted ambient messages {@link MeshAgent.evictedClassifications} remembers
100
- * the class of. */
101
- const CLASSIFICATION_CAP = 4096;
102
- /** How many ids one focus episode keeps out of recall in {@link MeshAgent.focusExcludedIds}, and,
103
- * bounded separately, how many id-less copies it counts in {@link MeshAgent.focusIdlessEntries}. */
104
- const FOCUS_EXCLUSION_CAP = 4096;
105
- /** #662: history reads one channel's recall makes while an id-less copy keeps arriving mid-read. */
106
- const IDLESS_READS = 3;
107
- /** How many ids each of {@link MeshAgent.protectedPullOnlyIds} and
108
- * {@link MeshAgent.protectedDropIds} holds. */
109
- const PROTECTED_DISPOSITION_CAP = 4096;
110
74
  /** How many unanswered directed messages, asked questions, and DM or anycast senders one session
111
75
  * remembers for reply correlation. */
112
76
  const MAX_CORRELATIONS = 1024;
@@ -164,36 +128,9 @@ function onceSpawner(payload) {
164
128
  return undefined;
165
129
  }
166
130
  }
167
- import { createHash, randomUUID } from "node:crypto";
168
- /** Which delivery daemon notice part a DM carries, anycast (SPEC §8.1) or mention (SPEC §8), if any.
169
- * Its sender is still to be checked against the delivery lease. */
170
- function deliveryNoticeOf(m) {
171
- for (const p of m.parts) {
172
- if (p.kind !== "data" || typeof p.data !== "object" || p.data === null || Array.isArray(p.data))
173
- continue;
174
- if (ANYCAST_NOTICE_KEY in p.data)
175
- return "anycast";
176
- if (MENTION_NOTICE_KEY in p.data)
177
- return "mention";
178
- }
179
- return undefined;
180
- }
181
- /** #662: what an id-less channel delivery shares with its stream copy. Both are the same published
182
- * bytes, read through the same authentication, so their content is all that links them. */
183
- function idlessDigest(m) {
184
- return createHash("sha256").update(JSON.stringify(m)).digest("base64url");
185
- }
186
131
  function sleep(ms) {
187
132
  return new Promise((r) => setTimeout(r, ms));
188
133
  }
189
- /** The dedup key a received message id contributes to ingest's coalescing, or undefined when the id
190
- * is EMPTY: an empty id is never a dedup key (#624). Undefined means "no identity to coalesce on",
191
- * so every id-keyed lookup in {@link MeshAgent.ingest} is skipped for such a message and it flows to
192
- * the buffer on its own merits. Real ids are returned unchanged: their coalescing (live/durable
193
- * copies of one message, and redelivery after handle) is untouched. */
194
- function ingestDedupKey(id) {
195
- return id === "" ? undefined : id;
196
- }
197
134
  /**
198
135
  * How long a non-empty automatic queue may make no progress before this session stops calling
199
136
  * itself `ready` (#1233).
@@ -291,43 +228,16 @@ export class MeshAgent extends EventEmitter {
291
228
  config;
292
229
  launchEnv;
293
230
  options;
294
- inbox = [];
295
- /** Role requests delivered on an auth mesh whose take is not recorded for the copy held (SPEC §8.1).
296
- * They count toward a wake and are never shown: {@link takeChosen} asks the delivery daemon to
297
- * record the take of each one a host chose to show, and a recorded one moves to {@link inbox}. */
298
- gated = [];
299
- /** Counts arrivals, to number each entry's {@link Pending.arrival}. */
300
- arrivals = 0;
301
- /** Why the last take could not be decided, when it could not, and when a held request may be
302
- * asked about again. Reported by `cotal_inbox`. */
303
- heldTake;
304
- /** Re-announces the held role requests once {@link heldTake} lets them be asked about again. */
305
- takeRetry;
306
- /** Ids already SURFACED to the model (handled) — bounded, commit-aware dedup ACROSS a drain. The
307
- * live↔durable transition window can deliver the two copies of one message far enough apart that the
308
- * first is already drained (removed from {@link inbox}) when the second arrives; the pending-inbox
309
- * check alone would then re-buffer and double-surface it. Recorded at HANDLE time ({@link drainInbox}),
310
- * never at receive time — so a later durable duplicate of an already-handled id is safe to ack (the
311
- * logical message was delivered), which is exactly what the removed endpoint-level `firstSeenChat`
312
- * got wrong (it acked at receive time, before handling). Two rotating windows bound memory. */
313
- handledIds = new Set();
314
- handledIdsPrev = new Set();
315
- /** Receive-time classes for overflow-evicted, unhandled channel ambient. Capacity exhaustion
316
- * permanently degrades unknown ambient to pull-only for this session rather than risk a late
317
- * live/durable copy changing from quiet to automatic. */
318
- evictedClassifications = new Map();
319
- /** Surfaced to the host but not yet committed or abandoned, counted per holding frame because
320
- * frames overlap. See {@link holdInFlight}. */
321
- inFlightIds = new Map();
322
- /** Ack-wait renewal timers of held durable deliveries, by receive key. See {@link keepOwned}. */
323
- renewals = new Map();
324
- classificationUnsafe = false;
325
- /** Terminal receive-time decisions that must survive the live→durable transition: successfully
326
- * surfaced pull-only messages and hard drops under muted/focus. Capacity loss degrades the
327
- * corresponding whole class fail-closed for the rest of the session. */
328
- protectedPullOnlyIds = new Set();
329
- protectedDropIds = new Set();
330
- dropUnsafe = false;
231
+ inboxState = new AgentInbox({
232
+ endpoint: () => this.ep,
233
+ config: () => this.config,
234
+ transportConnected: () => this._transportConnected,
235
+ emit: (event, item) => this.emit(event, item),
236
+ warning: (error) => this.handleEndpointWarning(error),
237
+ noteDirected: (item) => this.noteDirected(item),
238
+ inOrder: (write) => this.inOrder(write),
239
+ requireConnected: () => this.requireConnected(),
240
+ });
331
241
  /** How many times the endpoint saw this session's stored copies at their limit (#5404) since a
332
242
  * cotal_inbox reply last carried the notice. */
333
243
  _storedCopiesFull = 0;
@@ -336,42 +246,11 @@ export class MeshAgent extends EventEmitter {
336
246
  _connected = false;
337
247
  /** Raw NATS transport liveness, separate from `_connected` (the full Cotal bind/readiness). */
338
248
  _transportConnected = false;
339
- /** Wall-clock time of the latest inbox drain that actually committed at least one delivery.
340
- * This is measured only after the backing acknowledgements succeed, never inferred from a read
341
- * attempt or from an empty inbox. */
342
- _lastInboxDrainedAt;
343
- /** Wall-clock time of the latest drain that committed at least one AUTOMATIC delivery.
344
- *
345
- * Deliberately separate from {@link _lastInboxDrainedAt}, and #1233 is why: on a host-mode
346
- * connector the tool inbox and the connector-managed automatic queue are different queues, and
347
- * the measured seat was draining the first every few minutes while the second had not moved in
348
- * 13.8 hours. A progress mark that counts a pull-only drain as progress would have reported that
349
- * seat as making progress, which is the exact lie this field exists to refuse. */
350
- _lastAutomaticDrainedAt;
351
- /** The latest commit that took the OLDEST automatic delivery then queued (#1526).
352
- *
353
- * Distinct from {@link _lastAutomaticDrainedAt}, and the distinction is the defect: any automatic
354
- * commit used to count, so a seat that kept committing fresh arrivals over a head it could not
355
- * deliver reset its own stall clock on every turn and reported `ready` indefinitely, while the
356
- * entries at the front of its queue aged past two hours. A queue drains when its head moves; a
357
- * queue that serves only its newest arrivals is not draining, it is skipping. */
358
- _lastAutomaticHeadDrainedAt;
359
249
  /** Latest connection failure, retained until the endpoint binds so a bounded readiness gate can
360
250
  * explain why an otherwise healthy host never joined the mesh. */
361
251
  lastConnectionError;
362
252
  endpointNoticeLog = new Map();
363
253
  _status = "idle";
364
- _attention = "open"; // F3: fail-open default; reset to open on SessionStart
365
- _recallCursor = { ts: 0, id: "" };
366
- /** Recall items stamped ahead of this session's clock that it has already handed over. They are
367
- * tracked by id rather than by {@link _recallCursor}, because a sender's clock must not be able to
368
- * move this session's mark. Bounded by {@link MAX_AHEAD}. */
369
- aheadDelivered = new Set();
370
- /** Per-channel attention overrides — the AUTHORITATIVE runtime state (read by {@link ingest} on
371
- * every message). Seeded from the agent-file default; mutated by {@link setChannelMode}; mirrored
372
- * to presence for peers. An absent key ⇒ that channel follows the global {@link _attention}. Reset
373
- * on restart (rebuilt from config; presence sweep clears the mirror). */
374
- channelModes = new Map();
375
254
  /** The turn relay's seat-side intake: every pulled-and-unyielded run turn, by goal id. Fed by
376
255
  * {@link pollTurns}; surfaced into host context by {@link surfacePendingTurns}; drained by
377
256
  * {@link yieldTurn} (explicit) and the working→idle boundary in {@link setStatus} (automatic
@@ -414,37 +293,6 @@ export class MeshAgent extends EventEmitter {
414
293
  /** By `contextId`, the questions this seat asked inside a {@link withCorrelation} scope, and whom
415
294
  * they went to (bounded). See {@link answersQuestion}. */
416
295
  asked = new Map();
417
- /** Chat-stream frontier captured when this agent entered `focus` — recall surfaces ambient
418
- * published after it ("since you entered focus"). Undefined unless in focus. */
419
- focusSince;
420
- enteringFocus = false;
421
- /** The receive-key namespace secret (#624): a per-session random value minted at construction,
422
- * never written to any wire or log. Minted receive keys are `${secret}.${seq}`, so they are
423
- * DISJOINT from wire ids by construction: an attacker-chosen wire id cannot equal one, so one
424
- * verdict can never select two entries through a forged collision. Recognition is FUNCTIONAL
425
- * (the key starts with the secret), so it cannot saturate the way a bounded set would: every
426
- * minted key stays recognizable for the session's life, with nothing to expire or overflow. */
427
- recvKeySecret = randomUUID().replace(/-/g, "");
428
- recvKeySeq = 0;
429
- focusExcludedIds = new Map();
430
- /** #662: id-less channel deliveries settled this focus episode, per content digest. Live ingest has
431
- * no stream sequence, so content is all an id-less delivery shares with its stream copy, and
432
- * identical copies may each have had a different disposition. So recall binds each settled copy
433
- * to a stream sequence and hands back only the copies no settled copy is bound to. Replaced, never
434
- * cleared, per episode, so a hold from an earlier episode cannot un-count a later one. */
435
- focusIdless = new Map();
436
- /** Unbound copies plus bound sequences in {@link focusIdless}, bounded by
437
- * {@link FOCUS_EXCLUSION_CAP}. */
438
- focusIdlessEntries = 0;
439
- /** Bumped as each channel's recall read starts: a copy settled before a read that the read could
440
- * not bind is behind the focus frontier or out of retention, and no later read will see it. */
441
- idlessEpoch = 0;
442
- /** Recall runs one at a time, so an older read can never settle over a newer one (#662). */
443
- recallLock = Promise.resolve();
444
- /** Bumped each time the transport goes down, so a settled copy's frontier read can tell whether
445
- * the connection stayed up from the copy's arrival to the answer (#662). */
446
- transportDrops = 0;
447
- focusRecallUnsafeChannels = new Set();
448
296
  _stopping = false;
449
297
  /** Presence writes go out IN CALL ORDER (#636, #2055). setStatus is two awaited puts and every
450
298
  * connector fires it without awaiting, so two overlapping calls interleave their puts and the
@@ -464,10 +312,7 @@ export class MeshAgent extends EventEmitter {
464
312
  this.launchEnv = env === undefined ? undefined : { ...env };
465
313
  // Seed per-channel attention from the operator's file default (one-way: the runtime never writes
466
314
  // back — the persona file is a shared template). muted/quiet are validated disjoint at file load.
467
- for (const c of config.quiet ?? [])
468
- this.channelModes.set(c, "quiet");
469
- for (const c of config.muted ?? [])
470
- this.channelModes.set(c, "muted");
315
+ this.inboxState.seedChannelModes(config);
471
316
  const userAuth = config.userAuth;
472
317
  this.ep = new CotalEndpoint({
473
318
  space: config.space,
@@ -489,7 +334,7 @@ export class MeshAgent extends EventEmitter {
489
334
  ackWaitMs: config.ackWaitMs, // undefined → endpoint default (60s); shortened in tests to observe redelivery
490
335
  channels: config.subscribe, // the endpoint's live filter = the active read set
491
336
  allowSubscribe: config.allowSubscribe, // the complete ACL decides whether a DLV durable exists
492
- channelModes: Object.fromEntries(this.channelModes), // seed presence so file defaults are visible at boot
337
+ channelModes: this.channelModeEntries(), // seed presence so file defaults are visible at boot
493
338
  // A seat reads the roster when it acts (readRoster) instead of watching it: a watch delivers
494
339
  // every member's heartbeat to every member, K² per heartbeat in a space of K (SPEC §6).
495
340
  watchPresence: false,
@@ -534,9 +379,9 @@ export class MeshAgent extends EventEmitter {
534
379
  this.ep.on("message", (m, d, meta) => {
535
380
  const notice = meta?.kind === "dm" ? deliveryNoticeOf(m) : undefined;
536
381
  if (notice !== undefined)
537
- void this.ingestNotice(m, d, meta, notice);
382
+ void this.inboxState.ingestNotice(m, d, meta, notice);
538
383
  else
539
- this.ingest(m, d, meta);
384
+ this.inboxState.ingest(m, d, meta);
540
385
  });
541
386
  this.ep.on("error", (e) => this.handleEndpointError(e));
542
387
  // A warning is a condition the endpoint is already surviving. Log it through the same bounded
@@ -554,22 +399,7 @@ export class MeshAgent extends EventEmitter {
554
399
  if (this._transportConnected === e.connected)
555
400
  return;
556
401
  this._transportConnected = e.connected;
557
- if (!e.connected) {
558
- this.transportDrops++;
559
- // This reader cannot vouch for a copy across a drop, so a role request taken and not yet
560
- // handed to a host is taken again before it is shown.
561
- for (const p of this.inbox.filter((x) => x.take && !this.inFlightIds.has(x.item.recvKey)))
562
- this.regate(p);
563
- }
564
- // No take is asked while the link is down (see mayAskTakes), so a host that read then was offered
565
- // none of the held requests. Each whose take is not out, and whose copy is still this reader's,
566
- // is announced again now.
567
- if (e.connected && this.mayAskTakes()) {
568
- this.dropLapsedTakes();
569
- for (const p of this.gated)
570
- if (p.taking === undefined)
571
- this.emit("incoming", p.item);
572
- }
402
+ this.inboxState.onTransport(e.connected);
573
403
  this.emit("transport", e);
574
404
  // A notice lost while the transport was down can only matter to a turn this seat holds: the
575
405
  // manager repeats the notice for a turn the seat has not pulled yet. So only a holder pulls.
@@ -627,39 +457,18 @@ export class MeshAgent extends EventEmitter {
627
457
  }
628
458
  /** The latest successful, non-empty inbox drain in this session. */
629
459
  get lastInboxDrainedAt() {
630
- return this._lastInboxDrainedAt;
460
+ return this.inboxState.lastInboxDrainedAt;
631
461
  }
632
462
  /** The latest drain that committed at least one AUTOMATIC (connector-managed) delivery. */
633
463
  get lastAutomaticDrainedAt() {
634
- return this._lastAutomaticDrainedAt;
464
+ return this.inboxState.lastAutomaticDrainedAt;
635
465
  }
636
466
  /** The latest drain that committed the automatic delivery that was then at the FRONT of the queue
637
467
  * (#1526). This is the fact {@link automaticQueueStalledForMs} measures progress by; the looser
638
468
  * {@link lastAutomaticDrainedAt} is still reported beside it, because the two disagreeing is
639
469
  * precisely the shape of a wedged head being served around. */
640
470
  get lastAutomaticHeadDrainedAt() {
641
- return this._lastAutomaticHeadDrainedAt;
642
- }
643
- /** The receive key of the oldest still-queued automatic delivery: the queue's head, and the entry
644
- * whose commit counts as progress. */
645
- oldestAutomaticKey() {
646
- let head;
647
- for (const pending of this.inbox) {
648
- if (pending.pullOnly)
649
- continue;
650
- if (head === undefined || pending.receivedAt < head.receivedAt)
651
- head = pending;
652
- }
653
- return head?.item.recvKey;
654
- }
655
- /** Record head progress when this batch takes the current head of the automatic queue. Call
656
- * BEFORE the batch leaves {@link inbox}, since the head is read from the live queue. */
657
- noteAutomaticHeadProgress(selected) {
658
- const head = this.oldestAutomaticKey();
659
- if (head === undefined)
660
- return;
661
- if (selected.some((p) => !p.pullOnly && p.item.recvKey === head))
662
- this._lastAutomaticHeadDrainedAt = Date.now();
471
+ return this.inboxState.lastAutomaticHeadDrainedAt;
663
472
  }
664
473
  /**
665
474
  * How long a non-empty automatic queue has been making no progress, or `undefined`.
@@ -682,12 +491,7 @@ export class MeshAgent extends EventEmitter {
682
491
  * timed out.
683
492
  */
684
493
  automaticQueueStalledForMs(now = Date.now()) {
685
- const oldest = this.oldestAutomaticReceivedAt();
686
- if (oldest === undefined)
687
- return undefined;
688
- const since = Math.max(oldest, this._lastAutomaticHeadDrainedAt ?? 0);
689
- const held = now - since;
690
- return held > 0 ? held : 0;
494
+ return this.inboxState.automaticQueueStalledForMs(now);
691
495
  }
692
496
  /** Whether {@link stop} has been called. Terminal, and never cleared: a stopped session does not
693
497
  * serve again. This is the ONLY way to tell a deliberate shutdown from a lost connection, because
@@ -712,9 +516,7 @@ export class MeshAgent extends EventEmitter {
712
516
  }
713
517
  /** Session-local channel degradation, independent of transport liveness. */
714
518
  get channelDeliveryIssue() {
715
- return this.dropUnsafe
716
- ? "Channel drop tracking exhausted. Ambient channel traffic is suppressed until restart; non-historical mentions on unmuted channels wake to pull their buffered body. Focus recall may be incomplete."
717
- : undefined;
519
+ return this.inboxState.channelDeliveryIssue;
718
520
  }
719
521
  /** The connection, write-health and channel-delivery facts combined. Repeated presence write
720
522
  * failures or exhausted drop tracking degrade a bound session even with a live transport.
@@ -734,7 +536,7 @@ export class MeshAgent extends EventEmitter {
734
536
  if (this._stopping)
735
537
  return "stopped";
736
538
  if (this._connected) {
737
- if (this.dropUnsafe)
539
+ if (this.channelDeliveryIssue)
738
540
  return "degraded";
739
541
  if ((this.presenceWriteFailure?.consecutiveFailures ?? 0) >= 2)
740
542
  return "degraded";
@@ -936,10 +738,7 @@ export class MeshAgent extends EventEmitter {
936
738
  async stopOnce() {
937
739
  this._stopping = true;
938
740
  this.emit("stopping");
939
- for (const key of [...this.renewals.keys()])
940
- this.stopRenewal(key);
941
- clearTimeout(this.takeRetry);
942
- this.takeRetry = undefined;
741
+ this.inboxState.stop();
943
742
  this.managerBearers.clear();
944
743
  if (this.turnRetryTimer !== undefined) {
945
744
  clearTimeout(this.turnRetryTimer);
@@ -1017,162 +816,6 @@ export class MeshAgent extends EventEmitter {
1017
816
  return { ok: false, message: `Reconnect failed: ${e.message || e.name}. Still retrying automatically — or run /reconnect to retry now.` };
1018
817
  }
1019
818
  }
1020
- // ---- inbox ---------------------------------------------------------------
1021
- /** A DM shaped as a delivery daemon's notice is one only when its authenticated sender holds the
1022
- * space's delivery lease. Any other sender's copy is an ordinary DM. */
1023
- async ingestNotice(m, delivery, meta, notice) {
1024
- const holder = await this.ep.deliveryLeaseHolder(0);
1025
- this.ingest(m, delivery, meta, holder !== undefined && holder === m.from.id ? notice : undefined);
1026
- }
1027
- ingest(m, delivery, meta, notice) {
1028
- // #624: an EMPTY id is never a dedup key. The id-keyed coalescing below exists to collapse the
1029
- // copies of ONE message across the live/durable paths, and identity is what makes that safe. A
1030
- // message that carries an empty id asserts no identity (SPEC §5 requires a unique id; the sender
1031
- // already violated it), so keying the coalescing on "" reads empty-equals-empty as a duplicate
1032
- // and silently drops every empty-id message after the first: measured live, two distinct
1033
- // empty-id messages arrived and only the first ever buffered. Treating an empty id as NO id
1034
- // costs the coalescing for those messages only (a redelivered copy can surface twice), which is
1035
- // the wire contract's at-least-once stance; collapsing distinct messages is not a stance at all.
1036
- const key = ingestDedupKey(m.id);
1037
- // Already SURFACED and drained? This is a post-handle cross-path duplicate (the transition window's
1038
- // second copy, arriving after the first was handled). Don't surface it again; if it's the durable
1039
- // copy, ack it so JetStream stops redelivering — safe because the logical message was already
1040
- // handled (handledIds is recorded at drain time, never at receive time).
1041
- if (key !== undefined && (this.handledIds.has(key) || this.handledIdsPrev.has(key))) {
1042
- if (delivery.durable)
1043
- delivery.ack();
1044
- return;
1045
- }
1046
- if (key !== undefined && (this.protectedPullOnlyIds.has(key) || this.protectedDropIds.has(key))) {
1047
- if (delivery.durable)
1048
- delivery.ack();
1049
- return;
1050
- }
1051
- // Duplicate id still PENDING — keep ONE entry. Take the freshest ack handle, but NEVER downgrade a
1052
- // durable (committing) ack to a live no-op. Two cases produce a duplicate: same-path JetStream
1053
- // redelivery (always durable → upgrade to the fresh handle), and the cross-path live/durable
1054
- // transition window. There, if the DURABLE copy arrived first and a LIVE copy lands second,
1055
- // overwriting with the live no-op would leave the durable copy uncommitted → JS redelivers it → it
1056
- // double-surfaces. So only a durable delivery may replace the stored handle; a live duplicate is
1057
- // dropped as-is. A durable duplicate also re-announces the still-pending item through the
1058
- // ordinary `incoming` policy path. That turns JetStream redelivery into a timer-free retry for
1059
- // a wake the host dropped, without bypassing quiet/attention or adapter in-flight guards.
1060
- const existing = key === undefined ? undefined : this.inbox.find((p) => p.item.id === key) ?? this.gated.find((p) => p.item.id === key);
1061
- if (existing) {
1062
- if (delivery.durable) {
1063
- existing.ack = delivery.ack;
1064
- // A fresh copy is a fresh ownership: a hold that outlived the copy it renewed keeps this one.
1065
- this.own(existing, delivery.redelivery);
1066
- if (delivery.take)
1067
- existing.take = delivery.take;
1068
- if (this.inFlightIds.has(existing.item.recvKey))
1069
- this.keepOwned(existing.item.recvKey);
1070
- // A role request taken for an earlier copy and not yet shown needs a take of this one: the
1071
- // broker redelivered it, so the earlier take may have lost it to another holder.
1072
- else if (existing.take && existing.takenCount !== existing.item.deliveryCount)
1073
- this.regate(existing);
1074
- this.emit("incoming", existing.item);
1075
- }
1076
- return;
1077
- }
1078
- if (!meta)
1079
- throw new Error(`message ${m.id} delivered without MessageMeta — its class is unauthenticated`);
1080
- const item = this.toInboxItem(m, meta.kind, meta.historical);
1081
- if (notice)
1082
- item.notice = notice;
1083
- // A notice asks for nothing and answers nothing. A role request awaiting its take is recorded
1084
- // as asked of us only once it is taken, since a refused take drops it unseen.
1085
- else if (!delivery.take)
1086
- this.noteDirected(item);
1087
- // Per-channel override is the FINAL word for a channel message (DMs/anycast are never channel-
1088
- // scoped, so they bypass this entirely and always buffer). Evaluated BEFORE the global mode:
1089
- // - `muted` → hard drop, incl. @mention (a mention rides the channel; you can't keep it if you
1090
- // dropped the channel). Acking does NOT delete (Limits-retained) but it's not locally recallable.
1091
- // - `quiet` → buffer ambient as pull-only; an @mention remains automatic. Overrides global
1092
- // `focus` so "retain this channel, but only surface ambient on explicit pull" stays expressible.
1093
- // Focus (global, only when NOT overridden): channel ambient AND @mentions are acked-and-dropped —
1094
- // they stay recallable via cotal_inbox (recallAmbient), or if recall itself cannot vouch for the
1095
- // channel (replay=off, or a wildcard join — #977), that is reported rather than left silent; an
1096
- // @mention still *wakes* (mention-wake), body pulled (F4=B), never auto-injected (the mention
1097
- // tag is payload-forgeable).
1098
- if (item.kind === "channel") {
1099
- const cm = this.channelModes.get(item.channel ?? "");
1100
- // #662: an id-less delivery is counted, not excluded, since recall can only match it by content.
1101
- // Content cannot tell a late copy of one recall handed back from a new identical message, so
1102
- // every copy takes its own disposition, and a late copy surfaces again rather than a new one
1103
- // losing its lane (#624: an empty id asserts no identity).
1104
- const digest = item.id === "" && (this.enteringFocus || this._attention === "focus") ? idlessDigest(m) : undefined;
1105
- const tally = digest === undefined ? undefined : this.reserveIdless(digest, item.channel ?? "");
1106
- const copy = tally ? this.settleIdless(tally) : undefined;
1107
- const idless = copy && { digest: digest, copy };
1108
- // chatFrontier() is asynchronous. Channel traffic retained while entering focus must not also
1109
- // appear in post-watermark recall if it lands after the server captured the frontier.
1110
- if (this.enteringFocus)
1111
- this.excludeFromFocus(item);
1112
- if (this.dropUnsafe) {
1113
- this.excludeFromFocus(item);
1114
- if (cm !== "muted" && item.mentionsMe && !item.historical) {
1115
- // Mentions are an untrusted wake hint, never authority to inject a channel body.
1116
- // Keep the body in the bounded pull lane while normal ambient stays fail-closed.
1117
- // After exhaustion an unknown late copy can surface once, disclosed by the warning.
1118
- this.buffer(item, delivery, true, idless);
1119
- this.emit("mention-wake", item);
1120
- }
1121
- else
1122
- delivery.ack();
1123
- return;
1124
- }
1125
- if (cm === "muted") {
1126
- this.evictedClassifications.delete(item.id);
1127
- this.excludeFromFocus(item);
1128
- this.protectDisposition(item.id, "drop");
1129
- delivery.ack();
1130
- return;
1131
- }
1132
- const remembered = item.id === "" ? undefined : this.evictedClassifications.get(item.id); // #624: an empty id never carries a remembered classification
1133
- if (remembered)
1134
- this.evictedClassifications.delete(item.id);
1135
- const snapshottedPullOnly = item.historical || (!item.mentionsMe && (remembered?.pullOnly ?? cm === "quiet"));
1136
- if (cm === "quiet" || snapshottedPullOnly)
1137
- this.excludeFromFocus(item);
1138
- // An unsafe channel cannot recall even a fresh mention. Keep that body in the existing
1139
- // bounded pull lane, never the automatic lane, including on quiet channels. Muted and
1140
- // historical traffic retain their earlier gates. Exclude this id so recall cannot repeat it.
1141
- if (this._attention === "focus" && item.mentionsMe && !item.historical && this.focusRecallUnsafeChannels.has(item.channel ?? "")) {
1142
- this.excludeFromFocus(item);
1143
- this.buffer(item, delivery, true, idless);
1144
- this.emit("mention-wake", item);
1145
- return;
1146
- }
1147
- // Current normal+focus remains the stronger hard gate unless this exact id was previously
1148
- // received pull-only. classificationUnsafe must not turn focus-dropped traffic into a buffer.
1149
- if (cm !== "quiet" && !snapshottedPullOnly && this._attention === "focus") {
1150
- this.protectDisposition(item.id, "drop");
1151
- delivery.ack();
1152
- // #662: recall cannot pick this copy out from an identical one with another disposition, so
1153
- // its body is held here, pull-only, and recall hands back only copies nothing is bound to.
1154
- if (item.id === "")
1155
- this.buffer(item, { ack: () => { } }, true, idless);
1156
- if (item.mentionsMe)
1157
- this.emit("mention-wake", item);
1158
- return;
1159
- }
1160
- // Every historical channel message is pull-only, including mentions. Joining a channel must
1161
- // not revive an old request as a fresh turn. This is decided before focus handling too, so
1162
- // history stays recallable without emitting mention-wake.
1163
- // A reply on a channel is pull-only too when the host answers every turn on the channel
1164
- // (config.channelRepliesPullOnly): that answer replies to the message that started the turn,
1165
- // so a turn on a peer's reply is a turn on its automatic output, and two such seats on one
1166
- // channel would answer each other without end (#2395).
1167
- const automaticReply = this.config.channelRepliesPullOnly === true && !!item.replyTo;
1168
- const pullOnly = snapshottedPullOnly || (!item.mentionsMe && (automaticReply || this.classificationUnsafe));
1169
- if (pullOnly)
1170
- this.excludeFromFocus(item);
1171
- this.buffer(item, delivery, pullOnly, idless);
1172
- return;
1173
- }
1174
- this.buffer(item, delivery, false);
1175
- }
1176
819
  /** Record a DM or role request as a peer's message to us: its sender becomes addressable by name,
1177
820
  * and it waits for an answer unless it answers our own question. */
1178
821
  noteDirected(item) {
@@ -1189,294 +832,19 @@ export class MeshAgent extends EventEmitter {
1189
832
  this.unanswered.delete(this.unanswered.keys().next().value);
1190
833
  }
1191
834
  }
1192
- buffer(item, delivery, pullOnly, idless) {
1193
- const pending = { item, ack: delivery.ack, pullOnly, receivedAt: Date.now(), arrival: this.arrivals++ };
1194
- this.own(pending, delivery.redelivery);
1195
- if (idless)
1196
- pending.idless = { tally: this.focusIdless, ...idless };
1197
- if (delivery.take) {
1198
- // Held until its take is recorded. A copy beyond the bound is left unacked, so the broker
1199
- // redelivers it once there is room, as the overflow valve below does for directed mail.
1200
- pending.take = delivery.take;
1201
- this.gated.push(pending);
1202
- if (this.gated.length > MAX_INBOX)
1203
- this.gated.shift();
1204
- this.emit("incoming", item);
1205
- return;
1206
- }
1207
- this.admit(pending);
1208
- this.emit("incoming", item);
1209
- }
1210
- /** Put one entry in the shown inbox at its arrival place, running the overflow valve. */
1211
- admit(pending) {
1212
- let at = this.inbox.length;
1213
- while (at > 0 && this.inbox[at - 1].arrival > pending.arrival)
1214
- at--;
1215
- this.inbox.splice(at, 0, pending);
1216
- if (this.inbox.length > MAX_INBOX) {
1217
- // Prefer sacrificing pull-only backlog so it cannot crowd out DMs/mentions. Overflow is bounded
1218
- // local loss for channel traffic only: an evicted channel item is acked without being marked
1219
- // handled. An evicted directed item is never acked (below).
1220
- let excess = this.inbox.length - MAX_INBOX;
1221
- while (excess-- > 0) {
1222
- // Eviction order, weakest claim first. The tiers below `pullOnly` also protect against live
1223
- // mention floods: `mentionsMe` derives from the payload `mentions` field, forgeable by the
1224
- // sender. A peer that stamps every flooded message with the victim's name
1225
- // makes none of its traffic pull-only, so `findIndex` returned -1, the old code fell through
1226
- // to index 0, and index 0 is the OLDEST entry: a directed message waiting to be handled. It
1227
- // was spliced out and acked unhandled, which is unrecoverable (#791). Ambient channel chatter
1228
- // at volume did the same thing with no forgery at all.
1229
- //
1230
- // So directedness decides who gets sacrificed, and directedness is read from `kind`, which is
1231
- // derived from the delivering subject and broker-policed rather than from the payload.
1232
- //
1233
- // A held entry is never sacrificed: the host's verdict commits through its ack handle, and its
1234
- // renewal is what keeps the broker from redelivering it meanwhile. Evicting one would drop
1235
- // both, handing a long turn's role request to a second worker while the first still runs it
1236
- // and leaving the first one's completion nothing to ack. Holds are capped at
1237
- // {@link MAX_IN_FLIGHT}, so the buffer stays bounded.
1238
- const evictable = (p) => !this.inFlightIds.has(p.item.recvKey);
1239
- let index = this.inbox.findIndex((p) => p.pullOnly && evictable(p));
1240
- // Channel traffic before anything addressed to us specifically, forged mentions included.
1241
- if (index < 0)
1242
- index = this.inbox.findIndex((p) => p.item.kind === "channel" && evictable(p));
1243
- // Only when the whole buffer is directed mail does the oldest lose, and that case carries no
1244
- // attacker advantage: a peer cannot force DMs it is not authorised to send.
1245
- if (index < 0)
1246
- index = this.inbox.findIndex(evictable);
1247
- if (index < 0)
1248
- break;
1249
- const [evicted] = this.inbox.splice(index, 1);
1250
- const sacrificingDirected = evicted.item.kind !== "channel";
1251
- this.rememberEvicted(evicted);
1252
- // #662: a held id-less message is no longer settled here, so recall may hand it back.
1253
- if (evicted.idless?.tally === this.focusIdless)
1254
- this.unsettleIdless(evicted.idless.digest, evicted.idless.copy);
1255
- // A directed message is never acked on overflow: leaving it un-acked lets JetStream redeliver
1256
- // it once we have room, which turns unrecoverable loss into a delay. Channel ambient is still
1257
- // acked, because replaying it is what the history flood was (#775).
1258
- //
1259
- // The broker paces that delay, so nothing here has to end it. An un-acked message comes
1260
- // back only after its durable's ack wait, so an inbox that stays full costs one redelivery
1261
- // per parked message per ack wait, and the message stays pending until a redelivery finds
1262
- // room: a DM on the recipient's own durable (`cotal deliver pending` counts it), a role
1263
- // request on its role's shared queue (which that command does not read). Giving up on
1264
- // it instead, as a per-id eviction count once did (#807), acked a message the sender had
1265
- // seen stored and the live recipient never read, with one stderr line as the only trace:
1266
- // a seat whose turn ran long lost its oldest DMs exactly that way (#2215).
1267
- if (!sacrificingDirected)
1268
- evicted.ack();
1269
- }
1270
- }
1271
- }
1272
- rememberEvicted(p) {
1273
- if (p.item.kind !== "channel" || p.item.mentionsMe || !p.item.channel)
1274
- return;
1275
- if (p.item.id === "")
1276
- return; // #624: an empty id is never a key; one delivery's classification must not inherit to another
1277
- if (this.classificationUnsafe)
1278
- return;
1279
- if (!this.evictedClassifications.has(p.item.id) && this.evictedClassifications.size >= CLASSIFICATION_CAP) {
1280
- this.classificationUnsafe = true;
1281
- this.evictedClassifications.clear();
1282
- return;
1283
- }
1284
- this.evictedClassifications.set(p.item.id, { pullOnly: p.pullOnly, channel: p.item.channel });
1285
- }
1286
- excludeFromFocus(item) {
1287
- if ((!this.enteringFocus && this._attention !== "focus") || item.kind !== "channel" || !item.channel)
1288
- return;
1289
- if (item.id === "")
1290
- return; // #662: an id-less delivery is counted in focusIdless instead
1291
- if (!this.focusExcludedIds.has(item.id) && this.focusExcludedIds.size >= FOCUS_EXCLUSION_CAP) {
1292
- const oldest = this.focusExcludedIds.entries().next().value;
1293
- if (oldest) {
1294
- this.focusExcludedIds.delete(oldest[0]);
1295
- this.focusRecallUnsafeChannels.add(oldest[1]);
1296
- }
1297
- }
1298
- this.focusExcludedIds.set(item.id, item.channel);
1299
- }
1300
- /** #662: the tally for one more settled or handed-back copy of an id-less message; the caller adds
1301
- * the copy. At the bound, the channel's recall is reported incomplete rather than risk handing
1302
- * back a copy it no longer counts. */
1303
- reserveIdless(digest, channel) {
1304
- if (this.focusIdlessEntries >= FOCUS_EXCLUSION_CAP) {
1305
- this.focusRecallUnsafeChannels.add(channel);
1306
- return undefined;
1307
- }
1308
- let tally = this.focusIdless.get(digest);
1309
- if (!tally)
1310
- this.focusIdless.set(digest, (tally = { channel, unbound: [], bound: new Set() }));
1311
- this.focusIdlessEntries++;
1312
- return tally;
1313
- }
1314
- /** #662: add one settled copy, bounded by the chat frontier read after it arrived. If that read
1315
- * fails, or answers only after the transport dropped, nothing bounds the copy, so the channel's
1316
- * recall is reported incomplete instead. */
1317
- settleIdless(tally) {
1318
- const episode = this.focusIdless;
1319
- const drops = this.transportDrops;
1320
- const unbounded = () => {
1321
- if (episode === this.focusIdless)
1322
- this.focusRecallUnsafeChannels.add(tally.channel);
1323
- };
1324
- const copy = { epoch: this.idlessEpoch, ready: Promise.resolve() };
1325
- copy.ready = this.ep.chatFrontier().then((frontier) => {
1326
- if (drops === this.transportDrops)
1327
- copy.frontier = frontier;
1328
- else
1329
- unbounded();
1330
- }, unbounded);
1331
- tally.unbound.push(copy);
1332
- return copy;
1333
- }
1334
- /** #662: forget a tally with nothing left in it, so the map is bounded by the entries it counts. */
1335
- dropEmptyIdless(digest, tally) {
1336
- if (!tally.unbound.length && !tally.bound.size)
1337
- this.focusIdless.delete(digest);
1338
- }
1339
- /** #662: one buffered copy of an id-less message left the inbox unhandled. Only that copy is
1340
- * un-counted, or the sequence it was bound to freed: an identical copy may have been muted. A copy
1341
- * no read has bound yet stays in arrival order, marked, so the read binds it to its own stream
1342
- * copy rather than letting an earlier identical copy take that one. */
1343
- unsettleIdless(digest, copy) {
1344
- const tally = this.focusIdless.get(digest);
1345
- if (!tally)
1346
- return;
1347
- if (tally.unbound.includes(copy)) {
1348
- copy.evicted = true;
1349
- return;
1350
- }
1351
- if (copy.seq === undefined || !tally.bound.delete(copy.seq))
1352
- return;
1353
- this.focusIdlessEntries--;
1354
- this.dropEmptyIdless(digest, tally);
1355
- }
1356
- /** #662: after a complete read of `channel` that started at `epoch`, a copy settled before it that
1357
- * it could not bind will not be in a later read either, so it is dropped. The read is the
1358
- * channel's whole retained window, so a bound sequence it lacks aged out. */
1359
- settleIdlessRead(channel, epoch, retained) {
1360
- for (const [digest, tally] of this.focusIdless) {
1361
- if (tally.channel !== channel)
1362
- continue;
1363
- const before = tally.unbound.length + tally.bound.size;
1364
- tally.unbound = tally.unbound.filter((c) => c.epoch >= epoch);
1365
- for (const seq of tally.bound)
1366
- if (!retained.has(seq))
1367
- tally.bound.delete(seq);
1368
- this.focusIdlessEntries -= before - tally.unbound.length - tally.bound.size;
1369
- this.dropEmptyIdless(digest, tally);
1370
- }
1371
- }
1372
- /** #662: the frontier reads of every copy of `channel` settled so far have answered. */
1373
- async idlessReady(tallies, channel) {
1374
- const reads = [];
1375
- for (const tally of tallies.values())
1376
- if (tally.channel === channel)
1377
- for (const c of tally.unbound)
1378
- reads.push(c.ready);
1379
- await Promise.all(reads);
1380
- }
1381
- /** #662: bind the copies settled before the read that started at `epoch` to its unbound stream
1382
- * copies `free` (ascending). Identical copies share a sender subject, so they arrive in stream
1383
- * order, and a frontier, read after its copy arrived, bounds that copy and every copy before it.
1384
- * Frontier reads can answer out of order, so their sizes say nothing about arrival order. So the
1385
- * copies bind in arrival order, latest first, each to the latest free stream copy at or below the
1386
- * lowest frontier from it on, and a stream copy none of them takes (a reconnect gap) stays free.
1387
- * An evicted copy takes its own stream copy and leaves it free, so recall hands that one back. */
1388
- bindIdless(tally, epoch, free) {
1389
- let bound = Infinity;
1390
- for (let i = tally.unbound.length - 1; i >= 0; i--) {
1391
- const copy = tally.unbound[i];
1392
- if (copy.frontier === undefined)
1393
- continue;
1394
- bound = Math.min(bound, copy.frontier);
1395
- if (copy.epoch >= epoch)
1396
- continue;
1397
- let at = free.length - 1;
1398
- while (at >= 0 && free[at] > bound)
1399
- at--;
1400
- if (at < 0)
1401
- continue;
1402
- const [seq] = free.splice(at, 1);
1403
- tally.unbound.splice(i, 1);
1404
- if (copy.evicted) {
1405
- this.focusIdlessEntries--;
1406
- continue;
1407
- }
1408
- copy.seq = seq;
1409
- tally.bound.add(seq);
1410
- }
1411
- }
1412
- /** #662: a copy settled after the read that started at `epoch` began may or may not be in it, so it
1413
- * binds nothing there, and an unbound stream copy at `seq` it could be is neither free nor bound. */
1414
- idlessUnsure(tally, epoch, seq) {
1415
- if (!tally || tally.bound.has(seq))
1416
- return false;
1417
- return tally.unbound.some((c) => c.epoch >= epoch && c.frontier !== undefined && c.frontier >= seq);
1418
- }
1419
- protectDisposition(id, disposition) {
1420
- if (id === "")
1421
- return; // #624: an empty id is never a dedup key, so it is never recorded as one
1422
- const ids = disposition === "drop" ? this.protectedDropIds : this.protectedPullOnlyIds;
1423
- if ((disposition === "drop" ? this.dropUnsafe : this.classificationUnsafe) || ids.has(id))
1424
- return;
1425
- if (ids.size >= PROTECTED_DISPOSITION_CAP) {
1426
- if (disposition === "drop")
1427
- this.dropUnsafe = true;
1428
- else
1429
- this.classificationUnsafe = true;
1430
- // Keep known hard drops suppressed across later mode changes. The set stays bounded.
1431
- if (disposition !== "drop")
1432
- ids.clear();
1433
- return;
1434
- }
1435
- ids.add(id);
1436
- }
1437
- /** Normalize a wire message into an {@link InboxItem}. `kind` is the **authenticated** class
1438
- * from {@link MessageMeta} (subject-derived), never the forgeable payload `to`/`toService`;
1439
- * core has already normalized `channel` and `toService` from the authenticated chat and svc
1440
- * subjects. Shared by live ingest and focus recall ({@link recallAmbient}). */
1441
- toInboxItem(m, kind, historical) {
1442
- const text = partsToText(m.parts);
1443
- return {
1444
- id: m.id,
1445
- // The wire id when there is one; a minted opaque key when the id is the empty string (#624:
1446
- // an empty id is never a dedup key, so it is never an address either; but the delivery still
1447
- // needs to be individually drainable/ackable, or it can never clear and never commit).
1448
- recvKey: m.id !== "" ? m.id : `${this.recvKeySecret}.${++this.recvKeySeq}`,
1449
- ts: m.ts,
1450
- fromId: m.from.id,
1451
- fromName: m.from.name,
1452
- fromRole: m.from.role,
1453
- kind,
1454
- channel: m.channel,
1455
- service: m.toService,
1456
- mentions: m.mentions,
1457
- mentionsMe: m.mentions?.includes(this.config.name.toLowerCase()) ?? false,
1458
- historical,
1459
- text,
1460
- replyTo: m.replyTo,
1461
- contextId: m.contextId,
1462
- };
1463
- }
1464
835
  /** Return pending messages in stable receive order. Automatic delivery excludes quiet ambient;
1465
836
  * pull-only is the explicit cotal_inbox lane.
1466
837
  *
1467
838
  * A role request whose take is not yet recorded is never returned (SPEC §8.1): a host shows one
1468
839
  * only through {@link takeChosen}. */
1469
840
  peekInbox(scope = "all") {
1470
- this.dropLapsedTakes();
1471
- return this.inbox.filter((p) => this.inScope(p, scope)).map((p) => p.item);
841
+ return this.inboxState.peekInbox(scope);
1472
842
  }
1473
843
  /** Every pending message in scope as {@link peekInbox} will return it once the held role requests
1474
844
  * that may be asked about now are taken, in that order. It is for choosing what to show, never for
1475
845
  * showing: hand the choice to {@link takeChosen}. */
1476
846
  inboxPreview(scope = "all") {
1477
- this.dropLapsedTakes();
1478
- const askable = scope === "pull-only" ? [] : this.gated.filter((p) => this.mayOffer(p));
1479
- return [...this.inbox.filter((p) => this.inScope(p, scope)), ...askable].sort((a, b) => a.arrival - b.arrival).map((p) => p.item);
847
+ return this.inboxState.inboxPreview(scope);
1480
848
  }
1481
849
  /** Choose what to show from {@link inboxPreview}, ask the delivery daemon to record the take of
1482
850
  * each held role request in the choice, wait up to `waitMs` for the answers, and return the chosen
@@ -1484,141 +852,12 @@ export class MeshAgent extends EventEmitter {
1484
852
  * about to see the request, which ends its sender's chance to cancel it, so call this just before
1485
853
  * showing and choose only what will be shown. A request whose take is refused or not yet decided
1486
854
  * is left out. Never throws. */
1487
- async takeChosen(scope, choose, waitMs = TAKE_WAIT_MS) {
1488
- const chosen = choose(this.inboxPreview(scope));
1489
- const keys = new Set(chosen.map((i) => i.recvKey));
1490
- const asked = scope === "pull-only" ? [] : this.gated
1491
- .filter((p) => keys.has(p.item.recvKey) && this.mayOffer(p));
1492
- for (const p of asked)
1493
- p.awaited = (p.awaited ?? 0) + 1;
1494
- const runs = asked.map((p) => (p.taking !== undefined && p.takingDrops === this.transportDrops ? p.taking : this.startTake(p)));
1495
- if (runs.length) {
1496
- let timer;
1497
- const waited = new Promise((resolve) => { timer = setTimeout(resolve, waitMs); timer.unref?.(); });
1498
- await Promise.race([Promise.allSettled(runs), waited]);
1499
- clearTimeout(timer);
1500
- }
1501
- for (const p of asked)
1502
- p.awaited = (p.awaited ?? 1) - 1;
1503
- const showable = new Map(this.peekInbox(scope).map((i) => [i.recvKey, i]));
1504
- return chosen.flatMap((i) => showable.get(i.recvKey) ?? []);
855
+ async takeChosen(scope, choose, waitMs) {
856
+ return this.inboxState.takeChosen(scope, choose, waitMs);
1505
857
  }
1506
858
  /** How many role requests are held because their take is pending or could not be decided, and why. */
1507
859
  heldTakes() {
1508
- const count = this.gated.filter((p) => p.taking === undefined).length;
1509
- const waiting = this.gated.filter((p) => p.taking !== undefined && p.takingDrops === this.transportDrops).length;
1510
- if (waiting)
1511
- return {
1512
- count: waiting + (this.heldTake ? count : 0),
1513
- detail: "the delivery daemon has not answered the take yet",
1514
- };
1515
- return count && this.heldTake ? { count, detail: this.heldTake.detail } : { count };
1516
- }
1517
- /** Whether a held role request may be asked about now: the link is up, and no take nobody could
1518
- * decide is being waited out ({@link heldTake}). */
1519
- mayAskTakes() {
1520
- return this._transportConnected && (!this.heldTake || Date.now() >= this.heldTake.retryAt);
1521
- }
1522
- /** Whether a read may be offered a held role request now: the link is up and its take is out
1523
- * already, or a take may be asked ({@link mayAskTakes}). A take that is out when the link drops
1524
- * cannot admit the request until the link is back, so a read then is offered nothing of it. */
1525
- mayOffer(p) {
1526
- return (p.taking !== undefined && this._transportConnected) || this.mayAskTakes();
1527
- }
1528
- startTake(p) {
1529
- const run = this.runTake(p).catch((e) => this.handleEndpointWarning(e)).finally(() => {
1530
- if (p.taking === run)
1531
- p.taking = undefined;
1532
- });
1533
- p.taking = run;
1534
- p.takingDrops = this.transportDrops;
1535
- return run;
1536
- }
1537
- /** Take one held role request for the copy it holds, and admit it only if it still holds that copy
1538
- * once the take is recorded: the same delivery, its ack wait not run out, the link not dropped. */
1539
- async runTake(p) {
1540
- const take = p.take;
1541
- if (!take)
1542
- return;
1543
- // The copy is renewed before the take is asked for, so the take cannot outlive it. A copy whose
1544
- // ack wait already ran out may be another holder's now: drop it unacked and let the broker redeliver.
1545
- if (!this._transportConnected)
1546
- return;
1547
- if (!this.renewOwned(p)) {
1548
- this.dropGated(p);
1549
- return;
1550
- }
1551
- const count = p.item.deliveryCount;
1552
- const drops = this.transportDrops;
1553
- const outcome = await take();
1554
- if (!this.gated.includes(p) || p.take !== take)
1555
- return;
1556
- // An answer that crossed a link drop is the old link's, so the request stays held and a read
1557
- // waiting on this take shows nothing of it. Its host is told again once the link is up, so the
1558
- // next read takes it on the new link. While the link is down a read could not take it, and the
1559
- // read that waited on this take may still be running, so the reconnect announces it. A read
1560
- // that asked again on the new link owns the request now, so this take says nothing.
1561
- if (this.transportDrops !== drops) {
1562
- if (p.takingDrops !== drops)
1563
- return;
1564
- if (this._transportConnected)
1565
- this.emit("incoming", p.item);
1566
- return;
1567
- }
1568
- if (outcome.taken) {
1569
- this.heldTake = undefined;
1570
- const current = p.item.deliveryCount === count && p.ownedUntil !== undefined && Date.now() < p.ownedUntil;
1571
- if (!current)
1572
- return; // a newer copy arrived meanwhile and needs its own take
1573
- p.takenCount = count;
1574
- p.takenOnce = true;
1575
- this.gated.splice(this.gated.indexOf(p), 1);
1576
- this.noteDirected(p.item);
1577
- this.admit(p);
1578
- // Its arrival was announced already. A read waiting on this take shows it next; one that
1579
- // stopped waiting is told it may be shown now, as of a new arrival.
1580
- if (!p.awaited)
1581
- this.emit("incoming", p.item);
1582
- return;
1583
- }
1584
- if (outcome.drop) {
1585
- this.dropGated(p);
1586
- return;
1587
- }
1588
- const first = this.heldTake?.reason !== outcome.reason;
1589
- this.heldTake = { reason: outcome.reason, detail: outcome.detail, retryAt: Date.now() + TAKE_RETRY_MS };
1590
- // Nothing asks about a held request on its own, so its host hears of it again when it may.
1591
- this.takeRetry ??= setTimeout(() => {
1592
- this.takeRetry = undefined;
1593
- for (const held of this.gated)
1594
- this.emit("incoming", held.item);
1595
- }, TAKE_RETRY_MS);
1596
- this.takeRetry.unref?.();
1597
- if (first && outcome.reason !== "daemon-busy")
1598
- this.handleEndpointWarning(new Error(outcome.detail));
1599
- }
1600
- /** Forget a role request whose take was refused or whose copy ran out. It is never acked: a
1601
- * cancelled one is gone already, and one another holder took must stay with that holder. */
1602
- dropGated(p) {
1603
- const i = this.gated.indexOf(p);
1604
- if (i >= 0)
1605
- this.gated.splice(i, 1);
1606
- }
1607
- /** Move a taken role request back to the held set when its copy changed before it was shown. */
1608
- regate(p) {
1609
- const i = this.inbox.indexOf(p);
1610
- if (i < 0)
1611
- return;
1612
- this.inbox.splice(i, 1);
1613
- p.takenCount = undefined;
1614
- this.gated.push(p);
1615
- }
1616
- /** Drop every taken role request not yet shown whose copy ran out: the broker may have handed it
1617
- * to another holder, whose take of the later copy wins. One the host holds is renewed and kept. */
1618
- dropLapsedTakes() {
1619
- const now = Date.now();
1620
- this.inbox = this.inbox.filter((p) => !p.take || this.inFlightIds.has(p.item.recvKey) || (p.ownedUntil !== undefined && now < p.ownedUntil));
1621
- this.gated = this.gated.filter((p) => p.taking !== undefined || p.ownedUntil === undefined || now < p.ownedUntil);
860
+ return this.inboxState.heldTakes();
1622
861
  }
1623
862
  /** Mark a surfaced batch as mid-delivery, so the overflow valve will not evict it out from under
1624
863
  * the host. Pair with {@link releaseInFlight} on the delivery verdict, whichever way it goes.
@@ -1635,117 +874,23 @@ export class MeshAgent extends EventEmitter {
1635
874
  * with the overflow valve free to ack them. Declining to surface costs a deferral: the messages
1636
875
  * stay buffered and go out on a later frame, once a verdict releases capacity. */
1637
876
  holdInFlight(ids) {
1638
- // #624: the keys here are RECEIVE keys. An id-less delivery's wire id is "", so raw-id keying
1639
- // merged the counts of DISTINCT empty-id deliveries, and the eviction valve's in-flight check
1640
- // then deferred acking an evicted id-less item while any other id-less batch was held:
1641
- // cross-delivery interference in the safe direction, but interference. Receive keys are unique
1642
- // per delivery, so each is protected exactly for the frames holding it.
1643
- let fresh = 0;
1644
- for (const id of ids)
1645
- if (!this.inFlightIds.has(id))
1646
- fresh++;
1647
- if (this.inFlightIds.size + fresh > MAX_IN_FLIGHT)
1648
- return false;
1649
- for (const id of ids) {
1650
- this.inFlightIds.set(id, (this.inFlightIds.get(id) ?? 0) + 1);
1651
- this.keepOwned(id);
1652
- }
1653
- return true;
877
+ return this.inboxState.holdInFlight(ids);
1654
878
  }
1655
879
  /** Whether this id is currently protected from the overflow valve — i.e. some frame is mid-delivery
1656
880
  * holding it. Read-only observability for the property {@link holdInFlight} establishes. A caller
1657
881
  * that needs a batch to actually BE in flight before it acts must wait on this, never on a sleep:
1658
882
  * the handoff runs through a real relay process whose latency is not the caller's to predict. */
1659
883
  isInFlight(id) {
1660
- return this.inFlightIds.has(id);
884
+ return this.inboxState.isInFlight(id);
1661
885
  }
1662
886
  /** One frame's verdict is in (either way). The id is ordinary backlog again only once EVERY frame
1663
887
  * holding it has reported — a release from one must not speak for another still in flight. */
1664
888
  releaseInFlight(ids) {
1665
- for (const id of ids) {
1666
- const held = this.inFlightIds.get(id);
1667
- if (held === undefined)
1668
- continue;
1669
- if (held <= 1) {
1670
- this.inFlightIds.delete(id);
1671
- this.stopRenewal(id);
1672
- }
1673
- else
1674
- this.inFlightIds.set(id, held - 1);
1675
- }
1676
- }
1677
- own(p, redelivery) {
1678
- p.redelivery = redelivery;
1679
- p.ownedUntil = redelivery && Date.now() + redelivery.ackWaitMs;
1680
- p.item.deliveryCount = redelivery?.deliveryCount;
1681
- }
1682
- /** Keep a held durable delivery with this reader for as long as the hold lasts, by restarting its
1683
- * ack wait at half the wait. Only a HELD delivery is renewed: a queued one keeps the broker's
1684
- * timer, so a reader busy with other work does not sit on a role request another reader could
1685
- * take, and an abandoned one (released) runs out and redelivers. */
1686
- keepOwned(key) {
1687
- if (this.renewals.has(key))
1688
- return;
1689
- const p = this.inbox.find((x) => x.item.recvKey === key);
1690
- if (!p?.redelivery || !this.renewOwned(p))
1691
- return;
1692
- const timer = setInterval(() => {
1693
- const held = this.inbox.find((x) => x.item.recvKey === key);
1694
- if (!held || !this.renewOwned(held))
1695
- this.stopRenewal(key);
1696
- }, p.redelivery.ackWaitMs / 2);
1697
- timer.unref?.();
1698
- this.renewals.set(key, timer);
1699
- }
1700
- stopRenewal(key) {
1701
- clearInterval(this.renewals.get(key));
1702
- this.renewals.delete(key);
1703
- }
1704
- /** Restart one copy's ack wait, unless this reader can no longer vouch that it still holds that
1705
- * copy. JetStream acks by stream sequence rather than by delivery, so a renewal sent after the
1706
- * wait may have run out (a stalled loop, a link down for longer than the wait) would extend the
1707
- * copy the broker has since handed to another reader. Renewal stops instead and the copy is the
1708
- * broker's to redeliver. Nothing is sent while the link is down, because the client buffers a
1709
- * publish and would replay it after the wait had run out; a handle whose connection was torn
1710
- * down throws, which is the same lost ownership. */
1711
- renewOwned(p) {
1712
- if (!p.redelivery || p.ownedUntil === undefined || Date.now() >= p.ownedUntil)
1713
- return false;
1714
- if (!this._transportConnected)
1715
- return true;
1716
- try {
1717
- p.redelivery.working();
1718
- }
1719
- catch {
1720
- return false;
1721
- }
1722
- p.ownedUntil = Date.now() + p.redelivery.ackWaitMs;
1723
- return true;
889
+ return this.inboxState.releaseInFlight(ids);
1724
890
  }
1725
891
  /** Return scoped pending messages and ack them — call only when they're actually surfaced. */
1726
892
  drainInbox(limit, scope = "all") {
1727
- this.dropLapsedTakes();
1728
- const eligible = this.inbox.filter((p) => this.inScope(p, scope));
1729
- const n = limit && limit > 0 ? Math.min(limit, eligible.length) : eligible.length;
1730
- const selected = eligible.slice(0, n);
1731
- // Head progress is read from the LIVE queue, so it must be recorded while the batch is still in
1732
- // it (#1526).
1733
- this.noteAutomaticHeadProgress(selected);
1734
- // Remove by OBJECT IDENTITY, not by a set of wire ids (#624): the id-less entries share "", so
1735
- // an id set would remove every id-less neighbor beyond the limit and outside the scope while
1736
- // acking only the selected — silent loss by selection. Identity removes exactly what was taken.
1737
- const taken = new Set(selected);
1738
- this.inbox = this.inbox.filter((p) => !taken.has(p));
1739
- const automatic = selected.some((p) => !p.pullOnly);
1740
- const items = this.commitPending(selected);
1741
- if (items.length)
1742
- this._lastInboxDrainedAt = Date.now();
1743
- // Automatic progress is recorded from the SELECTED entries' own classification, not from the
1744
- // returned items: `commitPending` returns `InboxItem`s, which do not carry `pullOnly`, and
1745
- // counting a pull-only drain as automatic progress is the #1233 lie in miniature.
1746
- if (items.length && automatic)
1747
- this._lastAutomaticDrainedAt = Date.now();
1748
- return items;
893
+ return this.inboxState.drainInbox(limit, scope);
1749
894
  }
1750
895
  /** Ack exact surfaced deliveries without assuming they still form the physical inbox prefix.
1751
896
  * Takes RECEIVE keys ({@link InboxItem.recvKey}): the wire id for real messages, a minted key
@@ -1755,84 +900,21 @@ export class MeshAgent extends EventEmitter {
1755
900
  * requested key whose item is present is acked and (for a real id) marked handled, including an
1756
901
  * item overflow-evicted during the turn. */
1757
902
  drainInboxDeliveries(keys) {
1758
- const requested = [...new Set(keys)];
1759
- const wanted = new Set(requested);
1760
- // A role request the host surfaced can be back in the held set when its copy changed since. The
1761
- // host showed it, so its commit acks it from there too. One never taken was never shown, so it
1762
- // stays held: it is not acked, marked handled or reported missing.
1763
- const regated = this.gated.filter((p) => wanted.has(p.item.recvKey) && p.takenOnce);
1764
- const unshown = new Set(this.gated.filter((p) => wanted.has(p.item.recvKey) && !p.takenOnce).map((p) => p.item.recvKey));
1765
- if (regated.length)
1766
- this.gated = this.gated.filter((p) => !regated.includes(p));
1767
- const selected = [...this.inbox.filter((p) => wanted.has(p.item.recvKey)), ...regated];
1768
- this.noteAutomaticHeadProgress(selected);
1769
- const present = new Set(selected.map((p) => p.item.recvKey));
1770
- const pullOnly = new Map(selected.map((p) => [p.item.recvKey, p.pullOnly]));
1771
- for (const id of requested) {
1772
- const remembered = id !== "" ? this.evictedClassifications.get(id) : undefined;
1773
- if (!pullOnly.has(id) && remembered)
1774
- pullOnly.set(id, remembered.pullOnly);
1775
- }
1776
- this.inbox = this.inbox.filter((p) => !present.has(p.item.recvKey));
1777
- const automatic = selected.some((p) => !p.pullOnly);
1778
- const items = this.commitPending(selected);
1779
- if (items.length)
1780
- this._lastInboxDrainedAt = Date.now();
1781
- if (items.length && automatic)
1782
- this._lastAutomaticDrainedAt = Date.now();
1783
- for (const id of requested) {
1784
- // A MINTED key (an id-less delivery) is never handled-authority: its wire id is "", which
1785
- // markHandled already refuses, so skipping it here is the same at-least-once stance rather
1786
- // than a new one. Recording the minted key itself would pollute handledIds with a string a
1787
- // future REAL id could legitimately equal, arming the exact suppression this change removes.
1788
- if (!present.has(id) && !unshown.has(id) && !id.startsWith(`${this.recvKeySecret}.`)) {
1789
- this.markHandled(id, pullOnly.get(id) ?? false);
1790
- this.evictedClassifications.delete(id);
1791
- }
1792
- }
1793
- return { items, missingKeys: requested.filter((key) => !present.has(key) && !unshown.has(key)) };
1794
- }
1795
- commitPending(taken) {
1796
- for (const p of taken) {
1797
- p.ack();
1798
- this.markHandled(p.item.id, p.pullOnly);
1799
- this.evictedClassifications.delete(p.item.id);
1800
- }
1801
- return taken.map((p) => p.item);
1802
- }
1803
- inScope(p, scope) {
1804
- return scope === "all" || (scope === "pull-only" ? p.pullOnly : !p.pullOnly);
1805
- }
1806
- /** Record an id as surfaced/handled, for {@link ingest}'s commit-aware cross-path dedup. Bounded via
1807
- * two rotating windows: when the live set fills, it becomes the previous window and a fresh one
1808
- * starts — so memory stays ~2× the cap while the lookup horizon never shrinks below it. */
1809
- markHandled(id, pullOnly = false) {
1810
- if (id === "")
1811
- return; // #624: an empty id is never a dedup key, so it is never recorded as one
1812
- if (pullOnly)
1813
- this.protectDisposition(id, "pull-only");
1814
- this.handledIds.add(id);
1815
- if (this.handledIds.size >= 4096) {
1816
- this.handledIdsPrev = this.handledIds;
1817
- this.handledIds = new Set();
1818
- }
903
+ return this.inboxState.drainInboxDeliveries(keys);
1819
904
  }
1820
905
  /** Receive keys of the pending messages in scope, role requests held for their take included,
1821
906
  * for bookkeeping that shows no body. */
1822
907
  inboxKeys(scope = "all") {
1823
- const held = scope === "pull-only" ? [] : this.gated;
1824
- return [...this.inbox.filter((p) => this.inScope(p, scope)), ...held].map((p) => p.item.recvKey);
908
+ return this.inboxState.inboxKeys(scope);
1825
909
  }
1826
910
  /** The pending messages in scope, role requests held for their take included, for a notice that
1827
911
  * counts them and shows no body. A held request is shown only through {@link takeChosen}. */
1828
912
  inboxPending(scope = "all") {
1829
- const held = scope === "pull-only" ? [] : this.gated;
1830
- return [...this.inbox.filter((p) => this.inScope(p, scope)), ...held].map((p) => p.item);
913
+ return this.inboxState.inboxPending(scope);
1831
914
  }
1832
915
  /** Pending messages in scope, counting role requests still held for their take. */
1833
916
  inboxCount(scope = "all") {
1834
- const held = scope === "pull-only" ? 0 : this.gated.length;
1835
- return held + (scope === "all" ? this.inbox.length : this.inbox.filter((p) => this.inScope(p, scope)).length);
917
+ return this.inboxState.inboxCount(scope);
1836
918
  }
1837
919
  /**
1838
920
  * Read a bounded presentation snapshot without draining, acknowledging, committing, holding or
@@ -1841,23 +923,11 @@ export class MeshAgent extends EventEmitter {
1841
923
  * read-only.
1842
924
  */
1843
925
  inboxSnapshot(limit = 3) {
1844
- if (!Number.isInteger(limit) || limit < 0 || limit > 8)
1845
- throw new Error(`inbox preview limit must be a whole number from 0 to 8, got ${limit}`);
1846
- // A held role request is counted and never previewed: its body is shown only once a host takes
1847
- // it to show it (SPEC §8.1).
1848
- this.dropLapsedTakes();
1849
- return { unread: this.inbox.length + this.gated.length, items: this.inbox.slice(0, limit).map((p) => ({ ...p.item })) };
926
+ return this.inboxState.inboxSnapshot(limit);
1850
927
  }
1851
928
  /** Local receive time of the oldest still-buffered automatic delivery, if any. */
1852
929
  oldestAutomaticReceivedAt() {
1853
- let oldest;
1854
- for (const pending of [...this.inbox, ...this.gated]) {
1855
- if (pending.pullOnly)
1856
- continue;
1857
- if (oldest === undefined || pending.receivedAt < oldest)
1858
- oldest = pending.receivedAt;
1859
- }
1860
- return oldest;
930
+ return this.inboxState.oldestAutomaticReceivedAt();
1861
931
  }
1862
932
  /**
1863
933
  * How far this session has read the focus-mode channel recall.
@@ -1880,7 +950,7 @@ export class MeshAgent extends EventEmitter {
1880
950
  * so no sender can push it past the messages it has not read yet.
1881
951
  */
1882
952
  get recallCursor() {
1883
- return this._recallCursor;
953
+ return this.inboxState.recallCursor;
1884
954
  }
1885
955
  /**
1886
956
  * Record the last recall item actually handed to the caller.
@@ -1891,8 +961,7 @@ export class MeshAgent extends EventEmitter {
1891
961
  * its own, so the next call resumes strictly after the last one delivered.
1892
962
  */
1893
963
  noteRecalled(mark) {
1894
- if (afterRecallMark(mark, this._recallCursor))
1895
- this._recallCursor = mark;
964
+ return this.inboxState.noteRecalled(mark);
1896
965
  }
1897
966
  /**
1898
967
  * Does this recall item claim a time this session has not reached?
@@ -1917,11 +986,11 @@ export class MeshAgent extends EventEmitter {
1917
986
  * rather than reasoning about it.
1918
987
  */
1919
988
  recallAhead(item) {
1920
- return item.ts > Date.now();
989
+ return this.inboxState.recallAhead(item);
1921
990
  }
1922
991
  /** Has this future-stamped recall item already been handed over in this session? */
1923
992
  recallAheadSeen(id) {
1924
- return this.aheadDelivered.has(id);
993
+ return this.inboxState.recallAheadSeen(id);
1925
994
  }
1926
995
  /**
1927
996
  * How many more future-stamped recall items this session will take responsibility for.
@@ -1933,38 +1002,22 @@ export class MeshAgent extends EventEmitter {
1933
1002
  * ITS OWN messages and cannot use it to silence anyone else's.
1934
1003
  */
1935
1004
  recallAheadRoom() {
1936
- return MAX_AHEAD - this.aheadDelivered.size;
1005
+ return this.inboxState.recallAheadRoom();
1937
1006
  }
1938
1007
  /** Record that a future-stamped recall item was actually handed to the caller. */
1939
1008
  noteRecalledAhead(id) {
1940
- if (this.aheadDelivered.size < MAX_AHEAD)
1941
- this.aheadDelivered.add(id);
1942
- }
1943
- /**
1944
- * Forget how far a recall walk had read, because the thing it was walking is gone.
1945
- *
1946
- * The mark is session-local, which is a longer life than it can honestly carry: it describes a
1947
- * position in ONE walk over ONE frontier, and {@link setAttention} both drops the frontier on the
1948
- * way out of focus and captures a new one on the way in. Measured before this reset, a mark left
1949
- * over from an earlier focus episode filtered a new episode's messages out of recall whenever they
1950
- * were stamped behind it, which a lagging or a chosen clock produces. The focus watermark was
1951
- * already cleared here; the mark that walks it was not.
1952
- */
1953
- resetRecallWalk() {
1954
- this._recallCursor = { ts: 0, id: "" };
1955
- this.aheadDelivered.clear();
1009
+ return this.inboxState.noteRecalledAhead(id);
1956
1010
  }
1957
1011
  /** Buffered receive-time lane for one delivery, addressed by its receive key. Undefined means it
1958
1012
  * is no longer pending. */
1959
1013
  inboxScope(key) {
1960
- const pending = this.inbox.find((p) => p.item.recvKey === key) ?? this.gated.find((p) => p.item.recvKey === key);
1961
- return pending ? (pending.pullOnly ? "pull-only" : "automatic") : undefined;
1014
+ return this.inboxState.inboxScope(key);
1962
1015
  }
1963
1016
  /** Count of automatic messages that count as directed for a wake decision: real dm/anycast
1964
1017
  * (authenticated kind) or a live channel @mention. Pull-only history never counts, including
1965
1018
  * mentions retained while focused. The Stop→idle flush uses this in `dnd`/`focus`. */
1966
1019
  directedPendingCount() {
1967
- return this.gated.length + this.inbox.filter((p) => !p.pullOnly && (p.item.kind !== "channel" || (!p.item.historical && p.item.mentionsMe))).length;
1020
+ return this.inboxState.directedPendingCount();
1968
1021
  }
1969
1022
  /** Buffered items that should WAKE a Stop→idle flush — the mode-and-channel-aware predicate the
1970
1023
  * connectors use instead of branching on attention themselves:
@@ -1984,21 +1037,7 @@ export class MeshAgent extends EventEmitter {
1984
1037
  * turns. A surface that cannot surface a run turn, such as the remote gateway, waits on this, so
1985
1038
  * a turn it can never clear does not end every wait at once. */
1986
1039
  inboxWake(skip) {
1987
- // A role request held for its take wakes when a read could take it: the read the wake leads to
1988
- // takes it. While the link is down, or a take nobody could decide is waited out, none wakes, and
1989
- // each is announced again when the link is back or the wait ends (see runTake). An in-flight take
1990
- // already owns its next announcement; an old-link take must not block a new read after reconnect.
1991
- // A copy whose ack wait ran out is dropped first, as a read drops it.
1992
- this.dropLapsedTakes();
1993
- const held = this.mayAskTakes() ? this.gated.filter((p) => (p.taking === undefined || p.takingDrops !== this.transportDrops) && !skip?.(p.item)).length : 0;
1994
- return held + this.inbox.filter((p) => {
1995
- const it = p.item;
1996
- if (p.pullOnly || (it.kind === "channel" && it.historical) || skip?.(it))
1997
- return false;
1998
- if (it.kind !== "channel" || it.mentionsMe)
1999
- return true;
2000
- return this._attention === "open";
2001
- }).length;
1040
+ return this.inboxState.inboxWake(skip);
2002
1041
  }
2003
1042
  /** Ask any push layer (the channel) to wake the session now — used by the Stop→idle flush
2004
1043
  * to deliver a batch of held messages. Emits `"wake"`; a no-op if nothing listens. Never acks
@@ -2011,15 +1050,15 @@ export class MeshAgent extends EventEmitter {
2011
1050
  /** This agent's global attention mode. Authoritative here; mirrored to presence (advisory) so peers
2012
1051
  * can see it. Delivery never reads it back from presence — local state wins. */
2013
1052
  get attention() {
2014
- return this._attention;
1053
+ return this.inboxState.attention;
2015
1054
  }
2016
1055
  /** This agent's per-channel override for `channel` (undefined ⇒ follow the global mode). */
2017
1056
  channelMode(channel) {
2018
- return channel ? this.channelModes.get(channel) : undefined;
1057
+ return this.inboxState.channelMode(channel);
2019
1058
  }
2020
1059
  /** A snapshot of every per-channel override (for the at-a-glance views). */
2021
1060
  channelModeEntries() {
2022
- return Object.fromEntries(this.channelModes);
1061
+ return this.inboxState.channelModeEntries();
2023
1062
  }
2024
1063
  /** Set (or clear, with `"normal"`) one channel's attention override. Validates the channel is
2025
1064
  * concrete and within this agent's read ACL (`allowSubscribe` — so a mode can be pre-set for a
@@ -2032,34 +1071,7 @@ export class MeshAgent extends EventEmitter {
2032
1071
  * enters another turn and both stay readable with cotal_inbox. `normal` makes nothing automatic
2033
1072
  * again. */
2034
1073
  async setChannelMode(channel, mode) {
2035
- if (!isConcreteChannel(channel))
2036
- throw new Error(`"${channel}" must be a concrete channel (no wildcard) to set its attention`);
2037
- if (!channelInAllow(this.config.allowSubscribe, channel))
2038
- throw new Error(`"${channel}" is not within your read ACL (allowSubscribe) [${this.config.allowSubscribe.join(", ")}]`);
2039
- await this.inOrder(() => {
2040
- if (mode === "normal")
2041
- this.channelModes.delete(channel);
2042
- else {
2043
- this.channelModes.set(channel, mode);
2044
- this.lowerBuffered(channel, mode === "muted");
2045
- }
2046
- return this.ep.setChannelModes(this.channelModeEntries());
2047
- });
2048
- }
2049
- /** Make the automatic entries buffered from `channel` (a concrete channel or a pattern) pull-only,
2050
- * mentions of this agent too when `mentions` is set, except on a channel `keep` still covers. An
2051
- * entry a host is handing over keeps its lane, since the turn carrying it acks it. */
2052
- lowerBuffered(channel, mentions, keep = []) {
2053
- for (const p of this.inbox) {
2054
- if (p.pullOnly || p.item.kind !== "channel" || !p.item.channel || !channelInAllow([channel], p.item.channel))
2055
- continue;
2056
- if (channelInAllow(keep, p.item.channel))
2057
- continue;
2058
- if ((p.item.mentionsMe && !mentions) || this.inFlightIds.has(p.item.recvKey))
2059
- continue;
2060
- p.pullOnly = true;
2061
- this.excludeFromFocus(p.item);
2062
- }
1074
+ return this.inboxState.setChannelMode(channel, mode);
2063
1075
  }
2064
1076
  /** Set the attention mode. Entering `focus` captures the chat frontier as the focus-watermark
2065
1077
  * (recall surfaces ambient published after it); leaving focus clears it. Requires a live
@@ -2069,42 +1081,7 @@ export class MeshAgent extends EventEmitter {
2069
1081
  * it landed just before or after the captured frontier. Only ambient arriving after the local
2070
1082
  * switch is ack-dropped. */
2071
1083
  async setAttention(mode) {
2072
- await this.inOrder(async () => {
2073
- if (mode === "focus") {
2074
- await this.requireConnected();
2075
- this.focusExcludedIds.clear();
2076
- this.focusIdless = new Map();
2077
- this.focusIdlessEntries = 0;
2078
- this.focusRecallUnsafeChannels.clear();
2079
- this.enteringFocus = this._attention !== "focus";
2080
- try {
2081
- this.focusSince = await this.ep.chatFrontier();
2082
- }
2083
- catch (error) {
2084
- this.enteringFocus = false;
2085
- this.focusExcludedIds.clear();
2086
- this.focusIdless = new Map();
2087
- this.focusIdlessEntries = 0;
2088
- this.focusRecallUnsafeChannels.clear();
2089
- throw error;
2090
- }
2091
- this.enteringFocus = false;
2092
- this.resetRecallWalk();
2093
- }
2094
- else {
2095
- this.enteringFocus = false;
2096
- this.focusSince = undefined;
2097
- this.focusExcludedIds.clear();
2098
- this.focusIdless = new Map();
2099
- this.focusIdlessEntries = 0;
2100
- this.focusRecallUnsafeChannels.clear();
2101
- this.resetRecallWalk();
2102
- }
2103
- this._attention = mode;
2104
- // Mirror to presence (advisory observability — peers can see "they're in focus"). Best-effort:
2105
- // a no-op until the KV is bound, and never read back into delivery.
2106
- await this.ep.setAttention(mode);
2107
- });
1084
+ return this.inboxState.setAttention(mode);
2108
1085
  }
2109
1086
  /** Focus recall: the channel ambient + @mentions ack-dropped since this agent entered focus,
2110
1087
  * read back from the chat stream on demand and **replay-gated per channel** (a `replay=off`
@@ -2119,113 +1096,7 @@ export class MeshAgent extends EventEmitter {
2119
1096
  * part: an exclusion that lands after that does not hide one, since recall would then move past
2120
1097
  * the rest of it (#613). */
2121
1098
  async recallAmbient(underway = new Set()) {
2122
- const run = this.recallLock.then(() => this.recallAmbientOnce(underway));
2123
- this.recallLock = run.catch(() => { });
2124
- return run;
2125
- }
2126
- async recallAmbientOnce(underway) {
2127
- if (this._attention !== "focus" || this.focusSince === undefined)
2128
- return { items: [], droppedChannels: [] };
2129
- const items = [];
2130
- const droppedChannels = [];
2131
- for (const channel of this.ep.joinedChannels()) {
2132
- if (!isConcreteChannel(channel)) {
2133
- droppedChannels.push(channel);
2134
- continue;
2135
- }
2136
- if (this.focusRecallUnsafeChannels.has(channel)) {
2137
- droppedChannels.push(channel);
2138
- continue;
2139
- }
2140
- const tallies = this.focusIdless;
2141
- let epoch = 0;
2142
- let read;
2143
- // #662: a copy settled while a read runs may or may not be in it, so the read is retried with
2144
- // that copy settled first, a bounded number of times.
2145
- for (let attempt = 1; attempt <= IDLESS_READS; attempt++) {
2146
- epoch = ++this.idlessEpoch;
2147
- // The read starts after every copy settled before it knows its frontier, so a copy it
2148
- // cannot bind is behind the focus start or out of retention.
2149
- await this.idlessReady(tallies, channel);
2150
- read = await this.ep.recallChannel(channel, this.focusSince);
2151
- if (read.unanswered)
2152
- break;
2153
- await this.idlessReady(tallies, channel); // copies settled during the read
2154
- const unsure = read.messages.some((m, i) => m.id === "" && this.idlessUnsure(tallies.get(idlessDigest(m)), epoch, read.seqs[i]));
2155
- if (!unsure)
2156
- break;
2157
- }
2158
- const { messages, seqs, dropped, unanswered } = read;
2159
- // A read that did not answer says nothing about what the channel retains, so nothing settles.
2160
- if (unanswered) {
2161
- droppedChannels.push(channel);
2162
- continue;
2163
- }
2164
- if (this.focusRecallUnsafeChannels.has(channel)) {
2165
- droppedChannels.push(channel);
2166
- continue;
2167
- }
2168
- // #662: each copy settled before the read is bound to one retained stream copy at or below its
2169
- // frontier. A copy no settled copy is bound to was never received (a reconnect gap) or was
2170
- // evicted; it is handed back into the inbox, which acks each by receive key, since the recall
2171
- // mark cannot order identical copies.
2172
- const retained = new Set();
2173
- const digests = [];
2174
- const free = new Map();
2175
- for (const [i, m] of messages.entries()) {
2176
- if (m.id !== "" || tallies !== this.focusIdless)
2177
- continue;
2178
- retained.add(seqs[i]);
2179
- const digest = (digests[i] = idlessDigest(m));
2180
- if (tallies.get(digest)?.bound.has(seqs[i]))
2181
- continue;
2182
- const list = free.get(digest);
2183
- if (list)
2184
- list.push(seqs[i]);
2185
- else
2186
- free.set(digest, [seqs[i]]);
2187
- }
2188
- for (const [digest, list] of free) {
2189
- const tally = tallies.get(digest);
2190
- if (tally)
2191
- this.bindIdless(tally, epoch, list);
2192
- }
2193
- let incomplete = dropped;
2194
- for (const [i, m] of messages.entries()) {
2195
- if (m.id !== "") {
2196
- if (underway.has(m.id) || !this.focusExcludedIds.has(m.id))
2197
- items.push(this.toInboxItem(m, "channel", true));
2198
- continue;
2199
- }
2200
- if (tallies !== this.focusIdless)
2201
- continue; // focus was left or re-entered during the read
2202
- const seq = seqs[i];
2203
- const digest = digests[i];
2204
- const tally = tallies.get(digest);
2205
- if (tally?.bound.has(seq))
2206
- continue;
2207
- // Still unsure after the last read: neither claimed nor handed back, and reported.
2208
- if (this.idlessUnsure(tally, epoch, seq)) {
2209
- incomplete = true;
2210
- continue;
2211
- }
2212
- // A full inbox leaves it in the stream for a later call, rather than evict something else.
2213
- const fresh = this.inbox.length < MAX_INBOX ? this.reserveIdless(digest, channel) : undefined;
2214
- if (!fresh) {
2215
- incomplete = true;
2216
- continue;
2217
- }
2218
- const copy = { epoch, frontier: seq, ready: Promise.resolve(), seq };
2219
- fresh.bound.add(seq);
2220
- this.buffer(this.toInboxItem(m, "channel", true), { ack: () => { } }, true, { digest, copy });
2221
- }
2222
- if (tallies === this.focusIdless)
2223
- this.settleIdlessRead(channel, epoch, retained);
2224
- if (incomplete)
2225
- droppedChannels.push(channel);
2226
- }
2227
- items.sort((a, b) => a.ts - b.ts);
2228
- return { items, droppedChannels };
1099
+ return this.inboxState.recallAmbient(underway);
2229
1100
  }
2230
1101
  // ---- sending -------------------------------------------------------------
2231
1102
  async send(text, channel, mentions) {
@@ -2245,21 +1116,27 @@ export class MeshAgent extends EventEmitter {
2245
1116
  * already existed (joined, registry, or prior traffic) and close matches when it
2246
1117
  * did not. Does not refuse create. Graded before multicast so the new message cannot
2247
1118
  * make the name look pre-existing.
1119
+ *
1120
+ * #7401: only a channel list that was read supports calling the name new, as with mentions
1121
+ * (#1229). When the read fails, `unchecked` carries its error and the name is neither known
1122
+ * nor new; joined and registered names still count as known and still feed the hints.
2248
1123
  */
2249
1124
  async describeSendChannel(channel) {
2250
1125
  if (this.ep.getChannelConfig(channel))
2251
1126
  return { known: true, hints: [] };
2252
1127
  const known = new Set(this.ep.joinedChannels().filter(isConcreteChannel));
1128
+ let unchecked;
2253
1129
  try {
2254
1130
  for (const row of await this.ep.listChannels())
2255
1131
  known.add(row.channel);
2256
1132
  }
2257
- catch {
2258
- /* no stream: joined + registry cache still distinguish a typo of a channel we are on */
1133
+ catch (e) {
1134
+ unchecked = e.message;
2259
1135
  }
2260
1136
  if (known.has(channel))
2261
1137
  return { known: true, hints: [] };
2262
- return { known: false, hints: closeChannelNames(channel, [...known]) };
1138
+ const hints = closeChannelNames(channel, [...known]);
1139
+ return unchecked === undefined ? { known: false, hints } : { known: false, hints, unchecked };
2263
1140
  }
2264
1141
  /** Throw if any name isn't a peer we've observed. Validates against the FULL roster
2265
1142
  * (incl. self — your own name is a valid participant; resolvePeer's self-filter would
@@ -3768,7 +2645,7 @@ export class MeshAgent extends EventEmitter {
3768
2645
  : "this space's host runs an older delivery service, so this durable leave was not applied and the channel stays joined; the host must update the delivery service";
3769
2646
  const result = await this.ep.leaveChannel(channel, legacyDiscoveryMessage);
3770
2647
  if (result.left)
3771
- this.lowerBuffered(channel, false, this.joinedChannels());
2648
+ this.inboxState.lowerBuffered(channel, false, this.joinedChannels());
3772
2649
  return result;
3773
2650
  }
3774
2651
  // ---- internals -----------------------------------------------------------
@@ -3786,6 +2663,8 @@ export class MeshAgent extends EventEmitter {
3786
2663
  async whenConnected(timeoutMs = CONNECT_GRACE_MS) {
3787
2664
  if (this._connected)
3788
2665
  return;
2666
+ // An endpoint waiting out its rebuild backoff would not try again inside this window (#7408).
2667
+ this.ep.retryNow();
3789
2668
  if (!(await this.awaitConnection(timeoutMs)))
3790
2669
  throw new Error(this.notConnectedMessage());
3791
2670
  }