From aedddc6068f280fd4f6ba47eace2e406a4dd2be4 Mon Sep 17 00:00:00 2001 From: dawn <90008@klbr.net> Date: Wed, 30 Sep 2026 22:38:46 +0300 Subject: [PATCH] e2e: boot test daemons from a per-run snapshot of the compiled daemon instead of gleam run Signed-off-by: dawn <90008@klbr.net> --- agents.md | 2 +- cli/test/e2e/harness_test.go | 19 ++++++++++++++++--- test/e2e/harness.py | 18 ++++++++++++++++++ test/e2e/run.py | 1 + test/snapshot-daemon.sh | 27 +++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100755 test/snapshot-daemon.sh diff --git a/agents.md b/agents.md index 9e1163d60..a169ed94f 100644 --- a/agents.md +++ b/agents.md @@ -21,7 +21,7 @@ Anything that opens a browser must set `ALBEDO_NO_BROWSER=1`; `test.sh` exports `./test.sh` is the gate, in a fixed order: format check, cargo test + renderer install, `gleam test`, the Python harness suites, Go vet + unit tests, the Go e2e suite, then the Python e2e suite. The order is not stylistic. -The daemon suites boot a real daemon with `gleam run`, and a nested gleam build waits on the build lock the outer `gleam test` holds, so they must never run inside `gleam test`. Anything that builds or runs gleam concurrently with `gleam test` deadlocks the same way. This is also why the e2e suites drive the prebuilt `cli/bin/albedo` binary instead of invoking gleam, and why the Go e2e suite runs with `-count=1` after a fresh build: it boots one hermetic daemon and must never reuse a cached run or a stale binary. +The e2e suites compile the daemon once per run with `test/snapshot-daemon.sh` and boot every test daemon from that copy through `ALBEDO_DAEMON`, never through `gleam run`, so their daemons neither queue on gleam's build lock nor see a rebuild mid-run. The one compile still takes the lock, which `gleam test` holds for its whole run: an e2e suite started from inside `gleam test` deadlocks, and one started beside it waits for it to finish. The Python e2e suite drives the prebuilt `cli/bin/albedo` binary; the Go e2e suite builds its own CLI and runs with `-count=1`, because it boots a hermetic daemon and must never reuse a cached run or a stale binary. Test placement (expanded rules in `.agents/skills/writing-tests/SKILL.md`): the default is an end-to-end scenario through the real daemon on the shared harness in `test/e2e/` — a scripted fake model provider, one shared daemon for the tests that isolate themselves by session, workspace and provider profile, and a fresh daemon for each `@exclusive` test that changes global state or restarts it. Unit tests are the exception; each module docstring must say which bug it catches that E2E cannot. `test/harness/api_docs_test.py` is the invariant test to imitate: it builds the model's real Python namespace and fails whenever a public binding exists that the model is never told about. `test/manual/` is opt-in (needs a provider, a PTY, or benchmark artifacts) and is never part of the gate. diff --git a/cli/test/e2e/harness_test.go b/cli/test/e2e/harness_test.go index 6b262a875..94963f904 100644 --- a/cli/test/e2e/harness_test.go +++ b/cli/test/e2e/harness_test.go @@ -101,8 +101,20 @@ func bootSuite() (func() (string, bool), error) { return teardown, fmt.Errorf("building ./cmd/albedo: %w\n%s", err, out) } + // The daemon boots from a snapshot of its compiled code rather than + // through `gleam run`; see test/snapshot-daemon.sh. + snapshot := exec.Command(filepath.Join(root, "test", "snapshot-daemon.sh"), filepath.Join(temp, "daemon")) + launcher, err := snapshot.Output() + if err != nil { + var exit *exec.ExitError + if errors.As(err, &exit) { + return teardown, fmt.Errorf("snapshotting the daemon: %w\n%s", err, exit.Stderr) + } + return teardown, fmt.Errorf("snapshotting the daemon: %w", err) + } + suite.root, suite.home = root, home - suite.env = hermeticEnv(root, home, userHome, scratch) + suite.env = hermeticEnv(root, home, userHome, scratch, strings.TrimSpace(string(launcher))) suite.provider = newFakeProvider() // A models.dev refresh or the cache-TTL table's remote copy would reach // the network from a test. @@ -226,8 +238,8 @@ func repoRoot() (string, error) { // hermeticEnv builds the environment for every CLI invocation and the daemon // it spawns: no real home, no inherited albedo state, temporary files inside -// the suite's tree, and the parent watcher. -func hermeticEnv(root, home, userHome, scratch string) []string { +// the suite's tree, the snapshot's launcher, and the parent watcher. +func hermeticEnv(root, home, userHome, scratch, daemonExe string) []string { var env []string for _, kv := range os.Environ() { name, _, _ := strings.Cut(kv, "=") @@ -242,6 +254,7 @@ func hermeticEnv(root, home, userHome, scratch string) []string { "ALBEDO_HOME="+home, "ALBEDO_PARENT_PID="+strconv.Itoa(os.Getpid()), "ALBEDO_ROOT="+root, + "ALBEDO_DAEMON="+daemonExe, "ALBEDO_NO_BROWSER=1", // Two schedulers keep a booting test VM from pinning every core. "ERL_FLAGS=+S 2:2 +SDcpu 2:2 +sbwt none +sbwtdcpu none +sbwtdio none", diff --git a/test/e2e/harness.py b/test/e2e/harness.py index bb8278767..83012785e 100644 --- a/test/e2e/harness.py +++ b/test/e2e/harness.py @@ -399,6 +399,8 @@ class Provider: _daemons: list[Daemon] = [] +# The launcher of this run's daemon build; see snapshot_daemon(). +_executable = None _daemons_lock = threading.RLock() _discarding: list[threading.Thread] = [] _current = threading.local() @@ -495,6 +497,8 @@ class Daemon: ALBEDO_MCP_CLOSED=str(self.root / "closed"), ALBEDO_MCP_AMBIENT="must-not-reach-the-server", ) + if _executable: + self.env["ALBEDO_DAEMON"] = _executable self.connection = None self.base = None self._pid = None @@ -666,6 +670,20 @@ def _alive(pid): return True +def snapshot_daemon(): + """Compile the daemon once for the run and boot every test daemon from a + copy, not through `gleam run` (see test/snapshot-daemon.sh).""" + global _executable + result = subprocess.run( + [str(ROOT / "test/snapshot-daemon.sh"), tempfile.mkdtemp(prefix="daemon-")], + text=True, + capture_output=True, + ) + if result.returncode: + raise RuntimeError("building the daemon failed:\n" + result.stderr) + _executable = result.stdout.strip() + + def current_daemon(): """The daemon the runner gave this thread.""" daemon = getattr(_current, "daemon", None) diff --git a/test/e2e/run.py b/test/e2e/run.py index 9db053f59..7e6d5b42c 100644 --- a/test/e2e/run.py +++ b/test/e2e/run.py @@ -151,6 +151,7 @@ def main(): fcntl.flock(lock, fcntl.LOCK_EX) started = time.monotonic() try: + harness.snapshot_daemon() successful = run_suite( suite, max(1, args.jobs), max(1, args.exclusive_jobs) ) diff --git a/test/snapshot-daemon.sh b/test/snapshot-daemon.sh new file mode 100755 index 000000000..03d7f6478 --- /dev/null +++ b/test/snapshot-daemon.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Compile the daemon and copy its code into $1 beside a launcher, then print the +# launcher's path for ALBEDO_DAEMON. Test daemons boot from the copy instead of +# `gleam run`: none of them takes the build lock, so they boot side by side and +# beside a running `gleam test`, and a rebuild during the run cannot change the +# code under a daemon that is still loading modules. +set -euo pipefail +root="$(cd "$(dirname "$0")/.." && pwd)" +out="$1" +(cd "$root" && gleam build) >&2 +mkdir -p "$out" +for ebin in "$root"/build/dev/erlang/*/ebin; do + package="$(dirname "$ebin")" + name="$(basename "$package")" + mkdir -p "$out/$name" + cp -R "$ebin" "$out/$name/ebin" + # priv keeps pointing at the source tree, as under `gleam run`. + if [ -e "$package/priv" ]; then + ln -s "$(cd "$package/priv" && pwd -P)" "$out/$name/priv" + fi +done +cat >"$out/albedo-daemon" <