@sjawhar/claude-envoy 5.4.1 → 5.6.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/dist/codex-hook.js +386 -38
- package/dist/dispatch.js +2029 -1783
- package/dist/envoy-channel.js +388 -40
- package/dist/session-hook.js +612 -64
- package/hooks/session-hook.ts +73 -27
- package/legion/agents/plan-reviewer.md +1 -1
- package/package.json +1 -1
- package/src/codex/codex-session-record.ts +18 -10
package/hooks/session-hook.ts
CHANGED
|
@@ -116,6 +116,7 @@ import {
|
|
|
116
116
|
} from "@legion/pi-shared/jj-attribution"
|
|
117
117
|
import { z } from "zod"
|
|
118
118
|
import { claudeProjectDirectory, configuredValue } from "../src/claude-session"
|
|
119
|
+
import { procStarted } from "../src/codex/codex-session-record"
|
|
119
120
|
import { sessionHandoffFile, writeSessionHandoff } from "../src/session-identity"
|
|
120
121
|
import { recordChosenTitle } from "../src/session-state"
|
|
121
122
|
import { parseHookArguments } from "./hook-arguments"
|
|
@@ -184,9 +185,66 @@ const RefreshOutcome = z.object({
|
|
|
184
185
|
})
|
|
185
186
|
/** Tests only: the file the answers hook writes its own timing to, `{ began, printed, ended }` in
|
|
186
187
|
* ms after its process started (when its code began, when it printed or found nothing to print,
|
|
187
|
-
* and when it exits),
|
|
188
|
-
*
|
|
188
|
+
* and when it exits), `deadline`, the print deadline it applied, and `waits`, each wait it armed
|
|
189
|
+
* on the way (`armedWait`), so a test holds the hook to its deadline by what the hook did rather
|
|
190
|
+
* than by the time bun's start-up or the host's scheduling took; and `refresher`, the refresher it
|
|
191
|
+
* started (null when it started none), so a test waits on that process's end rather than on a
|
|
192
|
+
* scan of every process on the machine. The hook writes the file whole (`writeAtomically`) as
|
|
193
|
+
* soon as it started a refresher, naming it, so a test that ended the hook before it exited
|
|
194
|
+
* still knows that refresher, and again as it exits.
|
|
195
|
+
* Unset, as Claude Code runs it, the hook writes nothing and records nothing. */
|
|
189
196
|
const ANSWERS_TIMING_FILE = "ENVOY_ANSWERS_TIMING_FILE"
|
|
197
|
+
/** The refresher this hook process started, for `ANSWERS_TIMING_FILE`: its pid, and when it
|
|
198
|
+
* started (`procStarted`), which a later process given the same pid does not share, or null where
|
|
199
|
+
* /proc does not give it at once; null when it started none. */
|
|
200
|
+
let startedRefresher: { readonly pid: number; readonly started: string | null } | null = null
|
|
201
|
+
/** Each wait the answers hook armed, for `ANSWERS_TIMING_FILE`: a look at the refresher's outcome,
|
|
202
|
+
* the Dispatch read, or the read left in flight after printing; when it armed it (ms after the
|
|
203
|
+
* process started), for how long, and the time it was armed against (`limit`). */
|
|
204
|
+
const armedWaits: {
|
|
205
|
+
readonly kind: "refresher" | "read" | "settle"
|
|
206
|
+
readonly at: number
|
|
207
|
+
readonly ms: number
|
|
208
|
+
readonly limit: number
|
|
209
|
+
}[] = []
|
|
210
|
+
|
|
211
|
+
/** How long a wait of `kind` armed at `at` (a `performance.now()` reading, now by default) lasts:
|
|
212
|
+
* until `limit`, another such reading, in whole ms because the failure line names the wait, at
|
|
213
|
+
* most `capMs` and at least `leastMs`. Recorded for `ANSWERS_TIMING_FILE`. */
|
|
214
|
+
function armedWait(
|
|
215
|
+
kind: "refresher" | "read" | "settle",
|
|
216
|
+
limit: number,
|
|
217
|
+
{ capMs = Number.POSITIVE_INFINITY, leastMs = 0, at = performance.now() } = {},
|
|
218
|
+
): number {
|
|
219
|
+
const ms = Math.max(leastMs, Math.min(capMs, Math.max(0, Math.round(limit - at))))
|
|
220
|
+
if (process.env[ANSWERS_TIMING_FILE] !== undefined) armedWaits.push({ kind, at, ms, limit })
|
|
221
|
+
return ms
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Writes `timing`, `armedWaits` and `startedRefresher` to `ANSWERS_TIMING_FILE` when that is set. */
|
|
225
|
+
function writeAnswersTiming(timing: Record<string, number | null>): void {
|
|
226
|
+
const timingFile = process.env[ANSWERS_TIMING_FILE]
|
|
227
|
+
if (timingFile === undefined) return
|
|
228
|
+
try {
|
|
229
|
+
writeAtomically(
|
|
230
|
+
timingFile,
|
|
231
|
+
JSON.stringify({ ...timing, waits: armedWaits, refresher: startedRefresher }),
|
|
232
|
+
)
|
|
233
|
+
} catch {
|
|
234
|
+
// The tests' seam only: a path it cannot write never changes what the hook printed or its exit.
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Names the refresher `pid` in `ANSWERS_TIMING_FILE` at once, when that is set: right after its
|
|
239
|
+
* spawn, while it can be no other process's pid. Its start comes from `/proc/<pid>/stat` alone (a
|
|
240
|
+
* 1 to 3 ms read), never `ps`, which can take seconds (procps `ps -p` reads every process: 0.76 to
|
|
241
|
+
* 0.98 s unloaded on the dev box, up to 8.2 s under load), time no part of the hook's deadline
|
|
242
|
+
* is for. With no /proc the start is null, and the test tells the refresher's end by its pid. */
|
|
243
|
+
function nameRefresher(pid: number): void {
|
|
244
|
+
if (process.env[ANSWERS_TIMING_FILE] === undefined) return
|
|
245
|
+
startedRefresher = { pid, started: procStarted(pid) ?? null }
|
|
246
|
+
writeAnswersTiming({})
|
|
247
|
+
}
|
|
190
248
|
|
|
191
249
|
const DISPATCH_HOST_LINE = "export DISPATCH_HOST=claude"
|
|
192
250
|
/** What an overlay export line starts with: the one line of the env file this hook replaces
|
|
@@ -367,12 +425,6 @@ function dispatchFirst(raw: unknown): void {
|
|
|
367
425
|
)
|
|
368
426
|
}
|
|
369
427
|
|
|
370
|
-
/** The ms left until `at`, a `performance.now()` reading, in whole ms because the failure line
|
|
371
|
-
* names the wait. */
|
|
372
|
-
function remainingUntil(at: number): number {
|
|
373
|
-
return Math.max(0, Math.round(at - performance.now()))
|
|
374
|
-
}
|
|
375
|
-
|
|
376
428
|
/** Whether a refresher holds `lock`: it exists, is younger than `REFRESH_LOCK_STALE_MS`, and the
|
|
377
429
|
* pid in it is alive or not yet written. A lock its refresher died holding is stale at once. */
|
|
378
430
|
function refreshLockHeld(lock: string): boolean {
|
|
@@ -452,12 +504,12 @@ async function refreshedAnswers(
|
|
|
452
504
|
}
|
|
453
505
|
const lock = join(directory, REFRESH_LOCK)
|
|
454
506
|
if (!refreshLockHeld(lock)) {
|
|
455
|
-
Bun.spawn(
|
|
456
|
-
|
|
457
|
-
stdout: "ignore",
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
507
|
+
const refresher = Bun.spawn(
|
|
508
|
+
[process.execPath, Bun.main, "answers-refresh", session, cwd, "--host", HOST],
|
|
509
|
+
{ stdin: "ignore", stdout: "ignore", stderr: "ignore", detached: true },
|
|
510
|
+
)
|
|
511
|
+
refresher.unref()
|
|
512
|
+
nameRefresher(refresher.pid)
|
|
461
513
|
}
|
|
462
514
|
const file = join(directory, REFRESH_OUTCOME)
|
|
463
515
|
for (;;) {
|
|
@@ -476,13 +528,14 @@ async function refreshedAnswers(
|
|
|
476
528
|
reason: `the refresher read ${read}, not the Dispatch envoy.json names now`,
|
|
477
529
|
}
|
|
478
530
|
}
|
|
479
|
-
|
|
480
|
-
|
|
531
|
+
// One reading of the clock both decides that the deadline has come and arms the next look.
|
|
532
|
+
const now = performance.now()
|
|
533
|
+
if (Math.round(by - now) <= 0) {
|
|
481
534
|
return {
|
|
482
535
|
reason: `a "!" command in envoy.json had not finished ${Math.round(by)} ms after the hook started`,
|
|
483
536
|
}
|
|
484
537
|
}
|
|
485
|
-
await Bun.sleep(
|
|
538
|
+
await Bun.sleep(armedWait("refresher", by, { capMs: REFRESH_POLL_MS, at: now }))
|
|
486
539
|
}
|
|
487
540
|
}
|
|
488
541
|
|
|
@@ -522,7 +575,7 @@ async function answersMessage(
|
|
|
522
575
|
env: process.env,
|
|
523
576
|
// The read itself always gets the least wait, so a start-up and a resolution that spent the
|
|
524
577
|
// budget on a loaded host never cost a list Dispatch answers at once.
|
|
525
|
-
deadlineMs:
|
|
578
|
+
deadlineMs: armedWait("read", by, { leastMs: ANSWERS_LEAST_WAIT_MS }),
|
|
526
579
|
})
|
|
527
580
|
}
|
|
528
581
|
|
|
@@ -562,19 +615,12 @@ async function answers(text: string): Promise<never> {
|
|
|
562
615
|
printed = performance.now()
|
|
563
616
|
await Promise.race([
|
|
564
617
|
sessionAnswersSettled(input.session_id),
|
|
565
|
-
Bun.sleep(
|
|
618
|
+
Bun.sleep(armedWait("settle", by + ANSWERS_SETTLE_MS)),
|
|
566
619
|
])
|
|
567
620
|
} catch (error) {
|
|
568
621
|
process.stderr.write(`envoy: people's answers were not added — ${messageFor(error)}\n`)
|
|
569
622
|
}
|
|
570
|
-
|
|
571
|
-
if (timingFile !== undefined) {
|
|
572
|
-
try {
|
|
573
|
-
writeFileSync(timingFile, JSON.stringify({ began, printed, ended: performance.now() }))
|
|
574
|
-
} catch {
|
|
575
|
-
// The tests' seam only: a path it cannot write never changes what the hook printed or its exit.
|
|
576
|
-
}
|
|
577
|
-
}
|
|
623
|
+
writeAnswersTiming({ began, printed, ended: performance.now(), deadline: by })
|
|
578
624
|
// Exits rather than returns: a read still running would hold the process open.
|
|
579
625
|
process.exit(0)
|
|
580
626
|
}
|
|
@@ -4,7 +4,7 @@ description: |
|
|
|
4
4
|
Plan and spec reviewer. It changes nothing. Use after drafting a plan: checks that it can be
|
|
5
5
|
carried out as written, approves when in doubt except on the goal and the requirements, where
|
|
6
6
|
doubt is a rejection, and names at most three blocking issues, each
|
|
7
|
-
with its evidence. Use before
|
|
7
|
+
with its evidence. Use on a spec before an approval request: checks each factual claim in it
|
|
8
8
|
against the code, the docs or a read-only command it runs, and marks each claim verified, false
|
|
9
9
|
or unverified.
|
|
10
10
|
tools: Read, Bash
|
package/package.json
CHANGED
|
@@ -192,23 +192,31 @@ const PS_OPTIONS = {
|
|
|
192
192
|
timeout: 1_000,
|
|
193
193
|
} as const
|
|
194
194
|
|
|
195
|
-
/** When process `pid` started
|
|
196
|
-
* where
|
|
197
|
-
|
|
198
|
-
export function processStarted(pid: number): string | undefined {
|
|
195
|
+
/** When process `pid` started by `/proc/<pid>/stat` alone: its 22nd field (clock ticks since
|
|
196
|
+
* boot); undefined where that file cannot be read (no /proc, or no such process). */
|
|
197
|
+
export function procStarted(pid: number): string | undefined {
|
|
199
198
|
try {
|
|
200
199
|
const stat = readFileSync(`/proc/${pid}/stat`, "utf8")
|
|
201
200
|
return stat.slice(stat.lastIndexOf(")") + 2).split(" ")[19]
|
|
202
201
|
} catch {
|
|
203
|
-
|
|
204
|
-
// within a second of the process it replaced exiting, and the hook and the server read it
|
|
205
|
-
// the same way.
|
|
206
|
-
const ps = spawnSync("ps", ["-o", "lstart=", "-p", String(pid)], PS_OPTIONS)
|
|
207
|
-
const started = ps.status === 0 ? ps.stdout.trim() : ""
|
|
208
|
-
return started === "" ? undefined : `ps:${started}`
|
|
202
|
+
return undefined
|
|
209
203
|
}
|
|
210
204
|
}
|
|
211
205
|
|
|
206
|
+
/** When process `pid` started: `procStarted`, or where that cannot be read (macOS has no /proc),
|
|
207
|
+
* `ps`'s start time to the second, prefixed `ps:`; undefined where neither can be read. Two
|
|
208
|
+
* processes that had one id never share it. */
|
|
209
|
+
export function processStarted(pid: number): string | undefined {
|
|
210
|
+
const started = procStarted(pid)
|
|
211
|
+
if (started !== undefined) return started
|
|
212
|
+
// No /proc (macOS): `ps` names the start to the second, which a reused pid does not repeat
|
|
213
|
+
// within a second of the process it replaced exiting, and the hook and the server read it the
|
|
214
|
+
// same way.
|
|
215
|
+
const ps = spawnSync("ps", ["-o", "lstart=", "-p", String(pid)], PS_OPTIONS)
|
|
216
|
+
const fromPs = ps.status === 0 ? ps.stdout.trim() : ""
|
|
217
|
+
return fromPs === "" ? undefined : `ps:${fromPs}`
|
|
218
|
+
}
|
|
219
|
+
|
|
212
220
|
/** Whether `record` belongs to the `codex` process `pid` now runs, rather than to an earlier
|
|
213
221
|
* process that had the same id. A record or a process whose start cannot be read is not taken:
|
|
214
222
|
* records outlive their process, and a new `codex` that reused the pid would otherwise bind to the
|