Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .changeset/strict-containment-answers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
"@schlessera/brain": minor
"@schlessera/brain-ui-sdk": patch
"@schlessera/brain-ui-server": patch
"@schlessera/brain-module-diagrams": patch
"@schlessera/brain-module-travel": patch
"@schlessera/brain-module-images": patch
---

**Breaking: `safeResolve` and `resolveWritable` from `@schlessera/brain/module` now give the strictest containment answer in every row (#3422).** They return `null` for:

- an absolute path, even one inside the brain root;
- any `..` segment, even when the result stays inside;
- `:` or `\` in a segment;
- a dangling symlink, even one whose target is inside;
- a path that leaves the root through one symlink and comes back through another, since every existing component must now stay inside, not only the final path.

A root whose realpath fails was already refused, and still is. The answer is still the canonical path.

The CLI still accepts an absolute path argument inside the brain. Before resolving one, it converts it to a brain-relative path with the new `brainRelativeArgument(root, arg)`, which `@schlessera/brain/module` also exports for module commands. This covers `--out`, document paths and the other CLI path flags. An absolute path outside the brain is still refused, and so is one that reaches the outside through a symlink inside the brain or has a `..` segment.

Migration for module authors: pass paths relative to the brain root. A module command that takes a path argument from the user passes it through `brainRelativeArgument(ctx.root, arg)` first, as the first-party commands do. To re-check an absolute path you already hold, such as an earlier answer from `safeResolve`, pass it relative to the root's realpath (`relative(safeResolve(root, ".")!, path)`). Normalise a link target before resolving it (`join(dirname(path), target)`) instead of sending its `..`. Do not rely on a dangling link to name a file you are about to create.

The ui-sdk's `resolveInRepo` and the server's file `safeResolve` give the same answers. The SDK newly refuses `..`, `:` and `\`, dangling links and out-and-back links. The server newly refuses a missing brain root instead of falling back to the lexical path.

The deny-direction containment checks now treat `/` as holding every absolute path. Before, a `/` source passed the worker launcher's refusal of "the brain or its ancestors" for a transport bind, a managed worktree generation and a reference source view, and the server's sync transport mounted a local remote of `/`. A host credential state directory of `/` was also accepted. All of these are now refused.
5 changes: 4 additions & 1 deletion api-report/core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export "./module" (./src/module.ts)
type WriteFileSafelyOptions
WriteRefusedError
assertScratchWritable
brainRelativeArgument
buildTaxonomy
configurationRoot
geminiCompletions
Expand Down Expand Up @@ -1425,6 +1426,8 @@ signatures (every public export and the types it is made of)
}, z.core.$strict>>>;
}, z.core.$strict>>;
}, z.core.$strict>;
brainRelativeArgument (packages/core/src/lib/safe-path.ts)
export function brainRelativeArgument(root: string, arg: string): string;
buildPathMatcher (packages/core/src/lib/reranker.ts)
export function buildPathMatcher(patterns: readonly string[] | undefined): ((path: string) => boolean) | undefined;
buildTaxonomy (packages/core/src/lib/taxonomy.ts)
Expand Down Expand Up @@ -1578,7 +1581,7 @@ signatures (every public export and the types it is made of)
resolveReranker (packages/core/src/lib/registry.ts)
export function resolveReranker(config?: RerankerSettings): Reranker | undefined;
resolveWritable (packages/core/src/lib/safe-path.ts)
export function resolveWritable(root: string, relOrAbs: string): string | null;
export function resolveWritable(root: string, relPath: string): string | null;
restoreStorageAttachment (packages/core/src/lib/storage-attachments.ts)
export function restoreStorageAttachment(root: string, path: string, expected: StorageAttachmentVersion, copy: (fd: number) => void, assertCurrent?: () => void): void;
rewriteGeneratedRegion (packages/core/src/lib/generated-regions.ts)
Expand Down
4 changes: 2 additions & 2 deletions docs/decisions/container-fresh-proc.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A fresh /proc for agent workers in a container

The agent worker launcher gives every worker its own user and PID namespaces
and a fresh procfs (`workerCommand`, `packages/ui-sdk/src/server/worker-launcher.ts:510-690`).
and a fresh procfs (`workerCommand`, `packages/ui-sdk/src/server/worker-launcher.ts:514-694`).
Under Docker's default masked `/proc` paths the kernel refuses that procfs
mount, so every turn on such a host refuses with
`bwrap: Can't mount proc on /newroot/proc: Operation not permitted` (#1622,
Expand All @@ -22,7 +22,7 @@ does not change.
/proc because the parent /proc has masked or covered paths; in Docker, run
the container with --security-opt systempaths=unconfined", followed by
bubblewrap's own line. Every other launch failure keeps the generic list
(`FRESH_PROC_REFUSED`, `packages/ui-sdk/src/server/worker-launcher.ts:1637`).
(`FRESH_PROC_REFUSED`, `packages/ui-sdk/src/server/worker-launcher.ts:1646`).
The match is on bubblewrap's literal message, not a guess from the
environment: a host that refuses for another reason is not told to change
Docker options.
Expand Down
22 changes: 11 additions & 11 deletions docs/decisions/governed-writable-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ maintenance worker no longer receives the keys the provider relay carries
(`GEMINI_API_KEY`, `ANTHROPIC_API_KEY`, `OPENAI_API_KEY` and keys the operator
lists for it), and the brain's `.env*` files are masked as they are for a
credential-free worker. Its `brain` CLI reaches those providers through its own
relay (`providerRelayFor`, `packages/ui-server/src/brain/host-boundary.ts:421-428`).
relay (`providerRelayFor`, `packages/ui-server/src/brain/host-boundary.ts:422-429`).
What remains is narrower and stated in the status: keys the relay does not
carry (the `jev` reranker's, a `brain sync` agent runner's login) and every
key on a host without managed native storage, where there is no relay.
Expand Down Expand Up @@ -596,7 +596,7 @@ lists the keys each first-party command reads. The launcher withholds every
provider credential in the subprocess descriptor from a governed or strict
turn worker, except what its backend declares for its own inference and what
an operator admits (`const turnProfile`,
`packages/ui-sdk/src/server/worker-launcher.ts:681-691`). A relayed turn keeps no
`packages/ui-sdk/src/server/worker-launcher.ts:684-694`). A relayed turn keeps no
key. An unrelayed one keeps only its runtime's own inference credential, under
the 2026-10-22 ruling that hosts without managed native storage keep today's
inference credentials; pi resolves its own credentials there, so it keeps all
Expand Down Expand Up @@ -1029,7 +1029,7 @@ worker's command starts, the server opens that tmpfs from outside through the
sandbox's `/proc/<pid>/root`. It waits until the path is a tmpfs of exactly
the allowance on another device than the server's own `/tmp`, and keeps the
descriptor
(`function* openPrivateTurnTempSteps`, `packages/ui-sdk/src/server/worker-launcher.ts:1137-1165`).
(`function* openPrivateTurnTempSteps`, `packages/ui-sdk/src/server/worker-launcher.ts:1146-1174`).
Every later read goes through that descriptor, after the worker's namespace
is gone as well, and never enters another mount, so runtime state below
`/tmp` is never read as turn temp. The allowance (256 MiB by default,
Expand Down Expand Up @@ -2208,7 +2208,7 @@ The worker's private `/tmp` and the other host masks remain in place.

After the masks, the launcher re-binds only the explicit runtime inputs the
worker needs, read-only, with the same resolution the restricted envelope uses
(`export function restrictedReadPaths`, `packages/ui-sdk/src/server/worker-launcher.ts:1912`):
(`export function restrictedReadPaths`, `packages/ui-sdk/src/server/worker-launcher.ts:1921`):

- the Bun executable;
- the backend entry's package, its `node_modules` chains and linked workspace
Expand Down Expand Up @@ -2308,9 +2308,9 @@ and subdirectories through retained aliases. It does not restore unrelated
host-temp siblings or make runtime bytes writable.

The concrete checks and mounts are in (`validateRuntimePlacement`,
`packages/ui-sdk/src/server/worker-launcher.ts:610-616`),
(`temporaryRuntimeMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:637-676`)
and (`runtimeMaskOverlays`, `packages/ui-sdk/src/server/worker-launcher.ts:792-810`).
`packages/ui-sdk/src/server/worker-launcher.ts:613-619`),
(`temporaryRuntimeMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:640-679`)
and (`runtimeMaskOverlays`, `packages/ui-sdk/src/server/worker-launcher.ts:801-819`).
Alias restoration recognizes names already supplied by a read-only bind or an
explicit system-view symlink. It does not mount a canonical executable over an
existing alias such as `/bin/sh`; missing owned temporary paths still receive
Expand Down Expand Up @@ -3479,11 +3479,11 @@ An ordinary group-shared directory was accessible, but the worker's private
mount and namespace links through `/proc/<pid>` were denied. Changing the
private directory to `0770` did not remove that process-access barrier. The
current launcher opens mounts directly (`function openPrivateState`,
`packages/ui-sdk/src/server/worker-launcher.ts:1100-1128`;
`packages/ui-sdk/src/server/worker-launcher.ts:1109-1137`;
`function* openPrivateTurnTempSteps`,
`packages/ui-sdk/src/server/worker-launcher.ts:1137-1163`) before population and
`packages/ui-sdk/src/server/worker-launcher.ts:1146-1172`) before population and
start-gate release (`function* launchAgentWorkerSteps`,
`packages/ui-sdk/src/server/worker-launcher.ts:1326-1414`). Group modes and a uid
`packages/ui-sdk/src/server/worker-launcher.ts:1335-1423`). Group modes and a uid
switch alone therefore cannot implement the selected cross-uid lifecycle.

