Skip to content

Decide agent writable folders per session; grant each thread only its own storage - #7994

Merged
ymichael merged 4 commits into
mainfrom
bb/unify-agent-writable-folders-thr_ur4sww5mkn
Nov 22, 2026
Merged

ymichael merged 4 commits into
mainfrom
bb/unify-agent-writable-folders-thr_ur4sww5mkn

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The daemon built one list of extra writable folders per environment runtime (runtimeWorkspaceWriteRoots in apps/host-daemon/src/runtime-manager.ts) and handed it to every provider process as static provider options. Because the list was per environment, it could not name a thread's own storage folder, so it named the shared <data-dir>/thread-storage root instead (#7625). That let every thread write into every other thread's storage, and it showed Claude Code the root as an extra working directory. The git list for linked worktrees covered objects/, refs/ and logs/ but not <commonDir>/packed-refs.lock, which git takes for every ref deletion, so git branch -D failed and git stash pop left a stray refs/stash (#4488). The server had no way to add folders of its own, which multi-repo and non-git projects need.

What changed

  • Contract, HOST_DAEMON_PROTOCOL_VERSION 276 → 277. The thread runtime context (thread.start, thread.rewind.prepare, turn.submit/thread.goal.clear resumeContext) gains a required additionalWorkspaceFolders: string[]. host.read_workspace_agent_context gains folderPaths and returns folders: {path, isGitRepo}[].
  • Server (thread-runtime-config.ts). It decides each session's extra folders. The list is empty in this PR, so these folders come later from project/environment folders. When it is not empty, the server asks the daemon whether each folder is a git repo and adds a "You are working across these folders" block to the instructions (workspace-folder-instructions.ts). Single-folder threads see no change: the RPC gets folderPaths: [] and no block is added.
  • Daemon (RuntimeManager.resolveThreadWorkspaceWriteRoots). For each session start, resume or rewind it builds the list from the workspace's worktree git roots, the server's folders, each extra folder's worktree git roots and the thread's own <thread-storage>/<threadId> folder, with duplicates removed.
  • agent-runtime. Write roots move from AgentRuntimeOptions and the static adapter options to per-thread StartThreadArgs/ResumeThreadArgs/rewind args. They are stored in the thread's runtime config and sent in providerOptions.additionalWorkspaceWriteRoots on session start, turn start and steer. The wire key is unchanged, so no bridge needed a protocol change. Model listing and other provider-level calls no longer carry roots.
  • host-workspace. Worktree git roots add <commonDir>/packed-refs and <commonDir>/packed-refs.lock. config and hooks/ stay protected.
  • Claude Code bridge (session-options.ts). additionalDirectories (--add-dir) now gets only existing directories. File roots stay in the sandbox allowWrite list. Live testing showed that passing packed-refs.lock through --add-dir stops the allowWrite grant from taking effect.

Where this differs from the plan

  • fix(provider-claude-code): SDK sandbox option drops the user's sandbox.filesystem.allowWrite #3034 needs no code change; it does not reproduce. I ran the issue's repro (claude -p --setting-sources … --settings <bb sandbox json>, the same --settings JSON the Agent SDK builds from its sandbox option) on Claude Code 2.1.325 and on the reporter's 2.1.259. With allowWrite set in user settings (absolute path and ~/Library/Caches) and in project settings, the user's path stayed writable alongside bb's roots every time. Reading allowWrite from settings files and merging it in would duplicate what Claude Code already does, and would risk changing how relative paths resolve. I suggest closing fix(provider-claude-code): SDK sandbox option drops the user's sandbox.filesystem.allowWrite #3034 as not reproducible.
  • Codex worktree threads cannot delete branches or drop stashes: packed-refs.lock is outside the git writable roots #4488 is fixed on macOS only. Seatbelt (both Codex and Claude Code) honours a file-path grant for a lock file that doesn't exist yet. On Linux, both bubblewrap sandboxes drop writable paths that don't exist (Codex bwrap.rs: writable_roots.retain(... .exists()); sandbox-runtime: "Skipping non-existent write path"). So the new entries are harmless there but don't help. A Linux fix would need <commonDir> writable with config/hooks denied, which Codex's legacy sandboxPolicy can't express.
  • Codex's own gitWritableRootsForWorkspace (in plugins/provider-codex/src/session-params.ts) is not removed. It computes a subset of what the daemon now sends, so it never conflicts. Removing it is a separate simplification.

How you verified

  • Live, before the fix (scratch linked worktree on macOS):
    • codex sandbox with today's roots: git branch -D fails with Unable to create '<commonDir>/packed-refs.lock': Operation not permitted, and stash pop leaves refs/stash.
    • Adding packed-refs and packed-refs.lock: the branch is deleted, refs/stash is cleared, and config and hooks/ are still denied.
    • Claude CLI: the same result, except that file roots passed through --add-dir break it.
  • Live, after the fix: pnpm start:worktree from this branch (isolated dev data dir), with a Claude Code thread and a Codex thread, each in a new worktree environment:
    • Claude Code's "Additional working directories" are the worktree git dirs plus …/thread-storage/thr_f56qdzz4h7, the thread's own folder, which matches $BB_THREAD_STORAGE. git branch qa-tmp && git branch -D qa-tmp gives Deleted branch qa-tmp.
    • Codex: Deleted branch qa-tmp, stash-ref=[], its own storage folder is writable, and touch <thread-storage>/other-thread.txt gives Operation not permitted.
  • Tests:
    • runtime.command-contract.test.ts: two threads in one runtime get different roots on thread/start, and turn/start carries the thread's own roots.
    • runtime-manager.test.ts: per-thread storage folder, git roots for the main and extra worktree folders, duplicates removed.
    • workspace-agent-context.test.ts: git status for a plain folder, a repo, a subfolder of a repo and a missing folder.
    • workspace-folder-instructions.test.ts: the folders block.
    • thread-runtime-config.test.ts: single-folder threads unchanged.
    • Claude bridge: only directories go to --add-dir, and every root goes to allowWrite.
    • Contract fixtures and protocol version updated.
  • Commands: pnpm exec turbo run typecheck for @bb/agent-runtime, @bb/host-daemon, @bb/server, @bb/host-daemon-contract and bb-plugin-provider-claude-code. Targeted turbo run test for those packages and @bb/host-workspace (full @bb/agent-runtime unit suite). turbo run lint on the touched packages.

Fixes #7625
Fixes #4488

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 4 commits November 21, 2026 11:50
The server now sends each session's extra workspace folders to the
daemon (empty for now). The daemon adds the workspace's git metadata,
each extra folder's git metadata and the thread's own storage folder,
and passes that list per thread instead of baking one list into the
environment runtime. Agents are told their folders when there is more
than one.

- Thread storage: grant <root>/<threadId>, not the shared root (#7625)
- Worktrees: grant <commonDir>/packed-refs and packed-refs.lock so ref
  deletion and stash pop work in the sandbox (#4488)
- Claude Code: pass only existing directories to --add-dir; file roots
  stay in the sandbox allowWrite list
- HOST_DAEMON_PROTOCOL_VERSION 277

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n resume

Git deletes a packed ref by writing packed-refs.new and renaming it over
packed-refs, so the shared git write roots now include packed-refs.new.
Without it, Codex failed `git branch -D` on any packed branch.

The thread's own storage folder is now created whenever its write roots are
resolved, so resume and rewind recreate it after archive wipes storage, not
only thread start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 796cdcc into main Nov 22, 2026
52 checks passed
@ymichael
ymichael deleted the bb/unify-agent-writable-folders-thr_ur4sww5mkn branch November 22, 2026 04:02
k0d3r1s added a commit to valksor/vbb that referenced this pull request Nov 24, 2026
Brings upstream get-bb#8006 (Android timeline text selection) and get-bb#7994
(per-session agent writable folders).

Conflict resolution keeps the fork's protocol numbering at 286 with
upstream's 277 incompatible, adopts the per-thread
resolveThreadWorkspaceWriteRoots flow alongside the fork's MCP session
binding in the daemon thread handlers, updates fork test fixtures for
the new required additionalWorkspaceFolders and folders contract
fields, and keeps both sides' bridge test fixtures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant