@phren/cli 0.3.68 → 0.3.69

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.
@@ -20,3 +20,6 @@ export declare function claudeSubagentView(screen: string): boolean;
20
20
  * text, a subagent's view, or no single-line live box on screen (typed text
21
21
  * that wraps leaves no rule under the prompt line). */
22
22
  export declare function claudeInputEmpty(screen: string): boolean;
23
+ /** Exact visible typed text in an unambiguous single-line composer. Wrapped,
24
+ * collapsed-paste and dim suggestion inputs cannot prove the delivery's text. */
25
+ export declare function claudeInputMatches(screen: string, text: string): boolean;
@@ -59,6 +59,16 @@ export function claudeInputEmpty(screen) {
59
59
  const dim = dimText(line);
60
60
  return !!dim && !SUBAGENT_VIEW.test(dim);
61
61
  }
62
+ /** Exact visible typed text in an unambiguous single-line composer. Wrapped,
63
+ * collapsed-paste and dim suggestion inputs cannot prove the delivery's text. */
64
+ export function claudeInputMatches(screen, text) {
65
+ const line = liveInputLine(screen);
66
+ if (line === undefined || dimText(line) || /[\r\n]/.test(text))
67
+ return false;
68
+ // Remove only Claude's prompt and its separator space. Extra whitespace,
69
+ // including ambiguous screen padding, cannot prove an exact match.
70
+ return stripTerminal(line).replace(/^\s*❯[ \u00a0]/, "") === text;
71
+ }
62
72
  /** The dim text in the live input box (between its two rules), "" when the
63
73
  * box holds typed text, undefined when there is no live box on screen. */
