xonecas.io
64875
Mc

mini-coder / commit 335726e

fix(agent): land steering after an assistant turn's tool results

xonecas2026-09-23 335726e505d1f7ded958578291bb59feda07a67f parent e35926a

1 file changed

src/agent.ts+28−11
@@ -58,19 +58,27 @@ export interface AgentRun {
5858 export async function runAgentTurn(run: AgentRun): Promise<void> {
5959 const { messages, session, signal, interaction, onEvent } = run;
6060
61- const pause = async (): Promise<boolean> => {
62- if (!interaction.isPauseRequested()) return true;
61+ /** Appends a user message the model reads at the next step boundary. */
62+ const steer = (texts: string[]): void => {
63+ const content = texts.filter((text) => text.length > 0).join("\n");
64+ if (content.length === 0) return;
65+ const message: UserMessage = { role: "user", content, timestamp: Date.now() };
66+ messages.push(message);
67+ session.appendMessage(message);
68+ };
69+
70+ /**
71+ * Returns the steering typed while paused, or ""; the caller decides where
72+ * it lands, because a user message between an assistant turn and its tool
73+ * results is rejected by the provider.
74+ */
75+ const pause = async (): Promise<string> => {
76+ if (!interaction.isPauseRequested()) return "";
6377 interaction.clearPause();
6478 onEvent({ type: "phase", phase: "pausing" });
6579 const steering = await interaction.requestSteering();
66- if (signal.aborted) return false;
67- if (steering.length > 0) {
68- const message: UserMessage = { role: "user", content: steering, timestamp: Date.now() };
69- messages.push(message);
70- session.appendMessage(message);
71- }
7280 onEvent({ type: "phase", phase: "idle" });
73- return true;
81+ return steering;
7482 };
7583
7684 for (;;) {
@@ -78,10 +86,12 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
7886 onEvent({ type: "cancelled" });
7987 return;
8088 }
81- if (!(await pause())) {
89+ const steering = await pause();
90+ if (signal.aborted) {
8291 onEvent({ type: "cancelled" });
8392 return;
8493 }
94+ steer([steering]);
8595
8696 onEvent({ type: "phase", phase: "preparing" });
8797 session.appendRequest({
@@ -146,15 +156,21 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
146156 return;
147157 }
148158
159+ // Steering is held until every result of this assistant turn has landed:
160+ // the provider rejects a user message between an assistant turn and its
161+ // tool results.
162+ const held: string[] = [];
149163 for (const call of toolCalls) {
150164 if (signal.aborted) {
151165 onEvent({ type: "cancelled" });
152166 return;
153167 }
154- if (!(await pause())) {
168+ const steering = await pause();
169+ if (signal.aborted) {
155170 onEvent({ type: "cancelled" });
156171 return;
157172 }
173+ if (steering !== "") held.push(steering);
158174 onEvent({ type: "phase", phase: "runningTool", detail: call.name });
159175
160176 const tool = run.toolNames.find((name) => name === call.name);
@@ -186,5 +202,6 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
186202 session.appendMessage(toolMessage);
187203 onEvent({ type: "toolResult", name: call.name, callId: call.id, text: result.text, isError: result.isError });
188204 }
205+ steer(held);
189206 }
190207 }