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

@@ -43,16 +43,18 @@ describe('Workspace Files Session scope lookup', () => {
meta.id === coldId ? { asOfSeq: 4, values: { workingDirectory: coldWorktree } } : undefined)
const ctx = new Context()
ctx.provide('fs', {} as never)
ctx.provide('sandboxPolicy', { workspaceRoot: resolve('fallback-workspace') } as never)
// A live Session's root is the one the sandbox policy bounds it by.
const resolvePolicy = vi.fn(() => ({ mode: 'workspace-write', workspaceRoot: liveWorktree }))
ctx.provide('sandboxPolicy', { workspaceRoot: resolve('fallback-workspace'), resolve: resolvePolicy } as never)
ctx.provide('sessionPersistence', { stat } as never)
ctx.provide('sessionProjectionCache', { cachedSnapshot } as never)
ctx.provide('workingDirectory', { get: () => liveWorktree } as never)
const sessions = await ctx.plugin(SessionStore)
const typert = await ctx.plugin(TypertRegistry)
const workspaceFiles = await ctx.plugin(WorkspaceFiles, CAPS)
try {
ctx.sessions.create(liveId, { meta: { cwd: liveRoot, origin: 'subagent' } })
const live = ctx.sessions.create(liveId, { meta: { cwd: liveRoot, origin: 'subagent' } })
await expect(ctx.workspaceFiles.scopeOf(liveId)).resolves.toEqual({ sessionId: liveId, workspaceRoot: liveWorktree })
expect(resolvePolicy).toHaveBeenCalledWith({ session: live })
await expect(ctx.workspaceFiles.scopeOf(coldId)).resolves.toEqual({ sessionId: coldId, workspaceRoot: coldWorktree })
await expect(ctx.workspaceFiles.scopeOf(plainId)).resolves.toEqual({ sessionId: plainId, workspaceRoot: coldRoot })
expect(cachedSnapshot).toHaveBeenCalledWith(expect.objectContaining({ id: coldId }), ['workingDirectory'])
@@ -82,7 +84,8 @@ describe('Workspace Files Session scope lookup', () => {
})
const ctx = new Context()
ctx.provide('fs', {} as never)
ctx.provide('sandboxPolicy', { workspaceRoot: fallbackRoot } as never)
const resolvePolicy = ({ session }: { session: { header: SessionHeader } }) => ({ mode: 'read-only', workspaceRoot: session.header.cwd ?? fallbackRoot })
ctx.provide('sandboxPolicy', { workspaceRoot: fallbackRoot, resolve: resolvePolicy } as never)
ctx.provide('sessionPersistence', { stat } as never)
const sessions = await ctx.plugin(SessionStore)
const typert = await ctx.plugin(TypertRegistry)