Skip to content

Commit cf1a572

Browse files
committed
fix(kiro): explain empty exec output and stop reopening a delivered final answer
Two defects made a Kiro code-mode session unable to see its own tool output and unable to end its turn. **Empty exec output was unexplained.** A code-mode `exec` cell that never calls `text()`/`notify()` returns nothing — the last expression value is not echoed. Kiro received the generic "The tool completed without textual output.", so the model concluded its earlier context was lost and restarted finished work. Cursor already had the actionable wording for exactly this state; Kiro had no equivalent. The detection now lives in `src/adapters/exec-tool-result-normalize.ts` and both adapters share one message, so the wording cannot drift. Cursor keeps `normalizeCursorToolResultText` as a wrapper: it owns Computer Use precedence and isError policy, which are not Kiro's. Checked before `text.trim()` deliberately — the wrapper form ("Script completed\nWall time ...\nOutput:\n") is non-blank and would otherwise pass through as though it were real output. Non-exec tools keep the generic message; asserting code-mode semantics for arbitrary tools would tell a model to call `text()` in a runtime that has neither. **A delivered final answer was told to continue.** The trailing-turn predicate was `turns.at(-1)?.kind === "assistant"`, which cannot tell "stopped mid-task" from "already answered". Both got a continuation or completion-retry prompt, so a closed task read as a still-open goal and looped. The turn now carries whether it was the delivered final answer, keyed off Responses `phase: "final_answer"` (preserved through the parser) rather than guessed from position. Kiro still requires a trailing user turn, so the turn is still appended — only its text changes, to a neutral acknowledgement that withholds any instruction to resume. Removing the append instead would throw at the `currentTurn` pop. The `text_fallback` and thinking-tag paths are both excluded, since either would re-append the resume instruction and reinstate the loop. A merged assistant turn counts as final only when its last component was, so commentary after an answer correctly reopens continuation. Verification: `bun x tsc --noEmit` clean; 201 tests pass across kiro-adapter, kiro-stream, cursor-exec-empty-result, cursor-toolresult-normalize and server-kiro-completion-e2e; privacy:scan and the core/lab boundary and repo-hygiene guards green. Both new assertions were driven red by reverting each fix individually, so neither is vacuous.
1 parent 80e251a commit cf1a572

5 files changed

Lines changed: 238 additions & 39 deletions

File tree

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

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
* dropping images oldest-first — see toolCallStep in protobuf-request.ts).
1010
*/
1111