64
74
  function liveInputDimText(screen) {
@@ -109,7 +109,7 @@ export declare class PromptDeliveries {
109
109
  private release;
110
110
  private open;
111
111
  /** A message Phren typed into `target`'s pane that no conversation there has submitted yet. */
112
- pending(target: Target, text: string): boolean;
112
+ pending(target: Target, text: string, id?: string): boolean;
113
113
  /** Whether `record` still holds its pane: open, and either young or in a pane whose hooks will settle it. */
114
114
  private inFlight;
115
115
  /** Whether any message typed into `target`'s pane is still waiting to be taken or refused.
@@ -261,7 +261,12 @@ export class PromptDeliveries {
261
261
  return this.records.find(record => record.hash === hash && this.samePane(record, target) && (record.state === "typed" || record.state === "queued"));
262
262
  }
263
263
  /** A message Phren typed into `target`'s pane that no conversation there has submitted yet. */
264
- pending(target, text) { return !!this.open(target, text); }
264
+ pending(target, text, id) {
265
+ if (!id)
266
+ return !!this.open(target, text);
267
+ const record = this.find(id, target);
268
+ return !!record && PromptDeliveries.open(record) && record.session === target.session && record.hash === promptHash(text);
269
+ }
265
270
  /** Whether `record` still holds its pane: open, and either young or in a pane whose hooks will settle it. */
266
271
  inFlight(record) {
267
272
  return PromptDeliveries.open(record) && (this.now() - record.at < IN_FLIGHT_MS || this.hookPanes.has(this.paneKey(record)));
@@ -37,7 +37,7 @@ export declare function gitRepository(pane: Json, target: Target, child: unknown
37
37
  export declare function herdrWorktreeWorkers(s: Json): WorktreeWorker[];
38
38
  /** What `takesPrompt` checks before typing into a Claude pane Herdr reads working: nothing structured
39
39
  * pending there and no dialog on its screen. */
40
- export declare function promptGuards(agentHooks: Pick<AgentHooks, "approval" | "terminalPrompt" | "servedQuestion">, target: Target): PromptGuards;
40
+ export declare function promptGuards(agentHooks: Pick<AgentHooks, "approval" | "terminalPrompt" | "servedQuestion">, target: Target): Required<PromptGuards>;
41
41
  /** A Claude pane showing a subagent's view (its empty input reads "Message
42
42
  * @<agent>…") takes typed text for that subagent, so a phone message never
43
43
  * reached the turn and failed when it ended (owner, 2026-10-21). Escape returns
@@ -50,12 +50,9 @@ export declare function leaveClaudeSubagentView(agentHooks: Pick<AgentHooks, "pa
50
50
  * it again under the same delivery id, once. Any other outcome, a changed
51
51
  * pane, or ten minutes end the watch. */
52
52
  export declare function retryAfterSubagentView(agentHooks: AgentHooks, target: Target, terminal: unknown, text: string, id: string, intervalMs?: number, limitMs?: number, escapeWaitMs?: number): void;
53
- /** A busy Claude Code queues typed text on Enter, but after a long paste
54
- * ("paste again to expand") it can swallow that Enter and leave the text in
55
- * its input line, where it stays after the turn ends. Follow the pane: once
56
- * it has stayed finished for two looks while the prompt is still unsubmitted,
57
- * press Enter once. A closed or replaced pane, a delivery the conversation
58
- * took, or ten minutes end the watch; a pane asking for input is never answered. */
59
- export declare function followQueuedDelivery(agentHooks: AgentHooks, target: Target, terminal: unknown, text: string, intervalMs?: number): void;
53
+ /** Observe while the original conversation works, then allow 30 seconds for
54
+ * its submission acknowledgement after it finishes. A new legitimate turn
55
+ * resets that window. No observation sends input to the terminal. */
56
+ export declare function followQueuedDelivery(agentHooks: AgentHooks, target: Target, terminal: unknown, text: string, id?: string, intervalMs?: number): void;
60
57
  export declare function paneRoute(ctx: PaneRouteContext, url: URL, data: Json, response: ServerResponse, headers?: IncomingHttpHeaders): Promise<unknown>;
61
58
  export declare function paneRouteOnce(ctx: PaneRouteContext, url: URL, data: Json, response: ServerResponse, typing: () => void | Promise<void>): Promise<unknown>;
@@ -5,7 +5,8 @@ import path from "node:path";
5
5
  import { setTimeout as sleep } from "node:timers/promises";
6
6
  import { z } from "zod";
7
7
  import { TURN_ENDED_WITHOUT_IT } from "./prompt-deliveries.js";
8
- import { claudeSubagentView } from "./claude-suggestion.js";
8
+ import { screenDialog } from "./pane-status.js";
9
+ import { claudeInputMatches, claudeSubagentView } from "./claude-suggestion.js";
9
10
  import { gitBranches, gitDiscard, gitLog, gitPulls, gitShow, gitStage, gitStatus, gitTree, gitUnstage } from "./git.js";
10
11
  import { fanoutWorktrees } from "./fanouts.js";
11
12
  import { gitWorktrees, resolveWorktree } from "./git-worktrees.js";
@@ -222,28 +223,46 @@ async function relaysSuggestion(target, text) {
222
223
  }
223
224
  /** Herdr answered the request with a refusal (a 409 carrying its own error code): it performed nothing. */
224
225
  const herdrRefused = (error) => error instanceof BridgeError && error.status === 409 && typeof error.details?.herdrCode === "string";
225
- /** An idle agent that has not taken typed text within the first wait
226
- * likely lost the Enter while it redrew (an update notice, a resize): the
227
- * text sits in its input line. Press Enter once more and wait again. A
228
- * pane that turned working took it; one still idle is reported unsubmitted,
229
- * so the phone shows the message as not sent instead of waiting forever. */
230
- async function resubmitIfIdle(agentHooks, target, text) {
231
- const status = async () => { try {
232
- return String(findPane(await snapshot(target.server), target)?.agent_status ?? "unknown");
226
+ /** Fresh terminal and conversation evidence for a delivery follow-up. Failure
227
+ * ends recovery; a later valid submission can still settle the failed receipt. */
228
+ async function deliveryPane(target, terminal) {
229
+ const pane = findPane(await snapshot(target.server), target);
230
+ if (!pane || typeof terminal !== "string" || pane.terminal_id !== terminal || pane.agent !== target.source)
231
+ throw new Error("The terminal instance changed or could not be verified.");
232
+ if (await paneIdentity(target.server, pane, true) !== target.session)
233
+ throw new Error("The conversation changed or could not be verified.");
234
+ return pane;
235
+ }
236
+ /** Screen reads cannot authorize a later key write: neither tmux nor Herdr
237
+ * exposes an atomic Claude composer/identity/held-state check-and-submit.
238
+ * Observe the receipt and report what can be verified, without writing keys.
239
+ * In particular, expanding a collapsed paste with keys would have the same
240
+ * race and still would not expose authoritative full composer text. */
241
+ async function observeIdleDelivery(agentHooks, target, terminal, text, id) {
242
+ const fail = (reason) => { agentHooks.deliveries.fail(id, target, reason); return "unsubmitted"; };
243
+ const guards = promptGuards(agentHooks, target);
244
+ try {
245
+ if (!id)
246
+ return fail("No delivery identity is available; check the terminal.");
247
+ const pane = await deliveryPane(target, terminal);
248
+ if (pane.agent_status === "working" && !guards.held())
249
+ return await agentHooks.deliveries.awaitLate(target, text);
250
+ if (!["idle", "done"].includes(String(pane.agent_status)))
251
+ return fail("The agent is not idle or done; observation stopped.");
252
+ if (guards.held())
253
+ return fail("The agent has a held approval, question or terminal prompt; observation stopped.");
254
+ const screen = await guards.screen();
255
+ // These are observations only. The owner or harness may change the input
256
+ // even before readScreen resolves; no screen result permits an Enter.
257
+ if (!agentHooks.deliveries.pending(target, text, id))
258
+ return agentHooks.deliveries.status(id, target).state === "delivered" ? "delivered" : "unsubmitted";
259
+ if (screenDialog(target.source, screen) || !claudeInputMatches(screen, text))
260
+ return fail("The full message cannot be verified in the input box; check the terminal.");
261
+ return fail("The message is visible but cannot be safely resubmitted; check and submit it in the terminal.");
262
+ }
263
+ catch (error) {
264
+ return fail(error instanceof Error ? error.message : "The delivery checks failed; check the terminal.");
233
265
  }
234
- catch {
235
- return "unknown";
236
- } };
237
- // A pane that turned working started a turn: its UserPromptSubmit is on its way (the agent runs its
238
- // other hooks too). Wait for it rather than answering "queued" for a message the agent took.
239
- if (await status() === "working")
240
- return await agentHooks.deliveries.awaitLate(target, text);
241
- const late = agentHooks.deliveries.awaitLate(target, text);
242
- await terminalProvider().sendKeys(target.server, target.pane, ["enter"]);
243
- const outcome = await late;
244
- if (outcome !== "pending")
245
- return outcome;
246
- return await status() === "working" ? "pending" : "unsubmitted";
247
266
  }
248
267
  /** A Claude pane showing a subagent's view (its empty input reads "Message
249
268
  * @<agent>…") takes typed text for that subagent, so a phone message never
@@ -322,76 +341,64 @@ export function retryAfterSubagentView(agentHooks, target, terminal, text, id, i
322
341
  const expected = agentHooks.deliveries.expect(target, text, 1_500, undefined, id, typeof terminal === "string" ? terminal : undefined);
323
342
  await terminalProvider().prompt(target.server, target.pane, text);
324
343
  if (await expected === "pending")
325
- await resubmitIfIdle(agentHooks, target, text);
344
+ await observeIdleDelivery(agentHooks, target, terminal, text, id);
326
345
  };
327
346
  const schedule = () => { const timer = setTimeout(() => void look().catch(() => undefined), intervalMs); timer.unref?.(); };
328
347
  schedule();
329
348
  }
330
- /** A busy Claude Code queues typed text on Enter, but after a long paste
331
- * ("paste again to expand") it can swallow that Enter and leave the text in
332
- * its input line, where it stays after the turn ends. Follow the pane: once
333
- * it has stayed finished for two looks while the prompt is still unsubmitted,
334
- * press Enter once. A closed or replaced pane, a delivery the conversation
335
- * took, or ten minutes end the watch; a pane asking for input is never answered. */
336
- export function followQueuedDelivery(agentHooks, target, terminal, text, intervalMs = 2_000) {
337
- const started = Date.now();
338
- let finishedLooks = 0;
349
+ /** Observe while the original conversation works, then allow 30 seconds for
350
+ * its submission acknowledgement after it finishes. A new legitimate turn
351
+ * resets that window. No observation sends input to the terminal. */
352
+ export function followQueuedDelivery(agentHooks, target, terminal, text, id, intervalMs = 2_000) {
353
+ let finishedAt;
354
+ const fail = (reason) => agentHooks.deliveries.fail(id, target, reason);
339
355
  const look = async () => {
340
- if (!agentHooks.deliveries.pending(target, text) || Date.now() - started > 600_000)
356
+ if (!id || !agentHooks.deliveries.pending(target, text, id))
341
357
  return;
342
- let pane;
343
358
  try {
344
- pane = findPane(await snapshot(target.server), target);
345
- }
346
- catch {
347
- pane = undefined;
348
- }
349
- if (!pane) {
359
+ const pane = await deliveryPane(target, terminal);
360
+ const status = String(pane.agent_status);
361
+ if (promptGuards(agentHooks, target).held() || !["working", "idle", "done"].includes(status)) {
362
+ fail("The agent needs input or its status is unknown; observation stopped.");
363
+ return;
364
+ }
365
+ if (status === "working")
366
+ finishedAt = undefined;
367
+ else {
368
+ finishedAt ??= Date.now();
369
+ if (Date.now() - finishedAt >= 30_000) {
370
+ await observeIdleDelivery(agentHooks, target, terminal, text, id);
371
+ return;
372
+ }
373
+ }
350
374
  schedule();
351
- return;
352
375
  }
353
- if (pane.terminal_id !== terminal)
354
- return;
355
- const status = String(pane.agent_status);
356
- finishedLooks = status === "idle" || status === "done" ? finishedLooks + 1 : 0;
357
- if (finishedLooks >= 2) {
358
- await resubmitIfIdle(agentHooks, target, text).catch(() => undefined);
359
- return;
376
+ catch {
377
+ fail("The terminal or conversation could not be verified; observation stopped.");
360
378
  }
361
- schedule();
362
379
  };
363
380
  const schedule = () => { const timer = setTimeout(() => void look(), intervalMs); timer.unref?.(); };
364
381
  schedule();
365
382
  }
366
- /** A fresh Claude Code that took its first prompt starts working. One that
367
- * stays idle kept the brief in its input line (a long paste can swallow the
368
- * Enter): press Enter once, only while it is idle, so a dialog is never
369
- * answered. False when it still has not started. */
383
+ /** A starting pane has no verified conversation/delivery binding yet, so
384
+ * observe its initial submission without sending delayed keys. */
370
385
  async function submitStartingPrompt(server, target, terminal, looks = 4, intervalMs = 500) {
371
- const status = async () => {
386
+ for (let look = 0; look < looks; look++) {
387
+ await sleep(intervalMs);
372
388
  try {
373
389
  const pane = findPane(await snapshot(server), target);
374
- return pane && pane.terminal_id === terminal ? String(pane.agent_status) : "gone";
390
+ if (!pane || pane.terminal_id !== terminal)
391
+ return false;
392
+ if (pane.agent_status === "working")
393
+ return true;
394
+ if (!["idle", "done"].includes(String(pane.agent_status)))
395
+ return false;
375
396
  }
376
397
  catch {
377
- return "unknown";
398
+ return false;
378
399
  }
379
- };
380
- const settled = async () => {
381
- for (let look = 0; look < looks; look++) {
382
- await sleep(intervalMs);
383
- const current = await status();
384
- if (current !== "idle" && current !== "done")
385
- return current;
386
- }
387
- return "idle";
388
- };
389
- const first = await settled();
390
- if (first !== "idle")
391
- return first !== "gone";
392
- await terminalProvider().sendKeys(server, target.pane, ["enter"]);
393
- const second = await settled();
394
- return second !== "idle" && second !== "gone";
400
+ }
401
+ return false;
395
402
  }
396
403
  /** One key per character; Herdr's send_keys takes single characters and named
397
404
  * keys only, and a tty password read is corrupted by a bracketed paste. */
@@ -544,8 +551,8 @@ export async function paneRouteOnce(ctx, url, data, response, typing) {
544
551
  }
545
552
  refuseWorkingSlash(current, text);
546
553
  });
547
- // A dispatched worker's brief is often long enough for Claude Code to
548
- // swallow the Enter; a slash command opens a menu an Enter would answer.
554
+ // Observe a fresh Claude pane without adding keys to its initial send;
555
+ // a slash command opens a menu and needs no submission observation.
549
556
  const submitted = target.source !== "claude" || text.trim().startsWith("/")
550
557
  || await submitStartingPrompt(target.server, target, pane.terminal_id);
551
558
  if (!submitted)
@@ -701,9 +708,9 @@ export async function paneRouteOnce(ctx, url, data, response, typing) {
701
708
  // command opens a menu that a second Enter would answer.
702
709
  if (outcome === "pending" && target.source === "claude" && !text.trim().startsWith("/")) {
703
710
  if (!busy)
704
- outcome = await resubmitIfIdle(agentHooks, target, text);
711
+ outcome = await observeIdleDelivery(agentHooks, target, pane.terminal_id, text, deliveryId);
705
712
  else
706
- followQueuedDelivery(agentHooks, target, pane.terminal_id, text);
713
+ followQueuedDelivery(agentHooks, target, pane.terminal_id, text, deliveryId);
707
714
  }
708
715
  if (deliveryId && target.source === "claude" && outcome !== "delivered")
709
716
  retryAfterSubagentView(agentHooks, target, pane.terminal_id, text, deliveryId);
@@ -715,7 +722,8 @@ export async function paneRouteOnce(ctx, url, data, response, typing) {
715
722
  if (outcome === "delivered")
716
723
  result = { ok: true, delivered: true, state: "delivered" };
717
724
  else if (outcome === "unsubmitted") {
718
- const reason = "The agent did not take the message; it may still be in its input line.";
725
+ const failed = deliveryId ? agentHooks.deliveries.status(deliveryId, target) : undefined;
726
+ const reason = failed?.state === "failed" && failed.reason ? failed.reason : "The agent did not take the message; it may still be in its input line.";
719
727
  if (deliveryId)
720
728
  agentHooks.deliveries.fail(deliveryId, target, reason);
721
729
  result = { ok: true, deliveryUncertain: true, unsubmitted: true, ...(deliveryId ? { state: "failed", reason } : {}) };
@@ -38214,7 +38214,7 @@ var init_package_metadata = __esm({
38214
38214
  __dirname = path35.dirname(fileURLToPath2(import.meta.url));
38215
38215
  ROOT = path35.join(__dirname, "..");
38216
38216
  PACKAGE_JSON_PATH = path35.join(ROOT, "package.json");
38217
- BUNDLED_VERSION = true ? "0.3.68" : "0.0.0";
38217
+ BUNDLED_VERSION = true ? "0.3.69" : "0.0.0";
38218
38218
  PACKAGE_JSON = readPackageJson();
38219
38219
  PACKAGE_NAME = PACKAGE_JSON.name || "phren";
38220
38220
  VERSION = PACKAGE_JSON.version || BUNDLED_VERSION;
@@ -65756,8 +65756,10 @@ var init_prompt_deliveries = __esm({
65756
65756
  return this.records.find((record4) => record4.hash === hash3 && this.samePane(record4, target2) && (record4.state === "typed" || record4.state === "queued"));
65757
65757
  }
65758
65758
  /** A message Phren typed into `target`'s pane that no conversation there has submitted yet. */
65759
- pending(target2, text6) {
65760
- return !!this.open(target2, text6);
65759
+ pending(target2, text6, id2) {
65760
+ if (!id2) return !!this.open(target2, text6);
65761
+ const record4 = this.find(id2, target2);
65762
+ return !!record4 && _PromptDeliveries.open(record4) && record4.session === target2.session && record4.hash === promptHash(text6);
65761
65763
  }
65762
65764
  /** Whether `record` still holds its pane: open, and either young or in a pane whose hooks will settle it. */
65763
65765
  inFlight(record4) {
@@ -66428,6 +66430,11 @@ function claudeInputEmpty(screen) {
66428
66430
  const dim = dimText(line);
66429
66431
  return !!dim && !SUBAGENT_VIEW.test(dim);
66430
66432
  }
66433
+ function claudeInputMatches(screen, text6) {
66434
+ const line = liveInputLine(screen);
66435
+ if (line === void 0 || dimText(line) || /[\r\n]/.test(text6)) return false;
66436
+ return stripTerminal(line).replace(/^\s*❯[ \u00a0]/, "") === text6;
66437
+ }
66431
66438
  function liveInputDimText(screen) {
66432
66439
  const line = liveInputLine(screen);
66433
66440
  return line === void 0 ? void 0 : dimText(line);
@@ -72430,20 +72437,31 @@ async function relaysSuggestion(target2, text6) {
72430
72437
  const words = plain2(text6), cut = shown.endsWith("\u2026") ? plain2(shown.slice(0, -1)) : void 0;
72431
72438
  return words === plain2(shown) || cut !== void 0 && (words === cut || words === `${cut}\u2026` || words === `${cut} \u2026`);
72432
72439
  }
72433
- async function resubmitIfIdle(agentHooks, target2, text6) {
72434
- const status = async () => {
72435
- try {
72436
- return String(findPane(await snapshot(target2.server), target2)?.agent_status ?? "unknown");
72437
- } catch {
72438
- return "unknown";
72439
- }
72440
+ async function deliveryPane(target2, terminal2) {
72441
+ const pane = findPane(await snapshot(target2.server), target2);
72442
+ if (!pane || typeof terminal2 !== "string" || pane.terminal_id !== terminal2 || pane.agent !== target2.source) throw new Error("The terminal instance changed or could not be verified.");
72443
+ if (await paneIdentity(target2.server, pane, true) !== target2.session) throw new Error("The conversation changed or could not be verified.");
72444
+ return pane;
72445
+ }
72446
+ async function observeIdleDelivery(agentHooks, target2, terminal2, text6, id2) {
72447
+ const fail = (reason3) => {
72448
+ agentHooks.deliveries.fail(id2, target2, reason3);
72449
+ return "unsubmitted";
72440
72450
  };
72441
- if (await status() === "working") return await agentHooks.deliveries.awaitLate(target2, text6);
72442
- const late = agentHooks.deliveries.awaitLate(target2, text6);
72443
- await terminalProvider().sendKeys(target2.server, target2.pane, ["enter"]);
72444
- const outcome = await late;
72445
- if (outcome !== "pending") return outcome;
72446
- return await status() === "working" ? "pending" : "unsubmitted";
72451
+ const guards = promptGuards(agentHooks, target2);
72452
+ try {
72453
+ if (!id2) return fail("No delivery identity is available; check the terminal.");
72454
+ const pane = await deliveryPane(target2, terminal2);
72455
+ if (pane.agent_status === "working" && !guards.held()) return await agentHooks.deliveries.awaitLate(target2, text6);
72456
+ if (!["idle", "done"].includes(String(pane.agent_status))) return fail("The agent is not idle or done; observation stopped.");
72457
+ if (guards.held()) return fail("The agent has a held approval, question or terminal prompt; observation stopped.");
72458
+ const screen = await guards.screen();
72459
+ if (!agentHooks.deliveries.pending(target2, text6, id2)) return agentHooks.deliveries.status(id2, target2).state === "delivered" ? "delivered" : "unsubmitted";
72460
+ if (screenDialog(target2.source, screen) || !claudeInputMatches(screen, text6)) return fail("The full message cannot be verified in the input box; check the terminal.");
72461
+ return fail("The message is visible but cannot be safely resubmitted; check and submit it in the terminal.");
72462
+ } catch (error62) {
72463
+ return fail(error62 instanceof Error ? error62.message : "The delivery checks failed; check the terminal.");
72464
+ }
72447
72465
  }
72448
72466
  async function leaveClaudeSubagentView(agentHooks, target2, waitMs = 250) {
72449
72467
  if (target2.source !== "claude") return false;
@@ -72496,7 +72514,7 @@ function retryAfterSubagentView(agentHooks, target2, terminal2, text6, id2, inte
72496
72514
  `);
72497
72515
  const expected = agentHooks.deliveries.expect(target2, text6, 1500, void 0, id2, typeof terminal2 === "string" ? terminal2 : void 0);
72498
72516
  await terminalProvider().prompt(target2.server, target2.pane, text6);
72499
- if (await expected === "pending") await resubmitIfIdle(agentHooks, target2, text6);
72517
+ if (await expected === "pending") await observeIdleDelivery(agentHooks, target2, terminal2, text6, id2);
72500
72518
  };
72501
72519
  const schedule2 = () => {
72502
72520
  const timer = setTimeout(() => void look().catch(() => void 0), intervalMs);
@@ -72504,29 +72522,30 @@ function retryAfterSubagentView(agentHooks, target2, terminal2, text6, id2, inte
72504
72522
  };
72505
72523
  schedule2();
72506
72524
  }
72507
- function followQueuedDelivery(agentHooks, target2, terminal2, text6, intervalMs = 2e3) {
72508
- const started = Date.now();
72509
- let finishedLooks = 0;
72525
+ function followQueuedDelivery(agentHooks, target2, terminal2, text6, id2, intervalMs = 2e3) {
72526
+ let finishedAt;
72527
+ const fail = (reason3) => agentHooks.deliveries.fail(id2, target2, reason3);
72510
72528
  const look = async () => {
72511
- if (!agentHooks.deliveries.pending(target2, text6) || Date.now() - started > 6e5) return;
72512
- let pane;
72529
+ if (!id2 || !agentHooks.deliveries.pending(target2, text6, id2)) return;
72513
72530
  try {
72514
- pane = findPane(await snapshot(target2.server), target2);
72515
- } catch {
72516
- pane = void 0;
72517
- }
72518
- if (!pane) {
72531
+ const pane = await deliveryPane(target2, terminal2);
72532
+ const status = String(pane.agent_status);
72533
+ if (promptGuards(agentHooks, target2).held() || !["working", "idle", "done"].includes(status)) {
72534
+ fail("The agent needs input or its status is unknown; observation stopped.");
72535
+ return;
72536
+ }
72537
+ if (status === "working") finishedAt = void 0;
72538
+ else {
72539
+ finishedAt ??= Date.now();
72540
+ if (Date.now() - finishedAt >= 3e4) {
72541
+ await observeIdleDelivery(agentHooks, target2, terminal2, text6, id2);
72542
+ return;
72543
+ }
72544
+ }
72519
72545
  schedule2();
72520
- return;
72521
- }
72522
- if (pane.terminal_id !== terminal2) return;
72523
- const status = String(pane.agent_status);
72524
- finishedLooks = status === "idle" || status === "done" ? finishedLooks + 1 : 0;
72525
- if (finishedLooks >= 2) {
72526
- await resubmitIfIdle(agentHooks, target2, text6).catch(() => void 0);
72527
- return;
72546
+ } catch {
72547
+ fail("The terminal or conversation could not be verified; observation stopped.");
72528
72548
  }
72529
- schedule2();
72530
72549
  };
72531
72550
  const schedule2 = () => {
72532
72551
  const timer = setTimeout(() => void look(), intervalMs);
@@ -72535,27 +72554,18 @@ function followQueuedDelivery(agentHooks, target2, terminal2, text6, intervalMs
72535
72554
  schedule2();
72536
72555
  }
72537
72556
  async function submitStartingPrompt(server, target2, terminal2, looks = 4, intervalMs = 500) {
72538
- const status = async () => {
72557
+ for (let look = 0; look < looks; look++) {
72558
+ await sleep4(intervalMs);
72539
72559
  try {
72540
72560
  const pane = findPane(await snapshot(server), target2);
72541
- return pane && pane.terminal_id === terminal2 ? String(pane.agent_status) : "gone";
72561
+ if (!pane || pane.terminal_id !== terminal2) return false;
72562
+ if (pane.agent_status === "working") return true;
72563
+ if (!["idle", "done"].includes(String(pane.agent_status))) return false;
72542
72564
  } catch {
72543
- return "unknown";
72544
- }
72545
- };
72546
- const settled4 = async () => {
72547
- for (let look = 0; look < looks; look++) {
72548
- await sleep4(intervalMs);
72549
- const current2 = await status();
72550
- if (current2 !== "idle" && current2 !== "done") return current2;
72565
+ return false;
72551
72566
  }
72552
- return "idle";
72553
- };
72554
- const first = await settled4();
72555
- if (first !== "idle") return first !== "gone";
72556
- await terminalProvider().sendKeys(server, target2.pane, ["enter"]);
72557
- const second = await settled4();
72558
- return second !== "idle" && second !== "gone";
72567
+ }
72568
+ return false;
72559
72569
  }
72560
72570
  function secretKeys(text6) {
72561
72571
  return [...text6].map((character) => character === " " ? "space" : character);
@@ -72779,14 +72789,15 @@ async function paneRouteOnce(ctx, url2, data, response, typing) {
72779
72789
  busy2 = String(current2.agent_status) === "working";
72780
72790
  }, data.hookQueued === true ? 0 : 2e4);
72781
72791
  if (outcome === "pending" && target2.source === "claude" && !text6.trim().startsWith("/")) {
72782
- if (!busy2) outcome = await resubmitIfIdle(agentHooks, target2, text6);
72783
- else followQueuedDelivery(agentHooks, target2, pane.terminal_id, text6);
72792
+ if (!busy2) outcome = await observeIdleDelivery(agentHooks, target2, pane.terminal_id, text6, deliveryId);
72793
+ else followQueuedDelivery(agentHooks, target2, pane.terminal_id, text6, deliveryId);
72784
72794
  }
72785
72795
  if (deliveryId && target2.source === "claude" && outcome !== "delivered") retryAfterSubagentView(agentHooks, target2, pane.terminal_id, text6, deliveryId);
72786
72796
  if (/^\/[a-z][a-z0-9_-]*$/i.test(text6.trim())) agentHooks.menuOpened(target2, text6.trim());
72787
72797
  if (outcome === "delivered") result = { ok: true, delivered: true, state: "delivered" };
72788
72798
  else if (outcome === "unsubmitted") {
72789
- const reason3 = "The agent did not take the message; it may still be in its input line.";
72799
+ const failed3 = deliveryId ? agentHooks.deliveries.status(deliveryId, target2) : void 0;
72800
+ const reason3 = failed3?.state === "failed" && failed3.reason ? failed3.reason : "The agent did not take the message; it may still be in its input line.";
72790
72801
  if (deliveryId) agentHooks.deliveries.fail(deliveryId, target2, reason3);
72791
72802
  result = { ok: true, deliveryUncertain: true, unsubmitted: true, ...deliveryId ? { state: "failed", reason: reason3 } : {} };
72792
72803
  } else {
@@ -72993,6 +73004,7 @@ var init_server_pane_routes = __esm({
72993
73004
  init_git_write_policy();
72994
73005
  init_zod();
72995
73006
  init_prompt_deliveries();
73007
+ init_pane_status();
72996
73008
  init_claude_suggestion();
72997
73009
  init_git();
72998
73010
  init_fanouts();
@@ -87893,7 +87905,7 @@ async function main() {
87893
87905
  return askpass2(args[1]);
87894
87906
  }
87895
87907
  const { runBridge: runBridge2 } = await Promise.resolve().then(() => (init_command(), command_exports));
87896
- return runBridge2(args, "0.3.68");
87908
+ return runBridge2(args, "0.3.69");
87897
87909
  }
87898
87910
  main().then((code) => {
87899
87911
  process.exitCode = code;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phren/cli",
3
- "version": "0.3.68",
3
+ "version": "0.3.69",
4
4
  "description": "Knowledge layer for AI agents - CLI, MCP server, and data layer",
5
5
  "type": "module",
6
6
  "bin": {