small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
albedo robot-docs perf.md
20 kB · 99 lines
Markdown
at main

Performance checks #

test/perf/run.py measures retained memory, HTTP payload bytes and disk writes through the real daemon. It replays committed bonsai SSE responses and gates every commit; replay is the only measurement mode. --mode record makes those recordings (see Isolated recording) and measures nothing.

Commands #

nix develop -c go -C cli build -o bin/albedo ./cmd/albedo
nix develop -c python3 test/perf/run.py --mode replay --runs 5 --out /tmp/perf/results.json [--baseline main-results.json]
python3 test/perf/compare.py /tmp/perf/results.json [--baseline main-results.json]

ALBEDO_TEST_DAEMON accepts a launcher from test/snapshot-daemon.sh; without it the shared E2E harness builds one snapshot for the invocation. ALBEDO_TEST_CLI selects a prebuilt CLI and PERF_COMMIT names the commit when the checkout has no usable git. Each repetition gets a fresh hermetic daemon home, and the harness stops every private daemon on success and failure. A failed measurement is not retried, and a scenario that stops early reports its own error rather than the recording's unused responses. Replay pairs each proxied provider request with its recorded answer and fails when the counts differ.

In replay, a scenario's repetitions run at the same time, each on its own daemon (--jobs 1 runs them in turn; a recording is one run). A replay gives every daemon request the 180 s a turn has (harness.REQUEST_SECONDS, 20 s for the e2e suites), since five daemons replaying beside the runner's other workflows can hold a prompt's acknowledgement past 20 s, and request latency is not gated. On valefar at CI size (12 CPUs, 12 GiB) a five-run replay of the eight scenarios before idle takes about 2.4 minutes of scenario time concurrently and 10.5 minutes sequentially; idle adds about 1.5 minutes. --scenario NAME selects a diagnostic subset and derives no gates.

