@phren/cli 0.3.19 → 0.3.20

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.
Files changed (43) hide show
  1. package/dist/bridge/agent-hooks.d.ts +16 -1
  2. package/dist/bridge/agent-hooks.js +51 -4
  3. package/dist/bridge/changes.d.ts +4 -0
  4. package/dist/bridge/changes.js +25 -2
  5. package/dist/bridge/command.js +24 -6
  6. package/dist/bridge/dispatch-returns.d.ts +11 -0
  7. package/dist/bridge/dispatch-returns.js +47 -7
  8. package/dist/bridge/dispatch.d.ts +19 -0
  9. package/dist/bridge/dispatch.js +164 -5
  10. package/dist/bridge/hook-fast.d.ts +13 -1
  11. package/dist/bridge/hook-fast.js +17 -2
  12. package/dist/bridge/models.d.ts +11 -1
  13. package/dist/bridge/models.js +47 -2
  14. package/dist/bridge/server-launch.d.ts +20 -0
  15. package/dist/bridge/server-launch.js +63 -1
  16. package/dist/bridge/server-routes.d.ts +4 -0
  17. package/dist/bridge/server-routes.js +2 -2
  18. package/dist/bridge/server.js +6 -1
  19. package/dist/bridge/speech-live.d.ts +45 -0
  20. package/dist/bridge/speech-live.js +413 -0
  21. package/dist/bridge/speech-transcribe.d.ts +5 -2
  22. package/dist/bridge/speech-transcribe.js +8 -5
  23. package/dist/bridge/speech-voice.d.ts +22 -1
  24. package/dist/bridge/speech-voice.js +20 -2
  25. package/dist/bridge/speech.d.ts +24 -4
  26. package/dist/bridge/speech.js +115 -14
  27. package/dist/bridge/stalls.d.ts +9 -2
  28. package/dist/bridge/stalls.js +20 -4
  29. package/dist/bridge/transcript-preview.d.ts +11 -1
  30. package/dist/bridge/transcript-preview.js +37 -11
  31. package/dist/bridge/transcripts.d.ts +2 -1
  32. package/dist/bridge/transcripts.js +7 -3
  33. package/dist/bridge/turn-records.d.ts +6 -3
  34. package/dist/bridge/turn-records.js +9 -4
  35. package/dist/bridge/usage.d.ts +1 -1
  36. package/dist/bridge/usage.js +4 -3
  37. package/dist/bridge/worker-close.js +2 -1
  38. package/dist/bridge/worker-unfinished.d.ts +37 -0
  39. package/dist/bridge/worker-unfinished.js +145 -0
  40. package/dist/bridge-hook.mjs +2001 -1130
  41. package/dist/cli-registry.js +2 -1
  42. package/dist/modules/registry.js +2 -2
  43. package/package.json +1 -1
@@ -1,8 +1,11 @@
1
1
  import { once } from "node:events";
2
+ import { Agent as HttpAgent, request as httpRequest } from "node:http";
3
+ import { Agent as HttpsAgent, request as httpsRequest } from "node:https";
4
+ import { Readable } from "node:stream";
2
5
  import { z } from "zod";
3
6
  import { BridgeError } from "./protocol.js";
4
7
  import { readSpeechKey } from "./speech-key.js";
5
- import { FALLBACK_SPEECH_MODEL, resolveSpeechModel, resolveSpeechVoice, voiceId } from "./speech-voice.js";
8
+ import { FALLBACK_SPEECH_MODEL, resolveSpeechModel, resolveSpeechRegion, resolveSpeechVoice, voiceId } from "./speech-voice.js";
6
9
  export { DEFAULT_SPEECH_MODEL, DEFAULT_SPEECH_VOICE, FALLBACK_SPEECH_MODEL } from "./speech-voice.js";
7
10
  /** Spoken replies for the phone's talk mode. The phone sends a sentence; the
8
11
  * Hook voices it with ElevenLabs and streams the audio back. The API key is
@@ -46,6 +49,10 @@ export const speechRequest = z.object({
46
49
  /** Answer JSON with the audio and when each character is spoken, so the
47
50
  * phone can highlight the word being read (talk mode's karaoke). */
48
51
  timestamps: z.boolean().optional(),
52
+ /** With `timestamps`, stream the audio and alignment as ElevenLabs makes
53
+ * them, one JSON line per chunk, instead of one JSON reply once the whole
54
+ * sentence is voiced (about 0.6 s later with v4 Turbo). */
55
+ stream: z.boolean().optional(),
49
56
  /** A voice the phone picked; otherwise this computer's setting. */
50
57
  voice: voiceId.optional(),
