Skip to content

Commit 60537f0

Browse files
committed
fix(kiro): address review findings on completion suppression and failed exec wrappers
Three findings from the automated reviewers on cf1a572, each verified against the code before changing it. **Suppressing the resume wording was not sufficient (Codex P2).** The neutral acknowledgement stopped the continuation text, but `completionMode` stayed `required`, so the request kept advertising `codex_kiro_final_answer` together with its instructions. The model then either answered again, or replied with ordinary text and tripped `needsFallback`, whose retry payload ends with `KIRO_COMPLETION_RETRY_MESSAGE` and explicitly reopens the finished task. A turn whose history already ends with a delivered final answer now resolves to `disabled`, so the completion contract is not offered at all. Detection reads the parsed messages via `hasTrailingDeliveredFinalAnswer`, since the mode is needed to build the tool catalog before the turn list exists. It mirrors the turn-merge rule: a tool call in that message, or any later user or tool-result message, means work continued. `forcedCompletionMode` still wins, so the fallback retry that passes `text_fallback` is never silently downgraded. **Internal state was inferred from user content (CodeRabbit).** The acknowledgement was recognized by comparing `currentUim.content` to the constant, so a genuine user message quoting that sentence was treated as proxy filler and lost both its thinking-tag injection and its completion retry. The generated turn now carries an `answerDeliveredAck` flag that survives the `currentTurn` pop, and the checks test the flag. **A failed exec wrapper was normalized as an empty success (Codex P2).** `Script failed\nWall time ...\nOutput:\n` matched the success regex, so the only failure signal was replaced with text claiming the cell completed, was not blocked, and should not be retried — reachable through Responses history, where `function_call_output` is parsed with `isError: false`. `Script failed` is out of the shared success regex and gets its own failure guidance. Cursor keeps the broader behavior through a local wrapper arm, because its Computer Use branch marks such results `isError` separately and its suite pins that. Verification: `bun x tsc --noEmit` clean; 203 tests pass across kiro-adapter, kiro-stream, cursor-exec-empty-result, cursor-toolresult-normalize and server-kiro-completion-e2e; privacy:scan, core-lab-boundary and repo-hygiene green. Each of the three new assertions was driven red by reverting its fix individually.
1 parent cf1a572 commit 60537f0

4 files changed

Lines changed: 153 additions & 13 deletions

File tree

‎src/adapters/cursor/tool-result-normalize.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,19 @@
1212
import {
1313
EMPTY_EXEC_OUTPUT_MESSAGE,
1414
EMPTY_EXEC_OUTPUT_REGEX,
15+
FAILED_EXEC_OUTPUT_REGEX,
1516
isCodexExecBridgeTool,
1617
} from "../exec-tool-result-normalize";
1718

