Linked worktrees get a validated grant of the repository Git directory
链接工作树获得经校验的仓库 Git 目录授权
The `workspace-write` boundary of a Session is the directory it works in, and the sandbox policy [reads no Git layout](2026-11-03-session-workspace-root.md). A linked worktree stores its index, its `HEAD`, and the repository's objects and refs in the repository's Git directory, outside the worktree. A Session that enters a worktree therefore cannot run `git add` or `git commit` under `workspace-write`, the default mo
English
Problem
The workspace-write boundary of a Session is the directory it works in, and the sandbox policy reads no Git layout. A linked worktree stores its index, its HEAD, and the repository's objects and refs in the repository's Git directory, outside the worktree. A Session that enters a worktree therefore cannot run git add or git commit under workspace-write, the default mode of the shipped compositions, without an escalation for each command.
A grant derived from the worktree has a forgery risk: the Session can write every file in its directory, including the .git file that names the Git directory.
Decision
The plugin @deepseek-ai/dsh-sandbox-policy-git registers the shared Git directory of the worktree a Session works in through ctx.sandboxPolicy.registerWritableRoot. The worktree service mounts it, so every composition that can move a Session into a worktree has it. The sandbox policy, the working-directory service, and the sandbox providers are unchanged.
The directory is granted only when a Git directory names the Session's directory as a worktree: the .git file points at <common>/worktrees/<name>, whose commondir resolves to <common> and whose gitdir resolves back to that .git file; <common> neither contains the Session's directory nor lies inside it; and <common> holds HEAD, objects, and refs, the entries Git requires of a Git directory. The pointer files that name the worktree lie in <common>/worktrees/<name>, which can be writable without <common>: a deployment or another plugin may grant that directory alone. The plugin therefore refuses a layout whose administration directory lies inside a directory the Session's resolved policy already lets it write, unless the result is the Git directory this plugin already granted. Without that rule a writable <X>/worktrees/<name> yields a grant of <X>, a configured grant of one administration directory widens to the whole Git directory, and a bare repository stored under a directory named worktrees lets the Session use its grant to obtain that directory's parent.
The layout is read at Agent creation and after next() in the working-directory/access waterfall, which every Host move and the validation before each model request pass through. Both callers await the read, so resolve stays synchronous and the grant precedes the first operation in the new directory. Every read is complete and replaces the registration, because a registered directory that no longer exists fails every confined operation of the Session. A grant ends in the step that commits a move to another directory, through a prepended session/event listener, so no policy combines the entered directory with the Git directory of the one left; the window between that step and the end of the read holds no grant. A read registers nothing once its Agent was disposed, takes its caller's cancellation, and reads each pointer file within a byte cap, since the .git file is Session-writable input.
The grant is the whole Git directory. On Windows the plugin registers nothing, since process confinement ignores additional directories.
Alternatives considered
Grant only objects, refs, logs, and the worktree's administration directory. Under the macOS sandbox this admits a commit, a new branch, a tag, and a stash, and denies deleting a packed branch, git config, git remote add, git pack-refs, and git gc, which lock files at the top of the Git directory.
Exclude hooks and config from the grant. The registration interface accepts directories and the execution policy has no deny list. Adding one changes the shared sandbox types for one consumer, and a Session in the main checkout can already write both.
Read once per directory and keep the result. It saves filesystem round trips in a remote execution world. A Git directory that is removed and restored then stays ungranted, and a result kept across a Host move can describe a layout that changed.
Derive the grant inside the sandbox policy. The policy would carry Git worktree knowledge and an asynchronous derivation behind a synchronous resolve; the workspace-root decision removed both.
Grant only worktrees the Host recorded. The worktree service's creation and entry records name worktrees no pointer file can forge. A Session opened directly in a linked worktree has no such record and would stay unable to commit, and the plugin would read the records of the service that mounts it.
Filter grants by the committed directory inside policy resolution. It removes the window between a committed move and the end of the read without an event listener, and it adds a parameter to the shared registration interface for one consumer.
Mount the plugin from the base composition. Every Session started in a linked worktree would receive the grant, also where no worktree capability is selected. The default file permissions of the product would change without a selected capability that needs it.
Consequences
A Session that works in a linked worktree can write its repository's hooks and config and the administration directories of the repository's other worktrees; a Git command the user runs outside the sandbox executes those hooks. The package README states this.
A Session whose directory is a subdirectory of a worktree, or a submodule checkout, receives no grant. A command that resolved its policy before a move committed keeps the permissions it resolved, and one that resolves it before the read of the entered directory ends holds no grant, so a Git command issued in the same step as enter_worktree can be denied once.
The checks compare the pointer files with what the Session can write now. Files planted while a Session could write more, under danger-full-access or from the main checkout before it entered a worktree, pass them, and the grant then reaches a directory that party could already write at the time, such as a subdirectory of the original project a Session left for a worktree.
The forge panel's New worktree option counts the policy's additional writable directories. In a linked checkout the grant therefore makes the option available at the panel's next inspection, and enter_worktree creates a worktree there.
Required verification: the real-sandbox suites of the plugin and of the worktree service commit from a worktree without a manual registration and show that a .git file naming another worktree's administration directory gains no write access, and that pointer files inside a deployment-granted directory leave its parent unwritable.
中文
问题
Session 的 workspace-write 边界是它工作的目录,沙箱策略不读取任何 Git 布局。链接工作树把自己的索引、HEAD 以及仓库的对象和引用存放在仓库的 Git 目录中,位于工作树之外。因此进入工作树的 Session 无法在 workspace-write(随产品发布的组合的默认模式)下运行 git add 或 git commit,除非对每条命令升权。
从工作树推导授权有伪造风险:Session 可以写入自己目录中的每个文件,包括指明 Git 目录的 .git 文件。
决策
插件 @deepseek-ai/dsh-sandbox-policy-git 通过 ctx.sandboxPolicy.registerWritableRoot 注册 Session 所在工作树的共享 Git 目录。工作树服务挂载它,因此每个能把 Session 移入工作树的组合都带有它。沙箱策略、工作目录服务和沙箱提供方都不变。
只有某个 Git 目录把 Session 的目录指明为工作树时才授权:.git 文件指向 <common>/worktrees/<name>,其中的 commondir 解析为 <common>,gitdir 解析回该 .git 文件;<common> 既不包含 Session 的目录,也不位于其中;并且 <common> 含有 HEAD、objects 和 refs,即 Git 对 Git 目录要求的条目。指明工作树的指针文件位于 <common>/worktrees/<name>,而它可以在 <common> 不可写的情况下可写:部署方或其他插件可能只授权这一个目录。因此,如果管理目录位于 Session 解析出的策略已允许它写入的目录之内,本插件拒绝该布局,除非结果正是本插件已经授权的 Git 目录。没有这条规则时,一个可写的 <X>/worktrees/<name> 会换来对 <X> 的授权,只配置了某个管理目录的授权会被扩大到整个 Git 目录,而存放在名为 worktrees 的目录下的裸仓库会让 Session 借自己的授权取得该目录的上级目录。
布局在 Agent 创建时读取,并在 working-directory/access 流水线的 next() 之后读取;Host 的每次移动以及每次模型请求之前的校验都经过该流水线。两处的调用方都会等待读取完成,因此 resolve 保持同步,授权先于新目录中的第一次操作。每次读取都是完整读取并替换注册,因为已注册的目录一旦不存在,该 Session 的所有受限操作都会失败。授权通过一个前置的 session/event 监听器,在提交“移到另一个目录”的同一步结束,因此不会有策略把新目录与旧目录的 Git 目录组合在一起;从这一步到读取结束之间没有授权。Agent 被销毁之后,读取不再注册任何目录;读取接受调用方的取消,并在字节上限内读取每个指针文件,因为 .git 文件是 Session 可写的输入。
授权对象是整个 Git 目录。在 Windows 上本插件不注册任何目录,因为那里的进程限制忽略额外目录。
考虑过的替代方案
只授权 objects、refs、logs 和该工作树的管理目录。 在 macOS 沙箱下,这允许提交、新建分支、打标签和 stash,但拒绝删除已打包的分支、git config、git remote add、git pack-refs 和 git gc,它们会在 Git 目录的顶层创建锁文件。
把 hooks 和 config 排除在授权之外。 注册接口只接受目录,执行策略也没有拒绝列表。增加一个会为单个使用方修改共享的沙箱类型,而在主检出目录中工作的 Session 本来就能写入这两处。
每个目录只读取一次并保留结果。 这能在远程执行环境中节省文件系统往返。但 Git 目录被删除再恢复后将一直得不到授权,而且跨越 Host 移动保留的结果可能描述的是已经变化的布局。
在沙箱策略内部推导授权。 策略将携带 Git 工作树知识,并在同步的 resolve 背后进行异步推导;工作区根决策已经移除了这两者。
只授权 Host 记录过的工作树。 工作树服务的创建与进入记录所指明的工作树无法用指针文件伪造。但直接在链接工作树中打开的 Session 没有这样的记录,仍然无法提交,而且本插件要读取挂载它的那个服务的记录。
在策略解析内部按已提交的目录过滤授权。 这样不需要事件监听器就能消除从提交移动到读取结束之间的窗口,但要为单个使用方给共享的注册接口增加参数。
由基础组合挂载本插件。 每个在链接工作树中启动的 Session 都会获得授权,包括没有选择任何工作树能力的情况。产品的默认文件权限会在没有所选能力需要它的情况下改变。
影响
在链接工作树中工作的 Session 可以写入其仓库的 hooks 和 config,以及仓库其他工作树的管理目录;用户在沙箱外运行的 Git 命令会执行这些钩子。包的 README 说明了这一点。
目录是工作树的子目录或子模块检出目录的 Session 不会获得授权。在移动提交之前已解析策略的命令保留它解析到的权限,在新目录读取结束之前解析策略的命令没有授权,因此与 enter_worktree 在同一步发出的 Git 命令可能被拒绝一次。
校验把指针文件与 Session 现在能写的目录相比较。在 Session 能写更多目录时预先放好的文件(例如 danger-full-access 下,或进入工作树之前在主检出目录中)可以通过校验,这样得到的授权只会落在该方当时已经能写的目录上,例如 Session 进入工作树之前所在的原始项目的某个子目录。
forge 面板的“新建工作树”选项会计入策略的额外可写目录。因此在链接检出目录中,授权使该选项在面板下一次检查时可用,enter_worktree 也可以在那里创建工作树。
必需的验证:本插件和工作树服务的真实沙箱测试在没有手动注册的情况下从工作树提交,表明指明另一个工作树管理目录的 .git 文件得不到任何写权限,并表明位于部署方已授权目录内的指针文件不会让其上级目录可写。