**Authority and launch.** One trusted external supervisor owns each worker
Expand Down Expand Up @@ -3918,7 +3918,7 @@ files, `.claude/`, `.agents/`, `.pi/`, `.mcp.json`, and
- The core CLI loads `brain.config.ts` or `brain.config.json` at the canonical
brain cwd (`loadUserConfig`, `packages/core/src/lib/config.ts:594-628`).
Host commands bind that cwd to the selected brain (`function hostSpawn`,
`packages/ui-server/src/brain/host-boundary.ts:512-523`).
`packages/ui-server/src/brain/host-boundary.ts:513-524`).
- Bun startup uses root `bunfig.toml` and `package.json`; retained literal
preload/module inputs can identify a nested module's nearest package scope.
The inventory walks parent directories only within the originating journal
Expand Down
4 changes: 2 additions & 2 deletions docs/decisions/managed-worktrees.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Each governed or strict turn whose journal is recording opens one worker
generation, and every worker of that turn binds the generation's root-owned
directory whole at `/tmp/brain-worktrees`, inside the turn's own `/tmp`
(`// Managed worktrees (#2430): a journaled turn's workers bind one empty generation.`, `packages/ui-server/src/ws/run-session.ts:570-582`;
`// Managed worktrees (#2430): the whole access generation`, `packages/ui-sdk/src/server/worker-launcher.ts:853-864`).
`// Managed worktrees (#2430): the whole access generation`, `packages/ui-sdk/src/server/worker-launcher.ts:862-873`).
A granted worktree appears there as `/tmp/brain-worktrees/<worktree id>/` on the
worker's next path lookup; the worker cannot create entries beside it, and a
restricted worker never binds a generation. Nothing returns a host path to the
Expand Down Expand Up @@ -212,7 +212,7 @@ nothing.
**Shared source is read through views.** Each such turn's workers bind the
public view and their principal's private view whole and read-only at
`/tmp/brain-reference/public` and `/tmp/brain-reference/private`
(`// Shared reference source (#2432)`, `packages/ui-sdk/src/server/worker-launcher.ts:865-876`).
(`// Shared reference source (#2432)`, `packages/ui-sdk/src/server/worker-launcher.ts:874-885`).
A view the domain has not published to yet is opened empty first, so the turn
sees its own first acquisition
(`async bindable(principalId: string`, `packages/ui-server/src/brain/reference-source.ts:741-753`).
Expand Down
4 changes: 2 additions & 2 deletions docs/decisions/policy-write-boundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,13 @@ sessions.
before the runtime starts and read it after the worker is gone, while the
tmpfs is reachable only inside the worker. The launcher does both from the
host (`function* launchAgentWorkerSteps`,
`packages/ui-sdk/src/server/worker-launcher.ts:1326-1581`):
`packages/ui-sdk/src/server/worker-launcher.ts:1335-1590`):