51
58
  /** The output formats the phone plays (`SPEECH_FORMATS` names). Without
@@ -84,7 +91,65 @@ export class SpeechState {
84
91
  this.bench(model, "slow");
85
92
  }
86
93
  }
87
- const sharedState = new SpeechState();
94
+ /** The Hook's own: /v1/speech and /v1/speech/live learn together. */
95
+ export const sharedSpeechState = new SpeechState();
96
+ /** Idle connections to ElevenLabs are kept this long, so the next sentence's
97
+ * request skips the TCP and TLS handshake. fetch's own pool lets them go
98
+ * after four seconds, shorter than talk mode's pause between replies. */
99
+ const KEEP_ALIVE_MS = 60_000;
100
+ let pool;
101
+ /** How long ElevenLabs may send nothing, before its headers or between two
102
+ * chunks of audio, before the request is given up on: fetch's own limits were
103
+ * 300 s, and the pool's timeout only closes idle sockets. */
104
+ export const SPEECH_STALL_MS = 30_000;
105
+ /** POSTs to ElevenLabs over the Hook's kept-alive pool and answers a fetch
106
+ * Response whose body is the upstream socket, so audio is passed on chunk by
107
+ * chunk as it arrives. Honours HTTPS_PROXY as fetch does, when
108
+ * NODE_USE_ENV_PROXY is set. A pooled socket ElevenLabs closed while it sat
109
+ * idle fails on its first write; that request is sent once more on a fresh
110
+ * one. */
111
+ export function elevenLabsFetch(url, init, stallMs = SPEECH_STALL_MS) {
112
+ const proxyEnv = process.env.NODE_USE_ENV_PROXY === "1" ? process.env : undefined;
113
+ pool ??= {
114
+ http: new HttpAgent({ keepAlive: true, timeout: KEEP_ALIVE_MS, proxyEnv }),
115
+ https: new HttpsAgent({ keepAlive: true, timeout: KEEP_ALIVE_MS, proxyEnv }),
116
+ };
117
+ const target = new URL(url), secure = target.protocol === "https:";
118
+ const body = typeof init.body === "string" ? init.body : "";
119
+ const attempt = (retried) => new Promise((resolve, reject) => {
120
+ let answered = false;
121
+ const request = (secure ? httpsRequest : httpRequest)(target, {
122
+ method: init.method ?? "GET", signal: init.signal ?? undefined, agent: secure ? pool.https : pool.http,
123
+ headers: { ...init.headers, "Content-Length": String(Buffer.byteLength(body)) },
124
+ }, upstream => {
125
+ answered = true;
126
+ const status = upstream.statusCode ?? 0;
127
+ if (status < 200 || status > 599) {
128
+ upstream.destroy();
129
+ reject(new Error(`HTTP ${status}`));
130
+ return;
131
+ }
132
+ // A Response with one of these statuses may not have a body.
133
+ if (status === 204 || status === 205 || status === 304) {
134
+ upstream.resume();
135
+ resolve(new Response(null, { status }));
136
+ return;
137
+ }
138
+ resolve(new Response(Readable.toWeb(upstream), { status }));
139
+ });
140
+ // Covers the wait for the headers and each gap in the body after them.
141
+ request.setTimeout(stallMs, () => request.destroy(new Error("ElevenLabs stalled")));
142
+ request.on("error", error => {
143
+ if (!retried && !answered && request.reusedSocket && error.code === "ECONNRESET") {
144
+ resolve(attempt(true));
145
+ return;
146
+ }
147
+ reject(error);
148
+ });
149
+ request.end(body);
150
+ });
151
+ return attempt(false);
152
+ }
88
153
  /** ElevenLabs' machine-readable reason (`detail.status`), never its text. */
