From 53c85c99647ded9fab199c97b9383744e2524364 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 18:54:00 +0100 Subject: 0.8.0: comp.sh reports how to build for the machine it runs on Detects distro, arch, libc/glibc, CPU baseline and toolchain, prints the target triple, build command, cfg values, and the portable paths (static musl, older glibc image, cargo-zigbuild). --run builds and checks; --musl and --old-glibc select the other plans. Verified: native gnu build needs GLIBC_2.34; static musl build runs on Debian 10 and reaches the live API. --- CHANGELOG.md | 22 +++++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 28 +++--- comp.sh | 248 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/session.rs | 1 - tests/sessions.rs | 3 + 7 files changed, 293 insertions(+), 13 deletions(-) create mode 100755 comp.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index b71c654..b19c17b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,28 @@ break the interface, **PATCH** for bug fixes. file, and the tool schemas the model sees. Anything a script or a resumed session could depend on counts as API. +## 0.8.0 + +### Added +- `comp.sh`: detects distro, architecture, libc and glibc version, CPU baseline + and toolchain, then prints the exact build command for this machine, the + `cfg` values the code will compile against, and the portable alternatives: + static musl, an older-glibc container, and `cargo-zigbuild` with a pinned + glibc. `--run` executes the native build and its checks; `--musl` and + `--old-glibc` select the other plans. + +Verified on this machine: the native x86_64 build requires `GLIBC_2.34`, the +static musl build is 4.1 MiB, runs on Debian 10 (glibc 2.28), and reaches the +live API through musl's resolver. + +### Fixed +- The session-lock test was flaky under parallel test execution: a test that + spawns a child process briefly inherits the session's file description between + fork and exec, keeping the lock alive while another test dropped its handle. + The spawning test now serialises with the others. Test-only — the harness never + drops a session while a child is starting. +- Removed a stray `File::open` left behind in the session loader. + ## 0.7.1 ### Fixed diff --git a/Cargo.lock b/Cargo.lock index 9326d1c..ccec677 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" [[package]] name = "agent686" -version = "0.7.1" +version = "0.8.0" dependencies = [ "crossterm", "libc", diff --git a/Cargo.toml b/Cargo.toml index f96cf58..291c064 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "agent686" -version = "0.7.1" +version = "0.8.0" edition = "2021" description = "A small coding agent for a 32-bit Debian box" publish = false diff --git a/README.md b/README.md index f98e661..1b070e5 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,33 @@ # agent686 -A small coding agent for a 32-bit Debian box: one binary, six tools, line mode. +A small coding agent for Debian-class Linux: one binary, six tools, line mode. -Built because the existing harnesses are TypeScript bundles that need a modern -CPU, and the target here is a Debian 12 i386 machine with 2 cores and 4 GB. +Built because the existing harnesses are TypeScript bundles that either need a +modern CPU or a Node runtime. This one is pure Rust with no runtime dependency, +and it builds for whatever machine you are on. ``` -3.5 MiB ELF 32-bit LSB pie, Intel i386 -GLIBC_2.34 required (target has 2.36) -startup ~0.8 ms +4.0 MiB x86_64 glibc build, GLIBC_2.34 required +4.1 MiB x86_64 static musl build, no libc dependency +~1 ms startup ``` ## Build ```sh -cargo build --release --target i686-unknown-linux-gnu -./scripts/check-glibc.sh target/i686-unknown-linux-gnu/release/agent686 +./comp.sh # detect this machine, print the exact build plan +./comp.sh --run # build it, then run the glibc gate +./comp.sh --musl --run # static build: runs on Debian 10 and anything newer +./comp.sh --old-glibc # recipe for building against an older glibc ``` -The gate matters: the dev host's 32-bit glibc is newer than the target's, so a +`comp.sh` reports the distro, libc and glibc version, the CPU baseline, the +target triple, and the `cfg` values the code will see. A native glibc build +inherits this machine's glibc floor, which is why the portable paths are printed +next to it: static musl removes the floor entirely, and building inside an older +Debian image lowers it. + +The gate matters: the dev host's glibc is newer than the target's, so a dependency can silently raise the required symbol version. ## Run @@ -30,7 +39,6 @@ export OPENCODE_API_KEY=... # or write it to ~/.config/agent686/key ./agent686 -c # continue the most recent session ./agent686 --session # resume it, or create it if the id is new ./agent686 --list # id, model, message count, age -./agent686 -c # continue; prints where you left off ./agent686 --show [id] # render a transcript ./agent686 --doctor # key, dirs, provider, model ./agent686 --yolo "..." # approve every tool call diff --git a/comp.sh b/comp.sh new file mode 100755 index 0000000..6b798c6 --- /dev/null +++ b/comp.sh @@ -0,0 +1,248 @@ +#!/usr/bin/env bash +# +# comp.sh — work out how to build agent686 for *this* machine. +# +# Default is dry: it prints the plan. `--run` executes the native build and its +# checks. `--musl` selects the static plan; `--old-glibc` prints the container +# recipe for the oldest glibc we support. +# +# The point is to make the target explicit, because "it built" on a modern host +# says nothing about whether it runs on baseline Debian. +set -euo pipefail + +here=$(cd "$(dirname "$0")" && pwd) +run=0 +plan=native + +for arg in "$@"; do + case "$arg" in + --run) run=1 ;; + --musl) plan=musl ;; + --old-glibc) plan=old-glibc ;; + -h | --help) + sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//' + exit 0 + ;; + *) + echo "comp.sh: unknown argument: $arg" >&2 + exit 2 + ;; + esac +done + +say() { printf '%s\n' "$*"; } +section() { printf '\n\033[1m== %s\033[0m\n' "$*"; } +note() { printf ' %s\n' "$*"; } + +# ---------------------------------------------------------------- detection + +arch=$(uname -m) +kernel=$(uname -r) + +distro=unknown +if [ -r /etc/os-release ]; then + # shellcheck disable=SC1091 + . /etc/os-release + distro=${PRETTY_NAME:-${ID:-unknown}} +fi + +libc=unknown +libc_version= +if getconf GNU_LIBC_VERSION >/dev/null 2>&1; then + libc=glibc + libc_version=$(getconf GNU_LIBC_VERSION | awk '{print $2}') +elif command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | head -1 | grep -qi musl; then + libc=musl + libc_version=$(ldd --version 2>&1 | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1) +elif ls /lib/ld-musl-*.so.1 >/dev/null 2>&1; then + libc=musl +fi + +cpu_model=$(grep -m1 'model name' /proc/cpuinfo 2>/dev/null | cut -d: -f2- | sed 's/^ *//' || true) +cpu_flags=$(grep -m1 '^flags' /proc/cpuinfo 2>/dev/null | cut -d: -f2- || true) + +rustc_version=$(rustc -V 2>/dev/null || echo "not installed") +cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed") + +# ---------------------------------------------------------------- triple map + +triple_gnu=unknown-unknown-linux-gnu +triple_musl=unknown-unknown-linux-musl +baseline_flag= + +case "$arch" in + x86_64 | amd64) + triple_gnu=x86_64-unknown-linux-gnu + triple_musl=x86_64-unknown-linux-musl + # x86-64 v1: SSE2 only. Anything higher excludes pre-Nehalem CPUs. + baseline_flag="-C target-cpu=x86-64" + ;; + aarch64 | arm64) + triple_gnu=aarch64-unknown-linux-gnu + triple_musl=aarch64-unknown-linux-musl + ;; + armv7l | armv7) + triple_gnu=armv7-unknown-linux-gnueabihf + triple_musl=armv7-unknown-linux-musleabihf + ;; + armv6l) + triple_gnu=arm-unknown-linux-gnueabihf + triple_musl=arm-unknown-linux-musleabihf + ;; + i686 | i386) + triple_gnu=i686-unknown-linux-gnu + triple_musl=i686-unknown-linux-musl + ;; + riscv64) + triple_gnu=riscv64gc-unknown-linux-gnu + triple_musl=riscv64gc-unknown-linux-musl + ;; + ppc64le) + triple_gnu=powerpc64le-unknown-linux-gnu + triple_musl=powerpc64le-unknown-linux-musl + ;; + s390x) + triple_gnu=s390x-unknown-linux-gnu + triple_musl=s390x-unknown-linux-musl + ;; + *) + note "unrecognised architecture '$arch'; edit comp.sh's triple map" + ;; +esac + +native_triple=$triple_gnu +[ "$libc" = musl ] && native_triple=$triple_musl + +# The i686 gnu target uses linker flags from .cargo/config.toml; setting +# RUSTFLAGS at all would shadow them, so repeat them when needed. +defsym_flag= +if [ "$triple_gnu" = i686-unknown-linux-gnu ]; then + defsym_flag="-C link-arg=-Wl,--defsym,pidfd_spawnp=0 -C link-arg=-Wl,--defsym,pidfd_getpid=0" +fi + +# ---------------------------------------------------------------- report + +section "machine" +note "distro $distro" +note "arch $arch ($(getconf LONG_BIT 2>/dev/null || echo '?')-bit)" +note "kernel $kernel" +note "libc ${libc}${libc_version:+ $libc_version}" +note "cpu ${cpu_model:-unknown}" +note "rust $rustc_version" +note "cargo $cargo_version" +if [ -n "$cpu_flags" ]; then + for feature in sse2 sse4_2 avx avx2; do + case " $cpu_flags " in + *" $feature "*) note "has $feature" ;; + esac + done +fi + +section "target" +case "$plan" in + native) + say " target $native_triple" + ;; + musl) + say " target $triple_musl (static)" + ;; + old-glibc) + say " target $triple_gnu, built inside an older image" + ;; +esac + +if [ "$baseline_flag" = "-C target-cpu=x86-64" ]; then + note "baseline x86-64 (SSE2): runs on any 64-bit x86 CPU" +fi + +case "$plan" in + native) + if [ "$libc" = glibc ]; then + say "" + say " Building here links against this machine's glibc ($libc_version)." + say " The binary will refuse to start on any machine with an older one." + say " For a portable build, use --musl or --old-glibc." + fi + ;; +esac + +section "commands" + +case "$plan" in + native | musl) + triple=$native_triple + [ "$plan" = musl ] && triple=$triple_musl + extra_flags=$baseline_flag + [ "$triple" = i686-unknown-linux-gnu ] && extra_flags="$extra_flags $defsym_flag" + + say " rustup target add $triple # if not already installed" + if [ "$plan" = musl ] && [ "$triple" != i686-unknown-linux-musl ]; then + say " # needs a C toolchain for ring: apt install musl-tools" + fi + if [ -n "$extra_flags" ]; then + say " RUSTFLAGS=\"$extra_flags\" \\" + fi + say " cargo build --release --target $triple" + case "$triple" in + *-gnu) + say " ./scripts/check-glibc.sh target/$triple/release/agent686" + ;; + *) + say " file target/$triple/release/agent686 # expect: statically linked" + ;; + esac + ;; + old-glibc) + say " # The floor is the glibc in the image you build in:" + say " # debian:12 bookworm -> glibc 2.36" + say " # debian:11 bullseye -> glibc 2.31" + say " # debian:10 buster -> glibc 2.28" + say "" + say " docker run --rm -it -v \"\$PWD:/src\" -w /src rust:1-bullseye bash -lc '" + say " apt-get update &&" + say " apt-get install -y --no-install-recommends build-essential pkg-config &&" + say " RUSTFLAGS=\"$baseline_flag\" cargo build --release &&" + say " ./scripts/check-glibc.sh target/release/agent686'" + say "" + say " # Alternative without Docker, pinning glibc with zig:" + say " # cargo install cargo-zigbuild" + say " # cargo zigbuild --release --target $triple_gnu.2.28" + ;; +esac + +section "cfg the code will see" +if command -v rustc >/dev/null 2>&1; then + triple_for_cfg=$native_triple + [ "$plan" = musl ] && triple_for_cfg=$triple_musl + [ "$plan" = old-glibc ] && triple_for_cfg=$triple_gnu + rustc --print cfg --target "$triple_for_cfg" 2>/dev/null \ + | grep -E '^(target_(arch|env|family|os|pointer_width|vendor)|target_feature=)' \ + | sed 's/^/ /' | sort +else + note "rustc not found; no cfg to show" +fi + +if [ "$run" -eq 1 ]; then + if [ "$plan" = old-glibc ]; then + echo "comp.sh: --old-glibc is a recipe; run the docker command above" >&2 + exit 2 + fi + triple=$native_triple + [ "$plan" = musl ] && triple=$triple_musl + extra_flags=$baseline_flag + [ "$triple" = i686-unknown-linux-gnu ] && extra_flags="$extra_flags $defsym_flag" + + section "building" + rustup target add "$triple" >/dev/null 2>&1 || true + ( + cd "$here" + RUSTFLAGS="$extra_flags" cargo build --release --target "$triple" + ) + binary="$here/target/$triple/release/agent686" + section "result" + note "$(file -b "$binary")" + case "$triple" in + *-gnu) "$here/scripts/check-glibc.sh" "$binary" ;; + *) note "static: no glibc floor to check" ;; + esac +fi diff --git a/src/session.rs b/src/session.rs index 8e0ef02..a1da28f 100644 --- a/src/session.rs +++ b/src/session.rs @@ -131,7 +131,6 @@ impl Session { pub fn open(id: &str) -> Result<(Session, Value, Vec), String> { let path = sessions_dir().join(format!("{id}.jsonl")); let raw = std::fs::read(&path).map_err(|e| format!("{}: {e}", path.display()))?; - let _ = File::open(&path); let mut model = String::new(); let mut system = Value::Null; diff --git a/tests/sessions.rs b/tests/sessions.rs index df2f3b2..748b689 100644 --- a/tests/sessions.rs +++ b/tests/sessions.rs @@ -67,6 +67,9 @@ fn sessions_create_list_and_replay() { #[test] fn cli_lists_sessions_without_a_key() { + // Spawning holds a copy of the process's file descriptions between fork and + // exec, which would keep a session lock alive while another test drops it. + let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); let dir = temp_dir("cli"); let output = Command::new(env!("CARGO_BIN_EXE_agent686")) .env("AGENT686_DATA", &dir) -- cgit v1.2.3