Skip to content

fix(github): Refuse moving a branch to a commit GitHub already has - #3703

Closed
sentry-junior[bot] wants to merge 4 commits into
mainfrom
fix/github-push-empty-pr-guard
Closed

sentry-junior[bot] wants to merge 4 commits into
mainfrom
fix/github-push-empty-pr-guard

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

GitHub closes an open PR at once when a push leaves its head branch with no commits ahead of its base. This happened in #3047, where a --force-with-lease push sent old main to a stacked PR branch.

The Git push policy now refuses that push before it leaves the sandbox, and makes no extra GitHub request:

  • Git sends only the objects that the remote does not have. A push that moves a branch to old main, or to any commit that GitHub already has, sends a pack with 0 objects.
  • The host reads the pack header and gives plugins packObjectCount, and creates on each ref update.
  • The GitHub policy refuses a push with 0 pack objects that updates an existing branch. Deletes and new branches are allowed.

Tradeoff: the policy also refuses a rewind of a branch to one of its own older commits. The denial tells the agent to push a revert commit instead.

The github-code workflow reads state after a push and tells the agent to report a PR that its push closed.

Follow-up: deleting a branch also closes its open PR. This PR does not check deletes.

via @dcramer.

--

View Session [Sentry]

@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 21, 2026 6:42pm UTC

Request Review

@dcramer

dcramer commented Nov 18, 2026

Copy link
Copy Markdown
Member

the approach here doesnt work for me as it adds another roundtrip to github on every push

is there just something we can tweak so the agent never ends up in this situation in the first place?

Replace the open pull request lookup with a check of the pack object
count, so the push policy makes no extra GitHub request.
@sentry-junior

sentry-junior Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Yes. I swapped the lookup for a check that adds no GitHub request (the default branch lookup stays, with its 10-minute cache).

Git sends only the objects that the remote lacks. When the agent moves a branch to old main, or to any commit GitHub already has, the pack has 0 objects. The host already buffers the push body, so it now reads the 12-byte pack header. The policy refuses a push with 0 pack objects that updates an existing branch. That is what happened in #3047. I replayed that --force-with-lease push against a local remote with real git: 0 objects, refused. Normal pushes with new commits still go through.

The cost: the agent can't rewind a branch to an older commit of its own. The denial tells it to push a revert commit instead. The skill also checks state after a push, so a closed PR gets reported.

@sentry-junior sentry-junior Bot changed the title fix(github): Refuse pushes that leave an open PR with no commits ahead fix(github): Refuse moving a branch to a commit GitHub already has Nov 18, 2026
@sentry-junior
sentry-junior Bot marked this pull request as ready for review November 18, 2026 18:22
@sentry-junior
sentry-junior Bot requested a review from dcramer November 18, 2026 18:22
@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 18, 2026
@dcramer

dcramer commented Nov 18, 2026

Copy link
Copy Markdown
Member

did we trace how the agent got in this situation in the first place tho? this feels more like defense-in-depth vs root cause

@dcramer

dcramer commented Nov 21, 2026

Copy link
Copy Markdown
Member

I'm a little bit concerned about the complexity of this change. I don't understand Git primitives well enough to know if this is going to be good or bad, if this is the correct change, or if this is going to cause common user experience issues

@sentry-junior

sentry-junior Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

In plain terms, a git push uploads only the commits GitHub doesn't already have. If a push uploads nothing new but still moves an existing branch, the branch is being pointed at something GitHub already has, such as old main, an older commit, or another branch's commit. The policy refuses exactly that case. It reads a 12-byte header at the start of the upload that holds the object count.

What this means for normal work:

Flow Result
Commit, then push Allowed. A new commit is always uploaded.
Amend, rebase, or squash, then force-push Allowed. Rewriting makes new commits.
Merge the base into a PR branch Allowed. The merge commit is new.
Create a new branch at an existing commit Allowed.
Delete a branch Allowed. Not checked here.
Drop the last commit (reset --hard HEAD~1 + force-push) Refused. The agent pushes a revert commit instead.
Reset a branch to match main or another branch Refused.

The two refused rows are the only behavior change you'd notice. Both are rare for an agent, and both are what closes a PR. If the header can't be read, the push is refused, the same as the current unreadable-push rules. I tested the parser against real git push output, including a force-push and push options.

My take: this is safe for the common flows, but it's defense-in-depth, not the fix for how the agent ended up pushing a stale checkout. That fix is separate and doesn't depend on this PR. If the plugin API change and the refused rewinds aren't worth it to you, closing this PR is a reasonable call.

