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
8 changes: 8 additions & 0 deletions .changeset/safe-path-once.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@schlessera/brain-common": patch
"@schlessera/brain": patch
"@schlessera/brain-ui-sdk": patch
"@schlessera/brain-ui-server": patch
---

Symlink-safe path containment now has one implementation, in the new internal entry `@schlessera/brain-common/internal/safe-path` (#2506). It holds `isWithin`, the containment primitive whose equality, separator, trailing-separator and normalisation answers are explicit parameters, and one canonicalisation algorithm with a synchronous and an asynchronous variant. core's `safeResolve`, the SDK's `resolveInRepo` and the server's `safeResolve` are thin wrappers over it, and the scratch-directory copies read the shared `BRAIN_SCRATCH_DIR`. Each caller keeps its own answers, with two exceptions: the server's `safeResolve` now refuses a path through a symlink cycle, as core and the SDK already did, instead of answering a path that resolves nowhere; and the server's check that core's query entry lies inside its package no longer refuses a directory whose name starts with `..`. `safeResolve` and `SCRATCH_DIR` stay exported from `@schlessera/brain/module` with their signatures and answers unchanged. The new entry is internal and carries no compatibility promise.
9 changes: 9 additions & 0 deletions api-report/common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ export "./internal/provider-unavailable" (./src/provider-unavailable.ts)
providerUnavailableNotice
withheldKeys

export "./internal/safe-path" (./src/safe-path.ts)
type ContainedPath
type ContainmentPolicy
type ContainmentRefusal
type WithinRule
isWithin
resolveContained
resolveContainedSync

export "./internal/worker-areas" (./src/worker-areas.ts)
BRAIN_SCRATCH_DIR
BRAIN_WORKSPACES_DIR
Expand Down
6 changes: 3 additions & 3 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:509-689`).
and a fresh procfs (`workerCommand`, `packages/ui-sdk/src/server/worker-launcher.ts:510-690`).
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:1635`).
(`FRESH_PROC_REFUSED`, `packages/ui-sdk/src/server/worker-launcher.ts:1636`).
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 Expand Up @@ -145,7 +145,7 @@ most host-global `/proc` files check only the file mode, not a capability.
**Ruling on #1629 (2026-10-11): a root-run server's worker gets its whole
`/proc` read-only. Root servers still qualify.** `workerCommand` adds
`--remount-ro /proc` after `--proc /proc` when the server's uid or effective
uid is 0 (`rootProcCover`, `packages/ui-sdk/src/server/worker-launcher.ts:334-336`).
uid is 0 (`rootProcCover`, `packages/ui-sdk/src/server/worker-launcher.ts:335-337`).
Measured as container root with `systempaths=unconfined` and the seccomp
profile above, with bubblewrap `0.9.0` from the image and the host's `0.12.0`,
each in the ordinary and the restricted mode:
Expand Down
2 changes: 1 addition & 1 deletion docs/decisions/design-kit/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ found, and what the ruling did not say:
(`LINK_BLOCK_SCHEMA`, `packages/ui-sdk/src/tool-contracts/blocks.ts:429-449`)
mirrors the props. `classifyLink` (`classifyLink`, `packages/render-template/src/links.ts:252-311`)
is pure. The edge table records the new dependency
(`"@schlessera/brain-ui-sdk"`, `tests/allowed-edges.ts:75`), and ui-kit now
(`"@schlessera/brain-ui-sdk"`, `tests/allowed-edges.ts:76`), and ui-kit now
builds and publishes ahead of ui-sdk. At that point the kit's own row was unchanged.
#558 later moved the pure classifier to the template's `./links` leaf and
added the kit-to-template edge; D13's purity gate still holds.
Expand Down
42 changes: 21 additions & 21 deletions docs/decisions/governed-writable-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,11 @@ before application-lock admission, acquires its own complete native checkpoint
asynchronously and runs a credential-free, read-only Git query. It releases
that input after the worker drains, without creating or publishing a writable
stage (`createScratchIgnoreQuery`,
`packages/ui-server/src/brain/scratch-mask-query.ts:7-43`). Exit 1 means the
`packages/ui-server/src/brain/scratch-mask-query.ts:8-44`). Exit 1 means the
directory is not ignored. Launch failure, another exit or timeout means the
rule could not be verified; the mask remains refused without forwarding worker
stderr or claiming that the ignore file needs repair (`assertScratchMaskAsync`,
`packages/ui-sdk/src/server/bridge-tools/mask.ts:224-235`).
`packages/ui-sdk/src/server/bridge-tools/mask.ts:228-239`).

