diff --git a/nixos/knife-linux.Containerfile b/nixos/knife-linux.Containerfile new file mode 100644 index 0000000000000000000000000000000000000000..10fa3f98ce3a18e4e826bece47df7baac5566a05 --- /dev/null +++ b/nixos/knife-linux.Containerfile @@ -0,0 +1,28 @@ +FROM registry.fedoraproject.org/fedora:44@sha256:0d2a354dfdf7ba75083fe371eb6239842fe13f38c52d339dae1ebcf13c7c4062 + +# Development tools, plus the libraries and fonts T3's headless Chrome needs. +RUN dnf -y install --setopt=install_weak_deps=False \ + bzip2 cargo cmake diffutils fd-find file findutils gcc gcc-c++ git hostname iproute jq less \ + lsof make neovim openssh-clients openssl-devel patch procps-ng python3 ripgrep rsync rust tar \ + time tree unzip which xz zip zlib-ng-compat-devel zsh \ + alsa-lib at-spi2-atk at-spi2-core atk dbus-libs expat fontconfig glib2 libX11 libXcomposite \ + libXdamage libXext libXfixes libXrandr libxcb libxkbcommon liberation-mono-fonts \ + liberation-sans-fonts liberation-serif-fonts mesa-libgbm nss \ + && dnf clean all + +RUN curl -fsSLo /tmp/node.tar.xz https://nodejs.org/dist/v26.12.1/node-v26.12.1-linux-x64.tar.xz \ + && echo "6170b2d474e1c626f43cff3adc118df9fe3b573258a4fc3d850c8dbc6b079bbd /tmp/node.tar.xz" | sha256sum -c \ + && mkdir -p /opt/node/26.12.1 \ + && tar -xJf /tmp/node.tar.xz -C /opt/node/26.12.1 --strip-components=1 \ + && rm /tmp/node.tar.xz \ + && ln -s /opt/node/26.12.1/bin/node /opt/node/26.12.1/bin/npm /opt/node/26.12.1/bin/npx /usr/local/bin/ \ + && npm install --global --prefix=/usr/local --allow-scripts=pnpm pnpm@12.14.0 vite-plus@1.3.0 \ + && npm cache clean --force + +ARG UID +ARG GID +RUN groupadd --gid "$GID" chloe && useradd --uid "$UID" --gid "$GID" --no-create-home --home-dir /home/clo clo +USER clo +ENV HOME=/home/clo LANG=C.UTF-8 PATH=/home/clo/.local/bin:/usr/local/bin:/usr/bin:/bin +WORKDIR /Volumes/Code/project +ENTRYPOINT ["/home/clo/.local/bin/knife", "start", "--mode", "desktop", "--host", "127.0.0.1", "--port", "3773", "--no-browser"] diff --git a/nixos/knife-linux.nix b/nixos/knife-linux.nix new file mode 100644 index 0000000000000000000000000000000000000000..0caa358588c4be38e374c5e4e863d8210cc754ec --- /dev/null +++ b/nixos/knife-linux.nix @@ -0,0 +1,54 @@ +{ config, lib, pkgs, ... }: +let + sandbox = (builtins.fromJSON (builtins.readFile ../config/mac-studio-sandbox.json)).mountPoint; + root = "/srv/knife-linux"; + uid = toString config.users.users.clo.uid; + gid = toString config.users.groups.chloe.gid; + engine = pkgs.writeText "knife-linux-containers.conf" '' + [engine] + tmp_dir = "/run/knife-linux/libpod" + lock_type = "file" + ''; + # A second rootful store must not share Nomad's lock segment or state directory. + podman = pkgs.writeShellScriptBin "knife-linux-podman" '' + export CONTAINERS_CONF_OVERRIDE=${engine} + exec ${pkgs.podman}/bin/podman --root=${root}/containers/storage --runroot=/run/knife-linux/containers "$@" + ''; + run = "${podman}/bin/knife-linux-podman"; +in +{ + environment.systemPackages = [ podman ]; + + systemd.services.knife-linux = { + description = "The Knife Linux agent container"; + wantedBy = [ "multi-user.target" ]; + wants = [ "network-online.target" ]; + after = [ "network-online.target" "mac-studio-sandbox.service" ]; + requires = [ "mac-studio-sandbox.service" ]; + serviceConfig = { + Type = "notify"; + NotifyAccess = "all"; + ExecStartPre = [ + "${pkgs.util-linux}/bin/mountpoint -q ${root}" + "${run} build --quiet --build-arg=UID=${uid} --build-arg=GID=${gid} --tag=localhost/knife-linux --file=${./knife-linux.Containerfile} /var/empty" + ]; + ExecStart = lib.concatStringsSep " " [ + run "run --rm --replace --name=knife-linux --pull=never" + "--sdnotify=conmon --cgroups=no-conmon --log-driver=journald --init --hostname=knife-linux" + "--user=${uid}:${gid} --umask=0077 --cap-drop=ALL --security-opt=no-new-privileges" + "--memory=32g --cpus=12 --pids-limit=32768 --shm-size=2g --network=podman" + "--volume=${root}/home:/home/clo" + "--volume=${sandbox}:/Volumes/Code" + "--volume=/opt/studio-project-store/current/bin/jj-real:/usr/local/bin/jj:ro" + "localhost/knife-linux" + ]; + ExecStop = "${run} stop --ignore --time=90 knife-linux"; + ExecStopPost = "${run} rm --ignore --force knife-linux"; + KillMode = "mixed"; + TimeoutStartSec = "30min"; + TimeoutStopSec = 120; + Restart = "always"; + RestartSec = 5; + }; + }; +} diff --git a/nixos/snowglobe.nix b/nixos/snowglobe.nix index fa2dbe2dd86dd6880451ba11a3c9c50ad007f753..662090570e4397ec1ddf881768f66b7b877e7553 100644 --- a/nixos/snowglobe.nix +++ b/nixos/snowglobe.nix @@ -16,7 +16,7 @@ let }; in { - imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./mac-vm.nix ./zfs-read-cache.nix ]; + imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./knife-linux.nix ./mac-vm.nix ./zfs-read-cache.nix ]; networking.hostName = "snowglobe"; environment.etc."machine-info".text = '' diff --git a/tools/agent-home.py b/tools/agent-home.py new file mode 100755 index 0000000000000000000000000000000000000000..93f95f7eb2e0b98b87a9e5aa9c20b938151d1b08 --- /dev/null +++ b/tools/agent-home.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +"""Package Claude and Codex instructions and skills with shared local targets.""" +import argparse +from pathlib import Path +import tarfile + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("archive", type=Path) + parser.add_argument("--source-home", type=Path, default=Path.home()) + args = parser.parse_args() + instructions = [args.source_home / ".claude/CLAUDE.md", args.source_home / ".codex/AGENTS.md"] + if instructions[0].read_bytes() != instructions[1].read_bytes(): + parser.error("Claude and Codex have different global instructions; choose a shared version first.") + targets = {} + + def keep(member): + return None if any(part in {".DS_Store", "__pycache__", ".git"} for part in Path(member.name).parts) else member + + with tarfile.open(args.archive, "w:gz", dereference=True) as archive: + archive.add(instructions[0], arcname=".config/agents/AGENTS.md") + for provider, name in [("claude", "CLAUDE.md"), ("codex", "AGENTS.md")]: + link = tarfile.TarInfo(f".{provider}/{name}") + link.type = tarfile.SYMTYPE + link.linkname = "../.config/agents/AGENTS.md" + archive.addfile(link) + for skill in sorted((args.source_home / f".{provider}/skills").iterdir()): + if not skill.is_dir(): + continue + source = skill.resolve(strict=True) + target = targets.get(source) + if target is None: + target = f".config/agents/skills/{skill.name}" + if target in targets.values(): + parser.error(f"Different skills share the name {skill.name}; choose their shared version first.") + targets[source] = target + archive.add(source, arcname=target, filter=keep) + link = tarfile.TarInfo(f".{provider}/skills/{skill.name}") + link.type = tarfile.SYMTYPE + link.linkname = "../../" + target + archive.addfile(link) + args.archive.chmod(0o600) + + +if __name__ == "__main__": + main() diff --git a/tools/knife-linux-codex.patch b/tools/knife-linux-codex.patch new file mode 100644 index 0000000000000000000000000000000000000000..7a009dff6c1cb59ede45b049028fd5d7556fe764 --- /dev/null +++ b/tools/knife-linux-codex.patch @@ -0,0 +1,40 @@ +--- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts ++++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts +@@ -30,7 +30,7 @@ + + const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "models_cache.json"]); + // Finder maintains metadata separately for each directory, including shadow homes. +-const SHADOW_LOCAL_ENTRY_NAMES = new Set(["log", "memories", "tmp", ".DS_Store"]); ++const SHADOW_LOCAL_ENTRY_NAMES = new Set(["log", "memories", "tmp", ".tmp", ".DS_Store"]); + // Codex creates these runtime locks itself when it starts in a shadow home before the shared + // home has one. They guard shared state, so a local copy is replaced with the shared link. + const REPLACEABLE_SHARED_RUNTIME_ENTRIES = new Set(["mcp-oauth-locks", ".sqlite-maintenance.lock"]); +--- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts ++++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +@@ -234,11 +234,13 @@ + yield* fileSystem.makeDirectory(path.join(sharedHome, "log")); + yield* fileSystem.makeDirectory(path.join(sharedHome, "memories")); + yield* fileSystem.makeDirectory(path.join(sharedHome, "tmp")); ++ yield* fileSystem.makeDirectory(path.join(sharedHome, ".tmp")); + yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n'); + yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n'); + yield* fileSystem.makeDirectory(path.join(shadowHome, "log"), { recursive: true }); + yield* fileSystem.makeDirectory(path.join(shadowHome, "memories"), { recursive: true }); + yield* fileSystem.makeDirectory(path.join(shadowHome, "tmp"), { recursive: true }); ++ yield* fileSystem.makeDirectory(path.join(shadowHome, ".tmp"), { recursive: true }); + + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ +@@ -260,7 +262,12 @@ + .readLink(path.join(shadowHome, "tmp")) + .pipe(Effect.result); + ++ const dotTmpLinkResult = yield* fileSystem ++ .readLink(path.join(shadowHome, ".tmp")) ++ .pipe(Effect.result); ++ + expect(configTarget).toBe(path.join(sharedHome, "config.toml")); ++ expect(dotTmpLinkResult._tag).toBe("Failure"); + expect(logLinkResult._tag).toBe("Failure"); + expect(memoriesLinkResult._tag).toBe("Failure"); + expect(tmpLinkResult._tag).toBe("Failure"); diff --git a/tools/knife-linux.md b/tools/knife-linux.md new file mode 100644 index 0000000000000000000000000000000000000000..8fdaeb87fa702fef7c39bd5a33867b3083d2a1db --- /dev/null +++ b/tools/knife-linux.md @@ -0,0 +1,100 @@ +# Headless Knife on Snow Globe + +`knife-linux` is a Fedora 44 Podman container that runs The Knife for Linux +agents directly beside ZFS. [knife-linux.nix](../nixos/knife-linux.nix) builds it +from [knife-linux.Containerfile](../nixos/knife-linux.Containerfile) on each start +and runs it as the `knife-linux` system service: uid 3000 (`clo`), no +capabilities, 12 CPUs, a 32 GiB memory limit, and Podman's NAT network. Knife +listens on container loopback port 3773; devices reach it through the mesh as +`snowglobe` (device `4b85ee48…`). User namespaces work inside, so Chrome's +sandbox and agent sandboxes run unprivileged. + +| Container path | Host source | +| --- | --- | +| `/Volumes/Code` | the Mac VM's NFS export, `/srv/mac-studio-vm/sandbox` | +| `/home/clo` | `/srv/knife-linux/home` on `globe/clover/knife-linux` | +| `/usr/local/bin/jj` | the storage host's `jj-real`, read-only | + +Projects keep the Mac VM's paths, so jj workspaces, Knife project paths, and +Claude memory keys match on both. A workspace's `node_modules` serves one OS at +a time; run `vp i` after moving a workspace between them. pnpm's global config +uses the storage host's `/Volumes/Code/cache/pnpm12` store and `files` index +format. jj per-repo configs in `~/.config/jj/repos` are copies of the Mac VM's, +keyed by each repository's `config-id`. + +`globe/clover/knife-linux` must stay case-sensitive. Its parent is not, and +Fedora packages and overlay layers fail under case folding. Podman's store lives +on this dataset because the root NVMe is nearly full. `knife-linux-podman` is the +only entry point to that store: it pins a separate temp directory and file locks, +because a second rootful store sharing Nomad's lock segment would free the other +store's locks during its post-reboot refresh. + +```sh +ssh root@10.0.0.1 'systemctl status knife-linux' +ssh root@10.0.0.1 'journalctl -u knife-linux -n 80 --no-pager' +ssh -t root@10.0.0.1 'knife-linux-podman exec -it knife-linux bash -l' +``` + +The mesh identity and floating profiles in `~/.knife` are the ones the retired +`knife-linux` VM enrolled. Only the container runs that identity. Retirement +recovery archives are kept privately on the owner's Mac at +`~/.local/share/knife-linux-recovery/20261023` and on the external Documents drive +at `Knife Recovery/20261023`. Each copy was restored into an isolated container +with networking disabled, checking the application, databases, and identity keys. + +## Runtime and updates + +The active runtime is `~/.local/share/knife-server/current`, pointing to +`build-372d14b9d5b4-linux-tmp`. This is a pinned native build of Clover's +`knife-merge` fork, not the older published Linux container. Source archive +SHA256: `372d14b9d5b41eef1e8bcc856966fbc8749dd091df1ac09682c9238770f68b39`. +Private installation archives are on ZFS in `/srv/vm/.knife-linux-deploy-20261023`. +Apply [knife-linux-codex.patch](knife-linux-codex.patch) before building. It keeps +Codex's `.tmp` plugin locks local to each profile so restarts can recreate drivers. +[knife-server](knife-server) is the `~/.local/bin/knife` launcher. + +Build inside the container from a fresh source directory, keeping the current +runtime available until verification succeeds: + +```sh +pnpm install --frozen-lockfile +vp run --filter t3 build +CARGO_BUILD_JOBS=8 cargo build --locked --release \ + --manifest-path native/knife-mesh/Cargo.toml -p knife-mesh-node \ + --target x86_64-unknown-linux-gnu +CARGO_BUILD_JOBS=4 cargo build --locked --release \ + --manifest-path native/resource-monitor/Cargo.toml +``` + +After the server build, stage `libknife_mesh_node.so` as +`apps/server/dist/knife-mesh/linux-x64/knife_mesh.node` and the resource monitor as +`apps/server/dist/resource-monitor/linux-x64/t3-resource-monitor`. Stage the font +assets in `apps/server/dist/client/fonts` and link `node` in the runtime root to +`/opt/node/26.12.1/bin/node`. Switch `current` and `systemctl restart knife-linux` +only when idle. Automatic fork updates are not configured for this source build. + +## Agent homes + +[agent-home.py](agent-home.py) exports global instructions and skill catalogs, +dereferencing Mac/Nix links. Instructions live once in `~/.config/agents/AGENTS.md`; +Claude and Codex link to it. Shared skill trees live in `~/.config/agents/skills`. + +Memory snapshots from the owner Mac and the Mac VM were imported separately from +credentials and transcripts. Claude project paths under `~/.claude/projects` +link to canonical stores in `~/.local/share/agents/imported-memories`, including +the Mac VM's `/Volumes/Code/project` keys. Conflicting versions are preserved, +and indexes include both versions. Original sources remain intact. + +Codex uses the shared Claude-memory hook at `~/.config/agents/codex-memory-hook.py`, +configured in `~/.codex/config.toml`. Generated Codex memory artifacts also retain +their original `~/.codex/memories*` locations. Managed agent homes use the global +instructions, skills, project memories, and hook configuration. + +Knife links each Codex profile home's shared entries to `~/.codex`. If Codex +writes a real file where Knife expects a link, that profile fails at startup with +"already exists and is not a symlink". If the file matches the `~/.codex` copy, +delete it; Knife recreates the link on restart. +Initialize the global Codex home before materializing fresh floating profiles: +independent first runs create local SQLite files that conflict with shared-home +links. Do not copy `.knife` identities, auth vaults, or a live SQLite database +file over an active process. diff --git a/tools/knife-server b/tools/knife-server new file mode 100755 index 0000000000000000000000000000000000000000..2450a88e11a43bdfcc46d9ddfdcad8d21f450503 --- /dev/null +++ b/tools/knife-server @@ -0,0 +1,9 @@ +#!/bin/sh +set -eu + +runtime="$HOME/.local/share/knife-server/current" +export T3CODE_HOME="${T3CODE_HOME:-$HOME/.knife}" +export T3CODE_CLI_PATH="$HOME/.local/bin/knife" +export KNIFE_MESH_ADDON="$runtime/apps/server/dist/knife-mesh/linux-x64/knife_mesh.node" +export T3CODE_RESOURCE_MONITOR_PATH="$runtime/apps/server/dist/resource-monitor/linux-x64/t3-resource-monitor" +exec "$runtime/node" "$runtime/apps/server/dist/bin.mjs" "$@"