Skip to content

Commit 53826a5

Browse files
authored
fix(agent): avoid redundant PR checkout (#3579)
1 parent 204f6f0 commit 53826a5

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

packages/agent/src/server/agent-server.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,8 +3504,12 @@ describe("AgentServer HTTP Mode", () => {
35043504
"https://github.com/org/repo/pull/1",
35053505
);
35063506
expect(prompt).toContain(
3507-
"gh pr checkout https://github.com/org/repo/pull/1",
3507+
"If it is not already checked out, check it out with `gh pr checkout https://github.com/org/repo/pull/1`",
35083508
);
3509+
expect(prompt).toContain(
3510+
"Do not check it out again when it is already active",
3511+
);
3512+
expect(prompt).not.toContain("Check out the existing PR branch");
35093513
expect(prompt).toContain("git_signed_commit");
35103514
expect(prompt).toContain("Committing (signed commits required)");
35113515
expect(prompt).not.toContain("Create a draft pull request");
@@ -3827,13 +3831,18 @@ describe("AgentServer HTTP Mode", () => {
38273831
expect(context).not.toContain("gh pr checkout");
38283832
});
38293833

3830-
it("returns auto-update PR context for Slack-origin runs", () => {
3834+
it("avoids redundant PR checkout for auto-update runs", () => {
38313835
process.env.POSTHOG_CODE_INTERACTION_ORIGIN = "slack";
38323836
const s = createServer();
38333837
const context = (s as unknown as TestableServer).buildDetectedPrContext(
38343838
prUrl,
38353839
);
3836-
expect(context).toContain(`gh pr checkout ${prUrl}`);
3840+
expect(context).toContain(
3841+
`If it is not already checked out, check it out with \`gh pr checkout ${prUrl}\``,
3842+
);
3843+
expect(context).toContain(
3844+
"Do not check it out again when it is already active",
3845+
);
38373846
expect(context).toContain(
38383847
"Make changes, commit, and push to that branch",
38393848
);

packages/agent/src/server/agent-server.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3139,6 +3139,10 @@ export class AgentServer {
31393139
].join("\n");
31403140
}
31413141

3142+
private buildExistingPrCheckoutInstruction(prUrl: string): string {
3143+
return `Continue working on the existing PR branch. If it is not already checked out, check it out with \`gh pr checkout ${prUrl}\`. Do not check it out again when it is already active.`;
3144+
}
3145+
31423146
private buildDetectedPrContext(prUrl: string): string {
31433147
if (!this.shouldAutoPublishCloudChanges()) {
31443148
return (
@@ -3152,7 +3156,7 @@ export class AgentServer {
31523156
`IMPORTANT — OVERRIDE PREVIOUS INSTRUCTIONS ABOUT CREATING BRANCHES/PRs.\n` +
31533157
`You already have an open pull request: ${prUrl}\n` +
31543158
`You MUST:\n` +
3155-
`1. Check out the existing PR branch with \`gh pr checkout ${prUrl}\`\n` +
3159+
`1. ${this.buildExistingPrCheckoutInstruction(prUrl)}\n` +
31563160
`2. Make changes, commit, and push to that branch\n` +
31573161
`You MUST NOT create a new branch, close the existing PR, or create a new PR.`
31583162
);
@@ -3283,7 +3287,7 @@ ${signedCommitInstructions}${prLinkInstructions}${shellEfficiencyInstructions}
32833287
This task already has an open pull request: ${prUrl}
32843288
32853289
After completing the requested changes:
3286-
1. Check out the existing PR branch with \`gh pr checkout ${prUrl}\`
3290+
1. ${this.buildExistingPrCheckoutInstruction(prUrl)}
32873291
2. Stage your changes with \`git add\`, then call the \`git_signed_commit\` tool with a clear \`message\` (do NOT use \`git commit\`/\`git push\` — they are blocked). This commits to the existing PR branch.
32883292
- If the branch is behind its base, call the \`git_signed_merge\` tool first — it merges the base in server-side with a Verified merge commit. Only if it reports a conflict: fetch and rebase locally (\`git fetch origin <base>\`, \`git rebase origin/<base>\`, resolve, \`git rebase --continue\`), then call the \`git_signed_rewrite\` tool to force-update this same PR branch.
32893293
3. For every PR review comment or review thread you addressed, treat the thread as done only after BOTH of these:

0 commit comments

Comments
 (0)