Skip to content

Commit b3ab276

Browse files
authored
fix: steering near run completion receives a response (#125038)
* fix(agent): consume late steering before terminalizing * test(gateway): split provider bootstrap coverage
1 parent a2b5345 commit b3ab276

4 files changed

Lines changed: 431 additions & 387 deletions

File tree

‎packages/agent-core/src/agent-loop.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,42 @@ describe("agentLoop tool termination", () => {
14961496
expect(getSteeringMessages).toHaveBeenCalled();
14971497
});
14981498

1499+
it("delivers steering admitted while the final follow-up drain is pending", async () => {
1500+
const followUpDrainStarted = createDeferred();
1501+
const releaseFollowUpDrain = createDeferred();
1502+
const steer = { role: "user" as const, content: "one more thing", timestamp: 2 };
1503+
const queued: AgentMessage[] = [];
1504+
const requestMessages: Message[][] = [];
1505+
const run = runAgentLoop(
1506+
[{ role: "user", content: "start", timestamp: 1 }],
1507+
{ systemPrompt: "", messages: [] },
1508+
{
1509+
...config,
1510+
getSteeringMessages: async () => queued.splice(0, 1),
1511+
getFollowUpMessages: async () => {
1512+
followUpDrainStarted.resolve();
1513+
await releaseFollowUpDrain.promise;
1514+
return [];
1515+
},
1516+
},
1517+
() => {},
1518+
undefined,
1519+
createTurnSequenceStream(
1520+
[[{ type: "text", text: "initial response" }], [{ type: "text", text: "steer response" }]],
1521+
requestMessages,
1522+
),
1523+
);
1524+
1525+
await followUpDrainStarted.promise;
1526+
queued.push(steer);
1527+
releaseFollowUpDrain.resolve();
1528+
await run;
1529+
1530+
expect(requestMessages).toHaveLength(2);
1531+
expect(requestMessages[1]?.at(-1)).toBe(steer);
1532+
expect(queued).toEqual([]);
1533+
});
1534+
14991535
it("suppresses sequential tools when steering arrives from awaited message_end", async () => {
15001536
const execute = vi.fn(async () => ({ content: [], details: {} }));
15011537
const requestMessages: Message[][] = [];

‎packages/agent-core/src/agent-loop.ts‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,16 +509,15 @@ async function runLoop(
509509
}
510510
}
511511

512-
const followUpMessages = (await config.getFollowUpMessages?.()) || [];
513-
if (followUpMessages.length > 0) {
514-
// Follow-up messages arrive after a turn would otherwise end; route them through the
515-
// same pending-message path so event ordering matches steering messages.
516-
pendingMessages = followUpMessages;
517-
continue;
512+
pendingMessages = (await config.getFollowUpMessages?.()) || [];
513+
if (pendingMessages.length === 0) {
514+
// Recheck after the awaited follow-up drain so agent_end cannot strand an accepted steer.
515+
const finalSteering = getSteeringAtCheckpoint(config);
516+
pendingMessages = Array.isArray(finalSteering) ? finalSteering : await finalSteering;
517+
}
518+
if (pendingMessages.length === 0) {
519+
break;
518520
}
519-
520-
// No more messages, exit
521-
break;
522521
}
523522

524523
await emit({ type: "agent_end", messages: newMessages });

0 commit comments

Comments
 (0)