89
154
  async function upstreamCode(upstream) {
90
155
  try {
@@ -143,9 +208,10 @@ export function speakableText(text) {
143
208
  .replace(/\s+/g, " ")
144
209
  .trim();
145
210
  }
146
- /** Starts ElevenLabs' streaming synthesis and returns its audio body. */
147
- export async function synthesizeSpeech(text, signal, options = {}) {
148
- const { upstream, model, format, started } = await elevenLabs("stream", text, signal, options);
211
+ /** Starts ElevenLabs' streaming synthesis and returns its body: the audio,
212
+ * or with `stream/with-timestamps` JSON lines of audio and alignment. */
213
+ export async function synthesizeSpeech(text, signal, options = {}, endpoint = "stream") {
214
+ const { upstream, model, format, started } = await elevenLabs(endpoint, text, signal, options);
149
215
  if (!upstream.body)
150
216
  throw await speechError(upstream);
151
217
  timed(text, model, started, options);
@@ -172,7 +238,7 @@ export async function synthesizeTimedSpeech(text, signal, options = {}) {
172
238
  function timed(text, model, started, options) {
173
239
  if (model === FALLBACK_SPEECH_MODEL || text.length > TIMED_TEXT)
174
240
  return;
175
- (options.state ?? sharedState).recordLatency(model, (options.now ?? Date.now)() - started);
241
+ (options.state ?? sharedSpeechState).recordLatency(model, (options.now ?? Date.now)() - started);
176
242
  }
177
243
  /** ElevenLabs' alignment, kept only when its three lists line up. */
178
244
  export function alignmentOf(raw) {
@@ -185,6 +251,36 @@ export function alignmentOf(raw) {
185
251
  return null;
186
252
  return { characters: characters, starts: starts, ends: ends };
187
253
  }
254
+ /** ElevenLabs' streamed timestamps, one JSON object per line, as the phone's
255
+ * lines: `{audio, alignment}`, the alignment's times in seconds from the
256
+ * start of the whole reply (as ElevenLabs counts them) and null for a chunk
257
+ * with no characters. A chunk without audio is dropped. */
258
+ async function* timedLines(body) {
259
+ const decoder = new TextDecoder();
260
+ let pending = "";
261
+ for await (const bytes of body) {
262
+ pending += decoder.decode(bytes, { stream: true });
263
+ const lines = pending.split("\n");
264
+ pending = lines.pop();
265
+ for (const line of lines) {
266
+ const out = timedLine(line);
267
+ if (out)
268
+ yield out;
269
+ }
270
+ }
271
+ const out = timedLine(pending + decoder.decode());
272
+ if (out)
273
+ yield out;
274
+ }
275
+ function timedLine(line) {
276
+ if (!line.trim())
277
+ return undefined;
278
+ const raw = JSON.parse(line);
279
+ if (typeof raw.audio_base64 !== "string" || !raw.audio_base64)
280
+ return undefined;
281
+ const alignment = alignmentOf(raw.alignment);
282
+ return JSON.stringify({ audio: raw.audio_base64, alignment: alignment?.characters.length ? alignment : null }) + "\n";
283
+ }
188
284
  /** The plan doesn't allow this output format (Creator: "Output format
189
285
  * 'pcm_44100' is only available on the Pro tier and above", 403
190
286
  * `output_format_not_allowed`). */
@@ -207,8 +303,9 @@ async function elevenLabs(endpoint, text, signal, options) {
207
303
  if (!key)
208
304
  throw new BridgeError(503, "Spoken replies aren't set up on this computer: it has no ElevenLabs key.", { code: "speech-unconfigured" });
209
305
  const { voice } = await resolveSpeechVoice(options.voice);
210
- const state = options.state ?? sharedState, now = options.now ?? Date.now;
306
+ const state = options.state ?? sharedSpeechState, now = options.now ?? Date.now;
211
307
  const chosen = options.model ?? (await resolveSpeechModel()).model;
308
+ const origin = options.origin ?? (await resolveSpeechRegion()).origin;
212
309
  const models = chosen === FALLBACK_SPEECH_MODEL ? [chosen] : state.benchedReason(chosen) ? [FALLBACK_SPEECH_MODEL] : [chosen, FALLBACK_SPEECH_MODEL];
213
310
  const accepted = new Set(options.formats ?? []);
214
311
  let failure;
@@ -216,11 +313,11 @@ async function elevenLabs(endpoint, text, signal, options) {
216
313
  // Per model, so a format refused under one isn't tried again under the next.
217
314
  const formats = SPEECH_FORMATS.filter(format => format === BASE_FORMAT || (accepted.has(format) && state.formatAllowed(format)));
218
315
  for (const format of formats) {
219
- const url = `https://api.elevenlabs.io/v1/text-to-speech/${encodeURIComponent(voice)}/${endpoint}?output_format=${format}`;
316
+ const url = `${origin}/v1/text-to-speech/${encodeURIComponent(voice)}/${endpoint}?output_format=${format}`;
220
317
  const started = now();
221
318
  let upstream;
222
319
  try {
223
- upstream = await (options.fetch ?? fetch)(url, {
320
+ upstream = await (options.fetch ?? elevenLabsFetch)(url, {
224
321
  method: "POST", signal,
225
322
  headers: { "xi-api-key": key, "Content-Type": "application/json" },
226
323
  body: JSON.stringify({ text, model_id: model, voice_settings: { stability: 0.6, similarity_boost: 0.75 } }),
@@ -251,7 +348,9 @@ async function elevenLabs(endpoint, text, signal, options) {
251
348
  * format, model, alignment }`, the audio base64 in the format served and the
252
349
  * alignment null when ElevenLabs sent none. The streamed reply names its
253
350
  * format in `X-Phren-Audio` and `X-Phren-Audio-Rate`. A phone that sends no
254
- * `formats` always gets pcm_24000.
351
+ * `formats` always gets pcm_24000. With `timestamps` and `stream`, the
352
+ * streamed reply is `application/x-ndjson`, one `{audio, alignment}` line per
353
+ * chunk (see timedLines), with the same headers.
255
354
  * Failures before the first byte are thrown for the route's JSON error; a
256
355
  * failure mid-stream cuts the response off, which the phone treats as an
257
356
  * error. The phone hanging up cancels the ElevenLabs request. */
@@ -268,7 +367,7 @@ export async function streamSpeech(data, response, options = {}) {
268
367
  const abort = new AbortController();
269
368
  response.once("close", () => { if (!response.writableEnded)
270
369
  abort.abort(); });
271
- if (timestamps) {
370
+ if (timestamps && !request.stream) {
272
371
  const { audio, alignment, model, format } = await synthesizeTimedSpeech(text, abort.signal, options);
273
372
  const info = SPEECH_FORMAT_INFO[format];
274
373
  response.statusCode = 200;
@@ -276,15 +375,17 @@ export async function streamSpeech(data, response, options = {}) {
276
375
  response.end(JSON.stringify({ audio, audioFormat: info.audio, sampleRate: info.sampleRate, format, model, alignment }));
277
376
  return;
278
377
  }
279
- const { body: audio, model, format } = await synthesizeSpeech(text, abort.signal, options);
378
+ const { body: audio, model, format } = await synthesizeSpeech(text, abort.signal, options, timestamps ? "stream/with-timestamps" : "stream");
280
379
  const info = SPEECH_FORMAT_INFO[format];
281
380
  response.statusCode = 200;
282
- response.setHeader("Content-Type", info.contentType);
381
+ response.setHeader("Content-Type", timestamps ? "application/x-ndjson" : info.contentType);
283
382
  response.setHeader("X-Phren-Audio", info.audio);
284
383
  response.setHeader("X-Phren-Audio-Rate", String(info.sampleRate));
285
384
  response.setHeader("X-Phren-Speech-Model", model);
385
+ // The phone sets up its player from these headers while the first audio is still on its way.
386
+ response.flushHeaders();
286
387
  try {
287
- for await (const chunk of audio) {
388
+ for await (const chunk of timestamps ? timedLines(audio) : audio) {
288
389
  if (!response.write(chunk))
289
390
  await once(response, "drain", { signal: abort.signal });
290
391
  }
@@ -12,11 +12,18 @@ export interface StallReaders {
12
12
  }
13
13
  export declare function stallThreshold(): number;
14
14
  /** Requires successful observations of both sources. Identity, terminal, work
15
- * status and either source changing all reset the clock. Missing data is unknown. */
15
+ * status and either source changing all reset the clock. Missing data is unknown.
16
+ * `live` reads the awaited work the pane's turn still waits on (`liveWork`:
17
+ * awaited background shells and monitors, running children). A worker that
18
+ * ended its turn on purpose while a build runs sits still on screen and in its
19
+ * transcript, so once the clock runs out `live` is asked, and any live work
20
+ * starts the clock over instead of reporting a stall, for at most
21
+ * BACKGROUND_STALE_MS: work that never ends (a build deadlocked on a lock, a
22
+ * child that died without its completion record) does not hide a stall forever. */
16
23
  export declare class StallDetector {
17
24
  private readers;
18
25
  private entries;
19
26
  constructor(readers?: StallReaders);
20
- observe(target: Target, pane: Json): Promise<Stall | undefined>;
27
+ observe(target: Target, pane: Json, live?: () => Promise<number | undefined>): Promise<Stall | undefined>;
21
28
  }
22
29
  export declare const sessionStalls: StallDetector;
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
2
2
  import { open } from "node:fs/promises";
3
3
  import { targetTranscriptPath } from "./transcripts.js";
4
4
  import { terminalProvider } from "./terminal.js";
5
+ import { BACKGROUND_STALE_MS } from "./session-activity.js";
5
6
  export function stallThreshold() {
6
7
  const value = Number(process.env.PHREN_STALL_MS ?? 300_000);
7
8
  return Number.isFinite(value) && value >= 0 ? value : 300_000;
@@ -18,7 +19,14 @@ async function transcriptStamp(target) {
18
19
  }
19
20
  }
20
21
  /** Requires successful observations of both sources. Identity, terminal, work
21
- * status and either source changing all reset the clock. Missing data is unknown. */
22
+ * status and either source changing all reset the clock. Missing data is unknown.
23
+ * `live` reads the awaited work the pane's turn still waits on (`liveWork`:
24
+ * awaited background shells and monitors, running children). A worker that
25
+ * ended its turn on purpose while a build runs sits still on screen and in its
26
+ * transcript, so once the clock runs out `live` is asked, and any live work
27
+ * starts the clock over instead of reporting a stall, for at most
28
+ * BACKGROUND_STALE_MS: work that never ends (a build deadlocked on a lock, a
29
+ * child that died without its completion record) does not hide a stall forever. */
22
30
  export class StallDetector {
23
31
  readers;
24
32
  entries = new Map();
@@ -28,7 +36,7 @@ export class StallDetector {
28
36
  }) {
29
37
  this.readers = readers;
30
38
  }
31
- async observe(target, pane) {
39
+ async observe(target, pane, live) {
32
40
  const key = JSON.stringify([target.server, target.pane]);
33
41
  if (pane.agent_status !== "working" || this.readers.threshold() === 0) {
34
42
  this.entries.delete(key);
@@ -50,8 +58,16 @@ export class StallDetector {
50
58
  }
51
59
  while (this.entries.size > 1024)
52
60
  this.entries.delete(this.entries.keys().next().value);
53
- const since = this.entries.get(key).since;
54
- return now - since >= this.readers.threshold() ? { stalled: true, stalledSince: new Date(since).toISOString(), stallFor: Math.floor((now - since) / 1000) } : undefined;
61
+ const entry = this.entries.get(key), since = entry.since;
62
+ if (now - since < this.readers.threshold())
63
+ return undefined;
64
+ const held = entry.liveSince === undefined || now - entry.liveSince < BACKGROUND_STALE_MS;
65
+ if (live && held && await live().catch(() => undefined)) {
66
+ entry.since = now;
67
+ entry.liveSince ??= since;
68
+ return undefined;
69
+ }
70
+ return { stalled: true, stalledSince: new Date(since).toISOString(), stallFor: Math.floor((now - since) / 1000) };
55
71
  }
56
72
  }
57
73
  export const sessionStalls = new StallDetector();
@@ -1,8 +1,16 @@
1
1
  import { type Target } from "./protocol.js";
2
2
  import type { Entry } from "./transcripts.js";
3
+ /**
4
+ * The reply being written. `delta` and `streamed` appear only on text built
5
+ * from a harness's own deltas (Codex, OpenCode, phren's agent), never on text
6
+ * read off a terminal: `delta` is what was appended since this socket's
7
+ * previous preview frame (the whole text when the block is new to it).
8
+ */
3
9
  export interface TranscriptPreview {
4
10
  turnStartedAt: string;
5
11
  text: string;
12
+ delta?: string;
13
+ streamed?: true;
6
14
  }
7
15
  export declare const PREVIEW_INTERVAL_MS = 500;
8
16
  /** Only the last Claude reply after the current prompt is eligible. A missing
@@ -92,7 +100,9 @@ export declare class TranscriptPreviewStream {
92
100
  private current;
93
101
  private observedLine;
94
102
  private wasWorking;
95
- private readonly codexCompleted;
103
+ /** Replies that already landed as entries: a sidecar or thread a tick
104
+ * behind them must not bring the same text back as a preview. */
105
+ private readonly completed;
96
106
  private readonly rollout;
97
107
  constructor(target: Target, pane?: () => Promise<string>, delta?: (file?: string) => Promise<TranscriptPreview | null>);
98
108
  observe(entries: Entry[], reset?: boolean): void;
@@ -5,6 +5,8 @@ import { readPaneText } from "./pane-text.js";
5
5
  import { object, objects } from "./protocol.js";
6
6
  import { withTranscriptIndex } from "./transcript-index.js";
7
7
  import { stripTerminal } from "../terminal-text.js";
8
+ /** Harnesses whose preview text is the reply's own Markdown, from their deltas. */
9
+ const STREAMED_SOURCES = new Set(["codex", "opencode", "phren"]);
8
10
  export const PREVIEW_INTERVAL_MS = 500;
9
11
  const MAX_TEXT = 32_768;
10
12
  // A JSON-escaped UTF-16 code unit takes at most six bytes, plus metadata.
@@ -351,7 +353,9 @@ export async function readDeltaPreview(target, file, rollout) {
351
353
  if (target.source === "codex")
352
354
  return await codexThreadPreview(target.session)
353
355
  ?? (file && rollout ? await rollout.read(file).catch(() => null) : null);
354
- if (target.source !== "opencode" || !file)
356
+ // OpenCode's plugin and phren's agent write the same sidecar beside their
357
+ // event log (packages/agent/src/session/preview.ts).
358
+ if ((target.source !== "opencode" && target.source !== "phren") || !file)
355
359
  return null;
356
360
  try {
357
361
  const handle = await open(file + ".preview.json", constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
@@ -402,7 +406,9 @@ export class TranscriptPreviewStream {
402
406
  current = null;
403
407
  observedLine = -1;
404
408
  wasWorking = false;
405
- codexCompleted = new Set();
409
+ /** Replies that already landed as entries: a sidecar or thread a tick
410
+ * behind them must not bring the same text back as a preview. */
411
+ completed = new Set();
406
412
  rollout = new CodexRolloutPreview();
407
413
  constructor(target, pane = () => readPreviewPane(target), delta = file => readDeltaPreview(target, file, this.rollout)) {
408
414
  this.target = target;
@@ -416,7 +422,7 @@ export class TranscriptPreviewStream {
416
422
  this.landed = false;
417
423
  this.ended = false;
418
424
  this.wasWorking = false;
419
- this.codexCompleted.clear();
425
+ this.completed.clear();
420
426
  }
421
427
  for (const { raw, line } of entries) {
422
428
  if (line <= this.observedLine)
@@ -428,25 +434,40 @@ export class TranscriptPreviewStream {
428
434
  const payload = object(raw.payload);
429
435
  if (raw.type === "event_msg" && payload.type === "task_started") {
430
436
  this.ended = false;
431
- this.codexCompleted.clear();
437
+ this.completed.clear();
432
438
  }
433
439
  else if (raw.type === "response_item" && payload.type === "message" && payload.role === "user" && !raw.phrenQueued) {
434
440
  this.ended = false;
435
- this.codexCompleted.clear();
441
+ this.completed.clear();
436
442
  }
437
443
  else if (raw.type === "response_item" && payload.type === "message" && payload.role === "assistant") {
438
444
  const text = (typeof payload.content === "string" ? payload.content
439
445
  : objects(payload.content).map(block => typeof block.text === "string" ? block.text : "").join("\n")).trim();
440
446
  if (text) {
441
- this.codexCompleted.add(text);
442
- if (this.codexCompleted.size > 32)
443
- this.codexCompleted.delete(this.codexCompleted.values().next().value);
447
+ this.completed.add(text);
448
+ if (this.completed.size > 32)
449
+ this.completed.delete(this.completed.values().next().value);
444
450
  }
445
451
  }
446
452
  else if (raw.type === "event_msg" && ["task_complete", "task_completed", "turn_aborted", "task_aborted"].includes(String(payload.type)))
447
453
  this.ended = true;
448
454
  continue;
449
455
  }
456
+ if (this.target.source === "phren") {
457
+ const data = object(raw.data), message = object(data.message);
458
+ if (raw.type === "user/message")
459
+ this.completed.clear();
460
+ else if (raw.type === "assistant/message") {
461
+ const text = (typeof message.content === "string" ? message.content
462
+ : objects(message.content).filter(block => block.type === "text").map(block => String(block.text ?? "")).join("\n")).trim();
463
+ if (text) {
464
+ this.completed.add(text);
465
+ if (this.completed.size > 32)
466
+ this.completed.delete(this.completed.values().next().value);
467
+ }
468
+ }
469
+ continue;
470
+ }
450
471
  if (this.target.source === "copilot") {
451
472
  // A prompt starts the turn; idle or abort ends it (Copilot writes a
452
473
  // turn_start/turn_end pair per model call, not per prompt).
@@ -497,14 +518,14 @@ export class TranscriptPreviewStream {
497
518
  let next = null;
498
519
  if (status === "working") {
499
520
  this.wasWorking = true;
500
- if (this.target.source === "codex" || this.target.source === "opencode") {
521
+ if (STREAMED_SOURCES.has(this.target.source)) {
501
522
  // These harnesses own a delta source. Never scrape their pane, even
502
523
  // when the source is temporarily empty or unavailable.
503
524
  if (!this.ended)
504
525
  next = await this.delta(file);
505
- if (this.target.source === "codex" && next) {
526
+ if (this.target.source !== "opencode" && next) {
506
527
  const text = next.text.trim();
507
- if (text && [...this.codexCompleted].some(completed => completed.startsWith(text)))
528
+ if (text && [...this.completed].some(completed => completed.startsWith(text)))
508
529
  next = null;
509
530
  }
510
531
  }
@@ -552,6 +573,11 @@ export class TranscriptPreviewStream {
552
573
  return undefined;
553
574
  if ((next || !previewChanged) && sentAt - this.lastSent < PREVIEW_INTERVAL_MS)
554
575
  return undefined;
576
+ if (next && STREAMED_SOURCES.has(this.target.source)) {
577
+ const before = this.current?.turnStartedAt === next.turnStartedAt ? this.current.text : "";
578
+ next = { turnStartedAt: next.turnStartedAt, text: next.text,
579
+ delta: before && next.text.startsWith(before) ? next.text.slice(before.length) : next.text, streamed: true };
580
+ }
555
581
  this.current = next;
556
582
  this.sentActivity = activity;
557
583
  if (next || activityChanged)
@@ -60,7 +60,8 @@ export declare function childAgent(tree: ChildAgentRelation[], id: string): Loca
60
60
  * teammates, workflow agents and fan-out jobs a finished turn still waits on. */
61
61
  export declare function runningChildAgents(tree: ChildAgentRelation[]): ChildAgentRelation[];
62
62
  /**
63
- * The phren store whose `.runtime/sessions` holds phren-agent event logs.
63
+ * The phren store whose `.sessions` holds phren-agent event logs (and
64
+ * `.runtime/sessions` OpenCode's plugin logs).
64
65
  * `PHREN_PATH` or the shared `~/.phren` root — the two resolutions the CLI's
65
66
  * `findPhrenPath` makes without a working directory, which a service has none
66
67
  * of. Kept inline rather than importing phren-paths: that module drags in
@@ -99,7 +99,8 @@ function toolOutputReferences(output) {
99
99
  return Array.isArray(result.content) ? { ...result, content: imageReferences(result.content) } : output;
100
100
  }
101
101
  /**
102
- * The phren store whose `.runtime/sessions` holds phren-agent event logs.
102
+ * The phren store whose `.sessions` holds phren-agent event logs (and
103
+ * `.runtime/sessions` OpenCode's plugin logs).
103
104
  * `PHREN_PATH` or the shared `~/.phren` root — the two resolutions the CLI's
104
105
  * `findPhrenPath` makes without a working directory, which a service has none
105
106
  * of. Kept inline rather than importing phren-paths: that module drags in
@@ -211,8 +212,11 @@ export async function transcriptPath(source, session, account) {
211
212
  throw new BridgeError(400, "Invalid conversation identity.");
212
213
  const bases = source === "codex" ? [{ base: path.join(codexHome(), "sessions") }]
213
214
  : source === "claude" ? claudeHomes().map(home => ({ id: home.id, base: path.join(home.dir, "projects") }))
214
- : source === "phren" || source === "opencode" ? [{ base: path.join(phrenStoreRoot(), ".runtime", "sessions") }]
215
- : [{ base: path.join(process.env.COPILOT_HOME || path.join(homedir(), ".copilot"), "session-state") }];
215
+ // phren-agent keeps its event logs in the store's `.sessions` (the CLI's
216
+ // sessionsDir); OpenCode's plugin writes under `.runtime/sessions`.
217
+ : source === "phren" ? [{ base: path.join(phrenStoreRoot(), ".sessions") }, { base: path.join(phrenStoreRoot(), ".runtime", "sessions") }]
218
+ : source === "opencode" ? [{ base: path.join(phrenStoreRoot(), ".runtime", "sessions") }]
219
+ : [{ base: path.join(process.env.COPILOT_HOME || path.join(homedir(), ".copilot"), "session-state") }];
216
220
  const pattern = source === "codex" ? `*/*/*/rollout-*-${session}.jsonl` : source === "claude" ? `*/${session}.jsonl`
217
221
  : source === "phren" ? `session-${session}.events.jsonl` : source === "opencode" ? `opencode-${session}.events.jsonl` : `${session}/events.jsonl`;
218
222
  const found = [];
@@ -18,16 +18,17 @@ declare const turnRecordSchema: z.ZodObject<{
18
18
  prompt: z.ZodOptional<z.ZodObject<{
19
19
  seq: z.ZodNumber;
20
20
  at: z.ZodString;
21
- }, z.core.$strict>>;
21
+ }, z.core.$strip>>;
22
22
  stop: z.ZodOptional<z.ZodObject<{
23
23
  seq: z.ZodNumber;
24
24
  at: z.ZodString;
25
25
  background: z.ZodOptional<z.ZodNumber>;
26
26
  reply: z.ZodOptional<z.ZodString>;
27
27
  truncated: z.ZodOptional<z.ZodBoolean>;
28
- }, z.core.$strict>>;
28
+ cwd: z.ZodOptional<z.ZodString>;
29
+ }, z.core.$strip>>;
29
30
  at: z.ZodString;
30
- }, z.core.$strict>;
31
+ }, z.core.$strip>;
31
32
  export type TurnRecord = z.infer<typeof turnRecordSchema>;
32
33
  export interface TurnEvent {
33
34
  event: string;
@@ -40,6 +41,8 @@ export interface TurnEvent {
40
41
  background?: number;
41
42
  /** Stop only: the harness's last assistant message, when it reports one. */
42
43
  reply?: string;
44
+ /** Stop only: the agent's working directory, as its hook payload names it. */
45
+ cwd?: string;
43
46
  at?: number;
44
47
  }
45
48
  export declare const turnPath: (server: string, pane: string) => string;
@@ -24,7 +24,9 @@ import { atomicInPrivateDir, bridgeRoot, provider, serverName, sessionId } from
24
24
  /** Longest final reply kept from a Stop payload, in UTF-8 bytes (the receipt's limit). */
25
25
  export const TURN_REPLY_LIMIT = 4000;
26
26
  const MAX_RECORD_BYTES = 16_384;
27
- const stamp = z.object({ seq: z.number().int().min(0), at: z.string().datetime() }).strict();
27
+ // Not strict: a field a newer Hook adds (as `stop.cwd` was) is dropped on
28
+ // read here, never a reason to throw the pane's record away.
29
+ const stamp = z.object({ seq: z.number().int().min(0), at: z.string().datetime() });
28
30
  const turnRecordSchema = z.object({
29
31
  terminal: z.string().min(1).max(200), source: provider, session: sessionId,
30
32
  /** Climbs with every recorded event, so prompt and stop are ordered without trusting the clock. */
@@ -37,9 +39,11 @@ const turnRecordSchema = z.object({
37
39
  /** Background tasks (shells, subagents, monitors) the harness still ran when the turn stopped. */
38
40
  background: z.number().int().min(0).max(999).optional(),
39
41
  reply: z.string().max(TURN_REPLY_LIMIT).optional(), truncated: z.boolean().optional(),
40
- }).strict().optional(),
42
+ /** The agent's working directory when the turn stopped: where its uncommitted work would be. */
43
+ cwd: z.string().max(4096).optional(),
44
+ }).optional(),
41
45
  at: z.string().datetime(),
42
- }).strict();
46
+ });
43
47
  export const turnPath = (server, pane) => path.join(bridgeRoot(), "turns", encodeURIComponent(serverName.parse(server)), encodeURIComponent(pane) + ".json");
44
48
  /** `value` cut to at most `limit` UTF-8 bytes on a character boundary. */
45
49
  export function truncateUtf8(value, limit = TURN_REPLY_LIMIT) {
@@ -70,7 +74,8 @@ export function nextTurn(previous, event) {
70
74
  else {
71
75
  const reply = event.reply?.trim() ? truncateUtf8(event.reply.trim()) : undefined;
72
76
  record.stop = { seq: record.seq, at, ...(event.background !== undefined ? { background: Math.min(999, Math.max(0, Math.floor(event.background))) } : {}),
73
- ...(reply ? { reply: reply.text, ...(reply.truncated ? { truncated: true } : {}) } : {}) };
77
+ ...(reply ? { reply: reply.text, ...(reply.truncated ? { truncated: true } : {}) } : {}),
78
+ ...(event.cwd && path.isAbsolute(event.cwd) && event.cwd.length <= 4096 ? { cwd: event.cwd } : {}) };
74
79
  }
75
80
  return record;
76
81
  }
@@ -87,7 +87,7 @@ export declare function fetchOpenRouterUsage(key: string, fetchImpl?: typeof fet
87
87
  /** ElevenLabs' subscription: characters used this billing period against the plan's limit. */
88
88
  export declare function elevenLabsUsage(value: unknown, now?: Date): AccountUsage;
89
89
  /** The key the Hook already uses for /v1/speech, sent only to ElevenLabs; the answer carries counts, never the key. */
90
- export declare function fetchElevenLabsUsage(key: string, fetchImpl?: typeof fetch, now?: Date): Promise<AccountUsage>;
90
+ export declare function fetchElevenLabsUsage(key: string, fetchImpl?: typeof fetch, now?: Date, origin?: string): Promise<AccountUsage>;
91
91
  /** Claude Code's documented status-line payload: rate_limits carries the
92
92
  * five_hour and seven_day windows (the 5h/7d unified limits) and, when the
93
93
  * build emits them, per-model keys such as seven_day_fable. Every window
@@ -10,6 +10,7 @@ import { atomicInPrivateDir, bridgeRoot, object } from "./protocol.js";
10
10
  import { stripTerminal } from "../terminal-text.js";
11
11
  import { codexExecutable } from "./codex-binary.js";
12
12
  import { readSpeechKey } from "./speech-key.js";
13
+ import { resolveSpeechRegion, SPEECH_REGIONS } from "./speech-voice.js";
13
14
  import { CODEX_ACCOUNT, claudeAccountSubscription, claudeAccountEmail, claudeAccountRef, claudeHomeOfEnv, claudeHomes } from "./claude-accounts.js";
14
15
  import { planName, subscriptionDate } from "./subscription.js";
15
16
  const exec = promisify(execFile);
@@ -352,11 +353,11 @@ export function elevenLabsUsage(value, now = new Date()) {
352
353
  return { source: "elevenlabs", ...metadata, windows: entry ? [{ ...entry, usedCharacters: used, limitCharacters: limit }] : [], updatedAt: now.toISOString() };
353
354
  }
354
355
  /** The key the Hook already uses for /v1/speech, sent only to ElevenLabs; the answer carries counts, never the key. */
355
- export async function fetchElevenLabsUsage(key, fetchImpl = fetch, now = new Date()) {
356
+ export async function fetchElevenLabsUsage(key, fetchImpl = fetch, now = new Date(), origin = SPEECH_REGIONS.global) {
356
357
  const controller = new AbortController();
357
358
  const timer = setTimeout(() => controller.abort(), 8_000);
358
359
  try {
359
- const response = await fetchImpl("https://api.elevenlabs.io/v1/user/subscription", {
360
+ const response = await fetchImpl(`${origin}/v1/user/subscription`, {
360
361
  headers: { "xi-api-key": key, accept: "application/json" },
361
362
  redirect: "error",
362
363
  signal: controller.signal,
@@ -377,7 +378,7 @@ async function liveElevenLabsUsage(now) {
377
378
  if (!key)
378
379
  return undefined;
379
380
  try {
380
- return await fetchElevenLabsUsage(key, fetch, now);
381
+ return await fetchElevenLabsUsage(key, fetch, now, (await resolveSpeechRegion()).origin);
381
382
  }
382
383
  catch {
383
384
  return { source: "elevenlabs", windows: [], updatedAt: now.toISOString(), message: "Could not read ElevenLabs usage. Check this computer's ElevenLabs key." };
@@ -51,7 +51,8 @@ export async function closeFinishedWorker(input, queue) {
51
51
  return { ok: true, closed: false };
52
52
  const seen = (await workerStates({ targets: [{ ...data.target, dispatch: data.dispatch }] })).workers[0];
53
53
  const turn = workerTurnKey(seen);
54
- if (!seen.completed || seen.background || seen.error || turn !== data.turn)
54
+ // A turn that stopped mid-task, or whose checkout could not be read, keeps its pane.
55
+ if (!seen.completed || seen.background || seen.error || seen.unfinished || seen.unchecked || turn !== data.turn)
55
56
  return { ok: true, closed: false };
56
57
  // One final fresh binding before the terminal mutation.
57
58
  const current = await validateTarget(data.target, false, true);
@@ -0,0 +1,37 @@
1
+ /** The reply's closing sentence when it announces a next step ("Let me…",
2
+ * "I'll…", "Now running…"), else undefined. */
3
+ export declare function announcedNextStep(reply: string | undefined): string | undefined;
4
+ /** A checkout git could not read in time (or at all): its work is unknown,
5
+ * so a turn there is never closed on the strength of it. */
6
+ export declare const UNKNOWN = "unknown";
7
+ export type Uncommitted = number | typeof UNKNOWN | undefined;
8
+ /** Tracked files with uncommitted changes (staged or not) in the checkout at
9
+ * `directory`. Untracked files are left out: a main checkout often holds the
10
+ * owner's own (worktree folders, scratch files) and they are not the worker's.
11
+ * Undefined when it is not a repository; UNKNOWN when git does not answer in
12
+ * time or fails otherwise. */
13
+ export declare function uncommittedFiles(directory: string, timeoutMs?: number): Promise<Uncommitted>;
14
+ /** `uncommittedFiles` for the returns loop, which asks about a finished
15
+ * worker on every poll (15 s) for as long as its receipt is watched: a read
16
+ * under a minute old answers. An UNKNOWN read is not kept, so the next poll
17
+ * (and the close recheck) asks git again. */
18
+ export declare function recentUncommitted(directory: string, now?: number, read?: (directory: string) => Promise<Uncommitted>): Promise<Uncommitted>;
19
+ /** The checkout `directory` is in: the nearest folder holding `.git` (a
20
+ * linked worktree's is a file), or undefined outside any. */
21
+ export declare function checkoutRoot(directory: string): Promise<string | undefined>;
22
+ /** Whether any of `folders` (other panes' working folders) is in the same
23
+ * checkout as `directory`. Uncommitted files there may be the owner's or a
24
+ * sibling worker's, so they say nothing about this worker. */
25
+ export declare function sharedCheckout(directory: string, folders: readonly unknown[]): Promise<boolean>;
26
+ /** Why a turn that ended is not done: it announced a next step, or it left
27
+ * uncommitted work with no PR reported or named in the reply (`unfinished`). `unchecked` when its
28
+ * checkout could not be read: the turn may be done, but it is not safe to
29
+ * close. Empty when it reads as done. */
30
+ export declare function unfinishedTurn(input: {
31
+ reply?: string;
32
+ prs?: readonly unknown[];
33
+ directory?: string;
34
+ }, uncommitted?: (directory: string) => Promise<Uncommitted>): Promise<{
35
+ unfinished?: string;
36
+ unchecked?: true;
37
+ }>;