1. It finds the sandbox's first process from the kernel's record of
bubblewrap's only child, then opens the tmpfs root through
`/proc/<pid>/root` and keeps that directory descriptor
(`function openPrivateState`,
`packages/ui-sdk/src/server/worker-launcher.ts:1100-1124`). bubblewrap's
`packages/ui-sdk/src/server/worker-launcher.ts:1109-1133`). bubblewrap's
process exists before its mounts do, so the open is accepted only once the
path is its own mount (a device other than its parent's), a tmpfs of
exactly the allowance, and empty; until then it would name the host's
Expand Down
10 changes: 5 additions & 5 deletions docs/decisions/worker-writable-areas.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ on the transcript persist path and is not part of this record.

**One resolution, two consumers.** `prepareWorkerSandbox` resolves the
areas and per-turn XDG layout once per worker
(`export function prepareWorkerSandbox`, `packages/ui-sdk/src/server/worker-launcher.ts:902-943`).
(`export function prepareWorkerSandbox`, `packages/ui-sdk/src/server/worker-launcher.ts:911-952`).
The backend passes the result both to the launcher, which turns it into
bubblewrap arguments, and to `workerSandboxNote`, which renders the agent's
note (`export function workerSandboxNote`, `packages/ui-sdk/src/server/worker-sandbox.ts:592-657`).
The launcher re-checks the resolved layout against the actual launch: the
same brain and envelope, each writable area still a real directory at its
own path in the brain. The backend selects runtime HOME separately.
A mismatch refuses the launch instead of binding something the note does not
describe (`function sandboxMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:824-854`).
describe (`function sandboxMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:833-863`).
A launch without a resolved layout keeps the 0.41 view: only the per-turn
scratch is writable.

Expand Down Expand Up @@ -153,7 +153,7 @@ through the roadblock path. This is a worker write boundary, not an immutable
view: host-side changes to that directory remain visible. A backend may select a different runtime HOME
as described below; it does not make the host home writable. `prepareWorkerSandbox` resolves this same XDG mode on every eligible
host (`export function prepareWorkerSandbox`,
`packages/ui-sdk/src/server/worker-launcher.ts:902-943`). Host-backed overlay
`packages/ui-sdk/src/server/worker-launcher.ts:911-952`). Host-backed overlay
uppers cannot live on the worker's private tmpfs; keeping them would leave
an unbounded write area. Tools reading XDG config see the per-turn directory,
while direct reads of real HOME retain the read-only host view.
Expand Down Expand Up @@ -267,14 +267,14 @@ The `read_only_brain` code and its