12+
import {
13+
EMPTY_EXEC_OUTPUT_MESSAGE,
14+
EMPTY_EXEC_OUTPUT_REGEX,
15+
isCodexExecBridgeTool,
16+
} from "../exec-tool-result-normalize";
17+
1218
const COMPUTER_USE_TOOL_NAMES = new Set([
1319
"node_repl",
1420
"node_repl__js",
@@ -29,31 +35,6 @@ function isNodeReplOrComputerUseTool(toolName?: string, toolNamespace?: string):
2935
return lower.startsWith("mcp__node_repl") || lower.startsWith("mcp__computer_use");
3036
}
3137

32-
/**
33-
* Codex exec / shell-bridge tool names (flat and MCP-prefixed display aliases). An empty result
34-
* here is almost always a code-mode cell that never called text()/notify() — the cursor model
35-
* reads the blank [tool_result], concludes prior results were lost, and spirals into
36-
* re-orientation retries (devlog 260826_cursor_responses_gap, live subagent transcripts).
37-
*/
38-
function isCodexExecBridgeTool(toolName?: string, toolNamespace?: string): boolean {
39-
if (toolNamespace && toolNamespace.includes("opencodex-responses")) return true;
40-
if (!toolName) return false;
41-
const lower = toolName.toLowerCase();
42-
return (
43-
lower === "exec"
44-
|| lower === "exec_command"
45-
|| lower === "shell_command"
46-
// Codex CLI/desktop native tool names: the multi-round "이전 출력이 비어 있어 처음부터"
47-
// restart loop reproduced via codex exec because `shell` was not in this set
48-
// (devlog 260826 gap-8 QA round 2).
49-
|| lower === "shell"
50-
|| lower === "local_shell"
51-
|| lower === "container.exec"
52-
|| lower.startsWith("mcp_opencodex-responses_")
53-
|| lower.startsWith("mcp__opencodex-responses__")
54-
);
55-
}
56-
5738
/** Failure states the Computer Use / node_repl runtime reports as PLAIN TEXT inside a non-error result. */
5839
const RUNTIME_FAILURE_GUIDANCE: ReadonlyArray<{ marker: string; guidance: string }> = [
5940
{
@@ -74,9 +55,6 @@ const RUNTIME_FAILURE_GUIDANCE: ReadonlyArray<{ marker: string; guidance: string
7455
},
7556
];
7657

77-
/** Matches exec wrappers whose only payload is an empty-output marker. */
78-
const EMPTY_EXEC_OUTPUT_REGEX = /^(?:(?:Script completed|Script failed|Command finished|Execution finished)[^\n]*\n+)?(?:Wall time[^\n]*\n+)?(?:Output:\s*)?(?:<empty>)?\s*$/;
79-
8058
export interface NormalizedToolResultText {
8159
text: string;
8260
isError: boolean;
@@ -107,7 +85,7 @@ export function normalizeCursorToolResultText(
10785
}
10886
if (isCodexExecBridgeTool(options.toolName, options.toolNamespace) && EMPTY_EXEC_OUTPUT_REGEX.test(text.trim())) {
10987
return {
110-
text: "[empty output: the exec cell completed but emitted nothing. This is NOT lost context and NOT a blocked tool — in code mode call text(...) or notify(...) on any value you need to see (a bare await tools.exec_command(...) is not echoed automatically); in shell mode the command simply printed nothing. Do not re-run the same call expecting different output.]",
88+
text: EMPTY_EXEC_OUTPUT_MESSAGE,
11189
isError: false,
11290
changed: true,
11391
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Provider-neutral empty-exec-output normalization.
3+
*
4+
* A code-mode `exec` cell that never calls `text()`/`notify()` returns nothing: the last
5+
* expression value is NOT echoed automatically. The routed model reads a blank tool result,
6+
* concludes its earlier output was lost, and burns turns re-running the same call or restarting
7+
* the task from scratch. Naming that state explicitly is what breaks the loop.
8+
*
9+
* This module owns the shared detection so every adapter reports the same thing. Cursor keeps its
10+
* own wrapper (`normalizeCursorToolResultText`) for Computer Use precedence and isError policy;
11+
* Kiro consumes this helper directly.
12+
*/
13+
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*$/;
16+
17+
/**
18+
* The guidance itself. Worded to close all three wrong conclusions a model draws from a blank
19+
* result: that context was lost, that the tool is blocked, and that retrying will differ.
20+
*/
21+
export const EMPTY_EXEC_OUTPUT_MESSAGE =
22+
"[empty output: the exec cell completed but emitted nothing. This is NOT lost context and NOT a blocked tool — in code mode call text(...) or notify(...) on any value you need to see (a bare await tools.exec_command(...) is not echoed automatically); in shell mode the command simply printed nothing. Do not re-run the same call expecting different output.]";
23+
24+
/**
25+
* Codex exec / shell-bridge tool names (flat and MCP-prefixed display aliases). An empty result
26+
* here is almost always a code-mode cell that never called text()/notify().
27+
*/
28+
export function isCodexExecBridgeTool(toolName?: string, toolNamespace?: string): boolean {
29+
if (toolNamespace && toolNamespace.includes("opencodex-responses")) return true;
30+
if (!toolName) return false;
31+
const lower = toolName.toLowerCase();
32+
return (
33+
lower === "exec"
34+
|| lower === "exec_command"
35+
|| lower === "shell_command"
36+
// Codex CLI/desktop native tool names: the multi-round "이전 출력이 비어 있어 처음부터"
37+
// restart loop reproduced via codex exec because `shell` was not in this set
38+
// (devlog 260826 gap-8 QA round 2).
39+
|| lower === "shell"
40+
|| lower === "local_shell"
41+
|| lower === "container.exec"
42+
|| lower.startsWith("mcp_opencodex-responses_")
43+
|| lower.startsWith("mcp__opencodex-responses__")
44+
);
45+
}
46+
47+
/** True when this result is an exec-bridge call that produced no usable output. */
48+
export function isEmptyExecToolResult(
49+
text: string,
50+
options: { toolName?: string; toolNamespace?: string } = {},
51+
): boolean {
52+
return isCodexExecBridgeTool(options.toolName, options.toolNamespace)
53+
&& EMPTY_EXEC_OUTPUT_REGEX.test(text.trim());
54+
}
55+
56+
/**
57+
* Returns the guidance text when this is an empty exec-bridge result, else `undefined` so the
58+
* caller keeps its own fallback. Undefined rather than the original text: an adapter must be able
59+
* to tell "not my case" from "normalized to the same string".
60+
*/
61+
export function normalizeEmptyExecToolResultText(
62+
text: string,
63+
options: { toolName?: string; toolNamespace?: string } = {},
64+
): string | undefined {
65+
return isEmptyExecToolResult(text, options) ? EMPTY_EXEC_OUTPUT_MESSAGE : undefined;
66+
}

‎src/adapters/kiro-constants.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ export const KIRO_COMPLETION_RETRY_MESSAGE =
2222
export const KIRO_TOOL_RESULT_CARRIER_MESSAGE = "The requested tool result is attached.";
2323
export const KIRO_EMPTY_TOOL_RESULT_MESSAGE = "The tool completed without textual output.";
2424

25+
/**
26+
* Placeholder for the user turn Kiro requires after an assistant turn that ALREADY delivered its
27+
* final answer.
28+
*
29+
* The protocol needs a trailing user turn, but the usual continuation/retry text instructs the
30+
* model to keep working, which reopens a finished task and reads as a still-open goal. This states
31+
* the delivered state and explicitly withholds a new request, so the turn stays structurally valid
32+
* without asking for more work.
33+
*/
34+
export const KIRO_ANSWER_DELIVERED_MESSAGE =
35+
"The previous final answer was delivered to the user and that task is closed. No new request has been made yet. Do not repeat, revise, or continue that work; wait for the user's next instruction.";
36+
2537
export const KIRO_COMPLETION_INSTRUCTIONS =
2638
`When tools are available, ordinary assistant text is mid-task commentary and does not end the turn. Continue using tools after progress updates. When the task is fully complete and no more tool calls are needed, call ${KIRO_COMPLETION_TOOL_NAME} exactly once with the complete user-facing final answer in \`answer\`. Do not provide the final answer as ordinary assistant text.`;
2739

‎src/adapters/kiro.ts‎

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ import { extractKiroImages, normalizeKiroImages, type KiroImage } from "./kiro-i
4242
import { sniffImageDimensions } from "./anthropic-image-guard";
4343
import { fetchKiroWithRetry, noteKiroTransientThrottle } from "./kiro-retry";
4444
import { convertKiroToolContext } from "./kiro-tools";
45+
import { normalizeEmptyExecToolResultText } from "./exec-tool-result-normalize";
4546
import { identifyRoutedModel } from "./identity";
4647
import { buildNonOpenAIToolCatalogNudgeFromNames, isBareShellBridgeTool, isCodexCodeModeExecTool } from "./tool-catalog-nudge";
4748
import {
49+
KIRO_ANSWER_DELIVERED_MESSAGE,
4850
KIRO_COMPLETION_INSTRUCTIONS,
4951
KIRO_COMPLETION_RETRY_MESSAGE,
5052
KIRO_COMPLETION_TOOL_NAME,
@@ -344,7 +346,19 @@ function validateKiroCapabilities(parsed: OcxParsedRequest): void {
344346

345347
type KiroTurn =
346348
| { kind: "user"; content: string; images: KiroImage[]; toolResults: KiroToolResult[] }
347-
| { kind: "assistant"; content: string; toolUses: KiroToolUse[]; redactedReasoning?: string };
349+
| {
350+
kind: "assistant";
351+
content: string;
352+
toolUses: KiroToolUse[];
353+
redactedReasoning?: string;
354+
/**
355+
* True when this assistant turn was the DELIVERED final answer (Responses
356+
* `phase: "final_answer"`). A trailing assistant turn normally means the model stopped
357+
* mid-task and needs a continuation prompt, but a delivered final answer already ended its
358+
* turn — prompting it again restarts finished work as if a goal were still open.
359+
*/
360+
finalAnswer?: boolean;
361+
};
348362

349363
function appendTurnText(target: string, next: string): string {
350364
if (!next) return target;
@@ -521,15 +535,24 @@ export function buildKiroPayload(
521535
turns.push({ kind: "user", content, images: [...images], toolResults: [...toolResults] });
522536
}
523537
};
524-
const pushAssistant = (content: string, toolUses: KiroToolUse[], redactedReasoning?: string): void => {
538+
const pushAssistant = (content: string, toolUses: KiroToolUse[], redactedReasoning?: string, finalAnswer?: boolean): void => {
525539
const last = turns.at(-1);
526540
if (last?.kind === "assistant") {
527541
last.content = appendTurnText(last.content, content);
528542
last.toolUses.push(...toolUses);
529543
// Merged turns keep the newest blob: it covers the reasoning up to the merged turn's end.
530544
if (redactedReasoning) last.redactedReasoning = redactedReasoning;
545+
// A merged turn is final only if its LAST component was: commentary appended after a final
546+
// answer means the model kept working, so the turn is no longer terminal.
547+
last.finalAnswer = finalAnswer === true;
531548
} else {
532-
turns.push({ kind: "assistant", content, toolUses: [...toolUses], ...(redactedReasoning ? { redactedReasoning } : {}) });
549+
turns.push({
550+
kind: "assistant",
551+
content,
552+
toolUses: [...toolUses],
553+
...(redactedReasoning ? { redactedReasoning } : {}),
554+
...(finalAnswer ? { finalAnswer: true } : {}),
555+
});
533556
}
534557
};
535558

@@ -559,14 +582,24 @@ export function buildKiroPayload(
559582
const hasReasoning = aMsg.content.some(part => part.type === "thinking" && part.thinking.trim());
560583
if (hasReasoning) continue;
561584
}
562-
pushAssistant(text, toolUses, aMsg.kiroRedactedReasoning);
585+
// `phase` survives the Responses round trip (parser.ts assistant branch), so a replayed
586+
// final answer is identifiable here rather than guessed from turn position.
587+
pushAssistant(text, toolUses, aMsg.kiroRedactedReasoning, aMsg.phase === "final_answer" && toolUses.length === 0);
563588
} else if (msg.role === "toolResult") {
564589
const tr = msg as OcxToolResultMessage;
565590
if (tr.containsEncryptedContent) {
566591
throw new Error(`Kiro cannot translate encrypted output for tool call ${JSON.stringify(tr.toolCallId)}`);
567592
}
568593
const text = userContentText(tr.content);
569-
const resultText = text.trim() ? text : KIRO_EMPTY_TOOL_RESULT_MESSAGE;
594+
// An empty code-mode exec result needs the SPECIFIC reason, not the generic fallback: the
595+
// model otherwise reads a blank result, concludes its earlier context was lost, and restarts
596+
// the task instead of calling text()/notify(). Checked before `text.trim()` because the
597+
// wrapper form ("Script completed\nWall time ...\nOutput:\n") is non-blank and would
598+
// otherwise pass through as if it were real output.
599+
const resultText = normalizeEmptyExecToolResultText(text, {
600+
toolName: tr.toolName,
601+
toolNamespace: tr.toolNamespace,
602+
}) ?? (text.trim() ? text : KIRO_EMPTY_TOOL_RESULT_MESSAGE);
570603
const images = extractKiroImages(tr.content);
571604
const toolUseId = normalizeToolId(tr.toolCallId);
572605
if (!priorCalls.has(toolUseId)) {
@@ -587,10 +620,20 @@ export function buildKiroPayload(
587620
if (turns.length === 0 || turns[0].kind === "assistant") {
588621
turns.unshift({ kind: "user", content: KIRO_CONTINUATION_MESSAGE, images: [], toolResults: [] });
589622
}
590-
if (turns.at(-1)?.kind === "assistant") {
623+
// Kiro requires the request to end with a user turn, so a trailing assistant turn always gets
624+
// one appended (the pop below throws otherwise). What that turn SAYS is the load-bearing part.
625+
//
626+
// Normally a trailing assistant turn means the model stopped mid-task, and a continuation/retry
627+
// prompt is correct. A DELIVERED final answer is the exception: the turn already ended, and
628+
// telling that model to "continue" or to call the completion tool again reopens finished work —
629+
// the completed-task-behaves-like-an-open-goal loop. It gets a neutral acknowledgement instead:
630+
// structurally valid, but carrying no instruction to resume.
631+
const trailing = turns.at(-1);
632+
if (trailing?.kind === "assistant") {
633+
const resumeText = completionMode === "text_fallback" ? KIRO_COMPLETION_RETRY_MESSAGE : KIRO_CONTINUATION_MESSAGE;
591634
turns.push({
592635
kind: "user",
593-
content: completionMode === "text_fallback" ? KIRO_COMPLETION_RETRY_MESSAGE : KIRO_CONTINUATION_MESSAGE,
636+
content: trailing.finalAnswer ? KIRO_ANSWER_DELIVERED_MESSAGE : resumeText,
594637
images: [],
595638
toolResults: [],
596639
});
@@ -638,10 +681,17 @@ export function buildKiroPayload(
638681
currentUim.userInputMessageContext = { ...(currentUim.userInputMessageContext ?? {}), tools: kiroTools };
639682
}
640683
if (completionMode === "text_fallback") {
641-
if (currentUim.content !== KIRO_COMPLETION_RETRY_MESSAGE) {
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) {
642688
currentUim.content = appendTurnText(currentUim.content, KIRO_COMPLETION_RETRY_MESSAGE);
643689
}
644-
} else if (!currentUim.userInputMessageContext?.toolResults && currentUim.content !== KIRO_CONTINUATION_MESSAGE) {
690+
} else if (
691+
!currentUim.userInputMessageContext?.toolResults
692+
&& currentUim.content !== KIRO_CONTINUATION_MESSAGE
693+
&& currentUim.content !== KIRO_ANSWER_DELIVERED_MESSAGE
694+
) {
645695
currentUim.content = injectKiroThinkingTags(currentUim.content, parsed);
646696
}
647697

0 commit comments

Comments
 (0)