**Credentials never reach a process that evaluates repository inputs.** A
read-only host does not stop sandboxed code from stealing a credential it was
Expand Down 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:420-427`).
relay (`providerRelayFor`, `packages/ui-server/src/brain/host-boundary.ts:421-428`).
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:680-690`). A relayed turn keeps no
`packages/ui-sdk/src/server/worker-launcher.ts:681-691`). 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 @@ -1027,22 +1027,22 @@ 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:1136-1164`).
(`function* openPrivateTurnTempSteps`, `packages/ui-sdk/src/server/worker-launcher.ts:1137-1165`).
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,
`BRAIN_UI_TURN_TEMP_BYTES`) is admitted in the same ledger as runtime state,
against its own budget, and a turn that cannot reserve it is refused as
`worker_state_busy` (docs/hosting/agent-workers.md). The `turn_temp` tool
reports usage and purges paths through the same descriptor
(`export function purgeTurnTemp`, `packages/ui-sdk/src/server/worker-turn-temp.ts:450-458`).
(`export function purgeTurnTemp`, `packages/ui-sdk/src/server/worker-turn-temp.ts:451-459`).
The restricted envelope gets the same sized `/tmp` and promotes nothing.
After the worker exits, the backend
rewrites the answer text the turn appended to its saved transcript, and only
that text. It looks at Markdown link and image destinations and reference
definitions outside code. A file is promoted once per turn however often it
is linked, up to 128 MiB per file, 256 MiB and 64 files per turn
(`export function createTurnTempPromoter`, `packages/ui-sdk/src/server/worker-turn-temp.ts:124-135`).
(`export function createTurnTempPromoter`, `packages/ui-sdk/src/server/worker-turn-temp.ts:125-136`).
The copy goes to `.brain/scratch/turns/<turnId>/` under a name the reader's
file links accept, so the rewritten link is brain-relative and is pinned like
any other scratch link. Each result reaches the server as a `promotion` or
Expand All @@ -1065,10 +1065,10 @@ Live blocks still use streamed-text offsets; the client and server share
the span rebasing function and discard ambiguous overlaps as plain Markdown.

**Pinning.** A link into `.brain/scratch/` pins the target. Prune skips pinned
files (`export function pruneScratch`, `packages/core/src/lib/scratch.ts:641`).
files (`export function pruneScratch`, `packages/core/src/lib/scratch.ts:645`).
A pin is released when every conversation that links the file is deleted or
falls out of retention. If the scratch area still exceeds its size limit
(`SCRATCH_MAX_BYTES`, `scratch.ts:58`) after unpinned files are pruned, the
(`SCRATCH_MAX_BYTES`, `scratch.ts:62`) after unpinned files are pruned, the
overage is reported. Pinned files are never deleted. A writer that prunes
after writing into scratch also passes the paths it just produced, protected
for that one call (`pinnedPaths`), so retained pins over the cap cannot make it
Expand Down Expand Up @@ -1352,7 +1352,7 @@ external editor throughout a native tool's execution.
The parent observes the worker's resolved mount sources, including strict
startup copies and writable area aliases; it does not substitute newer
canonical bytes for a frozen view (`createWorkerFileObserver`,
`packages/ui-sdk/src/server/worker-file-observer.ts:13`). A failed or changing
`packages/ui-sdk/src/server/worker-file-observer.ts:15`). A failed or changing
read cannot establish a new version. Claude's native `file_unchanged` response
also preserves the previous observation: it supplied no new bytes
(`const finishNativeFile`,
Expand Down Expand Up @@ -2186,7 +2186,7 @@ a filesystem socket that is visible through the read-only host root: a Docker
socket, a session bus, an SSH agent or a host proxy. Each of those is an
unrecorded way out, and some reach privileged host effects. The launcher
already masks `.git` in restricted workers for exactly this reason
(`function maskedGit`, `packages/ui-sdk/src/server/worker-launcher.ts:226`).
(`function maskedGit`, `packages/ui-sdk/src/server/worker-launcher.ts:227`).

`governed` and `strict` workers therefore mask the host's runtime and state
directories with empty read-only tmpfs mounts. Maintenance workers share the
Expand All @@ -2204,7 +2204,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:1910`):
(`export function restrictedReadPaths`, `packages/ui-sdk/src/server/worker-launcher.ts:1911`):

- the Bun executable;
- the backend entry's package, its `node_modules` chains and linked workspace
Expand All @@ -2228,7 +2228,7 @@ such ancestor with empty tmpfs instead; it never binds that ancestor's real
contents. Search permission uses the helper's primary shared group class when
that group owns the directory, including when only the other class has execute
permission. Existing canonical aliases and nested masks fold into that cover
(`maintenanceMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:385-444`).
(`maintenanceMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:386-445`).

