refactor(sandbox-policy): resolve policies only for Sessions; callers build maintenance policies

Review of the consumers layer:

- `SandboxPolicyRequest.root` is removed. `resolve` answers only for a Session.
  `prepare` accepts a Session or a checkout directory and returns that
  checkout's validated Git administration directories. The worktree service
  builds the policy of removal and restoration itself: the Session's mode and
  grants, the source checkout as the root, and that checkout's derived
  directories. The forge panel swaps the root for its two reads of the source
  repository.
- The Git layout reads move to `src/linked-checkout.ts` with direct tests; the
  security rationale stays in the README, which the function comment links.
- `resolve` and the model-visible policy share one merge of configured,
  registered, and derived writable roots.
- `workspace-files` takes a live Session's root from the sandbox policy instead
  of repeating its fallback chain.
- `TurnRecorder` takes a fixed directory and an explicit `moveTo`, which the
  plugin calls on `workspace-root/change`; each turn keeps its own canonical
  paths.

A real-sandbox test restores a missing worktree of a linked source checkout in
a process that never worked in it.
This commit is contained in:
LegGasai committed 2026-11-17 15:09:22 +08:00
1 parent 0ae2613c98
commit e3cd2edebf
33 files changed
+373 -256

No files matched your search

+18 -29
View File
@@ -81,13 +81,6 @@ interface SandboxPolicyRequest {
session?: Session
/** Explicit approved mode override, which outranks session policy. */
mode?: SandboxMode
/**
* Checkout that bounds this call instead of the session's workspace root: Host maintenance of a
* checkout the session does not work in, such as removing or restoring the session's worktree from
* its source repository while the session's own root is that worktree. The session's mode and
* registered grants still apply. Never reachable from model input.
*/
root?: string
}
```
@@ -220,12 +213,11 @@ A session's workspace root comes from the working-directory service when it is m
* boundary, and the Git administration directories {@link prepare} derived
* for that root join the configured extra writable directories; the
* configured root is the fallback for agentless calls and, without the
* working-directory service, sessions without a header directory, and a
* requested `root` replaces the session's root for the call. Configured extra
* roots, this Session's active registrations, and the derived Git directories
* are snapshotted together; existing operations and processes keep their
* policy.
* @param request - optional session, approved mode override, and root override.
* working-directory service, sessions without a header directory. Configured
* extra roots, this Session's active registrations, and the derived Git
* directories are snapshotted together; existing operations and processes
* keep their policy.
* @param request - optional session and approved mode override.
* @returns the fully resolved per-call mode, absolute workspace root, and extra writable directories.
* @throws `SandboxInvalidRootError` (`SANDBOX_INVALID_ROOT`) when the selected root is relative or
* contains a NUL byte.
@@ -254,24 +246,21 @@ registerWritableRoot(session: Session, directory: string): () => void
overrideOf(session: Session): SandboxMode | undefined
/**
* Derive the Git administration directories of the session's workspace root
* so the next {@link resolve} grants them: a linked checkout's `.git` file
* names `<common>/worktrees/<name>`, whose `commondir` names the repository's
* common directory and whose `gitdir` names the checkout's `.git` again; only
* that layout grants the common directory, since the checkout's own `.git`
* pointer is writable under workspace-write. Any other layout, a root without
* `.git`, and a common directory containing the root add nothing. One
* derivation runs per root and its result is kept once the layout was read,
* so a pointer written later does not widen the policy until a Host operation
* moves a Session's workspace to that root again; a derived directory that disappears is
* derived again, and a failed read is logged once per root and retried on
* the next call.
* @param request - session whose workspace root is inspected, or the root {@link resolve} would select
* for the same request.
* Derive the Git administration directories a linked checkout writes outside itself, as
* `./linked-checkout.ts` reads them. For a Session the checkout is its workspace root, and the next
* {@link resolve} for that Session grants the result. For a named `directory`, a Host operation that
* runs in a checkout no Session works in adds the result to the policy it builds; `resolve` is
* unaffected. One derivation runs per directory and its result is kept once the layout was read, so a
* pointer written later does not widen a policy until a Host operation moves a Session's workspace to
* that directory again; a derived directory that disappears is derived again, and a failed read is
* logged once per directory and retried on the next call.
* @param target - Session whose workspace root is inspected, or the checkout directory itself.
* @param signal - caller cancellation, checked before and after the shared derivation.
* @returns the derived directories outside the root, canonical in the execution world.
* @returns the derived directories outside the checkout, canonical in the execution world.
* @throws `SandboxInvalidRootError` (`SANDBOX_INVALID_ROOT`) when the directory is relative or
* contains a NUL byte.
*/
async prepare(request: Pick<SandboxPolicyRequest, 'session' | 'root'>, signal?: AbortSignal): Promise<readonly string[]>
async prepare(target: { session: Session } | { directory: string }, signal?: AbortSignal): Promise<readonly string[]>
```
Types: [Session](session.md)
+18 -29
View File
@@ -81,13 +81,6 @@ interface SandboxPolicyRequest {
session?: Session
/** Explicit approved mode override, which outranks session policy. */
mode?: SandboxMode
/**
* Checkout that bounds this call instead of the session's workspace root: Host maintenance of a
* checkout the session does not work in, such as removing or restoring the session's worktree from
* its source repository while the session's own root is that worktree. The session's mode and
* registered grants still apply. Never reachable from model input.
*/
root?: string
}
```
@@ -220,12 +213,11 @@ A session's workspace root comes from the working-directory service when it is m
* boundary, and the Git administration directories {@link prepare} derived
* for that root join the configured extra writable directories; the
* configured root is the fallback for agentless calls and, without the
* working-directory service, sessions without a header directory, and a
* requested `root` replaces the session's root for the call. Configured extra
* roots, this Session's active registrations, and the derived Git directories
* are snapshotted together; existing operations and processes keep their
* policy.
* @param request - optional session, approved mode override, and root override.
* working-directory service, sessions without a header directory. Configured
* extra roots, this Session's active registrations, and the derived Git
* directories are snapshotted together; existing operations and processes
* keep their policy.
* @param request - optional session and approved mode override.
* @returns the fully resolved per-call mode, absolute workspace root, and extra writable directories.
* @throws `SandboxInvalidRootError` (`SANDBOX_INVALID_ROOT`) when the selected root is relative or
* contains a NUL byte.
@@ -254,24 +246,21 @@ registerWritableRoot(session: Session, directory: string): () => void
overrideOf(session: Session): SandboxMode | undefined
/**
* Derive the Git administration directories of the session's workspace root
* so the next {@link resolve} grants them: a linked checkout's `.git` file
* names `<common>/worktrees/<name>`, whose `commondir` names the repository's
* common directory and whose `gitdir` names the checkout's `.git` again; only
* that layout grants the common directory, since the checkout's own `.git`
* pointer is writable under workspace-write. Any other layout, a root without
* `.git`, and a common directory containing the root add nothing. One
* derivation runs per root and its result is kept once the layout was read,
* so a pointer written later does not widen the policy until a Host operation
* moves a Session's workspace to that root again; a derived directory that disappears is
* derived again, and a failed read is logged once per root and retried on
* the next call.
* @param request - session whose workspace root is inspected, or the root {@link resolve} would select
* for the same request.
* Derive the Git administration directories a linked checkout writes outside itself, as
* `./linked-checkout.ts` reads them. For a Session the checkout is its workspace root, and the next
* {@link resolve} for that Session grants the result. For a named `directory`, a Host operation that
* runs in a checkout no Session works in adds the result to the policy it builds; `resolve` is
* unaffected. One derivation runs per directory and its result is kept once the layout was read, so a
* pointer written later does not widen a policy until a Host operation moves a Session's workspace to
* that directory again; a derived directory that disappears is derived again, and a failed read is
* logged once per directory and retried on the next call.
* @param target - Session whose workspace root is inspected, or the checkout directory itself.
* @param signal - caller cancellation, checked before and after the shared derivation.
* @returns the derived directories outside the root, canonical in the execution world.
* @returns the derived directories outside the checkout, canonical in the execution world.
* @throws `SandboxInvalidRootError` (`SANDBOX_INVALID_ROOT`) when the directory is relative or
* contains a NUL byte.
*/
async prepare(request: Pick<SandboxPolicyRequest, 'session' | 'root'>, signal?: AbortSignal): Promise<readonly string[]>
async prepare(target: { session: Session } | { directory: string }, signal?: AbortSignal): Promise<readonly string[]>
```
Types: [Session](session.zh.md)
+1 -1
View File
@@ -455,7 +455,7 @@ Host Remote file reads and workspace directory observations over the composed fi
```ts cordis-catalog
/**
* Resolve the scope other Host services use to open the same `session/<id>/<path>` addresses:
* a live Session's directory, else a stored Session's cached directory, else its header directory.
* a live Session's sandbox workspace root, else a stored Session's cached directory, else its header directory.
* @param sessionId - Session identity received on the wire.
* @param signal - cancellation for the persistence read of a stored Session.
* @returns the scope, or undefined when no live or stored Session has that identity.
+1 -1
View File
@@ -455,7 +455,7 @@ Host Remote file reads and workspace directory observations over the composed fi
```ts cordis-catalog
/**
* Resolve the scope other Host services use to open the same `session/<id>/<path>` addresses:
* a live Session's directory, else a stored Session's cached directory, else its header directory.
* a live Session's sandbox workspace root, else a stored Session's cached directory, else its header directory.
* @param sessionId - Session identity received on the wire.
* @param signal - cancellation for the persistence read of a stored Session.
* @returns the scope, or undefined when no live or stored Session has that identity.