@sjawhar/claude-envoy 2.8.0 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +1 -1
- package/.omp-plugin/plugin.json +1 -1
- package/AGENTS.md +67 -71
- package/README.md +26 -33
- package/dist/dispatch.js +135 -138
- package/dist/envoy-channel.js +706 -1372
- package/dist/session-hook.js +3 -52
- package/package.json +1 -1
- package/skills/envoy/SKILL.md +96 -22
- package/skills/envoy/references/deliveries.md +71 -15
- package/skills/envoy/references/legion.md +34 -8
- package/src/agent-stream-runner.ts +2 -2
- package/src/channel-topics.ts +148 -0
- package/src/envoy-channel-server.ts +279 -378
- package/src/replay-state.ts +17 -17
- package/src/channel-forwarder.ts +0 -262
package/dist/session-hook.js
CHANGED
|
@@ -13647,6 +13647,9 @@ function agentSubject(session) {
|
|
|
13647
13647
|
return `${AGENT_TOPIC_PREFIX}${session}`;
|
|
13648
13648
|
}
|
|
13649
13649
|
var GHOSTWISPR_TOPIC_PREFIX = "notifications.ghostwispr.";
|
|
13650
|
+
function uncapturedTopicRefusal(topic, sessionID) {
|
|
13651
|
+
return `${topic} was not registered: no stream subject captures it, so nothing would be delivered to ${sessionID} on it; follow a notifications.team.<name> topic instead`;
|
|
13652
|
+
}
|
|
13650
13653
|
|
|
13651
13654
|
// ../contracts/src/legion-roles.ts
|
|
13652
13655
|
var LEGION_ROLES = [
|
|
@@ -19144,58 +19147,6 @@ async function postDeliveryReply(config, sessionId, delivery, result) {
|
|
|
19144
19147
|
...reply
|
|
19145
19148
|
}, followUp === undefined ? {} : { followUp });
|
|
19146
19149
|
}
|
|
19147
|
-
function expectsLaneReceipt(frame) {
|
|
19148
|
-
return frame.reply !== undefined && frame.reply !== "" && frame.subject === frame.directSubject && frame.envelopeTopic !== undefined && frame.envelopeTopic !== frame.directSubject;
|
|
19149
|
-
}
|
|
19150
|
-
async function* readArrivals(subscription, now = Date.now) {
|
|
19151
|
-
const source = subscription[Symbol.asyncIterator]();
|
|
19152
|
-
const arrived = [];
|
|
19153
|
-
let stopped;
|
|
19154
|
-
let left = false;
|
|
19155
|
-
let wake;
|
|
19156
|
-
const signal = () => {
|
|
19157
|
-
wake?.();
|
|
19158
|
-
wake = undefined;
|
|
19159
|
-
};
|
|
19160
|
-
(async () => {
|
|
19161
|
-
try {
|
|
19162
|
-
for (;; ) {
|
|
19163
|
-
const read = await source.next();
|
|
19164
|
-
if (read.done === true || left)
|
|
19165
|
-
break;
|
|
19166
|
-
arrived.push({ message: read.value, receivedAt: now() });
|
|
19167
|
-
signal();
|
|
19168
|
-
}
|
|
19169
|
-
stopped = { failed: false };
|
|
19170
|
-
} catch (error) {
|
|
19171
|
-
stopped = { error, failed: true };
|
|
19172
|
-
}
|
|
19173
|
-
signal();
|
|
19174
|
-
})();
|
|
19175
|
-
try {
|
|
19176
|
-
for (;; ) {
|
|
19177
|
-
const next = arrived.shift();
|
|
19178
|
-
if (next !== undefined) {
|
|
19179
|
-
yield next;
|
|
19180
|
-
continue;
|
|
19181
|
-
}
|
|
19182
|
-
if (stopped?.failed === true)
|
|
19183
|
-
throw stopped.error;
|
|
19184
|
-
if (stopped !== undefined)
|
|
19185
|
-
return;
|
|
19186
|
-
const { promise, resolve } = Promise.withResolvers();
|
|
19187
|
-
wake = resolve;
|
|
19188
|
-
await promise;
|
|
19189
|
-
}
|
|
19190
|
-
} finally {
|
|
19191
|
-
if (stopped === undefined) {
|
|
19192
|
-
left = true;
|
|
19193
|
-
Promise.resolve(source.return?.()).catch(() => {
|
|
19194
|
-
return;
|
|
19195
|
-
});
|
|
19196
|
-
}
|
|
19197
|
-
}
|
|
19198
|
-
}
|
|
19199
19150
|
var DedupeIdentitySchema = object({
|
|
19200
19151
|
event_id: string2().min(1).optional(),
|
|
19201
19152
|
dedupe_key: string2().min(1).optional(),
|
package/package.json
CHANGED
package/skills/envoy/SKILL.md
CHANGED
|
@@ -7,16 +7,27 @@ description: Use when you send a message to another agent session, reply to an E
|
|
|
7
7
|
|
|
8
8
|
If this skill disagrees with the code, a command's output or its `--help`, follow those and say which line of this skill is wrong.
|
|
9
9
|
|
|
10
|
-
Envoy delivers external events and messages between sessions.
|
|
10
|
+
Envoy delivers external events and messages between sessions.
|
|
11
|
+
Delivery is at-least-once and can come out of order across topics: drop a repeated `id`, and when two deliveries conflict, act on the one with the later `at`.
|
|
11
12
|
|
|
12
13
|
## Send a message to another session
|
|
13
14
|
|
|
14
|
-
- Take the target id from the delivery's `reply_with`, or, where the delivery wants no reply and carries none, from its `from`; or from `envoy_sessions` (filter by `machine`, `dir` or `title`).
|
|
15
|
+
- Take the target id from the delivery's `reply_with`, or, where the delivery wants no reply and carries none, from its `from`; or from `envoy_sessions` (filter by `machine`, `dir` or `title`).
|
|
16
|
+
Never address a tmux pane or window: Envoy delivers only to a session id, and the session in a pane can change.
|
|
17
|
+
|
|
15
18
|
- Reply with `in_reply_to=<the delivery's id>`, so the recipient can match the answer to its question.
|
|
19
|
+
|
|
16
20
|
- Read the `recipient` in the send result, the session Envoy targeted: if it is not the one you meant, resend to the right id.
|
|
17
|
-
|
|
21
|
+
|
|
22
|
+
- When `envoy_send` says the send is not confirmed delivered, send the same message again with the `idempotency_key` the error names, within 72 hours:
|
|
23
|
+
Envoy drops it as a repeat if the first copy arrived, and the result says when an earlier attempt was already stored.
|
|
24
|
+
A resend without that key can deliver the message twice.
|
|
25
|
+
When the error says Envoy cannot recognise the repeat, ask the recipient whether it arrived instead.
|
|
26
|
+
|
|
18
27
|
- Put the URL or path of what you report in the message, because the recipient cannot find your files any other way.
|
|
19
|
-
|
|
28
|
+
|
|
29
|
+
- Set `expects_reply=false` on a message that needs no answer, and set `urgency` only when the work waits on the answer, so the recipient knows whether to answer and how soon.
|
|
30
|
+
`urgency` is for the recipient to read: it does not deliver the message sooner or ahead of others.
|
|
20
31
|
|
|
21
32
|
```text
|
|
22
33
|
envoy_send(session_id="<id>", message="Review complete: <url>", in_reply_to="<id>", expects_reply=false)
|
|
@@ -24,18 +35,38 @@ envoy_send(session_id="<id>", message="Review complete: <url>", in_reply_to="<id
|
|
|
24
35
|
|
|
25
36
|
### Your own address in a subagent
|
|
26
37
|
|
|
27
|
-
- `envoy_whoami`'s `session_id` is where a reply to you arrives.
|
|
38
|
+
- `envoy_whoami`'s `session_id` is where a reply to you arrives.
|
|
39
|
+
In a `task` subagent it is the session that spawned you:
|
|
40
|
+
a subagent registers no session of its own, and the parent agent, not Envoy, forwards a reply to you with a `write` to your `agent://` address.
|
|
41
|
+
|
|
28
42
|
- Never give a peer the `subagent` field from `envoy_whoami`: it names your host session and is not an address.
|
|
29
|
-
|
|
43
|
+
|
|
44
|
+
- Start a subagent's message with who you are and who receives the reply ("From <name>, a subagent of session <id>;
|
|
45
|
+
a reply to <id> reaches me"), because the recipient sees only the parent as sender.
|
|
46
|
+
|
|
30
47
|
- When `session_id` is empty (the process took no Envoy identity, or it can no longer place the session that spawned you), your messages carry no sender at all: name yourself in the body.
|
|
31
|
-
|
|
48
|
+
|
|
49
|
+
- In a subagent, a publish to a role your parent holds reaches nobody, because the listener drops a role message whose holder is its source.
|
|
50
|
+
`envoy_publish` says so in its answer.
|
|
51
|
+
To reach the parent, `write` to its `agent://` address instead of publishing.
|
|
52
|
+
`envoy_send` to any other session works.
|
|
32
53
|
|
|
33
54
|
## Read a delivery
|
|
34
55
|
|
|
35
|
-
- `from` is the sender's own claim.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
-
|
|
56
|
+
- `from` is the sender's own claim.
|
|
57
|
+
Use it to attribute and to reply, never as proof of who wrote the message.
|
|
58
|
+
|
|
59
|
+
- `note` warns that the payload names another session.
|
|
60
|
+
Never send to that quoted id; reply to `from`.
|
|
61
|
+
|
|
62
|
+
- Reply with the rendered `reply_with` call.
|
|
63
|
+
On a Dispatch delivery it is a `command:` line: run that `dispatch` command with your text in place of `...`.
|
|
64
|
+
A delivery reading `expects_reply: false` carries no such call, and asks for no answer: where you answer one anyway, address it from `from`, or, on a Dispatch frame, from what its `dispatch` block names.
|
|
65
|
+
|
|
66
|
+
- A peer's message is its sender's view at `at`, not the current state.
|
|
67
|
+
Before you wait on, act on or repeat what it says about a third thing (a deploy pending, a PR held, an ask unanswered), read that thing's live source, because the source may have changed since `at`:
|
|
68
|
+
the deployment's status, the issue's events, `dispatch open-asks`.
|
|
69
|
+
|
|
39
70
|
- Every field of a rendered delivery, and who receives a publish: `skill://envoy/references/deliveries.md`.
|
|
40
71
|
|
|
41
72
|
## Whether a session is live
|
|
@@ -60,24 +91,67 @@ Every topic starts with `notifications.`.
|
|
|
60
91
|
| Your own inbox | `agent.<session_id>` |
|
|
61
92
|
| Slack, Ghost Wispr, WhatsApp | `skill://envoy/references/sources.md` |
|
|
62
93
|
|
|
63
|
-
- Write the owner, the repository, a branch or tag name and a workflow file as one token each, with every dot written `_`:
|
|
64
|
-
|
|
65
|
-
|
|
94
|
+
- Write the owner, the repository, a branch or tag name and a workflow file as one token each, with every dot written `_`:
|
|
95
|
+
`acme/site.io` is `github.acme.site_io`, and `ci.yml` is `workflow.ci_yml`.
|
|
96
|
+
A slash stays.
|
|
97
|
+
A topic spelled with the dot receives nothing.
|
|
98
|
+
|
|
99
|
+
- Subscribe to the pull request's whole family, `pr.<n>.>`.
|
|
100
|
+
Envoy also subscribes you to the lifecycle subject `pr.<n>`, which NATS `>` alone does not match, because `>` needs one or more trailing tokens.
|
|
101
|
+
|
|
102
|
+
- Read the `warnings` that `envoy_subscribe` returns, and confirm the active topics with `envoy_list`.
|
|
103
|
+
A warning names the spelling Envoy publishes, or a token that is not a GitHub topic kind; fix the topic it names.
|
|
104
|
+
"No GitHub event ... in the stream's retention window" means nothing came from that repository in the last 72 hours:
|
|
105
|
+
check that the GitHub App that sends Envoy its webhooks is installed on that repository before you rely on a wake.
|
|
66
106
|
|
|
67
107
|
## Wait for CI or a merge
|
|
68
108
|
|
|
69
|
-
- Subscribe to `github.<owner>.<repo>.pr.<n>.>` and end the turn.
|
|
70
|
-
|
|
71
|
-
|
|
109
|
+
- Subscribe to `github.<owner>.<repo>.pr.<n>.>` and end the turn.
|
|
110
|
+
Never start a `gh` poller: the event wakes you, and a poller burns turns.
|
|
111
|
+
|
|
112
|
+
- One `pr.<n>.checks` event arrives per commit whose checks settle, the head or not.
|
|
113
|
+
Compare its `sha` with the head you wait on, because an earlier commit's settlement can arrive first.
|
|
114
|
+
A commit whose message carries one of GitHub's skip keywords (`[skip ci]` and its kin) starts no workflow, so no settlement of it may ever arrive.
|
|
115
|
+
|
|
116
|
+
- Settlement waits until the commit has had no check event for 5 seconds (`ENVOY_CI_DEBOUNCE`) and every reported check run and check suite of it has finished.
|
|
117
|
+
No event arrives before that.
|
|
118
|
+
Envoy does not read GitHub's required-checks list, so a green settlement does not mean GitHub will merge the PR.
|
|
119
|
+
|
|
72
120
|
- A `pr.<n>` event with `closed` and `merged: true` means the PR merged; it carries `merge_commit_sha`, `merged_by` and `head_sha`.
|
|
121
|
+
|
|
73
122
|
- Code that orders settlements of one commit follows `skill://envoy/references/check-settlement.md`.
|
|
74
|
-
|
|
123
|
+
|
|
124
|
+
- A Legion implementer waits this way only on a re-run of a red check its task names.
|
|
125
|
+
Otherwise it pushes and completes its phase, and the daemon reads CI for it (`skill://legion-worker`).
|
|
75
126
|
|
|
76
127
|
## Roles and team topics
|
|
77
128
|
|
|
78
129
|
- Send to one session with `envoy_send`, to the one session that holds a named duty with a role, and to a group with a team topic.
|
|
79
|
-
|
|
80
|
-
-
|
|
81
|
-
|
|
130
|
+
|
|
131
|
+
- Publish to a role with `envoy_publish(topic="notifications.role.<role>", ...)`;
|
|
132
|
+
never subscribe to a role topic, because the listener forwards a role message to its holder only.
|
|
133
|
+
A publish returns once Envoy's stream has stored the forward for the holder, with the live `holder` and its `holder_state`.
|
|
134
|
+
When nobody holds the role, Envoy keeps the message for the session that claims the role next: the answer says it was kept, and that session is handed it once, within one hour of the keep.
|
|
135
|
+
A holder that is not `live` returns an error instead: that refusal names the holder, its `holder_state` (`unresponsive` or `gone`), its `last_seen` and `claim_released: false`, and nothing was forwarded and nothing kept, so publish the message again once the holder answers rather than starting a replacement for it or taking anything from it.
|
|
136
|
+
A forward the stream did not store for a live holder within about 4 seconds errs too, and you see that one after about 8 seconds, because the client tries once more.
|
|
137
|
+
No error releases the role's claim: a holder whose `holder_state` is `unresponsive` keeps its role.
|
|
138
|
+
`envoy_role_get(role=...)` finds the holder first.
|
|
139
|
+
The forward is kept on the holder's inbox, so the holder's session gets it once from its own durable consumer, also after a restart, a reconnect, or while it was not running.
|
|
140
|
+
|
|
141
|
+
- A message that must reach more than 4 role topics is the project controller's to send, not yours.
|
|
142
|
+
Each publish wakes a session, and a woken session reads its whole context again, which the provider charges as uncached input: a mean of 576,083 tokens a wake, a median of 493,636 and a ninetieth percentile of 1,327,888.
|
|
143
|
+
The controller is also the one session that can read whether a wave of wakes is already running, and a fan-out sent into a daemon restart or a pod roll is what puts an Anthropic organization over its per-minute uncached-input cap.
|
|
144
|
+
Send the controller the topics and the text, and it sends them at its own rate (`skill://legion-controller`).
|
|
145
|
+
|
|
146
|
+
- For a team, the group subscribes to `notifications.team.<name>.>`, and any member publishes to `notifications.team.<name>.all`.
|
|
147
|
+
Envoy's stream keeps a team topic, so a publish returns once it is stored and every session that follows the topic gets it once from its own consumer
|
|
148
|
+
— yours too, when you subscribe — including a session that was not running then, for up to 72 hours.
|
|
149
|
+
A session that stopped without unregistering is named nowhere and fails nothing: its copy waits in its consumer until it answers again.
|
|
150
|
+
A team topic is not kept for a session that subscribes later: a consumer starts where its session is, not at the beginning.
|
|
151
|
+
Every other `notifications.` topic no source in the table uses is refused by name, at publish and at subscribe, because no session would ever receive it.
|
|
152
|
+
A topic outside `notifications.` is refused at publish alone: you may subscribe to one, and nothing is stored under it, so you receive nothing on it.
|
|
153
|
+
|
|
82
154
|
- How a role claim survives a resume, a fork or a dead process: `skill://envoy/references/roles.md`.
|
|
83
|
-
|
|
155
|
+
|
|
156
|
+
- In a Legion pane, the plugin claims your role for you, at your session's first turn;
|
|
157
|
+
read `skill://envoy/references/legion.md` before you claim, release or publish to a role, or answer a role delivery failure there.
|
|
@@ -35,32 +35,88 @@ envoy:
|
|
|
35
35
|
```
|
|
36
36
|
|
|
37
37
|
- `to`: the local inbox that receives this delivery.
|
|
38
|
-
|
|
39
|
-
- `
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
- `
|
|
38
|
+
|
|
39
|
+
- `from`: the sending session's self-asserted id, with its session title in parentheses when the envelope carries one.
|
|
40
|
+
Attribution and a direct-reply target, not an authenticated identity.
|
|
41
|
+
|
|
42
|
+
- `at`: when the server that accepted the delivery took it, by that server's clock:
|
|
43
|
+
the Envoy listener the sender sent through for a session's message or a webhook event, Dispatch's server for a Dispatch event.
|
|
44
|
+
The sending session sets no time of its own; its machine's clock counts only where that listener runs on the same machine.
|
|
45
|
+
It is not the present time: a delivery read late carries an old `at`.
|
|
46
|
+
|
|
47
|
+
- `delivered`: when this session got the delivery, by this machine's clock.
|
|
48
|
+
An Oh My Pi session shows it in every model request, beside `at`, or beside `published_at` on a notice that names a message the session could not be shown.
|
|
49
|
+
A session the host moves to shows when it got the delivery there, which can be later than another session got it.
|
|
50
|
+
A session that got the delivery before its process restarted can show an estimate of that time, taken from its transcript, never later than the delivery.
|
|
51
|
+
|
|
52
|
+
- `waited`: present when `delivered` is 15 minutes or more after the send time, less any clock difference `clocks` states: how long the delivery waited, in words.
|
|
53
|
+
An old send time on a late delivery says the delivery waited, not that a clock is wrong.
|
|
54
|
+
|
|
55
|
+
- `clocks`: present when this machine's clock runs 15 minutes or more ahead of or behind the Envoy listener's, read from the listener's replies, or when `delivered` is 15 minutes or more before the send time less that difference:
|
|
56
|
+
which way the clocks differ, and by how much.
|
|
57
|
+
Only this machine's clock and the listener's are measured, so a send time still after the delivery once the measured difference is taken off says the sending server's own clock runs ahead of the listener's.
|
|
58
|
+
A difference under 15 minutes is not shown and stays in `waited`.
|
|
59
|
+
|
|
43
60
|
- `id`: the delivery id; pass it as `in_reply_to` when you reply.
|
|
61
|
+
|
|
44
62
|
- `by`: the expiry deadline, when the sender set one.
|
|
45
|
-
|
|
46
|
-
- `
|
|
47
|
-
|
|
63
|
+
|
|
64
|
+
- `urgency`: how soon the sender asks you to act: `low`, `med`, `high` or `blocking`.
|
|
65
|
+
It is for you to read only: it does not change when a delivery reaches you, or its place among others.
|
|
66
|
+
|
|
67
|
+
- `expects_reply`: what the sender wants back.
|
|
68
|
+
`false` means nothing: do not reply, and do not acknowledge.
|
|
69
|
+
`optional` means a reply is welcome and nothing waits on it.
|
|
70
|
+
`required` means the sender waits on your answer.
|
|
71
|
+
|
|
72
|
+
- `re`: the delivery this one replies to.
|
|
73
|
+
A Dispatch frame names it as a `dispatch://` ref:
|
|
74
|
+
the ask (`dispatch://KEY/ask/<id>`, or `dispatch://PROJECT/artifact/<slug>/ask/<id>` for a document ask) or the message (`dispatch://KEY/message/<id>`), never the quoted text.
|
|
75
|
+
On an ask, `dispatch.question` holds the start of the question's text; `dispatch read` gives the whole.
|
|
76
|
+
|
|
48
77
|
- `supersedes`: an earlier delivery this one replaces; act on this one in its place.
|
|
49
|
-
|
|
50
|
-
- `
|
|
78
|
+
|
|
79
|
+
- `reply_with`: the direct-reply call to the sender.
|
|
80
|
+
On a Dispatch frame it is `command:`, the `dispatch` command line that answers it, with `...` where your text goes.
|
|
81
|
+
It is absent when `expects_reply` is `false`: a delivery that wants nothing back offers no way to send something.
|
|
82
|
+
Where you answer one anyway, the card still names what you need — a Dispatch frame names its comment, message or ask, and an agent's delivery names its `id` and its sender.
|
|
83
|
+
|
|
84
|
+
- `reply_role`: the role-publish reply call, when the sender holds a role.
|
|
85
|
+
It is absent under `false` for the same reason.
|
|
86
|
+
|
|
51
87
|
- `summary`: the one-line source summary.
|
|
88
|
+
|
|
52
89
|
- `message`: the complete payload; it can hold several paragraphs.
|
|
90
|
+
|
|
53
91
|
- `note`: the payload names another session; never use that quoted id as the recipient.
|
|
54
|
-
|
|
92
|
+
|
|
93
|
+
- `unrecognised`: present when the payload failed validation or came from an unknown source.
|
|
94
|
+
It names what failed, not every unknown key.
|
|
55
95
|
|
|
56
96
|
## Who receives a publish
|
|
57
97
|
|
|
58
98
|
It depends on where a session takes its deliveries, not on whether the topic is built in or your own.
|
|
59
99
|
|
|
60
|
-
- An Oh My Pi or Claude Code session takes its messages from a durable consumer the listener keeps for it on Envoy's stream and relays to it in order, acknowledging each one only once the session wrote it.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
100
|
+
- An Oh My Pi or Claude Code session takes its messages from a durable consumer the listener keeps for it on Envoy's stream and relays to it in order, acknowledging each one only once the session wrote it.
|
|
101
|
+
So every message the stream keeps — your inbox, and each topic you follow with `envoy_subscribe` — reaches you once, whoever sent it:
|
|
102
|
+
your own publish included, and a subagent's publish to a topic its parent follows.
|
|
103
|
+
One published while you were restarting, disconnected or not running reaches you when you are back, up to 72 hours later.
|
|
104
|
+
A session does not take its messages from a NATS subscription of its own; `packages/envoy/AGENTS.md`, "Session consumers", is the rule.
|
|
105
|
+
|
|
106
|
+
- A team topic (`notifications.team.<name>`) is kept too, so it reaches every session that follows it with `envoy_subscribe`, once, including one that was not running when it was published, for up to 72 hours.
|
|
107
|
+
A session that subscribes later gets nothing published before it followed.
|
|
108
|
+
|
|
109
|
+
- A role is not a subject its holder subscribes to:
|
|
110
|
+
the listener finds the live holder and forwards the message to it, and drops the message when that holder is the message's source session.
|
|
111
|
+
The stream keeps the forward on the holder's inbox, so the holder's session gets it once, also after a restart or while it was not running.
|
|
112
|
+
No session answers a receipt for one: a publish returns once the stream stored the forward.
|
|
113
|
+
A role message no live holder can take is kept for the session that claims the role next, which is handed it once, within one hour of the keep.
|
|
114
|
+
|
|
115
|
+
- Every other `notifications.` topic, one no source uses and the stream does not keep, is refused by name
|
|
116
|
+
— at `envoy_publish` and at `envoy_subscribe` alike — because no session would ever receive it.
|
|
117
|
+
Use a team topic instead.
|
|
118
|
+
Your session is never left unregistered by one:
|
|
119
|
+
a registration that carries such a topic from an older subscription of yours keeps its other topics and names that one in a warning.
|
|
64
120
|
|
|
65
121
|
## Listener API errors
|
|
66
122
|
|
|
@@ -2,14 +2,40 @@
|
|
|
2
2
|
|
|
3
3
|
Read this in a Legion pane before you touch a role claim, or when a Legion role message fails.
|
|
4
4
|
|
|
5
|
-
- The Legion plugin claims your assigned role for you, through a daemon-minted role token:
|
|
5
|
+
- The Legion plugin claims your assigned role for you, through a daemon-minted role token:
|
|
6
|
+
it registers your session and reports it ready at session start, and takes the role at your first turn, or at a bound (30 s by default) when no turn comes first.
|
|
7
|
+
Never call `envoy_role_set` and never subscribe to your role topic yourself, because the claim is already made (`packages/pi-shared/src/legion-claim-core.ts`).
|
|
8
|
+
|
|
6
9
|
- Never build a role token from an issue number or title: only the daemon maps tokens to issues.
|
|
7
|
-
|
|
8
|
-
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
|
|
11
|
+
- A Legion claim is last-claim-wins.
|
|
12
|
+
It survives parking and worker re-creation, and stays through the issue's linger after close (the deployment's `linger_hours`).
|
|
13
|
+
|
|
14
|
+
- `no_holder`, `holder_lapsed` and `delivery_failed` on a Legion role are the daemon's liveness signals.
|
|
15
|
+
Never add a second subscriber or retry by hand:
|
|
16
|
+
the daemon revives or recreates the worker and re-delivers, or relaunches the root process, which gets a `catch-up` notice of its tree and reads each issue's handoffs on its Dispatch issue (`dispatch handoff-read`).
|
|
17
|
+
|
|
18
|
+
- `no_holder`: no session holds the role's claim.
|
|
19
|
+
Only the reaper releases a claim, once its holder is `gone`;
|
|
20
|
+
a Legion role whose agent process exits is `gone` at once, so its successor's soft claim takes the role without waiting out the live window.
|
|
21
|
+
|
|
22
|
+
- `holder_lapsed`: a session holds the claim but has not registered within the live window, so the listener refused the publish `404` and forwarded nothing.
|
|
23
|
+
The error names the holder, its `holder_state` and its `last_seen`, and says `claim_released: false`.
|
|
24
|
+
An `unresponsive` holder is not dead: it keeps the role, nothing replaces it, and the daemon holds the packet and publishes it again.
|
|
25
|
+
|
|
26
|
+
- `delivery_failed`: the stream stored no forward for the holder:
|
|
27
|
+
the holder lookup failed, the publish errored, or the stream did not store the forward within the send's window.
|
|
28
|
+
Every forward goes into the stream, so a holder that is live but not reading is forwarded to like any other and its own consumer reads the message when it answers again;
|
|
29
|
+
nothing is published live, and a holder cannot have seen a message the stream never stored.
|
|
30
|
+
It releases no claim.
|
|
31
|
+
|
|
32
|
+
- No `receipt_timeout`: this release's listener waits for no receipt, so no signal here says a holder was slow.
|
|
33
|
+
A role send answered `200` says that the stream stored the forward for the holder, and a forward the stream did not store is `delivery_failed`.
|
|
34
|
+
|
|
13
35
|
- A role message forwarded to you is kept on your inbox in the stream, which your own durable consumer reads, so your session gets it once, also after the pane restarts or reconnects.
|
|
36
|
+
|
|
14
37
|
- Never forward these failure payloads to an architect, because the daemon already acts on them.
|
|
15
|
-
|
|
38
|
+
|
|
39
|
+
- A role send the listener refuses `404` also raises one exception on `notifications.envoy.exceptions.notifications.role.<role>`, with the reason it refused: `unclaimed` or `holder_lapsed`.
|
|
40
|
+
It is a bus signal and nothing more: no consumer acts on a peer's refusal, and the daemon's own refused outbox row retries by itself until the role is held or its architect cannot hold it again.
|
|
41
|
+
Never re-send a refused message by hand on the strength of one.
|
|
@@ -15,7 +15,7 @@ import { AgentStreamPublisher } from "@legion/pi-shared/agent-stream"
|
|
|
15
15
|
import type { ModConfigRow, ModEffort, ModEvent } from "../hooks/mod-protocol"
|
|
16
16
|
import { claudeAgentStreamCommands, createClaudeAgentStream } from "./agent-stream-claude"
|
|
17
17
|
import { rememberBounded } from "./bounded-set"
|
|
18
|
-
import type {
|
|
18
|
+
import type { ChannelConnection, ChannelTopicSubscription } from "./channel-topics"
|
|
19
19
|
import type { ModLink } from "./mod-link"
|
|
20
20
|
|
|
21
21
|
const encoder = new TextEncoder()
|
|
@@ -47,7 +47,7 @@ export interface AgentStreamOptions {
|
|
|
47
47
|
readonly link: ModLink
|
|
48
48
|
/** The session's own NATS connection: the frames go out on it, and the control subject is
|
|
49
49
|
* taken on it. The session's NATS user may already publish and subscribe to both. */
|
|
50
|
-
readonly connection:
|
|
50
|
+
readonly connection: ChannelConnection
|
|
51
51
|
/** The session id the Agents page knows this session by, which both subjects name. */
|
|
52
52
|
readonly sessionId: string
|
|
53
53
|
/**
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { RelayHeaders } from "@legion/envoy-client/consumer-delivery"
|
|
2
|
+
import { DedupeIdentitySchema } from "@legion/envoy-client/delivery"
|
|
3
|
+
import { messageFor } from "@legion/envoy-client/errors"
|
|
4
|
+
import { expandSubscriptionTopics } from "@legion/envoy-client/transport"
|
|
5
|
+
import { z } from "zod"
|
|
6
|
+
|
|
7
|
+
/** A message as the broker yields it (a nats.js `Msg` satisfies this). */
|
|
8
|
+
export interface ChannelBrokerMessage {
|
|
9
|
+
readonly subject: string
|
|
10
|
+
readonly data: Uint8Array
|
|
11
|
+
/**
|
|
12
|
+
* The message's reply subject. Two kinds of message carry one: a relay frame, whose reply is
|
|
13
|
+
* the relay's own answer token (`RelayMessage`, consumer delivery), and a request on the live
|
|
14
|
+
* view's control subject, answered there (`./agent-stream-runner`). No Envoy envelope is
|
|
15
|
+
* answered on a reply: a session publishes no receipt.
|
|
16
|
+
*/
|
|
17
|
+
readonly reply?: string
|
|
18
|
+
/** The message's NATS headers; only a relay frame (consumer delivery) carries any read here. */
|
|
19
|
+
readonly headers?: RelayHeaders
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ChannelTopicSubscription extends AsyncIterable<ChannelBrokerMessage> {
|
|
23
|
+
unsubscribe(): void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A nats.js connection status event; only its `type` is read. */
|
|
27
|
+
export interface ChannelConnectionStatus {
|
|
28
|
+
readonly type: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The NATS connection surface a channel session holds. */
|
|
32
|
+
export interface ChannelConnection {
|
|
33
|
+
/** Subscribes one subject. A channel session opens two: the session's relay subject, where
|
|
34
|
+
* every Envoy message arrives, and the live view's control subject, which a viewer takes
|
|
35
|
+
* the session on (`./agent-stream-runner`). */
|
|
36
|
+
subscribe(subject: string): ChannelTopicSubscription
|
|
37
|
+
publish(subject: string, data: Uint8Array): void
|
|
38
|
+
flush(): Promise<void>
|
|
39
|
+
isClosed(): boolean
|
|
40
|
+
drain(): Promise<void>
|
|
41
|
+
close(): Promise<void>
|
|
42
|
+
/** The connection's status events (nats.js `status()`), which end when it closes. */
|
|
43
|
+
status(): AsyncIterable<ChannelConnectionStatus>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The topics this session follows, and the one broker connection it holds. Nothing is subscribed
|
|
48
|
+
* for a topic: the listener relays every followed subject from the session's own durable consumer
|
|
49
|
+
* (`ConsumerDelivery`), so a topic is followed by registering it, and the only subscription that
|
|
50
|
+
* carries Envoy traffic is that consumer's relay subject.
|
|
51
|
+
*/
|
|
52
|
+
export interface ChannelTopics {
|
|
53
|
+
/** Start following a topic; one already followed is left alone. */
|
|
54
|
+
follow(topic: string): void
|
|
55
|
+
/** Stop following the given topics, or every topic when the list is empty. */
|
|
56
|
+
unfollow(topics: readonly string[]): void
|
|
57
|
+
/** Topics currently followed, in follow order. */
|
|
58
|
+
topics(): readonly string[]
|
|
59
|
+
/** True once the connection is closed for good. */
|
|
60
|
+
isClosed(): boolean
|
|
61
|
+
/** Drain the NATS connection; never rejects. */
|
|
62
|
+
close(): Promise<void>
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ChannelTopicsOptions {
|
|
66
|
+
/** How long close waits for a broker drain before closing outright. */
|
|
67
|
+
readonly drainTimeoutMs?: number
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// A delivery's identity fields parse independently: a field the shared rule refuses identifies
|
|
71
|
+
// nothing, and its valid sibling can still recognise a repeat. `topic` follows the same rule
|
|
72
|
+
// before it is exposed as the envelope topic.
|
|
73
|
+
const DeliveryIdentity = DedupeIdentitySchema.extend({
|
|
74
|
+
event_id: DedupeIdentitySchema.shape.event_id.catch(undefined),
|
|
75
|
+
dedupe_key: DedupeIdentitySchema.shape.dedupe_key.catch(undefined),
|
|
76
|
+
source: DedupeIdentitySchema.shape.source.catch(undefined),
|
|
77
|
+
source_event_id: DedupeIdentitySchema.shape.source_event_id.catch(undefined),
|
|
78
|
+
topic: z.string().min(1).optional().catch(undefined),
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const DEFAULT_DRAIN_TIMEOUT_MS = 1_000
|
|
82
|
+
|
|
83
|
+
/** A frame's identity fields, each kept only when valid; undefined for a frame that is not JSON. */
|
|
84
|
+
export function deliveryIdentity(raw: string): z.infer<typeof DeliveryIdentity> | undefined {
|
|
85
|
+
let parsed: unknown
|
|
86
|
+
try {
|
|
87
|
+
parsed = JSON.parse(raw)
|
|
88
|
+
} catch {
|
|
89
|
+
return undefined
|
|
90
|
+
}
|
|
91
|
+
const identity = DeliveryIdentity.safeParse(parsed)
|
|
92
|
+
return identity.success ? identity.data : undefined
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function report(what: string, error: unknown): void {
|
|
96
|
+
process.stderr.write(`envoy-channel: ${what} — ${messageFor(error)}\n`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* One channel process follows its direct Envoy route and every topic a human or a tool added to
|
|
101
|
+
* it. Each is a concrete subject the registration carries (`expandSubscriptionTopics`), and the
|
|
102
|
+
* listener relays all of them from the session's own consumer, so nothing here subscribes: the
|
|
103
|
+
* set is what a registration advertises and what the relay is filtered on.
|
|
104
|
+
*/
|
|
105
|
+
export function createChannelTopics(
|
|
106
|
+
connection: ChannelConnection,
|
|
107
|
+
options: ChannelTopicsOptions = {},
|
|
108
|
+
): ChannelTopics {
|
|
109
|
+
const following = new Set<string>()
|
|
110
|
+
const drainTimeoutMs = options.drainTimeoutMs ?? DEFAULT_DRAIN_TIMEOUT_MS
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
follow(topic) {
|
|
114
|
+
for (const subject of expandSubscriptionTopics([topic])) following.add(subject)
|
|
115
|
+
},
|
|
116
|
+
unfollow(topics) {
|
|
117
|
+
if (topics.length === 0) {
|
|
118
|
+
following.clear()
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
for (const subject of expandSubscriptionTopics(topics)) following.delete(subject)
|
|
122
|
+
},
|
|
123
|
+
topics() {
|
|
124
|
+
return [...following]
|
|
125
|
+
},
|
|
126
|
+
isClosed() {
|
|
127
|
+
return connection.isClosed()
|
|
128
|
+
},
|
|
129
|
+
async close() {
|
|
130
|
+
following.clear()
|
|
131
|
+
if (connection.isClosed()) return
|
|
132
|
+
const timeout = Promise.withResolvers<void>()
|
|
133
|
+
const timer = setTimeout(timeout.resolve, drainTimeoutMs)
|
|
134
|
+
try {
|
|
135
|
+
await Promise.race([connection.drain(), timeout.promise])
|
|
136
|
+
} catch (error) {
|
|
137
|
+
report("draining the broker connection failed", error)
|
|
138
|
+
}
|
|
139
|
+
clearTimeout(timer)
|
|
140
|
+
if (connection.isClosed()) return
|
|
141
|
+
try {
|
|
142
|
+
await connection.close()
|
|
143
|
+
} catch (error) {
|
|
144
|
+
report("closing the broker connection failed", error)
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
}
|