Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.

perf(agent): overlap existing PR checkout with startup - #3684

Merged
trunk-io[bot] merged 1 commit into
mainfrom
posthog-code/precheckout-existing-pr
Jul 22, 2026
Merged

perf(agent): overlap existing PR checkout with startup#3684
trunk-io[bot] merged 1 commit into
mainfrom
posthog-code/precheckout-existing-pr

Conversation

@tatoalo

@tatoalo tatoalo commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Problem

When a cloud task run resumes onto an existing open PR, the agent server checked out the PR branch only after the agent session had started. The checkout sat on the critical path, serializing behind skill-bundle install, native-resume prep, and MCP relay startup, adding wall-clock latency to every run that lands on an existing PR.

Changes

  • Overlap the existing-PR checkout with session startup: kick off checkoutExistingPullRequest as a fire-and-forget promise right after waitForRepoReady, then await it only after skill-bundle install, native-resume prep, and the MCP relay server have run in between.
  • New packages/agent/src/server/pr-checkout.ts resolves the PR head branch, skips the checkout when that branch is already active, and returns a structured result (already_active / checked_out / failed) so startup never throws on a checkout hiccup.
  • On failure, the server logs a warning and falls back to the existing system-prompt instruction that tells the agent to gh pr checkout itself, so a pre-checkout failure can't block session start.

@trunk-io

trunk-io Bot commented Jul 22, 2026

Copy link
Copy Markdown

😎 Merged successfully - details.

@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 2a8d038.

@tatoalo tatoalo self-assigned this Jul 22, 2026
@tatoalo
tatoalo marked this pull request as ready for review July 22, 2026 09:59
@greptile-apps

greptile-apps Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
packages/agent/src/server/pr-checkout.ts:56-57
**Branch Name Hides Remote Mismatch**

For a fork PR whose head branch has the same name as the current local branch, `headRefName` matches even though the local branch belongs to a different remote. This returns `already_active` without checking out the PR head, so the agent can edit or commit against the base branch instead of the existing PR.

### Issue 2 of 2
packages/agent/src/server/agent-server.ts:1508-1517
**Checkout Races Repository Writes**

This starts `gh pr checkout` while skill installation and native-resume preparation write under the same `repositoryPath`. If the target branch replaces or tracks `.posthog/skills` or `sessions`, checkout can conflict with those writes, fail after partial work, or leave startup artifacts associated with the wrong branch.

Reviews (1): Last reviewed commit: "perf(agent): overlap existing PR checkou..." | Re-trigger Greptile

Comment thread packages/agent/src/server/pr-checkout.ts Outdated
Comment thread packages/agent/src/server/agent-server.ts Outdated
tatoalo added a commit that referenced this pull request Jul 22, 2026
Overlaps the best-effort existing-PR checkout with session startup so the branch is ready by the time the ACP session starts, instead of the agent paying for `gh pr checkout` on the critical path mid-task.

Wraps the fire-and-await in try/finally so a throw from skill-bundle install / native-resume prep / MCP relay startup can never abandon an in-flight checkout mutating the working tree; bounds the checkout with an overall 60s deadline + AbortController so a hung `gh` can't stall session start; validates the prUrl shape before handing it to `gh`; and adds unit + gating tests.

Review fixes for #3684: dangling-checkout on the error path, startup-latency budget, prUrl validation, and test coverage for the checkout args, cwd forwarding, detached-HEAD, empty-branch, view-succeeds/checkout-fails, and the auto-publish gating contract.

Generated-By: PostHog Code
Task-Id: 29c3cf5a-82e2-4f36-aeba-d0322ab741a5
tatoalo added a commit that referenced this pull request Jul 22, 2026
Overlaps the best-effort existing-PR checkout with session startup so the branch is ready by the time the ACP session starts, instead of the agent paying for `gh pr checkout` on the critical path mid-task.

Wraps the fire-and-await in try/finally so a throw from skill-bundle install / native-resume prep / MCP relay startup can never abandon an in-flight checkout mutating the working tree; bounds the checkout with an overall 60s deadline + AbortController so a hung `gh` can't stall session start; validates the prUrl shape before handing it to `gh`; and adds unit + gating tests.

Review fixes for #3684: dangling-checkout on the error path, startup-latency budget, prUrl validation, and test coverage for the checkout args, cwd forwarding, detached-HEAD, empty-branch, view-succeeds/checkout-fails, and the auto-publish gating contract.

