@sjawhar/pi-envoy 16.41.0 → 16.41.1
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/dispatch.js +15 -22
- package/dist/envoy.js +2217 -2224
- package/package.json +1 -1
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
|
|
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/
|
|
37372
|
-
import {
|
|
37373
|
-
|
|
37374
|
-
|
|
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/
|
|
37474
|
-
|
|
37475
|
-
|
|
37476
|
-
|
|
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
|
|
37479
|
-
|
|
37480
|
-
|
|
37481
|
-
|
|
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
|
|
37486
|
-
|
|
37487
|
-
|
|
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
|
|
37501
|
+
return interests;
|
|
37492
37502
|
}
|
|
37493
|
-
|
|
37494
|
-
|
|
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
|
|
37500
|
-
|
|
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
|
|
37506
|
-
|
|
37507
|
-
|
|
37508
|
-
|
|
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
|
-
|
|
37514
|
-
|
|
37515
|
-
|
|
37516
|
-
|
|
37517
|
-
|
|
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
|
|
37527
|
-
|
|
37528
|
-
|
|
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
|
|
37532
|
-
if (
|
|
37533
|
-
|
|
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
|
|
37586
|
-
|
|
37587
|
-
|
|
37588
|
-
|
|
37589
|
-
|
|
37590
|
-
|
|
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
|
-
|
|
37593
|
-
|
|
37594
|
-
|
|
37595
|
-
|
|
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
|
-
|
|
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
|
-
|
|
37606
|
-
|
|
37607
|
-
|
|
37608
|
-
|
|
37609
|
-
|
|
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
|
-
|
|
37613
|
-
|
|
37614
|
-
|
|
37615
|
-
|
|
37616
|
-
|
|
37617
|
-
|
|
37618
|
-
|
|
37619
|
-
|
|
37620
|
-
|
|
37621
|
-
|
|
37622
|
-
|
|
37623
|
-
|
|
37624
|
-
|
|
37625
|
-
|
|
37626
|
-
|
|
37627
|
-
|
|
37628
|
-
|
|
37629
|
-
|
|
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
|
-
|
|
37645
|
-
|
|
37646
|
-
|
|
37647
|
-
|
|
37648
|
-
|
|
37649
|
-
|
|
37650
|
-
|
|
37651
|
-
|
|
37652
|
-
|
|
37653
|
-
|
|
37654
|
-
|
|
37655
|
-
const
|
|
37656
|
-
|
|
37657
|
-
|
|
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
|
-
|
|
37663
|
-
|
|
37664
|
-
|
|
37665
|
-
|
|
37666
|
-
|
|
37667
|
-
|
|
37668
|
-
|
|
37669
|
-
|
|
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 {
|
|
37673
|
-
} finally {
|
|
37674
|
-
closeSync2(fd);
|
|
37642
|
+
return { unanswered: error };
|
|
37675
37643
|
}
|
|
37676
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
37688
|
-
|
|
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
|
-
|
|
37692
|
-
|
|
37693
|
-
|
|
37694
|
-
|
|
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
|
-
|
|
37697
|
-
|
|
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
|
-
|
|
37701
|
-
|
|
37702
|
-
|
|
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
|
|
37706
|
-
|
|
37707
|
-
|
|
37708
|
-
|
|
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
|
|
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
|
-
|
|
37802
|
-
|
|
37803
|
-
|
|
37804
|
-
|
|
37805
|
-
|
|
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
|
|
37814
|
-
|
|
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/
|
|
37847
|
-
|
|
37848
|
-
|
|
37849
|
-
|
|
37850
|
-
|
|
37851
|
-
|
|
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
|
-
|
|
37855
|
-
const
|
|
37856
|
-
|
|
37857
|
-
|
|
37858
|
-
}
|
|
37859
|
-
const
|
|
37860
|
-
|
|
37861
|
-
|
|
37862
|
-
|
|
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
|
-
|
|
37865
|
-
|
|
37866
|
-
const
|
|
37867
|
-
|
|
37868
|
-
|
|
37869
|
-
|
|
37870
|
-
|
|
37871
|
-
|
|
37872
|
-
|
|
37873
|
-
|
|
37874
|
-
|
|
37875
|
-
|
|
37876
|
-
|
|
37877
|
-
|
|
37878
|
-
|
|
37879
|
-
|
|
37880
|
-
|
|
37881
|
-
|
|
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
|
-
|
|
37944
|
-
|
|
37945
|
-
|
|
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
|
-
|
|
37949
|
-
|
|
37950
|
-
|
|
37951
|
-
|
|
37952
|
-
|
|
37953
|
-
|
|
37954
|
-
|
|
37955
|
-
|
|
37956
|
-
|
|
37957
|
-
|
|
37958
|
-
|
|
37959
|
-
|
|
37960
|
-
|
|
37961
|
-
|
|
37962
|
-
|
|
37963
|
-
|
|
37964
|
-
|
|
37965
|
-
|
|
37966
|
-
|
|
37967
|
-
|
|
37968
|
-
|
|
37969
|
-
|
|
37970
|
-
|
|
37971
|
-
|
|
37972
|
-
|
|
37973
|
-
|
|
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
|
-
|
|
37976
|
-
|
|
37977
|
-
|
|
37978
|
-
|
|
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
|
-
|
|
37981
|
-
|
|
37982
|
-
|
|
37983
|
-
|
|
37984
|
-
|
|
37985
|
-
|
|
37986
|
-
|
|
37987
|
-
|
|
37988
|
-
|
|
37989
|
-
|
|
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
|
-
|
|
38021
|
+
queued = await host.showFailureNotice(notice) === "queued";
|
|
37992
38022
|
} catch (error) {
|
|
37993
|
-
|
|
38023
|
+
if (!alive())
|
|
38024
|
+
return "stopped";
|
|
38025
|
+
safely("failure notice warning", () => host.failureNoticeFailed(notice, error));
|
|
38026
|
+
return "hole";
|
|
37994
38027
|
}
|
|
37995
|
-
|
|
37996
|
-
|
|
37997
|
-
|
|
37998
|
-
|
|
37999
|
-
|
|
38000
|
-
|
|
38001
|
-
|
|
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
|
-
|
|
38011
|
-
|
|
38012
|
-
|
|
38013
|
-
|
|
38014
|
-
|
|
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
|
-
|
|
38174
|
-
|
|
38175
|
-
|
|
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
|
-
|
|
38178
|
-
|
|
38179
|
-
|
|
38180
|
-
|
|
38181
|
-
|
|
38182
|
-
|
|
38183
|
-
|
|
38184
|
-
|
|
38185
|
-
|
|
38186
|
-
|
|
38187
|
-
const
|
|
38188
|
-
|
|
38189
|
-
|
|
38190
|
-
|
|
38191
|
-
|
|
38192
|
-
|
|
38193
|
-
|
|
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
|
-
|
|
38216
|
-
|
|
38217
|
-
|
|
38218
|
-
|
|
38219
|
-
|
|
38220
|
-
|
|
38221
|
-
|
|
38222
|
-
|
|
38223
|
-
|
|
38224
|
-
|
|
38225
|
-
|
|
38226
|
-
|
|
38227
|
-
|
|
38228
|
-
|
|
38229
|
-
|
|
38230
|
-
|
|
38231
|
-
|
|
38232
|
-
|
|
38233
|
-
|
|
38234
|
-
|
|
38235
|
-
|
|
38236
|
-
|
|
38237
|
-
|
|
38238
|
-
|
|
38239
|
-
|
|
38240
|
-
|
|
38241
|
-
|
|
38242
|
-
|
|
38243
|
-
|
|
38244
|
-
|
|
38245
|
-
|
|
38246
|
-
|
|
38247
|
-
|
|
38248
|
-
|
|
38249
|
-
|
|
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
|
-
|
|
38260
|
-
|
|
38261
|
-
|
|
38262
|
-
|
|
38263
|
-
|
|
38264
|
-
|
|
38265
|
-
|
|
38266
|
-
|
|
38267
|
-
|
|
38268
|
-
|
|
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
|
-
|
|
38271
|
-
|
|
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
|
-
|
|
38187
|
+
if (alive()) {
|
|
38188
|
+
answered.clear();
|
|
38189
|
+
listenerFailed(error);
|
|
38190
|
+
}
|
|
38191
|
+
return;
|
|
38274
38192
|
}
|
|
38275
|
-
|
|
38276
|
-
|
|
38277
|
-
|
|
38278
|
-
|
|
38279
|
-
|
|
38280
|
-
|
|
38281
|
-
|
|
38282
|
-
|
|
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
|
-
|
|
38297
|
-
|
|
38298
|
-
|
|
38299
|
-
|
|
38300
|
-
|
|
38301
|
-
|
|
38302
|
-
|
|
38303
|
-
|
|
38304
|
-
|
|
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
|
-
|
|
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
|
-
|
|
38313
|
-
|
|
38314
|
-
|
|
38315
|
-
|
|
38316
|
-
|
|
38317
|
-
|
|
38318
|
-
|
|
38319
|
-
|
|
38320
|
-
|
|
38321
|
-
|
|
38322
|
-
|
|
38323
|
-
|
|
38324
|
-
|
|
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
|
-
|
|
38329
|
-
|
|
38330
|
-
|
|
38331
|
-
|
|
38332
|
-
|
|
38333
|
-
|
|
38334
|
-
|
|
38335
|
-
|
|
38336
|
-
|
|
38337
|
-
|
|
38338
|
-
|
|
38339
|
-
|
|
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
|
-
|
|
38388
|
-
|
|
38389
|
-
|
|
38390
|
-
|
|
38391
|
-
|
|
38392
|
-
|
|
38393
|
-
|
|
38394
|
-
|
|
38395
|
-
|
|
38396
|
-
|
|
38397
|
-
|
|
38398
|
-
|
|
38399
|
-
|
|
38400
|
-
|
|
38401
|
-
|
|
38402
|
-
|
|
38403
|
-
|
|
38404
|
-
|
|
38405
|
-
|
|
38406
|
-
|
|
38407
|
-
|
|
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
|
-
|
|
38443
|
-
|
|
38444
|
-
return { claimed: false, holder: held.data.holder };
|
|
38296
|
+
reading.read = result.next_after;
|
|
38297
|
+
continue;
|
|
38445
38298
|
}
|
|
38446
|
-
|
|
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
|
-
|
|
38449
|
-
|
|
38450
|
-
|
|
38451
|
-
|
|
38452
|
-
|
|
38453
|
-
|
|
38454
|
-
|
|
38455
|
-
|
|
38456
|
-
|
|
38457
|
-
|
|
38458
|
-
|
|
38459
|
-
|
|
38460
|
-
|
|
38461
|
-
|
|
38462
|
-
|
|
38463
|
-
|
|
38464
|
-
|
|
38465
|
-
|
|
38466
|
-
|
|
38467
|
-
|
|
38468
|
-
|
|
38469
|
-
|
|
38470
|
-
|
|
38471
|
-
|
|
38472
|
-
}
|
|
38473
|
-
|
|
38474
|
-
|
|
38475
|
-
|
|
38476
|
-
|
|
38477
|
-
|
|
38478
|
-
|
|
38479
|
-
|
|
38480
|
-
|
|
38481
|
-
|
|
38482
|
-
|
|
38483
|
-
|
|
38484
|
-
|
|
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
|
-
|
|
38357
|
+
await sweepOnce();
|
|
38554
38358
|
} catch (error) {
|
|
38555
|
-
|
|
38556
|
-
host.log(`replay: ${what} failed`, error);
|
|
38557
|
-
} catch {}
|
|
38359
|
+
log("replay: a sweep failed", error);
|
|
38558
38360
|
}
|
|
38559
38361
|
};
|
|
38560
|
-
const
|
|
38561
|
-
|
|
38562
|
-
|
|
38563
|
-
|
|
38564
|
-
|
|
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
|
|
38574
|
-
const
|
|
38575
|
-
|
|
38576
|
-
if (
|
|
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
|
|
38579
|
-
|
|
38580
|
-
|
|
38581
|
-
|
|
38582
|
-
|
|
38583
|
-
|
|
38584
|
-
|
|
38585
|
-
|
|
38586
|
-
|
|
38587
|
-
|
|
38588
|
-
|
|
38589
|
-
|
|
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
|
-
|
|
38593
|
-
|
|
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
|
|
38600
|
-
|
|
38601
|
-
|
|
38602
|
-
|
|
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
|
-
|
|
38605
|
-
|
|
38606
|
-
|
|
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
|
-
|
|
38609
|
-
|
|
38610
|
-
|
|
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
|
-
|
|
38613
|
-
|
|
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
|
-
|
|
38617
|
-
|
|
38468
|
+
const alive = bind()?.alive;
|
|
38469
|
+
if (alive === undefined)
|
|
38470
|
+
return;
|
|
38618
38471
|
try {
|
|
38619
|
-
await
|
|
38620
|
-
|
|
38621
|
-
|
|
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
|
-
|
|
38635
|
-
|
|
38636
|
-
|
|
38637
|
-
|
|
38638
|
-
|
|
38639
|
-
|
|
38640
|
-
|
|
38641
|
-
|
|
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
|
-
|
|
38645
|
-
|
|
38646
|
-
|
|
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
|
-
|
|
38650
|
-
|
|
38651
|
-
|
|
38652
|
-
|
|
38653
|
-
|
|
38654
|
-
|
|
38655
|
-
|
|
38656
|
-
|
|
38657
|
-
|
|
38658
|
-
|
|
38659
|
-
const
|
|
38660
|
-
|
|
38661
|
-
|
|
38662
|
-
|
|
38663
|
-
|
|
38664
|
-
|
|
38665
|
-
|
|
38666
|
-
|
|
38667
|
-
|
|
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
|
-
|
|
38678
|
-
|
|
38679
|
-
|
|
38680
|
-
|
|
38681
|
-
|
|
38682
|
-
|
|
38683
|
-
|
|
38684
|
-
|
|
38685
|
-
|
|
38686
|
-
|
|
38687
|
-
|
|
38688
|
-
|
|
38689
|
-
|
|
38690
|
-
|
|
38691
|
-
|
|
38692
|
-
|
|
38693
|
-
|
|
38694
|
-
|
|
38695
|
-
|
|
38696
|
-
|
|
38697
|
-
|
|
38698
|
-
|
|
38699
|
-
|
|
38700
|
-
|
|
38701
|
-
const
|
|
38702
|
-
if (
|
|
38703
|
-
return
|
|
38704
|
-
|
|
38705
|
-
|
|
38706
|
-
|
|
38707
|
-
|
|
38708
|
-
|
|
38709
|
-
|
|
38710
|
-
|
|
38711
|
-
|
|
38712
|
-
|
|
38713
|
-
|
|
38714
|
-
|
|
38715
|
-
|
|
38716
|
-
|
|
38717
|
-
|
|
38718
|
-
|
|
38719
|
-
|
|
38720
|
-
|
|
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 (
|
|
38723
|
-
|
|
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
|
-
|
|
38731
|
-
|
|
38732
|
-
|
|
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
|
-
|
|
38735
|
-
|
|
38736
|
-
|
|
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
|
|
38746
|
-
|
|
38747
|
-
|
|
38748
|
-
|
|
38749
|
-
|
|
38750
|
-
|
|
38751
|
-
|
|
38752
|
-
|
|
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
|
|
38775
|
-
|
|
38776
|
-
|
|
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
|
|
38830
|
+
return earliest;
|
|
38779
38831
|
};
|
|
38780
|
-
const
|
|
38781
|
-
|
|
38782
|
-
|
|
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
|
|
38788
|
-
|
|
38789
|
-
if (binding === undefined)
|
|
38844
|
+
const settle = (entry, body) => {
|
|
38845
|
+
if (entry.settled)
|
|
38790
38846
|
return;
|
|
38791
|
-
|
|
38792
|
-
|
|
38793
|
-
|
|
38794
|
-
for (const
|
|
38795
|
-
|
|
38796
|
-
|
|
38797
|
-
|
|
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
|
-
|
|
38801
|
-
|
|
38802
|
-
|
|
38803
|
-
if (
|
|
38804
|
-
|
|
38805
|
-
|
|
38806
|
-
|
|
38807
|
-
|
|
38808
|
-
|
|
38809
|
-
|
|
38810
|
-
|
|
38811
|
-
|
|
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
|
-
|
|
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
|
-
|
|
38816
|
-
|
|
38817
|
-
|
|
38818
|
-
|
|
38819
|
-
|
|
38820
|
-
|
|
38821
|
-
|
|
38822
|
-
|
|
38823
|
-
|
|
38824
|
-
|
|
38825
|
-
|
|
38826
|
-
|
|
38827
|
-
|
|
38828
|
-
|
|
38829
|
-
|
|
38830
|
-
|
|
38831
|
-
|
|
38832
|
-
|
|
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 (
|
|
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
|
-
|
|
38964
|
-
|
|
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
|
-
|
|
38979
|
-
|
|
38980
|
-
|
|
38981
|
-
|
|
38982
|
-
|
|
38983
|
-
|
|
38984
|
-
|
|
38985
|
-
|
|
38986
|
-
|
|
38987
|
-
|
|
38988
|
-
|
|
38989
|
-
|
|
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
|
-
|
|
38992
|
-
if (![...readings.values()].some((reading) => reading.more))
|
|
38993
|
-
break;
|
|
38951
|
+
switchStream(frame.streamID);
|
|
38994
38952
|
}
|
|
38995
|
-
|
|
38996
|
-
|
|
38997
|
-
|
|
38998
|
-
|
|
38999
|
-
|
|
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
|
-
|
|
39004
|
-
|
|
39005
|
-
|
|
39006
|
-
|
|
39007
|
-
|
|
39008
|
-
return
|
|
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
|
-
|
|
39011
|
-
|
|
39012
|
-
|
|
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
|
-
|
|
38985
|
+
entries.set(key, entry);
|
|
38986
|
+
queue.push(entry);
|
|
38987
|
+
pump();
|
|
39015
38988
|
};
|
|
39016
|
-
const
|
|
39017
|
-
|
|
39018
|
-
const binding = bind();
|
|
39019
|
-
if (binding === undefined)
|
|
38989
|
+
const resumePoint = () => {
|
|
38990
|
+
if (streamID === undefined)
|
|
39020
38991
|
return;
|
|
39021
|
-
|
|
39022
|
-
|
|
39023
|
-
|
|
39024
|
-
|
|
39025
|
-
|
|
39026
|
-
|
|
39027
|
-
|
|
39028
|
-
|
|
39029
|
-
|
|
39030
|
-
|
|
39031
|
-
|
|
39032
|
-
|
|
39033
|
-
|
|
39034
|
-
|
|
39035
|
-
|
|
39036
|
-
|
|
39037
|
-
|
|
39038
|
-
|
|
39039
|
-
|
|
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
|
-
|
|
39042
|
-
|
|
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
|
-
|
|
39045
|
-
|
|
39046
|
-
|
|
39047
|
-
|
|
39048
|
-
|
|
39049
|
-
|
|
39050
|
-
|
|
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
|
-
|
|
39053
|
-
|
|
39068
|
+
const next = new Map;
|
|
39069
|
+
for (const topic of topics) {
|
|
39070
|
+
if (topic === ownSubject)
|
|
39054
39071
|
continue;
|
|
39055
|
-
|
|
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
|
-
|
|
39064
|
-
|
|
39065
|
-
|
|
39066
|
-
|
|
39067
|
-
|
|
39068
|
-
|
|
39069
|
-
|
|
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
|
-
|
|
39072
|
-
|
|
39073
|
-
|
|
39074
|
-
|
|
39075
|
-
|
|
39076
|
-
|
|
39077
|
-
|
|
39078
|
-
|
|
39079
|
-
|
|
39080
|
-
|
|
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
|
-
|
|
39090
|
-
|
|
39091
|
-
|
|
39092
|
-
|
|
39093
|
-
|
|
39094
|
-
|
|
39095
|
-
|
|
39096
|
-
|
|
39097
|
-
|
|
39098
|
-
const
|
|
39099
|
-
|
|
39100
|
-
|
|
39101
|
-
|
|
39102
|
-
|
|
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
|
-
|
|
39110
|
-
|
|
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
|
-
|
|
39136
|
-
|
|
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
|
-
|
|
39139
|
-
|
|
39140
|
-
|
|
39141
|
-
|
|
39142
|
-
|
|
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
|
-
|
|
39181
|
-
|
|
39182
|
-
|
|
39183
|
-
|
|
39184
|
-
|
|
39185
|
-
|
|
39186
|
-
|
|
39187
|
-
|
|
39188
|
-
|
|
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
|
-
|
|
39207
|
-
|
|
39208
|
-
|
|
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
|
|
39211
|
-
|
|
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
|
-
|
|
39214
|
-
|
|
39215
|
-
|
|
39216
|
-
|
|
39217
|
-
|
|
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
|
|
39223
|
-
if (
|
|
39224
|
-
|
|
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
|
-
|
|
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
|
|
39233
|
-
|
|
39234
|
-
|
|
39235
|
-
|
|
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
|
|
39239
|
-
|
|
39240
|
-
|
|
39241
|
-
|
|
39242
|
-
|
|
39243
|
-
|
|
39244
|
-
|
|
39245
|
-
|
|
39246
|
-
|
|
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
|
-
|
|
39249
|
-
return result.success ? result.data : undefined;
|
|
39264
|
+
return nonEmpty(env[HARNESS_DIRECTORY_VARIABLE[harness]]);
|
|
39250
39265
|
}
|
|
39251
|
-
function
|
|
39252
|
-
|
|
39253
|
-
|
|
39254
|
-
|
|
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
|
-
|
|
39316
|
-
|
|
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
|
-
|
|
39321
|
-
|
|
39322
|
-
|
|
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
|
|
39341
|
-
const
|
|
39342
|
-
if (
|
|
39343
|
-
return;
|
|
39344
|
-
const
|
|
39345
|
-
|
|
39346
|
-
|
|
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
|
-
|
|
39349
|
-
|
|
39350
|
-
|
|
39351
|
-
|
|
39352
|
-
|
|
39353
|
-
|
|
39354
|
-
|
|
39355
|
-
|
|
39356
|
-
|
|
39357
|
-
|
|
39358
|
-
|
|
39359
|
-
|
|
39360
|
-
|
|
39361
|
-
|
|
39362
|
-
|
|
39363
|
-
|
|
39364
|
-
|
|
39365
|
-
|
|
39366
|
-
|
|
39367
|
-
|
|
39368
|
-
|
|
39369
|
-
|
|
39370
|
-
|
|
39371
|
-
|
|
39372
|
-
|
|
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
|
|
39386
|
-
return
|
|
39322
|
+
function offWithError(error) {
|
|
39323
|
+
return { enabled: false, notice: null, error };
|
|
39387
39324
|
}
|
|
39388
|
-
|
|
39389
|
-
|
|
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
|
-
|
|
39328
|
+
url2 = new URL(value);
|
|
39400
39329
|
} catch {
|
|
39401
39330
|
return;
|
|
39402
39331
|
}
|
|
39403
|
-
|
|
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
|
|
39410
|
-
|
|
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
|
|
39413
|
-
|
|
39414
|
-
|
|
39345
|
+
function danglingLink(filePath) {
|
|
39346
|
+
try {
|
|
39347
|
+
return lstatSync(filePath).isSymbolicLink() ? readlinkSync2(filePath) : null;
|
|
39348
|
+
} catch {
|
|
39349
|
+
return null;
|
|
39350
|
+
}
|
|
39415
39351
|
}
|
|
39416
|
-
function
|
|
39417
|
-
|
|
39418
|
-
|
|
39419
|
-
|
|
39420
|
-
|
|
39421
|
-
|
|
39422
|
-
|
|
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
|
-
|
|
39515
|
-
|
|
39516
|
-
|
|
39517
|
-
|
|
39518
|
-
|
|
39519
|
-
|
|
39520
|
-
|
|
39521
|
-
|
|
39522
|
-
|
|
39523
|
-
|
|
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
|
-
|
|
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 =
|
|
39534
|
-
|
|
39535
|
-
|
|
39536
|
-
|
|
39537
|
-
|
|
39538
|
-
|
|
39539
|
-
|
|
39540
|
-
|
|
39541
|
-
|
|
39542
|
-
}
|
|
39543
|
-
|
|
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
|
-
|
|
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
|
-
|
|
39578
|
-
|
|
39579
|
-
|
|
39580
|
-
|
|
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
|
-
|
|
39583
|
-
|
|
39584
|
-
|
|
39585
|
-
|
|
39586
|
-
|
|
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
|
|
39595
|
-
|
|
39596
|
-
|
|
39597
|
-
|
|
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
|
-
|
|
39601
|
-
|
|
39602
|
-
|
|
39603
|
-
|
|
39604
|
-
|
|
39605
|
-
|
|
39606
|
-
|
|
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
|
-
|
|
39609
|
-
|
|
39610
|
-
|
|
39611
|
-
|
|
39612
|
-
|
|
39613
|
-
|
|
39614
|
-
|
|
39615
|
-
|
|
39616
|
-
|
|
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
|
-
|
|
39650
|
-
|
|
39651
|
-
|
|
39652
|
-
|
|
39653
|
-
|
|
39654
|
-
|
|
39655
|
-
|
|
39656
|
-
|
|
39657
|
-
|
|
39658
|
-
|
|
39659
|
-
|
|
39660
|
-
|
|
39661
|
-
|
|
39662
|
-
|
|
39663
|
-
|
|
39664
|
-
|
|
39665
|
-
|
|
39666
|
-
|
|
39667
|
-
|
|
39668
|
-
|
|
39669
|
-
|
|
39670
|
-
|
|
39671
|
-
|
|
39672
|
-
|
|
39673
|
-
|
|
39674
|
-
|
|
39675
|
-
|
|
39676
|
-
|
|
39677
|
-
|
|
39678
|
-
|
|
39679
|
-
|
|
39680
|
-
|
|
39681
|
-
|
|
39682
|
-
|
|
39683
|
-
|
|
39684
|
-
|
|
39685
|
-
|
|
39686
|
-
|
|
39687
|
-
|
|
39688
|
-
|
|
39689
|
-
|
|
39690
|
-
|
|
39691
|
-
|
|
39692
|
-
|
|
39693
|
-
|
|
39694
|
-
|
|
39695
|
-
|
|
39696
|
-
|
|
39697
|
-
|
|
39698
|
-
|
|
39699
|
-
|
|
39700
|
-
|
|
39701
|
-
|
|
39702
|
-
|
|
39703
|
-
|
|
39704
|
-
|
|
39705
|
-
|
|
39706
|
-
|
|
39707
|
-
|
|
39708
|
-
|
|
39709
|
-
|
|
39710
|
-
|
|
39711
|
-
|
|
39712
|
-
|
|
39713
|
-
|
|
39714
|
-
|
|
39715
|
-
|
|
39716
|
-
|
|
39717
|
-
|
|
39718
|
-
|
|
39719
|
-
|
|
39720
|
-
|
|
39721
|
-
|
|
39722
|
-
|
|
39723
|
-
|
|
39724
|
-
|
|
39725
|
-
|
|
39726
|
-
|
|
39727
|
-
|
|
39728
|
-
|
|
39729
|
-
|
|
39730
|
-
|
|
39731
|
-
|
|
39732
|
-
|
|
39733
|
-
|
|
39734
|
-
|
|
39735
|
-
|
|
39736
|
-
|
|
39737
|
-
|
|
39738
|
-
|
|
39739
|
-
|
|
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
|
-
|
|
39744
|
-
|
|
39745
|
-
if (
|
|
39746
|
-
|
|
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
|
-
|
|
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
|
|
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 =
|
|
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))
|
|
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.
|
|
42709
|
+
var version2 = "16.41.1";
|
|
42717
42710
|
var package_default = {
|
|
42718
42711
|
name,
|
|
42719
42712
|
version: version2,
|