Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/adapters/exec-tool-result-normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
* `function_call_output` is parsed with `isError: false`. Cursor combines this set with
* `isFailedEmptyExecWrapper` below for Computer Use, where a failed wrapper is separately marked
* `isError`.
*
* `(?=(X))\1` pins each wildcard run to its maximal match — without it, adjacent `\n+`/`\s*`
* runs can repartition a newline block combinatorially (the ReDoS shape this had before).
*/
export const EMPTY_EXEC_OUTPUT_REGEX = /^(?:(?:Script completed|Command finished|Execution finished)[^\n]*\n+)?(?:Wall time[^\n]*\n+)?(?:Output:\s*)?(?:<empty>)?\s*$/;
export const EMPTY_EXEC_OUTPUT_REGEX = /^(?:(?:Script completed|Command finished|Execution finished)(?=([^\n]*))\1(?=(\n+))\2)?(?:Wall time(?=([^\n]*))\3(?=(\n+))\4)?(?:Output:(?=(\s*))\5)?(?:<empty>)?(?=(\s*))\6$/;

function skipFailedWrapperBlankSeparators(text: string, start: number): number {
let index = start;
Expand Down
41 changes: 41 additions & 0 deletions tests/adapters/exec-tool-result-normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { parseRequest } from "../../src/responses/parser";
import {
CODE_MODE_HOST_CONTRACT_SENTENCE,
CODE_MODE_HOST_FAILURE_GUIDANCE,
EMPTY_EXEC_OUTPUT_REGEX,
annotateCodeModeHostFailure,
} from "../../src/adapters/exec-tool-result-normalize";

Expand Down Expand Up @@ -112,3 +113,43 @@ describe("code-mode host failure annotation", () => {
});
});

describe("empty exec output wrapper detection", () => {
test("recognizes each optional section and their combinations", () => {
for (const text of [
"Script completed\nWall time 0.1 seconds\nOutput:\n",
"Script completed\nWall time 0.1 seconds\nOutput:\n<empty>\n",
"Command finished\nOutput:\n",
"Execution finished\n\n\nWall time 1s\n\nOutput:\n\n<empty>\n\n",
"Wall time 5s\n",
"Wall time 5s\nOutput:\n<empty>",
"Output:<empty>",
"Output:\n\n\n",
"<empty>",
"\n\n\n",
]) {
expect(EMPTY_EXEC_OUTPUT_REGEX.test(text)).toBe(true);
}
for (const text of [
"Script completed",
"Script completed\nreal output\n",
"Script failed\nOutput:\n",
"Output: hi\n",
"text\n<empty>",
"x<empty>",
"Script completed\nWall time\nOutput:\n<empty>x",
"Wall time\n<empty> trailing",
]) {
expect(EMPTY_EXEC_OUTPUT_REGEX.test(text)).toBe(false);
}
});

// The previous pattern let adjacent `\n+`/`\s*` quantifiers repartition a newline block
// combinatorially; these inputs keep that a timeout-scale regression rather than a silent one.
test("stays linear on pathological whitespace runs", () => {
const newlines = "\n".repeat(200_000);
expect(EMPTY_EXEC_OUTPUT_REGEX.test(`Script completed\n${newlines}!`)).toBe(false);
expect(EMPTY_EXEC_OUTPUT_REGEX.test(`Output:${newlines}`)).toBe(true);
expect(EMPTY_EXEC_OUTPUT_REGEX.test(`Script completed\nWall time x\n${newlines}trailing`)).toBe(false);
});
});

Loading