Generated-By: PostHog Code
Task-Id: 29c3cf5a-82e2-4f36-aeba-d0322ab741a5
@tatoalo
tatoalo force-pushed the posthog-code/precheckout-existing-pr branch from c288055 to c480d36 Compare July 22, 2026 10:13
tatoalo added a commit that referenced this pull request Jul 22, 2026
Overlaps the best-effort existing-PR checkout with session startup so the branch is ready by the time the ACP session starts, instead of the agent paying for `gh pr checkout` on the critical path mid-task.

Wraps the fire-and-await in try/finally so a throw from skill-bundle install / native-resume prep / MCP relay startup can never abandon an in-flight checkout mutating the working tree; bounds the checkout with an overall 60s deadline + AbortController so a hung `gh` can't stall session start; validates the prUrl shape before handing it to `gh`; compares HEAD against the PR head SHA (not just the branch name) so a fork PR sharing a branch name with a different local remote can't short-circuit as already_active; and adds unit + gating tests.

Review fixes for #3684 (subagent review + Greptile threads): dangling-checkout on the error path, startup-latency budget, prUrl validation, branch-name-vs-SHA mismatch, documented overlap safety (skill bundles install under gitignored .posthog/), and test coverage for checkout args, cwd forwarding, detached-HEAD/SHA match, empty-branch, missing-OID fallback, view-succeeds/checkout-fails, and the auto-publish gating contract.

Generated-By: PostHog Code
Task-Id: 29c3cf5a-82e2-4f36-aeba-d0322ab741a5
tatoalo added a commit that referenced this pull request Jul 22, 2026
Overlaps the best-effort existing-PR checkout with session startup so the branch is ready by the time the ACP session starts, instead of the agent paying for `gh pr checkout` on the critical path mid-task.

Wraps the fire-and-await in try/finally so a throw from skill-bundle install / native-resume prep / MCP relay startup can never abandon an in-flight checkout mutating the working tree; bounds the checkout with an overall 60s deadline + AbortController so a hung `gh` can't stall session start; validates the prUrl shape before handing it to `gh`; compares HEAD against the PR head SHA (not just the branch name) so a fork PR sharing a branch name with a different local remote can't short-circuit as already_active; and adds unit + gating tests.

Review fixes for #3684 (subagent review + Greptile threads): dangling-checkout on the error path, startup-latency budget, prUrl validation, branch-name-vs-SHA mismatch, documented overlap safety (skill bundles install under gitignored .posthog/), and test coverage for checkout args, cwd forwarding, detached-HEAD/SHA match, empty-branch, missing-OID fallback, view-succeeds/checkout-fails, and the auto-publish gating contract.

Generated-By: PostHog Code
Task-Id: 29c3cf5a-82e2-4f36-aeba-d0322ab741a5
@tatoalo
tatoalo force-pushed the posthog-code/precheckout-existing-pr branch from c480d36 to 635769a Compare July 22, 2026 10:15
@tatoalo
tatoalo marked this pull request as draft July 22, 2026 10:15
@tatoalo
tatoalo marked this pull request as ready for review July 22, 2026 10:19
@greptile-apps

greptile-apps Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
packages/agent/src/server/pr-checkout.ts:118-119
**Detached HEAD looks active** When the repository is detached at the PR head commit, `currentHead` equals `prHeadOid`, so this returns `already_active` without switching to the PR branch. The session can then create commits that are not attached to the PR branch, leaving the existing PR unchanged. Verify that HEAD is attached to the expected branch before skipping `gh pr checkout`.

Reviews (2): Last reviewed commit: "perf(agent): overlap existing PR checkou..." | Re-trigger Greptile

Comment thread packages/agent/src/server/pr-checkout.ts Outdated
Comment thread packages/agent/src/server/pr-checkout.ts Outdated
@veria-ai

veria-ai Bot commented Jul 22, 2026

Copy link
Copy Markdown

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

tatoalo added a commit that referenced this pull request Jul 22, 2026
Overlaps the best-effort existing-PR checkout with session startup so the branch is ready by the time the ACP session starts, instead of the agent paying for `gh pr checkout` on the critical path mid-task.

Wraps the fire-and-await in try/finally so a throw from skill-bundle install / native-resume prep / MCP relay startup can never abandon an in-flight checkout mutating the working tree; bounds the checkout with an overall 60s deadline + AbortController so a hung `gh` can't stall session start; parses prUrl (and the workspace origin) and rejects PRs whose repository does not match the connected workspace before handing anything to `gh`; only skips checkout when HEAD is attached to the PR branch at its head commit (not detached, not just a SHA/branch-name match); and adds unit + gating tests.