@dcramer

dcramer commented Nov 21, 2026

Copy link
Copy Markdown
Member

Doesn't this effectively disallow rebases for the most part? What we were actually trying to solve for was when somebody asked to rebase, sometimes what would happen is Junior would accidentally close the branch for whatever reason. As a user, never have this problem when I rebase, so I was trying to understand what was going wrong here in the flow

@sentry-junior

sentry-junior Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

No, normal rebases still work. A rebase rewrites each commit, so every rebased commit gets a new SHA that GitHub has never seen, and the push uploads them. This check only fires when a push uploads nothing and still moves the branch. A rebase or git pull --rebase that changes nothing is a no-op push, so the check doesn't fire on it either.

These rebase-adjacent cases are refused:

  • Undoing a rebase. Force-pushing the branch back to its old commit, such as ORIG_HEAD, is refused because GitHub already has that commit.
  • A rebase that drops every PR commit. This happens when the base already has all of them, for example a stacked PR whose parent merged. The branch then equals its base, so GitHub would close the PR anyway.

What went wrong in that flow: the rebase itself was fine. Partway through the task, the agent's working copy was reset to a fresh clone of main, and the agent wasn't told. The branch's local commit was gone, so the local branch pointed at old main. The agent then force-pushed it with --force-with-lease. The lease only checks that the remote branch hasn't moved since the agent last saw it, and it hadn't, so the push went through. The branch then had no commits ahead of its base, and GitHub closed the PR. You don't hit this because your checkout doesn't silently swap out under you.

So this PR is a guard on the push, not the fix for that. The real fix is to tell the agent when its working copy was reset during a task, so it re-fetches the branch before it pushes. That's a separate change. If you'd rather fix the cause and skip this guard, closing this PR is fine.

@github-actions

Copy link
Copy Markdown
Contributor

7 screenshot changes — 7 changed · 0 added · 0 removed

Review screenshots in Frameshift

Automation Run Details · Desktop
Automation Run Details · Desktop
Changed
Conversations Pinned · Desktop
Conversations Pinned · Desktop
Changed
Conversations Pinned Light · Desktop
Conversations Pinned Light · Desktop
Changed
Conversations Sections Collapsed · Desktop
Conversations Sections Collapsed · Desktop
Changed
Inbox Item Evidence Card · Desktop
Inbox Item Evidence Card · Desktop
Changed
Inbox Item Evidence Card Light · Desktop
Inbox Item Evidence Card Light · Desktop
Changed
Show 1 more changed screenshot
Inbox Palette Pages · Desktop
Inbox Palette Pages · Desktop
Changed

@dcramer

dcramer commented Nov 21, 2026

Copy link
Copy Markdown
Member

Okay, but removing a commit and force pushing it is a totally normal flow in a PR as long as it doesn't remove every commit

@sentry-junior

sentry-junior Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

You're right, and that's a real flaw in this version. Dropping the last of three commits with reset --hard HEAD~1 and a force-push moves the branch to a commit GitHub already has, so this check refuses it, even though the PR still has two commits.

Without asking GitHub, the push can't tell "dropped one commit" apart from "dropped every commit". It doesn't know the PR's base. So there are only two ways to fix it:

  1. Ask GitHub only on the rare push. Keep the free pack check as a filter. Look up the open PR and compare it to its base only when a push uploads nothing and moves an existing branch. Normal pushes still make no extra request, and dropping a commit works. It does bring back the lookup code from the first version, which adds to the complexity you already flagged.
  2. Drop this guard. Fix the cause instead: tell the agent when its working copy was reset mid-task, so it re-fetches the branch before it pushes.

I'd go with 2 and close this PR. The guard can't be both cheap and correct, and the cause is a missing notice, not git usage.

@dcramer

dcramer commented Nov 21, 2026

Copy link
Copy Markdown
Member

Can you dig into what happened with the reset? Because I'm just trying to really fix this core problem here. And I don't know if it's because the sandbox got dropped or something else is going on here

@sentry-junior

sentry-junior Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

I dug in. It was the sandbox, and the cause is in how we resume it, not in git.

Timeline

  1. The agent's sandbox VM session had been running for about 24 hours. We reuse one named sandbox per conversation and keep extending its timeout.
  2. About 5 minutes before the end, HTTPS to github.com started failing with certificate has expired. Vercel's network proxy issues a certificate when the session starts, and it lasts 24 hours. So the fix commit was made locally but couldn't be pushed.
  3. Then Vercel ended the session at its 24-hour max session duration (Pro/Enterprise limit). The running command died with exit 143 (SIGTERM).
  4. The next tool call went through tryRestoreHintedSandbox in chat/sandbox/session.ts, which calls Sandbox.get({ name, resume: true }). Vercel started a new session of the same named sandbox. We create these sandboxes with persistent: false, and the SDK says that flag disables "automatic restore of the filesystem between sessions". So the new session booted from the Workspace snapshot: a fresh clone of main from two days earlier. The reflog showed only clone and reset.
  5. We record that path as resumed. The sandbox name is unchanged, and nothing tells the agent the filesystem was reset. It force-pushed HEAD, which was now old main. The lease matched, and GitHub closed the PR.

What to fix

  • Main fix: detect a new VM session on resume. Compare the session id with the one we had. When it changed and the sandbox isn't persistent, tell the agent in the tool result that the sandbox restarted, and that files and commits it hadn't pushed are gone. It's small and in one place.
  • Option: create these sandboxes with persistent: true, so Vercel restores the filesystem on resume and nothing is lost. The cost is a filesystem snapshot per sandbox. I didn't find a recorded reason for persistent: false. It came in with the egress proxy work.
  • Cheap extra: start a new session before the 24-hour limit, between Turns rather than mid-command.

With any of these, this PR's push guard isn't needed for this failure. I'd close it and do the session-change notice.

@dcramer

dcramer commented Nov 21, 2026

Copy link
Copy Markdown
Member

Okay, let's be thorough about the analysis. I want a holistic fix, and I don't want to have persistent sandboxes. Unless you're saying we're able to restore the exact same sandbox and persistence would help us a lot. But the problem with persistence is I need to not persist the disk for all of time. I don't know how Vercel is working exactly on persistence, to be fair

@sentry-junior

sentry-junior Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Here's the full picture, then a fix that doesn't need persistence.

How Vercel persistence works. A sandbox is a name with a series of VM sessions. The 24-hour cap applies to each session, not to the sandbox. With persistent: true, Vercel snapshots the disk when a session stops and boots the next session from that snapshot. Retention is configurable: snapshotExpiration is a TTL from last use, and keepLastSnapshots: { count: 1, deleteEvicted: true } keeps only the newest. So the disk wouldn't live forever. You'd pay snapshot storage, and the snapshot is taken at the stop, so a command that was running still dies. It would have saved the lost commit. It's not required for a correct fix, and I agree with skipping it.

The actual defect. Junior can move the agent onto a new session without saying so. All of these paths in chat/sandbox/session.ts do it:

  • Mid-Turn stop (this incident). The keepalive tick hits the stopped session, catches the error, and calls invalidateSession silently. The next tool call runs tryRestoreHintedSandbox: Sandbox.get({ name, resume: true }) boots a new session from the Workspace snapshot. We label that resumed. The command that died returned exit 143 as a normal result.
  • Between Turns (inferred from the code, not observed). The keepalive only runs during a Turn. Production extends 15 minutes per tick (VERCEL_SANDBOX_KEEPALIVE_MS=900000). A conversation idle longer than that comes back the same silent way. Local work that wasn't pushed is gone, and only the general "sandbox files can disappear" rule warns about it.
  • The 24-hour cap. It's predictable, and the egress certificate lasts the same 24 hours. That's why HTTPS broke 5 minutes before the stop.

Fix. The rule: the agent never runs a command on a sandbox reset it wasn't told about.

  1. Track the session. Store the VM sessionId in the conversation's sandbox ref, next to the name. On acquire, if the session isn't the one we last used, mark the sandbox as reset. This one check covers all three paths. It doesn't depend on catching the error that stopped the old session.
  2. Tell the agent. The first sandbox tool result after a reset says: "The sandbox restarted. Files and commits that were not pushed are gone, and repositories are back at the Workspace snapshot. Fetch the branch before you push." The command that died gets the same notice in place of the bare exit 143.
  3. Rotate before the cap. At the start of a Turn, if the session is older than about 20 hours, start a new one there, with the same notice. Then the 24-hour stop and the expired certificate don't land mid-task.

This replaces this PR. The push guard would only catch one symptom, and its refusals break normal flows.

@sentry-junior

sentry-junior Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of #3710, which tells the agent when its sandbox session was replaced and is being extended to persist the session id and rotate sessions before the 24-hour limit.

@sentry-junior sentry-junior Bot closed this Nov 21, 2026
sentry-junior Bot added a commit that referenced this pull request Nov 21, 2026
A command killed by a session stop came back as a normal exit (143), and
the next call ran on a new sandbox without the old files and said nothing.

