Skip to content

Commit e37ea58

Browse files
authored
Merge branch 'main' into feature/bug-report-terminal-output
2 parents ebc6356 + 622323a commit e37ea58

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

codex-cli/src/utils/agent/agent-loop.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,6 @@ export class AgentLoop {
861861
throw error;
862862
}
863863
}
864-
turnInput = []; // clear turn input, prepare for function call results
865864

866865
// If the user requested cancellation while we were awaiting the network
867866
// request, abort immediately before we start handling the stream.
@@ -894,6 +893,8 @@ export class AgentLoop {
894893
// eslint-disable-next-line no-constant-condition
895894
while (true) {
896895
try {
896+
let newTurnInput: Array<ResponseInputItem> = [];
897+
897898
// eslint-disable-next-line no-await-in-loop
898899
for await (const event of stream as AsyncIterable<ResponseEvent>) {
899900
log(`AgentLoop.run(): response event ${event.type}`);
@@ -935,7 +936,7 @@ export class AgentLoop {
935936
"requires_action"
936937
) {
937938
// TODO: remove this once we can depend on streaming events
938-
const newTurnInput = await this.processEventsWithoutStreaming(
939+
newTurnInput = await this.processEventsWithoutStreaming(
939940
event.response.output,
940941
stageItem,
941942
);
@@ -970,24 +971,30 @@ export class AgentLoop {
970971

971972
if (delta.length === 0) {
972973
// No new input => end conversation.
973-
turnInput = [];
974+
newTurnInput = [];
974975
} else {
975976
// Re‑send full transcript *plus* the new delta so the
976977
// stateless backend receives complete context.
977-
turnInput = [...this.transcript, ...delta];
978+
newTurnInput = [...this.transcript, ...delta];
978979
// The prefix ends at the current transcript length –
979980
// everything after this index is new for the next
980981
// iteration.
981982
transcriptPrefixLen = this.transcript.length;
982983
}
983-
} else {
984-
turnInput = newTurnInput;
985984
}
986985
}
987986
lastResponseId = event.response.id;
988987
this.onLastResponseId(event.response.id);
989988
}
990989
}
990+
991+
// Set after we have consumed all stream events in case the stream wasn't
992+
// complete or we missed events for whatever reason. That way, we will set
993+
// the next turn to an empty array to prevent an infinite loop.
994+
// And don't update the turn input too early otherwise we won't have the
995+
// current turn inputs available for retries.
996+
turnInput = newTurnInput;
997+
991998
// Stream finished successfully – leave the retry loop.
992999
break;
9931000
} catch (err: unknown) {

0 commit comments

Comments
 (0)