@sjawhar/claude-envoy 4.6.0 → 4.6.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/dist/codex-tool-hook.js +2 -3
- package/dist/envoy-channel.js +42 -20
- package/package.json +1 -1
- package/src/codex/codex-delivery.ts +71 -28
- package/src/codex/codex-inbox.ts +10 -6
package/.omp-plugin/plugin.json
CHANGED
package/dist/codex-tool-hook.js
CHANGED
|
@@ -8,15 +8,14 @@ function codexInboxDirectory(stateDirectory, sessionId) {
|
|
|
8
8
|
return join(stateDirectory, "codex-inbox", encodeURIComponent(sessionId));
|
|
9
9
|
}
|
|
10
10
|
var lastOrder = 0;
|
|
11
|
-
function
|
|
11
|
+
function inboxItemName() {
|
|
12
12
|
const order = Math.max(Date.now(), lastOrder + 1);
|
|
13
13
|
lastOrder = order;
|
|
14
14
|
return `${String(order).padStart(15, "0")}-${randomBytes(4).toString("hex")}.json`;
|
|
15
15
|
}
|
|
16
|
-
async function putInbox(directory, item) {
|
|
16
|
+
async function putInbox(directory, item, name = inboxItemName()) {
|
|
17
17
|
const waiting = join(directory, "waiting");
|
|
18
18
|
await mkdir(waiting, { recursive: true, mode: 448 });
|
|
19
|
-
const name = itemName();
|
|
20
19
|
const temporary = join(directory, `.${name}.tmp`);
|
|
21
20
|
await writeFile(temporary, JSON.stringify(item), { mode: 384 });
|
|
22
21
|
await rename(temporary, join(waiting, name));
|
package/dist/envoy-channel.js
CHANGED
|
@@ -57868,7 +57868,7 @@ var import_nats2 = __toESM(require_mod4(), 1);
|
|
|
57868
57868
|
|
|
57869
57869
|
// package.json
|
|
57870
57870
|
var name2 = "@sjawhar/claude-envoy";
|
|
57871
|
-
var version2 = "4.6.
|
|
57871
|
+
var version2 = "4.6.1";
|
|
57872
57872
|
var legion = {
|
|
57873
57873
|
daemonApiVersion: 160
|
|
57874
57874
|
};
|
|
@@ -63253,15 +63253,14 @@ function codexInboxDirectory(stateDirectory, sessionId) {
|
|
|
63253
63253
|
return join14(stateDirectory, "codex-inbox", encodeURIComponent(sessionId));
|
|
63254
63254
|
}
|
|
63255
63255
|
var lastOrder = 0;
|
|
63256
|
-
function
|
|
63256
|
+
function inboxItemName() {
|
|
63257
63257
|
const order = Math.max(Date.now(), lastOrder + 1);
|
|
63258
63258
|
lastOrder = order;
|
|
63259
63259
|
return `${String(order).padStart(15, "0")}-${randomBytes3(4).toString("hex")}.json`;
|
|
63260
63260
|
}
|
|
63261
|
-
async function putInbox(directory, item) {
|
|
63261
|
+
async function putInbox(directory, item, name = inboxItemName()) {
|
|
63262
63262
|
const waiting = join14(directory, "waiting");
|
|
63263
63263
|
await mkdir3(waiting, { recursive: true, mode: 448 });
|
|
63264
|
-
const name = itemName();
|
|
63265
63264
|
const temporary = join14(directory, `.${name}.tmp`);
|
|
63266
63265
|
await writeFile2(temporary, JSON.stringify(item), { mode: 384 });
|
|
63267
63266
|
await rename2(temporary, join14(waiting, name));
|
|
@@ -63386,6 +63385,8 @@ function createCodexDelivery(options) {
|
|
|
63386
63385
|
}
|
|
63387
63386
|
};
|
|
63388
63387
|
const sweep = () => {
|
|
63388
|
+
if (stopped)
|
|
63389
|
+
return sweeping;
|
|
63389
63390
|
sweeping = sweeping.then(sweepOnce).catch((error) => {
|
|
63390
63391
|
options.warn(`the Codex inbox could not be swept, and will be again: ${messageFor(error)}`);
|
|
63391
63392
|
});
|
|
@@ -63419,13 +63420,30 @@ function createCodexDelivery(options) {
|
|
|
63419
63420
|
return;
|
|
63420
63421
|
}
|
|
63421
63422
|
const directory = options.inboxDirectory(options.sessionId());
|
|
63422
|
-
const name =
|
|
63423
|
-
const written =
|
|
63424
|
-
|
|
63423
|
+
const name = inboxItemName();
|
|
63424
|
+
const written = Promise.withResolvers();
|
|
63425
|
+
written.promise.catch(() => {
|
|
63426
|
+
return;
|
|
63425
63427
|
});
|
|
63426
|
-
|
|
63428
|
+
const landed = Promise.withResolvers();
|
|
63429
|
+
waiting.set(name, {
|
|
63430
|
+
directory,
|
|
63431
|
+
item,
|
|
63432
|
+
landed: landed.promise,
|
|
63433
|
+
resolve: written.resolve,
|
|
63434
|
+
reject: written.reject
|
|
63435
|
+
});
|
|
63436
|
+
try {
|
|
63437
|
+
await putInbox(directory, item, name);
|
|
63438
|
+
landed.resolve(true);
|
|
63439
|
+
} catch (error) {
|
|
63440
|
+
landed.resolve(false);
|
|
63441
|
+
waiting.delete(name);
|
|
63442
|
+
throw error;
|
|
63443
|
+
}
|
|
63444
|
+
if (!running && !ended)
|
|
63427
63445
|
sweep();
|
|
63428
|
-
await written;
|
|
63446
|
+
await written.promise;
|
|
63429
63447
|
},
|
|
63430
63448
|
turnStarted() {
|
|
63431
63449
|
running = true;
|
|
@@ -63453,17 +63471,21 @@ function createCodexDelivery(options) {
|
|
|
63453
63471
|
stop() {
|
|
63454
63472
|
stopped = true;
|
|
63455
63473
|
clearInterval(timer);
|
|
63456
|
-
|
|
63457
|
-
|
|
63458
|
-
|
|
63459
|
-
|
|
63460
|
-
|
|
63461
|
-
|
|
63462
|
-
if (await
|
|
63463
|
-
|
|
63464
|
-
|
|
63465
|
-
|
|
63466
|
-
|
|
63474
|
+
return sweeping.then(() => {
|
|
63475
|
+
const left = [...waiting];
|
|
63476
|
+
waiting.clear();
|
|
63477
|
+
return Promise.all(left.map(async ([name, waiter]) => {
|
|
63478
|
+
if (!await waiter.landed)
|
|
63479
|
+
return;
|
|
63480
|
+
if (await withdrawWaiting(waiter.directory, name)) {
|
|
63481
|
+
waiter.reject(new Error("the Codex session's delivery stopped before the message was taken"));
|
|
63482
|
+
} else {
|
|
63483
|
+
if (await takenBy(waiter.directory, name) === "hook")
|
|
63484
|
+
options.taken?.(waiter.item);
|
|
63485
|
+
waiter.resolve();
|
|
63486
|
+
}
|
|
63487
|
+
}));
|
|
63488
|
+
}).then(() => {
|
|
63467
63489
|
return;
|
|
63468
63490
|
});
|
|
63469
63491
|
}
|
package/package.json
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { messageFor } from "@legion/envoy-client/errors"
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
type CodexInboxItem,
|
|
4
|
+
inboxItemName,
|
|
5
|
+
putInbox,
|
|
6
|
+
takeInbox,
|
|
7
|
+
takenBy,
|
|
8
|
+
withdrawWaiting,
|
|
9
|
+
} from "./codex-inbox"
|
|
3
10
|
|
|
4
11
|
/**
|
|
5
12
|
* Puts a line into a Codex session's conversation.
|
|
@@ -19,11 +26,12 @@ import { type CodexInboxItem, putInbox, takeInbox, takenBy, withdrawWaiting } fr
|
|
|
19
26
|
* the Envoy frame it carries is not answered and the listener sends it again.
|
|
20
27
|
*
|
|
21
28
|
* Every write settles, because the session's frames are written one behind another and one that
|
|
22
|
-
* never settled would hold back every later one: a
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
29
|
+
* never settled would hold back every later one: a write's waiter is in place before its line can
|
|
30
|
+
* be seen, so whatever takes the line, a sweep or the hook, finds the write to settle; a line the
|
|
31
|
+
* hook did not take is queued at the turn's end, and, while no turn runs, by a sweep every
|
|
32
|
+
* `pollMs`; a sweep looks at the inbox of every session a line still waits in, so a `/new` that
|
|
33
|
+
* moves the session to another id while a line waits for the old one queues it for the new one;
|
|
34
|
+
* and a sweep that fails is logged and tried again rather than ending the ones after it.
|
|
27
35
|
*/
|
|
28
36
|
export interface CodexDeliveryOptions {
|
|
29
37
|
/** The session the `codex` process runs now, as its record names it. */
|
|
@@ -60,7 +68,9 @@ export interface CodexDelivery {
|
|
|
60
68
|
/** Whether a turn runs, as the transcript last said. */
|
|
61
69
|
turnRunning(): boolean
|
|
62
70
|
/** Stops watching waiting lines; a write still waiting rejects. Resolves once every waiting line
|
|
63
|
-
* is withdrawn or found taken
|
|
71
|
+
* is withdrawn or found taken: a line still being written is withdrawn once it is in place, and
|
|
72
|
+
* a sweep already running first settles the writes of the lines it takes. No sweep starts after
|
|
73
|
+
* this. */
|
|
64
74
|
stop(): Promise<void>
|
|
65
75
|
}
|
|
66
76
|
|
|
@@ -69,6 +79,9 @@ const WAITING_POLL_MS = 250
|
|
|
69
79
|
interface Waiter {
|
|
70
80
|
readonly directory: string
|
|
71
81
|
readonly item: CodexInboxItem
|
|
82
|
+
/** Whether the line's file is in place: false when writing it failed, and the write then rejects
|
|
83
|
+
* with that failure itself. */
|
|
84
|
+
readonly landed: Promise<boolean>
|
|
72
85
|
readonly resolve: () => void
|
|
73
86
|
readonly reject: (error: Error) => void
|
|
74
87
|
}
|
|
@@ -103,6 +116,9 @@ export function createCodexDelivery(options: CodexDeliveryOptions): CodexDeliver
|
|
|
103
116
|
}
|
|
104
117
|
}
|
|
105
118
|
const sweep = (): Promise<void> => {
|
|
119
|
+
// Once stop() has run, every line left is stop()'s to withdraw or find taken: a sweep that
|
|
120
|
+
// queued one now would leave stop() a line it cannot withdraw and whose queueing it never saw.
|
|
121
|
+
if (stopped) return sweeping
|
|
106
122
|
sweeping = sweeping.then(sweepOnce).catch((error: unknown) => {
|
|
107
123
|
options.warn(`the Codex inbox could not be swept, and will be again: ${messageFor(error)}`)
|
|
108
124
|
})
|
|
@@ -139,13 +155,33 @@ export function createCodexDelivery(options: CodexDeliveryOptions): CodexDeliver
|
|
|
139
155
|
return
|
|
140
156
|
}
|
|
141
157
|
const directory = options.inboxDirectory(options.sessionId())
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
158
|
+
// The waiter is in place before the write of its line starts. A waiter registered only once
|
|
159
|
+
// the write returned missed a sweep or a stop() that came between: the sweep queued the line
|
|
160
|
+
// with no write to settle, or stop() did not see it, and the write never settled.
|
|
161
|
+
const name = inboxItemName()
|
|
162
|
+
const written = Promise.withResolvers<void>()
|
|
163
|
+
// A sweep can settle the write before putInbox returns; the write is awaited below.
|
|
164
|
+
written.promise.catch(() => undefined)
|
|
165
|
+
const landed = Promise.withResolvers<boolean>()
|
|
166
|
+
waiting.set(name, {
|
|
167
|
+
directory,
|
|
168
|
+
item,
|
|
169
|
+
landed: landed.promise,
|
|
170
|
+
resolve: written.resolve,
|
|
171
|
+
reject: written.reject,
|
|
145
172
|
})
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
173
|
+
try {
|
|
174
|
+
await putInbox(directory, item, name)
|
|
175
|
+
landed.resolve(true)
|
|
176
|
+
} catch (error) {
|
|
177
|
+
landed.resolve(false)
|
|
178
|
+
waiting.delete(name)
|
|
179
|
+
throw error
|
|
180
|
+
}
|
|
181
|
+
// The turn may have ended while the line was being written, after its sweep ran. A session
|
|
182
|
+
// that ended meanwhile gets the line once it runs again, as the poll waits for that too.
|
|
183
|
+
if (!running && !ended) void sweep()
|
|
184
|
+
await written.promise
|
|
149
185
|
},
|
|
150
186
|
turnStarted() {
|
|
151
187
|
running = true
|
|
@@ -174,21 +210,28 @@ export function createCodexDelivery(options: CodexDeliveryOptions): CodexDeliver
|
|
|
174
210
|
clearInterval(timer)
|
|
175
211
|
// A refused write leaves its Envoy frame unanswered, so Envoy sends it again. Each line is
|
|
176
212
|
// withdrawn before its write is refused, so the hook cannot deliver it as well; a line the
|
|
177
|
-
// hook took first was delivered, and its write resolves.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
213
|
+
// hook took first was delivered, and its write resolves. A sweep that already started
|
|
214
|
+
// settles the writes of the lines it takes, so stop() takes what is left only once it ends.
|
|
215
|
+
return sweeping
|
|
216
|
+
.then(() => {
|
|
217
|
+
const left = [...waiting]
|
|
218
|
+
waiting.clear()
|
|
219
|
+
return Promise.all(
|
|
220
|
+
left.map(async ([name, waiter]) => {
|
|
221
|
+
// A write whose line never landed rejects with its own failure.
|
|
222
|
+
if (!(await waiter.landed)) return
|
|
223
|
+
if (await withdrawWaiting(waiter.directory, name)) {
|
|
224
|
+
waiter.reject(
|
|
225
|
+
new Error("the Codex session's delivery stopped before the message was taken"),
|
|
226
|
+
)
|
|
227
|
+
} else {
|
|
228
|
+
if ((await takenBy(waiter.directory, name)) === "hook") options.taken?.(waiter.item)
|
|
229
|
+
waiter.resolve()
|
|
230
|
+
}
|
|
231
|
+
}),
|
|
232
|
+
)
|
|
233
|
+
})
|
|
234
|
+
.then(() => undefined)
|
|
192
235
|
},
|
|
193
236
|
}
|
|
194
237
|
}
|
package/src/codex/codex-inbox.ts
CHANGED
|
@@ -34,20 +34,24 @@ export function codexInboxDirectory(stateDirectory: string, sessionId: string):
|
|
|
34
34
|
return join(stateDirectory, "codex-inbox", encodeURIComponent(sessionId))
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
/** A file name
|
|
37
|
+
/** A file name for a new line: it sorts after every name chosen before it in this process. */
|
|
38
38
|
let lastOrder = 0
|
|
39
|
-
function
|
|
39
|
+
export function inboxItemName(): string {
|
|
40
40
|
const order = Math.max(Date.now(), lastOrder + 1)
|
|
41
41
|
lastOrder = order
|
|
42
42
|
return `${String(order).padStart(15, "0")}-${randomBytes(4).toString("hex")}.json`
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
/** Writes `item` as a waiting message and answers
|
|
46
|
-
* `
|
|
47
|
-
|
|
45
|
+
/** Writes `item` as a waiting message named `name` and answers the name, which `takenBy` and
|
|
46
|
+
* `withdrawWaiting` look up. The file is renamed into place whole, so a reader never sees half of
|
|
47
|
+
* it. A caller that must be ready for the line before anyone can see it chooses `name` first. */
|
|
48
|
+
export async function putInbox(
|
|
49
|
+
directory: string,
|
|
50
|
+
item: CodexInboxItem,
|
|
51
|
+
name: string = inboxItemName(),
|
|
52
|
+
): Promise<string> {
|
|
48
53
|
const waiting = join(directory, "waiting")
|
|
49
54
|
await mkdir(waiting, { recursive: true, mode: 0o700 })
|
|
50
|
-
const name = itemName()
|
|
51
55
|
const temporary = join(directory, `.${name}.tmp`)
|
|
52
56
|
await writeFile(temporary, JSON.stringify(item), { mode: 0o600 })
|
|
53
57
|
await rename(temporary, join(waiting, name))
|