Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): resolve the heredoc redirection word like the shell

liuzixiang9741a07975f0
diff --git a/packages/agent-runtime/src/mutation-recovery.test.ts b/packages/agent-runtime/src/mutation-recovery.test.ts
index 2c71c1056..1f9c3cf9a 100644
--- a/packages/agent-runtime/src/mutation-recovery.test.ts
+++ b/packages/agent-runtime/src/mutation-recovery.test.ts
@@ -141,6 +141,24 @@ describe("isPatchCommand", () => {
expect(isPatchCommand("cat <<-EOF\n body\n EOF\npatch notes\nEOF")).toBe(false);
});
+ it("reads the heredoc redirection word the way the shell does", () => {
+ // #2016 review round five: `<< EOF` (a space before the word) and
+ // `<<\EOF` (an escaped word) both name the same delimiter, and an empty
+ // quoted word is still a word. Missing any of them made payload become
+ // commands, or a real following patch disappear into a body.
+ expect(isPatchCommand("cat << EOF\npatch notes\nEOF")).toBe(false);
+ expect(isPatchCommand("cat <<\tEOF\npatch notes\nEOF")).toBe(false);
+ expect(isPatchCommand("cat << EOF\nbody\nEOF\npatch -p1 < x.diff")).toBe(true);
+ expect(isPatchCommand("cat <<- EOF\n\tbody\n\tEOF\npatch notes")).toBe(true);
+ expect(isPatchCommand("cat <<\\EOF\nhello\nEOF\npatch -p1 < x.diff")).toBe(true);
+ expect(isPatchCommand("cat <<'EOF'\nhello\nEOF\npatch -p1 < x.diff")).toBe(true);
+ expect(isPatchCommand('cat <<"EOF"\nhello\nEOF\npatch -p1 < x.diff')).toBe(true);
+ // An empty quoted word: the body ends at an empty line, so text before it
+ // is payload and text after it is a command.
+ expect(isPatchCommand('cat <<""\npatch notes')).toBe(false);
+ expect(isPatchCommand('cat <<""\n\npatch notes')).toBe(true);
+ });
+
it("queues every heredoc on the opening line before reading bodies", () => {
// #2016 review round three: one stored tag made the parser treat the
// second redirect as payload on the opening line, so `cat <<A <<B` never
diff --git a/packages/agent-runtime/src/mutation-recovery.ts b/packages/agent-runtime/src/mutation-recovery.ts
index 545ee77b0..594623096 100644
--- a/packages/agent-runtime/src/mutation-recovery.ts
+++ b/packages/agent-runtime/src/mutation-recovery.ts
@@ -176,10 +176,17 @@ function splitCommandSegments(command: string): string[] {
}
/**
- * Read the tag after `<<` (or `<<-`). Null when there is no tag word.
+ * Read the redirection word after `<<` (or `<<-`). Null when there is none.
+ *
+ * The word is resolved the way the shell resolves it, because that spelling is
+ * what a closing line must match: spaces or tabs may separate it from `<<`,
+ * quotes and backslashes are removed (`<<\EOF` and `<<'EOF'` both mean `EOF`),
+ * and an empty quoted word (`<<""`) is still a word — its body ends at an
+ * empty line. Whether a word exists is tracked apart from its text for exactly
+ * that case (#2016 review round five).
*
* `stripTabs` reports the `<<-` form, whose delimiter line may carry leading
- * tabs; every other form requires the tag to be the whole line.
+ * tabs; every other form requires the delimiter to be the whole line.
*/
function parseHeredocTag(
command: string,
@@ -191,30 +198,69 @@ function parseHeredocTag(
stripTabs = true;
index += 1;
}
+ // `<< EOF` is the same redirection as `<<EOF`; only spaces and tabs, since a
+ // word cannot start on the next line.
+ while (index < command.length && (command[index] === " " || command[index] === "\t")) {
+ index += 1;
+ }
let text = "";
+ /** Whether a redirection word exists; an empty quoted word still counts. */
+ let hasWord = false;
let quote: '"' | "'" | null = null;
while (index < command.length) {
const char = command[index]!;
- if (quote) {
- if (char === quote) {
+ if (quote === "'") {
+ if (char === "'") {
+ quote = null;
+ index += 1;
+ continue;
+ }
+ hasWord = true;
+ text += char;
+ index += 1;
+ continue;
+ }
+ if (quote === '"') {
+ if (
+ char === "\\" &&
+ index + 1 < command.length &&
+ (command[index + 1] === '"' || command[index + 1] === "\\")
+ ) {
+ hasWord = true;
+ text += command[++index]!;
+ index += 1;
+ continue;
+ }
+ if (char === '"') {
quote = null;
index += 1;
continue;
}
+ hasWord = true;
text += char;
index += 1;
continue;
}
if (char === '"' || char === "'") {
quote = char;
+ hasWord = true;
+ index += 1;
+ continue;
+ }
+ // An unquoted backslash escapes the next character and is dropped, so
+ // `<<\EOF` names the delimiter `EOF` and its closing line is `EOF`.
+ if (char === "\\") {
+ hasWord = true;
+ if (index + 1 < command.length) text += command[++index]!;
index += 1;
continue;
}
if (/\s/.test(char) || ";|&<>".includes(char)) break;
+ hasWord = true;
text += char;
index += 1;
}
- return text ? { text, stripTabs, end: index } : null;
+ return hasWord ? { text, stripTabs, end: index } : null;
}
/**
diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.ts
index dc6ce5bbd..741d1ac97 100644
--- a/packages/agent-runtime/src/runtime.test.ts
+++ b/packages/agent-runtime/src/runtime.test.ts
@@ -2067,6 +2067,11 @@ describe("DesktopAgentRuntime configuration matching", () => {
{ command: "cat <<'EOF'\n EOF\nordinary text\npatch notes\nEOF" },
{ command: "cat <<A <<B\nA\nordinary text\npatch notes\nB" },
{ command: "cat <<EOF\n\tEOF\npatch -p1 < x.diff" },
+ // Spaced and escaped redirection words name the same delimiter (#2016
+ // review round five); both bodies stay stdin.
+ { command: "cat << EOF\npatch notes\nEOF" },
+ { command: "cat <<\\EOF\npatch notes\nEOF" },
+ { command: 'cat <<""\npatch notes' },
];
for (const [index, args] of payloadOnly.entries()) {
for (let attempt = 1; attempt <= 5; attempt += 1) {