@sjawhar/pi-envoy 16.41.0 → 16.42.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
@@ -33217,6 +33217,14 @@ function projectedToken(path) {
33217
33217
  };
33218
33218
  }
33219
33219
 
33220
+ // ../contracts/src/trailing-slash.ts
33221
+ function withoutTrailingSlashes(url) {
33222
+ let end = url.length;
33223
+ while (end > 0 && url[end - 1] === "/")
33224
+ end -= 1;
33225
+ return url.slice(0, end);
33226
+ }
33227
+
33220
33228
  // ../../node_modules/.bun/oauth4webapi@3.8.8/node_modules/oauth4webapi/build/index.js
33221
33229
  var USER_AGENT;
33222
33230
  if (typeof navigator === "undefined" || !navigator.userAgent?.startsWith?.("Mozilla/5.0 ")) {
@@ -34307,12 +34315,6 @@ function statusOf(error) {
34307
34315
  }
34308
34316
  return 0;
34309
34317
  }
34310
- function withoutTrailingSlashes(url) {
34311
- let end = url.length;
34312
- while (end > 0 && url[end - 1] === "/")
34313
- end -= 1;
34314
- return url.slice(0, end);
34315
- }
34316
34318
 
34317
34319
  class BrokerTokenClient {
34318
34320
  #brokerUrl;
@@ -35699,7 +35701,7 @@ class DispatchClient {
35699
35701
  this.fetchImpl = fetchImpl;
35700
35702
  this.writeRetry = writeRetry;
35701
35703
  this.#credential = credential;
35702
- this.#baseUrl = baseUrl.replace(/\/+$/, "");
35704
+ this.#baseUrl = withoutTrailingSlashes(baseUrl);
35703
35705
  this.#signal = requestSignal(signal);
35704
35706
  this.#madeAt = this.#now();
35705
35707
  }
@@ -37368,2395 +37370,2259 @@ function streamStores(topic) {
37368
37370
  return STREAM_SUBJECT_PREFIXES.some((prefix) => topic.length > prefix.length && topic.startsWith(prefix));
37369
37371
  }
37370
37372
 
37371
- // ../envoy-client/src/envoy-config.ts
37372
- import {
37373
- chmodSync,
37374
- closeSync as closeSync2,
37375
- existsSync,
37376
- lstatSync,
37377
- mkdtempSync,
37378
- openSync as openSync2,
37379
- readFileSync as readFileSync3,
37380
- readlinkSync as readlinkSync2,
37381
- rmSync as rmSync2,
37382
- statSync as statSync2
37383
- } from "fs";
37384
- import { homedir, tmpdir } from "os";
37385
- import * as path from "path";
37386
-
37387
- // ../envoy-client/src/json-position.ts
37388
- function jsonErrorPosition(text) {
37389
- let at = 0;
37390
- const fail = () => {
37391
- throw new RangeError("not JSON");
37392
- };
37393
- const space = () => {
37394
- while (at < text.length && `
37395
- \r`.includes(text.charAt(at)))
37396
- at += 1;
37397
- };
37398
- const expect = (char) => {
37399
- if (text.charAt(at) !== char)
37400
- fail();
37401
- at += 1;
37402
- };
37403
- const string = () => {
37404
- expect('"');
37405
- while (at < text.length) {
37406
- const char = text.charAt(at);
37407
- if (char === '"') {
37408
- at += 1;
37409
- return;
37410
- }
37411
- if (char < " ")
37412
- fail();
37413
- at += char === "\\" ? 2 : 1;
37414
- }
37415
- fail();
37416
- };
37417
- const value = () => {
37418
- space();
37419
- const char = text.charAt(at);
37420
- if (char === "{" || char === "[") {
37421
- const close = char === "{" ? "}" : "]";
37422
- at += 1;
37423
- space();
37424
- if (text.charAt(at) === close) {
37425
- at += 1;
37426
- return;
37427
- }
37428
- for (;; ) {
37429
- if (close === "}") {
37430
- space();
37431
- string();
37432
- space();
37433
- expect(":");
37434
- }
37435
- value();
37436
- space();
37437
- if (text.charAt(at) === ",") {
37438
- at += 1;
37439
- continue;
37440
- }
37441
- expect(close);
37442
- return;
37443
- }
37444
- }
37445
- if (char === '"') {
37446
- string();
37447
- return;
37448
- }
37449
- for (const word of ["true", "false", "null"]) {
37450
- if (text.startsWith(word, at)) {
37451
- at += word.length;
37452
- return;
37453
- }
37454
- }
37455
- const number = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/.exec(text.slice(at));
37456
- if (number === null)
37457
- fail();
37458
- at += number?.[0].length ?? 0;
37459
- };
37460
- try {
37461
- value();
37462
- space();
37463
- if (at < text.length)
37464
- fail();
37465
- return;
37466
- } catch {
37467
- const before = text.slice(0, at).split(`
37468
- `);
37469
- return `line ${before.length}, column ${(before.at(-1)?.length ?? 0) + 1}`;
37470
- }
37373
+ // ../envoy-client/src/machine.ts
37374
+ import { hostname as hostname3 } from "os";
37375
+ function machineID() {
37376
+ return process.env["ENVOY_MACHINE_ID"] || process.env["AGENTBOX_HOST"] || hostname3();
37471
37377
  }
37472
37378
 
37473
- // ../envoy-client/src/secret-file.ts
37474
- import { readFileSync as readFileSync2 } from "fs";
37475
- function sendable(value) {
37476
- return !/[\s\p{Cc}]/u.test(value);
37379
+ // ../envoy-client/src/transport.ts
37380
+ var REPLAY_TIMEOUT_MS = 1e4;
37381
+ var REPLAY_UNAVAILABLE_CODE = "REPLAY_UNAVAILABLE";
37382
+ var SessionStateSchema = _enum2(["live", "unresponsive", "gone"]);
37383
+ 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.";
37384
+ var WireStateSchema = SessionStateSchema.default("live");
37385
+ var InterestWireSchema = object({
37386
+ session_id: string2(),
37387
+ machine_id: string2(),
37388
+ dir: string2(),
37389
+ topics: array(string2()),
37390
+ updated_at: number2().int().optional(),
37391
+ warnings: array(string2()).optional()
37392
+ });
37393
+ var DeliveryAnswerWireSchema = discriminatedUnion("mode", [
37394
+ object({
37395
+ mode: literal("consumer"),
37396
+ subject: string2().min(1),
37397
+ stream_id: string2(),
37398
+ gap: boolean2(),
37399
+ consumer_start: number2().int().nonnegative().optional()
37400
+ }),
37401
+ object({ mode: literal("core"), reason: string2() })
37402
+ ]);
37403
+ var SessionWireSchema = object({
37404
+ session_id: string2(),
37405
+ machine_id: string2(),
37406
+ host: string2().optional(),
37407
+ dir: string2(),
37408
+ port: number2().int(),
37409
+ title: string2(),
37410
+ topics: array(string2()),
37411
+ roles: array(string2()).optional(),
37412
+ capabilities: array(string2()).optional(),
37413
+ self_subscribed: boolean2(),
37414
+ last_seen: number2().int().optional(),
37415
+ state: WireStateSchema,
37416
+ plugin: string2().optional(),
37417
+ plugin_version: string2().optional()
37418
+ });
37419
+ var RoleWireSchema = object({
37420
+ role: string2(),
37421
+ holder: string2(),
37422
+ host: string2().optional(),
37423
+ last_seen: number2().int(),
37424
+ state: WireStateSchema,
37425
+ holder_state: SessionStateSchema.optional(),
37426
+ client_instance_id: string2().optional(),
37427
+ handed_back: boolean2().optional()
37428
+ });
37429
+ var RoleHeldWireSchema = object({
37430
+ error: string2(),
37431
+ role: string2(),
37432
+ holder: string2()
37433
+ });
37434
+ var ErrorWireSchema = object({
37435
+ error: string2(),
37436
+ code: string2().optional(),
37437
+ expected: array(string2()).optional(),
37438
+ state: SessionStateSchema.optional(),
37439
+ holder: string2().optional(),
37440
+ holder_state: SessionStateSchema.optional(),
37441
+ last_seen: number2().int().optional(),
37442
+ claim_released: boolean2().optional(),
37443
+ unresponsive: array(string2()).optional()
37444
+ });
37445
+ var EnvelopeResponseSchema = EnvelopeSchema.extend({
37446
+ recipient: string2().optional(),
37447
+ holder: string2().optional(),
37448
+ holder_state: SessionStateSchema.optional(),
37449
+ recipient_state: SessionStateSchema.optional(),
37450
+ last_seen: number2().int().optional(),
37451
+ unresponsive: array(string2()).optional(),
37452
+ duplicate: boolean2().optional()
37453
+ });
37454
+ var PublishResponseSchema = EnvelopeResponseSchema.extend({
37455
+ kept: boolean2().optional(),
37456
+ kept_reason: _enum2(["unclaimed", "holder_lapsed"]).optional()
37457
+ });
37458
+ var ReplayWireSchema = object({
37459
+ stream: object({
37460
+ id: string2().min(1).optional(),
37461
+ created: string2(),
37462
+ first_seq: number2().int().nonnegative(),
37463
+ last_seq: number2().int().nonnegative()
37464
+ }),
37465
+ subjects: array(object({
37466
+ subject: string2(),
37467
+ messages: array(object({
37468
+ seq: number2().int().positive(),
37469
+ subject: string2(),
37470
+ time: string2(),
37471
+ data: string2()
37472
+ })),
37473
+ next_after: number2().int().nonnegative(),
37474
+ more: boolean2(),
37475
+ refused: string2().optional()
37476
+ }))
37477
+ });
37478
+ var DELIVERY_NOT_ANSWERED = "not_answered";
37479
+ var DELIVERY_UNREADABLE = "unreadable_answer";
37480
+ function parseSubscribeAnswer(body) {
37481
+ const interest = InterestWireSchema.parse(body);
37482
+ const raw = typeof body === "object" && body !== null && "delivery" in body ? body.delivery : undefined;
37483
+ if (raw === undefined || raw === null) {
37484
+ return { ...interest, delivery: { mode: "core", reason: DELIVERY_NOT_ANSWERED } };
37485
+ }
37486
+ const delivery = DeliveryAnswerWireSchema.safeParse(raw);
37487
+ return {
37488
+ ...interest,
37489
+ delivery: delivery.success ? delivery.data : { mode: "core", reason: DELIVERY_UNREADABLE }
37490
+ };
37477
37491
  }
37478
- function readSecretFile(variable, filePath) {
37479
- let contents;
37480
- try {
37481
- contents = readFileSync2(filePath, "utf8");
37482
- } catch (error) {
37483
- throw new Error(`${variable} names ${filePath}, which could not be read: ${messageFor(error)}`);
37492
+ function mergeInterestSources(registryTopics, liveTopics) {
37493
+ const interests = new Map;
37494
+ for (const topic of registryTopics) {
37495
+ interests.set(topic, liveTopics.includes(topic) ? "both" : "registry");
37484
37496
  }
37485
- const value = contents.trim();
37486
- if (value.length === 0)
37487
- throw new Error(`${variable} names ${filePath}, which is empty`);
37488
- if (!sendable(value)) {
37489
- throw new Error(`${variable} names ${filePath}, which holds whitespace or a control character`);
37497
+ for (const topic of liveTopics) {
37498
+ if (!interests.has(topic))
37499
+ interests.set(topic, "live");
37490
37500
  }
37491
- return value;
37501
+ return interests;
37492
37502
  }
37493
-
37494
- // ../envoy-client/src/envoy-config.ts
37495
- var COMMAND_TIMEOUT_MS = 1e4;
37496
- function nonEmpty(value) {
37497
- return value === undefined || value === "" ? undefined : value;
37503
+ function holderStateOf(role) {
37504
+ return role.holder_state ?? role.state;
37498
37505
  }
37499
- function absolute(name, value) {
37500
- if (value !== undefined && !path.isAbsolute(value)) {
37501
- throw new Error(`${name} is ${value}, which is not an absolute path`);
37502
- }
37503
- return value;
37506
+ function namedProcess(id) {
37507
+ return id === "" ? undefined : id;
37504
37508
  }
37505
- function neutralConfig(env) {
37506
- const xdgConfigHome = nonEmpty(env.XDG_CONFIG_HOME);
37507
- if (xdgConfigHome !== undefined && !path.isAbsolute(xdgConfigHome)) {
37508
- return { error: `XDG_CONFIG_HOME is ${xdgConfigHome}, which is not an absolute path` };
37509
- }
37510
- const configHome = xdgConfigHome ?? path.join(nonEmpty(env.HOME) ?? homedir(), ".config");
37511
- return { path: path.resolve(configHome, "legion", "envoy.json") };
37509
+ function roleHeldBy(info, sessionID, clientInstanceID) {
37510
+ if (info === undefined || info.holder !== sessionID || info.handed_back === true)
37511
+ return false;
37512
+ const holding = namedProcess(info.client_instance_id);
37513
+ return holding === undefined || holding === namedProcess(clientInstanceID);
37512
37514
  }
37513
- var HARNESS_DIRECTORY_VARIABLE = {
37514
- omp: "PI_CODING_AGENT_DIR",
37515
- claude: "CLAUDE_CONFIG_DIR"
37516
- };
37517
- var PLUGIN_HARNESSES = Object.keys(HARNESS_DIRECTORY_VARIABLE);
37518
- function namedDirectory(harness, env) {
37519
- if (harness === "omp") {
37520
- const profile = nonEmpty(env.OMP_PROFILE);
37521
- if (profile === undefined || profile === "default")
37522
- return;
37523
- }
37524
- return nonEmpty(env[HARNESS_DIRECTORY_VARIABLE[harness]]);
37515
+ function mayActOnRoleRecord(writer, clientInstanceID, holdsRole) {
37516
+ const wrote = namedProcess(writer);
37517
+ if (wrote === undefined || wrote === namedProcess(clientInstanceID))
37518
+ return true;
37519
+ return !holdsRole;
37525
37520
  }
37526
- function harnessDirectory(harness, env) {
37527
- for (const plugin of harness === "command" ? PLUGIN_HARNESSES : [harness]) {
37528
- if (harness === "command" && plugin === "claude" && env.CLAUDE_CODE_CHILD_SESSION !== "1") {
37521
+ function newClientInstanceID() {
37522
+ return Array.from(crypto.getRandomValues(new Uint8Array(16)), (byte) => byte.toString(16).padStart(2, "0")).join("");
37523
+ }
37524
+ function expandSubscriptionTopics(topics) {
37525
+ const expanded = new Set;
37526
+ for (const topic of topics) {
37527
+ const base = topic.endsWith(".>") ? topic.slice(0, -2) : undefined;
37528
+ if (base === undefined || base.includes("*") || base.includes(">")) {
37529
+ expanded.add(topic);
37529
37530
  continue;
37530
37531
  }
37531
- const directory = namedDirectory(plugin, env);
37532
- if (directory !== undefined)
37533
- return absolute(HARNESS_DIRECTORY_VARIABLE[plugin], directory);
37532
+ const segments = base.split(".");
37533
+ if (segments.length < 2 || segments.some((segment) => segment.length === 0)) {
37534
+ throw new TypeError(`cannot expand Envoy wildcard topic "${topic}": its concrete base must have at least two non-empty segments`);
37535
+ }
37536
+ expanded.add(base);
37537
+ expanded.add(topic);
37534
37538
  }
37535
- return;
37536
- }
37537
- function handedConfigPath(env) {
37538
- const handed = nonEmpty(env.ENVOY_CONFIG_FILE);
37539
- return handed === undefined ? undefined : path.resolve(absolute("ENVOY_CONFIG_FILE", handed));
37540
- }
37541
- function envoyConfigPath(harness, env) {
37542
- const handedFirst = harness === "command" ? handedConfigPath(env) : undefined;
37543
- if (handedFirst !== undefined)
37544
- return handedFirst;
37545
- const directory = harnessDirectory(harness, env);
37546
- if (directory !== undefined)
37547
- return path.resolve(directory, "envoy.json");
37548
- const handed = handedConfigPath(env);
37549
- if (handed !== undefined)
37550
- return handed;
37551
- const neutral = neutralConfig(env);
37552
- if ("error" in neutral)
37553
- throw new Error(neutral.error);
37554
- return neutral.path;
37555
- }
37556
- var httpUrlSchema = url({ protocol: /^https?$/, error: "must be an http or https URL" });
37557
- var DispatchSectionSchema = strictObject({
37558
- enabled: boolean2().optional(),
37559
- serverUrl: httpUrlSchema.optional(),
37560
- token: string2().optional()
37561
- });
37562
- var EnvoySectionSchema = strictObject({
37563
- enabled: boolean2().optional(),
37564
- url: httpUrlSchema.optional(),
37565
- natsUrls: array(string2().trim().min(1, "must not be empty")).optional(),
37566
- token: string2().optional(),
37567
- natsSeed: string2().optional()
37568
- });
37569
- var EnvoyFileSchema = looseObject({
37570
- $schema: string2().optional(),
37571
- dispatch: DispatchSectionSchema.optional(),
37572
- envoy: EnvoySectionSchema.optional()
37573
- });
37574
- var CheckoutFileSchema = looseObject({
37575
- $schema: string2().optional(),
37576
- dispatch: DispatchSectionSchema.optional()
37577
- });
37578
- var AskgateSectionSchema = strictObject({ enabled: boolean2().optional() });
37579
- function offWithNotice(notice) {
37580
- return { enabled: false, notice, error: null };
37581
- }
37582
- function offWithError(error) {
37583
- return { enabled: false, notice: null, error };
37539
+ return [...expanded];
37584
37540
  }
37585
- function httpUrl2(value) {
37586
- let url2;
37587
- try {
37588
- url2 = new URL(value);
37589
- } catch {
37590
- return;
37541
+ function refusalStateText(parsed) {
37542
+ const sentences = [];
37543
+ const state = parsed.holder_state ?? parsed.state;
37544
+ if (state !== undefined) {
37545
+ const who = parsed.holder === undefined ? "That session" : `Holder ${parsed.holder}`;
37546
+ const seen = parsed.last_seen === undefined || parsed.last_seen <= 0 ? "" : `, last seen ${new Date(parsed.last_seen).toISOString()}`;
37547
+ sentences.push(`${who} is ${state}${seen}.`);
37591
37548
  }
37592
- return url2.protocol === "http:" || url2.protocol === "https:" ? url2 : undefined;
37593
- }
37594
- function describeSchemaIssue(filePath, error) {
37595
- const issue = error.issues[0];
37596
- if (!issue)
37597
- return `${filePath}: invalid envoy.json`;
37598
- if (issue.code === "unrecognized_keys") {
37599
- const section = issue.path.join(".");
37600
- const keys = issue.keys.map((key) => `${section}.${key}`).join(", ");
37601
- return `${filePath}: unrecognized ${section} key(s): ${keys}`;
37549
+ const stored = parsed.unresponsive ?? [];
37550
+ if (stored.length > 0) {
37551
+ const one = stored.length === 1;
37552
+ 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.`);
37602
37553
  }
37603
- return `${filePath}: ${issue.path.join(".")}: ${issue.message}`;
37554
+ if (sentences.length === 0)
37555
+ return "";
37556
+ if (state === "unresponsive" || stored.length > 0)
37557
+ sentences.push(UNRESPONSIVE_RULE);
37558
+ return ` ${sentences.join(" ")}`;
37604
37559
  }
37605
- function danglingLink(filePath) {
37606
- try {
37607
- return lstatSync(filePath).isSymbolicLink() ? readlinkSync2(filePath) : null;
37608
- } catch {
37609
- return null;
37560
+
37561
+ class EnvoyApiError extends Error {
37562
+ details;
37563
+ name = "EnvoyApiError";
37564
+ expected;
37565
+ code;
37566
+ holder;
37567
+ sessionState;
37568
+ lastSeen;
37569
+ claimReleased;
37570
+ unresponsive;
37571
+ constructor(details) {
37572
+ let parsed;
37573
+ try {
37574
+ const result = ErrorWireSchema.safeParse(JSON.parse(details.responseBody));
37575
+ if (result.success)
37576
+ parsed = result.data;
37577
+ } catch {}
37578
+ super(parsed === undefined ? `${details.method} ${details.url} failed with ${details.status}: ${details.responseBody}` : `${parsed.error}${parsed.expected === undefined ? "" : ` (expected: ${parsed.expected.join(", ")})`}${refusalStateText(parsed)}`);
37579
+ this.details = details;
37580
+ this.expected = parsed?.expected;
37581
+ this.code = parsed?.code;
37582
+ this.holder = parsed?.holder;
37583
+ this.sessionState = parsed?.holder_state ?? parsed?.state;
37584
+ this.lastSeen = parsed?.last_seen;
37585
+ this.claimReleased = parsed?.claim_released;
37586
+ this.unresponsive = parsed?.unresponsive;
37610
37587
  }
37611
37588
  }
37612
- function parseFile(filePath, schema, checkout) {
37613
- let raw;
37614
- try {
37615
- raw = readFileSync3(filePath, "utf-8");
37616
- } catch (error) {
37617
- if (hasErrnoCode(error, "ENOENT") || hasErrnoCode(error, "ENOTDIR")) {
37618
- return { kind: "absent", link: danglingLink(filePath) };
37619
- }
37620
- return { kind: "invalid", reason: `${filePath}: could not be read (${messageFor(error)})` };
37621
- }
37622
- let parsedJson;
37623
- try {
37624
- parsedJson = JSON.parse(raw);
37625
- } catch {
37626
- const position = jsonErrorPosition(raw);
37627
- return {
37628
- kind: "invalid",
37629
- reason: `${filePath}: invalid JSON${position === undefined ? "" : ` at ${position}`}`
37630
- };
37631
- }
37632
- if (typeof parsedJson === "object" && parsedJson !== null && "natsUrls" in parsedJson) {
37633
- return {
37634
- kind: "invalid",
37635
- reason: checkout ? `${filePath}: the top-level natsUrls is not read, and a checkout's envoy.json holds only a dispatch section` : `${filePath}: the top-level natsUrls is not read; move it to envoy.natsUrls`
37636
- };
37637
- }
37638
- const parsed = schema.safeParse(parsedJson);
37639
- if (!parsed.success) {
37640
- return { kind: "invalid", reason: describeSchemaIssue(filePath, parsed.error) };
37589
+ var mintedDedupeKey2 = new RegExp(MINTED_DEDUPE_KEY_PATTERN);
37590
+
37591
+ class EnvoySendUnconfirmedError extends Error {
37592
+ targetSessionID;
37593
+ idempotencyKey;
37594
+ attempts;
37595
+ last;
37596
+ name = "EnvoySendUnconfirmedError";
37597
+ resendSafe;
37598
+ constructor(targetSessionID, idempotencyKey, attempts, last) {
37599
+ const got = last instanceof EnvoyApiError ? `the listener answered ${last.details.status} (${last.message})` : `the listener did not answer (${last instanceof Error ? last.message : String(last)})`;
37600
+ const tried = attempts === 1 ? "on its only attempt" : `on the last of ${attempts} attempts`;
37601
+ const resendSafe = mintedDedupeKey2.test(`agent.${targetSessionID.trim()}.${idempotencyKey}`);
37602
+ const resend = resendSafe ? `Sending it again with idempotency_key ${idempotencyKey} within ` + `${DELIVERY_DUPLICATE_WINDOW_MS / 3600000} hours is the safe resend: Envoy drops it as ` + "a repeat if the first copy arrived (a recipient that restarted in between can rarely " + "see it twice)." : `Envoy cannot recognise a resend under idempotency_key ${idempotencyKey} as a repeat ` + "(the key is not a lower-case UUID, or the session id holds a dot): ask the recipient " + "whether it arrived before sending it again.";
37603
+ super(`The send to ${targetSessionID} is not confirmed delivered: ${got} ${tried}. It may or may ` + `not have arrived. ${resend}`, { cause: last });
37604
+ this.targetSessionID = targetSessionID;
37605
+ this.idempotencyKey = idempotencyKey;
37606
+ this.attempts = attempts;
37607
+ this.last = last;
37608
+ this.resendSafe = resendSafe;
37641
37609
  }
37642
- return { kind: "valid", settings: parsed.data };
37643
37610
  }
37644
- var FAILURE_KEPT_MS = 60000;
37645
- var COMMAND_OUTCOMES = Symbol.for("legion.envoy-config.commands.v1");
37646
- var processGlobal = globalThis;
37647
- var commandOutcomes = processGlobal[COMMAND_OUTCOMES] ?? new Map;
37648
- processGlobal[COMMAND_OUTCOMES] = commandOutcomes;
37649
- function runCommand(command, env, cwd) {
37650
- const childEnv = {};
37651
- for (const [name, variable] of Object.entries(env)) {
37652
- if (variable !== undefined)
37653
- childEnv[name] = variable;
37654
- }
37655
- const directory = mkdtempSync(path.join(tmpdir(), "envoy-command-"));
37656
- try {
37657
- chmodSync(directory, 448);
37658
- const output = path.join(directory, "stdout");
37659
- const fd = openSync2(output, "wx", 384);
37660
- let result;
37611
+ function resolveCallTiming(config) {
37612
+ return {
37613
+ timeoutMs: config.timeoutMs ?? LISTENER_CALL_TIMEOUT_MS,
37614
+ attempts: config.retry === false ? 1 : LISTENER_CALL_ATTEMPTS,
37615
+ retryDelayMs: LISTENER_CALL_RETRY_DELAY_MS
37616
+ };
37617
+ }
37618
+ function createEnvoyClient(config) {
37619
+ const baseUrl = withoutTrailingSlashes(config.baseUrl);
37620
+ const { timeoutMs, attempts, retryDelayMs } = resolveCallTiming(config);
37621
+ const credential = config.credential;
37622
+ const attemptOnce = async (url, init, deadlineMs, actingSession) => {
37623
+ let response;
37624
+ let unminted;
37661
37625
  try {
37662
- result = Bun.spawnSync(["/bin/sh", "-c", command], {
37663
- cwd,
37664
- env: childEnv,
37665
- stdin: "ignore",
37666
- stdout: fd,
37667
- stderr: "ignore",
37668
- timeout: COMMAND_TIMEOUT_MS,
37669
- detached: true
37626
+ const headers = new Headers(init.headers);
37627
+ let presented;
37628
+ try {
37629
+ const ready = credential?.headers(init.method ?? "GET", url, actingSession);
37630
+ presented = ready instanceof Promise ? await ready : ready;
37631
+ } catch (error) {
37632
+ unminted = error;
37633
+ }
37634
+ for (const [name, value] of Object.entries(presented ?? {}))
37635
+ headers.set(name, value);
37636
+ response = await config.fetch(url, {
37637
+ ...init,
37638
+ headers,
37639
+ signal: AbortSignal.timeout(deadlineMs)
37670
37640
  });
37671
37641
  } catch (error) {
37672
- return { failure: `could not start: ${messageFor(error)}`, timedOut: false };
37673
- } finally {
37674
- closeSync2(fd);
37642
+ return { unanswered: error };
37675
37643
  }
37676
- try {
37677
- process.kill(-result.pid, "SIGKILL");
37678
- } catch {}
37679
- if (result.exitedDueToTimeout) {
37644
+ if (unminted !== undefined && (response.status === 401 || response.status === 403)) {
37680
37645
  return {
37681
- failure: `did not finish within ${COMMAND_TIMEOUT_MS / 1000} s, and does not run again in this process`,
37682
- timedOut: true
37646
+ unanswered: new Error(`${init.method ?? "GET"} ${url} answered ${response.status}, and this machine's Envoy credential could not be minted: ${messageFor(unminted)}`, { cause: unminted })
37683
37647
  };
37684
37648
  }
37685
- if (!result.success) {
37649
+ try {
37650
+ return { response, body: await response.text() };
37651
+ } catch (error) {
37652
+ if (response.status >= 400 && response.status < 500) {
37653
+ return { response, body: `(its body did not arrive: ${String(error)})` };
37654
+ }
37655
+ return { unanswered: error };
37656
+ }
37657
+ };
37658
+ const request = async (path, init, options = {}) => {
37659
+ const url = `${baseUrl}${path}`;
37660
+ const tries = options.retry === false ? 1 : attempts;
37661
+ for (let attempt = 0;attempt < tries; attempt += 1) {
37662
+ const outcome = await attemptOnce(url, init, options.timeoutMs ?? timeoutMs, options.actingSession);
37663
+ if ("unanswered" in outcome) {
37664
+ options.onUnanswered?.();
37665
+ if (attempt + 1 < tries) {
37666
+ await waitForRetry(retryDelayMs);
37667
+ continue;
37668
+ }
37669
+ throw outcome.unanswered;
37670
+ }
37671
+ const { response, body } = outcome;
37672
+ if (response.ok)
37673
+ return body;
37674
+ const error = new EnvoyApiError({
37675
+ method: init.method ?? "GET",
37676
+ url,
37677
+ status: response.status,
37678
+ responseBody: body
37679
+ });
37680
+ if (response.status >= 500) {
37681
+ options.onUnanswered?.();
37682
+ if (attempt + 1 < tries) {
37683
+ await waitForRetry(retryDelayMs);
37684
+ continue;
37685
+ }
37686
+ }
37687
+ throw error;
37688
+ }
37689
+ throw new Error("Envoy request exhausted retries");
37690
+ };
37691
+ const post = (path, body, options) => request(path, {
37692
+ method: "POST",
37693
+ headers: { "Content-Type": "application/json" },
37694
+ body: JSON.stringify(body)
37695
+ }, options);
37696
+ return {
37697
+ subscribe: async (input) => parseSubscribeAnswer(JSON.parse(await post("/v1/interests/subscribe", {
37698
+ session_id: input.sessionID,
37699
+ dir: input.directory,
37700
+ topics: expandSubscriptionTopics(input.topics),
37701
+ port: input.port,
37702
+ title: input.title,
37703
+ ...input.capabilities === undefined ? {} : { capabilities: input.capabilities },
37704
+ driving: input.driving,
37705
+ ...input.selfSubscribed === undefined ? {} : { self_subscribed: input.selfSubscribed },
37706
+ ...input.delivery === undefined ? {} : { delivery: input.delivery },
37707
+ ...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID },
37708
+ host: machineID(),
37709
+ ...input.place === undefined ? {} : { tmux_place: input.place },
37710
+ plugin: input.plugin.name,
37711
+ plugin_version: input.plugin.version
37712
+ }, { actingSession: input.sessionID }))),
37713
+ unsubscribe: async (input) => {
37714
+ await post("/v1/interests/unsubscribe", {
37715
+ session_id: input.sessionID,
37716
+ topics: expandSubscriptionTopics(input.topics),
37717
+ ...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID }
37718
+ }, { actingSession: input.sessionID });
37719
+ },
37720
+ getInterest: async (sessionID) => InterestWireSchema.parse(JSON.parse(await request(`/v1/interests/${sessionID}`, {}))),
37721
+ send: async (input) => {
37722
+ const idempotencyKey = input.idempotencyKey ?? crypto.randomUUID();
37723
+ let unanswered = false;
37724
+ let response;
37725
+ try {
37726
+ const answer = await post("/v1/messages/send", {
37727
+ source: input.source ?? "agent",
37728
+ ...sourceSession(input.sourceSessionID),
37729
+ target_session: input.targetSessionID,
37730
+ message: input.message,
37731
+ idempotency_key: idempotencyKey,
37732
+ ...messageMetadata(input)
37733
+ }, {
37734
+ onUnanswered: () => {
37735
+ unanswered = true;
37736
+ },
37737
+ ...input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID }
37738
+ });
37739
+ response = EnvelopeResponseSchema.parse(JSON.parse(answer));
37740
+ } catch (error) {
37741
+ if (error instanceof EnvoyApiError && error.details.status < 500 && !unanswered) {
37742
+ throw error;
37743
+ }
37744
+ throw new EnvoySendUnconfirmedError(input.targetSessionID, idempotencyKey, attempts, error);
37745
+ }
37686
37746
  return {
37687
- failure: `exited with ${result.signalCode ?? `status ${result.exitCode}`}`,
37688
- timedOut: false
37747
+ envelope: response,
37748
+ recipient: response.recipient ?? input.targetSessionID,
37749
+ confirmed: response.recipient !== undefined,
37750
+ recipientState: response.recipient_state ?? "live",
37751
+ ...response.last_seen === undefined || response.last_seen <= 0 ? {} : { lastSeen: response.last_seen },
37752
+ duplicate: response.duplicate === true
37689
37753
  };
37690
- }
37691
- const value = readFileSync3(output, "utf-8").trim();
37692
- if (value.length === 0)
37693
- return { failure: "printed nothing", timedOut: false };
37694
- if (!sendable(value)) {
37754
+ },
37755
+ publish: async (input) => {
37756
+ const idempotencyKey = input.idempotencyKey ?? crypto.randomUUID();
37757
+ const response = PublishResponseSchema.parse(JSON.parse(await post("/v1/messages/publish", {
37758
+ source: input.source ?? "agent",
37759
+ ...sourceSession(input.sourceSessionID),
37760
+ topic: input.topic,
37761
+ message: input.message,
37762
+ ...input.payload === undefined ? {} : { payload: input.payload },
37763
+ idempotency_key: idempotencyKey,
37764
+ ...messageMetadata(input),
37765
+ ...input.keep === undefined ? {} : { keep: input.keep }
37766
+ }, input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID })));
37695
37767
  return {
37696
- failure: "printed more than one line, or whitespace inside its value",
37697
- timedOut: false
37768
+ envelope: response,
37769
+ ...response.holder === undefined ? {} : { holder: response.holder },
37770
+ ...response.holder_state === undefined ? {} : { holderState: response.holder_state },
37771
+ unresponsive: response.unresponsive ?? [],
37772
+ ...response.kept === undefined ? {} : { kept: response.kept },
37773
+ ...response.kept_reason === undefined ? {} : { keptReason: response.kept_reason }
37698
37774
  };
37699
- }
37700
- return { value };
37701
- } finally {
37702
- rmSync2(directory, { recursive: true, force: true });
37703
- }
37775
+ },
37776
+ unregisterSession: async (input) => {
37777
+ const query = input.clientInstanceID === undefined ? "" : `?client_instance_id=${encodeURIComponent(input.clientInstanceID)}`;
37778
+ await request(`/v1/sessions/${encodeURIComponent(input.sessionID)}${query}`, { method: "DELETE" }, { actingSession: input.sessionID });
37779
+ },
37780
+ setRole: async (input) => {
37781
+ const soft = input.soft === true;
37782
+ const previous = soft ? input.previousSessionID ?? "" : "";
37783
+ const body = {
37784
+ session_id: input.sessionID,
37785
+ role: input.role,
37786
+ ...soft ? { soft: true } : {},
37787
+ ...previous === "" ? {} : { previous_session_id: previous },
37788
+ ...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID }
37789
+ };
37790
+ let raw;
37791
+ try {
37792
+ raw = await post("/v1/roles/set", body, { actingSession: input.sessionID });
37793
+ } catch (error) {
37794
+ if (error instanceof EnvoyApiError && error.details.status === 409) {
37795
+ let answer;
37796
+ try {
37797
+ answer = JSON.parse(error.details.responseBody);
37798
+ } catch {
37799
+ throw error;
37800
+ }
37801
+ const held = RoleHeldWireSchema.safeParse(answer);
37802
+ if (held.success)
37803
+ return { claimed: false, holder: held.data.holder };
37804
+ }
37805
+ throw error;
37806
+ }
37807
+ return { claimed: true, interest: InterestWireSchema.parse(JSON.parse(raw)) };
37808
+ },
37809
+ getRole: async (role) => RoleWireSchema.parse(JSON.parse(await request(`/v1/roles/${encodeURIComponent(role)}`, { method: "GET" }))),
37810
+ listSessions: async (input = {}) => {
37811
+ const search = new URLSearchParams;
37812
+ if (input.directory !== undefined)
37813
+ search.set("dir", input.directory);
37814
+ if (input.title !== undefined)
37815
+ search.set("title", input.title);
37816
+ if (input.include !== undefined)
37817
+ search.set("include", input.include);
37818
+ const query = search.size === 0 ? "" : `?${search.toString()}`;
37819
+ return SessionWireSchema.array().parse(JSON.parse(await request(`/v1/sessions${query}`, {})));
37820
+ },
37821
+ replay: async (input) => ReplayWireSchema.parse(JSON.parse(await post("/v1/replay", {
37822
+ session_id: input.sessionID,
37823
+ subjects: input.subjects.map((cursor) => cursor.after === undefined ? { subject: cursor.subject } : { subject: cursor.subject, after: cursor.after }),
37824
+ ...input.maxMessages === undefined ? {} : { max_messages: input.maxMessages }
37825
+ }, {
37826
+ timeoutMs: input.timeoutMs ?? REPLAY_TIMEOUT_MS,
37827
+ retry: false,
37828
+ actingSession: input.sessionID
37829
+ })))
37830
+ };
37704
37831
  }
37705
- function fileValue(value, field, filePath, env, commands = "run") {
37706
- if (!value.startsWith("!")) {
37707
- if (!sendable(value)) {
37708
- throw new Error(`${filePath}: ${field} holds whitespace or a control character`);
37709
- }
37710
- return value;
37711
- }
37712
- const command = value.slice(1);
37713
- const cwd = path.dirname(path.resolve(filePath));
37714
- const key = JSON.stringify([command, filePath, cwd]);
37715
- let outcome = commandOutcomes.get(key);
37716
- if (commands === "kept") {
37717
- if (outcome === undefined) {
37718
- throw new Error(`${filePath}: ${field}'s command has not run in this process yet, and this read does not run it`);
37719
- }
37720
- } else if (outcome === undefined || "failure" in outcome && !outcome.timedOut && Date.now() - outcome.at >= FAILURE_KEPT_MS) {
37721
- outcome = { ...runCommand(command, env, cwd), at: Date.now() };
37722
- commandOutcomes.set(key, outcome);
37723
- }
37724
- if ("failure" in outcome)
37725
- throw new Error(`${filePath}: ${field}'s command ${outcome.failure}`);
37726
- return outcome.value;
37832
+ function waitForRetry(delayMs) {
37833
+ const { promise, resolve } = Promise.withResolvers();
37834
+ setTimeout(resolve, delayMs);
37835
+ return promise;
37727
37836
  }
37728
- function statDevice(directory) {
37729
- try {
37730
- return statSync2(directory).dev;
37731
- } catch {
37732
- return;
37733
- }
37734
- }
37735
- function moveNotice(oldFile, newFile, move, deviceOf) {
37736
- const oldDevice = deviceOf(path.dirname(oldFile));
37737
- let ancestor = path.dirname(newFile);
37738
- let newDevice = deviceOf(ancestor);
37739
- while (newDevice === undefined && path.dirname(ancestor) !== ancestor) {
37740
- ancestor = path.dirname(ancestor);
37741
- newDevice = deviceOf(ancestor);
37742
- }
37743
- if (oldDevice !== undefined && newDevice !== undefined && oldDevice !== newDevice) {
37744
- return `${oldFile} is not read, and is on another filesystem than ${newFile} (a mount): create ${newFile}, and move ${oldFile} on the machine that owns it`;
37745
- }
37746
- return `${oldFile} is not read: ${move}`;
37747
- }
37748
- function oldUserFileNotice(filePath, env, deviceOf) {
37749
- const home = nonEmpty(env.HOME) ?? homedir();
37750
- const old = path.join(home, ".config", "opencode", "envoy.json");
37751
- if (!existsSync(old))
37752
- return;
37753
- const selected = path.resolve(filePath);
37754
- const userFile = path.resolve(home, ".config", "legion", "envoy.json");
37755
- if (selected === userFile) {
37756
- return moveNotice(old, userFile, "mkdir -p ~/.config/legion && mv ~/.config/opencode/envoy.json ~/.config/legion/", deviceOf);
37757
- }
37758
- const profileFile = PLUGIN_HARNESSES.map((harness) => namedDirectory(harness, env)).some((directory) => directory !== undefined && path.isAbsolute(directory) && selected === path.resolve(directory, "envoy.json"));
37759
- if (profileFile) {
37760
- return `${old} is not read for this profile; copy or link it to ${filePath}, or write that file`;
37761
- }
37762
- const configHome = nonEmpty(env.XDG_CONFIG_HOME);
37763
- const neutral = neutralConfig(env);
37764
- if (configHome !== undefined && path.isAbsolute(configHome) && "path" in neutral && selected === neutral.path) {
37765
- return `${old} is not read under XDG_CONFIG_HOME=${configHome}`;
37766
- }
37767
- return;
37768
- }
37769
- function missingConfigNotice(filePath, missing, off) {
37770
- const link = missing.link === null ? "" : ` (a link to ${missing.link}, which does not exist)`;
37771
- const old = missing.oldUserFileNotice === undefined ? "" : `; ${missing.oldUserFileNotice}`;
37772
- return `no envoy.json at ${filePath}${link}: ${off}${old}`;
37773
- }
37774
- function readEnvoyConfig(filePath, env, options) {
37775
- const deviceOf = options.deviceOf ?? statDevice;
37776
- const chosen = parseFile(filePath, EnvoyFileSchema, false);
37777
- if (chosen.kind === "invalid")
37778
- return { kind: "failed", path: filePath, error: chosen.reason };
37779
- const resolvedChosen = path.resolve(filePath);
37780
- const checkoutPath = path.resolve(options.cwd, ".envoy", "envoy.json");
37781
- const checkoutFile = checkoutPath === resolvedChosen ? { kind: "absent", link: null } : parseFile(checkoutPath, CheckoutFileSchema, true);
37782
- const own = chosen.kind === "valid" ? chosen.settings : {};
37783
- let checkout = null;
37784
- if (checkoutFile.kind === "invalid") {
37785
- checkout = { kind: "invalid", path: checkoutPath, error: checkoutFile.reason };
37786
- } else if (checkoutFile.kind === "valid") {
37787
- checkout = {
37788
- kind: "valid",
37789
- path: checkoutPath,
37790
- dispatch: checkoutFile.settings.dispatch ?? {},
37791
- envoyError: Object.hasOwn(checkoutFile.settings, "envoy") ? `${checkoutPath}: envoy is not read from a checkout's envoy.json; put it in ${filePath}` : null,
37792
- askgateError: Object.hasOwn(checkoutFile.settings, "askgate") ? `askgate in ${checkoutPath} is not read; set it in ${filePath}` : null
37793
- };
37794
- }
37795
- const oldCheckout = path.resolve(options.cwd, ".opencode", "envoy.json");
37796
- const warnings = oldCheckout !== resolvedChosen && !existsSync(checkoutPath) && existsSync(oldCheckout) ? [
37797
- moveNotice(oldCheckout, checkoutPath, `mkdir -p ${path.dirname(checkoutPath)} && mv ${oldCheckout} ${checkoutPath}`, deviceOf)
37798
- ] : [];
37799
- const unreadOldUserFile = chosen.kind === "absent" ? oldUserFileNotice(filePath, env, deviceOf) : undefined;
37837
+ function messageMetadata(input) {
37800
37838
  return {
37801
- kind: "read",
37802
- path: filePath,
37803
- dispatch: own.dispatch ?? {},
37804
- envoy: own.envoy ?? {},
37805
- checkout,
37806
- missing: chosen.kind === "absent" ? {
37807
- link: chosen.link,
37808
- ...unreadOldUserFile === undefined ? {} : { oldUserFileNotice: unreadOldUserFile }
37809
- } : null,
37810
- warnings
37839
+ ...input.inReplyTo === undefined ? {} : { in_reply_to: input.inReplyTo },
37840
+ ...input.supersedes === undefined ? {} : { supersedes: input.supersedes },
37841
+ ...input.urgency === undefined ? {} : { urgency: input.urgency },
37842
+ ...input.expectsReply === undefined ? {} : { expects_reply: input.expectsReply },
37843
+ ...input.expiresAt === undefined ? {} : { expires_at: input.expiresAt }
37811
37844
  };
37812
37845
  }
37813
- function askgateSetting(filePath) {
37814
- const stayOn = (reason) => ({
37815
- enabled: true,
37816
- error: `${reason}; AskGate stays on`
37817
- });
37818
- let raw;
37819
- try {
37820
- raw = readFileSync3(filePath, "utf-8");
37821
- } catch (error) {
37822
- if (hasErrnoCode(error, "ENOENT") || hasErrnoCode(error, "ENOTDIR")) {
37823
- return { enabled: true, error: null };
37824
- }
37825
- return stayOn(`${filePath}: could not be read (${messageFor(error)}); askgate.enabled is not read`);
37826
- }
37827
- let parsedJson;
37828
- try {
37829
- parsedJson = JSON.parse(raw);
37830
- } catch {
37831
- const position = jsonErrorPosition(raw);
37832
- const at = position === undefined ? "" : ` at ${position}`;
37833
- return stayOn(`${filePath}: invalid JSON${at}; askgate.enabled is not read`);
37834
- }
37835
- if (typeof parsedJson !== "object" || parsedJson === null || !("askgate" in parsedJson)) {
37836
- return { enabled: true, error: null };
37837
- }
37838
- const parsed = object({ askgate: AskgateSectionSchema }).safeParse(parsedJson);
37839
- if (!parsed.success) {
37840
- const unknownKey = parsed.error.issues[0]?.code === "unrecognized_keys";
37841
- return stayOn(unknownKey ? `${describeSchemaIssue(filePath, parsed.error)}; the one askgate key is askgate.enabled, true or false` : `${filePath}: askgate.enabled must be true or false`);
37842
- }
37843
- return { enabled: parsed.data.askgate.enabled ?? true, error: null };
37846
+ function sourceSession(sessionID) {
37847
+ return sessionID === undefined || sessionID === "" ? {} : { source_session: sessionID };
37844
37848
  }
37845
37849
 
37846
- // ../envoy-client/src/bearer.ts
37847
- function bearerCredential(token) {
37848
- return {
37849
- scheme: "Bearer",
37850
- headers: () => ({ Authorization: `Bearer ${token}` }),
37851
- secret: () => token
37850
+ // ../envoy-client/src/replay.ts
37851
+ var REPLAY_LEDGER_TTL_MS = DELIVERY_DUPLICATE_WINDOW_MS;
37852
+ var REPLAY_LEDGER_LIMIT = 20000;
37853
+ var REPLAY_PAGES_PER_SWEEP = 20;
37854
+ var REPLAY_FAILURES_BEFORE_NOTICE = 3;
37855
+ var REPLAY_STATE_REFRESH_MS = 60 * 60 * 1000;
37856
+ var REPLAY_MAX_SUBJECTS = 32;
37857
+ var START_CURSORS_TIMEOUT_MS = 2000;
37858
+ var ROLE_FORWARD_KEY_PREFIX = "envoy.role.forward.";
37859
+ var ReplayLedgerEntrySchema = object({
37860
+ event_id: string2().min(1).optional(),
37861
+ dedupe_key: string2().min(1).optional(),
37862
+ at: number2()
37863
+ });
37864
+ var ReplayCursorSchema = object({
37865
+ since: number2().int().nonnegative().optional(),
37866
+ after: number2().int().nonnegative().optional(),
37867
+ from_start: number2().int().nonnegative().optional()
37868
+ });
37869
+ var ReplayPendingNoticeSchema = _enum2([
37870
+ "retention_gap",
37871
+ "stream_moved_back",
37872
+ "stream_recreated"
37873
+ ]);
37874
+ var PENDING_NOTICE_MESSAGES = {
37875
+ stream_recreated: "Envoy's notification stream was recreated, so messages published to this session while its connection was down before that may be lost.",
37876
+ 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.",
37877
+ 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."
37878
+ };
37879
+ var ReplayStateSchema = object({
37880
+ stream_id: string2().optional(),
37881
+ stream_created: string2().optional(),
37882
+ subjects: record(string2(), ReplayCursorSchema),
37883
+ notices: array(string2()).optional()
37884
+ });
37885
+ var ReplaySnapshotSchema = object({
37886
+ state: ReplayStateSchema.optional(),
37887
+ ledger: array(ReplayLedgerEntrySchema)
37888
+ });
37889
+ function createReplayEngine(options) {
37890
+ const { host, transport, dedupe } = options;
37891
+ const now = options.now ?? Date.now;
37892
+ const ledger = createLedger(now);
37893
+ const cursors = new Map;
37894
+ const cursorStarts = new Map;
37895
+ let streamID;
37896
+ let streamCreated;
37897
+ const pending = new Set;
37898
+ const failures = new Map;
37899
+ let unwritten = [];
37900
+ let writtenState;
37901
+ let writtenShape;
37902
+ let writtenAt;
37903
+ let writes = Promise.resolve();
37904
+ let epoch = 0;
37905
+ let listenerNoticed;
37906
+ const answered = new Set;
37907
+ let running;
37908
+ let rerun;
37909
+ let consumerMode = false;
37910
+ const safely = (what, call) => {
37911
+ try {
37912
+ call();
37913
+ } catch (error) {
37914
+ try {
37915
+ host.log(`replay: ${what} failed`, error);
37916
+ } catch {}
37917
+ }
37852
37918
  };
37853
- }
37854
- function bearerSource(env, section, written) {
37855
- const pointer = nonEmpty(env[`${section}_TOKEN_FILE`]);
37856
- if (pointer !== undefined) {
37857
- return { kind: "variable", token: readSecretFile(`${section}_TOKEN_FILE`, pointer) };
37858
- }
37859
- const plain = nonEmpty(env[`${section}_TOKEN`]);
37860
- if (plain !== undefined) {
37861
- if (!sendable(plain)) {
37862
- throw new Error(`${section}_TOKEN holds whitespace or a control character`);
37919
+ const log = (message, error) => safely("log", () => host.log(message, error));
37920
+ const notify = (notice) => safely("notice", () => host.notify(notice));
37921
+ const tell = (kind) => {
37922
+ pending.delete(kind);
37923
+ notify({ kind, message: PENDING_NOTICE_MESSAGES[kind] });
37924
+ };
37925
+ const sessionRead = (read) => () => {
37926
+ try {
37927
+ return read();
37928
+ } catch {
37929
+ return;
37863
37930
  }
37864
- return { kind: "variable", token: plain };
37865
- }
37866
- const bearer = written();
37867
- return bearer === undefined ? { kind: "none" } : { kind: "file", ...bearer };
37868
- }
37869
- function fileBearer(bearer, url, section, env, commands = "run") {
37870
- const named = httpUrl2(url)?.origin;
37871
- const paired = httpUrl2(bearer.server)?.origin;
37872
- if (named !== paired) {
37873
- throw new Error(`${section}_URL names ${named}, but ${bearer.path}'s ${bearer.field} is for ${paired}; set ${section}_TOKEN or ${section}_TOKEN_FILE for it`);
37874
- }
37875
- if (bearer.checkout && bearer.token.startsWith("!")) {
37876
- throw new Error(`${bearer.path}: ${bearer.field} is a command, which a checkout's envoy.json cannot run`);
37877
- }
37878
- if (bearer.token === "") {
37879
- throw new Error(`${bearer.field} in ${bearer.path} must be a non-empty bearer token`);
37880
- }
37881
- return fileValue(bearer.token, bearer.field, bearer.path, env, commands);
37882
- }
37883
-
37884
- // ../envoy-client/src/envoy-settings.ts
37885
- var DEFAULT_ENVOY_URL = "http://127.0.0.1:9020";
37886
- var DEFAULT_HEARTBEAT_MS = SESSION_HEARTBEAT_MS;
37887
- var MIN_PASS_DEADLINE_MS = 30000;
37888
- var DEFAULT_TMUX_PLACE_CACHE_MS = 30000;
37889
- var MIN_INTERVAL_MS = 25;
37890
- var MAX_INTERVAL_MS = 2147483647;
37891
- var REGISTRATION_SETTLE_MS = 5000;
37892
- var SHUTDOWN_SETTLE_MS = 250;
37893
- var HEARTBEAT_RETRY_MIN_MS = 20000;
37894
- var HEARTBEAT_RETRY_MAX_MS = 40000;
37895
- function heartbeatRetryDelayMs(draw = Math.random) {
37896
- return Math.round(HEARTBEAT_RETRY_MIN_MS + draw() * (HEARTBEAT_RETRY_MAX_MS - HEARTBEAT_RETRY_MIN_MS));
37897
- }
37898
- function namedInterval(raw) {
37899
- const parsed = Number(raw);
37900
- return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
37901
- }
37902
- function resolvedInterval(raw, fallback) {
37903
- const named = namedInterval(raw);
37904
- return named === undefined ? fallback : Math.min(Math.max(named, MIN_INTERVAL_MS), MAX_INTERVAL_MS);
37905
- }
37906
- function natsSeedFromEnvironment(env) {
37907
- const { NATS_NKEY_SEED_FILE: file, NATS_NKEY_SEED: plain } = env;
37908
- if (file !== undefined) {
37909
- if (file === "")
37910
- throw new Error("NATS_NKEY_SEED_FILE is set but empty");
37911
- return {
37912
- value: readSecretFile("NATS_NKEY_SEED_FILE", file),
37913
- source: `NATS_NKEY_SEED_FILE (${file})`
37914
- };
37915
- }
37916
- if (plain === undefined)
37917
- return;
37918
- const value = plain.trim();
37919
- if (value.length === 0)
37920
- throw new Error("NATS_NKEY_SEED is set but empty");
37921
- return { value, source: "NATS_NKEY_SEED" };
37922
- }
37923
- var NO_NATS_SERVER = "Envoy has no NATS server (envoy.natsUrls, ENVOY_NATS_URL)";
37924
- var NO_NATS_CONNECTION = "this session opens no NATS connection";
37925
- function natsServerKey(url) {
37926
- const match = /^([a-z][a-z0-9+.-]*):\/\/([^@/]*@)?(\[[^\]]*\]|[^:/]*)(?::(\d+))?(\/.*)?$/i.exec(url);
37927
- if (match === null)
37928
- return url;
37929
- const [, scheme = "", userinfo = "", host = "", port = "4222", rest = ""] = match;
37930
- return `${scheme.toLowerCase()}://${userinfo}${host.toLowerCase()}:${port}${rest.replace(/\/+$/, "")}`;
37931
- }
37932
- function natsCredential(env, section, namedNatsUrls, filePath) {
37933
- const fromEnvironment = natsSeedFromEnvironment(env);
37934
- if (fromEnvironment !== undefined || section.natsSeed === undefined)
37935
- return fromEnvironment;
37936
- if (namedNatsUrls.length > 0) {
37937
- const servers = new Set((section.natsUrls ?? []).map(natsServerKey));
37938
- const named = new Set(namedNatsUrls.map(natsServerKey));
37939
- if (named.size !== servers.size || ![...named].every((server) => servers.has(server))) {
37940
- throw new Error(`ENVOY_NATS_URL names other servers than ${filePath}'s envoy.natsUrls; set NATS_NKEY_SEED_FILE for them`);
37931
+ };
37932
+ const currentSession = sessionRead(() => host.sessionID());
37933
+ const writeSession = sessionRead(() => host.writeSessionID === undefined ? host.sessionID() : host.writeSessionID());
37934
+ const bindTo = (sessionID, read) => {
37935
+ if (sessionID === undefined || sessionID === "" || read() !== sessionID)
37936
+ return;
37937
+ const bound = epoch;
37938
+ return { sessionID, alive: () => bound === epoch && read() === sessionID };
37939
+ };
37940
+ const bind = () => bindTo(currentSession(), currentSession);
37941
+ const stateJSON = (afters = true) => {
37942
+ const subjects = {};
37943
+ for (const subject of [...cursors.keys()].sort()) {
37944
+ const cursor = cursors.get(subject) ?? {};
37945
+ subjects[subject] = {
37946
+ ...cursor.since === undefined ? {} : { since: cursor.since },
37947
+ ...!afters || cursor.after === undefined ? {} : { after: cursor.after },
37948
+ ...cursor.from_start === undefined ? {} : { from_start: cursor.from_start }
37949
+ };
37941
37950
  }
37942
- }
37943
- return {
37944
- value: fileValue(section.natsSeed, "envoy.natsSeed", filePath, env),
37945
- source: `envoy.natsSeed in ${filePath}`
37951
+ return JSON.stringify({
37952
+ ...streamID === undefined ? {} : { stream_id: streamID },
37953
+ ...streamCreated === undefined ? {} : { stream_created: streamCreated },
37954
+ subjects,
37955
+ ...pending.size === 0 ? {} : { notices: [...pending].sort() }
37956
+ });
37946
37957
  };
37947
- }
37948
- function envoySettings(env, config, options = {}) {
37949
- if (config.kind === "failed")
37950
- return offWithError(config.error);
37951
- const section = config.envoy;
37952
- if (section.enabled === false) {
37953
- return offWithNotice(`${config.path} sets envoy.enabled to false: Envoy is off`);
37954
- }
37955
- const namedUrl = nonEmpty(env.ENVOY_URL);
37956
- const namedNatsUrls = (env.ENVOY_NATS_URL ?? "").split(",").map((url) => url.trim()).filter((url) => url.length > 0);
37957
- const addressed = namedUrl !== undefined || namedNatsUrls.length > 0 || section.url !== undefined || (section.natsUrls ?? []).length > 0 || section.enabled === true;
37958
- if (!addressed) {
37959
- return offWithNotice(config.missing === null ? `${config.path} names no Envoy listener or NATS server, and neither ENVOY_URL nor ENVOY_NATS_URL is set: Envoy is off` : missingConfigNotice(config.path, config.missing, "Envoy is off"));
37960
- }
37961
- if (namedUrl !== undefined && httpUrl2(namedUrl) === undefined) {
37962
- return offWithError("ENVOY_URL must be an http or https URL");
37963
- }
37964
- const listener = section.url ?? DEFAULT_ENVOY_URL;
37965
- const url = namedUrl ?? listener;
37966
- let credential;
37967
- try {
37968
- const source = bearerSource(env, "ENVOY", () => section.token === undefined ? undefined : {
37969
- token: section.token,
37970
- field: "envoy.token",
37971
- path: config.path,
37972
- server: listener,
37973
- checkout: false
37958
+ const record2 = (identity) => {
37959
+ const entry = ledger.record(identity);
37960
+ if (entry !== undefined)
37961
+ unwritten.push(entry);
37962
+ };
37963
+ const flush = (alive, atOnce = false) => {
37964
+ writes = writes.then(async () => {
37965
+ if (!alive())
37966
+ return;
37967
+ const json = stateJSON();
37968
+ const delivered = unwritten;
37969
+ if (json === writtenState && delivered.length === 0)
37970
+ return;
37971
+ const shape = stateJSON(false);
37972
+ if (delivered.length === 0 && !atOnce && shape === writtenShape && writtenAt !== undefined && now() - writtenAt < REPLAY_STATE_REFRESH_MS) {
37973
+ return;
37974
+ }
37975
+ unwritten = [];
37976
+ const state = json === writtenState ? undefined : JSON.parse(json);
37977
+ try {
37978
+ await host.write({
37979
+ delivered,
37980
+ state,
37981
+ snapshot: () => ({ state: JSON.parse(json), ledger: ledger.list() })
37982
+ });
37983
+ if (state !== undefined) {
37984
+ writtenState = json;
37985
+ writtenShape = shape;
37986
+ writtenAt = now();
37987
+ }
37988
+ } catch (error) {
37989
+ unwritten = [...delivered, ...unwritten];
37990
+ log("replay: could not persist the replay state; the next write carries it", error);
37991
+ }
37974
37992
  });
37975
- if (source.kind === "none") {
37976
- credential = brokerRouteConfigured(env).traded ? brokerCredential(env, options.session) : undefined;
37977
- } else {
37978
- credential = bearerCredential(source.kind === "variable" ? source.token : fileBearer(source, url, "ENVOY", env));
37993
+ return writes;
37994
+ };
37995
+ const syncFollowed = () => {
37996
+ const followed = new Set(host.followedSubjects());
37997
+ for (const subject of [...cursors.keys()]) {
37998
+ if (!followed.has(subject)) {
37999
+ cursors.delete(subject);
38000
+ cursorStarts.delete(subject);
38001
+ }
37979
38002
  }
37980
- } catch (error) {
37981
- return offWithError(messageFor(error));
37982
- }
37983
- const natsUrls = namedNatsUrls.length > 0 ? namedNatsUrls : section.natsUrls ?? [];
37984
- let inbound;
37985
- if (options.nats === false)
37986
- inbound = { off: NO_NATS_CONNECTION };
37987
- else if (natsUrls.length === 0)
37988
- inbound = { off: NO_NATS_SERVER };
37989
- else {
38003
+ for (const subject of followed) {
38004
+ if (!cursors.has(subject))
38005
+ cursors.set(subject, {});
38006
+ }
38007
+ };
38008
+ const failureNotice = (envelope, publishedAt, error, subject) => ({
38009
+ eventId: envelope.event_id ?? "unknown",
38010
+ sender: senderLabel(envelope),
38011
+ summary: envelope.payload_summary ?? "unknown",
38012
+ publishedAt,
38013
+ error,
38014
+ readWith: envelope.source === "dispatch" ? "dispatch read" : "envoy_inbox",
38015
+ ...subject === undefined ? {} : { subject },
38016
+ envelope
38017
+ });
38018
+ const showFailureNotice = async (notice, alive) => {
38019
+ let queued;
37990
38020
  try {
37991
- inbound = { natsUrls, natsSeed: natsCredential(env, section, namedNatsUrls, config.path) };
38021
+ queued = await host.showFailureNotice(notice) === "queued";
37992
38022
  } catch (error) {
37993
- inbound = { off: messageFor(error) };
38023
+ if (!alive())
38024
+ return "stopped";
38025
+ safely("failure notice warning", () => host.failureNoticeFailed(notice, error));
38026
+ return "hole";
37994
38027
  }
37995
- }
37996
- const heartbeatMs = resolvedInterval(env.ENVOY_HEARTBEAT_MS, DEFAULT_HEARTBEAT_MS);
37997
- const tmuxPlaceCacheMs = resolvedInterval(env.ENVOY_TMUX_PLACE_CACHE_MS, DEFAULT_TMUX_PLACE_CACHE_MS);
37998
- const namedPassDeadlineMs = namedInterval(env.ENVOY_PASS_DEADLINE_MS);
37999
- return {
38000
- enabled: true,
38001
- url: normalizeEnvoyUrl(url),
38002
- credential,
38003
- inbound,
38004
- heartbeatMs,
38005
- tmuxPlaceCacheMs,
38006
- passDeadlineMs: Math.min(Math.max(namedPassDeadlineMs ?? MIN_PASS_DEADLINE_MS, heartbeatMs), MAX_INTERVAL_MS),
38007
- passDeadlineRaisedFrom: namedPassDeadlineMs !== undefined && namedPassDeadlineMs < heartbeatMs ? namedPassDeadlineMs : undefined
38028
+ if (!alive())
38029
+ return "stopped";
38030
+ if (queued)
38031
+ return "hole";
38032
+ record2(replayLedgerIdentity(notice.envelope));
38033
+ await flush(alive);
38034
+ return "done";
38008
38035
  };
38009
- }
38010
- function normalizeEnvoyUrl(value) {
38011
- return value.replace(/\/+$/, "");
38012
- }
38013
-
38014
- // ../envoy-client/src/machine.ts
38015
- import { hostname as hostname3 } from "os";
38016
- function machineID() {
38017
- return process.env["ENVOY_MACHINE_ID"] || process.env["AGENTBOX_HOST"] || hostname3();
38018
- }
38019
-
38020
- // ../envoy-client/src/transport.ts
38021
- var REPLAY_TIMEOUT_MS = 1e4;
38022
- var REPLAY_UNAVAILABLE_CODE = "REPLAY_UNAVAILABLE";
38023
- var SessionStateSchema = _enum2(["live", "unresponsive", "gone"]);
38024
- 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.";
38025
- var WireStateSchema = SessionStateSchema.default("live");
38026
- var InterestWireSchema = object({
38027
- session_id: string2(),
38028
- machine_id: string2(),
38029
- dir: string2(),
38030
- topics: array(string2()),
38031
- updated_at: number2().int().optional(),
38032
- warnings: array(string2()).optional()
38033
- });
38034
- var DeliveryAnswerWireSchema = discriminatedUnion("mode", [
38035
- object({
38036
- mode: literal("consumer"),
38037
- subject: string2().min(1),
38038
- stream_id: string2(),
38039
- gap: boolean2(),
38040
- consumer_start: number2().int().nonnegative().optional()
38041
- }),
38042
- object({ mode: literal("core"), reason: string2() })
38043
- ]);
38044
- var SessionWireSchema = object({
38045
- session_id: string2(),
38046
- machine_id: string2(),
38047
- host: string2().optional(),
38048
- dir: string2(),
38049
- port: number2().int(),
38050
- title: string2(),
38051
- topics: array(string2()),
38052
- roles: array(string2()).optional(),
38053
- capabilities: array(string2()).optional(),
38054
- self_subscribed: boolean2(),
38055
- last_seen: number2().int().optional(),
38056
- state: WireStateSchema,
38057
- plugin: string2().optional(),
38058
- plugin_version: string2().optional()
38059
- });
38060
- var RoleWireSchema = object({
38061
- role: string2(),
38062
- holder: string2(),
38063
- host: string2().optional(),
38064
- last_seen: number2().int(),
38065
- state: WireStateSchema,
38066
- holder_state: SessionStateSchema.optional(),
38067
- client_instance_id: string2().optional(),
38068
- handed_back: boolean2().optional()
38069
- });
38070
- var RoleHeldWireSchema = object({
38071
- error: string2(),
38072
- role: string2(),
38073
- holder: string2()
38074
- });
38075
- var ErrorWireSchema = object({
38076
- error: string2(),
38077
- code: string2().optional(),
38078
- expected: array(string2()).optional(),
38079
- state: SessionStateSchema.optional(),
38080
- holder: string2().optional(),
38081
- holder_state: SessionStateSchema.optional(),
38082
- last_seen: number2().int().optional(),
38083
- claim_released: boolean2().optional(),
38084
- unresponsive: array(string2()).optional()
38085
- });
38086
- var EnvelopeResponseSchema = EnvelopeSchema.extend({
38087
- recipient: string2().optional(),
38088
- holder: string2().optional(),
38089
- holder_state: SessionStateSchema.optional(),
38090
- recipient_state: SessionStateSchema.optional(),
38091
- last_seen: number2().int().optional(),
38092
- unresponsive: array(string2()).optional(),
38093
- duplicate: boolean2().optional()
38094
- });
38095
- var PublishResponseSchema = EnvelopeResponseSchema.extend({
38096
- kept: boolean2().optional(),
38097
- kept_reason: _enum2(["unclaimed", "holder_lapsed"]).optional()
38098
- });
38099
- var ReplayWireSchema = object({
38100
- stream: object({
38101
- id: string2().min(1).optional(),
38102
- created: string2(),
38103
- first_seq: number2().int().nonnegative(),
38104
- last_seq: number2().int().nonnegative()
38105
- }),
38106
- subjects: array(object({
38107
- subject: string2(),
38108
- messages: array(object({
38109
- seq: number2().int().positive(),
38110
- subject: string2(),
38111
- time: string2(),
38112
- data: string2()
38113
- })),
38114
- next_after: number2().int().nonnegative(),
38115
- more: boolean2(),
38116
- refused: string2().optional()
38117
- }))
38118
- });
38119
- var DELIVERY_NOT_ANSWERED = "not_answered";
38120
- var DELIVERY_UNREADABLE = "unreadable_answer";
38121
- function parseSubscribeAnswer(body) {
38122
- const interest = InterestWireSchema.parse(body);
38123
- const raw = typeof body === "object" && body !== null && "delivery" in body ? body.delivery : undefined;
38124
- if (raw === undefined || raw === null) {
38125
- return { ...interest, delivery: { mode: "core", reason: DELIVERY_NOT_ANSWERED } };
38126
- }
38127
- const delivery = DeliveryAnswerWireSchema.safeParse(raw);
38128
- return {
38129
- ...interest,
38130
- delivery: delivery.success ? delivery.data : { mode: "core", reason: DELIVERY_UNREADABLE }
38131
- };
38132
- }
38133
- function mergeInterestSources(registryTopics, liveTopics) {
38134
- const interests = new Map;
38135
- for (const topic of registryTopics) {
38136
- interests.set(topic, liveTopics.includes(topic) ? "both" : "registry");
38137
- }
38138
- for (const topic of liveTopics) {
38139
- if (!interests.has(topic))
38140
- interests.set(topic, "live");
38141
- }
38142
- return interests;
38143
- }
38144
- function holderStateOf(role) {
38145
- return role.holder_state ?? role.state;
38146
- }
38147
- function namedProcess(id) {
38148
- return id === "" ? undefined : id;
38149
- }
38150
- function roleHeldBy(info, sessionID, clientInstanceID) {
38151
- if (info === undefined || info.holder !== sessionID || info.handed_back === true)
38152
- return false;
38153
- const holding = namedProcess(info.client_instance_id);
38154
- return holding === undefined || holding === namedProcess(clientInstanceID);
38155
- }
38156
- function mayActOnRoleRecord(writer, clientInstanceID, holdsRole) {
38157
- const wrote = namedProcess(writer);
38158
- if (wrote === undefined || wrote === namedProcess(clientInstanceID))
38159
- return true;
38160
- return !holdsRole;
38161
- }
38162
- function newClientInstanceID() {
38163
- return Array.from(crypto.getRandomValues(new Uint8Array(16)), (byte) => byte.toString(16).padStart(2, "0")).join("");
38164
- }
38165
- function expandSubscriptionTopics(topics) {
38166
- const expanded = new Set;
38167
- for (const topic of topics) {
38168
- const base = topic.endsWith(".>") ? topic.slice(0, -2) : undefined;
38169
- if (base === undefined || base.includes("*") || base.includes(">")) {
38170
- expanded.add(topic);
38171
- continue;
38036
+ const handle = async (seq, buffered, alive) => {
38037
+ const { message } = buffered;
38038
+ const envelope = parseEnvelope(message.data);
38039
+ if (envelope === undefined) {
38040
+ log(`replay: skipping stored message ${seq} on ${message.subject}: not an Envoy envelope`);
38041
+ return "done";
38172
38042
  }
38173
- const segments = base.split(".");
38174
- if (segments.length < 2 || segments.some((segment) => segment.length === 0)) {
38175
- throw new TypeError(`cannot expand Envoy wildcard topic "${topic}": its concrete base must have at least two non-empty segments`);
38043
+ if (isRoleForward(envelope))
38044
+ return "done";
38045
+ const identity = replayLedgerIdentity(envelope);
38046
+ if (ledger.has(identity)) {
38047
+ failures.delete(seq);
38048
+ return "done";
38176
38049
  }
38177
- expanded.add(base);
38178
- expanded.add(topic);
38179
- }
38180
- return [...expanded];
38181
- }
38182
- function refusalStateText(parsed) {
38183
- const sentences = [];
38184
- const state = parsed.holder_state ?? parsed.state;
38185
- if (state !== undefined) {
38186
- const who = parsed.holder === undefined ? "That session" : `Holder ${parsed.holder}`;
38187
- const seen = parsed.last_seen === undefined || parsed.last_seen <= 0 ? "" : `, last seen ${new Date(parsed.last_seen).toISOString()}`;
38188
- sentences.push(`${who} is ${state}${seen}.`);
38189
- }
38190
- const stored = parsed.unresponsive ?? [];
38191
- if (stored.length > 0) {
38192
- const one = stored.length === 1;
38193
- 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.`);
38194
- }
38195
- if (sentences.length === 0)
38196
- return "";
38197
- if (state === "unresponsive" || stored.length > 0)
38198
- sentences.push(UNRESPONSIVE_RULE);
38199
- return ` ${sentences.join(" ")}`;
38200
- }
38201
-
38202
- class EnvoyApiError extends Error {
38203
- details;
38204
- name = "EnvoyApiError";
38205
- expected;
38206
- code;
38207
- holder;
38208
- sessionState;
38209
- lastSeen;
38210
- claimReleased;
38211
- unresponsive;
38212
- constructor(details) {
38213
- let parsed;
38050
+ if (dedupe?.pending(envelope) === true)
38051
+ return "hole";
38052
+ if (!alive())
38053
+ return "stopped";
38054
+ const giveUp = async (error) => {
38055
+ const handled = await showFailureNotice(failureNotice(envelope, message.time, error, message.subject), alive);
38056
+ if (handled === "done")
38057
+ failures.delete(seq);
38058
+ return handled;
38059
+ };
38060
+ const failure = failures.get(seq);
38061
+ if (failure !== undefined && failure.count >= REPLAY_FAILURES_BEFORE_NOTICE) {
38062
+ return giveUp(failure.error);
38063
+ }
38064
+ const followed = buffered.subjects[0] ?? message.subject;
38065
+ let outcome;
38066
+ let error = "the host could not show it";
38214
38067
  try {
38215
- const result = ErrorWireSchema.safeParse(JSON.parse(details.responseBody));
38216
- if (result.success)
38217
- parsed = result.data;
38218
- } catch {}
38219
- super(parsed === undefined ? `${details.method} ${details.url} failed with ${details.status}: ${details.responseBody}` : `${parsed.error}${parsed.expected === undefined ? "" : ` (expected: ${parsed.expected.join(", ")})`}${refusalStateText(parsed)}`);
38220
- this.details = details;
38221
- this.expected = parsed?.expected;
38222
- this.code = parsed?.code;
38223
- this.holder = parsed?.holder;
38224
- this.sessionState = parsed?.holder_state ?? parsed?.state;
38225
- this.lastSeen = parsed?.last_seen;
38226
- this.claimReleased = parsed?.claim_released;
38227
- this.unresponsive = parsed?.unresponsive;
38228
- }
38229
- }
38230
- var mintedDedupeKey2 = new RegExp(MINTED_DEDUPE_KEY_PATTERN);
38231
-
38232
- class EnvoySendUnconfirmedError extends Error {
38233
- targetSessionID;
38234
- idempotencyKey;
38235
- attempts;
38236
- last;
38237
- name = "EnvoySendUnconfirmedError";
38238
- resendSafe;
38239
- constructor(targetSessionID, idempotencyKey, attempts, last) {
38240
- const got = last instanceof EnvoyApiError ? `the listener answered ${last.details.status} (${last.message})` : `the listener did not answer (${last instanceof Error ? last.message : String(last)})`;
38241
- const tried = attempts === 1 ? "on its only attempt" : `on the last of ${attempts} attempts`;
38242
- const resendSafe = mintedDedupeKey2.test(`agent.${targetSessionID.trim()}.${idempotencyKey}`);
38243
- const resend = resendSafe ? `Sending it again with idempotency_key ${idempotencyKey} within ` + `${DELIVERY_DUPLICATE_WINDOW_MS / 3600000} hours is the safe resend: Envoy drops it as ` + "a repeat if the first copy arrived (a recipient that restarted in between can rarely " + "see it twice)." : `Envoy cannot recognise a resend under idempotency_key ${idempotencyKey} as a repeat ` + "(the key is not a lower-case UUID, or the session id holds a dot): ask the recipient " + "whether it arrived before sending it again.";
38244
- super(`The send to ${targetSessionID} is not confirmed delivered: ${got} ${tried}. It may or may ` + `not have arrived. ${resend}`, { cause: last });
38245
- this.targetSessionID = targetSessionID;
38246
- this.idempotencyKey = idempotencyKey;
38247
- this.attempts = attempts;
38248
- this.last = last;
38249
- this.resendSafe = resendSafe;
38250
- }
38251
- }
38252
- function resolveCallTiming(config) {
38253
- return {
38254
- timeoutMs: config.timeoutMs ?? LISTENER_CALL_TIMEOUT_MS,
38255
- attempts: config.retry === false ? 1 : LISTENER_CALL_ATTEMPTS,
38256
- retryDelayMs: LISTENER_CALL_RETRY_DELAY_MS
38068
+ outcome = await host.deliver({
38069
+ envelope,
38070
+ data: message.data,
38071
+ seq,
38072
+ subject: message.subject,
38073
+ followed,
38074
+ time: message.time,
38075
+ since: cursors.get(followed)?.since
38076
+ });
38077
+ } catch (thrown) {
38078
+ outcome = "failed";
38079
+ error = messageFor(thrown);
38080
+ }
38081
+ if (!alive())
38082
+ return "stopped";
38083
+ if (outcome === "shown") {
38084
+ failures.delete(seq);
38085
+ record2(identity);
38086
+ await flush(alive);
38087
+ return "done";
38088
+ }
38089
+ if (outcome === "suppressed") {
38090
+ failures.delete(seq);
38091
+ return "done";
38092
+ }
38093
+ if (outcome === "queued") {
38094
+ failures.delete(seq);
38095
+ return "hole";
38096
+ }
38097
+ const count = (failure?.count ?? 0) + 1;
38098
+ failures.set(seq, { count, error });
38099
+ if (count >= REPLAY_FAILURES_BEFORE_NOTICE)
38100
+ return giveUp(error);
38101
+ log(`replay: could not show stored message ${seq} (${envelope.event_id}): ${error}`);
38102
+ return "hole";
38257
38103
  };
38258
- }
38259
- function createEnvoyClient(config) {
38260
- const baseUrl = normalizeEnvoyUrl(config.baseUrl);
38261
- const { timeoutMs, attempts, retryDelayMs } = resolveCallTiming(config);
38262
- const credential = config.credential;
38263
- const attemptOnce = async (url, init, deadlineMs, actingSession) => {
38264
- let response;
38265
- let unminted;
38266
- try {
38267
- const headers = new Headers(init.headers);
38268
- let presented;
38104
+ const listenerFailed = (error) => {
38105
+ if (error instanceof EnvoyApiError) {
38106
+ const status = error.details.status;
38107
+ if (status === 404) {
38108
+ if (listenerNoticed === "not_served")
38109
+ return;
38110
+ listenerNoticed = "not_served";
38111
+ notify({
38112
+ kind: "not_served",
38113
+ 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."
38114
+ });
38115
+ return;
38116
+ }
38117
+ if (status === 503 && error.code === REPLAY_UNAVAILABLE_CODE || status === 401 || status === 403) {
38118
+ const key = `unavailable:${status}:${error.message}`;
38119
+ if (listenerNoticed === key)
38120
+ return;
38121
+ listenerNoticed = key;
38122
+ notify({
38123
+ kind: "unavailable",
38124
+ status,
38125
+ error: error.message,
38126
+ message: `Envoy replay is unavailable (${status}: ${error.message}), so messages published while this session's connection is down are not recovered until it answers.`
38127
+ });
38128
+ return;
38129
+ }
38130
+ }
38131
+ log("replay: the listener's replay failed; the next trigger tries again", error);
38132
+ };
38133
+ const streamReplaced = (stream) => {
38134
+ if (stream.id === undefined) {
38135
+ return streamCreated !== undefined && streamCreated !== "" && stream.created !== streamCreated;
38136
+ }
38137
+ return streamID !== undefined && streamID !== "" && stream.id !== streamID;
38138
+ };
38139
+ const streamMovedBack = (stream) => {
38140
+ for (const cursor of cursors.values()) {
38141
+ if (cursor.after !== undefined && cursor.after > stream.last_seq)
38142
+ return true;
38143
+ }
38144
+ return false;
38145
+ };
38146
+ const sweepOnce = async () => {
38147
+ const binding = bind();
38148
+ if (binding === undefined)
38149
+ return;
38150
+ const { sessionID, alive } = binding;
38151
+ syncFollowed();
38152
+ const readings = new Map;
38153
+ for (const [subject, cursor] of cursors) {
38154
+ readings.set(subject, {
38155
+ read: cursor.from_start === undefined ? cursor.after : 0,
38156
+ more: true
38157
+ });
38158
+ }
38159
+ const buffer = new Map;
38160
+ const holes = new Map;
38161
+ let gapNoticed = false;
38162
+ if (pending.size > 0) {
38163
+ for (const kind of [...pending].sort()) {
38164
+ if (kind === "retention_gap")
38165
+ gapNoticed = true;
38166
+ tell(kind);
38167
+ }
38168
+ await flush(alive);
38169
+ if (!alive())
38170
+ return;
38171
+ }
38172
+ if (consumerMode)
38173
+ return;
38174
+ for (let page = 0;page < REPLAY_PAGES_PER_SWEEP; page += 1) {
38175
+ let handledAny = false;
38176
+ const asking = [...readings].filter(([, reading]) => reading.more).sort(([, a], [, b]) => (a.read ?? -1) - (b.read ?? -1)).slice(0, REPLAY_MAX_SUBJECTS);
38177
+ if (asking.length === 0)
38178
+ break;
38179
+ let response;
38269
38180
  try {
38270
- const ready = credential?.headers(init.method ?? "GET", url, actingSession);
38271
- presented = ready instanceof Promise ? await ready : ready;
38181
+ response = await transport.replay({
38182
+ sessionID,
38183
+ subjects: asking.map(([subject, reading]) => reading.read === undefined ? { subject } : { subject, after: reading.read }),
38184
+ ...options.maxMessages === undefined ? {} : { maxMessages: options.maxMessages }
38185
+ });
38272
38186
  } catch (error) {
38273
- unminted = error;
38187
+ if (alive()) {
38188
+ answered.clear();
38189
+ listenerFailed(error);
38190
+ }
38191
+ return;
38274
38192
  }
38275
- for (const [name, value] of Object.entries(presented ?? {}))
38276
- headers.set(name, value);
38277
- response = await config.fetch(url, {
38278
- ...init,
38279
- headers,
38280
- signal: AbortSignal.timeout(deadlineMs)
38281
- });
38282
- } catch (error) {
38283
- return { unanswered: error };
38284
- }
38285
- if (unminted !== undefined && (response.status === 401 || response.status === 403)) {
38286
- return {
38287
- unanswered: new Error(`${init.method ?? "GET"} ${url} answered ${response.status}, and this machine's Envoy credential could not be minted: ${messageFor(unminted)}`, { cause: unminted })
38288
- };
38289
- }
38290
- try {
38291
- return { response, body: await response.text() };
38292
- } catch (error) {
38293
- if (response.status >= 400 && response.status < 500) {
38294
- return { response, body: `(its body did not arrive: ${String(error)})` };
38193
+ if (!alive())
38194
+ return;
38195
+ listenerNoticed = undefined;
38196
+ for (const result of response.subjects) {
38197
+ if (result.refused === undefined)
38198
+ answered.add(result.subject);
38199
+ else
38200
+ answered.delete(result.subject);
38295
38201
  }
38296
- return { unanswered: error };
38297
- }
38298
- };
38299
- const request = async (path, init, options = {}) => {
38300
- const url = `${baseUrl}${path}`;
38301
- const tries = options.retry === false ? 1 : attempts;
38302
- for (let attempt = 0;attempt < tries; attempt += 1) {
38303
- const outcome = await attemptOnce(url, init, options.timeoutMs ?? timeoutMs, options.actingSession);
38304
- if ("unanswered" in outcome) {
38305
- options.onUnanswered?.();
38306
- if (attempt + 1 < tries) {
38307
- await waitForRetry(retryDelayMs);
38308
- continue;
38202
+ const { stream } = response;
38203
+ const replaced = streamReplaced(stream);
38204
+ streamID = stream.id ?? (replaced ? undefined : streamID);
38205
+ streamCreated = stream.created;
38206
+ if (replaced) {
38207
+ const at = now();
38208
+ for (const subject of cursors.keys()) {
38209
+ cursors.set(subject, { since: stream.last_seq, after: stream.last_seq });
38210
+ cursorStarts.set(subject, at);
38309
38211
  }
38310
- throw outcome.unanswered;
38212
+ failures.clear();
38213
+ pending.add("stream_recreated");
38214
+ await flush(alive);
38215
+ if (!alive())
38216
+ return;
38217
+ tell("stream_recreated");
38218
+ await flush(alive);
38219
+ return;
38311
38220
  }
38312
- const { response, body } = outcome;
38313
- if (response.ok)
38314
- return body;
38315
- const error = new EnvoyApiError({
38316
- method: init.method ?? "GET",
38317
- url,
38318
- status: response.status,
38319
- responseBody: body
38320
- });
38321
- if (response.status >= 500) {
38322
- options.onUnanswered?.();
38323
- if (attempt + 1 < tries) {
38324
- await waitForRetry(retryDelayMs);
38221
+ if (streamMovedBack(stream)) {
38222
+ const from = Math.max(0, stream.first_seq - 1);
38223
+ for (const [subject, cursor] of cursors) {
38224
+ if (cursor.after === undefined)
38225
+ continue;
38226
+ cursors.set(subject, { ...cursor, after: from });
38227
+ const reading = readings.get(subject);
38228
+ if (reading !== undefined) {
38229
+ reading.read = from;
38230
+ reading.more = true;
38231
+ }
38232
+ }
38233
+ buffer.clear();
38234
+ holes.clear();
38235
+ failures.clear();
38236
+ pending.add("stream_moved_back");
38237
+ await flush(alive, true);
38238
+ if (!alive())
38239
+ return;
38240
+ tell("stream_moved_back");
38241
+ await flush(alive);
38242
+ if (!alive())
38243
+ return;
38244
+ continue;
38245
+ }
38246
+ for (const [, reading] of asking)
38247
+ reading.more = false;
38248
+ for (const result of response.subjects) {
38249
+ const reading = readings.get(result.subject);
38250
+ const markedAt = cursors.get(result.subject)?.from_start;
38251
+ if (reading === undefined || markedAt === undefined || result.refused !== undefined) {
38325
38252
  continue;
38326
38253
  }
38254
+ const start = Math.max(0, stream.first_seq - 1);
38255
+ cursors.set(result.subject, { since: start, after: start });
38256
+ cursorStarts.set(result.subject, now());
38257
+ if (now() - markedAt < DELIVERY_DUPLICATE_WINDOW_MS)
38258
+ reading.read = start;
38327
38259
  }
38328
- throw error;
38329
- }
38330
- throw new Error("Envoy request exhausted retries");
38331
- };
38332
- const post = (path, body, options) => request(path, {
38333
- method: "POST",
38334
- headers: { "Content-Type": "application/json" },
38335
- body: JSON.stringify(body)
38336
- }, options);
38337
- return {
38338
- subscribe: async (input) => parseSubscribeAnswer(JSON.parse(await post("/v1/interests/subscribe", {
38339
- session_id: input.sessionID,
38340
- dir: input.directory,
38341
- topics: expandSubscriptionTopics(input.topics),
38342
- port: input.port,
38343
- title: input.title,
38344
- ...input.capabilities === undefined ? {} : { capabilities: input.capabilities },
38345
- driving: input.driving,
38346
- ...input.selfSubscribed === undefined ? {} : { self_subscribed: input.selfSubscribed },
38347
- ...input.delivery === undefined ? {} : { delivery: input.delivery },
38348
- ...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID },
38349
- host: machineID(),
38350
- ...input.place === undefined ? {} : { tmux_place: input.place },
38351
- plugin: input.plugin.name,
38352
- plugin_version: input.plugin.version
38353
- }, { actingSession: input.sessionID }))),
38354
- unsubscribe: async (input) => {
38355
- await post("/v1/interests/unsubscribe", {
38356
- session_id: input.sessionID,
38357
- topics: expandSubscriptionTopics(input.topics),
38358
- ...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID }
38359
- }, { actingSession: input.sessionID });
38360
- },
38361
- getInterest: async (sessionID) => InterestWireSchema.parse(JSON.parse(await request(`/v1/interests/${sessionID}`, {}))),
38362
- send: async (input) => {
38363
- const idempotencyKey = input.idempotencyKey ?? crypto.randomUUID();
38364
- let unanswered = false;
38365
- let response;
38366
- try {
38367
- const answer = await post("/v1/messages/send", {
38368
- source: input.source ?? "agent",
38369
- ...sourceSession(input.sourceSessionID),
38370
- target_session: input.targetSessionID,
38371
- message: input.message,
38372
- idempotency_key: idempotencyKey,
38373
- ...messageMetadata(input)
38374
- }, {
38375
- onUnanswered: () => {
38376
- unanswered = true;
38377
- },
38378
- ...input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID }
38379
- });
38380
- response = EnvelopeResponseSchema.parse(JSON.parse(answer));
38381
- } catch (error) {
38382
- if (error instanceof EnvoyApiError && error.details.status < 500 && !unanswered) {
38383
- throw error;
38260
+ let gapped = false;
38261
+ for (const result of response.subjects) {
38262
+ const reading = readings.get(result.subject);
38263
+ if (reading?.read === undefined || result.refused !== undefined)
38264
+ continue;
38265
+ if (reading.read >= stream.first_seq - 1)
38266
+ continue;
38267
+ gapped = true;
38268
+ reading.read = stream.first_seq - 1;
38269
+ const cursor = cursors.get(result.subject);
38270
+ if (cursor?.after !== undefined && cursor.after < reading.read) {
38271
+ cursors.set(result.subject, { ...cursor, after: reading.read });
38384
38272
  }
38385
- throw new EnvoySendUnconfirmedError(input.targetSessionID, idempotencyKey, attempts, error);
38386
38273
  }
38387
- return {
38388
- envelope: response,
38389
- recipient: response.recipient ?? input.targetSessionID,
38390
- confirmed: response.recipient !== undefined,
38391
- recipientState: response.recipient_state ?? "live",
38392
- ...response.last_seen === undefined || response.last_seen <= 0 ? {} : { lastSeen: response.last_seen },
38393
- duplicate: response.duplicate === true
38394
- };
38395
- },
38396
- publish: async (input) => {
38397
- const idempotencyKey = input.idempotencyKey ?? crypto.randomUUID();
38398
- const response = PublishResponseSchema.parse(JSON.parse(await post("/v1/messages/publish", {
38399
- source: input.source ?? "agent",
38400
- ...sourceSession(input.sourceSessionID),
38401
- topic: input.topic,
38402
- message: input.message,
38403
- ...input.payload === undefined ? {} : { payload: input.payload },
38404
- idempotency_key: idempotencyKey,
38405
- ...messageMetadata(input),
38406
- ...input.keep === undefined ? {} : { keep: input.keep }
38407
- }, input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID })));
38408
- return {
38409
- envelope: response,
38410
- ...response.holder === undefined ? {} : { holder: response.holder },
38411
- ...response.holder_state === undefined ? {} : { holderState: response.holder_state },
38412
- unresponsive: response.unresponsive ?? [],
38413
- ...response.kept === undefined ? {} : { kept: response.kept },
38414
- ...response.kept_reason === undefined ? {} : { keptReason: response.kept_reason }
38415
- };
38416
- },
38417
- unregisterSession: async (input) => {
38418
- const query = input.clientInstanceID === undefined ? "" : `?client_instance_id=${encodeURIComponent(input.clientInstanceID)}`;
38419
- await request(`/v1/sessions/${encodeURIComponent(input.sessionID)}${query}`, { method: "DELETE" }, { actingSession: input.sessionID });
38420
- },
38421
- setRole: async (input) => {
38422
- const soft = input.soft === true;
38423
- const previous = soft ? input.previousSessionID ?? "" : "";
38424
- const body = {
38425
- session_id: input.sessionID,
38426
- role: input.role,
38427
- ...soft ? { soft: true } : {},
38428
- ...previous === "" ? {} : { previous_session_id: previous },
38429
- ...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID }
38430
- };
38431
- let raw;
38432
- try {
38433
- raw = await post("/v1/roles/set", body, { actingSession: input.sessionID });
38434
- } catch (error) {
38435
- if (error instanceof EnvoyApiError && error.details.status === 409) {
38436
- let answer;
38437
- try {
38438
- answer = JSON.parse(error.details.responseBody);
38439
- } catch {
38440
- throw error;
38274
+ if (gapped) {
38275
+ if (!gapNoticed)
38276
+ pending.add("retention_gap");
38277
+ await flush(alive, true);
38278
+ if (!alive())
38279
+ return;
38280
+ if (pending.has("retention_gap")) {
38281
+ gapNoticed = true;
38282
+ tell("retention_gap");
38283
+ await flush(alive);
38284
+ }
38285
+ }
38286
+ for (const result of response.subjects) {
38287
+ const reading = readings.get(result.subject);
38288
+ if (reading === undefined || result.refused !== undefined)
38289
+ continue;
38290
+ if (reading.read === undefined) {
38291
+ const cursor = cursors.get(result.subject);
38292
+ if (cursor !== undefined && cursor.after === undefined) {
38293
+ cursors.set(result.subject, { since: result.next_after, after: result.next_after });
38294
+ cursorStarts.set(result.subject, now());
38441
38295
  }
38442
- const held = RoleHeldWireSchema.safeParse(answer);
38443
- if (held.success)
38444
- return { claimed: false, holder: held.data.holder };
38296
+ reading.read = result.next_after;
38297
+ continue;
38445
38298
  }
38446
- throw error;
38299
+ for (const message of result.messages) {
38300
+ if (message.seq <= reading.read)
38301
+ continue;
38302
+ const buffered = buffer.get(message.seq);
38303
+ if (buffered === undefined) {
38304
+ buffer.set(message.seq, { message, subjects: [result.subject] });
38305
+ } else if (!buffered.subjects.includes(result.subject)) {
38306
+ buffered.subjects.push(result.subject);
38307
+ }
38308
+ }
38309
+ reading.read = Math.max(reading.read, result.next_after);
38310
+ reading.more = result.more;
38447
38311
  }
38448
- return { claimed: true, interest: InterestWireSchema.parse(JSON.parse(raw)) };
38449
- },
38450
- getRole: async (role) => RoleWireSchema.parse(JSON.parse(await request(`/v1/roles/${encodeURIComponent(role)}`, { method: "GET" }))),
38451
- listSessions: async (input = {}) => {
38452
- const search = new URLSearchParams;
38453
- if (input.directory !== undefined)
38454
- search.set("dir", input.directory);
38455
- if (input.title !== undefined)
38456
- search.set("title", input.title);
38457
- if (input.include !== undefined)
38458
- search.set("include", input.include);
38459
- const query = search.size === 0 ? "" : `?${search.toString()}`;
38460
- return SessionWireSchema.array().parse(JSON.parse(await request(`/v1/sessions${query}`, {})));
38461
- },
38462
- replay: async (input) => ReplayWireSchema.parse(JSON.parse(await post("/v1/replay", {
38463
- session_id: input.sessionID,
38464
- subjects: input.subjects.map((cursor) => cursor.after === undefined ? { subject: cursor.subject } : { subject: cursor.subject, after: cursor.after }),
38465
- ...input.maxMessages === undefined ? {} : { max_messages: input.maxMessages }
38466
- }, {
38467
- timeoutMs: input.timeoutMs ?? REPLAY_TIMEOUT_MS,
38468
- retry: false,
38469
- actingSession: input.sessionID
38470
- })))
38471
- };
38472
- }
38473
- function waitForRetry(delayMs) {
38474
- const { promise, resolve } = Promise.withResolvers();
38475
- setTimeout(resolve, delayMs);
38476
- return promise;
38477
- }
38478
- function messageMetadata(input) {
38479
- return {
38480
- ...input.inReplyTo === undefined ? {} : { in_reply_to: input.inReplyTo },
38481
- ...input.supersedes === undefined ? {} : { supersedes: input.supersedes },
38482
- ...input.urgency === undefined ? {} : { urgency: input.urgency },
38483
- ...input.expectsReply === undefined ? {} : { expects_reply: input.expectsReply },
38484
- ...input.expiresAt === undefined ? {} : { expires_at: input.expiresAt }
38312
+ let bound = Number.POSITIVE_INFINITY;
38313
+ for (const reading of readings.values()) {
38314
+ if (reading.more && reading.read !== undefined)
38315
+ bound = Math.min(bound, reading.read);
38316
+ }
38317
+ const ready = [...buffer.keys()].filter((seq) => seq <= bound).sort((a, b) => a - b);
38318
+ for (const seq of ready) {
38319
+ const buffered = buffer.get(seq);
38320
+ if (buffered === undefined)
38321
+ continue;
38322
+ buffer.delete(seq);
38323
+ if (!buffered.subjects.some((subject) => cursors.has(subject)))
38324
+ continue;
38325
+ const handled = await handle(seq, buffered, alive);
38326
+ if (handled === "stopped")
38327
+ return;
38328
+ if (handled === "done")
38329
+ handledAny = true;
38330
+ if (handled === "hole") {
38331
+ for (const subject of buffered.subjects) {
38332
+ if (!holes.has(subject))
38333
+ holes.set(subject, seq);
38334
+ }
38335
+ }
38336
+ }
38337
+ if (!alive())
38338
+ return;
38339
+ for (const [subject, reading] of readings) {
38340
+ const cursor = cursors.get(subject);
38341
+ if (cursor?.after === undefined || reading.read === undefined)
38342
+ continue;
38343
+ let after = Math.min(reading.read, bound);
38344
+ const hole = holes.get(subject);
38345
+ if (hole !== undefined)
38346
+ after = Math.min(after, hole - 1);
38347
+ if (after > cursor.after)
38348
+ cursors.set(subject, { ...cursor, after });
38349
+ }
38350
+ await flush(alive, handledAny);
38351
+ if (![...readings.values()].some((reading) => reading.more))
38352
+ break;
38353
+ }
38485
38354
  };
38486
- }
38487
- function sourceSession(sessionID) {
38488
- return sessionID === undefined || sessionID === "" ? {} : { source_session: sessionID };
38489
- }
38490
-
38491
- // ../envoy-client/src/replay.ts
38492
- var REPLAY_LEDGER_TTL_MS = DELIVERY_DUPLICATE_WINDOW_MS;
38493
- var REPLAY_LEDGER_LIMIT = 20000;
38494
- var REPLAY_PAGES_PER_SWEEP = 20;
38495
- var REPLAY_FAILURES_BEFORE_NOTICE = 3;
38496
- var REPLAY_STATE_REFRESH_MS = 60 * 60 * 1000;
38497
- var REPLAY_MAX_SUBJECTS = 32;
38498
- var START_CURSORS_TIMEOUT_MS = 2000;
38499
- var ROLE_FORWARD_KEY_PREFIX = "envoy.role.forward.";
38500
- var ReplayLedgerEntrySchema = object({
38501
- event_id: string2().min(1).optional(),
38502
- dedupe_key: string2().min(1).optional(),
38503
- at: number2()
38504
- });
38505
- var ReplayCursorSchema = object({
38506
- since: number2().int().nonnegative().optional(),
38507
- after: number2().int().nonnegative().optional(),
38508
- from_start: number2().int().nonnegative().optional()
38509
- });
38510
- var ReplayPendingNoticeSchema = _enum2([
38511
- "retention_gap",
38512
- "stream_moved_back",
38513
- "stream_recreated"
38514
- ]);
38515
- var PENDING_NOTICE_MESSAGES = {
38516
- stream_recreated: "Envoy's notification stream was recreated, so messages published to this session while its connection was down before that may be lost.",
38517
- 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.",
38518
- 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."
38519
- };
38520
- var ReplayStateSchema = object({
38521
- stream_id: string2().optional(),
38522
- stream_created: string2().optional(),
38523
- subjects: record(string2(), ReplayCursorSchema),
38524
- notices: array(string2()).optional()
38525
- });
38526
- var ReplaySnapshotSchema = object({
38527
- state: ReplayStateSchema.optional(),
38528
- ledger: array(ReplayLedgerEntrySchema)
38529
- });
38530
- function createReplayEngine(options) {
38531
- const { host, transport, dedupe } = options;
38532
- const now = options.now ?? Date.now;
38533
- const ledger = createLedger(now);
38534
- const cursors = new Map;
38535
- const cursorStarts = new Map;
38536
- let streamID;
38537
- let streamCreated;
38538
- const pending = new Set;
38539
- const failures = new Map;
38540
- let unwritten = [];
38541
- let writtenState;
38542
- let writtenShape;
38543
- let writtenAt;
38544
- let writes = Promise.resolve();
38545
- let epoch = 0;
38546
- let listenerNoticed;
38547
- const answered = new Set;
38548
- let running;
38549
- let rerun;
38550
- let consumerMode = false;
38551
- const safely = (what, call) => {
38355
+ const runSweep = async () => {
38552
38356
  try {
38553
- call();
38357
+ await sweepOnce();
38554
38358
  } catch (error) {
38555
- try {
38556
- host.log(`replay: ${what} failed`, error);
38557
- } catch {}
38359
+ log("replay: a sweep failed", error);
38558
38360
  }
38559
38361
  };
38560
- const log = (message, error) => safely("log", () => host.log(message, error));
38561
- const notify = (notice) => safely("notice", () => host.notify(notice));
38562
- const tell = (kind) => {
38563
- pending.delete(kind);
38564
- notify({ kind, message: PENDING_NOTICE_MESSAGES[kind] });
38565
- };
38566
- const sessionRead = (read) => () => {
38567
- try {
38568
- return read();
38569
- } catch {
38570
- return;
38362
+ const sweep = () => {
38363
+ if (running === undefined) {
38364
+ running = runSweep().finally(() => {
38365
+ running = undefined;
38366
+ });
38367
+ return running;
38571
38368
  }
38369
+ rerun ??= running.then(() => {
38370
+ rerun = undefined;
38371
+ return sweep();
38372
+ });
38373
+ return rerun;
38572
38374
  };
38573
- const currentSession = sessionRead(() => host.sessionID());
38574
- const writeSession = sessionRead(() => host.writeSessionID === undefined ? host.sessionID() : host.writeSessionID());
38575
- const bindTo = (sessionID, read) => {
38576
- if (sessionID === undefined || sessionID === "" || read() !== sessionID)
38375
+ const started = (cursor) => cursor?.after !== undefined || cursor?.from_start !== undefined;
38376
+ const startOnce = async () => {
38377
+ const binding = bind();
38378
+ if (binding === undefined)
38577
38379
  return;
38578
- const bound = epoch;
38579
- return { sessionID, alive: () => bound === epoch && read() === sessionID };
38580
- };
38581
- const bind = () => bindTo(currentSession(), currentSession);
38582
- const stateJSON = (afters = true) => {
38583
- const subjects = {};
38584
- for (const subject of [...cursors.keys()].sort()) {
38585
- const cursor = cursors.get(subject) ?? {};
38586
- subjects[subject] = {
38587
- ...cursor.since === undefined ? {} : { since: cursor.since },
38588
- ...!afters || cursor.after === undefined ? {} : { after: cursor.after },
38589
- ...cursor.from_start === undefined ? {} : { from_start: cursor.from_start }
38590
- };
38380
+ const { sessionID, alive } = binding;
38381
+ const unstarted = [...new Set(host.followedSubjects())].filter((subject) => !started(cursors.get(subject)));
38382
+ let changed = false;
38383
+ for (let index = 0;index < unstarted.length; index += REPLAY_MAX_SUBJECTS) {
38384
+ let response;
38385
+ try {
38386
+ response = await transport.replay({
38387
+ sessionID,
38388
+ subjects: unstarted.slice(index, index + REPLAY_MAX_SUBJECTS).map((subject) => ({ subject })),
38389
+ timeoutMs: START_CURSORS_TIMEOUT_MS
38390
+ });
38391
+ } catch (error) {
38392
+ if (!alive())
38393
+ return;
38394
+ const own = `${AGENT_TOPIC_PREFIX}${sessionID}`;
38395
+ const marked = unstarted.includes(own) && !started(cursors.get(own));
38396
+ if (marked) {
38397
+ cursors.set(own, { from_start: now() });
38398
+ changed = true;
38399
+ }
38400
+ 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);
38401
+ break;
38402
+ }
38403
+ if (!alive())
38404
+ return;
38405
+ const { stream } = response;
38406
+ if (streamReplaced(stream))
38407
+ break;
38408
+ streamID = stream.id ?? streamID;
38409
+ streamCreated = stream.created;
38410
+ const at = now();
38411
+ for (const result of response.subjects) {
38412
+ if (result.refused !== undefined || started(cursors.get(result.subject)))
38413
+ continue;
38414
+ cursors.set(result.subject, { since: result.next_after, after: result.next_after });
38415
+ cursorStarts.set(result.subject, at);
38416
+ changed = true;
38417
+ }
38591
38418
  }
38592
- return JSON.stringify({
38593
- ...streamID === undefined ? {} : { stream_id: streamID },
38594
- ...streamCreated === undefined ? {} : { stream_created: streamCreated },
38595
- subjects,
38596
- ...pending.size === 0 ? {} : { notices: [...pending].sort() }
38597
- });
38419
+ if (changed)
38420
+ await flush(alive, true);
38598
38421
  };
38599
- const record2 = (identity) => {
38600
- const entry = ledger.record(identity);
38601
- if (entry !== undefined)
38602
- unwritten.push(entry);
38422
+ const startCursors = async () => {
38423
+ while (running !== undefined)
38424
+ await running;
38425
+ running = startOnce().catch((error) => log("replay: starting the cursors failed", error)).finally(() => {
38426
+ running = undefined;
38427
+ });
38428
+ await running;
38603
38429
  };
38604
- const flush = (alive, atOnce = false) => {
38605
- writes = writes.then(async () => {
38606
- if (!alive())
38430
+ return {
38431
+ sweep,
38432
+ startCursors,
38433
+ startPending: () => [...cursors.values()].some((cursor) => cursor.from_start !== undefined),
38434
+ setConsumerMode(on) {
38435
+ consumerMode = on;
38436
+ },
38437
+ async followedChanged() {
38438
+ const alive = bind()?.alive;
38439
+ if (alive === undefined)
38607
38440
  return;
38608
- const json = stateJSON();
38609
- const delivered = unwritten;
38610
- if (json === writtenState && delivered.length === 0)
38441
+ try {
38442
+ syncFollowed();
38443
+ await flush(alive);
38444
+ } catch (error) {
38445
+ log("replay: could not take the followed subjects", error);
38446
+ }
38447
+ },
38448
+ delivered: (frame) => ledger.has(replayLedgerIdentity(frame)),
38449
+ async recordDelivered(frame) {
38450
+ const alive = bind()?.alive;
38451
+ if (alive === undefined)
38611
38452
  return;
38612
- const shape = stateJSON(false);
38613
- if (delivered.length === 0 && !atOnce && shape === writtenShape && writtenAt !== undefined && now() - writtenAt < REPLAY_STATE_REFRESH_MS) {
38453
+ record2(replayLedgerIdentity(frame));
38454
+ await flush(alive);
38455
+ },
38456
+ async recordDeliveredFor(sessionID, frame) {
38457
+ const alive = bindTo(sessionID, writeSession)?.alive;
38458
+ if (alive === undefined)
38459
+ return;
38460
+ record2(replayLedgerIdentity(frame));
38461
+ await flush(alive);
38462
+ },
38463
+ async liveFailed(followed, envelope, error, receivedAt) {
38464
+ const started = cursorStarts.get(followed);
38465
+ if (!isRoleForward(envelope) && answered.has(followed) && cursors.get(followed)?.after !== undefined && (started === undefined || started <= receivedAt)) {
38614
38466
  return;
38615
38467
  }
38616
- unwritten = [];
38617
- const state = json === writtenState ? undefined : JSON.parse(json);
38468
+ const alive = bind()?.alive;
38469
+ if (alive === undefined)
38470
+ return;
38618
38471
  try {
38619
- await host.write({
38620
- delivered,
38621
- state,
38622
- snapshot: () => ({ state: JSON.parse(json), ledger: ledger.list() })
38623
- });
38624
- if (state !== undefined) {
38625
- writtenState = json;
38626
- writtenShape = shape;
38627
- writtenAt = now();
38628
- }
38629
- } catch (error) {
38630
- unwritten = [...delivered, ...unwritten];
38631
- log("replay: could not persist the replay state; the next write carries it", error);
38472
+ await showFailureNotice(failureNotice(envelope, inboundTimestamp(envelope.issued_at), messageFor(error)), alive);
38473
+ } catch (thrown) {
38474
+ log("replay: could not report a failed live frame", thrown);
38632
38475
  }
38633
- });
38634
- return writes;
38635
- };
38636
- const syncFollowed = () => {
38637
- const followed = new Set(host.followedSubjects());
38638
- for (const subject of [...cursors.keys()]) {
38639
- if (!followed.has(subject)) {
38640
- cursors.delete(subject);
38641
- cursorStarts.delete(subject);
38476
+ },
38477
+ followedBefore: (topic, seq) => {
38478
+ const since = cursors.get(topic)?.since;
38479
+ return since !== undefined && seq > since;
38480
+ },
38481
+ restore(snapshot) {
38482
+ epoch += 1;
38483
+ ledger.restore(snapshot.ledger);
38484
+ cursors.clear();
38485
+ cursorStarts.clear();
38486
+ streamID = snapshot.state?.stream_id;
38487
+ streamCreated = snapshot.state?.stream_created;
38488
+ pending.clear();
38489
+ for (const kind of snapshot.state?.notices ?? []) {
38490
+ const known = ReplayPendingNoticeSchema.safeParse(kind);
38491
+ if (known.success)
38492
+ pending.add(known.data);
38642
38493
  }
38643
- }
38644
- for (const subject of followed) {
38645
- if (!cursors.has(subject))
38646
- cursors.set(subject, {});
38647
- }
38494
+ for (const [subject, cursor] of Object.entries(snapshot.state?.subjects ?? {})) {
38495
+ cursors.set(subject, { ...cursor });
38496
+ }
38497
+ writtenState = snapshot.state === undefined ? undefined : stateJSON();
38498
+ writtenShape = snapshot.state === undefined ? undefined : stateJSON(false);
38499
+ writtenAt = undefined;
38500
+ unwritten = [];
38501
+ failures.clear();
38502
+ listenerNoticed = undefined;
38503
+ answered.clear();
38504
+ },
38505
+ state: () => JSON.parse(stateJSON())
38648
38506
  };
38649
- const failureNotice = (envelope, publishedAt, error, subject) => ({
38650
- eventId: envelope.event_id ?? "unknown",
38651
- sender: senderLabel(envelope),
38652
- summary: envelope.payload_summary ?? "unknown",
38653
- publishedAt,
38654
- error,
38655
- readWith: envelope.source === "dispatch" ? "dispatch read" : "envoy_inbox",
38656
- ...subject === undefined ? {} : { subject },
38657
- envelope
38658
- });
38659
- const showFailureNotice = async (notice, alive) => {
38660
- let queued;
38661
- try {
38662
- queued = await host.showFailureNotice(notice) === "queued";
38663
- } catch (error) {
38664
- if (!alive())
38665
- return "stopped";
38666
- safely("failure notice warning", () => host.failureNoticeFailed(notice, error));
38667
- return "hole";
38668
- }
38669
- if (!alive())
38670
- return "stopped";
38671
- if (queued)
38672
- return "hole";
38673
- record2(replayLedgerIdentity(notice.envelope));
38674
- await flush(alive);
38675
- return "done";
38507
+ }
38508
+ function replaySuppressed(rendered) {
38509
+ return rendered.skip || rendered.malformedDelivery === true;
38510
+ }
38511
+ var REPLAYED_SUFFIX = "while this session's connection was down.";
38512
+ function replayedText(content, publishedAt) {
38513
+ return `${content}
38514
+ Published at ${publishedAt} ${REPLAYED_SUFFIX}`;
38515
+ }
38516
+ function failureNoticeFields(notice) {
38517
+ 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.";
38518
+ return {
38519
+ notice: "message not shown",
38520
+ event_id: notice.eventId,
38521
+ from: notice.sender,
38522
+ summary: notice.summary,
38523
+ published_at: notice.publishedAt,
38524
+ ...notice.subject === undefined ? {} : { subject: notice.subject },
38525
+ error: notice.error,
38526
+ detail: `This session could not be shown this message, so it is not coming back on its own. ${readWith}`
38676
38527
  };
38677
- const handle = async (seq, buffered, alive) => {
38678
- const { message } = buffered;
38679
- const envelope = parseEnvelope(message.data);
38680
- if (envelope === undefined) {
38681
- log(`replay: skipping stored message ${seq} on ${message.subject}: not an Envoy envelope`);
38682
- return "done";
38683
- }
38684
- if (isRoleForward(envelope))
38685
- return "done";
38686
- const identity = replayLedgerIdentity(envelope);
38687
- if (ledger.has(identity)) {
38688
- failures.delete(seq);
38689
- return "done";
38690
- }
38691
- if (dedupe?.pending(envelope) === true)
38692
- return "hole";
38693
- if (!alive())
38694
- return "stopped";
38695
- const giveUp = async (error) => {
38696
- const handled = await showFailureNotice(failureNotice(envelope, message.time, error, message.subject), alive);
38697
- if (handled === "done")
38698
- failures.delete(seq);
38699
- return handled;
38700
- };
38701
- const failure = failures.get(seq);
38702
- if (failure !== undefined && failure.count >= REPLAY_FAILURES_BEFORE_NOTICE) {
38703
- return giveUp(failure.error);
38704
- }
38705
- const followed = buffered.subjects[0] ?? message.subject;
38706
- let outcome;
38707
- let error = "the host could not show it";
38708
- try {
38709
- outcome = await host.deliver({
38710
- envelope,
38711
- data: message.data,
38712
- seq,
38713
- subject: message.subject,
38714
- followed,
38715
- time: message.time,
38716
- since: cursors.get(followed)?.since
38717
- });
38718
- } catch (thrown) {
38719
- outcome = "failed";
38720
- error = messageFor(thrown);
38528
+ }
38529
+ var REPLAY_NOTICE_NAMES = {
38530
+ stream_recreated: "stream recreated",
38531
+ stream_moved_back: "stream moved back"
38532
+ };
38533
+ function replayNoticeFields(notice) {
38534
+ return {
38535
+ notice: REPLAY_NOTICE_NAMES[notice.kind] ?? "messages may be missing",
38536
+ detail: notice.message
38537
+ };
38538
+ }
38539
+ function envoyNoticeText(fields) {
38540
+ return encode5({ envoy: fields });
38541
+ }
38542
+ var PRIVATE_STREAM_SUBJECTS = [
38543
+ `${AGENT_TOPIC_PREFIX}>`,
38544
+ `${LEGION_NOTICE_TOPIC_PREFIX}>`,
38545
+ `${EXCEPTION_TOPIC_PREFIX}${AGENT_TOPIC_PREFIX}>`
38546
+ ];
38547
+ function subjectsOverlap(a, b) {
38548
+ const aTokens = a.split(".");
38549
+ const bTokens = b.split(".");
38550
+ for (let index = 0;index < aTokens.length && index < bTokens.length; index += 1) {
38551
+ const aToken = aTokens[index];
38552
+ const bToken = bTokens[index];
38553
+ if (aToken === ">" || bToken === ">")
38554
+ return true;
38555
+ if (aToken !== "*" && bToken !== "*" && aToken !== bToken)
38556
+ return false;
38557
+ }
38558
+ return aTokens.length === bTokens.length;
38559
+ }
38560
+ function reachesPrivateStreamSubject(ownAgentSubject, topic) {
38561
+ if (!PRIVATE_STREAM_SUBJECTS.some((subject) => subjectsOverlap(topic, subject)))
38562
+ return false;
38563
+ return topic !== ownAgentSubject || /[*>]/.test(ownAgentSubject);
38564
+ }
38565
+ function replayFollowedSubjects(ownAgentSubject, topics, heldForRole = () => false) {
38566
+ const followed = [...topics].filter((topic) => !topic.startsWith(ROLE_TOPIC_PREFIX) && !reachesPrivateStreamSubject(ownAgentSubject, topic) && !heldForRole(topic));
38567
+ return [...new Set([ownAgentSubject, ...followed])];
38568
+ }
38569
+ function rememberedTopics(state) {
38570
+ return Object.keys(state?.subjects ?? {}).filter((topic) => !topic.startsWith(AGENT_TOPIC_PREFIX) && !topic.startsWith(ROLE_TOPIC_PREFIX));
38571
+ }
38572
+ async function closeReplayedRemoval(frame, host) {
38573
+ const closed = (subscriptionRemovedTopics(frame.data, host.sessionID) ?? []).filter((topic) => host.engine.followedBefore(topic, frame.seq) && host.close(topic));
38574
+ if (closed.length === 0)
38575
+ return closed;
38576
+ const unregistering = host.unregister(closed);
38577
+ await host.followedChanged();
38578
+ await unregistering;
38579
+ return closed;
38580
+ }
38581
+ function routeReplayNotice(notice, routes) {
38582
+ if (notice.kind === "not_served" || notice.kind === "unavailable") {
38583
+ routes.warn(notice.message);
38584
+ return;
38585
+ }
38586
+ routes.card(envoyNoticeText(replayNoticeFields(notice)));
38587
+ }
38588
+ function isRoleForward(envelope) {
38589
+ return envelope.dedupe_key?.startsWith(ROLE_FORWARD_KEY_PREFIX) === true || envelope.topic?.startsWith(ROLE_TOPIC_PREFIX) === true;
38590
+ }
38591
+ function replayLedgerIdentity(frame) {
38592
+ return {
38593
+ ...frame.event_id === undefined || frame.event_id === "" ? {} : { event_id: frame.event_id },
38594
+ ...frame.dedupe_key === undefined || !dedupeKeyNamesItsEvent(frame) ? {} : { dedupe_key: frame.dedupe_key }
38595
+ };
38596
+ }
38597
+ var StoredEnvelopeSchema = EnvelopeSchema.extend({
38598
+ expects_reply: InboundExpectsReplySchema.optional()
38599
+ });
38600
+ function parseEnvelope(data) {
38601
+ let parsed;
38602
+ try {
38603
+ parsed = JSON.parse(data);
38604
+ } catch {
38605
+ return;
38606
+ }
38607
+ const result = StoredEnvelopeSchema.safeParse(parsed);
38608
+ return result.success ? result.data : undefined;
38609
+ }
38610
+ function createLedger(now) {
38611
+ let entries = new Set;
38612
+ const byEvent = new Map;
38613
+ const byKey = new Map;
38614
+ const drop = (entry) => {
38615
+ entries.delete(entry);
38616
+ if (entry.event_id !== undefined && byEvent.get(entry.event_id) === entry) {
38617
+ byEvent.delete(entry.event_id);
38721
38618
  }
38722
- if (!alive())
38723
- return "stopped";
38724
- if (outcome === "shown") {
38725
- failures.delete(seq);
38726
- record2(identity);
38727
- await flush(alive);
38728
- return "done";
38619
+ if (entry.dedupe_key !== undefined && byKey.get(entry.dedupe_key) === entry) {
38620
+ byKey.delete(entry.dedupe_key);
38729
38621
  }
38730
- if (outcome === "suppressed") {
38731
- failures.delete(seq);
38732
- return "done";
38622
+ };
38623
+ const add = (entry) => {
38624
+ if (entries.size >= REPLAY_LEDGER_LIMIT) {
38625
+ const oldest = entries.values().next();
38626
+ if (oldest.done !== true)
38627
+ drop(oldest.value);
38733
38628
  }
38734
- if (outcome === "queued") {
38735
- failures.delete(seq);
38736
- return "hole";
38629
+ entries.add(entry);
38630
+ if (entry.event_id !== undefined)
38631
+ byEvent.set(entry.event_id, entry);
38632
+ if (entry.dedupe_key !== undefined)
38633
+ byKey.set(entry.dedupe_key, entry);
38634
+ };
38635
+ const expire = () => {
38636
+ const at = now();
38637
+ for (const entry of entries) {
38638
+ if (at - entry.at < REPLAY_LEDGER_TTL_MS)
38639
+ break;
38640
+ drop(entry);
38737
38641
  }
38738
- const count = (failure?.count ?? 0) + 1;
38739
- failures.set(seq, { count, error });
38740
- if (count >= REPLAY_FAILURES_BEFORE_NOTICE)
38741
- return giveUp(error);
38742
- log(`replay: could not show stored message ${seq} (${envelope.event_id}): ${error}`);
38743
- return "hole";
38744
38642
  };
38745
- const listenerFailed = (error) => {
38746
- if (error instanceof EnvoyApiError) {
38747
- const status = error.details.status;
38748
- if (status === 404) {
38749
- if (listenerNoticed === "not_served")
38750
- return;
38751
- listenerNoticed = "not_served";
38752
- notify({
38753
- kind: "not_served",
38754
- 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."
38755
- });
38643
+ const has = (identity) => {
38644
+ expire();
38645
+ return identity.event_id !== undefined && byEvent.has(identity.event_id) || identity.dedupe_key !== undefined && byKey.has(identity.dedupe_key);
38646
+ };
38647
+ return {
38648
+ has,
38649
+ record(identity) {
38650
+ if (identity.event_id === undefined && identity.dedupe_key === undefined)
38756
38651
  return;
38757
- }
38758
- if (status === 503 && error.code === REPLAY_UNAVAILABLE_CODE || status === 401 || status === 403) {
38759
- const key = `unavailable:${status}:${error.message}`;
38760
- if (listenerNoticed === key)
38761
- return;
38762
- listenerNoticed = key;
38763
- notify({
38764
- kind: "unavailable",
38765
- status,
38766
- error: error.message,
38767
- message: `Envoy replay is unavailable (${status}: ${error.message}), so messages published while this session's connection is down are not recovered until it answers.`
38768
- });
38652
+ if (has(identity))
38769
38653
  return;
38770
- }
38654
+ const entry = { ...identity, at: now() };
38655
+ add(entry);
38656
+ return entry;
38657
+ },
38658
+ restore(restored) {
38659
+ entries = new Set;
38660
+ byEvent.clear();
38661
+ byKey.clear();
38662
+ const at = now();
38663
+ 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);
38664
+ for (const entry of kept)
38665
+ add({ ...entry });
38666
+ },
38667
+ list() {
38668
+ expire();
38669
+ return [...entries];
38670
+ }
38671
+ };
38672
+ }
38673
+
38674
+ // ../envoy-client/src/consumer-delivery.ts
38675
+ var DELIVER_SUBJECT_PREFIX = "notifications.envoy.deliver.";
38676
+ function deliverSubject(sessionID) {
38677
+ return `${DELIVER_SUBJECT_PREFIX}${sessionID}`;
38678
+ }
38679
+ var RelayHeader = {
38680
+ subject: "Envoy-Subject",
38681
+ streamSeq: "Envoy-Stream-Seq",
38682
+ streamID: "Envoy-Stream-Id",
38683
+ delivered: "Envoy-Delivered",
38684
+ storedAt: "Envoy-Stored-At",
38685
+ gap: "Envoy-Gap"
38686
+ };
38687
+ var RELAY_PROGRESS_INTERVAL_MS = 20000;
38688
+ var FOLLOW_CLOCK_ALLOWANCE_MS = 2000;
38689
+ var RELAY_ATTEMPTS_BEFORE_FAILED = 3;
38690
+ var decoder4 = new TextDecoder;
38691
+ var DECIMAL = /^[0-9]+$/;
38692
+ var RFC3339 = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d+))?(Z|[+-]\d{2}:\d{2})$/;
38693
+ function positiveInteger(text) {
38694
+ if (text === null || text === undefined || !DECIMAL.test(text))
38695
+ return;
38696
+ const value = Number(text);
38697
+ return Number.isSafeInteger(value) && value > 0 ? value : undefined;
38698
+ }
38699
+ function parseStoredAt(text) {
38700
+ const match = RFC3339.exec(text);
38701
+ if (match === null)
38702
+ return;
38703
+ const fraction = (match[2] ?? "").padEnd(3, "0").slice(0, 3);
38704
+ const ms = Date.parse(`${match[1]}.${fraction}${match[3]}`);
38705
+ return Number.isNaN(ms) ? undefined : ms;
38706
+ }
38707
+ function decodeRelayFrame(message) {
38708
+ const reply = message.reply ?? "";
38709
+ if (reply === "")
38710
+ return { error: "no reply subject" };
38711
+ const headers = message.headers;
38712
+ if (headers === undefined)
38713
+ return { error: "no headers" };
38714
+ const subject = headers.get(RelayHeader.subject) ?? "";
38715
+ if (subject === "")
38716
+ return { error: `no ${RelayHeader.subject}` };
38717
+ const streamSeq = positiveInteger(headers.get(RelayHeader.streamSeq));
38718
+ if (streamSeq === undefined)
38719
+ return { error: `bad ${RelayHeader.streamSeq}` };
38720
+ const streamID = headers.get(RelayHeader.streamID) ?? "";
38721
+ if (streamID === "")
38722
+ return { error: `no ${RelayHeader.streamID}` };
38723
+ const delivered = positiveInteger(headers.get(RelayHeader.delivered));
38724
+ if (delivered === undefined)
38725
+ return { error: `bad ${RelayHeader.delivered}` };
38726
+ const storedAt = headers.get(RelayHeader.storedAt) ?? "";
38727
+ const storedAtMs = parseStoredAt(storedAt);
38728
+ if (storedAtMs === undefined)
38729
+ return { error: `bad ${RelayHeader.storedAt}` };
38730
+ return {
38731
+ frame: {
38732
+ subject,
38733
+ streamSeq,
38734
+ streamID,
38735
+ delivered,
38736
+ storedAt,
38737
+ storedAtMs,
38738
+ gap: headers.get(RelayHeader.gap) === "true",
38739
+ data: typeof message.data === "string" ? message.data : decoder4.decode(message.data),
38740
+ reply
38741
+ }
38742
+ };
38743
+ }
38744
+ function newEpoch() {
38745
+ return crypto.randomUUID();
38746
+ }
38747
+ var GAP_MESSAGES = {
38748
+ retention_gap: "The notification stream no longer holds some messages published to this session while it was away: messages older than 72 hours may be missing.",
38749
+ stream_recreated: "Envoy's notification stream was recreated, so messages published to this session before that may be lost."
38750
+ };
38751
+ var ConsumerDeliverySnapshotSchema = object({
38752
+ resume: object({ stream_id: string2().min(1), seq: number2().int().nonnegative() }).optional(),
38753
+ follows: record(string2(), number2()).default({})
38754
+ });
38755
+ function parseIdentity(data) {
38756
+ let parsed;
38757
+ try {
38758
+ parsed = JSON.parse(data);
38759
+ } catch {
38760
+ return;
38761
+ }
38762
+ const identity = DedupeIdentitySchema.safeParse(parsed);
38763
+ if (!identity.success)
38764
+ return;
38765
+ const { event_id, dedupe_key } = identity.data;
38766
+ return event_id === undefined && dedupe_key === undefined ? undefined : identity.data;
38767
+ }
38768
+ function reaches(topic, subject) {
38769
+ return expandSubscriptionTopics([topic]).some((pattern) => subjectsOverlap(pattern, subject));
38770
+ }
38771
+ function every(ms, callback) {
38772
+ const timer = setInterval(callback, ms);
38773
+ return () => clearInterval(timer);
38774
+ }
38775
+ function createConsumerDelivery(options) {
38776
+ const { host, ledger, sessionID } = options;
38777
+ const now = options.now ?? Date.now;
38778
+ const repeat = options.every ?? every;
38779
+ const ownSubject = agentSubject(sessionID);
38780
+ let currentEpoch = { id: newEpoch(), startedAt: now() };
38781
+ let follows = new Map;
38782
+ let streamID;
38783
+ let held = new Set;
38784
+ let written;
38785
+ let registeredResume;
38786
+ let gapTold = false;
38787
+ let closed = false;
38788
+ let relayed;
38789
+ let persisted = JSON.stringify({ follows: {} });
38790
+ const entries = new Map;
38791
+ const queue = [];
38792
+ let blocking;
38793
+ let pumping = false;
38794
+ const safely = (what, call) => {
38795
+ try {
38796
+ call();
38797
+ } catch (error) {
38798
+ try {
38799
+ host.log(`consumer delivery: ${what} failed`, error);
38800
+ } catch {}
38771
38801
  }
38772
- log("replay: the listener's replay failed; the next trigger tries again", error);
38773
38802
  };
38774
- const streamReplaced = (stream) => {
38775
- if (stream.id === undefined) {
38776
- return streamCreated !== undefined && streamCreated !== "" && stream.created !== streamCreated;
38803
+ const log = (message, error) => safely("log", () => host.log(`consumer delivery: ${message}`, error));
38804
+ const send = (reply, body) => safely(`reply ${body}`, () => options.publish(reply, body));
38805
+ const tellGap = (kind) => safely("gap notice", () => host.gap({ kind, message: GAP_MESSAGES[kind] }));
38806
+ const switchStream = (id) => {
38807
+ if (streamID === id)
38808
+ return;
38809
+ streamID = id;
38810
+ held = new Set;
38811
+ written = undefined;
38812
+ };
38813
+ const tellReplacedResume = (id) => {
38814
+ const asked = resumePoint() ?? registeredResume;
38815
+ if (asked === undefined || asked.stream_id === id)
38816
+ return;
38817
+ registeredResume = undefined;
38818
+ tellGap("stream_recreated");
38819
+ };
38820
+ const followedAt = (subject) => {
38821
+ if (subject === ownSubject)
38822
+ return Number.NEGATIVE_INFINITY;
38823
+ let earliest;
38824
+ for (const [topic, at] of follows) {
38825
+ if (!reaches(topic, subject))
38826
+ continue;
38827
+ if (earliest === undefined || at < earliest)
38828
+ earliest = at;
38777
38829
  }
38778
- return streamID !== undefined && streamID !== "" && stream.id !== streamID;
38830
+ return earliest;
38779
38831
  };
38780
- const streamMovedBack = (stream) => {
38781
- for (const cursor of cursors.values()) {
38782
- if (cursor.after !== undefined && cursor.after > stream.last_seq)
38832
+ const followedOnCorePath = (frame) => {
38833
+ const state = ledger.state();
38834
+ if (state.stream_id !== frame.streamID)
38835
+ return false;
38836
+ for (const [topic, cursor] of Object.entries(state.subjects)) {
38837
+ if (cursor.since === undefined || frame.streamSeq <= cursor.since)
38838
+ continue;
38839
+ if (reaches(topic, frame.subject))
38783
38840
  return true;
38784
38841
  }
38785
38842
  return false;
38786
38843
  };
38787
- const sweepOnce = async () => {
38788
- const binding = bind();
38789
- if (binding === undefined)
38844
+ const settle = (entry, body) => {
38845
+ if (entry.settled)
38790
38846
  return;
38791
- const { sessionID, alive } = binding;
38792
- syncFollowed();
38793
- const readings = new Map;
38794
- for (const [subject, cursor] of cursors) {
38795
- readings.set(subject, {
38796
- read: cursor.from_start === undefined ? cursor.after : 0,
38797
- more: true
38798
- });
38847
+ entry.settled = true;
38848
+ entry.stopProgress();
38849
+ entries.delete(entry.key);
38850
+ for (const reply of entry.replies)
38851
+ send(reply, body);
38852
+ if (body === "done" && entry.frame.streamID === streamID) {
38853
+ held.delete(entry.frame.streamSeq);
38854
+ written = Math.max(written ?? 0, entry.frame.streamSeq);
38799
38855
  }
38800
- const buffer = new Map;
38801
- const holes = new Map;
38802
- let gapNoticed = false;
38803
- if (pending.size > 0) {
38804
- for (const kind of [...pending].sort()) {
38805
- if (kind === "retention_gap")
38806
- gapNoticed = true;
38807
- tell(kind);
38808
- }
38809
- await flush(alive);
38810
- if (!alive())
38811
- return;
38856
+ release(entry);
38857
+ };
38858
+ const release = (entry) => {
38859
+ if (blocking !== entry)
38860
+ return;
38861
+ blocking = undefined;
38862
+ pump();
38863
+ };
38864
+ const alreadyWritten = (identity) => identity !== undefined && ledger.delivered(identity);
38865
+ const failed = (entry, error) => {
38866
+ if (entry.settled)
38867
+ return;
38868
+ if (entry.frame.delivered < RELAY_ATTEMPTS_BEFORE_FAILED) {
38869
+ log(`frame ${entry.frame.streamSeq} failed; it comes again`, error);
38870
+ settle(entry, "nak");
38871
+ return;
38812
38872
  }
38813
- if (consumerMode)
38873
+ safely("failure notice", () => {
38874
+ const { subject, data } = entry.frame;
38875
+ const { envelope } = renderInbound(data, sessionID, subject);
38876
+ if (envelope !== undefined)
38877
+ ledger.liveFailed(subject, envelope, error, now());
38878
+ });
38879
+ settle(entry, "done");
38880
+ };
38881
+ const handOff = (entry) => {
38882
+ const handle = {
38883
+ ...entry.frame,
38884
+ late: entry.late,
38885
+ done: () => settle(entry, "done"),
38886
+ nak: () => settle(entry, "nak"),
38887
+ release: () => release(entry)
38888
+ };
38889
+ blocking = entry;
38890
+ let result;
38891
+ try {
38892
+ result = host.deliver(handle);
38893
+ } catch (error) {
38894
+ failed(entry, error);
38895
+ release(entry);
38814
38896
  return;
38815
- for (let page = 0;page < REPLAY_PAGES_PER_SWEEP; page += 1) {
38816
- let handledAny = false;
38817
- const asking = [...readings].filter(([, reading]) => reading.more).sort(([, a], [, b]) => (a.read ?? -1) - (b.read ?? -1)).slice(0, REPLAY_MAX_SUBJECTS);
38818
- if (asking.length === 0)
38819
- break;
38820
- let response;
38821
- try {
38822
- response = await transport.replay({
38823
- sessionID,
38824
- subjects: asking.map(([subject, reading]) => reading.read === undefined ? { subject } : { subject, after: reading.read }),
38825
- ...options.maxMessages === undefined ? {} : { maxMessages: options.maxMessages }
38826
- });
38827
- } catch (error) {
38828
- if (alive()) {
38829
- answered.clear();
38830
- listenerFailed(error);
38831
- }
38832
- return;
38833
- }
38834
- if (!alive())
38835
- return;
38836
- listenerNoticed = undefined;
38837
- for (const result of response.subjects) {
38838
- if (result.refused === undefined)
38839
- answered.add(result.subject);
38840
- else
38841
- answered.delete(result.subject);
38842
- }
38843
- const { stream } = response;
38844
- const replaced = streamReplaced(stream);
38845
- streamID = stream.id ?? (replaced ? undefined : streamID);
38846
- streamCreated = stream.created;
38847
- if (replaced) {
38848
- const at = now();
38849
- for (const subject of cursors.keys()) {
38850
- cursors.set(subject, { since: stream.last_seq, after: stream.last_seq });
38851
- cursorStarts.set(subject, at);
38852
- }
38853
- failures.clear();
38854
- pending.add("stream_recreated");
38855
- await flush(alive);
38856
- if (!alive())
38857
- return;
38858
- tell("stream_recreated");
38859
- await flush(alive);
38860
- return;
38861
- }
38862
- if (streamMovedBack(stream)) {
38863
- const from = Math.max(0, stream.first_seq - 1);
38864
- for (const [subject, cursor] of cursors) {
38865
- if (cursor.after === undefined)
38866
- continue;
38867
- cursors.set(subject, { ...cursor, after: from });
38868
- const reading = readings.get(subject);
38869
- if (reading !== undefined) {
38870
- reading.read = from;
38871
- reading.more = true;
38872
- }
38873
- }
38874
- buffer.clear();
38875
- holes.clear();
38876
- failures.clear();
38877
- pending.add("stream_moved_back");
38878
- await flush(alive, true);
38879
- if (!alive())
38880
- return;
38881
- tell("stream_moved_back");
38882
- await flush(alive);
38883
- if (!alive())
38884
- return;
38885
- continue;
38886
- }
38887
- for (const [, reading] of asking)
38888
- reading.more = false;
38889
- for (const result of response.subjects) {
38890
- const reading = readings.get(result.subject);
38891
- const markedAt = cursors.get(result.subject)?.from_start;
38892
- if (reading === undefined || markedAt === undefined || result.refused !== undefined) {
38893
- continue;
38894
- }
38895
- const start = Math.max(0, stream.first_seq - 1);
38896
- cursors.set(result.subject, { since: start, after: start });
38897
- cursorStarts.set(result.subject, now());
38898
- if (now() - markedAt < DELIVERY_DUPLICATE_WINDOW_MS)
38899
- reading.read = start;
38900
- }
38901
- let gapped = false;
38902
- for (const result of response.subjects) {
38903
- const reading = readings.get(result.subject);
38904
- if (reading?.read === undefined || result.refused !== undefined)
38905
- continue;
38906
- if (reading.read >= stream.first_seq - 1)
38907
- continue;
38908
- gapped = true;
38909
- reading.read = stream.first_seq - 1;
38910
- const cursor = cursors.get(result.subject);
38911
- if (cursor?.after !== undefined && cursor.after < reading.read) {
38912
- cursors.set(result.subject, { ...cursor, after: reading.read });
38913
- }
38914
- }
38915
- if (gapped) {
38916
- if (!gapNoticed)
38917
- pending.add("retention_gap");
38918
- await flush(alive, true);
38919
- if (!alive())
38897
+ }
38898
+ if (!(result instanceof Promise)) {
38899
+ release(entry);
38900
+ return;
38901
+ }
38902
+ result.then(() => release(entry), (error) => {
38903
+ failed(entry, error);
38904
+ release(entry);
38905
+ });
38906
+ };
38907
+ function pump() {
38908
+ if (pumping)
38909
+ return;
38910
+ pumping = true;
38911
+ try {
38912
+ while (blocking === undefined && !closed) {
38913
+ const entry = queue.shift();
38914
+ if (entry === undefined)
38920
38915
  return;
38921
- if (pending.has("retention_gap")) {
38922
- gapNoticed = true;
38923
- tell("retention_gap");
38924
- await flush(alive);
38925
- }
38926
- }
38927
- for (const result of response.subjects) {
38928
- const reading = readings.get(result.subject);
38929
- if (reading === undefined || result.refused !== undefined)
38930
- continue;
38931
- if (reading.read === undefined) {
38932
- const cursor = cursors.get(result.subject);
38933
- if (cursor !== undefined && cursor.after === undefined) {
38934
- cursors.set(result.subject, { since: result.next_after, after: result.next_after });
38935
- cursorStarts.set(result.subject, now());
38936
- }
38937
- reading.read = result.next_after;
38938
- continue;
38939
- }
38940
- for (const message of result.messages) {
38941
- if (message.seq <= reading.read)
38942
- continue;
38943
- const buffered = buffer.get(message.seq);
38944
- if (buffered === undefined) {
38945
- buffer.set(message.seq, { message, subjects: [result.subject] });
38946
- } else if (!buffered.subjects.includes(result.subject)) {
38947
- buffered.subjects.push(result.subject);
38948
- }
38949
- }
38950
- reading.read = Math.max(reading.read, result.next_after);
38951
- reading.more = result.more;
38952
- }
38953
- let bound = Number.POSITIVE_INFINITY;
38954
- for (const reading of readings.values()) {
38955
- if (reading.more && reading.read !== undefined)
38956
- bound = Math.min(bound, reading.read);
38957
- }
38958
- const ready = [...buffer.keys()].filter((seq) => seq <= bound).sort((a, b) => a - b);
38959
- for (const seq of ready) {
38960
- const buffered = buffer.get(seq);
38961
- if (buffered === undefined)
38916
+ if (entry.settled)
38962
38917
  continue;
38963
- buffer.delete(seq);
38964
- if (!buffered.subjects.some((subject) => cursors.has(subject)))
38918
+ if (alreadyWritten(entry.identity)) {
38919
+ settle(entry, "done");
38965
38920
  continue;
38966
- const handled = await handle(seq, buffered, alive);
38967
- if (handled === "stopped")
38968
- return;
38969
- if (handled === "done")
38970
- handledAny = true;
38971
- if (handled === "hole") {
38972
- for (const subject of buffered.subjects) {
38973
- if (!holes.has(subject))
38974
- holes.set(subject, seq);
38975
- }
38976
38921
  }
38922
+ handOff(entry);
38977
38923
  }
38978
- if (!alive())
38979
- return;
38980
- for (const [subject, reading] of readings) {
38981
- const cursor = cursors.get(subject);
38982
- if (cursor?.after === undefined || reading.read === undefined)
38983
- continue;
38984
- let after = Math.min(reading.read, bound);
38985
- const hole = holes.get(subject);
38986
- if (hole !== undefined)
38987
- after = Math.min(after, hole - 1);
38988
- if (after > cursor.after)
38989
- cursors.set(subject, { ...cursor, after });
38924
+ } finally {
38925
+ pumping = false;
38926
+ }
38927
+ }
38928
+ const receive = (message) => {
38929
+ const decoded = decodeRelayFrame(message);
38930
+ if ("error" in decoded) {
38931
+ log(`dropping an undecodable relay frame: ${decoded.error}`);
38932
+ if (message.reply !== undefined && message.reply !== "")
38933
+ send(message.reply, "done");
38934
+ return;
38935
+ }
38936
+ const { frame } = decoded;
38937
+ if (closed) {
38938
+ send(frame.reply, "nak");
38939
+ return;
38940
+ }
38941
+ send(frame.reply, "received");
38942
+ if (frame.gap && !gapTold)
38943
+ tellGap("retention_gap");
38944
+ gapTold = false;
38945
+ if (frame.streamID !== streamID) {
38946
+ if (streamID !== undefined && (written !== undefined || held.size > 0)) {
38947
+ tellGap("stream_recreated");
38948
+ } else if (streamID === undefined) {
38949
+ tellReplacedResume(frame.streamID);
38990
38950
  }
38991
- await flush(alive, handledAny);
38992
- if (![...readings.values()].some((reading) => reading.more))
38993
- break;
38951
+ switchStream(frame.streamID);
38994
38952
  }
38995
- };
38996
- const runSweep = async () => {
38997
- try {
38998
- await sweepOnce();
38999
- } catch (error) {
39000
- log("replay: a sweep failed", error);
38953
+ const key = `${frame.streamID}:${frame.streamSeq}`;
38954
+ const first = entries.get(key);
38955
+ if (first !== undefined) {
38956
+ first.replies.push(frame.reply);
38957
+ return;
39001
38958
  }
39002
- };
39003
- const sweep = () => {
39004
- if (running === undefined) {
39005
- running = runSweep().finally(() => {
39006
- running = undefined;
39007
- });
39008
- return running;
38959
+ const since = followedAt(frame.subject);
38960
+ const unfollowed = (since === undefined || frame.storedAtMs < since - FOLLOW_CLOCK_ALLOWANCE_MS) && !followedOnCorePath(frame);
38961
+ const identity = unfollowed ? undefined : parseIdentity(frame.data);
38962
+ if (unfollowed || alreadyWritten(identity)) {
38963
+ send(frame.reply, "done");
38964
+ written = Math.max(written ?? 0, frame.streamSeq);
38965
+ return;
39009
38966
  }
39010
- rerun ??= running.then(() => {
39011
- rerun = undefined;
39012
- return sweep();
38967
+ held.add(frame.streamSeq);
38968
+ const entry = {
38969
+ frame,
38970
+ identity,
38971
+ late: frame.delivered > 1 || frame.storedAtMs < currentEpoch.startedAt,
38972
+ replies: [frame.reply],
38973
+ key,
38974
+ stopProgress: () => {
38975
+ return;
38976
+ },
38977
+ settled: false
38978
+ };
38979
+ entry.stopProgress = repeat(RELAY_PROGRESS_INTERVAL_MS, () => {
38980
+ if (entry.settled)
38981
+ return;
38982
+ for (const reply of entry.replies)
38983
+ send(reply, "progress");
39013
38984
  });
39014
- return rerun;
38985
+ entries.set(key, entry);
38986
+ queue.push(entry);
38987
+ pump();
39015
38988
  };
39016
- const started = (cursor) => cursor?.after !== undefined || cursor?.from_start !== undefined;
39017
- const startOnce = async () => {
39018
- const binding = bind();
39019
- if (binding === undefined)
38989
+ const resumePoint = () => {
38990
+ if (streamID === undefined)
39020
38991
  return;
39021
- const { sessionID, alive } = binding;
39022
- const unstarted = [...new Set(host.followedSubjects())].filter((subject) => !started(cursors.get(subject)));
39023
- let changed = false;
39024
- for (let index = 0;index < unstarted.length; index += REPLAY_MAX_SUBJECTS) {
39025
- let response;
39026
- try {
39027
- response = await transport.replay({
39028
- sessionID,
39029
- subjects: unstarted.slice(index, index + REPLAY_MAX_SUBJECTS).map((subject) => ({ subject })),
39030
- timeoutMs: START_CURSORS_TIMEOUT_MS
39031
- });
39032
- } catch (error) {
39033
- if (!alive())
39034
- return;
39035
- const own = `${AGENT_TOPIC_PREFIX}${sessionID}`;
39036
- const marked = unstarted.includes(own) && !started(cursors.get(own));
39037
- if (marked) {
39038
- cursors.set(own, { from_start: now() });
39039
- changed = true;
38992
+ if (held.size > 0)
38993
+ return { stream_id: streamID, seq: Math.min(...held) - 1 };
38994
+ return written === undefined ? undefined : { stream_id: streamID, seq: written };
38995
+ };
38996
+ const seededResume = () => {
38997
+ const state = ledger.state();
38998
+ const stream = state.stream_id;
38999
+ if (stream === undefined)
39000
+ return;
39001
+ let lowest;
39002
+ for (const [subject, cursor] of Object.entries(state.subjects)) {
39003
+ if (cursor.after === undefined || !streamStores(subject))
39004
+ continue;
39005
+ if (lowest === undefined || cursor.after < lowest)
39006
+ lowest = cursor.after;
39007
+ }
39008
+ return lowest === undefined ? undefined : { stream_id: stream, seq: lowest };
39009
+ };
39010
+ const snapshot = () => {
39011
+ const resume = resumePoint();
39012
+ return {
39013
+ ...resume === undefined ? {} : { resume },
39014
+ follows: Object.fromEntries(follows)
39015
+ };
39016
+ };
39017
+ const persist = () => {
39018
+ const current = snapshot();
39019
+ const serialized = JSON.stringify(current);
39020
+ if (serialized === persisted)
39021
+ return;
39022
+ if (!options.persist(current))
39023
+ return;
39024
+ persisted = serialized;
39025
+ };
39026
+ return {
39027
+ subject: deliverSubject(sessionID),
39028
+ receive,
39029
+ epoch: () => currentEpoch,
39030
+ beginEpoch() {
39031
+ currentEpoch = { id: newEpoch(), startedAt: now() };
39032
+ return currentEpoch;
39033
+ },
39034
+ registration() {
39035
+ const resume = resumePoint() ?? seededResume();
39036
+ registeredResume = resume;
39037
+ return {
39038
+ consumer: true,
39039
+ epoch: currentEpoch.id,
39040
+ ...resume === undefined ? {} : { resume }
39041
+ };
39042
+ },
39043
+ answered(answer, topics) {
39044
+ if (answer.mode === "consumer") {
39045
+ if (answer.stream_id !== "" && answer.stream_id !== streamID) {
39046
+ tellReplacedResume(answer.stream_id);
39047
+ switchStream(answer.stream_id);
39040
39048
  }
39041
- 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);
39042
- break;
39049
+ const floor = (answer.consumer_start ?? 0) - 1;
39050
+ if (answer.stream_id === streamID && floor > (written ?? -1))
39051
+ written = floor;
39052
+ if (answer.gap) {
39053
+ tellGap("retention_gap");
39054
+ gapTold = true;
39055
+ }
39056
+ relayed = new Set([...topics].filter(streamStores));
39057
+ } else {
39058
+ relayed = undefined;
39043
39059
  }
39044
- if (!alive())
39045
- return;
39046
- const { stream } = response;
39047
- if (streamReplaced(stream))
39048
- break;
39049
- streamID = stream.id ?? streamID;
39050
- streamCreated = stream.created;
39060
+ ledger.setConsumerMode(relayed !== undefined);
39061
+ persist();
39062
+ return answer.mode;
39063
+ },
39064
+ relays: (topic) => relayed?.has(topic) === true,
39065
+ relaying: () => relayed !== undefined,
39066
+ setFollowed(topics) {
39051
39067
  const at = now();
39052
- for (const result of response.subjects) {
39053
- if (result.refused !== undefined || started(cursors.get(result.subject)))
39068
+ const next = new Map;
39069
+ for (const topic of topics) {
39070
+ if (topic === ownSubject)
39054
39071
  continue;
39055
- cursors.set(result.subject, { since: result.next_after, after: result.next_after });
39056
- cursorStarts.set(result.subject, at);
39057
- changed = true;
39072
+ next.set(topic, follows.get(topic) ?? at);
39058
39073
  }
39074
+ follows = next;
39075
+ persist();
39076
+ },
39077
+ persist,
39078
+ resumePoint,
39079
+ snapshot,
39080
+ restore(restored) {
39081
+ const merged = new Map(Object.entries(restored.follows));
39082
+ for (const [topic, at] of follows)
39083
+ if (!merged.has(topic))
39084
+ merged.set(topic, at);
39085
+ follows = merged;
39086
+ streamID = restored.resume?.stream_id;
39087
+ held = new Set;
39088
+ written = restored.resume?.seq;
39089
+ persisted = JSON.stringify(snapshot());
39090
+ },
39091
+ close() {
39092
+ closed = true;
39093
+ relayed = undefined;
39094
+ ledger.setConsumerMode(false);
39095
+ queue.length = 0;
39096
+ for (const entry of [...entries.values()])
39097
+ settle(entry, "nak");
39098
+ blocking = undefined;
39059
39099
  }
39060
- if (changed)
39061
- await flush(alive, true);
39062
39100
  };
39063
- const startCursors = async () => {
39064
- while (running !== undefined)
39065
- await running;
39066
- running = startOnce().catch((error) => log("replay: starting the cursors failed", error)).finally(() => {
39067
- running = undefined;
39068
- });
39069
- await running;
39101
+ }
39102
+ var NOT_ASKED = { mode: "core", reason: "not_asked" };
39103
+ function registeredWithoutRelay(ledger, delivery, topics) {
39104
+ if (delivery === undefined) {
39105
+ ledger.setConsumerMode(false);
39106
+ return;
39107
+ }
39108
+ delivery.answered(NOT_ASKED, topics);
39109
+ }
39110
+
39111
+ // ../envoy-client/src/envoy-config.ts
39112
+ import {
39113
+ chmodSync,
39114
+ closeSync as closeSync2,
39115
+ existsSync,
39116
+ lstatSync,
39117
+ mkdtempSync,
39118
+ openSync as openSync2,
39119
+ readFileSync as readFileSync3,
39120
+ readlinkSync as readlinkSync2,
39121
+ rmSync as rmSync2,
39122
+ statSync as statSync2
39123
+ } from "fs";
39124
+ import { homedir, tmpdir } from "os";
39125
+ import * as path from "path";
39126
+
39127
+ // ../envoy-client/src/json-position.ts
39128
+ function jsonErrorPosition(text) {
39129
+ let at = 0;
39130
+ const fail = () => {
39131
+ throw new RangeError("not JSON");
39070
39132
  };
39071
- return {
39072
- sweep,
39073
- startCursors,
39074
- startPending: () => [...cursors.values()].some((cursor) => cursor.from_start !== undefined),
39075
- setConsumerMode(on) {
39076
- consumerMode = on;
39077
- },
39078
- async followedChanged() {
39079
- const alive = bind()?.alive;
39080
- if (alive === undefined)
39133
+ const space = () => {
39134
+ while (at < text.length && `
39135
+ \r`.includes(text.charAt(at)))
39136
+ at += 1;
39137
+ };
39138
+ const expect = (char) => {
39139
+ if (text.charAt(at) !== char)
39140
+ fail();
39141
+ at += 1;
39142
+ };
39143
+ const string = () => {
39144
+ expect('"');
39145
+ while (at < text.length) {
39146
+ const char = text.charAt(at);
39147
+ if (char === '"') {
39148
+ at += 1;
39081
39149
  return;
39082
- try {
39083
- syncFollowed();
39084
- await flush(alive);
39085
- } catch (error) {
39086
- log("replay: could not take the followed subjects", error);
39087
39150
  }
39088
- },
39089
- delivered: (frame) => ledger.has(replayLedgerIdentity(frame)),
39090
- async recordDelivered(frame) {
39091
- const alive = bind()?.alive;
39092
- if (alive === undefined)
39093
- return;
39094
- record2(replayLedgerIdentity(frame));
39095
- await flush(alive);
39096
- },
39097
- async recordDeliveredFor(sessionID, frame) {
39098
- const alive = bindTo(sessionID, writeSession)?.alive;
39099
- if (alive === undefined)
39100
- return;
39101
- record2(replayLedgerIdentity(frame));
39102
- await flush(alive);
39103
- },
39104
- async liveFailed(followed, envelope, error, receivedAt) {
39105
- const started = cursorStarts.get(followed);
39106
- if (!isRoleForward(envelope) && answered.has(followed) && cursors.get(followed)?.after !== undefined && (started === undefined || started <= receivedAt)) {
39151
+ if (char < " ")
39152
+ fail();
39153
+ at += char === "\\" ? 2 : 1;
39154
+ }
39155
+ fail();
39156
+ };
39157
+ const value = () => {
39158
+ space();
39159
+ const char = text.charAt(at);
39160
+ if (char === "{" || char === "[") {
39161
+ const close = char === "{" ? "}" : "]";
39162
+ at += 1;
39163
+ space();
39164
+ if (text.charAt(at) === close) {
39165
+ at += 1;
39107
39166
  return;
39108
39167
  }
39109
- const alive = bind()?.alive;
39110
- if (alive === undefined)
39168
+ for (;; ) {
39169
+ if (close === "}") {
39170
+ space();
39171
+ string();
39172
+ space();
39173
+ expect(":");
39174
+ }
39175
+ value();
39176
+ space();
39177
+ if (text.charAt(at) === ",") {
39178
+ at += 1;
39179
+ continue;
39180
+ }
39181
+ expect(close);
39111
39182
  return;
39112
- try {
39113
- await showFailureNotice(failureNotice(envelope, inboundTimestamp(envelope.issued_at), messageFor(error)), alive);
39114
- } catch (thrown) {
39115
- log("replay: could not report a failed live frame", thrown);
39116
- }
39117
- },
39118
- followedBefore: (topic, seq) => {
39119
- const since = cursors.get(topic)?.since;
39120
- return since !== undefined && seq > since;
39121
- },
39122
- restore(snapshot) {
39123
- epoch += 1;
39124
- ledger.restore(snapshot.ledger);
39125
- cursors.clear();
39126
- cursorStarts.clear();
39127
- streamID = snapshot.state?.stream_id;
39128
- streamCreated = snapshot.state?.stream_created;
39129
- pending.clear();
39130
- for (const kind of snapshot.state?.notices ?? []) {
39131
- const known = ReplayPendingNoticeSchema.safeParse(kind);
39132
- if (known.success)
39133
- pending.add(known.data);
39134
39183
  }
39135
- for (const [subject, cursor] of Object.entries(snapshot.state?.subjects ?? {})) {
39136
- cursors.set(subject, { ...cursor });
39184
+ }
39185
+ if (char === '"') {
39186
+ string();
39187
+ return;
39188
+ }
39189
+ for (const word of ["true", "false", "null"]) {
39190
+ if (text.startsWith(word, at)) {
39191
+ at += word.length;
39192
+ return;
39137
39193
  }
39138
- writtenState = snapshot.state === undefined ? undefined : stateJSON();
39139
- writtenShape = snapshot.state === undefined ? undefined : stateJSON(false);
39140
- writtenAt = undefined;
39141
- unwritten = [];
39142
- failures.clear();
39143
- listenerNoticed = undefined;
39144
- answered.clear();
39145
- },
39146
- state: () => JSON.parse(stateJSON())
39147
- };
39148
- }
39149
- function replaySuppressed(rendered) {
39150
- return rendered.skip || rendered.malformedDelivery === true;
39151
- }
39152
- var REPLAYED_SUFFIX = "while this session's connection was down.";
39153
- function replayedText(content, publishedAt) {
39154
- return `${content}
39155
- Published at ${publishedAt} ${REPLAYED_SUFFIX}`;
39156
- }
39157
- function failureNoticeFields(notice) {
39158
- 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.";
39159
- return {
39160
- notice: "message not shown",
39161
- event_id: notice.eventId,
39162
- from: notice.sender,
39163
- summary: notice.summary,
39164
- published_at: notice.publishedAt,
39165
- ...notice.subject === undefined ? {} : { subject: notice.subject },
39166
- error: notice.error,
39167
- detail: `This session could not be shown this message, so it is not coming back on its own. ${readWith}`
39168
- };
39169
- }
39170
- var REPLAY_NOTICE_NAMES = {
39171
- stream_recreated: "stream recreated",
39172
- stream_moved_back: "stream moved back"
39173
- };
39174
- function replayNoticeFields(notice) {
39175
- return {
39176
- notice: REPLAY_NOTICE_NAMES[notice.kind] ?? "messages may be missing",
39177
- detail: notice.message
39194
+ }
39195
+ const number = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/.exec(text.slice(at));
39196
+ if (number === null)
39197
+ fail();
39198
+ at += number?.[0].length ?? 0;
39178
39199
  };
39179
- }
39180
- function envoyNoticeText(fields) {
39181
- return encode5({ envoy: fields });
39182
- }
39183
- var PRIVATE_STREAM_SUBJECTS = [
39184
- `${AGENT_TOPIC_PREFIX}>`,
39185
- `${LEGION_NOTICE_TOPIC_PREFIX}>`,
39186
- `${EXCEPTION_TOPIC_PREFIX}${AGENT_TOPIC_PREFIX}>`
39187
- ];
39188
- function subjectsOverlap(a, b) {
39189
- const aTokens = a.split(".");
39190
- const bTokens = b.split(".");
39191
- for (let index = 0;index < aTokens.length && index < bTokens.length; index += 1) {
39192
- const aToken = aTokens[index];
39193
- const bToken = bTokens[index];
39194
- if (aToken === ">" || bToken === ">")
39195
- return true;
39196
- if (aToken !== "*" && bToken !== "*" && aToken !== bToken)
39197
- return false;
39200
+ try {
39201
+ value();
39202
+ space();
39203
+ if (at < text.length)
39204
+ fail();
39205
+ return;
39206
+ } catch {
39207
+ const before = text.slice(0, at).split(`
39208
+ `);
39209
+ return `line ${before.length}, column ${(before.at(-1)?.length ?? 0) + 1}`;
39198
39210
  }
39199
- return aTokens.length === bTokens.length;
39200
- }
39201
- function reachesPrivateStreamSubject(ownAgentSubject, topic) {
39202
- if (!PRIVATE_STREAM_SUBJECTS.some((subject) => subjectsOverlap(topic, subject)))
39203
- return false;
39204
- return topic !== ownAgentSubject || /[*>]/.test(ownAgentSubject);
39205
39211
  }
39206
- function replayFollowedSubjects(ownAgentSubject, topics, heldForRole = () => false) {
39207
- const followed = [...topics].filter((topic) => !topic.startsWith(ROLE_TOPIC_PREFIX) && !reachesPrivateStreamSubject(ownAgentSubject, topic) && !heldForRole(topic));
39208
- return [...new Set([ownAgentSubject, ...followed])];
39212
+
39213
+ // ../envoy-client/src/secret-file.ts
39214
+ import { readFileSync as readFileSync2 } from "fs";
39215
+ function sendable(value) {
39216
+ return !/[\s\p{Cc}]/u.test(value);
39209
39217
  }
39210
- function rememberedTopics(state) {
39211
- return Object.keys(state?.subjects ?? {}).filter((topic) => !topic.startsWith(AGENT_TOPIC_PREFIX) && !topic.startsWith(ROLE_TOPIC_PREFIX));
39218
+ function readSecretFile(variable, filePath) {
39219
+ let contents;
39220
+ try {
39221
+ contents = readFileSync2(filePath, "utf8");
39222
+ } catch (error) {
39223
+ throw new Error(`${variable} names ${filePath}, which could not be read: ${messageFor(error)}`);
39224
+ }
39225
+ const value = contents.trim();
39226
+ if (value.length === 0)
39227
+ throw new Error(`${variable} names ${filePath}, which is empty`);
39228
+ if (!sendable(value)) {
39229
+ throw new Error(`${variable} names ${filePath}, which holds whitespace or a control character`);
39230
+ }
39231
+ return value;
39212
39232
  }
39213
- async function closeReplayedRemoval(frame, host) {
39214
- const closed = (subscriptionRemovedTopics(frame.data, host.sessionID) ?? []).filter((topic) => host.engine.followedBefore(topic, frame.seq) && host.close(topic));
39215
- if (closed.length === 0)
39216
- return closed;
39217
- const unregistering = host.unregister(closed);
39218
- await host.followedChanged();
39219
- await unregistering;
39220
- return closed;
39233
+
39234
+ // ../envoy-client/src/envoy-config.ts
39235
+ var COMMAND_TIMEOUT_MS = 1e4;
39236
+ function nonEmpty(value) {
39237
+ return value === undefined || value === "" ? undefined : value;
39221
39238
  }
39222
- function routeReplayNotice(notice, routes) {
39223
- if (notice.kind === "not_served" || notice.kind === "unavailable") {
39224
- routes.warn(notice.message);
39225
- return;
39239
+ function absolute(name, value) {
39240
+ if (value !== undefined && !path.isAbsolute(value)) {
39241
+ throw new Error(`${name} is ${value}, which is not an absolute path`);
39226
39242
  }
39227
- routes.card(envoyNoticeText(replayNoticeFields(notice)));
39228
- }
39229
- function isRoleForward(envelope) {
39230
- return envelope.dedupe_key?.startsWith(ROLE_FORWARD_KEY_PREFIX) === true || envelope.topic?.startsWith(ROLE_TOPIC_PREFIX) === true;
39243
+ return value;
39231
39244
  }
39232
- function replayLedgerIdentity(frame) {
39233
- return {
39234
- ...frame.event_id === undefined || frame.event_id === "" ? {} : { event_id: frame.event_id },
39235
- ...frame.dedupe_key === undefined || !dedupeKeyNamesItsEvent(frame) ? {} : { dedupe_key: frame.dedupe_key }
39236
- };
39245
+ function neutralConfig(env) {
39246
+ const xdgConfigHome = nonEmpty(env.XDG_CONFIG_HOME);
39247
+ if (xdgConfigHome !== undefined && !path.isAbsolute(xdgConfigHome)) {
39248
+ return { error: `XDG_CONFIG_HOME is ${xdgConfigHome}, which is not an absolute path` };
39249
+ }
39250
+ const configHome = xdgConfigHome ?? path.join(nonEmpty(env.HOME) ?? homedir(), ".config");
39251
+ return { path: path.resolve(configHome, "legion", "envoy.json") };
39237
39252
  }
39238
- var StoredEnvelopeSchema = EnvelopeSchema.extend({
39239
- expects_reply: InboundExpectsReplySchema.optional()
39240
- });
39241
- function parseEnvelope(data) {
39242
- let parsed;
39243
- try {
39244
- parsed = JSON.parse(data);
39245
- } catch {
39246
- return;
39253
+ var HARNESS_DIRECTORY_VARIABLE = {
39254
+ omp: "PI_CODING_AGENT_DIR",
39255
+ claude: "CLAUDE_CONFIG_DIR"
39256
+ };
39257
+ var PLUGIN_HARNESSES = Object.keys(HARNESS_DIRECTORY_VARIABLE);
39258
+ function namedDirectory(harness, env) {
39259
+ if (harness === "omp") {
39260
+ const profile = nonEmpty(env.OMP_PROFILE);
39261
+ if (profile === undefined || profile === "default")
39262
+ return;
39247
39263
  }
39248
- const result = StoredEnvelopeSchema.safeParse(parsed);
39249
- return result.success ? result.data : undefined;
39264
+ return nonEmpty(env[HARNESS_DIRECTORY_VARIABLE[harness]]);
39250
39265
  }
39251
- function createLedger(now) {
39252
- let entries = new Set;
39253
- const byEvent = new Map;
39254
- const byKey = new Map;
39255
- const drop = (entry) => {
39256
- entries.delete(entry);
39257
- if (entry.event_id !== undefined && byEvent.get(entry.event_id) === entry) {
39258
- byEvent.delete(entry.event_id);
39259
- }
39260
- if (entry.dedupe_key !== undefined && byKey.get(entry.dedupe_key) === entry) {
39261
- byKey.delete(entry.dedupe_key);
39262
- }
39263
- };
39264
- const add = (entry) => {
39265
- if (entries.size >= REPLAY_LEDGER_LIMIT) {
39266
- const oldest = entries.values().next();
39267
- if (oldest.done !== true)
39268
- drop(oldest.value);
39269
- }
39270
- entries.add(entry);
39271
- if (entry.event_id !== undefined)
39272
- byEvent.set(entry.event_id, entry);
39273
- if (entry.dedupe_key !== undefined)
39274
- byKey.set(entry.dedupe_key, entry);
39275
- };
39276
- const expire = () => {
39277
- const at = now();
39278
- for (const entry of entries) {
39279
- if (at - entry.at < REPLAY_LEDGER_TTL_MS)
39280
- break;
39281
- drop(entry);
39282
- }
39283
- };
39284
- const has = (identity) => {
39285
- expire();
39286
- return identity.event_id !== undefined && byEvent.has(identity.event_id) || identity.dedupe_key !== undefined && byKey.has(identity.dedupe_key);
39287
- };
39288
- return {
39289
- has,
39290
- record(identity) {
39291
- if (identity.event_id === undefined && identity.dedupe_key === undefined)
39292
- return;
39293
- if (has(identity))
39294
- return;
39295
- const entry = { ...identity, at: now() };
39296
- add(entry);
39297
- return entry;
39298
- },
39299
- restore(restored) {
39300
- entries = new Set;
39301
- byEvent.clear();
39302
- byKey.clear();
39303
- const at = now();
39304
- 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);
39305
- for (const entry of kept)
39306
- add({ ...entry });
39307
- },
39308
- list() {
39309
- expire();
39310
- return [...entries];
39266
+ function harnessDirectory(harness, env) {
39267
+ for (const plugin of harness === "command" ? PLUGIN_HARNESSES : [harness]) {
39268
+ if (harness === "command" && plugin === "claude" && env.CLAUDE_CODE_CHILD_SESSION !== "1") {
39269
+ continue;
39311
39270
  }
39312
- };
39313
- }
39314
-
39315
- // ../envoy-client/src/consumer-delivery.ts
39316
- var DELIVER_SUBJECT_PREFIX = "notifications.envoy.deliver.";
39317
- function deliverSubject(sessionID) {
39318
- return `${DELIVER_SUBJECT_PREFIX}${sessionID}`;
39271
+ const directory = namedDirectory(plugin, env);
39272
+ if (directory !== undefined)
39273
+ return absolute(HARNESS_DIRECTORY_VARIABLE[plugin], directory);
39274
+ }
39275
+ return;
39319
39276
  }
39320
- var RelayHeader = {
39321
- subject: "Envoy-Subject",
39322
- streamSeq: "Envoy-Stream-Seq",
39323
- streamID: "Envoy-Stream-Id",
39324
- delivered: "Envoy-Delivered",
39325
- storedAt: "Envoy-Stored-At",
39326
- gap: "Envoy-Gap"
39327
- };
39328
- var RELAY_PROGRESS_INTERVAL_MS = 20000;
39329
- var FOLLOW_CLOCK_ALLOWANCE_MS = 2000;
39330
- var RELAY_ATTEMPTS_BEFORE_FAILED = 3;
39331
- var decoder4 = new TextDecoder;
39332
- var DECIMAL = /^[0-9]+$/;
39333
- var RFC3339 = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d+))?(Z|[+-]\d{2}:\d{2})$/;
39334
- function positiveInteger(text) {
39335
- if (text === null || text === undefined || !DECIMAL.test(text))
39336
- return;
39337
- const value = Number(text);
39338
- return Number.isSafeInteger(value) && value > 0 ? value : undefined;
39277
+ function handedConfigPath(env) {
39278
+ const handed = nonEmpty(env.ENVOY_CONFIG_FILE);
39279
+ return handed === undefined ? undefined : path.resolve(absolute("ENVOY_CONFIG_FILE", handed));
39339
39280
  }
39340
- function parseStoredAt(text) {
39341
- const match = RFC3339.exec(text);
39342
- if (match === null)
39343
- return;
39344
- const fraction = (match[2] ?? "").padEnd(3, "0").slice(0, 3);
39345
- const ms = Date.parse(`${match[1]}.${fraction}${match[3]}`);
39346
- return Number.isNaN(ms) ? undefined : ms;
39281
+ function envoyConfigPath(harness, env) {
39282
+ const handedFirst = harness === "command" ? handedConfigPath(env) : undefined;
39283
+ if (handedFirst !== undefined)
39284
+ return handedFirst;
39285
+ const directory = harnessDirectory(harness, env);
39286
+ if (directory !== undefined)
39287
+ return path.resolve(directory, "envoy.json");
39288
+ const handed = handedConfigPath(env);
39289
+ if (handed !== undefined)
39290
+ return handed;
39291
+ const neutral = neutralConfig(env);
39292
+ if ("error" in neutral)
39293
+ throw new Error(neutral.error);
39294
+ return neutral.path;
39347
39295
  }
39348
- function decodeRelayFrame(message) {
39349
- const reply = message.reply ?? "";
39350
- if (reply === "")
39351
- return { error: "no reply subject" };
39352
- const headers = message.headers;
39353
- if (headers === undefined)
39354
- return { error: "no headers" };
39355
- const subject = headers.get(RelayHeader.subject) ?? "";
39356
- if (subject === "")
39357
- return { error: `no ${RelayHeader.subject}` };
39358
- const streamSeq = positiveInteger(headers.get(RelayHeader.streamSeq));
39359
- if (streamSeq === undefined)
39360
- return { error: `bad ${RelayHeader.streamSeq}` };
39361
- const streamID = headers.get(RelayHeader.streamID) ?? "";
39362
- if (streamID === "")
39363
- return { error: `no ${RelayHeader.streamID}` };
39364
- const delivered = positiveInteger(headers.get(RelayHeader.delivered));
39365
- if (delivered === undefined)
39366
- return { error: `bad ${RelayHeader.delivered}` };
39367
- const storedAt = headers.get(RelayHeader.storedAt) ?? "";
39368
- const storedAtMs = parseStoredAt(storedAt);
39369
- if (storedAtMs === undefined)
39370
- return { error: `bad ${RelayHeader.storedAt}` };
39371
- return {
39372
- frame: {
39373
- subject,
39374
- streamSeq,
39375
- streamID,
39376
- delivered,
39377
- storedAt,
39378
- storedAtMs,
39379
- gap: headers.get(RelayHeader.gap) === "true",
39380
- data: typeof message.data === "string" ? message.data : decoder4.decode(message.data),
39381
- reply
39382
- }
39383
- };
39296
+ var httpUrlSchema = url({ protocol: /^https?$/, error: "must be an http or https URL" });
39297
+ var DispatchSectionSchema = strictObject({
39298
+ enabled: boolean2().optional(),
39299
+ serverUrl: httpUrlSchema.optional(),
39300
+ token: string2().optional()
39301
+ });
39302
+ var EnvoySectionSchema = strictObject({
39303
+ enabled: boolean2().optional(),
39304
+ url: httpUrlSchema.optional(),
39305
+ natsUrls: array(string2().trim().min(1, "must not be empty")).optional(),
39306
+ token: string2().optional(),
39307
+ natsSeed: string2().optional()
39308
+ });
39309
+ var EnvoyFileSchema = looseObject({
39310
+ $schema: string2().optional(),
39311
+ dispatch: DispatchSectionSchema.optional(),
39312
+ envoy: EnvoySectionSchema.optional()
39313
+ });
39314
+ var CheckoutFileSchema = looseObject({
39315
+ $schema: string2().optional(),
39316
+ dispatch: DispatchSectionSchema.optional()
39317
+ });
39318
+ var AskgateSectionSchema = strictObject({ enabled: boolean2().optional() });
39319
+ function offWithNotice(notice) {
39320
+ return { enabled: false, notice, error: null };
39384
39321
  }
39385
- function newEpoch() {
39386
- return crypto.randomUUID();
39322
+ function offWithError(error) {
39323
+ return { enabled: false, notice: null, error };
39387
39324
  }
39388
- var GAP_MESSAGES = {
39389
- retention_gap: "The notification stream no longer holds some messages published to this session while it was away: messages older than 72 hours may be missing.",
39390
- stream_recreated: "Envoy's notification stream was recreated, so messages published to this session before that may be lost."
39391
- };
39392
- var ConsumerDeliverySnapshotSchema = object({
39393
- resume: object({ stream_id: string2().min(1), seq: number2().int().nonnegative() }).optional(),
39394
- follows: record(string2(), number2()).default({})
39395
- });
39396
- function parseIdentity(data) {
39397
- let parsed;
39325
+ function httpUrl2(value) {
39326
+ let url2;
39398
39327
  try {
39399
- parsed = JSON.parse(data);
39328
+ url2 = new URL(value);
39400
39329
  } catch {
39401
39330
  return;
39402
39331
  }
39403
- const identity = DedupeIdentitySchema.safeParse(parsed);
39404
- if (!identity.success)
39405
- return;
39406
- const { event_id, dedupe_key } = identity.data;
39407
- return event_id === undefined && dedupe_key === undefined ? undefined : identity.data;
39332
+ return url2.protocol === "http:" || url2.protocol === "https:" ? url2 : undefined;
39408
39333
  }
39409
- function reaches(topic, subject) {
39410
- return expandSubscriptionTopics([topic]).some((pattern) => subjectsOverlap(pattern, subject));
39334
+ function describeSchemaIssue(filePath, error) {
39335
+ const issue = error.issues[0];
39336
+ if (!issue)
39337
+ return `${filePath}: invalid envoy.json`;
39338
+ if (issue.code === "unrecognized_keys") {
39339
+ const section = issue.path.join(".");
39340
+ const keys = issue.keys.map((key) => `${section}.${key}`).join(", ");
39341
+ return `${filePath}: unrecognized ${section} key(s): ${keys}`;
39342
+ }
39343
+ return `${filePath}: ${issue.path.join(".")}: ${issue.message}`;
39411
39344
  }
39412
- function every(ms, callback) {
39413
- const timer = setInterval(callback, ms);
39414
- return () => clearInterval(timer);
39345
+ function danglingLink(filePath) {
39346
+ try {
39347
+ return lstatSync(filePath).isSymbolicLink() ? readlinkSync2(filePath) : null;
39348
+ } catch {
39349
+ return null;
39350
+ }
39415
39351
  }
39416
- function createConsumerDelivery(options) {
39417
- const { host, ledger, sessionID } = options;
39418
- const now = options.now ?? Date.now;
39419
- const repeat = options.every ?? every;
39420
- const ownSubject = agentSubject(sessionID);
39421
- let currentEpoch = { id: newEpoch(), startedAt: now() };
39422
- let follows = new Map;
39423
- let streamID;
39424
- let held = new Set;
39425
- let written;
39426
- let registeredResume;
39427
- let gapTold = false;
39428
- let closed = false;
39429
- let relayed;
39430
- let persisted = JSON.stringify({ follows: {} });
39431
- const entries = new Map;
39432
- const queue = [];
39433
- let blocking;
39434
- let pumping = false;
39435
- const safely = (what, call) => {
39436
- try {
39437
- call();
39438
- } catch (error) {
39439
- try {
39440
- host.log(`consumer delivery: ${what} failed`, error);
39441
- } catch {}
39442
- }
39443
- };
39444
- const log = (message, error) => safely("log", () => host.log(`consumer delivery: ${message}`, error));
39445
- const send = (reply, body) => safely(`reply ${body}`, () => options.publish(reply, body));
39446
- const tellGap = (kind) => safely("gap notice", () => host.gap({ kind, message: GAP_MESSAGES[kind] }));
39447
- const switchStream = (id) => {
39448
- if (streamID === id)
39449
- return;
39450
- streamID = id;
39451
- held = new Set;
39452
- written = undefined;
39453
- };
39454
- const tellReplacedResume = (id) => {
39455
- const asked = resumePoint() ?? registeredResume;
39456
- if (asked === undefined || asked.stream_id === id)
39457
- return;
39458
- registeredResume = undefined;
39459
- tellGap("stream_recreated");
39460
- };
39461
- const followedAt = (subject) => {
39462
- if (subject === ownSubject)
39463
- return Number.NEGATIVE_INFINITY;
39464
- let earliest;
39465
- for (const [topic, at] of follows) {
39466
- if (!reaches(topic, subject))
39467
- continue;
39468
- if (earliest === undefined || at < earliest)
39469
- earliest = at;
39470
- }
39471
- return earliest;
39472
- };
39473
- const followedOnCorePath = (frame) => {
39474
- const state = ledger.state();
39475
- if (state.stream_id !== frame.streamID)
39476
- return false;
39477
- for (const [topic, cursor] of Object.entries(state.subjects)) {
39478
- if (cursor.since === undefined || frame.streamSeq <= cursor.since)
39479
- continue;
39480
- if (reaches(topic, frame.subject))
39481
- return true;
39482
- }
39483
- return false;
39484
- };
39485
- const settle = (entry, body) => {
39486
- if (entry.settled)
39487
- return;
39488
- entry.settled = true;
39489
- entry.stopProgress();
39490
- entries.delete(entry.key);
39491
- for (const reply of entry.replies)
39492
- send(reply, body);
39493
- if (body === "done" && entry.frame.streamID === streamID) {
39494
- held.delete(entry.frame.streamSeq);
39495
- written = Math.max(written ?? 0, entry.frame.streamSeq);
39496
- }
39497
- release(entry);
39498
- };
39499
- const release = (entry) => {
39500
- if (blocking !== entry)
39501
- return;
39502
- blocking = undefined;
39503
- pump();
39504
- };
39505
- const alreadyWritten = (identity) => identity !== undefined && ledger.delivered(identity);
39506
- const failed = (entry, error) => {
39507
- if (entry.settled)
39508
- return;
39509
- if (entry.frame.delivered < RELAY_ATTEMPTS_BEFORE_FAILED) {
39510
- log(`frame ${entry.frame.streamSeq} failed; it comes again`, error);
39511
- settle(entry, "nak");
39512
- return;
39352
+ function parseFile(filePath, schema, checkout) {
39353
+ let raw;
39354
+ try {
39355
+ raw = readFileSync3(filePath, "utf-8");
39356
+ } catch (error) {
39357
+ if (hasErrnoCode(error, "ENOENT") || hasErrnoCode(error, "ENOTDIR")) {
39358
+ return { kind: "absent", link: danglingLink(filePath) };
39513
39359
  }
39514
- safely("failure notice", () => {
39515
- const { subject, data } = entry.frame;
39516
- const { envelope } = renderInbound(data, sessionID, subject);
39517
- if (envelope !== undefined)
39518
- ledger.liveFailed(subject, envelope, error, now());
39519
- });
39520
- settle(entry, "done");
39521
- };
39522
- const handOff = (entry) => {
39523
- const handle = {
39524
- ...entry.frame,
39525
- late: entry.late,
39526
- done: () => settle(entry, "done"),
39527
- nak: () => settle(entry, "nak"),
39528
- release: () => release(entry)
39360
+ return { kind: "invalid", reason: `${filePath}: could not be read (${messageFor(error)})` };
39361
+ }
39362
+ let parsedJson;
39363
+ try {
39364
+ parsedJson = JSON.parse(raw);
39365
+ } catch {
39366
+ const position = jsonErrorPosition(raw);
39367
+ return {
39368
+ kind: "invalid",
39369
+ reason: `${filePath}: invalid JSON${position === undefined ? "" : ` at ${position}`}`
39529
39370
  };
39530
- blocking = entry;
39371
+ }
39372
+ if (typeof parsedJson === "object" && parsedJson !== null && "natsUrls" in parsedJson) {
39373
+ return {
39374
+ kind: "invalid",
39375
+ reason: checkout ? `${filePath}: the top-level natsUrls is not read, and a checkout's envoy.json holds only a dispatch section` : `${filePath}: the top-level natsUrls is not read; move it to envoy.natsUrls`
39376
+ };
39377
+ }
39378
+ const parsed = schema.safeParse(parsedJson);
39379
+ if (!parsed.success) {
39380
+ return { kind: "invalid", reason: describeSchemaIssue(filePath, parsed.error) };
39381
+ }
39382
+ return { kind: "valid", settings: parsed.data };
39383
+ }
39384
+ var FAILURE_KEPT_MS = 60000;
39385
+ var COMMAND_OUTCOMES = Symbol.for("legion.envoy-config.commands.v1");
39386
+ var processGlobal = globalThis;
39387
+ var commandOutcomes = processGlobal[COMMAND_OUTCOMES] ?? new Map;
39388
+ processGlobal[COMMAND_OUTCOMES] = commandOutcomes;
39389
+ function runCommand(command, env, cwd) {
39390
+ const childEnv = {};
39391
+ for (const [name, variable] of Object.entries(env)) {
39392
+ if (variable !== undefined)
39393
+ childEnv[name] = variable;
39394
+ }
39395
+ const directory = mkdtempSync(path.join(tmpdir(), "envoy-command-"));
39396
+ try {
39397
+ chmodSync(directory, 448);
39398
+ const output = path.join(directory, "stdout");
39399
+ const fd = openSync2(output, "wx", 384);
39531
39400
  let result;
39532
39401
  try {
39533
- result = host.deliver(handle);
39534
- } catch (error) {
39535
- failed(entry, error);
39536
- release(entry);
39537
- return;
39538
- }
39539
- if (!(result instanceof Promise)) {
39540
- release(entry);
39541
- return;
39542
- }
39543
- result.then(() => release(entry), (error) => {
39544
- failed(entry, error);
39545
- release(entry);
39546
- });
39547
- };
39548
- function pump() {
39549
- if (pumping)
39550
- return;
39551
- pumping = true;
39552
- try {
39553
- while (blocking === undefined && !closed) {
39554
- const entry = queue.shift();
39555
- if (entry === undefined)
39556
- return;
39557
- if (entry.settled)
39558
- continue;
39559
- if (alreadyWritten(entry.identity)) {
39560
- settle(entry, "done");
39561
- continue;
39562
- }
39563
- handOff(entry);
39564
- }
39402
+ result = Bun.spawnSync(["/bin/sh", "-c", command], {
39403
+ cwd,
39404
+ env: childEnv,
39405
+ stdin: "ignore",
39406
+ stdout: fd,
39407
+ stderr: "ignore",
39408
+ timeout: COMMAND_TIMEOUT_MS,
39409
+ detached: true
39410
+ });
39411
+ } catch (error) {
39412
+ return { failure: `could not start: ${messageFor(error)}`, timedOut: false };
39565
39413
  } finally {
39566
- pumping = false;
39567
- }
39568
- }
39569
- const receive = (message) => {
39570
- const decoded = decodeRelayFrame(message);
39571
- if ("error" in decoded) {
39572
- log(`dropping an undecodable relay frame: ${decoded.error}`);
39573
- if (message.reply !== undefined && message.reply !== "")
39574
- send(message.reply, "done");
39575
- return;
39414
+ closeSync2(fd);
39576
39415
  }
39577
- const { frame } = decoded;
39578
- if (closed) {
39579
- send(frame.reply, "nak");
39580
- return;
39416
+ try {
39417
+ process.kill(-result.pid, "SIGKILL");
39418
+ } catch {}
39419
+ if (result.exitedDueToTimeout) {
39420
+ return {
39421
+ failure: `did not finish within ${COMMAND_TIMEOUT_MS / 1000} s, and does not run again in this process`,
39422
+ timedOut: true
39423
+ };
39581
39424
  }
39582
- send(frame.reply, "received");
39583
- if (frame.gap && !gapTold)
39584
- tellGap("retention_gap");
39585
- gapTold = false;
39586
- if (frame.streamID !== streamID) {
39587
- if (streamID !== undefined && (written !== undefined || held.size > 0)) {
39588
- tellGap("stream_recreated");
39589
- } else if (streamID === undefined) {
39590
- tellReplacedResume(frame.streamID);
39591
- }
39592
- switchStream(frame.streamID);
39425
+ if (!result.success) {
39426
+ return {
39427
+ failure: `exited with ${result.signalCode ?? `status ${result.exitCode}`}`,
39428
+ timedOut: false
39429
+ };
39593
39430
  }
39594
- const key = `${frame.streamID}:${frame.streamSeq}`;
39595
- const first = entries.get(key);
39596
- if (first !== undefined) {
39597
- first.replies.push(frame.reply);
39598
- return;
39431
+ const value = readFileSync3(output, "utf-8").trim();
39432
+ if (value.length === 0)
39433
+ return { failure: "printed nothing", timedOut: false };
39434
+ if (!sendable(value)) {
39435
+ return {
39436
+ failure: "printed more than one line, or whitespace inside its value",
39437
+ timedOut: false
39438
+ };
39599
39439
  }
39600
- const since = followedAt(frame.subject);
39601
- const unfollowed = (since === undefined || frame.storedAtMs < since - FOLLOW_CLOCK_ALLOWANCE_MS) && !followedOnCorePath(frame);
39602
- const identity = unfollowed ? undefined : parseIdentity(frame.data);
39603
- if (unfollowed || alreadyWritten(identity)) {
39604
- send(frame.reply, "done");
39605
- written = Math.max(written ?? 0, frame.streamSeq);
39606
- return;
39440
+ return { value };
39441
+ } finally {
39442
+ rmSync2(directory, { recursive: true, force: true });
39443
+ }
39444
+ }
39445
+ function fileValue(value, field, filePath, env, commands = "run") {
39446
+ if (!value.startsWith("!")) {
39447
+ if (!sendable(value)) {
39448
+ throw new Error(`${filePath}: ${field} holds whitespace or a control character`);
39607
39449
  }
39608
- held.add(frame.streamSeq);
39609
- const entry = {
39610
- frame,
39611
- identity,
39612
- late: frame.delivered > 1 || frame.storedAtMs < currentEpoch.startedAt,
39613
- replies: [frame.reply],
39614
- key,
39615
- stopProgress: () => {
39616
- return;
39617
- },
39618
- settled: false
39619
- };
39620
- entry.stopProgress = repeat(RELAY_PROGRESS_INTERVAL_MS, () => {
39621
- if (entry.settled)
39622
- return;
39623
- for (const reply of entry.replies)
39624
- send(reply, "progress");
39625
- });
39626
- entries.set(key, entry);
39627
- queue.push(entry);
39628
- pump();
39629
- };
39630
- const resumePoint = () => {
39631
- if (streamID === undefined)
39632
- return;
39633
- if (held.size > 0)
39634
- return { stream_id: streamID, seq: Math.min(...held) - 1 };
39635
- return written === undefined ? undefined : { stream_id: streamID, seq: written };
39636
- };
39637
- const seededResume = () => {
39638
- const state = ledger.state();
39639
- const stream = state.stream_id;
39640
- if (stream === undefined)
39641
- return;
39642
- let lowest;
39643
- for (const [subject, cursor] of Object.entries(state.subjects)) {
39644
- if (cursor.after === undefined || !streamStores(subject))
39645
- continue;
39646
- if (lowest === undefined || cursor.after < lowest)
39647
- lowest = cursor.after;
39450
+ return value;
39451
+ }
39452
+ const command = value.slice(1);
39453
+ const cwd = path.dirname(path.resolve(filePath));
39454
+ const key = JSON.stringify([command, filePath, cwd]);
39455
+ let outcome = commandOutcomes.get(key);
39456
+ if (commands === "kept") {
39457
+ if (outcome === undefined) {
39458
+ throw new Error(`${filePath}: ${field}'s command has not run in this process yet, and this read does not run it`);
39648
39459
  }
39649
- return lowest === undefined ? undefined : { stream_id: stream, seq: lowest };
39650
- };
39651
- const snapshot = () => {
39652
- const resume = resumePoint();
39653
- return {
39654
- ...resume === undefined ? {} : { resume },
39655
- follows: Object.fromEntries(follows)
39656
- };
39657
- };
39658
- const persist = () => {
39659
- const current = snapshot();
39660
- const serialized = JSON.stringify(current);
39661
- if (serialized === persisted)
39662
- return;
39663
- if (!options.persist(current))
39664
- return;
39665
- persisted = serialized;
39666
- };
39667
- return {
39668
- subject: deliverSubject(sessionID),
39669
- receive,
39670
- epoch: () => currentEpoch,
39671
- beginEpoch() {
39672
- currentEpoch = { id: newEpoch(), startedAt: now() };
39673
- return currentEpoch;
39674
- },
39675
- registration() {
39676
- const resume = resumePoint() ?? seededResume();
39677
- registeredResume = resume;
39678
- return {
39679
- consumer: true,
39680
- epoch: currentEpoch.id,
39681
- ...resume === undefined ? {} : { resume }
39682
- };
39683
- },
39684
- answered(answer, topics) {
39685
- if (answer.mode === "consumer") {
39686
- if (answer.stream_id !== "" && answer.stream_id !== streamID) {
39687
- tellReplacedResume(answer.stream_id);
39688
- switchStream(answer.stream_id);
39689
- }
39690
- const floor = (answer.consumer_start ?? 0) - 1;
39691
- if (answer.stream_id === streamID && floor > (written ?? -1))
39692
- written = floor;
39693
- if (answer.gap) {
39694
- tellGap("retention_gap");
39695
- gapTold = true;
39696
- }
39697
- relayed = new Set([...topics].filter(streamStores));
39698
- } else {
39699
- relayed = undefined;
39700
- }
39701
- ledger.setConsumerMode(relayed !== undefined);
39702
- persist();
39703
- return answer.mode;
39704
- },
39705
- relays: (topic) => relayed?.has(topic) === true,
39706
- relaying: () => relayed !== undefined,
39707
- setFollowed(topics) {
39708
- const at = now();
39709
- const next = new Map;
39710
- for (const topic of topics) {
39711
- if (topic === ownSubject)
39712
- continue;
39713
- next.set(topic, follows.get(topic) ?? at);
39714
- }
39715
- follows = next;
39716
- persist();
39717
- },
39718
- persist,
39719
- resumePoint,
39720
- snapshot,
39721
- restore(restored) {
39722
- const merged = new Map(Object.entries(restored.follows));
39723
- for (const [topic, at] of follows)
39724
- if (!merged.has(topic))
39725
- merged.set(topic, at);
39726
- follows = merged;
39727
- streamID = restored.resume?.stream_id;
39728
- held = new Set;
39729
- written = restored.resume?.seq;
39730
- persisted = JSON.stringify(snapshot());
39731
- },
39732
- close() {
39733
- closed = true;
39734
- relayed = undefined;
39735
- ledger.setConsumerMode(false);
39736
- queue.length = 0;
39737
- for (const entry of [...entries.values()])
39738
- settle(entry, "nak");
39739
- blocking = undefined;
39460
+ } else if (outcome === undefined || "failure" in outcome && !outcome.timedOut && Date.now() - outcome.at >= FAILURE_KEPT_MS) {
39461
+ outcome = { ...runCommand(command, env, cwd), at: Date.now() };
39462
+ commandOutcomes.set(key, outcome);
39463
+ }
39464
+ if ("failure" in outcome)
39465
+ throw new Error(`${filePath}: ${field}'s command ${outcome.failure}`);
39466
+ return outcome.value;
39467
+ }
39468
+ function statDevice(directory) {
39469
+ try {
39470
+ return statSync2(directory).dev;
39471
+ } catch {
39472
+ return;
39473
+ }
39474
+ }
39475
+ function moveNotice(oldFile, newFile, move, deviceOf) {
39476
+ const oldDevice = deviceOf(path.dirname(oldFile));
39477
+ let ancestor = path.dirname(newFile);
39478
+ let newDevice = deviceOf(ancestor);
39479
+ while (newDevice === undefined && path.dirname(ancestor) !== ancestor) {
39480
+ ancestor = path.dirname(ancestor);
39481
+ newDevice = deviceOf(ancestor);
39482
+ }
39483
+ if (oldDevice !== undefined && newDevice !== undefined && oldDevice !== newDevice) {
39484
+ return `${oldFile} is not read, and is on another filesystem than ${newFile} (a mount): create ${newFile}, and move ${oldFile} on the machine that owns it`;
39485
+ }
39486
+ return `${oldFile} is not read: ${move}`;
39487
+ }
39488
+ function oldUserFileNotice(filePath, env, deviceOf) {
39489
+ const home = nonEmpty(env.HOME) ?? homedir();
39490
+ const old = path.join(home, ".config", "opencode", "envoy.json");
39491
+ if (!existsSync(old))
39492
+ return;
39493
+ const selected = path.resolve(filePath);
39494
+ const userFile = path.resolve(home, ".config", "legion", "envoy.json");
39495
+ if (selected === userFile) {
39496
+ return moveNotice(old, userFile, "mkdir -p ~/.config/legion && mv ~/.config/opencode/envoy.json ~/.config/legion/", deviceOf);
39497
+ }
39498
+ const profileFile = PLUGIN_HARNESSES.map((harness) => namedDirectory(harness, env)).some((directory) => directory !== undefined && path.isAbsolute(directory) && selected === path.resolve(directory, "envoy.json"));
39499
+ if (profileFile) {
39500
+ return `${old} is not read for this profile; copy or link it to ${filePath}, or write that file`;
39501
+ }
39502
+ const configHome = nonEmpty(env.XDG_CONFIG_HOME);
39503
+ const neutral = neutralConfig(env);
39504
+ if (configHome !== undefined && path.isAbsolute(configHome) && "path" in neutral && selected === neutral.path) {
39505
+ return `${old} is not read under XDG_CONFIG_HOME=${configHome}`;
39506
+ }
39507
+ return;
39508
+ }
39509
+ function missingConfigNotice(filePath, missing, off) {
39510
+ const link = missing.link === null ? "" : ` (a link to ${missing.link}, which does not exist)`;
39511
+ const old = missing.oldUserFileNotice === undefined ? "" : `; ${missing.oldUserFileNotice}`;
39512
+ return `no envoy.json at ${filePath}${link}: ${off}${old}`;
39513
+ }
39514
+ function readEnvoyConfig(filePath, env, options) {
39515
+ const deviceOf = options.deviceOf ?? statDevice;
39516
+ const chosen = parseFile(filePath, EnvoyFileSchema, false);
39517
+ if (chosen.kind === "invalid")
39518
+ return { kind: "failed", path: filePath, error: chosen.reason };
39519
+ const resolvedChosen = path.resolve(filePath);
39520
+ const checkoutPath = path.resolve(options.cwd, ".envoy", "envoy.json");
39521
+ const checkoutFile = checkoutPath === resolvedChosen ? { kind: "absent", link: null } : parseFile(checkoutPath, CheckoutFileSchema, true);
39522
+ const own = chosen.kind === "valid" ? chosen.settings : {};
39523
+ let checkout = null;
39524
+ if (checkoutFile.kind === "invalid") {
39525
+ checkout = { kind: "invalid", path: checkoutPath, error: checkoutFile.reason };
39526
+ } else if (checkoutFile.kind === "valid") {
39527
+ checkout = {
39528
+ kind: "valid",
39529
+ path: checkoutPath,
39530
+ dispatch: checkoutFile.settings.dispatch ?? {},
39531
+ envoyError: Object.hasOwn(checkoutFile.settings, "envoy") ? `${checkoutPath}: envoy is not read from a checkout's envoy.json; put it in ${filePath}` : null,
39532
+ askgateError: Object.hasOwn(checkoutFile.settings, "askgate") ? `askgate in ${checkoutPath} is not read; set it in ${filePath}` : null
39533
+ };
39534
+ }
39535
+ const oldCheckout = path.resolve(options.cwd, ".opencode", "envoy.json");
39536
+ const warnings = oldCheckout !== resolvedChosen && !existsSync(checkoutPath) && existsSync(oldCheckout) ? [
39537
+ moveNotice(oldCheckout, checkoutPath, `mkdir -p ${path.dirname(checkoutPath)} && mv ${oldCheckout} ${checkoutPath}`, deviceOf)
39538
+ ] : [];
39539
+ const unreadOldUserFile = chosen.kind === "absent" ? oldUserFileNotice(filePath, env, deviceOf) : undefined;
39540
+ return {
39541
+ kind: "read",
39542
+ path: filePath,
39543
+ dispatch: own.dispatch ?? {},
39544
+ envoy: own.envoy ?? {},
39545
+ checkout,
39546
+ missing: chosen.kind === "absent" ? {
39547
+ link: chosen.link,
39548
+ ...unreadOldUserFile === undefined ? {} : { oldUserFileNotice: unreadOldUserFile }
39549
+ } : null,
39550
+ warnings
39551
+ };
39552
+ }
39553
+ function askgateSetting(filePath) {
39554
+ const stayOn = (reason) => ({
39555
+ enabled: true,
39556
+ error: `${reason}; AskGate stays on`
39557
+ });
39558
+ let raw;
39559
+ try {
39560
+ raw = readFileSync3(filePath, "utf-8");
39561
+ } catch (error) {
39562
+ if (hasErrnoCode(error, "ENOENT") || hasErrnoCode(error, "ENOTDIR")) {
39563
+ return { enabled: true, error: null };
39740
39564
  }
39565
+ return stayOn(`${filePath}: could not be read (${messageFor(error)}); askgate.enabled is not read`);
39566
+ }
39567
+ let parsedJson;
39568
+ try {
39569
+ parsedJson = JSON.parse(raw);
39570
+ } catch {
39571
+ const position = jsonErrorPosition(raw);
39572
+ const at = position === undefined ? "" : ` at ${position}`;
39573
+ return stayOn(`${filePath}: invalid JSON${at}; askgate.enabled is not read`);
39574
+ }
39575
+ if (typeof parsedJson !== "object" || parsedJson === null || !("askgate" in parsedJson)) {
39576
+ return { enabled: true, error: null };
39577
+ }
39578
+ const parsed = object({ askgate: AskgateSectionSchema }).safeParse(parsedJson);
39579
+ if (!parsed.success) {
39580
+ const unknownKey = parsed.error.issues[0]?.code === "unrecognized_keys";
39581
+ return stayOn(unknownKey ? `${describeSchemaIssue(filePath, parsed.error)}; the one askgate key is askgate.enabled, true or false` : `${filePath}: askgate.enabled must be true or false`);
39582
+ }
39583
+ return { enabled: parsed.data.askgate.enabled ?? true, error: null };
39584
+ }
39585
+
39586
+ // ../envoy-client/src/bearer.ts
39587
+ function bearerCredential(token) {
39588
+ return {
39589
+ scheme: "Bearer",
39590
+ headers: () => ({ Authorization: `Bearer ${token}` }),
39591
+ secret: () => token
39741
39592
  };
39742
39593
  }
39743
- var NOT_ASKED = { mode: "core", reason: "not_asked" };
39744
- function registeredWithoutRelay(ledger, delivery, topics) {
39745
- if (delivery === undefined) {
39746
- ledger.setConsumerMode(false);
39747
- return;
39594
+ function bearerSource(env, section, written) {
39595
+ const pointer = nonEmpty(env[`${section}_TOKEN_FILE`]);
39596
+ if (pointer !== undefined) {
39597
+ return { kind: "variable", token: readSecretFile(`${section}_TOKEN_FILE`, pointer) };
39748
39598
  }
39749
- delivery.answered(NOT_ASKED, topics);
39599
+ const plain = nonEmpty(env[`${section}_TOKEN`]);
39600
+ if (plain !== undefined) {
39601
+ if (!sendable(plain)) {
39602
+ throw new Error(`${section}_TOKEN holds whitespace or a control character`);
39603
+ }
39604
+ return { kind: "variable", token: plain };
39605
+ }
39606
+ const bearer = written();
39607
+ return bearer === undefined ? { kind: "none" } : { kind: "file", ...bearer };
39608
+ }
39609
+ function fileBearer(bearer, url, section, env, commands = "run") {
39610
+ const named = httpUrl2(url)?.origin;
39611
+ const paired = httpUrl2(bearer.server)?.origin;
39612
+ if (named !== paired) {
39613
+ throw new Error(`${section}_URL names ${named}, but ${bearer.path}'s ${bearer.field} is for ${paired}; set ${section}_TOKEN or ${section}_TOKEN_FILE for it`);
39614
+ }
39615
+ if (bearer.checkout && bearer.token.startsWith("!")) {
39616
+ throw new Error(`${bearer.path}: ${bearer.field} is a command, which a checkout's envoy.json cannot run`);
39617
+ }
39618
+ if (bearer.token === "") {
39619
+ throw new Error(`${bearer.field} in ${bearer.path} must be a non-empty bearer token`);
39620
+ }
39621
+ return fileValue(bearer.token, bearer.field, bearer.path, env, commands);
39750
39622
  }
39751
39623
 
39752
39624
  // ../envoy-client/src/dispatch-config.ts
39753
39625
  var DEFAULT_SERVER_URL = "http://localhost:8766";
39754
- function withoutTrailingSlashes2(url) {
39755
- let end = url.length;
39756
- while (end > 0 && url[end - 1] === "/")
39757
- end -= 1;
39758
- return url.slice(0, end);
39759
- }
39760
39626
  function checkoutPair(checkout, serverUrl) {
39761
39627
  const { token } = checkout.dispatch;
39762
39628
  if (token === undefined) {
@@ -39818,7 +39684,7 @@ function dispatchSettings(env, config, options = {}) {
39818
39684
  enabled: true,
39819
39685
  url,
39820
39686
  credential,
39821
- ...publicUrl === undefined ? {} : { publicUrl: publicUrl.replace(/\/+$/, "") }
39687
+ ...publicUrl === undefined ? {} : { publicUrl: withoutTrailingSlashes(publicUrl) }
39822
39688
  };
39823
39689
  }
39824
39690
  function brokerRouteForced(env) {
@@ -39874,7 +39740,7 @@ function dispatchTarget(env, config) {
39874
39740
  return offWithError(messageFor(error));
39875
39741
  }
39876
39742
  if (route.traded) {
39877
- const brokered = withoutTrailingSlashes2(explicitUrl ?? own.serverUrl ?? DEFAULT_SERVER_URL);
39743
+ const brokered = withoutTrailingSlashes(explicitUrl ?? own.serverUrl ?? DEFAULT_SERVER_URL);
39878
39744
  return { enabled: true, url: brokered, source: { kind: "broker" } };
39879
39745
  }
39880
39746
  if (forced) {
@@ -39882,7 +39748,7 @@ function dispatchTarget(env, config) {
39882
39748
  }
39883
39749
  return offWithError(explicitUrl === undefined ? `${config.path}: dispatch is enabled and no token is: set dispatch.token, or DISPATCH_TOKEN or DISPATCH_TOKEN_FILE` : `DISPATCH_URL is set and no token is: set DISPATCH_TOKEN or DISPATCH_TOKEN_FILE, or dispatch.token in ${config.path}`);
39884
39750
  }
39885
- const url = (explicitUrl ?? (source.kind === "file" ? source.server : own.serverUrl ?? DEFAULT_SERVER_URL)).replace(/\/+$/, "");
39751
+ const url = withoutTrailingSlashes(explicitUrl ?? (source.kind === "file" ? source.server : own.serverUrl ?? DEFAULT_SERVER_URL));
39886
39752
  return { enabled: true, url, source };
39887
39753
  }
39888
39754
  function activeDispatchSettings(resolution) {
@@ -41179,6 +41045,133 @@ function writeSessionTitle(dir, title) {
41179
41045
  writeAtomically2(join6(dir, "title"), title);
41180
41046
  }
41181
41047
 
41048
+ // ../envoy-client/src/envoy-settings.ts
41049
+ var DEFAULT_ENVOY_URL = "http://127.0.0.1:9020";
41050
+ var DEFAULT_HEARTBEAT_MS = SESSION_HEARTBEAT_MS;
41051
+ var MIN_PASS_DEADLINE_MS = 30000;
41052
+ var DEFAULT_TMUX_PLACE_CACHE_MS = 30000;
41053
+ var MIN_INTERVAL_MS = 25;
41054
+ var MAX_INTERVAL_MS = 2147483647;
41055
+ var REGISTRATION_SETTLE_MS = 5000;
41056
+ var SHUTDOWN_SETTLE_MS = 250;
41057
+ var HEARTBEAT_RETRY_MIN_MS = 20000;
41058
+ var HEARTBEAT_RETRY_MAX_MS = 40000;
41059
+ function heartbeatRetryDelayMs(draw = Math.random) {
41060
+ return Math.round(HEARTBEAT_RETRY_MIN_MS + draw() * (HEARTBEAT_RETRY_MAX_MS - HEARTBEAT_RETRY_MIN_MS));
41061
+ }
41062
+ function namedInterval(raw) {
41063
+ const parsed = Number(raw);
41064
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
41065
+ }
41066
+ function resolvedInterval(raw, fallback) {
41067
+ const named = namedInterval(raw);
41068
+ return named === undefined ? fallback : Math.min(Math.max(named, MIN_INTERVAL_MS), MAX_INTERVAL_MS);
41069
+ }
41070
+ function natsSeedFromEnvironment(env) {
41071
+ const { NATS_NKEY_SEED_FILE: file, NATS_NKEY_SEED: plain } = env;
41072
+ if (file !== undefined) {
41073
+ if (file === "")
41074
+ throw new Error("NATS_NKEY_SEED_FILE is set but empty");
41075
+ return {
41076
+ value: readSecretFile("NATS_NKEY_SEED_FILE", file),
41077
+ source: `NATS_NKEY_SEED_FILE (${file})`
41078
+ };
41079
+ }
41080
+ if (plain === undefined)
41081
+ return;
41082
+ const value = plain.trim();
41083
+ if (value.length === 0)
41084
+ throw new Error("NATS_NKEY_SEED is set but empty");
41085
+ return { value, source: "NATS_NKEY_SEED" };
41086
+ }
41087
+ var NO_NATS_SERVER = "Envoy has no NATS server (envoy.natsUrls, ENVOY_NATS_URL)";
41088
+ var NO_NATS_CONNECTION = "this session opens no NATS connection";
41089
+ function natsServerKey(url) {
41090
+ const match = /^([a-z][a-z0-9+.-]*):\/\/([^@/]*@)?(\[[^\]]*\]|[^:/]*)(?::(\d+))?(\/.*)?$/i.exec(url);
41091
+ if (match === null)
41092
+ return url;
41093
+ const [, scheme = "", userinfo = "", host = "", port = "4222", rest = ""] = match;
41094
+ return `${scheme.toLowerCase()}://${userinfo}${host.toLowerCase()}:${port}${withoutTrailingSlashes(rest)}`;
41095
+ }
41096
+ function natsCredential(env, section, namedNatsUrls, filePath) {
41097
+ const fromEnvironment = natsSeedFromEnvironment(env);
41098
+ if (fromEnvironment !== undefined || section.natsSeed === undefined)
41099
+ return fromEnvironment;
41100
+ if (namedNatsUrls.length > 0) {
41101
+ const servers = new Set((section.natsUrls ?? []).map(natsServerKey));
41102
+ const named = new Set(namedNatsUrls.map(natsServerKey));
41103
+ if (named.size !== servers.size || ![...named].every((server) => servers.has(server))) {
41104
+ throw new Error(`ENVOY_NATS_URL names other servers than ${filePath}'s envoy.natsUrls; set NATS_NKEY_SEED_FILE for them`);
41105
+ }
41106
+ }
41107
+ return {
41108
+ value: fileValue(section.natsSeed, "envoy.natsSeed", filePath, env),
41109
+ source: `envoy.natsSeed in ${filePath}`
41110
+ };
41111
+ }
41112
+ function envoySettings(env, config, options = {}) {
41113
+ if (config.kind === "failed")
41114
+ return offWithError(config.error);
41115
+ const section = config.envoy;
41116
+ if (section.enabled === false) {
41117
+ return offWithNotice(`${config.path} sets envoy.enabled to false: Envoy is off`);
41118
+ }
41119
+ const namedUrl = nonEmpty(env.ENVOY_URL);
41120
+ const namedNatsUrls = (env.ENVOY_NATS_URL ?? "").split(",").map((url) => url.trim()).filter((url) => url.length > 0);
41121
+ const addressed = namedUrl !== undefined || namedNatsUrls.length > 0 || section.url !== undefined || (section.natsUrls ?? []).length > 0 || section.enabled === true;
41122
+ if (!addressed) {
41123
+ return offWithNotice(config.missing === null ? `${config.path} names no Envoy listener or NATS server, and neither ENVOY_URL nor ENVOY_NATS_URL is set: Envoy is off` : missingConfigNotice(config.path, config.missing, "Envoy is off"));
41124
+ }
41125
+ if (namedUrl !== undefined && httpUrl2(namedUrl) === undefined) {
41126
+ return offWithError("ENVOY_URL must be an http or https URL");
41127
+ }
41128
+ const listener = section.url ?? DEFAULT_ENVOY_URL;
41129
+ const url = namedUrl ?? listener;
41130
+ let credential;
41131
+ try {
41132
+ const source = bearerSource(env, "ENVOY", () => section.token === undefined ? undefined : {
41133
+ token: section.token,
41134
+ field: "envoy.token",
41135
+ path: config.path,
41136
+ server: listener,
41137
+ checkout: false
41138
+ });
41139
+ if (source.kind === "none") {
41140
+ credential = brokerRouteConfigured(env).traded ? brokerCredential(env, options.session) : undefined;
41141
+ } else {
41142
+ credential = bearerCredential(source.kind === "variable" ? source.token : fileBearer(source, url, "ENVOY", env));
41143
+ }
41144
+ } catch (error) {
41145
+ return offWithError(messageFor(error));
41146
+ }
41147
+ const natsUrls = namedNatsUrls.length > 0 ? namedNatsUrls : section.natsUrls ?? [];
41148
+ let inbound;
41149
+ if (options.nats === false)
41150
+ inbound = { off: NO_NATS_CONNECTION };
41151
+ else if (natsUrls.length === 0)
41152
+ inbound = { off: NO_NATS_SERVER };
41153
+ else {
41154
+ try {
41155
+ inbound = { natsUrls, natsSeed: natsCredential(env, section, namedNatsUrls, config.path) };
41156
+ } catch (error) {
41157
+ inbound = { off: messageFor(error) };
41158
+ }
41159
+ }
41160
+ const heartbeatMs = resolvedInterval(env.ENVOY_HEARTBEAT_MS, DEFAULT_HEARTBEAT_MS);
41161
+ const tmuxPlaceCacheMs = resolvedInterval(env.ENVOY_TMUX_PLACE_CACHE_MS, DEFAULT_TMUX_PLACE_CACHE_MS);
41162
+ const namedPassDeadlineMs = namedInterval(env.ENVOY_PASS_DEADLINE_MS);
41163
+ return {
41164
+ enabled: true,
41165
+ url: withoutTrailingSlashes(url),
41166
+ credential,
41167
+ inbound,
41168
+ heartbeatMs,
41169
+ tmuxPlaceCacheMs,
41170
+ passDeadlineMs: Math.min(Math.max(namedPassDeadlineMs ?? MIN_PASS_DEADLINE_MS, heartbeatMs), MAX_INTERVAL_MS),
41171
+ passDeadlineRaisedFrom: namedPassDeadlineMs !== undefined && namedPassDeadlineMs < heartbeatMs ? namedPassDeadlineMs : undefined
41172
+ };
41173
+ }
41174
+
41182
41175
  // ../envoy-client/src/nats-auth.ts
41183
41176
  var import_nats = __toESM(require_mod4(), 1);
41184
41177
  var seedKeys = import_nats.nkeys;
@@ -42713,7 +42706,7 @@ var import_nats2 = __toESM(require_mod4(), 1);
42713
42706
 
42714
42707
  // package.json
42715
42708
  var name = "@sjawhar/pi-envoy";
42716
- var version2 = "16.41.0";
42709
+ var version2 = "16.42.0";
42717
42710
  var package_default = {
42718
42711
  name,
42719
42712
  version: version2,