Skip to content

Commit dc95711

Browse files
t3dotggclaude
andcommitted
fix(server): auto-link only the host's own new change request
A create command beside one that lists change requests (gh pr list, gh api) now links nothing, a URL from another host than the create command's is skipped, and a # right after a separator starts a comment. The link step gets its own span. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4e5eaea commit dc95711

2 files changed

Lines changed: 57 additions & 28 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,24 @@ describe("PullRequestSyncReactor", () => {
15941594
output: "https://github.com/owner/repository/pull/124",
15951595
linked: [],
15961596
},
1597+
{
1598+
name: "a create command after a comment that starts at a separator",
1599+
input: "gh pr view --json url --jq .url;# note; gh pr create --fill",
1600+
output: "https://github.com/owner/repository/pull/124",
1601+
linked: [],
1602+
},
1603+
{
1604+
name: "a create beside a command that lists other pull requests",
1605+
input: "gh pr list --json url --jq '.[].url' && gh pr create --fill > /dev/null",
1606+
output: "https://github.com/owner/repository/pull/120",
1607+
linked: [],
1608+
},
1609+
{
1610+
name: "another host's URL beside a create command",
1611+
input: "gh pr create --fill",
1612+
output: "https://gitlab.com/owner/repository/-/merge_requests/3",
1613+
linked: [],
1614+
},
15971615
])("links what the command created: $name", ({ input, output, linked }) =>
15981616
Effect.scoped(
15991617
Effect.gen(function* () {

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

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ const isChangeRequestCloseCommand = (command: string) =>
5555
const COMMAND_START =
5656
"(?:^|[;&|({`\\n]|\\b(?:do|then|else)\\s)\\s*(?:(?:\\w+=\\S*|env|time|nohup|timeout\\s+\\S+)\\s+)*";
5757
const CREATE_COMMANDS = sourceControlDefinitions.definitions.flatMap(
58-
({ changeRequestCreateCommand: command }) =>
58+
({ kind, changeRequestCreateCommand: command }) =>
5959
command === undefined
6060
? []
61-
: [{ command, run: new RegExp(COMMAND_START + command.source, "gu") }],
61+
: [{ kind, command, run: new RegExp(COMMAND_START + command.source, "gu") }],
6262
);
6363
const createCommandCount = (code: string) =>
6464
CREATE_COMMANDS.reduce((count, { run }) => count + [...code.matchAll(run)].length, 0);
@@ -69,8 +69,11 @@ const SHELL_WRAPPER = /^\s*(?:\S*\/)?(?:ba|z|da|k)?sh\s+-l?c\s+(?=["'])([\s\S]*)
6969
const SHELL_WORD_PIECE = /"((?:\\[\s\S]|[^"\\])*)"|'([^']*)'|\\([\s\S])|(\S)/gu;
7070
// Heredoc bodies, quoted strings and comments are text a command carries, not commands it runs.
7171
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;
72+
const QUOTED_OR_COMMENT = /'[^']*'|"(?:\\[\s\S]|[^"\\])*"|(?<=^|[\s;&|(])#[^\n]*/gmu;
7373
const COMMAND_SEPARATOR = /&&|\|\||[;|\n]/u;
74+
// Commands that print other change requests' URLs, so output beside them proves nothing.
75+
const LISTS_CHANGE_REQUESTS =
76+
/\b(?:gh\s+(?:pr\s+(?:list|status)|search\s+prs|api)|glab\s+(?:mr\s+list|api))\b/u;
7477
const URL_LINE = /^[ \t]*(https?:\/\/\S+)[ \t]*$/gmu;
7578

7679
/** The commands a shell input runs, without the text it carries. */
@@ -104,10 +107,16 @@ function changeRequestsCreatedBy(input: string, output: string) {
104107
.split(COMMAND_SEPARATOR)
105108
.filter((part) => createCommandCount(part) === 0)
106109
.join("\n");
110+
if (LISTS_CHANGE_REQUESTS.test(otherCommands)) return [];
111+
const creators = new Set(
112+
CREATE_COMMANDS.filter(({ run }) => code.match(run) !== null).map(({ kind }) => kind),
113+
);
107114
const created = new Map<string, ThreadPullRequestKey & { readonly url: string }>();
108115
for (const [, url = ""] of output.matchAll(URL_LINE)) {
109-
const link = sourceControlDefinitions.parseChangeRequestUrl(url)?.link;
110-
if (link === undefined) continue;
116+
const parsed = sourceControlDefinitions.parseChangeRequestUrl(url);
117+
// Only the host whose create command ran can have printed a new change request.
118+
if (parsed === null || !creators.has(parsed.definition.kind)) continue;
119+
const { link } = parsed;
111120
const named = new RegExp(`(?:^|(?<=[\\s#/=(]))${link.number}(?=$|[\\s);&|/])`, "u");
112121
if (named.test(otherCommands)) continue;
113122
const key = normalizeThreadPullRequestKey(link);
@@ -568,30 +577,32 @@ export const make = Effect.gen(function* () {
568577
);
569578

570579
// An agent that opens a pull request from a shell gets it linked as link_pull_request would.
571-
const linkCreatedChangeRequests = (threadId: ThreadId, item: CommandExecution) =>
572-
isOrchestrationV2WorkActive(item.status)
573-
? Effect.void
574-
: Effect.forEach(
575-
changeRequestsCreatedBy(item.input, item.output ?? ""),
576-
(created) =>
577-
engine
578-
.dispatch({
579-
type: "thread.pull-request.link",
580-
// Stable per command, so a repeated settlement cannot relink one the user unlinked.
581-
commandId: CommandId.make(
582-
`server:pr-created-link:${threadId}:${item.id}:${threadPullRequestKeyOf(created)}`,
583-
),
584-
threadId,
585-
...created,
586-
source: "created",
587-
})
588-
.pipe(
589-
Effect.catchCause(
590-
logSkipped("linking a pull request a command created failed", { threadId }),
591-
),
580+
const linkCreatedChangeRequests = Effect.fn("PullRequestSyncReactor.linkCreatedChangeRequests")(
581+
function* (threadId: ThreadId, item: CommandExecution) {
582+
if (isOrchestrationV2WorkActive(item.status)) return;
583+
yield* Effect.forEach(
584+
changeRequestsCreatedBy(item.input, item.output ?? ""),
585+
(created) =>
586+
engine
587+
.dispatch({
588+
type: "thread.pull-request.link",
589+
// Stable per command, so a repeated settlement cannot relink one the user unlinked.
590+
commandId: CommandId.make(
591+
`server:pr-created-link:${threadId}:${item.id}:${threadPullRequestKeyOf(created)}`,
592592
),
593-
{ discard: true },
594-
);
593+
threadId,
594+
...created,
595+
source: "created",
596+
})
597+
.pipe(
598+
Effect.catchCause(
599+
logSkipped("linking a pull request a command created failed", { threadId }),
600+
),
601+
),
602+
{ discard: true },
603+
);
604+
},
605+
);
595606

596607
const start: PullRequestSyncReactor["Service"]["start"] = Effect.fn(
597608
"PullRequestSyncReactor.start",

0 commit comments

Comments
 (0)