Skip to content

Commit 1392c73

Browse files
committed
fix(kiro): suppress duplicate completion fallback
1 parent 07cc535 commit 1392c73

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎docs-site/src/content/docs/reference/adapters.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ emits its answer as final text, and never exposes the private tool to Codex or C
103103
If Kiro emits progress without calling the completion tool, the adapter makes one continuation. That
104104
single retry may finish with a validated private completion or plain final text. It cannot recurse:
105105
an empty or reasoning-only retry is returned as retryable incomplete, while a real client tool call
106-
keeps the turn open. Tool-free requests retain normal text completion behavior.
106+
keeps the turn open. If the retry only repeats the preceding commentary after whitespace
107+
normalization, the duplicate output is suppressed while the turn still completes. Tool-free
108+
requests retain normal text completion behavior.
107109

108110
### Reasoning effort
109111

‎src/adapters/kiro.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ function retryableKiroIncomplete(
517517
};
518518
}
519519

520+
function normalizedKiroAnswer(text: string): string {
521+
return text.trim().replace(/\s+/g, " ");
522+
}
523+
520524
async function* parseKiroAttempt(
521525
response: Response,
522526
mode: KiroCompletionMode,
@@ -525,6 +529,7 @@ async function* parseKiroAttempt(
525529
contextWindow: number | undefined,
526530
nameMap: Map<string, string> | undefined,
527531
conversationId: string | undefined,
532+
previousAssistantText?: string,
528533
): AsyncGenerator<AdapterEvent, KiroAttemptResult> {
529534
const emptyResult = (): KiroAttemptResult => ({ assistantText: "", sawReasoning: false });
530535
if (!response.body) {
@@ -841,7 +846,9 @@ async function* parseKiroAttempt(
841846
if (mode === "text_fallback") {
842847
if (completionAnswer !== undefined) {
843848
for (const event of fallbackEvents) yield event;
844-
yield { type: "text_delta", text: completionAnswer, phase: "final_answer" };
849+
if (normalizedKiroAnswer(completionAnswer) !== normalizedKiroAnswer(previousAssistantText ?? "")) {
850+
yield { type: "text_delta", text: completionAnswer, phase: "final_answer" };
851+
}
845852
return {
846853
assistantText,
847854
sawReasoning,
@@ -857,8 +864,10 @@ async function* parseKiroAttempt(
857864
};
858865
}
859866
if (sawText) {
867+
const repeated = normalizedKiroAnswer(assistantText) === normalizedKiroAnswer(previousAssistantText ?? "");
860868
for (const event of fallbackEvents) {
861-
yield event.type === "text_delta" ? { ...event, phase: "final_answer" } : event;
869+
if (event.type !== "text_delta") yield event;
870+
else if (!repeated) yield { ...event, phase: "final_answer" };
862871
}
863872
return {
864873
assistantText,
@@ -1019,6 +1028,7 @@ export async function* parseKiroStream(
10191028
contextWindow,
10201029
fallback.nameMap,
10211030
fallback.conversationId,
1031+
firstResult.assistantText,
10221032
);
10231033
let secondNext = await second.next();
10241034
while (!secondNext.done) {

‎tests/kiro-stream.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,41 @@ describe("kiro adapter — parseStream", () => {
312312
});
313313
});
314314

315+
test.each([
316+
["plain text", [eventFrame({ content: " I am checking.\n" })]],
317+
["private completion", completionFrames(" I am checking. ")],
318+
])("suppresses a whitespace-equivalent repeated %s fallback", async (_label, frames) => {
319+
globalThis.fetch = (async () => new Response(streamOf(...frames))) as typeof fetch;
320+
const adapter = createKiroAdapter(provider);
321+
await adapter.buildRequest(parsedWith([{ role: "user", content: "do it" }], [bashTool]));
322+
323+
const events = await collectAdapterEvents(adapter.parseStream(new Response(streamOf(
324+
eventFrame({ content: "I am checking." }),
325+
))));
326+
327+
expect(events.filter(event => event.type === "text_delta")).toEqual([
328+
{ type: "text_delta", text: "I am checking.", phase: "commentary" },
329+
]);
330+
expect(events.at(-1)).toMatchObject({ type: "done", endTurn: true });
331+
expect(JSON.stringify(events)).not.toContain(KIRO_COMPLETION_TOOL_NAME);
332+
});
333+
334+
test("keeps a distinct private-completion fallback as the final answer", async () => {
335+
globalThis.fetch = (async () => new Response(streamOf(...completionFrames("Done.")))) as typeof fetch;
336+
const adapter = createKiroAdapter(provider);
337+
await adapter.buildRequest(parsedWith([{ role: "user", content: "do it" }], [bashTool]));
338+
339+
const events = await collectAdapterEvents(adapter.parseStream(new Response(streamOf(
340+
eventFrame({ content: "I am checking." }),
341+
))));
342+
343+
expect(events.filter(event => event.type === "text_delta")).toEqual([
344+
{ type: "text_delta", text: "I am checking.", phase: "commentary" },
345+
{ type: "text_delta", text: "Done.", phase: "final_answer" },
346+
]);
347+
expect(events.at(-1)).toMatchObject({ type: "done", endTurn: true });
348+
});
349+
315350
test("reasoning-only required response receives one fallback and can finish in plain text", async () => {
316351
let fetches = 0;
317352
globalThis.fetch = (async () => {

0 commit comments

Comments
 (0)