**Serving.** The file routes refuse any symlink at or below `.brain` for a
scratch path, and a scratch listing omits links
(`const policy: ContainmentPolicy = isScratchPath(rel)`, `packages/ui-server/src/files/walker.ts:97-99`).
(`const policy: ContainmentPolicy = isScratchPath(rel)`, `packages/ui-server/src/files/walker.ts:96-98`).
Elsewhere in the brain an in-brain link resolves as before. Those checks
run before the open, so a file whose requested or resolved path lies in
`.brain/scratch/` or `workspaces/` is then read once, from its resolved
path, through descriptors that never follow a link (each directory from the brain root opened with
`O_NOFOLLOW | O_DIRECTORY` through the previous one, the file with
`O_NOFOLLOW`), and the route serves that snapshot; the size cap bounds it
(`export function readAnchoredSnapshot`, `packages/ui-server/src/files/walker.ts:116-152`).
(`export function readAnchoredSnapshot`, `packages/ui-server/src/files/walker.ts:115-151`).
Core's prune reads every entry with `lstat`, and on Linux removes each file
and emptied directory through its parent's descriptor in the same way
(`function anchored`, `packages/core/src/lib/scratch.ts:517-547`), because a
Expand Down
2 changes: 1 addition & 1 deletion docs/hosting/agent-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ owner-only `0700`/`0600` modes. Before mounting the proxy, the launcher checks
canonical paths, server ownership, the expected helper group, exact modes and
socket type. Missing entries, symlinks and widened permissions refuse before
the payload starts (`if (input.egress)`,
`packages/ui-sdk/src/server/worker-launcher.ts:553-568`). This does not select
`packages/ui-sdk/src/server/worker-launcher.ts:556-571`). This does not select
the ordinary inference route or enable the proxy by default.

Native output stages use the existing manager's active stage identity. The
Expand Down
2 changes: 1 addition & 1 deletion docs/hosting/paid-cli-provider-qualification.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ SDK conversion without adding a public embedding-provider seam.

The image CLI preserves `bytes`, `costIsEstimate`, `costUsd`, `model`, `note`,
`output`, `outputs`, `provider`, `reason` and `renamed`
(`const payload`, `packages/module-images/src/cli.ts:407-419`). Its help example
(`const payload`, `packages/module-images/src/cli.ts:408-420`). Its help example
is abbreviated; it is not the complete machine contract. Internal image raw
usage does not imply that this envelope exposes it.

Expand Down
21 changes: 21 additions & 0 deletions docs/integration-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ same-commit update of the index or relevant authoritative component. How they ar

The reasoning is in [decisions/contract-versioning.md](decisions/contract-versioning.md).

## Strictest path containment answers (breaking before 1.0, 0.45.0, #3422)

`safeResolve` and `resolveWritable` from `@schlessera/brain/module` now return
`null` in these cases:

- an absolute path, even inside the root;
- any `..` segment;
- `:` or `\` in a segment;
- a dangling symlink;
- a path any existing component of which leaves the root.

The CLI is not affected for paths inside the brain. Before calling them, it
converts an absolute path argument inside the brain (`--out`, document paths
and the other CLI path flags) to a brain-relative path, through the additive
`brainRelativeArgument` export. An absolute argument outside the brain is still
refused, and so is one through a symlink that leaves it. The maintainer rulings
are recorded [on the issue](https://github.com/schlessera/brain-kit/issues/3422#issuecomment-6599244174)
and, for the CLI, [on the PR](https://github.com/schlessera/brain-kit/pull/3465#issuecomment-6602766385). See [the package API component](integration-contract/package-api.md#strictest-path-containment-answers-breaking-before-10-0450-3422).
This ships in the 0.45.0 minor with the `breaking` label and a changeset that
names the break.

## Explicit local chat deletion (additive, #3230)

An authenticated owner can delete its own chat's local history with
Expand Down
Loading
Loading