Scenarios: boot (empty daemon, then one idle session); chat (five text turns); coding (eight cells that read and edit a fixture and run its tests); long (60 text turns: 20 warm-up, then 40 gated, with HTTP and albedo sessions read hydration at the end); fan-out (one turn in each of 20 sessions); agents (a parent and three children that mail it); kernel (one cell at rest, five warm-up cells, then 30 cells of ordinary work); pool (a cold kernel, its release, a wake that warms spares, a second session adopting a forked spare, then rest); idle (a session never used, a text turn in another, a cell in a third, then one hour of the daemon's timers at ALBEDO_TEST_TIME_SCALE=60). idle's recording is chat's first turn and pool's first turn, which it replays unchanged.

Memory #

Linux measurements need /proc/<pid>/smaps_rollup and an inspect-enabled daemon. measurement.py reuses test/manual/agent_density.py process discovery, which on Linux identifies helpers by the role name they give themselves (albedo-kernel, -spare, -zygote, -bridge) and claims a reparented one by ALBEDO_HOME in its environment. One erl node per measured daemon stays connected to the inspect node and calls test/perf/albedo_perf_probe.erl; starting an erl per call cost about 0.5 s each.

A retained checkpoint waits 300 ms for the session's two quiet hibernation ticks, runs a major GC of every BEAM process, then reports erlang:memory() by category plus heap_slack (heap block space no data occupies) and the twelve largest processes, each process's PSS and USS, each session actor's memory with the processes it owns, and each held kernel's USS. A 50 ms sampler records the tree's peak PSS and USS and the BEAM total between checkpoints; a peak is a sampled maximum, not an allocation trace. Every turn is measured except in long and kernel, which take a checkpoint at the end of warm-up and on every fifth turn after it; wire bytes are counted on every turn.

The tree gates use USS, not PSS. PSS divides shared file pages among every process that maps them, so another BEAM on the host moved the empty daemon's PSS by 8 MB; USS is private to the measured processes. PSS stays in each scenario's report. Daemon USS still moves in allocator-carrier steps of 2 to 5 MB between checkpoints, which BEAM allocated memory does not.

Concurrency does not move the gated numbers: in three five-run batches, two concurrent and one sequential, every sequential median lay within the spread of the concurrent ones, with no larger MAD.

The BEAM gates (idle_session_beam, retained_turn_beam, long's warm-up) count total less heap_slack and less the binaries no process references (binary minus owned_binary), and fit a Theil-Sen slope (the median pairwise slope). Two things moved them on an unchanged daemon (pipeline 3mzd7hmuc4322 failed idle_session_beam by 25 KB). A major GC sizes each heap to the next step of the heap-size table above its live data, so a process whose data sits near a step flips between two sizes: in every fan-out run the runtime actor alternated between 1.57 and 2.55 MB at sessions 17 to 20, the points with the most leverage on a least-squares slope. And erlang:memory(binary) grew 102 KB with most sessions but 233 KB with a few, at random ones, while the binaries processes reference grew 95 to 96 KB with every session; the rest are held by ETS or a NIF. Over six batches since, idle_session_beam read 209 to 211 KB per session with MAD at most 1.2 KB (it was 218 to 279 KB, MAD up to 20 KB) and retained_turn_beam 0.3 to 2 KB.

The memory sampler and the write counter run on separate threads: a /proc scan inside the 50 ms loop thinned it to a few samples per turn, and turn_peak_tree_uss missed the coding kernel's start (2 MB instead of 23 MB).

HTTP bytes #

transport.py puts a transparent TCP proxy in front of the provider gateway and another in front of the daemon API. Counts include HTTP headers, request compression and chunk framing, and exclude TCP/IP headers. Provider rows carry request and response bytes and the reported prompt and cached tokens, attributed to the turn that caused them. A fixed full-state read after each settled turn measures client hydration; readiness polling bypasses the measured connection.

Provider request bytes are identical across replay runs. Hydration bytes vary by up to about 160 B in 73 KB, because event payloads carry timestamps and durations; the one-percent byte rule absorbs this.

The replay gateway serves the recorded status, headers and exact SSE body bytes with content-length framing; it does not reproduce upstream packet timing. Recordings were made at temperature zero, seed 42, max_tokens 512.

Freshness #

Each recorded request has SHA-256 digests for its system prompt, tool schemas and message shapes. Per-run fixture paths and UUIDs are normalized before hashing. Replay reports changed categories in scenarios.<name>.recordings.stale_turns; staleness does not fail the memory or byte gate. Recordings retain their UTC creation time and model. A stale recording needs deliberate refresh against bonsai, not a synthetic response edit.

Retained growth per turn #

Long's first 20 turns are reported as warmup_retained (memory after turn 20 minus before turn 1, about 3.4 MB of USS) and not gated. Over turns 20 to 60, retained_turn_beam is about 0 (medians 1.5, 4.4 and 1.9 KB per turn in three batches) and is the precise leak gate. retained_turn_tree_uss, process memory per turn, is 10 to 16 KB: the daemon's SQLite writer connection keeps SQLite's default 2 MB page cache (readers and the checkpoint connection set cache_size=-128), and a 160-turn diagnostic showed daemon memory still growing 12 to 13 KB per turn through turn 120 and 2.6 KB per turn over turns 120 to 160, matching that cache filling while the transcript grows about 2.7 KB per turn. The writer cache is a deliberate one-time cost, so this number has a 24 KB regression floor and a budget instead of a target of zero.

Kernels #

kernel_rest_uss is one kernel's USS after its first cell (16.3 MB in every run). kernel_cell_slope is the kernel's USS per cell over cells 6 to 36 (about 8 KB). A 600-cell diagnostic on valefar, cycling the recorded cells, showed that growth is bounded: kernel USS climbs about 2 KB per cell, then drops by about 1.2 MB at irregular points (near cells 250 and 350), and after 600 cells sat 1.2 MB below cell 10. The gated 30-cell slope measures the first ramp of that sawtooth, so a per-cell leak adds to it and shows as a regression.

The pool scenario runs the product's pool defaults (two spares, the Linux zygote) with its timers at ALBEDO_TEST_TIME_SCALE=6. kernel_pool_pss is what remains after every kernel is released and the spares cool down: 0, since the zygote exits after its idle minute. Reported but not gated: a cold-launched kernel costs 16.3 MB USS, a kernel forked from the zygote 6.6 MB, and the warm pool (zygote and spares) 19.9 MB PSS. Under a scaled clock the zygote's idle minute and the spare cool-down are scaled too; before that, an unscaled timer left the zygote alive at a scaled rest checkpoint.

Disk writes #

measurement.Writes reads /proc/<pid>/io for the daemon, every descendant (kernels, the zygote and its forks, bridges, erl_child_setup, jobs) and any helper that names itself albedo-* with this home, and counts write_bytes minus cancelled_write_bytes: pages the tree dirtied, less pages truncated before writeback. A reaped child's counters move into its parent's, so a process that exits under the tree stays counted; one reparented outside it keeps its last value, sampled every 0.5 s. Before counting, the harness writes and syncs 256 KiB under the fixture root and fails unless its own write_bytes grew by that much, so a home on tmpfs (where nothing is accounted) fails instead of reporting 0. CI's homes are on the microVM's ext4 /tmp.

writes_shim.c, built with cc and preloaded into the tree (LD_PRELOAD), adds what every write/pwrite/writev call returns to a per-file slot in a table on /dev/shm. It names the files; it counts calls rather than pages and misses writes through shared mappings (SQLite's -shm) and inside libc, so it does not add up to the total. Each checkpoint carries both, and each scenario reports the files its window wrote, by median over the runs, with session ids and temp suffixes folded to *.

The store backfills its WAL into albedo.sqlite up to 30 s after a commit (robot-docs/runtime.md), so a measurement that ends right after its writes would hand their backfill to whatever is measured next. Boot's first checkpoint, both ends of long's gated turns and the start of idle's hour therefore wait until the wal-index in -shm says every frame is backfilled (Writes.backfilled: nBackfill equals mxFrame). The harness only reads that file and never opens the database: closing a descriptor of -shm drops every POSIX lock the closing process holds there.

disk_written_boot is what the tree wrote by the first checkpoint on an empty home, backfill included: schema creation and the fixture's provider profile. disk_written_per_turn is the median of long's turns 21 to 60, with the home flushed (syncfs) before and after each turn as pages would be written back between a person's turns. WAL frames are 4,120 bytes and straddle pages, so a few more bytes of content per turn move it by whole pages: batch medians ranged 487 to 500 KB on one daemon. disk_appended_per_turn is the precise number: what write calls gave every file except albedo.sqlite per turn (WAL frames, logs, state files), 181,312 to 181,314 B in every run on main after session_prompts (275,866 to 275,869 before it). Checkpoint copies into the database are left out because a timer starts them; disk_backfilled_per_turn counts them instead: what write calls gave albedo.sqlite over the same turns, both ends waiting for the backfill. A page rewritten every turn is copied once per 30 s of turns, so it is about 13 KB per turn where backfilling after every turn would copy every rewritten page each turn. disk_written_idle_hour is what idle's daemon writes over its hour, from once the turns before it are backfilled. Under the scaled clock the backfill's 30 s age is scaled too; while it was compared against the real clock, the turns' backfill (800 KB) landed 30 real seconds into the scaled hour. Writeback is the kernel's and is not scaled, so a file rewritten every 10 s would coalesce 60 times more often than in real time; the harness calls syncfs on the home every 30 scaled seconds, the kernel's dirty expiry, so such rewrites are written as often as they would be. Deadlines compared against the wall clock (unloading a session after an hour) do not fire in the scaled hour.

On valefar at CI size: boot 1,589,248 B in nine batches (MAD 0) and 1,683,456 in one; a steady text turn about 490 KB, of which 276 KB were write calls outside the database (seven SQLite commits and 66 WAL frames, most to the sessions row and the free pages its overflow chain left); moving the prompts out of that row (402a7545) took the turn to 319 KB and 181 KB. The scaled hour writes 0 to 180 KB, all in its first scaled minute when the held kernel is released, then nothing. One unscaled hour wrote 278 KB, all within its first two minutes. The per-file breakdown names only albedo.sqlite-wal and albedo.sqlite in long and idle.

The gate caught both injected regressions against batch L: a timer rewriting a small file every 10 s raised disk_written_idle_hour from 0 to 1.87 MB (over budget, home/touched named), and appending 10 KB to a file per submitted input raised disk_appended_per_turn by exactly 10,000 B and disk_written_per_turn by 20.5 KB (both regressed, home/turns.log named). Batches M and N of the unchanged daemon pass against L.

test/manual/disk_writes.py --minutes N reads ri_diskio_byteswritten (proc_pid_rusage) for a live macOS daemon's tree and lists the files under the home that changed.

Comparison #

Results schema version 1 contains each scenario's medians, median absolute deviations (MAD), raw samples, request rows and recording freshness, plus the derived gated numbers and their verdicts. A single-sample gate carries single_sample.

budgets.toml holds an absolute ceiling and a regression floor per gated number, set from the three batches above; its comments give the measurements behind each. A memory number regresses when it exceeds the baseline by more than the largest of three times the baseline's MAD, five percent of the baseline, and its floor. Only the baseline's noise counts: a regression that also adds noise must not widen its own margin (the deliberate leak raised per-turn BEAM MAD from 1.5 to 11 KB). A byte count regresses past one percent. A disk number regresses past the larger of three times the baseline's MAD and its floor, with no percentage share, since five percent of a 490 KB turn would hide 10 KB more per turn. A number over its ceiling fails with or without a baseline, so repeated small regressions cannot accumulate. With no ancestor result there is no baseline and only budgets apply.

Each gated number's baseline is the newest ancestor main record that passed it, or that accepted.toml names for it: a line <metric>.<commit> = { value = <value>, reason = "..." }, whose value must equal the record's. A regressed record never becomes the baseline by itself, so a regression stays red on every later push until it is fixed or accepted in a reviewed commit; the commit that adds the line is the one that accepted it. Ceilings apply either way. CI prints the line for each regressed number, and the run page shows it. Seeded on b3459abb: disk_written_boot (1,662,976 B: the 929,792 B baseline before it was the scaled-age backfill drifting out of boot's window) and request_bytes_per_turn (+1,252 B of prompt text from 09f5f5e4, a49b933b and be373e31).

The gate catches both injected regressions on scratch branches at CI size against a batch as baseline: keeping a 40 KB copy of each submitted operation in the session actor raised retained_turn_tree_uss to 53.7 KB and retained_turn_beam to 23.6 KB per turn (both over budget, and regressed), and an unused 7.6 KB tool schema raised request_bytes_per_turn from 48,704 to 56,421 B (regressed and over budget). The other two batches compared against the same baseline pass.

Results site #

CI records each run in okami.mom's atproto repo (pet.nekomimi.albedo.perf.run, rkey = commit sha, the full result as a blob) and logs its page, https://sites.wisp.place/okami.mom/albedo-perf/<sha7>/. albedo-perf is one static app (wisp SPA mode) that reads the records and the run's own blob from the PDS at view time; / lists runs and /main/ is the latest main push. Nothing is deployed per run: the site is redeployed only when its own files change (its version.json hash differs from the served one), from a push or manual run on main, never another branch or a pull request. test/perf/report/README.md has the record layout, routes and caching.

Output safety #

Private daemons receive an explicit environment allowlist: PATH, HOME and TMPDIR under the fixture, LANG, fixed VM flags, and the ALBEDO variables required for the private snapshot. They never inherit the caller's provider credentials. The daemon profile uses a dummy key. Only an external provider gateway may hold a real key.

SecretScanner rejects provided credential values and recognizable API keys, AWS credentials, JWTs and private-key headers before a result or recording is written. It also scans decoded SSE and reconstructed model deltas, since a key can span chunks. Report generation must scan every site file and record before publication. Errors report the affected output and match category, never a matched value. A secret scan is a second check, not a sandbox: recording runs model tools, so it needs an isolated execution boundary.

MacOS supports recording orchestration only; it does not report Linux PSS/USS. Model tools must not execute under the developer's host account: the recording launcher provides the container boundary, and CI's replay runs as a separate unprivileged UID (test/perf/report/README.md).

Isolated recording #

Run python3 test/perf/record_container.py --out /tmp/perf-recordings-<id> on valefar from a checkout other than the working one. The launcher copies the tracked source to a private temporary directory (mode 0700), builds the snapshot daemon and CLI inside nix develop, and records every scenario in one container. The local alpine:3.22 image must exist; the launcher pins it by image ID. --memory 8g, --cpus 4, --pids 4096 (the fan-out scenario's 20 sessions need it) and --timeout 3600 bound the run; --scenario is diagnostic only.

The container runs as UID 65534 with every capability dropped, no-new-privileges, a read-only root, Docker's default seccomp profile, tmpfs /home and /tmp, and --network none. Source, daemon, CLI and the Nix-store closure of the commands they run are read-only mounts; ps comes from the flake's locked nixpkgs. The recordings directory is the only writable mount. No host environment, Docker socket, credentials or home directory enters it. Docker's runsc (gVisor) runtime is used when present; valefar has only runc.

A host bridge forwards a Unix socket to bonsai at 127.0.0.1:8899 and nowhere else; inside, a second bridge serves that socket on 127.0.0.1:8899. Before recording, a real agent cell in the same container must fail to reach 1.1.1.1, 10.0.0.30 and the Docker host, fail to read an unmounted host file, and see no Docker socket; isolation.json keeps the result. /tmp/bonsai-gpu.lock is held only while the recording container runs, not during the build or the probe. Afterwards the container is removed, a second container with the same limits makes the output readable, SecretScanner checks every file, and only then are the files copied to --out. A failed run copies nothing. Commit the scenario recordings, not results.json or isolation.json.