Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(work): register gh PRs created after a heredoc in the same shell call (#20361)

Arvin Xu08232e8efa32
diff --git a/packages/database/src/models/work/__tests__/githubToolResult.test.ts b/packages/database/src/models/work/__tests__/githubToolResult.test.ts
index dd618157d3e..6c415293a32 100644
--- a/packages/database/src/models/work/__tests__/githubToolResult.test.ts
+++ b/packages/database/src/models/work/__tests__/githubToolResult.test.ts
@@ -284,6 +284,116 @@ describe('normalizeGithubShellToolResult', () => {
});
});
+ it('registers gh pr create that follows a heredoc body with an apostrophe', () => {
+ // Claude Code writes the PR body with a heredoc in the same Bash call; the
+ // body is file content, so its `panel's` must not read as an open quote.
+ const operation = normalizeGithubShellToolResult({
+ data: {
+ command: [
+ "cd /repo && cat > /tmp/pr-body.md <<'EOF'",
+ "Rename in the panel's menu now opens a dialog.",
+ 'EOF',
+ 'gh pr create --base canary --title "Fix rename" --body-file /tmp/pr-body.md',
+ ].join('\n'),
+ output: 'https://github.com/lobehub/lobehub/pull/20360\n',
+ },
+ toolName: 'Bash',
+ });
+
+ expect(operation?.params).toMatchObject({
+ changeType: 'created',
+ identifier: 'lobehub/lobehub#20360',
+ resourceType: 'github_pull_request',
+ title: 'Fix rename',
+ });
+ });
+
+ it('treats an unquoted newline as a command separator', () => {
+ const operation = normalizeGithubShellToolResult({
+ data: {
+ command: 'git push -u origin fix/x\ngh pr create --title "Fix x" --body "Details"',
+ output: 'https://github.com/lobehub/lobehub/pull/20361\n',
+ },
+ toolName: 'Bash',
+ });
+
+ expect(operation?.params).toMatchObject({
+ identifier: 'lobehub/lobehub#20361',
+ title: 'Fix x',
+ });
+ });
+
+ it('keeps a newline inside a quoted body as part of the argument', () => {
+ const operation = normalizeGithubShellToolResult({
+ data: {
+ command: 'gh pr create --title "Fix y" --body "line one\nline two"',
+ output: 'https://github.com/lobehub/lobehub/pull/20362\n',
+ },
+ toolName: 'Bash',
+ });
+
+ expect(operation?.params).toMatchObject({
+ identifier: 'lobehub/lobehub#20362',
+ title: 'Fix y',
+ });
+ });
+
+ it('strips a heredoc body inside a codex login-shell wrapper', () => {
+ const payload = [
+ "cat > /tmp/b.md <<'EOF'",
+ "It's ready",
+ 'EOF',
+ 'gh pr create --title "Fix z" --body-file /tmp/b.md',
+ ].join('\n');
+ const operation = normalizeGithubShellToolResult({
+ data: {
+ command: `/bin/zsh -lc '${payload.replaceAll("'", `'\\''`)}'`,
+ exitCode: 0,
+ output: 'https://github.com/lobehub/lobehub/pull/20363\n',
+ },
+ toolName: 'command_execution',
+ });
+
+ expect(operation?.params).toMatchObject({
+ identifier: 'lobehub/lobehub#20363',
+ title: 'Fix z',
+ });
+ });
+
+ it('keeps a heredoc example inside a quoted body verbatim', () => {
+ // A `<<` inside quotes is literal text, not a redirection β€” the body
+ // snapshot must still mirror what was sent to GitHub.
+ const body = ['Repro:', 'cat <<EOF', 'hello', 'EOF', 'Done.'].join('\n');
+ const operation = normalizeGithubShellToolResult({
+ data: {
+ command: `gh pr create --title "Fix w" --body "${body}"`,
+ output: 'https://github.com/lobehub/lobehub/pull/20365\n',
+ },
+ toolName: 'Bash',
+ });
+
+ expect(operation?.params).toMatchObject({
+ identifier: 'lobehub/lobehub#20365',
+ title: 'Fix w',
+ });
+ expect(operation?.params.content).toBe(body);
+ });
+
+ it('leaves a quoted `<<` without a delimiter line untouched', () => {
+ const operation = normalizeGithubShellToolResult({
+ data: {
+ command: 'gh pr create --title "Shift a << b" --body "Details"',
+ output: 'https://github.com/lobehub/lobehub/pull/20364\n',
+ },
+ toolName: 'Bash',
+ });
+
+ expect(operation?.params).toMatchObject({
+ identifier: 'lobehub/lobehub#20364',
+ title: 'Shift a << b',
+ });
+ });
+
it('skips failed commands and non-gh shell output', () => {
expect(
normalizeGithubShellToolResult({
diff --git a/packages/database/src/models/work/shellCommandParsing.ts b/packages/database/src/models/work/shellCommandParsing.ts
index f34cc85f0c5..0be4faf62d6 100644
--- a/packages/database/src/models/work/shellCommandParsing.ts
+++ b/packages/database/src/models/work/shellCommandParsing.ts
@@ -7,10 +7,68 @@
const CONTROL_OPERATORS = new Set(['&&', '||', ';', '|', '&']);
+interface PendingHeredoc {
+ delimiter: string;
+ stripTabs: boolean;
+}
+
+/**
+ * Read an unquoted here-document operator at `start` β€” `<<EOF`, `<<-EOF`,
+ * `<<'EOF'`, `<< "EOF"` β€” and return its delimiter plus the index just past
+ * it. A `<<<` here-string, or a `<<` with no delimiter word, is not one.
+ */
+const readHeredocOperator = (
+ input: string,
+ start: number,
+): { end: number; heredoc: PendingHeredoc } | null => {
+ if (!input.startsWith('<<', start) || input[start + 2] === '<' || input[start - 1] === '<')
+ return null;
+
+ let i = start + 2;
+ const stripTabs = input[i] === '-';
+ if (stripTabs) i++;
+ while (input[i] === ' ' || input[i] === '\t') i++;
+
+ const quote = input[i] === "'" || input[i] === '"' ? input[i] : '';
+ if (quote) i++;
+ const word = /^[\w.-]+/.exec(input.slice(i))?.[0];
+ if (!word) return null;
+ i += word.length;
+ if (quote) {
+ if (input[i] !== quote) return null;
+ i++;
+ }
+
+ return { end: i, heredoc: { delimiter: word, stripTabs } };
+};
+
+/**
+ * Skip the bodies of the heredocs opened on the line that just ended, in
+ * order, returning the index of the first line after the last delimiter. A
+ * body missing its delimiter runs to the end of input, as in the shell.
+ */
+const skipHeredocBodies = (input: string, start: number, pending: PendingHeredoc[]): number => {
+ let i = start;
+ for (const { delimiter, stripTabs } of pending) {
+ while (i < input.length) {
+ const lineEnd = input.indexOf('\n', i);
+ const line = input.slice(i, lineEnd === -1 ? input.length : lineEnd);
+ i = lineEnd === -1 ? input.length : lineEnd + 1;
+ if ((stripTabs ? line.replace(/^\t+/, '') : line) === delimiter) break;
+ }
+ }
+ return i;
+};
+
/**
* Minimal POSIX-ish tokenizer: whitespace splitting with single/double quote
- * and backslash handling. Returns null on unterminated quotes β€” better to
- * skip registration than to mis-attribute flag values.
+ * and backslash handling. An unquoted newline ends a command like `;` does, so
+ * a multi-line script yields one segment per line. An unquoted heredoc
+ * operator drops its body: the body is file content, not command text, and an
+ * apostrophe in prose (`panel's`) would otherwise read as an unterminated
+ * quote and void the `gh pr create` on the next line. A `<<` inside quotes
+ * (a heredoc example in a PR body) stays literal. Returns null on unterminated
+ * quotes β€” better to skip registration than to mis-attribute flag values.
*
* Deliberately hand-rolled instead of adding a `shell-quote`-style dependency:
* a real shell parser would also expand what we must keep literal (`$VAR`,
@@ -25,6 +83,7 @@ const tokenizeShellCommand = (input: string): string[] | null => {
const tokens: string[] = [];
let current = '';
let hasCurrent = false;
+ let pendingHeredocs: PendingHeredoc[] = [];
let i = 0;
const push = () => {
@@ -72,10 +131,22 @@ const tokenizeShellCommand = (input: string): string[] | null => {
hasCurrent = true;
i += 2;
}
+ } else if (ch === '\n') {
+ push();
+ tokens.push(';');
+ i = skipHeredocBodies(input, i + 1, pendingHeredocs);
+ pendingHeredocs = [];
} else if (/\s/.test(ch)) {
push();
i++;
} else {
+ const operator = ch === '<' ? readHeredocOperator(input, i) : null;
+ if (operator) {
+ push();
+ pendingHeredocs.push(operator.heredoc);
+ i = operator.end;
+ continue;
+ }
current += ch;
hasCurrent = true;
i++;
@@ -137,8 +208,9 @@ const expandShellWrapperSegment = (segment: string[]): string[][] => {
};
/**
- * Parse raw shell command text into simple-command token segments: tokenize,
- * split on control operators, and expand login-shell `-c` wrappers in place.
+ * Parse raw shell command text into simple-command token segments: tokenize
+ * (dropping heredoc bodies), split on control operators and unquoted newlines,
+ * and expand login-shell `-c` wrappers in place.
* Returns null when the text cannot be tokenized (unterminated quoting).
*/
export const parseShellCommandSegments = (command: string): string[][] | null => {