@sjawhar/pi-envoy 16.40.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 +42 -28
- package/dist/envoy.js +2239 -2230
- package/dist/skills/dispatch/SKILL.md +6 -7
- package/package.json +1 -1
package/dist/envoy.js
CHANGED
|
@@ -29799,6 +29799,9 @@ var askEventPayloadFields = {
|
|
|
29799
29799
|
actor: object({ kind: string2().optional(), id: string2().optional() }).passthrough().optional(),
|
|
29800
29800
|
at: string2().optional()
|
|
29801
29801
|
}).nullish(),
|
|
29802
|
+
asked_of: string2().nullish(),
|
|
29803
|
+
asked_of_by: string2().nullish(),
|
|
29804
|
+
asked_of_at: string2().nullish(),
|
|
29802
29805
|
thread_logins: array(string2()).optional()
|
|
29803
29806
|
};
|
|
29804
29807
|
var AskEventPayloadSchema = intersection(object(askEventPayloadFields), object({
|
|
@@ -30064,6 +30067,8 @@ var BOARD_NEIGHBOUR_REFUSAL = "The server refuses a key that does not exist, one
|
|
|
30064
30067
|
var OWNER_REFERENCE = "Exactly one of issue and project is required. An issue is a native KEY or external owner/repo#n reference; a project is a project key such as CORE and addresses an unlinked project document named by artifact.";
|
|
30065
30068
|
var ASK_QUESTION_CONTRACT = "The question names the concrete case first: which machines, files, sessions or people, and the " + "numbers. It asks the choice in those terms, saying what happens to whom under each option " + '("Ship Friday and risk signing out 1,240 users, or hold to Monday?"), never "How should we ' + 'proceed?" or "Which option?", because a reader cannot work back from a general question to ' + "the case. It carries every fact the reader needs to decide and nothing " + "that does not change the decision. Generalize only after the specifics, if at all. Then give " + "your recommendation and its reason.";
|
|
30066
30069
|
var ASK_OPTIONS_CONTRACT = "Options carry the genuinely different approaches. Each option has a label, and its description " + "says what that approach costs.";
|
|
30070
|
+
var ASK_IS_FOR_A_PERSON = "An ask is a question for a person, never a request to another agent. Ask an agent by envoy_send or a comment on the issue.";
|
|
30071
|
+
var ASK_WAITS_ON_THE_ASSIGNEE = "An ask waits on the issue's assignee until a person hands it on. No agent can hand one on.";
|
|
30067
30072
|
var HUMAN_AGREED_TO_DOCUMENT = "the human has agreed to every point in the document";
|
|
30068
30073
|
function documentOwnerValidation(requireArtifact, alwaysRequireArtifact = false) {
|
|
30069
30074
|
return {
|
|
@@ -30144,7 +30149,7 @@ var commentValidation = {
|
|
|
30144
30149
|
message: `${commentOwner.message} ${COMMENT_TURN_RULE}`,
|
|
30145
30150
|
refusal: (value) => commentProblem(value) ?? `${commentOwner.message} ${COMMENT_TURN_RULE}`
|
|
30146
30151
|
};
|
|
30147
|
-
var ISSUE_UPDATE_FIELDS_RULE = "Issue update requires at least one field besides issue: status, title, labels, priority, external_links, route, parent, blocked_by, components, repo, before, or after.";
|
|
30152
|
+
var ISSUE_UPDATE_FIELDS_RULE = "Issue update requires at least one field besides issue: status, title, labels, priority, assignee, external_links, route, parent, blocked_by, components, repo, before, or after.";
|
|
30148
30153
|
var ISSUE_UPDATE_DONE_RULE = "status done requires reason, a non-empty note saying why the issue is closing, posted on the issue before it closes so the close carries its note";
|
|
30149
30154
|
var ISSUE_UPDATE_REASON_RULE = "reason goes only with status done";
|
|
30150
30155
|
var ISSUE_UPDATE_RULE = `${ISSUE_UPDATE_FIELDS_RULE} ${ISSUE_UPDATE_DONE_RULE}; ${ISSUE_UPDATE_REASON_RULE}.`;
|
|
@@ -30158,7 +30163,7 @@ function issueUpdateProblem(value) {
|
|
|
30158
30163
|
const status = typeof input.status === "string" ? `status ${input.status}` : "an update without status done";
|
|
30159
30164
|
return `${ISSUE_UPDATE_REASON_RULE}: it is the note posted on the issue as it closes, and ${status} closes nothing. Drop reason, or set status done to close the issue.`;
|
|
30160
30165
|
}
|
|
30161
|
-
const changesAField = typeof input.status === "string" || typeof input.title === "string" || Array.isArray(input.labels) || typeof input.priority === "number" || input.priority === null || Array.isArray(input.external_links) || typeof input.route === "string" || typeof input.parent === "string" || Array.isArray(input.blocked_by) || typeof input.components === "object" && input.components !== null || typeof input.repo === "string" || typeof input.before === "string" || typeof input.after === "string";
|
|
30166
|
+
const changesAField = typeof input.status === "string" || typeof input.title === "string" || Array.isArray(input.labels) || typeof input.priority === "number" || input.priority === null || typeof input.assignee === "string" || input.assignee === null || Array.isArray(input.external_links) || typeof input.route === "string" || typeof input.parent === "string" || Array.isArray(input.blocked_by) || typeof input.components === "object" && input.components !== null || typeof input.repo === "string" || typeof input.before === "string" || typeof input.after === "string";
|
|
30162
30167
|
return changesAField ? undefined : ISSUE_UPDATE_FIELDS_RULE;
|
|
30163
30168
|
}
|
|
30164
30169
|
var messageValidation = {
|
|
@@ -30290,7 +30295,7 @@ var dispatchToolSpecs = [
|
|
|
30290
30295
|
priority: 1,
|
|
30291
30296
|
before: "DSP-2"
|
|
30292
30297
|
},
|
|
30293
|
-
description: "Update an existing issue: move its lifecycle status, retitle it, replace its labels, set " + "its priority, link a URL (the pull request that delivers it, a run, a document), set its " + "route, set or clear its parent, replace the issues it is blocked by, attach it to " + "architecture components, set the repository it targets, or move it on its project's " + "board. Status is one of " + `${ISSUE_STATUSES.join(", ")}; outside Legion, move it yourself as the work advances; inside ` + "Legion the daemon moves it. A non-empty external_links is " + "merged into the issue's existing links by URL, so linking the pull request you just opened " + "keeps every earlier link; an empty external_links removes every link instead. components " + "replaces the issue's own attachment. Closing an issue " + "(status done) requires reason, the note that says why: it is posted on the issue as a " + "message, then the issue closes, so the close carries its note; closing an issue that is " + "already closed is refused (ISSUE_CLOSED) before anything is posted. Reason goes only " + "with status done. Closing an issue withdraws every open ask " + "it holds (resolution kind withdrawn, naming the closer), and reopening it brings exactly " + "those asks back. " + "A closed issue still takes messages and comments, though not asks or artifacts; of this " + "tool's fields it takes only a board move " + "(before, after), components, " + "a reopening status (any status but done), and an external_links write that adds no new " + "link (so a link can move to the issue that replaced it); everything else, priority " + "included, waits for the reopen. " + "priority is yours to set and a human overrides it. before and after move the issue on " + "its project's board: before puts it above that issue, after below, and both together " + "land it between them. A project's board order is one agent's to keep; in a Legion " + "deployment that is the controller. Never reassign an issue a person holds to get a " + "faster answer: who holds it is their call. At least one " + `field besides issue is required. ${ISSUE_REFERENCE}`,
|
|
30298
|
+
description: "Update an existing issue: move its lifecycle status, retitle it, replace its labels, set " + "its priority, set or clear its assignee, link a URL (the pull request that delivers it, a " + "run, a document), set its " + "route, set or clear its parent, replace the issues it is blocked by, attach it to " + "architecture components, set the repository it targets, or move it on its project's " + "board. Status is one of " + `${ISSUE_STATUSES.join(", ")}; outside Legion, move it yourself as the work advances; inside ` + "Legion the daemon moves it. A non-empty external_links is " + "merged into the issue's existing links by URL, so linking the pull request you just opened " + "keeps every earlier link; an empty external_links removes every link instead. components " + "replaces the issue's own attachment. Closing an issue " + "(status done) requires reason, the note that says why: it is posted on the issue as a " + "message, then the issue closes, so the close carries its note; closing an issue that is " + "already closed is refused (ISSUE_CLOSED) before anything is posted. Reason goes only " + "with status done. Closing an issue withdraws every open ask " + "it holds (resolution kind withdrawn, naming the closer), and reopening it brings exactly " + "those asks back. " + "A closed issue still takes messages and comments, though not asks or artifacts; of this " + "tool's fields it takes only a board move " + "(before, after), components, " + "a reopening status (any status but done), and an external_links write that adds no new " + "link (so a link can move to the issue that replaced it); everything else, priority " + "included, waits for the reopen. " + "priority is yours to set and a human overrides it. assignee is the person who answers " + "this issue's asks, and every ask on it waits on them until a person hands that ask on. " + "Passing null unassigns the issue. Set the assignee when a person said who owns the work, as you " + "file the issue or hand it over. before and after move the issue on " + "its project's board: before puts it above that issue, after below, and both together " + "land it between them. A project's board order is one agent's to keep; in a Legion " + "deployment that is the controller. Never reassign an issue a person holds to get a " + "faster answer: who holds it is their call. At least one " + `field besides issue is required. ${ISSUE_REFERENCE}`,
|
|
30294
30299
|
arguments: (z) => ({
|
|
30295
30300
|
issue: z.string().describe(ISSUE_REFERENCE),
|
|
30296
30301
|
status: z.enum(ISSUE_STATUSES).describe("New lifecycle status.").optional(),
|
|
@@ -30298,6 +30303,7 @@ var dispatchToolSpecs = [
|
|
|
30298
30303
|
title: z.string({ min: 1, max: ISSUE_TITLE_MAX }).describe("Replacement title, at most 1,000 characters.").optional(),
|
|
30299
30304
|
labels: z.array(z.string({ min: 1, max: 40 }), { max: 20 }).describe("Replacement label set, at most 20 labels of up to 40 characters; replaces every existing label.").optional(),
|
|
30300
30305
|
priority: z.number({ int: true, min: 0, max: 3 }).nullable().optional().describe("Coarse priority: 0 is P0 (highest) through 3 is P3 (lowest); null clears it."),
|
|
30306
|
+
assignee: z.string().nullable().optional().describe("Email of the person who answers this issue's asks (someone who has signed in to Dispatch); null unassigns the issue. Set it only when a person said who owns the issue, never to move a question off the person holding it."),
|
|
30301
30307
|
external_links: z.array(z.string({ min: 1 })).describe("URLs to link; a non-empty list merges into the issue's existing external links by URL, an empty list removes every link instead.").optional(),
|
|
30302
30308
|
route: z.string().describe("Route the issue to role:<name> or session:<id>; an empty string clears it.").optional(),
|
|
30303
30309
|
parent: z.string().describe("Parent issue key in the same project; an empty string clears the parent.").optional(),
|
|
@@ -30340,7 +30346,7 @@ var dispatchToolSpecs = [
|
|
|
30340
30346
|
}
|
|
30341
30347
|
]
|
|
30342
30348
|
},
|
|
30343
|
-
description: "Open a to-do or permission only a human can give, or a decision that has no document to " + "live in. A question about the design an issue's document records is not this tool: write " + "it into that document as a decision block (dispatch doc-edit inserting an ask block at the " + "end of the section it concerns), at every phase, approved spec or not; the block reaches " + "the Inbox and its answer lands next to its context. Never give an ask an Approve option: a " + "document is approved through dispatch request-approval. Do not use this tool for a status " + "update or discussion; use dispatch message instead. Never create an issue to park a " + "question: the question is an ask on the issue it concerns. " + ASK_QUESTION_CONTRACT + " " + ASK_OPTIONS_CONTRACT + " For an action only a human can perform, state what it changes and risks as constraints. " + "Anything that requires a human to do, including a credential or grant renewal, is an ask, " + "never a message. Measure what you can first, and put the measurement and the size of the " + "population it covers in the question; drop an option that cannot fix most of that " + "population, never offer it cut down, and keep an option not to act (Hold). Set urgency high " + "when your work halts on the answer, and keep working on everything else. " + "Anchor a to-do about a document passage, thread reply_to/reply_to_ask, or cite a dispatch:// " + `reference \u2014 it must be answerable from its own text and anchor alone, never "see above". ` + `A quote anchor is pinned to its block. ${ASK_BOUND_RULE} The lines images appends count ` + `toward that bound, and an ask has at most 8 options. ${RELATED_CHECK_RESULT} ` + `An ask among the related items says whether it is answered, by whom and with which options. ${OWNER_REFERENCE}`,
|
|
30349
|
+
description: `${ASK_IS_FOR_A_PERSON} ${ASK_WAITS_ON_THE_ASSIGNEE} ` + "Open a to-do or permission only a human can give, or a decision that has no document to " + "live in. A question about the design an issue's document records is not this tool: write " + "it into that document as a decision block (dispatch doc-edit inserting an ask block at the " + "end of the section it concerns), at every phase, approved spec or not; the block reaches " + "the Inbox and its answer lands next to its context. Never give an ask an Approve option: a " + "document is approved through dispatch request-approval. Do not use this tool for a status " + "update or discussion; use dispatch message instead. Never create an issue to park a " + "question: the question is an ask on the issue it concerns. " + ASK_QUESTION_CONTRACT + " " + ASK_OPTIONS_CONTRACT + " For an action only a human can perform, state what it changes and risks as constraints. " + "Anything that requires a human to do, including a credential or grant renewal, is an ask, " + "never a message. Measure what you can first, and put the measurement and the size of the " + "population it covers in the question; drop an option that cannot fix most of that " + "population, never offer it cut down, and keep an option not to act (Hold). Set urgency high " + "when your work halts on the answer, and keep working on everything else. " + "Anchor a to-do about a document passage, thread reply_to/reply_to_ask, or cite a dispatch:// " + `reference \u2014 it must be answerable from its own text and anchor alone, never "see above". ` + `A quote anchor is pinned to its block. ${ASK_BOUND_RULE} The lines images appends count ` + `toward that bound, and an ask has at most 8 options. ${RELATED_CHECK_RESULT} ` + `An ask among the related items says whether it is answered, by whom and with which options. ${OWNER_REFERENCE}`,
|
|
30344
30350
|
arguments: (z) => ({
|
|
30345
30351
|
issue: z.string().describe(ISSUE_REFERENCE).optional(),
|
|
30346
30352
|
project: z.string().describe("Project key owning the document.").optional(),
|
|
@@ -30378,7 +30384,7 @@ var dispatchToolSpecs = [
|
|
|
30378
30384
|
}
|
|
30379
30385
|
]
|
|
30380
30386
|
},
|
|
30381
|
-
description: "Edit an open question in place. Use it to correct or refine the same decision; retract the " + "old ask and open a new one when the decision itself changes. " + ASK_QUESTION_CONTRACT + " " + ASK_OPTIONS_CONTRACT + " Previous text remains in the event log. Only the asking session can edit it; answered or " + "resolved asks cannot be edited. An ask that lives as an `ask` block in a document is written " + "in the document too, changing only the fields you name - pass urgency alone and the " + "question's wording, formatting, links and comment anchors are untouched - so the edit " + "writes a document version and closes a spec's design gate until that version is approved; " + "text the block cannot carry back unchanged is refused, naming the field - an option label " + 'containing ": ", the separator between a label and its description, is one example.',
|
|
30387
|
+
description: `${ASK_IS_FOR_A_PERSON} ${ASK_WAITS_ON_THE_ASSIGNEE} ` + "Edit an open question in place. Use it to correct or refine the same decision; retract the " + "old ask and open a new one when the decision itself changes. " + ASK_QUESTION_CONTRACT + " " + ASK_OPTIONS_CONTRACT + " Previous text remains in the event log. Only the asking session can edit it; answered or " + "resolved asks cannot be edited. An ask that lives as an `ask` block in a document is written " + "in the document too, changing only the fields you name - pass urgency alone and the " + "question's wording, formatting, links and comment anchors are untouched - so the edit " + "writes a document version and closes a spec's design gate until that version is approved; " + "text the block cannot carry back unchanged is refused, naming the field - an option label " + 'containing ": ", the separator between a label and its description, is one example.',
|
|
30382
30388
|
arguments: (z) => ({
|
|
30383
30389
|
ask: z.string().describe("Ask id (uuid); an 8+ hex prefix unique among this session's own open asks works too."),
|
|
30384
30390
|
question: z.string().optional().describe(`${ASK_QUESTION_CONTRACT} Replaces the ask's question. ${ASK_BOUND_RULE}`),
|
|
@@ -33211,6 +33217,14 @@ function projectedToken(path) {
|
|
|
33211
33217
|
};
|
|
33212
33218
|
}
|
|
33213
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
|
+
|
|
33214
33228
|
// ../../node_modules/.bun/oauth4webapi@3.8.8/node_modules/oauth4webapi/build/index.js
|
|
33215
33229
|
var USER_AGENT;
|
|
33216
33230
|
if (typeof navigator === "undefined" || !navigator.userAgent?.startsWith?.("Mozilla/5.0 ")) {
|
|
@@ -34301,12 +34315,6 @@ function statusOf(error) {
|
|
|
34301
34315
|
}
|
|
34302
34316
|
return 0;
|
|
34303
34317
|
}
|
|
34304
|
-
function withoutTrailingSlashes(url) {
|
|
34305
|
-
let end = url.length;
|
|
34306
|
-
while (end > 0 && url[end - 1] === "/")
|
|
34307
|
-
end -= 1;
|
|
34308
|
-
return url.slice(0, end);
|
|
34309
|
-
}
|
|
34310
34318
|
|
|
34311
34319
|
class BrokerTokenClient {
|
|
34312
34320
|
#brokerUrl;
|
|
@@ -35693,7 +35701,7 @@ class DispatchClient {
|
|
|
35693
35701
|
this.fetchImpl = fetchImpl;
|
|
35694
35702
|
this.writeRetry = writeRetry;
|
|
35695
35703
|
this.#credential = credential;
|
|
35696
|
-
this.#baseUrl = baseUrl
|
|
35704
|
+
this.#baseUrl = withoutTrailingSlashes(baseUrl);
|
|
35697
35705
|
this.#signal = requestSignal(signal);
|
|
35698
35706
|
this.#madeAt = this.#now();
|
|
35699
35707
|
}
|
|
@@ -36829,6 +36837,7 @@ var DISPATCH_PAYLOAD_SCHEMAS = {
|
|
|
36829
36837
|
"ask.anchor_refreshed": AskEventPayloadSchema,
|
|
36830
36838
|
"ask.edited": AskEditedEventPayloadSchema,
|
|
36831
36839
|
"ask.handed_back": AskEventPayloadSchema,
|
|
36840
|
+
"ask.asked_of": AskEventPayloadSchema,
|
|
36832
36841
|
"ask.answered": AskEventPayloadSchema,
|
|
36833
36842
|
"ask.resolved": AskEventPayloadSchema,
|
|
36834
36843
|
"comment.created": CommentEventPayloadSchema,
|
|
@@ -36948,6 +36957,15 @@ function dispatchCompact(event, comment) {
|
|
|
36948
36957
|
};
|
|
36949
36958
|
break;
|
|
36950
36959
|
}
|
|
36960
|
+
case "ask.asked_of": {
|
|
36961
|
+
const handedOnBy = ask.data.asked_of_by;
|
|
36962
|
+
record = {
|
|
36963
|
+
...record,
|
|
36964
|
+
asked_of: ask.data.asked_of ?? "the assignee of its issue",
|
|
36965
|
+
...handedOnBy === null || handedOnBy === undefined || handedOnBy === "" ? {} : { handed_on_by: handedOnBy }
|
|
36966
|
+
};
|
|
36967
|
+
break;
|
|
36968
|
+
}
|
|
36951
36969
|
case "ask.resolved": {
|
|
36952
36970
|
const resolved = askResolutionText(ask.data.resolution);
|
|
36953
36971
|
record = resolved === undefined ? record : { ...record, resolved };
|
|
@@ -37352,2395 +37370,2259 @@ function streamStores(topic) {
|
|
|
37352
37370
|
return STREAM_SUBJECT_PREFIXES.some((prefix) => topic.length > prefix.length && topic.startsWith(prefix));
|
|
37353
37371
|
}
|
|
37354
37372
|
|
|
37355
|
-
// ../envoy-client/src/
|
|
37356
|
-
import {
|
|
37357
|
-
|
|
37358
|
-
|
|
37359
|
-
existsSync,
|
|
37360
|
-
lstatSync,
|
|
37361
|
-
mkdtempSync,
|
|
37362
|
-
openSync as openSync2,
|
|
37363
|
-
readFileSync as readFileSync3,
|
|
37364
|
-
readlinkSync as readlinkSync2,
|
|
37365
|
-
rmSync as rmSync2,
|
|
37366
|
-
statSync as statSync2
|
|
37367
|
-
} from "fs";
|
|
37368
|
-
import { homedir, tmpdir } from "os";
|
|
37369
|
-
import * as path from "path";
|
|
37370
|
-
|
|
37371
|
-
// ../envoy-client/src/json-position.ts
|
|
37372
|
-
function jsonErrorPosition(text) {
|
|
37373
|
-
let at = 0;
|
|
37374
|
-
const fail = () => {
|
|
37375
|
-
throw new RangeError("not JSON");
|
|
37376
|
-
};
|
|
37377
|
-
const space = () => {
|
|
37378
|
-
while (at < text.length && `
|
|
37379
|
-
\r`.includes(text.charAt(at)))
|
|
37380
|
-
at += 1;
|
|
37381
|
-
};
|
|
37382
|
-
const expect = (char) => {
|
|
37383
|
-
if (text.charAt(at) !== char)
|
|
37384
|
-
fail();
|
|
37385
|
-
at += 1;
|
|
37386
|
-
};
|
|
37387
|
-
const string = () => {
|
|
37388
|
-
expect('"');
|
|
37389
|
-
while (at < text.length) {
|
|
37390
|
-
const char = text.charAt(at);
|
|
37391
|
-
if (char === '"') {
|
|
37392
|
-
at += 1;
|
|
37393
|
-
return;
|
|
37394
|
-
}
|
|
37395
|
-
if (char < " ")
|
|
37396
|
-
fail();
|
|
37397
|
-
at += char === "\\" ? 2 : 1;
|
|
37398
|
-
}
|
|
37399
|
-
fail();
|
|
37400
|
-
};
|
|
37401
|
-
const value = () => {
|
|
37402
|
-
space();
|
|
37403
|
-
const char = text.charAt(at);
|
|
37404
|
-
if (char === "{" || char === "[") {
|
|
37405
|
-
const close = char === "{" ? "}" : "]";
|
|
37406
|
-
at += 1;
|
|
37407
|
-
space();
|
|
37408
|
-
if (text.charAt(at) === close) {
|
|
37409
|
-
at += 1;
|
|
37410
|
-
return;
|
|
37411
|
-
}
|
|
37412
|
-
for (;; ) {
|
|
37413
|
-
if (close === "}") {
|
|
37414
|
-
space();
|
|
37415
|
-
string();
|
|
37416
|
-
space();
|
|
37417
|
-
expect(":");
|
|
37418
|
-
}
|
|
37419
|
-
value();
|
|
37420
|
-
space();
|
|
37421
|
-
if (text.charAt(at) === ",") {
|
|
37422
|
-
at += 1;
|
|
37423
|
-
continue;
|
|
37424
|
-
}
|
|
37425
|
-
expect(close);
|
|
37426
|
-
return;
|
|
37427
|
-
}
|
|
37428
|
-
}
|
|
37429
|
-
if (char === '"') {
|
|
37430
|
-
string();
|
|
37431
|
-
return;
|
|
37432
|
-
}
|
|
37433
|
-
for (const word of ["true", "false", "null"]) {
|
|
37434
|
-
if (text.startsWith(word, at)) {
|
|
37435
|
-
at += word.length;
|
|
37436
|
-
return;
|
|
37437
|
-
}
|
|
37438
|
-
}
|
|
37439
|
-
const number = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/.exec(text.slice(at));
|
|
37440
|
-
if (number === null)
|
|
37441
|
-
fail();
|
|
37442
|
-
at += number?.[0].length ?? 0;
|
|
37443
|
-
};
|
|
37444
|
-
try {
|
|
37445
|
-
value();
|
|
37446
|
-
space();
|
|
37447
|
-
if (at < text.length)
|
|
37448
|
-
fail();
|
|
37449
|
-
return;
|
|
37450
|
-
} catch {
|
|
37451
|
-
const before = text.slice(0, at).split(`
|
|
37452
|
-
`);
|
|
37453
|
-
return `line ${before.length}, column ${(before.at(-1)?.length ?? 0) + 1}`;
|
|
37454
|
-
}
|
|
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();
|
|
37455
37377
|
}
|
|
37456
37378
|
|
|
37457
|
-
// ../envoy-client/src/
|
|
37458
|
-
|
|
37459
|
-
|
|
37460
|
-
|
|
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
|
+
};
|
|
37461
37491
|
}
|
|
37462
|
-
function
|
|
37463
|
-
|
|
37464
|
-
|
|
37465
|
-
|
|
37466
|
-
} catch (error) {
|
|
37467
|
-
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");
|
|
37468
37496
|
}
|
|
37469
|
-
const
|
|
37470
|
-
|
|
37471
|
-
|
|
37472
|
-
if (!sendable(value)) {
|
|
37473
|
-
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");
|
|
37474
37500
|
}
|
|
37475
|
-
return
|
|
37501
|
+
return interests;
|
|
37476
37502
|
}
|
|
37477
|
-
|
|
37478
|
-
|
|
37479
|
-
var COMMAND_TIMEOUT_MS = 1e4;
|
|
37480
|
-
function nonEmpty(value) {
|
|
37481
|
-
return value === undefined || value === "" ? undefined : value;
|
|
37503
|
+
function holderStateOf(role) {
|
|
37504
|
+
return role.holder_state ?? role.state;
|
|
37482
37505
|
}
|
|
37483
|
-
function
|
|
37484
|
-
|
|
37485
|
-
throw new Error(`${name} is ${value}, which is not an absolute path`);
|
|
37486
|
-
}
|
|
37487
|
-
return value;
|
|
37506
|
+
function namedProcess(id) {
|
|
37507
|
+
return id === "" ? undefined : id;
|
|
37488
37508
|
}
|
|
37489
|
-
function
|
|
37490
|
-
|
|
37491
|
-
|
|
37492
|
-
|
|
37493
|
-
|
|
37494
|
-
const configHome = xdgConfigHome ?? path.join(nonEmpty(env.HOME) ?? homedir(), ".config");
|
|
37495
|
-
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);
|
|
37496
37514
|
}
|
|
37497
|
-
|
|
37498
|
-
|
|
37499
|
-
|
|
37500
|
-
|
|
37501
|
-
|
|
37502
|
-
function namedDirectory(harness, env) {
|
|
37503
|
-
if (harness === "omp") {
|
|
37504
|
-
const profile = nonEmpty(env.OMP_PROFILE);
|
|
37505
|
-
if (profile === undefined || profile === "default")
|
|
37506
|
-
return;
|
|
37507
|
-
}
|
|
37508
|
-
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;
|
|
37509
37520
|
}
|
|
37510
|
-
function
|
|
37511
|
-
|
|
37512
|
-
|
|
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);
|
|
37513
37530
|
continue;
|
|
37514
37531
|
}
|
|
37515
|
-
const
|
|
37516
|
-
if (
|
|
37517
|
-
|
|
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);
|
|
37518
37538
|
}
|
|
37519
|
-
return;
|
|
37520
|
-
}
|
|
37521
|
-
function handedConfigPath(env) {
|
|
37522
|
-
const handed = nonEmpty(env.ENVOY_CONFIG_FILE);
|
|
37523
|
-
return handed === undefined ? undefined : path.resolve(absolute("ENVOY_CONFIG_FILE", handed));
|
|
37524
|
-
}
|
|
37525
|
-
function envoyConfigPath(harness, env) {
|
|
37526
|
-
const handedFirst = harness === "command" ? handedConfigPath(env) : undefined;
|
|
37527
|
-
if (handedFirst !== undefined)
|
|
37528
|
-
return handedFirst;
|
|
37529
|
-
const directory = harnessDirectory(harness, env);
|
|
37530
|
-
if (directory !== undefined)
|
|
37531
|
-
return path.resolve(directory, "envoy.json");
|
|
37532
|
-
const handed = handedConfigPath(env);
|
|
37533
|
-
if (handed !== undefined)
|
|
37534
|
-
return handed;
|
|
37535
|
-
const neutral = neutralConfig(env);
|
|
37536
|
-
if ("error" in neutral)
|
|
37537
|
-
throw new Error(neutral.error);
|
|
37538
|
-
return neutral.path;
|
|
37539
|
-
}
|
|
37540
|
-
var httpUrlSchema = url({ protocol: /^https?$/, error: "must be an http or https URL" });
|
|
37541
|
-
var DispatchSectionSchema = strictObject({
|
|
37542
|
-
enabled: boolean2().optional(),
|
|
37543
|
-
serverUrl: httpUrlSchema.optional(),
|
|
37544
|
-
token: string2().optional()
|
|
37545
|
-
});
|
|
37546
|
-
var EnvoySectionSchema = strictObject({
|
|
37547
|
-
enabled: boolean2().optional(),
|
|
37548
|
-
url: httpUrlSchema.optional(),
|
|
37549
|
-
natsUrls: array(string2().trim().min(1, "must not be empty")).optional(),
|
|
37550
|
-
token: string2().optional(),
|
|
37551
|
-
natsSeed: string2().optional()
|
|
37552
|
-
});
|
|
37553
|
-
var EnvoyFileSchema = looseObject({
|
|
37554
|
-
$schema: string2().optional(),
|
|
37555
|
-
dispatch: DispatchSectionSchema.optional(),
|
|
37556
|
-
envoy: EnvoySectionSchema.optional()
|
|
37557
|
-
});
|
|
37558
|
-
var CheckoutFileSchema = looseObject({
|
|
37559
|
-
$schema: string2().optional(),
|
|
37560
|
-
dispatch: DispatchSectionSchema.optional()
|
|
37561
|
-
});
|
|
37562
|
-
var AskgateSectionSchema = strictObject({ enabled: boolean2().optional() });
|
|
37563
|
-
function offWithNotice(notice) {
|
|
37564
|
-
return { enabled: false, notice, error: null };
|
|
37565
|
-
}
|
|
37566
|
-
function offWithError(error) {
|
|
37567
|
-
return { enabled: false, notice: null, error };
|
|
37539
|
+
return [...expanded];
|
|
37568
37540
|
}
|
|
37569
|
-
function
|
|
37570
|
-
|
|
37571
|
-
|
|
37572
|
-
|
|
37573
|
-
|
|
37574
|
-
|
|
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}.`);
|
|
37575
37548
|
}
|
|
37576
|
-
|
|
37577
|
-
|
|
37578
|
-
|
|
37579
|
-
|
|
37580
|
-
if (!issue)
|
|
37581
|
-
return `${filePath}: invalid envoy.json`;
|
|
37582
|
-
if (issue.code === "unrecognized_keys") {
|
|
37583
|
-
const section = issue.path.join(".");
|
|
37584
|
-
const keys = issue.keys.map((key) => `${section}.${key}`).join(", ");
|
|
37585
|
-
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.`);
|
|
37586
37553
|
}
|
|
37587
|
-
|
|
37554
|
+
if (sentences.length === 0)
|
|
37555
|
+
return "";
|
|
37556
|
+
if (state === "unresponsive" || stored.length > 0)
|
|
37557
|
+
sentences.push(UNRESPONSIVE_RULE);
|
|
37558
|
+
return ` ${sentences.join(" ")}`;
|
|
37588
37559
|
}
|
|
37589
|
-
|
|
37590
|
-
|
|
37591
|
-
|
|
37592
|
-
|
|
37593
|
-
|
|
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;
|
|
37594
37587
|
}
|
|
37595
37588
|
}
|
|
37596
|
-
|
|
37597
|
-
|
|
37598
|
-
|
|
37599
|
-
|
|
37600
|
-
|
|
37601
|
-
|
|
37602
|
-
|
|
37603
|
-
|
|
37604
|
-
|
|
37605
|
-
|
|
37606
|
-
|
|
37607
|
-
|
|
37608
|
-
|
|
37609
|
-
|
|
37610
|
-
|
|
37611
|
-
|
|
37612
|
-
|
|
37613
|
-
|
|
37614
|
-
|
|
37615
|
-
|
|
37616
|
-
if (typeof parsedJson === "object" && parsedJson !== null && "natsUrls" in parsedJson) {
|
|
37617
|
-
return {
|
|
37618
|
-
kind: "invalid",
|
|
37619
|
-
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`
|
|
37620
|
-
};
|
|
37621
|
-
}
|
|
37622
|
-
const parsed = schema.safeParse(parsedJson);
|
|
37623
|
-
if (!parsed.success) {
|
|
37624
|
-
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;
|
|
37625
37609
|
}
|
|
37626
|
-
return { kind: "valid", settings: parsed.data };
|
|
37627
37610
|
}
|
|
37628
|
-
|
|
37629
|
-
|
|
37630
|
-
|
|
37631
|
-
|
|
37632
|
-
|
|
37633
|
-
|
|
37634
|
-
|
|
37635
|
-
|
|
37636
|
-
|
|
37637
|
-
|
|
37638
|
-
|
|
37639
|
-
const
|
|
37640
|
-
|
|
37641
|
-
|
|
37642
|
-
|
|
37643
|
-
|
|
37644
|
-
|
|
37645
|
-
|
|
37646
|
-
|
|
37647
|
-
|
|
37648
|
-
|
|
37649
|
-
|
|
37650
|
-
|
|
37651
|
-
|
|
37652
|
-
|
|
37653
|
-
|
|
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;
|
|
37625
|
+
try {
|
|
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)
|
|
37654
37640
|
});
|
|
37655
37641
|
} catch (error) {
|
|
37656
|
-
return {
|
|
37657
|
-
} finally {
|
|
37658
|
-
closeSync2(fd);
|
|
37642
|
+
return { unanswered: error };
|
|
37659
37643
|
}
|
|
37660
|
-
|
|
37661
|
-
process.kill(-result.pid, "SIGKILL");
|
|
37662
|
-
} catch {}
|
|
37663
|
-
if (result.exitedDueToTimeout) {
|
|
37644
|
+
if (unminted !== undefined && (response.status === 401 || response.status === 403)) {
|
|
37664
37645
|
return {
|
|
37665
|
-
|
|
37666
|
-
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 })
|
|
37667
37647
|
};
|
|
37668
37648
|
}
|
|
37669
|
-
|
|
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
|
+
}
|
|
37670
37746
|
return {
|
|
37671
|
-
|
|
37672
|
-
|
|
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
|
|
37673
37753
|
};
|
|
37674
|
-
}
|
|
37675
|
-
|
|
37676
|
-
|
|
37677
|
-
|
|
37678
|
-
|
|
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 })));
|
|
37679
37767
|
return {
|
|
37680
|
-
|
|
37681
|
-
|
|
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 }
|
|
37682
37774
|
};
|
|
37683
|
-
}
|
|
37684
|
-
|
|
37685
|
-
|
|
37686
|
-
|
|
37687
|
-
|
|
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
|
+
};
|
|
37688
37831
|
}
|
|
37689
|
-
function
|
|
37690
|
-
|
|
37691
|
-
|
|
37692
|
-
|
|
37693
|
-
}
|
|
37694
|
-
return value;
|
|
37695
|
-
}
|
|
37696
|
-
const command = value.slice(1);
|
|
37697
|
-
const cwd = path.dirname(path.resolve(filePath));
|
|
37698
|
-
const key = JSON.stringify([command, filePath, cwd]);
|
|
37699
|
-
let outcome = commandOutcomes.get(key);
|
|
37700
|
-
if (commands === "kept") {
|
|
37701
|
-
if (outcome === undefined) {
|
|
37702
|
-
throw new Error(`${filePath}: ${field}'s command has not run in this process yet, and this read does not run it`);
|
|
37703
|
-
}
|
|
37704
|
-
} else if (outcome === undefined || "failure" in outcome && !outcome.timedOut && Date.now() - outcome.at >= FAILURE_KEPT_MS) {
|
|
37705
|
-
outcome = { ...runCommand(command, env, cwd), at: Date.now() };
|
|
37706
|
-
commandOutcomes.set(key, outcome);
|
|
37707
|
-
}
|
|
37708
|
-
if ("failure" in outcome)
|
|
37709
|
-
throw new Error(`${filePath}: ${field}'s command ${outcome.failure}`);
|
|
37710
|
-
return outcome.value;
|
|
37711
|
-
}
|
|
37712
|
-
function statDevice(directory) {
|
|
37713
|
-
try {
|
|
37714
|
-
return statSync2(directory).dev;
|
|
37715
|
-
} catch {
|
|
37716
|
-
return;
|
|
37717
|
-
}
|
|
37718
|
-
}
|
|
37719
|
-
function moveNotice(oldFile, newFile, move, deviceOf) {
|
|
37720
|
-
const oldDevice = deviceOf(path.dirname(oldFile));
|
|
37721
|
-
let ancestor = path.dirname(newFile);
|
|
37722
|
-
let newDevice = deviceOf(ancestor);
|
|
37723
|
-
while (newDevice === undefined && path.dirname(ancestor) !== ancestor) {
|
|
37724
|
-
ancestor = path.dirname(ancestor);
|
|
37725
|
-
newDevice = deviceOf(ancestor);
|
|
37726
|
-
}
|
|
37727
|
-
if (oldDevice !== undefined && newDevice !== undefined && oldDevice !== newDevice) {
|
|
37728
|
-
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`;
|
|
37729
|
-
}
|
|
37730
|
-
return `${oldFile} is not read: ${move}`;
|
|
37731
|
-
}
|
|
37732
|
-
function oldUserFileNotice(filePath, env, deviceOf) {
|
|
37733
|
-
const home = nonEmpty(env.HOME) ?? homedir();
|
|
37734
|
-
const old = path.join(home, ".config", "opencode", "envoy.json");
|
|
37735
|
-
if (!existsSync(old))
|
|
37736
|
-
return;
|
|
37737
|
-
const selected = path.resolve(filePath);
|
|
37738
|
-
const userFile = path.resolve(home, ".config", "legion", "envoy.json");
|
|
37739
|
-
if (selected === userFile) {
|
|
37740
|
-
return moveNotice(old, userFile, "mkdir -p ~/.config/legion && mv ~/.config/opencode/envoy.json ~/.config/legion/", deviceOf);
|
|
37741
|
-
}
|
|
37742
|
-
const profileFile = PLUGIN_HARNESSES.map((harness) => namedDirectory(harness, env)).some((directory) => directory !== undefined && path.isAbsolute(directory) && selected === path.resolve(directory, "envoy.json"));
|
|
37743
|
-
if (profileFile) {
|
|
37744
|
-
return `${old} is not read for this profile; copy or link it to ${filePath}, or write that file`;
|
|
37745
|
-
}
|
|
37746
|
-
const configHome = nonEmpty(env.XDG_CONFIG_HOME);
|
|
37747
|
-
const neutral = neutralConfig(env);
|
|
37748
|
-
if (configHome !== undefined && path.isAbsolute(configHome) && "path" in neutral && selected === neutral.path) {
|
|
37749
|
-
return `${old} is not read under XDG_CONFIG_HOME=${configHome}`;
|
|
37750
|
-
}
|
|
37751
|
-
return;
|
|
37752
|
-
}
|
|
37753
|
-
function missingConfigNotice(filePath, missing, off) {
|
|
37754
|
-
const link = missing.link === null ? "" : ` (a link to ${missing.link}, which does not exist)`;
|
|
37755
|
-
const old = missing.oldUserFileNotice === undefined ? "" : `; ${missing.oldUserFileNotice}`;
|
|
37756
|
-
return `no envoy.json at ${filePath}${link}: ${off}${old}`;
|
|
37832
|
+
function waitForRetry(delayMs) {
|
|
37833
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
37834
|
+
setTimeout(resolve, delayMs);
|
|
37835
|
+
return promise;
|
|
37757
37836
|
}
|
|
37758
|
-
function
|
|
37759
|
-
const deviceOf = options.deviceOf ?? statDevice;
|
|
37760
|
-
const chosen = parseFile(filePath, EnvoyFileSchema, false);
|
|
37761
|
-
if (chosen.kind === "invalid")
|
|
37762
|
-
return { kind: "failed", path: filePath, error: chosen.reason };
|
|
37763
|
-
const resolvedChosen = path.resolve(filePath);
|
|
37764
|
-
const checkoutPath = path.resolve(options.cwd, ".envoy", "envoy.json");
|
|
37765
|
-
const checkoutFile = checkoutPath === resolvedChosen ? { kind: "absent", link: null } : parseFile(checkoutPath, CheckoutFileSchema, true);
|
|
37766
|
-
const own = chosen.kind === "valid" ? chosen.settings : {};
|
|
37767
|
-
let checkout = null;
|
|
37768
|
-
if (checkoutFile.kind === "invalid") {
|
|
37769
|
-
checkout = { kind: "invalid", path: checkoutPath, error: checkoutFile.reason };
|
|
37770
|
-
} else if (checkoutFile.kind === "valid") {
|
|
37771
|
-
checkout = {
|
|
37772
|
-
kind: "valid",
|
|
37773
|
-
path: checkoutPath,
|
|
37774
|
-
dispatch: checkoutFile.settings.dispatch ?? {},
|
|
37775
|
-
envoyError: Object.hasOwn(checkoutFile.settings, "envoy") ? `${checkoutPath}: envoy is not read from a checkout's envoy.json; put it in ${filePath}` : null,
|
|
37776
|
-
askgateError: Object.hasOwn(checkoutFile.settings, "askgate") ? `askgate in ${checkoutPath} is not read; set it in ${filePath}` : null
|
|
37777
|
-
};
|
|
37778
|
-
}
|
|
37779
|
-
const oldCheckout = path.resolve(options.cwd, ".opencode", "envoy.json");
|
|
37780
|
-
const warnings = oldCheckout !== resolvedChosen && !existsSync(checkoutPath) && existsSync(oldCheckout) ? [
|
|
37781
|
-
moveNotice(oldCheckout, checkoutPath, `mkdir -p ${path.dirname(checkoutPath)} && mv ${oldCheckout} ${checkoutPath}`, deviceOf)
|
|
37782
|
-
] : [];
|
|
37783
|
-
const unreadOldUserFile = chosen.kind === "absent" ? oldUserFileNotice(filePath, env, deviceOf) : undefined;
|
|
37837
|
+
function messageMetadata(input) {
|
|
37784
37838
|
return {
|
|
37785
|
-
|
|
37786
|
-
|
|
37787
|
-
|
|
37788
|
-
|
|
37789
|
-
|
|
37790
|
-
missing: chosen.kind === "absent" ? {
|
|
37791
|
-
link: chosen.link,
|
|
37792
|
-
...unreadOldUserFile === undefined ? {} : { oldUserFileNotice: unreadOldUserFile }
|
|
37793
|
-
} : null,
|
|
37794
|
-
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 }
|
|
37795
37844
|
};
|
|
37796
37845
|
}
|
|
37797
|
-
function
|
|
37798
|
-
|
|
37799
|
-
enabled: true,
|
|
37800
|
-
error: `${reason}; AskGate stays on`
|
|
37801
|
-
});
|
|
37802
|
-
let raw;
|
|
37803
|
-
try {
|
|
37804
|
-
raw = readFileSync3(filePath, "utf-8");
|
|
37805
|
-
} catch (error) {
|
|
37806
|
-
if (hasErrnoCode(error, "ENOENT") || hasErrnoCode(error, "ENOTDIR")) {
|
|
37807
|
-
return { enabled: true, error: null };
|
|
37808
|
-
}
|
|
37809
|
-
return stayOn(`${filePath}: could not be read (${messageFor(error)}); askgate.enabled is not read`);
|
|
37810
|
-
}
|
|
37811
|
-
let parsedJson;
|
|
37812
|
-
try {
|
|
37813
|
-
parsedJson = JSON.parse(raw);
|
|
37814
|
-
} catch {
|
|
37815
|
-
const position = jsonErrorPosition(raw);
|
|
37816
|
-
const at = position === undefined ? "" : ` at ${position}`;
|
|
37817
|
-
return stayOn(`${filePath}: invalid JSON${at}; askgate.enabled is not read`);
|
|
37818
|
-
}
|
|
37819
|
-
if (typeof parsedJson !== "object" || parsedJson === null || !("askgate" in parsedJson)) {
|
|
37820
|
-
return { enabled: true, error: null };
|
|
37821
|
-
}
|
|
37822
|
-
const parsed = object({ askgate: AskgateSectionSchema }).safeParse(parsedJson);
|
|
37823
|
-
if (!parsed.success) {
|
|
37824
|
-
const unknownKey = parsed.error.issues[0]?.code === "unrecognized_keys";
|
|
37825
|
-
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`);
|
|
37826
|
-
}
|
|
37827
|
-
return { enabled: parsed.data.askgate.enabled ?? true, error: null };
|
|
37846
|
+
function sourceSession(sessionID) {
|
|
37847
|
+
return sessionID === undefined || sessionID === "" ? {} : { source_session: sessionID };
|
|
37828
37848
|
}
|
|
37829
37849
|
|
|
37830
|
-
// ../envoy-client/src/
|
|
37831
|
-
|
|
37832
|
-
|
|
37833
|
-
|
|
37834
|
-
|
|
37835
|
-
|
|
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
|
+
}
|
|
37836
37918
|
};
|
|
37837
|
-
|
|
37838
|
-
|
|
37839
|
-
const
|
|
37840
|
-
|
|
37841
|
-
|
|
37842
|
-
}
|
|
37843
|
-
const
|
|
37844
|
-
|
|
37845
|
-
|
|
37846
|
-
|
|
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;
|
|
37847
37930
|
}
|
|
37848
|
-
|
|
37849
|
-
|
|
37850
|
-
const
|
|
37851
|
-
|
|
37852
|
-
|
|
37853
|
-
|
|
37854
|
-
|
|
37855
|
-
|
|
37856
|
-
|
|
37857
|
-
|
|
37858
|
-
|
|
37859
|
-
|
|
37860
|
-
|
|
37861
|
-
|
|
37862
|
-
|
|
37863
|
-
|
|
37864
|
-
|
|
37865
|
-
|
|
37866
|
-
}
|
|
37867
|
-
|
|
37868
|
-
// ../envoy-client/src/envoy-settings.ts
|
|
37869
|
-
var DEFAULT_ENVOY_URL = "http://127.0.0.1:9020";
|
|
37870
|
-
var DEFAULT_HEARTBEAT_MS = SESSION_HEARTBEAT_MS;
|
|
37871
|
-
var MIN_PASS_DEADLINE_MS = 30000;
|
|
37872
|
-
var DEFAULT_TMUX_PLACE_CACHE_MS = 30000;
|
|
37873
|
-
var MIN_INTERVAL_MS = 25;
|
|
37874
|
-
var MAX_INTERVAL_MS = 2147483647;
|
|
37875
|
-
var REGISTRATION_SETTLE_MS = 5000;
|
|
37876
|
-
var SHUTDOWN_SETTLE_MS = 250;
|
|
37877
|
-
var HEARTBEAT_RETRY_MIN_MS = 20000;
|
|
37878
|
-
var HEARTBEAT_RETRY_MAX_MS = 40000;
|
|
37879
|
-
function heartbeatRetryDelayMs(draw = Math.random) {
|
|
37880
|
-
return Math.round(HEARTBEAT_RETRY_MIN_MS + draw() * (HEARTBEAT_RETRY_MAX_MS - HEARTBEAT_RETRY_MIN_MS));
|
|
37881
|
-
}
|
|
37882
|
-
function namedInterval(raw) {
|
|
37883
|
-
const parsed = Number(raw);
|
|
37884
|
-
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
|
|
37885
|
-
}
|
|
37886
|
-
function resolvedInterval(raw, fallback) {
|
|
37887
|
-
const named = namedInterval(raw);
|
|
37888
|
-
return named === undefined ? fallback : Math.min(Math.max(named, MIN_INTERVAL_MS), MAX_INTERVAL_MS);
|
|
37889
|
-
}
|
|
37890
|
-
function natsSeedFromEnvironment(env) {
|
|
37891
|
-
const { NATS_NKEY_SEED_FILE: file, NATS_NKEY_SEED: plain } = env;
|
|
37892
|
-
if (file !== undefined) {
|
|
37893
|
-
if (file === "")
|
|
37894
|
-
throw new Error("NATS_NKEY_SEED_FILE is set but empty");
|
|
37895
|
-
return {
|
|
37896
|
-
value: readSecretFile("NATS_NKEY_SEED_FILE", file),
|
|
37897
|
-
source: `NATS_NKEY_SEED_FILE (${file})`
|
|
37898
|
-
};
|
|
37899
|
-
}
|
|
37900
|
-
if (plain === undefined)
|
|
37901
|
-
return;
|
|
37902
|
-
const value = plain.trim();
|
|
37903
|
-
if (value.length === 0)
|
|
37904
|
-
throw new Error("NATS_NKEY_SEED is set but empty");
|
|
37905
|
-
return { value, source: "NATS_NKEY_SEED" };
|
|
37906
|
-
}
|
|
37907
|
-
var NO_NATS_SERVER = "Envoy has no NATS server (envoy.natsUrls, ENVOY_NATS_URL)";
|
|
37908
|
-
var NO_NATS_CONNECTION = "this session opens no NATS connection";
|
|
37909
|
-
function natsServerKey(url) {
|
|
37910
|
-
const match = /^([a-z][a-z0-9+.-]*):\/\/([^@/]*@)?(\[[^\]]*\]|[^:/]*)(?::(\d+))?(\/.*)?$/i.exec(url);
|
|
37911
|
-
if (match === null)
|
|
37912
|
-
return url;
|
|
37913
|
-
const [, scheme = "", userinfo = "", host = "", port = "4222", rest = ""] = match;
|
|
37914
|
-
return `${scheme.toLowerCase()}://${userinfo}${host.toLowerCase()}:${port}${rest.replace(/\/+$/, "")}`;
|
|
37915
|
-
}
|
|
37916
|
-
function natsCredential(env, section, namedNatsUrls, filePath) {
|
|
37917
|
-
const fromEnvironment = natsSeedFromEnvironment(env);
|
|
37918
|
-
if (fromEnvironment !== undefined || section.natsSeed === undefined)
|
|
37919
|
-
return fromEnvironment;
|
|
37920
|
-
if (namedNatsUrls.length > 0) {
|
|
37921
|
-
const servers = new Set((section.natsUrls ?? []).map(natsServerKey));
|
|
37922
|
-
const named = new Set(namedNatsUrls.map(natsServerKey));
|
|
37923
|
-
if (named.size !== servers.size || ![...named].every((server) => servers.has(server))) {
|
|
37924
|
-
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
|
+
};
|
|
37925
37950
|
}
|
|
37926
|
-
|
|
37927
|
-
|
|
37928
|
-
|
|
37929
|
-
|
|
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
|
+
});
|
|
37930
37957
|
};
|
|
37931
|
-
|
|
37932
|
-
|
|
37933
|
-
|
|
37934
|
-
|
|
37935
|
-
|
|
37936
|
-
|
|
37937
|
-
|
|
37938
|
-
|
|
37939
|
-
|
|
37940
|
-
|
|
37941
|
-
|
|
37942
|
-
|
|
37943
|
-
|
|
37944
|
-
|
|
37945
|
-
|
|
37946
|
-
|
|
37947
|
-
|
|
37948
|
-
|
|
37949
|
-
|
|
37950
|
-
|
|
37951
|
-
|
|
37952
|
-
|
|
37953
|
-
|
|
37954
|
-
|
|
37955
|
-
|
|
37956
|
-
|
|
37957
|
-
|
|
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
|
+
}
|
|
37958
37992
|
});
|
|
37959
|
-
|
|
37960
|
-
|
|
37961
|
-
|
|
37962
|
-
|
|
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
|
+
}
|
|
37963
38002
|
}
|
|
37964
|
-
|
|
37965
|
-
|
|
37966
|
-
|
|
37967
|
-
|
|
37968
|
-
|
|
37969
|
-
|
|
37970
|
-
|
|
37971
|
-
|
|
37972
|
-
|
|
37973
|
-
|
|
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;
|
|
37974
38020
|
try {
|
|
37975
|
-
|
|
38021
|
+
queued = await host.showFailureNotice(notice) === "queued";
|
|
37976
38022
|
} catch (error) {
|
|
37977
|
-
|
|
38023
|
+
if (!alive())
|
|
38024
|
+
return "stopped";
|
|
38025
|
+
safely("failure notice warning", () => host.failureNoticeFailed(notice, error));
|
|
38026
|
+
return "hole";
|
|
37978
38027
|
}
|
|
37979
|
-
|
|
37980
|
-
|
|
37981
|
-
|
|
37982
|
-
|
|
37983
|
-
|
|
37984
|
-
|
|
37985
|
-
|
|
37986
|
-
credential,
|
|
37987
|
-
inbound,
|
|
37988
|
-
heartbeatMs,
|
|
37989
|
-
tmuxPlaceCacheMs,
|
|
37990
|
-
passDeadlineMs: Math.min(Math.max(namedPassDeadlineMs ?? MIN_PASS_DEADLINE_MS, heartbeatMs), MAX_INTERVAL_MS),
|
|
37991
|
-
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";
|
|
37992
38035
|
};
|
|
37993
|
-
|
|
37994
|
-
|
|
37995
|
-
|
|
37996
|
-
|
|
37997
|
-
|
|
37998
|
-
|
|
37999
|
-
import { hostname as hostname3 } from "os";
|
|
38000
|
-
function machineID() {
|
|
38001
|
-
return process.env["ENVOY_MACHINE_ID"] || process.env["AGENTBOX_HOST"] || hostname3();
|
|
38002
|
-
}
|
|
38003
|
-
|
|
38004
|
-
// ../envoy-client/src/transport.ts
|
|
38005
|
-
var REPLAY_TIMEOUT_MS = 1e4;
|
|
38006
|
-
var REPLAY_UNAVAILABLE_CODE = "REPLAY_UNAVAILABLE";
|
|
38007
|
-
var SessionStateSchema = _enum2(["live", "unresponsive", "gone"]);
|
|
38008
|
-
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.";
|
|
38009
|
-
var WireStateSchema = SessionStateSchema.default("live");
|
|
38010
|
-
var InterestWireSchema = object({
|
|
38011
|
-
session_id: string2(),
|
|
38012
|
-
machine_id: string2(),
|
|
38013
|
-
dir: string2(),
|
|
38014
|
-
topics: array(string2()),
|
|
38015
|
-
updated_at: number2().int().optional(),
|
|
38016
|
-
warnings: array(string2()).optional()
|
|
38017
|
-
});
|
|
38018
|
-
var DeliveryAnswerWireSchema = discriminatedUnion("mode", [
|
|
38019
|
-
object({
|
|
38020
|
-
mode: literal("consumer"),
|
|
38021
|
-
subject: string2().min(1),
|
|
38022
|
-
stream_id: string2(),
|
|
38023
|
-
gap: boolean2(),
|
|
38024
|
-
consumer_start: number2().int().nonnegative().optional()
|
|
38025
|
-
}),
|
|
38026
|
-
object({ mode: literal("core"), reason: string2() })
|
|
38027
|
-
]);
|
|
38028
|
-
var SessionWireSchema = object({
|
|
38029
|
-
session_id: string2(),
|
|
38030
|
-
machine_id: string2(),
|
|
38031
|
-
host: string2().optional(),
|
|
38032
|
-
dir: string2(),
|
|
38033
|
-
port: number2().int(),
|
|
38034
|
-
title: string2(),
|
|
38035
|
-
topics: array(string2()),
|
|
38036
|
-
roles: array(string2()).optional(),
|
|
38037
|
-
capabilities: array(string2()).optional(),
|
|
38038
|
-
self_subscribed: boolean2(),
|
|
38039
|
-
last_seen: number2().int().optional(),
|
|
38040
|
-
state: WireStateSchema,
|
|
38041
|
-
plugin: string2().optional(),
|
|
38042
|
-
plugin_version: string2().optional()
|
|
38043
|
-
});
|
|
38044
|
-
var RoleWireSchema = object({
|
|
38045
|
-
role: string2(),
|
|
38046
|
-
holder: string2(),
|
|
38047
|
-
host: string2().optional(),
|
|
38048
|
-
last_seen: number2().int(),
|
|
38049
|
-
state: WireStateSchema,
|
|
38050
|
-
holder_state: SessionStateSchema.optional(),
|
|
38051
|
-
client_instance_id: string2().optional(),
|
|
38052
|
-
handed_back: boolean2().optional()
|
|
38053
|
-
});
|
|
38054
|
-
var RoleHeldWireSchema = object({
|
|
38055
|
-
error: string2(),
|
|
38056
|
-
role: string2(),
|
|
38057
|
-
holder: string2()
|
|
38058
|
-
});
|
|
38059
|
-
var ErrorWireSchema = object({
|
|
38060
|
-
error: string2(),
|
|
38061
|
-
code: string2().optional(),
|
|
38062
|
-
expected: array(string2()).optional(),
|
|
38063
|
-
state: SessionStateSchema.optional(),
|
|
38064
|
-
holder: string2().optional(),
|
|
38065
|
-
holder_state: SessionStateSchema.optional(),
|
|
38066
|
-
last_seen: number2().int().optional(),
|
|
38067
|
-
claim_released: boolean2().optional(),
|
|
38068
|
-
unresponsive: array(string2()).optional()
|
|
38069
|
-
});
|
|
38070
|
-
var EnvelopeResponseSchema = EnvelopeSchema.extend({
|
|
38071
|
-
recipient: string2().optional(),
|
|
38072
|
-
holder: string2().optional(),
|
|
38073
|
-
holder_state: SessionStateSchema.optional(),
|
|
38074
|
-
recipient_state: SessionStateSchema.optional(),
|
|
38075
|
-
last_seen: number2().int().optional(),
|
|
38076
|
-
unresponsive: array(string2()).optional(),
|
|
38077
|
-
duplicate: boolean2().optional()
|
|
38078
|
-
});
|
|
38079
|
-
var PublishResponseSchema = EnvelopeResponseSchema.extend({
|
|
38080
|
-
kept: boolean2().optional(),
|
|
38081
|
-
kept_reason: _enum2(["unclaimed", "holder_lapsed"]).optional()
|
|
38082
|
-
});
|
|
38083
|
-
var ReplayWireSchema = object({
|
|
38084
|
-
stream: object({
|
|
38085
|
-
id: string2().min(1).optional(),
|
|
38086
|
-
created: string2(),
|
|
38087
|
-
first_seq: number2().int().nonnegative(),
|
|
38088
|
-
last_seq: number2().int().nonnegative()
|
|
38089
|
-
}),
|
|
38090
|
-
subjects: array(object({
|
|
38091
|
-
subject: string2(),
|
|
38092
|
-
messages: array(object({
|
|
38093
|
-
seq: number2().int().positive(),
|
|
38094
|
-
subject: string2(),
|
|
38095
|
-
time: string2(),
|
|
38096
|
-
data: string2()
|
|
38097
|
-
})),
|
|
38098
|
-
next_after: number2().int().nonnegative(),
|
|
38099
|
-
more: boolean2(),
|
|
38100
|
-
refused: string2().optional()
|
|
38101
|
-
}))
|
|
38102
|
-
});
|
|
38103
|
-
var DELIVERY_NOT_ANSWERED = "not_answered";
|
|
38104
|
-
var DELIVERY_UNREADABLE = "unreadable_answer";
|
|
38105
|
-
function parseSubscribeAnswer(body) {
|
|
38106
|
-
const interest = InterestWireSchema.parse(body);
|
|
38107
|
-
const raw = typeof body === "object" && body !== null && "delivery" in body ? body.delivery : undefined;
|
|
38108
|
-
if (raw === undefined || raw === null) {
|
|
38109
|
-
return { ...interest, delivery: { mode: "core", reason: DELIVERY_NOT_ANSWERED } };
|
|
38110
|
-
}
|
|
38111
|
-
const delivery = DeliveryAnswerWireSchema.safeParse(raw);
|
|
38112
|
-
return {
|
|
38113
|
-
...interest,
|
|
38114
|
-
delivery: delivery.success ? delivery.data : { mode: "core", reason: DELIVERY_UNREADABLE }
|
|
38115
|
-
};
|
|
38116
|
-
}
|
|
38117
|
-
function mergeInterestSources(registryTopics, liveTopics) {
|
|
38118
|
-
const interests = new Map;
|
|
38119
|
-
for (const topic of registryTopics) {
|
|
38120
|
-
interests.set(topic, liveTopics.includes(topic) ? "both" : "registry");
|
|
38121
|
-
}
|
|
38122
|
-
for (const topic of liveTopics) {
|
|
38123
|
-
if (!interests.has(topic))
|
|
38124
|
-
interests.set(topic, "live");
|
|
38125
|
-
}
|
|
38126
|
-
return interests;
|
|
38127
|
-
}
|
|
38128
|
-
function holderStateOf(role) {
|
|
38129
|
-
return role.holder_state ?? role.state;
|
|
38130
|
-
}
|
|
38131
|
-
function namedProcess(id) {
|
|
38132
|
-
return id === "" ? undefined : id;
|
|
38133
|
-
}
|
|
38134
|
-
function roleHeldBy(info, sessionID, clientInstanceID) {
|
|
38135
|
-
if (info === undefined || info.holder !== sessionID || info.handed_back === true)
|
|
38136
|
-
return false;
|
|
38137
|
-
const holding = namedProcess(info.client_instance_id);
|
|
38138
|
-
return holding === undefined || holding === namedProcess(clientInstanceID);
|
|
38139
|
-
}
|
|
38140
|
-
function mayActOnRoleRecord(writer, clientInstanceID, holdsRole) {
|
|
38141
|
-
const wrote = namedProcess(writer);
|
|
38142
|
-
if (wrote === undefined || wrote === namedProcess(clientInstanceID))
|
|
38143
|
-
return true;
|
|
38144
|
-
return !holdsRole;
|
|
38145
|
-
}
|
|
38146
|
-
function newClientInstanceID() {
|
|
38147
|
-
return Array.from(crypto.getRandomValues(new Uint8Array(16)), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
38148
|
-
}
|
|
38149
|
-
function expandSubscriptionTopics(topics) {
|
|
38150
|
-
const expanded = new Set;
|
|
38151
|
-
for (const topic of topics) {
|
|
38152
|
-
const base = topic.endsWith(".>") ? topic.slice(0, -2) : undefined;
|
|
38153
|
-
if (base === undefined || base.includes("*") || base.includes(">")) {
|
|
38154
|
-
expanded.add(topic);
|
|
38155
|
-
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";
|
|
38156
38042
|
}
|
|
38157
|
-
|
|
38158
|
-
|
|
38159
|
-
|
|
38043
|
+
if (isRoleForward(envelope))
|
|
38044
|
+
return "done";
|
|
38045
|
+
const identity = replayLedgerIdentity(envelope);
|
|
38046
|
+
if (ledger.has(identity)) {
|
|
38047
|
+
failures.delete(seq);
|
|
38048
|
+
return "done";
|
|
38160
38049
|
}
|
|
38161
|
-
|
|
38162
|
-
|
|
38163
|
-
|
|
38164
|
-
|
|
38165
|
-
|
|
38166
|
-
|
|
38167
|
-
|
|
38168
|
-
|
|
38169
|
-
|
|
38170
|
-
|
|
38171
|
-
const
|
|
38172
|
-
|
|
38173
|
-
|
|
38174
|
-
|
|
38175
|
-
|
|
38176
|
-
|
|
38177
|
-
|
|
38178
|
-
}
|
|
38179
|
-
if (sentences.length === 0)
|
|
38180
|
-
return "";
|
|
38181
|
-
if (state === "unresponsive" || stored.length > 0)
|
|
38182
|
-
sentences.push(UNRESPONSIVE_RULE);
|
|
38183
|
-
return ` ${sentences.join(" ")}`;
|
|
38184
|
-
}
|
|
38185
|
-
|
|
38186
|
-
class EnvoyApiError extends Error {
|
|
38187
|
-
details;
|
|
38188
|
-
name = "EnvoyApiError";
|
|
38189
|
-
expected;
|
|
38190
|
-
code;
|
|
38191
|
-
holder;
|
|
38192
|
-
sessionState;
|
|
38193
|
-
lastSeen;
|
|
38194
|
-
claimReleased;
|
|
38195
|
-
unresponsive;
|
|
38196
|
-
constructor(details) {
|
|
38197
|
-
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";
|
|
38198
38067
|
try {
|
|
38199
|
-
|
|
38200
|
-
|
|
38201
|
-
|
|
38202
|
-
|
|
38203
|
-
|
|
38204
|
-
|
|
38205
|
-
|
|
38206
|
-
|
|
38207
|
-
|
|
38208
|
-
|
|
38209
|
-
|
|
38210
|
-
|
|
38211
|
-
|
|
38212
|
-
|
|
38213
|
-
|
|
38214
|
-
|
|
38215
|
-
|
|
38216
|
-
|
|
38217
|
-
|
|
38218
|
-
|
|
38219
|
-
|
|
38220
|
-
|
|
38221
|
-
|
|
38222
|
-
|
|
38223
|
-
|
|
38224
|
-
|
|
38225
|
-
|
|
38226
|
-
|
|
38227
|
-
|
|
38228
|
-
|
|
38229
|
-
|
|
38230
|
-
|
|
38231
|
-
|
|
38232
|
-
|
|
38233
|
-
|
|
38234
|
-
}
|
|
38235
|
-
}
|
|
38236
|
-
function resolveCallTiming(config) {
|
|
38237
|
-
return {
|
|
38238
|
-
timeoutMs: config.timeoutMs ?? LISTENER_CALL_TIMEOUT_MS,
|
|
38239
|
-
attempts: config.retry === false ? 1 : LISTENER_CALL_ATTEMPTS,
|
|
38240
|
-
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";
|
|
38241
38103
|
};
|
|
38242
|
-
|
|
38243
|
-
|
|
38244
|
-
|
|
38245
|
-
|
|
38246
|
-
|
|
38247
|
-
|
|
38248
|
-
|
|
38249
|
-
|
|
38250
|
-
|
|
38251
|
-
|
|
38252
|
-
|
|
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;
|
|
38253
38180
|
try {
|
|
38254
|
-
|
|
38255
|
-
|
|
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
|
+
});
|
|
38256
38186
|
} catch (error) {
|
|
38257
|
-
|
|
38187
|
+
if (alive()) {
|
|
38188
|
+
answered.clear();
|
|
38189
|
+
listenerFailed(error);
|
|
38190
|
+
}
|
|
38191
|
+
return;
|
|
38258
38192
|
}
|
|
38259
|
-
|
|
38260
|
-
|
|
38261
|
-
|
|
38262
|
-
|
|
38263
|
-
|
|
38264
|
-
|
|
38265
|
-
|
|
38266
|
-
|
|
38267
|
-
return { unanswered: error };
|
|
38268
|
-
}
|
|
38269
|
-
if (unminted !== undefined && (response.status === 401 || response.status === 403)) {
|
|
38270
|
-
return {
|
|
38271
|
-
unanswered: new Error(`${init.method ?? "GET"} ${url} answered ${response.status}, and this machine's Envoy credential could not be minted: ${messageFor(unminted)}`, { cause: unminted })
|
|
38272
|
-
};
|
|
38273
|
-
}
|
|
38274
|
-
try {
|
|
38275
|
-
return { response, body: await response.text() };
|
|
38276
|
-
} catch (error) {
|
|
38277
|
-
if (response.status >= 400 && response.status < 500) {
|
|
38278
|
-
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);
|
|
38279
38201
|
}
|
|
38280
|
-
|
|
38281
|
-
|
|
38282
|
-
|
|
38283
|
-
|
|
38284
|
-
|
|
38285
|
-
|
|
38286
|
-
|
|
38287
|
-
|
|
38288
|
-
|
|
38289
|
-
options.onUnanswered?.();
|
|
38290
|
-
if (attempt + 1 < tries) {
|
|
38291
|
-
await waitForRetry(retryDelayMs);
|
|
38292
|
-
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);
|
|
38293
38211
|
}
|
|
38294
|
-
|
|
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;
|
|
38295
38220
|
}
|
|
38296
|
-
|
|
38297
|
-
|
|
38298
|
-
|
|
38299
|
-
|
|
38300
|
-
|
|
38301
|
-
|
|
38302
|
-
|
|
38303
|
-
|
|
38304
|
-
|
|
38305
|
-
|
|
38306
|
-
|
|
38307
|
-
|
|
38308
|
-
|
|
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) {
|
|
38309
38252
|
continue;
|
|
38310
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;
|
|
38311
38259
|
}
|
|
38312
|
-
|
|
38313
|
-
|
|
38314
|
-
|
|
38315
|
-
|
|
38316
|
-
|
|
38317
|
-
|
|
38318
|
-
|
|
38319
|
-
|
|
38320
|
-
|
|
38321
|
-
|
|
38322
|
-
|
|
38323
|
-
|
|
38324
|
-
dir: input.directory,
|
|
38325
|
-
topics: expandSubscriptionTopics(input.topics),
|
|
38326
|
-
port: input.port,
|
|
38327
|
-
title: input.title,
|
|
38328
|
-
...input.capabilities === undefined ? {} : { capabilities: input.capabilities },
|
|
38329
|
-
driving: input.driving,
|
|
38330
|
-
...input.selfSubscribed === undefined ? {} : { self_subscribed: input.selfSubscribed },
|
|
38331
|
-
...input.delivery === undefined ? {} : { delivery: input.delivery },
|
|
38332
|
-
...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID },
|
|
38333
|
-
host: machineID(),
|
|
38334
|
-
...input.place === undefined ? {} : { tmux_place: input.place },
|
|
38335
|
-
plugin: input.plugin.name,
|
|
38336
|
-
plugin_version: input.plugin.version
|
|
38337
|
-
}, { actingSession: input.sessionID }))),
|
|
38338
|
-
unsubscribe: async (input) => {
|
|
38339
|
-
await post("/v1/interests/unsubscribe", {
|
|
38340
|
-
session_id: input.sessionID,
|
|
38341
|
-
topics: expandSubscriptionTopics(input.topics),
|
|
38342
|
-
...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID }
|
|
38343
|
-
}, { actingSession: input.sessionID });
|
|
38344
|
-
},
|
|
38345
|
-
getInterest: async (sessionID) => InterestWireSchema.parse(JSON.parse(await request(`/v1/interests/${sessionID}`, {}))),
|
|
38346
|
-
send: async (input) => {
|
|
38347
|
-
const idempotencyKey = input.idempotencyKey ?? crypto.randomUUID();
|
|
38348
|
-
let unanswered = false;
|
|
38349
|
-
let response;
|
|
38350
|
-
try {
|
|
38351
|
-
const answer = await post("/v1/messages/send", {
|
|
38352
|
-
source: input.source ?? "agent",
|
|
38353
|
-
...sourceSession(input.sourceSessionID),
|
|
38354
|
-
target_session: input.targetSessionID,
|
|
38355
|
-
message: input.message,
|
|
38356
|
-
idempotency_key: idempotencyKey,
|
|
38357
|
-
...messageMetadata(input)
|
|
38358
|
-
}, {
|
|
38359
|
-
onUnanswered: () => {
|
|
38360
|
-
unanswered = true;
|
|
38361
|
-
},
|
|
38362
|
-
...input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID }
|
|
38363
|
-
});
|
|
38364
|
-
response = EnvelopeResponseSchema.parse(JSON.parse(answer));
|
|
38365
|
-
} catch (error) {
|
|
38366
|
-
if (error instanceof EnvoyApiError && error.details.status < 500 && !unanswered) {
|
|
38367
|
-
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 });
|
|
38368
38272
|
}
|
|
38369
|
-
throw new EnvoySendUnconfirmedError(input.targetSessionID, idempotencyKey, attempts, error);
|
|
38370
38273
|
}
|
|
38371
|
-
|
|
38372
|
-
|
|
38373
|
-
|
|
38374
|
-
|
|
38375
|
-
|
|
38376
|
-
|
|
38377
|
-
|
|
38378
|
-
|
|
38379
|
-
|
|
38380
|
-
|
|
38381
|
-
|
|
38382
|
-
|
|
38383
|
-
|
|
38384
|
-
|
|
38385
|
-
|
|
38386
|
-
|
|
38387
|
-
|
|
38388
|
-
|
|
38389
|
-
|
|
38390
|
-
|
|
38391
|
-
|
|
38392
|
-
return {
|
|
38393
|
-
envelope: response,
|
|
38394
|
-
...response.holder === undefined ? {} : { holder: response.holder },
|
|
38395
|
-
...response.holder_state === undefined ? {} : { holderState: response.holder_state },
|
|
38396
|
-
unresponsive: response.unresponsive ?? [],
|
|
38397
|
-
...response.kept === undefined ? {} : { kept: response.kept },
|
|
38398
|
-
...response.kept_reason === undefined ? {} : { keptReason: response.kept_reason }
|
|
38399
|
-
};
|
|
38400
|
-
},
|
|
38401
|
-
unregisterSession: async (input) => {
|
|
38402
|
-
const query = input.clientInstanceID === undefined ? "" : `?client_instance_id=${encodeURIComponent(input.clientInstanceID)}`;
|
|
38403
|
-
await request(`/v1/sessions/${encodeURIComponent(input.sessionID)}${query}`, { method: "DELETE" }, { actingSession: input.sessionID });
|
|
38404
|
-
},
|
|
38405
|
-
setRole: async (input) => {
|
|
38406
|
-
const soft = input.soft === true;
|
|
38407
|
-
const previous = soft ? input.previousSessionID ?? "" : "";
|
|
38408
|
-
const body = {
|
|
38409
|
-
session_id: input.sessionID,
|
|
38410
|
-
role: input.role,
|
|
38411
|
-
...soft ? { soft: true } : {},
|
|
38412
|
-
...previous === "" ? {} : { previous_session_id: previous },
|
|
38413
|
-
...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID }
|
|
38414
|
-
};
|
|
38415
|
-
let raw;
|
|
38416
|
-
try {
|
|
38417
|
-
raw = await post("/v1/roles/set", body, { actingSession: input.sessionID });
|
|
38418
|
-
} catch (error) {
|
|
38419
|
-
if (error instanceof EnvoyApiError && error.details.status === 409) {
|
|
38420
|
-
let answer;
|
|
38421
|
-
try {
|
|
38422
|
-
answer = JSON.parse(error.details.responseBody);
|
|
38423
|
-
} catch {
|
|
38424
|
-
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());
|
|
38425
38295
|
}
|
|
38426
|
-
|
|
38427
|
-
|
|
38428
|
-
return { claimed: false, holder: held.data.holder };
|
|
38296
|
+
reading.read = result.next_after;
|
|
38297
|
+
continue;
|
|
38429
38298
|
}
|
|
38430
|
-
|
|
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;
|
|
38431
38311
|
}
|
|
38432
|
-
|
|
38433
|
-
|
|
38434
|
-
|
|
38435
|
-
|
|
38436
|
-
|
|
38437
|
-
|
|
38438
|
-
|
|
38439
|
-
|
|
38440
|
-
|
|
38441
|
-
|
|
38442
|
-
|
|
38443
|
-
|
|
38444
|
-
|
|
38445
|
-
|
|
38446
|
-
|
|
38447
|
-
|
|
38448
|
-
|
|
38449
|
-
|
|
38450
|
-
|
|
38451
|
-
|
|
38452
|
-
|
|
38453
|
-
|
|
38454
|
-
|
|
38455
|
-
|
|
38456
|
-
}
|
|
38457
|
-
|
|
38458
|
-
|
|
38459
|
-
|
|
38460
|
-
|
|
38461
|
-
|
|
38462
|
-
|
|
38463
|
-
|
|
38464
|
-
|
|
38465
|
-
|
|
38466
|
-
|
|
38467
|
-
|
|
38468
|
-
|
|
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
|
+
}
|
|
38469
38354
|
};
|
|
38470
|
-
|
|
38471
|
-
function sourceSession(sessionID) {
|
|
38472
|
-
return sessionID === undefined || sessionID === "" ? {} : { source_session: sessionID };
|
|
38473
|
-
}
|
|
38474
|
-
|
|
38475
|
-
// ../envoy-client/src/replay.ts
|
|
38476
|
-
var REPLAY_LEDGER_TTL_MS = DELIVERY_DUPLICATE_WINDOW_MS;
|
|
38477
|
-
var REPLAY_LEDGER_LIMIT = 20000;
|
|
38478
|
-
var REPLAY_PAGES_PER_SWEEP = 20;
|
|
38479
|
-
var REPLAY_FAILURES_BEFORE_NOTICE = 3;
|
|
38480
|
-
var REPLAY_STATE_REFRESH_MS = 60 * 60 * 1000;
|
|
38481
|
-
var REPLAY_MAX_SUBJECTS = 32;
|
|
38482
|
-
var START_CURSORS_TIMEOUT_MS = 2000;
|
|
38483
|
-
var ROLE_FORWARD_KEY_PREFIX = "envoy.role.forward.";
|
|
38484
|
-
var ReplayLedgerEntrySchema = object({
|
|
38485
|
-
event_id: string2().min(1).optional(),
|
|
38486
|
-
dedupe_key: string2().min(1).optional(),
|
|
38487
|
-
at: number2()
|
|
38488
|
-
});
|
|
38489
|
-
var ReplayCursorSchema = object({
|
|
38490
|
-
since: number2().int().nonnegative().optional(),
|
|
38491
|
-
after: number2().int().nonnegative().optional(),
|
|
38492
|
-
from_start: number2().int().nonnegative().optional()
|
|
38493
|
-
});
|
|
38494
|
-
var ReplayPendingNoticeSchema = _enum2([
|
|
38495
|
-
"retention_gap",
|
|
38496
|
-
"stream_moved_back",
|
|
38497
|
-
"stream_recreated"
|
|
38498
|
-
]);
|
|
38499
|
-
var PENDING_NOTICE_MESSAGES = {
|
|
38500
|
-
stream_recreated: "Envoy's notification stream was recreated, so messages published to this session while its connection was down before that may be lost.",
|
|
38501
|
-
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.",
|
|
38502
|
-
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."
|
|
38503
|
-
};
|
|
38504
|
-
var ReplayStateSchema = object({
|
|
38505
|
-
stream_id: string2().optional(),
|
|
38506
|
-
stream_created: string2().optional(),
|
|
38507
|
-
subjects: record(string2(), ReplayCursorSchema),
|
|
38508
|
-
notices: array(string2()).optional()
|
|
38509
|
-
});
|
|
38510
|
-
var ReplaySnapshotSchema = object({
|
|
38511
|
-
state: ReplayStateSchema.optional(),
|
|
38512
|
-
ledger: array(ReplayLedgerEntrySchema)
|
|
38513
|
-
});
|
|
38514
|
-
function createReplayEngine(options) {
|
|
38515
|
-
const { host, transport, dedupe } = options;
|
|
38516
|
-
const now = options.now ?? Date.now;
|
|
38517
|
-
const ledger = createLedger(now);
|
|
38518
|
-
const cursors = new Map;
|
|
38519
|
-
const cursorStarts = new Map;
|
|
38520
|
-
let streamID;
|
|
38521
|
-
let streamCreated;
|
|
38522
|
-
const pending = new Set;
|
|
38523
|
-
const failures = new Map;
|
|
38524
|
-
let unwritten = [];
|
|
38525
|
-
let writtenState;
|
|
38526
|
-
let writtenShape;
|
|
38527
|
-
let writtenAt;
|
|
38528
|
-
let writes = Promise.resolve();
|
|
38529
|
-
let epoch = 0;
|
|
38530
|
-
let listenerNoticed;
|
|
38531
|
-
const answered = new Set;
|
|
38532
|
-
let running;
|
|
38533
|
-
let rerun;
|
|
38534
|
-
let consumerMode = false;
|
|
38535
|
-
const safely = (what, call) => {
|
|
38355
|
+
const runSweep = async () => {
|
|
38536
38356
|
try {
|
|
38537
|
-
|
|
38357
|
+
await sweepOnce();
|
|
38538
38358
|
} catch (error) {
|
|
38539
|
-
|
|
38540
|
-
host.log(`replay: ${what} failed`, error);
|
|
38541
|
-
} catch {}
|
|
38359
|
+
log("replay: a sweep failed", error);
|
|
38542
38360
|
}
|
|
38543
38361
|
};
|
|
38544
|
-
const
|
|
38545
|
-
|
|
38546
|
-
|
|
38547
|
-
|
|
38548
|
-
|
|
38549
|
-
|
|
38550
|
-
const sessionRead = (read) => () => {
|
|
38551
|
-
try {
|
|
38552
|
-
return read();
|
|
38553
|
-
} catch {
|
|
38554
|
-
return;
|
|
38362
|
+
const sweep = () => {
|
|
38363
|
+
if (running === undefined) {
|
|
38364
|
+
running = runSweep().finally(() => {
|
|
38365
|
+
running = undefined;
|
|
38366
|
+
});
|
|
38367
|
+
return running;
|
|
38555
38368
|
}
|
|
38369
|
+
rerun ??= running.then(() => {
|
|
38370
|
+
rerun = undefined;
|
|
38371
|
+
return sweep();
|
|
38372
|
+
});
|
|
38373
|
+
return rerun;
|
|
38556
38374
|
};
|
|
38557
|
-
const
|
|
38558
|
-
const
|
|
38559
|
-
|
|
38560
|
-
if (
|
|
38375
|
+
const started = (cursor) => cursor?.after !== undefined || cursor?.from_start !== undefined;
|
|
38376
|
+
const startOnce = async () => {
|
|
38377
|
+
const binding = bind();
|
|
38378
|
+
if (binding === undefined)
|
|
38561
38379
|
return;
|
|
38562
|
-
const
|
|
38563
|
-
|
|
38564
|
-
|
|
38565
|
-
|
|
38566
|
-
|
|
38567
|
-
|
|
38568
|
-
|
|
38569
|
-
|
|
38570
|
-
|
|
38571
|
-
|
|
38572
|
-
|
|
38573
|
-
|
|
38574
|
-
|
|
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
|
+
}
|
|
38575
38418
|
}
|
|
38576
|
-
|
|
38577
|
-
|
|
38578
|
-
...streamCreated === undefined ? {} : { stream_created: streamCreated },
|
|
38579
|
-
subjects,
|
|
38580
|
-
...pending.size === 0 ? {} : { notices: [...pending].sort() }
|
|
38581
|
-
});
|
|
38419
|
+
if (changed)
|
|
38420
|
+
await flush(alive, true);
|
|
38582
38421
|
};
|
|
38583
|
-
const
|
|
38584
|
-
|
|
38585
|
-
|
|
38586
|
-
|
|
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;
|
|
38587
38429
|
};
|
|
38588
|
-
|
|
38589
|
-
|
|
38590
|
-
|
|
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)
|
|
38591
38440
|
return;
|
|
38592
|
-
|
|
38593
|
-
|
|
38594
|
-
|
|
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)
|
|
38595
38452
|
return;
|
|
38596
|
-
|
|
38597
|
-
|
|
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)) {
|
|
38598
38466
|
return;
|
|
38599
38467
|
}
|
|
38600
|
-
|
|
38601
|
-
|
|
38468
|
+
const alive = bind()?.alive;
|
|
38469
|
+
if (alive === undefined)
|
|
38470
|
+
return;
|
|
38602
38471
|
try {
|
|
38603
|
-
await
|
|
38604
|
-
|
|
38605
|
-
|
|
38606
|
-
snapshot: () => ({ state: JSON.parse(json), ledger: ledger.list() })
|
|
38607
|
-
});
|
|
38608
|
-
if (state !== undefined) {
|
|
38609
|
-
writtenState = json;
|
|
38610
|
-
writtenShape = shape;
|
|
38611
|
-
writtenAt = now();
|
|
38612
|
-
}
|
|
38613
|
-
} catch (error) {
|
|
38614
|
-
unwritten = [...delivered, ...unwritten];
|
|
38615
|
-
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);
|
|
38616
38475
|
}
|
|
38617
|
-
}
|
|
38618
|
-
|
|
38619
|
-
|
|
38620
|
-
|
|
38621
|
-
|
|
38622
|
-
|
|
38623
|
-
|
|
38624
|
-
|
|
38625
|
-
|
|
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);
|
|
38626
38493
|
}
|
|
38627
|
-
|
|
38628
|
-
|
|
38629
|
-
|
|
38630
|
-
|
|
38631
|
-
|
|
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())
|
|
38632
38506
|
};
|
|
38633
|
-
|
|
38634
|
-
|
|
38635
|
-
|
|
38636
|
-
|
|
38637
|
-
|
|
38638
|
-
|
|
38639
|
-
|
|
38640
|
-
|
|
38641
|
-
|
|
38642
|
-
|
|
38643
|
-
const
|
|
38644
|
-
|
|
38645
|
-
|
|
38646
|
-
|
|
38647
|
-
|
|
38648
|
-
|
|
38649
|
-
|
|
38650
|
-
|
|
38651
|
-
|
|
38652
|
-
}
|
|
38653
|
-
if (!alive())
|
|
38654
|
-
return "stopped";
|
|
38655
|
-
if (queued)
|
|
38656
|
-
return "hole";
|
|
38657
|
-
record2(replayLedgerIdentity(notice.envelope));
|
|
38658
|
-
await flush(alive);
|
|
38659
|
-
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}`
|
|
38660
38527
|
};
|
|
38661
|
-
|
|
38662
|
-
|
|
38663
|
-
|
|
38664
|
-
|
|
38665
|
-
|
|
38666
|
-
|
|
38667
|
-
|
|
38668
|
-
|
|
38669
|
-
|
|
38670
|
-
|
|
38671
|
-
|
|
38672
|
-
|
|
38673
|
-
|
|
38674
|
-
|
|
38675
|
-
|
|
38676
|
-
|
|
38677
|
-
|
|
38678
|
-
|
|
38679
|
-
|
|
38680
|
-
|
|
38681
|
-
|
|
38682
|
-
|
|
38683
|
-
|
|
38684
|
-
|
|
38685
|
-
const
|
|
38686
|
-
if (
|
|
38687
|
-
return
|
|
38688
|
-
|
|
38689
|
-
|
|
38690
|
-
|
|
38691
|
-
|
|
38692
|
-
|
|
38693
|
-
|
|
38694
|
-
|
|
38695
|
-
|
|
38696
|
-
|
|
38697
|
-
|
|
38698
|
-
|
|
38699
|
-
|
|
38700
|
-
|
|
38701
|
-
|
|
38702
|
-
|
|
38703
|
-
|
|
38704
|
-
|
|
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);
|
|
38705
38618
|
}
|
|
38706
|
-
if (
|
|
38707
|
-
|
|
38708
|
-
if (outcome === "shown") {
|
|
38709
|
-
failures.delete(seq);
|
|
38710
|
-
record2(identity);
|
|
38711
|
-
await flush(alive);
|
|
38712
|
-
return "done";
|
|
38619
|
+
if (entry.dedupe_key !== undefined && byKey.get(entry.dedupe_key) === entry) {
|
|
38620
|
+
byKey.delete(entry.dedupe_key);
|
|
38713
38621
|
}
|
|
38714
|
-
|
|
38715
|
-
|
|
38716
|
-
|
|
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);
|
|
38717
38628
|
}
|
|
38718
|
-
|
|
38719
|
-
|
|
38720
|
-
|
|
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);
|
|
38721
38641
|
}
|
|
38722
|
-
const count = (failure?.count ?? 0) + 1;
|
|
38723
|
-
failures.set(seq, { count, error });
|
|
38724
|
-
if (count >= REPLAY_FAILURES_BEFORE_NOTICE)
|
|
38725
|
-
return giveUp(error);
|
|
38726
|
-
log(`replay: could not show stored message ${seq} (${envelope.event_id}): ${error}`);
|
|
38727
|
-
return "hole";
|
|
38728
38642
|
};
|
|
38729
|
-
const
|
|
38730
|
-
|
|
38731
|
-
|
|
38732
|
-
|
|
38733
|
-
|
|
38734
|
-
|
|
38735
|
-
|
|
38736
|
-
|
|
38737
|
-
kind: "not_served",
|
|
38738
|
-
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."
|
|
38739
|
-
});
|
|
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)
|
|
38740
38651
|
return;
|
|
38741
|
-
|
|
38742
|
-
if (status === 503 && error.code === REPLAY_UNAVAILABLE_CODE || status === 401 || status === 403) {
|
|
38743
|
-
const key = `unavailable:${status}:${error.message}`;
|
|
38744
|
-
if (listenerNoticed === key)
|
|
38745
|
-
return;
|
|
38746
|
-
listenerNoticed = key;
|
|
38747
|
-
notify({
|
|
38748
|
-
kind: "unavailable",
|
|
38749
|
-
status,
|
|
38750
|
-
error: error.message,
|
|
38751
|
-
message: `Envoy replay is unavailable (${status}: ${error.message}), so messages published while this session's connection is down are not recovered until it answers.`
|
|
38752
|
-
});
|
|
38652
|
+
if (has(identity))
|
|
38753
38653
|
return;
|
|
38754
|
-
}
|
|
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 {}
|
|
38755
38801
|
}
|
|
38756
|
-
log("replay: the listener's replay failed; the next trigger tries again", error);
|
|
38757
38802
|
};
|
|
38758
|
-
const
|
|
38759
|
-
|
|
38760
|
-
|
|
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;
|
|
38761
38829
|
}
|
|
38762
|
-
return
|
|
38830
|
+
return earliest;
|
|
38763
38831
|
};
|
|
38764
|
-
const
|
|
38765
|
-
|
|
38766
|
-
|
|
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))
|
|
38767
38840
|
return true;
|
|
38768
38841
|
}
|
|
38769
38842
|
return false;
|
|
38770
38843
|
};
|
|
38771
|
-
const
|
|
38772
|
-
|
|
38773
|
-
if (binding === undefined)
|
|
38844
|
+
const settle = (entry, body) => {
|
|
38845
|
+
if (entry.settled)
|
|
38774
38846
|
return;
|
|
38775
|
-
|
|
38776
|
-
|
|
38777
|
-
|
|
38778
|
-
for (const
|
|
38779
|
-
|
|
38780
|
-
|
|
38781
|
-
|
|
38782
|
-
|
|
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);
|
|
38783
38855
|
}
|
|
38784
|
-
|
|
38785
|
-
|
|
38786
|
-
|
|
38787
|
-
if (
|
|
38788
|
-
|
|
38789
|
-
|
|
38790
|
-
|
|
38791
|
-
|
|
38792
|
-
|
|
38793
|
-
|
|
38794
|
-
|
|
38795
|
-
|
|
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;
|
|
38796
38872
|
}
|
|
38797
|
-
|
|
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);
|
|
38798
38896
|
return;
|
|
38799
|
-
|
|
38800
|
-
|
|
38801
|
-
|
|
38802
|
-
|
|
38803
|
-
|
|
38804
|
-
|
|
38805
|
-
|
|
38806
|
-
|
|
38807
|
-
|
|
38808
|
-
|
|
38809
|
-
|
|
38810
|
-
|
|
38811
|
-
|
|
38812
|
-
|
|
38813
|
-
|
|
38814
|
-
|
|
38815
|
-
|
|
38816
|
-
|
|
38817
|
-
}
|
|
38818
|
-
if (!alive())
|
|
38819
|
-
return;
|
|
38820
|
-
listenerNoticed = undefined;
|
|
38821
|
-
for (const result of response.subjects) {
|
|
38822
|
-
if (result.refused === undefined)
|
|
38823
|
-
answered.add(result.subject);
|
|
38824
|
-
else
|
|
38825
|
-
answered.delete(result.subject);
|
|
38826
|
-
}
|
|
38827
|
-
const { stream } = response;
|
|
38828
|
-
const replaced = streamReplaced(stream);
|
|
38829
|
-
streamID = stream.id ?? (replaced ? undefined : streamID);
|
|
38830
|
-
streamCreated = stream.created;
|
|
38831
|
-
if (replaced) {
|
|
38832
|
-
const at = now();
|
|
38833
|
-
for (const subject of cursors.keys()) {
|
|
38834
|
-
cursors.set(subject, { since: stream.last_seq, after: stream.last_seq });
|
|
38835
|
-
cursorStarts.set(subject, at);
|
|
38836
|
-
}
|
|
38837
|
-
failures.clear();
|
|
38838
|
-
pending.add("stream_recreated");
|
|
38839
|
-
await flush(alive);
|
|
38840
|
-
if (!alive())
|
|
38841
|
-
return;
|
|
38842
|
-
tell("stream_recreated");
|
|
38843
|
-
await flush(alive);
|
|
38844
|
-
return;
|
|
38845
|
-
}
|
|
38846
|
-
if (streamMovedBack(stream)) {
|
|
38847
|
-
const from = Math.max(0, stream.first_seq - 1);
|
|
38848
|
-
for (const [subject, cursor] of cursors) {
|
|
38849
|
-
if (cursor.after === undefined)
|
|
38850
|
-
continue;
|
|
38851
|
-
cursors.set(subject, { ...cursor, after: from });
|
|
38852
|
-
const reading = readings.get(subject);
|
|
38853
|
-
if (reading !== undefined) {
|
|
38854
|
-
reading.read = from;
|
|
38855
|
-
reading.more = true;
|
|
38856
|
-
}
|
|
38857
|
-
}
|
|
38858
|
-
buffer.clear();
|
|
38859
|
-
holes.clear();
|
|
38860
|
-
failures.clear();
|
|
38861
|
-
pending.add("stream_moved_back");
|
|
38862
|
-
await flush(alive, true);
|
|
38863
|
-
if (!alive())
|
|
38864
|
-
return;
|
|
38865
|
-
tell("stream_moved_back");
|
|
38866
|
-
await flush(alive);
|
|
38867
|
-
if (!alive())
|
|
38868
|
-
return;
|
|
38869
|
-
continue;
|
|
38870
|
-
}
|
|
38871
|
-
for (const [, reading] of asking)
|
|
38872
|
-
reading.more = false;
|
|
38873
|
-
for (const result of response.subjects) {
|
|
38874
|
-
const reading = readings.get(result.subject);
|
|
38875
|
-
const markedAt = cursors.get(result.subject)?.from_start;
|
|
38876
|
-
if (reading === undefined || markedAt === undefined || result.refused !== undefined) {
|
|
38877
|
-
continue;
|
|
38878
|
-
}
|
|
38879
|
-
const start = Math.max(0, stream.first_seq - 1);
|
|
38880
|
-
cursors.set(result.subject, { since: start, after: start });
|
|
38881
|
-
cursorStarts.set(result.subject, now());
|
|
38882
|
-
if (now() - markedAt < DELIVERY_DUPLICATE_WINDOW_MS)
|
|
38883
|
-
reading.read = start;
|
|
38884
|
-
}
|
|
38885
|
-
let gapped = false;
|
|
38886
|
-
for (const result of response.subjects) {
|
|
38887
|
-
const reading = readings.get(result.subject);
|
|
38888
|
-
if (reading?.read === undefined || result.refused !== undefined)
|
|
38889
|
-
continue;
|
|
38890
|
-
if (reading.read >= stream.first_seq - 1)
|
|
38891
|
-
continue;
|
|
38892
|
-
gapped = true;
|
|
38893
|
-
reading.read = stream.first_seq - 1;
|
|
38894
|
-
const cursor = cursors.get(result.subject);
|
|
38895
|
-
if (cursor?.after !== undefined && cursor.after < reading.read) {
|
|
38896
|
-
cursors.set(result.subject, { ...cursor, after: reading.read });
|
|
38897
|
-
}
|
|
38898
|
-
}
|
|
38899
|
-
if (gapped) {
|
|
38900
|
-
if (!gapNoticed)
|
|
38901
|
-
pending.add("retention_gap");
|
|
38902
|
-
await flush(alive, true);
|
|
38903
|
-
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)
|
|
38904
38915
|
return;
|
|
38905
|
-
if (
|
|
38906
|
-
gapNoticed = true;
|
|
38907
|
-
tell("retention_gap");
|
|
38908
|
-
await flush(alive);
|
|
38909
|
-
}
|
|
38910
|
-
}
|
|
38911
|
-
for (const result of response.subjects) {
|
|
38912
|
-
const reading = readings.get(result.subject);
|
|
38913
|
-
if (reading === undefined || result.refused !== undefined)
|
|
38914
|
-
continue;
|
|
38915
|
-
if (reading.read === undefined) {
|
|
38916
|
-
const cursor = cursors.get(result.subject);
|
|
38917
|
-
if (cursor !== undefined && cursor.after === undefined) {
|
|
38918
|
-
cursors.set(result.subject, { since: result.next_after, after: result.next_after });
|
|
38919
|
-
cursorStarts.set(result.subject, now());
|
|
38920
|
-
}
|
|
38921
|
-
reading.read = result.next_after;
|
|
38922
|
-
continue;
|
|
38923
|
-
}
|
|
38924
|
-
for (const message of result.messages) {
|
|
38925
|
-
if (message.seq <= reading.read)
|
|
38926
|
-
continue;
|
|
38927
|
-
const buffered = buffer.get(message.seq);
|
|
38928
|
-
if (buffered === undefined) {
|
|
38929
|
-
buffer.set(message.seq, { message, subjects: [result.subject] });
|
|
38930
|
-
} else if (!buffered.subjects.includes(result.subject)) {
|
|
38931
|
-
buffered.subjects.push(result.subject);
|
|
38932
|
-
}
|
|
38933
|
-
}
|
|
38934
|
-
reading.read = Math.max(reading.read, result.next_after);
|
|
38935
|
-
reading.more = result.more;
|
|
38936
|
-
}
|
|
38937
|
-
let bound = Number.POSITIVE_INFINITY;
|
|
38938
|
-
for (const reading of readings.values()) {
|
|
38939
|
-
if (reading.more && reading.read !== undefined)
|
|
38940
|
-
bound = Math.min(bound, reading.read);
|
|
38941
|
-
}
|
|
38942
|
-
const ready = [...buffer.keys()].filter((seq) => seq <= bound).sort((a, b) => a - b);
|
|
38943
|
-
for (const seq of ready) {
|
|
38944
|
-
const buffered = buffer.get(seq);
|
|
38945
|
-
if (buffered === undefined)
|
|
38916
|
+
if (entry.settled)
|
|
38946
38917
|
continue;
|
|
38947
|
-
|
|
38948
|
-
|
|
38918
|
+
if (alreadyWritten(entry.identity)) {
|
|
38919
|
+
settle(entry, "done");
|
|
38949
38920
|
continue;
|
|
38950
|
-
const handled = await handle(seq, buffered, alive);
|
|
38951
|
-
if (handled === "stopped")
|
|
38952
|
-
return;
|
|
38953
|
-
if (handled === "done")
|
|
38954
|
-
handledAny = true;
|
|
38955
|
-
if (handled === "hole") {
|
|
38956
|
-
for (const subject of buffered.subjects) {
|
|
38957
|
-
if (!holes.has(subject))
|
|
38958
|
-
holes.set(subject, seq);
|
|
38959
|
-
}
|
|
38960
38921
|
}
|
|
38922
|
+
handOff(entry);
|
|
38961
38923
|
}
|
|
38962
|
-
|
|
38963
|
-
|
|
38964
|
-
|
|
38965
|
-
|
|
38966
|
-
|
|
38967
|
-
|
|
38968
|
-
|
|
38969
|
-
|
|
38970
|
-
|
|
38971
|
-
|
|
38972
|
-
|
|
38973
|
-
|
|
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);
|
|
38974
38950
|
}
|
|
38975
|
-
|
|
38976
|
-
if (![...readings.values()].some((reading) => reading.more))
|
|
38977
|
-
break;
|
|
38951
|
+
switchStream(frame.streamID);
|
|
38978
38952
|
}
|
|
38979
|
-
|
|
38980
|
-
|
|
38981
|
-
|
|
38982
|
-
|
|
38983
|
-
|
|
38984
|
-
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;
|
|
38985
38958
|
}
|
|
38986
|
-
|
|
38987
|
-
|
|
38988
|
-
|
|
38989
|
-
|
|
38990
|
-
|
|
38991
|
-
|
|
38992
|
-
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;
|
|
38993
38966
|
}
|
|
38994
|
-
|
|
38995
|
-
|
|
38996
|
-
|
|
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");
|
|
38997
38984
|
});
|
|
38998
|
-
|
|
38985
|
+
entries.set(key, entry);
|
|
38986
|
+
queue.push(entry);
|
|
38987
|
+
pump();
|
|
38999
38988
|
};
|
|
39000
|
-
const
|
|
39001
|
-
|
|
39002
|
-
const binding = bind();
|
|
39003
|
-
if (binding === undefined)
|
|
38989
|
+
const resumePoint = () => {
|
|
38990
|
+
if (streamID === undefined)
|
|
39004
38991
|
return;
|
|
39005
|
-
|
|
39006
|
-
|
|
39007
|
-
|
|
39008
|
-
|
|
39009
|
-
|
|
39010
|
-
|
|
39011
|
-
|
|
39012
|
-
|
|
39013
|
-
|
|
39014
|
-
|
|
39015
|
-
|
|
39016
|
-
|
|
39017
|
-
|
|
39018
|
-
|
|
39019
|
-
|
|
39020
|
-
|
|
39021
|
-
|
|
39022
|
-
|
|
39023
|
-
|
|
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);
|
|
39024
39048
|
}
|
|
39025
|
-
|
|
39026
|
-
|
|
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;
|
|
39027
39059
|
}
|
|
39028
|
-
|
|
39029
|
-
|
|
39030
|
-
|
|
39031
|
-
|
|
39032
|
-
|
|
39033
|
-
|
|
39034
|
-
|
|
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) {
|
|
39035
39067
|
const at = now();
|
|
39036
|
-
|
|
39037
|
-
|
|
39068
|
+
const next = new Map;
|
|
39069
|
+
for (const topic of topics) {
|
|
39070
|
+
if (topic === ownSubject)
|
|
39038
39071
|
continue;
|
|
39039
|
-
|
|
39040
|
-
cursorStarts.set(result.subject, at);
|
|
39041
|
-
changed = true;
|
|
39072
|
+
next.set(topic, follows.get(topic) ?? at);
|
|
39042
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;
|
|
39043
39099
|
}
|
|
39044
|
-
if (changed)
|
|
39045
|
-
await flush(alive, true);
|
|
39046
39100
|
};
|
|
39047
|
-
|
|
39048
|
-
|
|
39049
|
-
|
|
39050
|
-
|
|
39051
|
-
|
|
39052
|
-
|
|
39053
|
-
|
|
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");
|
|
39054
39132
|
};
|
|
39055
|
-
|
|
39056
|
-
|
|
39057
|
-
|
|
39058
|
-
|
|
39059
|
-
|
|
39060
|
-
|
|
39061
|
-
|
|
39062
|
-
|
|
39063
|
-
|
|
39064
|
-
|
|
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;
|
|
39065
39149
|
return;
|
|
39066
|
-
try {
|
|
39067
|
-
syncFollowed();
|
|
39068
|
-
await flush(alive);
|
|
39069
|
-
} catch (error) {
|
|
39070
|
-
log("replay: could not take the followed subjects", error);
|
|
39071
39150
|
}
|
|
39072
|
-
|
|
39073
|
-
|
|
39074
|
-
|
|
39075
|
-
|
|
39076
|
-
|
|
39077
|
-
|
|
39078
|
-
|
|
39079
|
-
|
|
39080
|
-
|
|
39081
|
-
|
|
39082
|
-
const
|
|
39083
|
-
|
|
39084
|
-
|
|
39085
|
-
|
|
39086
|
-
|
|
39087
|
-
},
|
|
39088
|
-
async liveFailed(followed, envelope, error, receivedAt) {
|
|
39089
|
-
const started = cursorStarts.get(followed);
|
|
39090
|
-
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;
|
|
39091
39166
|
return;
|
|
39092
39167
|
}
|
|
39093
|
-
|
|
39094
|
-
|
|
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);
|
|
39095
39182
|
return;
|
|
39096
|
-
try {
|
|
39097
|
-
await showFailureNotice(failureNotice(envelope, inboundTimestamp(envelope.issued_at), messageFor(error)), alive);
|
|
39098
|
-
} catch (thrown) {
|
|
39099
|
-
log("replay: could not report a failed live frame", thrown);
|
|
39100
|
-
}
|
|
39101
|
-
},
|
|
39102
|
-
followedBefore: (topic, seq) => {
|
|
39103
|
-
const since = cursors.get(topic)?.since;
|
|
39104
|
-
return since !== undefined && seq > since;
|
|
39105
|
-
},
|
|
39106
|
-
restore(snapshot) {
|
|
39107
|
-
epoch += 1;
|
|
39108
|
-
ledger.restore(snapshot.ledger);
|
|
39109
|
-
cursors.clear();
|
|
39110
|
-
cursorStarts.clear();
|
|
39111
|
-
streamID = snapshot.state?.stream_id;
|
|
39112
|
-
streamCreated = snapshot.state?.stream_created;
|
|
39113
|
-
pending.clear();
|
|
39114
|
-
for (const kind of snapshot.state?.notices ?? []) {
|
|
39115
|
-
const known = ReplayPendingNoticeSchema.safeParse(kind);
|
|
39116
|
-
if (known.success)
|
|
39117
|
-
pending.add(known.data);
|
|
39118
39183
|
}
|
|
39119
|
-
|
|
39120
|
-
|
|
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;
|
|
39121
39193
|
}
|
|
39122
|
-
|
|
39123
|
-
|
|
39124
|
-
|
|
39125
|
-
|
|
39126
|
-
|
|
39127
|
-
listenerNoticed = undefined;
|
|
39128
|
-
answered.clear();
|
|
39129
|
-
},
|
|
39130
|
-
state: () => JSON.parse(stateJSON())
|
|
39131
|
-
};
|
|
39132
|
-
}
|
|
39133
|
-
function replaySuppressed(rendered) {
|
|
39134
|
-
return rendered.skip || rendered.malformedDelivery === true;
|
|
39135
|
-
}
|
|
39136
|
-
var REPLAYED_SUFFIX = "while this session's connection was down.";
|
|
39137
|
-
function replayedText(content, publishedAt) {
|
|
39138
|
-
return `${content}
|
|
39139
|
-
Published at ${publishedAt} ${REPLAYED_SUFFIX}`;
|
|
39140
|
-
}
|
|
39141
|
-
function failureNoticeFields(notice) {
|
|
39142
|
-
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.";
|
|
39143
|
-
return {
|
|
39144
|
-
notice: "message not shown",
|
|
39145
|
-
event_id: notice.eventId,
|
|
39146
|
-
from: notice.sender,
|
|
39147
|
-
summary: notice.summary,
|
|
39148
|
-
published_at: notice.publishedAt,
|
|
39149
|
-
...notice.subject === undefined ? {} : { subject: notice.subject },
|
|
39150
|
-
error: notice.error,
|
|
39151
|
-
detail: `This session could not be shown this message, so it is not coming back on its own. ${readWith}`
|
|
39152
|
-
};
|
|
39153
|
-
}
|
|
39154
|
-
var REPLAY_NOTICE_NAMES = {
|
|
39155
|
-
stream_recreated: "stream recreated",
|
|
39156
|
-
stream_moved_back: "stream moved back"
|
|
39157
|
-
};
|
|
39158
|
-
function replayNoticeFields(notice) {
|
|
39159
|
-
return {
|
|
39160
|
-
notice: REPLAY_NOTICE_NAMES[notice.kind] ?? "messages may be missing",
|
|
39161
|
-
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;
|
|
39162
39199
|
};
|
|
39163
|
-
|
|
39164
|
-
|
|
39165
|
-
|
|
39166
|
-
|
|
39167
|
-
|
|
39168
|
-
|
|
39169
|
-
|
|
39170
|
-
|
|
39171
|
-
|
|
39172
|
-
|
|
39173
|
-
const aTokens = a.split(".");
|
|
39174
|
-
const bTokens = b.split(".");
|
|
39175
|
-
for (let index = 0;index < aTokens.length && index < bTokens.length; index += 1) {
|
|
39176
|
-
const aToken = aTokens[index];
|
|
39177
|
-
const bToken = bTokens[index];
|
|
39178
|
-
if (aToken === ">" || bToken === ">")
|
|
39179
|
-
return true;
|
|
39180
|
-
if (aToken !== "*" && bToken !== "*" && aToken !== bToken)
|
|
39181
|
-
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}`;
|
|
39182
39210
|
}
|
|
39183
|
-
return aTokens.length === bTokens.length;
|
|
39184
|
-
}
|
|
39185
|
-
function reachesPrivateStreamSubject(ownAgentSubject, topic) {
|
|
39186
|
-
if (!PRIVATE_STREAM_SUBJECTS.some((subject) => subjectsOverlap(topic, subject)))
|
|
39187
|
-
return false;
|
|
39188
|
-
return topic !== ownAgentSubject || /[*>]/.test(ownAgentSubject);
|
|
39189
39211
|
}
|
|
39190
|
-
|
|
39191
|
-
|
|
39192
|
-
|
|
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);
|
|
39193
39217
|
}
|
|
39194
|
-
function
|
|
39195
|
-
|
|
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;
|
|
39196
39232
|
}
|
|
39197
|
-
|
|
39198
|
-
|
|
39199
|
-
|
|
39200
|
-
|
|
39201
|
-
|
|
39202
|
-
await host.followedChanged();
|
|
39203
|
-
await unregistering;
|
|
39204
|
-
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;
|
|
39205
39238
|
}
|
|
39206
|
-
function
|
|
39207
|
-
if (
|
|
39208
|
-
|
|
39209
|
-
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`);
|
|
39210
39242
|
}
|
|
39211
|
-
|
|
39212
|
-
}
|
|
39213
|
-
function isRoleForward(envelope) {
|
|
39214
|
-
return envelope.dedupe_key?.startsWith(ROLE_FORWARD_KEY_PREFIX) === true || envelope.topic?.startsWith(ROLE_TOPIC_PREFIX) === true;
|
|
39243
|
+
return value;
|
|
39215
39244
|
}
|
|
39216
|
-
function
|
|
39217
|
-
|
|
39218
|
-
|
|
39219
|
-
|
|
39220
|
-
}
|
|
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") };
|
|
39221
39252
|
}
|
|
39222
|
-
var
|
|
39223
|
-
|
|
39224
|
-
|
|
39225
|
-
|
|
39226
|
-
|
|
39227
|
-
|
|
39228
|
-
|
|
39229
|
-
|
|
39230
|
-
|
|
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;
|
|
39231
39263
|
}
|
|
39232
|
-
|
|
39233
|
-
return result.success ? result.data : undefined;
|
|
39264
|
+
return nonEmpty(env[HARNESS_DIRECTORY_VARIABLE[harness]]);
|
|
39234
39265
|
}
|
|
39235
|
-
function
|
|
39236
|
-
|
|
39237
|
-
|
|
39238
|
-
|
|
39239
|
-
const drop = (entry) => {
|
|
39240
|
-
entries.delete(entry);
|
|
39241
|
-
if (entry.event_id !== undefined && byEvent.get(entry.event_id) === entry) {
|
|
39242
|
-
byEvent.delete(entry.event_id);
|
|
39243
|
-
}
|
|
39244
|
-
if (entry.dedupe_key !== undefined && byKey.get(entry.dedupe_key) === entry) {
|
|
39245
|
-
byKey.delete(entry.dedupe_key);
|
|
39246
|
-
}
|
|
39247
|
-
};
|
|
39248
|
-
const add = (entry) => {
|
|
39249
|
-
if (entries.size >= REPLAY_LEDGER_LIMIT) {
|
|
39250
|
-
const oldest = entries.values().next();
|
|
39251
|
-
if (oldest.done !== true)
|
|
39252
|
-
drop(oldest.value);
|
|
39253
|
-
}
|
|
39254
|
-
entries.add(entry);
|
|
39255
|
-
if (entry.event_id !== undefined)
|
|
39256
|
-
byEvent.set(entry.event_id, entry);
|
|
39257
|
-
if (entry.dedupe_key !== undefined)
|
|
39258
|
-
byKey.set(entry.dedupe_key, entry);
|
|
39259
|
-
};
|
|
39260
|
-
const expire = () => {
|
|
39261
|
-
const at = now();
|
|
39262
|
-
for (const entry of entries) {
|
|
39263
|
-
if (at - entry.at < REPLAY_LEDGER_TTL_MS)
|
|
39264
|
-
break;
|
|
39265
|
-
drop(entry);
|
|
39266
|
-
}
|
|
39267
|
-
};
|
|
39268
|
-
const has = (identity) => {
|
|
39269
|
-
expire();
|
|
39270
|
-
return identity.event_id !== undefined && byEvent.has(identity.event_id) || identity.dedupe_key !== undefined && byKey.has(identity.dedupe_key);
|
|
39271
|
-
};
|
|
39272
|
-
return {
|
|
39273
|
-
has,
|
|
39274
|
-
record(identity) {
|
|
39275
|
-
if (identity.event_id === undefined && identity.dedupe_key === undefined)
|
|
39276
|
-
return;
|
|
39277
|
-
if (has(identity))
|
|
39278
|
-
return;
|
|
39279
|
-
const entry = { ...identity, at: now() };
|
|
39280
|
-
add(entry);
|
|
39281
|
-
return entry;
|
|
39282
|
-
},
|
|
39283
|
-
restore(restored) {
|
|
39284
|
-
entries = new Set;
|
|
39285
|
-
byEvent.clear();
|
|
39286
|
-
byKey.clear();
|
|
39287
|
-
const at = now();
|
|
39288
|
-
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);
|
|
39289
|
-
for (const entry of kept)
|
|
39290
|
-
add({ ...entry });
|
|
39291
|
-
},
|
|
39292
|
-
list() {
|
|
39293
|
-
expire();
|
|
39294
|
-
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;
|
|
39295
39270
|
}
|
|
39296
|
-
|
|
39297
|
-
|
|
39298
|
-
|
|
39299
|
-
|
|
39300
|
-
|
|
39301
|
-
function deliverSubject(sessionID) {
|
|
39302
|
-
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;
|
|
39303
39276
|
}
|
|
39304
|
-
|
|
39305
|
-
|
|
39306
|
-
|
|
39307
|
-
streamID: "Envoy-Stream-Id",
|
|
39308
|
-
delivered: "Envoy-Delivered",
|
|
39309
|
-
storedAt: "Envoy-Stored-At",
|
|
39310
|
-
gap: "Envoy-Gap"
|
|
39311
|
-
};
|
|
39312
|
-
var RELAY_PROGRESS_INTERVAL_MS = 20000;
|
|
39313
|
-
var FOLLOW_CLOCK_ALLOWANCE_MS = 2000;
|
|
39314
|
-
var RELAY_ATTEMPTS_BEFORE_FAILED = 3;
|
|
39315
|
-
var decoder4 = new TextDecoder;
|
|
39316
|
-
var DECIMAL = /^[0-9]+$/;
|
|
39317
|
-
var RFC3339 = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d+))?(Z|[+-]\d{2}:\d{2})$/;
|
|
39318
|
-
function positiveInteger(text) {
|
|
39319
|
-
if (text === null || text === undefined || !DECIMAL.test(text))
|
|
39320
|
-
return;
|
|
39321
|
-
const value = Number(text);
|
|
39322
|
-
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));
|
|
39323
39280
|
}
|
|
39324
|
-
function
|
|
39325
|
-
const
|
|
39326
|
-
if (
|
|
39327
|
-
return;
|
|
39328
|
-
const
|
|
39329
|
-
|
|
39330
|
-
|
|
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;
|
|
39331
39295
|
}
|
|
39332
|
-
|
|
39333
|
-
|
|
39334
|
-
|
|
39335
|
-
|
|
39336
|
-
|
|
39337
|
-
|
|
39338
|
-
|
|
39339
|
-
|
|
39340
|
-
|
|
39341
|
-
|
|
39342
|
-
|
|
39343
|
-
|
|
39344
|
-
|
|
39345
|
-
|
|
39346
|
-
|
|
39347
|
-
|
|
39348
|
-
|
|
39349
|
-
|
|
39350
|
-
|
|
39351
|
-
|
|
39352
|
-
|
|
39353
|
-
|
|
39354
|
-
|
|
39355
|
-
|
|
39356
|
-
|
|
39357
|
-
subject,
|
|
39358
|
-
streamSeq,
|
|
39359
|
-
streamID,
|
|
39360
|
-
delivered,
|
|
39361
|
-
storedAt,
|
|
39362
|
-
storedAtMs,
|
|
39363
|
-
gap: headers.get(RelayHeader.gap) === "true",
|
|
39364
|
-
data: typeof message.data === "string" ? message.data : decoder4.decode(message.data),
|
|
39365
|
-
reply
|
|
39366
|
-
}
|
|
39367
|
-
};
|
|
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 };
|
|
39368
39321
|
}
|
|
39369
|
-
function
|
|
39370
|
-
return
|
|
39322
|
+
function offWithError(error) {
|
|
39323
|
+
return { enabled: false, notice: null, error };
|
|
39371
39324
|
}
|
|
39372
|
-
|
|
39373
|
-
|
|
39374
|
-
stream_recreated: "Envoy's notification stream was recreated, so messages published to this session before that may be lost."
|
|
39375
|
-
};
|
|
39376
|
-
var ConsumerDeliverySnapshotSchema = object({
|
|
39377
|
-
resume: object({ stream_id: string2().min(1), seq: number2().int().nonnegative() }).optional(),
|
|
39378
|
-
follows: record(string2(), number2()).default({})
|
|
39379
|
-
});
|
|
39380
|
-
function parseIdentity(data) {
|
|
39381
|
-
let parsed;
|
|
39325
|
+
function httpUrl2(value) {
|
|
39326
|
+
let url2;
|
|
39382
39327
|
try {
|
|
39383
|
-
|
|
39328
|
+
url2 = new URL(value);
|
|
39384
39329
|
} catch {
|
|
39385
39330
|
return;
|
|
39386
39331
|
}
|
|
39387
|
-
|
|
39388
|
-
if (!identity.success)
|
|
39389
|
-
return;
|
|
39390
|
-
const { event_id, dedupe_key } = identity.data;
|
|
39391
|
-
return event_id === undefined && dedupe_key === undefined ? undefined : identity.data;
|
|
39332
|
+
return url2.protocol === "http:" || url2.protocol === "https:" ? url2 : undefined;
|
|
39392
39333
|
}
|
|
39393
|
-
function
|
|
39394
|
-
|
|
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}`;
|
|
39395
39344
|
}
|
|
39396
|
-
function
|
|
39397
|
-
|
|
39398
|
-
|
|
39345
|
+
function danglingLink(filePath) {
|
|
39346
|
+
try {
|
|
39347
|
+
return lstatSync(filePath).isSymbolicLink() ? readlinkSync2(filePath) : null;
|
|
39348
|
+
} catch {
|
|
39349
|
+
return null;
|
|
39350
|
+
}
|
|
39399
39351
|
}
|
|
39400
|
-
function
|
|
39401
|
-
|
|
39402
|
-
|
|
39403
|
-
|
|
39404
|
-
|
|
39405
|
-
|
|
39406
|
-
|
|
39407
|
-
let streamID;
|
|
39408
|
-
let held = new Set;
|
|
39409
|
-
let written;
|
|
39410
|
-
let registeredResume;
|
|
39411
|
-
let gapTold = false;
|
|
39412
|
-
let closed = false;
|
|
39413
|
-
let relayed;
|
|
39414
|
-
let persisted = JSON.stringify({ follows: {} });
|
|
39415
|
-
const entries = new Map;
|
|
39416
|
-
const queue = [];
|
|
39417
|
-
let blocking;
|
|
39418
|
-
let pumping = false;
|
|
39419
|
-
const safely = (what, call) => {
|
|
39420
|
-
try {
|
|
39421
|
-
call();
|
|
39422
|
-
} catch (error) {
|
|
39423
|
-
try {
|
|
39424
|
-
host.log(`consumer delivery: ${what} failed`, error);
|
|
39425
|
-
} catch {}
|
|
39426
|
-
}
|
|
39427
|
-
};
|
|
39428
|
-
const log = (message, error) => safely("log", () => host.log(`consumer delivery: ${message}`, error));
|
|
39429
|
-
const send = (reply, body) => safely(`reply ${body}`, () => options.publish(reply, body));
|
|
39430
|
-
const tellGap = (kind) => safely("gap notice", () => host.gap({ kind, message: GAP_MESSAGES[kind] }));
|
|
39431
|
-
const switchStream = (id) => {
|
|
39432
|
-
if (streamID === id)
|
|
39433
|
-
return;
|
|
39434
|
-
streamID = id;
|
|
39435
|
-
held = new Set;
|
|
39436
|
-
written = undefined;
|
|
39437
|
-
};
|
|
39438
|
-
const tellReplacedResume = (id) => {
|
|
39439
|
-
const asked = resumePoint() ?? registeredResume;
|
|
39440
|
-
if (asked === undefined || asked.stream_id === id)
|
|
39441
|
-
return;
|
|
39442
|
-
registeredResume = undefined;
|
|
39443
|
-
tellGap("stream_recreated");
|
|
39444
|
-
};
|
|
39445
|
-
const followedAt = (subject) => {
|
|
39446
|
-
if (subject === ownSubject)
|
|
39447
|
-
return Number.NEGATIVE_INFINITY;
|
|
39448
|
-
let earliest;
|
|
39449
|
-
for (const [topic, at] of follows) {
|
|
39450
|
-
if (!reaches(topic, subject))
|
|
39451
|
-
continue;
|
|
39452
|
-
if (earliest === undefined || at < earliest)
|
|
39453
|
-
earliest = at;
|
|
39454
|
-
}
|
|
39455
|
-
return earliest;
|
|
39456
|
-
};
|
|
39457
|
-
const followedOnCorePath = (frame) => {
|
|
39458
|
-
const state = ledger.state();
|
|
39459
|
-
if (state.stream_id !== frame.streamID)
|
|
39460
|
-
return false;
|
|
39461
|
-
for (const [topic, cursor] of Object.entries(state.subjects)) {
|
|
39462
|
-
if (cursor.since === undefined || frame.streamSeq <= cursor.since)
|
|
39463
|
-
continue;
|
|
39464
|
-
if (reaches(topic, frame.subject))
|
|
39465
|
-
return true;
|
|
39466
|
-
}
|
|
39467
|
-
return false;
|
|
39468
|
-
};
|
|
39469
|
-
const settle = (entry, body) => {
|
|
39470
|
-
if (entry.settled)
|
|
39471
|
-
return;
|
|
39472
|
-
entry.settled = true;
|
|
39473
|
-
entry.stopProgress();
|
|
39474
|
-
entries.delete(entry.key);
|
|
39475
|
-
for (const reply of entry.replies)
|
|
39476
|
-
send(reply, body);
|
|
39477
|
-
if (body === "done" && entry.frame.streamID === streamID) {
|
|
39478
|
-
held.delete(entry.frame.streamSeq);
|
|
39479
|
-
written = Math.max(written ?? 0, entry.frame.streamSeq);
|
|
39480
|
-
}
|
|
39481
|
-
release(entry);
|
|
39482
|
-
};
|
|
39483
|
-
const release = (entry) => {
|
|
39484
|
-
if (blocking !== entry)
|
|
39485
|
-
return;
|
|
39486
|
-
blocking = undefined;
|
|
39487
|
-
pump();
|
|
39488
|
-
};
|
|
39489
|
-
const alreadyWritten = (identity) => identity !== undefined && ledger.delivered(identity);
|
|
39490
|
-
const failed = (entry, error) => {
|
|
39491
|
-
if (entry.settled)
|
|
39492
|
-
return;
|
|
39493
|
-
if (entry.frame.delivered < RELAY_ATTEMPTS_BEFORE_FAILED) {
|
|
39494
|
-
log(`frame ${entry.frame.streamSeq} failed; it comes again`, error);
|
|
39495
|
-
settle(entry, "nak");
|
|
39496
|
-
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) };
|
|
39497
39359
|
}
|
|
39498
|
-
|
|
39499
|
-
|
|
39500
|
-
|
|
39501
|
-
|
|
39502
|
-
|
|
39503
|
-
|
|
39504
|
-
|
|
39505
|
-
|
|
39506
|
-
|
|
39507
|
-
|
|
39508
|
-
...entry.frame,
|
|
39509
|
-
late: entry.late,
|
|
39510
|
-
done: () => settle(entry, "done"),
|
|
39511
|
-
nak: () => settle(entry, "nak"),
|
|
39512
|
-
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}`}`
|
|
39513
39370
|
};
|
|
39514
|
-
|
|
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);
|
|
39515
39400
|
let result;
|
|
39516
39401
|
try {
|
|
39517
|
-
result =
|
|
39518
|
-
|
|
39519
|
-
|
|
39520
|
-
|
|
39521
|
-
|
|
39522
|
-
|
|
39523
|
-
|
|
39524
|
-
|
|
39525
|
-
|
|
39526
|
-
}
|
|
39527
|
-
|
|
39528
|
-
failed(entry, error);
|
|
39529
|
-
release(entry);
|
|
39530
|
-
});
|
|
39531
|
-
};
|
|
39532
|
-
function pump() {
|
|
39533
|
-
if (pumping)
|
|
39534
|
-
return;
|
|
39535
|
-
pumping = true;
|
|
39536
|
-
try {
|
|
39537
|
-
while (blocking === undefined && !closed) {
|
|
39538
|
-
const entry = queue.shift();
|
|
39539
|
-
if (entry === undefined)
|
|
39540
|
-
return;
|
|
39541
|
-
if (entry.settled)
|
|
39542
|
-
continue;
|
|
39543
|
-
if (alreadyWritten(entry.identity)) {
|
|
39544
|
-
settle(entry, "done");
|
|
39545
|
-
continue;
|
|
39546
|
-
}
|
|
39547
|
-
handOff(entry);
|
|
39548
|
-
}
|
|
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 };
|
|
39549
39413
|
} finally {
|
|
39550
|
-
|
|
39551
|
-
}
|
|
39552
|
-
}
|
|
39553
|
-
const receive = (message) => {
|
|
39554
|
-
const decoded = decodeRelayFrame(message);
|
|
39555
|
-
if ("error" in decoded) {
|
|
39556
|
-
log(`dropping an undecodable relay frame: ${decoded.error}`);
|
|
39557
|
-
if (message.reply !== undefined && message.reply !== "")
|
|
39558
|
-
send(message.reply, "done");
|
|
39559
|
-
return;
|
|
39414
|
+
closeSync2(fd);
|
|
39560
39415
|
}
|
|
39561
|
-
|
|
39562
|
-
|
|
39563
|
-
|
|
39564
|
-
|
|
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
|
+
};
|
|
39565
39424
|
}
|
|
39566
|
-
|
|
39567
|
-
|
|
39568
|
-
|
|
39569
|
-
|
|
39570
|
-
|
|
39571
|
-
if (streamID !== undefined && (written !== undefined || held.size > 0)) {
|
|
39572
|
-
tellGap("stream_recreated");
|
|
39573
|
-
} else if (streamID === undefined) {
|
|
39574
|
-
tellReplacedResume(frame.streamID);
|
|
39575
|
-
}
|
|
39576
|
-
switchStream(frame.streamID);
|
|
39425
|
+
if (!result.success) {
|
|
39426
|
+
return {
|
|
39427
|
+
failure: `exited with ${result.signalCode ?? `status ${result.exitCode}`}`,
|
|
39428
|
+
timedOut: false
|
|
39429
|
+
};
|
|
39577
39430
|
}
|
|
39578
|
-
const
|
|
39579
|
-
|
|
39580
|
-
|
|
39581
|
-
|
|
39582
|
-
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
|
+
};
|
|
39583
39439
|
}
|
|
39584
|
-
|
|
39585
|
-
|
|
39586
|
-
|
|
39587
|
-
|
|
39588
|
-
|
|
39589
|
-
|
|
39590
|
-
|
|
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`);
|
|
39591
39449
|
}
|
|
39592
|
-
|
|
39593
|
-
|
|
39594
|
-
|
|
39595
|
-
|
|
39596
|
-
|
|
39597
|
-
|
|
39598
|
-
|
|
39599
|
-
|
|
39600
|
-
|
|
39601
|
-
},
|
|
39602
|
-
settled: false
|
|
39603
|
-
};
|
|
39604
|
-
entry.stopProgress = repeat(RELAY_PROGRESS_INTERVAL_MS, () => {
|
|
39605
|
-
if (entry.settled)
|
|
39606
|
-
return;
|
|
39607
|
-
for (const reply of entry.replies)
|
|
39608
|
-
send(reply, "progress");
|
|
39609
|
-
});
|
|
39610
|
-
entries.set(key, entry);
|
|
39611
|
-
queue.push(entry);
|
|
39612
|
-
pump();
|
|
39613
|
-
};
|
|
39614
|
-
const resumePoint = () => {
|
|
39615
|
-
if (streamID === undefined)
|
|
39616
|
-
return;
|
|
39617
|
-
if (held.size > 0)
|
|
39618
|
-
return { stream_id: streamID, seq: Math.min(...held) - 1 };
|
|
39619
|
-
return written === undefined ? undefined : { stream_id: streamID, seq: written };
|
|
39620
|
-
};
|
|
39621
|
-
const seededResume = () => {
|
|
39622
|
-
const state = ledger.state();
|
|
39623
|
-
const stream = state.stream_id;
|
|
39624
|
-
if (stream === undefined)
|
|
39625
|
-
return;
|
|
39626
|
-
let lowest;
|
|
39627
|
-
for (const [subject, cursor] of Object.entries(state.subjects)) {
|
|
39628
|
-
if (cursor.after === undefined || !streamStores(subject))
|
|
39629
|
-
continue;
|
|
39630
|
-
if (lowest === undefined || cursor.after < lowest)
|
|
39631
|
-
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`);
|
|
39632
39459
|
}
|
|
39633
|
-
|
|
39634
|
-
|
|
39635
|
-
|
|
39636
|
-
|
|
39637
|
-
|
|
39638
|
-
|
|
39639
|
-
|
|
39640
|
-
|
|
39641
|
-
|
|
39642
|
-
|
|
39643
|
-
|
|
39644
|
-
|
|
39645
|
-
|
|
39646
|
-
|
|
39647
|
-
|
|
39648
|
-
|
|
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
|
-
|
|
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 };
|
|
39724
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
|
|
39725
39592
|
};
|
|
39726
39593
|
}
|
|
39727
|
-
|
|
39728
|
-
|
|
39729
|
-
if (
|
|
39730
|
-
|
|
39731
|
-
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) };
|
|
39732
39598
|
}
|
|
39733
|
-
|
|
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);
|
|
39734
39622
|
}
|
|
39735
39623
|
|
|
39736
39624
|
// ../envoy-client/src/dispatch-config.ts
|
|
39737
39625
|
var DEFAULT_SERVER_URL = "http://localhost:8766";
|
|
39738
|
-
function withoutTrailingSlashes2(url) {
|
|
39739
|
-
let end = url.length;
|
|
39740
|
-
while (end > 0 && url[end - 1] === "/")
|
|
39741
|
-
end -= 1;
|
|
39742
|
-
return url.slice(0, end);
|
|
39743
|
-
}
|
|
39744
39626
|
function checkoutPair(checkout, serverUrl) {
|
|
39745
39627
|
const { token } = checkout.dispatch;
|
|
39746
39628
|
if (token === undefined) {
|
|
@@ -39802,7 +39684,7 @@ function dispatchSettings(env, config, options = {}) {
|
|
|
39802
39684
|
enabled: true,
|
|
39803
39685
|
url,
|
|
39804
39686
|
credential,
|
|
39805
|
-
...publicUrl === undefined ? {} : { publicUrl: publicUrl
|
|
39687
|
+
...publicUrl === undefined ? {} : { publicUrl: withoutTrailingSlashes(publicUrl) }
|
|
39806
39688
|
};
|
|
39807
39689
|
}
|
|
39808
39690
|
function brokerRouteForced(env) {
|
|
@@ -39858,7 +39740,7 @@ function dispatchTarget(env, config) {
|
|
|
39858
39740
|
return offWithError(messageFor(error));
|
|
39859
39741
|
}
|
|
39860
39742
|
if (route.traded) {
|
|
39861
|
-
const brokered =
|
|
39743
|
+
const brokered = withoutTrailingSlashes(explicitUrl ?? own.serverUrl ?? DEFAULT_SERVER_URL);
|
|
39862
39744
|
return { enabled: true, url: brokered, source: { kind: "broker" } };
|
|
39863
39745
|
}
|
|
39864
39746
|
if (forced) {
|
|
@@ -39866,7 +39748,7 @@ function dispatchTarget(env, config) {
|
|
|
39866
39748
|
}
|
|
39867
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}`);
|
|
39868
39750
|
}
|
|
39869
|
-
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));
|
|
39870
39752
|
return { enabled: true, url, source };
|
|
39871
39753
|
}
|
|
39872
39754
|
function activeDispatchSettings(resolution) {
|
|
@@ -41163,6 +41045,133 @@ function writeSessionTitle(dir, title) {
|
|
|
41163
41045
|
writeAtomically2(join6(dir, "title"), title);
|
|
41164
41046
|
}
|
|
41165
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
|
+
|
|
41166
41175
|
// ../envoy-client/src/nats-auth.ts
|
|
41167
41176
|
var import_nats = __toESM(require_mod4(), 1);
|
|
41168
41177
|
var seedKeys = import_nats.nkeys;
|
|
@@ -42697,7 +42706,7 @@ var import_nats2 = __toESM(require_mod4(), 1);
|
|
|
42697
42706
|
|
|
42698
42707
|
// package.json
|
|
42699
42708
|
var name = "@sjawhar/pi-envoy";
|
|
42700
|
-
var version2 = "16.
|
|
42709
|
+
var version2 = "16.41.1";
|
|
42701
42710
|
var package_default = {
|
|
42702
42711
|
name,
|
|
42703
42712
|
version: version2,
|