Skip to content

Codex worktree threads cannot delete branches or drop stashes: packed-refs.lock is outside the git writable roots #4488

Closed

Description

@Ernxst

Summary

In a Codex thread on a bb-managed worktree, git branch -D fails inside the sandbox because git cannot create <commonDir>/packed-refs.lock. git stash pop hits the same lock and exits 0, but leaves refs/stash pointing at the popped commit. I expected both to work, since creating a branch and pushing a stash already do.

Versions and environment

  • bb 0.43.4, desktop app, macOS
  • Provider: codex, Codex CLI 0.157.1, default workspace permissions
  • Environment: bb worktree (--new-environment worktree)

Steps to reproduce

In a Codex thread spawned with --new-environment worktree, run inside the sandbox without escalation:

  1. git branch bb-sandbox-test && git branch -D bb-sandbox-test
  2. echo probe > probe.txt && git add probe.txt && git stash push -- probe.txt && git stash pop
  3. git stash list; git for-each-ref refs/stash

Expected vs actual

Actual:

$ git branch bb-sandbox-test && git branch -D bb-sandbox-test
error: could not delete reference refs/heads/bb-sandbox-test: Unable to create '<repo>/.git/packed-refs.lock': Operation not permitted
(exit 1)

$ git stash push -- probe.txt && git stash pop
Saved working directory and index state WIP on <branch>: 3068402 ...
...
error: Unable to create '<repo>/.git/packed-refs.lock': Operation not permitted
Dropped refs/stash@{0} (184822bc04fcacfccc47b84c8f4b46d016f654dd)
(exit 0)

$ git stash list; git for-each-ref refs/stash
184822bc04fcacfccc47b84c8f4b46d016f654dd commit	refs/stash

Expected: the branch is deleted, and after stash pop no refs/stash remains.

Evidence

gitWritableRootsForWorkspace grants the worktree's own git dir, <commonDir>/objects, and the ref directory for HEAD (refs/heads and logs/refs/heads). It never grants the common dir itself, and git takes <commonDir>/packed-refs.lock to delete any ref.

Granting <commonDir>/packed-refs, packed-refs.lock, refs/ and logs/refs/ would cover branch and stash deletion while keeping <commonDir>/config and hooks/ protected.

What you ruled out

  • Creating a branch and git stash push succeed in the same thread, so the git dir is partly writable; only ref deletion fails.
  • git fetch --prune origin succeeded, but there was nothing to prune, so it neither confirms nor rules out the same failure.
  • Not the permission-profile problem in feat(codex): select named permission profiles in execution controls #2524: the default workspace permissions are in effect here.

Suggested priority and effort (optional)

Every Codex worktree thread that deletes a branch needs an unsandboxed retry or an approval. The stash case fails silently and leaves a stray ref. The change is to one function.

Checks

  • I reproduced this on the latest release or on main, or I say above that I could not.
  • I searched open and closed issues for the same problem.
  • If an agent wrote this, the body ends with > AGENT GENERATED and links the thread or report.

Reproduced by an agent in a local bb thread; the thread is not publicly linkable.

AGENT GENERATED