- After a 137 or 143 exit, probe the session. A stopped session fails the
  call through the existing sandbox-unavailable tool error.
- When a sandbox tool call runs on a new sandbox that lost the earlier
  files, its result gets a sandbox_notice that says so and asks the agent
  to check its checkout first.

Refs #3047, #3703

Co-Authored-By: David Cramer <[email protected]>
dcramer added a commit that referenced this pull request Nov 22, 2026
…ns (#3710)

When a Vercel Sandbox session stops during a Turn, two things go wrong
today:

1. The provider kills the running command with a signal and returns a
normal command result, so the call reads as an ordinary non-zero exit
and never reaches the sandbox-unavailable tool error.
2. The next call reacquires the sandbox. A non-persistent sandbox
resumes into a new session from its source snapshot, and the agent is
not told. Local commits and other files are gone. An agent that then
pushes `HEAD` can push an old commit over its own work.

Vercel stops a session 24 hours after it starts, so a long conversation
can lose its session in the middle of a Turn.

Context: #3047. #3703 is the push-guard
backstop for the same failure.

## Change

- **Kill path.** After a SIGTERM or SIGKILL exit, the bash executor
probes the session with the probe that cached sessions already use. A
stopped session throws the provider error. That marks the session
unavailable and goes through the existing
`createSandboxUnavailableToolError` path, which says the outcome is not
reliable and the next call uses a fresh session. When the session is
live, or the probe fails for another reason, the command's own exit
stands.
- **Replacement notice.** `SandboxRef.toolSession` stores the sandbox
and session id of the last sandbox tool call (`bash`, `readFile`,
`writeFile`, `editFile`). It is saved in thread state next to
`app_sandbox_id`, so it survives Run and execution slice boundaries. A
call on a different session gets a `sandbox_notice` in its result when
the earlier files are gone: the Sandbox changed, or a new session
started on a Sandbox that does not keep files across sessions. The
notice says that files and local commits are gone and to check HEAD and
the branch before acting. This also covers a conversation that stayed
idle past the keepalive and resumes into a new session.
- `SandboxSession.keepsFilesAcrossSessions` reports this. Vercel maps it
from `sandbox.persistent`. Docker is always `false`, because a restarted
container keeps its id, so a new session id is a new container.
- Only tool calls update the stored session, so the between-step
AGENTS.md read cannot hide a replacement.
- `switchWorkspace` clears it, because the switch result already says
that files do not carry over.
- **Session rotation at Turn start.** `SandboxSession.startedAtMs` gives
the session start time. On Vercel it comes from the session's
`startedAt`, or `createdAt` when `startedAt` is not set. On Docker it
comes from the container's `StartedAt`. When the first Run of a Turn
reopens a stored sandbox whose session is older than 20 hours, it stops
that session and resumes a new one before any work starts. A Run that
resumes a paused Turn keeps its session, so rotation does not drop
unpushed work in the middle of a Turn. A sandbox that does not keep
files gets the replacement notice on its next tool call.
- The bash, file, and command executors move to
`sandbox/tool-executors.ts` to keep `session.ts` under the file length
limit. The only behavior change there is the probe.

Reused: the sandbox-unavailable tool error and session invalidation, the
probe from the reacquire path in #3515, and the stored
`SandboxRef` with its existing `onSandboxRefChanged` persistence.
Unknown outcomes from cancelled commands (#915) stay as
they are.

## Limits and open questions

- **Turn start after recovery.** "Turn start" means the Turn has no
paused record. When recovery restarts a Turn after a lost worker without
a paused record, that Run can still rotate an old session.
- **Persistent sandboxes.** Rotation stops the session and resumes it
right away. Conversation sandboxes are non-persistent today. For a
persistent sandbox, a resume while the stop still snapshots has not been
checked against the live API.
- **Plugin tools.** A plugin tool that runs a command through the
sandbox records the new session too, and it drops `sandbox_notice`. If
the first call after a replacement is a plugin tool, the agent does not
see the notice.
- **Stop timing.** The kill check assumes the session API rejects the
probe right after the provider kills the command. If a stopping session
still accepts the probe, the kill is reported as a normal exit, and the
next call still gets the replacement notice.

<!-- junior-request-attribution:start -->
via @dcramer.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A2129e94419c987dc504daaf71f9cea79 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A2129e94419c987dc504daaf71f9cea79)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A2129e94419c987dc504daaf71f9cea79/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: David Cramer <[email protected]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 06a37928 Deployed Nov 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant