Skip to content

Commit 4e5eaea

Browse files
t3dotggclaude
andcommitted
fix(server): skip quoted create commands and output with extra URLs
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1f9fc5a commit 4e5eaea

2 files changed

Lines changed: 51 additions & 18 deletions

File tree

‎apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,13 @@ describe("PullRequestSyncReactor", () => {
15281528
].join("\n"),
15291529
linked: [124],
15301530
},
1531+
{
1532+
name: "a Codex script quoted in pieces around a heredoc",
1533+
input:
1534+
"/bin/zsh -lc \"cat > /tmp/body.md <<'EOF'\nSet the \"'`hidden`'\" flag.\nEOF\ngh pr create --fill --body-file /tmp/body.md\"",
1535+
output: "https://github.com/owner/repository/pull/62\n",
1536+
linked: [62],
1537+
},
15311538
{
15321539
name: "an older pull request read in the same command",
15331540
input: "gh pr view 120 --json url --jq .url && gh pr create --fill",
@@ -1567,6 +1574,19 @@ describe("PullRequestSyncReactor", () => {
15671574
output: "https://github.com/owner/repository/pull/124",
15681575
linked: [],
15691576
},
1577+
{
1578+
name: "a create command with a separator inside quotes",
1579+
input: "echo 'example: gh pr create; gh pr create --fill' && gh pr view --json url --jq .url",
1580+
output: "https://github.com/owner/repository/pull/124",
1581+
linked: [],
1582+
},
1583+
{
1584+
name: "more URLs than create commands",
1585+
input: "gh pr view --json url --jq .url && gh pr create --fill",
1586+
output:
1587+
"https://github.com/owner/repository/pull/120\nhttps://github.com/owner/repository/pull/124",
1588+
linked: [],
1589+
},
15701590
{
15711591
name: "a create command only in heredoc text",
15721592
input:

‎apps/server/src/orchestration-v2/PullRequestSyncReactor.ts‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,49 +58,62 @@ const CREATE_COMMANDS = sourceControlDefinitions.definitions.flatMap(
5858
({ changeRequestCreateCommand: command }) =>
5959
command === undefined
6060
? []
61-
: [{ command, run: new RegExp(COMMAND_START + command.source, "u") }],
61+
: [{ command, run: new RegExp(COMMAND_START + command.source, "gu") }],
6262
);
63-
const runsChangeRequestCreateCommand = (script: string) =>
64-
CREATE_COMMANDS.some(({ run }) => run.test(script));
65-
66-
// Codex runs each command as `/bin/zsh -lc "<script>"`.
67-
const SHELL_WRAPPER = /^\s*(?:\S*\/)?(?:ba|z|da|k)?sh\s+-l?c\s+(["'])([\s\S]*)\1\s*$/u;
68-
// A heredoc body is text a command writes, such as a pull request body, not commands it runs.
63+
const createCommandCount = (code: string) =>
64+
CREATE_COMMANDS.reduce((count, { run }) => count + [...code.matchAll(run)].length, 0);
65+
66+
// Codex runs each command as `/bin/zsh -lc <script>`, quoting the script in pieces such as
67+
// `"a "'`b`'" c"`.
68+
const SHELL_WRAPPER = /^\s*(?:\S*\/)?(?:ba|z|da|k)?sh\s+-l?c\s+(?=["'])([\s\S]*)$/u;
69+
const SHELL_WORD_PIECE = /"((?:\\[\s\S]|[^"\\])*)"|'([^']*)'|\\([\s\S])|(\S)/gu;
70+
// Heredoc bodies, quoted strings and comments are text a command carries, not commands it runs.
6971
const HEREDOC_BODY = /<<-?[ \t]*(['"]?)(\w+)\1[^\n]*\n[\s\S]*?(?:\n[ \t]*\2[ \t]*(?=\n|$)|$)/gu;
72+
const QUOTED_OR_COMMENT = /'[^']*'|"(?:\\[\s\S]|[^"\\])*"|(?<=^|\s)#[^\n]*/gmu;
7073
const COMMAND_SEPARATOR = /&&|\|\||[;|\n]/u;
7174
const URL_LINE = /^[ \t]*(https?:\/\/\S+)[ \t]*$/gmu;
7275

73-
function shellScript(input: string): string {
74-
const [, quote, script] = SHELL_WRAPPER.exec(input) ?? [];
75-
if (script === undefined) return input;
76-
return quote === '"' ? script.replace(/\\(["\\$`])/gu, "$1") : script;
76+
/** The commands a shell input runs, without the text it carries. */
77+
function shellCode(input: string): string {
78+
const [, wrapped] = SHELL_WRAPPER.exec(input) ?? [];
79+
const script =
80+
wrapped === undefined
81+
? input
82+
: Array.from(
83+
wrapped.matchAll(SHELL_WORD_PIECE),
84+
([, double, single, escaped, bare]) =>
85+
double?.replace(/\\(["\\$`])/gu, "$1") ?? single ?? escaped ?? bare,
86+
).join("");
87+
return script.replace(HEREDOC_BODY, "<<").replace(QUOTED_OR_COMMENT, "''");
7788
}
7889

7990
type CommandExecution = Extract<OrchestrationV2TurnItem, { readonly type: "command_execution" }>;
8091

8192
/**
8293
* The change requests a shell command opened: URLs on lines of their own in the output of a
8394
* command that runs a host's create command. One the command also names, as `gh pr view 12`
84-
* does, existed before it ran.
95+
* does, existed before it ran. More URLs than create commands means something else printed
96+
* some, such as `gh pr list`, so none are taken.
8597
*/
8698
function changeRequestsCreatedBy(input: string, output: string) {
8799
if (!CREATE_COMMANDS.some(({ command }) => command.test(input))) return [];
88-
const script = shellScript(input).replace(HEREDOC_BODY, "<<");
89-
if (!runsChangeRequestCreateCommand(script)) return [];
90-
const otherCommands = script
100+
const code = shellCode(input);
101+
const creates = createCommandCount(code);
102+
if (creates === 0) return [];
103+
const otherCommands = code
91104
.split(COMMAND_SEPARATOR)
92-
.filter((part) => !runsChangeRequestCreateCommand(part))
105+
.filter((part) => createCommandCount(part) === 0)
93106
.join("\n");
94107
const created = new Map<string, ThreadPullRequestKey & { readonly url: string }>();
95108
for (const [, url = ""] of output.matchAll(URL_LINE)) {
96109
const link = sourceControlDefinitions.parseChangeRequestUrl(url)?.link;
97110
if (link === undefined) continue;
98-
const named = new RegExp(`(?:^|(?<=[\\s#/=('"]))${link.number}(?=$|[\\s'");&|/])`, "u");
111+
const named = new RegExp(`(?:^|(?<=[\\s#/=(]))${link.number}(?=$|[\\s);&|/])`, "u");
99112
if (named.test(otherCommands)) continue;
100113
const key = normalizeThreadPullRequestKey(link);
101114
created.set(threadPullRequestKeyOf(key), { ...key, url });
102115
}
103-
return [...created.values()];
116+
return created.size > creates ? [] : [...created.values()];
104117
}
105118

106119
const isPullRequestProviderError = Schema.is(PullRequestProviderError);

0 commit comments

Comments
 (0)