Review fixes for #3684 (subagent review + Greptile/veria-ai threads): dangling-checkout on the error path, startup-latency budget, prUrl validation + workspace repo-membership check, detached-HEAD vs branch-attached distinction, branch-name-vs-SHA mismatch, documented overlap safety (skill bundles install under gitignored .posthog/), and test coverage for checkout args, cwd forwarding, detached-HEAD-at-SHA, foreign-repo rejection, empty-branch, missing-OID fallback, view-succeeds/checkout-fails, and the auto-publish gating contract.

Generated-By: PostHog Code
Task-Id: 29c3cf5a-82e2-4f36-aeba-d0322ab741a5
tatoalo added a commit that referenced this pull request Jul 22, 2026
Overlaps the best-effort existing-PR checkout with session startup so the branch is ready by the time the ACP session starts, instead of the agent paying for `gh pr checkout` on the critical path mid-task.

Wraps the fire-and-await in try/finally so a throw from skill-bundle install / native-resume prep / MCP relay startup can never abandon an in-flight checkout mutating the working tree; bounds the checkout with an overall 60s deadline + AbortController so a hung `gh` can't stall session start; parses prUrl (and the workspace origin) and rejects PRs whose repository does not match the connected workspace before handing anything to `gh`; only skips checkout when HEAD is attached to the PR branch at its head commit (not detached, not just a SHA/branch-name match); and adds unit + gating tests.

Review fixes for #3684 (subagent review + Greptile/veria-ai threads): dangling-checkout on the error path, startup-latency budget, prUrl validation + workspace repo-membership check, detached-HEAD vs branch-attached distinction, branch-name-vs-SHA mismatch, documented overlap safety (skill bundles install under gitignored .posthog/), and test coverage for checkout args, cwd forwarding, detached-HEAD-at-SHA, foreign-repo rejection, empty-branch, missing-OID fallback, view-succeeds/checkout-fails, and the auto-publish gating contract.

Generated-By: PostHog Code
Task-Id: 29c3cf5a-82e2-4f36-aeba-d0322ab741a5
@tatoalo
tatoalo force-pushed the posthog-code/precheckout-existing-pr branch from 635769a to 10f37a6 Compare July 22, 2026 10:30
tatoalo added a commit that referenced this pull request Jul 22, 2026
Overlaps the best-effort existing-PR checkout with session startup so the branch is ready by the time the ACP session starts, instead of the agent paying for `gh pr checkout` on the critical path mid-task.

Wraps the fire-and-await in try/finally so a throw from skill-bundle install / native-resume prep / MCP relay startup can never abandon an in-flight checkout mutating the working tree; bounds the checkout with an overall 60s deadline + AbortController so a hung `gh` can't stall session start; parses prUrl (and the workspace origin) and rejects PRs whose repository does not match the connected workspace before handing anything to `gh`; only skips checkout when HEAD is attached to the PR branch at its head commit (not detached, not just a SHA/branch-name match); and adds unit + gating tests.

Review fixes for #3684 (subagent review + Greptile/veria-ai threads): dangling-checkout on the error path, startup-latency budget, prUrl validation + workspace repo-membership check, detached-HEAD vs branch-attached distinction, branch-name-vs-SHA mismatch, documented overlap safety (skill bundles install under gitignored .posthog/), and test coverage for checkout args, cwd forwarding, detached-HEAD-at-SHA, foreign-repo rejection, empty-branch, missing-OID fallback, view-succeeds/checkout-fails, and the auto-publish gating contract.

Generated-By: PostHog Code
Task-Id: 29c3cf5a-82e2-4f36-aeba-d0322ab741a5
@tatoalo
tatoalo force-pushed the posthog-code/precheckout-existing-pr branch from 10f37a6 to 8ef8d6e Compare July 22, 2026 10:31
@tatoalo
tatoalo marked this pull request as draft July 22, 2026 10:31
@tatoalo
tatoalo marked this pull request as ready for review July 22, 2026 10:32
@tatoalo
tatoalo enabled auto-merge (squash) July 22, 2026 10:32
@greptile-apps

