diff --git a/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.i18n.yaml b/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.i18n.yaml index 57dfd6183a..e2f91fe73b 100644 --- a/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.i18n.yaml +++ b/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write .agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.md -2026-11-23-cloud-joins-workspace-sync.md: 7445d185500d5eb677764c81bef04a3add708644 -2026-11-23-cloud-joins-workspace-sync.zh.md: 20c1fb742a0c2e5b2b0208037a403aa93c567ee3 +2026-11-23-cloud-joins-workspace-sync.md: 767d5e41742ac7f1ecdb6c69887dbba1dd0b07f1 +2026-11-23-cloud-joins-workspace-sync.zh.md: eaad5ae4a9307d397ed90775813fe83e40294656 diff --git a/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.md b/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.md index 7445d18550..767d5e4174 100644 --- a/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.md +++ b/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.md @@ -13,7 +13,7 @@ A cloud session filed in one of the account's project workspaces worked in the h The hosted Host is a device of each account's folder and repository workspaces, in a managed mode both Consumers take from a new `managedRoot` setting (`avnHomePath('sandbox', 'accounts')` in the hosted bundle, with `AVN_SYNCTHING` and `AVN_HOSTED_SANDBOX=1`): - **Placement.** A workspace arrives only once a session of it runs a turn on the Host, in `//.aeven-projects/` (`placeManagedWorkspace` in `@aeven/workspace`, numbered when the name holds other files). Existing copies are never searched. A repository's turn waits for its clone or copy; a folder's reads wait for the files they need, as on any device. A session that recorded the placeholder directory before its workspace arrived then works in the bound directory: on a Host without folders of its own, `working-directory` prefers a bound workspace's directory to a recorded one outside it, or to none. -- **Git in the jail.** `workspace-git-sync`'s Git runner takes a `confine` hook, which the managed mode builds on the sandbox capability: every Git command runs in the account's jail with the work tree as its root and the one transfer folder it uses as the only other root, its environment passed through `env`, `safe.directory=*` because the jail's user owns neither the directories the Host created nor the work tree, and the remote's HTTPS form, the one form the egress proxy carries. +- **Git in the jail.** `workspace-git-sync`'s Git runner takes a `confine` hook, which the managed mode builds on the sandbox capability: every Git command runs in the account's jail with the work tree as its root and the one transfer folder it uses as the only other root, its environment passed through `env`, `safe.directory=*` because the jail's user owns neither the directories the Host created nor the work tree (the jail sets it for every confined command, so the agent's own `git` works in the clone too), and the remote's HTTPS form, the one form the egress proxy carries. - **No Host file access inside a work tree.** Copies into or out of a work tree run as confined `cp` commands: a checkpoint's untracked files, a received checkpoint's untracked files, and environment files, which the Host first writes into a staging folder no jail has mounted. A checkpoint folder moves into place with one rename instead of a recursive copy, and its description is created exclusively. Transfer folders live in the account's `.aeven-projects/.transfer`, which no session's jail mounts. The Host publishes no environment files, shows no conflict text, and resolves no conflicts, since each would read or rename files an account's commands can write. - **Every device.** Untracked files that are symbolic links no longer travel in checkpoints, and a received environment file whose path has an empty, `.`, `..`, or `.git` segment is skipped. A checkpoint records whether it carries work. A device's own checkpoint without work, which a pull or a commit there writes, no longer outranks another device's work, and receiving one only fast-forwards a clean work tree; a device whose branch is ahead of a received checkpoint applies its changes and files on top instead of skipping it; and a rewrite of unchanged work after a restart keeps its time. Before this, a pull on a desktop after a cloud turn masked the cloud's uncommitted work, and a cloud clone behind the desktops had its work skipped. diff --git a/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.zh.md b/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.zh.md index 20c1fb742a..eaad5ae4a9 100644 --- a/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.zh.md +++ b/.agents/notes/implemented/architecture/2026-11-23-cloud-joins-workspace-sync.zh.md @@ -13,7 +13,7 @@ Status: implemented 托管 Host 成为每个账户的文件夹工作区与仓库工作区的一台设备,两个 Consumer 都从新的 `managedRoot` 设置进入托管模式(托管 bundle 中为 `avnHomePath('sandbox', 'accounts')`,需同时设置 `AVN_SYNCTHING` 与 `AVN_HOSTED_SANDBOX=1`): - **放置。** 工作区只在其会话于该 Host 上运行一个轮次时才到达,落在 `//.aeven-projects/`(`@aeven/workspace` 的 `placeManagedWorkspace`,名称下已有其他文件时加编号)。不会搜索已有副本。仓库的轮次会等待其克隆或复制;文件夹的读取会像在任何设备上一样等待所需文件。在工作区到达之前记录了占位目录的会话,随后在绑定的目录中工作:在没有自己文件夹的 Host 上,`working-directory` 优先使用已绑定工作区的目录,而不是其外部的已记录目录或没有目录。 -- **Git 在监狱中运行。** `workspace-git-sync` 的 Git 运行器接受一个 `confine` 钩子,托管模式基于沙箱能力构建它:每条 Git 命令都在账户的监狱中运行,以工作树为根、以它使用的那一个传输文件夹为唯一的其他根;其环境通过 `env` 传入;使用 `safe.directory=*`,因为监狱用户既不拥有 Host 创建的目录也不拥有工作树;并使用远程的 HTTPS 形式,这是出口代理唯一承载的形式。 +- **Git 在监狱中运行。** `workspace-git-sync` 的 Git 运行器接受一个 `confine` 钩子,托管模式基于沙箱能力构建它:每条 Git 命令都在账户的监狱中运行,以工作树为根、以它使用的那一个传输文件夹为唯一的其他根;其环境通过 `env` 传入;使用 `safe.directory=*`,因为监狱用户既不拥有 Host 创建的目录也不拥有工作树(监狱为每条受限命令设置它,因此智能体自己的 `git` 在克隆中也能运行);并使用远程的 HTTPS 形式,这是出口代理唯一承载的形式。 - **Host 不访问工作树内的文件。** 进出工作树的复制以受限的 `cp` 命令运行:检查点的未跟踪文件、收到的检查点的未跟踪文件,以及环境文件,Host 会先把环境文件写入一个从未被任何监狱挂载过的暂存文件夹。检查点文件夹通过一次重命名就位,而不是递归复制,其描述以独占方式创建。传输文件夹位于账户的 `.aeven-projects/.transfer`,没有任何会话的监狱挂载它。该 Host 不发布环境文件、不显示冲突文本、也不解决冲突,因为这些都要读取或重命名账户命令可以写入的文件。 - **所有设备。** 作为符号链接的未跟踪文件不再随检查点传输;收到的环境文件路径中含有空、`.`、`..` 或 `.git` 分量时会被跳过。检查点会记录它是否带有工作。设备自己不带工作的检查点(在那里拉取或提交时写入)不再压过另一台设备的工作,收到这样的检查点只会快进干净的工作树;分支领先于收到的检查点的设备会在其上应用该检查点的更改和文件,而不是跳过它;重启后重写未变化的工作会保留其时间。在此之前,云端轮次之后在桌面上拉取会遮住云端未提交的工作,落后于桌面的云端克隆的工作也会被跳过。 diff --git a/packages/sandbox/sandbox-jail/README.i18n.yaml b/packages/sandbox/sandbox-jail/README.i18n.yaml index 87d37b8cb8..a869901afd 100644 --- a/packages/sandbox/sandbox-jail/README.i18n.yaml +++ b/packages/sandbox/sandbox-jail/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/sandbox/sandbox-jail/README.md -README.md: 7eebf85f430410ff48ffac60cea46a57fb8144d9 -README.zh.md: 7a0d8e57aecbb69aa5a325470f448ca6f515af8f +README.md: 17db7ffcfa38a5ae5f392ca7cdf5638e47e29397 +README.zh.md: 8b61e3feed0ea3f28cf8da727d782db42a1300e6 diff --git a/packages/sandbox/sandbox-jail/README.md b/packages/sandbox/sandbox-jail/README.md index 7eebf85f43..17db7ffcfa 100644 --- a/packages/sandbox/sandbox-jail/README.md +++ b/packages/sandbox/sandbox-jail/README.md @@ -51,7 +51,7 @@ Choose it for a multi-account Linux Host. Choose `avn-sandbox-local` for a perso | `launcher` | the npm launcher | Host path of the Landlock launcher | | `readOnlyPaths` | `/usr` and a minimal `/etc` set | Host paths mounted read-only at the same path; missing paths are skipped | | `symlinks` | `/bin`, `/sbin`, `/lib`, `/lib64` into `/usr` | Symlinks created in the jail root | -| `env` | `PATH`, `LANG`, `TMPDIR`, one BLAS thread, headless matplotlib | The complete environment of confined commands; `HOME` is the account folder | +| `env` | `PATH`, `LANG`, `TMPDIR`, one BLAS thread, headless matplotlib | The complete environment of confined commands; `HOME` is the account folder, and Git trusts every repository the jail mounts (`safe.directory=*`), because owners outside the jail show as `nobody` | | `tmpSizeMb` | `512` | Size bound of each of `/tmp` and `/dev/shm` | | `cgroup` | none | Delegated cgroup directory plus combined and per-account limits; an optional `ioMax` in either limits disk I/O, and an optional `memoryHigh` slows a group before `memoryMax` ends it | | `quotaCommand` | none | Argv that limits the account folder's disk use, run with the folder appended in a command's own launch step until `quotaRecord` lists the account; a failure refuses that command | diff --git a/packages/sandbox/sandbox-jail/README.zh.md b/packages/sandbox/sandbox-jail/README.zh.md index 7a0d8e57ae..8b61e3feed 100644 --- a/packages/sandbox/sandbox-jail/README.zh.md +++ b/packages/sandbox/sandbox-jail/README.zh.md @@ -51,7 +51,7 @@ kind: "package-reference" | `launcher` | npm launcher | Landlock launcher 的主机路径 | | `readOnlyPaths` | `/usr` 与最小的 `/etc` 集合 | 以相同路径只读挂载的主机路径;不存在的路径会被跳过 | | `symlinks` | `/bin`、`/sbin`、`/lib`、`/lib64` 指向 `/usr` | 在监狱根目录中创建的符号链接 | -| `env` | `PATH`、`LANG`、`TMPDIR`、单线程 BLAS、无界面 matplotlib | 受限命令的完整环境;`HOME` 为账户文件夹 | +| `env` | `PATH`、`LANG`、`TMPDIR`、单线程 BLAS、无界面 matplotlib | 受限命令的完整环境;`HOME` 为账户文件夹,Git 信任监狱挂载的每个仓库(`safe.directory=*`),因为监狱外的所有者显示为 `nobody` | | `tmpSizeMb` | `512` | `/tmp` 与 `/dev/shm` 各自的大小上限 | | `cgroup` | 无 | 委派的 cgroup 目录,以及总限制与单账户限制;任一限制中可选的 `ioMax` 限制磁盘 I/O,可选的 `memoryHigh` 在 `memoryMax` 终止一个组之前先让它变慢 | | `quotaCommand` | 无 | 限制账户文件夹磁盘用量的 argv,末尾追加该文件夹,在命令自身的启动步骤中运行,直到 `quotaRecord` 列出该账户;失败则拒绝该条命令 | diff --git a/packages/sandbox/sandbox-jail/src/profile.ts b/packages/sandbox/sandbox-jail/src/profile.ts index c4afeefc43..948abc27eb 100644 --- a/packages/sandbox/sandbox-jail/src/profile.ts +++ b/packages/sandbox/sandbox-jail/src/profile.ts @@ -51,6 +51,24 @@ export interface JailProfile { sandboxUser?: { readonly accountsRoot: string; readonly accountsPath: string } | undefined } +/** + * Git configuration that lets Git work in any repository the jail mounts: `safe.directory=*`, appended after the Git + * entries `env` already carries, in the command scope Git reads from its environment. Inside the jail's user + * namespace every owner but the jail's own user shows as `nobody`, so a folder the Host created, such as a + * repository workspace synced to this Host, fails Git's ownership check for every command; the jail, not that check, + * keeps one account from another. + * @param env - the Git configuration entries the jail already sets (`GIT_CONFIG_COUNT` and its keys and values). + * @returns the entries to add, renumbering `GIT_CONFIG_COUNT`. + */ +export function trustedGitEnv(env: Readonly>): Record { + const index = Number(env.GIT_CONFIG_COUNT ?? '0') + return { + GIT_CONFIG_COUNT: String(index + 1), + [`GIT_CONFIG_KEY_${String(index)}`]: 'safe.directory', + [`GIT_CONFIG_VALUE_${String(index)}`]: '*', + } +} + /** First descriptor number {@link jailArgs} assigns to a file the launch step opens; 3 carries the seccomp filter. */ export const FIRST_BIND_DESCRIPTOR = 10 @@ -91,7 +109,7 @@ export function jailArgs( ...profile.seccomp ? ['--seccomp', '3'] : [], ] const egressEnv = profile.egress === undefined ? {} : relayEnv(profile.egress.port, profile.egress.gitHosts) - const env = { ...profile.env, ...egressEnv, HOME: policy.workspaceRoot } + const env = { ...profile.env, ...egressEnv, ...trustedGitEnv(egressEnv), HOME: policy.workspaceRoot } for (const [name, value] of Object.entries(env)) args.push('--setenv', name, value) for (const path of profile.readOnlyPaths) args.push('--ro-bind', path, path) for (const [link, target] of Object.entries(profile.symlinks)) args.push('--symlink', target, link) diff --git a/packages/sandbox/sandbox-jail/tests/git-env.spec.ts b/packages/sandbox/sandbox-jail/tests/git-env.spec.ts new file mode 100644 index 0000000000..d20b3b8c22 --- /dev/null +++ b/packages/sandbox/sandbox-jail/tests/git-env.spec.ts @@ -0,0 +1,51 @@ +/** + * Git inside the jail trusts every repository it can see: owners outside the jail's user namespace show as `nobody`, + * so a repository workspace the Host created would otherwise fail Git's ownership check for every command. + */ +import { describe, expect, it } from 'vitest' +import { relayEnv } from '../src/egress.ts' +import { jailArgs, trustedGitEnv } from '../src/profile.ts' + +/** The value of one `--setenv` pair in a jail argv. */ +function setenv(args: readonly string[], name: string): string | undefined { + for (let index = 0; index < args.length - 2; index++) { + if (args[index] === '--setenv' && args[index + 1] === name) return args[index + 2] + } + return undefined +} + +const PROFILE = { + bwrap: '/usr/bin/bwrap', launcher: '/opt/landlock-run', readOnlyPaths: [], symlinks: {}, + env: { PATH: '/usr/bin:/bin' }, tmpSizeBytes: 1024, hostname: 'jail', seccomp: false, +} +const POLICY = { mode: 'workspace-write', workspaceRoot: '/accounts/a1/.aeven-projects/808f70b3-2' } as const + +describe('trustedGitEnv', () => { + it('adds safe.directory=* as the only Git entry without egress', () => { + expect(trustedGitEnv({})).toEqual({ GIT_CONFIG_COUNT: '1', GIT_CONFIG_KEY_0: 'safe.directory', GIT_CONFIG_VALUE_0: '*' }) + }) + + it('appends after the egress URL rewrites, keeping them', () => { + const egress = relayEnv(3128, ['github.com', 'gitlab.com']) + expect(trustedGitEnv(egress)).toEqual({ GIT_CONFIG_COUNT: '3', GIT_CONFIG_KEY_2: 'safe.directory', GIT_CONFIG_VALUE_2: '*' }) + }) +}) + +describe('jailArgs', () => { + it('sets safe.directory=* in every confined command, beside the egress rewrites', () => { + const { args } = jailArgs({ ...PROFILE, egress: { relay: '/opt/relay', port: 3128, gitHosts: ['github.com'] } }, POLICY) + + expect(setenv(args, 'GIT_CONFIG_COUNT')).toBe('2') + expect(setenv(args, 'GIT_CONFIG_KEY_0')).toBe('url.http://127.0.0.1:3128/git/github.com/.insteadOf') + expect(setenv(args, 'GIT_CONFIG_KEY_1')).toBe('safe.directory') + expect(setenv(args, 'GIT_CONFIG_VALUE_1')).toBe('*') + expect(setenv(args, 'HOME')).toBe(POLICY.workspaceRoot) + }) + + it('sets it without an egress proxy too', () => { + const { args } = jailArgs(PROFILE, POLICY) + + expect(setenv(args, 'GIT_CONFIG_COUNT')).toBe('1') + expect(setenv(args, 'GIT_CONFIG_KEY_0')).toBe('safe.directory') + }) +})