This supports disjoint private-state layouts, including a nested operational
database below a server-owned `0700` parent. New ancestor coverage that would
Expand Down Expand Up @@ -2304,9 +2304,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:609-615`),
(`temporaryRuntimeMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:636-675`)
and (`runtimeMaskOverlays`, `packages/ui-sdk/src/server/worker-launcher.ts:791-809`).
`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`).
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 @@ -3337,7 +3337,7 @@ before mounting them: both belong to the server, helper mode uses its primary
GID and exact `0750`/`0660` modes, and no-helper mode retains exact
`0700`/`0600`. Missing endpoints, aliases, non-sockets, foreign ownership and
wider modes refuse before payload execution
(`function restrictedMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:250-284`).
(`function restrictedMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:251-285`).
This compatibility check does not change default autonomous admission or add
another route.

Expand Down Expand Up @@ -3475,11 +3475,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:1099-1127`;
`packages/ui-sdk/src/server/worker-launcher.ts:1100-1128`;
`function* openPrivateTurnTempSteps`,
`packages/ui-sdk/src/server/worker-launcher.ts:1136-1162`) before population and
`packages/ui-sdk/src/server/worker-launcher.ts:1137-1163`) before population and
start-gate release (`function* launchAgentWorkerSteps`,
`packages/ui-sdk/src/server/worker-launcher.ts:1325-1413`). Group modes and a uid
`packages/ui-sdk/src/server/worker-launcher.ts:1326-1414`). 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 @@ -3914,7 +3914,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:511-522`).
`packages/ui-server/src/brain/host-boundary.ts:512-523`).
- 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
2 changes: 1 addition & 1 deletion docs/decisions/index-query-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ neither justified a service or configurable query-provider seam.

> **2026-10-05 — Implementation context (#697).** UI-server now declares the
> optional peer and resolves the entry lazily, once per app
> (`createCoreQueryAccess`, `packages/ui-server/src/core-queries.ts:192-227`). The
> (`createCoreQueryAccess`, `packages/ui-server/src/core-queries.ts:193-228`). The
> source-backed range is `>=0.40.0 <1.0.0`: the published 0.40.0 manifest is the
> first to export `./queries`, its declarations name all nine operations and its
> query source matches the source the server was migrated against; 0.39.0 has no
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:852-863`).
`// Managed worktrees (#2430): the whole access generation`, `packages/ui-sdk/src/server/worker-launcher.ts:853-864`).
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:864-875`).
(`// Shared reference source (#2432)`, `packages/ui-sdk/src/server/worker-launcher.ts:865-876`).
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
6 changes: 3 additions & 3 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:1325-1580`):
`packages/ui-sdk/src/server/worker-launcher.ts:1326-1581`):

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:1099-1123`). bubblewrap's
`packages/ui-sdk/src/server/worker-launcher.ts:1100-1124`). 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 All @@ -513,7 +513,7 @@ host (`function* launchAgentWorkerSteps`,
code: the absolute `/bin/sh` reads one line from the worker's stdin with
its `read` builtin, which only the server writes, and then becomes the
command; closed stdin or any other line ends the worker unstarted
(`const STATE_GATE`, `packages/ui-sdk/src/server/worker-launcher.ts:56`).
(`const STATE_GATE`, `packages/ui-sdk/src/server/worker-launcher.ts:57`).
The gate runs no `PATH` lookup, so nothing in the worker's environment or
the brain can stand in for it, and the shell reads a pipe a byte at a time,
so the runtime's own input is untouched. The server copies the snapshot in
Expand Down
8 changes: 5 additions & 3 deletions docs/decisions/public-export-boundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,17 @@ its watch implementation and CLI remain implementation details.
An internal-only package (#1396): `@schlessera/brain-common` has no root
entry. Its entries, `./internal/env`, `./internal/frontmatter` and
`./internal/worker-areas`, `./internal/protected-paths`,
`./internal/governance-policy` and `./internal/claude-subscription`, follow
the internal-entry rule above.
`./internal/governance-policy`, `./internal/claude-subscription` and
`./internal/safe-path`, follow the internal-entry rule above.
The policy helper (#1744) serves both core and the server without requiring
the server’s optional core peer; it adds no supported extension interface. The first
two replace byte-identical copies of `env-core.ts` and `frontmatter-parse.ts`
that the packages carried; the third (#1693) holds the worker write-area rules
that the ui-sdk's sandbox note and core's CLI refusal both state. The
subscription billing guard (#2493) replaces the copies core's CLI runner and
the chat backend carried. A name a
the chat backend carried. Symlink-safe containment (#2506) replaces the
resolvers core, the ui-sdk and ui-server carried and their hand-written
containment predicates. A name a
public entry re-exports from it, such as core's `DynamicEnvReadSpec` and
`readEnvVar`, stays part of that package's supported surface and is recorded
in that package's report.
Expand Down
Loading
Loading