Files
cobble/src/harness/tools/computer/computer-dirs-shell.ts
T
tben d7e66a5123 Keep computer caches off the data volume and shrink data archives
One install's production archive was 2 GB: 2457 MB of computer
workspaces (983 MB of npm, pip and bun caches, plus 697 MB of
node_modules and 446 MB of pip installs), 993 MB of Chrome profile
caches, 148 MB of database snapshots and 15 MB of bun-sqlite scratch
space, against 178 MB of bot files. Runs had HOME on the mounted
workspace and no cache variables, so every side cache was persisted
forever, and the archiver tarred the whole volume with one exclusion.

- Runs keep their caches inside the container: computerCacheEnv points
  XDG_CACHE_HOME, npm_config_cache, PIP_CACHE_DIR,
  BUN_INSTALL_CACHE_DIR and PYTHONPYCACHEPREFIX at the account's own
  home, never the run's folder.
- prune-computer-caches (daily) clears what earlier runs left in bot
  folders, user folders, workspaces and Chrome profiles, skipping a
  computer with a task queued or running. The agent clears a profile's
  regenerable cache at each start and caps its HTTP cache.
- listArchiveExclusions is the one definition of what a data archive
  leaves out, as exact member paths (--no-wildcards --exclude-from):
  computers, Chrome profile caches, db/.backups and .bun-sqlite. A
  restore keeps the running install's db/.backups, since no archive
  carries them, and still leaves computer folders live.
- A computer's workspace has a quota (computer_workspace_quota_mb,
  Settings > General, default AppData.ComputerWorkspaceQuotaMb): the
  agent measures it, the app refuses a write past it and warns after
  computer_bash and browser calls.
- An upload's bytes go with the messages that carried them, and
  prune-orphan-media takes what is left after
  AppData.MediaOrphanKeepDays; moved-aside old computer homes are
  pruned after AppData.ComputerOldHomeKeepDays.
- install.sh removes the image the previous container ran and dangling
  layers, so updates stop accumulating gigabytes on the host.
2026-11-12 20:36:46 +01:00

50 lines
2.4 KiB
TypeScript

import type { ComputerRunDirs } from "@/src/utils/computer-work-dirs";
import computerWorkspacePath from "@/src/utils/computer-workspace-path";
import shellQuote from "@/src/utils/shell-quote";
import computerCacheEnv from "./computer-cache-env";
/**
* The shell that puts a command in the run's folder on the computer:
* creates the user's folder in the bot's, moves into it and makes it
* `HOME`, so `~` and every relative path land there. The coordinator also
* creates the workspace's shared folder. A worker's shared folder is a
* link in its own (`workspaces/<id>`, made by the computer's agent) to the
* real one; when it's gone the link is put back, never a folder made in
* its place, which would keep the workspace's work from its other bots. A
* worker (its own account, no sudo) also gets `umask 007`, so its files
* stay its workspace group's and nobody else's, and installs into that
* home that work without root (`pip install --user`, `npm install -g` into
* `~/.local`, on `PATH`). Caches go to the account's own home inside the
* container instead (`computerCacheEnv`), so no cache a run makes is kept
* on the data volume. Ends with `; `, ready to prefix a command.
*/
export default function computerDirsShell({ dirs }: { dirs: ComputerRunDirs }) {
const cwd = shellQuote({ text: dirs.cwd });
const cache = computerCacheEnv({ dirs });
const cache_dir = shellQuote({ text: cache.cache_home });
if (!dirs.run_as) {
const folders = [
dirs.cwd,
cache.cache_home,
...(dirs.shared_dir ? [dirs.shared_dir] : []),
]
.map((dir) => shellQuote({ text: dir }))
.join(" ");
return `mkdir -p ${folders} && cd ${cwd} && export HOME=${cwd} ${cache.assignments}; `;
}
const shared = dirs.shared_dir ? sharedLinkShell({ link: dirs.shared_dir }) : "";
return `umask 007 && ${shared}mkdir -p ${cwd} ${cache_dir} && cd ${cwd} && export HOME=${cwd} ${cache.assignments} && export PATH="$HOME/.local/bin:$PATH" NPM_CONFIG_PREFIX="$HOME/.local"; `;
}
function sharedLinkShell({ link }: { link: string }) {
const quoted = shellQuote({ text: link });
const parent = shellQuote({ text: link.replace(/\/[^/]+$/, "") });
const target = shellQuote({ text: computerWorkspacePath({ absolute: link }) });
return `{ [ -e ${quoted} ] || [ -L ${quoted} ] || { mkdir -p ${parent} && ln -s ${target} ${quoted}; }; } && `;
}