@sjawhar/claude-envoy 5.16.6 → 5.16.8

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.
@@ -0,0 +1,43 @@
1
+ /** A stream the hook ends before it exits: `end` calls back once the stream has taken every byte. */
2
+ export interface EndableStream {
3
+ end(callback: () => void): unknown
4
+ }
5
+
6
+ /**
7
+ * Ends the open-asks hook once what it wrote is out. Its work is done when `openAsks` returns, but
8
+ * a credential it asked the secrets broker for may still be trading in the background, under the
9
+ * broker client's own bounds (a 30 s attempt, `TRADE_ATTEMPT_MS`; a metadata server's 10 s; a
10
+ * pool's 20 s), and a process with a pending request or timer does not exit on its own: with a
11
+ * broker that never answers, the hook ran for 30 s, past the 20 s hooks.json gives it, and Claude
12
+ * Code would have killed it. Exiting cuts that trade, which writes nothing the next process reads,
13
+ * except on the device-login route: there the trade holds the login's lock and may yet save a
14
+ * refresh token the broker rotated, so a hook whose process holds that lock (`holdsLock`) is left
15
+ * to finish as before.
16
+ *
17
+ * The exit waits until each of `streams` (stdout and stderr) has taken every byte written to it:
18
+ * in Bun 1.4.4 an empty write's callback, `writableLength` and `writableNeedDrain` each let an exit
19
+ * drop the tail of a 300,000-byte write to a pipe (2 or 3 runs of 5), and `end` lost nothing (5 of
20
+ * 5). The lock is read after that, so a trade that takes it while they drain is left to finish too.
21
+ * Nothing the trade runs writes to `process.stderr` (its retry line goes through `console.warn`,
22
+ * which writes to the file descriptor and needs no open stream), so ending the streams first cannot
23
+ * fail it.
24
+ *
25
+ * The read of the lock and the exit are one synchronous step, with no await between them, so no
26
+ * other task of this process runs between the two: a read that waited (an async one) let this
27
+ * process's own trade take the lock and start a refresh in between, which the exit then cut
28
+ * (Legion's review 6014045405: 67 of 3,000 runs of its probe; none of 3,000 with a synchronous
29
+ * read). `holdsLock` must therefore answer synchronously.
30
+ */
31
+ export async function endOnceWritten(
32
+ streams: readonly EndableStream[],
33
+ holdsLock: () => boolean,
34
+ exit: (code: number) => void,
35
+ ): Promise<void> {
36
+ for (const stream of streams) {
37
+ const ended = Promise.withResolvers<void>()
38
+ stream.end(() => ended.resolve())
39
+ await ended.promise
40
+ }
41
+ if (holdsLock()) return
42
+ exit(0)
43
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Two of the open-asks hook's three waits, which it runs one after the other: jj's own user config
3
+ * chain for the pane's attribution overlay; then the resolution of Dispatch, which may run a `!`
4
+ * dispatch.token command for up to envoy-client's `COMMAND_TIMEOUT_MS` (10 s); then the Dispatch
5
+ * read of the session's open asks. Claude Code kills the hook at the `timeout` hooks.json gives
6
+ * `open-asks`, and the session then loses its exports and its summary, so the three, after Bun's
7
+ * start-up, must end inside it: tests/open-asks-hook.test.ts holds their sum and a start-up
8
+ * allowance below that timeout. A module of its own so that the test can import them:
9
+ * session-hook.ts runs the hook when it is loaded.
10
+ */
11
+
12
+ /** The bound on resolving jj's own user config chain for the overlay. Not
13
+ * `JJ_CONFIG_PATH_TIMEOUT_MS`, which is the Oh My Pi lane's bound inside the 30 s that harness
14
+ * gives its session-start handler. A jj that never answers costs the session the export and
15
+ * nothing else. */
16
+ export const JJ_BASE_CHAIN_TIMEOUT_MS = 3_000
17
+
18
+ /** The bound on the Dispatch read of the session's open asks. */
19
+ export const OPEN_ASKS_TIMEOUT_MS = 3_000
@@ -88,6 +88,7 @@
88
88
 
89
89
  import { appendFileSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"
90
90
  import { join } from "node:path"
91
+ import { deviceLoginFile, loginLockHeldBy } from "@legion/cipher-client/device-login-file"
91
92
  import { scrubProcessStreams } from "@legion/contracts/held-secrets"
92
93
  import { urlForText } from "@legion/contracts/log-text"
93
94
  import { formatOpenAsksSummary, linkBaseOf } from "@legion/envoy-client/dispatch-execute"
@@ -119,13 +120,14 @@ import { claudeProjectDirectory, configuredValue } from "../src/claude-session"
119
120
  import { procStarted } from "../src/codex/codex-session-record"
120
121
  import { sessionHandoffFile, writeSessionHandoff } from "../src/session-identity"
121
122
  import { recordChosenTitle } from "../src/session-state"
123
+ import { endOnceWritten } from "./end-once-written"
122
124
  import { parseHookArguments } from "./hook-arguments"
125
+ import { JJ_BASE_CHAIN_TIMEOUT_MS, OPEN_ASKS_TIMEOUT_MS } from "./open-asks-bounds"
123
126
 
124
127
  // Every line this hook writes, the context Claude Code hands the model on stdout included, passes
125
128
  // the scrub of the secrets it holds (`@legion/contracts/held-secrets`).
126
129
  scrubProcessStreams()
127
130
 
128
- const OPEN_ASKS_TIMEOUT_MS = 3_000
129
131
  const OpenAsksInput = z.object({
130
132
  session_id: z.string().min(1),
131
133
  cwd: z.string().optional(),
@@ -250,12 +252,6 @@ const DISPATCH_HOST_LINE = "export DISPATCH_HOST=claude"
250
252
  /** What an overlay export line starts with: the one line of the env file this hook replaces
251
253
  * rather than adds to. */
252
254
  const JJ_CONFIG_EXPORT = "export JJ_CONFIG="
253
- /** The bound on resolving jj's own user config chain for the overlay, inside the 20 s hooks.json
254
- * gives this hook: 3 s of jj and then 3 s of the Dispatch read (`OPEN_ASKS_TIMEOUT_MS`) still
255
- * leave the session its summary. Not `JJ_CONFIG_PATH_TIMEOUT_MS`, which is the Oh My Pi lane's
256
- * bound inside the 30 s that harness gives its session-start handler. A jj that never answers
257
- * costs the session the export and nothing else. */
258
- const JJ_BASE_CHAIN_TIMEOUT_MS = 3_000
259
255
 
260
256
  /** `value` as a single-quoted shell word: Claude Code sources this file before every bash command
261
257
  * of the session, so a path or a config chain holding a space, a quote or a `$` must reach the
@@ -663,6 +659,22 @@ async function answersRefresh(session: string, cwd: string): Promise<never> {
663
659
  process.exit(0)
664
660
  }
665
661
 
662
+ /** Whether this process holds the machine's device-login lock: `<login>.lock`, beside the login at
663
+ * the path cipher-client keeps it (`deviceLoginFile`: `AGENT_SECRETS_DEVICE_LOGIN_FILE`, else
664
+ * `$HOME/.config/legion/device-login.json`), whose first word is its holder's pid. cipher-client
665
+ * reads it, synchronously (`loginLockHeldBy`, as `endOnceWritten` needs): a named pipe or a link
666
+ * there is read as no lock, at once, so it cannot hold the hook open. */
667
+ function holdsDeviceLoginLock(): boolean {
668
+ let login: string
669
+ try {
670
+ login = deviceLoginFile(process.env)
671
+ } catch {
672
+ // Neither the variable nor HOME names a place: this machine keeps no login, so holds no lock.
673
+ return false
674
+ }
675
+ return loginLockHeldBy(login, process.pid)
676
+ }
677
+
666
678
  if (mode === "answers-refresh") {
667
679
  const [session, cwd] = modeArguments
668
680
  if (session === undefined || cwd === undefined) {
@@ -678,5 +690,9 @@ if (mode !== "open-asks" && mode !== "dispatch-first" && mode !== "answers") {
678
690
  const text = await Bun.stdin.text()
679
691
  if (mode === "answers") await answers(text)
680
692
  const input: unknown = JSON.parse(text)
681
- if (mode === "open-asks") await openAsks(input)
682
- else dispatchFirst(input)
693
+ if (mode === "open-asks") {
694
+ await openAsks(input)
695
+ await endOnceWritten([process.stdout, process.stderr], holdsDeviceLoginLock, (code) =>
696
+ process.exit(code),
697
+ )
698
+ } else dispatchFirst(input)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/claude-envoy",
3
- "version": "5.16.6",
3
+ "version": "5.16.8",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Claude Code plugin for Legion's Envoy bus",
6
6
  "type": "module",