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
Conversation
|
😎 Merged successfully - details. |
|
React Doctor found no issues in the changed files. 🎉 Reviewed by React Doctor for commit |
tatoalo
marked this pull request as ready for review
July 22, 2026 09:59
Contributor
Prompt To Fix All With AIFix 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 |
k11kirky
approved these changes
Jul 22, 2026
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
force-pushed
the
posthog-code/precheckout-existing-pr
branch
from
July 22, 2026 10:13
c288055 to
c480d36
Compare
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
force-pushed
the
posthog-code/precheckout-existing-pr
branch
from
July 22, 2026 10:15
c480d36 to
635769a
Compare
tatoalo
marked this pull request as draft
July 22, 2026 10:15
tatoalo
marked this pull request as ready for review
July 22, 2026 10:19
Contributor
Prompt To Fix All With AIFix 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 |
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo 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
force-pushed
the
posthog-code/precheckout-existing-pr
branch
from
July 22, 2026 10:30
635769a to
10f37a6
Compare
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
force-pushed
the
posthog-code/precheckout-existing-pr
branch
from
July 22, 2026 10:31
10f37a6 to
8ef8d6e
Compare
tatoalo
marked this pull request as draft
July 22, 2026 10:31
tatoalo
marked this pull request as ready for review
July 22, 2026 10:32
tatoalo
enabled auto-merge (squash)
July 22, 2026 10:32
Contributor
Prompt To Fix All With AIFix 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 |
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
force-pushed
the
posthog-code/precheckout-existing-pr
branch
from
July 22, 2026 10:40
8ef8d6e to
2a8d038
Compare
tatoalo
marked this pull request as draft
July 22, 2026 10:40
tatoalo
marked this pull request as ready for review
July 22, 2026 10:41
Contributor
|
Reviews (4): Last reviewed commit: "perf(agent): overlap existing PR checkou..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
checkoutExistingPullRequestas a fire-and-forget promise right afterwaitForRepoReady, thenawaitit only after skill-bundle install, native-resume prep, and the MCP relay server have run in between.packages/agent/src/server/pr-checkout.tsresolves 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.gh pr checkoutitself, so a pre-checkout failure can't block session start.