@sjawhar/pi-envoy 17.7.0 → 17.9.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/envoy.js CHANGED
@@ -29706,6 +29706,9 @@ function agentSubject(session) {
29706
29706
  return `${AGENT_TOPIC_PREFIX}${session}`;
29707
29707
  }
29708
29708
  var GHOSTWISPR_TOPIC_PREFIX = "notifications.ghostwispr.";
29709
+ function uncapturedTopicRefusal(topic, sessionID) {
29710
+ return `${topic} was not registered: no stream subject captures it, so nothing would be delivered to ${sessionID} on it; follow a notifications.team.<name> topic instead`;
29711
+ }
29709
29712
 
29710
29713
  // ../contracts/src/legion-roles.ts
29711
29714
  var GATED_STEPS = ["decisions", "criteria", "spec"];
@@ -37465,58 +37468,6 @@ async function postDeliveryReply(config, sessionId, delivery, result) {
37465
37468
  ...reply
37466
37469
  }, followUp === undefined ? {} : { followUp });
37467
37470
  }
37468
- function expectsLaneReceipt(frame) {
37469
- return frame.reply !== undefined && frame.reply !== "" && frame.subject === frame.directSubject && frame.envelopeTopic !== undefined && frame.envelopeTopic !== frame.directSubject;
37470
- }
37471
- async function* readArrivals(subscription, now = Date.now) {
37472
- const source = subscription[Symbol.asyncIterator]();
37473
- const arrived = [];
37474
- let stopped;
37475
- let left = false;
37476
- let wake;
37477
- const signal = () => {
37478
- wake?.();
37479
- wake = undefined;
37480
- };
37481
- (async () => {
37482
- try {
37483
- for (;; ) {
37484
- const read = await source.next();
37485
- if (read.done === true || left)
37486
- break;
37487
- arrived.push({ message: read.value, receivedAt: now() });
37488
- signal();
37489
- }
37490
- stopped = { failed: false };
37491
- } catch (error) {
37492
- stopped = { error, failed: true };
37493
- }
37494
- signal();
37495
- })();
37496
- try {
37497
- for (;; ) {
37498
- const next = arrived.shift();
37499
- if (next !== undefined) {
37500
- yield next;
37501
- continue;
37502
- }
37503
- if (stopped?.failed === true)
37504
- throw stopped.error;
37505
- if (stopped !== undefined)
37506
- return;
37507
- const { promise, resolve } = Promise.withResolvers();
37508
- wake = resolve;
37509
- await promise;
37510
- }
37511
- } finally {
37512
- if (stopped === undefined) {
37513
- left = true;
37514
- Promise.resolve(source.return?.()).catch(() => {
37515
- return;
37516
- });
37517
- }
37518
- }
37519
- }
37520
37471
  var DedupeIdentitySchema = object({
37521
37472
  event_id: string2().min(1).optional(),
37522
37473
  dedupe_key: string2().min(1).optional(),
@@ -38255,6 +38206,267 @@ function subscriptionRemovedTopics(raw, sessionID) {
38255
38206
  return parsedPayload.data.topics ?? [];
38256
38207
  }
38257
38208
 
38209
+ // ../envoy-client/src/delivery-ledger.ts
38210
+ var DELIVERY_LEDGER_TTL_MS = DELIVERY_DUPLICATE_WINDOW_MS;
38211
+ var DELIVERY_LEDGER_LIMIT = 20000;
38212
+ var LedgerEntrySchema = object({
38213
+ event_id: string2().min(1).optional(),
38214
+ dedupe_key: string2().min(1).optional(),
38215
+ at: number2()
38216
+ });
38217
+ var LedgerStateSchema = object({
38218
+ topics: array(string2()).default([])
38219
+ });
38220
+ var LedgerSnapshotSchema = object({
38221
+ state: LedgerStateSchema.optional(),
38222
+ ledger: array(LedgerEntrySchema)
38223
+ });
38224
+ function createDeliveryLedger(options) {
38225
+ const { host } = options;
38226
+ const now = options.now ?? Date.now;
38227
+ const ledger = createLedger(now);
38228
+ let topics = [];
38229
+ let unwritten = [];
38230
+ let writtenState;
38231
+ let writes = Promise.resolve();
38232
+ let epoch = 0;
38233
+ const safely = (what, call) => {
38234
+ try {
38235
+ call();
38236
+ } catch (error) {
38237
+ try {
38238
+ host.log(`delivery ledger: ${what} failed`, error);
38239
+ } catch {}
38240
+ }
38241
+ };
38242
+ const log = (message, error) => safely("log", () => host.log(message, error));
38243
+ const sessionRead = (read) => () => {
38244
+ try {
38245
+ return read();
38246
+ } catch {
38247
+ return;
38248
+ }
38249
+ };
38250
+ const currentSession = sessionRead(() => host.sessionID());
38251
+ const writeSession = sessionRead(() => host.writeSessionID === undefined ? host.sessionID() : host.writeSessionID());
38252
+ const bindTo = (sessionID, read) => {
38253
+ if (sessionID === undefined || sessionID === "" || read() !== sessionID)
38254
+ return;
38255
+ const bound = epoch;
38256
+ return { sessionID, alive: () => bound === epoch && read() === sessionID };
38257
+ };
38258
+ const bind = () => bindTo(currentSession(), currentSession);
38259
+ const stateJSON = () => JSON.stringify({ topics: [...topics].sort() });
38260
+ const record = (identity) => {
38261
+ const entry = ledger.record(identity);
38262
+ if (entry !== undefined)
38263
+ unwritten.push(entry);
38264
+ };
38265
+ const flush = (alive) => {
38266
+ const written = writes.then(async () => {
38267
+ if (!alive())
38268
+ return false;
38269
+ const json = stateJSON();
38270
+ const delivered = unwritten;
38271
+ if (json === writtenState && delivered.length === 0)
38272
+ return true;
38273
+ unwritten = [];
38274
+ const state = json === writtenState ? undefined : JSON.parse(json);
38275
+ try {
38276
+ await host.write({
38277
+ delivered,
38278
+ state,
38279
+ snapshot: () => ({ state: JSON.parse(json), ledger: ledger.list() })
38280
+ });
38281
+ if (state !== undefined)
38282
+ writtenState = json;
38283
+ return true;
38284
+ } catch (error) {
38285
+ unwritten = [...delivered, ...unwritten];
38286
+ log("delivery ledger: could not persist the state; the next write carries it", error);
38287
+ return false;
38288
+ }
38289
+ });
38290
+ writes = written.then(() => {
38291
+ return;
38292
+ });
38293
+ return written;
38294
+ };
38295
+ const syncFollowed = () => {
38296
+ topics = [...new Set(host.followedSubjects())];
38297
+ };
38298
+ const failureNotice = (envelope, error) => ({
38299
+ eventId: envelope.event_id ?? "unknown",
38300
+ sender: senderLabel(envelope),
38301
+ summary: envelope.payload_summary ?? "unknown",
38302
+ publishedAt: inboundTimestamp(envelope.issued_at),
38303
+ error,
38304
+ readWith: envelope.source === "dispatch" ? "dispatch read" : "envoy_inbox",
38305
+ envelope
38306
+ });
38307
+ const showFailureNotice = async (sessionID, notice, alive) => {
38308
+ let queued;
38309
+ try {
38310
+ queued = await host.showFailureNotice(sessionID, notice) === "queued";
38311
+ } catch (error) {
38312
+ if (alive())
38313
+ safely("failure notice warning", () => host.failureNoticeFailed(notice, error));
38314
+ return false;
38315
+ }
38316
+ if (queued || !alive())
38317
+ return true;
38318
+ record(ledgerIdentity(notice.envelope));
38319
+ await flush(alive);
38320
+ return true;
38321
+ };
38322
+ return {
38323
+ async followedChanged() {
38324
+ const alive = bind()?.alive;
38325
+ if (alive === undefined)
38326
+ return;
38327
+ try {
38328
+ syncFollowed();
38329
+ await flush(alive);
38330
+ } catch (error) {
38331
+ log("delivery ledger: could not take the followed subjects", error);
38332
+ }
38333
+ },
38334
+ delivered: (frame) => ledger.has(ledgerIdentity(frame)),
38335
+ async recordDelivered(frame) {
38336
+ const alive = bind()?.alive;
38337
+ if (alive === undefined)
38338
+ return;
38339
+ record(ledgerIdentity(frame));
38340
+ await flush(alive);
38341
+ },
38342
+ async recordDeliveredFor(sessionID, frame) {
38343
+ const alive = bindTo(sessionID, writeSession)?.alive;
38344
+ if (alive === undefined)
38345
+ return false;
38346
+ record(ledgerIdentity(frame));
38347
+ return await flush(alive);
38348
+ },
38349
+ async liveFailed(sessionID, envelope, error) {
38350
+ const alive = bindTo(sessionID, currentSession)?.alive;
38351
+ if (alive === undefined) {
38352
+ log("delivery ledger: a failed frame was not reported here; the session it names is closed");
38353
+ return false;
38354
+ }
38355
+ try {
38356
+ return await showFailureNotice(sessionID, failureNotice(envelope, messageFor(error)), alive);
38357
+ } catch (thrown) {
38358
+ log("delivery ledger: could not report a frame the session was not shown", thrown);
38359
+ return false;
38360
+ }
38361
+ },
38362
+ restore(snapshot) {
38363
+ epoch += 1;
38364
+ ledger.restore(snapshot.ledger);
38365
+ topics = [...new Set(snapshot.state?.topics ?? [])];
38366
+ writtenState = snapshot.state === undefined ? undefined : stateJSON();
38367
+ unwritten = [];
38368
+ }
38369
+ };
38370
+ }
38371
+ var STORED_WHILE_AWAY_SUFFIX = "while this session's connection was down.";
38372
+ function storedText(content, publishedAt) {
38373
+ return `${content}
38374
+ Published at ${publishedAt} ${STORED_WHILE_AWAY_SUFFIX}`;
38375
+ }
38376
+ function failureNoticeFields(notice) {
38377
+ const readWith = notice.readWith === "dispatch read" ? "Read it with the `dispatch read` command on the issue or ask it concerns." : "Read it with the envoy_inbox tool.";
38378
+ return {
38379
+ notice: "message not shown",
38380
+ event_id: notice.eventId,
38381
+ from: notice.sender,
38382
+ summary: notice.summary,
38383
+ published_at: notice.publishedAt,
38384
+ error: notice.error,
38385
+ detail: `This session could not be shown this message, so it is not coming back on its own. ${readWith}`
38386
+ };
38387
+ }
38388
+ function envoyNoticeText(fields) {
38389
+ return encode5({ envoy: fields });
38390
+ }
38391
+ async function closeReplayedRemoval(frame, host) {
38392
+ const closed = (subscriptionRemovedTopics(frame.data, host.sessionID) ?? []).filter((topic) => host.relay?.followedBefore(topic, frame.storedAtMs) !== false && host.close(topic));
38393
+ if (closed.length === 0)
38394
+ return closed;
38395
+ const unregistering = host.unregister(closed);
38396
+ await host.followedChanged();
38397
+ await unregistering;
38398
+ return closed;
38399
+ }
38400
+ function ledgerIdentity(frame) {
38401
+ return {
38402
+ ...frame.event_id === undefined || frame.event_id === "" ? {} : { event_id: frame.event_id },
38403
+ ...frame.dedupe_key === undefined || !dedupeKeyNamesItsEvent(frame) ? {} : { dedupe_key: frame.dedupe_key }
38404
+ };
38405
+ }
38406
+ function createLedger(now) {
38407
+ let entries = new Set;
38408
+ const byEvent = new Map;
38409
+ const byKey = new Map;
38410
+ const drop = (entry) => {
38411
+ entries.delete(entry);
38412
+ if (entry.event_id !== undefined && byEvent.get(entry.event_id) === entry) {
38413
+ byEvent.delete(entry.event_id);
38414
+ }
38415
+ if (entry.dedupe_key !== undefined && byKey.get(entry.dedupe_key) === entry) {
38416
+ byKey.delete(entry.dedupe_key);
38417
+ }
38418
+ };
38419
+ const add = (entry) => {
38420
+ if (entries.size >= DELIVERY_LEDGER_LIMIT) {
38421
+ const oldest = entries.values().next();
38422
+ if (oldest.done !== true)
38423
+ drop(oldest.value);
38424
+ }
38425
+ entries.add(entry);
38426
+ if (entry.event_id !== undefined)
38427
+ byEvent.set(entry.event_id, entry);
38428
+ if (entry.dedupe_key !== undefined)
38429
+ byKey.set(entry.dedupe_key, entry);
38430
+ };
38431
+ const expire = () => {
38432
+ const at = now();
38433
+ for (const entry of entries) {
38434
+ if (at - entry.at < DELIVERY_LEDGER_TTL_MS)
38435
+ break;
38436
+ drop(entry);
38437
+ }
38438
+ };
38439
+ const has = (identity) => {
38440
+ expire();
38441
+ return identity.event_id !== undefined && byEvent.has(identity.event_id) || identity.dedupe_key !== undefined && byKey.has(identity.dedupe_key);
38442
+ };
38443
+ return {
38444
+ has,
38445
+ record(identity) {
38446
+ if (identity.event_id === undefined && identity.dedupe_key === undefined)
38447
+ return;
38448
+ if (has(identity))
38449
+ return;
38450
+ const entry = { ...identity, at: now() };
38451
+ add(entry);
38452
+ return entry;
38453
+ },
38454
+ restore(restored) {
38455
+ entries = new Set;
38456
+ byEvent.clear();
38457
+ byKey.clear();
38458
+ const at = now();
38459
+ const kept = restored.filter((entry) => at - entry.at < DELIVERY_LEDGER_TTL_MS && (entry.event_id !== undefined || entry.dedupe_key !== undefined)).sort((a, b) => a.at - b.at);
38460
+ for (const entry of kept)
38461
+ add({ ...entry });
38462
+ },
38463
+ list() {
38464
+ expire();
38465
+ return [...entries];
38466
+ }
38467
+ };
38468
+ }
38469
+
38258
38470
  // ../envoy-client/src/stream-topics.ts
38259
38471
  var EXCEPTION_TOPIC_PREFIX = "notifications.envoy.exceptions.";
38260
38472
  var STREAM_SUBJECT_PREFIXES = [
@@ -38265,42 +38477,103 @@ var STREAM_SUBJECT_PREFIXES = [
38265
38477
  LEGION_NOTICE_TOPIC_PREFIX,
38266
38478
  GHOSTWISPR_TOPIC_PREFIX,
38267
38479
  "notifications.whatsapp.",
38268
- `${EXCEPTION_TOPIC_PREFIX}${AGENT_TOPIC_PREFIX}`
38480
+ "notifications.team.",
38481
+ `${EXCEPTION_TOPIC_PREFIX}${AGENT_TOPIC_PREFIX}`,
38482
+ `${EXCEPTION_TOPIC_PREFIX}notifications.team.`
38269
38483
  ];
38270
38484
  function streamStores(topic) {
38271
38485
  return STREAM_SUBJECT_PREFIXES.some((prefix) => topic.length > prefix.length && topic.startsWith(prefix));
38272
38486
  }
38273
-
38274
- // ../envoy-client/src/machine.ts
38275
- import { hostname as hostname3 } from "os";
38276
- function machineID() {
38277
- return process.env["ENVOY_MACHINE_ID"] || process.env["AGENTBOX_HOST"] || hostname3();
38487
+ function streamDeliversNothing(topic) {
38488
+ return topic.startsWith("notifications.") && !topic.startsWith(ROLE_TOPIC_PREFIX) && !streamStores(topic);
38278
38489
  }
38279
-
38280
- // ../envoy-client/src/transport.ts
38281
- var REPLAY_TIMEOUT_MS = 1e4;
38282
- var REPLAY_UNAVAILABLE_CODE = "REPLAY_UNAVAILABLE";
38283
- var SessionStateSchema = _enum2(["live", "unresponsive", "gone"]);
38284
- var UNRESPONSIVE_RULE = "An unresponsive session is not dead: it keeps its role claims and its issue claims, and a " + "message sent to it is stored until it answers. Start no replacement for it and take no claim " + `from it until it is gone, or until ${ISSUE_CLAIM_UNRESPONSIVE_GRACE_MS / 60000} minutes ` + "have passed since its last_seen, the time of its last registration.";
38285
- var WireStateSchema = SessionStateSchema.default("live");
38286
- var InterestWireSchema = object({
38287
- session_id: string2(),
38288
- machine_id: string2(),
38289
- dir: string2(),
38290
- topics: array(string2()),
38291
- updated_at: number2().int().optional(),
38292
- warnings: array(string2()).optional()
38293
- });
38294
- var DeliveryAnswerWireSchema = discriminatedUnion("mode", [
38295
- object({
38296
- mode: literal("consumer"),
38297
- subject: string2().min(1),
38298
- stream_id: string2(),
38299
- gap: boolean2(),
38300
- consumer_start: number2().int().nonnegative().optional()
38301
- }),
38302
- object({ mode: literal("core"), reason: string2() })
38303
- ]);
38490
+ function subscribeRefusesTopic(ownAgentSubject, topic) {
38491
+ return streamDeliversNothing(topic) && !reachesPrivateStreamSubject(ownAgentSubject, topic);
38492
+ }
38493
+ function refuseUncapturedTopics(sessionID, ownAgentSubject, topics) {
38494
+ const uncaptured = topics.filter((topic) => subscribeRefusesTopic(ownAgentSubject, topic));
38495
+ if (uncaptured.length === 0)
38496
+ return;
38497
+ throw new Error(uncaptured.map((topic) => uncapturedTopicRefusal(topic, sessionID)).join("; "));
38498
+ }
38499
+ var PRIVATE_STREAM_SUBJECTS = [
38500
+ `${AGENT_TOPIC_PREFIX}>`,
38501
+ `${LEGION_NOTICE_TOPIC_PREFIX}>`,
38502
+ `${EXCEPTION_TOPIC_PREFIX}${AGENT_TOPIC_PREFIX}>`,
38503
+ `${EXCEPTION_TOPIC_PREFIX}notifications.team.>`
38504
+ ];
38505
+ var CONTROLLER_NOTICE_SUFFIX = ".controller";
38506
+ function controllerNoticeRole(topic) {
38507
+ if (!topic.startsWith(LEGION_NOTICE_TOPIC_PREFIX) || !topic.endsWith(CONTROLLER_NOTICE_SUFFIX)) {
38508
+ return;
38509
+ }
38510
+ const project = topic.slice(LEGION_NOTICE_TOPIC_PREFIX.length, topic.length - CONTROLLER_NOTICE_SUFFIX.length);
38511
+ if (project === "" || /[.*>]/.test(project))
38512
+ return;
38513
+ return `legion-${project}-controller`;
38514
+ }
38515
+ function subjectsOverlap(a, b) {
38516
+ const aTokens = a.split(".");
38517
+ const bTokens = b.split(".");
38518
+ for (let index = 0;index < aTokens.length && index < bTokens.length; index += 1) {
38519
+ const aToken = aTokens[index];
38520
+ const bToken = bTokens[index];
38521
+ if (aToken === ">" || bToken === ">")
38522
+ return true;
38523
+ if (aToken !== "*" && bToken !== "*" && aToken !== bToken)
38524
+ return false;
38525
+ }
38526
+ return aTokens.length === bTokens.length;
38527
+ }
38528
+ function reachesPrivateStreamSubject(ownAgentSubject, topic, roles = []) {
38529
+ if (!PRIVATE_STREAM_SUBJECTS.some((subject) => subjectsOverlap(topic, subject)))
38530
+ return false;
38531
+ const role = controllerNoticeRole(topic);
38532
+ if (role !== undefined)
38533
+ return !roles.includes(role);
38534
+ return topic !== ownAgentSubject || /[*>]/.test(ownAgentSubject);
38535
+ }
38536
+ function registrationTopics(ownAgentSubject, followed, roles = []) {
38537
+ return [
38538
+ ...new Set([
38539
+ ownAgentSubject,
38540
+ ...[...followed].filter((topic) => !reachesPrivateStreamSubject(ownAgentSubject, topic, roles) && !streamDeliversNothing(topic))
38541
+ ])
38542
+ ];
38543
+ }
38544
+ function replayFollowedSubjects(ownAgentSubject, topics, heldForRole = () => false) {
38545
+ const followed = [...topics].filter((topic) => !topic.startsWith(ROLE_TOPIC_PREFIX) && !reachesPrivateStreamSubject(ownAgentSubject, topic) && !heldForRole(topic));
38546
+ return [...new Set([ownAgentSubject, ...followed])];
38547
+ }
38548
+ function rememberedTopics(topics) {
38549
+ return (topics ?? []).filter((topic) => !topic.startsWith(AGENT_TOPIC_PREFIX) && !topic.startsWith(ROLE_TOPIC_PREFIX));
38550
+ }
38551
+
38552
+ // ../envoy-client/src/machine.ts
38553
+ import { hostname as hostname3 } from "os";
38554
+ function machineID() {
38555
+ return process.env["ENVOY_MACHINE_ID"] || process.env["AGENTBOX_HOST"] || hostname3();
38556
+ }
38557
+
38558
+ // ../envoy-client/src/transport.ts
38559
+ var SessionStateSchema = _enum2(["live", "unresponsive", "gone"]);
38560
+ var UNRESPONSIVE_RULE = "An unresponsive session is not dead: it keeps its role claims and its issue claims, and a " + "message sent to it is stored until it answers. Start no replacement for it and take no claim " + `from it until it is gone, or until ${ISSUE_CLAIM_UNRESPONSIVE_GRACE_MS / 60000} minutes ` + "have passed since its last_seen, the time of its last registration.";
38561
+ var WireStateSchema = SessionStateSchema.default("live");
38562
+ var InterestWireSchema = object({
38563
+ session_id: string2(),
38564
+ machine_id: string2(),
38565
+ dir: string2(),
38566
+ topics: array(string2()),
38567
+ updated_at: number2().int().optional(),
38568
+ warnings: array(string2()).optional()
38569
+ });
38570
+ var DeliveryAnswerWireSchema = object({
38571
+ mode: literal("consumer"),
38572
+ subject: string2().min(1),
38573
+ stream_id: string2(),
38574
+ gap: boolean2(),
38575
+ consumer_start: number2().int().nonnegative().optional()
38576
+ });
38304
38577
  var SessionWireSchema = object({
38305
38578
  session_id: string2(),
38306
38579
  machine_id: string2(),
@@ -38315,7 +38588,8 @@ var SessionWireSchema = object({
38315
38588
  last_seen: number2().int().optional(),
38316
38589
  state: WireStateSchema,
38317
38590
  plugin: string2().optional(),
38318
- plugin_version: string2().optional()
38591
+ plugin_version: string2().optional(),
38592
+ delivery: string2().optional()
38319
38593
  });
38320
38594
  var RoleWireSchema = object({
38321
38595
  role: string2(),
@@ -38335,13 +38609,13 @@ var RoleHeldWireSchema = object({
38335
38609
  var ErrorWireSchema = object({
38336
38610
  error: string2(),
38337
38611
  code: string2().optional(),
38612
+ reason: string2().optional(),
38338
38613
  expected: array(string2()).optional(),
38339
38614
  state: SessionStateSchema.optional(),
38340
38615
  holder: string2().optional(),
38341
38616
  holder_state: SessionStateSchema.optional(),
38342
38617
  last_seen: number2().int().optional(),
38343
- claim_released: boolean2().optional(),
38344
- unresponsive: array(string2()).optional()
38618
+ claim_released: boolean2().optional()
38345
38619
  });
38346
38620
  var EnvelopeResponseSchema = EnvelopeSchema.extend({
38347
38621
  recipient: string2().optional(),
@@ -38349,46 +38623,30 @@ var EnvelopeResponseSchema = EnvelopeSchema.extend({
38349
38623
  holder_state: SessionStateSchema.optional(),
38350
38624
  recipient_state: SessionStateSchema.optional(),
38351
38625
  last_seen: number2().int().optional(),
38352
- unresponsive: array(string2()).optional(),
38353
38626
  duplicate: boolean2().optional()
38354
38627
  });
38355
38628
  var PublishResponseSchema = EnvelopeResponseSchema.extend({
38356
38629
  kept: boolean2().optional(),
38357
38630
  kept_reason: _enum2(["unclaimed", "holder_lapsed"]).optional()
38358
38631
  });
38359
- var ReplayWireSchema = object({
38360
- stream: object({
38361
- id: string2().min(1).optional(),
38362
- created: string2(),
38363
- first_seq: number2().int().nonnegative(),
38364
- last_seq: number2().int().nonnegative()
38365
- }),
38366
- subjects: array(object({
38367
- subject: string2(),
38368
- messages: array(object({
38369
- seq: number2().int().positive(),
38370
- subject: string2(),
38371
- time: string2(),
38372
- data: string2()
38373
- })),
38374
- next_after: number2().int().nonnegative(),
38375
- more: boolean2(),
38376
- refused: string2().optional()
38377
- }))
38378
- });
38379
- var DELIVERY_NOT_ANSWERED = "not_answered";
38380
- var DELIVERY_UNREADABLE = "unreadable_answer";
38632
+ var DeliveryRefusalSchema = object({
38633
+ mode: string2(),
38634
+ reason: string2().optional()
38635
+ });
38636
+ var DELIVERY_UNREADABLE = "answered a delivery field this build cannot read";
38381
38637
  function parseSubscribeAnswer(body) {
38382
38638
  const interest = InterestWireSchema.parse(body);
38383
38639
  const raw = typeof body === "object" && body !== null && "delivery" in body ? body.delivery : undefined;
38384
38640
  if (raw === undefined || raw === null) {
38385
- return { ...interest, delivery: { mode: "core", reason: DELIVERY_NOT_ANSWERED } };
38641
+ throw new Error(`the listener answered no delivery for ${interest.session_id}: it predates session consumers, and a session receives only what its own consumer holds`);
38386
38642
  }
38387
38643
  const delivery = DeliveryAnswerWireSchema.safeParse(raw);
38388
- return {
38389
- ...interest,
38390
- delivery: delivery.success ? delivery.data : { mode: "core", reason: DELIVERY_UNREADABLE }
38391
- };
38644
+ if (!delivery.success) {
38645
+ const named = DeliveryRefusalSchema.safeParse(raw);
38646
+ const said = named.success ? `answered delivery mode ${named.data.mode}${named.data.reason === undefined ? "" : ` (${named.data.reason})`}` : DELIVERY_UNREADABLE;
38647
+ throw new Error(`the listener gave ${interest.session_id} no consumer: it ${said}, and a session receives only what its own consumer holds`);
38648
+ }
38649
+ return { ...interest, delivery: delivery.data };
38392
38650
  }
38393
38651
  function mergeInterestSources(registryTopics, liveTopics) {
38394
38652
  const interests = new Map;
@@ -38434,27 +38692,20 @@ function expandSubscriptionTopics(topics) {
38434
38692
  if (segments.length < 2 || segments.some((segment) => segment.length === 0)) {
38435
38693
  throw new TypeError(`cannot expand Envoy wildcard topic "${topic}": its concrete base must have at least two non-empty segments`);
38436
38694
  }
38437
- expanded.add(base);
38695
+ if (!streamDeliversNothing(base))
38696
+ expanded.add(base);
38438
38697
  expanded.add(topic);
38439
38698
  }
38440
38699
  return [...expanded];
38441
38700
  }
38442
38701
  function refusalStateText(parsed) {
38443
- const sentences = [];
38444
38702
  const state = parsed.holder_state ?? parsed.state;
38445
- if (state !== undefined) {
38446
- const who = parsed.holder === undefined ? "That session" : `Holder ${parsed.holder}`;
38447
- const seen = parsed.last_seen === undefined || parsed.last_seen <= 0 ? "" : `, last seen ${new Date(parsed.last_seen).toISOString()}`;
38448
- sentences.push(`${who} is ${state}${seen}.`);
38449
- }
38450
- const stored = parsed.unresponsive ?? [];
38451
- if (stored.length > 0) {
38452
- const one = stored.length === 1;
38453
- sentences.push(`Envoy stored the message for ${stored.join(", ")}.`, `${one ? "That session" : "Those sessions"} stopped answering, so ${one ? "it" : "they"} did not fail this send.`);
38454
- }
38455
- if (sentences.length === 0)
38703
+ if (state === undefined)
38456
38704
  return "";
38457
- if (state === "unresponsive" || stored.length > 0)
38705
+ const who = parsed.holder === undefined ? "That session" : `Holder ${parsed.holder}`;
38706
+ const seen = parsed.last_seen === undefined || parsed.last_seen <= 0 ? "" : `, last seen ${new Date(parsed.last_seen).toISOString()}`;
38707
+ const sentences = [`${who} is ${state}${seen}.`];
38708
+ if (state === "unresponsive")
38458
38709
  sentences.push(UNRESPONSIVE_RULE);
38459
38710
  return ` ${sentences.join(" ")}`;
38460
38711
  }
@@ -38464,11 +38715,11 @@ class EnvoyApiError extends Error {
38464
38715
  name = "EnvoyApiError";
38465
38716
  expected;
38466
38717
  code;
38718
+ reason;
38467
38719
  holder;
38468
38720
  sessionState;
38469
38721
  lastSeen;
38470
38722
  claimReleased;
38471
- unresponsive;
38472
38723
  constructor(details) {
38473
38724
  let parsed;
38474
38725
  try {
@@ -38476,15 +38727,15 @@ class EnvoyApiError extends Error {
38476
38727
  if (result.success)
38477
38728
  parsed = result.data;
38478
38729
  } catch {}
38479
- super(parsed === undefined ? `${details.method} ${details.url} failed with ${details.status}: ${details.responseBody}` : `${parsed.error}${parsed.expected === undefined ? "" : ` (expected: ${parsed.expected.join(", ")})`}${refusalStateText(parsed)}`);
38730
+ super(parsed === undefined ? `${details.method} ${details.url} failed with ${details.status}: ${details.responseBody}` : `${parsed.error}${parsed.reason === undefined ? "" : ` (reason: ${parsed.reason})`}${parsed.expected === undefined ? "" : ` (expected: ${parsed.expected.join(", ")})`}${refusalStateText(parsed)}`);
38480
38731
  this.details = details;
38481
38732
  this.expected = parsed?.expected;
38482
38733
  this.code = parsed?.code;
38734
+ this.reason = parsed?.reason;
38483
38735
  this.holder = parsed?.holder;
38484
38736
  this.sessionState = parsed?.holder_state ?? parsed?.state;
38485
38737
  this.lastSeen = parsed?.last_seen;
38486
38738
  this.claimReleased = parsed?.claim_released;
38487
- this.unresponsive = parsed?.unresponsive;
38488
38739
  }
38489
38740
  }
38490
38741
  var mintedDedupeKey2 = new RegExp(MINTED_DEDUPE_KEY_PATTERN);
@@ -38594,23 +38845,24 @@ function createEnvoyClient(config) {
38594
38845
  headers: { "Content-Type": "application/json" },
38595
38846
  body: JSON.stringify(body)
38596
38847
  }, options);
38848
+ const subscribeBody = (input) => ({
38849
+ session_id: input.sessionID,
38850
+ dir: input.directory,
38851
+ topics: expandSubscriptionTopics(input.topics),
38852
+ port: input.port,
38853
+ title: input.title,
38854
+ ...input.capabilities === undefined ? {} : { capabilities: input.capabilities },
38855
+ driving: input.driving,
38856
+ ...input.selfSubscribed === undefined ? {} : { self_subscribed: input.selfSubscribed },
38857
+ delivery: input.delivery,
38858
+ ...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID },
38859
+ host: machineID(),
38860
+ ...input.place === undefined ? {} : { tmux_place: input.place },
38861
+ plugin: input.plugin.name,
38862
+ plugin_version: input.plugin.version
38863
+ });
38597
38864
  return {
38598
- subscribe: async (input) => parseSubscribeAnswer(JSON.parse(await post("/v1/interests/subscribe", {
38599
- session_id: input.sessionID,
38600
- dir: input.directory,
38601
- topics: expandSubscriptionTopics(input.topics),
38602
- port: input.port,
38603
- title: input.title,
38604
- ...input.capabilities === undefined ? {} : { capabilities: input.capabilities },
38605
- driving: input.driving,
38606
- ...input.selfSubscribed === undefined ? {} : { self_subscribed: input.selfSubscribed },
38607
- ...input.delivery === undefined ? {} : { delivery: input.delivery },
38608
- ...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID },
38609
- host: machineID(),
38610
- ...input.place === undefined ? {} : { tmux_place: input.place },
38611
- plugin: input.plugin.name,
38612
- plugin_version: input.plugin.version
38613
- }, { actingSession: input.sessionID }))),
38865
+ subscribe: async (input) => parseSubscribeAnswer(JSON.parse(await post("/v1/interests/subscribe", subscribeBody(input), { actingSession: input.sessionID }))),
38614
38866
  unsubscribe: async (input) => {
38615
38867
  await post("/v1/interests/unsubscribe", {
38616
38868
  session_id: input.sessionID,
@@ -38669,7 +38921,6 @@ function createEnvoyClient(config) {
38669
38921
  envelope: response,
38670
38922
  ...response.holder === undefined ? {} : { holder: response.holder },
38671
38923
  ...response.holder_state === undefined ? {} : { holderState: response.holder_state },
38672
- unresponsive: response.unresponsive ?? [],
38673
38924
  ...response.kept === undefined ? {} : { kept: response.kept },
38674
38925
  ...response.kept_reason === undefined ? {} : { keptReason: response.kept_reason }
38675
38926
  };
@@ -38699,885 +38950,45 @@ function createEnvoyClient(config) {
38699
38950
  } catch {
38700
38951
  throw error;
38701
38952
  }
38702
- const held = RoleHeldWireSchema.safeParse(answer);
38703
- if (held.success)
38704
- return { claimed: false, holder: held.data.holder };
38705
- }
38706
- throw error;
38707
- }
38708
- return { claimed: true, interest: InterestWireSchema.parse(JSON.parse(raw)) };
38709
- },
38710
- getRole: async (role) => RoleWireSchema.parse(JSON.parse(await request(`/v1/roles/${encodeURIComponent(role)}`, { method: "GET" }))),
38711
- listSessions: async (input = {}) => {
38712
- const search = new URLSearchParams;
38713
- if (input.directory !== undefined)
38714
- search.set("dir", input.directory);
38715
- if (input.title !== undefined)
38716
- search.set("title", input.title);
38717
- if (input.include !== undefined)
38718
- search.set("include", input.include);
38719
- const query = search.size === 0 ? "" : `?${search.toString()}`;
38720
- return SessionWireSchema.array().parse(JSON.parse(await request(`/v1/sessions${query}`, {})));
38721
- },
38722
- replay: async (input) => ReplayWireSchema.parse(JSON.parse(await post("/v1/replay", {
38723
- session_id: input.sessionID,
38724
- subjects: input.subjects.map((cursor) => cursor.after === undefined ? { subject: cursor.subject } : { subject: cursor.subject, after: cursor.after }),
38725
- ...input.maxMessages === undefined ? {} : { max_messages: input.maxMessages }
38726
- }, {
38727
- timeoutMs: input.timeoutMs ?? REPLAY_TIMEOUT_MS,
38728
- retry: false,
38729
- actingSession: input.sessionID
38730
- })))
38731
- };
38732
- }
38733
- function waitForRetry(delayMs) {
38734
- const { promise, resolve } = Promise.withResolvers();
38735
- setTimeout(resolve, delayMs);
38736
- return promise;
38737
- }
38738
- function messageMetadata(input) {
38739
- return {
38740
- ...input.inReplyTo === undefined ? {} : { in_reply_to: input.inReplyTo },
38741
- ...input.supersedes === undefined ? {} : { supersedes: input.supersedes },
38742
- ...input.urgency === undefined ? {} : { urgency: input.urgency },
38743
- ...input.expectsReply === undefined ? {} : { expects_reply: input.expectsReply },
38744
- ...input.expiresAt === undefined ? {} : { expires_at: input.expiresAt }
38745
- };
38746
- }
38747
- function sourceSession(sessionID) {
38748
- return sessionID === undefined || sessionID === "" ? {} : { source_session: sessionID };
38749
- }
38750
-
38751
- // ../envoy-client/src/replay.ts
38752
- var REPLAY_LEDGER_TTL_MS = DELIVERY_DUPLICATE_WINDOW_MS;
38753
- var REPLAY_LEDGER_LIMIT = 20000;
38754
- var REPLAY_PAGES_PER_SWEEP = 20;
38755
- var REPLAY_FAILURES_BEFORE_NOTICE = 3;
38756
- var REPLAY_STATE_REFRESH_MS = 60 * 60 * 1000;
38757
- var REPLAY_MAX_SUBJECTS = 32;
38758
- var START_CURSORS_TIMEOUT_MS = 2000;
38759
- var ROLE_FORWARD_KEY_PREFIX = "envoy.role.forward.";
38760
- var ReplayLedgerEntrySchema = object({
38761
- event_id: string2().min(1).optional(),
38762
- dedupe_key: string2().min(1).optional(),
38763
- at: number2()
38764
- });
38765
- var ReplayCursorSchema = object({
38766
- since: number2().int().nonnegative().optional(),
38767
- after: number2().int().nonnegative().optional(),
38768
- from_start: number2().int().nonnegative().optional()
38769
- });
38770
- var ReplayPendingNoticeSchema = _enum2([
38771
- "retention_gap",
38772
- "stream_moved_back",
38773
- "stream_recreated"
38774
- ]);
38775
- var PENDING_NOTICE_MESSAGES = {
38776
- stream_recreated: "Envoy's notification stream was recreated, so messages published to this session while its connection was down before that may be lost.",
38777
- stream_moved_back: "Envoy's notification stream was restored to an earlier point, so messages published to this session while its connection was down may be shown again, and any published between that point and the restore are missing.",
38778
- retention_gap: "The notification stream no longer holds some messages published to this session while its connection was down: messages older than 72 hours may be missing."
38779
- };
38780
- var ReplayStateSchema = object({
38781
- stream_id: string2().optional(),
38782
- stream_created: string2().optional(),
38783
- subjects: record(string2(), ReplayCursorSchema),
38784
- notices: array(string2()).optional()
38785
- });
38786
- var ReplaySnapshotSchema = object({
38787
- state: ReplayStateSchema.optional(),
38788
- ledger: array(ReplayLedgerEntrySchema)
38789
- });
38790
- function createReplayEngine(options) {
38791
- const { host, transport, dedupe } = options;
38792
- const now = options.now ?? Date.now;
38793
- const ledger = createLedger(now);
38794
- const cursors = new Map;
38795
- const cursorStarts = new Map;
38796
- let streamID;
38797
- let streamCreated;
38798
- const pending = new Set;
38799
- const failures = new Map;
38800
- let unwritten = [];
38801
- let writtenState;
38802
- let writtenShape;
38803
- let writtenAt;
38804
- let writes = Promise.resolve();
38805
- let epoch = 0;
38806
- let listenerNoticed;
38807
- const answered = new Set;
38808
- let running;
38809
- let rerun;
38810
- let consumerMode = false;
38811
- const safely = (what, call) => {
38812
- try {
38813
- call();
38814
- } catch (error) {
38815
- try {
38816
- host.log(`replay: ${what} failed`, error);
38817
- } catch {}
38818
- }
38819
- };
38820
- const log = (message, error) => safely("log", () => host.log(message, error));
38821
- const notify = (notice) => safely("notice", () => host.notify(notice));
38822
- const tell = (kind) => {
38823
- pending.delete(kind);
38824
- notify({ kind, message: PENDING_NOTICE_MESSAGES[kind] });
38825
- };
38826
- const sessionRead = (read) => () => {
38827
- try {
38828
- return read();
38829
- } catch {
38830
- return;
38831
- }
38832
- };
38833
- const currentSession = sessionRead(() => host.sessionID());
38834
- const writeSession = sessionRead(() => host.writeSessionID === undefined ? host.sessionID() : host.writeSessionID());
38835
- const bindTo = (sessionID, read) => {
38836
- if (sessionID === undefined || sessionID === "" || read() !== sessionID)
38837
- return;
38838
- const bound = epoch;
38839
- return { sessionID, alive: () => bound === epoch && read() === sessionID };
38840
- };
38841
- const bind = () => bindTo(currentSession(), currentSession);
38842
- const stateJSON = (afters = true) => {
38843
- const subjects = {};
38844
- for (const subject of [...cursors.keys()].sort()) {
38845
- const cursor = cursors.get(subject) ?? {};
38846
- subjects[subject] = {
38847
- ...cursor.since === undefined ? {} : { since: cursor.since },
38848
- ...!afters || cursor.after === undefined ? {} : { after: cursor.after },
38849
- ...cursor.from_start === undefined ? {} : { from_start: cursor.from_start }
38850
- };
38851
- }
38852
- return JSON.stringify({
38853
- ...streamID === undefined ? {} : { stream_id: streamID },
38854
- ...streamCreated === undefined ? {} : { stream_created: streamCreated },
38855
- subjects,
38856
- ...pending.size === 0 ? {} : { notices: [...pending].sort() }
38857
- });
38858
- };
38859
- const record2 = (identity) => {
38860
- const entry = ledger.record(identity);
38861
- if (entry !== undefined)
38862
- unwritten.push(entry);
38863
- };
38864
- const flush = (alive, atOnce = false) => {
38865
- const written = writes.then(async () => {
38866
- if (!alive())
38867
- return false;
38868
- const json = stateJSON();
38869
- const delivered = unwritten;
38870
- if (json === writtenState && delivered.length === 0)
38871
- return true;
38872
- const shape = stateJSON(false);
38873
- if (delivered.length === 0 && !atOnce && shape === writtenShape && writtenAt !== undefined && now() - writtenAt < REPLAY_STATE_REFRESH_MS) {
38874
- return true;
38875
- }
38876
- unwritten = [];
38877
- const state = json === writtenState ? undefined : JSON.parse(json);
38878
- try {
38879
- await host.write({
38880
- delivered,
38881
- state,
38882
- snapshot: () => ({ state: JSON.parse(json), ledger: ledger.list() })
38883
- });
38884
- if (state !== undefined) {
38885
- writtenState = json;
38886
- writtenShape = shape;
38887
- writtenAt = now();
38888
- }
38889
- return true;
38890
- } catch (error) {
38891
- unwritten = [...delivered, ...unwritten];
38892
- log("replay: could not persist the replay state; the next write carries it", error);
38893
- return false;
38894
- }
38895
- });
38896
- writes = written.then(() => {
38897
- return;
38898
- });
38899
- return written;
38900
- };
38901
- const syncFollowed = () => {
38902
- const followed = new Set(host.followedSubjects());
38903
- for (const subject of [...cursors.keys()]) {
38904
- if (!followed.has(subject)) {
38905
- cursors.delete(subject);
38906
- cursorStarts.delete(subject);
38907
- }
38908
- }
38909
- for (const subject of followed) {
38910
- if (!cursors.has(subject))
38911
- cursors.set(subject, {});
38912
- }
38913
- };
38914
- const failureNotice = (envelope, publishedAt, error, subject) => ({
38915
- eventId: envelope.event_id ?? "unknown",
38916
- sender: senderLabel(envelope),
38917
- summary: envelope.payload_summary ?? "unknown",
38918
- publishedAt,
38919
- error,
38920
- readWith: envelope.source === "dispatch" ? "dispatch read" : "envoy_inbox",
38921
- ...subject === undefined ? {} : { subject },
38922
- envelope
38923
- });
38924
- const showFailureNotice = async (notice, alive) => {
38925
- let queued;
38926
- try {
38927
- queued = await host.showFailureNotice(notice) === "queued";
38928
- } catch (error) {
38929
- if (!alive())
38930
- return "stopped";
38931
- safely("failure notice warning", () => host.failureNoticeFailed(notice, error));
38932
- return "hole";
38933
- }
38934
- if (!alive())
38935
- return "stopped";
38936
- if (queued)
38937
- return "hole";
38938
- record2(replayLedgerIdentity(notice.envelope));
38939
- await flush(alive);
38940
- return "done";
38941
- };
38942
- const handle = async (seq, buffered, alive) => {
38943
- const { message } = buffered;
38944
- const envelope = parseEnvelope(message.data);
38945
- if (envelope === undefined) {
38946
- log(`replay: skipping stored message ${seq} on ${message.subject}: not an Envoy envelope`);
38947
- return "done";
38948
- }
38949
- if (isRoleForward(envelope))
38950
- return "done";
38951
- const identity = replayLedgerIdentity(envelope);
38952
- if (ledger.has(identity)) {
38953
- failures.delete(seq);
38954
- return "done";
38955
- }
38956
- if (dedupe?.pending(envelope) === true)
38957
- return "hole";
38958
- if (!alive())
38959
- return "stopped";
38960
- const giveUp = async (error) => {
38961
- const handled = await showFailureNotice(failureNotice(envelope, message.time, error, message.subject), alive);
38962
- if (handled === "done")
38963
- failures.delete(seq);
38964
- return handled;
38965
- };
38966
- const failure = failures.get(seq);
38967
- if (failure !== undefined && failure.count >= REPLAY_FAILURES_BEFORE_NOTICE) {
38968
- return giveUp(failure.error);
38969
- }
38970
- const followed = buffered.subjects[0] ?? message.subject;
38971
- let outcome;
38972
- let error = "the host could not show it";
38973
- try {
38974
- outcome = await host.deliver({
38975
- envelope,
38976
- data: message.data,
38977
- seq,
38978
- subject: message.subject,
38979
- followed,
38980
- time: message.time,
38981
- since: cursors.get(followed)?.since
38982
- });
38983
- } catch (thrown) {
38984
- outcome = "failed";
38985
- error = messageFor(thrown);
38986
- }
38987
- if (!alive())
38988
- return "stopped";
38989
- if (outcome === "shown") {
38990
- failures.delete(seq);
38991
- record2(identity);
38992
- await flush(alive);
38993
- return "done";
38994
- }
38995
- if (outcome === "suppressed") {
38996
- failures.delete(seq);
38997
- return "done";
38998
- }
38999
- if (outcome === "queued") {
39000
- failures.delete(seq);
39001
- return "hole";
39002
- }
39003
- const count = (failure?.count ?? 0) + 1;
39004
- failures.set(seq, { count, error });
39005
- if (count >= REPLAY_FAILURES_BEFORE_NOTICE)
39006
- return giveUp(error);
39007
- log(`replay: could not show stored message ${seq} (${envelope.event_id}): ${error}`);
39008
- return "hole";
39009
- };
39010
- const listenerFailed = (error) => {
39011
- if (error instanceof EnvoyApiError) {
39012
- const status = error.details.status;
39013
- if (status === 404) {
39014
- if (listenerNoticed === "not_served")
39015
- return;
39016
- listenerNoticed = "not_served";
39017
- notify({
39018
- kind: "not_served",
39019
- message: "This session's Envoy listener does not serve replay (POST /v1/replay answered 404), so messages published while its connection is down are not recovered."
39020
- });
39021
- return;
39022
- }
39023
- if (status === 503 && error.code === REPLAY_UNAVAILABLE_CODE || status === 401 || status === 403) {
39024
- const key = `unavailable:${status}:${error.message}`;
39025
- if (listenerNoticed === key)
39026
- return;
39027
- listenerNoticed = key;
39028
- notify({
39029
- kind: "unavailable",
39030
- status,
39031
- error: error.message,
39032
- message: `Envoy replay is unavailable (${status}: ${error.message}), so messages published while this session's connection is down are not recovered until it answers.`
39033
- });
39034
- return;
39035
- }
39036
- }
39037
- log("replay: the listener's replay failed; the next trigger tries again", error);
39038
- };
39039
- const streamReplaced = (stream) => {
39040
- if (stream.id === undefined) {
39041
- return streamCreated !== undefined && streamCreated !== "" && stream.created !== streamCreated;
39042
- }
39043
- return streamID !== undefined && streamID !== "" && stream.id !== streamID;
39044
- };
39045
- const streamMovedBack = (stream) => {
39046
- for (const cursor of cursors.values()) {
39047
- if (cursor.after !== undefined && cursor.after > stream.last_seq)
39048
- return true;
39049
- }
39050
- return false;
39051
- };
39052
- const sweepOnce = async () => {
39053
- const binding = bind();
39054
- if (binding === undefined)
39055
- return;
39056
- const { sessionID, alive } = binding;
39057
- syncFollowed();
39058
- const readings = new Map;
39059
- for (const [subject, cursor] of cursors) {
39060
- readings.set(subject, {
39061
- read: cursor.from_start === undefined ? cursor.after : 0,
39062
- more: true
39063
- });
39064
- }
39065
- const buffer = new Map;
39066
- const holes = new Map;
39067
- let gapNoticed = false;
39068
- if (pending.size > 0) {
39069
- for (const kind of [...pending].sort()) {
39070
- if (kind === "retention_gap")
39071
- gapNoticed = true;
39072
- tell(kind);
39073
- }
39074
- await flush(alive);
39075
- if (!alive())
39076
- return;
39077
- }
39078
- if (consumerMode)
39079
- return;
39080
- for (let page = 0;page < REPLAY_PAGES_PER_SWEEP; page += 1) {
39081
- let handledAny = false;
39082
- const asking = [...readings].filter(([, reading]) => reading.more).sort(([, a], [, b]) => (a.read ?? -1) - (b.read ?? -1)).slice(0, REPLAY_MAX_SUBJECTS);
39083
- if (asking.length === 0)
39084
- break;
39085
- let response;
39086
- try {
39087
- response = await transport.replay({
39088
- sessionID,
39089
- subjects: asking.map(([subject, reading]) => reading.read === undefined ? { subject } : { subject, after: reading.read }),
39090
- ...options.maxMessages === undefined ? {} : { maxMessages: options.maxMessages }
39091
- });
39092
- } catch (error) {
39093
- if (alive()) {
39094
- answered.clear();
39095
- listenerFailed(error);
39096
- }
39097
- return;
39098
- }
39099
- if (!alive())
39100
- return;
39101
- listenerNoticed = undefined;
39102
- for (const result of response.subjects) {
39103
- if (result.refused === undefined)
39104
- answered.add(result.subject);
39105
- else
39106
- answered.delete(result.subject);
39107
- }
39108
- const { stream } = response;
39109
- const replaced = streamReplaced(stream);
39110
- streamID = stream.id ?? (replaced ? undefined : streamID);
39111
- streamCreated = stream.created;
39112
- if (replaced) {
39113
- const at = now();
39114
- for (const subject of cursors.keys()) {
39115
- cursors.set(subject, { since: stream.last_seq, after: stream.last_seq });
39116
- cursorStarts.set(subject, at);
39117
- }
39118
- failures.clear();
39119
- pending.add("stream_recreated");
39120
- await flush(alive);
39121
- if (!alive())
39122
- return;
39123
- tell("stream_recreated");
39124
- await flush(alive);
39125
- return;
39126
- }
39127
- if (streamMovedBack(stream)) {
39128
- const from = Math.max(0, stream.first_seq - 1);
39129
- for (const [subject, cursor] of cursors) {
39130
- if (cursor.after === undefined)
39131
- continue;
39132
- cursors.set(subject, { ...cursor, after: from });
39133
- const reading = readings.get(subject);
39134
- if (reading !== undefined) {
39135
- reading.read = from;
39136
- reading.more = true;
39137
- }
39138
- }
39139
- buffer.clear();
39140
- holes.clear();
39141
- failures.clear();
39142
- pending.add("stream_moved_back");
39143
- await flush(alive, true);
39144
- if (!alive())
39145
- return;
39146
- tell("stream_moved_back");
39147
- await flush(alive);
39148
- if (!alive())
39149
- return;
39150
- continue;
39151
- }
39152
- for (const [, reading] of asking)
39153
- reading.more = false;
39154
- for (const result of response.subjects) {
39155
- const reading = readings.get(result.subject);
39156
- const markedAt = cursors.get(result.subject)?.from_start;
39157
- if (reading === undefined || markedAt === undefined || result.refused !== undefined) {
39158
- continue;
39159
- }
39160
- const start = Math.max(0, stream.first_seq - 1);
39161
- cursors.set(result.subject, { since: start, after: start });
39162
- cursorStarts.set(result.subject, now());
39163
- if (now() - markedAt < DELIVERY_DUPLICATE_WINDOW_MS)
39164
- reading.read = start;
39165
- }
39166
- let gapped = false;
39167
- for (const result of response.subjects) {
39168
- const reading = readings.get(result.subject);
39169
- if (reading?.read === undefined || result.refused !== undefined)
39170
- continue;
39171
- if (reading.read >= stream.first_seq - 1)
39172
- continue;
39173
- gapped = true;
39174
- reading.read = stream.first_seq - 1;
39175
- const cursor = cursors.get(result.subject);
39176
- if (cursor?.after !== undefined && cursor.after < reading.read) {
39177
- cursors.set(result.subject, { ...cursor, after: reading.read });
39178
- }
39179
- }
39180
- if (gapped) {
39181
- if (!gapNoticed)
39182
- pending.add("retention_gap");
39183
- await flush(alive, true);
39184
- if (!alive())
39185
- return;
39186
- if (pending.has("retention_gap")) {
39187
- gapNoticed = true;
39188
- tell("retention_gap");
39189
- await flush(alive);
39190
- }
39191
- }
39192
- for (const result of response.subjects) {
39193
- const reading = readings.get(result.subject);
39194
- if (reading === undefined || result.refused !== undefined)
39195
- continue;
39196
- if (reading.read === undefined) {
39197
- const cursor = cursors.get(result.subject);
39198
- if (cursor !== undefined && cursor.after === undefined) {
39199
- cursors.set(result.subject, { since: result.next_after, after: result.next_after });
39200
- cursorStarts.set(result.subject, now());
39201
- }
39202
- reading.read = result.next_after;
39203
- continue;
39204
- }
39205
- for (const message of result.messages) {
39206
- if (message.seq <= reading.read)
39207
- continue;
39208
- const buffered = buffer.get(message.seq);
39209
- if (buffered === undefined) {
39210
- buffer.set(message.seq, { message, subjects: [result.subject] });
39211
- } else if (!buffered.subjects.includes(result.subject)) {
39212
- buffered.subjects.push(result.subject);
39213
- }
39214
- }
39215
- reading.read = Math.max(reading.read, result.next_after);
39216
- reading.more = result.more;
39217
- }
39218
- let bound = Number.POSITIVE_INFINITY;
39219
- for (const reading of readings.values()) {
39220
- if (reading.more && reading.read !== undefined)
39221
- bound = Math.min(bound, reading.read);
39222
- }
39223
- const ready = [...buffer.keys()].filter((seq) => seq <= bound).sort((a, b) => a - b);
39224
- for (const seq of ready) {
39225
- const buffered = buffer.get(seq);
39226
- if (buffered === undefined)
39227
- continue;
39228
- buffer.delete(seq);
39229
- if (!buffered.subjects.some((subject) => cursors.has(subject)))
39230
- continue;
39231
- const handled = await handle(seq, buffered, alive);
39232
- if (handled === "stopped")
39233
- return;
39234
- if (handled === "done")
39235
- handledAny = true;
39236
- if (handled === "hole") {
39237
- for (const subject of buffered.subjects) {
39238
- if (!holes.has(subject))
39239
- holes.set(subject, seq);
39240
- }
39241
- }
39242
- }
39243
- if (!alive())
39244
- return;
39245
- for (const [subject, reading] of readings) {
39246
- const cursor = cursors.get(subject);
39247
- if (cursor?.after === undefined || reading.read === undefined)
39248
- continue;
39249
- let after = Math.min(reading.read, bound);
39250
- const hole = holes.get(subject);
39251
- if (hole !== undefined)
39252
- after = Math.min(after, hole - 1);
39253
- if (after > cursor.after)
39254
- cursors.set(subject, { ...cursor, after });
39255
- }
39256
- await flush(alive, handledAny);
39257
- if (![...readings.values()].some((reading) => reading.more))
39258
- break;
39259
- }
39260
- };
39261
- const runSweep = async () => {
39262
- try {
39263
- await sweepOnce();
39264
- } catch (error) {
39265
- log("replay: a sweep failed", error);
39266
- }
39267
- };
39268
- const sweep = () => {
39269
- if (running === undefined) {
39270
- running = runSweep().finally(() => {
39271
- running = undefined;
39272
- });
39273
- return running;
39274
- }
39275
- rerun ??= running.then(() => {
39276
- rerun = undefined;
39277
- return sweep();
39278
- });
39279
- return rerun;
39280
- };
39281
- const started = (cursor) => cursor?.after !== undefined || cursor?.from_start !== undefined;
39282
- const startOnce = async () => {
39283
- const binding = bind();
39284
- if (binding === undefined)
39285
- return;
39286
- const { sessionID, alive } = binding;
39287
- const unstarted = [...new Set(host.followedSubjects())].filter((subject) => !started(cursors.get(subject)));
39288
- let changed = false;
39289
- for (let index = 0;index < unstarted.length; index += REPLAY_MAX_SUBJECTS) {
39290
- let response;
39291
- try {
39292
- response = await transport.replay({
39293
- sessionID,
39294
- subjects: unstarted.slice(index, index + REPLAY_MAX_SUBJECTS).map((subject) => ({ subject })),
39295
- timeoutMs: START_CURSORS_TIMEOUT_MS
39296
- });
39297
- } catch (error) {
39298
- if (!alive())
39299
- return;
39300
- const own = `${AGENT_TOPIC_PREFIX}${sessionID}`;
39301
- const marked = unstarted.includes(own) && !started(cursors.get(own));
39302
- if (marked) {
39303
- cursors.set(own, { from_start: now() });
39304
- changed = true;
39305
- }
39306
- log(marked ? `replay: could not read the stream's position for ${own} before registering while the session's NATS connection is down; its first sweep once the connection is back reads it from the stream's oldest message, so what is stored for it meanwhile is shown` : "replay: could not read the stream's position before registering while the session's NATS connection is down; the first sweep starts the cursors instead", error);
39307
- break;
39308
- }
39309
- if (!alive())
39310
- return;
39311
- const { stream } = response;
39312
- if (streamReplaced(stream))
39313
- break;
39314
- streamID = stream.id ?? streamID;
39315
- streamCreated = stream.created;
39316
- const at = now();
39317
- for (const result of response.subjects) {
39318
- if (result.refused !== undefined || started(cursors.get(result.subject)))
39319
- continue;
39320
- cursors.set(result.subject, { since: result.next_after, after: result.next_after });
39321
- cursorStarts.set(result.subject, at);
39322
- changed = true;
39323
- }
39324
- }
39325
- if (changed)
39326
- await flush(alive, true);
39327
- };
39328
- const startCursors = async () => {
39329
- while (running !== undefined)
39330
- await running;
39331
- running = startOnce().catch((error) => log("replay: starting the cursors failed", error)).finally(() => {
39332
- running = undefined;
39333
- });
39334
- await running;
39335
- };
39336
- return {
39337
- sweep,
39338
- startCursors,
39339
- startPending: () => [...cursors.values()].some((cursor) => cursor.from_start !== undefined),
39340
- setConsumerMode(on) {
39341
- consumerMode = on;
39342
- },
39343
- async followedChanged() {
39344
- const alive = bind()?.alive;
39345
- if (alive === undefined)
39346
- return;
39347
- try {
39348
- syncFollowed();
39349
- await flush(alive);
39350
- } catch (error) {
39351
- log("replay: could not take the followed subjects", error);
39352
- }
39353
- },
39354
- delivered: (frame) => ledger.has(replayLedgerIdentity(frame)),
39355
- async recordDelivered(frame) {
39356
- const alive = bind()?.alive;
39357
- if (alive === undefined)
39358
- return;
39359
- record2(replayLedgerIdentity(frame));
39360
- await flush(alive);
39361
- },
39362
- async recordDeliveredFor(sessionID, frame) {
39363
- const alive = bindTo(sessionID, writeSession)?.alive;
39364
- if (alive === undefined)
39365
- return false;
39366
- record2(replayLedgerIdentity(frame));
39367
- return await flush(alive);
39368
- },
39369
- async liveFailed(sessionID, followed, envelope, error, receivedAt) {
39370
- const started = cursorStarts.get(followed);
39371
- if (!isRoleForward(envelope) && answered.has(followed) && cursors.get(followed)?.after !== undefined && (started === undefined || started <= receivedAt)) {
39372
- return;
39373
- }
39374
- const alive = bindTo(sessionID, currentSession)?.alive;
39375
- if (alive === undefined) {
39376
- log("replay: a failed frame was not reported here; the session it names is closed");
39377
- return;
39378
- }
39379
- try {
39380
- await showFailureNotice(failureNotice(envelope, inboundTimestamp(envelope.issued_at), messageFor(error)), alive);
39381
- } catch (thrown) {
39382
- log("replay: could not report a failed live frame", thrown);
39383
- }
39384
- },
39385
- followedBefore: (topic, seq) => {
39386
- const since = cursors.get(topic)?.since;
39387
- return since !== undefined && seq > since;
39388
- },
39389
- restore(snapshot) {
39390
- epoch += 1;
39391
- ledger.restore(snapshot.ledger);
39392
- cursors.clear();
39393
- cursorStarts.clear();
39394
- streamID = snapshot.state?.stream_id;
39395
- streamCreated = snapshot.state?.stream_created;
39396
- pending.clear();
39397
- for (const kind of snapshot.state?.notices ?? []) {
39398
- const known = ReplayPendingNoticeSchema.safeParse(kind);
39399
- if (known.success)
39400
- pending.add(known.data);
39401
- }
39402
- for (const [subject, cursor] of Object.entries(snapshot.state?.subjects ?? {})) {
39403
- cursors.set(subject, { ...cursor });
39404
- }
39405
- writtenState = snapshot.state === undefined ? undefined : stateJSON();
39406
- writtenShape = snapshot.state === undefined ? undefined : stateJSON(false);
39407
- writtenAt = undefined;
39408
- unwritten = [];
39409
- failures.clear();
39410
- listenerNoticed = undefined;
39411
- answered.clear();
39412
- },
39413
- state: () => JSON.parse(stateJSON())
39414
- };
39415
- }
39416
- function replaySuppressed(rendered) {
39417
- return rendered.skip || rendered.malformedDelivery === true;
39418
- }
39419
- var REPLAYED_SUFFIX = "while this session's connection was down.";
39420
- function replayedText(content, publishedAt) {
39421
- return `${content}
39422
- Published at ${publishedAt} ${REPLAYED_SUFFIX}`;
39423
- }
39424
- function failureNoticeFields(notice) {
39425
- const readWith = notice.readWith === "dispatch read" ? "Read it with the `dispatch read` command on the issue or ask it concerns." : "Read it with the envoy_inbox tool.";
39426
- return {
39427
- notice: "message not shown",
39428
- event_id: notice.eventId,
39429
- from: notice.sender,
39430
- summary: notice.summary,
39431
- published_at: notice.publishedAt,
39432
- ...notice.subject === undefined ? {} : { subject: notice.subject },
39433
- error: notice.error,
39434
- detail: `This session could not be shown this message, so it is not coming back on its own. ${readWith}`
39435
- };
39436
- }
39437
- var REPLAY_NOTICE_NAMES = {
39438
- stream_recreated: "stream recreated",
39439
- stream_moved_back: "stream moved back"
39440
- };
39441
- function replayNoticeFields(notice) {
39442
- return {
39443
- notice: REPLAY_NOTICE_NAMES[notice.kind] ?? "messages may be missing",
39444
- detail: notice.message
39445
- };
39446
- }
39447
- function envoyNoticeText(fields) {
39448
- return encode5({ envoy: fields });
39449
- }
39450
- var PRIVATE_STREAM_SUBJECTS = [
39451
- `${AGENT_TOPIC_PREFIX}>`,
39452
- `${LEGION_NOTICE_TOPIC_PREFIX}>`,
39453
- `${EXCEPTION_TOPIC_PREFIX}${AGENT_TOPIC_PREFIX}>`
39454
- ];
39455
- function subjectsOverlap(a, b) {
39456
- const aTokens = a.split(".");
39457
- const bTokens = b.split(".");
39458
- for (let index = 0;index < aTokens.length && index < bTokens.length; index += 1) {
39459
- const aToken = aTokens[index];
39460
- const bToken = bTokens[index];
39461
- if (aToken === ">" || bToken === ">")
39462
- return true;
39463
- if (aToken !== "*" && bToken !== "*" && aToken !== bToken)
39464
- return false;
39465
- }
39466
- return aTokens.length === bTokens.length;
39467
- }
39468
- function reachesPrivateStreamSubject(ownAgentSubject, topic) {
39469
- if (!PRIVATE_STREAM_SUBJECTS.some((subject) => subjectsOverlap(topic, subject)))
39470
- return false;
39471
- return topic !== ownAgentSubject || /[*>]/.test(ownAgentSubject);
39472
- }
39473
- function replayFollowedSubjects(ownAgentSubject, topics, heldForRole = () => false) {
39474
- const followed = [...topics].filter((topic) => !topic.startsWith(ROLE_TOPIC_PREFIX) && !reachesPrivateStreamSubject(ownAgentSubject, topic) && !heldForRole(topic));
39475
- return [...new Set([ownAgentSubject, ...followed])];
39476
- }
39477
- function rememberedTopics(state) {
39478
- return Object.keys(state?.subjects ?? {}).filter((topic) => !topic.startsWith(AGENT_TOPIC_PREFIX) && !topic.startsWith(ROLE_TOPIC_PREFIX));
39479
- }
39480
- async function closeReplayedRemoval(frame, host) {
39481
- const closed = (subscriptionRemovedTopics(frame.data, host.sessionID) ?? []).filter((topic) => host.engine.followedBefore(topic, frame.seq) && host.close(topic));
39482
- if (closed.length === 0)
39483
- return closed;
39484
- const unregistering = host.unregister(closed);
39485
- await host.followedChanged();
39486
- await unregistering;
39487
- return closed;
39488
- }
39489
- function routeReplayNotice(notice, routes) {
39490
- if (notice.kind === "not_served" || notice.kind === "unavailable") {
39491
- routes.warn(notice.message);
39492
- return;
39493
- }
39494
- routes.card(envoyNoticeText(replayNoticeFields(notice)));
39495
- }
39496
- function isRoleForward(envelope) {
39497
- return envelope.dedupe_key?.startsWith(ROLE_FORWARD_KEY_PREFIX) === true || envelope.topic?.startsWith(ROLE_TOPIC_PREFIX) === true;
39498
- }
39499
- function replayLedgerIdentity(frame) {
39500
- return {
39501
- ...frame.event_id === undefined || frame.event_id === "" ? {} : { event_id: frame.event_id },
39502
- ...frame.dedupe_key === undefined || !dedupeKeyNamesItsEvent(frame) ? {} : { dedupe_key: frame.dedupe_key }
38953
+ const held = RoleHeldWireSchema.safeParse(answer);
38954
+ if (held.success)
38955
+ return { claimed: false, holder: held.data.holder };
38956
+ }
38957
+ throw error;
38958
+ }
38959
+ return { claimed: true, interest: InterestWireSchema.parse(JSON.parse(raw)) };
38960
+ },
38961
+ getRole: async (role) => RoleWireSchema.parse(JSON.parse(await request(`/v1/roles/${encodeURIComponent(role)}`, { method: "GET" }))),
38962
+ listSessions: async (input = {}) => {
38963
+ const search = new URLSearchParams;
38964
+ if (input.directory !== undefined)
38965
+ search.set("dir", input.directory);
38966
+ if (input.title !== undefined)
38967
+ search.set("title", input.title);
38968
+ if (input.include !== undefined)
38969
+ search.set("include", input.include);
38970
+ const query = search.size === 0 ? "" : `?${search.toString()}`;
38971
+ return SessionWireSchema.array().parse(JSON.parse(await request(`/v1/sessions${query}`, {})));
38972
+ }
39503
38973
  };
39504
38974
  }
39505
- var StoredEnvelopeSchema = EnvelopeSchema.extend({
39506
- expects_reply: InboundExpectsReplySchema.optional()
39507
- });
39508
- function parseEnvelope(data) {
39509
- let parsed;
39510
- try {
39511
- parsed = JSON.parse(data);
39512
- } catch {
39513
- return;
39514
- }
39515
- const result = StoredEnvelopeSchema.safeParse(parsed);
39516
- return result.success ? result.data : undefined;
38975
+ function waitForRetry(delayMs) {
38976
+ const { promise, resolve } = Promise.withResolvers();
38977
+ setTimeout(resolve, delayMs);
38978
+ return promise;
39517
38979
  }
39518
- function createLedger(now) {
39519
- let entries = new Set;
39520
- const byEvent = new Map;
39521
- const byKey = new Map;
39522
- const drop = (entry) => {
39523
- entries.delete(entry);
39524
- if (entry.event_id !== undefined && byEvent.get(entry.event_id) === entry) {
39525
- byEvent.delete(entry.event_id);
39526
- }
39527
- if (entry.dedupe_key !== undefined && byKey.get(entry.dedupe_key) === entry) {
39528
- byKey.delete(entry.dedupe_key);
39529
- }
39530
- };
39531
- const add = (entry) => {
39532
- if (entries.size >= REPLAY_LEDGER_LIMIT) {
39533
- const oldest = entries.values().next();
39534
- if (oldest.done !== true)
39535
- drop(oldest.value);
39536
- }
39537
- entries.add(entry);
39538
- if (entry.event_id !== undefined)
39539
- byEvent.set(entry.event_id, entry);
39540
- if (entry.dedupe_key !== undefined)
39541
- byKey.set(entry.dedupe_key, entry);
39542
- };
39543
- const expire = () => {
39544
- const at = now();
39545
- for (const entry of entries) {
39546
- if (at - entry.at < REPLAY_LEDGER_TTL_MS)
39547
- break;
39548
- drop(entry);
39549
- }
39550
- };
39551
- const has = (identity) => {
39552
- expire();
39553
- return identity.event_id !== undefined && byEvent.has(identity.event_id) || identity.dedupe_key !== undefined && byKey.has(identity.dedupe_key);
39554
- };
38980
+ function messageMetadata(input) {
39555
38981
  return {
39556
- has,
39557
- record(identity) {
39558
- if (identity.event_id === undefined && identity.dedupe_key === undefined)
39559
- return;
39560
- if (has(identity))
39561
- return;
39562
- const entry = { ...identity, at: now() };
39563
- add(entry);
39564
- return entry;
39565
- },
39566
- restore(restored) {
39567
- entries = new Set;
39568
- byEvent.clear();
39569
- byKey.clear();
39570
- const at = now();
39571
- const kept = restored.filter((entry) => at - entry.at < REPLAY_LEDGER_TTL_MS && (entry.event_id !== undefined || entry.dedupe_key !== undefined)).sort((a, b) => a.at - b.at);
39572
- for (const entry of kept)
39573
- add({ ...entry });
39574
- },
39575
- list() {
39576
- expire();
39577
- return [...entries];
39578
- }
38982
+ ...input.inReplyTo === undefined ? {} : { in_reply_to: input.inReplyTo },
38983
+ ...input.supersedes === undefined ? {} : { supersedes: input.supersedes },
38984
+ ...input.urgency === undefined ? {} : { urgency: input.urgency },
38985
+ ...input.expectsReply === undefined ? {} : { expects_reply: input.expectsReply },
38986
+ ...input.expiresAt === undefined ? {} : { expires_at: input.expiresAt }
39579
38987
  };
39580
38988
  }
38989
+ function sourceSession(sessionID) {
38990
+ return sessionID === undefined || sessionID === "" ? {} : { source_session: sessionID };
38991
+ }
39581
38992
 
39582
38993
  // ../envoy-client/src/consumer-delivery.ts
39583
38994
  var DELIVER_SUBJECT_PREFIX = "notifications.envoy.deliver.";
@@ -39682,6 +39093,7 @@ function every(ms, callback) {
39682
39093
  }
39683
39094
  function createConsumerDelivery(options) {
39684
39095
  const { host, ledger, sessionID } = options;
39096
+ const render = options.render ?? renderInbound;
39685
39097
  const now = options.now ?? Date.now;
39686
39098
  const repeat = options.every ?? every;
39687
39099
  const ownSubject = agentSubject(sessionID);
@@ -39693,9 +39105,9 @@ function createConsumerDelivery(options) {
39693
39105
  let registeredResume;
39694
39106
  let gapTold = false;
39695
39107
  let closed = false;
39696
- let relayed;
39697
39108
  let persisted = JSON.stringify({ follows: {} });
39698
39109
  const entries = new Map;
39110
+ const troubled = new Map;
39699
39111
  const queue = [];
39700
39112
  let blocking;
39701
39113
  let pumping = false;
@@ -39717,6 +39129,7 @@ function createConsumerDelivery(options) {
39717
39129
  streamID = id;
39718
39130
  held = new Set;
39719
39131
  written = undefined;
39132
+ troubled.clear();
39720
39133
  };
39721
39134
  const tellReplacedResume = (id) => {
39722
39135
  const asked = resumePoint() ?? registeredResume;
@@ -39737,18 +39150,6 @@ function createConsumerDelivery(options) {
39737
39150
  }
39738
39151
  return earliest;
39739
39152
  };
39740
- const followedOnCorePath = (frame) => {
39741
- const state = ledger.state();
39742
- if (state.stream_id !== frame.streamID)
39743
- return false;
39744
- for (const [topic, cursor] of Object.entries(state.subjects)) {
39745
- if (cursor.since === undefined || frame.streamSeq <= cursor.since)
39746
- continue;
39747
- if (reaches(topic, frame.subject))
39748
- return true;
39749
- }
39750
- return false;
39751
- };
39752
39153
  const settle = (entry, body) => {
39753
39154
  if (entry.settled)
39754
39155
  return;
@@ -39757,6 +39158,8 @@ function createConsumerDelivery(options) {
39757
39158
  entries.delete(entry.key);
39758
39159
  for (const reply of entry.replies)
39759
39160
  send(reply, body);
39161
+ if (body === "done")
39162
+ troubled.delete(entry.key);
39760
39163
  if (body === "done" && entry.frame.streamID === streamID) {
39761
39164
  held.delete(entry.frame.streamSeq);
39762
39165
  written = Math.max(written ?? 0, entry.frame.streamSeq);
@@ -39773,19 +39176,52 @@ function createConsumerDelivery(options) {
39773
39176
  const failed = (entry, error) => {
39774
39177
  if (entry.settled)
39775
39178
  return;
39776
- if (entry.frame.delivered < RELAY_ATTEMPTS_BEFORE_FAILED) {
39179
+ const trouble = troubled.get(entry.key) ?? { failures: 0, givenUp: false };
39180
+ troubled.set(entry.key, trouble);
39181
+ if (trouble.givenUp) {
39182
+ settle(entry, "nak");
39183
+ return;
39184
+ }
39185
+ trouble.failures += 1;
39186
+ if (trouble.failures < RELAY_ATTEMPTS_BEFORE_FAILED) {
39777
39187
  log(`frame ${entry.frame.streamSeq} failed; it comes again`, error);
39778
39188
  settle(entry, "nak");
39779
39189
  return;
39780
39190
  }
39781
39191
  log(`frame ${entry.frame.streamSeq} failed and does not come again; its session gets the notice`, error);
39782
- safely("failure notice", () => {
39783
- const { subject, data } = entry.frame;
39784
- const { envelope } = renderInbound(data, sessionID, subject);
39785
- if (envelope !== undefined)
39786
- ledger.liveFailed(sessionID, subject, envelope, error, now());
39192
+ const goesBack = (why, thrown) => {
39193
+ trouble.givenUp = true;
39194
+ log(`frame ${entry.frame.streamSeq} ${why}; it goes back to the stream`, thrown);
39195
+ settle(entry, "nak");
39196
+ };
39197
+ let envelope;
39198
+ try {
39199
+ envelope = render(entry.frame.data, sessionID, entry.frame.subject).envelope;
39200
+ } catch (thrown) {
39201
+ goesBack("could not be read to report it", thrown);
39202
+ return;
39203
+ }
39204
+ if (envelope === undefined) {
39205
+ log(`frame ${entry.frame.streamSeq} carries no envelope to report; it is dropped`);
39206
+ settle(entry, "done");
39207
+ return;
39208
+ }
39209
+ let telling;
39210
+ try {
39211
+ telling = ledger.liveFailed(sessionID, envelope, error);
39212
+ } catch (thrown) {
39213
+ goesBack("could not be reported", thrown);
39214
+ return;
39215
+ }
39216
+ telling.then((told) => {
39217
+ if (told) {
39218
+ settle(entry, "done");
39219
+ return;
39220
+ }
39221
+ goesBack("was reported to nobody");
39222
+ }, (thrown) => {
39223
+ goesBack("could not be reported", thrown);
39787
39224
  });
39788
- settle(entry, "done");
39789
39225
  };
39790
39226
  const handOff = (entry) => {
39791
39227
  const handle = {
@@ -39866,7 +39302,7 @@ function createConsumerDelivery(options) {
39866
39302
  return;
39867
39303
  }
39868
39304
  const since = followedAt(frame.subject);
39869
- const unfollowed = (since === undefined || frame.storedAtMs < since - FOLLOW_CLOCK_ALLOWANCE_MS) && !followedOnCorePath(frame);
39305
+ const unfollowed = since === undefined || frame.storedAtMs < since - FOLLOW_CLOCK_ALLOWANCE_MS;
39870
39306
  const identity = unfollowed ? undefined : parseIdentity(frame.data);
39871
39307
  if (unfollowed || alreadyWritten(identity)) {
39872
39308
  send(frame.reply, "done");
@@ -39902,20 +39338,6 @@ function createConsumerDelivery(options) {
39902
39338
  return { stream_id: streamID, seq: Math.min(...held) - 1 };
39903
39339
  return written === undefined ? undefined : { stream_id: streamID, seq: written };
39904
39340
  };
39905
- const seededResume = () => {
39906
- const state = ledger.state();
39907
- const stream = state.stream_id;
39908
- if (stream === undefined)
39909
- return;
39910
- let lowest;
39911
- for (const [subject, cursor] of Object.entries(state.subjects)) {
39912
- if (cursor.after === undefined || !streamStores(subject))
39913
- continue;
39914
- if (lowest === undefined || cursor.after < lowest)
39915
- lowest = cursor.after;
39916
- }
39917
- return lowest === undefined ? undefined : { stream_id: stream, seq: lowest };
39918
- };
39919
39341
  const snapshot = () => {
39920
39342
  const resume = resumePoint();
39921
39343
  return {
@@ -39941,7 +39363,7 @@ function createConsumerDelivery(options) {
39941
39363
  return currentEpoch;
39942
39364
  },
39943
39365
  registration() {
39944
- const resume = resumePoint() ?? seededResume();
39366
+ const resume = resumePoint();
39945
39367
  registeredResume = resume;
39946
39368
  return {
39947
39369
  consumer: true,
@@ -39949,29 +39371,24 @@ function createConsumerDelivery(options) {
39949
39371
  ...resume === undefined ? {} : { resume }
39950
39372
  };
39951
39373
  },
39952
- answered(answer, topics) {
39953
- if (answer.mode === "consumer") {
39954
- if (answer.stream_id !== "" && answer.stream_id !== streamID) {
39955
- tellReplacedResume(answer.stream_id);
39956
- switchStream(answer.stream_id);
39957
- }
39958
- const floor = (answer.consumer_start ?? 0) - 1;
39959
- if (answer.stream_id === streamID && floor > (written ?? -1))
39960
- written = floor;
39961
- if (answer.gap) {
39962
- tellGap("retention_gap");
39963
- gapTold = true;
39964
- }
39965
- relayed = new Set([...topics].filter(streamStores));
39966
- } else {
39967
- relayed = undefined;
39374
+ answered(answer) {
39375
+ if (answer.stream_id !== "" && answer.stream_id !== streamID) {
39376
+ tellReplacedResume(answer.stream_id);
39377
+ switchStream(answer.stream_id);
39378
+ }
39379
+ const floor = (answer.consumer_start ?? 0) - 1;
39380
+ if (answer.stream_id === streamID && floor > (written ?? -1))
39381
+ written = floor;
39382
+ if (answer.gap) {
39383
+ tellGap("retention_gap");
39384
+ gapTold = true;
39968
39385
  }
39969
- ledger.setConsumerMode(relayed !== undefined);
39970
39386
  persist();
39971
- return answer.mode;
39972
39387
  },
39973
- relays: (topic) => relayed?.has(topic) === true,
39974
- relaying: () => relayed !== undefined,
39388
+ followedBefore(topic, storedAtMs) {
39389
+ const at = follows.get(topic);
39390
+ return at !== undefined && storedAtMs >= at - FOLLOW_CLOCK_ALLOWANCE_MS;
39391
+ },
39975
39392
  setFollowed(topics) {
39976
39393
  const at = now();
39977
39394
  const next = new Map;
@@ -39999,8 +39416,6 @@ function createConsumerDelivery(options) {
39999
39416
  },
40000
39417
  close() {
40001
39418
  closed = true;
40002
- relayed = undefined;
40003
- ledger.setConsumerMode(false);
40004
39419
  queue.length = 0;
40005
39420
  for (const entry of [...entries.values()])
40006
39421
  settle(entry, "nak");
@@ -40008,13 +39423,61 @@ function createConsumerDelivery(options) {
40008
39423
  }
40009
39424
  };
40010
39425
  }
40011
- var NOT_ASKED = { mode: "core", reason: "not_asked" };
40012
- function registeredWithoutRelay(ledger, delivery, topics) {
40013
- if (delivery === undefined) {
40014
- ledger.setConsumerMode(false);
40015
- return;
40016
- }
40017
- delivery.answered(NOT_ASKED, topics);
39426
+ var GAP_NOTICE_NAMES = {
39427
+ retention_gap: "messages may be missing",
39428
+ stream_recreated: "stream recreated"
39429
+ };
39430
+ function gapNoticeFields(notice) {
39431
+ return { notice: GAP_NOTICE_NAMES[notice.kind], detail: notice.message };
39432
+ }
39433
+ function gapNoticeText(notice) {
39434
+ return envoyNoticeText(gapNoticeFields(notice));
39435
+ }
39436
+ function inboundUnavailableNoticeText(why) {
39437
+ return envoyNoticeText({
39438
+ notice: "Envoy inbound unavailable",
39439
+ error: why,
39440
+ detail: "The listener gave this session no durable consumer, which is the only path an Envoy message reaches it by, so it receives none. Everything else works, and the session registers again at every heartbeat; what the stream stored meanwhile arrives once a registration is accepted."
39441
+ });
39442
+ }
39443
+ function inboundUnavailable(tell) {
39444
+ let told = false;
39445
+ return {
39446
+ refused(error) {
39447
+ if (told)
39448
+ return;
39449
+ told = true;
39450
+ tell(messageFor(error));
39451
+ },
39452
+ accepted() {
39453
+ told = false;
39454
+ }
39455
+ };
39456
+ }
39457
+ function registerWithRelay({
39458
+ relay,
39459
+ ownAgentSubject,
39460
+ followed,
39461
+ roles,
39462
+ outage,
39463
+ send,
39464
+ answered
39465
+ }) {
39466
+ if (relay === undefined) {
39467
+ const refused = new Error("this session holds no relay subject, so the listener can give it no consumer and it can receive nothing");
39468
+ outage.refused(refused);
39469
+ return Promise.reject(refused);
39470
+ }
39471
+ const topics = registrationTopics(ownAgentSubject, followed, roles);
39472
+ relay.setFollowed(topics);
39473
+ return send(topics, relay.registration()).then((result) => {
39474
+ outage.accepted();
39475
+ answered(result);
39476
+ return result;
39477
+ }, (error) => {
39478
+ outage.refused(error);
39479
+ throw error;
39480
+ });
40018
39481
  }
40019
39482
 
40020
39483
  // ../envoy-client/src/envoy-config.ts
@@ -46044,7 +45507,7 @@ function every2(ms, callback) {
46044
45507
  // ../envoy-client/src/tool-contract.ts
46045
45508
  var SESSION_STATE_GUIDE = "A session is live, unresponsive or gone, by how long ago it last registered: live within 5 " + "minutes, unresponsive after that and within an hour, gone past the hour or with no record at " + `all. ${UNRESPONSIVE_RULE} A session named with no state is live.`;
46046
45509
  var DELIVERY_CONTRACT = "Delivery is at-least-once, possibly out of order across topics; use id for dedupe and at for freshness.";
46047
- var STORED_DELIVERY = "Stored topics (agent, dispatch, github, slack, legion, ghostwispr, whatsapp; a role message " + "reaches its holder as a stored copy on the holder's agent topic) reach each subscribed session " + "once from the durable consumer its listener relays to it, also one that was away or restarted. " + "Team and other ad-hoc topics are live only.";
45510
+ var STORED_DELIVERY = "Every notification topic is stored (agent, dispatch, github, slack, legion, ghostwispr, " + "whatsapp, team; a role message reaches its holder as a stored copy on the holder's agent " + "topic), and reaches each subscribed session once from the durable consumer its listener " + "relays to it, also one that was away or restarted. A 200 means the stream stored the message " + "for each subscribed session's consumer, not that any recipient has read it.";
46048
45511
  var TOPIC_GUIDE = "Topic guide (all are under notifications.): agent.<session_id> (subscribe: own inbox); role.<role> " + "(publish-to; holders claim via envoy_role_set). > matches one or more trailing tokens and does not " + "match the base subject. Envoy registers the concrete base when you subscribe to <subject>.>, so " + "the recommended default remains github.<owner>.<repo>.pr.<n>.>. <owner> and <repo> are one token " + "each, with every dot in the name written _ (sjawhar/.github is github.sjawhar._github; " + "acme/site.io is github.acme.site_io), and a topic spelled with the dot receives nothing. " + "Default PR subscription: " + "github.<owner>.<repo>.pr.<n>.> (it receives the quiet PR family): pr.<n> (lifecycle: " + "opened/synchronize/closed; closed carries merged, merge_commit_sha, merged_by, head_sha), " + "pr.<n>.comment, pr.<n>.review, pr.<n>.mention, pr.<n>.checks (one settlement event per commit whose checks settle, the head or not, naming its sha: " + 'passed/failed/cancelled/skipped with failing names and URLs; re-fires with superseded_settlement: "true" ' + "when new runs appear for the same commit). Other GitHub: issue.<n>, issue.<n>.comment, " + "issue.<n>.mention, mention, push.branch.<name>, push.tag.<name>, workflow.<file>.<action> (only " + "runs without an associated PR); slack.<team>.<channel>.message|mention and " + "slack.<team>.<channel>.thread.<ts>.message|mention; ghostwispr.<session>.<kind>; " + "whatsapp.<phone>.<jid>.<kind>; envoy.exceptions.<original-topic>.";
46049
45512
  var URGENCY_VALUES = EnvelopeSchema.shape.urgency.unwrap().options;
46050
45513
  function messageMetadataShape(schema) {
@@ -46085,7 +45548,7 @@ var EnvoyToolOperation = {
46085
45548
  var envoyToolSpecs = [
46086
45549
  {
46087
45550
  name: "envoy_subscribe",
46088
- description: `Subscribe this session to Envoy notification topics. ${STORED_DELIVERY} ${TOPIC_GUIDE}`,
45551
+ description: `Subscribe this session to Envoy notification topics. A notifications. topic no stream subject captures is refused by name: nothing would store it, so you would receive nothing on it; subscribe to a notifications.team.<name> topic instead. ${STORED_DELIVERY} ${TOPIC_GUIDE}`,
46089
45552
  arguments: (schema) => ({
46090
45553
  topics: schema.array(schema.string()).describe("NATS-style topic patterns to subscribe to.")
46091
45554
  }),
@@ -46121,7 +45584,7 @@ var envoyToolSpecs = [
46121
45584
  },
46122
45585
  {
46123
45586
  name: "envoy_publish",
46124
- description: `Publish an Envoy message to a notifications. topic. ${STORED_DELIVERY} A role or built-in topic is delivered as its guide says. A role message has three possible answers. First, the session that holds the role takes the message. That session must confirm the message inside the listener's window, or this tool errs. Second, no session holds the role. The listener then keeps the message, and gives it to the next session that claims that role in the hour that follows. This is a success, and the result says that the listener kept the message. Third, the listener keeps nothing and errs. A holder that stopped answering keeps its role but was not sent the message, and nothing waits for it, so publish the message again once that holder's holder_state reads live, and start no replacement for that session. A team or ad-hoc topic (such as notifications.team.deploy-squad.all) reaches every session subscribed to it when you publish, and is not kept for a session that subscribes later. The listener confirms it with each other session that holds its own NATS connection and registered the topic, and errs naming each session that did not confirm; a session that stopped without unregistering stays registered until Envoy finds it gone and is named until then. A named session is not a failed delivery to the other sessions. A session that is only unresponsive is named as no failure at all. The listener stores that session's copy, asks it for no receipt, and names the session under unresponsive. The accepted publish and the error both name it there. That session takes the message when it answers again. A session that holds its own NATS connection receives such a live topic directly, yours included. ${SESSION_STATE_GUIDE} ${DELIVERY_CONTRACT}`,
45587
+ description: `Publish an Envoy message to a notifications. topic. ${STORED_DELIVERY} A role or built-in topic is delivered as its guide says. A role message has three possible answers. First, a session holds the role, and the stream stores the forward for that holder's consumer. Its 200 says the stream stored it, and it does not say that the holder read it; it names the holder and the state the listener resolved the holder in. Second, no session holds the role. The listener then keeps the message, and gives it to the next session that claims that role in the hour that follows. This is a success, and the result says that the listener kept the message. Third, the listener keeps nothing and errs, which tells you to reach the agent some other way: the holder refuses the message, or the forward is not stored within four seconds. A holder that stopped answering keeps its role but was not sent the message, and nothing waits for it, so publish the message again once that holder's holder_state reads live, and start no replacement for that session. A team topic (such as notifications.team.deploy-squad.all) is stored like every other, so a session subscribed to it that was away still receives it when it returns, and one that subscribes after you publish does not. A notifications. topic no stream subject captures is refused by name: nothing would store it. ${SESSION_STATE_GUIDE} ${DELIVERY_CONTRACT}`,
46125
45588
  arguments: (schema) => ({ topic: schema.string(), ...messageArguments(schema) }),
46126
45589
  operation: EnvoyToolOperation.publish
46127
45590
  },
@@ -46180,10 +45643,6 @@ function publishConfirmationText(result) {
46180
45643
  const state = result.holderState === undefined ? "" : ` (${result.holderState})`;
46181
45644
  sentences.push(`Its role holder is ${result.holder}${state}.`);
46182
45645
  }
46183
- if (result.unresponsive.length > 0) {
46184
- const one = result.unresponsive.length === 1;
46185
- sentences.push(`Envoy stored it for ${result.unresponsive.join(", ")}.`, `${one ? "That session" : "Those sessions"} stopped answering, so ${one ? "it has" : "they have"} not taken it yet.`, UNRESPONSIVE_RULE);
46186
- }
46187
45646
  return sentences.join(" ");
46188
45647
  }
46189
45648
  function sessionsOnMachine(sessions, machine) {
@@ -47040,7 +46499,7 @@ var import_nats2 = __toESM(require_mod4(), 1);
47040
46499
 
47041
46500
  // package.json
47042
46501
  var name = "@sjawhar/pi-envoy";
47043
- var version2 = "17.7.0";
46502
+ var version2 = "17.9.0";
47044
46503
  var package_default = {
47045
46504
  name,
47046
46505
  version: version2,
@@ -47113,7 +46572,7 @@ function customEntries(entries, customType) {
47113
46572
  function newestState(entries) {
47114
46573
  const states = customEntries(entries, REPLAY_STATE_ENTRY);
47115
46574
  for (let index = states.length - 1;index >= 0; index -= 1) {
47116
- const parsed = ReplayStateSchema.safeParse(states[index]);
46575
+ const parsed = LedgerStateSchema.safeParse(states[index]);
47117
46576
  if (parsed.success)
47118
46577
  return parsed.data;
47119
46578
  }
@@ -47122,7 +46581,7 @@ function newestState(entries) {
47122
46581
  function replaySnapshot(entries, now = Date.now()) {
47123
46582
  const ledger = [];
47124
46583
  for (const data of customEntries(entries, REPLAY_DELIVERED_ENTRY)) {
47125
- const parsed = ReplayLedgerEntrySchema.safeParse(data);
46584
+ const parsed = LedgerEntrySchema.safeParse(data);
47126
46585
  if (parsed.success)
47127
46586
  ledger.push(parsed.data);
47128
46587
  }
@@ -47223,7 +46682,7 @@ function writtenCards(entries, recorded, now) {
47223
46682
  return found;
47224
46683
  }
47225
46684
  function rememberedSubjects(entries) {
47226
- return rememberedTopics(newestState(entries));
46685
+ return rememberedTopics(newestState(entries)?.topics);
47227
46686
  }
47228
46687
  function heldReplayNotices(entries) {
47229
46688
  const held = new Map;
@@ -47901,12 +47360,14 @@ function createRegistrationState(deps) {
47901
47360
  const heldRole = () => claimedRoleTopic?.slice(ROLE_TOPIC_PREFIX.length);
47902
47361
  const isRoleBound = (subject) => roleNoticeSubjects.has(subject);
47903
47362
  const endRole = () => {
47363
+ const held = claimedRoleSessionID ?? "";
47364
+ const closed = [...roleNoticeSubjects];
47904
47365
  claimedRoleTopic = undefined;
47905
47366
  claimedRoleSessionID = undefined;
47906
47367
  roleEnds += 1;
47907
- for (const subject of roleNoticeSubjects)
47908
- deps.closeSubject(subject);
47909
47368
  roleNoticeSubjects.clear();
47369
+ if (closed.length > 0)
47370
+ deps.closeSubjects(held, closed);
47910
47371
  };
47911
47372
  const refreshRegistration = (input) => {
47912
47373
  holds.adoptFor(input.sessionID);
@@ -48730,7 +48191,7 @@ class UnwrittenFrames {
48730
48191
  return "closed";
48731
48192
  }
48732
48193
  const hand = deliverAs !== "inert" || this.#host.settled();
48733
- const keys = identity === undefined ? [] : unwrittenCardKeys(replayLedgerIdentity(identity));
48194
+ const keys = identity === undefined ? [] : unwrittenCardKeys(ledgerIdentity(identity));
48734
48195
  const card = identity === undefined || keys.length === 0 ? undefined : {
48735
48196
  sessionID: session,
48736
48197
  identity,
@@ -48761,7 +48222,7 @@ class UnwrittenFrames {
48761
48222
  return "shown";
48762
48223
  }
48763
48224
  waitsOnCard(identity) {
48764
- return unwrittenCardKeys(replayLedgerIdentity(identity)).some((key) => this.#cards.has(key));
48225
+ return unwrittenCardKeys(ledgerIdentity(identity)).some((key) => this.#cards.has(key));
48765
48226
  }
48766
48227
  sendNotice(content) {
48767
48228
  const notice = {
@@ -52562,7 +52023,7 @@ var NATS_RETRY_INTERVAL_MS = 15000;
52562
52023
  var DELIVERY_PICTURES_TIMEOUT_MS = 20000;
52563
52024
  var WHOLE_BODY_TIMEOUT_MS = 1e4;
52564
52025
  var BTW_EVENTS_LIMIT = 1000;
52565
- var REPLAYED_BTW_NOTE = "This BTW was asked while this session was not taking messages. It is shown for your information only: it runs no side turn and needs no answer. Dispatch shows whether it was answered.";
52026
+ var LATE_BTW_NOTE = "This BTW was asked while this session was not taking messages. It is shown for your information only: it runs no side turn and needs no answer. Dispatch shows whether it was answered.";
52566
52027
  var MOVED_SESSION_REFUSAL = `The message was not sent: ${MOVED_SESSION_REASON}`;
52567
52028
  var CAPABILITIES_WITHOUT_BTW = DELIVERY_CAPABILITIES.filter((capability) => capability !== "btw");
52568
52029
  var ClaimIdentitySchema = DedupeIdentitySchema.extend({
@@ -52746,8 +52207,6 @@ function envoyExtension(pi) {
52746
52207
  return active;
52747
52208
  };
52748
52209
  const passDeadlineMs = () => active?.settings.passDeadlineMs ?? MIN_PASS_DEADLINE_MS;
52749
- const subscriptions = new Map;
52750
- const confirming = new Map;
52751
52210
  const delivered = createDeliveryDedupe();
52752
52211
  let connection;
52753
52212
  let sessionDirectory2 = "";
@@ -52858,7 +52317,7 @@ function envoyExtension(pi) {
52858
52317
  legionManagedTranscript = branch.some(isLegionManagedEntry);
52859
52318
  const entries = context.sessionManager.getEntries();
52860
52319
  handledDispatchAttempts = handledAttempts(entries);
52861
- replay?.restore(replaySnapshot(entries));
52320
+ ledger?.restore(replaySnapshot(entries));
52862
52321
  unwritten.requeueNotices(heldReplayNotices(entries));
52863
52322
  markPicturesShown(shownPictures(sessionID), transcriptPictures(branch));
52864
52323
  if (previousSessionID !== sessionID) {
@@ -52970,7 +52429,6 @@ function envoyExtension(pi) {
52970
52429
  continue;
52971
52430
  unanswered.delete(watched);
52972
52431
  renewRelayEpoch();
52973
- triggerReplay();
52974
52432
  }
52975
52433
  } catch (error) {
52976
52434
  logger3.warn("envoy: the NATS connection's status reader failed", {
@@ -53006,7 +52464,7 @@ function envoyExtension(pi) {
53006
52464
  clockOffset: () => serverClock.offset()
53007
52465
  });
53008
52466
  const handCard = (content, deliverAs, identity) => {
53009
- const known = identity === undefined ? {} : replayLedgerIdentity(identity);
52467
+ const known = identity === undefined ? {} : ledgerIdentity(identity);
53010
52468
  if (deliverAs === "inert") {
53011
52469
  pi.sendMessage({
53012
52470
  customType: "envoy-message",
@@ -53028,7 +52486,7 @@ function envoyExtension(pi) {
53028
52486
  hostIsOn: (card) => hostSessionID() === card,
53029
52487
  settled: hostSettled,
53030
52488
  recordDelivered: async (card, identity) => {
53031
- await replay?.recordDeliveredFor(card, identity);
52489
+ await ledger?.recordDeliveredFor(card, identity);
53032
52490
  },
53033
52491
  handCard,
53034
52492
  cardNotShown: (card, identity) => {
@@ -53071,14 +52529,13 @@ function envoyExtension(pi) {
53071
52529
  });
53072
52530
  }
53073
52531
  });
53074
- const showReplayCard = async (session, content, envelope, deliverAs = "card", beforeHandOff) => {
52532
+ const showReplayCard = (session, content, envelope, deliverAs = "card") => {
53075
52533
  const parsed = ClaimIdentitySchema.safeParse(envelope);
53076
52534
  const identity = parsed.success ? parsed.data : undefined;
53077
52535
  const claim = delivered.claim(identity);
53078
52536
  if (claim === undefined && identity !== undefined && (delivered.pending(identity) || unwritten.waitsOnCard(identity))) {
53079
52537
  return "queued";
53080
52538
  }
53081
- await beforeHandOff?.();
53082
52539
  const outcome = unwritten.sendCard(session, content, deliverAs, identity, claim);
53083
52540
  return outcome === "closed" ? "failed" : outcome;
53084
52541
  };
@@ -53265,49 +52722,21 @@ function envoyExtension(pi) {
53265
52722
  logger3.warn("envoy: Dispatch did not accept a direct message as this session's turn; it arrives as a card", { ...fields, error: messageFor(refusal.error) });
53266
52723
  }
53267
52724
  });
53268
- const deliver = async (addressedSession, topic, subject, raw, reply, receivedAt, relayed) => {
53269
- const session = addressedSession ?? sessionID;
53270
- const addressedSessionOpen = () => addressedSession === undefined || sessionOpen(session);
52725
+ const deliver = async (session, subject, raw, relayed) => {
52726
+ const addressedSessionOpen = () => sessionOpen(session);
53271
52727
  let rendered = renderInbound(raw, session, subject);
53272
- const cardSession = () => rendered.delivery !== undefined && addressedSession !== undefined ? addressedSession : hostSessionID();
53273
- const directSubject = agentSubject(sessionID);
53274
- if (expectsLaneReceipt({
53275
- subject,
53276
- directSubject,
53277
- envelopeTopic: rendered.envelope?.topic,
53278
- reply
53279
- })) {
53280
- try {
53281
- (await ensureConnection()).publish(reply);
53282
- } catch (error) {
53283
- console.warn(`[envoy] failed to acknowledge envelope ${rendered.envelope?.event_id ?? "unknown"}`, error);
53284
- }
53285
- }
53286
- if (relayed === undefined && relaysCoreTopic(topic))
53287
- return;
53288
- let removed = false;
53289
- for (const topic of subscriptionRemovedTopics(raw, sessionID) ?? []) {
53290
- if (closeIntentionally(topic))
53291
- removed = true;
53292
- }
53293
- if (removed) {
53294
- replayFollowedChanged();
53295
- if (relay?.sessionID === sessionID && relay.delivery.relaying()) {
53296
- Promise.resolve().then(registerSession).catch(() => {
53297
- return;
53298
- });
53299
- }
53300
- }
52728
+ const cardSession = () => rendered.delivery !== undefined ? session : hostSessionID();
52729
+ await closeRemovedSubscriptions(raw, relayed.storedAtMs);
53301
52730
  const dedupeIdentity = ClaimIdentitySchema.safeParse(rendered.envelope);
53302
52731
  const identity = dedupeIdentity.success ? dedupeIdentity.data : undefined;
53303
52732
  const held = rendered.skip ? undefined : delivered.claim(identity);
53304
52733
  if (held === undefined) {
53305
- relayed?.done();
52734
+ relayed.done();
53306
52735
  return;
53307
52736
  }
53308
52737
  let handing = true;
53309
52738
  let releasedWhileHanding = false;
53310
- const claim = relayed === undefined ? held : {
52739
+ const claim = {
53311
52740
  settle: () => {
53312
52741
  held.settle();
53313
52742
  relayed.done();
@@ -53321,7 +52750,7 @@ function envoyExtension(pi) {
53321
52750
  relayed.nak();
53322
52751
  }
53323
52752
  };
53324
- if (identity !== undefined && replay?.delivered(identity) === true) {
52753
+ if (identity !== undefined && ledger?.delivered(identity) === true) {
53325
52754
  claim.settle();
53326
52755
  return;
53327
52756
  }
@@ -53335,7 +52764,7 @@ function envoyExtension(pi) {
53335
52764
  if (envelope !== undefined)
53336
52765
  noteInbox(envelope);
53337
52766
  const recordTaken = async (frame) => {
53338
- if (replay === undefined || await replay.recordDeliveredFor(session, frame))
52767
+ if (ledger === undefined || await ledger.recordDeliveredFor(session, frame))
53339
52768
  return;
53340
52769
  logger3.warn("envoy: a delivery was not recorded in the session it was addressed to", {
53341
52770
  addressed: session,
@@ -53359,17 +52788,14 @@ function envoyExtension(pi) {
53359
52788
  const refuse = async (delivery, error) => {
53360
52789
  held.release();
53361
52790
  await postDispatchReply(delivery, { error }, session);
53362
- relayed?.done();
52791
+ relayed.done();
53363
52792
  };
53364
52793
  const offWorker = (work) => {
53365
- relayed?.release();
52794
+ relayed.release();
53366
52795
  work().catch((error) => {
53367
52796
  held.release();
53368
52797
  console.warn(`[envoy] failed to deliver envelope ${envelope?.event_id ?? "unknown"}`, error);
53369
- if (relayed !== undefined)
53370
- relayed.done();
53371
- else if (envelope !== undefined)
53372
- replay?.liveFailed(cardSession(), topic, envelope, error, receivedAt);
52798
+ relayed.done();
53373
52799
  });
53374
52800
  };
53375
52801
  try {
@@ -53380,10 +52806,10 @@ function envoyExtension(pi) {
53380
52806
  } else if (rendered.malformedDelivery === true) {
53381
52807
  console.warn("[envoy] dropping malformed Dispatch targeted delivery without a reply address");
53382
52808
  claim.settle();
53383
- } else if (rendered.delivery?.mode === "btw" && relayed?.late === true) {
52809
+ } else if (rendered.delivery?.mode === "btw" && relayed.late === true) {
53384
52810
  noteBtw(identity?.event_id);
53385
- unwritten.sendCard(cardSession(), `${replayedText(rendered.content, relayed.storedAt)}
53386
- ${REPLAYED_BTW_NOTE}`, "inert", identity, claim);
52811
+ unwritten.sendCard(cardSession(), `${storedText(rendered.content, relayed.storedAt)}
52812
+ ${LATE_BTW_NOTE}`, "inert", identity, claim);
53387
52813
  } else if (rendered.delivery?.mode === "btw") {
53388
52814
  noteBtw(identity?.event_id);
53389
52815
  if (identity?.event_id !== undefined) {
@@ -53466,84 +52892,35 @@ ${REPLAYED_BTW_NOTE}`, "inert", identity, claim);
53466
52892
  handing = false;
53467
52893
  held.release();
53468
52894
  console.warn(`[envoy] failed to deliver envelope ${envelope?.event_id ?? "unknown"}`, error);
53469
- if (relayed === undefined && envelope !== undefined) {
53470
- replay?.liveFailed(cardSession(), topic, envelope, error, receivedAt);
53471
- }
53472
52895
  throw error;
53473
52896
  }
53474
52897
  handing = false;
53475
52898
  if (releasedWhileHanding)
53476
- relayed?.nak();
52899
+ relayed.nak();
53477
52900
  };
53478
52901
  const RESUBSCRIBE_DELAY_MS = Number(process.env.ENVOY_RESUBSCRIBE_DELAY_MS ?? "") || 5000;
53479
52902
  let shuttingDown = false;
53480
- const intentionallyClosed = new Set;
53481
- const awaitingRetry = new Set;
53482
- const SUBSCRIBE_FLUSH_TIMEOUT_MS = Number(process.env.ENVOY_SUBSCRIBE_FLUSH_TIMEOUT_MS ?? "") || 1e4;
53483
- const unanswered = new Set;
53484
- const closeIntentionally = (topic) => {
53485
- const subscription = subscriptions.get(topic);
53486
- if (subscription === undefined) {
53487
- if (!awaitingRetry.has(topic))
53488
- return false;
53489
- intentionallyClosed.add(topic);
53490
- return true;
52903
+ const followed = new Set;
52904
+ const follow = (topic) => {
52905
+ let added = false;
52906
+ for (const subject of expandSubscriptionTopics([topic])) {
52907
+ if (followed.has(subject))
52908
+ continue;
52909
+ followed.add(subject);
52910
+ added = true;
53491
52911
  }
53492
- intentionallyClosed.add(topic);
53493
- subscription.unsubscribe();
53494
- subscriptions.delete(topic);
53495
- return true;
53496
- };
53497
- const lineageOf = (id) => {
53498
- const lineage = continuedBy(id);
53499
- return announced !== undefined && announced.from !== id && carriesSession(announced.reason) && !lineage.includes(announced.from) ? [...lineage, announced.from] : lineage;
53500
- };
53501
- const topicAddress = (topic) => {
53502
- if (!topic.startsWith(agentSubject("")))
53503
- return { addressed: undefined };
53504
- const host = hostSessionID();
53505
- if (host === "")
53506
- return { addressed: undefined };
53507
- return lineageOf(host).some((id) => topic === agentSubject(id)) ? { addressed: host } : { replaced: true };
52912
+ return added;
53508
52913
  };
53509
- const pump = async (topic, subscription) => {
53510
- try {
53511
- for await (const { message, receivedAt } of readArrivals(subscription)) {
53512
- const address = topicAddress(topic);
53513
- if ("replaced" in address) {
53514
- logger3.warn("envoy: frame for a session a switch replaced left undelivered", {
53515
- subject: message.subject
53516
- });
53517
- continue;
53518
- }
53519
- try {
53520
- await deliver(address.addressed, topic, message.subject, codec2.decode(message.data), message.reply ?? "", receivedAt);
53521
- } catch {}
53522
- }
53523
- } catch {}
53524
- if (subscriptions.get(topic) === subscription)
53525
- subscriptions.delete(topic);
53526
- if (shuttingDown)
53527
- return;
53528
- if (intentionallyClosed.delete(topic))
53529
- return;
53530
- const snapshot = registrationState.roleSnapshot(topic);
53531
- const retry = (delayMs) => {
53532
- awaitingRetry.add(topic);
53533
- setTimeout(() => {
53534
- awaitingRetry.delete(topic);
53535
- if (shuttingDown || subscriptions.has(topic))
53536
- return;
53537
- if (intentionallyClosed.delete(topic))
53538
- return;
53539
- subscribeUnlessRoleEnds(topic, snapshot).then((wanted) => {
53540
- if (wanted)
53541
- triggerReplay();
53542
- }, () => retry(NATS_RETRY_INTERVAL_MS));
53543
- }, delayMs);
53544
- };
53545
- retry(RESUBSCRIBE_DELAY_MS);
52914
+ const unfollow = (topic) => {
52915
+ let dropped = false;
52916
+ for (const subject of expandSubscriptionTopics([topic])) {
52917
+ if (followed.delete(subject))
52918
+ dropped = true;
52919
+ }
52920
+ return dropped;
53546
52921
  };
52922
+ const SUBSCRIBE_FLUSH_TIMEOUT_MS = Number(process.env.ENVOY_SUBSCRIBE_FLUSH_TIMEOUT_MS ?? "") || 1e4;
52923
+ const unanswered = new Set;
53547
52924
  const within = async (promise, ms, what) => {
53548
52925
  const expired = Promise.withResolvers();
53549
52926
  const timer = setTimeout(() => expired.reject(new Error(`${what} timed out after ${ms} ms`)), ms);
@@ -53582,53 +52959,6 @@ ${REPLAYED_BTW_NOTE}`, "inert", identity, claim);
53582
52959
  unanswered.delete(connection);
53583
52960
  }
53584
52961
  };
53585
- const subscribe = async (topic, connect = ensureConnection) => {
53586
- const subjects = expandSubscriptionTopics([topic]);
53587
- const inFlight = subjects.flatMap((subject) => confirming.get(subject) ?? []);
53588
- if (inFlight.length > 0)
53589
- await Promise.allSettled(inFlight);
53590
- const fresh = subjects.filter((candidate) => !subscriptions.has(candidate));
53591
- if (fresh.length === 0)
53592
- return false;
53593
- const activeConnection = await connect();
53594
- const opened = [];
53595
- const closeOpened = () => {
53596
- for (const [subject, subscription] of opened) {
53597
- if (subscriptions.get(subject) === subscription)
53598
- closeIntentionally(subject);
53599
- }
53600
- };
53601
- try {
53602
- for (const subject of fresh) {
53603
- const subscription = activeConnection.subscribe(subject);
53604
- intentionallyClosed.delete(subject);
53605
- subscriptions.set(subject, subscription);
53606
- opened.push([subject, subscription]);
53607
- pump(subject, subscription);
53608
- }
53609
- } catch (error) {
53610
- closeOpened();
53611
- throw error;
53612
- }
53613
- const settled = confirmSubscribed(activeConnection, `the subscription to ${topic}`);
53614
- const confirmation = settled.catch(() => {
53615
- return;
53616
- });
53617
- for (const [subject] of opened)
53618
- confirming.set(subject, confirmation);
53619
- try {
53620
- await settled;
53621
- } catch (error) {
53622
- closeOpened();
53623
- throw error;
53624
- } finally {
53625
- for (const [subject] of opened) {
53626
- if (confirming.get(subject) === confirmation)
53627
- confirming.delete(subject);
53628
- }
53629
- }
53630
- return true;
53631
- };
53632
52962
  const noteInbox = (envelope) => {
53633
52963
  inbox.unshift({
53634
52964
  event_id: envelope.event_id ?? "unknown",
@@ -53642,62 +52972,42 @@ ${REPLAYED_BTW_NOTE}`, "inert", identity, claim);
53642
52972
  const followedSubjects = () => {
53643
52973
  if (sessionID === "")
53644
52974
  return [];
53645
- const open = [...subscriptions.keys(), ...awaitingRetry].filter((subject) => !intentionallyClosed.has(subject));
53646
- return replayFollowedSubjects(agentSubject(sessionID), open, registrationState.isRoleBound);
53647
- };
53648
- const showReplayed = async (frame) => {
53649
- const addressed = sessionID;
53650
- const rendered = renderInbound(frame.data, addressed, frame.subject);
53651
- if (replay !== undefined) {
53652
- await closeReplayedRemoval(frame, {
53653
- sessionID: addressed,
53654
- engine: replay,
53655
- close: closeIntentionally,
53656
- unregister: async (topics) => {
53657
- await active?.client.unsubscribe({ sessionID: addressed, topics }).catch((error) => logger3.warn("envoy: a replayed subscription removal was not taken out of the registry", { topics, error: messageFor(error) }));
53658
- },
53659
- followedChanged: replayFollowedChanged
53660
- });
53661
- }
53662
- if (!sessionOpen(addressed)) {
53663
- logger3.warn("envoy: a replayed card was not shown here; the session it names is closed", {
53664
- addressed,
53665
- eventID: rendered.envelope?.event_id,
53666
- open: sessionID
53667
- });
53668
- return "failed";
53669
- }
53670
- if (replaySuppressed(rendered))
53671
- return "suppressed";
53672
- if (rendered.envelope !== undefined)
53673
- noteInbox(rendered.envelope);
53674
- const shown = replayedText(rendered.content, frame.time);
53675
- if (rendered.delivery?.mode === "btw") {
53676
- noteBtw(rendered.envelope?.event_id);
53677
- return showReplayCard(addressed, `${shown}
53678
- ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
53679
- }
53680
- return showReplayCard(addressed, shown, frame.envelope, "card", isHumanDispatchCard(rendered) ? () => armAskAwarenessForPersonInput(activeSessionContext, addressed) : undefined);
52975
+ return replayFollowedSubjects(agentSubject(sessionID), followed, registrationState.isRoleBound);
52976
+ };
52977
+ const closeRemovedSubscriptions = async (raw, storedAtMs) => {
52978
+ const closed = await closeReplayedRemoval({ data: raw, storedAtMs }, {
52979
+ sessionID,
52980
+ relay: relay?.sessionID === sessionID ? relay.delivery : undefined,
52981
+ close: unfollow,
52982
+ unregister: async (topics) => {
52983
+ await active?.client.unsubscribe({ sessionID, topics }).catch((error) => logger3.warn("envoy: a subscription removal was not taken out of the registry", {
52984
+ topics,
52985
+ error: messageFor(error)
52986
+ }));
52987
+ },
52988
+ followedChanged: ledgerFollowedChanged
52989
+ });
52990
+ if (closed.length === 0)
52991
+ return;
52992
+ Promise.resolve().then(registerSession).catch(() => {
52993
+ return;
52994
+ });
53681
52995
  };
53682
52996
  const transcriptSessionID = () => {
53683
52997
  const host = hostSessionID();
53684
52998
  return host !== "" && host === sessionID ? host : undefined;
53685
52999
  };
53686
- const tellReplayNotice = (notice) => {
53000
+ const tellGapNotice = (notice) => {
53687
53001
  logger3.warn(`envoy: ${notice.message}`);
53688
- routeReplayNotice(notice, {
53689
- warn: (message) => activeSessionContext?.ui.notify(`envoy: ${message}`, "warning"),
53690
- card: (content) => unwritten.sendNotice(content)
53691
- });
53002
+ unwritten.sendNotice(gapNoticeText(notice));
53692
53003
  };
53693
- const replay = active === undefined || !inbound ? undefined : createReplayEngine({
53004
+ const ledger = active === undefined || !inbound ? undefined : createDeliveryLedger({
53694
53005
  host: {
53695
53006
  sessionID: () => shuttingDown ? undefined : transcriptSessionID(),
53696
53007
  writeSessionID: transcriptSessionID,
53697
53008
  followedSubjects,
53698
- deliver: showReplayed,
53699
- showFailureNotice: async (notice) => {
53700
- const outcome = await showReplayCard(sessionID, envoyNoticeText(failureNoticeFields(notice)), notice.envelope, btwEvents.has(notice.eventId) ? "inert" : "card");
53009
+ showFailureNotice: async (addressed, notice) => {
53010
+ const outcome = showReplayCard(addressed, envoyNoticeText(failureNoticeFields(notice)), notice.envelope, btwEvents.has(notice.eventId) ? "inert" : "card");
53701
53011
  if (outcome === "failed")
53702
53012
  throw new Error("the session it names is closed");
53703
53013
  return outcome;
@@ -53709,7 +53019,6 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
53709
53019
  });
53710
53020
  activeSessionContext?.ui.notify(`envoy: message ${notice.eventId} from ${notice.sender} was not shown, nor was the notice naming it (${messageFor(error)})`, "warning");
53711
53021
  },
53712
- notify: tellReplayNotice,
53713
53022
  log: (message, error) => logger3.warn(`envoy: ${message}`, error === undefined ? undefined : { error: messageFor(error) }),
53714
53023
  write: (change) => {
53715
53024
  for (const entry of change.delivered)
@@ -53717,28 +53026,21 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
53717
53026
  if (change.state !== undefined)
53718
53027
  pi.appendEntry(REPLAY_STATE_ENTRY, change.state);
53719
53028
  }
53720
- },
53721
- transport: active.client,
53722
- dedupe: delivered
53029
+ }
53723
53030
  });
53724
- const replayOpen = () => !shuttingDown && sessionID !== "" && registrationState.inboundOpenSession() === sessionID;
53725
- const triggerReplay = () => {
53726
- if (replay !== undefined && replayOpen())
53727
- replay.sweep();
53728
- };
53729
- const replayFollowedChanged = async () => {
53730
- if (replay !== undefined && replayOpen())
53731
- await replay.followedChanged();
53031
+ const ledgerOpen = () => !shuttingDown && sessionID !== "" && registrationState.inboundOpenSession() === sessionID;
53032
+ const ledgerFollowedChanged = async () => {
53033
+ if (ledger !== undefined && ledgerOpen())
53034
+ await ledger.followedChanged();
53732
53035
  };
53733
53036
  let relay;
53734
- const relaysCoreTopic = (topic) => relay?.sessionID === sessionID && relay.delivery.relays(topic);
53735
53037
  const deliverRelayed = async (delivery, frame) => {
53736
53038
  const owner = relay;
53737
53039
  if (shuttingDown || owner?.delivery !== delivery || !sessionOpen(owner.sessionID)) {
53738
53040
  frame.nak();
53739
53041
  return;
53740
53042
  }
53741
- await deliver(owner.sessionID, frame.subject, frame.subject, frame.data, "", Date.now(), frame);
53043
+ await deliver(owner.sessionID, frame.subject, frame.data, frame);
53742
53044
  };
53743
53045
  const createRelay = (id, entries, ledger) => {
53744
53046
  let persistedFollows = "";
@@ -53747,7 +53049,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
53747
53049
  ledger,
53748
53050
  host: {
53749
53051
  deliver: (frame) => deliverRelayed(delivery, frame),
53750
- gap: tellReplayNotice,
53052
+ gap: tellGapNotice,
53751
53053
  log: (message, error) => logger3.warn(`envoy: ${message}`, error === undefined ? undefined : { error: messageFor(error) })
53752
53054
  },
53753
53055
  publish: (reply, body) => {
@@ -53794,11 +53096,11 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
53794
53096
  }
53795
53097
  };
53796
53098
  const openRelay = async (context, id, connect) => {
53797
- if (replay === undefined)
53099
+ if (ledger === undefined)
53798
53100
  return;
53799
53101
  if (relay?.sessionID !== id) {
53800
53102
  closeRelay();
53801
- relay = createRelay(id, context.sessionManager.getEntries(), replay);
53103
+ relay = createRelay(id, context.sessionManager.getEntries(), ledger);
53802
53104
  }
53803
53105
  await subscribeRelay(relay, connect);
53804
53106
  };
@@ -53841,17 +53143,10 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
53841
53143
  return;
53842
53144
  });
53843
53145
  };
53844
- const takeDeliveryAnswer = (registeredSession, topics, asked, result) => {
53845
- if (sessionID !== registeredSession || asked !== undefined && relay !== asked)
53846
- return;
53847
- if (asked !== undefined) {
53848
- asked.delivery.answered(result.delivery, topics);
53849
- return;
53850
- }
53851
- if (replay === undefined)
53146
+ const takeDeliveryAnswer = (registeredSession, asked, result) => {
53147
+ if (sessionID !== registeredSession || relay !== asked)
53852
53148
  return;
53853
- const open = relay?.sessionID === registeredSession ? relay : undefined;
53854
- registeredWithoutRelay(replay, open?.delivery, topics);
53149
+ asked.delivery.answered(result.delivery);
53855
53150
  };
53856
53151
  const agentStream = new AgentStreamPublisher({
53857
53152
  now: () => Date.now(),
@@ -54100,20 +53395,28 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54100
53395
  },
54101
53396
  dial: dialNats,
54102
53397
  connect: ensureConnection,
54103
- subscribeTopic: subscribe,
54104
- closeSubject: (subject) => {
54105
- closeIntentionally(subject);
53398
+ subscribeTopic: async (topic) => follow(topic),
53399
+ closeSubjects: (held, subjects) => {
53400
+ for (const subject of subjects)
53401
+ unfollow(subject);
53402
+ const id = held === "" ? sessionID : held;
53403
+ if (subjects.length === 0 || id === "")
53404
+ return;
53405
+ active?.client.unsubscribe({ sessionID: id, topics: subjects }).catch((error) => logger3.warn("envoy: a role's notice subjects were not taken out of the registry", {
53406
+ topics: subjects,
53407
+ sessionID: id,
53408
+ error: messageFor(error)
53409
+ }));
54106
53410
  }
54107
53411
  });
54108
- const subscribeUnlessRoleEnds = async (topic, snapshot) => {
53412
+ const followUnlessRoleEnds = (topic, snapshot) => {
54109
53413
  const roleEnded = () => snapshot.roleBound && registrationState.roleEndCount() !== snapshot.endsBefore;
54110
53414
  if (roleEnded())
54111
53415
  return false;
54112
- await subscribe(topic);
53416
+ follow(topic);
54113
53417
  if (!roleEnded())
54114
53418
  return true;
54115
- for (const subject of expandSubscriptionTopics([topic]))
54116
- closeIntentionally(subject);
53419
+ unfollow(topic);
54117
53420
  return false;
54118
53421
  };
54119
53422
  const registrationsInFlight = new Map;
@@ -54148,43 +53451,45 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54148
53451
  }
54149
53452
  await registrationState.deregister(id);
54150
53453
  };
53454
+ const inboundOutage = inboundUnavailable((why) => {
53455
+ logger3.warn(`envoy: this session receives nothing: ${why}`);
53456
+ activeSessionContext?.ui.notify(`envoy: inbound messages are unavailable (${why}); registering again every heartbeat`, "warning");
53457
+ unwritten.sendNotice(inboundUnavailableNoticeText(why));
53458
+ });
54151
53459
  const tmuxPlaces = createTmuxPlaceCache({
54152
53460
  read: () => readTmuxPlace(process.env, defaultExec),
54153
53461
  lifetimeMs: () => active?.settings.tmuxPlaceCacheMs ?? DEFAULT_TMUX_PLACE_CACHE_MS,
54154
53462
  onFailure: (error) => logger3.debug("envoy: the tmux place reading failed", { error: messageFor(error) })
54155
53463
  });
54156
- const registrationInput = (asking, place) => {
54157
- const own = agentSubject(sessionID);
54158
- const topics = [
54159
- ...new Set([
54160
- own,
54161
- ...[...subscriptions.keys()].filter((subject) => !registrationState.isRoleBound(subject) && !reachesPrivateStreamSubject(own, subject))
54162
- ])
54163
- ];
54164
- if (asking !== undefined)
54165
- asking.delivery.setFollowed(topics);
54166
- return {
54167
- sessionID,
54168
- directory: sessionDirectory2,
54169
- topics,
54170
- port: 0,
54171
- title: activeSessionContext?.sessionManager.getSessionName?.() ?? "",
54172
- capabilities: sideTurn(activeSessionContext) === undefined ? CAPABILITIES_WITHOUT_BTW : DELIVERY_CAPABILITIES,
54173
- driving: false,
54174
- selfSubscribed: true,
54175
- plugin: { name, version: version2 },
54176
- ...asking === undefined ? {} : { delivery: asking.delivery.registration() },
54177
- ...place === undefined ? {} : { place }
54178
- };
54179
- };
54180
- const sendRegistration = async (send) => {
53464
+ const sendRegistration = (send) => {
54181
53465
  const place = tmuxPlaces.place();
54182
53466
  const registeringFor = sessionID;
54183
- const asking = relay?.sessionID === registeringFor && relay.subscription !== undefined && replay?.startPending() !== true ? relay : undefined;
54184
- const input = registrationInput(asking, place);
54185
- const result = await send(input);
54186
- takeDeliveryAnswer(registeringFor, input.topics, asking, result);
54187
- return result;
53467
+ const asking = relay?.sessionID === registeringFor && relay.subscription !== undefined ? relay : undefined;
53468
+ const role = registrationState.heldRole();
53469
+ return registerWithRelay({
53470
+ relay: asking?.delivery,
53471
+ ownAgentSubject: agentSubject(registeringFor),
53472
+ followed,
53473
+ roles: role === undefined ? [] : [role],
53474
+ outage: inboundOutage,
53475
+ send: (topics, delivery) => send({
53476
+ sessionID: registeringFor,
53477
+ directory: sessionDirectory2,
53478
+ topics,
53479
+ port: 0,
53480
+ title: activeSessionContext?.sessionManager.getSessionName?.() ?? "",
53481
+ capabilities: sideTurn(activeSessionContext) === undefined ? CAPABILITIES_WITHOUT_BTW : DELIVERY_CAPABILITIES,
53482
+ driving: false,
53483
+ selfSubscribed: true,
53484
+ plugin: { name, version: version2 },
53485
+ delivery,
53486
+ ...place === undefined ? {} : { place }
53487
+ }),
53488
+ answered: (result) => {
53489
+ if (asking !== undefined)
53490
+ takeDeliveryAnswer(registeringFor, asking, result);
53491
+ }
53492
+ });
54188
53493
  };
54189
53494
  const registerSession = () => trackRegistration(sessionID, sendRegistration(registrationState.refreshRegistration));
54190
53495
  let writtenTitle;
@@ -54239,7 +53544,6 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54239
53544
  return;
54240
53545
  writeHeartbeatTitle(context);
54241
53546
  unwritten.handBackWithdrawn();
54242
- triggerReplay();
54243
53547
  if (healing) {
54244
53548
  const refreshMs = requireEnvoy().settings.heartbeatMs;
54245
53549
  if (registrationState.registrationFresherThan(sessionID, refreshMs))
@@ -54406,7 +53710,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54406
53710
  lastHeard = "";
54407
53711
  }
54408
53712
  for (const other of handle.forgetOtherAgentSessions(host)) {
54409
- closeIntentionally(agentSubject(other));
53713
+ unfollow(agentSubject(other));
54410
53714
  leftSessions.add(other);
54411
53715
  }
54412
53716
  if (relay !== undefined && relay.sessionID !== host)
@@ -54417,7 +53721,6 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54417
53721
  return true;
54418
53722
  }
54419
53723
  let inboundFailure;
54420
- let openedInbound = false;
54421
53724
  if (inbound && registrationState.inboundOpenSession() !== host) {
54422
53725
  try {
54423
53726
  handle.markAgentSessionOpen(host);
@@ -54429,24 +53732,18 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54429
53732
  await recoverRegisteredInterests(handle);
54430
53733
  }
54431
53734
  handle.markInboundOpen(host);
54432
- openedInbound = true;
54433
53735
  } catch (error) {
54434
53736
  inboundFailure = error;
54435
53737
  }
54436
53738
  }
54437
53739
  handle.check();
54438
- if (inboundFailure !== undefined && replay !== undefined)
54439
- await replay.startCursors();
54440
53740
  const registration = await trackRegistration(host, sendRegistration(handle.register)).then(() => {
54441
53741
  return;
54442
53742
  }, (error) => ({ error }));
54443
53743
  handle.check();
54444
- if (registration === undefined) {
53744
+ if (registration === undefined)
54445
53745
  handle.markEstablished(host);
54446
- ensureHeartbeat(context);
54447
- }
54448
- if (openedInbound)
54449
- triggerReplay();
53746
+ ensureHeartbeat(context);
54450
53747
  const decided = await registrationState.roleMoves.run((move) => settleRole(context, host, cause, registration === undefined, move)) ?? false;
54451
53748
  handle.check();
54452
53749
  leftSessions.delete(host);
@@ -54563,6 +53860,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54563
53860
  if (context === undefined || context.sessionManager.getSessionId() !== targetSessionID) {
54564
53861
  throw new Error(`Envoy has no active session for Legion notice subscription: ${targetSessionID}`);
54565
53862
  }
53863
+ requireInbound();
54566
53864
  if (registrationState.establishedFor() !== targetSessionID) {
54567
53865
  await establishForBridge(context);
54568
53866
  }
@@ -54576,15 +53874,16 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54576
53874
  }
54577
53875
  const snapshot = registrationState.roleSnapshot(topic);
54578
53876
  try {
54579
- if (!await subscribeUnlessRoleEnds(topic, snapshot))
53877
+ if (!followUnlessRoleEnds(topic, snapshot))
54580
53878
  return;
53879
+ await registerSession();
54581
53880
  } catch (error) {
54582
53881
  if (registrationState.roleEndCount() === snapshot.endsBefore) {
53882
+ unfollow(topic);
54583
53883
  registrationState.unmarkRoleBoundSubjects(marked);
54584
53884
  }
54585
53885
  throw error;
54586
53886
  }
54587
- await registerSession();
54588
53887
  };
54589
53888
  const claimInstance = {
54590
53889
  claim,
@@ -54622,13 +53921,13 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54622
53921
  try {
54623
53922
  await establish();
54624
53923
  } catch (error) {
54625
- context.ui.notify(`envoy: NATS unavailable (${messageFor(error)}); retrying in the background`, "warning");
53924
+ context.ui.notify(`envoy: this session is not registered (${messageFor(error)}); retrying in the background`, "warning");
54626
53925
  let attempting = false;
54627
53926
  context.setInterval(() => {
54628
53927
  if (established || attempting)
54629
53928
  return;
54630
53929
  attempting = true;
54631
- establish().then(() => context.ui.notify("envoy: NATS connection established", "warning")).catch(() => {
53930
+ establish().then(() => context.ui.notify("envoy: this session is registered", "warning")).catch(() => {
54632
53931
  return;
54633
53932
  }).finally(() => {
54634
53933
  attempting = false;
@@ -54725,9 +54024,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
54725
54024
  clearTimeout(timer);
54726
54025
  connection = undefined;
54727
54026
  activeSessionContext = undefined;
54728
- subscriptions.clear();
54729
- intentionallyClosed.clear();
54730
- awaitingRetry.clear();
54027
+ followed.clear();
54731
54028
  unanswered.clear();
54732
54029
  agentStreamControl = undefined;
54733
54030
  agentStreamControlSession = "";
@@ -55040,16 +54337,13 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
55040
54337
  const { client } = requireEnvoy();
55041
54338
  switch (operation) {
55042
54339
  case EnvoyToolOperation.subscribe: {
54340
+ requireInbound();
54341
+ const requested = topicsFor(parameters);
54342
+ refuseUncapturedTopics(sessionID, agentSubject(sessionID), requested);
55043
54343
  const added = [];
55044
54344
  const already = [];
55045
- let openFailure;
55046
- for (const topic of topicsFor(parameters)) {
55047
- try {
55048
- (await subscribe(topic) ? added : already).push(topic);
55049
- } catch (error) {
55050
- openFailure = error;
55051
- break;
55052
- }
54345
+ for (const topic of requested) {
54346
+ (follow(topic) ? added : already).push(topic);
55053
54347
  }
55054
54348
  let warnings;
55055
54349
  let registrationError;
@@ -55059,11 +54353,8 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
55059
54353
  } catch (error) {
55060
54354
  registrationError = messageFor(error);
55061
54355
  }
55062
- await replayFollowedChanged();
55063
- triggerReplay();
54356
+ await ledgerFollowedChanged();
55064
54357
  }
55065
- if (openFailure !== undefined)
55066
- throw openFailure;
55067
54358
  const subscribed = `Subscribed: ${added.join(", ") || "(none new)"}`;
55068
54359
  return toolSuccess(warnings === undefined || warnings.length === 0 ? subscribed : `${subscribed}
55069
54360
  Warnings: ${warnings.join("; ")}`, {
@@ -55075,11 +54366,11 @@ Warnings: ${warnings.join("; ")}`, {
55075
54366
  }
55076
54367
  case EnvoyToolOperation.unsubscribe: {
55077
54368
  const inbox = agentSubject(sessionID);
55078
- const candidates = topicsFor(parameters, [...subscriptions.keys()]).filter((subject) => subject !== inbox);
54369
+ const candidates = topicsFor(parameters, [...followed]).filter((subject) => subject !== inbox);
55079
54370
  const kept = candidates.filter(registrationState.isRoleBound);
55080
- const removed = candidates.filter((subject) => !registrationState.isRoleBound(subject)).filter(closeIntentionally);
54371
+ const removed = candidates.filter((subject) => !registrationState.isRoleBound(subject)).filter((subject) => followed.delete(subject));
55081
54372
  if (removed.length > 0)
55082
- await replayFollowedChanged();
54373
+ await ledgerFollowedChanged();
55083
54374
  const registryRemoved = removed.filter((topic) => !topic.startsWith(ROLE_TOPIC_PREFIX));
55084
54375
  const registrationError = registryRemoved.length === 0 ? undefined : await client.unsubscribe({ sessionID, topics: registryRemoved }).then(registerSession).then(() => {
55085
54376
  return;
@@ -55094,7 +54385,7 @@ Kept (they last while this session holds role ${registrationState.heldRole()}):
55094
54385
  }
55095
54386
  case EnvoyToolOperation.listInterests: {
55096
54387
  const registry = await client.getInterest(sessionID);
55097
- const interests = mergeInterestSources(registry.topics, [...subscriptions.keys()]);
54388
+ const interests = mergeInterestSources(registry.topics, [...followed]);
55098
54389
  return toolSuccess(JSON.stringify({ ...registry, topics: [...interests.keys()] }, null, 2), {
55099
54390
  interests: [...interests].map(([topic, source]) => ({ topic, source }))
55100
54391
  });
@@ -55136,7 +54427,6 @@ Not delivered: that holder is the session this subagent sends as, and the listen
55136
54427
  topic,
55137
54428
  ...result.holder === undefined ? {} : { holder: result.holder },
55138
54429
  ...result.holderState === undefined ? {} : { holder_state: result.holderState },
55139
- ...result.unresponsive.length === 0 ? {} : { unresponsive: result.unresponsive },
55140
54430
  ...result.kept === undefined ? {} : { kept: result.kept },
55141
54431
  ...result.keptReason === undefined ? {} : { kept_reason: result.keptReason },
55142
54432
  ...undelivered ? { undelivered_echo: true } : {}
@@ -55184,7 +54474,7 @@ Not delivered: that holder is the session this subagent sends as, and the listen
55184
54474
  ...subagent === undefined ? {} : { subagent }
55185
54475
  }, null, 2), {
55186
54476
  sessionID: address,
55187
- topics: [...subscriptions.keys()],
54477
+ topics: [...followed],
55188
54478
  ...subagent === undefined ? {} : { subagent }
55189
54479
  });
55190
54480
  }