xonecas.io
64875
Mc

mini-coder / commit 99f81c1

feat: queue steering messages mid-turn

xonecas2026-04-15 99f81c1f938deb73168e06cdf39662a669dc20eb parent 92878b3

13 files changed

TODO.md+5−0
@@ -5,8 +5,13 @@ Don't keep completed items bloating the file, remove them before committing.
55 Keep the file structured and minimal.
66 The spec.md and code are the sources of truth, not this file, don't assume anything because it's documented here.
77
8+## In progress
9+
10+- [ ] Terminal-Bench leaderboard run `leaderboard-2026-04-13__15-33-42` is running in the background using the published npm package `mini-coder@0.5.6`. Do not publish a new version or otherwise interfere with this benchmark setup until the run completes.
11+
812 ## Open items
913
14+- [ ] Headless mode and json output should be separated, without --json headless mode outputs the final response only.
1015 - [ ] Invalid `settings.json` is fatal instead of falling back to no saved settings (`src/settings.ts`)
1116
1217 ## Low priority (deferred until further notice)
spec.md+13−7
@@ -200,7 +200,7 @@ Plugins are declared in a config file (`~/.config/mini-coder/plugins.json` or si
200200
201201 ## Agent loop
202202
203-The core runtime. Streaming is the default behavior throughout the turn: user-visible state should update incrementally as the model emits text, thinking, and tool-call events, rather than waiting for whole responses to complete. This is what happens on each turn:
203+The core runtime. Streaming is the default behavior throughout the turn: user-visible state should update incrementally as the model emits text, thinking, and tool-call events, rather than waiting for whole responses to complete. This is what happens while the agent is active:
204204
205205 1. **User submits a message** — the input text (plus any embedded images or skill bodies from `/skill:name`) becomes a pi-ai `UserMessage`. It is appended to the session's message history, rendered in the UI immediately, and persisted to the DB.
206206
@@ -209,15 +209,21 @@ The core runtime. Streaming is the default behavior throughout the turn: user-vi
209209 3. **Stream to LLM** — call `streamSimple(model, context, options)` from pi-ai. Iterate over the event stream:
210210 - `text_delta` / `thinking_delta` / `toolcall_delta` → update the in-progress assistant message and the UI incrementally (stream raw markdown text, show thinking if enabled, accumulate tool call arguments as they arrive).
211211 - `toolcall_end` → finalize the structured tool call in the in-progress `AssistantMessage`.
212- - `done` → append the `AssistantMessage` to history and DB. Update cumulative stats. If `stopReason` is `"toolUse"`, go to step 4. If `"stop"` or `"length"`, return to the input prompt.
212+ - `done` → append the `AssistantMessage` to history and DB. Update cumulative stats. If `stopReason` is `"toolUse"`, go to step 4. If `"stop"` or `"length"` and queued steering messages exist, go to step 5. Otherwise return to the input prompt.
213213 - `error` → display error in the log, return to the input prompt.
214214
215215 4. **Tool execution** — when the LLM requests tool calls:
216216 - Execute each tool call. For `shell`: run the command, capture output, truncate if needed. For `edit`: perform the replacement. For `readImage`: read and base64-encode the file. For plugin tools: delegate to the plugin.
217217 - Each result becomes a `ToolResultMessage` appended to history and DB (same turn number).
218- - After all tool results are appended, loop back to step 2 (re-stream with the updated context).
218+ - After all tool results are appended, if queued steering messages exist go to step 5; otherwise loop back to step 2 (re-stream with the updated context).
219219
220-5. **Interrupt** — if the user presses `Escape` during streaming with no overlay open:
220+5. **Steering messages** — if the user submits a model-visible message while an active run is already in progress, mini-coder queues it instead of dropping it or interrupting the current work.
221+ - Steering messages are consumed FIFO at the next model-request boundary: after the current assistant message is finalized and after any tool calls already requested by that assistant message have completed and their tool results have been appended.
222+ - When consumed, each steering message is appended to history and DB as a normal `UserMessage`, starting a new conversational turn.
223+ - After appending the next queued steering message, loop back to step 2 so the next `streamSimple(...)` call sees the updated history.
224+ - Submitting a steering message does not abort the current stream or running tool. Use `Escape` to interrupt immediately.
225+
226+6. **Interrupt** — if the user presses `Escape` during streaming with no overlay open:
221227 - Abort the stream via `AbortSignal`.
222228 - The partial `AssistantMessage` (with `stopReason: "aborted"`) is appended to history and DB as-is. This preserves context so the LLM knows what it was doing when interrupted.
223229 - Return to the input prompt with focus on the input. The user can continue the conversation or `/undo` the interrupted turn.
@@ -692,7 +698,7 @@ frame3: ────────────────────────
692698
693699 ### Input area
694700
695-Multi-line text input with no prompt prefix — the blinking cursor is the affordance. Intrinsic height starts at 2 lines when empty (`minHeight: 2`), grows with content up to `maxHeight: 10`, then scrolls internally. Enter submits, Shift+Enter adds newlines (via cel-tui's `TextInput` `onKeyPress` pattern).
701+Multi-line text input with no prompt prefix — the blinking cursor is the affordance. Intrinsic height starts at 2 lines when empty (`minHeight: 2`), grows with content up to `maxHeight: 10`, then scrolls internally. Enter submits, Shift+Enter adds newlines (via cel-tui's `TextInput` `onKeyPress` pattern). If the agent is already working, a submitted model-visible message becomes a queued steering message rather than interrupting or disappearing; use `Escape` to interrupt immediately.
696702
697703 Supports:
698704
@@ -910,11 +916,11 @@ CREATE INDEX idx_prompt_history_created_at ON prompt_history(created_at, id);
910916
911917 **Messages as JSON blobs**: pi-ai's `Message` type (UserMessage, AssistantMessage, ToolResultMessage) is already serializable, and we also persist internal UI-only log entries in the same table. We store the full object as JSON in `data` rather than normalizing into columns. Session load returns the complete chronological log; model context construction filters out UI-only messages before replaying the remaining pi-ai messages into a pi-ai `Context`.
912918
913-**Turn grouping**: the `turn` column groups only conversational messages. A turn is: one user message + one or more assistant messages + any tool result messages from that agent loop. Persisted UI messages are stored alongside them but have `turn = NULL`, so they remain visible in session history without becoming part of `/undo`. `/fork` copies all existing persisted messages to a new session, preserving conversational turn numbers and existing UI messages as-is, then appends a new UI-only notic …
919+**Turn grouping**: the `turn` column groups only conversational messages. A turn is: one user message + the assistant messages and tool result messages produced after that user message until the next user message is consumed. Persisted UI messages are stored alongside them but have `turn = NULL`, so they remain visible in session history without becoming part of `/undo`. Queued steering messages submitted mid-run still start their own turn when they are consumed; tool results already requested b …
914920
915921 **Cumulative stats are computed, not stored**: the status bar's cumulative `in`, `out`, and `$cost` values are computed by summing `usage` from assistant messages on session load (deserialize all messages, filter for `role: "assistant"`, sum their `usage` fields). The message count per session is small (hundreds), so this is fast. No separate counters to keep in sync. During an active session, a running in-memory accumulator is updated after each assistant message to avoid re-scanning. `context% …
916922
917-**Turn number assignment**: when a user message is appended, its turn number is `MAX(turn) + 1` for the session (or 1 for the first conversational message). All subsequent assistant responses and tool results in the same agent loop share that turn number. UI-only messages do not receive a turn number. This is what makes `/undo` atomic for conversation history without removing system notices that are still true after the undo.
923+**Turn number assignment**: when a user message is appended, whether from an idle submission or a queued steering message that is being consumed, its turn number is `MAX(turn) + 1` for the session (or 1 for the first conversational message). Assistant responses and tool results keep the current active turn number until another queued user message is consumed and becomes the next model input. UI-only messages do not receive a turn number. This is what makes `/undo` atomic for conversation history …
918924
919925 **Model/effort on the session**: stored at creation for display in `/session` list. The user can change models and effort mid-session via `/model` and `/effort`; those commands update the current in-memory state and global user settings, but the session record still reflects the initial values for that session. Individual assistant messages record their actual model via pi-ai's `AssistantMessage.model`.
920926
src/agent.test.ts+115−0
@@ -334,6 +334,121 @@ describe("agent loop", () => {
334334 expect(loadMessages(db, session.id)).toHaveLength(0);
335335 });
336336
337+ test("consumes queued steering messages one at a time after stop responses in FIFO turn order", async () => {
338+ faux.setResponses([
339+ fauxAssistantMessage("First response."),
340+ fauxAssistantMessage("Second response."),
341+ fauxAssistantMessage("Third response."),
342+ ]);
343+
344+ const events: AgentEvent[] = [];
345+ const session = createSession(db, { cwd: tmp });
346+ const userMsg = makeUser("initial prompt");
347+ const turn = appendMessage(db, session.id, userMsg);
348+ const queuedMessages = [makeUser("steer one"), makeUser("steer two")];
349+
350+ const result = await runAgentLoop({
351+ db,
352+ sessionId: session.id,
353+ turn,
354+ model: faux.getModel(),
355+ systemPrompt: "Test",
356+ tools: [],
357+ toolHandlers: new Map(),
358+ messages: [userMsg],
359+ cwd: tmp,
360+ takeQueuedUserMessage: () => queuedMessages.shift() ?? null,
361+ onEvent: (event) => events.push(event),
362+ });
363+
364+ expect(result.stopReason).toBe("stop");
365+ expect(result.messages.map((message) => message.role)).toEqual([
366+ "user",
367+ "assistant",
368+ "user",
369+ "assistant",
370+ "user",
371+ "assistant",
372+ ]);
373+ expect(result.messages[2]).toMatchObject({
374+ role: "user",
375+ content: "steer one",
376+ });
377+ expect(result.messages[4]).toMatchObject({
378+ role: "user",
379+ content: "steer two",
380+ });
381+
382+ const dbTurns = db
383+ .query<{ turn: number | null }, [string]>(
384+ "SELECT turn FROM messages WHERE session_id = ? ORDER BY id",
385+ )
386+ .all(session.id)
387+ .map((row) => row.turn);
388+ expect(dbTurns).toEqual([1, 1, 2, 2, 3, 3]);
389+
390+ const queuedUserEvents = events.filter(
391+ (event) => event.type === "user_message",
392+ );
393+ expect(queuedUserEvents).toHaveLength(2);
394+ expect(queuedUserEvents[0]).toMatchObject({
395+ type: "user_message",
396+ message: { role: "user", content: "steer one" },
397+ });
398+ expect(queuedUserEvents[1]).toMatchObject({
399+ type: "user_message",
400+ message: { role: "user", content: "steer two" },
401+ });
402+ });
403+
404+ test("consumes a queued steering message after tool results and starts a new turn", async () => {
405+ faux.setResponses([
406+ fauxAssistantMessage([fauxToolCall("shell", { command: "echo hi" })], {
407+ stopReason: "toolUse",
408+ }),
409+ fauxAssistantMessage("Handled steering."),
410+ ]);
411+
412+ const session = createSession(db, { cwd: tmp });
413+ const userMsg = makeUser("initial prompt");
414+ const turn = appendMessage(db, session.id, userMsg);
415+ const queuedMessages = [makeUser("steer after tool")];
416+
417+ const result = await runAgentLoop({
418+ db,
419+ sessionId: session.id,
420+ turn,
421+ model: faux.getModel(),
422+ systemPrompt: "Test",
423+ tools: builtinToolDefs(),
424+ toolHandlers: builtinToolHandlers(),
425+ messages: [userMsg],
426+ cwd: tmp,
427+ takeQueuedUserMessage: () => queuedMessages.shift() ?? null,
428+ });
429+
430+ expect(result.stopReason).toBe("stop");
431+ expect(result.messages.map((message) => message.role)).toEqual([
432+ "user",
433+ "assistant",
434+ "toolResult",
435+ "user",
436+ "assistant",
437+ ]);
438+ expect(result.messages[3]).toMatchObject({
439+ role: "user",
440+ content: "steer after tool",
441+ });
442+
443+ const dbTurns = db
444+ .query<{ turn: number | null }, [string]>(
445+ "SELECT turn FROM messages WHERE session_id = ? ORDER BY id",
446+ )
447+ .all(session.id)
448+ .map((row) => row.turn);
449+ expect(dbTurns).toEqual([1, 1, 1, 2, 2]);
450+ });
451+
337452 test("emits events during streaming", async () => {
338453 faux.setResponses([fauxAssistantMessage("Hello world")]);
339454
src/agent.ts+141−29
@@ -18,6 +18,7 @@ import type {
1818 Tool,
1919 ToolCall,
2020 ToolResultMessage,
21+ UserMessage,
2122 } from "@mariozechner/pi-ai";
2223 import { streamSimple } from "@mariozechner/pi-ai";
2324 import { appendMessage } from "./session.ts";
@@ -79,6 +80,7 @@ export type AgentEvent =
7980 args: Record<string, unknown>;
8081 content: AssistantMessage["content"];
8182 }
83+ | { type: "user_message"; message: UserMessage }
8284 | { type: "assistant_message"; message: AssistantMessage }
8385 | {
8486 type: "tool_start";
@@ -131,6 +133,8 @@ interface RunAgentOpts {
131133 signal?: AbortSignal;
132134 /** Callback for UI events. */
133135 onEvent?: (event: AgentEvent) => void;
136+ /** Dequeue the next queued steering message, if one is waiting. */
137+ takeQueuedUserMessage?: () => UserMessage | null;
134138 }
135139
136140 /** Result of the agent loop. */
@@ -462,6 +466,19 @@ async function streamAssistantMessage(
462466 return mergeAssistantMessage(partialAssistantMessage, finalAssistantMessage);
463467 }
464468
469+function appendUserMessage(
470+ db: Database,
471+ sessionId: string,
472+ messages: Message[],
473+ userMessage: UserMessage,
474+ onEvent: RunAgentOpts["onEvent"],
475+): number {
476+ messages.push(userMessage);
477+ const turn = appendMessage(db, sessionId, userMessage);
478+ onEvent?.({ type: "user_message", message: userMessage });
479+ return turn;
480+}
481+
465482 function appendAssistantMessage(
466483 db: Database,
467484 sessionId: string,
@@ -490,15 +507,22 @@ function resolveLoopStopReason(
490507 return { messages, stopReason: assistantMessage.stopReason };
491508 }
492509
493- if (
494- assistantMessage.stopReason === "stop" ||
495- assistantMessage.stopReason === "length"
496- ) {
497- onEvent?.({ type: "done", message: assistantMessage });
498- return { messages, stopReason: assistantMessage.stopReason };
510+ return null;
511+}
512+
513+function consumeQueuedUserMessage(
514+ db: Database,
515+ sessionId: string,
516+ messages: Message[],
517+ takeQueuedUserMessage: RunAgentOpts["takeQueuedUserMessage"],
518+ onEvent: RunAgentOpts["onEvent"],
519+): number | null {
520+ const queuedUserMessage = takeQueuedUserMessage?.();
521+ if (!queuedUserMessage) {
522+ return null;
499523 }
500524
501- return null;
525+ return appendUserMessage(db, sessionId, messages, queuedUserMessage, onEvent);
502526 }
503527
504528 function getAssistantToolCalls(message: AssistantMessage): ToolCall[] {
@@ -581,6 +605,72 @@ function appendToolResultMessage(
581605 onEvent?.({ type: "tool_result", message: toolResultMessage });
582606 }
583607
608+function finalizeStoppedAssistantMessage(
609+ db: Database,
610+ sessionId: string,
611+ messages: Message[],
612+ assistantMessage: AssistantMessage,
613+ stopReason: "stop" | "length",
614+ takeQueuedUserMessage: RunAgentOpts["takeQueuedUserMessage"],
615+ onEvent: RunAgentOpts["onEvent"],
616+): AgentLoopResult | number {
617+ const queuedTurn = consumeQueuedUserMessage(
618+ db,
619+ sessionId,
620+ messages,
621+ takeQueuedUserMessage,
622+ onEvent,
623+ );
624+ if (queuedTurn !== null) {
625+ return queuedTurn;
626+ }
627+
628+ onEvent?.({ type: "done", message: assistantMessage });
629+ return { messages, stopReason };
630+}
631+
632+async function executeAssistantToolCalls(
633+ assistantMessage: AssistantMessage,
634+ opts: Pick<
635+ RunAgentOpts,
636+ | "db"
637+ | "sessionId"
638+ | "messages"
639+ | "toolHandlers"
640+ | "cwd"
641+ | "signal"
642+ | "onEvent"
643+ | "model"
644+ >,
645+ turn: number,
646+): Promise<AgentLoopResult | null> {
647+ for (const toolCall of getAssistantToolCalls(assistantMessage)) {
648+ const result = await executeToolCall(toolCall, opts);
649+ appendToolResultMessage(
650+ opts.db,
651+ opts.sessionId,
652+ opts.messages,
653+ toolCall,
654+ result,
655+ turn,
656+ opts.onEvent,
657+ );
658+
659+ if (opts.signal?.aborted) {
660+ opts.onEvent?.({
661+ type: "aborted",
662+ message: buildIncompleteAssistantMessage(
663+ { model: opts.model, signal: opts.signal },
664+ assistantMessage,
665+ ),
666+ });
667+ return { messages: opts.messages, stopReason: "aborted" };
668+ }
669+ }
670+
671+ return null;
672+}
673+
584674 // ---------------------------------------------------------------------------
585675 // Agent loop
586676 // ---------------------------------------------------------------------------
@@ -609,7 +699,9 @@ export async function runAgentLoop(
609699 onEvent,
610700 toolHandlers,
611701 cwd,
702+ takeQueuedUserMessage,
612703 } = opts;
704+ let currentTurn = turn;
613705
614706 while (true) {
615707 const assistantMessage = await streamAssistantMessage(opts);
@@ -618,7 +710,7 @@ export async function runAgentLoop(
618710 sessionId,
619711 messages,
620712 assistantMessage,
621- turn,
713+ currentTurn,
622714 onEvent,
623715 );
624716
@@ -631,33 +723,53 @@ export async function runAgentLoop(
631723 return stopResult;
632724 }
633725
634- for (const toolCall of getAssistantToolCalls(assistantMessage)) {
635- const result = await executeToolCall(toolCall, {
636- toolHandlers,
637- cwd,
638- signal,
639- onEvent,
640- });
641- appendToolResultMessage(
726+ if (
727+ assistantMessage.stopReason === "stop" ||
728+ assistantMessage.stopReason === "length"
729+ ) {
730+ const finalResult = finalizeStoppedAssistantMessage(
642731 db,
643732 sessionId,
644733 messages,
645- toolCall,
646- result,
647- turn,
734+ assistantMessage,
735+ assistantMessage.stopReason,
736+ takeQueuedUserMessage,
648737 onEvent,
649738 );
650-
651- if (signal?.aborted) {
652- onEvent?.({
653- type: "aborted",
654- message: buildIncompleteAssistantMessage(
655- { model, signal },
656- assistantMessage,
657- ),
658- });
659- return { messages, stopReason: "aborted" };
739+ if (typeof finalResult === "number") {
740+ currentTurn = finalResult;
741+ continue;
660742 }
743+ return finalResult;
744+ }
745+
746+ const toolStopResult = await executeAssistantToolCalls(
747+ assistantMessage,
748+ {
749+ db,
750+ sessionId,
751+ messages,
752+ toolHandlers,
753+ cwd,
754+ signal,
755+ onEvent,
756+ model,
757+ },
758+ currentTurn,
759+ );
760+ if (toolStopResult) {
761+ return toolStopResult;
762+ }
763+
764+ const queuedTurn = consumeQueuedUserMessage(
765+ db,
766+ sessionId,
767+ messages,
768+ takeQueuedUserMessage,
769+ onEvent,
770+ );
771+ if (queuedTurn !== null) {
772+ currentTurn = queuedTurn;
661773 }
662774 }
663775 }
src/headless.test.ts+1−0
@@ -55,6 +55,7 @@ function createTestState(): AppState {
5555 running: false,
5656 abortController: null,
5757 activeTurnPromise: null,
58+ queuedUserMessages: [],
5859 showReasoning: true,
5960 verbose: false,
6061 versionLabel: "dev",
src/index.test.ts+1−0
@@ -51,6 +51,7 @@ function createTestState(plugins: LoadedPlugin[] = []): AppState {
5151 running: false,
5252 abortController: null,
5353 activeTurnPromise: null,
54+ queuedUserMessages: [],
5455 showReasoning: true,
5556 verbose: false,
5657 versionLabel: "dev",
src/index.ts+4−0
@@ -19,6 +19,7 @@ import type {
1919 OAuthCredentials,
2020 ThinkingLevel,
2121 Tool,
22+ UserMessage,
2223 } from "@mariozechner/pi-ai";
2324 import { getEnvApiKey, getModels, getProviders } from "@mariozechner/pi-ai";
2425 import { getOAuthApiKey, getOAuthProviders } from "@mariozechner/pi-ai/oauth";
@@ -561,6 +562,8 @@ export interface AppState {
561562 abortController: AbortController | null;
562563 /** Promise for the active conversational turn, used to serialize cleanup like `/undo`. */
563564 activeTurnPromise: Promise<void> | null;
565+ /** Resolved user messages queued while the current run is still active. */
566+ queuedUserMessages: UserMessage[];
564567 /** Whether to show thinking content. */
565568 showReasoning: boolean;
566569 /** Whether to show full (un-truncated) tool output. */
@@ -641,6 +644,7 @@ export async function init(): Promise<AppState> {
641644 running: false,
642645 abortController: null,
643646 activeTurnPromise: null,
647+ queuedUserMessages: [],
644648 showReasoning: startup.showReasoning,
645649 verbose: startup.verbose,
646650 customModels: customResult.models,
src/submit.ts+53−6
@@ -204,12 +204,63 @@ export function resolveRawInput(
204204 }
205205 }
206206
207+function recordRawPromptHistory(
208+ rawInput: string,
209+ state: Pick<AppState, "db" | "cwd">,
210+ sessionId: string,
211+): void {
212+ appendPromptHistory(state.db, {
213+ text: rawInput,
214+ cwd: state.cwd,
215+ sessionId,
216+ });
217+ truncatePromptHistory(state.db, MAX_PROMPT_HISTORY);
218+}
219+
220+/**
221+ * Queue resolved user content for the next model-request boundary of an active run.
222+ *
223+ * The raw prompt is recorded immediately in prompt history, but the model-visible
224+ * `UserMessage` is only appended to session history when the agent loop consumes it.
225+ *
226+ * @param rawInput - Exact raw submitted prompt text.
227+ * @param content - Resolved model-visible user content.
228+ * @param state - Mutable application state.
229+ */
230+export function queueResolvedInput(
231+ rawInput: string,
232+ content: UserMessage["content"],
233+ state: AppState,
234+): void {
235+ if (!state.running) {
236+ throw new Error("Cannot queue input while no turn is running.");
237+ }
238+ if (isEmptyUserContent(content)) {
239+ throw new Error("Cannot queue empty input.");
240+ }
241+
242+ const session = ensureSession(state);
243+ recordRawPromptHistory(rawInput, state, session.id);
244+ state.queuedUserMessages.push({
245+ role: "user",
246+ content,
247+ timestamp: Date.now(),
248+ });
249+}
250+
207251 // ---------------------------------------------------------------------------
208252 // Turn submission
209253 // ---------------------------------------------------------------------------
210254
211255 function handleAgentEvent(event: AgentEvent, state: AppState): void {
212256 switch (event.type) {
257+ case "user_message":
258+ state.messages.push(event.message);
259+ state.contextTokens = addMessageToContextTokens(
260+ state.contextTokens,
261+ event.message,
262+ );
263+ break;
213264 case "assistant_message":
214265 state.messages.push(event.message);
215266 state.stats = addMessageToStats(state.stats, event.message);
@@ -270,12 +321,7 @@ export async function submitResolvedInput(
270321 }
271322
272323 const session = ensureSession(state);
273- appendPromptHistory(state.db, {
274- text: rawInput,
275- cwd: state.cwd,
276- sessionId: session.id,
277- });
278- truncatePromptHistory(state.db, MAX_PROMPT_HISTORY);
324+ recordRawPromptHistory(rawInput, state, session.id);
279325
280326 const userMessage = {
281327 role: "user",
@@ -318,6 +364,7 @@ export async function submitResolvedInput(
318364 apiKey: state.providers.get(state.model.provider),
319365 effort: state.effort,
320366 signal: state.abortController.signal,
367+ takeQueuedUserMessage: () => state.queuedUserMessages.shift() ?? null,
321368 onEvent: (event) => {
322369 handleAgentEvent(event, state);
323370 hooks?.onEvent?.(event, state);
src/ui.test.ts+129−0
@@ -243,6 +243,7 @@ function createTestState(): AppState {
243243 running: false,
244244 abortController: null,
245245 activeTurnPromise: null,
246+ queuedUserMessages: [],
246247 showReasoning: DEFAULT_SHOW_REASONING,
247248 verbose: DEFAULT_VERBOSE,
248249 versionLabel: "dev",
@@ -659,6 +660,134 @@ describe("ui rendering", () => {
659660 }
660661 });
661662
663+ test("queues a mid-run submitted prompt, records its raw input immediately, and replays it on the next model request", async () => {
664+ const api = "ui-steering-message-test";
665+ const sourceId = "ui-steering-message-test-source";
666+ const model: Model<string> = {
667+ id: "ui-steering-message-model",
668+ name: "UI Steering Message Model",
669+ api,
670+ provider: "test",
671+ baseUrl: "http://localhost:0",
672+ reasoning: false,
673+ input: ["text"],
674+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
675+ contextWindow: 128000,
676+ maxTokens: 4096,
677+ };
678+
679+ const releaseFirstResponse = createDeferred<void>();
680+ let requestCount = 0;
681+ const buildProviderStream = () => {
682+ requestCount += 1;
683+ const stream = createAssistantMessageEventStream();
684+
685+ if (requestCount === 1) {
686+ const firstMessage: AssistantMessage = {
687+ role: "assistant",
688+ content: [fauxText("First response.")],
689+ api,
690+ provider: model.provider,
691+ model: model.id,
692+ usage: {
693+ input: 0,
694+ output: 0,
695+ cacheRead: 0,
696+ cacheWrite: 0,
697+ totalTokens: 0,
698+ cost: {
699+ input: 0,
700+ output: 0,
701+ cacheRead: 0,
702+ cacheWrite: 0,
703+ total: 0,
704+ },
705+ },
706+ stopReason: "stop",
707+ timestamp: Date.now(),
708+ };
709+
710+ queueMicrotask(() => {
711+ void releaseFirstResponse.promise.then(() => {
712+ stream.push({
713+ type: "done",
714+ reason: "stop",
715+ message: firstMessage,
716+ });
717+ stream.end(firstMessage);
718+ });
719+ });
720+
721+ return stream;
722+ }
723+
724+ const secondMessage = fauxAssistantMessage("Handled steering.");
725+ queueMicrotask(() => {
726+ stream.push({
727+ type: "done",
728+ reason: "stop",
729+ message: secondMessage,
730+ });
731+ stream.end(secondMessage);
732+ });
733+ return stream;
734+ };
735+
736+ registerApiProvider(
737+ {
738+ api,
739+ stream: () => buildProviderStream(),
740+ streamSimple: () => buildProviderStream(),
741+ },
742+ sourceId,
743+ );
744+
745+ const state = createTestState();
746+ state.cwd = process.cwd();
747+ state.canonicalCwd = process.cwd();
748+ state.model = model;
749+
750+ try {
751+ handleInput("hello", state);
752+ await waitFor(() => state.running && state.session !== null);
753+
754+ handleInput("steer later", state);
755+
756+ const queuedRows = state.db
757+ .query<
758+ { text: string; cwd: string; session_id: string | null },
759+ []
760+ >("SELECT text, cwd, session_id FROM prompt_history ORDER BY id DESC LIMIT 2")
761+ .all();
762+ expect(queuedRows.map((row) => row.text)).toEqual([
763+ "steer later",
764+ "hello",
765+ ]);
766+ expect(queuedRows[0]?.cwd).toBe(state.cwd);
767+ expect(queuedRows[0]?.session_id).toBe(state.session?.id ?? null);
768+ expect(state.messages.map((message) => message.role)).toEqual(["user"]);
769+
770+ releaseFirstResponse.resolve();
771+
772+ await waitFor(() => !state.running);
773+ expect(state.messages.map((message) => message.role)).toEqual([
774+ "user",
775+ "assistant",
776+ "user",
777+ "assistant",
778+ ]);
779+ expect(state.messages[2]).toMatchObject({
780+ role: "user",
781+ content: "steer later",
782+ });
783+ expect(requestCount).toBe(2);
784+ } finally {
785+ await stopRunningTurn(state);
786+ unregisterApiProviders(sourceId);
787+ state.db.close();
788+ }
789+ });
790+
662791 test("completed turns keep in-memory messages and stats authoritative", async () => {
663792 const api = "ui-authoritative-state-test";
664793 const sourceId = "ui-authoritative-state-test-source";
src/ui/agent.ts+16−2
@@ -13,7 +13,11 @@ import type { AssistantMessage } from "@mariozechner/pi-ai";
1313 import type { AgentEvent } from "../agent.ts";
1414 import { getErrorMessage } from "../errors.ts";
1515 import type { AppState } from "../index.ts";
16-import { resolveRawInput, submitResolvedInput } from "../submit.ts";
16+import {
17+ queueResolvedInput,
18+ resolveRawInput,
19+ submitResolvedInput,
20+} from "../submit.ts";
1721 import type {
1822 PendingToolResult,
1923 StreamingConversationState,
@@ -103,7 +107,12 @@ export function createUiAgentController(
103107 break;
104108 }
105109
106- if (!state.model || state.running) {
110+ if (state.running) {
111+ queueResolvedInput(rawInput, resolved.content, state);
112+ return;
113+ }
114+
115+ if (!state.model) {
107116 return;
108117 }
109118
@@ -154,6 +163,11 @@ export function createUiAgentController(
154163 runtime.render();
155164 break;
156165
166+ case "user_message":
167+ runtime.scrollConversationToBottom();
168+ runtime.render();
169+ break;
170+
157171 case "assistant_message":
158172 streamingContent = [];
159173 runtime.render();
src/ui/commands.test.ts+1−0
@@ -91,6 +91,7 @@ function createTestState(): AppState {
9191 running: false,
9292 abortController: null,
9393 activeTurnPromise: null,
94+ queuedUserMessages: [],
9495 showReasoning: true,
9596 verbose: false,
9697 versionLabel: "dev",
src/ui/render-performance.test.ts+1−0
@@ -94,6 +94,7 @@ function createTestState(): AppState {
9494 running: false,
9595 abortController: null,
9696 activeTurnPromise: null,
97+ queuedUserMessages: [],
9798 showReasoning: DEFAULT_SHOW_REASONING,
9899 verbose: DEFAULT_VERBOSE,
99100 versionLabel: "dev",
src/ui/status.test.ts+1−0
@@ -106,6 +106,7 @@ function createTestState(): AppState {
106106 running: false,
107107 abortController: null,
108108 activeTurnPromise: null,
109+ queuedUserMessages: [],
109110 showReasoning: DEFAULT_SHOW_REASONING,
110111 verbose: DEFAULT_VERBOSE,
111112 versionLabel: "dev",