greptile-apps Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
packages/agent/src/server/pr-checkout.ts:157
**Timeout leaves partial checkout.** When the 60-second deadline expires during `gh pr checkout`, aborting the child stops further writes but does not restore the index or working tree. The function then reports `failed` and startup continues, so the agent's fallback checkout can encounter a partially switched or dirty repository and fail again. Preserve the original repository state or restore it after an interrupted checkout before allowing startup to continue.

Reviews (3): Last reviewed commit: "perf(agent): overlap existing PR checkou..." | Re-trigger Greptile

Comment thread packages/agent/src/server/pr-checkout.ts Outdated
@tatoalo
tatoalo disabled auto-merge July 22, 2026 10:37
tatoalo added a commit that referenced this pull request Jul 22, 2026
Overlaps the best-effort existing-PR checkout with session startup so the branch is ready by the time the ACP session starts, instead of the agent paying for `gh pr checkout` on the critical path mid-task.

Wraps the fire-and-await in try/finally so a throw from skill-bundle install / native-resume prep / MCP relay startup can never abandon an in-flight checkout mutating the working tree; bounds the checkout with an overall 60s deadline + AbortController so a hung `gh` can't stall session start; on an interrupted checkout it restores the original branch/HEAD (merge --abort, checkout --force, reset --hard) so the agent's fallback checkout starts clean; parses prUrl (and the workspace origin) and rejects PRs whose repository does not match the connected workspace before handing anything to `gh`; only skips checkout when HEAD is attached to the PR branch at its head commit (not detached, not just a SHA/branch-name match); and adds unit + gating tests.

Review fixes for #3684 (subagent review + Greptile/veria-ai threads): dangling-checkout on the error path, startup-latency budget, prUrl validation + workspace repo-membership check, detached-HEAD vs branch-attached distinction, partial-checkout restore on interrupt/timeout, branch-name-vs-SHA mismatch, documented overlap safety (skill bundles install under gitignored .posthog/), and test coverage for checkout args, cwd forwarding, detached-HEAD-at-SHA, foreign-repo rejection, restore-on-interrupted-checkout, empty-branch, missing-OID fallback, view-succeeds/checkout-fails, and the auto-publish gating contract.

Generated-By: PostHog Code
Task-Id: 29c3cf5a-82e2-4f36-aeba-d0322ab741a5
Overlaps the best-effort existing-PR checkout with session startup so the branch is ready by the time the ACP session starts, instead of the agent paying for `gh pr checkout` on the critical path mid-task.

Wraps the fire-and-await in try/finally so a throw from skill-bundle install / native-resume prep / MCP relay startup can never abandon an in-flight checkout mutating the working tree; bounds the checkout with an overall 60s deadline + AbortController so a hung `gh` can't stall session start; on an interrupted checkout it restores the original branch/HEAD (merge --abort, checkout --force, reset --hard) so the agent's fallback checkout starts clean; parses prUrl (and the workspace origin) and rejects PRs whose repository does not match the connected workspace before handing anything to `gh`; only skips checkout when HEAD is attached to the PR branch at its head commit (not detached, not just a SHA/branch-name match); and adds unit + gating tests.

Review fixes for #3684 (subagent review + Greptile/veria-ai threads): dangling-checkout on the error path, startup-latency budget, prUrl validation + workspace repo-membership check, detached-HEAD vs branch-attached distinction, partial-checkout restore on interrupt/timeout, branch-name-vs-SHA mismatch, documented overlap safety (skill bundles install under gitignored .posthog/), and test coverage for checkout args, cwd forwarding, detached-HEAD-at-SHA, foreign-repo rejection, restore-on-interrupted-checkout, empty-branch, missing-OID fallback, view-succeeds/checkout-fails, and the auto-publish gating contract.

Generated-By: PostHog Code
Task-Id: 29c3cf5a-82e2-4f36-aeba-d0322ab741a5
@tatoalo
tatoalo force-pushed the posthog-code/precheckout-existing-pr branch from 8ef8d6e to 2a8d038 Compare July 22, 2026 10:40
@tatoalo
tatoalo marked this pull request as draft July 22, 2026 10:40
@tatoalo
tatoalo marked this pull request as ready for review July 22, 2026 10:41
@greptile-apps

greptile-apps Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Reviews (4): Last reviewed commit: "perf(agent): overlap existing PR checkou..." | Re-trigger Greptile

@trunk-io
trunk-io Bot merged commit 380c385 into main Jul 22, 2026
40 checks passed
@trunk-io
trunk-io Bot deleted the posthog-code/precheckout-existing-pr branch July 22, 2026 11:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants