rookery-agent 0.5.0 → 0.5.2

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.
@@ -47,7 +47,7 @@ interface PtyProcess {
47
47
  resize(cols: number, rows: number): void;
48
48
  kill(signal?: string): void;
49
49
  }
50
- interface PtyModule {
50
+ export interface PtyModule {
51
51
  spawn(file: string, args: string[] | string, options: {
52
52
  name: string;
53
53
  cols: number;
@@ -133,6 +133,8 @@ export interface TuiSpawnSpec {
133
133
  /** Runs once the process is gone - the temp folders go then, not earlier. */
134
134
  cleanup: () => void;
135
135
  lingerMs?: number;
136
+ /** Tests hand in a fake pty here; otherwise the native binding is loaded. */
137
+ pty?: PtyModule;
136
138
  }
137
139
  export interface TuiRunSpec extends TuiSpawnSpec {
138
140
  prompt: string;
@@ -164,6 +166,8 @@ declare class TerminalWatch {
164
166
  contextTokens: number | undefined;
165
167
  /** The model of the newest assistant entry - `/model` can change it mid-session. */
166
168
  model: string | undefined;
169
+ /** Sub-agents, workflows and background commands the session has not heard back from. */
170
+ readonly work: BackgroundWork;
167
171
  constructor(spec: TuiSpawnSpec,
168
172
  /** A resumed session: its transcript already has a history to skip. */
169
173
  resumed: boolean);
@@ -192,6 +196,75 @@ declare class TerminalWatch {
192
196
  holds: boolean;
193
197
  }>;
194
198
  }
199
+ /** What a session still has running in the background, by kind. */
200
+ export interface PendingWork {
201
+ agents: number;
202
+ workflows: number;
203
+ commands: number;
204
+ /** Of `agents`, those that sent an interim result and may or may not notify again. */
205
+ interim: number;
206
+ }
207
+ /**
208
+ * The work a Claude Code session started in the background and has not heard
209
+ * back from, read off its transcript.
210
+ *
211
+ * The Stop hook fires whenever the agent stops talking - also when it has
212
+ * just sent sub-agents off (`Agent` with `run_in_background`), started a
213
+ * workflow or a background command, and says it will write the report once
214
+ * they are back. Claude Code picks the conversation back up by itself when
215
+ * one of them finishes, so such a stop is not the end of the run.
216
+ *
217
+ * Two sources, because neither covers everything: the launches and their
218
+ * `<task-notification>`s (agents and commands, by id), and the
219
+ * `pendingBackgroundAgentCount` / `pendingWorkflowCount` Claude Code writes
220
+ * on the `turn_duration` entry right after a stop (the only trace of a
221
+ * workflow). That count is a snapshot that stops holding once the
222
+ * conversation goes on, so the next message drops it. Seen live, it also
223
+ * leaves out sub-agents that wait on background work of their own; those stay
224
+ * open here, counted as `interim`, until a notification for them no longer
225
+ * says so.
226
+ */
227
+ export declare class BackgroundWork {
228
+ #private;
229
+ /**
230
+ * A result came back after the agent last spoke: Claude Code wakes the
231
+ * agent with it by itself. Seen live - the notifications land right behind the stop,
232
+ * and a nudge typed then only queues a second, redundant turn.
233
+ */
234
+ resuming: boolean;
235
+ observe(entry: Record<string, unknown>): void;
236
+ pending(): PendingWork;
237
+ }
238
+ export declare const pendingTotal: (pending: PendingWork) => number;
239
+ /** "2 sub-agents and 1 background command", for the card and the timeout message. */
240
+ export declare function describePending(pending: PendingWork): string;
241
+ /** How a run's gate has dealt with its agent's stops so far. */
242
+ export interface StopGate {
243
+ commandNudges: number;
244
+ interimNudged: boolean;
245
+ }
246
+ /** What a stop means for the run: the end, or not yet - and whether the agent is told something. */
247
+ export type StopVerdict = {
248
+ action: 'finish';
249
+ } | {
250
+ action: 'wait';
251
+ detail: string;
252
+ nudge?: string;
253
+ finishAfterMs?: number;
254
+ };
255
+ /**
256
+ * The completion gate. A stop while sub-agents or workflows run is waited
257
+ * out: Claude Code wakes the agent with each result, and the stop after the
258
+ * last one is the real end. Background commands wake nobody once the agent
259
+ * no longer cares about them (a dev server left running), so the agent is
260
+ * asked to settle them. A sub-agent that has only sent an interim result
261
+ * holds the run while the agent's own report still says it is waiting; after
262
+ * a final-sounding one the run ends unless a result arrives within a grace
263
+ * period - live, some such sub-agents notified again and others never did.
264
+ * A report that itself says it is still waiting, with
265
+ * nothing left running, gets one request for the final report.
266
+ */
267
+ export declare function stopVerdict(pending: PendingWork, text: string, gate: StopGate, resuming?: boolean): StopVerdict;
195
268
  /**
196
269
  * Runs Claude Code in a pty and yields what a print run would have yielded.
197
270
  * Throws only before the process exists; afterwards failures are events.
@@ -228,6 +301,14 @@ export interface TuiConversationHandlers {
228
301
  /** The process is gone, however it ended. */
229
302
  onExit(): void;
230
303
  }
304
+ /**
305
+ * What to do about a typed message the transcript has not confirmed yet.
306
+ * Retyping is only for keys that never arrived: a long message - a
307
+ * report-back carrying a whole report - scrolls its first line off the
308
+ * screen once sent, and typing it again while Claude Code worked on it
309
+ * queued every copy into the next turn (the same notice three times over).
310
+ */
311
+ export declare function unconfirmedMessage(screen: string | null, prompt: string): 'enter' | 'wait' | 'retype';
231
312
  /**
232
313
  * The `UserPromptSubmit` hook of a conversation terminal: whatever Rookery
233
314
  * wrote into the context file for this one message goes to Claude Code as
@@ -1 +1 @@
1
- {"version":3,"file":"claude-tui.d.ts","sourceRoot":"","sources":["../../src/providers/claude-tui.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAK3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAe,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAGhE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,4DAA4D;AAC5D,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,QAAQ,CAAC;AAErD,MAAM,WAAW,cAAc;IAC7B,qDAAqD;IACrD,GAAG,EAAE,MAAM,CAAC;IACZ,qEAAqE;IACrE,iBAAiB,EAAE,MAAM,CAAC;IAC1B,KAAK,EAAE,QAAQ,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,wDAAwD;AACxD,UAAU,UAAU;IAClB,MAAM,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC;IAClD,MAAM,CAAC,QAAQ,EAAE,C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AG,IAAI;IA4B7B,4EAA4E;IAC5E,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAiB/B,oFAAoF;IACpF,MAAM,IAAI,IAAI;CAIf;AAqCD;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,IAAI,EAAE,YAAY,EAClB,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,uBAAuB,GAChC,OAAO,CAAC,oBAAoB,CAAC,CAQ/B"}
@@ -2,6 +2,7 @@ import { EventEmitter } from 'node:events';
2
2
  import { access, open, readdir, readFile, stat, writeFile } from 'node:fs/promises';
3
3
  import { homedir } from 'node:os';
4
4
  import { join } from 'node:path';
5
+ import { looksInterim } from '../util/interim-report.js';
5
6
  import { quoteForCmd } from './process.js';
6
7
  import { userSettingsGuard } from './user-settings-guard.js';
7
8
  const MINUTE_MS = 60_000;
@@ -35,6 +36,14 @@ const SETTLE_LIMIT_MS = 3000;
35
36
  const POLL_MS = 250;
36
37
  /** How long after a Stop the transcript is watched for work a blocking hook set going again. */
37
38
  const HOOK_BLOCK_GRACE_MS = 4 * POLL_MS;
39
+ /**
40
+ * Background work finished, Claude Code stopped while it ran, and nothing has
41
+ * picked the work back up for this long: the agent is asked once for its
42
+ * final report instead of leaving the run hanging until its timeout.
43
+ */
44
+ const RESUME_GRACE_MS = 30_000;
45
+ /** How often an agent is asked to settle the background commands it left running. */
46
+ const MAX_COMMAND_NUDGES = 2;
38
47
  /** The slower pace at which a lingering terminal's transcript is followed. */
39
48
  const LATE_POLL_MS = 4 * POLL_MS;
40
49
  const ESC = String.fromCharCode(27);
@@ -502,7 +511,9 @@ function typedPrompt(entry) {
502
511
  return null;
503
512
  text = blocks.map((block) => (block.type === 'text' && typeof block.text === 'string' ? block.text : '')).join('\n');
504
513
  }
505
- text = text.trim();
514
+ // Claude Code 2.1.296+ wraps pasted text in <pasted_content id="..."> tags;
515
+ // read as a wrapper, every report-back looked unsent and was typed again.
516
+ text = text.replace(/<\/?pasted_content(?: id="[^"]*")?>/g, '').trim();
506
517
  if (!text || text.startsWith('<'))
507
518
  return null;
508
519
  return text;
@@ -553,6 +564,8 @@ class TerminalWatch {
553
564
  contextTokens;
554
565
  /** The model of the newest assistant entry - `/model` can change it mid-session. */
555
566
  model;
567
+ /** Sub-agents, workflows and background commands the session has not heard back from. */
568
+ work = new BackgroundWork();
556
569
  #tail = null;
557
570
  /** The read in flight, if any; the next one starts when it is over. */
558
571
  #reading = Promise.resolve();
@@ -594,6 +607,7 @@ class TerminalWatch {
594
607
  const events = [];
595
608
  for (const entry of await tail.read()) {
596
609
  this.lastActivity = Date.now();
610
+ this.work.observe(entry);
597
611
  if (entry.isSidechain === true)
598
612
  continue;
599
613
  const typed = typedPrompt(entry);
@@ -673,6 +687,178 @@ class TerminalWatch {
673
687
  return { events, holds: this.assistantEntries === before };
674
688
  }
675
689
  }
690
+ const TASK_NOTIFICATION = /<task-notification>([\s\S]*?)<\/task-notification>/g;
691
+ const TASK_ID = /<task-id>([^<]+)<\/task-id>/;
692
+ /**
693
+ * A sub-agent that stopped with background work of its own still running
694
+ * (Claude Code 2.1.299). Seen live, the label is no promise either way: some
695
+ * notified again with "finished", others delivered their final result under
696
+ * the same label and never notified again.
697
+ */
698
+ const STILL_WORKING = /is waiting on its own background work|result below may be interim/;
699
+ const countOf = (value) => (typeof value === 'number' && value > 0 ? value : 0);
700
+ /** The text of a transcript entry's message, whether a plain string or text blocks. */
701
+ function entryText(entry) {
702
+ const content = entry.message?.content;
703
+ if (typeof content === 'string')
704
+ return content;
705
+ if (!Array.isArray(content))
706
+ return '';
707
+ return content
708
+ .map((block) => (block.type === 'text' && typeof block.text === 'string' ? block.text : ''))
709
+ .join('\n');
710
+ }
711
+ /**
712
+ * The work a Claude Code session started in the background and has not heard
713
+ * back from, read off its transcript.
714
+ *
715
+ * The Stop hook fires whenever the agent stops talking - also when it has
716
+ * just sent sub-agents off (`Agent` with `run_in_background`), started a
717
+ * workflow or a background command, and says it will write the report once
718
+ * they are back. Claude Code picks the conversation back up by itself when
719
+ * one of them finishes, so such a stop is not the end of the run.
720
+ *
721
+ * Two sources, because neither covers everything: the launches and their
722
+ * `<task-notification>`s (agents and commands, by id), and the
723
+ * `pendingBackgroundAgentCount` / `pendingWorkflowCount` Claude Code writes
724
+ * on the `turn_duration` entry right after a stop (the only trace of a
725
+ * workflow). That count is a snapshot that stops holding once the
726
+ * conversation goes on, so the next message drops it. Seen live, it also
727
+ * leaves out sub-agents that wait on background work of their own; those stay
728
+ * open here, counted as `interim`, until a notification for them no longer
729
+ * says so.
730
+ */
731
+ export class BackgroundWork {
732
+ #open = new Map();
733
+ /** Open sub-agents whose last notification was an interim one. */
734
+ #interim = new Set();
735
+ #reported = { agents: 0, workflows: 0 };
736
+ /**
737
+ * A result came back after the agent last spoke: Claude Code wakes the
738
+ * agent with it by itself. Seen live - the notifications land right behind the stop,
739
+ * and a nudge typed then only queues a second, redundant turn.
740
+ */
741
+ resuming = false;
742
+ observe(entry) {
743
+ if (entry.isSidechain === true)
744
+ return;
745
+ if (entry.type === 'system' && entry.subtype === 'turn_duration') {
746
+ this.#reported = {
747
+ agents: countOf(entry.pendingBackgroundAgentCount),
748
+ workflows: countOf(entry.pendingWorkflowCount),
749
+ };
750
+ return;
751
+ }
752
+ if (entry.type !== 'user' && entry.type !== 'assistant')
753
+ return;
754
+ this.#reported = { agents: 0, workflows: 0 };
755
+ if (entry.type !== 'user') {
756
+ this.resuming = false;
757
+ return;
758
+ }
759
+ const result = entry.toolUseResult;
760
+ if (result?.isAsync === true && typeof result.agentId === 'string')
761
+ this.#open.set(result.agentId, 'agents');
762
+ if (typeof result?.backgroundTaskId === 'string')
763
+ this.#open.set(result.backgroundTaskId, 'commands');
764
+ for (const [, block = ''] of entryText(entry).matchAll(TASK_NOTIFICATION)) {
765
+ const id = block.match(TASK_ID)?.[1]?.trim();
766
+ if (!id)
767
+ continue;
768
+ this.resuming = true;
769
+ if (STILL_WORKING.test(block)) {
770
+ this.#interim.add(id);
771
+ }
772
+ else {
773
+ this.#open.delete(id);
774
+ this.#interim.delete(id);
775
+ }
776
+ }
777
+ }
778
+ pending() {
779
+ const open = [...this.#open.values()];
780
+ const agents = open.filter((kind) => kind === 'agents').length;
781
+ const interim = [...this.#interim].filter((id) => this.#open.has(id)).length;
782
+ return {
783
+ // Claude Code's own count leaves the interim ones out.
784
+ agents: Math.max(agents - interim, this.#reported.agents) + interim,
785
+ workflows: this.#reported.workflows,
786
+ commands: open.length - agents,
787
+ interim,
788
+ };
789
+ }
790
+ }
791
+ export const pendingTotal = (pending) => pending.agents + pending.workflows + pending.commands;
792
+ /** "2 sub-agents and 1 background command", for the card and the timeout message. */
793
+ export function describePending(pending) {
794
+ const parts = [];
795
+ const add = (count, one, many) => {
796
+ if (count > 0)
797
+ parts.push(count + ' ' + (count === 1 ? one : many));
798
+ };
799
+ add(pending.agents, 'sub-agent', 'sub-agents');
800
+ add(pending.workflows, 'workflow', 'workflows');
801
+ add(pending.commands, 'background command', 'background commands');
802
+ const last = parts.pop() ?? 'nothing';
803
+ return parts.length ? parts.join(', ') + ' and ' + last : last;
804
+ }
805
+ /**
806
+ * How long a final-sounding report waits for sub-agents that only sent an
807
+ * interim result. ponytail: a fixed grace; a sub-agent that reports again
808
+ * later than this after a report that did not sound like waiting is cut off.
809
+ */
810
+ const INTERIM_AGENT_GRACE_MS = 2 * MINUTE_MS;
811
+ const COMMANDS_NUDGE = '[Rookery] Your run only ends once every background command you started has finished. ' +
812
+ 'If you still need their output, wait for it; if not, stop them with TaskStop. Then give your final report.';
813
+ const INTERIM_NUDGE = '[Rookery] Your last message says you are still waiting for something, but nothing you started is running any more. ' +
814
+ 'Finish the work now - what you answer next is the final report.';
815
+ const RESUME_NUDGE = '[Rookery] All background work you started has finished. Use its results and give your final report now.';
816
+ /**
817
+ * The completion gate. A stop while sub-agents or workflows run is waited
818
+ * out: Claude Code wakes the agent with each result, and the stop after the
819
+ * last one is the real end. Background commands wake nobody once the agent
820
+ * no longer cares about them (a dev server left running), so the agent is
821
+ * asked to settle them. A sub-agent that has only sent an interim result
822
+ * holds the run while the agent's own report still says it is waiting; after
823
+ * a final-sounding one the run ends unless a result arrives within a grace
824
+ * period - live, some such sub-agents notified again and others never did.
825
+ * A report that itself says it is still waiting, with
826
+ * nothing left running, gets one request for the final report.
827
+ */
828
+ export function stopVerdict(pending, text, gate, resuming = false) {
829
+ const total = pendingTotal(pending);
830
+ if (total > pending.interim || (total > 0 && looksInterim(text))) {
831
+ const detail = 'waiting for ' + describePending(pending) + ' before the final report';
832
+ if (pending.agents + pending.workflows > 0 || gate.commandNudges >= MAX_COMMAND_NUDGES)
833
+ return { action: 'wait', detail };
834
+ gate.commandNudges += 1;
835
+ return { action: 'wait', detail, nudge: COMMANDS_NUDGE };
836
+ }
837
+ if (resuming)
838
+ return { action: 'wait', detail: 'all background work is back and Claude Code is picking up the results' };
839
+ if (total > 0) {
840
+ return {
841
+ action: 'wait',
842
+ detail: 'giving ' + describePending(pending) + ' with an interim result a moment to report again',
843
+ finishAfterMs: INTERIM_AGENT_GRACE_MS,
844
+ };
845
+ }
846
+ if (!gate.interimNudged && looksInterim(text)) {
847
+ gate.interimNudged = true;
848
+ return {
849
+ action: 'wait',
850
+ detail: 'the report said it was still waiting for something; asked for the final report',
851
+ nudge: INTERIM_NUDGE,
852
+ };
853
+ }
854
+ return { action: 'finish' };
855
+ }
856
+ /** Types one line into a run's terminal and sends it, the way a person would. */
857
+ function nudgeTerminal(key, text) {
858
+ if (!tuiSessions.write(key, text))
859
+ return;
860
+ setTimeout(() => tuiSessions.write(key, '\r'), DIALOG_KEY_DELAY_MS);
861
+ }
676
862
  /**
677
863
  * The prompt as it goes on the command line. Through a `.cmd` shim the whole
678
864
  * command line is cmd.exe's to parse, and a task is outside text - a mail can
@@ -735,7 +921,7 @@ function startupDialogAnswerer(child, watch) {
735
921
  }
736
922
  /** Starts Claude Code in a pty and registers it. Throws only before the process exists. */
737
923
  async function spawnTerminal(spec, resumed) {
738
- const pty = await loadPty();
924
+ const pty = spec.pty ?? (await loadPty());
739
925
  if (!pty)
740
926
  throw new Error('Terminal support is not available on this system.');
741
927
  const prompt = await promptArgument(spec);
@@ -803,6 +989,14 @@ export async function* runTui(spec) {
803
989
  const watch = await spawnTerminal(spec, false);
804
990
  const started = Date.now();
805
991
  let finished = false;
992
+ const gate = { commandNudges: 0, interimNudged: false };
993
+ /** Set once a stop was not the end: the agent waits for its background work. */
994
+ let parked = false;
995
+ /** Parked on work that was still running; cleared, and said, once it is all back. */
996
+ let awaitingWork = false;
997
+ let resumeNudged = false;
998
+ /** A final-sounding report held for interim sub-agents: it stands unless the agent speaks again in time. */
999
+ let held = null;
806
1000
  try {
807
1001
  yield { type: 'status', label: 'terminal', detail: 'Claude Code is running in a terminal.' };
808
1002
  while (true) {
@@ -823,16 +1017,51 @@ export async function* runTui(spec) {
823
1017
  yield fatalError(watch.apiError);
824
1018
  return;
825
1019
  }
1020
+ const text = reported || watch.lastBlock;
1021
+ const verdict = stopVerdict(watch.work.pending(), text, gate, watch.work.resuming);
1022
+ if (verdict.action === 'wait') {
1023
+ // Not the end: the next stop is judged again once the work is back.
1024
+ await watch.clearMarker();
1025
+ parked = true;
1026
+ awaitingWork = pendingTotal(watch.work.pending()) > 0;
1027
+ resumeNudged = false;
1028
+ held = verdict.finishAfterMs
1029
+ ? { text, until: Date.now() + verdict.finishAfterMs, entries: watch.assistantEntries }
1030
+ : null;
1031
+ yield { type: 'status', label: 'waiting', detail: verdict.detail };
1032
+ if (verdict.nudge)
1033
+ nudgeTerminal(spec.key, verdict.nudge);
1034
+ continue;
1035
+ }
826
1036
  finished = true;
827
1037
  yield {
828
1038
  type: 'done',
829
- text: reported || watch.lastBlock,
1039
+ text,
830
1040
  providerSessionId: spec.sessionId,
831
1041
  usage: watch.usage(started),
832
1042
  };
833
1043
  return;
834
1044
  }
835
- if (Date.now() - watch.lastActivity > IDLE_LIMIT_MS) {
1045
+ if (held && watch.assistantEntries > held.entries)
1046
+ held = null;
1047
+ if (held && Date.now() > held.until) {
1048
+ finished = true;
1049
+ yield { type: 'done', text: held.text, providerSessionId: spec.sessionId, usage: watch.usage(started) };
1050
+ return;
1051
+ }
1052
+ // Sub-agents can work for a long while without a line in this
1053
+ // transcript; the run's own timeout bounds that wait, not the idle limit.
1054
+ const waiting = parked && pendingTotal(watch.work.pending()) > 0;
1055
+ if (awaitingWork && !waiting) {
1056
+ // Nothing is out any more; a timeout from here on is the report, not the wait.
1057
+ awaitingWork = false;
1058
+ yield { type: 'status', label: 'resumed', detail: 'all background work is back; the final report is being written' };
1059
+ }
1060
+ if (parked && !waiting && !resumeNudged && Date.now() - watch.lastActivity > RESUME_GRACE_MS) {
1061
+ resumeNudged = true;
1062
+ nudgeTerminal(spec.key, RESUME_NUDGE);
1063
+ }
1064
+ if (!waiting && Date.now() - watch.lastActivity > IDLE_LIMIT_MS) {
836
1065
  yield fatalError(IDLE_MESSAGE);
837
1066
  return;
838
1067
  }
@@ -910,6 +1139,25 @@ function commandOutput(screen, command) {
910
1139
  }
911
1140
  /** Lines Claude Code draws under its input box, and only there. */
912
1141
  const INPUT_FOOTERS = ['shift+tab to cycle', 'for shortcuts'];
1142
+ /** What Claude Code's spinner line says while it works on a message. */
1143
+ const WORKING_HINT = 'esc to interrupt';
1144
+ /**
1145
+ * What to do about a typed message the transcript has not confirmed yet.
1146
+ * Retyping is only for keys that never arrived: a long message - a
1147
+ * report-back carrying a whole report - scrolls its first line off the
1148
+ * screen once sent, and typing it again while Claude Code worked on it
1149
+ * queued every copy into the next turn (the same notice three times over).
1150
+ */
1151
+ export function unconfirmedMessage(screen, prompt) {
1152
+ if (screen === null)
1153
+ return 'enter';
1154
+ if (screen.includes(WORKING_HINT))
1155
+ return 'wait';
1156
+ const flat = screen.replace(/\s+/g, ' ');
1157
+ const text = prompt.replace(/\s+/g, ' ').trim();
1158
+ const shown = [text.slice(0, SNIPPET_LENGTH), text.slice(-SNIPPET_LENGTH)].some((snippet) => flat.includes(snippet));
1159
+ return shown || screen.includes('[Pasted text') ? 'enter' : 'retype';
1160
+ }
913
1161
  /**
914
1162
  * The `UserPromptSubmit` hook of a conversation terminal: whatever Rookery
915
1163
  * wrote into the context file for this one message goes to Claude Code as
@@ -1089,23 +1337,22 @@ export class ConversationTerminal {
1089
1337
  }
1090
1338
  /**
1091
1339
  * The message has to arrive. Every few seconds without it in the
1092
- * transcript, the screen says why: the text sits in the input box (a
1093
- * paste the TUI held on to) and gets another Enter, or it is not there at
1094
- * all (keys swallowed while the TUI was still setting up) and is typed
1095
- * again. Past the limit it is an error, not a turn waiting for ever.
1340
+ * transcript, the screen says why (`unconfirmedMessage`): the text is
1341
+ * still there and gets another Enter, Claude Code is already working on it
1342
+ * and the transcript has yet to catch up, or it is not there at all (keys
1343
+ * swallowed while the TUI was still setting up) and is typed again. Past
1344
+ * the limit it is an error, not a turn waiting for ever.
1096
1345
  */
1097
1346
  async #ensureAccepted(pending) {
1098
1347
  const typedAt = Date.now();
1099
- const snippet = pending.prompt.replace(/\s+/g, ' ').trim().slice(0, SNIPPET_LENGTH);
1100
1348
  while (Date.now() - typedAt < ACCEPT_DEADLINE_MS) {
1101
1349
  await sleep(ACCEPT_RETRY_MS);
1102
1350
  if (this.#pending !== pending || pending.accepted)
1103
1351
  return;
1104
- const screen = tuiSessions.screenText(this.key);
1105
- const waiting = screen === null || screen.includes('[Pasted text') || screen.replace(/\s+/g, ' ').includes(snippet);
1106
- if (waiting)
1352
+ const next = unconfirmedMessage(tuiSessions.screenText(this.key), pending.prompt);
1353
+ if (next === 'enter')
1107
1354
  tuiSessions.write(this.key, '\r');
1108
- else
1355
+ else if (next === 'retype')
1109
1356
  this.#type(pending.prompt);
1110
1357
  }
1111
1358
  if (this.#pending !== pending || pending.accepted)