@sjawhar/claude-envoy 6.14.0 → 6.14.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/.claude-plugin/plugin.json +1 -1
- package/.omp-plugin/plugin.json +1 -1
- package/README.md +42 -2
- package/dist/codex-hook.js +3 -0
- package/dist/dispatch.js +108 -57
- package/dist/envoy-channel.js +558 -219
- package/dist/session-hook.js +3 -0
- package/package.json +1 -1
- package/src/agent-stream-runner.ts +10 -7
- package/src/channel-connection.ts +21 -9
- package/src/codex/codex-server.ts +23 -2
- package/src/envoy-channel-server.ts +296 -113
- package/src/envoy-off.ts +16 -1
- package/src/inbound-outage-lines.ts +65 -0
- package/src/legion-claim.ts +13 -3
- package/src/stop-gates.ts +17 -0
package/dist/envoy-channel.js
CHANGED
|
@@ -41987,6 +41987,7 @@ var SESSION_LIVE_WINDOW_MS = 300000;
|
|
|
41987
41987
|
var LISTENER_CALL_TIMEOUT_MS = 5000;
|
|
41988
41988
|
var LISTENER_CALL_RETRY_DELAY_MS = 250;
|
|
41989
41989
|
var LISTENER_CALL_ATTEMPTS = 2;
|
|
41990
|
+
var BUS_UNANSWERED_PREFIX = "envoy: the message bus did not answer";
|
|
41990
41991
|
// ../contracts/src/related-forms.ts
|
|
41991
41992
|
function relatedCheckFormOf(suggestions) {
|
|
41992
41993
|
const { outcome, missing } = suggestions;
|
|
@@ -60886,6 +60887,8 @@ function keepNatsCredential(connection, credential, warn) {
|
|
|
60886
60887
|
if (credential.needsReconnect())
|
|
60887
60888
|
await connection.reconnect();
|
|
60888
60889
|
} catch (error) {
|
|
60890
|
+
if (stopped)
|
|
60891
|
+
return;
|
|
60889
60892
|
if (error instanceof SessionMovedError) {
|
|
60890
60893
|
schedule(0);
|
|
60891
60894
|
return;
|
|
@@ -61619,6 +61622,9 @@ class EnvoyApiError extends Error {
|
|
|
61619
61622
|
this.notSent = notSentAnswer && shown.afterUnanswered !== true;
|
|
61620
61623
|
}
|
|
61621
61624
|
}
|
|
61625
|
+
function isBusUnanswered(error) {
|
|
61626
|
+
return error instanceof EnvoyApiError && error.details.status === 503 && error.message.startsWith(BUS_UNANSWERED_PREFIX);
|
|
61627
|
+
}
|
|
61622
61628
|
var mintedDedupeKey2 = new RegExp(MINTED_DEDUPE_KEY_PATTERN);
|
|
61623
61629
|
|
|
61624
61630
|
class EnvoySendUnconfirmedError extends Error {
|
|
@@ -61641,6 +61647,28 @@ class EnvoySendUnconfirmedError extends Error {
|
|
|
61641
61647
|
this.resendSafe = resendSafe;
|
|
61642
61648
|
}
|
|
61643
61649
|
}
|
|
61650
|
+
var STOP_GATE = {
|
|
61651
|
+
subscribe: "refused",
|
|
61652
|
+
unsubscribe: "refused",
|
|
61653
|
+
getInterest: "refused",
|
|
61654
|
+
setRole: "refused",
|
|
61655
|
+
getRole: "refused",
|
|
61656
|
+
unregisterSession: "sent",
|
|
61657
|
+
handBack: "sent",
|
|
61658
|
+
send: "sent",
|
|
61659
|
+
publish: "sent",
|
|
61660
|
+
listSessions: "sent"
|
|
61661
|
+
};
|
|
61662
|
+
|
|
61663
|
+
class EnvoyStoppingError extends Error {
|
|
61664
|
+
call;
|
|
61665
|
+
name = "EnvoyStoppingError";
|
|
61666
|
+
constructor(call, method, url) {
|
|
61667
|
+
super(`this session is stopping, so it sends the listener nothing: ${method} ${url} was not sent`);
|
|
61668
|
+
this.call = call;
|
|
61669
|
+
}
|
|
61670
|
+
}
|
|
61671
|
+
var handBacks = new WeakMap;
|
|
61644
61672
|
function resolveCallTiming(config) {
|
|
61645
61673
|
return {
|
|
61646
61674
|
timeoutMs: config.timeoutMs ?? LISTENER_CALL_TIMEOUT_MS,
|
|
@@ -61664,7 +61692,9 @@ function createEnvoyClient(config) {
|
|
|
61664
61692
|
}
|
|
61665
61693
|
};
|
|
61666
61694
|
const noAnswer = (method, url, error) => new Error(`${method ?? "GET"} ${shown(url)} got no answer: ${errorClass(error)}`);
|
|
61667
|
-
const attemptOnce = async (url, init, deadlineMs, actingSession) => {
|
|
61695
|
+
const attemptOnce = async (url, init, deadlineMs, actingSession, refusedByStop) => {
|
|
61696
|
+
if (refusedByStop())
|
|
61697
|
+
return { stopping: true };
|
|
61668
61698
|
let response;
|
|
61669
61699
|
let unminted;
|
|
61670
61700
|
const deadline = AbortSignal.timeout(deadlineMs);
|
|
@@ -61680,6 +61710,8 @@ function createEnvoyClient(config) {
|
|
|
61680
61710
|
}
|
|
61681
61711
|
if (unminted !== undefined && deadline.aborted)
|
|
61682
61712
|
return { notSent: unminted };
|
|
61713
|
+
if (refusedByStop())
|
|
61714
|
+
return { stopping: true };
|
|
61683
61715
|
sent.note(presented);
|
|
61684
61716
|
for (const [name, value] of Object.entries(presented ?? {}))
|
|
61685
61717
|
headers.set(name, value);
|
|
@@ -61707,59 +61739,70 @@ function createEnvoyClient(config) {
|
|
|
61707
61739
|
return { unanswered: noAnswer(init.method, url, error) };
|
|
61708
61740
|
}
|
|
61709
61741
|
};
|
|
61710
|
-
const request = async (path, init, options = {}) => {
|
|
61742
|
+
const request = async (call, path, init, options = {}) => {
|
|
61711
61743
|
const url = `${baseUrl}${path}`;
|
|
61712
61744
|
const tries = options.retry === false ? 1 : attempts;
|
|
61745
|
+
const gated = STOP_GATE[call] === "refused";
|
|
61746
|
+
const refusedByStop = () => gated && config.stopping?.() === true;
|
|
61747
|
+
let busUnanswered;
|
|
61748
|
+
let sent = false;
|
|
61713
61749
|
let unanswered = false;
|
|
61750
|
+
let lastEnd;
|
|
61714
61751
|
for (let attempt = 0;attempt < tries; attempt += 1) {
|
|
61715
61752
|
options.onAttempt?.();
|
|
61716
|
-
const outcome = await attemptOnce(url, init, options.timeoutMs ?? timeoutMs, options.actingSession);
|
|
61753
|
+
const outcome = await attemptOnce(url, init, options.timeoutMs ?? timeoutMs, options.actingSession, refusedByStop);
|
|
61754
|
+
if ("stopping" in outcome) {
|
|
61755
|
+
if (sent && lastEnd !== undefined)
|
|
61756
|
+
throw lastEnd();
|
|
61757
|
+
throw new EnvoyStoppingError(call, init.method ?? "GET", shown(url));
|
|
61758
|
+
}
|
|
61759
|
+
let end;
|
|
61717
61760
|
if ("unanswered" in outcome) {
|
|
61718
61761
|
unanswered = true;
|
|
61719
61762
|
options.onUnanswered?.();
|
|
61720
|
-
|
|
61721
|
-
|
|
61722
|
-
|
|
61723
|
-
|
|
61724
|
-
|
|
61725
|
-
|
|
61726
|
-
|
|
61727
|
-
|
|
61728
|
-
|
|
61729
|
-
|
|
61730
|
-
|
|
61731
|
-
|
|
61732
|
-
|
|
61733
|
-
|
|
61734
|
-
|
|
61735
|
-
|
|
61736
|
-
|
|
61737
|
-
|
|
61738
|
-
|
|
61739
|
-
|
|
61740
|
-
|
|
61741
|
-
|
|
61742
|
-
|
|
61743
|
-
|
|
61744
|
-
tokens: redacted,
|
|
61745
|
-
redirectTo: redirectOrigin(response, url, redacted),
|
|
61746
|
-
afterUnanswered: unanswered
|
|
61747
|
-
});
|
|
61748
|
-
if (mayHaveReachedServer(response.status)) {
|
|
61763
|
+
sent = true;
|
|
61764
|
+
end = () => busUnanswered ?? outcome.unanswered;
|
|
61765
|
+
} else if ("notSent" in outcome) {
|
|
61766
|
+
end = () => {
|
|
61767
|
+
options.onNotSent?.();
|
|
61768
|
+
return busUnanswered ?? outcome.notSent;
|
|
61769
|
+
};
|
|
61770
|
+
} else {
|
|
61771
|
+
const { response, body } = outcome;
|
|
61772
|
+
if (response.ok)
|
|
61773
|
+
return body;
|
|
61774
|
+
const redacted = tokens();
|
|
61775
|
+
const error = new EnvoyApiError({
|
|
61776
|
+
method: init.method ?? "GET",
|
|
61777
|
+
url: shown(url),
|
|
61778
|
+
status: response.status,
|
|
61779
|
+
responseBody: body
|
|
61780
|
+
}, {
|
|
61781
|
+
tokens: redacted,
|
|
61782
|
+
redirectTo: redirectOrigin(response, url, redacted),
|
|
61783
|
+
afterUnanswered: unanswered
|
|
61784
|
+
});
|
|
61785
|
+
if (!mayHaveReachedServer(response.status))
|
|
61786
|
+
throw error;
|
|
61749
61787
|
if (!error.notSent) {
|
|
61750
61788
|
unanswered = true;
|
|
61751
61789
|
options.onUnanswered?.();
|
|
61790
|
+
sent = true;
|
|
61752
61791
|
}
|
|
61753
|
-
if (
|
|
61754
|
-
|
|
61755
|
-
|
|
61756
|
-
}
|
|
61792
|
+
if (options.busUnansweredStands === true && isBusUnanswered(error))
|
|
61793
|
+
busUnanswered = error;
|
|
61794
|
+
end = () => error.reason === undefined ? busUnanswered ?? error : error;
|
|
61757
61795
|
}
|
|
61758
|
-
|
|
61796
|
+
lastEnd = end;
|
|
61797
|
+
if (attempt + 1 < tries) {
|
|
61798
|
+
await waitForRetry(retryDelayMs);
|
|
61799
|
+
continue;
|
|
61800
|
+
}
|
|
61801
|
+
throw end();
|
|
61759
61802
|
}
|
|
61760
61803
|
throw new Error("Envoy request exhausted retries");
|
|
61761
61804
|
};
|
|
61762
|
-
const post = (path, body, options) => request(path, {
|
|
61805
|
+
const post = (call, path, body, options) => request(call, path, {
|
|
61763
61806
|
method: "POST",
|
|
61764
61807
|
headers: {
|
|
61765
61808
|
"Content-Type": "application/json",
|
|
@@ -61783,16 +61826,17 @@ function createEnvoyClient(config) {
|
|
|
61783
61826
|
plugin: input.plugin.name,
|
|
61784
61827
|
plugin_version: input.plugin.version
|
|
61785
61828
|
});
|
|
61786
|
-
|
|
61787
|
-
|
|
61788
|
-
|
|
61789
|
-
|
|
61790
|
-
|
|
61791
|
-
|
|
61792
|
-
|
|
61793
|
-
|
|
61794
|
-
},
|
|
61795
|
-
|
|
61829
|
+
const removeTopics = async (call, input) => {
|
|
61830
|
+
await post(call, "/v1/interests/unsubscribe", {
|
|
61831
|
+
session_id: input.sessionID,
|
|
61832
|
+
topics: expandSubscriptionTopics(input.topics),
|
|
61833
|
+
...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID }
|
|
61834
|
+
}, { actingSession: input.sessionID, busUnansweredStands: true });
|
|
61835
|
+
};
|
|
61836
|
+
const client = {
|
|
61837
|
+
subscribe: async (input) => parseSubscribeAnswer(answerJson(await post("subscribe", "/v1/interests/subscribe", subscribeBody(input), { actingSession: input.sessionID, busUnansweredStands: true }), "POST", "/v1/interests/subscribe")),
|
|
61838
|
+
unsubscribe: (input) => removeTopics("unsubscribe", input),
|
|
61839
|
+
getInterest: async (sessionID) => InterestWireSchema.parse(answerJson(await request("getInterest", `/v1/interests/${sessionID}`, {}), "GET", `/v1/interests/${sessionID}`)),
|
|
61796
61840
|
send: async (input) => {
|
|
61797
61841
|
const idempotencyKey = input.idempotencyKey ?? crypto.randomUUID();
|
|
61798
61842
|
let unanswered = false;
|
|
@@ -61800,7 +61844,7 @@ function createEnvoyClient(config) {
|
|
|
61800
61844
|
let made = 0;
|
|
61801
61845
|
let response;
|
|
61802
61846
|
try {
|
|
61803
|
-
const answer = await post("/v1/messages/send", {
|
|
61847
|
+
const answer = await post("send", "/v1/messages/send", {
|
|
61804
61848
|
source: input.source ?? "agent",
|
|
61805
61849
|
...sourceSession(input.sourceSessionID),
|
|
61806
61850
|
target_session: input.targetSessionID,
|
|
@@ -61830,19 +61874,24 @@ function createEnvoyClient(config) {
|
|
|
61830
61874
|
throw error;
|
|
61831
61875
|
throw new EnvoySendUnconfirmedError(input.targetSessionID, idempotencyKey, made, error);
|
|
61832
61876
|
}
|
|
61833
|
-
|
|
61877
|
+
const stored = {
|
|
61834
61878
|
envelope: response,
|
|
61835
61879
|
recipient: response.recipient ?? input.targetSessionID,
|
|
61836
61880
|
confirmed: response.recipient !== undefined,
|
|
61837
|
-
recipientState: response.recipient_state ?? "live",
|
|
61838
|
-
state: response.state === "stopped" ? "stopped" : "connected",
|
|
61839
61881
|
...lastSeenOf(response.last_seen),
|
|
61840
61882
|
duplicate: response.duplicate === true
|
|
61841
61883
|
};
|
|
61884
|
+
if (response.state === "no_receiver")
|
|
61885
|
+
return { ...stored, state: "no_receiver" };
|
|
61886
|
+
return {
|
|
61887
|
+
...stored,
|
|
61888
|
+
recipientState: response.recipient_state ?? "live",
|
|
61889
|
+
state: response.state === "stopped" ? "stopped" : "connected"
|
|
61890
|
+
};
|
|
61842
61891
|
},
|
|
61843
61892
|
publish: async (input) => {
|
|
61844
61893
|
const idempotencyKey = input.idempotencyKey ?? crypto.randomUUID();
|
|
61845
|
-
const raw = answerJson(await post("/v1/messages/publish", {
|
|
61894
|
+
const raw = answerJson(await post("publish", "/v1/messages/publish", {
|
|
61846
61895
|
source: input.source ?? "agent",
|
|
61847
61896
|
...sourceSession(input.sourceSessionID),
|
|
61848
61897
|
topic: input.topic,
|
|
@@ -61870,7 +61919,7 @@ function createEnvoyClient(config) {
|
|
|
61870
61919
|
},
|
|
61871
61920
|
unregisterSession: async (input) => {
|
|
61872
61921
|
const query = input.clientInstanceID === undefined ? "" : `?client_instance_id=${encodeURIComponent(input.clientInstanceID)}`;
|
|
61873
|
-
await request(`/v1/sessions/${encodeURIComponent(input.sessionID)}${query}`, { method: "DELETE" }, {
|
|
61922
|
+
await request("unregisterSession", `/v1/sessions/${encodeURIComponent(input.sessionID)}${query}`, { method: "DELETE" }, {
|
|
61874
61923
|
actingSession: input.sessionID,
|
|
61875
61924
|
...input.timeoutMs === undefined ? {} : { timeoutMs: Math.max(1, input.timeoutMs), retry: false }
|
|
61876
61925
|
});
|
|
@@ -61887,7 +61936,7 @@ function createEnvoyClient(config) {
|
|
|
61887
61936
|
};
|
|
61888
61937
|
let raw;
|
|
61889
61938
|
try {
|
|
61890
|
-
raw = await post("/v1/roles/set", body, { actingSession: input.sessionID });
|
|
61939
|
+
raw = await post("setRole", "/v1/roles/set", body, { actingSession: input.sessionID });
|
|
61891
61940
|
} catch (error) {
|
|
61892
61941
|
if (error instanceof EnvoyApiError && error.details.status === 409) {
|
|
61893
61942
|
let answer;
|
|
@@ -61905,7 +61954,7 @@ function createEnvoyClient(config) {
|
|
|
61905
61954
|
const { warnings, ...interest } = RoleSetWireSchema.parse(answerJson(raw, "POST", "/v1/roles/set"));
|
|
61906
61955
|
return { claimed: true, interest, warnings };
|
|
61907
61956
|
},
|
|
61908
|
-
getRole: async (role) => RoleWireSchema.parse(answerJson(await request(`/v1/roles/${encodeURIComponent(role)}`, { method: "GET" }), "GET", `/v1/roles/${encodeURIComponent(role)}`)),
|
|
61957
|
+
getRole: async (role) => RoleWireSchema.parse(answerJson(await request("getRole", `/v1/roles/${encodeURIComponent(role)}`, { method: "GET" }), "GET", `/v1/roles/${encodeURIComponent(role)}`)),
|
|
61909
61958
|
listSessions: async (input = {}) => {
|
|
61910
61959
|
const search = new URLSearchParams;
|
|
61911
61960
|
if (input.directory !== undefined)
|
|
@@ -61915,9 +61964,11 @@ function createEnvoyClient(config) {
|
|
|
61915
61964
|
if (input.include !== undefined)
|
|
61916
61965
|
search.set("include", input.include);
|
|
61917
61966
|
const query = search.size === 0 ? "" : `?${search.toString()}`;
|
|
61918
|
-
return SessionWireSchema.array().parse(answerJson(await request(`/v1/sessions${query}`, {}), "GET", `/v1/sessions${query}`));
|
|
61967
|
+
return SessionWireSchema.array().parse(answerJson(await request("listSessions", `/v1/sessions${query}`, {}), "GET", `/v1/sessions${query}`));
|
|
61919
61968
|
}
|
|
61920
61969
|
};
|
|
61970
|
+
handBacks.set(client, (input) => removeTopics("handBack", input));
|
|
61971
|
+
return client;
|
|
61921
61972
|
}
|
|
61922
61973
|
function waitForRetry(delayMs) {
|
|
61923
61974
|
const { promise, resolve } = Promise.withResolvers();
|
|
@@ -64553,6 +64604,118 @@ function createLedger(now) {
|
|
|
64553
64604
|
};
|
|
64554
64605
|
}
|
|
64555
64606
|
|
|
64607
|
+
// ../envoy-client/src/inbound-outage.ts
|
|
64608
|
+
var REPOSITORIES_UNKNOWN = "repositories_unknown";
|
|
64609
|
+
function refusalKind(error) {
|
|
64610
|
+
if (isBusUnanswered(error))
|
|
64611
|
+
return "unrecorded";
|
|
64612
|
+
if (error instanceof EnvoyApiError && error.reason === REPOSITORIES_UNKNOWN) {
|
|
64613
|
+
return "github_withheld";
|
|
64614
|
+
}
|
|
64615
|
+
return "refused";
|
|
64616
|
+
}
|
|
64617
|
+
var OUTAGE_DEPTH = {
|
|
64618
|
+
unrecorded: 0,
|
|
64619
|
+
github_withheld: 1,
|
|
64620
|
+
refused: 2,
|
|
64621
|
+
no_relay: 2
|
|
64622
|
+
};
|
|
64623
|
+
var NO_CONSUMER_NOTICE = {
|
|
64624
|
+
notice: "Envoy inbound unavailable",
|
|
64625
|
+
detail: "The listener gave this session no durable consumer, which is the only path an Envoy message reaches it by, so it receives none. Everything else works, and the session registers again at every heartbeat; what the stream stored meanwhile arrives once a registration is accepted."
|
|
64626
|
+
};
|
|
64627
|
+
var UNAVAILABLE_NOTICES = {
|
|
64628
|
+
unrecorded: {
|
|
64629
|
+
notice: "Envoy inbound unavailable",
|
|
64630
|
+
detail: "This session's registration is not recorded for certain: the listener's message bus did not answer in time, so the listener may or may not have recorded it. Envoy messages may still reach the session, but nothing confirms it until a registration is accepted. Everything else works, and the session registers again at every heartbeat."
|
|
64631
|
+
},
|
|
64632
|
+
github_withheld: {
|
|
64633
|
+
notice: "Envoy GitHub topics withheld",
|
|
64634
|
+
detail: "The listener could not read the repositories of this session's projects. So it follows none of this session's GitHub topics now, and it withholds their messages. It refused this registration. If the listener already held a registration of this session, it kept that registration and the session's consumer, so the session receives the messages of every other topic the listener follows for it; a topic the session began to follow after its last accepted registration is followed only once a registration is accepted. If the listener held no registration of this session, the session receives nothing until a registration is accepted. The session registers again at every heartbeat."
|
|
64635
|
+
},
|
|
64636
|
+
refused: NO_CONSUMER_NOTICE,
|
|
64637
|
+
no_relay: NO_CONSUMER_NOTICE
|
|
64638
|
+
};
|
|
64639
|
+
var RECEIVES_AGAIN_NOTICE = {
|
|
64640
|
+
notice: "Envoy inbound restored",
|
|
64641
|
+
detail: "The listener accepted a registration of this session, so the session receives Envoy messages again. What the stream stored while it received nothing arrives now."
|
|
64642
|
+
};
|
|
64643
|
+
var RESTORED_NOTICES = {
|
|
64644
|
+
unrecorded: {
|
|
64645
|
+
notice: "Envoy inbound restored",
|
|
64646
|
+
detail: "The listener recorded a registration of this session. A message the stream stored that has not reached the session arrives now."
|
|
64647
|
+
},
|
|
64648
|
+
github_withheld: {
|
|
64649
|
+
notice: "Envoy GitHub topics followed again",
|
|
64650
|
+
detail: "The listener accepted a registration of this session, so it follows this session's GitHub topics again, and their messages reach the session again. A GitHub message the listener withheld meanwhile may be missing."
|
|
64651
|
+
},
|
|
64652
|
+
refused: RECEIVES_AGAIN_NOTICE,
|
|
64653
|
+
no_relay: RECEIVES_AGAIN_NOTICE
|
|
64654
|
+
};
|
|
64655
|
+
function inboundUnavailableNoticeText({ kind, why }) {
|
|
64656
|
+
const { notice, detail } = UNAVAILABLE_NOTICES[kind];
|
|
64657
|
+
return envoyNoticeText({ notice, error: why, detail });
|
|
64658
|
+
}
|
|
64659
|
+
function inboundRestoredNoticeText(kind) {
|
|
64660
|
+
const { notice, detail } = RESTORED_NOTICES[kind];
|
|
64661
|
+
return envoyNoticeText({ notice, detail });
|
|
64662
|
+
}
|
|
64663
|
+
var INBOUND_FIRST_OPEN_BOUND_MS = 120000;
|
|
64664
|
+
function inboundUnavailable(tell, {
|
|
64665
|
+
now = () => Date.now(),
|
|
64666
|
+
firstOpenBoundMs = INBOUND_FIRST_OPEN_BOUND_MS,
|
|
64667
|
+
restored,
|
|
64668
|
+
held,
|
|
64669
|
+
stopping = () => false
|
|
64670
|
+
} = {}) {
|
|
64671
|
+
let told;
|
|
64672
|
+
let everAccepted = false;
|
|
64673
|
+
let heldSince;
|
|
64674
|
+
let unansweredSince;
|
|
64675
|
+
const say = (kind, error) => {
|
|
64676
|
+
if (stopping())
|
|
64677
|
+
return;
|
|
64678
|
+
if (told !== undefined && OUTAGE_DEPTH[kind] <= OUTAGE_DEPTH[told.kind])
|
|
64679
|
+
return;
|
|
64680
|
+
told = { kind, why: messageFor(error) };
|
|
64681
|
+
tell(told);
|
|
64682
|
+
};
|
|
64683
|
+
return {
|
|
64684
|
+
noRelay(error, relayMissing) {
|
|
64685
|
+
if (!everAccepted) {
|
|
64686
|
+
const at = now();
|
|
64687
|
+
const begins = heldSince === undefined;
|
|
64688
|
+
heldSince ??= at;
|
|
64689
|
+
if (at < heldSince + firstOpenBoundMs) {
|
|
64690
|
+
if (begins && !stopping())
|
|
64691
|
+
held?.(relayMissing);
|
|
64692
|
+
return;
|
|
64693
|
+
}
|
|
64694
|
+
}
|
|
64695
|
+
say("no_relay", error);
|
|
64696
|
+
},
|
|
64697
|
+
refused(error) {
|
|
64698
|
+
const kind = refusalKind(error);
|
|
64699
|
+
if (kind === "unrecorded") {
|
|
64700
|
+
const at = now();
|
|
64701
|
+
unansweredSince ??= at;
|
|
64702
|
+
if (at < unansweredSince + firstOpenBoundMs)
|
|
64703
|
+
return;
|
|
64704
|
+
}
|
|
64705
|
+
say(kind, error);
|
|
64706
|
+
},
|
|
64707
|
+
accepted() {
|
|
64708
|
+
everAccepted = true;
|
|
64709
|
+
unansweredSince = undefined;
|
|
64710
|
+
const last = told;
|
|
64711
|
+
if (last === undefined || stopping())
|
|
64712
|
+
return;
|
|
64713
|
+
told = undefined;
|
|
64714
|
+
restored?.(last);
|
|
64715
|
+
}
|
|
64716
|
+
};
|
|
64717
|
+
}
|
|
64718
|
+
|
|
64556
64719
|
// ../envoy-client/src/consumer-delivery.ts
|
|
64557
64720
|
var DELIVER_SUBJECT_PREFIX = "notifications.envoy.deliver.";
|
|
64558
64721
|
function deliverSubject(sessionID) {
|
|
@@ -65010,36 +65173,6 @@ function gapNoticeFields(notice) {
|
|
|
65010
65173
|
function gapNoticeText(notice) {
|
|
65011
65174
|
return envoyNoticeText(gapNoticeFields(notice));
|
|
65012
65175
|
}
|
|
65013
|
-
function inboundUnavailableNoticeText(why) {
|
|
65014
|
-
return envoyNoticeText({
|
|
65015
|
-
notice: "Envoy inbound unavailable",
|
|
65016
|
-
error: why,
|
|
65017
|
-
detail: "The listener gave this session no durable consumer, which is the only path an Envoy message reaches it by, so it receives none. Everything else works, and the session registers again at every heartbeat; what the stream stored meanwhile arrives once a registration is accepted."
|
|
65018
|
-
});
|
|
65019
|
-
}
|
|
65020
|
-
function inboundRestoredNoticeText() {
|
|
65021
|
-
return envoyNoticeText({
|
|
65022
|
-
notice: "Envoy inbound restored",
|
|
65023
|
-
detail: "The listener accepted this session's registration and gave it its durable consumer, so Envoy messages reach it again, the ones the stream stored during the outage first."
|
|
65024
|
-
});
|
|
65025
|
-
}
|
|
65026
|
-
function inboundUnavailable(tell, restored) {
|
|
65027
|
-
let told = false;
|
|
65028
|
-
return {
|
|
65029
|
-
refused(error) {
|
|
65030
|
-
if (told)
|
|
65031
|
-
return;
|
|
65032
|
-
told = true;
|
|
65033
|
-
tell(messageFor(error));
|
|
65034
|
-
},
|
|
65035
|
-
accepted() {
|
|
65036
|
-
if (!told)
|
|
65037
|
-
return;
|
|
65038
|
-
told = false;
|
|
65039
|
-
restored();
|
|
65040
|
-
}
|
|
65041
|
-
};
|
|
65042
|
-
}
|
|
65043
65176
|
|
|
65044
65177
|
// ../envoy-client/src/nats-connection.ts
|
|
65045
65178
|
var import_nats2 = __toESM(require_mod4(), 1);
|
|
@@ -65687,6 +65820,10 @@ class ListenerRelay {
|
|
|
65687
65820
|
#open() {
|
|
65688
65821
|
if (this.#closedForGood)
|
|
65689
65822
|
return;
|
|
65823
|
+
if (this.#options.stopping?.() === true) {
|
|
65824
|
+
this.close();
|
|
65825
|
+
return;
|
|
65826
|
+
}
|
|
65690
65827
|
clearTimeout(this.#reopenTimer);
|
|
65691
65828
|
this.#reopenTimer = undefined;
|
|
65692
65829
|
this.#generation += 1;
|
|
@@ -65872,8 +66009,11 @@ class ListenerRelay {
|
|
|
65872
66009
|
const subscription = "sid" in frame ? this.#subs.get(Number(frame.sid)) : undefined;
|
|
65873
66010
|
if (subscription !== undefined && SUBSCRIPTION_REFUSALS[code] === true) {
|
|
65874
66011
|
this.#subs.delete(subscription.sid);
|
|
65875
|
-
|
|
66012
|
+
const refused = new LoggableError(`the listener's relay socket refused the subject ${subscription.subject} (${code})`);
|
|
66013
|
+
subscription.fail(refused);
|
|
65876
66014
|
this.#logOnce(`${session}\x00${code}\x00${subscription.subject}`, `the listener's relay socket for ${session} refused the subject ${subscription.subject} (${code}); that subscription is closed`);
|
|
66015
|
+
if (subscription.subject === deliverSubject(session))
|
|
66016
|
+
this.#outage.refused(refused);
|
|
65877
66017
|
return;
|
|
65878
66018
|
}
|
|
65879
66019
|
const known = ERR_CODES[code] === true;
|
|
@@ -66016,7 +66156,7 @@ async function dialChannelNats(inbound, name, warn, dial = import_nats4.connect,
|
|
|
66016
66156
|
signal?.removeEventListener("abort", onAbort);
|
|
66017
66157
|
}
|
|
66018
66158
|
}
|
|
66019
|
-
async function dialChannelInbound(inbound, listenerUrl, session, name, warn, dial = import_nats4.connect, signal) {
|
|
66159
|
+
async function dialChannelInbound(inbound, listenerUrl, session, name, warn, dial = import_nats4.connect, signal, stopping) {
|
|
66020
66160
|
if ("natsUrls" in inbound) {
|
|
66021
66161
|
return { ...await dialChannelNats(inbound, name, warn, dial, signal), liveView: true };
|
|
66022
66162
|
}
|
|
@@ -66024,10 +66164,9 @@ async function dialChannelInbound(inbound, listenerUrl, session, name, warn, dia
|
|
|
66024
66164
|
url: listenerUrl,
|
|
66025
66165
|
session,
|
|
66026
66166
|
credential: inbound.relayCredential,
|
|
66027
|
-
outage: inboundUnavailable((why) => warn(`this session's relay socket has no token: ${why}; the session starts once a trade succeeds`),
|
|
66028
|
-
|
|
66029
|
-
}
|
|
66030
|
-
log: (line) => warn(line)
|
|
66167
|
+
outage: inboundUnavailable(({ why }) => warn(`this session's relay socket has no token: ${why}; the session starts once a trade succeeds`)),
|
|
66168
|
+
log: (line) => warn(line),
|
|
66169
|
+
...stopping === undefined ? {} : { stopping }
|
|
66031
66170
|
}, signal);
|
|
66032
66171
|
return {
|
|
66033
66172
|
connection: relay,
|
|
@@ -66052,11 +66191,10 @@ function channelReopener(options) {
|
|
|
66052
66191
|
return;
|
|
66053
66192
|
});
|
|
66054
66193
|
const next = await dialChannelInbound(options.inbound, options.listenerUrl, options.session, options.name, (text, dialError) => {
|
|
66055
|
-
|
|
66056
|
-
`);
|
|
66194
|
+
options.warn(text);
|
|
66057
66195
|
if (dialError !== undefined)
|
|
66058
66196
|
failed(dialError);
|
|
66059
|
-
}, import_nats4.connect, options.signal);
|
|
66197
|
+
}, import_nats4.connect, options.signal, options.stopping);
|
|
66060
66198
|
if (options.signal.aborted) {
|
|
66061
66199
|
next.stop();
|
|
66062
66200
|
next.connection.close().catch(() => {
|
|
@@ -66120,6 +66258,13 @@ function projectRefusedTopics(error, topics, ownAgentSubject) {
|
|
|
66120
66258
|
|
|
66121
66259
|
class NoRelaySubjectError extends Error {
|
|
66122
66260
|
}
|
|
66261
|
+
|
|
66262
|
+
class RegistrationStoppingError extends Error {
|
|
66263
|
+
name = "RegistrationStoppingError";
|
|
66264
|
+
constructor() {
|
|
66265
|
+
super("this session is stopping, so it registers nothing");
|
|
66266
|
+
}
|
|
66267
|
+
}
|
|
66123
66268
|
async function registerWithRelay({
|
|
66124
66269
|
relay,
|
|
66125
66270
|
relayMissing,
|
|
@@ -66130,11 +66275,14 @@ async function registerWithRelay({
|
|
|
66130
66275
|
send,
|
|
66131
66276
|
answered,
|
|
66132
66277
|
drop,
|
|
66133
|
-
log
|
|
66278
|
+
log,
|
|
66279
|
+
stopping = () => false
|
|
66134
66280
|
}) {
|
|
66281
|
+
if (stopping())
|
|
66282
|
+
throw new RegistrationStoppingError;
|
|
66135
66283
|
if (relay === undefined) {
|
|
66136
66284
|
const refused = new NoRelaySubjectError(`this session holds no relay subject, so the listener can give it no consumer and it can receive nothing${relayMissing === undefined ? "" : `, because its relay subject could not be opened: ${relayMissing}`}`);
|
|
66137
|
-
outage.
|
|
66285
|
+
outage.noRelay(refused, relayMissing);
|
|
66138
66286
|
throw refused;
|
|
66139
66287
|
}
|
|
66140
66288
|
let topics = registrationTopics(ownAgentSubject, followed(), roles);
|
|
@@ -66145,7 +66293,7 @@ async function registerWithRelay({
|
|
|
66145
66293
|
result = await send(topics, relay.registration());
|
|
66146
66294
|
} catch (error) {
|
|
66147
66295
|
const refused = projectRefusedTopics(error, topics, ownAgentSubject);
|
|
66148
|
-
if (refused === undefined) {
|
|
66296
|
+
if (refused === undefined || stopping()) {
|
|
66149
66297
|
outage.refused(error);
|
|
66150
66298
|
throw error;
|
|
66151
66299
|
}
|
|
@@ -66454,10 +66602,13 @@ function allowedAPIURL(value) {
|
|
|
66454
66602
|
}
|
|
66455
66603
|
|
|
66456
66604
|
// ../envoy-client/src/tool-contract.ts
|
|
66457
|
-
var SESSION_STATE_GUIDE = "A session is live, unresponsive or gone, by how long ago it last registered: live within 5 " + "minutes, unresponsive after that and within an hour, gone past the hour or with no record at " + `all. ${UNRESPONSIVE_RULE} A session named with no state is live. A send answered with state ` + "stopped means that session is not running: the message is stored for it and reaches it if " + `it runs again within ${DELIVERY_DUPLICATE_WINDOW_HOURS} hours, and nothing of its claims changes.`;
|
|
66605
|
+
var SESSION_STATE_GUIDE = "A session is live, unresponsive or gone, by how long ago it last registered: live within 5 " + "minutes, unresponsive after that and within an hour, gone past the hour or with no record at " + `all. ${UNRESPONSIVE_RULE} A session named with no state is live. A send answered with state ` + "stopped means that session is not running: the message is stored for it and reaches it if " + `it runs again within ${DELIVERY_DUPLICATE_WINDOW_HOURS} hours, and nothing of its claims changes. ` + "A send answered with state no_receiver means that session runs but takes no message into its " + "conversation now, as a Claude Code terminal with no mod does: the message is stored for it and " + `reaches it if it takes messages within ${DELIVERY_DUPLICATE_WINDOW_HOURS} hours.`;
|
|
66458
66606
|
var DELIVERY_CONTRACT = "Delivery is at-least-once, possibly out of order across topics; use id for dedupe and at for freshness.";
|
|
66459
66607
|
var PUBLISH_COUNTS = "The answer counts who gets it: recipients is every session other than you that the message " + `goes to, one that is stopped and gets it when it runs again within ${DELIVERY_DUPLICATE_WINDOW_HOURS} hours included, and ` + "recipients_connected is how many of those are running now. A listener that counts neither " + "sends neither, and a role topic names its holder instead.";
|
|
66460
|
-
var
|
|
66608
|
+
var storedDelivery = (stored, kinds) => `${stored} (${kinds.join(", ")}; a role message reaches its holder as a stored copy on the ` + "holder's agent topic), and reaches each subscribed session once from the durable consumer its " + "listener relays to it, also one that was away or restarted. A 200 means the stream stored " + "the message for each subscribed session's consumer, not that any recipient has read it.";
|
|
66609
|
+
var STORED_DELIVERY = storedDelivery("Every notification topic is stored", STORED_TOPIC_KINDS);
|
|
66610
|
+
var PUBLISH_REFUSED_KINDS = ["agent", "dispatch", "github"];
|
|
66611
|
+
var PUBLISHED_DELIVERY = "The listener refuses a publish to each of these topics by name, and stores nothing: an agent " + "topic, since envoy_send sends to one session; a topic under notifications.dispatch., since " + "only Dispatch publishes on Dispatch's lane; a topic under notifications.github., since only " + "the listener that receives GitHub's webhook publishes on GitHub's lane; " + "notifications.legion.wake, since only the listener reports a Legion pod wake; and a topic " + "under notifications.envoy.kept., notifications.envoy.resume. or notifications.envoy.takeover., " + `since those lanes are the listener's own. ${storedDelivery("Every other topic the stream captures is stored", STORED_TOPIC_KINDS.filter((kind) => !PUBLISH_REFUSED_KINDS.includes(kind)))}`;
|
|
66461
66612
|
var NOT_SENT_GUIDE = "An error that says the message was not sent means nothing was stored, so the same message may " + "be sent again later.";
|
|
66462
66613
|
var TOPIC_GUIDE = "Topic guide (all are under notifications.): agent.<session_id> (subscribe: own inbox); role.<role> " + "(publish-to; holders claim via envoy_role_set). > matches one or more trailing tokens and does not " + "match the base subject. Envoy registers the concrete base when you subscribe to <subject>.>, so " + "the recommended default remains github.<owner>.<repo>.pr.<n>.>. <owner> and <repo> are one token " + "each, with every dot in the name written _ (sjawhar/.github is github.sjawhar._github; " + "acme/site.io is github.acme.site_io), and a topic spelled with the dot receives nothing. " + "Default PR subscription: " + "github.<owner>.<repo>.pr.<n>.> (it receives the quiet PR family): pr.<n> (lifecycle: " + "opened/synchronize/closed; closed carries merged, merge_commit_sha, merged_by, head_sha), " + "pr.<n>.comment, pr.<n>.review, pr.<n>.mention, pr.<n>.checks (one settlement event per commit whose checks settle, the head or not, naming its sha: " + "passed/failed/cancelled/skipped check runs with failing names and URLs, check_runs, each check's latest run as " + "{name, id, conclusion} with conclusion GitHub's own word for that run where one is recorded (for example " + "success, skipped, neutral or timed_out; a skipped check ran nothing on that commit), and statuses, each commit status as " + "{context, state, created_at} with state success, failure or error and created_at the time GitHub created it, " + "left out when none is recorded; for each context keep the entry with the later created_at, and keep a context " + 'that a settlement leaves out as it was; re-fires with superseded_settlement: "true" ' + "when new runs appear, or a commit status is new or changes state, for the same commit). Other GitHub: issue.<n>, issue.<n>.comment, " + "issue.<n>.mention, mention, push.branch.<name>, push.tag.<name>, workflow.<file>.<action> (only " + "runs without an associated PR); slack.<team>.<channel>.message|mention and " + "slack.<team>.<channel>.thread.<ts>.message|mention; ghostwispr.<session>.<kind>; " + "whatsapp.<phone>.<jid>.<kind>; envoy.exceptions.<original-topic>.";
|
|
66463
66614
|
var URGENCY_VALUES = EnvelopeSchema.shape.urgency.unwrap().options;
|
|
@@ -66541,7 +66692,7 @@ var envoyToolSpecs = [
|
|
|
66541
66692
|
},
|
|
66542
66693
|
{
|
|
66543
66694
|
name: "envoy_publish",
|
|
66544
|
-
description: `Publish an Envoy message to a notifications. topic. ${
|
|
66695
|
+
description: `Publish an Envoy message to a notifications. topic. ${PUBLISHED_DELIVERY} A role or built-in topic is delivered as its guide says. A role message has three possible answers. First, a live session holds the role, and the stream stores the forward for that holder's consumer. Its 200 says the stream stored it. It does not say that the holder read it. It names the holder and the state the listener resolved the holder in. Second, no live session holds the role: nobody claims it, or its holder is unresponsive or gone. The listener then keeps the message for one hour. This is a success. The result says that the listener kept the message, and its kept_until is the time that hour ends. For a holder that is not live, the result also names that holder and its state. The next session to hold the role live before kept_until gets the message. That can be the same holder when it answers again. If no session takes the message by kept_until, Envoy drops it. If the publish named your session as its source, Envoy then sends your session a message that names the dropped message, unless it dropped it more than an hour late. Third, the listener keeps nothing and errs, which tells you to reach the agent some other way: the holder refuses the message, the forward is not stored within four seconds, or no live session holds the role and the publish names no session of yours as its source, so nothing could tell you that a kept copy expired. A team topic (such as notifications.team.deploy-squad.all) is stored like every other, so a session subscribed to it that was away still receives it when it returns, and one that subscribes after you publish does not. A notifications. topic no stream subject captures is refused by name: nothing would store it. ${NOT_SENT_GUIDE} ${PUBLISH_COUNTS} ${SESSION_STATE_GUIDE} ${DELIVERY_CONTRACT}`,
|
|
66545
66696
|
arguments: (schema) => ({ topic: schema.string(), ...messageArguments(schema) }),
|
|
66546
66697
|
operation: EnvoyToolOperation.publish
|
|
66547
66698
|
},
|
|
@@ -66589,9 +66740,9 @@ function lastSeenText(lastSeen) {
|
|
|
66589
66740
|
}
|
|
66590
66741
|
var STORED_ONCE = "an earlier attempt under the same idempotency_key was already stored, so this one stored no " + "second copy";
|
|
66591
66742
|
function sendConfirmationText(result) {
|
|
66592
|
-
if (result.state === "stopped") {
|
|
66743
|
+
if (result.state === "stopped" || result.state === "no_receiver") {
|
|
66593
66744
|
const seen = result.lastSeen === undefined ? "" : ` (last seen ${lastSeenText(result.lastSeen)})`;
|
|
66594
|
-
const waits = `the session is stopped${seen}; if it runs again within ${DELIVERY_DUPLICATE_WINDOW_HOURS} hours, it gets the message`;
|
|
66745
|
+
const waits = result.state === "stopped" ? `the session is stopped${seen}; if it runs again within ${DELIVERY_DUPLICATE_WINDOW_HOURS} hours, it gets the message` : `the session runs but takes no message into its conversation now; the message is stored, and if the session takes messages within ${DELIVERY_DUPLICATE_WINDOW_HOURS} hours, it gets the message`;
|
|
66595
66746
|
if (result.duplicate) {
|
|
66596
66747
|
return `queued once for ${result.recipient}: ${STORED_ONCE}; ${waits}`;
|
|
66597
66748
|
}
|
|
@@ -66870,12 +67021,20 @@ async function findHandoffReport(input) {
|
|
|
66870
67021
|
return { commit: head, ...report };
|
|
66871
67022
|
}
|
|
66872
67023
|
|
|
67024
|
+
// ../pi-shared/src/role-claim-stopping.ts
|
|
67025
|
+
class RoleClaimStoppingError extends Error {
|
|
67026
|
+
name = "RoleClaimStoppingError";
|
|
67027
|
+
constructor(role) {
|
|
67028
|
+
super(`this session is stopping, so it claims no role: role ${role} is not held here`);
|
|
67029
|
+
}
|
|
67030
|
+
}
|
|
67031
|
+
|
|
66873
67032
|
// src/envoy-channel-server.ts
|
|
66874
67033
|
var import_nats5 = __toESM(require_mod4(), 1);
|
|
66875
67034
|
|
|
66876
67035
|
// package.json
|
|
66877
67036
|
var name2 = "@sjawhar/claude-envoy";
|
|
66878
|
-
var version2 = "6.14.
|
|
67037
|
+
var version2 = "6.14.1";
|
|
66879
67038
|
var package_default = {
|
|
66880
67039
|
name: name2,
|
|
66881
67040
|
version: version2,
|
|
@@ -67862,12 +68021,11 @@ function controlSubscription(options) {
|
|
|
67862
68021
|
|
|
67863
68022
|
// src/agent-stream-runner.ts
|
|
67864
68023
|
var AGENT_STREAM_CALL_TIMEOUT_MS = 5000;
|
|
68024
|
+
var LIVE_VIEW_READ_FAILED = "the live view could not read this session's ";
|
|
67865
68025
|
var AGENT_STREAM_SEEN_ROWS_MAX = 512;
|
|
67866
68026
|
function startAgentStream(options) {
|
|
67867
|
-
const { link } = options;
|
|
68027
|
+
const { link, warn } = options;
|
|
67868
68028
|
const now = options.now ?? Date.now;
|
|
67869
|
-
const warn = options.warn ?? ((text) => void process.stderr.write(`envoy-channel: ${text}
|
|
67870
|
-
`));
|
|
67871
68029
|
let session = options.sessionId;
|
|
67872
68030
|
const control = controlSubscription({
|
|
67873
68031
|
connection: options.connection,
|
|
@@ -67926,7 +68084,7 @@ function startAgentStream(options) {
|
|
|
67926
68084
|
if (!control.stopped)
|
|
67927
68085
|
publishSettings();
|
|
67928
68086
|
}).catch((error) => {
|
|
67929
|
-
warn(
|
|
68087
|
+
warn(`${LIVE_VIEW_READ_FAILED}model \u2014 ${messageFor(error)}`);
|
|
67930
68088
|
}).finally(() => {
|
|
67931
68089
|
readingSettings = false;
|
|
67932
68090
|
});
|
|
@@ -67940,7 +68098,7 @@ function startAgentStream(options) {
|
|
|
67940
68098
|
if (!control.stopped)
|
|
67941
68099
|
publishCommands();
|
|
67942
68100
|
}).catch((error) => {
|
|
67943
|
-
warn(
|
|
68101
|
+
warn(`${LIVE_VIEW_READ_FAILED}commands \u2014 ${messageFor(error)}`);
|
|
67944
68102
|
}).finally(() => {
|
|
67945
68103
|
readingCommands = false;
|
|
67946
68104
|
});
|
|
@@ -68250,6 +68408,8 @@ async function pruneStaleSessionRoles(stateDirectory, currentSessionId) {
|
|
|
68250
68408
|
// src/envoy-off.ts
|
|
68251
68409
|
function inboundOffChannelSession(options) {
|
|
68252
68410
|
const { identity, stateDirectory, why } = options;
|
|
68411
|
+
let shutDown = false;
|
|
68412
|
+
const stopping = () => shutDown || options.stopping();
|
|
68253
68413
|
return {
|
|
68254
68414
|
delivery: { enqueue: async () => false, inbox: () => [] },
|
|
68255
68415
|
topics: () => [],
|
|
@@ -68270,13 +68430,16 @@ function inboundOffChannelSession(options) {
|
|
|
68270
68430
|
return;
|
|
68271
68431
|
},
|
|
68272
68432
|
async rememberRole(role) {
|
|
68433
|
+
if (stopping())
|
|
68434
|
+
return;
|
|
68273
68435
|
await writePersistedRole(roleStateFile(stateDirectory, identity.id), {
|
|
68274
68436
|
session_id: identity.id,
|
|
68275
68437
|
role
|
|
68276
68438
|
});
|
|
68277
68439
|
},
|
|
68440
|
+
stopping,
|
|
68278
68441
|
shutdown: async () => {
|
|
68279
|
-
|
|
68442
|
+
shutDown = true;
|
|
68280
68443
|
}
|
|
68281
68444
|
};
|
|
68282
68445
|
}
|
|
@@ -68301,6 +68464,46 @@ async function serveEnvoyOff(serverInfo, refusal) {
|
|
|
68301
68464
|
}
|
|
68302
68465
|
}
|
|
68303
68466
|
|
|
68467
|
+
// src/inbound-outage-lines.ts
|
|
68468
|
+
var RECEIVES_NOTHING = {
|
|
68469
|
+
line: (why) => `envoy-channel: this session receives nothing (${why}); registering again every heartbeat
|
|
68470
|
+
`,
|
|
68471
|
+
told: "that it receives nothing"
|
|
68472
|
+
};
|
|
68473
|
+
var INBOUND_UNAVAILABLE_LINES = {
|
|
68474
|
+
unrecorded: {
|
|
68475
|
+
line: (why) => `envoy-channel: this session's registration is not recorded for certain (${why}), and the session registers again at every heartbeat
|
|
68476
|
+
`,
|
|
68477
|
+
told: "that its registration is not recorded for certain"
|
|
68478
|
+
},
|
|
68479
|
+
github_withheld: {
|
|
68480
|
+
line: (why) => `envoy-channel: the listener follows none of this session's GitHub topics now (${why}); the session's other messages still reach it if the listener already held its registration, and none reach it until a registration is accepted if it held none; registering again every heartbeat
|
|
68481
|
+
`,
|
|
68482
|
+
told: "that its GitHub messages are withheld"
|
|
68483
|
+
},
|
|
68484
|
+
refused: RECEIVES_NOTHING,
|
|
68485
|
+
no_relay: RECEIVES_NOTHING
|
|
68486
|
+
};
|
|
68487
|
+
var RECEIVES_AGAIN = {
|
|
68488
|
+
line: `envoy-channel: this session receives again: a registration was accepted
|
|
68489
|
+
`,
|
|
68490
|
+
told: "that it receives again"
|
|
68491
|
+
};
|
|
68492
|
+
var INBOUND_RESTORED_LINES = {
|
|
68493
|
+
unrecorded: {
|
|
68494
|
+
line: `envoy-channel: the listener recorded a registration of this session
|
|
68495
|
+
`,
|
|
68496
|
+
told: "that the listener recorded its registration"
|
|
68497
|
+
},
|
|
68498
|
+
github_withheld: {
|
|
68499
|
+
line: `envoy-channel: the listener follows this session's GitHub topics again: a registration was accepted
|
|
68500
|
+
`,
|
|
68501
|
+
told: "that the listener follows its GitHub topics again"
|
|
68502
|
+
},
|
|
68503
|
+
refused: RECEIVES_AGAIN,
|
|
68504
|
+
no_relay: RECEIVES_AGAIN
|
|
68505
|
+
};
|
|
68506
|
+
|
|
68304
68507
|
// src/installed-build.ts
|
|
68305
68508
|
import { dirname as dirname5, join as join12, sep } from "path";
|
|
68306
68509
|
function pluginCacheSegments(bundlePath) {
|
|
@@ -69639,27 +69842,39 @@ function isNetworkOrTimeoutError(error) {
|
|
|
69639
69842
|
const code = "code" in error && typeof error.code === "string" ? error.code : undefined;
|
|
69640
69843
|
return code === "ConnectionRefused" || code === "ConnectionTimeout" || code === "EAI_AGAIN" || code === "ECONNREFUSED" || code === "ECONNRESET" || code === "EHOSTUNREACH" || code === "ENETUNREACH" || code === "ENOTFOUND" || code === "ETIMEDOUT" || error instanceof TypeError && (error.message === "Failed to fetch" || error.message === "fetch failed");
|
|
69641
69844
|
}
|
|
69642
|
-
async function callReadyWithRetry(label, call, sleep = (ms) => {
|
|
69845
|
+
async function callReadyWithRetry(label, call, host, sleep = (ms, signal) => {
|
|
69643
69846
|
const waited = Promise.withResolvers();
|
|
69644
|
-
setTimeout(waited.resolve, ms);
|
|
69645
|
-
|
|
69847
|
+
const timer = setTimeout(waited.resolve, ms);
|
|
69848
|
+
const stop = () => {
|
|
69849
|
+
clearTimeout(timer);
|
|
69850
|
+
waited.resolve();
|
|
69851
|
+
};
|
|
69852
|
+
if (signal?.aborted === true)
|
|
69853
|
+
stop();
|
|
69854
|
+
else
|
|
69855
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
69856
|
+
return waited.promise.finally(() => signal?.removeEventListener("abort", stop));
|
|
69646
69857
|
}) {
|
|
69858
|
+
const { log, signal } = host;
|
|
69859
|
+
const stopped = () => signal?.aborted === true;
|
|
69647
69860
|
for (let attempt = 1;; attempt++) {
|
|
69648
69861
|
try {
|
|
69649
69862
|
await call();
|
|
69650
69863
|
return;
|
|
69651
69864
|
} catch (error) {
|
|
69652
69865
|
const retryable = error instanceof LegionDaemonApiError ? outage(error.status) : isNetworkOrTimeoutError(error);
|
|
69653
|
-
if (!retryable)
|
|
69866
|
+
if (!retryable || stopped())
|
|
69654
69867
|
throw error;
|
|
69655
69868
|
if (attempt === READY_RETRY_ATTEMPTS) {
|
|
69656
|
-
|
|
69869
|
+
log(`${label} failed after ${attempt} attempts: ${messageFor(error)}`);
|
|
69657
69870
|
throw error;
|
|
69658
69871
|
}
|
|
69659
69872
|
const asked = error instanceof LegionDaemonApiError ? error.retryAfterMs : 0;
|
|
69660
69873
|
const wait = Math.min(Math.max(READY_RETRY_DELAY_MS, asked), READY_RETRY_AFTER_MAX_MS);
|
|
69661
|
-
|
|
69662
|
-
await sleep(wait);
|
|
69874
|
+
log(`${label} failed (attempt ${attempt}/${READY_RETRY_ATTEMPTS}), retrying in ${wait / 1000} s: ${messageFor(error)}`);
|
|
69875
|
+
await sleep(wait, signal);
|
|
69876
|
+
if (stopped())
|
|
69877
|
+
throw error;
|
|
69663
69878
|
}
|
|
69664
69879
|
}
|
|
69665
69880
|
}
|
|
@@ -69853,7 +70068,9 @@ async function bootLegionClaim(host) {
|
|
|
69853
70068
|
} catch (error) {
|
|
69854
70069
|
if (!(error instanceof LegionDaemonApiError))
|
|
69855
70070
|
throw error;
|
|
69856
|
-
host.
|
|
70071
|
+
if (host.signal?.aborted !== true) {
|
|
70072
|
+
host.log(`claims/register registration failed (${error.status}): ${error.detail}`);
|
|
70073
|
+
}
|
|
69857
70074
|
if (error.status < 400 || outage(error.status))
|
|
69858
70075
|
throw error;
|
|
69859
70076
|
host.stop();
|
|
@@ -69878,19 +70095,27 @@ async function bootLegionClaim(host) {
|
|
|
69878
70095
|
secret: claim.secret,
|
|
69879
70096
|
generation: claim.generation
|
|
69880
70097
|
};
|
|
69881
|
-
await callReadyWithRetry("claims/ready", () => daemon.ready(ready));
|
|
70098
|
+
await callReadyWithRetry("claims/ready", () => daemon.ready(ready), host);
|
|
69882
70099
|
host.onRoleRegained(async (role, reason) => {
|
|
69883
70100
|
if (role !== claim.claimToken)
|
|
69884
70101
|
return;
|
|
70102
|
+
const stopped = () => host.signal?.aborted === true;
|
|
70103
|
+
if (stopped())
|
|
70104
|
+
return;
|
|
69885
70105
|
try {
|
|
69886
|
-
await callReadyWithRetry("claims/ready after role regain", () => daemon.ready(ready));
|
|
70106
|
+
await callReadyWithRetry("claims/ready after role regain", () => daemon.ready(ready), host);
|
|
70107
|
+
if (stopped())
|
|
70108
|
+
return;
|
|
69887
70109
|
host.log(`re-ran claims/ready after role ${role} was ${reason}`);
|
|
69888
70110
|
} catch (error) {
|
|
70111
|
+
if (stopped())
|
|
70112
|
+
return;
|
|
69889
70113
|
host.log(`claims/ready after role ${role} was ${reason} failed; the daemon's queued task stays undelivered until the next regain or boot: ${messageFor(error)}`);
|
|
69890
70114
|
}
|
|
69891
70115
|
});
|
|
69892
70116
|
} catch (error) {
|
|
69893
|
-
|
|
70117
|
+
if (host.signal?.aborted !== true)
|
|
70118
|
+
stopAfterRegistration(host, claim.claimToken, error);
|
|
69894
70119
|
return { claimed: Promise.resolve() };
|
|
69895
70120
|
}
|
|
69896
70121
|
return { claimed: claimRoleAtFirstTurn(host, claim.claimToken, claim.replaces, firstTurn) };
|
|
@@ -69915,11 +70140,19 @@ function listenForFirstTurn(host) {
|
|
|
69915
70140
|
function claimRoleAtFirstTurn(host, role, replaces, firstTurn) {
|
|
69916
70141
|
const done = Promise.withResolvers();
|
|
69917
70142
|
let ran = false;
|
|
70143
|
+
const stop = () => {
|
|
70144
|
+
if (ran)
|
|
70145
|
+
return;
|
|
70146
|
+
ran = true;
|
|
70147
|
+
clearTimeout(bound);
|
|
70148
|
+
done.resolve();
|
|
70149
|
+
};
|
|
69918
70150
|
const claim = () => {
|
|
69919
70151
|
if (ran)
|
|
69920
70152
|
return;
|
|
69921
70153
|
ran = true;
|
|
69922
70154
|
clearTimeout(bound);
|
|
70155
|
+
host.signal?.removeEventListener("abort", stop);
|
|
69923
70156
|
(async () => {
|
|
69924
70157
|
try {
|
|
69925
70158
|
await claimRoleAgainOnBusy(() => host.claimRole(role, replaces), role, {
|
|
@@ -69936,6 +70169,10 @@ function claimRoleAtFirstTurn(host, role, replaces, firstTurn) {
|
|
|
69936
70169
|
};
|
|
69937
70170
|
const bound = setTimeout(claim, host.firstTurnBoundMs);
|
|
69938
70171
|
bound.unref?.();
|
|
70172
|
+
if (host.signal?.aborted === true)
|
|
70173
|
+
stop();
|
|
70174
|
+
else
|
|
70175
|
+
host.signal?.addEventListener("abort", stop, { once: true });
|
|
69939
70176
|
firstTurn(claim);
|
|
69940
70177
|
return done.promise;
|
|
69941
70178
|
}
|
|
@@ -70709,8 +70946,10 @@ function logLegion(line) {
|
|
|
70709
70946
|
function endPane() {
|
|
70710
70947
|
process.kill(process.ppid, "SIGTERM");
|
|
70711
70948
|
}
|
|
70712
|
-
function stopPane(why) {
|
|
70713
|
-
|
|
70949
|
+
function stopPane(why, server) {
|
|
70950
|
+
if (server.stopping())
|
|
70951
|
+
return;
|
|
70952
|
+
server.log(`${why}; stopping the pane so the daemon launches it again`);
|
|
70714
70953
|
endPane();
|
|
70715
70954
|
}
|
|
70716
70955
|
async function waitForTranscript(configDir, sessionID, signal, pollMs = 100) {
|
|
@@ -72159,6 +72398,14 @@ async function sessionTitle(environment, directory, sources = {}) {
|
|
|
72159
72398
|
};
|
|
72160
72399
|
}
|
|
72161
72400
|
|
|
72401
|
+
// src/stop-gates.ts
|
|
72402
|
+
function gatedStderr(stopping) {
|
|
72403
|
+
return (line) => {
|
|
72404
|
+
if (!stopping())
|
|
72405
|
+
process.stderr.write(line);
|
|
72406
|
+
};
|
|
72407
|
+
}
|
|
72408
|
+
|
|
72162
72409
|
// ../pi-shared/src/dispatch-user-turn.ts
|
|
72163
72410
|
function handledAttemptKey(messageId, attempt) {
|
|
72164
72411
|
return `${messageId}#${attempt}`;
|
|
@@ -72638,6 +72885,7 @@ async function postDispatchReply(identity, configFile, delivery, result) {
|
|
|
72638
72885
|
throw new Error(config.error ?? config.notice);
|
|
72639
72886
|
await postDeliveryReply(config, identity.id, delivery, result);
|
|
72640
72887
|
}
|
|
72888
|
+
var DELIVERY_FAILED_LINE = "envoy-channel: a delivery failed \u2014 ";
|
|
72641
72889
|
async function userTurnDispatchClient(identity, configFile, ms) {
|
|
72642
72890
|
const active = activeDispatchSettings((await sessionDispatchSettings(configFile, process.env, {
|
|
72643
72891
|
cwd: identity.directory,
|
|
@@ -72653,7 +72901,7 @@ function createChannelDelivery(input) {
|
|
|
72653
72901
|
tail = run.then(() => {
|
|
72654
72902
|
return;
|
|
72655
72903
|
}, (error) => {
|
|
72656
|
-
|
|
72904
|
+
input.stderr(`${DELIVERY_FAILED_LINE}${messageFor(error)}
|
|
72657
72905
|
`);
|
|
72658
72906
|
});
|
|
72659
72907
|
return run;
|
|
@@ -72666,7 +72914,7 @@ function createChannelDelivery(input) {
|
|
|
72666
72914
|
if (eventId !== undefined && inbox.some((entry) => entry.event_id === eventId))
|
|
72667
72915
|
return;
|
|
72668
72916
|
if (rendered.payloadShownAsText !== undefined) {
|
|
72669
|
-
|
|
72917
|
+
input.stderr(`envoy-channel: message ${eventId ?? "unknown"}'s payload is shown as its text; the card could not lay it out (${rendered.payloadShownAsText})
|
|
72670
72918
|
`);
|
|
72671
72919
|
}
|
|
72672
72920
|
inbox.unshift({
|
|
@@ -72685,7 +72933,7 @@ function createChannelDelivery(input) {
|
|
|
72685
72933
|
const passOverLateBtw = (rendered) => {
|
|
72686
72934
|
if (rendered.delivery?.mode !== "btw")
|
|
72687
72935
|
return false;
|
|
72688
|
-
|
|
72936
|
+
input.stderr(`envoy-channel: passing over late BTW ${rendered.envelope?.event_id ?? "unknown"}: it was asked while this session was not taking messages
|
|
72689
72937
|
`);
|
|
72690
72938
|
return true;
|
|
72691
72939
|
};
|
|
@@ -72696,19 +72944,19 @@ function createChannelDelivery(input) {
|
|
|
72696
72944
|
return tail.then(() => false);
|
|
72697
72945
|
if (rendered.rejectedDelivery !== undefined) {
|
|
72698
72946
|
const rejected = rendered.rejectedDelivery;
|
|
72699
|
-
|
|
72947
|
+
input.stderr(`envoy-channel: rejecting malformed Dispatch targeted delivery ${rejected.id}
|
|
72700
72948
|
`);
|
|
72701
72949
|
return postDispatchReply(input.identity, input.configFile, rejected, {
|
|
72702
72950
|
error: "Invalid Dispatch targeted delivery frame"
|
|
72703
72951
|
}).then(() => false, (error) => {
|
|
72704
|
-
|
|
72952
|
+
input.stderr(error instanceof DispatchWriteLandedError ? `envoy-channel: the report of the rejected delivery landed on Dispatch, which gave no copy of its answer \u2014 ${error.message}
|
|
72705
72953
|
` : `envoy-channel: could not report the rejected delivery to Dispatch \u2014 ${messageFor(error)}
|
|
72706
72954
|
`);
|
|
72707
72955
|
return false;
|
|
72708
72956
|
});
|
|
72709
72957
|
}
|
|
72710
72958
|
if (rendered.malformedDelivery === true) {
|
|
72711
|
-
|
|
72959
|
+
input.stderr(`envoy-channel: dropping malformed Dispatch targeted delivery without a reply address
|
|
72712
72960
|
`);
|
|
72713
72961
|
return tail.then(() => false);
|
|
72714
72962
|
}
|
|
@@ -72727,6 +72975,8 @@ function createChannelDelivery(input) {
|
|
|
72727
72975
|
},
|
|
72728
72976
|
notice(content, addressed) {
|
|
72729
72977
|
return queued(async () => {
|
|
72978
|
+
if (input.stopping?.() === true)
|
|
72979
|
+
return;
|
|
72730
72980
|
if (addressed !== undefined && addressed !== input.identity.id) {
|
|
72731
72981
|
throw new Error(`the notice names session ${addressed}, and this process serves ${input.identity.id}`);
|
|
72732
72982
|
}
|
|
@@ -72814,15 +73064,23 @@ async function startChannelSession(options) {
|
|
|
72814
73064
|
const watchedConnections = new WeakSet;
|
|
72815
73065
|
let router;
|
|
72816
73066
|
let turns;
|
|
73067
|
+
const userTopics = new Set;
|
|
73068
|
+
let heldRole;
|
|
73069
|
+
let shuttingDown = options.startSignal?.aborted === true;
|
|
73070
|
+
options.startSignal?.addEventListener("abort", () => {
|
|
73071
|
+
shuttingDown = true;
|
|
73072
|
+
}, { once: true });
|
|
73073
|
+
let shutdownStarted = false;
|
|
73074
|
+
const sessionStderr = gatedStderr(() => shuttingDown);
|
|
72817
73075
|
const delivery = createChannelDelivery({
|
|
72818
73076
|
identity,
|
|
72819
73077
|
notifier: options.notifier,
|
|
72820
73078
|
configFile: options.configFile,
|
|
72821
|
-
router: () => router
|
|
73079
|
+
router: () => router,
|
|
73080
|
+
stderr: sessionStderr,
|
|
73081
|
+
stopping: () => shuttingDown
|
|
72822
73082
|
});
|
|
72823
|
-
const
|
|
72824
|
-
let heldRole;
|
|
72825
|
-
let shuttingDown = false;
|
|
73083
|
+
const showNotice = (content, addressed) => shuttingDown ? Promise.resolve() : delivery.notice(content, addressed);
|
|
72826
73084
|
const handoffFile = options.handoffPid === undefined ? undefined : sessionHandoffFile(options.stateDirectory, options.handoffPid);
|
|
72827
73085
|
let registryWrites = Promise.resolve();
|
|
72828
73086
|
const serialized = (write) => {
|
|
@@ -72857,8 +73115,8 @@ async function startChannelSession(options) {
|
|
|
72857
73115
|
const dedupe = createDeliveryDedupe();
|
|
72858
73116
|
let ledgerBound;
|
|
72859
73117
|
const tellGapNotice = (notice) => {
|
|
72860
|
-
|
|
72861
|
-
|
|
73118
|
+
showNotice(gapNoticeText(notice)).catch((error) => {
|
|
73119
|
+
sessionStderr(`envoy-channel: ${notice.message} (the session could not be told \u2014 ${messageFor(error)})
|
|
72862
73120
|
`);
|
|
72863
73121
|
});
|
|
72864
73122
|
};
|
|
@@ -72868,13 +73126,13 @@ async function startChannelSession(options) {
|
|
|
72868
73126
|
host: {
|
|
72869
73127
|
sessionID: () => shuttingDown || ledgerBound !== identity.id ? undefined : identity.id,
|
|
72870
73128
|
followedSubjects: () => replayFollowedSubjects(directSubject, userTopics),
|
|
72871
|
-
showFailureNotice: (addressed, notice) =>
|
|
73129
|
+
showFailureNotice: (addressed, notice) => showNotice(envoyNoticeText(failureNoticeFields(notice)), addressed),
|
|
72872
73130
|
failureNoticeFailed: (notice, error) => {
|
|
72873
|
-
|
|
73131
|
+
sessionStderr(`envoy-channel: message ${notice.eventId} from ${notice.sender} was not shown, nor was the notice naming it \u2014 ${messageFor(error)}
|
|
72874
73132
|
`);
|
|
72875
73133
|
},
|
|
72876
73134
|
log: (message, error) => {
|
|
72877
|
-
|
|
73135
|
+
sessionStderr(`envoy-channel: ${message}${error === undefined ? "" : ` \u2014 ${messageFor(error)}`}
|
|
72878
73136
|
`);
|
|
72879
73137
|
},
|
|
72880
73138
|
write: (change) => stateWriter?.ledger(change.snapshot())
|
|
@@ -72884,7 +73142,7 @@ async function startChannelSession(options) {
|
|
|
72884
73142
|
const sessionId = identity.id;
|
|
72885
73143
|
const file = replayStateFile(options.stateDirectory, sessionId);
|
|
72886
73144
|
const snapshot = await readReplayState(file).catch((error) => {
|
|
72887
|
-
|
|
73145
|
+
sessionStderr(`envoy-channel: could not read the delivery state (${messageFor(error)}); this session's record starts over
|
|
72888
73146
|
`);
|
|
72889
73147
|
return { ledger: [] };
|
|
72890
73148
|
});
|
|
@@ -72901,7 +73159,7 @@ async function startChannelSession(options) {
|
|
|
72901
73159
|
return;
|
|
72902
73160
|
const file = userTurnStateFile(options.stateDirectory, identity.id);
|
|
72903
73161
|
const restored = await readUserTurnState(file).catch((error) => {
|
|
72904
|
-
|
|
73162
|
+
sessionStderr(`envoy-channel: could not read this session's record of the messages it took (${messageFor(error)}); its messages arrive as cards
|
|
72905
73163
|
`);
|
|
72906
73164
|
return;
|
|
72907
73165
|
});
|
|
@@ -72961,7 +73219,7 @@ async function startChannelSession(options) {
|
|
|
72961
73219
|
deliver: (frame) => deliverRelayed(owner, frame),
|
|
72962
73220
|
gap: tellGapNotice,
|
|
72963
73221
|
log: (message, error) => {
|
|
72964
|
-
|
|
73222
|
+
sessionStderr(`envoy-channel: ${message}${error === undefined ? "" : ` \u2014 ${messageFor(error)}`}
|
|
72965
73223
|
`);
|
|
72966
73224
|
}
|
|
72967
73225
|
},
|
|
@@ -72970,7 +73228,7 @@ async function startChannelSession(options) {
|
|
|
72970
73228
|
if (relay !== owner)
|
|
72971
73229
|
return false;
|
|
72972
73230
|
state.consumer(snapshot).catch((error) => {
|
|
72973
|
-
|
|
73231
|
+
sessionStderr(`envoy-channel: could not persist the consumer delivery state \u2014 ${messageFor(error)}
|
|
72974
73232
|
`);
|
|
72975
73233
|
});
|
|
72976
73234
|
return true;
|
|
@@ -73004,7 +73262,7 @@ async function startChannelSession(options) {
|
|
|
73004
73262
|
owner.delivery.receive(message);
|
|
73005
73263
|
}
|
|
73006
73264
|
} catch (error) {
|
|
73007
|
-
|
|
73265
|
+
sessionStderr(`envoy-channel: the relay subscription of ${owner.sessionID} stopped \u2014 ${natsFailureReason(error)}
|
|
73008
73266
|
`);
|
|
73009
73267
|
if (owner.subscription !== subscription)
|
|
73010
73268
|
return;
|
|
@@ -73057,7 +73315,9 @@ async function startChannelSession(options) {
|
|
|
73057
73315
|
return;
|
|
73058
73316
|
relay.delivery.beginEpoch();
|
|
73059
73317
|
serialized(register).catch((error) => {
|
|
73060
|
-
|
|
73318
|
+
if (shuttingDown)
|
|
73319
|
+
return;
|
|
73320
|
+
sessionStderr(`envoy-channel: could not register again after a reconnect (${messageFor(error)}); the next heartbeat retries
|
|
73061
73321
|
`);
|
|
73062
73322
|
});
|
|
73063
73323
|
};
|
|
@@ -73069,7 +73329,8 @@ async function startChannelSession(options) {
|
|
|
73069
73329
|
const followedTopics = createChannelTopics(() => connection);
|
|
73070
73330
|
function unfollowRemovedTopics(topics) {
|
|
73071
73331
|
dropFromTopicsAndRegistry(topics).catch((error) => {
|
|
73072
|
-
|
|
73332
|
+
sessionStderr(isBusUnanswered(error) ? `envoy-channel: the listener's message bus did not answer a request to drop a removed subscription from the registry \u2014 ${messageFor(error)}; the registry may already be without it, and the next heartbeat reconciles the registry and sets the consumer's filter to the topics it holds
|
|
73333
|
+
` : `envoy-channel: could not drop a removed subscription from the registry \u2014 ${messageFor(error)}; the next heartbeat reconciles it
|
|
73073
73334
|
`);
|
|
73074
73335
|
});
|
|
73075
73336
|
}
|
|
@@ -73084,24 +73345,23 @@ async function startChannelSession(options) {
|
|
|
73084
73345
|
clientInstanceID: options.clientInstanceID
|
|
73085
73346
|
}));
|
|
73086
73347
|
}
|
|
73087
|
-
const inboundOutage = inboundUnavailable((
|
|
73088
|
-
|
|
73089
|
-
|
|
73090
|
-
|
|
73091
|
-
`)
|
|
73092
|
-
delivery.notice(inboundUnavailableNoticeText(why)).catch((noticeError) => {
|
|
73093
|
-
process.stderr.write(`envoy-channel: the session could not be told that it receives nothing \u2014 ${messageFor(noticeError)}
|
|
73348
|
+
const inboundOutage = inboundUnavailable((outage) => {
|
|
73349
|
+
const { line, told } = INBOUND_UNAVAILABLE_LINES[outage.kind];
|
|
73350
|
+
sessionStderr(line(outage.why));
|
|
73351
|
+
showNotice(inboundUnavailableNoticeText(outage)).catch((noticeError) => {
|
|
73352
|
+
sessionStderr(`envoy-channel: the session could not be told ${told} \u2014 ${messageFor(noticeError)}
|
|
73094
73353
|
`);
|
|
73095
73354
|
});
|
|
73096
|
-
},
|
|
73097
|
-
|
|
73098
|
-
|
|
73099
|
-
|
|
73100
|
-
|
|
73101
|
-
|
|
73102
|
-
process.stderr.write(`envoy-channel: the session could not be told that it receives again \u2014 ${messageFor(noticeError)}
|
|
73355
|
+
}, {
|
|
73356
|
+
restored: ({ kind }) => {
|
|
73357
|
+
const { line, told } = INBOUND_RESTORED_LINES[kind];
|
|
73358
|
+
sessionStderr(line);
|
|
73359
|
+
showNotice(inboundRestoredNoticeText(kind)).catch((noticeError) => {
|
|
73360
|
+
sessionStderr(`envoy-channel: the session could not be told ${told} \u2014 ${messageFor(noticeError)}
|
|
73103
73361
|
`);
|
|
73104
|
-
|
|
73362
|
+
});
|
|
73363
|
+
},
|
|
73364
|
+
stopping: () => shuttingDown
|
|
73105
73365
|
});
|
|
73106
73366
|
options.reportOutagesTo?.(inboundOutage);
|
|
73107
73367
|
let stampedTitle;
|
|
@@ -73116,9 +73376,11 @@ async function startChannelSession(options) {
|
|
|
73116
73376
|
await restoreDelivery();
|
|
73117
73377
|
handoffDeliveryPending = false;
|
|
73118
73378
|
await withTimeout(connection.flush(), options.handoffFlushTimeoutMs ?? HANDOFF_FLUSH_TIMEOUT_MS, "flushing NATS").catch((error) => {
|
|
73119
|
-
|
|
73379
|
+
sessionStderr(`envoy-channel: could not confirm the relay subscription of ${identity.id} \u2014 ${natsFailureReason(error)}
|
|
73120
73380
|
`);
|
|
73121
73381
|
});
|
|
73382
|
+
if (shuttingDown)
|
|
73383
|
+
return;
|
|
73122
73384
|
}
|
|
73123
73385
|
const title = options.title?.(identity.id) ?? "";
|
|
73124
73386
|
if (options.stampTitle !== undefined && title !== "" && (stampedTitle?.sessionID !== identity.id || stampedTitle.title !== title)) {
|
|
@@ -73129,7 +73391,7 @@ async function startChannelSession(options) {
|
|
|
73129
73391
|
} catch (error) {
|
|
73130
73392
|
if (!stampFailureReported) {
|
|
73131
73393
|
stampFailureReported = true;
|
|
73132
|
-
|
|
73394
|
+
sessionStderr(`envoy-channel: could not write the session title for the dispatch command (${messageFor(error)}); retrying every heartbeat, and the command names this session by its id
|
|
73133
73395
|
`);
|
|
73134
73396
|
}
|
|
73135
73397
|
}
|
|
@@ -73183,29 +73445,39 @@ async function startChannelSession(options) {
|
|
|
73183
73445
|
userTopics.delete(topic);
|
|
73184
73446
|
followedTopics.unfollow(topics);
|
|
73185
73447
|
ledger.followedChanged().catch((error) => {
|
|
73186
|
-
|
|
73448
|
+
sessionStderr(`envoy-channel: the topics the listener refused stay in the delivery state until it is written next \u2014 ${messageFor(error)}
|
|
73187
73449
|
`);
|
|
73188
73450
|
});
|
|
73189
|
-
|
|
73190
|
-
|
|
73191
|
-
delivery.notice(refusedTopicsNoticeText(topics, reason)).catch((noticeError) => {
|
|
73192
|
-
process.stderr.write(`envoy-channel: the session could not be told which topics the listener refused \u2014 ${messageFor(noticeError)}
|
|
73451
|
+
showNotice(refusedTopicsNoticeText(topics, reason)).catch((noticeError) => {
|
|
73452
|
+
sessionStderr(`envoy-channel: the session could not be told which topics the listener refused \u2014 ${messageFor(noticeError)}
|
|
73193
73453
|
`);
|
|
73194
73454
|
});
|
|
73195
73455
|
},
|
|
73196
|
-
log: (line) =>
|
|
73197
|
-
`)
|
|
73456
|
+
log: (line) => sessionStderr(`envoy-channel: ${line}
|
|
73457
|
+
`),
|
|
73458
|
+
stopping: () => shuttingDown
|
|
73198
73459
|
});
|
|
73199
73460
|
};
|
|
73200
|
-
const writeRoleRecord = (file, role) =>
|
|
73201
|
-
|
|
73202
|
-
|
|
73203
|
-
|
|
73204
|
-
|
|
73461
|
+
const writeRoleRecord = async (file, role) => {
|
|
73462
|
+
if (shuttingDown)
|
|
73463
|
+
return;
|
|
73464
|
+
await writePersistedRole(file, {
|
|
73465
|
+
session_id: identity.id,
|
|
73466
|
+
role,
|
|
73467
|
+
client_instance_id: options.clientInstanceID
|
|
73468
|
+
});
|
|
73469
|
+
};
|
|
73470
|
+
const removeRoleRecord = async (file) => {
|
|
73471
|
+
if (shuttingDown)
|
|
73472
|
+
return;
|
|
73473
|
+
await rm5(file, { force: true });
|
|
73474
|
+
};
|
|
73205
73475
|
let roleTransferFrom;
|
|
73206
73476
|
const restoreRole = async (previousSessionID) => {
|
|
73207
73477
|
const previousFile = roleStateFile(options.stateDirectory, previousSessionID);
|
|
73208
73478
|
const persisted = await readPersistedRole(previousFile);
|
|
73479
|
+
if (shuttingDown)
|
|
73480
|
+
return "pending";
|
|
73209
73481
|
if (persisted === undefined)
|
|
73210
73482
|
return "settled";
|
|
73211
73483
|
if (!mayActOnRoleRecord(persisted.client_instance_id, options.clientInstanceID, heldRole !== undefined)) {
|
|
@@ -73219,18 +73491,20 @@ async function startChannelSession(options) {
|
|
|
73219
73491
|
previousSessionID: persisted.session_id,
|
|
73220
73492
|
clientInstanceID: options.clientInstanceID
|
|
73221
73493
|
});
|
|
73494
|
+
if (shuttingDown)
|
|
73495
|
+
return "pending";
|
|
73222
73496
|
if (!claim.claimed) {
|
|
73223
73497
|
if (claim.holder === identity.id)
|
|
73224
73498
|
return "pending";
|
|
73225
73499
|
if (claim.holder !== persisted.session_id)
|
|
73226
|
-
await
|
|
73500
|
+
await removeRoleRecord(previousFile);
|
|
73227
73501
|
return "settled";
|
|
73228
73502
|
}
|
|
73229
73503
|
logRoleClaimWarnings(warnOnStderr, persisted.role, sessionID, claim.warnings);
|
|
73230
73504
|
heldRole = persisted.role;
|
|
73231
73505
|
const currentFile = roleStateFile(options.stateDirectory, identity.id);
|
|
73232
73506
|
if (previousFile !== currentFile)
|
|
73233
|
-
await
|
|
73507
|
+
await removeRoleRecord(previousFile);
|
|
73234
73508
|
await writeRoleRecord(currentFile, persisted.role);
|
|
73235
73509
|
return "settled";
|
|
73236
73510
|
};
|
|
@@ -73240,12 +73514,14 @@ async function startChannelSession(options) {
|
|
|
73240
73514
|
if (hook === undefined)
|
|
73241
73515
|
return;
|
|
73242
73516
|
Promise.resolve().then(() => hook(role, reason)).catch((error) => {
|
|
73243
|
-
|
|
73517
|
+
sessionStderr(`envoy-channel: role ${role} regain hook failed (${reason}) \u2014 ${messageFor(error)}
|
|
73244
73518
|
`);
|
|
73245
73519
|
});
|
|
73246
73520
|
};
|
|
73247
73521
|
let roleRefreshFailureReported = false;
|
|
73248
73522
|
const refreshRoleRecord = async () => {
|
|
73523
|
+
if (shuttingDown)
|
|
73524
|
+
return;
|
|
73249
73525
|
const roleFile = roleStateFile(options.stateDirectory, identity.id);
|
|
73250
73526
|
try {
|
|
73251
73527
|
await touchStateFile(roleFile);
|
|
@@ -73253,7 +73529,7 @@ async function startChannelSession(options) {
|
|
|
73253
73529
|
} catch (error) {
|
|
73254
73530
|
if (!roleRefreshFailureReported) {
|
|
73255
73531
|
roleRefreshFailureReported = true;
|
|
73256
|
-
|
|
73532
|
+
sessionStderr(`envoy-channel: could not refresh the role record ${roleFile} (${messageFor(error)}); retrying every heartbeat
|
|
73257
73533
|
`);
|
|
73258
73534
|
}
|
|
73259
73535
|
}
|
|
@@ -73263,9 +73539,11 @@ async function startChannelSession(options) {
|
|
|
73263
73539
|
return;
|
|
73264
73540
|
const role = heldRole;
|
|
73265
73541
|
const holding = await options.client.getRole(role).catch(undefinedOnNotFound);
|
|
73542
|
+
if (shuttingDown)
|
|
73543
|
+
return;
|
|
73266
73544
|
if (roleHeldBy(holding, identity.id, options.clientInstanceID)) {
|
|
73267
73545
|
await refreshRoleRecord();
|
|
73268
|
-
if (afterOutage)
|
|
73546
|
+
if (afterOutage && !shuttingDown)
|
|
73269
73547
|
fireRegained(role, "reregistered");
|
|
73270
73548
|
return;
|
|
73271
73549
|
}
|
|
@@ -73276,10 +73554,12 @@ async function startChannelSession(options) {
|
|
|
73276
73554
|
soft: true,
|
|
73277
73555
|
clientInstanceID: options.clientInstanceID
|
|
73278
73556
|
});
|
|
73557
|
+
if (shuttingDown)
|
|
73558
|
+
return;
|
|
73279
73559
|
if (!result.claimed) {
|
|
73280
73560
|
if (result.holder !== identity.id) {
|
|
73281
73561
|
heldRole = undefined;
|
|
73282
|
-
await
|
|
73562
|
+
await removeRoleRecord(roleStateFile(options.stateDirectory, identity.id));
|
|
73283
73563
|
return;
|
|
73284
73564
|
}
|
|
73285
73565
|
await refreshRoleRecord();
|
|
@@ -73299,7 +73579,7 @@ async function startChannelSession(options) {
|
|
|
73299
73579
|
} catch (error) {
|
|
73300
73580
|
if (!roleTransferFailureReported) {
|
|
73301
73581
|
roleTransferFailureReported = true;
|
|
73302
|
-
|
|
73582
|
+
sessionStderr(`envoy-channel: could not take back the role held by ${from} (${messageFor(error)}); retrying every heartbeat
|
|
73303
73583
|
`);
|
|
73304
73584
|
}
|
|
73305
73585
|
}
|
|
@@ -73318,11 +73598,19 @@ async function startChannelSession(options) {
|
|
|
73318
73598
|
followedTopics.follow(nextSubject);
|
|
73319
73599
|
try {
|
|
73320
73600
|
await withTimeout(connection.flush(), options.handoffFlushTimeoutMs ?? HANDOFF_FLUSH_TIMEOUT_MS, "flushing NATS");
|
|
73601
|
+
if (shuttingDown) {
|
|
73602
|
+
followedTopics.unfollow([nextSubject]);
|
|
73603
|
+
return;
|
|
73604
|
+
}
|
|
73321
73605
|
await deregister(previous);
|
|
73322
73606
|
} catch (error) {
|
|
73323
73607
|
followedTopics.unfollow([nextSubject]);
|
|
73324
73608
|
throw error;
|
|
73325
73609
|
}
|
|
73610
|
+
if (shuttingDown) {
|
|
73611
|
+
followedTopics.unfollow([nextSubject]);
|
|
73612
|
+
return;
|
|
73613
|
+
}
|
|
73326
73614
|
identity.set(next);
|
|
73327
73615
|
options.adopted?.(previous, next);
|
|
73328
73616
|
forgetBrokerSession(previous);
|
|
@@ -73334,7 +73622,7 @@ async function startChannelSession(options) {
|
|
|
73334
73622
|
roleTransferFrom ??= previous;
|
|
73335
73623
|
await register();
|
|
73336
73624
|
await transferRole();
|
|
73337
|
-
|
|
73625
|
+
sessionStderr(`envoy: session id changed ${previous} -> ${next}; re-registered
|
|
73338
73626
|
`);
|
|
73339
73627
|
};
|
|
73340
73628
|
let interestsRead = false;
|
|
@@ -73392,6 +73680,8 @@ async function startChannelSession(options) {
|
|
|
73392
73680
|
heartbeatInFlight = true;
|
|
73393
73681
|
try {
|
|
73394
73682
|
await serialized(async () => {
|
|
73683
|
+
if (shuttingDown)
|
|
73684
|
+
return;
|
|
73395
73685
|
await adoptHandoff();
|
|
73396
73686
|
await resubscribeRelay();
|
|
73397
73687
|
try {
|
|
@@ -73403,6 +73693,8 @@ async function startChannelSession(options) {
|
|
|
73403
73693
|
if (shuttingDown)
|
|
73404
73694
|
return;
|
|
73405
73695
|
await reassertRole(registrationFailed);
|
|
73696
|
+
if (shuttingDown)
|
|
73697
|
+
return;
|
|
73406
73698
|
await transferRole();
|
|
73407
73699
|
await syncRegisteredInterests();
|
|
73408
73700
|
});
|
|
@@ -73410,12 +73702,14 @@ async function startChannelSession(options) {
|
|
|
73410
73702
|
outageReported = false;
|
|
73411
73703
|
clearRetry();
|
|
73412
73704
|
} catch (error) {
|
|
73705
|
+
if (shuttingDown)
|
|
73706
|
+
return;
|
|
73413
73707
|
const retryIn = fromRetry ? undefined : armRetry();
|
|
73414
73708
|
if (outageReported)
|
|
73415
73709
|
return;
|
|
73416
73710
|
outageReported = true;
|
|
73417
73711
|
const retrying = retryIn === undefined ? "retrying at the next heartbeat" : `retrying once in ${Math.round(retryIn / 1000)} s, then every heartbeat`;
|
|
73418
|
-
|
|
73712
|
+
sessionStderr(`envoy-channel: registry heartbeat failed (${messageFor(error)}); ${retrying}
|
|
73419
73713
|
`);
|
|
73420
73714
|
} finally {
|
|
73421
73715
|
heartbeatInFlight = false;
|
|
@@ -73453,7 +73747,7 @@ async function startChannelSession(options) {
|
|
|
73453
73747
|
followedTopics.follow(topic);
|
|
73454
73748
|
}
|
|
73455
73749
|
await syncRegisteredInterests().catch((error) => {
|
|
73456
|
-
|
|
73750
|
+
sessionStderr(`envoy-channel: could not read the registered interests (${messageFor(error)}); the next heartbeat retries
|
|
73457
73751
|
`);
|
|
73458
73752
|
});
|
|
73459
73753
|
const closedBeforeFlush = await flushThroughRedials(connection, options.startSignal);
|
|
@@ -73468,19 +73762,19 @@ async function startChannelSession(options) {
|
|
|
73468
73762
|
await serialized(async () => {
|
|
73469
73763
|
await register().catch((error) => {
|
|
73470
73764
|
registrationFailed = true;
|
|
73471
|
-
|
|
73765
|
+
sessionStderr(`envoy-channel: the first registration failed (${messageFor(error)}); the next heartbeat retries
|
|
73472
73766
|
`);
|
|
73473
73767
|
});
|
|
73474
73768
|
try {
|
|
73475
73769
|
roleTransferFrom = await restoreRole(identity.id) === "pending" ? identity.id : undefined;
|
|
73476
73770
|
} catch (error) {
|
|
73477
73771
|
if (error instanceof UnparseableRoleRecordError) {
|
|
73478
|
-
|
|
73772
|
+
sessionStderr(`envoy-channel: ${messageFor(error)}; the session starts without a role
|
|
73479
73773
|
`);
|
|
73480
73774
|
} else {
|
|
73481
73775
|
roleTransferFrom = identity.id;
|
|
73482
73776
|
roleTransferFailureReported = true;
|
|
73483
|
-
|
|
73777
|
+
sessionStderr(`envoy-channel: could not restore the role (${messageFor(error)}); the next heartbeat retries
|
|
73484
73778
|
`);
|
|
73485
73779
|
}
|
|
73486
73780
|
}
|
|
@@ -73491,8 +73785,10 @@ async function startChannelSession(options) {
|
|
|
73491
73785
|
() => pruneStaleSessionTurns(options.stateDirectory),
|
|
73492
73786
|
() => pruneStaleSessionRoles(options.stateDirectory, identity.id)
|
|
73493
73787
|
]) {
|
|
73788
|
+
if (shuttingDown)
|
|
73789
|
+
break;
|
|
73494
73790
|
await prune().catch((error) => {
|
|
73495
|
-
|
|
73791
|
+
sessionStderr(`envoy-channel: could not prune stale session state (${messageFor(error)})
|
|
73496
73792
|
`);
|
|
73497
73793
|
});
|
|
73498
73794
|
}
|
|
@@ -73514,7 +73810,7 @@ async function startChannelSession(options) {
|
|
|
73514
73810
|
connectionLost = { closeError, dialError: undefined };
|
|
73515
73811
|
if (relay !== undefined)
|
|
73516
73812
|
relay.subscription = undefined;
|
|
73517
|
-
|
|
73813
|
+
sessionStderr(`envoy-channel: the NATS connection closed for good (${lostConnectionReason(connectionLost)}); ${options.reopen === undefined ? "nothing dials a new one" : "dialing a new one"}
|
|
73518
73814
|
`);
|
|
73519
73815
|
serialized(register).catch(() => {
|
|
73520
73816
|
registrationFailed = true;
|
|
@@ -73540,10 +73836,10 @@ async function startChannelSession(options) {
|
|
|
73540
73836
|
lost.dialError = dialError;
|
|
73541
73837
|
});
|
|
73542
73838
|
} catch (error) {
|
|
73543
|
-
if (shuttingDown
|
|
73839
|
+
if (shuttingDown)
|
|
73544
73840
|
return;
|
|
73545
73841
|
lost.dialError = error;
|
|
73546
|
-
|
|
73842
|
+
sessionStderr(`envoy-channel: gave up opening a new NATS connection (${natsFailureReason(error)}); dialing again at the next heartbeat
|
|
73547
73843
|
`);
|
|
73548
73844
|
return;
|
|
73549
73845
|
}
|
|
@@ -73562,7 +73858,7 @@ async function startChannelSession(options) {
|
|
|
73562
73858
|
watchConnection(live);
|
|
73563
73859
|
await serialized(async () => {
|
|
73564
73860
|
await withTimeout(live.flush(), options.handoffFlushTimeoutMs ?? HANDOFF_FLUSH_TIMEOUT_MS, "flushing NATS").catch((error) => {
|
|
73565
|
-
|
|
73861
|
+
sessionStderr(`envoy-channel: could not confirm the relay subscription on the new NATS connection \u2014 ${natsFailureReason(error)}
|
|
73566
73862
|
`);
|
|
73567
73863
|
});
|
|
73568
73864
|
if (shuttingDown || live !== connection || live.isClosed())
|
|
@@ -73572,7 +73868,7 @@ async function startChannelSession(options) {
|
|
|
73572
73868
|
await register();
|
|
73573
73869
|
}).catch((error) => {
|
|
73574
73870
|
registrationFailed = true;
|
|
73575
|
-
|
|
73871
|
+
sessionStderr(`envoy-channel: could not register on the new NATS connection (${messageFor(error)}); the next heartbeat retries
|
|
73576
73872
|
`);
|
|
73577
73873
|
});
|
|
73578
73874
|
}
|
|
@@ -73588,7 +73884,7 @@ async function startChannelSession(options) {
|
|
|
73588
73884
|
renewRelayEpoch();
|
|
73589
73885
|
}
|
|
73590
73886
|
} catch (error) {
|
|
73591
|
-
|
|
73887
|
+
sessionStderr(`envoy-channel: the NATS connection's status reader failed \u2014 ${natsFailureReason(error)}
|
|
73592
73888
|
`);
|
|
73593
73889
|
}
|
|
73594
73890
|
})();
|
|
@@ -73619,6 +73915,8 @@ async function startChannelSession(options) {
|
|
|
73619
73915
|
return fresh;
|
|
73620
73916
|
},
|
|
73621
73917
|
async unfollow(topics) {
|
|
73918
|
+
if (shuttingDown)
|
|
73919
|
+
return [];
|
|
73622
73920
|
const requested = topics.length === 0 ? [...userTopics] : expandSubscriptionTopics(topics);
|
|
73623
73921
|
const removed = requested.filter((topic) => userTopics.delete(topic));
|
|
73624
73922
|
if (removed.length === 0)
|
|
@@ -73635,7 +73933,9 @@ async function startChannelSession(options) {
|
|
|
73635
73933
|
if (shuttingDown)
|
|
73636
73934
|
return;
|
|
73637
73935
|
serialized(register).catch((error) => {
|
|
73638
|
-
|
|
73936
|
+
if (shuttingDown)
|
|
73937
|
+
return;
|
|
73938
|
+
sessionStderr(`envoy-channel: could not advertise this session's delivery modes (${messageFor(error)}); the next heartbeat retries
|
|
73639
73939
|
`);
|
|
73640
73940
|
});
|
|
73641
73941
|
},
|
|
@@ -73647,13 +73947,19 @@ async function startChannelSession(options) {
|
|
|
73647
73947
|
},
|
|
73648
73948
|
async rememberRole(role) {
|
|
73649
73949
|
await serialized(async () => {
|
|
73950
|
+
if (shuttingDown)
|
|
73951
|
+
return;
|
|
73650
73952
|
heldRole = role;
|
|
73651
73953
|
await writeRoleRecord(roleStateFile(options.stateDirectory, identity.id), role);
|
|
73652
73954
|
});
|
|
73653
73955
|
},
|
|
73956
|
+
stopping() {
|
|
73957
|
+
return shuttingDown;
|
|
73958
|
+
},
|
|
73654
73959
|
async shutdown() {
|
|
73655
|
-
if (
|
|
73960
|
+
if (shutdownStarted)
|
|
73656
73961
|
return;
|
|
73962
|
+
shutdownStarted = true;
|
|
73657
73963
|
shuttingDown = true;
|
|
73658
73964
|
clearInterval(heartbeatTimer);
|
|
73659
73965
|
clearRetry();
|
|
@@ -73753,12 +74059,14 @@ async function executeEnvoyTool(runtime, name, input) {
|
|
|
73753
74059
|
}
|
|
73754
74060
|
case EnvoyToolOperation.setRole: {
|
|
73755
74061
|
const args = parseArguments(spec, input);
|
|
74062
|
+
refuseClaimWhileStopping(runtime.session, args.role);
|
|
73756
74063
|
const sessionID = identity.id;
|
|
73757
74064
|
const result = await runtime.client.setRole({
|
|
73758
74065
|
sessionID,
|
|
73759
74066
|
role: args.role,
|
|
73760
74067
|
clientInstanceID: runtime.clientInstanceID
|
|
73761
74068
|
});
|
|
74069
|
+
refuseClaimWhileStopping(runtime.session, args.role);
|
|
73762
74070
|
if (!result.claimed)
|
|
73763
74071
|
throw new Error(`role ${args.role} is held by ${result.holder}`);
|
|
73764
74072
|
logRoleClaimWarnings(warnOnStderr, args.role, sessionID, result.warnings);
|
|
@@ -73784,6 +74092,10 @@ async function executeEnvoyTool(runtime, name, input) {
|
|
|
73784
74092
|
}
|
|
73785
74093
|
}
|
|
73786
74094
|
}
|
|
74095
|
+
function refuseClaimWhileStopping(session, role) {
|
|
74096
|
+
if (session.stopping())
|
|
74097
|
+
throw new RoleClaimStoppingError(role);
|
|
74098
|
+
}
|
|
73787
74099
|
function pluginStateDirectory() {
|
|
73788
74100
|
const pluginData = configuredValue(process.env["CLAUDE_PLUGIN_DATA"]);
|
|
73789
74101
|
if (pluginData === undefined) {
|
|
@@ -73794,6 +74106,13 @@ function pluginStateDirectory() {
|
|
|
73794
74106
|
async function runEnvoyChannelServer() {
|
|
73795
74107
|
const directory = projectDirectory();
|
|
73796
74108
|
const pane = await legionPane(process.env);
|
|
74109
|
+
let stopping = false;
|
|
74110
|
+
const serverStderr = gatedStderr(() => stopping);
|
|
74111
|
+
const logLegionLine = (line) => {
|
|
74112
|
+
if (!stopping)
|
|
74113
|
+
logLegion(line);
|
|
74114
|
+
};
|
|
74115
|
+
const paneServer = { stopping: () => stopping, log: logLegionLine };
|
|
73797
74116
|
const configFile = sessionConfigFile("claude", process.env);
|
|
73798
74117
|
const currentSessionId = () => identity.id;
|
|
73799
74118
|
const { dispatch, envoy, notice } = await sessionSettings(configFile, process.env, {
|
|
@@ -73801,7 +74120,7 @@ async function runEnvoyChannelServer() {
|
|
|
73801
74120
|
session: currentSessionId
|
|
73802
74121
|
});
|
|
73803
74122
|
if (notice !== null)
|
|
73804
|
-
|
|
74123
|
+
serverStderr(`envoy-channel: ${notice}
|
|
73805
74124
|
`);
|
|
73806
74125
|
const overrideSessionId = process.env["ENVOY_SESSION_ID"];
|
|
73807
74126
|
const pluginData = configuredValue(process.env["CLAUDE_PLUGIN_DATA"]);
|
|
@@ -73825,7 +74144,7 @@ async function runEnvoyChannelServer() {
|
|
|
73825
74144
|
if (own !== "")
|
|
73826
74145
|
stampTitle(sessionId, own);
|
|
73827
74146
|
} catch (error) {
|
|
73828
|
-
|
|
74147
|
+
serverStderr(`envoy-channel: could not write the session title for the dispatch command \u2014 ${messageFor(error)}; the dispatch command's writes carry no session title
|
|
73829
74148
|
`);
|
|
73830
74149
|
}
|
|
73831
74150
|
};
|
|
@@ -73835,8 +74154,9 @@ async function runEnvoyChannelServer() {
|
|
|
73835
74154
|
CLAUDE_CODE_SESSION_ID: process.env["CLAUDE_CODE_SESSION_ID"]
|
|
73836
74155
|
}));
|
|
73837
74156
|
const refusal = `Envoy is off for this session: ${envoy.error ?? envoy.notice}`;
|
|
73838
|
-
if (pane !== undefined)
|
|
73839
|
-
stopPane(`a Legion pane needs Envoy for its task; ${refusal}
|
|
74157
|
+
if (pane !== undefined) {
|
|
74158
|
+
stopPane(`a Legion pane needs Envoy for its task; ${refusal}`, paneServer);
|
|
74159
|
+
}
|
|
73840
74160
|
await serveEnvoyOff(MCP_SERVER_INFO, refusal);
|
|
73841
74161
|
return;
|
|
73842
74162
|
}
|
|
@@ -73855,14 +74175,15 @@ async function runEnvoyChannelServer() {
|
|
|
73855
74175
|
const client = createEnvoyClient({
|
|
73856
74176
|
baseUrl: envoy.url,
|
|
73857
74177
|
fetch: globalThis.fetch,
|
|
73858
|
-
credential: envoy.credential
|
|
74178
|
+
credential: envoy.credential,
|
|
74179
|
+
stopping: () => stopping
|
|
73859
74180
|
});
|
|
73860
74181
|
const clientInstanceID = newClientInstanceID();
|
|
73861
74182
|
let runtime;
|
|
73862
74183
|
let starting = "it reads its settings";
|
|
73863
74184
|
let claim;
|
|
73864
74185
|
const daemon = pane === undefined ? undefined : paneDaemonClient(pane.daemonUrl);
|
|
73865
|
-
const phaseLog = paneReminderLog(
|
|
74186
|
+
const phaseLog = paneReminderLog(logLegionLine);
|
|
73866
74187
|
const claimBooted = Promise.withResolvers();
|
|
73867
74188
|
let reportFirstTurn;
|
|
73868
74189
|
const phaseHook = pane === undefined || daemon === undefined ? undefined : createPhaseHookEndpoint({
|
|
@@ -73923,7 +74244,7 @@ async function runEnvoyChannelServer() {
|
|
|
73923
74244
|
},
|
|
73924
74245
|
connected: () => modLink?.connected() === true
|
|
73925
74246
|
},
|
|
73926
|
-
warn: (text) =>
|
|
74247
|
+
warn: (text) => serverStderr(`envoy-channel: ${text}
|
|
73927
74248
|
`)
|
|
73928
74249
|
});
|
|
73929
74250
|
const writer = inbox ?? {
|
|
@@ -73944,7 +74265,6 @@ async function runEnvoyChannelServer() {
|
|
|
73944
74265
|
const capabilities = () => sessionCapabilities({ pane: inboxSocket !== undefined, mod: modLink?.connected() === true });
|
|
73945
74266
|
const booting = new AbortController;
|
|
73946
74267
|
const stopped = Promise.withResolvers();
|
|
73947
|
-
let stopping = false;
|
|
73948
74268
|
let session;
|
|
73949
74269
|
let stopNatsKeeper = () => {};
|
|
73950
74270
|
const stop = () => {
|
|
@@ -73972,7 +74292,7 @@ async function runEnvoyChannelServer() {
|
|
|
73972
74292
|
const linking = startModLink({
|
|
73973
74293
|
sessionId: claudeSessionId({ CLAUDE_CODE_SESSION_ID: process.env["CLAUDE_CODE_SESSION_ID"] }),
|
|
73974
74294
|
env: process.env,
|
|
73975
|
-
warn: (text) =>
|
|
74295
|
+
warn: (text) => serverStderr(`envoy-channel: ${text}
|
|
73976
74296
|
`)
|
|
73977
74297
|
}).then((link) => {
|
|
73978
74298
|
modLink = link;
|
|
@@ -73983,7 +74303,7 @@ async function runEnvoyChannelServer() {
|
|
|
73983
74303
|
else
|
|
73984
74304
|
linked.resolve(link);
|
|
73985
74305
|
}, (error) => {
|
|
73986
|
-
|
|
74306
|
+
serverStderr(`envoy-channel: no link to this session's Claude Code mod \u2014 ${messageFor(error)}; the session runs on without one
|
|
73987
74307
|
`);
|
|
73988
74308
|
});
|
|
73989
74309
|
const router = (state) => {
|
|
@@ -73995,7 +74315,7 @@ async function runEnvoyChannelServer() {
|
|
|
73995
74315
|
session: claude,
|
|
73996
74316
|
sessionId: () => identity.id,
|
|
73997
74317
|
state,
|
|
73998
|
-
warn: (text) =>
|
|
74318
|
+
warn: (text) => serverStderr(`envoy-channel: ${text}
|
|
73999
74319
|
`),
|
|
74000
74320
|
...inboxSocket === undefined ? {} : { inbox: (input) => notifier.notification(input) }
|
|
74001
74321
|
};
|
|
@@ -74006,19 +74326,24 @@ async function runEnvoyChannelServer() {
|
|
|
74006
74326
|
const { inbound } = envoy;
|
|
74007
74327
|
if ("off" in inbound) {
|
|
74008
74328
|
if (pane !== undefined) {
|
|
74009
|
-
stopPane(`a Legion pane's task reaches it over NATS, and inbound messages are off: ${inbound.off}
|
|
74329
|
+
stopPane(`a Legion pane's task reaches it over NATS, and inbound messages are off: ${inbound.off}`, paneServer);
|
|
74010
74330
|
}
|
|
74011
74331
|
stampOnce(() => identity.id);
|
|
74012
|
-
session = inboundOffChannelSession({
|
|
74332
|
+
session = inboundOffChannelSession({
|
|
74333
|
+
identity,
|
|
74334
|
+
stateDirectory,
|
|
74335
|
+
why: inbound.off,
|
|
74336
|
+
stopping: () => stopping
|
|
74337
|
+
});
|
|
74013
74338
|
} else {
|
|
74014
74339
|
starting = "relaySetting" in inbound ? "it opens the listener's relay socket for this session, and its stderr says why an open failed" : inbound.natsCredential.kind === "broker" ? "it waits for this session's broker token, and its stderr says what each trade came to" : "it connects to NATS";
|
|
74015
74340
|
const natsName = channelConnectionName(`claude-envoy-channel-${identity.id}`, bundlePath, name2);
|
|
74016
74341
|
const dialled = await dialChannelInbound(inbound, envoy.url, currentSessionId, natsName, (text, dialError) => {
|
|
74017
74342
|
if (dialError !== undefined)
|
|
74018
74343
|
starting = text;
|
|
74019
|
-
|
|
74344
|
+
serverStderr(`envoy-channel: ${text}
|
|
74020
74345
|
`);
|
|
74021
|
-
}, import_nats5.connect, booting.signal).catch((error) => {
|
|
74346
|
+
}, import_nats5.connect, booting.signal, () => stopping).catch((error) => {
|
|
74022
74347
|
if (stopping)
|
|
74023
74348
|
return;
|
|
74024
74349
|
throw error;
|
|
@@ -74037,6 +74362,9 @@ async function runEnvoyChannelServer() {
|
|
|
74037
74362
|
session: currentSessionId,
|
|
74038
74363
|
name: natsName,
|
|
74039
74364
|
signal: booting.signal,
|
|
74365
|
+
stopping: () => stopping,
|
|
74366
|
+
warn: (text) => serverStderr(`envoy-channel: ${text}
|
|
74367
|
+
`),
|
|
74040
74368
|
swapKeeper: (stop) => {
|
|
74041
74369
|
stopNatsKeeper();
|
|
74042
74370
|
stopNatsKeeper = stop;
|
|
@@ -74083,6 +74411,8 @@ async function runEnvoyChannelServer() {
|
|
|
74083
74411
|
agentStream = startAgentStream({
|
|
74084
74412
|
connection,
|
|
74085
74413
|
liveView: dialled.liveView,
|
|
74414
|
+
warn: (text) => serverStderr(`envoy-channel: ${text}
|
|
74415
|
+
`),
|
|
74086
74416
|
followsSessionId: configuredValue(overrideSessionId) === undefined,
|
|
74087
74417
|
...admission === undefined ? {} : { admitted: (id) => admission.admitted(id) },
|
|
74088
74418
|
isPane: inbox !== undefined,
|
|
@@ -74113,7 +74443,7 @@ async function runEnvoyChannelServer() {
|
|
|
74113
74443
|
clientInstanceID,
|
|
74114
74444
|
session,
|
|
74115
74445
|
signal: booting.signal,
|
|
74116
|
-
log:
|
|
74446
|
+
log: logLegionLine,
|
|
74117
74447
|
stop: endPane,
|
|
74118
74448
|
onFirstTurn: (started) => {
|
|
74119
74449
|
reportFirstTurn = started;
|
|
@@ -75607,13 +75937,14 @@ async function runCodexEnvoyServer() {
|
|
|
75607
75937
|
let recheck;
|
|
75608
75938
|
const endedNow = (sessionId) => sessionEnded(codexEndedFile(stateDirectory, codexPid, sessionId));
|
|
75609
75939
|
const configFile = sessionConfigFile("codex", process.env);
|
|
75610
|
-
const warn = (text) => void process.stderr.write(`envoy-channel: ${text}
|
|
75611
|
-
`);
|
|
75612
75940
|
const bundlePath = resolve5(pluginRoot, process.argv[1] ?? "");
|
|
75613
75941
|
let record;
|
|
75614
75942
|
let runtime;
|
|
75615
75943
|
let notReady = "this session's Envoy side starts at its first prompt";
|
|
75616
75944
|
const stopping = new AbortController;
|
|
75945
|
+
const serverStderr = gatedStderr(() => stopping.signal.aborted);
|
|
75946
|
+
const warn = (text) => serverStderr(`envoy-channel: ${text}
|
|
75947
|
+
`);
|
|
75617
75948
|
const server = new Server(MCP_SERVER_INFO, {
|
|
75618
75949
|
capabilities: { tools: {} },
|
|
75619
75950
|
instructions: CODEX_INSTRUCTIONS
|
|
@@ -75710,7 +76041,7 @@ async function runCodexEnvoyServer() {
|
|
|
75710
76041
|
notReady = STANDBY_NOTE;
|
|
75711
76042
|
const owning = await waitForOwnership(serversDirectory, process.pid, processStarted(process.pid) ?? null, stopping.signal, (entry) => {
|
|
75712
76043
|
self2 = entry;
|
|
75713
|
-
});
|
|
76044
|
+
}, undefined, undefined, warn);
|
|
75714
76045
|
if (owning === undefined) {
|
|
75715
76046
|
await stopped.promise;
|
|
75716
76047
|
await server.close().catch(() => {
|
|
@@ -75793,7 +76124,8 @@ async function runCodexEnvoyServer() {
|
|
|
75793
76124
|
const client = createEnvoyClient({
|
|
75794
76125
|
baseUrl: envoy.url,
|
|
75795
76126
|
credential: envoy.credential,
|
|
75796
|
-
fetch: globalThis.fetch
|
|
76127
|
+
fetch: globalThis.fetch,
|
|
76128
|
+
stopping: () => stopping.signal.aborted
|
|
75797
76129
|
});
|
|
75798
76130
|
const clientInstanceID = newClientInstanceID();
|
|
75799
76131
|
const runningSession = () => record?.session_id ?? bound.session_id;
|
|
@@ -75831,10 +76163,15 @@ async function runCodexEnvoyServer() {
|
|
|
75831
76163
|
try {
|
|
75832
76164
|
const { inbound } = envoy;
|
|
75833
76165
|
if ("off" in inbound) {
|
|
75834
|
-
session = inboundOffChannelSession({
|
|
76166
|
+
session = inboundOffChannelSession({
|
|
76167
|
+
identity: id,
|
|
76168
|
+
stateDirectory,
|
|
76169
|
+
why: inbound.off,
|
|
76170
|
+
stopping: () => stopping.signal.aborted
|
|
76171
|
+
});
|
|
75835
76172
|
} else {
|
|
75836
76173
|
const natsName = channelConnectionName(`codex-envoy-channel-${id.id}`, bundlePath, name);
|
|
75837
|
-
const dialled = await dialChannelInbound(inbound, envoy.url, () => id.id, natsName, warn, import_nats6.connect, stopping.signal).catch((error) => {
|
|
76174
|
+
const dialled = await dialChannelInbound(inbound, envoy.url, () => id.id, natsName, warn, import_nats6.connect, stopping.signal, () => stopping.signal.aborted).catch((error) => {
|
|
75838
76175
|
if (isStopping)
|
|
75839
76176
|
return;
|
|
75840
76177
|
throw error;
|
|
@@ -75854,6 +76191,8 @@ async function runCodexEnvoyServer() {
|
|
|
75854
76191
|
session: () => id.id,
|
|
75855
76192
|
name: natsName,
|
|
75856
76193
|
signal: stopping.signal,
|
|
76194
|
+
stopping: () => stopping.signal.aborted,
|
|
76195
|
+
warn,
|
|
75857
76196
|
swapKeeper: (stop) => {
|
|
75858
76197
|
stopNatsKeeper();
|
|
75859
76198
|
stopNatsKeeper = stop;
|