19+
/**
20+
* Cursor treats a failed-but-empty wrapper as an empty result too (its Computer Use branch marks
21+
* such results `isError` separately). The shared success regex deliberately excludes
22+
* `Script failed`, so restore that arm here rather than widening the shared one.
23+
*/
24+
function isEmptyOrFailedExecWrapper(text: string): boolean {
25+
return EMPTY_EXEC_OUTPUT_REGEX.test(text) || FAILED_EXEC_OUTPUT_REGEX.test(text);
26+
}
27+
1828
const COMPUTER_USE_TOOL_NAMES = new Set([
1929
"node_repl",
2030
"node_repl__js",
@@ -76,14 +86,14 @@ export function normalizeCursorToolResultText(
7686
): NormalizedToolResultText {
7787
const isError = options.isError === true;
7888
const computerUse = isNodeReplOrComputerUseTool(options.toolName, options.toolNamespace);
79-
if (computerUse && EMPTY_EXEC_OUTPUT_REGEX.test(text.trim())) {
89+
if (computerUse && isEmptyOrFailedExecWrapper(text.trim())) {
8090
return {
8191
text: "[empty output: the tool ran but produced no stdout or return value. Verify application state with get_app_state, or make the script emit output.]",
8292
isError: true,
8393
changed: true,
8494
};
8595
}
86-
if (isCodexExecBridgeTool(options.toolName, options.toolNamespace) && EMPTY_EXEC_OUTPUT_REGEX.test(text.trim())) {
96+
if (isCodexExecBridgeTool(options.toolName, options.toolNamespace) && isEmptyOrFailedExecWrapper(text.trim())) {
8797
return {
8898
text: EMPTY_EXEC_OUTPUT_MESSAGE,
8999
isError: false,

‎src/adapters/exec-tool-result-normalize.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,23 @@
1111
* Kiro consumes this helper directly.
1212
*/
1313

14-
/** Matches exec wrappers whose only payload is an empty-output marker. */
15-
export const EMPTY_EXEC_OUTPUT_REGEX = /^(?:(?:Script completed|Script failed|Command finished|Execution finished)[^\n]*\n+)?(?:Wall time[^\n]*\n+)?(?:Output:\s*)?(?:<empty>)?\s*$/;
14+
/**
15+
* Matches exec wrappers whose only payload is an empty-output marker.
16+
*
17+
* `Script failed` is deliberately NOT in this set. A failed cell with no captured output is still
18+
* a FAILURE, and the success guidance below ("not a blocked tool", "do not re-run") would erase the
19+
* only signal that anything went wrong — reachable through Responses history, where
20+
* `function_call_output` is parsed with `isError: false`. Cursor keeps its own broader regex for
21+
* Computer Use, where a failed wrapper is separately marked `isError`.
22+
*/
23+
export const EMPTY_EXEC_OUTPUT_REGEX = /^(?:(?:Script completed|Command finished|Execution finished)[^\n]*\n+)?(?:Wall time[^\n]*\n+)?(?:Output:\s*)?(?:<empty>)?\s*$/;
24+
25+
/** Wrapper for a cell that FAILED without emitting output: empty, but not a success. */
26+
export const FAILED_EXEC_OUTPUT_REGEX = /^Script failed[^\n]*\n*(?:Wall time[^\n]*\n*)?(?:Output:\s*)?(?:<empty>)?\s*$/;
27+
28+
/** Guidance for a failed cell whose output was empty: the failure must survive normalization. */
29+
export const FAILED_EXEC_OUTPUT_MESSAGE =
30+
"[exec failed with no captured output: the cell raised before emitting anything. This is a real failure, not an empty success — inspect the call for a thrown error or syntax problem before retrying.]";
1631

1732
/**
1833
* The guidance itself. Worded to close all three wrong conclusions a model draws from a blank
@@ -62,5 +77,9 @@ export function normalizeEmptyExecToolResultText(
6277
text: string,
6378
options: { toolName?: string; toolNamespace?: string } = {},
6479
): string | undefined {
65-
return isEmptyExecToolResult(text, options) ? EMPTY_EXEC_OUTPUT_MESSAGE : undefined;
80+
if (!isCodexExecBridgeTool(options.toolName, options.toolNamespace)) return undefined;
81+
const trimmed = text.trim();
82+
// Failure first: a failed wrapper must never be described as an empty success.
83+
if (FAILED_EXEC_OUTPUT_REGEX.test(trimmed)) return FAILED_EXEC_OUTPUT_MESSAGE;
84+
return EMPTY_EXEC_OUTPUT_REGEX.test(trimmed) ? EMPTY_EXEC_OUTPUT_MESSAGE : undefined;
6685
}

‎src/adapters/kiro.ts‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,19 @@ function validateKiroCapabilities(parsed: OcxParsedRequest): void {
345345
}
346346

347347
type KiroTurn =
348-
| { kind: "user"; content: string; images: KiroImage[]; toolResults: KiroToolResult[] }
348+
| {
349+
kind: "user";
350+
content: string;
351+
images: KiroImage[];
352+
toolResults: KiroToolResult[];
353+
/**
354+
* True only for the proxy-generated acknowledgement that follows a delivered final answer.
355+
* A flag rather than a content comparison: a real user message may legitimately quote the
356+
* same sentence, and treating that as internal state would strip its thinking tags and
357+
* completion retry.
358+
*/
359+
answerDeliveredAck?: boolean;
360+
}
349361
| {
350362
kind: "assistant";
351363
content: string;
@@ -360,6 +372,27 @@ type KiroTurn =
360372
finalAnswer?: boolean;
361373
};
362374

375+
/**
376+
* True when the LAST content-bearing message is an assistant final answer that closed its turn.
377+
*
378+
* Mirrors the turn-merge rule: a tool call in that message, or any later user/tool-result message,
379+
* means work continued, so the turn is no longer terminal. Empty assistant messages are skipped
380+
* rather than treated as continuation, since they carry no visible turn.
381+
*/
382+
function hasTrailingDeliveredFinalAnswer(messages: readonly OcxMessage[]): boolean {
383+
for (let i = messages.length - 1; i >= 0; i--) {
384+
const msg = messages[i];
385+
if (msg.role !== "assistant") return false;
386+
const aMsg = msg as OcxAssistantMessage;
387+
const hasToolCall = (aMsg.content ?? []).some(part => part.type === "toolCall");
388+
if (hasToolCall) return false;
389+
const hasText = (aMsg.content ?? []).some(part => part.type === "text" && part.text.trim());
390+
if (!hasText) continue;
391+
return aMsg.phase === "final_answer";
392+
}
393+
return false;
394+
}
395+
363396
function appendTurnText(target: string, next: string): string {
364397
if (!next) return target;
365398
return target ? `${target}\n\n${next}` : next;
@@ -467,8 +500,19 @@ export function buildKiroPayload(
467500
const registry = createKiroToolNameRegistry();
468501
const toolContext = convertKiroToolContext(parsed, registry);
469502
const ordinaryTools = toolContext.tools;
503+
// A turn whose history already ENDS with a delivered final answer has nothing to complete.
504+
// Leaving completion "required" here would keep advertising codex_kiro_final_answer with its
505+
// instructions, so the model answers again, or replies with ordinary text and trips the
506+
// `needsFallback` retry, which ends its payload with KIRO_COMPLETION_RETRY_MESSAGE and reopens
507+
// the finished task. Suppressing the mode is what actually closes that loop; the neutral
508+
// acknowledgement below only stops the resume wording.
509+
//
510+
// Read from parsed messages because `completionMode` is needed to build the tool catalog, which
511+
// happens before the turn list exists. `forcedCompletionMode` still wins: the fallback retry
512+
// passes "text_fallback" explicitly and must not be silently downgraded.
513+
const trailingDeliveredAnswer = hasTrailingDeliveredFinalAnswer(kiroPayloadMessages(parsed));
470514
const completionMode: KiroCompletionMode = forcedCompletionMode
471-
?? (ordinaryTools.length > 0 ? "required" : "disabled");
515+
?? (ordinaryTools.length > 0 && !trailingDeliveredAnswer ? "required" : "disabled");
472516
const kiroTools = completionMode === "disabled"
473517
? ordinaryTools
474518
: [...ordinaryTools, kiroCompletionTool()];
@@ -636,6 +680,7 @@ export function buildKiroPayload(
636680
content: trailing.finalAnswer ? KIRO_ANSWER_DELIVERED_MESSAGE : resumeText,
637681
images: [],
638682
toolResults: [],
683+
...(trailing.finalAnswer ? { answerDeliveredAck: true } : {}),
639684
});
640685
}
641686

@@ -651,6 +696,8 @@ export function buildKiroPayload(
651696

652697
const currentTurn = turns.pop();
653698
if (!currentTurn || currentTurn.kind !== "user") throw new Error("Kiro request must end with a user turn");
699+
// Survives the pop as state, so the checks below never infer intent from user-supplied text.
700+
const answerDeliveredAck = currentTurn.answerDeliveredAck === true;
654701
const toEntry = (turn: KiroTurn): KiroHistoryEntry => turn.kind === "assistant"
655702
? {
656703
assistantResponseMessage: {
@@ -681,16 +728,16 @@ export function buildKiroPayload(
681728
currentUim.userInputMessageContext = { ...(currentUim.userInputMessageContext ?? {}), tools: kiroTools };
682729
}
683730
if (completionMode === "text_fallback") {
684-
// Never append the retry instruction onto the answer-delivered placeholder: that placeholder
685-
// exists precisely to avoid asking a finished turn for another completion call, and appending
686-
// here would reinstate the loop the placeholder prevents.
687-
if (currentUim.content !== KIRO_COMPLETION_RETRY_MESSAGE && currentUim.content !== KIRO_ANSWER_DELIVERED_MESSAGE) {
731+
// Never append the retry instruction onto the answer-delivered acknowledgement: it exists
732+
// precisely to avoid asking a finished turn for another completion call, and appending here
733+
// would reinstate the loop it prevents.
734+
if (currentUim.content !== KIRO_COMPLETION_RETRY_MESSAGE && !answerDeliveredAck) {
688735
currentUim.content = appendTurnText(currentUim.content, KIRO_COMPLETION_RETRY_MESSAGE);
689736
}
690737
} else if (
691738
!currentUim.userInputMessageContext?.toolResults
692739
&& currentUim.content !== KIRO_CONTINUATION_MESSAGE
693-
&& currentUim.content !== KIRO_ANSWER_DELIVERED_MESSAGE
740+
&& !answerDeliveredAck
694741
) {
695742
currentUim.content = injectKiroThinkingTags(currentUim.content, parsed);
696743
}

‎tests/kiro-adapter.test.ts‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { createKiroAdapter } from "../src/adapters/kiro";
77
import {
88
KIRO_ANSWER_DELIVERED_MESSAGE,
99
KIRO_COMPLETION_RETRY_MESSAGE,
10+
KIRO_COMPLETION_TOOL_NAME,
1011
KIRO_CONTINUATION_MESSAGE,
1112
KIRO_EMPTY_TOOL_RESULT_MESSAGE,
1213
KIRO_TOOL_RESULT_CARRIER_MESSAGE,
1314
} from "../src/adapters/kiro-constants";
14-
import { EMPTY_EXEC_OUTPUT_MESSAGE } from "../src/adapters/exec-tool-result-normalize";
15+
import { EMPTY_EXEC_OUTPUT_MESSAGE, FAILED_EXEC_OUTPUT_MESSAGE } from "../src/adapters/exec-tool-result-normalize";
1516
import { MAX_KIRO_TOOL_CATALOG_BYTES, MAX_KIRO_TOOL_COUNT } from "../src/adapters/kiro-tools";
1617
import { applyProviderConfigHints, buildCatalogEntries } from "../src/codex/catalog";
1718
import { getValidAccessTokenSnapshot } from "../src/oauth";
@@ -337,6 +338,21 @@ describe("kiro adapter — buildRequest", () => {
337338
});
338339

339340
test("real exec output and empty non-exec results are left alone", async () => {
341+
// Review finding (Codex P2): a failed cell with no output is empty but NOT a success. The
342+
// success guidance would erase the only failure signal — reachable via Responses history,
343+
// where function_call_output is parsed with isError: false.
344+
const execTool0 = { name: "exec", description: "Run JavaScript", parameters: { type: "object" } };
345+
const failed = [
346+
{ role: "user", content: "run it" },
347+
{ role: "assistant", content: [{ type: "toolCall", id: "call-f", name: "exec", arguments: {} }] },
348+
{ role: "toolResult", toolCallId: "call-f", toolName: "exec", content: "Script failed\nWall time 0.1 seconds\nOutput:\n", isError: false },
349+
];
350+
const failedBody = await createKiroAdapter(provider).buildRequest(parsedWith(failed, [execTool0]));
351+
const failedText = JSON.parse(failedBody.body).conversationState.currentMessage.userInputMessage
352+
.userInputMessageContext.toolResults[0].content[0].text;
353+
expect(failedText).toBe(FAILED_EXEC_OUTPUT_MESSAGE);
354+
expect(failedText).not.toBe(EMPTY_EXEC_OUTPUT_MESSAGE);
355+
340356
const execTool = { name: "exec", description: "Run JavaScript", parameters: { type: "object" } };
341357
const withExecOutput = [
342358
{ role: "user", content: "run it" },
@@ -388,6 +404,54 @@ describe("kiro adapter — buildRequest", () => {
388404
expect(current.content).not.toBe(KIRO_ANSWER_DELIVERED_MESSAGE);
389405
});
390406

407+
// Review finding (Codex P2): suppressing the resume wording is not enough. While completion
408+
// stays "required" the request keeps advertising the completion tool, so the model answers again
409+
// or trips the text_fallback retry, which reopens the finished task.
410+
test("a delivered final answer stops advertising the completion tool", async () => {
411+
const delivered = [
412+
{ role: "user", content: "do it" },
413+
{ role: "assistant", phase: "final_answer", content: [{ type: "text", text: "Done." }] },
414+
];
415+
const { body } = await createKiroAdapter(provider).buildRequest(parsedWith(delivered, [bashTool]));
416+
const current = JSON.parse(body).conversationState.currentMessage.userInputMessage;
417+
const toolNames = (current.userInputMessageContext?.tools ?? [])
418+
.map((t: { toolSpecification?: { name?: string } }) => t.toolSpecification?.name);
419+
420+
expect(toolNames).toContain("bash");
421+
expect(toolNames).not.toContain(KIRO_COMPLETION_TOOL_NAME);
422+
// The instructions must go too: they tell the model to call a tool that is no longer offered.
423+
expect(current.content).not.toContain(KIRO_COMPLETION_TOOL_NAME);
424+
425+
// Control: an unfinished turn still gets the completion contract.
426+
const unfinished = [
427+
{ role: "user", content: "do it" },
428+
{ role: "assistant", content: [{ type: "text", text: "Working..." }] },
429+
];
430+
const open = await createKiroAdapter(provider).buildRequest(parsedWith(unfinished, [bashTool]));
431+
const openNames = (JSON.parse(open.body).conversationState.currentMessage.userInputMessage
432+
.userInputMessageContext?.tools ?? [])
433+
.map((t: { toolSpecification?: { name?: string } }) => t.toolSpecification?.name);
434+
expect(openNames).toContain(KIRO_COMPLETION_TOOL_NAME);
435+
});
436+
437+
// Review finding (CodeRabbit): the acknowledgement was detected by comparing user content, so a
438+
// real user message quoting that sentence lost its thinking tags and completion retry.
439+
test("a user message quoting the acknowledgement is still treated as user content", async () => {
440+
const messages = [
441+
{ role: "user", content: "do it" },
442+
{ role: "assistant", content: [{ type: "text", text: "ok" }] },
443+
{ role: "user", content: KIRO_ANSWER_DELIVERED_MESSAGE },
444+
];
445+
const { body } = await createKiroAdapter(provider).buildRequest({
446+
...parsedWith(messages, [bashTool]),
447+
options: { reasoning: "xhigh" },
448+
} as never);
449+
const current = JSON.parse(body).conversationState.currentMessage.userInputMessage;
450+
451+
// Real user text keeps its reasoning injection; internal state must not be inferred from it.
452+
expect(current.content).toContain("<thinking_mode>");
453+
});
454+
391455
test("commentary after a final answer reopens continuation", async () => {
392456
// A merged assistant turn is terminal only if its LAST component was the final answer.
393457
const messages = [

0 commit comments

Comments
 (0)