Activity

  1. added theissue type on Sep 30, 2026
  2. added
    providers
    workspaces
    provider-codex
    partial-repro
    on Sep 30, 2026
  3. SawyerHood commented on Sep 30, 2026

    @SawyerHood
    Collaborator

    Verified configuration report: https://get-bb.github.io/reports/issues/4488.html

    PARTIALLY REPRODUCED in two clean checkouts. Synthetic attached and detached worktree metadata yields additional write-root lists that omit packed-ref and stash paths; JSON config and typed policy agree. No policy was applied and no Codex/Git operations were run, so native sandbox enforcement and reported branch/stash outcomes remain unverified. Both-run evidence and controls are included.

  4. Ernxst commented on Sep 30, 2026

    @Ernxst
    Author

    Following up on the verification report: this checks the part it left unverified, sandbox enforcement in a live Codex thread.

    The run used a Codex thread (--new-environment worktree, permission mode accept-edits) on a fresh scratch repository, with bb 0.43.4, Codex CLI 0.157.1, git 2.50.1 (Apple Git-155) and macOS. Every command ran inside the sandbox without escalation. The path probe needed one command approval, allowed once with no extra permissions; its deny lines show it still ran sandboxed.

    Writable roots the thread received, git entries only (the others are the worktree, /private/tmp, $TMPDIR and bb thread storage):

    <commonDir>/logs
    <commonDir>/logs/refs/heads/bb
    <commonDir>/objects
    <commonDir>/refs
    <commonDir>/refs/heads/bb
    <commonDir>/worktrees/<worktree>
    

    These include all of <commonDir>/refs and <commonDir>/logs, which is more than the report's fixture produced. The common directory itself is still not a root.

    Creating and then removing a file at each common-dir path, on branch refs/heads/bb/<name>:

    $ c="$(git rev-parse --path-format=absolute --git-common-dir)"
    $ b="$(dirname "$(git symbolic-ref HEAD)")"
    $ for p in objects/probe "$b/probe" packed-refs.lock refs/stash.probe logs/refs/stash.probe config.probe; do
        if touch "$c/$p" 2>/dev/null; then echo "ok   $p"; rm -f "$c/$p"; else echo "deny $p"; fi
      done
    ok   objects/probe
    ok   refs/heads/bb/probe
    deny packed-refs.lock
    ok   refs/stash.probe
    ok   logs/refs/stash.probe
    deny config.probe
    

    The two ref commands in the same thread:

    $ git branch bb-sandbox-test && git branch -D bb-sandbox-test; echo "exit $?"
    error: could not delete reference refs/heads/bb-sandbox-test: Unable to create '<commonDir>/packed-refs.lock': Operation not permitted
    exit 1
    
    $ echo probe >> README && git stash push -- README && git stash pop; echo "exit $?"
    Saved working directory and index state WIP on bb/<name>: 164ef05 scratch
    ...
    error: Unable to create '<commonDir>/packed-refs.lock': Operation not permitted
    Dropped refs/stash@{0} (b80d0f9ed2dce93d43948abfb47516c0a2440c6c)
    exit 0
    
    $ git stash list; git for-each-ref refs/stash
    b80d0f9ed2dce93d43948abfb47516c0a2440c6c commit	refs/stash
    

    packed-refs.lock is the only path the ref commands need that the sandbox denies, and both failures name it. config stays protected. Since refs/ and logs/ are already writable in a live thread, the missing write is narrower than the issue body suggests: <commonDir>/packed-refs.lock and its rename over <commonDir>/packed-refs. Whether allowing those two paths is enough is not tested here.

    AGENT GENERATED

  5. dimiboi commented on Nov 24, 2026

    @dimiboi

    One more manifestation on bb desktop/CLI 0.50.1, macOS 26.7.1 arm64, Codex CLI 0.172.0, and Git 2.54.0 (Apple Git-157). In a BB-managed linked worktree, git commit printed the error below, returned 0, and advanced HEAD. Expected a successful commit without the lock error.

    error: Unable to create '<common-git-dir>/packed-refs.lock': Operation not permitted
    

    The managed workspace-write scope included the shared objects, refs, and logs directories and the worktree’s private Git directory. It omitted the common Git directory and the lock path. No lock file was present when inspected.

    I reproduced the exact stderr, exit status, and HEAD change in a disposable files-backend linked worktree. A normal git commit --allow-empty -m probe was clean. With chflags uchg applied to the disposable common Git directory, the same command printed the error and still advanced HEAD (git rev-parse HEAD before and after). I cleared the flag and removed the scratch repository. This isolates the Git behavior under denied lock creation. I did not retest the commit on a BB build containing #7994.

    AGENT GENERATED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    partial-reproprovider-codexprovidersworkspaces

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions